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

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