summaryrefslogtreecommitdiff
path: root/arch/x86/machine/trap.c
blob: 4b450ecf9cb176e7cdbc7396cf4d979c295fc68c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * Copyright (c) 2012 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <kern/assert.h>
#include <kern/init.h>
#include <kern/macros.h>
#include <kern/panic.h>
#include <kern/printk.h>
#include <machine/cpu.h>
#include <machine/lapic.h>
#include <machine/pic.h>
#include <machine/trap.h>

/*
 * Type for interrupt service routines and trap handler functions.
 */
typedef void (*trap_isr_fn_t)(void);
typedef void (*trap_handler_fn_t)(struct trap_frame *);

/*
 * Properties of a trap handler.
 */
struct trap_handler {
    trap_handler_fn_t fn;
};

/*
 * Low level interrupt service routines.
 */
void trap_isr_default(void);
void trap_isr_divide_error(void);
void trap_isr_debug(void);
void trap_isr_nmi(void);
void trap_isr_breakpoint(void);
void trap_isr_overflow(void);
void trap_isr_bound_range(void);
void trap_isr_invalid_opcode(void);
void trap_isr_device_not_available(void);
void trap_isr_double_fault(void);
void trap_isr_invalid_tss(void);
void trap_isr_segment_not_present(void);
void trap_isr_stack_segment_fault(void);
void trap_isr_general_protection(void);
void trap_isr_page_fault(void);
void trap_isr_math_fault(void);
void trap_isr_alignment_check(void);
void trap_isr_machine_check(void);
void trap_isr_simd_fp_exception(void);
void trap_isr_pic_int7(void);
void trap_isr_pic_int15(void);
void trap_isr_lapic_timer(void);
void trap_isr_lapic_error(void);
void trap_isr_lapic_spurious(void);

/*
 * Array of trap handlers.
 *
 * Entries in this table match the content of the IDT.
 */
static struct trap_handler trap_handlers[CPU_IDT_SIZE];

static void __init
trap_install(unsigned int vector, trap_isr_fn_t isr, trap_handler_fn_t fn)
{
    assert(vector < ARRAY_SIZE(trap_handlers));

    trap_handlers[vector].fn = fn;
    cpu_idt_set_gate(vector, isr);
}

static void
trap_default(struct trap_frame *frame)
{
    printk("trap: unhandled interrupt or exception:\n");
    trap_frame_show(frame);

    cpu_intr_disable();

    for (;;)
        cpu_idle();
}

void __init
trap_setup(void)
{
    size_t i;

    for (i = 0; i < ARRAY_SIZE(trap_handlers); i++)
        trap_install(i, trap_isr_default, trap_default);

    /* Architecture defined traps */
    trap_install(TRAP_DE, trap_isr_divide_error, trap_default);
    trap_install(TRAP_DB, trap_isr_debug, trap_default);
    trap_install(TRAP_NMI, trap_isr_nmi, trap_default);
    trap_install(TRAP_BP, trap_isr_breakpoint, trap_default);
    trap_install(TRAP_OF, trap_isr_overflow, trap_default);
    trap_install(TRAP_BR, trap_isr_bound_range, trap_default);
    trap_install(TRAP_UD, trap_isr_invalid_opcode, trap_default);
    trap_install(TRAP_NM, trap_isr_device_not_available, trap_default);
    trap_install(TRAP_DF, trap_isr_double_fault, trap_default);
    trap_install(TRAP_TS, trap_isr_invalid_tss, trap_default);
    trap_install(TRAP_NP, trap_isr_segment_not_present, trap_default);
    trap_install(TRAP_SS, trap_isr_stack_segment_fault, trap_default);
    trap_install(TRAP_GP, trap_isr_general_protection, trap_default);
    trap_install(TRAP_PF, trap_isr_page_fault, trap_default);
    trap_install(TRAP_MF, trap_isr_math_fault, trap_default);
    trap_install(TRAP_AC, trap_isr_alignment_check, trap_default);
    trap_install(TRAP_MC, trap_isr_machine_check, trap_default);
    trap_install(TRAP_XM, trap_isr_simd_fp_exception, trap_default);

    /* Basic PIC support */
    trap_install(TRAP_PIC_BASE + 7, trap_isr_pic_int7, pic_intr_spurious);
    trap_install(TRAP_PIC_BASE + 15, trap_isr_pic_int15, pic_intr_spurious);

    /* System defined traps */
    trap_install(TRAP_LAPIC_TIMER, trap_isr_lapic_timer, lapic_intr_timer);
    trap_install(TRAP_LAPIC_ERROR, trap_isr_lapic_error, lapic_intr_error);
    trap_install(TRAP_LAPIC_SPURIOUS, trap_isr_lapic_spurious,
                 lapic_intr_spurious);
}

void
trap_main(struct trap_frame *frame)
{
    if (frame->vector < ARRAY_SIZE(trap_handlers))
        trap_handlers[frame->vector].fn(frame);
    else
        trap_default(frame);
}

#ifdef __LP64__

void
trap_frame_show(struct trap_frame *frame)
{
    printk("trap:    rax: %#018lx\n", frame->rax);
    printk("trap:    rbx: %#018lx\n", frame->rbx);
    printk("trap:    rcx: %#018lx\n", frame->rcx);
    printk("trap:    rdx: %#018lx\n", frame->rdx);
    printk("trap:    rbp: %#018lx\n", frame->rbp);
    printk("trap:    rsi: %#018lx\n", frame->rsi);
    printk("trap:    rdi: %#018lx\n", frame->rdi);
    printk("trap:     r8: %#018lx\n", frame->r8);
    printk("trap:     r9: %#018lx\n", frame->r9);
    printk("trap:    r10: %#018lx\n", frame->r10);
    printk("trap:    r11: %#018lx\n", frame->r11);
    printk("trap:    r12: %#018lx\n", frame->r12);
    printk("trap:    r13: %#018lx\n", frame->r13);
    printk("trap:    r14: %#018lx\n", frame->r14);
    printk("trap:    r15: %#018lx\n", frame->r15);
    printk("trap: vector: %lu\n", frame->vector);
    printk("trap:  error: %#018lx\n", frame->error);
    printk("trap:    rip: %#018lx\n", frame->rip);
    printk("trap:     cs: %#018lx\n", frame->cs);
    printk("trap: rflags: %#018lx\n", frame->rflags);
    printk("trap:    rsp: %#018lx\n", frame->rsp);
    printk("trap:     ss: %#018lx\n", frame->ss);
}

#else /* __LP64__ */

void
trap_frame_show(struct trap_frame *frame)
{
    printk("trap:    eax: %#010lx\n", frame->eax);
    printk("trap:    ebx: %#010lx\n", frame->ebx);
    printk("trap:    ecx: %#010lx\n", frame->ecx);
    printk("trap:    edx: %#010lx\n", frame->edx);
    printk("trap:    ebp: %#010lx\n", frame->ebp);
    printk("trap:    esi: %#010lx\n", frame->esi);
    printk("trap:    edi: %#010lx\n", frame->edi);
    printk("trap:     ds: %#010lx\n", frame->ds);
    printk("trap:     es: %#010lx\n", frame->es);
    printk("trap:     fs: %#010lx\n", frame->fs);
    printk("trap:     gs: %#010lx\n", frame->gs);
    printk("trap: vector: %lu\n", frame->vector);
    printk("trap:  error: %#010lx\n", frame->error);
    printk("trap:    eip: %#010lx\n", frame->eip);
    printk("trap:     cs: %#010lx\n", frame->cs);
    printk("trap: eflags: %#010lx\n", frame->eflags);

    if (frame->cs & CPU_PL_USER) {
        printk("trap:    esp: %#010lx\n", frame->esp);
        printk("trap:     ss: %#010lx\n", frame->ss);
    }
}

#endif /* __LP64__ */