Skip to content
This repository was archived by the owner on Oct 11, 2021. It is now read-only.

Commit 553a433

Browse files
committed
πŸššπŸ’΅ CPU IRQ Support
1 parent b5cf9f9 commit 553a433

File tree

3 files changed

+230
-5
lines changed

3 files changed

+230
-5
lines changed

β€Žos/cpu/interrupt.asmβ€Ž

Lines changed: 137 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[extern isr_handler]
2+
[extern irq_handler]
23

34
isr_common_stub:
45
pusha ; pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
@@ -20,7 +21,28 @@ isr_common_stub:
2021
popa
2122
add esp, 8
2223
sti
23-
iret
24+
iret
25+
26+
irq_common_stub:
27+
28+
pusha
29+
mov ax, ds
30+
push eax
31+
mov ax, 0x10
32+
mov ds, ax
33+
mov es, ax
34+
mov fs, ax
35+
mov gs, ax
36+
call irq_handler
37+
pop ebx
38+
mov ds, bx
39+
mov es, bx
40+
mov fs, bx
41+
mov gs, bx
42+
popa
43+
add esp, 8
44+
sti
45+
iret
2446

2547
global isr0
2648
global isr1
@@ -55,6 +77,23 @@ global isr29
5577
global isr30
5678
global isr31
5779

80+
global irq0
81+
global irq1
82+
global irq2
83+
global irq3
84+
global irq4
85+
global irq5
86+
global irq6
87+
global irq7
88+
global irq8
89+
global irq9
90+
global irq10
91+
global irq11
92+
global irq12
93+
global irq13
94+
global irq14
95+
global irq15
96+
5897
; 0: Divide By Zero Exception
5998
isr0:
6099
cli
@@ -271,4 +310,100 @@ isr31:
271310
cli
272311
push byte 0
273312
push byte 31
274-
jmp isr_common_stub
313+
jmp isr_common_stub
314+
315+
irq0:
316+
cli
317+
push byte 0
318+
push byte 32
319+
jmp irq_common_stub
320+
321+
irq1:
322+
cli
323+
push byte 1
324+
push byte 33
325+
jmp irq_common_stub
326+
327+
irq2:
328+
cli
329+
push byte 2
330+
push byte 34
331+
jmp irq_common_stub
332+
333+
irq3:
334+
cli
335+
push byte 3
336+
push byte 35
337+
jmp irq_common_stub
338+
339+
irq4:
340+
cli
341+
push byte 4
342+
push byte 36
343+
jmp irq_common_stub
344+
345+
irq5:
346+
cli
347+
push byte 5
348+
push byte 37
349+
jmp irq_common_stub
350+
351+
irq6:
352+
cli
353+
push byte 6
354+
push byte 38
355+
jmp irq_common_stub
356+
357+
irq7:
358+
cli
359+
push byte 7
360+
push byte 39
361+
jmp irq_common_stub
362+
363+
irq8:
364+
cli
365+
push byte 8
366+
push byte 40
367+
jmp irq_common_stub
368+
369+
irq9:
370+
cli
371+
push byte 9
372+
push byte 41
373+
jmp irq_common_stub
374+
375+
irq10:
376+
cli
377+
push byte 10
378+
push byte 42
379+
jmp irq_common_stub
380+
381+
irq11:
382+
cli
383+
push byte 11
384+
push byte 43
385+
jmp irq_common_stub
386+
387+
irq12:
388+
cli
389+
push byte 12
390+
push byte 44
391+
jmp irq_common_stub
392+
393+
irq13:
394+
cli
395+
push byte 13
396+
push byte 45
397+
jmp irq_common_stub
398+
399+
irq14:
400+
cli
401+
push byte 14
402+
push byte 46
403+
jmp irq_common_stub
404+
405+
irq15:
406+
cli
407+
push byte 15
408+
push byte 47
409+
jmp irq_common_stub

β€Žos/cpu/isr.cβ€Ž

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,11 @@
88
#include "isr.h"
99
#include "idt.h"
1010
#include "../drivers/screen.h"
11+
#include "../drivers/ports.h"
1112
#include "../kernel/util.h"
1213

14+
isr_t interruptHandlers[256];
15+
1316
void isr_install()
1417
{
1518
set_idt_gate(0, (u32)isr0);
@@ -45,6 +48,36 @@ void isr_install()
4548
set_idt_gate(30, (u32)isr30);
4649
set_idt_gate(31, (u32)isr31);
4750

51+
// PIC
52+
port_byte_out(0x20, 0x11);
53+
port_byte_out(0xA0, 0x11);
54+
port_byte_out(0x21, 0x20);
55+
port_byte_out(0xA1, 0x28);
56+
port_byte_out(0x21, 0x04);
57+
port_byte_out(0xA1, 0x02);
58+
port_byte_out(0x21, 0x01);
59+
port_byte_out(0xA1, 0x01);
60+
port_byte_out(0x21, 0x0);
61+
port_byte_out(0xA1, 0x0);
62+
63+
// IRQ
64+
set_idt_gate(32, (u32)irq0);
65+
set_idt_gate(33, (u32)irq1);
66+
set_idt_gate(34, (u32)irq2);
67+
set_idt_gate(35, (u32)irq3);
68+
set_idt_gate(36, (u32)irq4);
69+
set_idt_gate(37, (u32)irq5);
70+
set_idt_gate(38, (u32)irq6);
71+
set_idt_gate(39, (u32)irq7);
72+
set_idt_gate(40, (u32)irq8);
73+
set_idt_gate(41, (u32)irq9);
74+
set_idt_gate(42, (u32)irq10);
75+
set_idt_gate(43, (u32)irq11);
76+
set_idt_gate(44, (u32)irq12);
77+
set_idt_gate(45, (u32)irq13);
78+
set_idt_gate(46, (u32)irq14);
79+
set_idt_gate(47, (u32)irq15);
80+
4881
set_idt(); // Load with assembly
4982
}
5083

@@ -84,9 +117,10 @@ char *exception_messages[] = {
84117
"Reserved",
85118
"Reserved",
86119
"Reserved",
87-
"Reserved"
88-
};
89-
void isr_handler(registers_t r) {
120+
"Reserved"};
121+
122+
void isr_handler(registers_t r)
123+
{
90124
kprint("received an interrupt: ");
91125
char s[3];
92126
asciiIntConverter(r.int_no, s);
@@ -95,3 +129,22 @@ void isr_handler(registers_t r) {
95129
kprint(exception_messages[r.int_no]);
96130
kprint("\n");
97131
}
132+
133+
void register_interrupt_handler(u8 n, isr_t handler)
134+
{
135+
136+
interruptHandlers[n] = handler;
137+
}
138+
139+
void irq_handler(registers_t r)
140+
{
141+
if (r.int_no >= 40)
142+
port_byte_out(0xA0, 0x20); // slave
143+
port_byte_out(0x20, 0x20); // master
144+
145+
if (interruptHandlers[r.int_no] != 0)
146+
{
147+
isr_t handler = interruptHandlers[r.int_no];
148+
handler(r);
149+
}
150+
}

β€Žos/cpu/isr.hβ€Ž

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,40 @@ extern void isr29();
4444
extern void isr30();
4545
extern void isr31();
4646

47+
extern void irq0();
48+
extern void irq1();
49+
extern void irq2();
50+
extern void irq3();
51+
extern void irq4();
52+
extern void irq5();
53+
extern void irq6();
54+
extern void irq7();
55+
extern void irq8();
56+
extern void irq9();
57+
extern void irq10();
58+
extern void irq11();
59+
extern void irq12();
60+
extern void irq13();
61+
extern void irq14();
62+
extern void irq15();
63+
64+
#define IRQ0 32
65+
#define IRQ1 33
66+
#define IRQ2 34
67+
#define IRQ3 35
68+
#define IRQ4 36
69+
#define IRQ5 37
70+
#define IRQ6 38
71+
#define IRQ7 39
72+
#define IRQ8 40
73+
#define IRQ9 41
74+
#define IRQ10 42
75+
#define IRQ11 43
76+
#define IRQ12 44
77+
#define IRQ13 45
78+
#define IRQ14 46
79+
#define IRQ15 47
80+
4781
typedef struct
4882
{
4983

@@ -57,4 +91,7 @@ typedef struct
5791
void isr_install();
5892
void isr_handler(registers_t r);
5993

94+
typedef void (*isr_t)(registers_t r);
95+
void register_interrupt_handler(u8 n, isr_t handler);
96+
6097
#endif

0 commit comments

Comments
Β (0)