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

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