Unable to enable paging

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
torii
Posts: 14
Joined: Sun Feb 02, 2025 5:59 pm
GitHub: https://github.com/Toriiiiiiiiii
Contact:

Unable to enable paging

Post by torii »

I am trying to set up paging for my kernel, however it is just triggering a page fault. I have followed the wiki as close as I can, but the issue is still occurring. Any help would be greatly appreciated :D

https://github.com/Toriiiiiiiiii/solkern
Writing bad code since 2019
Image Image
torii
Posts: 14
Joined: Sun Feb 02, 2025 5:59 pm
GitHub: https://github.com/Toriiiiiiiiii
Contact:

Re: Unable to enable paging

Post by torii »

Forgot to upload a screenshot - here you go!
page fault.png
page fault.png (8.69 KiB) Viewed 3576 times
Writing bad code since 2019
Image Image
User avatar
iansjack
Member
Member
Posts: 4811
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Unable to enable paging

Post by iansjack »

What does "men info" in the Qemu monitor tell you about your page tables?

You should also check in your debugger the address of, for example, page_tables. I'm not convinced that your code page aligns it.
CeriseSky
Posts: 3
Joined: Sun Jun 15, 2025 10:54 am
Libera.chat IRC: Indigo

Re: Unable to enable paging

Post by CeriseSky »

This can happen with interrupts enabled because by default the timer chip uses the same interrupt as page faults. Set up IRQs and you should be alright
If I see one more magic number in wiki code then I'm going to lose it.
nullplan
Member
Member
Posts: 1908
Joined: Wed Aug 30, 2017 8:24 am

Re: Unable to enable paging

Post by nullplan »

CeriseSky wrote: Thu Jun 26, 2025 7:47 am This can happen with interrupts enabled because by default the timer chip uses the same interrupt as page faults. Set up IRQs and you should be alright
That is false. The timer interrupt in BIOS-compatible configuration maps to the double fault. However, it is apparent from the OP that they need to fix their debug outputs first and foremost. If this was the timer interrupt, it would be happening at ca. 18Hz, which should leave more than enough time to print a couple of hex numbers to screen.

No, I suspect this is an access error, and then an access error happening while attempting to print the message. Which is normal for someone attempting paging the first time. I suspect something wrong with the page tables.
Carpe diem!
User avatar
iansjack
Member
Member
Posts: 4811
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Unable to enable paging

Post by iansjack »

Unless you can guarantee that malloc(4096) will return a page-aligned address it doesn’t seem to be a viable way to allocate memory for page tables.
CeriseSky
Posts: 3
Joined: Sun Jun 15, 2025 10:54 am
Libera.chat IRC: Indigo

Re: Unable to enable paging

Post by CeriseSky »

nullplan wrote: Thu Jun 26, 2025 9:31 am That is false. The timer interrupt in BIOS-compatible configuration maps to the double fault.
Right that's my bad. I remembered the timer causing some kind of issue if it wasn't remapped but either way OP has done that. You're probably right about the access error, it might be useful for OP to run it through bochs and keep track of what the setup function is actually putting in memory. It's crude but it might expose the problem if its obvious enough
If I see one more magic number in wiki code then I'm going to lose it.
Octocontrabass
Member
Member
Posts: 5873
Joined: Mon Mar 25, 2013 7:01 pm

Re: Unable to enable paging

Post by Octocontrabass »

torii wrote: Thu Jun 26, 2025 6:55 amI am trying to set up paging for my kernel, however it is just triggering a page fault.
Do you have any more information about that page fault? For example, where in your code it occurs (EIP) and what address is being accessed that causes the fault (CR2).

Since your exception handler doesn't work, try running QEMU with "-d int" so you can get that information out of QEMU's logs.
MichaelPetch
Member
Member
Posts: 832
Joined: Fri Aug 26, 2016 1:41 pm
Libera.chat IRC: mpetch

Re: Unable to enable paging

Post by MichaelPetch »

I think you should consider iansjack question about malloc returning page aligned memory. I don't even get as far as you do since the paging fails immediately after the paging bit is set in CR0. I see this when debugging:

Code: Select all

Breakpoint 2 at 0x2004c0: file mmu.c, line 19.
(gdb) c
Continuing.

Breakpoint 2, setupPageDirectory () at mmu.c:19
19          page_tables = (page_table_t*)malloc(page_table_list_cap * sizeof(page_table_t));
(gdb) n
32          for(int i = 0; i < 1024; ++i) {
(gdb) p page_tables
$1 = (page_table_t *) 0x20b5a0 <heap+32>
(gdb)
`page_tables` is clearly not 4096 byte aligned. Is the code in your Github the version you are running? If not please commit the latest code you are using.
User avatar
iansjack
Member
Member
Posts: 4811
Joined: Sat Mar 31, 2012 3:07 am
Location: Chichester, UK

Re: Unable to enable paging

Post by iansjack »

That figures. Although the struct has the “aligned” attribute that doesn’t affect the alignment of a dynamically added object.

Just use your physical page allocator to assign pages for tables rather than allocating them from the heap.
torii
Posts: 14
Joined: Sun Feb 02, 2025 5:59 pm
GitHub: https://github.com/Toriiiiiiiiii
Contact:

Re: Unable to enable paging

Post by torii »

Okay, heres an update: I managed to fix the issue by rewriting my page allocator. There must have been *something* wrong with it somewhere, as it now appears to be working fine.
Writing bad code since 2019
Image Image
Post Reply