Triple fault after changing CR3

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Thpertic
Member
Member
Posts: 56
Joined: Sun Sep 16, 2018 6:46 am

Triple fault after changing CR3

Post by Thpertic »

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: Select all

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: Select all

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.
nullplan
Member
Member
Posts: 1796
Joined: Wed Aug 30, 2017 8:24 am

Re: Triple fault after changing CR3

Post by nullplan »

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!
User avatar
iansjack
Member
Member
Posts: 4704
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Triple fault after changing CR3

Post by iansjack »

It looks to me, from that error message, that you have your linear and physical addresses the wrong way round.
Post Reply