2 * Trap handling routines
3 * NetWatch system management mode administration console
5 * Copyright (c) 2008 Jacob Potter and Joshua Wise. All rights reserved.
6 * This program is free software; you can redistribute and/or modify it under
7 * the terms found in the file LICENSE in the root of this source tree.
19 #define FAULT_WRAPPER(func_name) asm ( \
20 ".global " #func_name "_wrapper\n" \
21 #func_name "_wrapper:\n" \
25 "call " #func_name "\n" \
26 ); void func_name##_wrapper(void);
28 #define WRAPPER_INSTALL(idt, idt4_value, func_name, int_number) { \
29 *(int *)((void *)idt + (8 * int_number)) = \
30 ((int)func_name##_wrapper & 0xFFFF) | \
32 *(int *)((void *)idt + (8 * int_number) + 4) = \
33 ((int)func_name##_wrapper & 0xFFFF0000) | idt4_value; \
36 /* The 16 bits at offset 4 from the start of an interrupt gate are a
37 * bitfield, according to the Intel spec:
38 * 15 P - Segment Present - set to 1
39 * 14-13 DPL - Descriptor privilege level - set to 11 for trap/int, 00 for IPI
40 * 12-8 01111 for 32-bit trap gates, 01110 for 32-bit interrupt gates
42 * Trap: binary 11101111 0000000, hex EF00.
43 * Interrupt: binary 11101110 0000000, hex EE00.
46 #define INTERRUPT 0xEE00
48 typedef struct trap_t {
58 void die(struct trap_t * trap) {
59 outputf("Error %08x %%eip %08x", trap->error_code, trap->eip);
60 outputf("%%esp %08x %%eflags %08x", trap->esp, trap->eflags);
64 void fault_gp(struct trap_t * trap) {
65 outputf("GENERAL PROTECTION FAULT");
69 void fault_page(struct trap_t * trap) {
70 outputf("PAGE FAULT: %08x", trap->cr2);
73 void fault_divide(struct trap_t * trap) {
74 outputf("DIVISION FAULT");
77 void double_fault(struct trap_t * trap) {
78 outputf("DOUBLE FAULT");
82 FAULT_WRAPPER(fault_gp);
83 FAULT_WRAPPER(fault_page);
84 FAULT_WRAPPER(fault_divide);
85 FAULT_WRAPPER(double_fault);
87 /* pseudo_descriptor and x86_gate structs from 15-410 basis code. */
90 unsigned int filler[2]; //64 bits; or 8 bytes.
93 static struct x86_gate idt[64];
95 struct pseudo_descriptor {
98 unsigned long linear_base;
99 } __attribute__((packed));
101 void traps_install(void) {
103 struct pseudo_descriptor pdesc;
104 pdesc.limit = sizeof(idt) - 1;
105 pdesc.linear_base = v2p(&idt);
107 WRAPPER_INSTALL(idt, TRAP, fault_divide, T_DIVIDE_ERROR);
108 WRAPPER_INSTALL(idt, TRAP, fault_gp, T_GENERAL_PROTECTION);
109 WRAPPER_INSTALL(idt, TRAP, fault_page, T_PAGE_FAULT);
110 WRAPPER_INSTALL(idt, TRAP, double_fault, T_DOUBLE_FAULT);
113 asm volatile("lidt %0" : : "m" (pdesc.limit));