blob: 700eb84cd0d4d275f46fcd81d96296248b49565d [file] [log] [blame]
Martin Roth9df9e9392016-01-12 15:55:28 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 2 of the License.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
Subrata Banik8cfd76d2018-04-18 11:48:58 +053014#include <arch/cpu.h>
Aaron Durbin4b032e42018-04-20 01:39:30 -060015#include <arch/early_variables.h>
16#include <arch/exception.h>
17#include <commonlib/helpers.h>
Eric Biederman432aa6a2004-10-30 22:59:35 +000018#include <console/console.h>
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +030019#include <console/streams.h>
Aaron Durbinaae73d72018-04-21 00:10:10 -060020#include <cpu/x86/cr.h>
Subrata Banik8cfd76d2018-04-18 11:48:58 +053021#include <cpu/x86/lapic.h>
Aaron Durbin4b032e42018-04-20 01:39:30 -060022#include <stdint.h>
Eric Biederman432aa6a2004-10-30 22:59:35 +000023#include <string.h>
24
Martin Roth898a7752017-06-01 11:39:59 -060025#if IS_ENABLED(CONFIG_GDB_STUB)
Eric Biederman432aa6a2004-10-30 22:59:35 +000026
27/* BUFMAX defines the maximum number of characters in inbound/outbound buffers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000028 * At least NUM_REGBYTES*2 are needed for register packets
Eric Biederman432aa6a2004-10-30 22:59:35 +000029 */
30#define BUFMAX 400
31enum regnames {
32 EAX = 0, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
33 PC /* also known as eip */,
34 PS /* also known as eflags */,
35 CS, SS, DS, ES, FS, GS,
36 NUM_REGS /* Number of registers. */
37};
38
39static uint32_t gdb_stub_registers[NUM_REGS];
40
41#define GDB_SIG0 0 /* Signal 0 */
42#define GDB_SIGHUP 1 /* Hangup */
43#define GDB_SIGINT 2 /* Interrupt */
44#define GDB_SIGQUIT 3 /* Quit */
45#define GDB_SIGILL 4 /* Illegal instruction */
46#define GDB_SIGTRAP 5 /* Trace/breakpoint trap */
47#define GDB_SIGABRT 6 /* Aborted */
48#define GDB_SIGEMT 7 /* Emulation trap */
49#define GDB_SIGFPE 8 /* Arithmetic exception */
50#define GDB_SIGKILL 9 /* Killed */
51#define GDB_SIGBUS 10 /* Bus error */
52#define GDB_SIGSEGV 11 /* Segmentation fault */
53#define GDB_SIGSYS 12 /* Bad system call */
54#define GDB_SIGPIPE 13 /* Broken pipe */
55#define GDB_SIGALRM 14 /* Alarm clock */
56#define GDB_SIGTERM 15 /* Terminated */
57#define GDB_SIGURG 16 /* Urgent I/O condition */
58#define GDB_SIGSTOP 17 /* Stopped (signal) */
59#define GDB_SIGTSTP 18 /* Stopped (user) */
60#define GDB_SIGCONT 19 /* Continued */
61#define GDB_SIGCHLD 20 /* Child status changed */
62#define GDB_SIGTTIN 21 /* Stopped (tty input) */
63#define GDB_SIGTTOU 22 /* Stopped (tty output) */
64#define GDB_SIGIO 23 /* I/O possible */
65#define GDB_SIGXCPU 24 /* CPU time limit exceeded */
66#define GDB_SIGXFSZ 25 /* File size limit exceeded */
67#define GDB_SIGVTALRM 26 /* Virtual timer expired */
68#define GDB_SIGPROF 27 /* Profiling timer expired */
69#define GDB_SIGWINCH 28 /* Window size changed */
70#define GDB_SIGLOST 29 /* Resource lost */
71#define GDB_SIGUSR1 30 /* User defined signal 1 */
72#define GDB_SUGUSR2 31 /* User defined signal 2 */
73#define GDB_SIGPWR 32 /* Power fail/restart */
74#define GDB_SIGPOLL 33 /* Pollable event occurred */
75#define GDB_SIGWIND 34 /* SIGWIND */
76#define GDB_SIGPHONE 35 /* SIGPHONE */
77#define GDB_SIGWAITING 36 /* Process's LWPs are blocked */
78#define GDB_SIGLWP 37 /* Signal LWP */
79#define GDB_SIGDANGER 38 /* Swap space dangerously low */
80#define GDB_SIGGRANT 39 /* Monitor mode granted */
81#define GDB_SIGRETRACT 40 /* Need to relinquish monitor mode */
82#define GDB_SIGMSG 41 /* Monitor mode data available */
83#define GDB_SIGSOUND 42 /* Sound completed */
84#define GDB_SIGSAK 43 /* Secure attention */
85#define GDB_SIGPRIO 44 /* SIGPRIO */
Stefan Reinauer14e22772010-04-27 06:56:47 +000086
Eric Biederman432aa6a2004-10-30 22:59:35 +000087#define GDB_SIG33 45 /* Real-time event 33 */
88#define GDB_SIG34 46 /* Real-time event 34 */
89#define GDB_SIG35 47 /* Real-time event 35 */
90#define GDB_SIG36 48 /* Real-time event 36 */
91#define GDB_SIG37 49 /* Real-time event 37 */
92#define GDB_SIG38 50 /* Real-time event 38 */
93#define GDB_SIG39 51 /* Real-time event 39 */
94#define GDB_SIG40 52 /* Real-time event 40 */
95#define GDB_SIG41 53 /* Real-time event 41 */
96#define GDB_SIG42 54 /* Real-time event 42 */
97#define GDB_SIG43 55 /* Real-time event 43 */
98#define GDB_SIG44 56 /* Real-time event 44 */
99#define GDB_SIG45 57 /* Real-time event 45 */
100#define GDB_SIG46 58 /* Real-time event 46 */
101#define GDB_SIG47 59 /* Real-time event 47 */
102#define GDB_SIG48 60 /* Real-time event 48 */
103#define GDB_SIG49 61 /* Real-time event 49 */
104#define GDB_SIG50 62 /* Real-time event 50 */
105#define GDB_SIG51 63 /* Real-time event 51 */
106#define GDB_SIG52 64 /* Real-time event 52 */
107#define GDB_SIG53 65 /* Real-time event 53 */
108#define GDB_SIG54 66 /* Real-time event 54 */
109#define GDB_SIG55 67 /* Real-time event 55 */
110#define GDB_SIG56 68 /* Real-time event 56 */
111#define GDB_SIG57 69 /* Real-time event 57 */
112#define GDB_SIG58 70 /* Real-time event 58 */
113#define GDB_SIG59 71 /* Real-time event 59 */
114#define GDB_SIG60 72 /* Real-time event 60 */
115#define GDB_SIG61 73 /* Real-time event 61 */
116#define GDB_SIG62 74 /* Real-time event 62 */
117#define GDB_SIG63 75 /* Real-time event 63 */
118#define GDB_SIGCANCEL 76 /* LWP internal signal */
119#define GDB_SIG32 77 /* Real-time event 32 */
120#define GDB_SIG64 78 /* Real-time event 64 */
121#define GDB_SIG65 79 /* Real-time event 65 */
122#define GDB_SIG66 80 /* Real-time event 66 */
123#define GDB_SIG67 81 /* Real-time event 67 */
124#define GDB_SIG68 82 /* Real-time event 68 */
125#define GDB_SIG69 83 /* Real-time event 69 */
126#define GDB_SIG70 84 /* Real-time event 70 */
127#define GDB_SIG71 85 /* Real-time event 71 */
128#define GDB_SIG72 86 /* Real-time event 72 */
129#define GDB_SIG73 87 /* Real-time event 73 */
130#define GDB_SIG74 88 /* Real-time event 74 */
131#define GDB_SIG75 89 /* Real-time event 75 */
132#define GDB_SIG76 90 /* Real-time event 76 */
133#define GDB_SIG77 91 /* Real-time event 77 */
134#define GDB_SIG78 92 /* Real-time event 78 */
135#define GDB_SIG79 93 /* Real-time event 79 */
136#define GDB_SIG80 94 /* Real-time event 80 */
137#define GDB_SIG81 95 /* Real-time event 81 */
138#define GDB_SIG82 96 /* Real-time event 82 */
139#define GDB_SIG83 97 /* Real-time event 83 */
140#define GDB_SIG84 98 /* Real-time event 84 */
141#define GDB_SIG85 99 /* Real-time event 85 */
142#define GDB_SIG86 100 /* Real-time event 86 */
143#define GDB_SIG87 101 /* Real-time event 87 */
144#define GDB_SIG88 102 /* Real-time event 88 */
145#define GDB_SIG89 103 /* Real-time event 89 */
146#define GDB_SIG90 104 /* Real-time event 90 */
147#define GDB_SIG91 105 /* Real-time event 91 */
148#define GDB_SIG92 106 /* Real-time event 92 */
149#define GDB_SIG93 107 /* Real-time event 93 */
150#define GDB_SIG94 108 /* Real-time event 94 */
151#define GDB_SIG95 109 /* Real-time event 95 */
152#define GDB_SIG96 110 /* Real-time event 96 */
153#define GDB_SIG97 111 /* Real-time event 97 */
154#define GDB_SIG98 112 /* Real-time event 98 */
155#define GDB_SIG99 113 /* Real-time event 99 */
156#define GDB_SIG100 114 /* Real-time event 100 */
157#define GDB_SIG101 115 /* Real-time event 101 */
158#define GDB_SIG102 116 /* Real-time event 102 */
159#define GDB_SIG103 117 /* Real-time event 103 */
160#define GDB_SIG104 118 /* Real-time event 104 */
161#define GDB_SIG105 119 /* Real-time event 105 */
162#define GDB_SIG106 120 /* Real-time event 106 */
163#define GDB_SIG107 121 /* Real-time event 107 */
164#define GDB_SIG108 122 /* Real-time event 108 */
165#define GDB_SIG109 123 /* Real-time event 109 */
166#define GDB_SIG110 124 /* Real-time event 110 */
167#define GDB_SIG111 125 /* Real-time event 111 */
168#define GDB_SIG112 126 /* Real-time event 112 */
169#define GDB_SIG113 127 /* Real-time event 113 */
170#define GDB_SIG114 128 /* Real-time event 114 */
171#define GDB_SIG115 129 /* Real-time event 115 */
172#define GDB_SIG116 130 /* Real-time event 116 */
173#define GDB_SIG117 131 /* Real-time event 117 */
174#define GDB_SIG118 132 /* Real-time event 118 */
175#define GDB_SIG119 133 /* Real-time event 119 */
176#define GDB_SIG120 134 /* Real-time event 120 */
177#define GDB_SIG121 135 /* Real-time event 121 */
178#define GDB_SIG122 136 /* Real-time event 122 */
179#define GDB_SIG123 137 /* Real-time event 123 */
180#define GDB_SIG124 138 /* Real-time event 124 */
181#define GDB_SIG125 139 /* Real-time event 125 */
182#define GDB_SIG126 140 /* Real-time event 126 */
183#define GDB_SIG127 141 /* Real-time event 127 */
184#define GDB_SIGINFO 142 /* Information request */
185#define GDB_UNKNOWN 143 /* Unknown signal */
186#define GDB_DEFAULT 144 /* error: default signal */
187/* Mach exceptions */
188#define GDB_EXC_BAD_ACCESS 145 /* Could not access memory */
189#define GDB_EXC_BAD_INSTRCTION 146 /* Illegal instruction/operand */
190#define GDB_EXC_ARITHMETIC 147 /* Arithmetic exception */
191#define GDB_EXC_EMULATION 148 /* Emulation instruction */
192#define GDB_EXC_SOFTWARE 149 /* Software generated exception */
193#define GDB_EXC_BREAKPOINT 150 /* Breakpoint */
194
195
196
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700197static unsigned char exception_to_signal[] = {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000198 [0] = GDB_SIGFPE, /* divide by zero */
199 [1] = GDB_SIGTRAP, /* debug exception */
200 [2] = GDB_SIGSEGV, /* NMI Interrupt */
201 [3] = GDB_SIGTRAP, /* Breakpoint */
202 [4] = GDB_SIGSEGV, /* into instruction (overflow) */
203 [5] = GDB_SIGSEGV, /* bound instruction */
204 [6] = GDB_SIGILL, /* Invalid opcode */
205 [7] = GDB_SIGSEGV, /* coprocessor not available */
206 [8] = GDB_SIGSEGV, /* double fault */
207 [9] = GDB_SIGFPE, /* coprocessor segment overrun */
208 [10] = GDB_SIGSEGV, /* Invalid TSS */
209 [11] = GDB_SIGBUS, /* Segment not present */
210 [12] = GDB_SIGBUS, /* stack exception */
211 [13] = GDB_SIGSEGV, /* general protection */
212 [14] = GDB_SIGSEGV, /* page fault */
213 [15] = GDB_UNKNOWN, /* reserved */
214 [16] = GDB_SIGEMT, /* coprocessor error */
215 [17] = GDB_SIGBUS, /* alignment check */
216 [18] = GDB_SIGSEGV, /* machine check */
217 [19] = GDB_SIGFPE, /* simd floating point exception */
218 [20] = GDB_UNKNOWN,
219 [21] = GDB_UNKNOWN,
220 [22] = GDB_UNKNOWN,
221 [23] = GDB_UNKNOWN,
222 [24] = GDB_UNKNOWN,
223 [25] = GDB_UNKNOWN,
224 [26] = GDB_UNKNOWN,
225 [27] = GDB_UNKNOWN,
226 [28] = GDB_UNKNOWN,
227 [29] = GDB_UNKNOWN,
228 [30] = GDB_UNKNOWN,
229 [31] = GDB_UNKNOWN,
230 [32] = GDB_SIGINT, /* User interrupt */
231};
232
233static const char hexchars[] = "0123456789abcdef";
234static char in_buffer[BUFMAX];
235static char out_buffer[BUFMAX];
236
237
238static inline void stub_putc(int ch)
239{
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300240 gdb_tx_byte(ch);
241}
242
243static inline void stub_flush(void)
244{
245 gdb_tx_flush();
Eric Biederman432aa6a2004-10-30 22:59:35 +0000246}
247
248static inline int stub_getc(void)
249{
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300250 return gdb_rx_byte();
Eric Biederman432aa6a2004-10-30 22:59:35 +0000251}
252
253static int hex(char ch)
254{
255 if ((ch >= 'a') && (ch <= 'f'))
256 return (ch - 'a' + 10);
257 if ((ch >= '0') && (ch <= '9'))
258 return (ch - '0');
259 if ((ch >= 'A') && (ch <= 'F'))
260 return (ch - 'A' + 10);
261 return (-1);
262}
263
264/*
265 * While we find hexadecimal digits, build an int.
266 * Fals is returned if nothing is parsed true otherwise.
267 */
268static int parse_ulong(char **ptr, unsigned long *value)
269{
270 int digit;
271 char *start;
272
273 start = *ptr;
274 *value = 0;
275
Lee Leahy024b13d2017-03-16 13:41:11 -0700276 while ((digit = hex(**ptr)) >= 0) {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000277 *value = ((*value) << 4) | digit;
278 (*ptr)++;
279 }
280 return start != *ptr;
281}
282
283/* convert the memory pointed to by mem into hex, placing result in buf */
284/* return a pointer to the last char put in buf (null) */
285static void copy_to_hex(char *buf, void *addr, unsigned long count)
286{
287 unsigned char ch;
288 char *mem = addr;
289
Lee Leahy024b13d2017-03-16 13:41:11 -0700290 while (count--) {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000291 ch = *mem++;
292 *buf++ = hexchars[ch >> 4];
293 *buf++ = hexchars[ch & 0x0f];
294 }
295 *buf = 0;
Eric Biederman432aa6a2004-10-30 22:59:35 +0000296}
297
298
299/* convert the hex array pointed to by buf into binary to be placed in mem */
300/* return a pointer to the character AFTER the last byte written */
301static void copy_from_hex(void *addr, char *buf, unsigned long count)
302{
303 unsigned char ch;
304 char *mem = addr;
305
Lee Leahy024b13d2017-03-16 13:41:11 -0700306 while (count--) {
307 ch = hex(*buf++) << 4;
308 ch = ch + hex(*buf++);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000309 *mem++ = ch;
310 }
311}
312
313
314/* scan for the sequence $<data>#<checksum> */
315
316static int get_packet(char *buffer)
317{
318 unsigned char checksum;
319 unsigned char xmitcsum;
320 int count;
321 char ch;
322
323 /* Wishlit implement a timeout in get_packet */
324 do {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700325 /* wait around for the start character, ignore all other
326 * characters
327 */
Lee Leahy0b5678f2017-03-16 16:01:40 -0700328 while ((ch = (stub_getc() & 0x7f)) != '$')
329 ;
Eric Biederman432aa6a2004-10-30 22:59:35 +0000330 checksum = 0;
331 xmitcsum = -1;
332
333 count = 0;
334
335 /* now, read until a # or end of buffer is found */
336 while (count < BUFMAX) {
337 ch = stub_getc() & 0x7f;
338 if (ch == '#')
339 break;
340 checksum = checksum + ch;
341 buffer[count] = ch;
342 count = count + 1;
343 }
344 buffer[count] = 0;
345
346 if (ch == '#') {
347 xmitcsum = hex(stub_getc() & 0x7f) << 4;
348 xmitcsum += hex(stub_getc() & 0x7f);
349
350 if (checksum != xmitcsum) {
351 stub_putc('-'); /* failed checksum */
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300352 stub_flush();
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700353 } else {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000354 stub_putc('+'); /* successful transfer */
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300355 stub_flush();
Eric Biederman432aa6a2004-10-30 22:59:35 +0000356 }
357 }
Lee Leahy024b13d2017-03-16 13:41:11 -0700358 } while (checksum != xmitcsum);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000359 return 1;
360}
361
362/* send the packet in buffer.*/
363static void put_packet(char *buffer)
364{
365 unsigned char checksum;
366 int count;
367 char ch;
368
369 /* $<packet info>#<checksum>. */
370 do {
371 stub_putc('$');
372 checksum = 0;
373 count = 0;
374
375 while ((ch = buffer[count])) {
376 stub_putc(ch);
377 checksum += ch;
378 count += 1;
379 }
380
381 stub_putc('#');
382 stub_putc(hexchars[checksum >> 4]);
383 stub_putc(hexchars[checksum % 16]);
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300384 stub_flush();
Eric Biederman432aa6a2004-10-30 22:59:35 +0000385
386 } while ((stub_getc() & 0x7f) != '+');
387
388}
389#endif /* CONFIG_GDB_STUB */
390
Stefan Reinauer42dc7212009-10-24 00:47:07 +0000391#include <arch/registers.h>
Eric Biederman432aa6a2004-10-30 22:59:35 +0000392
Maciej Pijankaea921852009-10-27 14:29:29 +0000393void x86_exception(struct eregs *info);
394
Eric Biederman432aa6a2004-10-30 22:59:35 +0000395void x86_exception(struct eregs *info)
396{
Martin Roth898a7752017-06-01 11:39:59 -0600397#if IS_ENABLED(CONFIG_GDB_STUB)
Eric Biederman432aa6a2004-10-30 22:59:35 +0000398 int signo;
399 memcpy(gdb_stub_registers, info, 8*sizeof(uint32_t));
400 gdb_stub_registers[PC] = info->eip;
401 gdb_stub_registers[CS] = info->cs;
402 gdb_stub_registers[PS] = info->eflags;
403 signo = GDB_UNKNOWN;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700404 if (info->vector < ARRAY_SIZE(exception_to_signal))
Eric Biederman432aa6a2004-10-30 22:59:35 +0000405 signo = exception_to_signal[info->vector];
Stefan Reinauer14e22772010-04-27 06:56:47 +0000406
Martin Roth2ed0aa22016-01-05 20:58:58 -0700407 /* reply to the host that an exception has occurred */
Eric Biederman432aa6a2004-10-30 22:59:35 +0000408 out_buffer[0] = 'S';
409 out_buffer[1] = hexchars[(signo>>4) & 0xf];
410 out_buffer[2] = hexchars[signo & 0xf];
411 out_buffer[3] = '\0';
412 put_packet(out_buffer);
413
Lee Leahy024b13d2017-03-16 13:41:11 -0700414 while (1) {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000415 unsigned long addr, length;
416 char *ptr;
417 out_buffer[0] = '\0';
418 out_buffer[1] = '\0';
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700419 if (!get_packet(in_buffer))
Eric Biederman432aa6a2004-10-30 22:59:35 +0000420 break;
Lee Leahy024b13d2017-03-16 13:41:11 -0700421 switch (in_buffer[0]) {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000422 case '?': /* last signal */
423 out_buffer[0] = 'S';
424 out_buffer[1] = hexchars[(signo >> 4) & 0xf];
425 out_buffer[2] = hexchars[signo & 0xf];
426 out_buffer[3] = '\0';
427 break;
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200428 case 'g': /* return the value of the CPU registers */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700429 copy_to_hex(out_buffer, &gdb_stub_registers,
430 sizeof(gdb_stub_registers));
Eric Biederman432aa6a2004-10-30 22:59:35 +0000431 break;
432 case 'G': /* set the value of the CPU registers - return OK */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700433 copy_from_hex(&gdb_stub_registers, in_buffer + 1,
434 sizeof(gdb_stub_registers));
Eric Biederman432aa6a2004-10-30 22:59:35 +0000435 memcpy(info, gdb_stub_registers, 8*sizeof(uint32_t));
436 info->eip = gdb_stub_registers[PC];
437 info->cs = gdb_stub_registers[CS];
438 info->eflags = gdb_stub_registers[PS];
Lee Leahy024b13d2017-03-16 13:41:11 -0700439 memcpy(out_buffer, "OK", 3);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000440 break;
441 case 'm':
442 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
443 ptr = &in_buffer[1];
Lee Leahy024b13d2017-03-16 13:41:11 -0700444 if (parse_ulong(&ptr, &addr) &&
Eric Biederman432aa6a2004-10-30 22:59:35 +0000445 (*ptr++ == ',') &&
446 parse_ulong(&ptr, &length)) {
Eric Biedermana9e632c2004-11-18 22:38:08 +0000447 copy_to_hex(out_buffer, (void *)addr, length);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700448 } else
Eric Biederman432aa6a2004-10-30 22:59:35 +0000449 memcpy(out_buffer, "E01", 4);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000450 break;
451 case 'M':
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700452 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA
453 * return OK
454 */
Eric Biederman432aa6a2004-10-30 22:59:35 +0000455 ptr = &in_buffer[1];
Lee Leahy024b13d2017-03-16 13:41:11 -0700456 if (parse_ulong(&ptr, &addr) &&
Eric Biederman432aa6a2004-10-30 22:59:35 +0000457 (*(ptr++) == ',') &&
458 parse_ulong(&ptr, &length) &&
459 (*(ptr++) == ':')) {
460 copy_from_hex((void *)addr, ptr, length);
461 memcpy(out_buffer, "OK", 3);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700462 } else
Eric Biederman432aa6a2004-10-30 22:59:35 +0000463 memcpy(out_buffer, "E02", 4);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000464 break;
465 case 's':
466 case 'c':
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700467 /* cAA..AA Continue at address AA..AA(optional)
468 * sAA..AA Step one instruction from AA..AA(optional)
469 */
Eric Biederman432aa6a2004-10-30 22:59:35 +0000470 ptr = &in_buffer[1];
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700471 if (parse_ulong(&ptr, &addr))
Eric Biederman432aa6a2004-10-30 22:59:35 +0000472 info->eip = addr;
Eric Biederman432aa6a2004-10-30 22:59:35 +0000473
474 /* Clear the trace bit */
475 info->eflags &= ~(1 << 8);
476 /* Set the trace bit if we are single stepping */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700477 if (in_buffer[0] == 's')
Eric Biederman432aa6a2004-10-30 22:59:35 +0000478 info->eflags |= (1 << 8);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000479 return;
Eric Biederman432aa6a2004-10-30 22:59:35 +0000480 case 'D':
481 memcpy(out_buffer, "OK", 3);
482 break;
483 case 'k': /* kill request? */
484 break;
485 case 'q': /* query */
486 break;
487 case 'z': /* z0AAAA,LLLL remove memory breakpoint */
488 /* z1AAAA,LLLL remove hardware breakpoint */
489 /* z2AAAA,LLLL remove write watchpoint */
490 /* z3AAAA,LLLL remove read watchpoint */
491 /* z4AAAA,LLLL remove access watchpoint */
492 case 'Z': /* Z0AAAA,LLLL insert memory breakpoint */
493 /* Z1AAAA,LLLL insert hardware breakpoint */
494 /* Z2AAAA,LLLL insert write watchpoint */
495 /* Z3AAAA,LLLL insert read watchpoint */
496 /* Z4AAAA,LLLL insert access watchpoint */
497 break;
498 default:
499 break;
500 }
501 put_packet(out_buffer);
502 }
503#else /* !CONFIG_GDB_STUB */
Alexandru Gagniucde415eb2012-08-15 06:45:18 -0500504#define MDUMP_SIZE 0x80
Subrata Banik8cfd76d2018-04-18 11:48:58 +0530505 unsigned int logical_processor = 0;
506
507#if ENV_RAMSTAGE
508 logical_processor = cpu_index();
509#endif
Stefan Reinauer14e22772010-04-27 06:56:47 +0000510 printk(BIOS_EMERG,
Subrata Banik8cfd76d2018-04-18 11:48:58 +0530511 "CPU Index %d - APIC %d Unexpected Exception:"
512 "%d @ %02x:%08x - Halting\n"
Aaron Durbinaae73d72018-04-21 00:10:10 -0600513 "Code: %d eflags: %08x cr2: %08x\n"
Myles Watsonc4ddbff2009-02-09 17:52:54 +0000514 "eax: %08x ebx: %08x ecx: %08x edx: %08x\n"
515 "edi: %08x esi: %08x ebp: %08x esp: %08x\n",
Subrata Banik8cfd76d2018-04-18 11:48:58 +0530516 logical_processor, (unsigned int)lapicid(),
Eric Biederman432aa6a2004-10-30 22:59:35 +0000517 info->vector, info->cs, info->eip,
Aaron Durbinaae73d72018-04-21 00:10:10 -0600518 info->error_code, info->eflags, read_cr2(),
Eric Biederman432aa6a2004-10-30 22:59:35 +0000519 info->eax, info->ebx, info->ecx, info->edx,
520 info->edi, info->esi, info->ebp, info->esp);
Lee Leahy024b13d2017-03-16 13:41:11 -0700521 u8 *code = (u8 *)((uintptr_t)info->eip - (MDUMP_SIZE >> 1));
Alexandru Gagniucde415eb2012-08-15 06:45:18 -0500522 /* Align to 8-byte boundary please, and print eight bytes per row.
523 * This is done to make DRAM burst timing/reordering errors more
524 * evident from the looking at the dump */
Lee Leahy024b13d2017-03-16 13:41:11 -0700525 code = (u8 *)((uintptr_t)code & ~0x7);
Alexandru Gagniucde415eb2012-08-15 06:45:18 -0500526 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700527 for (i = 0; i < MDUMP_SIZE; i++) {
Lee Leahy024b13d2017-03-16 13:41:11 -0700528 if ((i & 0x07) == 0)
Stefan Reinauer96938852015-06-18 01:23:48 -0700529 printk(BIOS_EMERG, "\n%p:\t", code + i);
Alexandru Gagniucde415eb2012-08-15 06:45:18 -0500530 printk(BIOS_EMERG, "%.2x ", code[i]);
531 }
Eric Biederman432aa6a2004-10-30 22:59:35 +0000532 die("");
533#endif
534}
Aaron Durbin4b032e42018-04-20 01:39:30 -0600535
536#define GATE_P (1 << 15)
537#define GATE_DPL(x) (((x) & 0x3) << 13)
538#define GATE_SIZE_16 (0 << 11)
539#define GATE_SIZE_32 (1 << 11)
540
541#define IGATE_FLAGS (GATE_P | GATE_DPL(0) | GATE_SIZE_32 | (0x6 << 8))
542
543struct intr_gate {
544 uint16_t offset_0;
545 uint16_t segsel;
546 uint16_t flags;
547 uint16_t offset_1;
548#if ENV_X86_64
549 uint32_t offset_2;
550 uint32_t reserved;
551#endif
552} __packed;
553
554/* Even though the vecX symbols are interrupt entry points just treat them
555 like data to more easily get the pointer values in C. Because IDT entries
Jonathan Neuschäfer773cc1b2018-10-10 14:22:48 +0200556 format splits the offset field up, one can't use the linker to resolve
557 parts of a relocation on x86 ABI. An array of pointers is used to gather
Aaron Durbin4b032e42018-04-20 01:39:30 -0600558 the symbols. The IDT is initialized at runtime when exception_init() is
559 called. */
560extern u8 vec0[], vec1[], vec2[], vec3[], vec4[], vec5[], vec6[], vec7[];
561extern u8 vec8[], vec9[], vec10[], vec11[], vec12[], vec13[], vec14[], vec15[];
562extern u8 vec16[], vec17[], vec18[], vec19[];
563
564static const uintptr_t intr_entries[] = {
565 (uintptr_t)vec0, (uintptr_t)vec1, (uintptr_t)vec2, (uintptr_t)vec3,
566 (uintptr_t)vec4, (uintptr_t)vec5, (uintptr_t)vec6, (uintptr_t)vec7,
567 (uintptr_t)vec8, (uintptr_t)vec9, (uintptr_t)vec10, (uintptr_t)vec11,
568 (uintptr_t)vec12, (uintptr_t)vec13, (uintptr_t)vec14, (uintptr_t)vec15,
569 (uintptr_t)vec16, (uintptr_t)vec17, (uintptr_t)vec18, (uintptr_t)vec19,
570};
571
572static struct intr_gate idt[ARRAY_SIZE(intr_entries)] __aligned(8) CAR_GLOBAL;
573
574static inline uint16_t get_cs(void)
575{
576 uint16_t segment;
577
578 asm volatile (
579 "mov %%cs, %0\n"
580 : "=r" (segment)
581 :
582 : "memory"
583 );
584
585 return segment;
586}
587
588struct lidtarg {
589 uint16_t limit;
590#if ENV_X86_32
591 uint32_t base;
592#else
593 uint64_t base;
594#endif
595} __packed;
596
597/* This global is for src/cpu/x86/lapic/secondary.S usage which is only
598 used during ramstage. */
599struct lidtarg idtarg;
600
601static void load_idt(void *table, size_t sz)
602{
603 struct lidtarg lidtarg = {
604 .limit = sz - 1,
605 .base = (uintptr_t)table,
606 };
607
608 asm volatile (
609 "lidt %0"
610 :
611 : "m" (lidtarg)
612 : "memory"
613 );
614
615 if (ENV_RAMSTAGE)
616 memcpy(&idtarg, &lidtarg, sizeof(idtarg));
617}
618
619asmlinkage void exception_init(void)
620{
621 int i;
622 uint16_t segment;
Richard Spiegelb1299c72018-08-07 14:35:54 -0700623 struct intr_gate *gates;
Aaron Durbin4b032e42018-04-20 01:39:30 -0600624
625 segment = get_cs();
626 gates = car_get_var_ptr(idt);
627
628 /* Initialize IDT. */
629 for (i = 0; i < ARRAY_SIZE(idt); i++) {
630 gates[i].offset_0 = intr_entries[i];
631 gates[i].segsel = segment;
632 gates[i].flags = IGATE_FLAGS;
633 gates[i].offset_1 = intr_entries[i] >> 16;
634#if ENV_X86_64
635 gates[i].offset_2 = intr_entries[i] >> 32;
636#endif
637 }
638
639 load_idt(gates, sizeof(idt));
640}