Xyris  0.5
interrupt.s
Go to the documentation of this file.
1 ; Defined in isr.c
2 extern exceptionHandler
3 extern interruptHandler
4 align 4
5 
6 ; Common ISR code
7 exception_stub:
8  ; 1. Save CPU state
9  pushad ; Pushes edi,esi,ebp,esp,ebx,edx,ecx,eax
10  mov ax, ds ; Lower 16-bits of eax = ds.
11  push eax ; save the data segment descriptor
12  mov ax, 0x10 ; kernel data segment descriptor
13  mov ds, ax
14  mov es, ax
15  mov fs, ax
16  mov gs, ax
17  push esp ; Push struct registers *r
18  ; 2. Clear the direction flag (eflags) & call C handler
19  cld ; C code following the sysV ABI requires DF to be clear on function entry
20  call exceptionHandler
21 
22  ; 3. Restore state
23  add esp, 4
24  pop eax
25  mov ds, ax
26  mov es, ax
27  mov fs, ax
28  mov gs, ax
29  popad
30  add esp, 8 ; Cleans up the pushed error code and pushed ISR number
31  iret ; pops 5 things at once: CS, EIP, EFLAGS, SS, and ESP
32  ; These irets need to be iretq's when in long mode
33 
34 ; Common IRQ code. Identical to ISR code except for the 'call'
35 ; and the 'pop ebx'
36 interrupt_stub:
37  pushad
38  mov ax, ds
39  push eax
40  mov ax, 0x10
41  mov ds, ax
42  mov es, ax
43  mov fs, ax
44  mov gs, ax
45  push esp
46  cld
47  call interruptHandler ; Different than the ISR code
48  add esp, 4
49  pop ebx ; Different than the ISR code
50  mov ds, bx
51  mov es, bx
52  mov fs, bx
53  mov gs, bx
54  popad
55  add esp, 8
56  iret
57  ; These irets need to be iretq's when in long mode
58 
59 ; Macro expansions for isr and irq
60 
61 %macro m_exception_err 1
62 
63 global exception%1
64 exception%1:
65  push 0
66  push %1
67  jmp exception_stub
68 
69 %endmacro
70 
71 %macro m_exception_no_err 1
72 
73 global exception%1
74 exception%1:
75  push %1
76  jmp exception_stub
77 
78 %endmacro
79 
80 %macro m_interrupt 2
81 
82 global interrupt%1
83 interrupt%1:
84  push %1
85  push %2
86  jmp interrupt_stub
87 
88 %endmacro
89 
90 ; We don't get information about which interrupt was called when the handler
91 ; is run, so we will need to have a different handler for every interrupt.
92 ; Furthermore, some interrupts push an error code onto the stack but others
93 ; don't, so we will push a dummy error code for those which don't, so that
94 ; we have a consistent stack for all of them.
95 
96 ; All of the following Interrupt Service Routines are exception handlers.
97 ; As such, they push the error code along with their exception (interrupt)
98 ; number. If an exception has an error code, $0 will not be pushed as the CPU
99 ; already does that for us.
100 ;
101 m_exception_err 0 ; 0: Divide By Zero Exception
102 m_exception_err 1 ; 1: Debug Exception
103 m_exception_err 2 ; 2: Non Maskable Interrupt Exception
104 m_exception_err 3 ; 3: Int 3 Exception
105 m_exception_err 4 ; 4: INTO Exception
106 m_exception_err 5 ; 5: Out of Bounds Exception
107 m_exception_err 6 ; 6: Invalid Opcode Exception
108 m_exception_err 7 ; 7: Coprocessor Not Available Exception
109 m_exception_no_err 8 ; 8: Double Fault Exception (With Error Code!)
110 m_exception_err 9 ; 9: Coprocessor Segment Overrun Exception
111 m_exception_no_err 10 ; 10: Bad TSS Exception (With Error Code!)
112 m_exception_no_err 11 ; 11: Segment Not Present Exception (With Error Code!)
113 m_exception_no_err 12 ; 12: Stack Fault Exception (With Error Code!)
114 m_exception_no_err 13 ; 13: General Protection Fault Exception (With Error Code!)
115 m_exception_no_err 14 ; 14: Page Fault Exception (With Error Code!)
116 m_exception_err 15 ; 15: Reserved Exception
117 m_exception_err 16 ; 16: Floating Point Exception
118 m_exception_no_err 17 ; 17: Alignment Check Exception (With Error Code!)
119 m_exception_err 18 ; 18: Machine Check Exception
120 m_exception_err 19 ; 19: Reserved
121 m_exception_err 20 ; 20: Reserved
122 m_exception_err 21 ; 21: Reserved
123 m_exception_err 22 ; 22: Reserved
124 m_exception_err 23 ; 23: Reserved
125 m_exception_err 24 ; 24: Reserved
126 m_exception_err 25 ; 25: Reserved
127 m_exception_err 26 ; 26: Reserved
128 m_exception_err 27 ; 27: Reserved
129 m_exception_err 28 ; 28: Reserved
130 m_exception_err 29 ; 29: Reserved
131 m_exception_no_err 30 ; 30: Reserved (With Error Code!)
132 m_exception_err 31 ; 31: Reserved
133 
134 ; All of the following Interrupt Requests are stubs for hardware interrupts such
135 ; as device interrupts. These are not exceptions. Each Interrupt Request pushes
136 ; the IRQ value (starting at 0) along with their corresponding hardware interrupt
137 ; value (starting at 32).
138 ;
139 ; Interrupt Request handlers
140 m_interrupt 0, 32 ; System timer (PIT)
141 m_interrupt 1, 33 ; PS2 Keyboard
142 m_interrupt 2, 34 ; PIC2
143 m_interrupt 3, 35 ; COM2
144 m_interrupt 4, 36 ; COM1
145 m_interrupt 5, 37 ; LPT2
146 m_interrupt 6, 38 ; Floppy Disk Drive
147 m_interrupt 7, 39 ; LPT1
148 m_interrupt 8, 40 ; CMOS Real Time Clock
149 m_interrupt 9, 41 ; Unused / Generic
150 m_interrupt 10, 42 ; Unused / Generic
151 m_interrupt 11, 43 ; Unused / Generic
152 m_interrupt 12, 44 ; PS2 Mouse
153 m_interrupt 13, 45 ; Numeric Coprocessor
154 m_interrupt 14, 46 ; IDE0 (HDD)
155 m_interrupt 15, 47 ; IDE1 (HDD)