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

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