exceptions.s (3100B)
1 /* Exception handlers for CPU faults */ 2 .section .text 3 4 /* Double Fault Handler (Exception 0x08) */ 5 .global double_fault_handler 6 .type double_fault_handler, @function 7 double_fault_handler: 8 /* Double fault pushes error code, but we'll just print and halt */ 9 pushal 10 pushl $msg_double_fault 11 call terminal_writestring 12 addl $4, %esp 13 popal 14 addl $4, %esp /* Pop error code */ 15 cli 16 1: hlt 17 jmp 1b 18 .size double_fault_handler, . - double_fault_handler 19 20 /* General Protection Fault Handler (Exception 0x0d) */ 21 .global gpf_handler 22 .type gpf_handler, @function 23 gpf_handler: 24 /* GPF pushes error code */ 25 pushal 26 pushl $msg_gpf 27 call terminal_writestring 28 addl $4, %esp 29 30 /* Print error code */ 31 movl 32(%esp), %eax /* Get error code from stack */ 32 pushl %eax 33 call print_hex 34 addl $4, %esp 35 36 pushl $msg_newline 37 call terminal_writestring 38 addl $4, %esp 39 40 popal 41 addl $4, %esp /* Pop error code */ 42 43 /* Try to continue (iret) - may cause triple fault if not recoverable */ 44 iret 45 .size gpf_handler, . - gpf_handler 46 47 /* Page Fault Handler (Exception 0x0e) */ 48 .global page_fault_handler 49 .type page_fault_handler, @function 50 page_fault_handler: 51 /* Page fault pushes error code */ 52 /* Don't print anything - just silently handle and continue */ 53 addl $4, %esp /* Pop error code */ 54 55 /* Try to continue */ 56 iret 57 .size page_fault_handler, . - page_fault_handler 58 59 /* Helper function to print hex value */ 60 print_hex: 61 pushl %ebp 62 movl %esp, %ebp 63 pushl %ebx 64 pushl %esi 65 66 movl 8(%ebp), %eax /* Get value to print */ 67 movl $hex_buffer+10, %esi 68 movb $0, (%esi) /* Null terminator */ 69 decl %esi 70 71 movl $8, %ecx 72 print_hex_loop: 73 movl %eax, %ebx 74 andl $0xF, %ebx 75 cmpb $10, %bl 76 jl print_hex_digit 77 addb $('A'-10), %bl 78 jmp print_hex_store 79 print_hex_digit: 80 addb $'0', %bl 81 print_hex_store: 82 movb %bl, (%esi) 83 decl %esi 84 shrl $4, %eax 85 loop print_hex_loop 86 87 incl %esi 88 pushl %esi 89 call terminal_writestring 90 addl $4, %esp 91 92 popl %esi 93 popl %ebx 94 popl %ebp 95 ret 96 97 /* IRQ12 Handler - PS/2 Mouse */ 98 .global irq12_handler 99 .type irq12_handler, @function 100 irq12_handler: 101 pushal 102 103 /* Call C mouse handler */ 104 call mouse_handler 105 106 /* Send EOI to slave PIC (IRQ12 is on slave) */ 107 movb $0x20, %al 108 outb %al, $0xA0 /* Slave PIC */ 109 outb %al, $0x20 /* Master PIC */ 110 111 popal 112 iret 113 .size irq12_handler, . - irq12_handler 114 115 # IRQ1 handler (keyboard) 116 .global irq1_handler 117 .type irq1_handler, @function 118 irq1_handler: 119 pushal 120 call keyboard_handler 121 movb $0x20, %al 122 outb %al, $0x20 # Send EOI to master PIC 123 popal 124 iret 125 .size irq1_handler, . - irq1_handler 126 127 .section .data 128 msg_double_fault: 129 .asciz "!!! DOUBLE FAULT !!!\n" 130 msg_gpf: 131 .asciz "!!! GENERAL PROTECTION FAULT !!! Error code: 0x" 132 msg_page_fault: 133 .asciz "!!! PAGE FAULT !!! Address: 0x" 134 msg_err_code: 135 .asciz " Error code: 0x" 136 msg_newline: 137 .asciz "\n" 138 pf_count: 139 .long 0 140 141 .section .bss 142 hex_buffer: 143 .skip 12