Massive reorganizing and making all the makefiles consistent.
[akaros.git] / kern / src / trapentry.S
1 /* See COPYRIGHT for copyright information. */
2
3 #include <inc/mmu.h>
4 #include <inc/memlayout.h>
5 #include <inc/trap.h>
6
7
8
9 ###################################################################
10 # exceptions/interrupts
11 ###################################################################
12
13 /* The TRAPHANDLER macro defines a globally-visible function for handling
14  * a trap.  It pushes a trap number onto the stack, then jumps to _alltraps.
15  * It also builds this traps portion of the trap_tbl.
16  * Use TRAPHANDLER for traps where the CPU automatically pushes an error code.
17  */ 
18 #define TRAPHANDLER(name, num)                                                                  \
19         .text;                                                                                                          \
20         .globl name;            /* define global symbol for 'name' */   \
21         .type name, @function;  /* symbol type is function */           \
22         .align 2;               /* align function definition */                         \
23         name:                   /* function starts here */                                      \
24         pushl $(num);                                                                                           \
25         jmp _alltraps;                                                                                          \
26         .data;                                                                                                          \
27         .long name;                                                                                                     \
28         .long num
29
30 /* Use TRAPHANDLER_NOEC for traps where the CPU doesn't push an error code.
31  * It pushes a 0 in place of the error code, so the trap frame has the same
32  * format in either case.
33  */
34 #define TRAPHANDLER_NOEC(name, num)             \
35         .text;                                                          \
36         .globl name;                                            \
37         .type name, @function;                          \
38         .align 2;                                                       \
39         name:                                                           \
40         pushl $0;                                                       \
41         pushl $(num);                                           \
42         jmp _alltraps;                                          \
43         .data;                                                          \
44         .long name;                                                     \
45         .long num
46
47 /* Same as NOEC, but for IRQs instead.  num is the ISR number it is mapped to */
48 #define IRQ_HANDLER(name, num)                  \
49         .text;                                                          \
50         .globl name;                                            \
51         .type name, @function;                          \
52         .align 2;                                                       \
53         name:                                                           \
54         pushl $0;                                                       \
55         pushl $(num);                                           \
56         jmp _allirqs;                                           \
57         .data;                                                          \
58         .long name;                                                     \
59         .long num
60
61 .data
62 .globl trap_tbl
63 trap_tbl:
64
65 /*
66  * Lab 3: Your code here for generating entry points for the different traps.
67  */
68 TRAPHANDLER_NOEC(ISR_divide_error, T_DIVIDE)
69 TRAPHANDLER_NOEC(ISR_debug_exceptions, T_DEBUG)
70 TRAPHANDLER_NOEC(ISR_NMI, T_NMI)
71 TRAPHANDLER_NOEC(ISR_breakpoint, T_BRKPT)
72 TRAPHANDLER_NOEC(ISR_overflow, T_OFLOW)
73 TRAPHANDLER_NOEC(ISR_bounds_check, T_BOUND)
74 TRAPHANDLER_NOEC(ISR_invalid_opcode, T_ILLOP)
75 TRAPHANDLER_NOEC(ISR_device_not_available, T_DEVICE)
76 /* supposedly, DF generates an error code, but the one time we've had a DF so
77  * far, it didn't.  eventually, this should probably be handled with a task gate
78  * it might have pushed a 0, but just the rest of the stack was corrupt
79  */
80 TRAPHANDLER_NOEC(ISR_double_fault, T_DBLFLT)
81 /* 9 reserved */
82 TRAPHANDLER(ISR_invalid_TSS, T_TSS)
83 TRAPHANDLER(ISR_segment_not_present, T_SEGNP)
84 TRAPHANDLER(ISR_stack_exception, T_STACK)
85 TRAPHANDLER(ISR_general_protection_fault, T_GPFLT)
86 TRAPHANDLER(ISR_page_fault, T_PGFLT)
87 /* 15 reserved */
88 TRAPHANDLER_NOEC(ISR_floating_point_error, T_FPERR)
89 TRAPHANDLER(ISR_alignment_check, T_ALIGN)
90 TRAPHANDLER_NOEC(ISR_machine_check, T_MCHK)
91 TRAPHANDLER_NOEC(ISR_simd_error, T_SIMDERR)
92 /* 20 - 31 reserved */
93 IRQ_HANDLER(IRQ0, 32)
94 IRQ_HANDLER(IRQ1, 33)
95 IRQ_HANDLER(IRQ2, 34)
96 IRQ_HANDLER(IRQ3, 35)
97 IRQ_HANDLER(IRQ4, 36)
98 IRQ_HANDLER(IRQ5, 37)
99 IRQ_HANDLER(IRQ6, 38)
100 IRQ_HANDLER(IRQ7, 39)
101 IRQ_HANDLER(IRQ8, 40)
102 IRQ_HANDLER(IRQ9, 41)
103 IRQ_HANDLER(IRQ10, 42)
104 IRQ_HANDLER(IRQ11, 43)
105 IRQ_HANDLER(IRQ12, 44)
106 IRQ_HANDLER(IRQ13, 45)
107 IRQ_HANDLER(IRQ14, 46)
108 IRQ_HANDLER(IRQ15, 47)
109 /* 20 general purpose vectors, for use by the LAPIC.  Can expand later. */
110 IRQ_HANDLER(IRQ203, 235)
111 IRQ_HANDLER(IRQ204, 236)
112 IRQ_HANDLER(IRQ205, 237)
113 IRQ_HANDLER(IRQ206, 238)
114 IRQ_HANDLER(IRQ207, 239)
115 IRQ_HANDLER(IRQ208, 240)
116 IRQ_HANDLER(IRQ209, 241)
117 IRQ_HANDLER(IRQ210, 242)
118 IRQ_HANDLER(IRQ211, 243)
119 IRQ_HANDLER(IRQ212, 244)
120 IRQ_HANDLER(IRQ213, 245)
121 IRQ_HANDLER(IRQ214, 246)
122 IRQ_HANDLER(IRQ215, 247)
123 IRQ_HANDLER(IRQ216, 248)
124 IRQ_HANDLER(IRQ217, 249)
125 IRQ_HANDLER(IRQ218, 250)
126 IRQ_HANDLER(IRQ219, 251)
127 IRQ_HANDLER(IRQ220, 252)
128 IRQ_HANDLER(IRQ221, 253)
129 IRQ_HANDLER(IRQ222, 254)
130 IRQ_HANDLER(IRQ223, 255)
131
132 TRAPHANDLER_NOEC(ISR_syscall, T_SYSCALL)
133 /* Make sure default is last!! */
134 TRAPHANDLER_NOEC(ISR_default, T_DEFAULT)
135
136 .data
137 .globl trap_tbl_end
138 trap_tbl_end:
139
140 .text
141 _alltraps:
142         cld
143         pushl %ds
144         pushl %es
145         pushal
146         movw $GD_KD, %ax                # data segments aren't accessible by default
147         movw %ax, %ds
148         movw %ax, %es
149         pushl %esp
150         movl $0, %ebp                   # so we can backtrace to this point
151         call trap
152         popl %esp
153         popal
154         popl %es
155         popl %ds
156         addl $0x8, %esp                 # skip trapno and err
157         iret
158
159 /* will need to think about when we reenable interrupts.  right now, iret does it,
160  * if the previous EFLAGS had interrupts enabled 
161  */
162 _allirqs:
163         cld
164         pushl %ds
165         pushl %es
166         pushal
167         movw $GD_KD, %ax                # data segments aren't accessible by default
168         movw %ax, %ds
169         movw %ax, %es
170         pushl %esp
171         movl $0, %ebp                   # so we can backtrace to this point
172         call irq_handler
173         popl %esp
174         popal
175         popl %es
176         popl %ds
177         addl $0x8, %esp                 # skip IRQ number and err (which is 0)
178         iret
179
180 .globl sysenter_handler;
181 .type sysenter_handler, @function;
182 sysenter_handler:
183     cld
184         pushl %ds
185     pushl %es
186     pushal
187     movw $GD_KD, %ax
188     movw %ax, %ds
189     movw %ax, %es
190     pushl %esp
191         movl $0, %ebp                   # so we can backtrace to this point
192     call syscall_wrapper
193     popl %esp
194     popal
195     popl %es
196     popl %ds
197     movl %ebp, %ecx
198     movl %esi, %edx
199     sysexit