Commit | Line | Data |
---|---|---|
53197fc4 JW |
1 | /* |
2 | * Kernel Debug Core | |
3 | * | |
4 | * Maintainer: Jason Wessel <jason.wessel@windriver.com> | |
5 | * | |
6 | * Copyright (C) 2000-2001 VERITAS Software Corporation. | |
7 | * Copyright (C) 2002-2004 Timesys Corporation | |
8 | * Copyright (C) 2003-2004 Amit S. Kale <amitkale@linsyssoft.com> | |
9 | * Copyright (C) 2004 Pavel Machek <pavel@suse.cz> | |
10 | * Copyright (C) 2004-2006 Tom Rini <trini@kernel.crashing.org> | |
11 | * Copyright (C) 2004-2006 LinSysSoft Technologies Pvt. Ltd. | |
12 | * Copyright (C) 2005-2009 Wind River Systems, Inc. | |
13 | * Copyright (C) 2007 MontaVista Software, Inc. | |
14 | * Copyright (C) 2008 Red Hat, Inc., Ingo Molnar <mingo@redhat.com> | |
15 | * | |
16 | * Contributors at various stages not listed above: | |
17 | * Jason Wessel ( jason.wessel@windriver.com ) | |
18 | * George Anzinger <george@mvista.com> | |
19 | * Anurekh Saxena (anurekh.saxena@timesys.com) | |
20 | * Lake Stevens Instrument Division (Glenn Engel) | |
21 | * Jim Kingdon, Cygnus Support. | |
22 | * | |
23 | * Original KGDB stub: David Grothe <dave@gcom.com>, | |
24 | * Tigran Aivazian <tigran@sco.com> | |
25 | * | |
26 | * This file is licensed under the terms of the GNU General Public License | |
27 | * version 2. This program is licensed "as is" without any warranty of any | |
28 | * kind, whether express or implied. | |
29 | */ | |
30 | ||
31 | #include <linux/kernel.h> | |
32 | #include <linux/kgdb.h> | |
f5316b4a | 33 | #include <linux/kdb.h> |
53197fc4 JW |
34 | #include <linux/reboot.h> |
35 | #include <linux/uaccess.h> | |
36 | #include <asm/cacheflush.h> | |
37 | #include <asm/unaligned.h> | |
38 | #include "debug_core.h" | |
39 | ||
40 | #define KGDB_MAX_THREAD_QUERY 17 | |
41 | ||
42 | /* Our I/O buffers. */ | |
43 | static char remcom_in_buffer[BUFMAX]; | |
44 | static char remcom_out_buffer[BUFMAX]; | |
45 | ||
46 | /* Storage for the registers, in GDB format. */ | |
47 | static unsigned long gdb_regs[(NUMREGBYTES + | |
48 | sizeof(unsigned long) - 1) / | |
49 | sizeof(unsigned long)]; | |
50 | ||
51 | /* | |
52 | * GDB remote protocol parser: | |
53 | */ | |
54 | ||
f5316b4a JW |
55 | #ifdef CONFIG_KGDB_KDB |
56 | static int gdbstub_read_wait(void) | |
57 | { | |
58 | int ret = -1; | |
59 | int i; | |
60 | ||
61 | /* poll any additional I/O interfaces that are defined */ | |
62 | while (ret < 0) | |
63 | for (i = 0; kdb_poll_funcs[i] != NULL; i++) { | |
64 | ret = kdb_poll_funcs[i](); | |
65 | if (ret > 0) | |
66 | break; | |
67 | } | |
68 | return ret; | |
69 | } | |
70 | #else | |
71 | static int gdbstub_read_wait(void) | |
72 | { | |
73 | int ret = dbg_io_ops->read_char(); | |
74 | while (ret == NO_POLL_CHAR) | |
75 | ret = dbg_io_ops->read_char(); | |
76 | return ret; | |
77 | } | |
78 | #endif | |
53197fc4 JW |
79 | /* scan for the sequence $<data>#<checksum> */ |
80 | static void get_packet(char *buffer) | |
81 | { | |
82 | unsigned char checksum; | |
83 | unsigned char xmitcsum; | |
84 | int count; | |
85 | char ch; | |
86 | ||
87 | do { | |
88 | /* | |
89 | * Spin and wait around for the start character, ignore all | |
90 | * other characters: | |
91 | */ | |
f5316b4a | 92 | while ((ch = (gdbstub_read_wait())) != '$') |
53197fc4 JW |
93 | /* nothing */; |
94 | ||
95 | kgdb_connected = 1; | |
96 | checksum = 0; | |
97 | xmitcsum = -1; | |
98 | ||
99 | count = 0; | |
100 | ||
101 | /* | |
102 | * now, read until a # or end of buffer is found: | |
103 | */ | |
104 | while (count < (BUFMAX - 1)) { | |
f5316b4a | 105 | ch = gdbstub_read_wait(); |
53197fc4 JW |
106 | if (ch == '#') |
107 | break; | |
108 | checksum = checksum + ch; | |
109 | buffer[count] = ch; | |
110 | count = count + 1; | |
111 | } | |
112 | buffer[count] = 0; | |
113 | ||
114 | if (ch == '#') { | |
a9fa20a7 AS |
115 | xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4; |
116 | xmitcsum += hex_to_bin(gdbstub_read_wait()); | |
53197fc4 JW |
117 | |
118 | if (checksum != xmitcsum) | |
119 | /* failed checksum */ | |
120 | dbg_io_ops->write_char('-'); | |
121 | else | |
122 | /* successful transfer */ | |
123 | dbg_io_ops->write_char('+'); | |
124 | if (dbg_io_ops->flush) | |
125 | dbg_io_ops->flush(); | |
126 | } | |
127 | } while (checksum != xmitcsum); | |
128 | } | |
129 | ||
130 | /* | |
131 | * Send the packet in buffer. | |
132 | * Check for gdb connection if asked for. | |
133 | */ | |
134 | static void put_packet(char *buffer) | |
135 | { | |
136 | unsigned char checksum; | |
137 | int count; | |
138 | char ch; | |
139 | ||
140 | /* | |
141 | * $<packet info>#<checksum>. | |
142 | */ | |
143 | while (1) { | |
144 | dbg_io_ops->write_char('$'); | |
145 | checksum = 0; | |
146 | count = 0; | |
147 | ||
148 | while ((ch = buffer[count])) { | |
149 | dbg_io_ops->write_char(ch); | |
150 | checksum += ch; | |
151 | count++; | |
152 | } | |
153 | ||
154 | dbg_io_ops->write_char('#'); | |
155 | dbg_io_ops->write_char(hex_asc_hi(checksum)); | |
156 | dbg_io_ops->write_char(hex_asc_lo(checksum)); | |
157 | if (dbg_io_ops->flush) | |
158 | dbg_io_ops->flush(); | |
159 | ||
160 | /* Now see what we get in reply. */ | |
f5316b4a | 161 | ch = gdbstub_read_wait(); |
53197fc4 JW |
162 | |
163 | if (ch == 3) | |
f5316b4a | 164 | ch = gdbstub_read_wait(); |
53197fc4 JW |
165 | |
166 | /* If we get an ACK, we are done. */ | |
167 | if (ch == '+') | |
168 | return; | |
169 | ||
170 | /* | |
171 | * If we get the start of another packet, this means | |
172 | * that GDB is attempting to reconnect. We will NAK | |
173 | * the packet being sent, and stop trying to send this | |
174 | * packet. | |
175 | */ | |
176 | if (ch == '$') { | |
177 | dbg_io_ops->write_char('-'); | |
178 | if (dbg_io_ops->flush) | |
179 | dbg_io_ops->flush(); | |
180 | return; | |
181 | } | |
182 | } | |
183 | } | |
184 | ||
185 | static char gdbmsgbuf[BUFMAX + 1]; | |
186 | ||
187 | void gdbstub_msg_write(const char *s, int len) | |
188 | { | |
189 | char *bufptr; | |
190 | int wcount; | |
191 | int i; | |
192 | ||
a0de055c JW |
193 | if (len == 0) |
194 | len = strlen(s); | |
195 | ||
53197fc4 JW |
196 | /* 'O'utput */ |
197 | gdbmsgbuf[0] = 'O'; | |
198 | ||
199 | /* Fill and send buffers... */ | |
200 | while (len > 0) { | |
201 | bufptr = gdbmsgbuf + 1; | |
202 | ||
203 | /* Calculate how many this time */ | |
204 | if ((len << 1) > (BUFMAX - 2)) | |
205 | wcount = (BUFMAX - 2) >> 1; | |
206 | else | |
207 | wcount = len; | |
208 | ||
209 | /* Pack in hex chars */ | |
210 | for (i = 0; i < wcount; i++) | |
211 | bufptr = pack_hex_byte(bufptr, s[i]); | |
212 | *bufptr = '\0'; | |
213 | ||
214 | /* Move up */ | |
215 | s += wcount; | |
216 | len -= wcount; | |
217 | ||
218 | /* Write packet */ | |
219 | put_packet(gdbmsgbuf); | |
220 | } | |
221 | } | |
222 | ||
223 | /* | |
224 | * Convert the memory pointed to by mem into hex, placing result in | |
225 | * buf. Return a pointer to the last char put in buf (null). May | |
226 | * return an error. | |
227 | */ | |
55751145 | 228 | char *kgdb_mem2hex(char *mem, char *buf, int count) |
53197fc4 JW |
229 | { |
230 | char *tmp; | |
231 | int err; | |
232 | ||
233 | /* | |
234 | * We use the upper half of buf as an intermediate buffer for the | |
235 | * raw memory copy. Hex conversion will work against this one. | |
236 | */ | |
237 | tmp = buf + count; | |
238 | ||
239 | err = probe_kernel_read(tmp, mem, count); | |
55751145 JW |
240 | if (err) |
241 | return NULL; | |
242 | while (count > 0) { | |
243 | buf = pack_hex_byte(buf, *tmp); | |
244 | tmp++; | |
245 | count--; | |
53197fc4 | 246 | } |
55751145 | 247 | *buf = 0; |
53197fc4 | 248 | |
55751145 | 249 | return buf; |
53197fc4 JW |
250 | } |
251 | ||
252 | /* | |
253 | * Convert the hex array pointed to by buf into binary to be placed in | |
254 | * mem. Return a pointer to the character AFTER the last byte | |
255 | * written. May return an error. | |
256 | */ | |
257 | int kgdb_hex2mem(char *buf, char *mem, int count) | |
258 | { | |
259 | char *tmp_raw; | |
260 | char *tmp_hex; | |
261 | ||
262 | /* | |
263 | * We use the upper half of buf as an intermediate buffer for the | |
264 | * raw memory that is converted from hex. | |
265 | */ | |
266 | tmp_raw = buf + count * 2; | |
267 | ||
268 | tmp_hex = tmp_raw - 1; | |
269 | while (tmp_hex >= buf) { | |
270 | tmp_raw--; | |
a9fa20a7 AS |
271 | *tmp_raw = hex_to_bin(*tmp_hex--); |
272 | *tmp_raw |= hex_to_bin(*tmp_hex--) << 4; | |
53197fc4 JW |
273 | } |
274 | ||
275 | return probe_kernel_write(mem, tmp_raw, count); | |
276 | } | |
277 | ||
278 | /* | |
279 | * While we find nice hex chars, build a long_val. | |
280 | * Return number of chars processed. | |
281 | */ | |
282 | int kgdb_hex2long(char **ptr, unsigned long *long_val) | |
283 | { | |
284 | int hex_val; | |
285 | int num = 0; | |
286 | int negate = 0; | |
287 | ||
288 | *long_val = 0; | |
289 | ||
290 | if (**ptr == '-') { | |
291 | negate = 1; | |
292 | (*ptr)++; | |
293 | } | |
294 | while (**ptr) { | |
a9fa20a7 | 295 | hex_val = hex_to_bin(**ptr); |
53197fc4 JW |
296 | if (hex_val < 0) |
297 | break; | |
298 | ||
299 | *long_val = (*long_val << 4) | hex_val; | |
300 | num++; | |
301 | (*ptr)++; | |
302 | } | |
303 | ||
304 | if (negate) | |
305 | *long_val = -*long_val; | |
306 | ||
307 | return num; | |
308 | } | |
309 | ||
310 | /* | |
311 | * Copy the binary array pointed to by buf into mem. Fix $, #, and | |
312 | * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success. | |
313 | * The input buf is overwitten with the result to write to mem. | |
314 | */ | |
315 | static int kgdb_ebin2mem(char *buf, char *mem, int count) | |
316 | { | |
317 | int size = 0; | |
318 | char *c = buf; | |
319 | ||
320 | while (count-- > 0) { | |
321 | c[size] = *buf++; | |
322 | if (c[size] == 0x7d) | |
323 | c[size] = *buf++ ^ 0x20; | |
324 | size++; | |
325 | } | |
326 | ||
327 | return probe_kernel_write(mem, c, size); | |
328 | } | |
329 | ||
534af108 JW |
330 | #if DBG_MAX_REG_NUM > 0 |
331 | void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs) | |
332 | { | |
333 | int i; | |
334 | int idx = 0; | |
335 | char *ptr = (char *)gdb_regs; | |
336 | ||
337 | for (i = 0; i < DBG_MAX_REG_NUM; i++) { | |
338 | dbg_get_reg(i, ptr + idx, regs); | |
339 | idx += dbg_reg_def[i].size; | |
340 | } | |
341 | } | |
342 | ||
343 | void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs) | |
344 | { | |
345 | int i; | |
346 | int idx = 0; | |
347 | char *ptr = (char *)gdb_regs; | |
348 | ||
349 | for (i = 0; i < DBG_MAX_REG_NUM; i++) { | |
350 | dbg_set_reg(i, ptr + idx, regs); | |
351 | idx += dbg_reg_def[i].size; | |
352 | } | |
353 | } | |
354 | #endif /* DBG_MAX_REG_NUM > 0 */ | |
355 | ||
53197fc4 JW |
356 | /* Write memory due to an 'M' or 'X' packet. */ |
357 | static int write_mem_msg(int binary) | |
358 | { | |
359 | char *ptr = &remcom_in_buffer[1]; | |
360 | unsigned long addr; | |
361 | unsigned long length; | |
362 | int err; | |
363 | ||
364 | if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' && | |
365 | kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') { | |
366 | if (binary) | |
367 | err = kgdb_ebin2mem(ptr, (char *)addr, length); | |
368 | else | |
369 | err = kgdb_hex2mem(ptr, (char *)addr, length); | |
370 | if (err) | |
371 | return err; | |
372 | if (CACHE_FLUSH_IS_SAFE) | |
373 | flush_icache_range(addr, addr + length); | |
374 | return 0; | |
375 | } | |
376 | ||
377 | return -EINVAL; | |
378 | } | |
379 | ||
380 | static void error_packet(char *pkt, int error) | |
381 | { | |
382 | error = -error; | |
383 | pkt[0] = 'E'; | |
384 | pkt[1] = hex_asc[(error / 10)]; | |
385 | pkt[2] = hex_asc[(error % 10)]; | |
386 | pkt[3] = '\0'; | |
387 | } | |
388 | ||
389 | /* | |
390 | * Thread ID accessors. We represent a flat TID space to GDB, where | |
391 | * the per CPU idle threads (which under Linux all have PID 0) are | |
392 | * remapped to negative TIDs. | |
393 | */ | |
394 | ||
84a0bd5b | 395 | #define BUF_THREAD_ID_SIZE 8 |
53197fc4 JW |
396 | |
397 | static char *pack_threadid(char *pkt, unsigned char *id) | |
398 | { | |
84a0bd5b JW |
399 | unsigned char *limit; |
400 | int lzero = 1; | |
401 | ||
402 | limit = id + (BUF_THREAD_ID_SIZE / 2); | |
403 | while (id < limit) { | |
404 | if (!lzero || *id != 0) { | |
405 | pkt = pack_hex_byte(pkt, *id); | |
406 | lzero = 0; | |
407 | } | |
408 | id++; | |
409 | } | |
53197fc4 | 410 | |
84a0bd5b JW |
411 | if (lzero) |
412 | pkt = pack_hex_byte(pkt, 0); | |
53197fc4 JW |
413 | |
414 | return pkt; | |
415 | } | |
416 | ||
417 | static void int_to_threadref(unsigned char *id, int value) | |
418 | { | |
84a0bd5b | 419 | put_unaligned_be32(value, id); |
53197fc4 JW |
420 | } |
421 | ||
422 | static struct task_struct *getthread(struct pt_regs *regs, int tid) | |
423 | { | |
424 | /* | |
425 | * Non-positive TIDs are remapped to the cpu shadow information | |
426 | */ | |
427 | if (tid == 0 || tid == -1) | |
428 | tid = -atomic_read(&kgdb_active) - 2; | |
429 | if (tid < -1 && tid > -NR_CPUS - 2) { | |
430 | if (kgdb_info[-tid - 2].task) | |
431 | return kgdb_info[-tid - 2].task; | |
432 | else | |
433 | return idle_task(-tid - 2); | |
434 | } | |
435 | if (tid <= 0) { | |
436 | printk(KERN_ERR "KGDB: Internal thread select error\n"); | |
437 | dump_stack(); | |
438 | return NULL; | |
439 | } | |
440 | ||
441 | /* | |
442 | * find_task_by_pid_ns() does not take the tasklist lock anymore | |
443 | * but is nicely RCU locked - hence is a pretty resilient | |
444 | * thing to use: | |
445 | */ | |
446 | return find_task_by_pid_ns(tid, &init_pid_ns); | |
447 | } | |
448 | ||
449 | ||
450 | /* | |
451 | * Remap normal tasks to their real PID, | |
452 | * CPU shadow threads are mapped to -CPU - 2 | |
453 | */ | |
454 | static inline int shadow_pid(int realpid) | |
455 | { | |
456 | if (realpid) | |
457 | return realpid; | |
458 | ||
459 | return -raw_smp_processor_id() - 2; | |
460 | } | |
461 | ||
462 | /* | |
463 | * All the functions that start with gdb_cmd are the various | |
464 | * operations to implement the handlers for the gdbserial protocol | |
465 | * where KGDB is communicating with an external debugger | |
466 | */ | |
467 | ||
468 | /* Handle the '?' status packets */ | |
469 | static void gdb_cmd_status(struct kgdb_state *ks) | |
470 | { | |
471 | /* | |
472 | * We know that this packet is only sent | |
473 | * during initial connect. So to be safe, | |
474 | * we clear out our breakpoints now in case | |
475 | * GDB is reconnecting. | |
476 | */ | |
477 | dbg_remove_all_break(); | |
478 | ||
479 | remcom_out_buffer[0] = 'S'; | |
480 | pack_hex_byte(&remcom_out_buffer[1], ks->signo); | |
481 | } | |
482 | ||
55751145 | 483 | static void gdb_get_regs_helper(struct kgdb_state *ks) |
53197fc4 JW |
484 | { |
485 | struct task_struct *thread; | |
486 | void *local_debuggerinfo; | |
487 | int i; | |
488 | ||
489 | thread = kgdb_usethread; | |
490 | if (!thread) { | |
491 | thread = kgdb_info[ks->cpu].task; | |
492 | local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo; | |
493 | } else { | |
494 | local_debuggerinfo = NULL; | |
495 | for_each_online_cpu(i) { | |
496 | /* | |
497 | * Try to find the task on some other | |
498 | * or possibly this node if we do not | |
499 | * find the matching task then we try | |
500 | * to approximate the results. | |
501 | */ | |
502 | if (thread == kgdb_info[i].task) | |
503 | local_debuggerinfo = kgdb_info[i].debuggerinfo; | |
504 | } | |
505 | } | |
506 | ||
507 | /* | |
508 | * All threads that don't have debuggerinfo should be | |
509 | * in schedule() sleeping, since all other CPUs | |
510 | * are in kgdb_wait, and thus have debuggerinfo. | |
511 | */ | |
512 | if (local_debuggerinfo) { | |
513 | pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo); | |
514 | } else { | |
515 | /* | |
516 | * Pull stuff saved during switch_to; nothing | |
517 | * else is accessible (or even particularly | |
518 | * relevant). | |
519 | * | |
520 | * This should be enough for a stack trace. | |
521 | */ | |
522 | sleeping_thread_to_gdb_regs(gdb_regs, thread); | |
523 | } | |
55751145 JW |
524 | } |
525 | ||
526 | /* Handle the 'g' get registers request */ | |
527 | static void gdb_cmd_getregs(struct kgdb_state *ks) | |
528 | { | |
529 | gdb_get_regs_helper(ks); | |
53197fc4 JW |
530 | kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES); |
531 | } | |
532 | ||
533 | /* Handle the 'G' set registers request */ | |
534 | static void gdb_cmd_setregs(struct kgdb_state *ks) | |
535 | { | |
536 | kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES); | |
537 | ||
538 | if (kgdb_usethread && kgdb_usethread != current) { | |
539 | error_packet(remcom_out_buffer, -EINVAL); | |
540 | } else { | |
541 | gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs); | |
542 | strcpy(remcom_out_buffer, "OK"); | |
543 | } | |
544 | } | |
545 | ||
546 | /* Handle the 'm' memory read bytes */ | |
547 | static void gdb_cmd_memread(struct kgdb_state *ks) | |
548 | { | |
549 | char *ptr = &remcom_in_buffer[1]; | |
550 | unsigned long length; | |
551 | unsigned long addr; | |
55751145 | 552 | char *err; |
53197fc4 JW |
553 | |
554 | if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' && | |
555 | kgdb_hex2long(&ptr, &length) > 0) { | |
556 | err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length); | |
55751145 JW |
557 | if (!err) |
558 | error_packet(remcom_out_buffer, -EINVAL); | |
53197fc4 JW |
559 | } else { |
560 | error_packet(remcom_out_buffer, -EINVAL); | |
561 | } | |
562 | } | |
563 | ||
564 | /* Handle the 'M' memory write bytes */ | |
565 | static void gdb_cmd_memwrite(struct kgdb_state *ks) | |
566 | { | |
567 | int err = write_mem_msg(0); | |
568 | ||
569 | if (err) | |
570 | error_packet(remcom_out_buffer, err); | |
571 | else | |
572 | strcpy(remcom_out_buffer, "OK"); | |
573 | } | |
574 | ||
55751145 JW |
575 | #if DBG_MAX_REG_NUM > 0 |
576 | static char *gdb_hex_reg_helper(int regnum, char *out) | |
577 | { | |
578 | int i; | |
579 | int offset = 0; | |
580 | ||
581 | for (i = 0; i < regnum; i++) | |
582 | offset += dbg_reg_def[i].size; | |
583 | return kgdb_mem2hex((char *)gdb_regs + offset, out, | |
584 | dbg_reg_def[i].size); | |
585 | } | |
586 | ||
587 | /* Handle the 'p' individual regster get */ | |
588 | static void gdb_cmd_reg_get(struct kgdb_state *ks) | |
589 | { | |
590 | unsigned long regnum; | |
591 | char *ptr = &remcom_in_buffer[1]; | |
592 | ||
593 | kgdb_hex2long(&ptr, ®num); | |
594 | if (regnum >= DBG_MAX_REG_NUM) { | |
595 | error_packet(remcom_out_buffer, -EINVAL); | |
596 | return; | |
597 | } | |
598 | gdb_get_regs_helper(ks); | |
599 | gdb_hex_reg_helper(regnum, remcom_out_buffer); | |
600 | } | |
601 | ||
602 | /* Handle the 'P' individual regster set */ | |
603 | static void gdb_cmd_reg_set(struct kgdb_state *ks) | |
604 | { | |
605 | unsigned long regnum; | |
606 | char *ptr = &remcom_in_buffer[1]; | |
607 | ||
608 | kgdb_hex2long(&ptr, ®num); | |
609 | if (*ptr++ != '=' || | |
610 | !(!kgdb_usethread || kgdb_usethread == current) || | |
611 | !dbg_get_reg(regnum, gdb_regs, ks->linux_regs)) { | |
612 | error_packet(remcom_out_buffer, -EINVAL); | |
613 | return; | |
614 | } | |
615 | kgdb_hex2mem(ptr, (char *)gdb_regs, dbg_reg_def[regnum].size); | |
616 | dbg_set_reg(regnum, gdb_regs, ks->linux_regs); | |
617 | strcpy(remcom_out_buffer, "OK"); | |
618 | } | |
619 | #endif /* DBG_MAX_REG_NUM > 0 */ | |
620 | ||
53197fc4 JW |
621 | /* Handle the 'X' memory binary write bytes */ |
622 | static void gdb_cmd_binwrite(struct kgdb_state *ks) | |
623 | { | |
624 | int err = write_mem_msg(1); | |
625 | ||
626 | if (err) | |
627 | error_packet(remcom_out_buffer, err); | |
628 | else | |
629 | strcpy(remcom_out_buffer, "OK"); | |
630 | } | |
631 | ||
632 | /* Handle the 'D' or 'k', detach or kill packets */ | |
633 | static void gdb_cmd_detachkill(struct kgdb_state *ks) | |
634 | { | |
635 | int error; | |
636 | ||
637 | /* The detach case */ | |
638 | if (remcom_in_buffer[0] == 'D') { | |
639 | error = dbg_remove_all_break(); | |
640 | if (error < 0) { | |
641 | error_packet(remcom_out_buffer, error); | |
642 | } else { | |
643 | strcpy(remcom_out_buffer, "OK"); | |
644 | kgdb_connected = 0; | |
645 | } | |
646 | put_packet(remcom_out_buffer); | |
647 | } else { | |
648 | /* | |
649 | * Assume the kill case, with no exit code checking, | |
650 | * trying to force detach the debugger: | |
651 | */ | |
652 | dbg_remove_all_break(); | |
653 | kgdb_connected = 0; | |
654 | } | |
655 | } | |
656 | ||
657 | /* Handle the 'R' reboot packets */ | |
658 | static int gdb_cmd_reboot(struct kgdb_state *ks) | |
659 | { | |
660 | /* For now, only honor R0 */ | |
661 | if (strcmp(remcom_in_buffer, "R0") == 0) { | |
662 | printk(KERN_CRIT "Executing emergency reboot\n"); | |
663 | strcpy(remcom_out_buffer, "OK"); | |
664 | put_packet(remcom_out_buffer); | |
665 | ||
666 | /* | |
667 | * Execution should not return from | |
668 | * machine_emergency_restart() | |
669 | */ | |
670 | machine_emergency_restart(); | |
671 | kgdb_connected = 0; | |
672 | ||
673 | return 1; | |
674 | } | |
675 | return 0; | |
676 | } | |
677 | ||
678 | /* Handle the 'q' query packets */ | |
679 | static void gdb_cmd_query(struct kgdb_state *ks) | |
680 | { | |
681 | struct task_struct *g; | |
682 | struct task_struct *p; | |
84a0bd5b | 683 | unsigned char thref[BUF_THREAD_ID_SIZE]; |
53197fc4 JW |
684 | char *ptr; |
685 | int i; | |
686 | int cpu; | |
687 | int finished = 0; | |
688 | ||
689 | switch (remcom_in_buffer[1]) { | |
690 | case 's': | |
691 | case 'f': | |
fb82c0ff | 692 | if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10)) |
53197fc4 | 693 | break; |
53197fc4 JW |
694 | |
695 | i = 0; | |
696 | remcom_out_buffer[0] = 'm'; | |
697 | ptr = remcom_out_buffer + 1; | |
698 | if (remcom_in_buffer[1] == 'f') { | |
699 | /* Each cpu is a shadow thread */ | |
700 | for_each_online_cpu(cpu) { | |
701 | ks->thr_query = 0; | |
702 | int_to_threadref(thref, -cpu - 2); | |
84a0bd5b | 703 | ptr = pack_threadid(ptr, thref); |
53197fc4 JW |
704 | *(ptr++) = ','; |
705 | i++; | |
706 | } | |
707 | } | |
708 | ||
709 | do_each_thread(g, p) { | |
710 | if (i >= ks->thr_query && !finished) { | |
711 | int_to_threadref(thref, p->pid); | |
84a0bd5b | 712 | ptr = pack_threadid(ptr, thref); |
53197fc4 JW |
713 | *(ptr++) = ','; |
714 | ks->thr_query++; | |
715 | if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0) | |
716 | finished = 1; | |
717 | } | |
718 | i++; | |
719 | } while_each_thread(g, p); | |
720 | ||
721 | *(--ptr) = '\0'; | |
722 | break; | |
723 | ||
724 | case 'C': | |
725 | /* Current thread id */ | |
726 | strcpy(remcom_out_buffer, "QC"); | |
727 | ks->threadid = shadow_pid(current->pid); | |
728 | int_to_threadref(thref, ks->threadid); | |
729 | pack_threadid(remcom_out_buffer + 2, thref); | |
730 | break; | |
731 | case 'T': | |
fb82c0ff | 732 | if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16)) |
53197fc4 | 733 | break; |
fb82c0ff | 734 | |
53197fc4 JW |
735 | ks->threadid = 0; |
736 | ptr = remcom_in_buffer + 17; | |
737 | kgdb_hex2long(&ptr, &ks->threadid); | |
738 | if (!getthread(ks->linux_regs, ks->threadid)) { | |
739 | error_packet(remcom_out_buffer, -EINVAL); | |
740 | break; | |
741 | } | |
742 | if ((int)ks->threadid > 0) { | |
743 | kgdb_mem2hex(getthread(ks->linux_regs, | |
744 | ks->threadid)->comm, | |
745 | remcom_out_buffer, 16); | |
746 | } else { | |
747 | static char tmpstr[23 + BUF_THREAD_ID_SIZE]; | |
748 | ||
749 | sprintf(tmpstr, "shadowCPU%d", | |
750 | (int)(-ks->threadid - 2)); | |
751 | kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr)); | |
752 | } | |
753 | break; | |
a0de055c JW |
754 | #ifdef CONFIG_KGDB_KDB |
755 | case 'R': | |
756 | if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) { | |
757 | int len = strlen(remcom_in_buffer + 6); | |
758 | ||
759 | if ((len % 2) != 0) { | |
760 | strcpy(remcom_out_buffer, "E01"); | |
761 | break; | |
762 | } | |
763 | kgdb_hex2mem(remcom_in_buffer + 6, | |
764 | remcom_out_buffer, len); | |
765 | len = len / 2; | |
766 | remcom_out_buffer[len++] = 0; | |
767 | ||
768 | kdb_parse(remcom_out_buffer); | |
769 | strcpy(remcom_out_buffer, "OK"); | |
770 | } | |
771 | break; | |
772 | #endif | |
53197fc4 JW |
773 | } |
774 | } | |
775 | ||
776 | /* Handle the 'H' task query packets */ | |
777 | static void gdb_cmd_task(struct kgdb_state *ks) | |
778 | { | |
779 | struct task_struct *thread; | |
780 | char *ptr; | |
781 | ||
782 | switch (remcom_in_buffer[1]) { | |
783 | case 'g': | |
784 | ptr = &remcom_in_buffer[2]; | |
785 | kgdb_hex2long(&ptr, &ks->threadid); | |
786 | thread = getthread(ks->linux_regs, ks->threadid); | |
787 | if (!thread && ks->threadid > 0) { | |
788 | error_packet(remcom_out_buffer, -EINVAL); | |
789 | break; | |
790 | } | |
791 | kgdb_usethread = thread; | |
792 | ks->kgdb_usethreadid = ks->threadid; | |
793 | strcpy(remcom_out_buffer, "OK"); | |
794 | break; | |
795 | case 'c': | |
796 | ptr = &remcom_in_buffer[2]; | |
797 | kgdb_hex2long(&ptr, &ks->threadid); | |
798 | if (!ks->threadid) { | |
799 | kgdb_contthread = NULL; | |
800 | } else { | |
801 | thread = getthread(ks->linux_regs, ks->threadid); | |
802 | if (!thread && ks->threadid > 0) { | |
803 | error_packet(remcom_out_buffer, -EINVAL); | |
804 | break; | |
805 | } | |
806 | kgdb_contthread = thread; | |
807 | } | |
808 | strcpy(remcom_out_buffer, "OK"); | |
809 | break; | |
810 | } | |
811 | } | |
812 | ||
813 | /* Handle the 'T' thread query packets */ | |
814 | static void gdb_cmd_thread(struct kgdb_state *ks) | |
815 | { | |
816 | char *ptr = &remcom_in_buffer[1]; | |
817 | struct task_struct *thread; | |
818 | ||
819 | kgdb_hex2long(&ptr, &ks->threadid); | |
820 | thread = getthread(ks->linux_regs, ks->threadid); | |
821 | if (thread) | |
822 | strcpy(remcom_out_buffer, "OK"); | |
823 | else | |
824 | error_packet(remcom_out_buffer, -EINVAL); | |
825 | } | |
826 | ||
827 | /* Handle the 'z' or 'Z' breakpoint remove or set packets */ | |
828 | static void gdb_cmd_break(struct kgdb_state *ks) | |
829 | { | |
830 | /* | |
831 | * Since GDB-5.3, it's been drafted that '0' is a software | |
832 | * breakpoint, '1' is a hardware breakpoint, so let's do that. | |
833 | */ | |
834 | char *bpt_type = &remcom_in_buffer[1]; | |
835 | char *ptr = &remcom_in_buffer[2]; | |
836 | unsigned long addr; | |
837 | unsigned long length; | |
838 | int error = 0; | |
839 | ||
840 | if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') { | |
841 | /* Unsupported */ | |
842 | if (*bpt_type > '4') | |
843 | return; | |
844 | } else { | |
845 | if (*bpt_type != '0' && *bpt_type != '1') | |
846 | /* Unsupported. */ | |
847 | return; | |
848 | } | |
849 | ||
850 | /* | |
851 | * Test if this is a hardware breakpoint, and | |
852 | * if we support it: | |
853 | */ | |
854 | if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT)) | |
855 | /* Unsupported. */ | |
856 | return; | |
857 | ||
858 | if (*(ptr++) != ',') { | |
859 | error_packet(remcom_out_buffer, -EINVAL); | |
860 | return; | |
861 | } | |
862 | if (!kgdb_hex2long(&ptr, &addr)) { | |
863 | error_packet(remcom_out_buffer, -EINVAL); | |
864 | return; | |
865 | } | |
866 | if (*(ptr++) != ',' || | |
867 | !kgdb_hex2long(&ptr, &length)) { | |
868 | error_packet(remcom_out_buffer, -EINVAL); | |
869 | return; | |
870 | } | |
871 | ||
872 | if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0') | |
873 | error = dbg_set_sw_break(addr); | |
874 | else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0') | |
875 | error = dbg_remove_sw_break(addr); | |
876 | else if (remcom_in_buffer[0] == 'Z') | |
877 | error = arch_kgdb_ops.set_hw_breakpoint(addr, | |
878 | (int)length, *bpt_type - '0'); | |
879 | else if (remcom_in_buffer[0] == 'z') | |
880 | error = arch_kgdb_ops.remove_hw_breakpoint(addr, | |
881 | (int) length, *bpt_type - '0'); | |
882 | ||
883 | if (error == 0) | |
884 | strcpy(remcom_out_buffer, "OK"); | |
885 | else | |
886 | error_packet(remcom_out_buffer, error); | |
887 | } | |
888 | ||
889 | /* Handle the 'C' signal / exception passing packets */ | |
890 | static int gdb_cmd_exception_pass(struct kgdb_state *ks) | |
891 | { | |
892 | /* C09 == pass exception | |
893 | * C15 == detach kgdb, pass exception | |
894 | */ | |
895 | if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') { | |
896 | ||
897 | ks->pass_exception = 1; | |
898 | remcom_in_buffer[0] = 'c'; | |
899 | ||
900 | } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') { | |
901 | ||
902 | ks->pass_exception = 1; | |
903 | remcom_in_buffer[0] = 'D'; | |
904 | dbg_remove_all_break(); | |
905 | kgdb_connected = 0; | |
906 | return 1; | |
907 | ||
908 | } else { | |
909 | gdbstub_msg_write("KGDB only knows signal 9 (pass)" | |
910 | " and 15 (pass and disconnect)\n" | |
911 | "Executing a continue without signal passing\n", 0); | |
912 | remcom_in_buffer[0] = 'c'; | |
913 | } | |
914 | ||
915 | /* Indicate fall through */ | |
916 | return -1; | |
917 | } | |
918 | ||
919 | /* | |
920 | * This function performs all gdbserial command procesing | |
921 | */ | |
922 | int gdb_serial_stub(struct kgdb_state *ks) | |
923 | { | |
924 | int error = 0; | |
925 | int tmp; | |
926 | ||
55751145 | 927 | /* Initialize comm buffer and globals. */ |
53197fc4 | 928 | memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); |
55751145 JW |
929 | kgdb_usethread = kgdb_info[ks->cpu].task; |
930 | ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid); | |
931 | ks->pass_exception = 0; | |
53197fc4 JW |
932 | |
933 | if (kgdb_connected) { | |
84a0bd5b | 934 | unsigned char thref[BUF_THREAD_ID_SIZE]; |
53197fc4 JW |
935 | char *ptr; |
936 | ||
937 | /* Reply to host that an exception has occurred */ | |
938 | ptr = remcom_out_buffer; | |
939 | *ptr++ = 'T'; | |
940 | ptr = pack_hex_byte(ptr, ks->signo); | |
941 | ptr += strlen(strcpy(ptr, "thread:")); | |
942 | int_to_threadref(thref, shadow_pid(current->pid)); | |
943 | ptr = pack_threadid(ptr, thref); | |
944 | *ptr++ = ';'; | |
945 | put_packet(remcom_out_buffer); | |
946 | } | |
947 | ||
53197fc4 JW |
948 | while (1) { |
949 | error = 0; | |
950 | ||
951 | /* Clear the out buffer. */ | |
952 | memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer)); | |
953 | ||
954 | get_packet(remcom_in_buffer); | |
955 | ||
956 | switch (remcom_in_buffer[0]) { | |
957 | case '?': /* gdbserial status */ | |
958 | gdb_cmd_status(ks); | |
959 | break; | |
960 | case 'g': /* return the value of the CPU registers */ | |
961 | gdb_cmd_getregs(ks); | |
962 | break; | |
963 | case 'G': /* set the value of the CPU registers - return OK */ | |
964 | gdb_cmd_setregs(ks); | |
965 | break; | |
966 | case 'm': /* mAA..AA,LLLL Read LLLL bytes at address AA..AA */ | |
967 | gdb_cmd_memread(ks); | |
968 | break; | |
969 | case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */ | |
970 | gdb_cmd_memwrite(ks); | |
971 | break; | |
55751145 JW |
972 | #if DBG_MAX_REG_NUM > 0 |
973 | case 'p': /* pXX Return gdb register XX (in hex) */ | |
974 | gdb_cmd_reg_get(ks); | |
975 | break; | |
976 | case 'P': /* PXX=aaaa Set gdb register XX to aaaa (in hex) */ | |
977 | gdb_cmd_reg_set(ks); | |
978 | break; | |
979 | #endif /* DBG_MAX_REG_NUM > 0 */ | |
53197fc4 JW |
980 | case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */ |
981 | gdb_cmd_binwrite(ks); | |
982 | break; | |
983 | /* kill or detach. KGDB should treat this like a | |
984 | * continue. | |
985 | */ | |
986 | case 'D': /* Debugger detach */ | |
987 | case 'k': /* Debugger detach via kill */ | |
988 | gdb_cmd_detachkill(ks); | |
989 | goto default_handle; | |
990 | case 'R': /* Reboot */ | |
991 | if (gdb_cmd_reboot(ks)) | |
992 | goto default_handle; | |
993 | break; | |
994 | case 'q': /* query command */ | |
995 | gdb_cmd_query(ks); | |
996 | break; | |
997 | case 'H': /* task related */ | |
998 | gdb_cmd_task(ks); | |
999 | break; | |
1000 | case 'T': /* Query thread status */ | |
1001 | gdb_cmd_thread(ks); | |
1002 | break; | |
1003 | case 'z': /* Break point remove */ | |
1004 | case 'Z': /* Break point set */ | |
1005 | gdb_cmd_break(ks); | |
1006 | break; | |
dcc78711 JW |
1007 | #ifdef CONFIG_KGDB_KDB |
1008 | case '3': /* Escape into back into kdb */ | |
1009 | if (remcom_in_buffer[1] == '\0') { | |
1010 | gdb_cmd_detachkill(ks); | |
1011 | return DBG_PASS_EVENT; | |
1012 | } | |
1013 | #endif | |
53197fc4 JW |
1014 | case 'C': /* Exception passing */ |
1015 | tmp = gdb_cmd_exception_pass(ks); | |
1016 | if (tmp > 0) | |
1017 | goto default_handle; | |
1018 | if (tmp == 0) | |
1019 | break; | |
1020 | /* Fall through on tmp < 0 */ | |
1021 | case 'c': /* Continue packet */ | |
1022 | case 's': /* Single step packet */ | |
1023 | if (kgdb_contthread && kgdb_contthread != current) { | |
1024 | /* Can't switch threads in kgdb */ | |
1025 | error_packet(remcom_out_buffer, -EINVAL); | |
1026 | break; | |
1027 | } | |
1028 | dbg_activate_sw_breakpoints(); | |
1029 | /* Fall through to default processing */ | |
1030 | default: | |
1031 | default_handle: | |
1032 | error = kgdb_arch_handle_exception(ks->ex_vector, | |
1033 | ks->signo, | |
1034 | ks->err_code, | |
1035 | remcom_in_buffer, | |
1036 | remcom_out_buffer, | |
1037 | ks->linux_regs); | |
1038 | /* | |
1039 | * Leave cmd processing on error, detach, | |
1040 | * kill, continue, or single step. | |
1041 | */ | |
1042 | if (error >= 0 || remcom_in_buffer[0] == 'D' || | |
1043 | remcom_in_buffer[0] == 'k') { | |
1044 | error = 0; | |
1045 | goto kgdb_exit; | |
1046 | } | |
1047 | ||
1048 | } | |
1049 | ||
1050 | /* reply to the request */ | |
1051 | put_packet(remcom_out_buffer); | |
1052 | } | |
1053 | ||
1054 | kgdb_exit: | |
1055 | if (ks->pass_exception) | |
1056 | error = 1; | |
1057 | return error; | |
1058 | } | |
dcc78711 JW |
1059 | |
1060 | int gdbstub_state(struct kgdb_state *ks, char *cmd) | |
1061 | { | |
1062 | int error; | |
1063 | ||
1064 | switch (cmd[0]) { | |
1065 | case 'e': | |
1066 | error = kgdb_arch_handle_exception(ks->ex_vector, | |
1067 | ks->signo, | |
1068 | ks->err_code, | |
1069 | remcom_in_buffer, | |
1070 | remcom_out_buffer, | |
1071 | ks->linux_regs); | |
1072 | return error; | |
1073 | case 's': | |
1074 | case 'c': | |
1075 | strcpy(remcom_in_buffer, cmd); | |
1076 | return 0; | |
1077 | case '?': | |
1078 | gdb_cmd_status(ks); | |
1079 | break; | |
1080 | case '\0': | |
1081 | strcpy(remcom_out_buffer, ""); | |
1082 | break; | |
1083 | } | |
1084 | dbg_io_ops->write_char('+'); | |
1085 | put_packet(remcom_out_buffer); | |
1086 | return 0; | |
1087 | } |