OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Apr 25, 2024 1:02 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Triple fault after changing CR3
PostPosted: Sat Oct 26, 2019 12:33 pm 
Offline
Member
Member

Joined: Sun Sep 16, 2018 6:46 am
Posts: 56
Hi all,
I've finally been able to get a recursive paging setup working. I want to change the page directory now, though.
This is my init function where I want to set a new pd:
Code:
void vmm_init() {
    uint32_t eflags = interrupt_save_disable();   

   uint32_t *pd_p __attribute__((aligned(4096))) = pAllocPage();
   uint32_t *pd_v = 0xAAAAAA000;
   vMapPage(pd_p, pd_v, BIT_PD_PT_RW | BIT_PD_PT_PRESENT);

    // recursive mapping
    uint32_t *self_pde_v = 0xAAEAA000;
   uint32_t *self_pde_p __attribute__((aligned(4096))) = (uint32_t)pAllocPage();
   vMapPage(self_pde_p, self_pde_v, BIT_PD_PT_RW | BIT_PD_PT_PRESENT);
    memset(self_pde_v, 0, sizeof(self_pde_v));
   
   self_pde_v = (uint32_t)pd_p | BIT_PD_PT_PRESENT | BIT_PD_PT_RW;
   pd_v[PAGE_DIRECTORY_INDEX(PD_VADDR)] = self_pde_v;
   
   // 4MB page for the kernel
   uint32_t *kernel_pde_v = 0xAB2AA000;
   uint32_t *kernel_pde_p __attribute__((aligned(4096))) = (uint32_t)pAllocPage();
   vMapPage(kernel_pde_p, kernel_pde_v, BIT_PD_PT_RW | BIT_PD_PT_PRESENT);
   memset(kernel_pde_v, 0, sizeof(kernel_pde_v));
   kernel_pde_v = _start_addr_phys | BIT_PD_PAGE_SIZE | BIT_PD_PT_PRESENT | BIT_PD_PT_RW;
   pd_v[PAGE_DIRECTORY_INDEX(KERNEL_VIRTUAL_BASE)] = kernel_pde_v;

   set_cr3(pd_p);
   interrupt_restore(eflags);
}

Here I first allocate and map the addresses to the current pd, then I manually map the new one.

set_cr3() is just:
Code:
set_cr3:
        mov eax, [esp + 4]
        and eax, 0xFFFFF000
        mov cr3, eax
        ret

Bochs says bx_dbg_read_linear: physical memory read error (phy=0x000000810010547c, lin=0x00000000c010547c) where the linear address (0xc010547c) is the 'ret' instruction.
Do I have to identity map kinda like I had to do when initially enable paging?
Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Triple fault after changing CR3
PostPosted: Sat Oct 26, 2019 12:48 pm 
Offline
Member
Member

Joined: Wed Aug 30, 2017 8:24 am
Posts: 1604
Thpertic wrote:
Do I have to identity map kinda like I had to do when initially enable paging?


No, but the address you are executing at (and where you have your stack and your data) must be the same in both address spaces. Most kernels achieve this by mapping the kernel the same way in all processes.

If you have enabled paging already,why don't you just write your changes to the existing paging structure?

_________________
Carpe diem!


Top
 Profile  
 
 Post subject: Re: Triple fault after changing CR3
PostPosted: Sat Oct 26, 2019 12:59 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
It looks to me, from that error message, that you have your linear and physical addresses the wrong way round.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], Google [Bot], rdos, SemrushBot [Bot] and 211 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group