LCOV - code coverage report
Current view: top level - kernel/debug - gdbstub.c (source / functions) Hit Total Coverage
Test: Real Lines: 0 434 0.0 %
Date: 2020-10-17 15:46:16 Functions: 0 33 0.0 %
Legend: Neither, QEMU, Real, Both Branches: 0 0 -

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

Generated by: LCOV version 1.14