4 #include <inc/assert.h>
9 #include <kern/console.h>
10 #include <kern/syscall.h>
11 #include <kern/monitor.h>
12 #include <kern/sched.h>
13 #include <kern/kclock.h>
14 #include <kern/picirq.h>
16 u_int page_fault_mode
= PFM_NONE
;
17 static struct Taskstate ts
;
19 /* Interrupt descriptor table. (Must be built at run time because
20 * shifted function addresses can't be represented in relocation records.)
22 struct Gatedesc idt
[256] = { {0}, };
23 struct Pseudodesc idt_pd
=
25 0, sizeof(idt
) - 1, (unsigned long) idt
,
29 static const char *trapname(int trapno
)
31 static const char *excnames
[] = {
34 "Non-Maskable Interrupt",
37 "BOUND Range Exceeded",
39 "Device Not Available",
41 "Coprocessor Segment Overrun",
43 "Segment Not Present",
48 "x87 FPU Floating-Point Error",
51 "SIMD Floating-Point Exception"
54 if (trapno
< sizeof(excnames
)/sizeof(excnames
[0]))
55 return excnames
[trapno
];
56 if (trapno
== T_SYSCALL
)
59 return "(unknown trap)";
66 extern struct Segdesc gdt
[];
68 // Setup a TSS so that we get the right stack
69 // when we trap to the kernel.
70 ts
.ts_esp0
= KSTACKTOP
;
73 // Love to put this code in the initialization of gdt,
74 // but the compiler generates an error incorrectly.
75 gdt
[GD_TSS
>> 3] = SEG16(STS_T32A
, (u_long
) (&ts
),
76 sizeof(struct Taskstate
), 0);
77 gdt
[GD_TSS
>> 3].sd_s
= 0;
83 asm volatile("lidt idt_pd+2");
88 print_trapframe(struct Trapframe
*tf
)
90 printf("TRAP frame at %p\n", tf
);
91 printf(" edi 0x%08x\n", tf
->tf_edi
);
92 printf(" esi 0x%08x\n", tf
->tf_esi
);
93 printf(" ebp 0x%08x\n", tf
->tf_ebp
);
94 printf(" oesp 0x%08x\n", tf
->tf_oesp
);
95 printf(" ebx 0x%08x\n", tf
->tf_ebx
);
96 printf(" edx 0x%08x\n", tf
->tf_edx
);
97 printf(" ecx 0x%08x\n", tf
->tf_ecx
);
98 printf(" eax 0x%08x\n", tf
->tf_eax
);
99 printf(" es 0x----%04x\n", tf
->tf_es
);
100 printf(" ds 0x----%04x\n", tf
->tf_ds
);
101 printf(" trap 0x%08x %s\n", tf
->tf_trapno
, trapname(tf
->tf_trapno
));
102 printf(" err 0x%08x\n", tf
->tf_err
);
103 printf(" eip 0x%08x\n", tf
->tf_eip
);
104 printf(" cs 0x----%04x\n", tf
->tf_cs
);
105 printf(" flag 0x%08x\n", tf
->tf_eflags
);
106 printf(" esp 0x%08x\n", tf
->tf_esp
);
107 printf(" ss 0x----%04x\n", tf
->tf_ss
);
111 trap(struct Trapframe
*tf
)
113 // print_trapframe(tf);
115 // Handle processor exceptions
118 // Handle external interrupts
119 if (tf
->tf_trapno
== IRQ_OFFSET
+0) {
120 // irq 0 -- clock interrupt
123 if (tf
->tf_trapno
== IRQ_OFFSET
+4) {
127 if (IRQ_OFFSET
<= tf
->tf_trapno
128 && tf
->tf_trapno
< IRQ_OFFSET
+MAX_IRQS
) {
129 // just ingore spurious interrupts
130 printf("spurious interrupt on irq %d\n",
131 tf
->tf_trapno
- IRQ_OFFSET
);
136 // the user process or the kernel has a bug.
138 if (tf
->tf_cs
== GD_KT
)
139 panic("unhandled trap in kernel");
148 page_fault_handler(struct Trapframe
*tf
)
152 // Read processor's CR2 register to find the faulting address
156 // User-mode exception - destroy the environment.
157 printf("[%08x] user fault va %08x ip %08x\n",
158 curenv
->env_id
, fault_va
, tf
->tf_eip
);