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

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