Switching from 4MB to 4KB paging
Posted: Fri Aug 16, 2019 7:12 pm
I have 4MB paging enabled from the bootloader to load my higher-half kernel at 0xC0000000.
I created two Page Tables where I identity page the first 4MB and the higher-half kernel.
Then I created a Page Directory to hold them and set as current.
Now I have to switch the type of paging from 4MB to 4KB and set the new Page Directory.
This is my entire .asm code:
CR3 get loaded, but then Bochs throws this error: bx_dbg_read_linear: physical memory read error (phy=0x00000000c000527a, lin=0x00000000c010527a).
The code is executed at that address, so what should I do?
I tried to only switch cr4 and it worked, but the new PD is not loaded so it's useless.
Can anyone help me? Thanks.
I created two Page Tables where I identity page the first 4MB and the higher-half kernel.
Code: Select all
uint32_t *first4MBpt = kmalloc();
uint32_t virt = 0;
for (i = 0; i < 1024; i++, virt += PAGE_SIZE)
first4MBpt[i] = virt | 0x000000003;
uint32_t *kernelPT = kmalloc();
// Mapping kernel space - should be from the 768th pde and the 256th pte (for KERNEL_VIRTUAL_BASE == 0xC00000000)
for (i = 0x100000, virt = KERNEL_VIRTUAL_BASE; virt <= (_half_maxStack + KERNEL_VIRTUAL_BASE); i += PAGE_SIZE, virt += PAGE_SIZE)
kernelPT[PAGE_TABLE_INDEX(i)] = virt | 0x000000003;
Code: Select all
pageDirectory[0] = (uint32_t)(first4MBpt) | 0x000000003;
pageDirectory[PAGE_DIRECTORY_INDEX(KERNEL_VIRTUAL_BASE)] = (uint32_t)(kernelPT) | 0x000000003;
_cur_pageDirectory = pageDirectory;
This is my entire .asm code:
Code: Select all
section .text
; Needs a parameter: the physical address of the Page Directory
extern _cur_pageDirectory
global switch_to4kb
switch_to4kb:
pusha
; Load PDBR (CR3), it must contains the physical address of the Page Directory
mov ecx, [_cur_pageDirectory]
mov cr3, ecx
; Reset PSE bit in CR4 to enable 4KB pages
mov ecx, cr4
and ecx, 0xffffffef
mov cr4, ecx
popa
ret
The code is executed at that address, so what should I do?
I tried to only switch cr4 and it worked, but the new PD is not loaded so it's useless.
Can anyone help me? Thanks.