Page 1 of 1

Triple fault after changing CR3

Posted: Sat Oct 26, 2019 12:33 pm
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.

Re: Triple fault after changing CR3

Posted: Sat Oct 26, 2019 12:48 pm
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?

Re: Triple fault after changing CR3

Posted: Sat Oct 26, 2019 12:59 pm
by iansjack
It looks to me, from that error message, that you have your linear and physical addresses the wrong way round.