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