BXE: min->MIN, plus an spatch
[akaros.git] / kern / arch / x86 / trapentry32.S
1 /* See COPYRIGHT for copyright information.
2  * The two TRAP* macros (minus the .data parts) are from the JOS project.
3  * Everything else:
4  * Copyright (c) 2009 The Regents of the University of California
5  * Barret Rhoden <brho@cs.berkeley.edu>
6  * See LICENSE for details.
7  */
8 #include <arch/mmu.h>
9 #include <arch/trap.h>
10 #include <ros/memlayout.h>
11
12 ###################################################################
13 # exceptions/interrupts
14 ###################################################################
15
16 /* The TRAPHANDLER macro defines a globally-visible function for handling
17  * a trap.  It pushes a trap number onto the stack, then jumps to _alltraps.
18  * It also builds this traps portion of the trap_tbl.
19  * Use TRAPHANDLER for traps where the CPU automatically pushes an error code.
20  */
21 #define TRAPHANDLER(name, num)                                                                  \
22         .text;                                                                                                          \
23         .globl name;            /* define global symbol for 'name' */   \
24         .type name, @function;  /* symbol type is function */           \
25         .align 2;               /* align function definition */                         \
26         name:                   /* function starts here */                                      \
27         pushl $(num);                                                                                           \
28         jmp _alltraps;                                                                                          \
29         .data;                                                                                                          \
30         .long name;                                                                                                     \
31         .long num
32
33 /* Use TRAPHANDLER_NOEC for traps where the CPU doesn't push an error code.
34  * It pushes a 0 in place of the error code, so the trap frame has the same
35  * format in either case.
36  */
37 #define TRAPHANDLER_NOEC(name, num)             \
38         .text;                                                          \
39         .globl name;                                            \
40         .type name, @function;                          \
41         .align 2;                                                       \
42         name:                                                           \
43         pushl $0;                                                       \
44         pushl $(num);                                           \
45         jmp _alltraps;                                          \
46         .data;                                                          \
47         .long name;                                                     \
48         .long num
49
50 /* Same as NOEC, but for IRQs instead.  num is the ISR number it is mapped to */
51 #define IRQ_HANDLER(name, num)                  \
52         .text;                                                          \
53         .globl name;                                            \
54         .type name, @function;                          \
55         .align 2;                                                       \
56         name:                                                           \
57         pushl $0;                                                       \
58         pushl $(num);                                           \
59         jmp _allirqs;                                           \
60         .data;                                                          \
61         .long name;                                                     \
62         .long num
63
64 /* Only used in the kernel during SMP boot.  Send a LAPIC_EOI and iret. */
65 #define POKE_HANDLER(name, num)                 \
66         .text;                                                          \
67         .globl name;                                            \
68         .type name, @function;                          \
69         .align 2;                                                       \
70         name:;                                                          \
71         movl $0, (LAPIC_BASE + 0x0b0);      \
72         iret;                                                           \
73         .data;                                                          \
74         .long name;                                                     \
75         .long num
76
77 /* Same as above, but takes a specific function to jump to.  See comments
78  * below from _allirqs for details.
79  */
80 #define IRQ_HANDLER_SPEC(name, num, func)                                      \
81         .text;                                                                                                 \
82         .globl name;                                                                                   \
83         .type name, @function;                                                                 \
84         .align 2;                                                                                              \
85         name:                                                                                                  \
86         pushl $0;                                                                  \
87         pushl $(num);                                                              \
88         cld;                                                                       \
89         pushl %ds;                                                                 \
90         pushl %es;                                                                 \
91         pushl %fs;                                                                 \
92         pushl %gs;                                                                 \
93         pushal;                                                                    \
94         movw $0, %ax;                                                              \
95         movw %ax, %gs;                                                             \
96         movw %ax, %fs;                                                             \
97         movw $GD_KD, %ax;                                                          \
98         movw %ax, %ds;                                                             \
99         movw %ax, %es;                                                             \
100         pushl %esp;                                                                \
101         movl $0, %ebp;                                                             \
102         call (func);                                                               \
103         popl %esp;                                                                 \
104         popal;                                                                     \
105         popl %gs;                                                                  \
106         popl %fs;                                                                  \
107         popl %es;                                                                  \
108         popl %ds;                                                                  \
109         addl $0x8, %esp;                                                           \
110         iret;                                                                      \
111         .data;                                                                     \
112         .long name;                                                                \
113         .long num
114
115 .data
116 .globl trap_tbl
117 trap_tbl:
118
119 /* Generate entry points for the different traps.  Note that all of these bounce
120  * off the corresponding trap.c function, such as handle_irqs, and that the name
121  * e.g. ISR_NMI is soley for the little stup that jumps to something like
122  * _alltraps.
123  *
124  * Technically, these HANDLER entries do not need to be in numeric order.
125  * trap.c will do a 'foreach (up to last-1), set the IDT for the number to point
126  * to the func' in the order in which they appear in the trap tbl, so the 'last
127  * one wins'. */
128 TRAPHANDLER_NOEC(ISR_divide_error, T_DIVIDE)
129 TRAPHANDLER_NOEC(ISR_debug_exceptions, T_DEBUG)
130 TRAPHANDLER_NOEC(ISR_NMI, T_NMI)
131 TRAPHANDLER_NOEC(ISR_breakpoint, T_BRKPT)
132 TRAPHANDLER_NOEC(ISR_overflow, T_OFLOW)
133 TRAPHANDLER_NOEC(ISR_bounds_check, T_BOUND)
134 TRAPHANDLER_NOEC(ISR_invalid_opcode, T_ILLOP)
135 TRAPHANDLER_NOEC(ISR_device_not_available, T_DEVICE)
136 /* supposedly, DF generates an error code, but the one time we've had a DF so
137  * far, it didn't.  eventually, this should probably be handled with a task gate
138  * it might have pushed a 0, but just the rest of the stack was corrupt
139  */
140 TRAPHANDLER_NOEC(ISR_double_fault, T_DBLFLT)
141 /* 9 reserved */
142 TRAPHANDLER(ISR_invalid_TSS, T_TSS)
143 TRAPHANDLER(ISR_segment_not_present, T_SEGNP)
144 TRAPHANDLER(ISR_stack_exception, T_STACK)
145 TRAPHANDLER(ISR_general_protection_fault, T_GPFLT)
146 TRAPHANDLER(ISR_page_fault, T_PGFLT)
147 /* 15 reserved */
148 TRAPHANDLER_NOEC(ISR_floating_point_error, T_FPERR)
149 TRAPHANDLER(ISR_alignment_check, T_ALIGN)
150 TRAPHANDLER_NOEC(ISR_machine_check, T_MCHK)
151 TRAPHANDLER_NOEC(ISR_simd_error, T_SIMDERR)
152 /* 20 - 31 reserved */
153 /* 32-47 are PIC/8259 IRQ vectors */
154 IRQ_HANDLER(IRQ0, 32)
155 IRQ_HANDLER(IRQ1, 33)
156 IRQ_HANDLER(IRQ2, 34)
157 IRQ_HANDLER(IRQ3, 35)
158 IRQ_HANDLER(IRQ4, 36)
159 IRQ_HANDLER(IRQ5, 37)
160 IRQ_HANDLER(IRQ6, 38)
161 IRQ_HANDLER(IRQ7, 39)
162 IRQ_HANDLER(IRQ8, 40)
163 IRQ_HANDLER(IRQ9, 41)
164 IRQ_HANDLER(IRQ10, 42)
165 IRQ_HANDLER(IRQ11, 43)
166 IRQ_HANDLER(IRQ12, 44)
167 IRQ_HANDLER(IRQ13, 45)
168 IRQ_HANDLER(IRQ14, 46)
169 IRQ_HANDLER(IRQ15, 47)
170 TRAPHANDLER_NOEC(ISR_syscall, T_SYSCALL)
171 /* 49-223 are IOAPIC routing vectors (from IOAPIC to LAPIC) */
172 IRQ_HANDLER(IRQ17, 49)
173 IRQ_HANDLER(IRQ18, 50)
174 IRQ_HANDLER(IRQ19, 51)
175 IRQ_HANDLER(IRQ20, 52)
176 IRQ_HANDLER(IRQ21, 53)
177 IRQ_HANDLER(IRQ22, 54)
178 IRQ_HANDLER(IRQ23, 55)
179 IRQ_HANDLER(IRQ24, 56)
180 IRQ_HANDLER(IRQ25, 57)
181 IRQ_HANDLER(IRQ26, 58)
182 IRQ_HANDLER(IRQ27, 59)
183 IRQ_HANDLER(IRQ28, 60)
184 IRQ_HANDLER(IRQ29, 61)
185 IRQ_HANDLER(IRQ30, 62)
186 IRQ_HANDLER(IRQ31, 63)
187 IRQ_HANDLER(IRQ32, 64)
188 IRQ_HANDLER(IRQ33, 65)
189 IRQ_HANDLER(IRQ34, 66)
190 IRQ_HANDLER(IRQ35, 67)
191 IRQ_HANDLER(IRQ36, 68)
192 IRQ_HANDLER(IRQ37, 69)
193 IRQ_HANDLER(IRQ38, 70)
194 IRQ_HANDLER(IRQ39, 71)
195 IRQ_HANDLER(IRQ40, 72)
196 IRQ_HANDLER(IRQ41, 73)
197 IRQ_HANDLER(IRQ42, 74)
198 IRQ_HANDLER(IRQ43, 75)
199 IRQ_HANDLER(IRQ44, 76)
200 IRQ_HANDLER(IRQ45, 77)
201 IRQ_HANDLER(IRQ46, 78)
202 IRQ_HANDLER(IRQ47, 79)
203 IRQ_HANDLER(IRQ48, 80)
204 IRQ_HANDLER(IRQ49, 81)
205 IRQ_HANDLER(IRQ50, 82)
206 IRQ_HANDLER(IRQ51, 83)
207 IRQ_HANDLER(IRQ52, 84)
208 IRQ_HANDLER(IRQ53, 85)
209 IRQ_HANDLER(IRQ54, 86)
210 IRQ_HANDLER(IRQ55, 87)
211 IRQ_HANDLER(IRQ56, 88)
212 IRQ_HANDLER(IRQ57, 89)
213 IRQ_HANDLER(IRQ58, 90)
214 IRQ_HANDLER(IRQ59, 91)
215 IRQ_HANDLER(IRQ60, 92)
216 IRQ_HANDLER(IRQ61, 93)
217 IRQ_HANDLER(IRQ62, 94)
218 IRQ_HANDLER(IRQ63, 95)
219 IRQ_HANDLER(IRQ64, 96)
220 IRQ_HANDLER(IRQ65, 97)
221 IRQ_HANDLER(IRQ66, 98)
222 IRQ_HANDLER(IRQ67, 99)
223 IRQ_HANDLER(IRQ68, 100)
224 IRQ_HANDLER(IRQ69, 101)
225 IRQ_HANDLER(IRQ70, 102)
226 IRQ_HANDLER(IRQ71, 103)
227 IRQ_HANDLER(IRQ72, 104)
228 IRQ_HANDLER(IRQ73, 105)
229 IRQ_HANDLER(IRQ74, 106)
230 IRQ_HANDLER(IRQ75, 107)
231 IRQ_HANDLER(IRQ76, 108)
232 IRQ_HANDLER(IRQ77, 109)
233 IRQ_HANDLER(IRQ78, 110)
234 IRQ_HANDLER(IRQ79, 111)
235 IRQ_HANDLER(IRQ80, 112)
236 IRQ_HANDLER(IRQ81, 113)
237 IRQ_HANDLER(IRQ82, 114)
238 IRQ_HANDLER(IRQ83, 115)
239 IRQ_HANDLER(IRQ84, 116)
240 IRQ_HANDLER(IRQ85, 117)
241 IRQ_HANDLER(IRQ86, 118)
242 IRQ_HANDLER(IRQ87, 119)
243 IRQ_HANDLER(IRQ88, 120)
244 IRQ_HANDLER(IRQ89, 121)
245 IRQ_HANDLER(IRQ90, 122)
246 IRQ_HANDLER(IRQ91, 123)
247 IRQ_HANDLER(IRQ92, 124)
248 IRQ_HANDLER(IRQ93, 125)
249 IRQ_HANDLER(IRQ94, 126)
250 IRQ_HANDLER(IRQ95, 127)
251 IRQ_HANDLER(IRQ96, 128)
252 IRQ_HANDLER(IRQ97, 129)
253 IRQ_HANDLER(IRQ98, 130)
254 IRQ_HANDLER(IRQ99, 131)
255 IRQ_HANDLER(IRQ100, 132)
256 IRQ_HANDLER(IRQ101, 133)
257 IRQ_HANDLER(IRQ102, 134)
258 IRQ_HANDLER(IRQ103, 135)
259 IRQ_HANDLER(IRQ104, 136)
260 IRQ_HANDLER(IRQ105, 137)
261 IRQ_HANDLER(IRQ106, 138)
262 IRQ_HANDLER(IRQ107, 139)
263 IRQ_HANDLER(IRQ108, 140)
264 IRQ_HANDLER(IRQ109, 141)
265 IRQ_HANDLER(IRQ110, 142)
266 IRQ_HANDLER(IRQ111, 143)
267 IRQ_HANDLER(IRQ112, 144)
268 IRQ_HANDLER(IRQ113, 145)
269 IRQ_HANDLER(IRQ114, 146)
270 IRQ_HANDLER(IRQ115, 147)
271 IRQ_HANDLER(IRQ116, 148)
272 IRQ_HANDLER(IRQ117, 149)
273 IRQ_HANDLER(IRQ118, 150)
274 IRQ_HANDLER(IRQ119, 151)
275 IRQ_HANDLER(IRQ120, 152)
276 IRQ_HANDLER(IRQ121, 153)
277 IRQ_HANDLER(IRQ122, 154)
278 IRQ_HANDLER(IRQ123, 155)
279 IRQ_HANDLER(IRQ124, 156)
280 IRQ_HANDLER(IRQ125, 157)
281 IRQ_HANDLER(IRQ126, 158)
282 IRQ_HANDLER(IRQ127, 159)
283 IRQ_HANDLER(IRQ128, 160)
284 IRQ_HANDLER(IRQ129, 161)
285 IRQ_HANDLER(IRQ130, 162)
286 IRQ_HANDLER(IRQ131, 163)
287 IRQ_HANDLER(IRQ132, 164)
288 IRQ_HANDLER(IRQ133, 165)
289 IRQ_HANDLER(IRQ134, 166)
290 IRQ_HANDLER(IRQ135, 167)
291 IRQ_HANDLER(IRQ136, 168)
292 IRQ_HANDLER(IRQ137, 169)
293 IRQ_HANDLER(IRQ138, 170)
294 IRQ_HANDLER(IRQ139, 171)
295 IRQ_HANDLER(IRQ140, 172)
296 IRQ_HANDLER(IRQ141, 173)
297 IRQ_HANDLER(IRQ142, 174)
298 IRQ_HANDLER(IRQ143, 175)
299 IRQ_HANDLER(IRQ144, 176)
300 IRQ_HANDLER(IRQ145, 177)
301 IRQ_HANDLER(IRQ146, 178)
302 IRQ_HANDLER(IRQ147, 179)
303 IRQ_HANDLER(IRQ148, 180)
304 IRQ_HANDLER(IRQ149, 181)
305 IRQ_HANDLER(IRQ150, 182)
306 IRQ_HANDLER(IRQ151, 183)
307 IRQ_HANDLER(IRQ152, 184)
308 IRQ_HANDLER(IRQ153, 185)
309 IRQ_HANDLER(IRQ154, 186)
310 IRQ_HANDLER(IRQ155, 187)
311 IRQ_HANDLER(IRQ156, 188)
312 IRQ_HANDLER(IRQ157, 189)
313 IRQ_HANDLER(IRQ158, 190)
314 IRQ_HANDLER(IRQ159, 191)
315 IRQ_HANDLER(IRQ160, 192)
316 IRQ_HANDLER(IRQ161, 193)
317 IRQ_HANDLER(IRQ162, 194)
318 IRQ_HANDLER(IRQ163, 195)
319 IRQ_HANDLER(IRQ164, 196)
320 IRQ_HANDLER(IRQ165, 197)
321 IRQ_HANDLER(IRQ166, 198)
322 IRQ_HANDLER(IRQ167, 199)
323 IRQ_HANDLER(IRQ168, 200)
324 IRQ_HANDLER(IRQ169, 201)
325 IRQ_HANDLER(IRQ170, 202)
326 IRQ_HANDLER(IRQ171, 203)
327 IRQ_HANDLER(IRQ172, 204)
328 IRQ_HANDLER(IRQ173, 205)
329 IRQ_HANDLER(IRQ174, 206)
330 IRQ_HANDLER(IRQ175, 207)
331 IRQ_HANDLER(IRQ176, 208)
332 IRQ_HANDLER(IRQ177, 209)
333 IRQ_HANDLER(IRQ178, 210)
334 IRQ_HANDLER(IRQ179, 211)
335 IRQ_HANDLER(IRQ180, 212)
336 IRQ_HANDLER(IRQ181, 213)
337 IRQ_HANDLER(IRQ182, 214)
338 IRQ_HANDLER(IRQ183, 215)
339 IRQ_HANDLER(IRQ184, 216)
340 IRQ_HANDLER(IRQ185, 217)
341 IRQ_HANDLER(IRQ186, 218)
342 IRQ_HANDLER(IRQ187, 219)
343 IRQ_HANDLER(IRQ188, 220)
344 IRQ_HANDLER(IRQ189, 221)
345 IRQ_HANDLER(IRQ190, 222)
346 IRQ_HANDLER(IRQ191, 223)
347 /* 224-239 are OS IPI vectors (0xe0-0xef) */
348 IRQ_HANDLER(IRQ192, I_SMP_CALL0)
349 IRQ_HANDLER(IRQ193, I_SMP_CALL1)
350 IRQ_HANDLER(IRQ194, I_SMP_CALL2)
351 IRQ_HANDLER(IRQ195, I_SMP_CALL3)
352 IRQ_HANDLER(IRQ196, I_SMP_CALL4)
353 IRQ_HANDLER(IRQ197, 229)
354 IRQ_HANDLER(IRQ198, 230)
355 IRQ_HANDLER(IRQ199, 231)
356 IRQ_HANDLER(IRQ200, 232)
357 IRQ_HANDLER(IRQ201, 233)
358 IRQ_HANDLER(IRQ202, 234)
359 IRQ_HANDLER(IRQ203, 235)
360 IRQ_HANDLER(IRQ204, 236)
361 IRQ_HANDLER(IRQ205, I_TESTING)
362 POKE_HANDLER(IRQ206, I_POKE_CORE)
363 IRQ_HANDLER(IRQ207, I_KERNEL_MSG)
364 /* 240-255 are LAPIC vectors (0xf0-0xff), hightest priority class */
365 IRQ_HANDLER(IRQ208, 240)
366 IRQ_HANDLER(IRQ209, 241)
367 IRQ_HANDLER(IRQ210, 242)
368 IRQ_HANDLER(IRQ211, 243)
369 IRQ_HANDLER(IRQ212, 244)
370 IRQ_HANDLER(IRQ213, 245)
371 IRQ_HANDLER(IRQ214, 246)
372 IRQ_HANDLER(IRQ215, 247)
373 IRQ_HANDLER(IRQ216, 248)
374 IRQ_HANDLER(IRQ217, 249)
375 IRQ_HANDLER(IRQ218, 250)
376 IRQ_HANDLER(IRQ219, 251)
377 IRQ_HANDLER(IRQ220, 252)
378 IRQ_HANDLER(IRQ221, 253)
379 IRQ_HANDLER(IRQ222, 254)
380 IRQ_HANDLER(IRQ223, 255)
381 /* But make sure default is last!! */
382 TRAPHANDLER_NOEC(ISR_default, T_DEFAULT)
383
384 .data
385 .globl trap_tbl_end
386 trap_tbl_end:
387
388 /* Keep the exit paths of _alltraps, _allirqs, and sysenter_handler in sync
389  * with the corresponding pop_tf's.
390  */
391 .text
392 _alltraps:
393         cld
394         pushl %ds
395         pushl %es
396         pushl %fs
397         pushl %gs
398         pushal
399         movw $0, %ax;
400         movw %ax, %gs;
401         movw %ax, %fs;
402         movw $GD_KD, %ax                # data segments aren't accessible by default
403         movw %ax, %ds
404         movw %ax, %es
405         pushl %esp
406         movl $0, %ebp                   # so we can backtrace to this point
407         call trap
408         popl %esp
409         popal
410         popl %gs
411         popl %fs
412         popl %es
413         popl %ds
414         addl $0x8, %esp                 # skip trapno and err
415         iret
416
417 /* will need to think about when we reenable interrupts.  right now, iret does it,
418  * if the previous EFLAGS had interrupts enabled
419  */
420 _allirqs:
421         cld
422         pushl %ds
423         pushl %es
424         pushl %fs
425         pushl %gs
426         pushal
427         movw $0, %ax;
428         movw %ax, %gs;
429         movw %ax, %fs;
430         movw $GD_KD, %ax                # data segments aren't accessible by default
431         movw %ax, %ds
432         movw %ax, %es
433         pushl %esp
434         movl $0, %ebp                   # so we can backtrace to this point
435         call handle_irq
436         popl %esp
437         popal
438         popl %gs
439         popl %fs
440         popl %es
441         popl %ds
442         addl $0x8, %esp                 # skip IRQ number and err (which is 0)
443         iret
444
445 .globl sysenter_handler;
446 .type sysenter_handler, @function;
447 # All of the pushl zeros are to keep the trap frame looking the same as when we
448 # receive a trap or an interrupt
449 sysenter_handler:
450         cld
451         pushl $0                                # ss
452         pushl $0                                # esp
453         pushfl                                  # eflags
454         pushl $0                                # CS == 0 lets the kernel know it was a sysenter        
455         pushl $0                                # eip
456         pushl $0                                # err 
457         pushl $T_SYSCALL                # helps with print_trapframe
458         pushl %ds
459         pushl %es
460         pushl %fs
461         pushl %gs
462         pushal
463         movw $0, %ax;
464         movw %ax, %gs;
465         movw %ax, %fs;
466         movw $GD_KD, %ax
467         movw %ax, %ds
468         movw %ax, %es
469         pushl %esp
470         movl $0, %ebp                   # so we can backtrace to this point
471         call sysenter_callwrapper
472         popl %esp
473         popal
474         popl %gs
475         popl %fs
476         popl %es
477         popl %ds
478         addl $0x10, %esp                # pop T_SYSCALL and the three zeros
479         popfl                                   # restore EFLAGS (and usually enables interrupts!)
480         movl %ebp, %ecx
481         sti                                             # interrupts are turned off when starting a core
482         sysexit