1 #include "trap.h"
2 #include "gate.h"
3 #include <common/kprint.h>
4 #include <debug/traceback/traceback.h>
5 #include <process/process.h>
6 #include <process/ptrace.h>
7 #include <sched/sched.h>
8 
9 extern void ignore_int();
10 
11 // 0 #DE 除法错误
do_divide_error(struct pt_regs * regs,unsigned long error_code)12 void do_divide_error(struct pt_regs *regs, unsigned long error_code)
13 {
14     // kerror("do_divide_error(0)");
15     kerror("do_divide_error(0),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\t pid=%d\n", error_code,
16            regs->rsp, regs->rip, proc_current_cpu_id, current_pcb->pid);
17     traceback(regs);
18     current_pcb->state = PROC_STOPPED;
19     sched();
20 }
21 
22 // 1 #DB 调试异常
do_debug(struct pt_regs * regs,unsigned long error_code)23 void do_debug(struct pt_regs *regs, unsigned long error_code)
24 {
25     printk("[ ");
26     printk_color(RED, BLACK, "ERROR / TRAP");
27     printk(" ] do_debug(1),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d, pid:%d\n", error_code, regs->rsp, regs->rip,
28            proc_current_cpu_id, current_pcb->pid);
29 
30     while (1)
31         hlt();
32 }
33 
34 // 2 不可屏蔽中断
do_nmi(struct pt_regs * regs,unsigned long error_code)35 void do_nmi(struct pt_regs *regs, unsigned long error_code)
36 {
37 
38     printk("[ ");
39     printk_color(BLUE, BLACK, "INT");
40     printk(" ] do_nmi(2),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip,
41            proc_current_cpu_id);
42 
43     while (1)
44         hlt();
45 }
46 
47 // 3 #BP 断点异常
do_int3(struct pt_regs * regs,unsigned long error_code)48 void do_int3(struct pt_regs *regs, unsigned long error_code)
49 {
50 
51     printk("[ ");
52     printk_color(YELLOW, BLACK, "TRAP");
53     printk(" ] do_int3(3),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip,
54            proc_current_cpu_id);
55 
56     while (1)
57         hlt();
58 }
59 
60 // 4 #OF 溢出异常
do_overflow(struct pt_regs * regs,unsigned long error_code)61 void do_overflow(struct pt_regs *regs, unsigned long error_code)
62 {
63 
64     printk("[ ");
65     printk_color(YELLOW, BLACK, "TRAP");
66     printk(" ] do_overflow(4),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
67            regs->rip, proc_current_cpu_id);
68 
69     current_pcb->state = PROC_STOPPED;
70     sched();
71 }
72 
73 // 5 #BR 越界异常
do_bounds(struct pt_regs * regs,unsigned long error_code)74 void do_bounds(struct pt_regs *regs, unsigned long error_code)
75 {
76 
77     kerror("do_bounds(5),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp, regs->rip,
78            proc_current_cpu_id);
79 
80     while (1)
81         hlt();
82 }
83 
84 // 6 #UD 无效/未定义的机器码
do_undefined_opcode(struct pt_regs * regs,unsigned long error_code)85 void do_undefined_opcode(struct pt_regs *regs, unsigned long error_code)
86 {
87 
88     kerror("do_undefined_opcode(6),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d, pid:%ld", error_code,
89            regs->rsp, regs->rip, proc_current_cpu_id, current_pcb->pid);
90     traceback(regs);
91     current_pcb->state = PROC_STOPPED;
92     sched();
93 }
94 
95 // 7 #NM 设备异常(FPU不存在)
do_dev_not_avaliable(struct pt_regs * regs,unsigned long error_code)96 void do_dev_not_avaliable(struct pt_regs *regs, unsigned long error_code)
97 {
98 
99     kerror("do_dev_not_avaliable(7),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d, pid=%d\n", error_code, regs->rsp,
100            regs->rip, proc_current_cpu_id, current_pcb->pid);
101 
102     current_pcb->state = PROC_STOPPED;
103     sched();
104 }
105 
106 // 8 #DF 双重错误
do_double_fault(struct pt_regs * regs,unsigned long error_code)107 void do_double_fault(struct pt_regs *regs, unsigned long error_code)
108 {
109 
110     printk("[ ");
111     printk_color(RED, BLACK, "Terminate");
112     printk(" ] do_double_fault(8),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
113            regs->rip, proc_current_cpu_id);
114     traceback(regs);
115     current_pcb->state = PROC_STOPPED;
116     sched();
117 }
118 
119 // 9 协处理器越界(保留)
do_coprocessor_segment_overrun(struct pt_regs * regs,unsigned long error_code)120 void do_coprocessor_segment_overrun(struct pt_regs *regs, unsigned long error_code)
121 {
122 
123     kerror("do_coprocessor_segment_overrun(9),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code,
124            regs->rsp, regs->rip, proc_current_cpu_id);
125 
126     current_pcb->state = PROC_STOPPED;
127     sched();
128 }
129 
130 // 10 #TS 无效的TSS段
do_invalid_TSS(struct pt_regs * regs,unsigned long error_code)131 void do_invalid_TSS(struct pt_regs *regs, unsigned long error_code)
132 {
133 
134     printk("[");
135     printk_color(RED, BLACK, "ERROR");
136     printk("] do_invalid_TSS(10),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
137            regs->rip, proc_current_cpu_id);
138 
139     printk_color(YELLOW, BLACK, "Information:\n");
140     // 解析错误码
141     if (error_code & 0x01)
142         printk("The exception occurred during delivery of an event external to the program.\n");
143 
144     if (error_code & 0x02)
145         printk("Refers to a descriptor in the IDT.\n");
146     else
147     {
148         if (error_code & 0x04)
149             printk("Refers to a descriptor in the current LDT.\n");
150         else
151             printk("Refers to a descriptor in the GDT.\n");
152     }
153 
154     printk("Segment Selector Index:%10x\n", error_code & 0xfff8);
155 
156     printk("\n");
157 
158     current_pcb->state = PROC_STOPPED;
159     sched();
160 }
161 
162 // 11 #NP 段不存在
do_segment_not_exists(struct pt_regs * regs,unsigned long error_code)163 void do_segment_not_exists(struct pt_regs *regs, unsigned long error_code)
164 {
165 
166     kerror("do_segment_not_exists(11),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
167            regs->rip, proc_current_cpu_id);
168 
169     current_pcb->state = PROC_STOPPED;
170     sched();
171 }
172 
173 // 12 #SS SS段错误
do_stack_segment_fault(struct pt_regs * regs,unsigned long error_code)174 void do_stack_segment_fault(struct pt_regs *regs, unsigned long error_code)
175 {
176 
177     kerror("do_stack_segment_fault(12),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
178            regs->rip, proc_current_cpu_id);
179     // kinfo("cs=%#04x, ds=%#04x, ss=%#04x", regs->cs, regs->ds, regs->ss);
180     traceback(regs);
181     current_pcb->state = PROC_STOPPED;
182     sched();
183 }
184 
185 // 13 #GP 通用保护性异常
do_general_protection(struct pt_regs * regs,unsigned long error_code)186 void do_general_protection(struct pt_regs *regs, unsigned long error_code)
187 {
188 
189     kerror("do_general_protection(13),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\tpid=%ld\n", error_code,
190            regs->rsp, regs->rip, proc_current_cpu_id, current_pcb->pid);
191     if (error_code & 0x01)
192         printk_color(RED, BLACK,
193                      "The exception occurred during delivery of an event external to the program,such as an interrupt "
194                      "or an earlier exception.\n");
195 
196     if (error_code & 0x02)
197         printk_color(RED, BLACK, "Refers to a gate descriptor in the IDT;\n");
198     else
199         printk_color(RED, BLACK, "Refers to a descriptor in the GDT or the current LDT;\n");
200 
201     if ((error_code & 0x02) == 0)
202         if (error_code & 0x04)
203             printk_color(RED, BLACK, "Refers to a segment or gate descriptor in the LDT;\n");
204         else
205             printk_color(RED, BLACK, "Refers to a descriptor in the current GDT;\n");
206 
207     printk_color(RED, BLACK, "Segment Selector Index:%#010x\n", error_code & 0xfff8);
208     traceback(regs);
209     current_pcb->state = PROC_STOPPED;
210     sched();
211 }
212 
213 // 14 #PF 页故障
do_page_fault(struct pt_regs * regs,unsigned long error_code)214 void do_page_fault(struct pt_regs *regs, unsigned long error_code)
215 {
216 
217     unsigned long cr2 = 0;
218 
219     __asm__ __volatile__("movq	%%cr2,	%0" : "=r"(cr2)::"memory");
220 
221     kerror("do_page_fault(14),Error code :%#018lx,RSP:%#018lx, RBP=%#018lx, RIP:%#018lx CPU:%d, pid=%d\n", error_code,
222            regs->rsp, regs->rbp, regs->rip, proc_current_cpu_id, current_pcb->pid);
223     kerror("regs->rax = %#018lx\n", regs->rax);
224     if (!(error_code & 0x01))
225         printk_color(RED, BLACK, "Page Not-Present,\t");
226 
227     if (error_code & 0x02)
228         printk_color(RED, BLACK, "Write Cause Fault,\t");
229     else
230         printk_color(RED, BLACK, "Read Cause Fault,\t");
231 
232     if (error_code & 0x04)
233         printk_color(RED, BLACK, "Fault in user(3)\t");
234     else
235         printk_color(RED, BLACK, "Fault in supervisor(0,1,2)\t");
236 
237     if (error_code & 0x08)
238         printk_color(RED, BLACK, ",Reserved Bit Cause Fault\t");
239 
240     if (error_code & 0x10)
241         printk_color(RED, BLACK, ",Instruction fetch Cause Fault");
242 
243     printk_color(RED, BLACK, "\n");
244 
245     printk_color(RED, BLACK, "CR2:%#018lx\n", cr2);
246 
247     traceback(regs);
248     process_do_exit(-1);
249     // current_pcb->state = PROC_STOPPED;
250     // sched();
251 }
252 
253 // 15 Intel保留,请勿使用
254 
255 // 16 #MF x87FPU错误
do_x87_FPU_error(struct pt_regs * regs,unsigned long error_code)256 void do_x87_FPU_error(struct pt_regs *regs, unsigned long error_code)
257 {
258 
259     kerror("do_x87_FPU_error(16),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
260            regs->rip, proc_current_cpu_id);
261 
262     while (1)
263         hlt();
264 }
265 
266 // 17 #AC 对齐检测
do_alignment_check(struct pt_regs * regs,unsigned long error_code)267 void do_alignment_check(struct pt_regs *regs, unsigned long error_code)
268 {
269 
270     kerror("do_alignment_check(17),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
271            regs->rip, proc_current_cpu_id);
272 
273     current_pcb->state = PROC_STOPPED;
274     sched();
275 }
276 
277 // 18 #MC 机器检测
do_machine_check(struct pt_regs * regs,unsigned long error_code)278 void do_machine_check(struct pt_regs *regs, unsigned long error_code)
279 {
280 
281     kerror("do_machine_check(18),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
282            regs->rip, proc_current_cpu_id);
283 
284     current_pcb->state = PROC_STOPPED;
285     sched();
286 }
287 
288 // 19 #XM SIMD浮点异常
do_SIMD_exception(struct pt_regs * regs,unsigned long error_code)289 void do_SIMD_exception(struct pt_regs *regs, unsigned long error_code)
290 {
291 
292     kerror("do_SIMD_exception(19),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code, regs->rsp,
293            regs->rip, proc_current_cpu_id);
294 
295     current_pcb->state = PROC_STOPPED;
296     sched();
297 }
298 
299 // 20 #VE 虚拟化异常
do_virtualization_exception(struct pt_regs * regs,unsigned long error_code)300 void do_virtualization_exception(struct pt_regs *regs, unsigned long error_code)
301 {
302 
303     kerror("do_virtualization_exception(20),\tError Code:%#18lx,\tRSP:%#18lx,\tRIP:%#18lx\t CPU:%d\n", error_code,
304            regs->rsp, regs->rip, proc_current_cpu_id);
305 
306     current_pcb->state = PROC_STOPPED;
307     sched();
308 }
309 
310 // 21-21 Intel保留,请勿使用
311 
312 /**
313  * @brief 当系统收到未知的中断时,执行此处理函数
314  *
315  * @param regs
316  * @param error_code
317  */
ignore_int_handler(struct pt_regs * regs,unsigned long error_code)318 void ignore_int_handler(struct pt_regs *regs, unsigned long error_code)
319 {
320     kwarn("Unknown interrupt or fault at RIP.\n");
321 }
322 
sys_vector_init()323 void sys_vector_init()
324 {
325     // 将idt重置为新的ignore_int入点(此前在head.S中有设置,
326     // 但是那个不完整,某些版本的编译器的输出,在真机运行时会破坏进程执行环境,从而导致#GP
327     for (int i = 0; i < 256; ++i)
328         set_intr_gate(i, 0, ignore_int);
329 
330     set_trap_gate(0, 0, divide_error);
331     set_trap_gate(1, 0, debug);
332     set_intr_gate(2, 0, nmi);
333     set_system_trap_gate(3, 0, int3);
334     set_system_trap_gate(4, 0, overflow);
335     set_system_trap_gate(5, 0, bounds);
336     set_trap_gate(6, 0, undefined_opcode);
337     set_trap_gate(7, 0, dev_not_avaliable);
338     set_trap_gate(8, 0, double_fault);
339     set_trap_gate(9, 0, coprocessor_segment_overrun);
340     set_trap_gate(10, 0, invalid_TSS);
341     set_trap_gate(11, 0, segment_not_exists);
342     set_trap_gate(12, 0, stack_segment_fault);
343     set_trap_gate(13, 0, general_protection);
344     set_trap_gate(14, 0, page_fault);
345     // 中断号15由Intel保留,不能使用
346     set_trap_gate(16, 0, x87_FPU_error);
347     set_trap_gate(17, 0, alignment_check);
348     set_trap_gate(18, 0, machine_check);
349     set_trap_gate(19, 0, SIMD_exception);
350     set_trap_gate(20, 0, virtualization_exception);
351     // 中断号21-31由Intel保留,不能使用
352 
353     // 32-255为用户自定义中断内部
354 }