Kernel - Slight reworks to timer code
[tpg/acess2.git] / Kernel / arch / x86 / desctab.asm
1 ; AcessOS Microkernel Version
2 ;
3 ; desctab.asm
4 [BITS 32]
5
6
7 [section .data]
8 ; IDT
9 ALIGN 8
10 [global gIDT]
11 gIDT:
12         ; CS = 0x08, Type = 32-bit Interrupt (0xE = 1 110)
13         times   256     dd      0x00080000,0x00000E00
14 [global gIDTPtr]
15 gIDTPtr:
16         dw      256 * 16 - 1    ; Limit
17         dd      gIDT            ; Base
18
19 [section .text]
20
21 [global Desctab_Install]
22 Desctab_Install:
23         ; Set up IDT
24         ; Helper Macros
25         ; - Set an IDT entry to an ISR
26 %macro SETISR   1
27         mov eax, Isr%1
28         mov     WORD [gIDT + %1*8], ax
29         shr eax, 16
30         mov     WORD [gIDT + %1*8+6], ax
31         ; Enable
32         mov     ax, WORD [gIDT + %1*8 + 4]
33         or ax, 0x8000
34         mov     WORD [gIDT + %1*8 + 4], ax
35 %endmacro
36         ; Enable user calling of an ISR
37 %macro SET_USER 1
38         or WORD [gIDT + %1*8 + 4], 0x6000
39 %endmacro
40         ; Set an ISR as a trap (leaves interrupts enabled when invoked)
41 %macro SET_TRAP 1
42         or WORD [gIDT + %1*8 + 4], 0x0100
43 %endmacro
44
45         ; Error handlers
46         %assign i       0
47         %rep 32
48         SETISR  i
49         %assign i i+1
50         %endrep
51         
52         ; User Syscall
53         SETISR  0xAC
54         SET_USER        0xAC
55         SET_TRAP        0xAC    ; Interruptable
56         
57         ; MP ISRs
58         %if USE_MP
59         SETISR  0xED    ; 0xED Inter-processor HALT
60         SETISR  0xEE    ; 0xEE Timer
61         SETISR  0xEF    ; 0xEF Spurious Interrupt
62         %endif
63
64         ; IRQs
65         %assign i       0xF0
66         %rep 16
67         SETISR  i
68         %assign i i+1
69         %endrep
70
71         ; Load IDT
72         lidt [gIDTPtr]
73
74         ; Remap PIC
75         push edx        ; Save EDX
76         mov dx, 0x20
77         mov al, 0x11
78         out dx, al      ;       Init Command
79         mov dx, 0x21
80         mov al, 0xF0
81         out dx, al      ;       Offset (Start of IDT Range)
82         mov al, 0x04
83         out dx, al      ;       IRQ connected to Slave (00000100b) = IRQ2
84         mov al, 0x01
85         out dx, al      ;       Set Mode
86         mov al, 0x00
87         out dx, al      ;       Set Mode
88         
89         mov dx, 0xA0
90         mov al, 0x11
91         out dx, al      ;       Init Command
92         mov dx, 0xA1
93         mov al, 0xF8
94         out dx, al      ;       Offset (Start of IDT Range)
95         mov al, 0x02
96         out dx, al      ;       IRQ Line connected to master
97         mov al, 0x01
98         out dx, al      ;       Set Mode
99         mov dl, 0x00
100         out dx, al      ;       Set Mode
101         pop edx
102         
103         ret
104
105
106 ; ===============
107 ; = Define ISRs =
108 ; ===============
109 %macro  ISR_ERRNO       1
110 [global Isr%1]
111 Isr%1:
112         ;xchg bx, bx
113         push    %1
114         jmp     ErrorCommon
115 %endmacro
116 %macro  ISR_NOERR       1
117 [global Isr%1]
118 Isr%1:
119         ;xchg bx, bx
120         push    0
121         push    %1
122         jmp     ErrorCommon
123 %endmacro
124
125 %macro DEF_SYSCALL      1
126 [global Isr%1]
127 Isr%1:
128         push    0
129         push    %1
130         jmp     SyscallCommon
131 %endmacro
132
133 %macro DEF_IRQ  1
134 [global Isr%1]
135 Isr%1:
136         push    0
137         push    %1
138         jmp     IRQCommon
139 %endmacro
140
141 ISR_NOERR       0;  0: Divide By Zero Exception
142 ISR_NOERR       1;  1: Debug Exception
143 ISR_NOERR       2;  2: Non Maskable Interrupt Exception
144 ISR_NOERR       3;  3: Int 3 Exception
145 ISR_NOERR       4;  4: INTO Exception
146 ISR_NOERR       5;  5: Out of Bounds Exception
147 ISR_NOERR       6;  6: Invalid Opcode Exception
148 ISR_NOERR       7;  7: Coprocessor Not Available Exception
149 ISR_ERRNO       8;  8: Double Fault Exception (With Error Code!)
150 ISR_NOERR       9;  9: Coprocessor Segment Overrun Exception
151 ISR_ERRNO       10; 10: Bad TSS Exception (With Error Code!)
152 ISR_ERRNO       11; 11: Segment Not Present Exception (With Error Code!)
153 ISR_ERRNO       12; 12: Stack Fault Exception (With Error Code!)
154 ISR_ERRNO       13; 13: General Protection Fault Exception (With Error Code!)
155 ISR_ERRNO       14; 14: Page Fault Exception (With Error Code!)
156 ISR_NOERR       15; 15: Reserved Exception
157 ISR_NOERR       16; 16: Floating Point Exception
158 ISR_NOERR       17; 17: Alignment Check Exception
159 ISR_NOERR       18; 18: Machine Check Exception
160 ISR_NOERR       19; 19: Reserved
161 ISR_NOERR       20; 20: Reserved
162 ISR_NOERR       21; 21: Reserved
163 ISR_NOERR       22; 22: Reserved
164 ISR_NOERR       23; 23: Reserved
165 ISR_NOERR       24; 24: Reserved
166 ISR_NOERR       25; 25: Reserved
167 ISR_NOERR       26; 26: Reserved
168 ISR_NOERR       27; 27: Reserved
169 ISR_NOERR       28; 28: Reserved
170 ISR_NOERR       29; 29: Reserved
171 ISR_NOERR       30; 30: Reserved
172 ISR_NOERR       31; 31: Reserved
173
174 DEF_SYSCALL     0xAC    ; Acess System Call
175
176 %if USE_MP
177 [global Isr0xED]
178 ; 0xED - Interprocessor HALT
179 Isr0xED:
180         cli
181 .jmp:   hlt
182         jmp .jmp
183
184 [global Isr0xEE]
185 [extern SchedulerBase]
186 ; AP's Timer Interrupt
187 Isr0xEE:
188         push eax        ; Line up with interrupt number
189         mov eax, dr1    ; CPU Number
190         push eax
191         mov eax, [esp+4]        ; Load EAX back
192         jmp SchedulerBase
193 ; Spurious Interrupt
194 [global Isr0xEF]
195 Isr0xEF:
196         xchg bx, bx     ; MAGIC BREAK
197         iret
198 %endif
199
200 ; IRQs
201 ; - Timer
202 [global Isr240]
203 [global Isr240.jmp]
204 [extern SchedulerBase]
205 [extern SetAPICTimerCount]
206 Isr240:
207         push 0  ; Line up with Argument in errors
208         push 0  ; CPU Number
209         ;xchg bx, bx    ; MAGIC BREAK
210 Isr240.jmp:
211         %if USE_MP
212         jmp SetAPICTimerCount   ; This is reset once the bus speed has been calculated
213         %else
214         jmp SchedulerBase
215         %endif
216 ; - Assignable
217 %assign i       0xF1
218 %rep 16
219         DEF_IRQ i
220 %assign i i+1
221 %endrep
222
223 ; ---------------------
224 ; Common error handling
225 ; ---------------------
226 [extern ErrorHandler]
227 ErrorCommon:
228         ;xchg bx, bx    ; MAGIC BREAK
229         
230         pusha
231         push ds
232         push es
233         push fs
234         push gs
235
236         ; Clear TF      
237 ;       pushf
238 ;       and WORD [esp], 0xFEFF
239 ;       popf
240
241         mov ax, 0x10
242         mov ds, ax
243         mov es, ax
244         mov fs, ax
245         mov gs, ax
246         
247         push esp
248         call ErrorHandler
249         add esp, 4
250         
251         pop gs
252         pop fs
253         pop es
254         pop ds
255         popa
256         add esp, 8      ; Error Code and ID
257         iret
258
259 ; --------------------------
260 ; Common System Call Handler
261 ; --------------------------
262 [extern SyscallHandler]
263 SyscallCommon:
264         pusha
265         push ds
266         push es
267         push fs
268         push gs
269         
270         push esp
271         call SyscallHandler
272         add esp, 4
273         
274         ; Pass changes to TF on to the user
275         ; EFLAGS is stored at ESP[4+8+2+2]
276         ; 4 Segment Registers
277         ; 8 GPRs
278         ; 2 Error Code / Interrupt ID
279         ; 2 CS/EIP
280         pushf
281         pop eax
282         and eax, 0x100  ; 0x100 = Trace Flag
283         and WORD [esp+(4+8+2+2)*4], ~0x100      ; Clear
284         or DWORD [esp+(4+8+2+2)*4], eax ; Set for user
285         
286         pop gs
287         pop fs
288         pop es
289         pop ds
290         popa
291         add esp, 8      ; Error Code and ID
292         iret
293
294 ; ------------
295 ; IRQ Handling
296 ; ------------
297 [extern IRQ_Handler]
298 [global IRQCommon]
299 [global IRQCommon_handled]
300 IRQCommon_handled equ IRQCommon.handled
301 IRQCommon:
302         pusha
303         push ds
304         push es
305         push fs
306         push gs
307         
308         mov ax, 0x10
309         mov ds, ax
310         mov es, ax
311         mov fs, ax
312         mov gs, ax
313         
314         push esp
315         call IRQ_Handler
316 .handled:
317         add esp, 4
318         
319         pop gs
320         pop fs
321         pop es
322         pop ds
323         popa
324         add esp, 8      ; Error Code and ID
325         iret
326
327 ; vim: ft=nasm ts=8

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