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

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