3

I'm developing an operating system in C and I'm struggling on paging. I'm testing my kernel heap in a loop by allocating memory and outputting the low level memory allocation output that handles physical and virtual page allocation.

When PDE 0, everything works great for pages 0-1023 but as soon as the allocation moves to PDE 1, a page fault is raised with the present flag set, and sometimes the rw flag too if I start allocating from a different physical address.

Do I need to get the faulting address from cr2 and map it back to the PDE and page it belongs to and then set or the address with 3? After that I need to restart the instruction but how do I do that? Any suggestions?

Razor
  • 1,778
  • 4
  • 19
  • 36
Eino T
  • 131
  • 2
  • 10
  • I'm not sure I understand what you're asking in your first question. It sounds like you already know what to do with the address causing the fault? As for returning: page faults are the same as any other interrupt - EIP/RIP (in this case, pointing to *the instruction that caused the page fault*) is saved to the stack before your page fault handler is called. – pmdj Feb 24 '12 at 10:47
  • Thanks for your reply. The page fault was caused by a misfunctioning physical frame allocator that accidentaly allocated frame that was already allocated. Also kmallocing bytes that went over page border was a trouble. But all solved now. Thanks! – Eino T Feb 29 '12 at 18:10

1 Answers1

3

Page Fault is a fault exception, which means your page-fault handler is called directly by the processor as if an interrupt occurred.

After you have serviced the page-fault and would like to go back to the caller, you need to return from the fault via the IRET instruction. This will return the code-segment, eflags register and EIP (and potentially the user-mode SS and ESP if the fault was from ring 3) back to the instruction that triggered the fault.

SecurityMatt
  • 6,593
  • 1
  • 22
  • 28
  • Thanks. I finally got the insight that the restarting of instruction triggering #PF is handled manually by instruction like IRET. – firo May 13 '20 at 04:46