86edccbdd20d7a228534d52959d65fc5d395aee8
[coreboot.git] / src / arch / i386 / lib / exception.c
1 #include <console/console.h>
2 #include <string.h>
3
4 #if CONFIG_GDB_STUB == 1
5
6 /* BUFMAX defines the maximum number of characters in inbound/outbound buffers.
7  * At least NUM_REGBYTES*2 are needed for register packets 
8  */
9 #define BUFMAX 400
10 enum regnames {
11         EAX = 0, ECX, EDX, EBX, ESP, EBP, ESI, EDI,
12         PC /* also known as eip */,
13         PS /* also known as eflags */,
14         CS, SS, DS, ES, FS, GS,
15         NUM_REGS /* Number of registers. */
16 };
17
18 static uint32_t gdb_stub_registers[NUM_REGS];
19
20 #define GDB_SIG0         0     /* Signal 0 */
21 #define GDB_SIGHUP       1     /* Hangup */
22 #define GDB_SIGINT       2     /* Interrupt */
23 #define GDB_SIGQUIT      3     /* Quit */
24 #define GDB_SIGILL       4     /* Illegal instruction */
25 #define GDB_SIGTRAP      5     /* Trace/breakpoint trap */
26 #define GDB_SIGABRT      6     /* Aborted */
27 #define GDB_SIGEMT       7     /* Emulation trap */
28 #define GDB_SIGFPE       8     /* Arithmetic exception */
29 #define GDB_SIGKILL      9     /* Killed */
30 #define GDB_SIGBUS       10    /* Bus error */
31 #define GDB_SIGSEGV      11    /* Segmentation fault */
32 #define GDB_SIGSYS       12    /* Bad system call */
33 #define GDB_SIGPIPE      13    /* Broken pipe */
34 #define GDB_SIGALRM      14    /* Alarm clock */
35 #define GDB_SIGTERM      15    /* Terminated */
36 #define GDB_SIGURG       16    /* Urgent I/O condition */
37 #define GDB_SIGSTOP      17    /* Stopped (signal) */
38 #define GDB_SIGTSTP      18    /* Stopped (user) */
39 #define GDB_SIGCONT      19    /* Continued */
40 #define GDB_SIGCHLD      20    /* Child status changed */
41 #define GDB_SIGTTIN      21    /* Stopped (tty input) */
42 #define GDB_SIGTTOU      22    /* Stopped (tty output) */
43 #define GDB_SIGIO        23    /* I/O possible */
44 #define GDB_SIGXCPU      24    /* CPU time limit exceeded */
45 #define GDB_SIGXFSZ      25    /* File size limit exceeded */
46 #define GDB_SIGVTALRM    26    /* Virtual timer expired */
47 #define GDB_SIGPROF      27    /* Profiling timer expired */
48 #define GDB_SIGWINCH     28    /* Window size changed */
49 #define GDB_SIGLOST      29    /* Resource lost */
50 #define GDB_SIGUSR1      30    /* User defined signal 1 */
51 #define GDB_SUGUSR2      31    /* User defined signal 2 */
52 #define GDB_SIGPWR       32    /* Power fail/restart */
53 #define GDB_SIGPOLL      33    /* Pollable event occurred */
54 #define GDB_SIGWIND      34    /* SIGWIND */
55 #define GDB_SIGPHONE     35    /* SIGPHONE */
56 #define GDB_SIGWAITING   36    /* Process's LWPs are blocked */
57 #define GDB_SIGLWP       37    /* Signal LWP */
58 #define GDB_SIGDANGER    38    /* Swap space dangerously low */
59 #define GDB_SIGGRANT     39    /* Monitor mode granted */
60 #define GDB_SIGRETRACT   40    /* Need to relinquish monitor mode */
61 #define GDB_SIGMSG       41    /* Monitor mode data available */
62 #define GDB_SIGSOUND     42    /* Sound completed */
63 #define GDB_SIGSAK       43    /* Secure attention */
64 #define GDB_SIGPRIO      44    /* SIGPRIO */
65                         
66 #define GDB_SIG33        45    /* Real-time event 33 */
67 #define GDB_SIG34        46    /* Real-time event 34 */
68 #define GDB_SIG35        47    /* Real-time event 35 */
69 #define GDB_SIG36        48    /* Real-time event 36 */
70 #define GDB_SIG37        49    /* Real-time event 37 */
71 #define GDB_SIG38        50    /* Real-time event 38 */
72 #define GDB_SIG39        51    /* Real-time event 39 */
73 #define GDB_SIG40        52    /* Real-time event 40 */
74 #define GDB_SIG41        53    /* Real-time event 41 */
75 #define GDB_SIG42        54    /* Real-time event 42 */
76 #define GDB_SIG43        55    /* Real-time event 43 */
77 #define GDB_SIG44        56    /* Real-time event 44 */
78 #define GDB_SIG45        57    /* Real-time event 45 */
79 #define GDB_SIG46        58    /* Real-time event 46 */
80 #define GDB_SIG47        59    /* Real-time event 47 */
81 #define GDB_SIG48        60    /* Real-time event 48 */
82 #define GDB_SIG49        61    /* Real-time event 49 */
83 #define GDB_SIG50        62    /* Real-time event 50 */
84 #define GDB_SIG51        63    /* Real-time event 51 */
85 #define GDB_SIG52        64    /* Real-time event 52 */
86 #define GDB_SIG53        65    /* Real-time event 53 */
87 #define GDB_SIG54        66    /* Real-time event 54 */
88 #define GDB_SIG55        67    /* Real-time event 55 */
89 #define GDB_SIG56        68    /* Real-time event 56 */
90 #define GDB_SIG57        69    /* Real-time event 57 */
91 #define GDB_SIG58        70    /* Real-time event 58 */
92 #define GDB_SIG59        71    /* Real-time event 59 */
93 #define GDB_SIG60        72    /* Real-time event 60 */
94 #define GDB_SIG61        73    /* Real-time event 61 */
95 #define GDB_SIG62        74    /* Real-time event 62 */
96 #define GDB_SIG63        75    /* Real-time event 63 */
97 #define GDB_SIGCANCEL    76    /* LWP internal signal */
98 #define GDB_SIG32        77    /* Real-time event 32 */
99 #define GDB_SIG64        78    /* Real-time event 64 */
100 #define GDB_SIG65        79    /* Real-time event 65 */
101 #define GDB_SIG66        80    /* Real-time event 66 */
102 #define GDB_SIG67        81    /* Real-time event 67 */
103 #define GDB_SIG68        82    /* Real-time event 68 */
104 #define GDB_SIG69        83    /* Real-time event 69 */
105 #define GDB_SIG70        84    /* Real-time event 70 */
106 #define GDB_SIG71        85    /* Real-time event 71 */
107 #define GDB_SIG72        86    /* Real-time event 72 */
108 #define GDB_SIG73        87    /* Real-time event 73 */
109 #define GDB_SIG74        88    /* Real-time event 74 */
110 #define GDB_SIG75        89    /* Real-time event 75 */
111 #define GDB_SIG76        90    /* Real-time event 76 */
112 #define GDB_SIG77        91    /* Real-time event 77 */
113 #define GDB_SIG78        92    /* Real-time event 78 */
114 #define GDB_SIG79        93    /* Real-time event 79 */
115 #define GDB_SIG80        94    /* Real-time event 80 */
116 #define GDB_SIG81        95    /* Real-time event 81 */
117 #define GDB_SIG82        96    /* Real-time event 82 */
118 #define GDB_SIG83        97    /* Real-time event 83 */
119 #define GDB_SIG84        98    /* Real-time event 84 */
120 #define GDB_SIG85        99    /* Real-time event 85 */
121 #define GDB_SIG86       100    /* Real-time event 86 */
122 #define GDB_SIG87       101    /* Real-time event 87 */
123 #define GDB_SIG88       102    /* Real-time event 88 */
124 #define GDB_SIG89       103    /* Real-time event 89 */
125 #define GDB_SIG90       104    /* Real-time event 90 */
126 #define GDB_SIG91       105    /* Real-time event 91 */
127 #define GDB_SIG92       106    /* Real-time event 92 */
128 #define GDB_SIG93       107    /* Real-time event 93 */
129 #define GDB_SIG94       108    /* Real-time event 94 */
130 #define GDB_SIG95       109    /* Real-time event 95 */
131 #define GDB_SIG96       110    /* Real-time event 96 */
132 #define GDB_SIG97       111    /* Real-time event 97 */
133 #define GDB_SIG98       112    /* Real-time event 98 */
134 #define GDB_SIG99       113    /* Real-time event 99 */
135 #define GDB_SIG100      114    /* Real-time event 100 */
136 #define GDB_SIG101      115    /* Real-time event 101 */
137 #define GDB_SIG102      116    /* Real-time event 102 */
138 #define GDB_SIG103      117    /* Real-time event 103 */
139 #define GDB_SIG104      118    /* Real-time event 104 */
140 #define GDB_SIG105      119    /* Real-time event 105 */
141 #define GDB_SIG106      120    /* Real-time event 106 */
142 #define GDB_SIG107      121    /* Real-time event 107 */
143 #define GDB_SIG108      122    /* Real-time event 108 */
144 #define GDB_SIG109      123    /* Real-time event 109 */
145 #define GDB_SIG110      124    /* Real-time event 110 */
146 #define GDB_SIG111      125    /* Real-time event 111 */
147 #define GDB_SIG112      126    /* Real-time event 112 */
148 #define GDB_SIG113      127    /* Real-time event 113 */
149 #define GDB_SIG114      128    /* Real-time event 114 */
150 #define GDB_SIG115      129    /* Real-time event 115 */
151 #define GDB_SIG116      130    /* Real-time event 116 */
152 #define GDB_SIG117      131    /* Real-time event 117 */
153 #define GDB_SIG118      132    /* Real-time event 118 */
154 #define GDB_SIG119      133    /* Real-time event 119 */
155 #define GDB_SIG120      134    /* Real-time event 120 */
156 #define GDB_SIG121      135    /* Real-time event 121 */
157 #define GDB_SIG122      136    /* Real-time event 122 */
158 #define GDB_SIG123      137    /* Real-time event 123 */
159 #define GDB_SIG124      138    /* Real-time event 124 */
160 #define GDB_SIG125      139    /* Real-time event 125 */
161 #define GDB_SIG126      140    /* Real-time event 126 */
162 #define GDB_SIG127      141    /* Real-time event 127 */
163 #define GDB_SIGINFO     142    /* Information request */
164 #define GDB_UNKNOWN     143    /* Unknown signal */
165 #define GDB_DEFAULT     144    /* error: default signal */
166 /* Mach exceptions */
167 #define GDB_EXC_BAD_ACCESS     145 /* Could not access memory */
168 #define GDB_EXC_BAD_INSTRCTION 146 /* Illegal instruction/operand */
169 #define GDB_EXC_ARITHMETIC     147 /* Arithmetic exception */
170 #define GDB_EXC_EMULATION      148 /* Emulation instruction */
171 #define GDB_EXC_SOFTWARE       149 /* Software generated exception */
172 #define GDB_EXC_BREAKPOINT     150 /* Breakpoint */
173
174
175
176 static unsigned char exception_to_signal[] =
177 {
178         [0]  = GDB_SIGFPE,  /* divide by zero */
179         [1]  = GDB_SIGTRAP, /* debug exception */
180         [2]  = GDB_SIGSEGV, /* NMI Interrupt */
181         [3]  = GDB_SIGTRAP, /* Breakpoint */
182         [4]  = GDB_SIGSEGV, /* into instruction (overflow) */
183         [5]  = GDB_SIGSEGV, /* bound instruction */
184         [6]  = GDB_SIGILL,  /* Invalid opcode */
185         [7]  = GDB_SIGSEGV, /* coprocessor not available */
186         [8]  = GDB_SIGSEGV, /* double fault */
187         [9]  = GDB_SIGFPE,  /* coprocessor segment overrun */
188         [10] = GDB_SIGSEGV, /* Invalid TSS */
189         [11] = GDB_SIGBUS,  /* Segment not present */
190         [12] = GDB_SIGBUS,  /* stack exception */
191         [13] = GDB_SIGSEGV, /* general protection */
192         [14] = GDB_SIGSEGV, /* page fault */
193         [15] = GDB_UNKNOWN, /* reserved */
194         [16] = GDB_SIGEMT,  /* coprocessor error */
195         [17] = GDB_SIGBUS,  /* alignment check */
196         [18] = GDB_SIGSEGV, /* machine check */
197         [19] = GDB_SIGFPE,  /* simd floating point exception */
198         [20] = GDB_UNKNOWN,
199         [21] = GDB_UNKNOWN,
200         [22] = GDB_UNKNOWN,
201         [23] = GDB_UNKNOWN,
202         [24] = GDB_UNKNOWN,
203         [25] = GDB_UNKNOWN,
204         [26] = GDB_UNKNOWN,
205         [27] = GDB_UNKNOWN,
206         [28] = GDB_UNKNOWN,
207         [29] = GDB_UNKNOWN,
208         [30] = GDB_UNKNOWN,
209         [31] = GDB_UNKNOWN,
210         [32] = GDB_SIGINT,  /* User interrupt */
211 };
212
213 static const char hexchars[] = "0123456789abcdef";
214 static char in_buffer[BUFMAX];
215 static char out_buffer[BUFMAX];
216
217
218 static inline void stub_putc(int ch)
219 {
220         console_tx_byte(ch);
221 }
222
223 static inline int stub_getc(void)
224 {
225         return console_rx_byte();
226 }
227
228 static int hex(char ch)
229 {
230         if ((ch >= 'a') && (ch <= 'f'))
231                 return (ch - 'a' + 10);
232         if ((ch >= '0') && (ch <= '9'))
233                 return (ch - '0');
234         if ((ch >= 'A') && (ch <= 'F'))
235                 return (ch - 'A' + 10);
236         return (-1);
237 }
238
239 /*
240  * While we find hexadecimal digits, build an int.
241  * Fals is returned if nothing is parsed true otherwise.
242  */
243 static int parse_ulong(char **ptr, unsigned long *value)
244 {
245         int digit;
246         char *start;
247
248         start = *ptr;
249         *value = 0;
250
251         while((digit = hex(**ptr)) >= 0) {
252                 *value = ((*value) << 4) | digit;
253                 (*ptr)++;
254         }
255         return start != *ptr;
256 }
257
258 /* convert the memory pointed to by mem into hex, placing result in buf */
259 /* return a pointer to the last char put in buf (null) */
260 static void copy_to_hex(char *buf, void *addr, unsigned long count)
261 {
262         unsigned char ch;
263         char *mem = addr;
264
265         while(count--) {
266                 ch = *mem++;
267                 *buf++ = hexchars[ch >> 4];
268                 *buf++ = hexchars[ch & 0x0f];
269         }
270         *buf = 0;
271         return;
272 }
273
274
275 /* convert the hex array pointed to by buf into binary to be placed in mem */
276 /* return a pointer to the character AFTER the last byte written */
277 static void copy_from_hex(void *addr, char *buf, unsigned long count)
278 {
279         unsigned char ch;
280         char *mem = addr;
281
282         while(count--) {
283                 ch = hex (*buf++) << 4;
284                 ch = ch + hex (*buf++);
285                 *mem++ = ch;
286         }
287 }
288
289
290 /* scan for the sequence $<data>#<checksum>     */
291
292 static int get_packet(char *buffer)
293 {
294         unsigned char checksum;
295         unsigned char xmitcsum;
296         int count;
297         char ch;
298
299         /* Wishlit implement a timeout in get_packet */
300         do {
301                 /* wait around for the start character, ignore all other characters */
302                 while ((ch = (stub_getc() & 0x7f)) != '$');
303                 checksum = 0;
304                 xmitcsum = -1;
305
306                 count = 0;
307
308                 /* now, read until a # or end of buffer is found */
309                 while (count < BUFMAX) {
310                         ch = stub_getc() & 0x7f;
311                         if (ch == '#')
312                                 break;
313                         checksum = checksum + ch;
314                         buffer[count] = ch;
315                         count = count + 1;
316                 }
317                 buffer[count] = 0;
318
319                 if (ch == '#') {
320                         xmitcsum = hex(stub_getc() & 0x7f) << 4;
321                         xmitcsum += hex(stub_getc() & 0x7f);
322
323                         if (checksum != xmitcsum) {
324                                 stub_putc('-'); /* failed checksum */
325                         }
326                         else {
327                                 stub_putc('+'); /* successful transfer */
328                         }
329                 }
330         } while(checksum != xmitcsum);
331         return 1;
332 }
333
334 /* send the packet in buffer.*/
335 static void put_packet(char *buffer)
336 {
337         unsigned char checksum;
338         int count;
339         char ch;
340
341         /*  $<packet info>#<checksum>. */
342         do {
343                 stub_putc('$');
344                 checksum = 0;
345                 count = 0;
346
347                 while ((ch = buffer[count])) {
348                         stub_putc(ch);
349                         checksum += ch;
350                         count += 1;
351                 }
352
353                 stub_putc('#');
354                 stub_putc(hexchars[checksum >> 4]);
355                 stub_putc(hexchars[checksum % 16]);
356
357         } while ((stub_getc() & 0x7f) != '+');
358
359 }
360 #endif /* CONFIG_GDB_STUB */
361
362 struct eregs {
363         uint32_t eax, ecx, edx, ebx, esp, ebp, esi, edi;
364         uint32_t vector;
365         uint32_t error_code;
366         uint32_t eip;
367         uint32_t cs;
368         uint32_t eflags;
369 };
370
371 void x86_exception(struct eregs *info)
372 {
373 #if CONFIG_GDB_STUB == 1
374         int signo;
375         memcpy(gdb_stub_registers, info, 8*sizeof(uint32_t));
376         gdb_stub_registers[PC] = info->eip;
377         gdb_stub_registers[CS] = info->cs;
378         gdb_stub_registers[PS] = info->eflags;
379         signo = GDB_UNKNOWN;
380         if (info->vector < sizeof(exception_to_signal)/sizeof(exception_to_signal[0])) {
381                 signo = exception_to_signal[info->vector];
382         }
383         
384         /* reply to the host that an exception has occured */
385         out_buffer[0] = 'S';
386         out_buffer[1] = hexchars[(signo>>4) & 0xf];
387         out_buffer[2] = hexchars[signo & 0xf];
388         out_buffer[3] = '\0';
389         put_packet(out_buffer);
390
391         while(1) {
392                 unsigned long addr, length;
393                 char *ptr;
394                 out_buffer[0] = '\0';
395                 out_buffer[1] = '\0';
396                 if (!get_packet(in_buffer)) {
397                         break;
398                 }
399                 switch(in_buffer[0]) {
400                 case '?': /* last signal */
401                         out_buffer[0] = 'S';
402                         out_buffer[1] = hexchars[(signo >> 4) & 0xf];
403                         out_buffer[2] = hexchars[signo & 0xf];
404                         out_buffer[3] = '\0';
405                         break;
406                 case 'g': /* return the value of the cpu registers */
407                         copy_to_hex(out_buffer, &gdb_stub_registers, sizeof(gdb_stub_registers));
408                         break;
409                 case 'G': /* set the value of the CPU registers - return OK */
410                         copy_from_hex(&gdb_stub_registers, in_buffer + 1, sizeof(gdb_stub_registers));
411                         memcpy(info, gdb_stub_registers, 8*sizeof(uint32_t));
412                         info->eip    = gdb_stub_registers[PC];
413                         info->cs     = gdb_stub_registers[CS];
414                         info->eflags = gdb_stub_registers[PS];
415                         memcpy(out_buffer, "OK",3);
416                         break;
417                 case 'm':
418                         /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
419                         ptr = &in_buffer[1];
420                         if (    parse_ulong(&ptr, &addr) && 
421                                 (*ptr++ == ',') &&
422                                 parse_ulong(&ptr, &length)) {
423                                 copy_to_hex(out_buffer, addr, length);
424                         } else {
425                                 memcpy(out_buffer, "E01", 4);
426                         }
427                         break;
428                 case 'M':
429                         /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA return OK */
430                         ptr = &in_buffer[1];
431                         if (    parse_ulong(&ptr, &addr) && 
432                                 (*(ptr++) == ',') &&
433                                 parse_ulong(&ptr, &length) &&
434                                 (*(ptr++) == ':')) {
435                                 copy_from_hex((void *)addr, ptr, length);
436                                 memcpy(out_buffer, "OK", 3);
437                         }
438                         else {
439                                 memcpy(out_buffer, "E02", 4);
440                         }
441                         break;
442                 case 's':
443                 case 'c':
444                         /* cAA..AA    Continue at address AA..AA(optional) */
445                         /* sAA..AA    Step one instruction from AA..AA(optional) */
446                         ptr = &in_buffer[1];
447                         if (parse_ulong(&ptr, &addr)) {
448                                 info->eip = addr;
449                         }
450
451                         /* Clear the trace bit */
452                         info->eflags &= ~(1 << 8);
453                         /* Set the trace bit if we are single stepping */
454                         if (in_buffer[0] == 's') {
455                                 info->eflags |= (1 << 8);
456                         }
457                         return;
458                         break;
459                 case 'D':
460                         memcpy(out_buffer, "OK", 3);
461                         break;
462                 case 'k':  /* kill request? */
463                         break;
464                 case 'q':  /* query */
465                         break;
466                 case 'z':  /* z0AAAA,LLLL remove memory breakpoint */
467                            /* z1AAAA,LLLL remove hardware breakpoint */
468                            /* z2AAAA,LLLL remove write watchpoint */
469                            /* z3AAAA,LLLL remove read watchpoint */
470                            /* z4AAAA,LLLL remove access watchpoint */
471                 case 'Z':  /* Z0AAAA,LLLL insert memory breakpoint */
472                            /* Z1AAAA,LLLL insert hardware breakpoint */
473                            /* Z2AAAA,LLLL insert write watchpoint */
474                            /* Z3AAAA,LLLL insert read watchpoint */
475                            /* Z4AAAA,LLLL insert access watchpoint */
476                         break;
477                 default:
478                         break;
479                 }
480                 put_packet(out_buffer);
481         }
482 #else /* !CONFIG_GDB_STUB */
483         printk_emerg(
484                 "Unexpected Exception: %d @ %02x:%08lx - Halting\n"
485                 "Code: %d eflags: %08lx\n"
486                 "eax: %08lx ebx: %08lx ecx: %08lx edx: %08lx\n"
487                 "edi: %08lx esi: %08lx ebp: %08lx esp: %08lx\n",
488                 info->vector, info->cs, info->eip,
489                 info->error_code, info->eflags,
490                 info->eax, info->ebx, info->ecx, info->edx,
491                 info->edi, info->esi, info->ebp, info->esp);
492         die("");
493 #endif
494 }