SMP Work (APs now start, just seem to lock up on lgdt, or whatever is at that address)
[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   ; Double Fault TSS
25         times MAX_CPUS  dd 26*4-1, 0x00408900
26 [global gGDTPtr]
27 gGDTPtr:
28         dw      GDT_SIZE-1
29         dd      gGDT
30 ; IDT
31 ALIGN 8
32 gIDT:
33         times   256     dd      0x00080000,0x00000F00
34 [global gIDTPtr]
35 gIDTPtr:
36         dw      256 * 16 - 1    ; Limit
37         dd      gIDT            ; Base
38
39 [section .text]
40
41 [global Desctab_Install]
42 Desctab_Install:
43         ; Set GDT
44         lgdt [gGDTPtr]
45         mov ax, 0x10    ; PL0 Data
46         mov ss, ax
47         mov ds, ax
48         mov es, ax
49         mov gs, ax
50         mov fs, ax
51         jmp 0x08:.pl0code
52 .pl0code:
53
54         ; Set IDT
55 %macro SETISR   1
56         mov eax, Isr%1
57         mov     WORD [gIDT + %1*8], ax
58         shr eax, 16
59         mov     WORD [gIDT + %1*8+6], ax
60         mov     ax, WORD [gIDT + %1*8 + 4]
61         or ax, 0x8000
62         mov     WORD [gIDT + %1*8 + 4], ax
63 %endmacro
64 %macro SETUSER  1
65         mov     ax, WORD [gIDT + %1*8 + 4]
66         or ax, 0x6000
67         mov     WORD [gIDT + %1*8 + 4], ax
68 %endmacro
69         %assign i       0
70         %rep 32
71         SETISR  i
72         %assign i i+1
73         %endrep
74         
75         SETISR  0xAC
76         SETUSER 0xAC
77         
78         %assign i       0xF0
79         %rep 16
80         SETISR  i
81         %assign i i+1
82         %endrep
83
84         ; Load IDT
85         lidt [gIDTPtr]
86
87         ; Remap PIC
88         push edx        ; Save EDX
89         mov dx, 0x20
90         mov al, 0x11
91         out dx, al      ;       Init Command
92     mov dx, 0x21
93         mov al, 0xF0
94         out dx, al      ;       Offset (Start of IDT Range)
95     mov al, 0x04
96         out dx, al      ;       IRQ connected to Slave (00000100b) = IRQ2
97     mov al, 0x01
98         out dx, al      ;       Set Mode
99     mov al, 0x00
100         out dx, al      ;       Set Mode
101         
102         mov dx, 0xA0
103         mov al, 0x11
104         out dx, al      ;       Init Command
105     mov dx, 0xA1
106         mov al, 0xF8
107         out dx, al      ;       Offset (Start of IDT Range)
108     mov al, 0x02
109         out dx, al      ;       IRQ Line connected to master
110     mov al, 0x01
111         out dx, al      ;       Set Mode
112     mov dl, 0x00
113         out dx, al      ;       Set Mode
114         pop edx
115         
116         ret
117
118
119 ; ===============
120 ; = Define ISRs =
121 ; ===============
122 %macro  ISR_ERRNO       1
123 [global Isr%1]
124 Isr%1:
125         ;xchg bx, bx
126         push    %1
127         jmp     ErrorCommon
128 %endmacro
129 %macro  ISR_NOERR       1
130 [global Isr%1]
131 Isr%1:
132         xchg bx, bx
133         push    0
134         push    %1
135         jmp     ErrorCommon
136 %endmacro
137
138 %macro DEF_SYSCALL      1
139 [global Isr%1]
140 Isr%1:
141         push    0
142         push    %1
143         jmp     SyscallCommon
144 %endmacro
145
146 %macro DEF_IRQ  1
147 [global Isr%1]
148 Isr%1:
149         ;cli    ; HACK!
150         push    0
151         push    %1
152         jmp     IRQCommon
153 %endmacro
154
155 ISR_NOERR       0;  0: Divide By Zero Exception
156 ISR_NOERR       1;  1: Debug Exception
157 ISR_NOERR       2;  2: Non Maskable Interrupt Exception
158 ISR_NOERR       3;  3: Int 3 Exception
159 ISR_NOERR       4;  4: INTO Exception
160 ISR_NOERR       5;  5: Out of Bounds Exception
161 ISR_NOERR       6;  6: Invalid Opcode Exception
162 ISR_NOERR       7;  7: Coprocessor Not Available Exception
163 ISR_ERRNO       8;  8: Double Fault Exception (With Error Code!)
164 ISR_NOERR       9;  9: Coprocessor Segment Overrun Exception
165 ISR_ERRNO       10; 10: Bad TSS Exception (With Error Code!)
166 ISR_ERRNO       11; 11: Segment Not Present Exception (With Error Code!)
167 ISR_ERRNO       12; 12: Stack Fault Exception (With Error Code!)
168 ISR_ERRNO       13; 13: General Protection Fault Exception (With Error Code!)
169 ISR_ERRNO       14; 14: Page Fault Exception (With Error Code!)
170 ISR_NOERR       15; 15: Reserved Exception
171 ISR_NOERR       16; 16: Floating Point Exception
172 ISR_NOERR       17; 17: Alignment Check Exception
173 ISR_NOERR       18; 18: Machine Check Exception
174 ISR_NOERR       19; 19: Reserved
175 ISR_NOERR       20; 20: Reserved
176 ISR_NOERR       21; 21: Reserved
177 ISR_NOERR       22; 22: Reserved
178 ISR_NOERR       23; 23: Reserved
179 ISR_NOERR       24; 24: Reserved
180 ISR_NOERR       25; 25: Reserved
181 ISR_NOERR       26; 26: Reserved
182 ISR_NOERR       27; 27: Reserved
183 ISR_NOERR       28; 28: Reserved
184 ISR_NOERR       29; 29: Reserved
185 ISR_NOERR       30; 30: Reserved
186 ISR_NOERR       31; 31: Reserved
187
188 DEF_SYSCALL     0xAC    ; Acess System Call
189
190 ; IRQs
191 ; - Timer
192 [global Isr240]
193 Isr240:
194         push 0
195         jmp SchedulerBase
196 ; - Assignable
197 %assign i       0xF1
198 %rep 16
199         DEF_IRQ i
200 %assign i i+1
201 %endrep
202
203 ; ---------------------
204 ; Common error handling
205 ; ---------------------
206 [extern ErrorHandler]
207 ErrorCommon:
208         pusha
209         push ds
210         push es
211         push fs
212         push gs
213         
214         push esp
215         call ErrorHandler
216         add esp, 4
217         
218         pop gs
219         pop fs
220         pop es
221         pop ds
222         popa
223         add esp, 8      ; Error Code and ID
224         iret
225
226 ; --------------------------
227 ; Common System Call Handler
228 ; --------------------------
229 [extern SyscallHandler]
230 SyscallCommon:
231         pusha
232         push ds
233         push es
234         push fs
235         push gs
236         
237         push esp
238         call SyscallHandler
239         add esp, 4
240         
241         pop gs
242         pop fs
243         pop es
244         pop ds
245         popa
246         add esp, 8      ; Error Code and ID
247         iret
248
249 ; ------------
250 ; IRQ Handling
251 ; ------------
252 [extern IRQ_Handler]
253 IRQCommon:
254         pusha
255         push ds
256         push es
257         push fs
258         push gs
259         
260         push esp
261         call IRQ_Handler
262         add esp, 4
263         
264         pop gs
265         pop fs
266         pop es
267         pop ds
268         popa
269         add esp, 8      ; Error Code and ID
270         iret
271
272 ; --------------
273 ; Task Scheduler
274 ; --------------
275 [extern Proc_Scheduler]
276 SchedulerBase:
277         pusha
278         push ds
279         push es
280         push fs
281         push gs
282         
283         mov eax, [esp+12*4]     ; CPU Number
284         push eax        ; Pus as argument
285         
286         call Proc_Scheduler
287         
288         add esp, 4      ; Remove Argument
289         
290         pop gs
291         pop fs
292         pop es
293         pop ds
294
295         mov dx, 0x20
296         mov al, 0x20
297         out dx, al              ; ACK IRQ
298         popa
299         add esp, 4      ; CPU ID
300         ; No Error code / int num
301         iret

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