006bad8905d395585ca8134e05678f2c815e07f1
[sfrench/cifs-2.6.git] / kernel / debug / gdbstub.c
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>
33 #include <linux/kdb.h>
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
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
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                  */
92                 while ((ch = (gdbstub_read_wait())) != '$')
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)) {
105                         ch = gdbstub_read_wait();
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 == '#') {
115                         xmitcsum = hex_to_bin(gdbstub_read_wait()) << 4;
116                         xmitcsum += hex_to_bin(gdbstub_read_wait());
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. */
161                 ch = gdbstub_read_wait();
162
163                 if (ch == 3)
164                         ch = gdbstub_read_wait();
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
193         if (len == 0)
194                 len = strlen(s);
195
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  */
228 int kgdb_mem2hex(char *mem, char *buf, int count)
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);
240         if (!err) {
241                 while (count > 0) {
242                         buf = pack_hex_byte(buf, *tmp);
243                         tmp++;
244                         count--;
245                 }
246
247                 *buf = 0;
248         }
249
250         return err;
251 }
252
253 /*
254  * Convert the hex array pointed to by buf into binary to be placed in
255  * mem.  Return a pointer to the character AFTER the last byte
256  * written.  May return an error.
257  */
258 int kgdb_hex2mem(char *buf, char *mem, int count)
259 {
260         char *tmp_raw;
261         char *tmp_hex;
262
263         /*
264          * We use the upper half of buf as an intermediate buffer for the
265          * raw memory that is converted from hex.
266          */
267         tmp_raw = buf + count * 2;
268
269         tmp_hex = tmp_raw - 1;
270         while (tmp_hex >= buf) {
271                 tmp_raw--;
272                 *tmp_raw = hex_to_bin(*tmp_hex--);
273                 *tmp_raw |= hex_to_bin(*tmp_hex--) << 4;
274         }
275
276         return probe_kernel_write(mem, tmp_raw, count);
277 }
278
279 /*
280  * While we find nice hex chars, build a long_val.
281  * Return number of chars processed.
282  */
283 int kgdb_hex2long(char **ptr, unsigned long *long_val)
284 {
285         int hex_val;
286         int num = 0;
287         int negate = 0;
288
289         *long_val = 0;
290
291         if (**ptr == '-') {
292                 negate = 1;
293                 (*ptr)++;
294         }
295         while (**ptr) {
296                 hex_val = hex_to_bin(**ptr);
297                 if (hex_val < 0)
298                         break;
299
300                 *long_val = (*long_val << 4) | hex_val;
301                 num++;
302                 (*ptr)++;
303         }
304
305         if (negate)
306                 *long_val = -*long_val;
307
308         return num;
309 }
310
311 /*
312  * Copy the binary array pointed to by buf into mem.  Fix $, #, and
313  * 0x7d escaped with 0x7d. Return -EFAULT on failure or 0 on success.
314  * The input buf is overwitten with the result to write to mem.
315  */
316 static int kgdb_ebin2mem(char *buf, char *mem, int count)
317 {
318         int size = 0;
319         char *c = buf;
320
321         while (count-- > 0) {
322                 c[size] = *buf++;
323                 if (c[size] == 0x7d)
324                         c[size] = *buf++ ^ 0x20;
325                 size++;
326         }
327
328         return probe_kernel_write(mem, c, size);
329 }
330
331 #if DBG_MAX_REG_NUM > 0
332 void pt_regs_to_gdb_regs(unsigned long *gdb_regs, struct pt_regs *regs)
333 {
334         int i;
335         int idx = 0;
336         char *ptr = (char *)gdb_regs;
337
338         for (i = 0; i < DBG_MAX_REG_NUM; i++) {
339                 dbg_get_reg(i, ptr + idx, regs);
340                 idx += dbg_reg_def[i].size;
341         }
342 }
343
344 void gdb_regs_to_pt_regs(unsigned long *gdb_regs, struct pt_regs *regs)
345 {
346         int i;
347         int idx = 0;
348         char *ptr = (char *)gdb_regs;
349
350         for (i = 0; i < DBG_MAX_REG_NUM; i++) {
351                 dbg_set_reg(i, ptr + idx, regs);
352                 idx += dbg_reg_def[i].size;
353         }
354 }
355 #endif /* DBG_MAX_REG_NUM > 0 */
356
357 /* Write memory due to an 'M' or 'X' packet. */
358 static int write_mem_msg(int binary)
359 {
360         char *ptr = &remcom_in_buffer[1];
361         unsigned long addr;
362         unsigned long length;
363         int err;
364
365         if (kgdb_hex2long(&ptr, &addr) > 0 && *(ptr++) == ',' &&
366             kgdb_hex2long(&ptr, &length) > 0 && *(ptr++) == ':') {
367                 if (binary)
368                         err = kgdb_ebin2mem(ptr, (char *)addr, length);
369                 else
370                         err = kgdb_hex2mem(ptr, (char *)addr, length);
371                 if (err)
372                         return err;
373                 if (CACHE_FLUSH_IS_SAFE)
374                         flush_icache_range(addr, addr + length);
375                 return 0;
376         }
377
378         return -EINVAL;
379 }
380
381 static void error_packet(char *pkt, int error)
382 {
383         error = -error;
384         pkt[0] = 'E';
385         pkt[1] = hex_asc[(error / 10)];
386         pkt[2] = hex_asc[(error % 10)];
387         pkt[3] = '\0';
388 }
389
390 /*
391  * Thread ID accessors. We represent a flat TID space to GDB, where
392  * the per CPU idle threads (which under Linux all have PID 0) are
393  * remapped to negative TIDs.
394  */
395
396 #define BUF_THREAD_ID_SIZE      8
397
398 static char *pack_threadid(char *pkt, unsigned char *id)
399 {
400         unsigned char *limit;
401         int lzero = 1;
402
403         limit = id + (BUF_THREAD_ID_SIZE / 2);
404         while (id < limit) {
405                 if (!lzero || *id != 0) {
406                         pkt = pack_hex_byte(pkt, *id);
407                         lzero = 0;
408                 }
409                 id++;
410         }
411
412         if (lzero)
413                 pkt = pack_hex_byte(pkt, 0);
414
415         return pkt;
416 }
417
418 static void int_to_threadref(unsigned char *id, int value)
419 {
420         put_unaligned_be32(value, id);
421 }
422
423 static struct task_struct *getthread(struct pt_regs *regs, int tid)
424 {
425         /*
426          * Non-positive TIDs are remapped to the cpu shadow information
427          */
428         if (tid == 0 || tid == -1)
429                 tid = -atomic_read(&kgdb_active) - 2;
430         if (tid < -1 && tid > -NR_CPUS - 2) {
431                 if (kgdb_info[-tid - 2].task)
432                         return kgdb_info[-tid - 2].task;
433                 else
434                         return idle_task(-tid - 2);
435         }
436         if (tid <= 0) {
437                 printk(KERN_ERR "KGDB: Internal thread select error\n");
438                 dump_stack();
439                 return NULL;
440         }
441
442         /*
443          * find_task_by_pid_ns() does not take the tasklist lock anymore
444          * but is nicely RCU locked - hence is a pretty resilient
445          * thing to use:
446          */
447         return find_task_by_pid_ns(tid, &init_pid_ns);
448 }
449
450
451 /*
452  * Remap normal tasks to their real PID,
453  * CPU shadow threads are mapped to -CPU - 2
454  */
455 static inline int shadow_pid(int realpid)
456 {
457         if (realpid)
458                 return realpid;
459
460         return -raw_smp_processor_id() - 2;
461 }
462
463 /*
464  * All the functions that start with gdb_cmd are the various
465  * operations to implement the handlers for the gdbserial protocol
466  * where KGDB is communicating with an external debugger
467  */
468
469 /* Handle the '?' status packets */
470 static void gdb_cmd_status(struct kgdb_state *ks)
471 {
472         /*
473          * We know that this packet is only sent
474          * during initial connect.  So to be safe,
475          * we clear out our breakpoints now in case
476          * GDB is reconnecting.
477          */
478         dbg_remove_all_break();
479
480         remcom_out_buffer[0] = 'S';
481         pack_hex_byte(&remcom_out_buffer[1], ks->signo);
482 }
483
484 /* Handle the 'g' get registers request */
485 static void gdb_cmd_getregs(struct kgdb_state *ks)
486 {
487         struct task_struct *thread;
488         void *local_debuggerinfo;
489         int i;
490
491         thread = kgdb_usethread;
492         if (!thread) {
493                 thread = kgdb_info[ks->cpu].task;
494                 local_debuggerinfo = kgdb_info[ks->cpu].debuggerinfo;
495         } else {
496                 local_debuggerinfo = NULL;
497                 for_each_online_cpu(i) {
498                         /*
499                          * Try to find the task on some other
500                          * or possibly this node if we do not
501                          * find the matching task then we try
502                          * to approximate the results.
503                          */
504                         if (thread == kgdb_info[i].task)
505                                 local_debuggerinfo = kgdb_info[i].debuggerinfo;
506                 }
507         }
508
509         /*
510          * All threads that don't have debuggerinfo should be
511          * in schedule() sleeping, since all other CPUs
512          * are in kgdb_wait, and thus have debuggerinfo.
513          */
514         if (local_debuggerinfo) {
515                 pt_regs_to_gdb_regs(gdb_regs, local_debuggerinfo);
516         } else {
517                 /*
518                  * Pull stuff saved during switch_to; nothing
519                  * else is accessible (or even particularly
520                  * relevant).
521                  *
522                  * This should be enough for a stack trace.
523                  */
524                 sleeping_thread_to_gdb_regs(gdb_regs, thread);
525         }
526         kgdb_mem2hex((char *)gdb_regs, remcom_out_buffer, NUMREGBYTES);
527 }
528
529 /* Handle the 'G' set registers request */
530 static void gdb_cmd_setregs(struct kgdb_state *ks)
531 {
532         kgdb_hex2mem(&remcom_in_buffer[1], (char *)gdb_regs, NUMREGBYTES);
533
534         if (kgdb_usethread && kgdb_usethread != current) {
535                 error_packet(remcom_out_buffer, -EINVAL);
536         } else {
537                 gdb_regs_to_pt_regs(gdb_regs, ks->linux_regs);
538                 strcpy(remcom_out_buffer, "OK");
539         }
540 }
541
542 /* Handle the 'm' memory read bytes */
543 static void gdb_cmd_memread(struct kgdb_state *ks)
544 {
545         char *ptr = &remcom_in_buffer[1];
546         unsigned long length;
547         unsigned long addr;
548         int err;
549
550         if (kgdb_hex2long(&ptr, &addr) > 0 && *ptr++ == ',' &&
551                                         kgdb_hex2long(&ptr, &length) > 0) {
552                 err = kgdb_mem2hex((char *)addr, remcom_out_buffer, length);
553                 if (err)
554                         error_packet(remcom_out_buffer, err);
555         } else {
556                 error_packet(remcom_out_buffer, -EINVAL);
557         }
558 }
559
560 /* Handle the 'M' memory write bytes */
561 static void gdb_cmd_memwrite(struct kgdb_state *ks)
562 {
563         int err = write_mem_msg(0);
564
565         if (err)
566                 error_packet(remcom_out_buffer, err);
567         else
568                 strcpy(remcom_out_buffer, "OK");
569 }
570
571 /* Handle the 'X' memory binary write bytes */
572 static void gdb_cmd_binwrite(struct kgdb_state *ks)
573 {
574         int err = write_mem_msg(1);
575
576         if (err)
577                 error_packet(remcom_out_buffer, err);
578         else
579                 strcpy(remcom_out_buffer, "OK");
580 }
581
582 /* Handle the 'D' or 'k', detach or kill packets */
583 static void gdb_cmd_detachkill(struct kgdb_state *ks)
584 {
585         int error;
586
587         /* The detach case */
588         if (remcom_in_buffer[0] == 'D') {
589                 error = dbg_remove_all_break();
590                 if (error < 0) {
591                         error_packet(remcom_out_buffer, error);
592                 } else {
593                         strcpy(remcom_out_buffer, "OK");
594                         kgdb_connected = 0;
595                 }
596                 put_packet(remcom_out_buffer);
597         } else {
598                 /*
599                  * Assume the kill case, with no exit code checking,
600                  * trying to force detach the debugger:
601                  */
602                 dbg_remove_all_break();
603                 kgdb_connected = 0;
604         }
605 }
606
607 /* Handle the 'R' reboot packets */
608 static int gdb_cmd_reboot(struct kgdb_state *ks)
609 {
610         /* For now, only honor R0 */
611         if (strcmp(remcom_in_buffer, "R0") == 0) {
612                 printk(KERN_CRIT "Executing emergency reboot\n");
613                 strcpy(remcom_out_buffer, "OK");
614                 put_packet(remcom_out_buffer);
615
616                 /*
617                  * Execution should not return from
618                  * machine_emergency_restart()
619                  */
620                 machine_emergency_restart();
621                 kgdb_connected = 0;
622
623                 return 1;
624         }
625         return 0;
626 }
627
628 /* Handle the 'q' query packets */
629 static void gdb_cmd_query(struct kgdb_state *ks)
630 {
631         struct task_struct *g;
632         struct task_struct *p;
633         unsigned char thref[BUF_THREAD_ID_SIZE];
634         char *ptr;
635         int i;
636         int cpu;
637         int finished = 0;
638
639         switch (remcom_in_buffer[1]) {
640         case 's':
641         case 'f':
642                 if (memcmp(remcom_in_buffer + 2, "ThreadInfo", 10))
643                         break;
644
645                 i = 0;
646                 remcom_out_buffer[0] = 'm';
647                 ptr = remcom_out_buffer + 1;
648                 if (remcom_in_buffer[1] == 'f') {
649                         /* Each cpu is a shadow thread */
650                         for_each_online_cpu(cpu) {
651                                 ks->thr_query = 0;
652                                 int_to_threadref(thref, -cpu - 2);
653                                 ptr = pack_threadid(ptr, thref);
654                                 *(ptr++) = ',';
655                                 i++;
656                         }
657                 }
658
659                 do_each_thread(g, p) {
660                         if (i >= ks->thr_query && !finished) {
661                                 int_to_threadref(thref, p->pid);
662                                 ptr = pack_threadid(ptr, thref);
663                                 *(ptr++) = ',';
664                                 ks->thr_query++;
665                                 if (ks->thr_query % KGDB_MAX_THREAD_QUERY == 0)
666                                         finished = 1;
667                         }
668                         i++;
669                 } while_each_thread(g, p);
670
671                 *(--ptr) = '\0';
672                 break;
673
674         case 'C':
675                 /* Current thread id */
676                 strcpy(remcom_out_buffer, "QC");
677                 ks->threadid = shadow_pid(current->pid);
678                 int_to_threadref(thref, ks->threadid);
679                 pack_threadid(remcom_out_buffer + 2, thref);
680                 break;
681         case 'T':
682                 if (memcmp(remcom_in_buffer + 1, "ThreadExtraInfo,", 16))
683                         break;
684
685                 ks->threadid = 0;
686                 ptr = remcom_in_buffer + 17;
687                 kgdb_hex2long(&ptr, &ks->threadid);
688                 if (!getthread(ks->linux_regs, ks->threadid)) {
689                         error_packet(remcom_out_buffer, -EINVAL);
690                         break;
691                 }
692                 if ((int)ks->threadid > 0) {
693                         kgdb_mem2hex(getthread(ks->linux_regs,
694                                         ks->threadid)->comm,
695                                         remcom_out_buffer, 16);
696                 } else {
697                         static char tmpstr[23 + BUF_THREAD_ID_SIZE];
698
699                         sprintf(tmpstr, "shadowCPU%d",
700                                         (int)(-ks->threadid - 2));
701                         kgdb_mem2hex(tmpstr, remcom_out_buffer, strlen(tmpstr));
702                 }
703                 break;
704 #ifdef CONFIG_KGDB_KDB
705         case 'R':
706                 if (strncmp(remcom_in_buffer, "qRcmd,", 6) == 0) {
707                         int len = strlen(remcom_in_buffer + 6);
708
709                         if ((len % 2) != 0) {
710                                 strcpy(remcom_out_buffer, "E01");
711                                 break;
712                         }
713                         kgdb_hex2mem(remcom_in_buffer + 6,
714                                      remcom_out_buffer, len);
715                         len = len / 2;
716                         remcom_out_buffer[len++] = 0;
717
718                         kdb_parse(remcom_out_buffer);
719                         strcpy(remcom_out_buffer, "OK");
720                 }
721                 break;
722 #endif
723         }
724 }
725
726 /* Handle the 'H' task query packets */
727 static void gdb_cmd_task(struct kgdb_state *ks)
728 {
729         struct task_struct *thread;
730         char *ptr;
731
732         switch (remcom_in_buffer[1]) {
733         case 'g':
734                 ptr = &remcom_in_buffer[2];
735                 kgdb_hex2long(&ptr, &ks->threadid);
736                 thread = getthread(ks->linux_regs, ks->threadid);
737                 if (!thread && ks->threadid > 0) {
738                         error_packet(remcom_out_buffer, -EINVAL);
739                         break;
740                 }
741                 kgdb_usethread = thread;
742                 ks->kgdb_usethreadid = ks->threadid;
743                 strcpy(remcom_out_buffer, "OK");
744                 break;
745         case 'c':
746                 ptr = &remcom_in_buffer[2];
747                 kgdb_hex2long(&ptr, &ks->threadid);
748                 if (!ks->threadid) {
749                         kgdb_contthread = NULL;
750                 } else {
751                         thread = getthread(ks->linux_regs, ks->threadid);
752                         if (!thread && ks->threadid > 0) {
753                                 error_packet(remcom_out_buffer, -EINVAL);
754                                 break;
755                         }
756                         kgdb_contthread = thread;
757                 }
758                 strcpy(remcom_out_buffer, "OK");
759                 break;
760         }
761 }
762
763 /* Handle the 'T' thread query packets */
764 static void gdb_cmd_thread(struct kgdb_state *ks)
765 {
766         char *ptr = &remcom_in_buffer[1];
767         struct task_struct *thread;
768
769         kgdb_hex2long(&ptr, &ks->threadid);
770         thread = getthread(ks->linux_regs, ks->threadid);
771         if (thread)
772                 strcpy(remcom_out_buffer, "OK");
773         else
774                 error_packet(remcom_out_buffer, -EINVAL);
775 }
776
777 /* Handle the 'z' or 'Z' breakpoint remove or set packets */
778 static void gdb_cmd_break(struct kgdb_state *ks)
779 {
780         /*
781          * Since GDB-5.3, it's been drafted that '0' is a software
782          * breakpoint, '1' is a hardware breakpoint, so let's do that.
783          */
784         char *bpt_type = &remcom_in_buffer[1];
785         char *ptr = &remcom_in_buffer[2];
786         unsigned long addr;
787         unsigned long length;
788         int error = 0;
789
790         if (arch_kgdb_ops.set_hw_breakpoint && *bpt_type >= '1') {
791                 /* Unsupported */
792                 if (*bpt_type > '4')
793                         return;
794         } else {
795                 if (*bpt_type != '0' && *bpt_type != '1')
796                         /* Unsupported. */
797                         return;
798         }
799
800         /*
801          * Test if this is a hardware breakpoint, and
802          * if we support it:
803          */
804         if (*bpt_type == '1' && !(arch_kgdb_ops.flags & KGDB_HW_BREAKPOINT))
805                 /* Unsupported. */
806                 return;
807
808         if (*(ptr++) != ',') {
809                 error_packet(remcom_out_buffer, -EINVAL);
810                 return;
811         }
812         if (!kgdb_hex2long(&ptr, &addr)) {
813                 error_packet(remcom_out_buffer, -EINVAL);
814                 return;
815         }
816         if (*(ptr++) != ',' ||
817                 !kgdb_hex2long(&ptr, &length)) {
818                 error_packet(remcom_out_buffer, -EINVAL);
819                 return;
820         }
821
822         if (remcom_in_buffer[0] == 'Z' && *bpt_type == '0')
823                 error = dbg_set_sw_break(addr);
824         else if (remcom_in_buffer[0] == 'z' && *bpt_type == '0')
825                 error = dbg_remove_sw_break(addr);
826         else if (remcom_in_buffer[0] == 'Z')
827                 error = arch_kgdb_ops.set_hw_breakpoint(addr,
828                         (int)length, *bpt_type - '0');
829         else if (remcom_in_buffer[0] == 'z')
830                 error = arch_kgdb_ops.remove_hw_breakpoint(addr,
831                         (int) length, *bpt_type - '0');
832
833         if (error == 0)
834                 strcpy(remcom_out_buffer, "OK");
835         else
836                 error_packet(remcom_out_buffer, error);
837 }
838
839 /* Handle the 'C' signal / exception passing packets */
840 static int gdb_cmd_exception_pass(struct kgdb_state *ks)
841 {
842         /* C09 == pass exception
843          * C15 == detach kgdb, pass exception
844          */
845         if (remcom_in_buffer[1] == '0' && remcom_in_buffer[2] == '9') {
846
847                 ks->pass_exception = 1;
848                 remcom_in_buffer[0] = 'c';
849
850         } else if (remcom_in_buffer[1] == '1' && remcom_in_buffer[2] == '5') {
851
852                 ks->pass_exception = 1;
853                 remcom_in_buffer[0] = 'D';
854                 dbg_remove_all_break();
855                 kgdb_connected = 0;
856                 return 1;
857
858         } else {
859                 gdbstub_msg_write("KGDB only knows signal 9 (pass)"
860                         " and 15 (pass and disconnect)\n"
861                         "Executing a continue without signal passing\n", 0);
862                 remcom_in_buffer[0] = 'c';
863         }
864
865         /* Indicate fall through */
866         return -1;
867 }
868
869 /*
870  * This function performs all gdbserial command procesing
871  */
872 int gdb_serial_stub(struct kgdb_state *ks)
873 {
874         int error = 0;
875         int tmp;
876
877         /* Clear the out buffer. */
878         memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
879
880         if (kgdb_connected) {
881                 unsigned char thref[BUF_THREAD_ID_SIZE];
882                 char *ptr;
883
884                 /* Reply to host that an exception has occurred */
885                 ptr = remcom_out_buffer;
886                 *ptr++ = 'T';
887                 ptr = pack_hex_byte(ptr, ks->signo);
888                 ptr += strlen(strcpy(ptr, "thread:"));
889                 int_to_threadref(thref, shadow_pid(current->pid));
890                 ptr = pack_threadid(ptr, thref);
891                 *ptr++ = ';';
892                 put_packet(remcom_out_buffer);
893         }
894
895         kgdb_usethread = kgdb_info[ks->cpu].task;
896         ks->kgdb_usethreadid = shadow_pid(kgdb_info[ks->cpu].task->pid);
897         ks->pass_exception = 0;
898
899         while (1) {
900                 error = 0;
901
902                 /* Clear the out buffer. */
903                 memset(remcom_out_buffer, 0, sizeof(remcom_out_buffer));
904
905                 get_packet(remcom_in_buffer);
906
907                 switch (remcom_in_buffer[0]) {
908                 case '?': /* gdbserial status */
909                         gdb_cmd_status(ks);
910                         break;
911                 case 'g': /* return the value of the CPU registers */
912                         gdb_cmd_getregs(ks);
913                         break;
914                 case 'G': /* set the value of the CPU registers - return OK */
915                         gdb_cmd_setregs(ks);
916                         break;
917                 case 'm': /* mAA..AA,LLLL  Read LLLL bytes at address AA..AA */
918                         gdb_cmd_memread(ks);
919                         break;
920                 case 'M': /* MAA..AA,LLLL: Write LLLL bytes at address AA..AA */
921                         gdb_cmd_memwrite(ks);
922                         break;
923                 case 'X': /* XAA..AA,LLLL: Write LLLL bytes at address AA..AA */
924                         gdb_cmd_binwrite(ks);
925                         break;
926                         /* kill or detach. KGDB should treat this like a
927                          * continue.
928                          */
929                 case 'D': /* Debugger detach */
930                 case 'k': /* Debugger detach via kill */
931                         gdb_cmd_detachkill(ks);
932                         goto default_handle;
933                 case 'R': /* Reboot */
934                         if (gdb_cmd_reboot(ks))
935                                 goto default_handle;
936                         break;
937                 case 'q': /* query command */
938                         gdb_cmd_query(ks);
939                         break;
940                 case 'H': /* task related */
941                         gdb_cmd_task(ks);
942                         break;
943                 case 'T': /* Query thread status */
944                         gdb_cmd_thread(ks);
945                         break;
946                 case 'z': /* Break point remove */
947                 case 'Z': /* Break point set */
948                         gdb_cmd_break(ks);
949                         break;
950 #ifdef CONFIG_KGDB_KDB
951                 case '3': /* Escape into back into kdb */
952                         if (remcom_in_buffer[1] == '\0') {
953                                 gdb_cmd_detachkill(ks);
954                                 return DBG_PASS_EVENT;
955                         }
956 #endif
957                 case 'C': /* Exception passing */
958                         tmp = gdb_cmd_exception_pass(ks);
959                         if (tmp > 0)
960                                 goto default_handle;
961                         if (tmp == 0)
962                                 break;
963                         /* Fall through on tmp < 0 */
964                 case 'c': /* Continue packet */
965                 case 's': /* Single step packet */
966                         if (kgdb_contthread && kgdb_contthread != current) {
967                                 /* Can't switch threads in kgdb */
968                                 error_packet(remcom_out_buffer, -EINVAL);
969                                 break;
970                         }
971                         dbg_activate_sw_breakpoints();
972                         /* Fall through to default processing */
973                 default:
974 default_handle:
975                         error = kgdb_arch_handle_exception(ks->ex_vector,
976                                                 ks->signo,
977                                                 ks->err_code,
978                                                 remcom_in_buffer,
979                                                 remcom_out_buffer,
980                                                 ks->linux_regs);
981                         /*
982                          * Leave cmd processing on error, detach,
983                          * kill, continue, or single step.
984                          */
985                         if (error >= 0 || remcom_in_buffer[0] == 'D' ||
986                             remcom_in_buffer[0] == 'k') {
987                                 error = 0;
988                                 goto kgdb_exit;
989                         }
990
991                 }
992
993                 /* reply to the request */
994                 put_packet(remcom_out_buffer);
995         }
996
997 kgdb_exit:
998         if (ks->pass_exception)
999                 error = 1;
1000         return error;
1001 }
1002
1003 int gdbstub_state(struct kgdb_state *ks, char *cmd)
1004 {
1005         int error;
1006
1007         switch (cmd[0]) {
1008         case 'e':
1009                 error = kgdb_arch_handle_exception(ks->ex_vector,
1010                                                    ks->signo,
1011                                                    ks->err_code,
1012                                                    remcom_in_buffer,
1013                                                    remcom_out_buffer,
1014                                                    ks->linux_regs);
1015                 return error;
1016         case 's':
1017         case 'c':
1018                 strcpy(remcom_in_buffer, cmd);
1019                 return 0;
1020         case '?':
1021                 gdb_cmd_status(ks);
1022                 break;
1023         case '\0':
1024                 strcpy(remcom_out_buffer, "");
1025                 break;
1026         }
1027         dbg_io_ops->write_char('+');
1028         put_packet(remcom_out_buffer);
1029         return 0;
1030 }