Page 1 of 1

Unable to enable paging

Posted: Thu Jun 26, 2025 6:55 am
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

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 6:57 am
by torii
Forgot to upload a screenshot - here you go!
page fault.png
page fault.png (8.69 KiB) Viewed 3579 times

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 7:29 am
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.

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 7:47 am
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

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 9:31 am
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.

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 9:44 am
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.

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 11:45 am
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

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 2:58 pm
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.

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 9:46 pm
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.

Re: Unable to enable paging

Posted: Thu Jun 26, 2025 11:39 pm
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.

Re: Unable to enable paging

Posted: Sat Jun 28, 2025 11:24 am
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.