OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [or1ksim/] [debug/] [rsp-server.c] - Blame information for rev 233

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 jeremybenn
/* rsp-server.c -- Remote Serial Protocol server for GDB
2
 
3
   Copyright (C) 2008 Embecosm Limited
4
 
5
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
6
 
7
   This file is part of Or1ksim, the OpenRISC 1000 Architectural Simulator.
8
 
9
   This program is free software; you can redistribute it and/or modify it
10
   under the terms of the GNU General Public License as published by the Free
11
   Software Foundation; either version 3 of the License, or (at your option)
12
   any later version.
13
 
14
   This program is distributed in the hope that it will be useful, but WITHOUT
15
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
17
   more details.
18
 
19
   You should have received a copy of the GNU General Public License along
20
   with this program.  If not, see <http://www.gnu.org/licenses/>.  */
21
 
22
/* This program is commented throughout in a fashion suitable for processing
23
   with Doxygen. */
24
 
25
 
26
/* Autoconf and/or portability configuration */
27
#include "config.h"
28
#include "port.h"
29
 
30
/* System includes */
31
#include <stdlib.h>
32
#include <unistd.h>
33
#include <netdb.h>
34
#include <stdio.h>
35
#include <errno.h>
36
#include <sys/types.h>
37
#include <sys/socket.h>
38
#include <fcntl.h>
39
#include <arpa/inet.h>
40
#include <poll.h>
41
#include <netinet/tcp.h>
42
#include <signal.h>
43
 
44
/* Package includes */
45
#include "sim-config.h"
46
#include "except.h"
47
#include "opcode/or32.h"
48
#include "spr-defs.h"
49
#include "execute.h"
50
#include "debug-unit.h"
51
#include "sprs.h"
52
#include "toplevel-support.h"
53 143 jeremybenn
#include "dcache-model.h"
54
#include "icache-model.h"
55 19 jeremybenn
 
56
 
57
/* Define to log each packet */
58
/* #define RSP_TRACE  1 */
59
 
60
/*! Name of the Or1ksim RSP service */
61
#define OR1KSIM_RSP_SERVICE  "or1ksim-rsp"
62
 
63
/*! Protocol used by Or1ksim */
64
#define OR1KSIM_RSP_PROTOCOL  "tcp"
65
 
66
/*! Thread ID used by Or1ksim */
67
#define OR1KSIM_TID  1
68
 
69
/* Indices of GDB registers that are not GPRs. Must match GDB settings! */
70
#define PPC_REGNUM  (MAX_GPRS + 0)      /*!< Previous PC */
71
#define NPC_REGNUM  (MAX_GPRS + 1)      /*!< Next PC */
72
#define SR_REGNUM   (MAX_GPRS + 2)      /*!< Supervision Register */
73
#define NUM_REGS    (MAX_GRPS + 3)      /*!< Total GDB registers */
74
 
75
/*! Trap instruction for OR32 */
76
#define OR1K_TRAP_INSTR  0x21000001
77
 
78
/*! Definition of GDB target signals. Data taken from the GDB 6.8
79
    source. Only those we use defined here. */
80
enum target_signal {
81
  TARGET_SIGNAL_NONE =  0,
82
  TARGET_SIGNAL_INT  =  2,
83
  TARGET_SIGNAL_ILL  =  4,
84
  TARGET_SIGNAL_TRAP =  5,
85
  TARGET_SIGNAL_FPE  =  8,
86
  TARGET_SIGNAL_BUS  = 10,
87
  TARGET_SIGNAL_SEGV = 11,
88
  TARGET_SIGNAL_ALRM = 14,
89
  TARGET_SIGNAL_USR2 = 31,
90
  TARGET_SIGNAL_PWR  = 32
91
};
92
 
93
/*! The maximum number of characters in inbound/outbound buffers.  The largest
94
    packets are the 'G' packet, which must hold the 'G' and all the registers
95
    with two hex digits per byte and the 'g' reply, which must hold all the
96
    registers, and (in our implementation) an end-of-string (0)
97
    character. Adding the EOS allows us to print out the packet as a
98
    string. So at least NUMREGBYTES*2 + 1 (for the 'G' or the EOS) are needed
99
    for register packets */
100
#define GDB_BUF_MAX  ((NUM_REGS) * 8 + 1)
101
 
102
/*! Size of the matchpoint hash table. Largest prime < 2^10 */
103
#define MP_HASH_SIZE  1021
104
 
105
/*! String to map hex digits to chars */
106
static const char hexchars[]="0123456789abcdef";
107
 
108
/*! Data structure for RSP buffers. Can't be null terminated, since it may
109
  include zero bytes */
110
struct rsp_buf
111
{
112
  char  data[GDB_BUF_MAX];
113
  int   len;
114
};
115
 
116
/*! Enumeration of different types of matchpoint. These have explicit values
117
    matching the second digit of 'z' and 'Z' packets. */
118
enum mp_type {
119
  BP_MEMORY   = 0,
120
  BP_HARDWARE = 1,
121
  WP_WRITE    = 2,
122
  WP_READ     = 3,
123
  WP_ACCESS   = 4
124
};
125
 
126
/*! Data structure for a matchpoint hash table entry */
127
struct mp_entry
128
{
129
  enum mp_type       type;              /*!< Type of matchpoint */
130
  unsigned long int  addr;              /*!< Address with the matchpoint */
131
  unsigned long int  instr;             /*!< Substituted instruction */
132
  struct mp_entry   *next;              /*!< Next entry with this hash */
133
};
134
 
135
/*! Central data for the RSP connection */
136
static struct
137
{
138
  int                client_waiting;    /*!< Is client waiting a response? */
139
  int                proto_num;         /*!< Number of the protocol used */
140
  int                client_fd;         /*!< FD for talking to GDB */
141
  int                sigval;            /*!< GDB signal for any exception */
142
  unsigned long int  start_addr;        /*!< Start of last run */
143
  struct mp_entry   *mp_hash[MP_HASH_SIZE];     /*!< Matchpoint hash table */
144
} rsp;
145
 
146
/* Forward declarations of static functions */
147
static void               rsp_get_client ();
148
static void               rsp_client_request ();
149
static void               rsp_client_close ();
150
static void               put_packet (struct rsp_buf *buf);
151
static void               put_str_packet (const char *str);
152
static struct rsp_buf    *get_packet ();
153
static void               put_rsp_char (char  c);
154
static int                get_rsp_char ();
155
static int                rsp_unescape (char *data,
156
                                        int   len);
157
static void               mp_hash_init ();
158
static void               mp_hash_add (enum mp_type       type,
159
                                       unsigned long int  addr,
160
                                       unsigned long int  instr);
161
static struct mp_entry   *mp_hash_lookup (enum mp_type       type,
162
                                          unsigned long int  addr);
163
static struct mp_entry   *mp_hash_delete (enum mp_type       type,
164
                                          unsigned long int  addr);
165
static int                hex (int  c);
166
static void               reg2hex (unsigned long int  val,
167
                                   char              *buf);
168
static unsigned long int  hex2reg (char *buf);
169
static void               ascii2hex (char *dest,
170
                                     char *src);
171
static void               hex2ascii (char *dest,
172
                                     char *src);
173
static void               set_npc (unsigned long int  addr);
174
static void               rsp_report_exception ();
175
static void               rsp_continue (struct rsp_buf *buf);
176
static void               rsp_continue_with_signal (struct rsp_buf *buf);
177
static void               rsp_continue_generic (unsigned long int  addr,
178
                                                unsigned long int  except);
179
static void               rsp_read_all_regs ();
180
static void               rsp_write_all_regs (struct rsp_buf *buf);
181
static void               rsp_read_mem (struct rsp_buf *buf);
182
static void               rsp_write_mem (struct rsp_buf *buf);
183
static void               rsp_read_reg (struct rsp_buf *buf);
184
static void               rsp_write_reg (struct rsp_buf *buf);
185
static void               rsp_query (struct rsp_buf *buf);
186
static void               rsp_command (struct rsp_buf *buf);
187
static void               rsp_set (struct rsp_buf *buf);
188
static void               rsp_restart ();
189
static void               rsp_step (struct rsp_buf *buf);
190
static void               rsp_step_with_signal (struct rsp_buf *buf);
191
static void               rsp_step_generic (unsigned long int  addr,
192
                                            unsigned long int  except);
193
static void               rsp_vpkt (struct rsp_buf *buf);
194
static void               rsp_write_mem_bin (struct rsp_buf *buf);
195
static void               rsp_remove_matchpoint (struct rsp_buf *buf);
196
static void               rsp_insert_matchpoint (struct rsp_buf *buf);
197
 
198
 
199
/*---------------------------------------------------------------------------*/
200
/*!Initialize the Remote Serial Protocol connection
201
 
202
   Set up the central data structures.                                       */
203
/*---------------------------------------------------------------------------*/
204
void
205
rsp_init ()
206
{
207
  /* Clear out the central data structure */
208
  rsp.client_waiting =  0;               /* GDB client is not waiting for us */
209
  rsp.client_fd      = -1;              /* i.e. invalid */
210
  rsp.sigval         =  0;               /* No exception */
211
  rsp.start_addr     = EXCEPT_RESET;    /* Default restart point */
212
 
213
  /* Set up the matchpoint hash table */
214
  mp_hash_init ();
215
 
216
}       /* rsp_init () */
217
 
218
 
219
/*---------------------------------------------------------------------------*/
220
/*!Look for action on RSP
221
 
222
   This function is called when the processor has stalled, which, except for
223
   initialization, must be due to an interrupt.
224
 
225
   If we have no RSP client, we get one. We can make no progress until the
226
   client is available.
227
 
228
   Then if the cause is an exception following a step or continue command, and
229
   the exception not been notified to GDB, a packet reporting the cause of the
230
   exception is sent.
231
 
232
   The next client request is then processed.                                */
233
/*---------------------------------------------------------------------------*/
234
void
235
handle_rsp ()
236
{
237
  /* If we have no RSP client, wait until we get one. */
238
  while (-1 == rsp.client_fd)
239
    {
240
      rsp_get_client ();
241
      rsp.client_waiting = 0;            /* No longer waiting */
242
    }
243
 
244
  /* If we have an unacknowledged exception tell the GDB client. If this
245
     exception was a trap due to a memory breakpoint, then adjust the NPC. */
246
  if (rsp.client_waiting)
247
    {
248
      if ((TARGET_SIGNAL_TRAP == rsp.sigval) &&
249
          (NULL != mp_hash_lookup (BP_MEMORY, cpu_state.sprs[SPR_PPC])))
250
        {
251
          set_npc (cpu_state.sprs[SPR_PPC]);
252
        }
253
 
254
      rsp_report_exception();
255
      rsp.client_waiting = 0;            /* No longer waiting */
256
    }
257
 
258
  /* Get a RSP client request */
259
  rsp_client_request ();
260
 
261
}       /* handle_rsp () */
262
 
263
 
264
/*---------------------------------------------------------------------------*/
265
/*!Note an exception for future processing
266
 
267
   The simulator has encountered an exception. Record it here, so that a
268
   future call to handle_exception will report it back to the client. The
269
   signal is supplied in Or1ksim form and recorded in GDB form.
270
 
271
   We flag up a warning if an exception is already pending, and ignore the
272
   earlier exception.
273
 
274
   @param[in] except  The exception (Or1ksim form)                           */
275
/*---------------------------------------------------------------------------*/
276
void
277
rsp_exception (unsigned long int  except)
278
{
279
  int  sigval;                  /* GDB signal equivalent to exception */
280
 
281
  switch (except)
282
    {
283
    case EXCEPT_RESET:    sigval = TARGET_SIGNAL_PWR;  break;
284
    case EXCEPT_BUSERR:   sigval = TARGET_SIGNAL_BUS;  break;
285
    case EXCEPT_DPF:      sigval = TARGET_SIGNAL_SEGV; break;
286
    case EXCEPT_IPF:      sigval = TARGET_SIGNAL_SEGV; break;
287
    case EXCEPT_TICK:     sigval = TARGET_SIGNAL_ALRM; break;
288
    case EXCEPT_ALIGN:    sigval = TARGET_SIGNAL_BUS;  break;
289
    case EXCEPT_ILLEGAL:  sigval = TARGET_SIGNAL_ILL;  break;
290
    case EXCEPT_INT:      sigval = TARGET_SIGNAL_INT;  break;
291
    case EXCEPT_DTLBMISS: sigval = TARGET_SIGNAL_SEGV; break;
292
    case EXCEPT_ITLBMISS: sigval = TARGET_SIGNAL_SEGV; break;
293
    case EXCEPT_RANGE:    sigval = TARGET_SIGNAL_FPE;  break;
294
    case EXCEPT_SYSCALL:  sigval = TARGET_SIGNAL_USR2; break;
295
    case EXCEPT_FPE:      sigval = TARGET_SIGNAL_FPE;  break;
296
    case EXCEPT_TRAP:     sigval = TARGET_SIGNAL_TRAP; break;
297
 
298
    default:
299
      fprintf (stderr, "Warning: Unknown RSP exception %lu: Ignored\n", except);
300
      return;
301
    }
302
 
303
  if ((0 != rsp.sigval) && (sigval != rsp.sigval))
304
    {
305
      fprintf (stderr, "Warning: RSP signal %d received while signal "
306
               "%d pending: Pending exception replaced\n", sigval, rsp.sigval);
307
    }
308
 
309
  rsp.sigval         = sigval;          /* Save the signal value */
310
 
311
}       /* rsp_exception () */
312
 
313
 
314
/*---------------------------------------------------------------------------*/
315
/*!Get a new client connection.
316
 
317
   Blocks until the client connection is available.
318
 
319
   A lot of this code is copied from remote_open in gdbserver remote-utils.c.
320
 
321
   This involves setting up a socket to listen on a socket for attempted
322
   connections from a single GDB instance (we couldn't be talking to multiple
323
   GDBs at once!).
324
 
325
   The service is specified either as a port number in the Or1ksim configuration
326
   (parameter rsp_port in section debug, default 51000) or as a service name
327
   in the constant OR1KSIM_RSP_SERVICE.
328
 
329
   The protocol used for communication is specified in OR1KSIM_RSP_PROTOCOL. */
330
/*---------------------------------------------------------------------------*/
331
static void
332
rsp_get_client ()
333
{
334
  int                 tmp_fd;           /* Temporary descriptor for socket */
335
  int                 optval;           /* Socket options */
336
  struct sockaddr_in  sock_addr;        /* Socket address */
337
  socklen_t           len;              /* Size of the socket address */
338
 
339
  /* 0 is used as the RSP port number to indicate that we should use the
340
     service name instead. */
341
  if (0 == config.debug.rsp_port)
342
    {
343
      struct servent *service =
344
        getservbyname (OR1KSIM_RSP_SERVICE, "tcp");
345
 
346
      if (NULL == service)
347
        {
348
          fprintf (stderr, "Warning: RSP unable to find service \"%s\": %s\n",
349
                   OR1KSIM_RSP_SERVICE, strerror (errno));
350
          return;
351
        }
352
 
353
      config.debug.rsp_port = ntohs (service->s_port);
354
    }
355
 
356
  /* Open a socket on which we'll listen for clients */
357
  tmp_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
358
  if (tmp_fd < 0)
359
    {
360
      fprintf (stderr, "ERROR: Cannot open RSP socket\n");
361
      sim_done ();
362
    }
363
 
364
  /* Allow rapid reuse of the port on this socket */
365
  optval = 1;
366
  setsockopt (tmp_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
367
              sizeof (optval));
368
 
369
  /* Bind the port to the socket */
370
  sock_addr.sin_family      = PF_INET;
371
  sock_addr.sin_port        = htons (config.debug.rsp_port);
372
  sock_addr.sin_addr.s_addr = INADDR_ANY;
373
  if (bind (tmp_fd, (struct sockaddr *) &sock_addr, sizeof (sock_addr)))
374
    {
375
      fprintf (stderr, "ERROR: Cannot bind to RSP socket\n");
376
      sim_done ();
377
    }
378
 
379
  /* Listen for (at most one) client */
380
  if (listen (tmp_fd, 1))
381
    {
382
      fprintf (stderr, "ERROR: Cannot listen on RSP socket\n");
383
      sim_done ();
384
    }
385
 
386
  printf ("Listening for RSP on port %d\n", config.debug.rsp_port);
387
  fflush (stdout);
388
 
389
  /* Accept a client which connects */
390 85 jeremybenn
  len = sizeof (socklen_t);             /* Bug fix by Julius Baxter */
391 19 jeremybenn
  rsp.client_fd = accept (tmp_fd, (struct sockaddr *)&sock_addr, &len);
392
 
393
  if (-1 == rsp.client_fd)
394
    {
395
      fprintf (stderr, "Warning: Failed to accept RSP client\n");
396
      return;
397
    }
398
 
399
  /* Enable TCP keep alive process */
400
  optval = 1;
401
  setsockopt (rsp.client_fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval,
402
              sizeof (optval));
403
 
404
  /* Don't delay small packets, for better interactive response (disable
405
     Nagel's algorithm) */
406
  optval = 1;
407
  setsockopt (rsp.client_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&optval,
408
              sizeof (optval));
409
 
410
  /* Socket is no longer needed */
411
  close (tmp_fd);                       /* No longer need this */
412
  signal (SIGPIPE, SIG_IGN);            /* So we don't exit if client dies */
413
 
414
  printf ("Remote debugging from host %s\n", inet_ntoa (sock_addr.sin_addr));
415
 
416
  /* If fcntl () is available, use it to enable async I/O
417
#if defined(F_SETFL) && defined (FASYNC)
418
  save_fcntl_flags = fcntl (remote_desc, F_GETFL, 0);
419
  fcntl (remote_desc, F_SETFL, save_fcntl_flags | FASYNC);
420
#if defined (F_SETOWN)
421
  fcntl (remote_desc, F_SETOWN, getpid ());
422
#endif
423
#endif */
424
 
425
}       /* rsp_get_client () */
426
 
427
 
428
/*---------------------------------------------------------------------------*/
429
/*!Deal with a request from the GDB client session
430
 
431
   In general, apart from the simplest requests, this function replies on
432
   other functions to implement the functionality.                           */
433
/*---------------------------------------------------------------------------*/
434
static void
435
rsp_client_request ()
436
{
437
  struct rsp_buf *buf = get_packet ();  /* Message sent to us */
438
 
439
  // Null packet means we hit EOF or the link was closed for some other
440
  // reason. Close the client and return
441
  if (NULL == buf)
442
    {
443
      rsp_client_close ();
444
      return;
445
    }
446
 
447
#if RSP_TRACE
448
  printf ("Packet received %s: %d chars\n", buf->data, buf->len );
449
  fflush (stdout);
450
#endif
451
 
452
  switch (buf->data[0])
453
    {
454
    case '!':
455
      /* Request for extended remote mode */
456
      put_str_packet ("OK");
457
      return;
458
 
459
    case '?':
460
      /* Return last signal ID */
461
      rsp_report_exception();
462
      return;
463
 
464
    case 'A':
465
      /* Initialization of argv not supported */
466
      fprintf (stderr, "Warning: RSP 'A' packet not supported: ignored\n");
467
      put_str_packet ("E01");
468
      return;
469
 
470
    case 'b':
471
      /* Setting baud rate is deprecated */
472
      fprintf (stderr, "Warning: RSP 'b' packet is deprecated and not "
473
               "supported: ignored\n");
474
      return;
475
 
476
    case 'B':
477
      /* Breakpoints should be set using Z packets */
478
      fprintf (stderr, "Warning: RSP 'B' packet is deprecated (use 'Z'/'z' "
479
               "packets instead): ignored\n");
480
      return;
481
 
482
    case 'c':
483
      /* Continue */
484
      rsp_continue (buf);
485
      return;
486
 
487
    case 'C':
488
      /* Continue with signal */
489
      rsp_continue_with_signal (buf);
490
      return;
491
 
492
    case 'd':
493
      /* Disable debug using a general query */
494
      fprintf (stderr, "Warning: RSP 'd' packet is deprecated (define a 'Q' "
495
               "packet instead: ignored\n");
496
      return;
497
 
498
    case 'D':
499
      /* Detach GDB. Do this by closing the client. The rules say that
500
         execution should continue. TODO. Is this really then intended
501
         meaning? Or does it just mean that only vAttach will be recognized
502
         after this? */
503
      put_str_packet ("OK");
504
      rsp_client_close ();
505
      set_stall_state (0);
506
      rsp.sigval = TARGET_SIGNAL_NONE;  /* No signal now */
507
      return;
508
 
509
    case 'F':
510
      /* File I/O is not currently supported */
511
      fprintf (stderr, "Warning: RSP file I/O not currently supported: 'F' "
512
               "packet ignored\n");
513
      return;
514
 
515
    case 'g':
516
      rsp_read_all_regs ();
517
      return;
518
 
519
    case 'G':
520
      rsp_write_all_regs (buf);
521
      return;
522
 
523
    case 'H':
524
      /* Set the thread number of subsequent operations. For now ignore
525
         silently and just reply "OK" */
526
      put_str_packet ("OK");
527
      return;
528
 
529
    case 'i':
530
      /* Single instruction step */
531
      fprintf (stderr, "Warning: RSP cycle stepping not supported: target "
532
               "stopped immediately\n");
533
      rsp.client_waiting = 1;                   /* Stop reply will be sent */
534
      return;
535
 
536
    case 'I':
537
      /* Single instruction step with signal */
538
      fprintf (stderr, "Warning: RSP cycle stepping not supported: target "
539
               "stopped immediately\n");
540
      rsp.client_waiting = 1;                   /* Stop reply will be sent */
541
      return;
542
 
543
    case 'k':
544
      /* Kill request. Do nothing for now. */
545
      return;
546
 
547
    case 'm':
548
      /* Read memory (symbolic) */
549
      rsp_read_mem (buf);
550
      return;
551
 
552
    case 'M':
553
      /* Write memory (symbolic) */
554
      rsp_write_mem (buf);
555
      return;
556
 
557
    case 'p':
558
      /* Read a register */
559
      rsp_read_reg (buf);
560
      return;
561
 
562
    case 'P':
563
      /* Write a register */
564
      rsp_write_reg (buf);
565
      return;
566
 
567
    case 'q':
568
      /* Any one of a number of query packets */
569
      rsp_query (buf);
570
      return;
571
 
572
    case 'Q':
573
      /* Any one of a number of set packets */
574
      rsp_set (buf);
575
      return;
576
 
577
    case 'r':
578
      /* Reset the system. Deprecated (use 'R' instead) */
579
      fprintf (stderr, "Warning: RSP 'r' packet is deprecated (use 'R' "
580
               "packet instead): ignored\n");
581
      return;
582
 
583
    case 'R':
584
      /* Restart the program being debugged. */
585
      rsp_restart ();
586
      return;
587
 
588
    case 's':
589
      /* Single step (one high level instruction). This could be hard without
590
         DWARF2 info */
591
      rsp_step (buf);
592
      return;
593
 
594
    case 'S':
595
      /* Single step (one high level instruction) with signal. This could be
596
         hard without DWARF2 info */
597
      rsp_step_with_signal (buf);
598
      return;
599
 
600
    case 't':
601
      /* Search. This is not well defined in the manual and for now we don't
602
         support it. No response is defined. */
603
      fprintf (stderr, "Warning: RSP 't' packet not supported: ignored\n");
604
      return;
605
 
606
    case 'T':
607
      /* Is the thread alive. We are bare metal, so don't have a thread
608
         context. The answer is always "OK". */
609
      put_str_packet ("OK");
610
      return;
611
 
612
    case 'v':
613
      /* Any one of a number of packets to control execution */
614
      rsp_vpkt (buf);
615
      return;
616
 
617
    case 'X':
618
      /* Write memory (binary) */
619
      rsp_write_mem_bin (buf);
620
      return;
621
 
622
    case 'z':
623
      /* Remove a breakpoint/watchpoint. */
624
      rsp_remove_matchpoint (buf);
625
      return;
626
 
627
    case 'Z':
628
      /* Insert a breakpoint/watchpoint. */
629
      rsp_insert_matchpoint (buf);
630
      return;
631
 
632
    default:
633
      /* Unknown commands are ignored */
634
      fprintf (stderr, "Warning: Unknown RSP request %s\n", buf->data);
635
      return;
636
    }
637
}       /* rsp_client_request () */
638
 
639
 
640
/*---------------------------------------------------------------------------*/
641
/*!Close the connection to the client if it is open                          */
642
/*---------------------------------------------------------------------------*/
643
static void
644
rsp_client_close ()
645
{
646
  if (-1 != rsp.client_fd)
647
    {
648
      close (rsp.client_fd);
649
      rsp.client_fd = -1;
650
    }
651
}       /* rsp_client_close () */
652
 
653
 
654
/*---------------------------------------------------------------------------*/
655
/*!Send a packet to the GDB client
656
 
657
   Modeled on the stub version supplied with GDB. Put out the data preceded by
658
   a '$', followed by a '#' and a one byte checksum. '$', '#', '*' and '}' are
659
   escaped by preceding them with '}' and then XORing the character with
660
   0x20.
661
 
662
   @param[in] buf  The data to send                                          */
663
/*---------------------------------------------------------------------------*/
664
static void
665
put_packet (struct rsp_buf *buf)
666
{
667
  int  ch;                              /* Ack char */
668
 
669
  /* Construct $<packet info>#<checksum>. Repeat until the GDB client
670
     acknowledges satisfactory receipt. */
671
  do
672
    {
673
      unsigned char checksum = 0;        /* Computed checksum */
674
      int           count    = 0;        /* Index into the buffer */
675
 
676
#if RSP_TRACE
677
      printf ("Putting %s\n", buf->data);
678
      fflush (stdout);
679
#endif
680
 
681
      put_rsp_char ('$');               /* Start char */
682
 
683
      /* Body of the packet */
684
      for (count = 0; count < buf->len; count++)
685
        {
686
          unsigned char  ch = buf->data[count];
687
 
688
          /* Check for escaped chars */
689
          if (('$' == ch) || ('#' == ch) || ('*' == ch) || ('}' == ch))
690
            {
691
              ch       ^= 0x20;
692
              checksum += (unsigned char)'}';
693
              put_rsp_char ('}');
694
            }
695
 
696
          checksum += ch;
697
          put_rsp_char (ch);
698
        }
699
 
700
      put_rsp_char ('#');               /* End char */
701
 
702
      /* Computed checksum */
703
      put_rsp_char (hexchars[checksum >> 4]);
704
      put_rsp_char (hexchars[checksum % 16]);
705
 
706
      /* Check for ack of connection failure */
707
      ch = get_rsp_char ();
708
      if (-1 == ch)
709
        {
710
          return;                       /* Fail the put silently. */
711
        }
712
    }
713
  while ('+' != ch);
714
 
715
}       /* put_packet () */
716
 
717
 
718
/*---------------------------------------------------------------------------*/
719
/*!Convenience to put a constant string packet
720
 
721
   param[in] str  The text of the packet                                     */
722
/*---------------------------------------------------------------------------*/
723
static void
724
put_str_packet (const char *str)
725
{
726
  struct rsp_buf  buf;
727
  int             len = strlen (str);
728
 
729
  /* Construct the packet to send, so long as string is not too big,
730
     otherwise truncate. Add EOS at the end for convenient debug printout */
731
 
732
  if (len >= GDB_BUF_MAX)
733
    {
734
      fprintf (stderr, "Warning: String %s too large for RSP packet: "
735
               "truncated\n", str);
736
      len = GDB_BUF_MAX - 1;
737
    }
738
 
739
  strncpy (buf.data, str, len);
740
  buf.data[len] = 0;
741
  buf.len       = len;
742
 
743
  put_packet (&buf);
744
 
745
}       /* put_str_packet () */
746
 
747
 
748
/*---------------------------------------------------------------------------*/
749
/*!Get a packet from the GDB client
750
 
751
   Modeled on the stub version supplied with GDB. The data is in a static
752
   buffer. The data should be copied elsewhere if it is to be preserved across
753
   a subsequent call to get_packet().
754
 
755
   Unlike the reference implementation, we don't deal with sequence
756
   numbers. GDB has never used them, and this implementation is only intended
757
   for use with GDB 6.8 or later. Sequence numbers were removed from the RSP
758
   standard at GDB 5.0.
759
 
760
   @return  A pointer to the static buffer containing the data                */
761
/*---------------------------------------------------------------------------*/
762
static struct rsp_buf *
763
get_packet ()
764
{
765
  static struct rsp_buf  buf;           /* Survives the return */
766
 
767
  /* Keep getting packets, until one is found with a valid checksum */
768
  while (1)
769
    {
770
      unsigned char  checksum;          /* The checksum we have computed */
771
      int            count;             /* Index into the buffer */
772
      int            ch;                /* Current character */
773
 
774
      /* Wait around for the start character ('$'). Ignore all other
775
         characters */
776
      ch = get_rsp_char ();
777
      while (ch != '$')
778
        {
779
          if (-1 == ch)
780
            {
781
              return  NULL;             /* Connection failed */
782
            }
783
 
784
          ch = get_rsp_char ();
785
        }
786
 
787
      /* Read until a '#' or end of buffer is found */
788
      checksum =  0;
789
      count    =  0;
790
      while (count < GDB_BUF_MAX - 1)
791
        {
792
          ch = get_rsp_char ();
793
 
794
          /* Check for connection failure */
795
          if (-1 == ch)
796
            {
797
              return  NULL;
798
            }
799
 
800
          /* If we hit a start of line char begin all over again */
801
          if ('$' == ch)
802
            {
803
              checksum =  0;
804
              count    =  0;
805
 
806
              continue;
807
            }
808
 
809
          /* Break out if we get the end of line char */
810
          if ('#' == ch)
811
            {
812
              break;
813
            }
814
 
815
          /* Update the checksum and add the char to the buffer */
816
 
817
          checksum        = checksum + (unsigned char)ch;
818
          buf.data[count] = (char)ch;
819
          count           = count + 1;
820
        }
821
 
822
      /* Mark the end of the buffer with EOS - it's convenient for non-binary
823
         data to be valid strings. */
824
      buf.data[count] = 0;
825
      buf.len         = count;
826
 
827
      /* If we have a valid end of packet char, validate the checksum */
828
      if ('#' == ch)
829
        {
830
          unsigned char  xmitcsum;      /* The checksum in the packet */
831
 
832
          ch = get_rsp_char ();
833
          if (-1 == ch)
834
            {
835
              return  NULL;             /* Connection failed */
836
            }
837
          xmitcsum = hex (ch) << 4;
838
 
839
          ch = get_rsp_char ();
840
          if (-1 == ch)
841
            {
842
              return  NULL;             /* Connection failed */
843
            }
844
 
845
          xmitcsum += hex (ch);
846
 
847
          /* If the checksums don't match print a warning, and put the
848
             negative ack back to the client. Otherwise put a positive ack. */
849
          if (checksum != xmitcsum)
850
            {
851
              fprintf (stderr, "Warning: Bad RSP checksum: Computed "
852
                       "0x%02x, received 0x%02x\n", checksum, xmitcsum);
853
 
854
              put_rsp_char ('-');       /* Failed checksum */
855
            }
856
          else
857
            {
858
              put_rsp_char ('+');       /* successful transfer */
859
              break;
860
            }
861
        }
862
      else
863
        {
864
          fprintf (stderr, "Warning: RSP packet overran buffer\n");
865
        }
866
    }
867
 
868
  return &buf;                          /* Success */
869
 
870
}       /* get_packet () */
871
 
872
 
873
/*---------------------------------------------------------------------------*/
874
/*!Put a single character out onto the client socket
875
 
876
   This should only be called if the client is open, but we check for safety.
877
 
878
   @param[in] c  The character to put out                                    */
879
/*---------------------------------------------------------------------------*/
880
static void
881
put_rsp_char (char  c)
882
{
883
  if (-1 == rsp.client_fd)
884
    {
885
      fprintf (stderr, "Warning: Attempt to write '%c' to unopened RSP "
886
               "client: Ignored\n", c);
887
      return;
888
    }
889
 
890
  /* Write until successful (we retry after interrupts) or catastrophic
891
     failure. */
892
  while (1)
893
    {
894
      switch (write (rsp.client_fd, &c, sizeof (c)))
895
        {
896
        case -1:
897
          /* Error: only allow interrupts or would block */
898
          if ((EAGAIN != errno) && (EINTR != errno))
899
            {
900
              fprintf (stderr, "Warning: Failed to write to RSP client: "
901
                       "Closing client connection: %s\n",
902
                       strerror (errno));
903
              rsp_client_close ();
904
              return;
905
            }
906
 
907
          break;
908
 
909
        case 0:
910
          break;                /* Nothing written! Try again */
911
 
912
        default:
913
          return;               /* Success, we can return */
914
        }
915
    }
916
}       /* put_rsp_char () */
917
 
918
 
919
/*---------------------------------------------------------------------------*/
920
/*!Get a single character from the client socket
921
 
922
   This should only be called if the client is open, but we check for safety.
923
 
924
   @return  The character read, or -1 on failure                             */
925
/*---------------------------------------------------------------------------*/
926
static int
927
get_rsp_char ()
928
{
929
  if (-1 == rsp.client_fd)
930
    {
931
      fprintf (stderr, "Warning: Attempt to read from unopened RSP "
932
               "client: Ignored\n");
933
      return  -1;
934
    }
935
 
936
  /* Non-blocking read until successful (we retry after interrupts) or
937
     catastrophic failure. */
938
  while (1)
939
    {
940
      unsigned char  c;
941
 
942
      switch (read (rsp.client_fd, &c, sizeof (c)))
943
        {
944
        case -1:
945
          /* Error: only allow interrupts */
946
          if ((EAGAIN != errno) && (EINTR != errno))
947
            {
948
              fprintf (stderr, "Warning: Failed to read from RSP client: "
949
                       "Closing client connection: %s\n",
950
                       strerror (errno));
951
              rsp_client_close ();
952
              return  -1;
953
            }
954
 
955
          break;
956
 
957
        case 0:
958
          // EOF
959
          rsp_client_close ();
960
          return  -1;
961
 
962
        default:
963
          return  c & 0xff; /* Success, we can return (no sign extend!) */
964
        }
965
    }
966
}       /* get_rsp_char () */
967
 
968
 
969
/*---------------------------------------------------------------------------*/
970
/*!"Unescape" RSP binary data
971
 
972
   '#', '$' and '}' are escaped by preceding them by '}' and oring with 0x20.
973
 
974
   This function reverses that, modifying the data in place.
975
 
976
   @param[in] data  The array of bytes to convert
977
   @para[in]  len   The number of bytes to be converted
978
 
979
   @return  The number of bytes AFTER conversion                             */
980
/*---------------------------------------------------------------------------*/
981
static int
982
rsp_unescape (char *data,
983
              int   len)
984
{
985
  int  from_off = 0;             /* Offset to source char */
986
  int  to_off   = 0;             /* Offset to dest char */
987
 
988
  while (from_off < len)
989
    {
990
      /* Is it escaped */
991
      if ( '}' == data[from_off])
992
        {
993
          from_off++;
994
          data[to_off] = data[from_off] ^ 0x20;
995
        }
996
      else
997
        {
998
          data[to_off] = data[from_off];
999
        }
1000
 
1001
      from_off++;
1002
      to_off++;
1003
    }
1004
 
1005
  return  to_off;
1006
 
1007
}       /* rsp_unescape () */
1008
 
1009
 
1010
/*---------------------------------------------------------------------------*/
1011
/*!Initialize the matchpoint hash table
1012
 
1013
   This is an open hash table, so this function clears all the links to
1014
   NULL.                                                                     */
1015
/*---------------------------------------------------------------------------*/
1016
static void
1017
mp_hash_init ()
1018
{
1019
  int  i;
1020
 
1021
  for (i = 0; i < MP_HASH_SIZE; i++)
1022
    {
1023
      rsp.mp_hash[i] = NULL;
1024
    }
1025
}       /* mp_hash_init () */
1026
 
1027
 
1028
/*---------------------------------------------------------------------------*/
1029
/*!Add an entry to the matchpoint hash table
1030
 
1031
   Add the entry if it wasn't already there. If it was there do nothing. The
1032
   match just be on type and addr. The instr need not match, since if this is
1033
   a duplicate insertion (perhaps due to a lost packet) they will be
1034
   different.
1035
 
1036
   @param[in] type   The type of matchpoint
1037
   @param[in] addr   The address of the matchpoint
1038
   @para[in]  instr  The instruction to associate with the address           */
1039
/*---------------------------------------------------------------------------*/
1040
static void
1041
mp_hash_add (enum mp_type       type,
1042
             unsigned long int  addr,
1043
             unsigned long int  instr)
1044
{
1045
  int              hv    = addr % MP_HASH_SIZE;
1046
  struct mp_entry *curr;
1047
 
1048
  /* See if we already have the entry */
1049
  for(curr = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1050
    {
1051
      if ((type == curr->type) && (addr == curr->addr))
1052
        {
1053
          return;               /* We already have the entry */
1054
        }
1055
    }
1056
 
1057
  /* Insert the new entry at the head of the chain */
1058
  curr = malloc (sizeof (*curr));
1059
 
1060
  curr->type  = type;
1061
  curr->addr  = addr;
1062
  curr->instr = instr;
1063
  curr->next  = rsp.mp_hash[hv];
1064
 
1065
  rsp.mp_hash[hv] = curr;
1066
 
1067
}       /* mp_hash_add () */
1068
 
1069
 
1070
/*---------------------------------------------------------------------------*/
1071
/*!Look up an entry in the matchpoint hash table
1072
 
1073
   The match must be on type AND addr.
1074
 
1075
   @param[in] type   The type of matchpoint
1076
   @param[in] addr   The address of the matchpoint
1077
 
1078
   @return  The entry deleted, or NULL if the entry was not found            */
1079
/*---------------------------------------------------------------------------*/
1080
static struct mp_entry *
1081
mp_hash_lookup (enum mp_type       type,
1082
                unsigned long int  addr)
1083
{
1084
  int              hv   = addr % MP_HASH_SIZE;
1085
  struct mp_entry *curr;
1086
 
1087
  /* Search */
1088
  for (curr = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1089
    {
1090
      if ((type == curr->type) && (addr == curr->addr))
1091
        {
1092
          return  curr;         /* The entry found */
1093
        }
1094
    }
1095
 
1096
  /* Not found */
1097
  return  NULL;
1098
 
1099
}       /* mp_hash_lookup () */
1100
 
1101
 
1102
/*---------------------------------------------------------------------------*/
1103
/*!Delete an entry from the matchpoint hash table
1104
 
1105
   If it is there the entry is deleted from the hash table. If it is not
1106
   there, no action is taken. The match must be on type AND addr.
1107
 
1108
   The usual fun and games tracking the previous entry, so we can delete
1109
   things.
1110
 
1111
   @note  The deletion DOES NOT free the memory associated with the entry,
1112
          since that is returned. The caller should free the memory when they
1113
          have used the information.
1114
 
1115
   @param[in] type   The type of matchpoint
1116
   @param[in] addr   The address of the matchpoint
1117
 
1118
   @return  The entry deleted, or NULL if the entry was not found            */
1119
/*---------------------------------------------------------------------------*/
1120
static struct mp_entry *
1121
mp_hash_delete (enum mp_type       type,
1122
                unsigned long int  addr)
1123
{
1124
  int              hv   = addr % MP_HASH_SIZE;
1125
  struct mp_entry *prev = NULL;
1126
  struct mp_entry *curr;
1127
 
1128
  /* Search */
1129
  for (curr  = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1130
    {
1131
      if ((type == curr->type) && (addr == curr->addr))
1132
        {
1133
          /* Found - delete. Method depends on whether we are the head of
1134
             chain. */
1135
          if (NULL == prev)
1136
            {
1137
              rsp.mp_hash[hv] = curr->next;
1138
            }
1139
          else
1140
            {
1141
              prev->next = curr->next;
1142
            }
1143
 
1144
          return  curr;         /* The entry deleted */
1145
        }
1146
 
1147
      prev = curr;
1148
    }
1149
 
1150
  /* Not found */
1151
  return  NULL;
1152
 
1153
}       /* mp_hash_delete () */
1154
 
1155
 
1156
/*---------------------------------------------------------------------------*/
1157
/*!Utility to give the value of a hex char
1158
 
1159
   @param[in] ch  A character representing a hexadecimal digit. Done as -1,
1160
                  for consistency with other character routines, which can use
1161
                  -1 as EOF.
1162
 
1163
   @return  The value of the hex character, or -1 if the character is
1164
            invalid.                                                         */
1165
/*---------------------------------------------------------------------------*/
1166
static int
1167
hex (int  c)
1168
{
1169
  return  ((c >= 'a') && (c <= 'f')) ? c - 'a' + 10 :
1170
          ((c >= '0') && (c <= '9')) ? c - '0' :
1171
          ((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : -1;
1172
 
1173
}       /* hex () */
1174
 
1175
 
1176
/*---------------------------------------------------------------------------*/
1177
/*!Convert a register to a hex digit string
1178
 
1179
   The supplied 32-bit value is converted to an 8 digit hex string according
1180
   the target endianism. It is null terminated for convenient printing.
1181
 
1182
   @param[in]  val  The value to convert
1183
   @param[out] buf  The buffer for the text string                           */
1184
/*---------------------------------------------------------------------------*/
1185
static void
1186
reg2hex (unsigned long int  val,
1187
         char              *buf)
1188
{
1189
  int  n;                       /* Counter for digits */
1190
 
1191
  for (n = 0; n < 8; n++)
1192
    {
1193
#ifdef WORDSBIGENDIAN
1194
      int  nyb_shift = n * 4;
1195
#else
1196
      int  nyb_shift = 28 - (n * 4);
1197
#endif
1198
      buf[n] = hexchars[(val >> nyb_shift) & 0xf];
1199
    }
1200
 
1201
  buf[8] = 0;                    /* Useful to terminate as string */
1202
 
1203
}       /* reg2hex () */
1204
 
1205
 
1206
/*---------------------------------------------------------------------------*/
1207
/*!Convert a hex digit string to a register value
1208
 
1209
   The supplied 8 digit hex string is converted to a 32-bit value according
1210
   the target endianism
1211
 
1212
   @param[in] buf  The buffer with the hex string
1213
 
1214
   @return  The value to convert                                             */
1215
/*---------------------------------------------------------------------------*/
1216
static unsigned long int
1217
hex2reg (char *buf)
1218
{
1219
  int                n;         /* Counter for digits */
1220
  unsigned long int  val = 0;    /* The result */
1221
 
1222
  for (n = 0; n < 8; n++)
1223
    {
1224
#ifdef WORDSBIGENDIAN
1225
      int  nyb_shift = n * 4;
1226
#else
1227
      int  nyb_shift = 28 - (n * 4);
1228
#endif
1229
      val |= hex (buf[n]) << nyb_shift;
1230
    }
1231
 
1232
  return val;
1233
 
1234
}       /* hex2reg () */
1235
 
1236
 
1237
/*---------------------------------------------------------------------------*/
1238
/*!Convert an ASCII character string to pairs of hex digits
1239
 
1240
   Both source and destination are null terminated.
1241
 
1242
   @param[out] dest  Buffer to store the hex digit pairs (null terminated)
1243
   @param[in]  src   The ASCII string (null terminated)                      */
1244
/*---------------------------------------------------------------------------*/
1245
static void  ascii2hex (char *dest,
1246
                        char *src)
1247
{
1248
  int  i;
1249
 
1250
  /* Step through converting the source string */
1251
  for (i = 0; src[i] != '\0'; i++)
1252
    {
1253
      char  ch = src[i];
1254
 
1255
      dest[i * 2]     = hexchars[ch >> 4 & 0xf];
1256
      dest[i * 2 + 1] = hexchars[ch      & 0xf];
1257
    }
1258
 
1259
  dest[i * 2] = '\0';
1260
 
1261
}       /* ascii2hex () */
1262
 
1263
 
1264
/*---------------------------------------------------------------------------*/
1265
/*!Convert pairs of hex digits to an ASCII character string
1266
 
1267
   Both source and destination are null terminated.
1268
 
1269
   @param[out] dest  The ASCII string (null terminated)
1270
   @param[in]  src   Buffer holding the hex digit pairs (null terminated)    */
1271
/*---------------------------------------------------------------------------*/
1272
static void  hex2ascii (char *dest,
1273
                        char *src)
1274
{
1275
  int  i;
1276
 
1277
  /* Step through convering the source hex digit pairs */
1278
  for (i = 0; src[i * 2] != '\0' && src[i * 2 + 1] != '\0'; i++)
1279
    {
1280
      dest[i] = ((hex (src[i * 2]) & 0xf) << 4) | (hex (src[i * 2 + 1]) & 0xf);
1281
    }
1282
 
1283
  dest[i] = '\0';
1284
 
1285
}       /* hex2ascii () */
1286
 
1287
 
1288
/*---------------------------------------------------------------------------*/
1289
/*!Set the program counter
1290
 
1291
   This sets the value in the NPC SPR. Not completely trivial, since this is
1292
   actually cached in cpu_state.pc. Any reset of the NPC also involves
1293
   clearing the delay state and setting the pcnext global.
1294
 
1295
   Only actually do this if the requested address is different to the current
1296
   NPC (avoids clearing the delay pipe).
1297
 
1298
   @param[in] addr  The address to use                                       */
1299
/*---------------------------------------------------------------------------*/
1300
static void
1301
set_npc (unsigned long int  addr)
1302
{
1303
  if (cpu_state.pc != addr)
1304
    {
1305
      cpu_state.pc         = addr;
1306
      cpu_state.delay_insn = 0;
1307
      pcnext               = addr + 4;
1308
    }
1309
}       /* set_npc () */
1310
 
1311
 
1312
/*---------------------------------------------------------------------------*/
1313
/*!Send a packet acknowledging an exception has occurred
1314
 
1315
   This is only called if there is a client FD to talk to                    */
1316
/*---------------------------------------------------------------------------*/
1317
static void
1318
rsp_report_exception ()
1319
{
1320
  struct rsp_buf  buf;
1321
 
1322
  /* Construct a signal received packet */
1323
  buf.data[0] = 'S';
1324
  buf.data[1] = hexchars[rsp.sigval >> 4];
1325
  buf.data[2] = hexchars[rsp.sigval % 16];
1326
  buf.data[3] = 0;
1327
  buf.len     = strlen (buf.data);
1328
 
1329
  put_packet (&buf);
1330
 
1331
}       /* rsp_report_exception () */
1332
 
1333
 
1334
/*---------------------------------------------------------------------------*/
1335
/*!Handle a RSP continue request
1336
 
1337
   Parse the command to see if there is an address. Uses the underlying
1338
   generic continue function, with EXCEPT_NONE.
1339
 
1340
   @param[in] buf  The full continue packet                                  */
1341
/*---------------------------------------------------------------------------*/
1342
static void
1343
rsp_continue (struct rsp_buf *buf)
1344
{
1345
  unsigned long int  addr;              /* Address to continue from, if any */
1346
 
1347
  if (0 == strcmp ("c", buf->data))
1348
    {
1349
      addr = cpu_state.pc;      /* Default uses current NPC */
1350
    }
1351
  else if (1 != sscanf (buf->data, "c%lx", &addr))
1352
    {
1353
      fprintf (stderr,
1354
               "Warning: RSP continue address %s not recognized: ignored\n",
1355
               buf->data);
1356
      addr = cpu_state.pc;      /* Default uses current NPC */
1357
    }
1358
 
1359
  rsp_continue_generic (addr, EXCEPT_NONE);
1360
 
1361
}       /* rsp_continue () */
1362
 
1363
 
1364
/*---------------------------------------------------------------------------*/
1365
/*!Handle a RSP continue with signal request
1366
 
1367
   Currently null. Will use the underlying generic continue function.
1368
 
1369
   @param[in] buf  The full continue with signal packet                      */
1370
/*---------------------------------------------------------------------------*/
1371
static void
1372
rsp_continue_with_signal (struct rsp_buf *buf)
1373
{
1374
  printf ("RSP continue with signal '%s' received\n", buf->data);
1375
 
1376
}       /* rsp_continue_with_signal () */
1377
 
1378
 
1379
/*---------------------------------------------------------------------------*/
1380
/*!Generic processing of a continue request
1381
 
1382
   The signal may be EXCEPT_NONE if there is no exception to be
1383
   handled. Currently the exception is ignored.
1384
 
1385
   The single step flag is cleared in the debug registers and then the
1386
   processor is unstalled.
1387
 
1388
   @param[in] addr    Address from which to step
1389
   @param[in] except  The exception to use (if any)                          */
1390
/*---------------------------------------------------------------------------*/
1391
static void
1392
rsp_continue_generic (unsigned long int  addr,
1393
                      unsigned long int  except)
1394
{
1395
  /* Set the address as the value of the next program counter */
1396
  set_npc (addr);
1397
 
1398
  /* Clear Debug Reason Register and watchpoint break generation in Debug Mode
1399
     Register 2 */
1400
  cpu_state.sprs[SPR_DRR]   = 0;
1401
  cpu_state.sprs[SPR_DMR2] &= ~SPR_DMR2_WGB;
1402
 
1403
  /* Clear the single step trigger in Debug Mode Register 1 and set traps to be
1404
     handled by the debug unit in the Debug Stop Register */
1405
  cpu_state.sprs[SPR_DMR1] &= ~SPR_DMR1_ST;
1406
  cpu_state.sprs[SPR_DSR]  |= SPR_DSR_TE;
1407
 
1408
  /* Unstall the processor */
1409
  set_stall_state (0);
1410
 
1411
  /* Any signal is cleared. */
1412
  rsp.sigval = TARGET_SIGNAL_NONE;
1413
 
1414
  /* Note the GDB client is now waiting for a reply. */
1415
  rsp.client_waiting = 1;
1416
 
1417
}       /* rsp_continue_generic () */
1418
 
1419
 
1420
/*---------------------------------------------------------------------------*/
1421
/*!Handle a RSP read all registers request
1422
 
1423
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PPC
1424
   (i.e. SPR PPC), NPC (i.e. SPR NPC) and SR (i.e. SPR SR). Each register is
1425
   returned as a sequence of bytes in target endian order.
1426
 
1427
   Each byte is packed as a pair of hex digits.                              */
1428
/*---------------------------------------------------------------------------*/
1429
static void
1430
rsp_read_all_regs ()
1431
{
1432
  struct rsp_buf  buf;                  /* Buffer for the reply */
1433
  int             r;                    /* Register index */
1434
 
1435
  /* The GPRs */
1436
  for (r = 0; r < MAX_GPRS; r++)
1437
    {
1438
      reg2hex (cpu_state.reg[r], &(buf.data[r * 8]));
1439
    }
1440
 
1441
  /* PPC, NPC and SR */
1442
  reg2hex (cpu_state.sprs[SPR_PPC], &(buf.data[PPC_REGNUM * 8]));
1443
  reg2hex (cpu_state.pc,            &(buf.data[NPC_REGNUM * 8]));
1444
  reg2hex (cpu_state.sprs[SPR_SR],  &(buf.data[SR_REGNUM  * 8]));
1445
 
1446
  /* Finalize the packet and send it */
1447
  buf.data[NUM_REGS * 8] = 0;
1448
  buf.len                = NUM_REGS * 8;
1449
 
1450
  put_packet (&buf);
1451
 
1452
}       /* rsp_read_all_regs () */
1453
 
1454
 
1455
/*---------------------------------------------------------------------------*/
1456
/*!Handle a RSP write all registers request
1457
 
1458
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PPC
1459
   (i.e. SPR PPC), NPC (i.e. SPR NPC) and SR (i.e. SPR SR). Each register is
1460
   supplied as a sequence of bytes in target endian order.
1461
 
1462
   Each byte is packed as a pair of hex digits.
1463
 
1464
   @todo There is no error checking at present. Non-hex chars will generate a
1465
         warning message, but there is no other check that the right amount
1466
         of data is present. The result is always "OK".
1467
 
1468
   @param[in] buf  The original packet request.                              */
1469
/*---------------------------------------------------------------------------*/
1470
static void
1471
rsp_write_all_regs (struct rsp_buf *buf)
1472
{
1473
  int             r;                    /* Register index */
1474
 
1475
  /* The GPRs */
1476
  for (r = 0; r < MAX_GPRS; r++)
1477
    {
1478
      cpu_state.reg[r] = hex2reg (&(buf->data[r * 8]));
1479
    }
1480
 
1481
  /* PPC, NPC and SR */
1482
  cpu_state.sprs[SPR_PPC] = hex2reg (&(buf->data[PPC_REGNUM * 8]));
1483
  cpu_state.sprs[SPR_SR]  = hex2reg (&(buf->data[SR_REGNUM  * 8]));
1484
  set_npc (hex2reg (&(buf->data[NPC_REGNUM * 8])));
1485
 
1486
  /* Acknowledge. TODO: We always succeed at present, even if the data was
1487
     defective. */
1488
  put_str_packet ("OK");
1489
 
1490
}       /* rsp_write_all_regs () */
1491
 
1492
 
1493
/*---------------------------------------------------------------------------*/
1494
/*!Handle a RSP read memory (symbolic) request
1495
 
1496
   Syntax is:
1497
 
1498
     m<addr>,<length>:
1499
 
1500
   The response is the bytes, lowest address first, encoded as pairs of hex
1501
   digits.
1502
 
1503
   The length given is the number of bytes to be read.
1504
 
1505
   @note This function reuses buf, so trashes the original command.
1506
 
1507
   @param[in] buf  The command received                                      */
1508
/*---------------------------------------------------------------------------*/
1509
static void
1510
rsp_read_mem (struct rsp_buf *buf)
1511
{
1512
  unsigned int    addr;                 /* Where to read the memory */
1513
  int             len;                  /* Number of bytes to read */
1514
  int             off;                  /* Offset into the memory */
1515
 
1516
  if (2 != sscanf (buf->data, "m%x,%x:", &addr, &len))
1517
    {
1518
      fprintf (stderr, "Warning: Failed to recognize RSP read memory "
1519
               "command: %s\n", buf->data);
1520
      put_str_packet ("E01");
1521
      return;
1522
    }
1523
 
1524
  /* Make sure we won't overflow the buffer (2 chars per byte) */
1525
  if ((len * 2) >= GDB_BUF_MAX)
1526
    {
1527
      fprintf (stderr, "Warning: Memory read %s too large for RSP packet: "
1528
               "truncated\n", buf->data);
1529
      len = (GDB_BUF_MAX - 1) / 2;
1530
    }
1531
 
1532
  /* Refill the buffer with the reply */
1533
  for (off = 0; off < len; off++)
1534
    {
1535
      unsigned char  ch;                /* The byte at the address */
1536
 
1537
      /* Check memory area is valid */
1538
      if (NULL == verify_memoryarea (addr + off))
1539
        {
1540
          /* The error number doesn't matter. The GDB client will substitute
1541
             its own */
1542
          put_str_packet ("E01");
1543
          return;
1544
        }
1545
 
1546
      // Get the memory direct - no translation.
1547
      ch = eval_direct8 (addr + off, 0, 0);
1548
 
1549
      buf->data[off * 2]     = hexchars[ch >>   4];
1550
      buf->data[off * 2 + 1] = hexchars[ch &  0xf];
1551
    }
1552
 
1553
  buf->data[off * 2] = 0;                        /* End of string */
1554
  buf->len           = strlen (buf->data);
1555
  put_packet (buf);
1556
 
1557
}       /* rsp_read_mem () */
1558
 
1559
 
1560
/*---------------------------------------------------------------------------*/
1561
/*!Handle a RSP write memory (symbolic) request
1562
 
1563
   Syntax is:
1564
 
1565
     m<addr>,<length>:<data>
1566
 
1567
   The data is the bytes, lowest address first, encoded as pairs of hex
1568
   digits.
1569
 
1570
   The length given is the number of bytes to be written.
1571
 
1572
   @note This function reuses buf, so trashes the original command.
1573
 
1574
   @param[in] buf  The command received                                      */
1575
/*---------------------------------------------------------------------------*/
1576
static void
1577
rsp_write_mem (struct rsp_buf *buf)
1578
{
1579
  unsigned int    addr;                 /* Where to write the memory */
1580
  int             len;                  /* Number of bytes to write */
1581
  char           *symdat;               /* Pointer to the symboli data */
1582
  int             datlen;               /* Number of digits in symbolic data */
1583
  int             off;                  /* Offset into the memory */
1584
 
1585
  if (2 != sscanf (buf->data, "M%x,%x:", &addr, &len))
1586
    {
1587
      fprintf (stderr, "Warning: Failed to recognize RSP write memory "
1588
               "command: %s\n", buf->data);
1589
      put_str_packet ("E01");
1590
      return;
1591
    }
1592
 
1593
  /* Find the start of the data and check there is the amount we expect. */
1594
  symdat = memchr ((const void *)buf->data, ':', GDB_BUF_MAX) + 1;
1595
  datlen = buf->len - (symdat - buf->data);
1596
 
1597
  /* Sanity check */
1598
  if (len * 2 != datlen)
1599
    {
1600
      fprintf (stderr, "Warning: Write of %d digits requested, but %d digits "
1601
               "supplied: packet ignored\n", len * 2, datlen );
1602
      put_str_packet ("E01");
1603
      return;
1604
    }
1605
 
1606
  /* Write the bytes to memory */
1607
  for (off = 0; off < len; off++)
1608
    {
1609
      if (NULL == verify_memoryarea (addr + off))
1610
        {
1611
          /* The error number doesn't matter. The GDB client will substitute
1612
             its own */
1613
          put_str_packet ("E01");
1614
          return;
1615
        }
1616
      else
1617
        {
1618
          unsigned char  nyb1 = hex (symdat[off * 2]);
1619
          unsigned char  nyb2 = hex (symdat[off * 2 + 1]);
1620
 
1621 143 jeremybenn
          /* circumvent the read-only check usually done for mem accesses
1622
             data is in host order, because that's what set_direct32 needs
1623
 
1624
             We make sure both data and instruction cache are invalidated
1625
             first, so that the write goes through the cache. */
1626
          dc_inv (addr + off);
1627
          ic_inv (addr + off);
1628 19 jeremybenn
          set_program8 (addr + off, (nyb1 << 4) | nyb2);
1629
        }
1630
    }
1631
 
1632
  put_str_packet ("OK");
1633
 
1634
}       /* rsp_write_mem () */
1635
 
1636
 
1637
/*---------------------------------------------------------------------------*/
1638
/*!Read a single register
1639
 
1640
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PC
1641
   (i.e. SPR NPC) and SR (i.e. SPR SR). The register is returned as a
1642
   sequence of bytes in target endian order.
1643
 
1644
   Each byte is packed as a pair of hex digits.
1645
 
1646
   @param[in] buf  The original packet request. Reused for the reply.        */
1647
/*---------------------------------------------------------------------------*/
1648
static void
1649
rsp_read_reg (struct rsp_buf *buf)
1650
{
1651
  unsigned int  regnum;
1652
 
1653
  /* Break out the fields from the data */
1654
  if (1 != sscanf (buf->data, "p%x", &regnum))
1655
    {
1656
      fprintf (stderr, "Warning: Failed to recognize RSP read register "
1657
               "command: %s\n", buf->data);
1658
      put_str_packet ("E01");
1659
      return;
1660
    }
1661
 
1662
  /* Get the relevant register */
1663
  if (regnum < MAX_GPRS)
1664
    {
1665
      reg2hex (cpu_state.reg[regnum], buf->data);
1666
    }
1667
  else if (PPC_REGNUM == regnum)
1668
    {
1669
      reg2hex (cpu_state.sprs[SPR_PPC], buf->data);
1670
    }
1671
  else if (NPC_REGNUM == regnum)
1672
    {
1673
      reg2hex (cpu_state.pc, buf->data);
1674
    }
1675
  else if (SR_REGNUM == regnum)
1676
    {
1677
      reg2hex (cpu_state.sprs[SPR_SR], buf->data);
1678
    }
1679
  else
1680
    {
1681
      /* Error response if we don't know the register */
1682
      fprintf (stderr, "Warning: Attempt to read unknown register 0x%x: "
1683
               "ignored\n", regnum);
1684
      put_str_packet ("E01");
1685
      return;
1686
    }
1687
 
1688
  buf->len = strlen (buf->data);
1689
  put_packet (buf);
1690
 
1691
}       /* rsp_write_reg () */
1692
 
1693
 
1694
/*---------------------------------------------------------------------------*/
1695
/*!Write a single register
1696
 
1697
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PC
1698
   (i.e. SPR NPC) and SR (i.e. SPR SR). The register is specified as a
1699
   sequence of bytes in target endian order.
1700
 
1701
   Each byte is packed as a pair of hex digits.
1702
 
1703
   @param[in] buf  The original packet request.                              */
1704
/*---------------------------------------------------------------------------*/
1705
static void
1706
rsp_write_reg (struct rsp_buf *buf)
1707
{
1708
  unsigned int  regnum;
1709
  char          valstr[9];              /* Allow for EOS on the string */
1710
 
1711
  /* Break out the fields from the data */
1712
  if (2 != sscanf (buf->data, "P%x=%8s", &regnum, valstr))
1713
    {
1714
      fprintf (stderr, "Warning: Failed to recognize RSP write register "
1715
               "command: %s\n", buf->data);
1716
      put_str_packet ("E01");
1717
      return;
1718
    }
1719
 
1720
  /* Set the relevant register */
1721
  if (regnum < MAX_GPRS)
1722
    {
1723
      cpu_state.reg[regnum] = hex2reg (valstr);
1724
    }
1725
  else if (PPC_REGNUM == regnum)
1726
    {
1727
      cpu_state.sprs[SPR_PPC] = hex2reg (valstr);
1728
    }
1729
  else if (NPC_REGNUM == regnum)
1730
    {
1731
      set_npc (hex2reg (valstr));
1732
    }
1733
  else if (SR_REGNUM == regnum)
1734
    {
1735
      cpu_state.sprs[SPR_SR] = hex2reg (valstr);
1736
    }
1737
  else
1738
    {
1739
      /* Error response if we don't know the register */
1740
      fprintf (stderr, "Warning: Attempt to write unknown register 0x%x: "
1741
               "ignored\n", regnum);
1742
      put_str_packet ("E01");
1743
      return;
1744
    }
1745
 
1746
  put_str_packet ("OK");
1747
 
1748
}       /* rsp_write_reg () */
1749
 
1750
 
1751
/*---------------------------------------------------------------------------*/
1752
/*!Handle a RSP query request
1753
 
1754
   @param[in] buf  The request. Reused for any packets that need to be sent
1755
                   back.                                                     */
1756
/*---------------------------------------------------------------------------*/
1757
static void
1758
rsp_query (struct rsp_buf *buf)
1759
{
1760
  if (0 == strcmp ("qC", buf->data))
1761
    {
1762
      /* Return the current thread ID (unsigned hex). A null response
1763
         indicates to use the previously selected thread. We use the constant
1764
         OR1KSIM_TID to represent our single thread of control. */
1765
      sprintf (buf->data, "QC%x", OR1KSIM_TID);
1766
      buf->len = strlen (buf->data);
1767
      put_packet (buf);
1768
    }
1769
  else if (0 == strncmp ("qCRC", buf->data, strlen ("qCRC")))
1770
    {
1771
      /* Return CRC of memory area */
1772
      fprintf (stderr, "Warning: RSP CRC query not supported\n");
1773
      put_str_packet ("E01");
1774
    }
1775
  else if (0 == strcmp ("qfThreadInfo", buf->data))
1776
    {
1777
      /* Return info about active threads. We return just the constant
1778
         OR1KSIM_TID to represent our single thread of control. */
1779
      sprintf (buf->data, "m%x", OR1KSIM_TID);
1780
      buf->len = strlen (buf->data);
1781
      put_packet (buf);
1782
    }
1783
  else if (0 == strcmp ("qsThreadInfo", buf->data))
1784
    {
1785
      /* Return info about more active threads. We have no more, so return the
1786
         end of list marker, 'l' */
1787
      put_str_packet ("l");
1788
    }
1789
  else if (0 == strncmp ("qGetTLSAddr:", buf->data, strlen ("qGetTLSAddr:")))
1790
    {
1791
      /* We don't support this feature */
1792
      put_str_packet ("");
1793
    }
1794
  else if (0 == strncmp ("qL", buf->data, strlen ("qL")))
1795
    {
1796
      /* Deprecated and replaced by 'qfThreadInfo' */
1797
      fprintf (stderr, "Warning: RSP qL deprecated: no info returned\n");
1798
      put_str_packet ("qM001");
1799
    }
1800
  else if (0 == strcmp ("qOffsets", buf->data))
1801
    {
1802
      /* Report any relocation */
1803
      put_str_packet ("Text=0;Data=0;Bss=0");
1804
    }
1805
  else if (0 == strncmp ("qP", buf->data, strlen ("qP")))
1806
    {
1807
      /* Deprecated and replaced by 'qThreadExtraInfo' */
1808
      fprintf (stderr, "Warning: RSP qP deprecated: no info returned\n");
1809
      put_str_packet ("");
1810
    }
1811
  else if (0 == strncmp ("qRcmd,", buf->data, strlen ("qRcmd,")))
1812
    {
1813
      /* This is used to interface to commands to do "stuff" */
1814
      rsp_command (buf);
1815
    }
1816
  else if (0 == strncmp ("qSupported", buf->data, strlen ("qSupported")))
1817
    {
1818
      /* Report a list of the features we support. For now we just ignore any
1819
         supplied specific feature queries, but in the future these may be
1820
         supported as well. Note that the packet size allows for 'G' + all the
1821
         registers sent to us, or a reply to 'g' with all the registers and an
1822
         EOS so the buffer is a well formed string. */
1823
 
1824
      char  reply[GDB_BUF_MAX];
1825
 
1826
      sprintf (reply, "PacketSize=%x", GDB_BUF_MAX);
1827
      put_str_packet (reply);
1828
    }
1829
  else if (0 == strncmp ("qSymbol:", buf->data, strlen ("qSymbol:")))
1830
    {
1831
      /* Offer to look up symbols. Nothing we want (for now). TODO. This just
1832
         ignores any replies to symbols we looked up, but we didn't want to
1833
         do that anyway! */
1834
      put_str_packet ("OK");
1835
    }
1836
  else if (0 == strncmp ("qThreadExtraInfo,", buf->data,
1837
                         strlen ("qThreadExtraInfo,")))
1838
    {
1839
      /* Report that we are runnable, but the text must be hex ASCI
1840
         digits. For now do this by steam, reusing the original packet */
1841
      sprintf (buf->data, "%02x%02x%02x%02x%02x%02x%02x%02x%02x",
1842
               'R', 'u', 'n', 'n', 'a', 'b', 'l', 'e', 0);
1843
      buf->len = strlen (buf->data);
1844
      put_packet (buf);
1845
    }
1846
  else if (0 == strncmp ("qXfer:", buf->data, strlen ("qXfer:")))
1847
    {
1848
      /* For now we support no 'qXfer' requests, but these should not be
1849
         expected, since they were not reported by 'qSupported' */
1850
      fprintf (stderr, "Warning: RSP 'qXfer' not supported: ignored\n");
1851
      put_str_packet ("");
1852
    }
1853
  else
1854
    {
1855
      fprintf (stderr, "Unrecognized RSP query: ignored\n");
1856
    }
1857
}       /* rsp_query () */
1858
 
1859
 
1860
/*---------------------------------------------------------------------------*/
1861
/*!Handle a RSP qRcmd request
1862
 
1863
  The actual command follows the "qRcmd," in ASCII encoded to hex
1864
 
1865
   @param[in] buf  The request in full                                       */
1866
/*---------------------------------------------------------------------------*/
1867
static void
1868
rsp_command (struct rsp_buf *buf)
1869
{
1870
  char  cmd[GDB_BUF_MAX];
1871
 
1872
  hex2ascii (cmd, &(buf->data[strlen ("qRcmd,")]));
1873
 
1874
  /* Work out which command it is */
1875
  if (0 == strncmp ("readspr ", cmd, strlen ("readspr")))
1876
    {
1877
      unsigned int       regno;
1878
 
1879
      /* Parse and return error if we fail */
1880
      if( 1 != sscanf (cmd, "readspr %4x", &regno))
1881
        {
1882
          fprintf (stderr, "Warning: qRcmd %s not recognized: ignored\n",
1883
                   cmd);
1884
          put_str_packet ("E01");
1885
          return;
1886
        }
1887
 
1888
      /* SPR out of range */
1889
      if (regno > MAX_SPRS)
1890
        {
1891
          fprintf (stderr, "Warning: qRcmd readspr %x too large: ignored\n",
1892
                   regno);
1893
          put_str_packet ("E01");
1894
          return;
1895
        }
1896
 
1897
      /* Construct the reply */
1898
      sprintf (cmd, "%8lx", (unsigned long int)mfspr (regno));
1899
      ascii2hex (buf->data, cmd);
1900
      buf->len = strlen (buf->data);
1901
      put_packet (buf);
1902
    }
1903
  else if (0 == strncmp ("writespr ", cmd, strlen ("writespr")))
1904
    {
1905
      unsigned int       regno;
1906
      unsigned long int  val;
1907
 
1908
      /* Parse and return error if we fail */
1909
      if( 2 != sscanf (cmd, "writespr %4x %8lx", &regno, &val))
1910
        {
1911
          fprintf (stderr, "Warning: qRcmd %s not recognized: ignored\n",
1912
                   cmd);
1913
          put_str_packet ("E01");
1914
          return;
1915
        }
1916
 
1917
      /* SPR out of range */
1918
      if (regno > MAX_SPRS)
1919
        {
1920
          fprintf (stderr, "Warning: qRcmd writespr %x too large: ignored\n",
1921
                   regno);
1922
          put_str_packet ("E01");
1923
          return;
1924
        }
1925
 
1926
      /* Update the SPR and reply "OK" */
1927
      mtspr (regno, val);
1928
      put_str_packet ("OK");
1929
    }
1930
 
1931
}       /* rsp_command () */
1932
 
1933
 
1934
/*---------------------------------------------------------------------------*/
1935
/*!Handle a RSP set request
1936
 
1937
   @param[in] buf  The request                                               */
1938
/*---------------------------------------------------------------------------*/
1939
static void
1940
rsp_set (struct rsp_buf *buf)
1941
{
1942
  if (0 == strncmp ("QPassSignals:", buf->data, strlen ("QPassSignals:")))
1943
    {
1944
      /* Passing signals not supported */
1945
      put_str_packet ("");
1946
    }
1947
  else if ((0 == strncmp ("QTDP",    buf->data, strlen ("QTDP")))   ||
1948
           (0 == strncmp ("QFrame",  buf->data, strlen ("QFrame"))) ||
1949
           (0 == strcmp  ("QTStart", buf->data))                    ||
1950
           (0 == strcmp  ("QTStop",  buf->data))                    ||
1951
           (0 == strcmp  ("QTinit",  buf->data))                    ||
1952
           (0 == strncmp ("QTro",    buf->data, strlen ("QTro"))))
1953
    {
1954
      /* All tracepoint features are not supported. This reply is really only
1955
         needed to 'QTDP', since with that the others should not be
1956
         generated. */
1957
      put_str_packet ("");
1958
    }
1959
  else
1960
    {
1961
      fprintf (stderr, "Unrecognized RSP set request: ignored\n");
1962
    }
1963
}       /* rsp_set () */
1964
 
1965
 
1966
/*---------------------------------------------------------------------------*/
1967
/*!Handle a RSP restart request
1968
 
1969
   For now we just put the program counter back to the one used with the last
1970
   vRun request. There is no point in unstalling the processor, since we'll
1971
   never get control back.                                                   */
1972
/*---------------------------------------------------------------------------*/
1973
static void
1974
rsp_restart ()
1975
{
1976
  set_npc (rsp.start_addr);
1977
 
1978
}       /* rsp_restart () */
1979
 
1980
 
1981
/*---------------------------------------------------------------------------*/
1982
/*!Handle a RSP step request
1983
 
1984
   Parse the command to see if there is an address. Uses the underlying
1985
   generic step function, with EXCEPT_NONE.
1986
 
1987
   @param[in] buf  The full step packet                          */
1988
/*---------------------------------------------------------------------------*/
1989
static void
1990
rsp_step (struct rsp_buf *buf)
1991
{
1992
  unsigned long int  addr;              /* The address to step from, if any */
1993
 
1994
  if (0 == strcmp ("s", buf->data))
1995
    {
1996
      addr = cpu_state.pc;      /* Default uses current NPC */
1997
    }
1998
  else if (1 != sscanf (buf->data, "s%lx", &addr))
1999
    {
2000
      fprintf (stderr,
2001
               "Warning: RSP step address %s not recognized: ignored\n",
2002
               buf->data);
2003
      addr = cpu_state.pc;      /* Default uses current NPC */
2004
    }
2005
 
2006
  rsp_step_generic (addr, EXCEPT_NONE);
2007
 
2008
}       /* rsp_step () */
2009
 
2010
 
2011
/*---------------------------------------------------------------------------*/
2012
/*!Handle a RSP step with signal request
2013
 
2014
   Currently null. Will use the underlying generic step function.
2015
 
2016
   @param[in] buf  The full step with signal packet              */
2017
/*---------------------------------------------------------------------------*/
2018
static void
2019
rsp_step_with_signal (struct rsp_buf *buf)
2020
{
2021
  printf ("RSP step with signal '%s' received\n", buf->data);
2022
 
2023
}       /* rsp_step_with_signal () */
2024
 
2025
 
2026
/*---------------------------------------------------------------------------*/
2027
/*!Generic processing of a step request
2028
 
2029
   The signal may be EXCEPT_NONE if there is no exception to be
2030
   handled. Currently the exception is ignored.
2031
 
2032
   The single step flag is set in the debug registers and then the processor
2033
   is unstalled.
2034
 
2035
   @param[in] addr    Address from which to step
2036
   @param[in] except  The exception to use (if any)                          */
2037
/*---------------------------------------------------------------------------*/
2038
static void
2039
rsp_step_generic (unsigned long int  addr,
2040
                  unsigned long int  except)
2041
{
2042
  /* Set the address as the value of the next program counter */
2043
  set_npc (addr);
2044
 
2045
  /* Clear Debug Reason Register and watchpoint break generation in Debug Mode
2046
     Register 2 */
2047
  cpu_state.sprs[SPR_DRR]   = 0;
2048
  cpu_state.sprs[SPR_DMR2] &= ~SPR_DMR2_WGB;
2049
 
2050
  /* Set the single step trigger in Debug Mode Register 1 and set traps to be
2051
     handled by the debug unit in the Debug Stop Register */
2052
  cpu_state.sprs[SPR_DMR1] |= SPR_DMR1_ST;
2053
  cpu_state.sprs[SPR_DSR]  |= SPR_DSR_TE;
2054
 
2055
  /* Unstall the processor */
2056
  set_stall_state (0);
2057
 
2058
  /* Any signal is cleared. */
2059
  rsp.sigval = TARGET_SIGNAL_NONE;
2060
 
2061
  /* Note the GDB client is now waiting for a reply. */
2062
  rsp.client_waiting = 1;
2063
 
2064
}       /* rsp_step_generic () */
2065
 
2066
 
2067
/*---------------------------------------------------------------------------*/
2068
/*!Handle a RSP 'v' packet
2069
 
2070
   These are commands associated with executing the code on the target
2071
 
2072
   @param[in] buf  The request                                               */
2073
/*---------------------------------------------------------------------------*/
2074
static void
2075
rsp_vpkt (struct rsp_buf *buf)
2076
{
2077
  if (0 == strncmp ("vAttach;", buf->data, strlen ("vAttach;")))
2078
    {
2079
      /* Attaching is a null action, since we have no other process. We just
2080
         return a stop packet (using TRAP) to indicate we are stopped. */
2081
      put_str_packet ("S05");
2082
      return;
2083
    }
2084
  else if (0 == strcmp ("vCont?", buf->data))
2085
    {
2086
      /* For now we don't support this. */
2087
      put_str_packet ("");
2088
      return;
2089
    }
2090
  else if (0 == strncmp ("vCont", buf->data, strlen ("vCont")))
2091
    {
2092
      /* This shouldn't happen, because we've reported non-support via vCont?
2093
         above */
2094
      fprintf (stderr, "Warning: RSP vCont not supported: ignored\n" );
2095
      return;
2096
    }
2097
  else if (0 == strncmp ("vFile:", buf->data, strlen ("vFile:")))
2098
    {
2099
      /* For now we don't support this. */
2100
      fprintf (stderr, "Warning: RSP vFile not supported: ignored\n" );
2101
      put_str_packet ("");
2102
      return;
2103
    }
2104
  else if (0 == strncmp ("vFlashErase:", buf->data, strlen ("vFlashErase:")))
2105
    {
2106
      /* For now we don't support this. */
2107
      fprintf (stderr, "Warning: RSP vFlashErase not supported: ignored\n" );
2108
      put_str_packet ("E01");
2109
      return;
2110
    }
2111
  else if (0 == strncmp ("vFlashWrite:", buf->data, strlen ("vFlashWrite:")))
2112
    {
2113
      /* For now we don't support this. */
2114
      fprintf (stderr, "Warning: RSP vFlashWrite not supported: ignored\n" );
2115
      put_str_packet ("E01");
2116
      return;
2117
    }
2118
  else if (0 == strcmp ("vFlashDone", buf->data))
2119
    {
2120
      /* For now we don't support this. */
2121
      fprintf (stderr, "Warning: RSP vFlashDone not supported: ignored\n" );
2122
      put_str_packet ("E01");
2123
      return;
2124
    }
2125
  else if (0 == strncmp ("vRun;", buf->data, strlen ("vRun;")))
2126
    {
2127
      /* We shouldn't be given any args, but check for this */
2128
      if (buf->len > strlen ("vRun;"))
2129
        {
2130
          fprintf (stderr, "Warning: Unexpected arguments to RSP vRun "
2131
                   "command: ignored\n");
2132
        }
2133
 
2134
      /* Restart the current program. However unlike a "R" packet, "vRun"
2135
         should behave as though it has just stopped. We use signal
2136
         5 (TRAP). */
2137
      rsp_restart ();
2138
      put_str_packet ("S05");
2139
    }
2140
  else
2141
    {
2142
      fprintf (stderr, "Warning: Unknown RSP 'v' packet type %s: ignored\n",
2143
               buf->data);
2144
      put_str_packet ("E01");
2145
      return;
2146
    }
2147
}       /* rsp_vpkt () */
2148
 
2149
 
2150
/*---------------------------------------------------------------------------*/
2151
/*!Handle a RSP write memory (binary) request
2152
 
2153
   Syntax is:
2154
 
2155
     X<addr>,<length>:
2156
 
2157
   Followed by the specified number of bytes as raw binary. Response should be
2158
   "OK" if all copied OK, E<nn> if error <nn> has occurred.
2159
 
2160
   The length given is the number of bytes to be written. However the number
2161
   of data bytes may be greater, since '#', '$' and '}' are escaped by
2162
   preceding them by '}' and oring with 0x20.
2163
 
2164
   @param[in] buf  The command received                                      */
2165
/*---------------------------------------------------------------------------*/
2166
static void
2167
rsp_write_mem_bin (struct rsp_buf *buf)
2168
{
2169
  unsigned int  addr;                   /* Where to write the memory */
2170
  int           len;                    /* Number of bytes to write */
2171
  char         *bindat;                 /* Pointer to the binary data */
2172
  int           off;                    /* Offset to start of binary data */
2173
  int           newlen;                 /* Number of bytes in bin data */
2174
 
2175
  if (2 != sscanf (buf->data, "X%x,%x:", &addr, &len))
2176
    {
2177
      fprintf (stderr, "Warning: Failed to recognize RSP write memory "
2178
               "command: %s\n", buf->data);
2179
      put_str_packet ("E01");
2180
      return;
2181
    }
2182
 
2183
  /* Find the start of the data and "unescape" it */
2184
  bindat = memchr ((const void *)buf->data, ':', GDB_BUF_MAX) + 1;
2185
  off    = bindat - buf->data;
2186
  newlen = rsp_unescape (bindat, buf->len - off);
2187
 
2188
  /* Sanity check */
2189
  if (newlen != len)
2190
    {
2191
      int  minlen = len < newlen ? len : newlen;
2192
 
2193
      fprintf (stderr, "Warning: Write of %d bytes requested, but %d bytes "
2194
               "supplied. %d will be written\n", len, newlen, minlen);
2195
      len = minlen;
2196
    }
2197
 
2198
  /* Write the bytes to memory */
2199
  for (off = 0; off < len; off++)
2200
    {
2201
      if (NULL == verify_memoryarea (addr + off))
2202
        {
2203
          /* The error number doesn't matter. The GDB client will substitute
2204
             its own */
2205
          put_str_packet ("E01");
2206
          return;
2207
        }
2208
      else
2209
        {
2210 143 jeremybenn
          /* Circumvent the read-only check usually done for mem accesses
2211
 
2212
             We make sure both data and instruction cache are invalidated
2213
             first, so that the write goes through the cache. */
2214
          dc_inv (addr + off);
2215
          ic_inv (addr + off);
2216 19 jeremybenn
          set_program8 (addr + off, bindat[off]);
2217
        }
2218
    }
2219
 
2220
  put_str_packet ("OK");
2221
 
2222
}       /* rsp_write_mem_bin () */
2223
 
2224
 
2225
/*---------------------------------------------------------------------------*/
2226
/*!Handle a RSP remove breakpoint or matchpoint request
2227
 
2228
   For now only memory breakpoints are implemented, which are implemented by
2229
   substituting a breakpoint at the specified address. The implementation must
2230
   cope with the possibility of duplicate packets.
2231
 
2232
   @todo This doesn't work with icache/immu yet
2233
 
2234
   @param[in] buf  The command received                                      */
2235
/*---------------------------------------------------------------------------*/
2236
static void
2237
rsp_remove_matchpoint (struct rsp_buf *buf)
2238
{
2239
  enum mp_type       type;              /* What sort of matchpoint */
2240 143 jeremybenn
  int                type_for_scanf;    /* To avoid old GCC limitations */
2241 19 jeremybenn
  unsigned long int  addr;              /* Address specified */
2242
  int                len;               /* Matchpoint length (not used) */
2243
  struct mp_entry   *mpe;               /* Info about the replaced instr */
2244
 
2245 143 jeremybenn
  /* Break out the instruction. We have to use an intermediary for the type,
2246
     since older GCCs do not like taking the address of an enum
2247
     (dereferencing type-punned pointer). */
2248
  if (3 != sscanf (buf->data, "z%1d,%lx,%1d", &type_for_scanf, &addr, &len))
2249 19 jeremybenn
    {
2250
      fprintf (stderr, "Warning: RSP matchpoint deletion request not "
2251
               "recognized: ignored\n");
2252
      put_str_packet ("E01");
2253
      return;
2254
    }
2255
 
2256 143 jeremybenn
  type = type_for_scanf;
2257
 
2258 19 jeremybenn
  /* Sanity check that the length is 4 */
2259
  if (4 != len)
2260
    {
2261
      fprintf (stderr, "Warning: RSP matchpoint deletion length %d not "
2262
               "valid: 4 assumed\n", len);
2263
      len = 4;
2264
    }
2265
 
2266
  /* Sort out the type of matchpoint */
2267
  switch (type)
2268
    {
2269
    case BP_MEMORY:
2270
      /* Memory breakpoint - replace the original instruction. */
2271
      mpe = mp_hash_delete (type, addr);
2272
 
2273
      /* If the BP hasn't yet been deleted, put the original instruction
2274 143 jeremybenn
         back. Don't forget to free the hash table entry afterwards.
2275
 
2276
         We make sure both the instruction cache is invalidated first, so that
2277
         the write goes through the cache. */
2278 19 jeremybenn
      if (NULL != mpe)
2279
        {
2280 143 jeremybenn
          ic_inv (addr);
2281 19 jeremybenn
          set_program32 (addr, mpe->instr);
2282
          free (mpe);
2283
        }
2284
 
2285
      put_str_packet ("OK");
2286
 
2287
      return;
2288
 
2289
    case BP_HARDWARE:
2290
      put_str_packet ("");              /* Not supported */
2291
      return;
2292
 
2293
    case WP_WRITE:
2294
      put_str_packet ("");              /* Not supported */
2295
      return;
2296
 
2297
    case WP_READ:
2298
      put_str_packet ("");              /* Not supported */
2299
      return;
2300
 
2301
    case WP_ACCESS:
2302
      put_str_packet ("");              /* Not supported */
2303
      return;
2304
 
2305
    default:
2306
      fprintf (stderr, "Warning: RSP matchpoint type %d not "
2307
               "recognized: ignored\n", type);
2308
      put_str_packet ("E01");
2309
      return;
2310
 
2311
    }
2312
}       /* rsp_remove_matchpoint () */
2313
 
2314
 
2315
/*---------------------------------------------------------------------------*/
2316
/*!Handle a RSP insert breakpoint or matchpoint request
2317
 
2318
   For now only memory breakpoints are implemented, which are implemented by
2319
   substituting a breakpoint at the specified address. The implementation must
2320
   cope with the possibility of duplicate packets.
2321
 
2322
   @todo This doesn't work with icache/immu yet
2323
 
2324
   @param[in] buf  The command received                                      */
2325
/*---------------------------------------------------------------------------*/
2326
static void
2327
rsp_insert_matchpoint (struct rsp_buf *buf)
2328
{
2329
  enum mp_type       type;              /* What sort of matchpoint */
2330 143 jeremybenn
  int                type_for_scanf;    /* To avoid old GCC limitations */
2331 19 jeremybenn
  unsigned long int  addr;              /* Address specified */
2332
  int                len;               /* Matchpoint length (not used) */
2333
 
2334 143 jeremybenn
  /* Break out the instruction. We have to use an intermediary for the type,
2335
     since older GCCs do not like taking the address of an enum
2336
     (dereferencing type-punned pointer). */
2337
  if (3 != sscanf (buf->data, "Z%1d,%lx,%1d", &type_for_scanf, &addr, &len))
2338 19 jeremybenn
    {
2339
      fprintf (stderr, "Warning: RSP matchpoint insertion request not "
2340
               "recognized: ignored\n");
2341
      put_str_packet ("E01");
2342
      return;
2343
    }
2344
 
2345 143 jeremybenn
  type = type_for_scanf;
2346
 
2347 19 jeremybenn
  /* Sanity check that the length is 4 */
2348
  if (4 != len)
2349
    {
2350
      fprintf (stderr, "Warning: RSP matchpoint insertion length %d not "
2351
               "valid: 4 assumed\n", len);
2352
      len = 4;
2353
    }
2354
 
2355
  /* Sort out the type of matchpoint */
2356
  switch (type)
2357
    {
2358
    case BP_MEMORY:
2359 143 jeremybenn
      /* Memory breakpoint - substitute a TRAP instruction
2360
 
2361
         We make sure th instruction cache is invalidated first, so that the
2362
         read and write always work correctly. */
2363 19 jeremybenn
      mp_hash_add (type, addr, eval_direct32 (addr, 0, 0));
2364 143 jeremybenn
      ic_inv (addr);
2365 19 jeremybenn
      set_program32 (addr, OR1K_TRAP_INSTR);
2366
      put_str_packet ("OK");
2367
 
2368
      return;
2369
 
2370
    case BP_HARDWARE:
2371
      put_str_packet ("");              /* Not supported */
2372
      return;
2373
 
2374
    case WP_WRITE:
2375
      put_str_packet ("");              /* Not supported */
2376
      return;
2377
 
2378
    case WP_READ:
2379
      put_str_packet ("");              /* Not supported */
2380
      return;
2381
 
2382
    case WP_ACCESS:
2383
      put_str_packet ("");              /* Not supported */
2384
      return;
2385
 
2386
    default:
2387
      fprintf (stderr, "Warning: RSP matchpoint type %d not "
2388
               "recognized: ignored\n", type);
2389
      put_str_packet ("E01");
2390
      return;
2391
 
2392
    }
2393
 
2394
}       /* rsp_insert_matchpoint () */
2395
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.