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

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