[solved] #PF: altering page tables after loading PML4 to 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.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

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

Post 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
Last edited by austanss on Tue Feb 02, 2021 10:14 am, edited 1 time in total.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

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

Post 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.)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

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

Post 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.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

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

Post 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.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
AndrewAPrice
Member
Member
Posts: 2300
Joined: Mon Jun 05, 2006 11:00 pm
Location: USA (and Australia)

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

Post 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.
My OS is Perception.
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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.
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

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

Post 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.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
iansjack
Member
Member
Posts: 4703
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

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

Post 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?
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

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

Post 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!
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

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

Post 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.
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

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

Post 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.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
neon
Member
Member
Posts: 1567
Joined: Sun Feb 18, 2007 7:28 pm
Contact:

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

Post by neon »

Hi,

Why do you clear pml_4 right after the call to map_memory? (line 22 kconfigf)
OS Development Series | Wiki | os | ncc
char c[2]={"\x90\xC3"};int main(){void(*f)()=(void(__cdecl*)(void))(void*)&c;f();}
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

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

Post by austanss »

neon wrote:Hi,

Why do you clear pml_4 right after the call to map_memory? (line 22 kconfigf)
...
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
User avatar
austanss
Member
Member
Posts: 377
Joined: Sun Oct 11, 2020 9:46 pm
Location: United States

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

Post 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.
Skylight: https://github.com/austanss/skylight

I make stupid mistakes and my vision is terrible. Not a good combination.

NOTE: Never respond to my posts with "it's too hard".
Post Reply