Martin Roth | 9df9e939 | 2016-01-12 15:55:28 -0700 | [diff] [blame] | 1 | /* |
| 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 Banik | 8cfd76d | 2018-04-18 11:48:58 +0530 | [diff] [blame] | 14 | #include <arch/cpu.h> |
Aaron Durbin | 4b032e4 | 2018-04-20 01:39:30 -0600 | [diff] [blame] | 15 | #include <arch/early_variables.h> |
| 16 | #include <arch/exception.h> |
| 17 | #include <commonlib/helpers.h> |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 18 | #include <console/console.h> |
Kyösti Mälkki | f2f7f03 | 2014-04-04 15:05:28 +0300 | [diff] [blame] | 19 | #include <console/streams.h> |
Aaron Durbin | aae73d7 | 2018-04-21 00:10:10 -0600 | [diff] [blame] | 20 | #include <cpu/x86/cr.h> |
Subrata Banik | 8cfd76d | 2018-04-18 11:48:58 +0530 | [diff] [blame] | 21 | #include <cpu/x86/lapic.h> |
Aaron Durbin | 4b032e4 | 2018-04-20 01:39:30 -0600 | [diff] [blame] | 22 | #include <stdint.h> |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 23 | #include <string.h> |
| 24 | |
Martin Roth | 898a775 | 2017-06-01 11:39:59 -0600 | [diff] [blame] | 25 | #if IS_ENABLED(CONFIG_GDB_STUB) |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 26 | |
| 27 | /* BUFMAX defines the maximum number of characters in inbound/outbound buffers. |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 28 | * At least NUM_REGBYTES*2 are needed for register packets |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 29 | */ |
| 30 | #define BUFMAX 400 |
| 31 | enum 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 | |
| 39 | static 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 Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 86 | |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 87 | #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 Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 197 | static unsigned char exception_to_signal[] = { |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 198 | [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 | |
| 233 | static const char hexchars[] = "0123456789abcdef"; |
| 234 | static char in_buffer[BUFMAX]; |
| 235 | static char out_buffer[BUFMAX]; |
| 236 | |
| 237 | |
| 238 | static inline void stub_putc(int ch) |
| 239 | { |
Kyösti Mälkki | f2f7f03 | 2014-04-04 15:05:28 +0300 | [diff] [blame] | 240 | gdb_tx_byte(ch); |
| 241 | } |
| 242 | |
| 243 | static inline void stub_flush(void) |
| 244 | { |
| 245 | gdb_tx_flush(); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static inline int stub_getc(void) |
| 249 | { |
Kyösti Mälkki | f2f7f03 | 2014-04-04 15:05:28 +0300 | [diff] [blame] | 250 | return gdb_rx_byte(); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | static 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 | */ |
| 268 | static int parse_ulong(char **ptr, unsigned long *value) |
| 269 | { |
| 270 | int digit; |
| 271 | char *start; |
| 272 | |
| 273 | start = *ptr; |
| 274 | *value = 0; |
| 275 | |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 276 | while ((digit = hex(**ptr)) >= 0) { |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 277 | *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) */ |
| 285 | static void copy_to_hex(char *buf, void *addr, unsigned long count) |
| 286 | { |
| 287 | unsigned char ch; |
| 288 | char *mem = addr; |
| 289 | |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 290 | while (count--) { |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 291 | ch = *mem++; |
| 292 | *buf++ = hexchars[ch >> 4]; |
| 293 | *buf++ = hexchars[ch & 0x0f]; |
| 294 | } |
| 295 | *buf = 0; |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 296 | } |
| 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 */ |
| 301 | static void copy_from_hex(void *addr, char *buf, unsigned long count) |
| 302 | { |
| 303 | unsigned char ch; |
| 304 | char *mem = addr; |
| 305 | |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 306 | while (count--) { |
| 307 | ch = hex(*buf++) << 4; |
| 308 | ch = ch + hex(*buf++); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 309 | *mem++ = ch; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /* scan for the sequence $<data>#<checksum> */ |
| 315 | |
| 316 | static 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 Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 325 | /* wait around for the start character, ignore all other |
| 326 | * characters |
| 327 | */ |
Lee Leahy | 0b5678f | 2017-03-16 16:01:40 -0700 | [diff] [blame] | 328 | while ((ch = (stub_getc() & 0x7f)) != '$') |
| 329 | ; |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 330 | 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älkki | f2f7f03 | 2014-04-04 15:05:28 +0300 | [diff] [blame] | 352 | stub_flush(); |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 353 | } else { |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 354 | stub_putc('+'); /* successful transfer */ |
Kyösti Mälkki | f2f7f03 | 2014-04-04 15:05:28 +0300 | [diff] [blame] | 355 | stub_flush(); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 358 | } while (checksum != xmitcsum); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 359 | return 1; |
| 360 | } |
| 361 | |
| 362 | /* send the packet in buffer.*/ |
| 363 | static 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älkki | f2f7f03 | 2014-04-04 15:05:28 +0300 | [diff] [blame] | 384 | stub_flush(); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 385 | |
| 386 | } while ((stub_getc() & 0x7f) != '+'); |
| 387 | |
| 388 | } |
| 389 | #endif /* CONFIG_GDB_STUB */ |
| 390 | |
Stefan Reinauer | 42dc721 | 2009-10-24 00:47:07 +0000 | [diff] [blame] | 391 | #include <arch/registers.h> |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 392 | |
Maciej Pijanka | ea92185 | 2009-10-27 14:29:29 +0000 | [diff] [blame] | 393 | void x86_exception(struct eregs *info); |
| 394 | |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 395 | void x86_exception(struct eregs *info) |
| 396 | { |
Martin Roth | 898a775 | 2017-06-01 11:39:59 -0600 | [diff] [blame] | 397 | #if IS_ENABLED(CONFIG_GDB_STUB) |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 398 | 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 Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 404 | if (info->vector < ARRAY_SIZE(exception_to_signal)) |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 405 | signo = exception_to_signal[info->vector]; |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 406 | |
Martin Roth | 2ed0aa2 | 2016-01-05 20:58:58 -0700 | [diff] [blame] | 407 | /* reply to the host that an exception has occurred */ |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 408 | 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 Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 414 | while (1) { |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 415 | unsigned long addr, length; |
| 416 | char *ptr; |
| 417 | out_buffer[0] = '\0'; |
| 418 | out_buffer[1] = '\0'; |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 419 | if (!get_packet(in_buffer)) |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 420 | break; |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 421 | switch (in_buffer[0]) { |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 422 | 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 HAOUAS | 777ea89 | 2016-07-29 07:40:41 +0200 | [diff] [blame] | 428 | case 'g': /* return the value of the CPU registers */ |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 429 | copy_to_hex(out_buffer, &gdb_stub_registers, |
| 430 | sizeof(gdb_stub_registers)); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 431 | break; |
| 432 | case 'G': /* set the value of the CPU registers - return OK */ |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 433 | copy_from_hex(&gdb_stub_registers, in_buffer + 1, |
| 434 | sizeof(gdb_stub_registers)); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 435 | 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 Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 439 | memcpy(out_buffer, "OK", 3); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 440 | break; |
| 441 | case 'm': |
| 442 | /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ |
| 443 | ptr = &in_buffer[1]; |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 444 | if (parse_ulong(&ptr, &addr) && |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 445 | (*ptr++ == ',') && |
| 446 | parse_ulong(&ptr, &length)) { |
Eric Biederman | a9e632c | 2004-11-18 22:38:08 +0000 | [diff] [blame] | 447 | copy_to_hex(out_buffer, (void *)addr, length); |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 448 | } else |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 449 | memcpy(out_buffer, "E01", 4); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 450 | break; |
| 451 | case 'M': |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 452 | /* MAA..AA,LLLL: Write LLLL bytes at address AA.AA |
| 453 | * return OK |
| 454 | */ |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 455 | ptr = &in_buffer[1]; |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 456 | if (parse_ulong(&ptr, &addr) && |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 457 | (*(ptr++) == ',') && |
| 458 | parse_ulong(&ptr, &length) && |
| 459 | (*(ptr++) == ':')) { |
| 460 | copy_from_hex((void *)addr, ptr, length); |
| 461 | memcpy(out_buffer, "OK", 3); |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 462 | } else |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 463 | memcpy(out_buffer, "E02", 4); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 464 | break; |
| 465 | case 's': |
| 466 | case 'c': |
Lee Leahy | 6f80ccc | 2017-03-16 15:18:22 -0700 | [diff] [blame] | 467 | /* cAA..AA Continue at address AA..AA(optional) |
| 468 | * sAA..AA Step one instruction from AA..AA(optional) |
| 469 | */ |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 470 | ptr = &in_buffer[1]; |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 471 | if (parse_ulong(&ptr, &addr)) |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 472 | info->eip = addr; |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 473 | |
| 474 | /* Clear the trace bit */ |
| 475 | info->eflags &= ~(1 << 8); |
| 476 | /* Set the trace bit if we are single stepping */ |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 477 | if (in_buffer[0] == 's') |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 478 | info->eflags |= (1 << 8); |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 479 | return; |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 480 | 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 Gagniuc | de415eb | 2012-08-15 06:45:18 -0500 | [diff] [blame] | 504 | #define MDUMP_SIZE 0x80 |
Subrata Banik | 8cfd76d | 2018-04-18 11:48:58 +0530 | [diff] [blame] | 505 | unsigned int logical_processor = 0; |
| 506 | |
| 507 | #if ENV_RAMSTAGE |
| 508 | logical_processor = cpu_index(); |
| 509 | #endif |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 510 | printk(BIOS_EMERG, |
Subrata Banik | 8cfd76d | 2018-04-18 11:48:58 +0530 | [diff] [blame] | 511 | "CPU Index %d - APIC %d Unexpected Exception:" |
| 512 | "%d @ %02x:%08x - Halting\n" |
Aaron Durbin | aae73d7 | 2018-04-21 00:10:10 -0600 | [diff] [blame] | 513 | "Code: %d eflags: %08x cr2: %08x\n" |
Myles Watson | c4ddbff | 2009-02-09 17:52:54 +0000 | [diff] [blame] | 514 | "eax: %08x ebx: %08x ecx: %08x edx: %08x\n" |
| 515 | "edi: %08x esi: %08x ebp: %08x esp: %08x\n", |
Subrata Banik | 8cfd76d | 2018-04-18 11:48:58 +0530 | [diff] [blame] | 516 | logical_processor, (unsigned int)lapicid(), |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 517 | info->vector, info->cs, info->eip, |
Aaron Durbin | aae73d7 | 2018-04-21 00:10:10 -0600 | [diff] [blame] | 518 | info->error_code, info->eflags, read_cr2(), |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 519 | info->eax, info->ebx, info->ecx, info->edx, |
| 520 | info->edi, info->esi, info->ebp, info->esp); |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 521 | u8 *code = (u8 *)((uintptr_t)info->eip - (MDUMP_SIZE >> 1)); |
Alexandru Gagniuc | de415eb | 2012-08-15 06:45:18 -0500 | [diff] [blame] | 522 | /* 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 Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 525 | code = (u8 *)((uintptr_t)code & ~0x7); |
Alexandru Gagniuc | de415eb | 2012-08-15 06:45:18 -0500 | [diff] [blame] | 526 | int i; |
Lee Leahy | 9c7c6f7 | 2017-03-16 11:24:09 -0700 | [diff] [blame] | 527 | for (i = 0; i < MDUMP_SIZE; i++) { |
Lee Leahy | 024b13d | 2017-03-16 13:41:11 -0700 | [diff] [blame] | 528 | if ((i & 0x07) == 0) |
Stefan Reinauer | 9693885 | 2015-06-18 01:23:48 -0700 | [diff] [blame] | 529 | printk(BIOS_EMERG, "\n%p:\t", code + i); |
Alexandru Gagniuc | de415eb | 2012-08-15 06:45:18 -0500 | [diff] [blame] | 530 | printk(BIOS_EMERG, "%.2x ", code[i]); |
| 531 | } |
Eric Biederman | 432aa6a | 2004-10-30 22:59:35 +0000 | [diff] [blame] | 532 | die(""); |
| 533 | #endif |
| 534 | } |
Aaron Durbin | 4b032e4 | 2018-04-20 01:39:30 -0600 | [diff] [blame] | 535 | |
| 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 | |
| 543 | struct 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äfer | 773cc1b | 2018-10-10 14:22:48 +0200 | [diff] [blame] | 556 | 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 Durbin | 4b032e4 | 2018-04-20 01:39:30 -0600 | [diff] [blame] | 558 | the symbols. The IDT is initialized at runtime when exception_init() is |
| 559 | called. */ |
| 560 | extern u8 vec0[], vec1[], vec2[], vec3[], vec4[], vec5[], vec6[], vec7[]; |
| 561 | extern u8 vec8[], vec9[], vec10[], vec11[], vec12[], vec13[], vec14[], vec15[]; |
| 562 | extern u8 vec16[], vec17[], vec18[], vec19[]; |
| 563 | |
| 564 | static 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 | |
| 572 | static struct intr_gate idt[ARRAY_SIZE(intr_entries)] __aligned(8) CAR_GLOBAL; |
| 573 | |
| 574 | static 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 | |
| 588 | struct 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. */ |
| 599 | struct lidtarg idtarg; |
| 600 | |
| 601 | static 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 | |
| 619 | asmlinkage void exception_init(void) |
| 620 | { |
| 621 | int i; |
| 622 | uint16_t segment; |
Richard Spiegel | b1299c7 | 2018-08-07 14:35:54 -0700 | [diff] [blame] | 623 | struct intr_gate *gates; |
Aaron Durbin | 4b032e4 | 2018-04-20 01:39:30 -0600 | [diff] [blame] | 624 | |
| 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 | } |