Disabled debug in ld-acess.so/loadlib
[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         push    0
146         push    %1
147         jmp     IRQCommon
148 %endmacro
149
150 ISR_NOERR       0;  0: Divide By Zero Exception
151 ISR_NOERR       1;  1: Debug Exception
152 ISR_NOERR       2;  2: Non Maskable Interrupt Exception
153 ISR_NOERR       3;  3: Int 3 Exception
154 ISR_NOERR       4;  4: INTO Exception
155 ISR_NOERR       5;  5: Out of Bounds Exception
156 ISR_NOERR       6;  6: Invalid Opcode Exception
157 ISR_NOERR       7;  7: Coprocessor Not Available Exception
158 ISR_ERRNO       8;  8: Double Fault Exception (With Error Code!)
159 ISR_NOERR       9;  9: Coprocessor Segment Overrun Exception
160 ISR_ERRNO       10; 10: Bad TSS Exception (With Error Code!)
161 ISR_ERRNO       11; 11: Segment Not Present Exception (With Error Code!)
162 ISR_ERRNO       12; 12: Stack Fault Exception (With Error Code!)
163 ISR_ERRNO       13; 13: General Protection Fault Exception (With Error Code!)
164 ISR_ERRNO       14; 14: Page Fault Exception (With Error Code!)
165 ISR_NOERR       15; 15: Reserved Exception
166 ISR_NOERR       16; 16: Floating Point Exception
167 ISR_NOERR       17; 17: Alignment Check Exception
168 ISR_NOERR       18; 18: Machine Check Exception
169 ISR_NOERR       19; 19: Reserved
170 ISR_NOERR       20; 20: Reserved
171 ISR_NOERR       21; 21: Reserved
172 ISR_NOERR       22; 22: Reserved
173 ISR_NOERR       23; 23: Reserved
174 ISR_NOERR       24; 24: Reserved
175 ISR_NOERR       25; 25: Reserved
176 ISR_NOERR       26; 26: Reserved
177 ISR_NOERR       27; 27: Reserved
178 ISR_NOERR       28; 28: Reserved
179 ISR_NOERR       29; 29: Reserved
180 ISR_NOERR       30; 30: Reserved
181 ISR_NOERR       31; 31: Reserved
182
183 DEF_SYSCALL     0xAC    ; Acess System Call
184
185 ; IRQs
186 ; - Timer
187 [global _Isr240]
188 _Isr240:
189         push 0
190         jmp SchedulerBase
191 ; - Assignable
192 %assign i       0xF1
193 %rep 16
194         DEF_IRQ i
195 %assign i i+1
196 %endrep
197
198 ; ---------------------
199 ; Common error handling
200 ; ---------------------
201 [extern _ErrorHandler]
202 ErrorCommon:
203         pusha
204         push ds
205         push es
206         push fs
207         push gs
208         
209         push esp
210         call _ErrorHandler
211         add esp, 4
212         
213         pop gs
214         pop fs
215         pop es
216         pop ds
217         popa
218         add esp, 8      ; Error Code and ID
219         iret
220
221 ; --------------------------
222 ; Common System Call Handler
223 ; --------------------------
224 [extern _SyscallHandler]
225 SyscallCommon:
226         pusha
227         push ds
228         push es
229         push fs
230         push gs
231         
232         push esp
233         call _SyscallHandler
234         add esp, 4
235         
236         pop gs
237         pop fs
238         pop es
239         pop ds
240         popa
241         add esp, 8      ; Error Code and ID
242         iret
243
244 ; ------------
245 ; IRQ Handling
246 ; ------------
247 [extern _IRQ_Handler]
248 IRQCommon:
249         pusha
250         push ds
251         push es
252         push fs
253         push gs
254         
255         push esp
256         call _IRQ_Handler
257         add esp, 4
258         
259         pop gs
260         pop fs
261         pop es
262         pop ds
263         popa
264         add esp, 8      ; Error Code and ID
265         iret
266
267 ; --------------
268 ; Task Scheduler
269 ; --------------
270 [extern _Proc_Scheduler]
271 SchedulerBase:
272         pusha
273         push ds
274         push es
275         push fs
276         push gs
277         
278         mov eax, [esp+12*4]     ; CPU Number
279         push eax        ; Pus as argument
280         
281         call _Proc_Scheduler
282         
283         add esp, 4      ; Remove Argument
284         
285         pop gs
286         pop fs
287         pop es
288         pop ds
289
290         mov dx, 0x20
291         mov al, 0x20
292         out dx, al              ; ACK IRQ
293         popa
294         add esp, 4      ; CPU ID
295         ; No Error code / int num
296         iret

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