Kernel/x86 - Indent fixes only
[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  0xEE    ; 0xEE Timer
93         SETISR  0xEF    ; 0xEF Spurious Interrupt
94         %endif
95
96         ; IRQs
97         %assign i       0xF0
98         %rep 16
99         SETISR  i
100         %assign i i+1
101         %endrep
102
103         ; Load IDT
104         lidt [gIDTPtr]
105
106         ; Remap PIC
107         push edx        ; Save EDX
108         mov dx, 0x20
109         mov al, 0x11
110         out dx, al      ;       Init Command
111         mov dx, 0x21
112         mov al, 0xF0
113         out dx, al      ;       Offset (Start of IDT Range)
114         mov al, 0x04
115         out dx, al      ;       IRQ connected to Slave (00000100b) = IRQ2
116         mov al, 0x01
117         out dx, al      ;       Set Mode
118         mov al, 0x00
119         out dx, al      ;       Set Mode
120         
121         mov dx, 0xA0
122         mov al, 0x11
123         out dx, al      ;       Init Command
124         mov dx, 0xA1
125         mov al, 0xF8
126         out dx, al      ;       Offset (Start of IDT Range)
127         mov al, 0x02
128         out dx, al      ;       IRQ Line connected to master
129         mov al, 0x01
130         out dx, al      ;       Set Mode
131         mov dl, 0x00
132         out dx, al      ;       Set Mode
133         pop edx
134         
135         ret
136
137
138 ; ===============
139 ; = Define ISRs =
140 ; ===============
141 %macro  ISR_ERRNO       1
142 [global Isr%1]
143 Isr%1:
144         ;xchg bx, bx
145         push    %1
146         jmp     ErrorCommon
147 %endmacro
148 %macro  ISR_NOERR       1
149 [global Isr%1]
150 Isr%1:
151         ;xchg bx, bx
152         push    0
153         push    %1
154         jmp     ErrorCommon
155 %endmacro
156
157 %macro DEF_SYSCALL      1
158 [global Isr%1]
159 Isr%1:
160         push    0
161         push    %1
162         jmp     SyscallCommon
163 %endmacro
164
165 %macro DEF_IRQ  1
166 [global Isr%1]
167 Isr%1:
168         push    0
169         push    %1
170         jmp     IRQCommon
171 %endmacro
172
173 ISR_NOERR       0;  0: Divide By Zero Exception
174 ISR_NOERR       1;  1: Debug Exception
175 ISR_NOERR       2;  2: Non Maskable Interrupt Exception
176 ISR_NOERR       3;  3: Int 3 Exception
177 ISR_NOERR       4;  4: INTO Exception
178 ISR_NOERR       5;  5: Out of Bounds Exception
179 ISR_NOERR       6;  6: Invalid Opcode Exception
180 ISR_NOERR       7;  7: Coprocessor Not Available Exception
181 ISR_ERRNO       8;  8: Double Fault Exception (With Error Code!)
182 ISR_NOERR       9;  9: Coprocessor Segment Overrun Exception
183 ISR_ERRNO       10; 10: Bad TSS Exception (With Error Code!)
184 ISR_ERRNO       11; 11: Segment Not Present Exception (With Error Code!)
185 ISR_ERRNO       12; 12: Stack Fault Exception (With Error Code!)
186 ISR_ERRNO       13; 13: General Protection Fault Exception (With Error Code!)
187 ISR_ERRNO       14; 14: Page Fault Exception (With Error Code!)
188 ISR_NOERR       15; 15: Reserved Exception
189 ISR_NOERR       16; 16: Floating Point Exception
190 ISR_NOERR       17; 17: Alignment Check Exception
191 ISR_NOERR       18; 18: Machine Check Exception
192 ISR_NOERR       19; 19: Reserved
193 ISR_NOERR       20; 20: Reserved
194 ISR_NOERR       21; 21: Reserved
195 ISR_NOERR       22; 22: Reserved
196 ISR_NOERR       23; 23: Reserved
197 ISR_NOERR       24; 24: Reserved
198 ISR_NOERR       25; 25: Reserved
199 ISR_NOERR       26; 26: Reserved
200 ISR_NOERR       27; 27: Reserved
201 ISR_NOERR       28; 28: Reserved
202 ISR_NOERR       29; 29: Reserved
203 ISR_NOERR       30; 30: Reserved
204 ISR_NOERR       31; 31: Reserved
205
206 DEF_SYSCALL     0xAC    ; Acess System Call
207
208 %if USE_MP
209 [global Isr0xEE]
210 [extern SchedulerBase]
211 ; AP's Timer Interrupt
212 Isr0xEE:
213         push 0  ; Line up with interrupt number
214         xchg bx, bx     ; MAGIC BREAK
215         jmp SchedulerBase
216 ; Spurious Interrupt
217 [global Isr0xEF]
218 Isr0xEF:
219         xchg bx, bx     ; MAGIC BREAK
220         iret
221 %endif
222
223 ; IRQs
224 ; - Timer
225 [global Isr240]
226 [global Isr240.jmp]
227 [extern SchedulerBase]
228 [extern SetAPICTimerCount]
229 Isr240:
230         push 0  ; Line up with Argument in errors
231         push 0  ; CPU Number
232         ;xchg bx, bx    ; MAGIC BREAK
233 Isr240.jmp:
234         %if USE_MP
235         jmp SetAPICTimerCount   ; This is reset once the bus speed has been calculated
236         %else
237         jmp SchedulerBase
238         %endif
239 ; - Assignable
240 %assign i       0xF1
241 %rep 16
242         DEF_IRQ i
243 %assign i i+1
244 %endrep
245
246 ; ---------------------
247 ; Common error handling
248 ; ---------------------
249 [extern ErrorHandler]
250 ErrorCommon:
251         ;xchg bx, bx    ; MAGIC BREAK
252         
253         pusha
254         push ds
255         push es
256         push fs
257         push gs
258
259         ; Clear TF      
260 ;       pushf
261 ;       and WORD [esp], 0xFEFF
262 ;       popf
263
264         mov ax, 0x10
265         mov ds, ax
266         mov es, ax
267         mov fs, ax
268         mov gs, ax
269         
270         push esp
271         call ErrorHandler
272         add esp, 4
273         
274         pop gs
275         pop fs
276         pop es
277         pop ds
278         popa
279         add esp, 8      ; Error Code and ID
280         iret
281
282 ; --------------------------
283 ; Common System Call Handler
284 ; --------------------------
285 [extern SyscallHandler]
286 SyscallCommon:
287         pusha
288         push ds
289         push es
290         push fs
291         push gs
292         
293         push esp
294         call SyscallHandler
295         add esp, 4
296         
297         ; Pass changes to TF on to the user
298         ; EFLAGS is stored at ESP[4+8+2+2]
299         ; 4 Segment Registers
300         ; 8 GPRs
301         ; 2 Error Code / Interrupt ID
302         ; 2 CS/EIP
303         pushf
304         pop eax
305         and eax, 0x100  ; 0x100 = Trace Flag
306         and WORD [esp+(4+8+2+2)*4], ~0x100      ; Clear
307         or DWORD [esp+(4+8+2+2)*4], eax ; Set for user
308         
309         pop gs
310         pop fs
311         pop es
312         pop ds
313         popa
314         add esp, 8      ; Error Code and ID
315         iret
316
317 ; ------------
318 ; IRQ Handling
319 ; ------------
320 [extern IRQ_Handler]
321 [global IRQCommon]
322 [global IRQCommon_handled]
323 IRQCommon_handled equ IRQCommon.handled
324 IRQCommon:
325         pusha
326         push ds
327         push es
328         push fs
329         push gs
330         
331         mov ax, 0x10
332         mov ds, ax
333         mov es, ax
334         mov fs, ax
335         mov gs, ax
336         
337         push esp
338         call IRQ_Handler
339 .handled:
340         add esp, 4
341         
342         pop gs
343         pop fs
344         pop es
345         pop ds
346         popa
347         add esp, 8      ; Error Code and ID
348         iret

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