Page 1 of 2

[solved] #PF: altering page tables after loading PML4 to cr3

Posted: Tue Feb 02, 2021 7:24 am
by austanss
When I load PML4 to cr3, afterwards the page tables can't be altered or else a page fault is thrown.

I was stepping through the function (with a debugger) to set up paging. Everything seemed okay, I loaded the PML4 pointer into cr3, but as soon as I do, the debugger can't access PML4 anymore. Neither can the kernel. Attempting to access PML4 results in a page fault.

Source code: https://github.com/microNET-OS/microCOR ... memory.cxx

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 8:22 am
by neon
Hi,

All paging structures use physical frame numbers not virtual. Where in your code are you mapping the paging structures themselves into the address space? (With this said, this is where recursive paging can be helpful.)

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 8:23 am
by austanss
neon wrote:Hi,

All paging structures use physical frame numbers not virtual. Where in your code are you mapping the paging structures themselves into the address space? (With this said, this is where recursive paging can be helpful.)
I map PML4's page directly after requesting a page for it.

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 8:29 am
by iansjack
If you have mapped PML4's page, and all the pages in the table it refers to, then you shouldn't get a page fault. The next step is to determine at which memory address the page fault is occurring and the exact nature of the fault. CR2 and the error code for the page fault give you this information.

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 8:34 am
by austanss
iansjack wrote:all the pages in the table it refers to
I haven't done that...

I checked, the page fault error code is 0000 and CR2 is PML4's address.

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 8:38 am
by AndrewAPrice
Make sure the paging strutures you're trying to write to are mapped into virtual memory, and you're writing to them via their virtual address. Make sure that the entries in the paging structure are their physical addresses. Make sure you are loading the physical address of the PML4 into cr3.

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 8:56 am
by iansjack
So you tried to read a non-present page. Probably, as mentioned above, you are trying to read the physical page directly rather than using the mapping.

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 9:04 am
by austanss
I mapped the pages. Also, I don't know how I could access the physical address. I mapped its virtual address to its physical address.

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 9:25 am
by iansjack
Without seeing your code repository all that can be said is that you are doing something wrong. Are you sure that you understand the difference between physical and virtual addresses and how you access such memory from C?

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 9:27 am
by austanss
iansjack wrote:Without seeing your code repository all that can be said is that you are doing something wrong. Are you sure that you understand the difference between physical and virtual addresses and how you access such memory from C?
What do you mean you can't see my code repository? I linked it!

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 9:35 am
by neon
Hi,
I mapped the pages.
It is possible that I just missed it when I quickly scanned it earlier -- but just in case -- where at, specifically, are you mapping them? Source & line number please.

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 9:37 am
by austanss
neon wrote:Hi,
I mapped the pages.
It is possible that I just missed it when I quickly scanned it earlier -- but just in case -- where at, specifically, are you mapping them? Source & line number please.
src/kconfigf.cxx:20

Sorry, did it in a different file.

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 10:08 am
by neon
Hi,

Why do you clear pml_4 right after the call to map_memory? (line 22 kconfigf)

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 10:11 am
by austanss
neon wrote:Hi,

Why do you clear pml_4 right after the call to map_memory? (line 22 kconfigf)
...

Re: page fault: altering page tables after loading PML4 to c

Posted: Tue Feb 02, 2021 10:13 am
by austanss
rizxt wrote:
neon wrote:Hi,

Why do you clear pml_4 right after the call to map_memory? (line 22 kconfigf)
...
That fixes that issue.