page_fault.S 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* https://github.com/cirosantilli/x86-bare-metal-examples#page-fault */
  2. #include "common.h"
  3. BEGIN
  4. CLEAR
  5. STAGE2
  6. PROTECTED_MODE
  7. IDT_SETUP_ENTRY $14, $interrupt_handler
  8. lidt idt_descriptor
  9. SETUP_PAGING_4M
  10. /* Make page 0 not present, so that any access to it will segfault. */
  11. andb $0xFE, page_table
  12. PAGING_ON
  13. /* Access page 0, generating a segfault. */
  14. movb $0, 0
  15. PAGING_OFF
  16. jmp .
  17. IDT_START
  18. IDT_SKIP 14
  19. IDT_ENTRY
  20. IDT_END
  21. interrupt_handler:
  22. VGA_PRINT_STRING $message
  23. /* Mandatory because page faults push the error code to the stack.
  24. *
  25. * If we don't do this, then the stack will be wrong for iret,
  26. * likely leading to a general fault exception:
  27. * http://stackoverflow.com/questions/10581224/why-does-iret-from-a-page-fault-handler-generate-interrupt-13-general-protectio/33398064#33398064
  28. */
  29. pop %eax
  30. VGA_PRINT_HEX_4 <%eax>
  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:"