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

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