page_fault.S 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "common.h"
  2. BEGIN
  3. CLEAR
  4. STAGE2
  5. PROTECTED_MODE
  6. IDT_SETUP_ENTRY $14, $interrupt_handler
  7. lidt idt_descriptor
  8. SETUP_PAGING_4M
  9. /* Make page 0 not present, so that any access to it will segfault. */
  10. andb $0xFE, page_table
  11. PAGING_ON
  12. /* Access page 0, generating a segfault. */
  13. movb $0, 0
  14. PAGING_OFF
  15. jmp .
  16. IDT_START
  17. IDT_SKIP 14
  18. IDT_ENTRY
  19. IDT_END
  20. interrupt_handler:
  21. VGA_PRINT_STRING $message
  22. /*
  23. Mandatory because page faults push the error code to the stack.
  24. If we don't do this, then the stack will be wrong for iret,
  25. likely leading to a general fault exception:
  26. http://stackoverflow.com/questions/10581224/why-does-iret-from-a-page-fault-handler-generate-interrupt-13-general-protectio/33398064#33398064
  27. */
  28. pop %eax
  29. VGA_PRINT_HEX_4 <%eax>
  30. /*
  31. Make the page present. because iret will return to before the mov,
  32. and we'd get and infinite loop.
  33. */
  34. orb $1, page_table
  35. iret
  36. message:
  37. .asciz "Page fault handled. Error code:"