* mikeOS 16 bit and amd64 baremetal
[mascara-docs.git] / amd64 / pure64-0.5.0 / src / interrupt.asm
blob88b7127ef131a03f414369c6598ebaa94908b8d2
1 ; =============================================================================
2 ; Pure64 -- a 64-bit OS loader written in Assembly for x86-64 systems
3 ; Copyright (C) 2008-2012 Return Infinity -- see LICENSE.TXT
5 ; Interrupts
6 ; =============================================================================
9 ; -----------------------------------------------------------------------------
10 ; Default exception handler
11 exception_gate:
12 mov rsi, int_string
13 call os_print_string
14 mov rsi, exc_string
15 call os_print_string
16 exception_gate_halt:
17 cli ; Disable interrupts
18 hlt ; Halt the system
19 jmp exception_gate_halt
20 ; -----------------------------------------------------------------------------
23 ; -----------------------------------------------------------------------------
24 ; Default interrupt handler
25 interrupt_gate: ; handler for all other interrupts
26 iretq
27 ; -----------------------------------------------------------------------------
30 ; -----------------------------------------------------------------------------
31 ; Keyboard interrupt. IRQ 0x01, INT 0x21
32 ; This IRQ runs whenever there is input on the keyboard
33 align 16
34 keyboard:
35 push rdi
36 push rax
38 xor rax, rax
40 in al, 0x60 ; Get the scancode from the keyboard
41 test al, 0x80
42 jz keydown
43 jmp keyboard_done
45 keydown:
46 mov [0x000B8088], al ; Dump the scancode to the screen
48 mov rax, [os_Counter_RTC]
49 add rax, 10
50 mov [os_Counter_RTC], rax
52 keyboard_done:
53 mov rdi, [os_LocalAPICAddress] ; Acknowledge the IRQ on APIC
54 add rdi, 0xB0
55 xor eax, eax
56 stosd
58 pop rax
59 pop rdi
60 iretq
61 ; -----------------------------------------------------------------------------
64 ; -----------------------------------------------------------------------------
65 ; Real-time clock interrupt. IRQ 0x08, INT 0x28
66 align 16
67 rtc:
68 push rdi
69 push rax
71 add qword [os_Counter_RTC], 1 ; 64-bit counter started at bootup
73 mov al, 'R'
74 mov [0x000B8092], al
75 mov rax, [os_Counter_RTC]
76 and al, 1 ; Clear all but lowest bit (Can only be 0 or 1)
77 add al, 48
78 mov [0x000B8094], al
79 mov al, 0x0C ; Select RTC register C
80 out 0x70, al ; Port 0x70 is the RTC index, and 0x71 is the RTC data
81 in al, 0x71 ; Read the value in register C
83 mov rdi, [os_LocalAPICAddress] ; Acknowledge the IRQ on APIC
84 add rdi, 0xB0
85 xor eax, eax
86 stosd
88 pop rax
89 pop rdi
90 iretq
91 ; -----------------------------------------------------------------------------
94 ; -----------------------------------------------------------------------------
95 ; Spurious interrupt. INT 0xFF
96 align 16
97 spurious: ; handler for spurious interrupts
98 mov al, 'S'
99 mov [0x000B8080], al
100 iretq
101 ; -----------------------------------------------------------------------------
104 ; -----------------------------------------------------------------------------
105 ; CPU Exception Gates
106 exception_gate_00:
107 mov al, 0x00
108 jmp exception_gate_main
110 exception_gate_01:
111 mov al, 0x01
112 jmp exception_gate_main
114 exception_gate_02:
115 mov al, 0x02
116 jmp exception_gate_main
118 exception_gate_03:
119 mov al, 0x03
120 jmp exception_gate_main
122 exception_gate_04:
123 mov al, 0x04
124 jmp exception_gate_main
126 exception_gate_05:
127 mov al, 0x05
128 jmp exception_gate_main
130 exception_gate_06:
131 mov al, 0x06
132 jmp exception_gate_main
134 exception_gate_07:
135 mov al, 0x07
136 jmp exception_gate_main
138 exception_gate_08:
139 mov al, 0x08
140 jmp exception_gate_main
142 exception_gate_09:
143 mov al, 0x09
144 jmp exception_gate_main
146 exception_gate_10:
147 mov al, 0x0A
148 jmp exception_gate_main
150 exception_gate_11:
151 mov al, 0x0B
152 jmp exception_gate_main
154 exception_gate_12:
155 mov al, 0x0C
156 jmp exception_gate_main
158 exception_gate_13:
159 mov al, 0x0D
160 jmp exception_gate_main
162 exception_gate_14:
163 mov al, 0x0E
164 jmp exception_gate_main
166 exception_gate_15:
167 mov al, 0x0F
168 jmp exception_gate_main
170 exception_gate_16:
171 mov al, 0x10
172 jmp exception_gate_main
174 exception_gate_17:
175 mov al, 0x11
176 jmp exception_gate_main
178 exception_gate_18:
179 mov al, 0x12
180 jmp exception_gate_main
182 exception_gate_19:
183 mov al, 0x13
184 jmp exception_gate_main
186 exception_gate_main:
187 call os_print_newline
188 mov rsi, int_string
189 call os_print_string
190 mov rsi, exc_string00
191 and rax, 0xFF ; Clear out everything in RAX except for AL
192 mov bl, 8
193 mul bl ; AX = AL x BL
194 add rsi, rax ; Use the value in RAX as an offset to get to the right message
195 call os_print_string
196 mov rsi, adr_string
197 call os_print_string
198 mov rax, [rsp]
199 mov rdi, os_dump_reg_tstring
200 call os_int_to_hex_string ; Convert the register value to a hex string
201 mov rsi, os_dump_reg_tstring
202 call os_print_string ; Print the hex string
203 call os_print_newline
204 call os_dump_regs
206 exception_gate_main_hang:
208 jmp exception_gate_main_hang ; Hang. User must reset machine at this point
210 ; Strings for the error messages
211 int_string db 'Pure64 - Exception ', 0
212 adr_string db ' @ 0x', 0
213 exc_string db '?? - Unknown', 0
214 align 16
215 exc_string00 db '00 - DE', 0
216 exc_string01 db '01 - DB', 0
217 exc_string02 db '02 ', 0
218 exc_string03 db '03 - BP', 0
219 exc_string04 db '04 - OF', 0
220 exc_string05 db '05 - BR', 0
221 exc_string06 db '06 - UD', 0
222 exc_string07 db '07 - NM', 0
223 exc_string08 db '08 - DF', 0
224 exc_string09 db '09 ', 0 ; No longer generated on new CPU's
225 exc_string10 db '10 - TS', 0
226 exc_string11 db '11 - NP', 0
227 exc_string12 db '12 - SS', 0
228 exc_string13 db '13 - GP', 0
229 exc_string14 db '14 - PF', 0
230 exc_string15 db '15 ', 0
231 exc_string16 db '16 - MF', 0
232 exc_string17 db '17 - AC', 0
233 exc_string18 db '18 - MC', 0
234 exc_string19 db '19 - XM', 0
237 ; =============================================================================
238 ; EOF