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

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