Merge branch 'master' of [email protected]:acess2
[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         pusha
253         push ds
254         push es
255         push fs
256         push gs
257         
258         mov ax, 0x10
259         mov ds, ax
260         mov es, ax
261         mov fs, ax
262         mov gs, ax
263         
264         push esp
265         call ErrorHandler
266         add esp, 4
267         
268         pop gs
269         pop fs
270         pop es
271         pop ds
272         popa
273         add esp, 8      ; Error Code and ID
274         iret
275
276 ; --------------------------
277 ; Common System Call Handler
278 ; --------------------------
279 [extern SyscallHandler]
280 SyscallCommon:
281         pusha
282         push ds
283         push es
284         push fs
285         push gs
286         
287         push esp
288         call SyscallHandler
289         add esp, 4
290         
291         ; Pass changes to TF on to the user
292         ; EFLAGS is stored at ESP[4+8+2+2]
293         ; 4 Segment Registers
294         ; 8 GPRs
295         ; 2 Error Code / Interrupt ID
296         ; 2 CS/EIP
297         pushf
298         pop eax
299         and eax, 0x100  ; 0x100 = Trace Flag
300         and WORD [esp+(4+8+2+2)*4], ~0x100      ; Clear
301         or DWORD [esp+(4+8+2+2)*4], eax ; Set for user
302         
303         pop gs
304         pop fs
305         pop es
306         pop ds
307         popa
308         add esp, 8      ; Error Code and ID
309         iret
310
311 ; ------------
312 ; IRQ Handling
313 ; ------------
314 [extern IRQ_Handler]
315 [global IRQCommon]
316 [global IRQCommon_handled]
317 IRQCommon_handled equ IRQCommon.handled
318 IRQCommon:
319         pusha
320         push ds
321         push es
322         push fs
323         push gs
324         
325         mov ax, 0x10
326         mov ds, ax
327         mov es, ax
328         mov fs, ax
329         mov gs, ax
330         
331         push esp
332         call IRQ_Handler
333 .handled:
334         add esp, 4
335         
336         pop gs
337         pop fs
338         pop es
339         pop ds
340         popa
341         add esp, 8      ; Error Code and ID
342         iret

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