Commenting is nice
[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
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
231         ;xchg bx, bx    ; MAGIC BREAK
232 Isr240.jmp:
233         %if USE_MP
234         jmp SetAPICTimerCount   ; This is reset once the bus speed has been calculated
235         %else
236         jmp SchedulerBase
237         %endif
238 ; - Assignable
239 %assign i       0xF1
240 %rep 16
241         DEF_IRQ i
242 %assign i i+1
243 %endrep
244
245 ; ---------------------
246 ; Common error handling
247 ; ---------------------
248 [extern ErrorHandler]
249 ErrorCommon:
250         ;xchg bx, bx    ; MAGIC BREAK
251         pusha
252         push ds
253         push es
254         push fs
255         push gs
256         
257         mov ax, 0x10
258         mov ds, ax
259         mov es, ax
260         mov fs, ax
261         mov gs, ax
262         
263         push esp
264         call ErrorHandler
265         add esp, 4
266         
267         pop gs
268         pop fs
269         pop es
270         pop ds
271         popa
272         add esp, 8      ; Error Code and ID
273         iret
274
275 ; --------------------------
276 ; Common System Call Handler
277 ; --------------------------
278 [extern SyscallHandler]
279 SyscallCommon:
280         pusha
281         push ds
282         push es
283         push fs
284         push gs
285         
286         push esp
287         call SyscallHandler
288         add esp, 4
289         
290         pop gs
291         pop fs
292         pop es
293         pop ds
294         popa
295         add esp, 8      ; Error Code and ID
296         iret
297
298 ; ------------
299 ; IRQ Handling
300 ; ------------
301 [extern IRQ_Handler]
302 [global IRQCommon]
303 IRQCommon:
304         pusha
305         push ds
306         push es
307         push fs
308         push gs
309         
310         mov ax, 0x10
311         mov ds, ax
312         mov es, ax
313         mov fs, ax
314         mov gs, ax
315         
316         push esp
317         call IRQ_Handler
318         add esp, 4
319         
320         pop gs
321         pop fs
322         pop es
323         pop ds
324         popa
325         add esp, 8      ; Error Code and ID
326         iret

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