354a001eb375b3a1e747d7930aa3f18694f3a02c
[tpg/acess2.git] / KernelLand / Kernel / arch / x86 / desctab.asm
1 ; AcessOS Microkernel Version
2 ;
3 ; desctab.asm
4 [BITS 32]
5
6
7 [section .data]
8 ; IDT
9 ALIGN 8
10 [global gIDT]
11 gIDT:
12         ; CS = 0x08, Type = 32-bit Interrupt (0xE = 1 110)
13         times   256     dd      0x00080000,0x00000E00
14 [global gIDTPtr]
15 gIDTPtr:
16         dw      256 * 16 - 1    ; Limit
17         dd      gIDT            ; Base
18
19 [section .text]
20
21 [global Desctab_Install]
22 Desctab_Install:
23         ; Set up IDT
24         ; Helper Macros
25         ; - Set an IDT entry to an ISR
26 %macro SETISR   1
27         mov eax, Isr%1
28         mov     WORD [gIDT + %1*8], ax
29         shr eax, 16
30         mov     WORD [gIDT + %1*8+6], ax
31         ; Enable
32         mov     ax, WORD [gIDT + %1*8 + 4]
33         or ax, 0x8000
34         mov     WORD [gIDT + %1*8 + 4], ax
35 %endmacro
36         ; Enable user calling of an ISR
37 %macro SET_USER 1
38         or WORD [gIDT + %1*8 + 4], 0x6000
39 %endmacro
40         ; Set an ISR as a trap (leaves interrupts enabled when invoked)
41 %macro SET_TRAP 1
42         or WORD [gIDT + %1*8 + 4], 0x0100
43 %endmacro
44
45         ; Error handlers
46         %assign i       0
47         %rep 32
48         SETISR  i
49         %assign i i+1
50         %endrep
51         
52         ; User Syscall
53         SETISR  0xAC
54         SET_USER        0xAC
55         SET_TRAP        0xAC    ; Interruptable
56         
57         ; MP ISRs
58         %if USE_MP
59         SETISR  0xED    ; 0xED Inter-processor HALT
60         SETISR  0xEE    ; 0xEE Timer
61         SETISR  0xEF    ; 0xEF Spurious Interrupt
62         %endif
63
64         ; IRQs
65         %assign i       0xF0
66         %rep 16
67         SETISR  i
68         %assign i i+1
69         %endrep
70
71         ; Load IDT
72         lidt [gIDTPtr]
73
74         ; Remap PIC
75         push edx        ; Save EDX
76         mov dx, 0x20
77         mov al, 0x11
78         out dx, al      ;       Init Command
79         mov dx, 0x21
80         mov al, 0xF0
81         out dx, al      ;       Offset (Start of IDT Range)
82         mov al, 0x04
83         out dx, al      ;       IRQ connected to Slave (00000100b) = IRQ2
84         mov al, 0x01
85         out dx, al      ;       Set Mode
86         mov al, 0x00
87         out dx, al      ;       Set Mode
88         
89         mov dx, 0xA0
90         mov al, 0x11
91         out dx, al      ;       Init Command
92         mov dx, 0xA1
93         mov al, 0xF8
94         out dx, al      ;       Offset (Start of IDT Range)
95         mov al, 0x02
96         out dx, al      ;       IRQ Line connected to master
97         mov al, 0x01
98         out dx, al      ;       Set Mode
99         mov dl, 0x00
100         out dx, al      ;       Set Mode
101         pop edx
102         
103         ret
104
105
106 ; ===============
107 ; = Define ISRs =
108 ; ===============
109 %macro  ISR_ERRNO       1
110 [global Isr%1]
111 Isr%1:
112         ;xchg bx, bx
113         push    %1
114         jmp     ErrorCommon
115 %endmacro
116 %macro  ISR_NOERR       1
117 [global Isr%1]
118 Isr%1:
119         ;xchg bx, bx
120         push    0
121         push    %1
122         jmp     ErrorCommon
123 %endmacro
124
125 %macro DEF_SYSCALL      1
126 [global Isr%1]
127 Isr%1:
128         push    0
129         push    %1
130         jmp     SyscallCommon
131 %endmacro
132
133 %macro DEF_IRQ  1
134 [global Isr%1]
135 Isr%1:
136         push    0
137         push    %1
138         jmp     IRQCommon
139 %endmacro
140
141 ISR_NOERR       0;  0: Divide By Zero Exception
142 ISR_NOERR       1;  1: Debug Exception
143 ISR_NOERR       2;  2: Non Maskable Interrupt Exception
144 ISR_NOERR       3;  3: Int 3 Exception
145 ISR_NOERR       4;  4: INTO Exception
146 ISR_NOERR       5;  5: Out of Bounds Exception
147 ISR_NOERR       6;  6: Invalid Opcode Exception
148 ISR_NOERR       7;  7: Coprocessor Not Available Exception
149 ISR_ERRNO       8;  8: Double Fault Exception (With Error Code!)
150 ISR_NOERR       9;  9: Coprocessor Segment Overrun Exception
151 ISR_ERRNO       10; 10: Bad TSS Exception (With Error Code!)
152 ISR_ERRNO       11; 11: Segment Not Present Exception (With Error Code!)
153 ISR_ERRNO       12; 12: Stack Fault Exception (With Error Code!)
154 ISR_ERRNO       13; 13: General Protection Fault Exception (With Error Code!)
155 ISR_ERRNO       14; 14: Page Fault Exception (With Error Code!)
156 ISR_NOERR       15; 15: Reserved Exception
157 ISR_NOERR       16; 16: Floating Point Exception
158 ISR_NOERR       17; 17: Alignment Check Exception
159 ISR_NOERR       18; 18: Machine Check Exception
160 ISR_NOERR       19; 19: Reserved
161 ISR_NOERR       20; 20: Reserved
162 ISR_NOERR       21; 21: Reserved
163 ISR_NOERR       22; 22: Reserved
164 ISR_NOERR       23; 23: Reserved
165 ISR_NOERR       24; 24: Reserved
166 ISR_NOERR       25; 25: Reserved
167 ISR_NOERR       26; 26: Reserved
168 ISR_NOERR       27; 27: Reserved
169 ISR_NOERR       28; 28: Reserved
170 ISR_NOERR       29; 29: Reserved
171 ISR_NOERR       30; 30: Reserved
172 ISR_NOERR       31; 31: Reserved
173
174 DEF_SYSCALL     0xAC    ; Acess System Call
175
176 %if USE_MP
177 [global Isr0xED]
178 ; 0xED - Interprocessor HALT
179 Isr0xED:
180         cli
181 .jmp:   hlt
182         jmp .jmp
183
184 [global Isr0xEE]
185 [extern SchedulerBase]
186 ; AP's Timer Interrupt
187 Isr0xEE:
188         push eax        ; Line up with interrupt number
189         mov eax, dr1    ; CPU Number
190         push eax
191         mov eax, [esp+4]        ; Load EAX back
192         jmp SchedulerBase
193 ; Spurious Interrupt
194 [global Isr0xEF]
195 Isr0xEF:
196         xchg bx, bx     ; MAGIC BREAK
197         iret
198 %endif
199
200 ; IRQs
201 ; - Timer
202 [global Isr240]
203 [global Isr240.jmp]
204 [extern SchedulerBase]
205 [extern SetAPICTimerCount]
206 Isr240:
207         push 0  ; Line up with Argument in errors
208         push 0  ; CPU Number
209         ;xchg bx, bx    ; MAGIC BREAK
210 Isr240.jmp:
211         %if USE_MP
212         jmp SetAPICTimerCount   ; This is reset once the bus speed has been calculated
213         %else
214         jmp SchedulerBase
215         %endif
216 ; - Assignable
217 %assign i       0xF1
218 %rep 15
219         DEF_IRQ i
220 %assign i i+1
221 %endrep
222
223 [extern ReturnFromInterrupt]
224 ; ---------------------
225 ; Common error handling
226 ; ---------------------
227 [extern ErrorHandler]
228 ErrorCommon:
229         pusha
230         push ds
231         push es
232         push fs
233         push gs
234
235         ; Clear TF      
236 ;       pushf
237 ;       and WORD [esp], 0xFEFF
238 ;       popf
239
240         mov ax, 0x10
241         mov ds, ax
242         mov es, ax
243         mov fs, ax
244         mov gs, ax
245         
246         push esp
247         call ErrorHandler
248         add esp, 4
249         
250         jmp ReturnFromInterrupt
251
252 ; --------------------------
253 ; Common System Call Handler
254 ; --------------------------
255 [extern SyscallHandler]
256 SyscallCommon:
257         pusha
258         push ds
259         push es
260         push fs
261         push gs
262         
263         push esp
264         call SyscallHandler
265         add esp, 4
266         
267         ; Pass changes to TF on to the user
268         ; EFLAGS is stored at ESP[4+8+2+2]
269         ; 4 Segment Registers
270         ; 8 GPRs
271         ; 2 Error Code / Interrupt ID
272         ; 2 CS/EIP
273         pushf
274         pop eax
275         and eax, 0x100  ; 0x100 = Trace Flag
276         and WORD [esp+(4+8+2+2)*4], ~0x100      ; Clear
277         or DWORD [esp+(4+8+2+2)*4], eax ; Set for user
278
279         jmp ReturnFromInterrupt 
280
281 ; ------------
282 ; IRQ Handling
283 ; ------------
284 [extern IRQ_Handler]
285 [global IRQCommon]
286 [global IRQCommon_handled]
287 IRQCommon_handled equ IRQCommon.handled
288 IRQCommon:
289         pusha
290         push ds
291         push es
292         push fs
293         push gs
294         
295         mov ax, 0x10
296         mov ds, ax
297         mov es, ax
298         mov fs, ax
299         mov gs, ax
300         
301         push esp
302         call IRQ_Handler
303 .handled:
304         add esp, 4
305         
306         pop gs
307         pop fs
308         pop es
309         pop ds
310         popa
311         add esp, 8      ; Error Code and ID
312         iret
313
314 ; vim: ft=nasm ts=8

UCC git Repository :: git.ucc.asn.au