blob: f6bb8c9ccfa2d570cc2d7e897c02945863eee0d0 [file] [log] [blame]
Gabe Blacka7991512013-12-07 04:25:01 -08001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright 2013 Google Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
Gabe Black20cdb242013-12-11 00:23:15 -080030#include <arch/exception.h>
Gabe Blacka7991512013-12-07 04:25:01 -080031#include <exception.h>
32#include <libpayload.h>
33#include <stdint.h>
34
Julius Werner092cac52014-05-15 19:41:52 -070035u32 exception_stack[0x400] __attribute__((aligned(8)));
Gabe Blacka7991512013-12-07 04:25:01 -080036
Julius Werner092cac52014-05-15 19:41:52 -070037static exception_hook hook;
38static const char *names[EXC_COUNT] = {
39 [EXC_DE] = "Divide by Zero",
40 [EXC_DB] = "Debug",
41 [EXC_NMI] = "Non-Maskable-Interrupt",
42 [EXC_BP] = "Breakpoint",
43 [EXC_OF] = "Overflow",
44 [EXC_BR] = "Bound Range",
45 [EXC_UD] = "Invalid Opcode",
46 [EXC_NM] = "Device Not Available",
47 [EXC_DF] = "Double Fault",
48 [EXC_TS] = "Invalid TSS",
49 [EXC_NP] = "Segment Not Present",
50 [EXC_SS] = "Stack Fault",
51 [EXC_GP] = "General Protection Fault",
52 [EXC_PF] = "Page Fault",
53 [EXC_MF] = "x87 Floating Point",
54 [EXC_AC] = "Alignment Check",
55 [EXC_MC] = "Machine Check",
56 [EXC_XF] = "SIMD Floating Point",
57 [EXC_SX] = "Security",
Gabe Blacka7991512013-12-07 04:25:01 -080058};
59
60static void print_segment_error_code(u32 code)
61{
62 printf("%#x - descriptor %#x in the ", code, (code >> 3) & 0x1f);
63 if (code & (0x1 << 1)) {
64 printf("IDT");
65 } else {
66 if (code & 0x04)
67 printf("LDT");
68 else
69 printf("GDT");
70 }
71 if (code & (0x1 << 0))
72 printf(", external to the CPU");
73 else
74 printf(", internal to the CPU");
75}
76
77static void print_page_fault_error_code(u32 code)
78{
79 printf("%#x -", code);
80 if (code & (0x1 << 0))
81 printf(" page protection");
82 else
83 printf(" page not present");
84 if (code & (0x1 << 1))
85 printf(", write");
86 else
87 printf(", read");
88 if (code & (0x1 << 2))
89 printf(", user");
90 else
91 printf(", supervisor");
92 if (code & (0x1 << 3))
93 printf(", reserved bits set");
94 if (code & (0x1 << 4))
95 printf(", instruction fetch");
96}
97
98static void print_raw_error_code(u32 code)
99{
100 printf("%#x", code);
101}
102
Gabe Blacka7991512013-12-07 04:25:01 -0800103static void dump_stack(uintptr_t addr, size_t bytes)
104{
105 int i, j;
106 const int line = 8;
107 uint32_t *ptr = (uint32_t *)(addr & ~(line * sizeof(*ptr) - 1));
108
109 printf("Dumping stack:\n");
110 for (i = bytes / sizeof(*ptr); i >= 0; i -= line) {
111 printf("%p: ", ptr + i);
112 for (j = i; j < i + line; j++)
113 printf("%08x ", *(ptr + j));
114 printf("\n");
115 }
116}
117
Julius Werner092cac52014-05-15 19:41:52 -0700118static void dump_exception_state(void)
Gabe Blacka7991512013-12-07 04:25:01 -0800119{
Julius Werner092cac52014-05-15 19:41:52 -0700120 printf("%s Exception\n", names[exception_state->vector]);
Patrick Georgibcf07c32014-12-29 19:34:56 +0100121
Julius Werner092cac52014-05-15 19:41:52 -0700122 printf("Error code: ");
123 switch (exception_state->vector) {
124 case EXC_PF:
125 print_page_fault_error_code(exception_state->error_code);
126 break;
127 case EXC_TS:
128 case EXC_NP:
129 case EXC_SS:
130 case EXC_GP:
131 print_segment_error_code(exception_state->error_code);
132 break;
133 case EXC_DF:
134 case EXC_AC:
135 case EXC_SX:
136 print_raw_error_code(exception_state->error_code);
137 break;
138 default:
139 printf("n/a");
140 break;
Gabe Blacka7991512013-12-07 04:25:01 -0800141 }
Julius Werner092cac52014-05-15 19:41:52 -0700142 printf("\n");
143 printf("EIP: 0x%08x\n", exception_state->regs.eip);
144 printf("CS: 0x%04x\n", exception_state->regs.cs);
145 printf("EFLAGS: 0x%08x\n", exception_state->regs.eflags);
146 printf("EAX: 0x%08x\n", exception_state->regs.eax);
147 printf("ECX: 0x%08x\n", exception_state->regs.ecx);
148 printf("EDX: 0x%08x\n", exception_state->regs.edx);
149 printf("EBX: 0x%08x\n", exception_state->regs.ebx);
150 printf("ESP: 0x%08x\n", exception_state->regs.esp);
151 printf("EBP: 0x%08x\n", exception_state->regs.ebp);
152 printf("ESI: 0x%08x\n", exception_state->regs.esi);
153 printf("EDI: 0x%08x\n", exception_state->regs.edi);
154 printf("DS: 0x%04x\n", exception_state->regs.ds);
155 printf("ES: 0x%04x\n", exception_state->regs.es);
156 printf("SS: 0x%04x\n", exception_state->regs.ss);
157 printf("FS: 0x%04x\n", exception_state->regs.fs);
158 printf("GS: 0x%04x\n", exception_state->regs.gs);
Gabe Blacka7991512013-12-07 04:25:01 -0800159}
160
Gabe Black20cdb242013-12-11 00:23:15 -0800161void exception_dispatch(void)
162{
Julius Werner092cac52014-05-15 19:41:52 -0700163 u32 vec = exception_state->vector;
164 die_if(vec >= EXC_COUNT || !names[vec], "Bad exception vector %u", vec);
Gabe Black20cdb242013-12-11 00:23:15 -0800165
Julius Werner092cac52014-05-15 19:41:52 -0700166 if (hook && hook(vec))
167 return;
Gabe Black20cdb242013-12-11 00:23:15 -0800168
Julius Werner092cac52014-05-15 19:41:52 -0700169 dump_exception_state();
170 dump_stack(exception_state->regs.esp, 512);
171 halt();
Gabe Black20cdb242013-12-11 00:23:15 -0800172}
173
Gabe Blacka7991512013-12-07 04:25:01 -0800174void exception_init(void)
175{
Julius Werner092cac52014-05-15 19:41:52 -0700176 exception_stack_end = exception_stack + ARRAY_SIZE(exception_stack);
Gabe Blacka7991512013-12-07 04:25:01 -0800177 exception_init_asm();
178}
Gabe Black20cdb242013-12-11 00:23:15 -0800179
Julius Werner092cac52014-05-15 19:41:52 -0700180void exception_install_hook(exception_hook h)
Gabe Black20cdb242013-12-11 00:23:15 -0800181{
Julius Werner092cac52014-05-15 19:41:52 -0700182 die_if(hook, "Implement support for a list of hooks if you need it.");
183 hook = h;
Gabe Black20cdb242013-12-11 00:23:15 -0800184}