blob: f10c7bf8baf49f51c50c82568cca8ae4e6230298 [file] [log] [blame]
Patrick Georgi11f00792020-03-04 15:10:45 +01001/* SPDX-License-Identifier: GPL-2.0-only */
Martin Roth9df9e9392016-01-12 15:55:28 -07002
Subrata Banik8cfd76d2018-04-18 11:48:58 +05303#include <arch/cpu.h>
Aaron Durbin4b032e42018-04-20 01:39:30 -06004#include <arch/exception.h>
5#include <commonlib/helpers.h>
Eric Biederman432aa6a2004-10-30 22:59:35 +00006#include <console/console.h>
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +03007#include <console/streams.h>
Aaron Durbinaae73d72018-04-21 00:10:10 -06008#include <cpu/x86/cr.h>
Subrata Banik8cfd76d2018-04-18 11:48:58 +05309#include <cpu/x86/lapic.h>
Aaron Durbin4b032e42018-04-20 01:39:30 -060010#include <stdint.h>
Eric Biederman432aa6a2004-10-30 22:59:35 +000011#include <string.h>
12
Julius Wernercd49cce2019-03-05 16:53:33 -080013#if CONFIG(GDB_STUB)
Eric Biederman432aa6a2004-10-30 22:59:35 +000014
15/* BUFMAX defines the maximum number of characters in inbound/outbound buffers.
Stefan Reinauer14e22772010-04-27 06:56:47 +000016 * At least NUM_REGBYTES*2 are needed for register packets
Eric Biederman432aa6a2004-10-30 22:59:35 +000017 */
18#define BUFMAX 400
19enum regnames {
20 EAX = 0, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
21 PC /* also known as eip */,
22 PS /* also known as eflags */,
23 CS, SS, DS, ES, FS, GS,
24 NUM_REGS /* Number of registers. */
25};
26
27static uint32_t gdb_stub_registers[NUM_REGS];
28
29#define GDB_SIG0 0 /* Signal 0 */
30#define GDB_SIGHUP 1 /* Hangup */
31#define GDB_SIGINT 2 /* Interrupt */
32#define GDB_SIGQUIT 3 /* Quit */
33#define GDB_SIGILL 4 /* Illegal instruction */
34#define GDB_SIGTRAP 5 /* Trace/breakpoint trap */
35#define GDB_SIGABRT 6 /* Aborted */
36#define GDB_SIGEMT 7 /* Emulation trap */
37#define GDB_SIGFPE 8 /* Arithmetic exception */
38#define GDB_SIGKILL 9 /* Killed */
39#define GDB_SIGBUS 10 /* Bus error */
40#define GDB_SIGSEGV 11 /* Segmentation fault */
41#define GDB_SIGSYS 12 /* Bad system call */
42#define GDB_SIGPIPE 13 /* Broken pipe */
43#define GDB_SIGALRM 14 /* Alarm clock */
44#define GDB_SIGTERM 15 /* Terminated */
45#define GDB_SIGURG 16 /* Urgent I/O condition */
46#define GDB_SIGSTOP 17 /* Stopped (signal) */
47#define GDB_SIGTSTP 18 /* Stopped (user) */
48#define GDB_SIGCONT 19 /* Continued */
49#define GDB_SIGCHLD 20 /* Child status changed */
50#define GDB_SIGTTIN 21 /* Stopped (tty input) */
51#define GDB_SIGTTOU 22 /* Stopped (tty output) */
52#define GDB_SIGIO 23 /* I/O possible */
53#define GDB_SIGXCPU 24 /* CPU time limit exceeded */
54#define GDB_SIGXFSZ 25 /* File size limit exceeded */
55#define GDB_SIGVTALRM 26 /* Virtual timer expired */
56#define GDB_SIGPROF 27 /* Profiling timer expired */
57#define GDB_SIGWINCH 28 /* Window size changed */
58#define GDB_SIGLOST 29 /* Resource lost */
59#define GDB_SIGUSR1 30 /* User defined signal 1 */
60#define GDB_SUGUSR2 31 /* User defined signal 2 */
61#define GDB_SIGPWR 32 /* Power fail/restart */
62#define GDB_SIGPOLL 33 /* Pollable event occurred */
63#define GDB_SIGWIND 34 /* SIGWIND */
64#define GDB_SIGPHONE 35 /* SIGPHONE */
65#define GDB_SIGWAITING 36 /* Process's LWPs are blocked */
66#define GDB_SIGLWP 37 /* Signal LWP */
67#define GDB_SIGDANGER 38 /* Swap space dangerously low */
68#define GDB_SIGGRANT 39 /* Monitor mode granted */
69#define GDB_SIGRETRACT 40 /* Need to relinquish monitor mode */
70#define GDB_SIGMSG 41 /* Monitor mode data available */
71#define GDB_SIGSOUND 42 /* Sound completed */
72#define GDB_SIGSAK 43 /* Secure attention */
73#define GDB_SIGPRIO 44 /* SIGPRIO */
Stefan Reinauer14e22772010-04-27 06:56:47 +000074
Eric Biederman432aa6a2004-10-30 22:59:35 +000075#define GDB_SIG33 45 /* Real-time event 33 */
76#define GDB_SIG34 46 /* Real-time event 34 */
77#define GDB_SIG35 47 /* Real-time event 35 */
78#define GDB_SIG36 48 /* Real-time event 36 */
79#define GDB_SIG37 49 /* Real-time event 37 */
80#define GDB_SIG38 50 /* Real-time event 38 */
81#define GDB_SIG39 51 /* Real-time event 39 */
82#define GDB_SIG40 52 /* Real-time event 40 */
83#define GDB_SIG41 53 /* Real-time event 41 */
84#define GDB_SIG42 54 /* Real-time event 42 */
85#define GDB_SIG43 55 /* Real-time event 43 */
86#define GDB_SIG44 56 /* Real-time event 44 */
87#define GDB_SIG45 57 /* Real-time event 45 */
88#define GDB_SIG46 58 /* Real-time event 46 */
89#define GDB_SIG47 59 /* Real-time event 47 */
90#define GDB_SIG48 60 /* Real-time event 48 */
91#define GDB_SIG49 61 /* Real-time event 49 */
92#define GDB_SIG50 62 /* Real-time event 50 */
93#define GDB_SIG51 63 /* Real-time event 51 */
94#define GDB_SIG52 64 /* Real-time event 52 */
95#define GDB_SIG53 65 /* Real-time event 53 */
96#define GDB_SIG54 66 /* Real-time event 54 */
97#define GDB_SIG55 67 /* Real-time event 55 */
98#define GDB_SIG56 68 /* Real-time event 56 */
99#define GDB_SIG57 69 /* Real-time event 57 */
100#define GDB_SIG58 70 /* Real-time event 58 */
101#define GDB_SIG59 71 /* Real-time event 59 */
102#define GDB_SIG60 72 /* Real-time event 60 */
103#define GDB_SIG61 73 /* Real-time event 61 */
104#define GDB_SIG62 74 /* Real-time event 62 */
105#define GDB_SIG63 75 /* Real-time event 63 */
106#define GDB_SIGCANCEL 76 /* LWP internal signal */
107#define GDB_SIG32 77 /* Real-time event 32 */
108#define GDB_SIG64 78 /* Real-time event 64 */
109#define GDB_SIG65 79 /* Real-time event 65 */
110#define GDB_SIG66 80 /* Real-time event 66 */
111#define GDB_SIG67 81 /* Real-time event 67 */
112#define GDB_SIG68 82 /* Real-time event 68 */
113#define GDB_SIG69 83 /* Real-time event 69 */
114#define GDB_SIG70 84 /* Real-time event 70 */
115#define GDB_SIG71 85 /* Real-time event 71 */
116#define GDB_SIG72 86 /* Real-time event 72 */
117#define GDB_SIG73 87 /* Real-time event 73 */
118#define GDB_SIG74 88 /* Real-time event 74 */
119#define GDB_SIG75 89 /* Real-time event 75 */
120#define GDB_SIG76 90 /* Real-time event 76 */
121#define GDB_SIG77 91 /* Real-time event 77 */
122#define GDB_SIG78 92 /* Real-time event 78 */
123#define GDB_SIG79 93 /* Real-time event 79 */
124#define GDB_SIG80 94 /* Real-time event 80 */
125#define GDB_SIG81 95 /* Real-time event 81 */
126#define GDB_SIG82 96 /* Real-time event 82 */
127#define GDB_SIG83 97 /* Real-time event 83 */
128#define GDB_SIG84 98 /* Real-time event 84 */
129#define GDB_SIG85 99 /* Real-time event 85 */
130#define GDB_SIG86 100 /* Real-time event 86 */
131#define GDB_SIG87 101 /* Real-time event 87 */
132#define GDB_SIG88 102 /* Real-time event 88 */
133#define GDB_SIG89 103 /* Real-time event 89 */
134#define GDB_SIG90 104 /* Real-time event 90 */
135#define GDB_SIG91 105 /* Real-time event 91 */
136#define GDB_SIG92 106 /* Real-time event 92 */
137#define GDB_SIG93 107 /* Real-time event 93 */
138#define GDB_SIG94 108 /* Real-time event 94 */
139#define GDB_SIG95 109 /* Real-time event 95 */
140#define GDB_SIG96 110 /* Real-time event 96 */
141#define GDB_SIG97 111 /* Real-time event 97 */
142#define GDB_SIG98 112 /* Real-time event 98 */
143#define GDB_SIG99 113 /* Real-time event 99 */
144#define GDB_SIG100 114 /* Real-time event 100 */
145#define GDB_SIG101 115 /* Real-time event 101 */
146#define GDB_SIG102 116 /* Real-time event 102 */
147#define GDB_SIG103 117 /* Real-time event 103 */
148#define GDB_SIG104 118 /* Real-time event 104 */
149#define GDB_SIG105 119 /* Real-time event 105 */
150#define GDB_SIG106 120 /* Real-time event 106 */
151#define GDB_SIG107 121 /* Real-time event 107 */
152#define GDB_SIG108 122 /* Real-time event 108 */
153#define GDB_SIG109 123 /* Real-time event 109 */
154#define GDB_SIG110 124 /* Real-time event 110 */
155#define GDB_SIG111 125 /* Real-time event 111 */
156#define GDB_SIG112 126 /* Real-time event 112 */
157#define GDB_SIG113 127 /* Real-time event 113 */
158#define GDB_SIG114 128 /* Real-time event 114 */
159#define GDB_SIG115 129 /* Real-time event 115 */
160#define GDB_SIG116 130 /* Real-time event 116 */
161#define GDB_SIG117 131 /* Real-time event 117 */
162#define GDB_SIG118 132 /* Real-time event 118 */
163#define GDB_SIG119 133 /* Real-time event 119 */
164#define GDB_SIG120 134 /* Real-time event 120 */
165#define GDB_SIG121 135 /* Real-time event 121 */
166#define GDB_SIG122 136 /* Real-time event 122 */
167#define GDB_SIG123 137 /* Real-time event 123 */
168#define GDB_SIG124 138 /* Real-time event 124 */
169#define GDB_SIG125 139 /* Real-time event 125 */
170#define GDB_SIG126 140 /* Real-time event 126 */
171#define GDB_SIG127 141 /* Real-time event 127 */
172#define GDB_SIGINFO 142 /* Information request */
173#define GDB_UNKNOWN 143 /* Unknown signal */
174#define GDB_DEFAULT 144 /* error: default signal */
175/* Mach exceptions */
176#define GDB_EXC_BAD_ACCESS 145 /* Could not access memory */
177#define GDB_EXC_BAD_INSTRCTION 146 /* Illegal instruction/operand */
178#define GDB_EXC_ARITHMETIC 147 /* Arithmetic exception */
179#define GDB_EXC_EMULATION 148 /* Emulation instruction */
180#define GDB_EXC_SOFTWARE 149 /* Software generated exception */
181#define GDB_EXC_BREAKPOINT 150 /* Breakpoint */
182
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700183static unsigned char exception_to_signal[] = {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000184 [0] = GDB_SIGFPE, /* divide by zero */
185 [1] = GDB_SIGTRAP, /* debug exception */
186 [2] = GDB_SIGSEGV, /* NMI Interrupt */
187 [3] = GDB_SIGTRAP, /* Breakpoint */
188 [4] = GDB_SIGSEGV, /* into instruction (overflow) */
189 [5] = GDB_SIGSEGV, /* bound instruction */
190 [6] = GDB_SIGILL, /* Invalid opcode */
191 [7] = GDB_SIGSEGV, /* coprocessor not available */
192 [8] = GDB_SIGSEGV, /* double fault */
193 [9] = GDB_SIGFPE, /* coprocessor segment overrun */
194 [10] = GDB_SIGSEGV, /* Invalid TSS */
195 [11] = GDB_SIGBUS, /* Segment not present */
196 [12] = GDB_SIGBUS, /* stack exception */
197 [13] = GDB_SIGSEGV, /* general protection */
198 [14] = GDB_SIGSEGV, /* page fault */
199 [15] = GDB_UNKNOWN, /* reserved */
200 [16] = GDB_SIGEMT, /* coprocessor error */
201 [17] = GDB_SIGBUS, /* alignment check */
202 [18] = GDB_SIGSEGV, /* machine check */
203 [19] = GDB_SIGFPE, /* simd floating point exception */
204 [20] = GDB_UNKNOWN,
205 [21] = GDB_UNKNOWN,
206 [22] = GDB_UNKNOWN,
207 [23] = GDB_UNKNOWN,
208 [24] = GDB_UNKNOWN,
209 [25] = GDB_UNKNOWN,
210 [26] = GDB_UNKNOWN,
211 [27] = GDB_UNKNOWN,
212 [28] = GDB_UNKNOWN,
213 [29] = GDB_UNKNOWN,
214 [30] = GDB_UNKNOWN,
215 [31] = GDB_UNKNOWN,
216 [32] = GDB_SIGINT, /* User interrupt */
217};
218
219static const char hexchars[] = "0123456789abcdef";
220static char in_buffer[BUFMAX];
221static char out_buffer[BUFMAX];
222
Eric Biederman432aa6a2004-10-30 22:59:35 +0000223static inline void stub_putc(int ch)
224{
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300225 gdb_tx_byte(ch);
226}
227
228static inline void stub_flush(void)
229{
230 gdb_tx_flush();
Eric Biederman432aa6a2004-10-30 22:59:35 +0000231}
232
233static inline int stub_getc(void)
234{
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300235 return gdb_rx_byte();
Eric Biederman432aa6a2004-10-30 22:59:35 +0000236}
237
238static int hex(char ch)
239{
240 if ((ch >= 'a') && (ch <= 'f'))
241 return (ch - 'a' + 10);
242 if ((ch >= '0') && (ch <= '9'))
243 return (ch - '0');
244 if ((ch >= 'A') && (ch <= 'F'))
245 return (ch - 'A' + 10);
246 return (-1);
247}
248
249/*
250 * While we find hexadecimal digits, build an int.
251 * Fals is returned if nothing is parsed true otherwise.
252 */
253static int parse_ulong(char **ptr, unsigned long *value)
254{
255 int digit;
256 char *start;
257
258 start = *ptr;
259 *value = 0;
260
Lee Leahy024b13d2017-03-16 13:41:11 -0700261 while ((digit = hex(**ptr)) >= 0) {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000262 *value = ((*value) << 4) | digit;
263 (*ptr)++;
264 }
265 return start != *ptr;
266}
267
268/* convert the memory pointed to by mem into hex, placing result in buf */
269/* return a pointer to the last char put in buf (null) */
270static void copy_to_hex(char *buf, void *addr, unsigned long count)
271{
272 unsigned char ch;
273 char *mem = addr;
274
Lee Leahy024b13d2017-03-16 13:41:11 -0700275 while (count--) {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000276 ch = *mem++;
277 *buf++ = hexchars[ch >> 4];
278 *buf++ = hexchars[ch & 0x0f];
279 }
280 *buf = 0;
Eric Biederman432aa6a2004-10-30 22:59:35 +0000281}
282
Eric Biederman432aa6a2004-10-30 22:59:35 +0000283/* convert the hex array pointed to by buf into binary to be placed in mem */
284/* return a pointer to the character AFTER the last byte written */
285static void copy_from_hex(void *addr, char *buf, unsigned long count)
286{
287 unsigned char ch;
288 char *mem = addr;
289
Lee Leahy024b13d2017-03-16 13:41:11 -0700290 while (count--) {
291 ch = hex(*buf++) << 4;
292 ch = ch + hex(*buf++);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000293 *mem++ = ch;
294 }
295}
296
Eric Biederman432aa6a2004-10-30 22:59:35 +0000297/* scan for the sequence $<data>#<checksum> */
298
299static int get_packet(char *buffer)
300{
301 unsigned char checksum;
302 unsigned char xmitcsum;
303 int count;
304 char ch;
305
306 /* Wishlit implement a timeout in get_packet */
307 do {
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700308 /* wait around for the start character, ignore all other
309 * characters
310 */
Lee Leahy0b5678f2017-03-16 16:01:40 -0700311 while ((ch = (stub_getc() & 0x7f)) != '$')
312 ;
Eric Biederman432aa6a2004-10-30 22:59:35 +0000313 checksum = 0;
314 xmitcsum = -1;
315
316 count = 0;
317
318 /* now, read until a # or end of buffer is found */
319 while (count < BUFMAX) {
320 ch = stub_getc() & 0x7f;
321 if (ch == '#')
322 break;
323 checksum = checksum + ch;
324 buffer[count] = ch;
325 count = count + 1;
326 }
327 buffer[count] = 0;
328
329 if (ch == '#') {
330 xmitcsum = hex(stub_getc() & 0x7f) << 4;
331 xmitcsum += hex(stub_getc() & 0x7f);
332
333 if (checksum != xmitcsum) {
334 stub_putc('-'); /* failed checksum */
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300335 stub_flush();
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700336 } else {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000337 stub_putc('+'); /* successful transfer */
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300338 stub_flush();
Eric Biederman432aa6a2004-10-30 22:59:35 +0000339 }
340 }
Lee Leahy024b13d2017-03-16 13:41:11 -0700341 } while (checksum != xmitcsum);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000342 return 1;
343}
344
345/* send the packet in buffer.*/
346static void put_packet(char *buffer)
347{
348 unsigned char checksum;
349 int count;
350 char ch;
351
352 /* $<packet info>#<checksum>. */
353 do {
354 stub_putc('$');
355 checksum = 0;
356 count = 0;
357
358 while ((ch = buffer[count])) {
359 stub_putc(ch);
360 checksum += ch;
361 count += 1;
362 }
363
364 stub_putc('#');
365 stub_putc(hexchars[checksum >> 4]);
366 stub_putc(hexchars[checksum % 16]);
Kyösti Mälkkif2f7f032014-04-04 15:05:28 +0300367 stub_flush();
Eric Biederman432aa6a2004-10-30 22:59:35 +0000368
369 } while ((stub_getc() & 0x7f) != '+');
370
371}
372#endif /* CONFIG_GDB_STUB */
373
Stefan Reinauer42dc7212009-10-24 00:47:07 +0000374#include <arch/registers.h>
Eric Biederman432aa6a2004-10-30 22:59:35 +0000375
Maciej Pijankaea921852009-10-27 14:29:29 +0000376void x86_exception(struct eregs *info);
377
Eric Biederman432aa6a2004-10-30 22:59:35 +0000378void x86_exception(struct eregs *info)
379{
Julius Wernercd49cce2019-03-05 16:53:33 -0800380#if CONFIG(GDB_STUB)
Eric Biederman432aa6a2004-10-30 22:59:35 +0000381 int signo;
382 memcpy(gdb_stub_registers, info, 8*sizeof(uint32_t));
383 gdb_stub_registers[PC] = info->eip;
384 gdb_stub_registers[CS] = info->cs;
385 gdb_stub_registers[PS] = info->eflags;
386 signo = GDB_UNKNOWN;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700387 if (info->vector < ARRAY_SIZE(exception_to_signal))
Eric Biederman432aa6a2004-10-30 22:59:35 +0000388 signo = exception_to_signal[info->vector];
Stefan Reinauer14e22772010-04-27 06:56:47 +0000389
Martin Roth2ed0aa22016-01-05 20:58:58 -0700390 /* reply to the host that an exception has occurred */
Eric Biederman432aa6a2004-10-30 22:59:35 +0000391 out_buffer[0] = 'S';
392 out_buffer[1] = hexchars[(signo>>4) & 0xf];
393 out_buffer[2] = hexchars[signo & 0xf];
394 out_buffer[3] = '\0';
395 put_packet(out_buffer);
396
Lee Leahy024b13d2017-03-16 13:41:11 -0700397 while (1) {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000398 unsigned long addr, length;
399 char *ptr;
400 out_buffer[0] = '\0';
401 out_buffer[1] = '\0';
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700402 if (!get_packet(in_buffer))
Eric Biederman432aa6a2004-10-30 22:59:35 +0000403 break;
Lee Leahy024b13d2017-03-16 13:41:11 -0700404 switch (in_buffer[0]) {
Eric Biederman432aa6a2004-10-30 22:59:35 +0000405 case '?': /* last signal */
406 out_buffer[0] = 'S';
407 out_buffer[1] = hexchars[(signo >> 4) & 0xf];
408 out_buffer[2] = hexchars[signo & 0xf];
409 out_buffer[3] = '\0';
410 break;
Elyes HAOUAS777ea892016-07-29 07:40:41 +0200411 case 'g': /* return the value of the CPU registers */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700412 copy_to_hex(out_buffer, &gdb_stub_registers,
413 sizeof(gdb_stub_registers));
Eric Biederman432aa6a2004-10-30 22:59:35 +0000414 break;
415 case 'G': /* set the value of the CPU registers - return OK */
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700416 copy_from_hex(&gdb_stub_registers, in_buffer + 1,
417 sizeof(gdb_stub_registers));
Eric Biederman432aa6a2004-10-30 22:59:35 +0000418 memcpy(info, gdb_stub_registers, 8*sizeof(uint32_t));
419 info->eip = gdb_stub_registers[PC];
420 info->cs = gdb_stub_registers[CS];
421 info->eflags = gdb_stub_registers[PS];
Lee Leahy024b13d2017-03-16 13:41:11 -0700422 memcpy(out_buffer, "OK", 3);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000423 break;
424 case 'm':
425 /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */
426 ptr = &in_buffer[1];
Lee Leahy024b13d2017-03-16 13:41:11 -0700427 if (parse_ulong(&ptr, &addr) &&
Eric Biederman432aa6a2004-10-30 22:59:35 +0000428 (*ptr++ == ',') &&
429 parse_ulong(&ptr, &length)) {
Eric Biedermana9e632c2004-11-18 22:38:08 +0000430 copy_to_hex(out_buffer, (void *)addr, length);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700431 } else
Eric Biederman432aa6a2004-10-30 22:59:35 +0000432 memcpy(out_buffer, "E01", 4);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000433 break;
434 case 'M':
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700435 /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA
436 * return OK
437 */
Eric Biederman432aa6a2004-10-30 22:59:35 +0000438 ptr = &in_buffer[1];
Lee Leahy024b13d2017-03-16 13:41:11 -0700439 if (parse_ulong(&ptr, &addr) &&
Eric Biederman432aa6a2004-10-30 22:59:35 +0000440 (*(ptr++) == ',') &&
441 parse_ulong(&ptr, &length) &&
442 (*(ptr++) == ':')) {
443 copy_from_hex((void *)addr, ptr, length);
444 memcpy(out_buffer, "OK", 3);
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700445 } else
Eric Biederman432aa6a2004-10-30 22:59:35 +0000446 memcpy(out_buffer, "E02", 4);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000447 break;
448 case 's':
449 case 'c':
Lee Leahy6f80ccc2017-03-16 15:18:22 -0700450 /* cAA..AA Continue at address AA..AA(optional)
451 * sAA..AA Step one instruction from AA..AA(optional)
452 */
Eric Biederman432aa6a2004-10-30 22:59:35 +0000453 ptr = &in_buffer[1];
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700454 if (parse_ulong(&ptr, &addr))
Eric Biederman432aa6a2004-10-30 22:59:35 +0000455 info->eip = addr;
Eric Biederman432aa6a2004-10-30 22:59:35 +0000456
457 /* Clear the trace bit */
458 info->eflags &= ~(1 << 8);
459 /* Set the trace bit if we are single stepping */
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700460 if (in_buffer[0] == 's')
Eric Biederman432aa6a2004-10-30 22:59:35 +0000461 info->eflags |= (1 << 8);
Eric Biederman432aa6a2004-10-30 22:59:35 +0000462 return;
Eric Biederman432aa6a2004-10-30 22:59:35 +0000463 case 'D':
464 memcpy(out_buffer, "OK", 3);
465 break;
466 case 'k': /* kill request? */
467 break;
468 case 'q': /* query */
469 break;
470 case 'z': /* z0AAAA,LLLL remove memory breakpoint */
471 /* z1AAAA,LLLL remove hardware breakpoint */
472 /* z2AAAA,LLLL remove write watchpoint */
473 /* z3AAAA,LLLL remove read watchpoint */
474 /* z4AAAA,LLLL remove access watchpoint */
475 case 'Z': /* Z0AAAA,LLLL insert memory breakpoint */
476 /* Z1AAAA,LLLL insert hardware breakpoint */
477 /* Z2AAAA,LLLL insert write watchpoint */
478 /* Z3AAAA,LLLL insert read watchpoint */
479 /* Z4AAAA,LLLL insert access watchpoint */
480 break;
481 default:
482 break;
483 }
484 put_packet(out_buffer);
485 }
486#else /* !CONFIG_GDB_STUB */
Paul Menzel6663ad92019-06-24 18:44:33 +0200487
Jacob Garberbc674762019-05-14 11:21:41 -0600488 int logical_processor = 0;
Subrata Banik8cfd76d2018-04-18 11:48:58 +0530489
490#if ENV_RAMSTAGE
491 logical_processor = cpu_index();
492#endif
Paul Menzel6663ad92019-06-24 18:44:33 +0200493 u8 *code;
494#ifdef __x86_64__
495#define MDUMP_SIZE 0x100
496 printk(BIOS_EMERG,
497 "CPU Index %d - APIC %d Unexpected Exception:\n"
498 "%lld @ %02llx:%016llx - Halting\n"
499 "Code: %lld rflags: %016llx cr2: %016llx\n"
500 "rax: %016llx rbx: %016llx\n"
501 "rcx: %016llx rdx: %016llx\n"
502 "rdi: %016llx rsi: %016llx\n"
503 "rbp: %016llx rsp: %016llx\n"
504 "r08: %016llx r09: %016llx\n"
505 "r10: %016llx r11: %016llx\n"
506 "r12: %016llx r13: %016llx\n"
507 "r14: %016llx r15: %016llx\n",
508 logical_processor, (unsigned int)lapicid(),
509 info->vector, info->cs, info->rip,
510 info->error_code, info->rflags, read_cr2(),
511 info->rax, info->rbx, info->rcx, info->rdx,
512 info->rdi, info->rsi, info->rbp, info->rsp,
513 info->r8, info->r9, info->r10, info->r11,
514 info->r12, info->r13, info->r14, info->r15);
515 code = (u8 *)((uintptr_t)info->rip - (MDUMP_SIZE >> 2));
516#else
517#define MDUMP_SIZE 0x80
518
Stefan Reinauer14e22772010-04-27 06:56:47 +0000519 printk(BIOS_EMERG,
Subrata Banik8cfd76d2018-04-18 11:48:58 +0530520 "CPU Index %d - APIC %d Unexpected Exception:"
521 "%d @ %02x:%08x - Halting\n"
Aaron Durbinaae73d72018-04-21 00:10:10 -0600522 "Code: %d eflags: %08x cr2: %08x\n"
Myles Watsonc4ddbff2009-02-09 17:52:54 +0000523 "eax: %08x ebx: %08x ecx: %08x edx: %08x\n"
524 "edi: %08x esi: %08x ebp: %08x esp: %08x\n",
Subrata Banik8cfd76d2018-04-18 11:48:58 +0530525 logical_processor, (unsigned int)lapicid(),
Eric Biederman432aa6a2004-10-30 22:59:35 +0000526 info->vector, info->cs, info->eip,
Aaron Durbinaae73d72018-04-21 00:10:10 -0600527 info->error_code, info->eflags, read_cr2(),
Eric Biederman432aa6a2004-10-30 22:59:35 +0000528 info->eax, info->ebx, info->ecx, info->edx,
529 info->edi, info->esi, info->ebp, info->esp);
Paul Menzel6663ad92019-06-24 18:44:33 +0200530 code = (u8 *)((uintptr_t)info->eip - (MDUMP_SIZE >> 1));
531#endif
Alexandru Gagniucde415eb2012-08-15 06:45:18 -0500532 /* Align to 8-byte boundary please, and print eight bytes per row.
533 * This is done to make DRAM burst timing/reordering errors more
534 * evident from the looking at the dump */
Lee Leahy024b13d2017-03-16 13:41:11 -0700535 code = (u8 *)((uintptr_t)code & ~0x7);
Alexandru Gagniucde415eb2012-08-15 06:45:18 -0500536 int i;
Lee Leahy9c7c6f72017-03-16 11:24:09 -0700537 for (i = 0; i < MDUMP_SIZE; i++) {
Lee Leahy024b13d2017-03-16 13:41:11 -0700538 if ((i & 0x07) == 0)
Stefan Reinauer96938852015-06-18 01:23:48 -0700539 printk(BIOS_EMERG, "\n%p:\t", code + i);
Alexandru Gagniucde415eb2012-08-15 06:45:18 -0500540 printk(BIOS_EMERG, "%.2x ", code[i]);
541 }
Raul E Rangel5bb926e2020-06-29 11:28:25 -0600542
543 /* Align to 4-byte boundary and up the stack. */
544 u32 *ptr = (u32 *)(ALIGN_DOWN((uintptr_t)info->esp, sizeof(u32)) + MDUMP_SIZE - 4);
545 for (i = 0; i < MDUMP_SIZE / sizeof(u32); ++i, --ptr) {
546 printk(BIOS_EMERG, "\n%p:\t0x%08x", ptr, *ptr);
547 if ((uintptr_t)ptr == info->ebp)
548 printk(BIOS_EMERG, " <-ebp");
549 else if ((uintptr_t)ptr == info->esp)
550 printk(BIOS_EMERG, " <-esp");
551 }
552
553 die("\n");
Eric Biederman432aa6a2004-10-30 22:59:35 +0000554#endif
555}
Aaron Durbin4b032e42018-04-20 01:39:30 -0600556
557#define GATE_P (1 << 15)
558#define GATE_DPL(x) (((x) & 0x3) << 13)
559#define GATE_SIZE_16 (0 << 11)
560#define GATE_SIZE_32 (1 << 11)
561
562#define IGATE_FLAGS (GATE_P | GATE_DPL(0) | GATE_SIZE_32 | (0x6 << 8))
563
564struct intr_gate {
565 uint16_t offset_0;
566 uint16_t segsel;
567 uint16_t flags;
568 uint16_t offset_1;
569#if ENV_X86_64
570 uint32_t offset_2;
571 uint32_t reserved;
572#endif
573} __packed;
574
575/* Even though the vecX symbols are interrupt entry points just treat them
576 like data to more easily get the pointer values in C. Because IDT entries
Jonathan Neuschäfer773cc1b2018-10-10 14:22:48 +0200577 format splits the offset field up, one can't use the linker to resolve
578 parts of a relocation on x86 ABI. An array of pointers is used to gather
Aaron Durbin4b032e42018-04-20 01:39:30 -0600579 the symbols. The IDT is initialized at runtime when exception_init() is
580 called. */
581extern u8 vec0[], vec1[], vec2[], vec3[], vec4[], vec5[], vec6[], vec7[];
582extern u8 vec8[], vec9[], vec10[], vec11[], vec12[], vec13[], vec14[], vec15[];
583extern u8 vec16[], vec17[], vec18[], vec19[];
584
585static const uintptr_t intr_entries[] = {
586 (uintptr_t)vec0, (uintptr_t)vec1, (uintptr_t)vec2, (uintptr_t)vec3,
587 (uintptr_t)vec4, (uintptr_t)vec5, (uintptr_t)vec6, (uintptr_t)vec7,
588 (uintptr_t)vec8, (uintptr_t)vec9, (uintptr_t)vec10, (uintptr_t)vec11,
589 (uintptr_t)vec12, (uintptr_t)vec13, (uintptr_t)vec14, (uintptr_t)vec15,
590 (uintptr_t)vec16, (uintptr_t)vec17, (uintptr_t)vec18, (uintptr_t)vec19,
591};
592
Arthur Heymans9e1ea542019-11-20 22:09:15 +0100593static struct intr_gate idt[ARRAY_SIZE(intr_entries)] __aligned(8);
Aaron Durbin4b032e42018-04-20 01:39:30 -0600594
595static inline uint16_t get_cs(void)
596{
597 uint16_t segment;
598
599 asm volatile (
600 "mov %%cs, %0\n"
601 : "=r" (segment)
602 :
603 : "memory"
604 );
605
606 return segment;
607}
608
609struct lidtarg {
610 uint16_t limit;
611#if ENV_X86_32
612 uint32_t base;
613#else
614 uint64_t base;
615#endif
616} __packed;
617
618/* This global is for src/cpu/x86/lapic/secondary.S usage which is only
619 used during ramstage. */
620struct lidtarg idtarg;
621
622static void load_idt(void *table, size_t sz)
623{
624 struct lidtarg lidtarg = {
625 .limit = sz - 1,
626 .base = (uintptr_t)table,
627 };
628
629 asm volatile (
630 "lidt %0"
631 :
632 : "m" (lidtarg)
633 : "memory"
634 );
635
636 if (ENV_RAMSTAGE)
637 memcpy(&idtarg, &lidtarg, sizeof(idtarg));
638}
639
640asmlinkage void exception_init(void)
641{
642 int i;
643 uint16_t segment;
Aaron Durbin4b032e42018-04-20 01:39:30 -0600644
645 segment = get_cs();
Aaron Durbin4b032e42018-04-20 01:39:30 -0600646
647 /* Initialize IDT. */
648 for (i = 0; i < ARRAY_SIZE(idt); i++) {
Arthur Heymans9e1ea542019-11-20 22:09:15 +0100649 idt[i].offset_0 = intr_entries[i];
650 idt[i].segsel = segment;
651 idt[i].flags = IGATE_FLAGS;
652 idt[i].offset_1 = intr_entries[i] >> 16;
Aaron Durbin4b032e42018-04-20 01:39:30 -0600653#if ENV_X86_64
Arthur Heymans9e1ea542019-11-20 22:09:15 +0100654 idt[i].offset_2 = intr_entries[i] >> 32;
Aaron Durbin4b032e42018-04-20 01:39:30 -0600655#endif
656 }
657
Arthur Heymans9e1ea542019-11-20 22:09:15 +0100658 load_idt(idt, sizeof(idt));
Aaron Durbin4b032e42018-04-20 01:39:30 -0600659}