1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
|
;; /*
;; * ---------------------------------------------------
;; *
;; * Copyright (C) 2024-2025, Amlal El Mahrouss, licensed under the Apache 2.0 license.
;; *
;; * File: HalInterruptAPI.asm
;; * Purpose: Interrupt API, redirect raw interrupts into their handlers.
;; *
;; * ---------------------------------------------------
;; */
[bits 64]
%define kInterruptId 50
%macro IntExp 1
global __NE_INT_%1
__NE_INT_%1:
cli
std
o64 iret
%endmacro
%macro IntNormal 1
global __NE_INT_%1
__NE_INT_%1:
cli
std
add rsp, 8
o64 iret
%endmacro
; This file handles the core interrupt table
; Last edited 31/01/24
global ke_handle_irq
global kInterruptVectorTable
extern idt_handle_gpf
extern idt_handle_pf
extern ke_io_write
extern idt_handle_ud
extern idt_handle_generic
extern idt_handle_breakpoint
extern idt_handle_math
section .text
__NE_INT_0:
cli
push rcx
call idt_handle_generic
pop rcx
std
o64 iret
__NE_INT_1:
cli
push rcx
call idt_handle_generic
pop rcx
std
o64 iret
__NE_INT_2:
cli
push rcx
call idt_handle_generic
pop rcx
std
o64 iret
;; @brief Triggers a breakpoint and freeze the process. RIP is also fetched.
__NE_INT_3:
cli
push rcx
call idt_handle_breakpoint
pop rcx
std
o64 iret
__NE_INT_4:
cli
push rcx
call idt_handle_generic
pop rcx
std
o64 iret
__NE_INT_5:
cli
std
o64 iret
;; Invalid opcode interrupt
__NE_INT_6:
cli
push rcx
call idt_handle_ud
pop rcx
std
o64 iret
__NE_INT_7:
cli
push rcx
call idt_handle_generic
pop rcx
std
o64 iret
;; Invalid opcode interrupt
__NE_INT_8:
cli
push rcx
call idt_handle_math
pop rcx
std
o64 iret
IntNormal 9
IntExp 10
IntExp 11
IntExp 12
__NE_INT_13:
cli
push rcx
call idt_handle_gpf
pop rcx
std
add rsp, 8
o64 iret
__NE_INT_14:
cli
push rcx
call idt_handle_pf
pop rcx
std
o64 iret
IntNormal 15
IntNormal 16
IntExp 17
IntNormal 18
IntNormal 19
IntNormal 20
IntNormal 21
IntNormal 22
IntNormal 23
IntNormal 24
IntNormal 25
IntNormal 26
IntNormal 27
IntNormal 28
IntNormal 29
IntExp 30
IntNormal 31
[extern idt_handle_scheduler]
[extern kApicBaseAddress]
[global __NE_INT_33]
__NE_INT_32:
cli
push rax
mov rcx, rsp
call idt_handle_scheduler
pop rax
std
o64 iret
__NE_INT_33:
cli
push rax
; Read scancode from keyboard port to acknowledge
in al, 0x60
; Send EOI to master PIC
mov al, 0x20
out 0x20, al
pop rax
std
o64 iret
IntNormal 34
IntNormal 35
IntNormal 36
IntNormal 37
IntNormal 38
IntNormal 39
[extern rtl_rtl8139_interrupt_handler]
__NE_INT_40:
cli
push rax
mov rcx, rsp
call rtl_rtl8139_interrupt_handler
pop rax
std
o64 iret
IntNormal 41
IntNormal 42
IntNormal 43
IntNormal 44
IntNormal 45
IntNormal 46
IntNormal 47
IntNormal 48
IntNormal 49
[extern hal_system_call_enter]
[extern hal_kernel_call_enter]
__NE_INT_50:
cli
push rax
mov rax, hal_system_call_enter
mov rcx, r8
mov rdx, r9
mov r8, r10
mov r9, r11
call rax
pop rax
std
o64 iret
__NE_INT_51:
cli
push rax
mov rax, hal_kernel_call_enter
mov rcx, r8
mov rdx, r9
mov r8, r10
mov r9, r11
call rax
pop rax
std
o64 iret
IntNormal 52
IntNormal 53
IntNormal 54
IntNormal 55
IntNormal 56
IntNormal 57
IntNormal 58
IntNormal 59
IntNormal 60
%assign i 61
%rep 195
IntNormal i
%assign i i+1
%endrep
section .text
[global hal_load_gdt]
hal_load_gdt:
cli
lgdt [rcx]
mov ax, 0x10
mov ds, ax
mov es, ax
mov fs, ax
mov gs, ax
mov ss, ax
mov rax, 0x08
push rax
push hal_reload_segments
o64 retf
extern hal_real_init
hal_reload_segments:
std
jmp hal_real_init
ret
global hal_load_idt
hal_load_idt:
lidt [rcx]
; Master PIC initialization
mov al, 0x11 ; Start initialization in cascade mode
out 0x20, al ; Send initialization command to Master PIC
out 0xA0, al ; Send initialization command to Slave PIC
; Remap the PIC to use vectors 32-39 for Master and 40-47 for Slave
mov al, 0x20 ; Set Master PIC offset to 32
out 0x21, al ; Send offset to Master PIC
mov al, 0x28 ; Set Slave PIC offset to 40
out 0xA1, al ; Send offset to Slave PIC
; Configure Master PIC to inform Slave PIC at IRQ2
mov al, 0x04 ; Tell Master PIC there is a Slave PIC at IRQ2
out 0x21, al
; Configure Slave PIC identity
mov al, 0x02 ; Tell Slave PIC its cascade identity
out 0xA1, al
; Set both PICs to 8086 mode
mov al, 0x01 ; 8086 mode
out 0x21, al
out 0xA1, al
ret
section .data
kInterruptVectorTable:
%assign i 0
%rep 256
dq __NE_INT_%+i
%assign i i+1
%endrep
kApicBaseAddress:
dq 0
|