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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or_debug_proxy/] [src/] [gdb.c] - Blame information for rev 46

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

Line No. Rev Author Line
1 39 julius
/*$$HEADER*/
2
/******************************************************************************/
3
/*                                                                            */
4
/*                    H E A D E R   I N F O R M A T I O N                     */
5
/*                                                                            */
6
/******************************************************************************/
7
 
8
// Project Name                   : OpenRISC Debug Proxy
9
// File Name                      : gdb.c
10
// Prepared By                    : jb, rmd
11
// Project Start                  : 2008-10-01
12
 
13
/*$$COPYRIGHT NOTICE*/
14
/******************************************************************************/
15
/*                                                                            */
16
/*                      C O P Y R I G H T   N O T I C E                       */
17
/*                                                                            */
18
/******************************************************************************/
19
/*
20
  This library is free software; you can redistribute it and/or
21
  modify it under the terms of the GNU Lesser General Public
22
  License as published by the Free Software Foundation;
23
  version 2.1 of the License, a copy of which is available from
24
  http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt.
25
 
26
  This library is distributed in the hope that it will be useful,
27
  but WITHOUT ANY WARRANTY; without even the implied warranty of
28
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
29
  Lesser General Public License for more details.
30
 
31
  You should have received a copy of the GNU Lesser General Public
32
  License along with this library; if not, write to the Free Software
33
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
34
*/
35
 
36
/*$$DESCRIPTION*/
37
/******************************************************************************/
38
/*                                                                            */
39
/*                           D E S C R I P T I O N                            */
40
/*                                                                            */
41
/******************************************************************************/
42
//
43
// Implements GDB stub for OpenRISC debug proxy.
44
//
45
 
46
 
47
/*$$CHANGE HISTORY*/
48
/******************************************************************************/
49
/*                                                                            */
50
/*                         C H A N G E  H I S T O R Y                         */
51
/*                                                                            */
52
/******************************************************************************/
53
 
54
// Date         Version Description
55
//------------------------------------------------------------------------
56
// 081101               Imported code from "jp" project                 jb
57
// 090219               Adapted code from Jeremy Bennett's RSP server
58
//                      for the or1ksim project.                       rmb
59
// 090304               Finished RSP server code import, added extra
60
//                      functions, adding stability when debugging on
61
//                      a remote target.                                jb
62
// 090505               Remove PPC polling - caused intermittent errors 
63
//                      in or1k                                         jb
64 46 julius
// 090828               Fixed byte/non-aligned accesses. Removed legacy
65
//                      "remote JTAG" protocol.                         jb
66 39 julius
#ifdef CYGWIN_COMPILE
67
 
68
#else
69
// linux includes                  
70
#include <time.h>
71
#include <sched.h>
72
#endif
73
 
74
#include <stdio.h>
75
#include <ctype.h>
76
#include <string.h>
77
#include <stdlib.h>
78
#include <unistd.h>
79
#include <stdarg.h>
80
 
81
/* Libraries for JTAG proxy server.  */
82
#include <sys/stat.h>
83
#include <sys/types.h>
84
#include <sys/socket.h>
85
#include <netinet/in.h>
86
#include <sys/ioctl.h>
87
#include <sys/select.h>
88
#include <sys/poll.h>
89
#include <fcntl.h>
90
#include <netdb.h>
91
#include <netinet/tcp.h>
92
#include <signal.h>
93
#include <inttypes.h>
94
#include <errno.h>
95
#include <arpa/inet.h>
96
 
97
 
98
/*! Name of the Or1ksim RSP service */
99
#define OR1KSIM_RSP_SERVICE  "or1ksim-rsp"
100
 
101
#include "gdb.h" /* partially copied from gdb/config/or1k */
102
#include "or_debug_proxy.h"
103
 
104
#define MAX_GPRS    (32)
105
 
106
/* Indices of GDB registers that are not GPRs. Must match GDB settings! */
107
#define PPC_REGNUM  (MAX_GPRS + 0)      /*!< Previous PC */
108
#define NPC_REGNUM  (MAX_GPRS + 1)      /*!< Next PC */
109
#define SR_REGNUM   (MAX_GPRS + 2)      /*!< Supervision Register */
110
#define NUM_REGS    (MAX_GPRS + 3)      /*!< Total GDB registers */
111
 
112
/* OR1k CPU registers address */
113
#define NPC_CPU_REG_ADD         0x10                                                    /* Next PC */
114
#define SR_CPU_REG_ADD          0x11                                                    /* Supervision Register */
115
#define PPC_CPU_REG_ADD         0x12                                                    /* Previous PC */
116
#define DMR1_CPU_REG_ADD        ((6 << 11) + 16)        /* Debug Mode Register 1 (DMR1) 0x3010 */
117
#define DMR2_CPU_REG_ADD        ((6 << 11) + 17)        /* Debug Mode Register 2 (DMR2) 0x3011 */
118
#define DSR_CPU_REG_ADD         ((6 << 11) + 20)        /* Debug Stop Register (DSR) 0x3014 */
119
#define DRR_CPU_REG_ADD         ((6 << 11) + 21)        /* Debug Reason Register (DRR) 0x3015 */
120
 
121
/*! Trap instruction for OR32 */
122
#define OR1K_TRAP_INSTR  0x21000001
123
 
124
/*! The maximum number of characters in inbound/outbound buffers.  The largest
125
    packets are the 'G' packet, which must hold the 'G' and all the registers
126
    with two hex digits per byte and the 'g' reply, which must hold all the
127
    registers, and (in our implementation) an end-of-string (0)
128
    character. Adding the EOS allows us to print out the packet as a
129
    string. So at least NUMREGBYTES*2 + 1 (for the 'G' or the EOS) are needed
130
    for register packets */
131
#define GDB_BUF_MAX  ((NUM_REGS) * 8 + 1)
132
 
133
/*! Size of the matchpoint hash table. Largest prime < 2^10 */
134
#define MP_HASH_SIZE  1021
135
 
136
/* Definition of special-purpose registers (SPRs). */
137
#define MAX_SPRS (0x10000)
138
 
139
#define SPR_DMR1_ST                     0x00400000  /* Single-step trace*/
140
#define SPR_DMR2_WGB            0x003ff000  /* Watchpoints generating breakpoint */
141
#define SPR_DSR_TE                              0x00002000  /* Trap exception */
142
 
143
#define WORDSBIGENDIAN_N
144
 
145
/* Definition of OR1K exceptions */
146
#define EXCEPT_NONE     0x0000
147
#define EXCEPT_RESET    0x0100
148
#define EXCEPT_BUSERR   0x0200
149
#define EXCEPT_DPF      0x0300
150
#define EXCEPT_IPF      0x0400
151
#define EXCEPT_TICK     0x0500
152
#define EXCEPT_ALIGN    0x0600
153
#define EXCEPT_ILLEGAL  0x0700
154
#define EXCEPT_INT      0x0800
155
#define EXCEPT_DTLBMISS 0x0900
156
#define EXCEPT_ITLBMISS 0x0a00
157
#define EXCEPT_RANGE    0x0b00
158
#define EXCEPT_SYSCALL  0x0c00
159
#define EXCEPT_FPE      0x0d00
160
#define EXCEPT_TRAP     0x0e00
161
 
162
// Changed to #defines from static const int's due to compile error
163
// DRR (Debug Reason Register) Bits
164
#define SPR_DRR_RSTE  0x00000001  //!< Reset
165
#define SPR_DRR_BUSEE 0x00000002  //!< Bus error
166
#define SPR_DRR_DPFE  0x00000004  //!< Data page fault
167
#define SPR_DRR_IPFE  0x00000008  //!< Insn page fault
168
#define SPR_DRR_TTE   0x00000010  //!< Tick timer
169
#define SPR_DRR_AE    0x00000020  //!< Alignment
170
#define SPR_DRR_IIE   0x00000040  //!< Illegal instruction
171
#define SPR_DRR_IE    0x00000080  //!< Interrupt
172
#define SPR_DRR_DME   0x00000100  //!< DTLB miss
173
#define SPR_DRR_IME   0x00000200  //!< ITLB miss
174
#define SPR_DRR_RE    0x00000400  //!< Range fault
175
#define SPR_DRR_SCE   0x00000800  //!< System call
176
#define SPR_DRR_FPE   0x00001000  //!< Floating point
177
#define SPR_DRR_TE    0x00002000  //!< Trap
178
 
179
 
180
/*! Definition of GDB target signals. Data taken from the GDB 6.8
181
    source. Only those we use defined here. The exact meaning of
182
    signal number is defined by the header `include/gdb/signals.h'
183
    in the GDB source code. For an explanation of what each signal
184
    means, see target_signal_to_string.*/
185
enum target_signal {
186
  TARGET_SIGNAL_NONE =  0,
187
  TARGET_SIGNAL_INT  =  2,
188
  TARGET_SIGNAL_ILL  =  4,
189
  TARGET_SIGNAL_TRAP =  5,
190
  TARGET_SIGNAL_FPE  =  8,
191
  TARGET_SIGNAL_BUS  = 10,
192
  TARGET_SIGNAL_SEGV = 11,
193
  TARGET_SIGNAL_ALRM = 14,
194
  TARGET_SIGNAL_USR2 = 31,
195
  TARGET_SIGNAL_PWR  = 32
196
};
197
 
198
/*! String to map hex digits to chars */
199
static const char hexchars[]="0123456789abcdef";
200
 
201
 
202
//! Is the NPC cached?
203
 
204
//! Setting the NPC flushes the pipeline, so subsequent reads will return
205
//! zero until the processor has refilled the pipeline. This will not be
206
//! happening if the processor is stalled (as it is when GDB had control),
207
//! so we must cache the NPC. As soon as the processor is unstalled, this
208
//! cached value becomes invalid. So we must track the stall state, and if
209
//! appropriate cache the NPC.
210
enum stallStates {
211
  STALLED,
212
  UNSTALLED,
213
  UNKNOWN
214
} stallState;
215
 
216
int      npcIsCached;           //!< Is the NPC cached - should be bool
217
uint32_t  npcCachedValue;               //!< Cached value of the NPC
218
 
219 46 julius
/* OR32 Linux kernel debugging hacks */
220
/* If we're operating in virtual memory space, currently the standard VM base
221
   is 0xc0000000, since the GDB we're currently using is bare-metal we will
222
   do a translation of these VM addresses to their physical address - although
223
   not with any sophistication, we simply map all 0xc0000000 based addresses
224
   to 0x0. This allows GDB to read/wite the addresses the PC and NPC point to,
225
   access VM data areas, etc. */
226
/* Define OR32_KERNEL_DBUG_COMPAT=1 when compiling to enable this */
227
#define OR32_KERNEL_DBG_COMPAT 1
228
#define OR32_LINUX_VM_OFFSET 0xc0000000
229
#define OR32_LINUX_VM_MASK 0xf0000000
230 39 julius
 
231 46 julius
#define IS_VM_ADDR(adr) ((adr & OR32_LINUX_VM_MASK) == OR32_LINUX_VM_OFFSET)
232 39 julius
 
233
 
234 46 julius
 
235
 
236 39 julius
/************************
237
   JTAG Server Routines
238
************************/
239
int serverIP = 0;
240
int serverPort = 0;
241
int server_fd = 0;
242
int gdb_fd = 0;
243
 
244
static int tcp_level = 0;
245
 
246
/* global to store what chain the debug unit is currently connected to
247
(not the JTAG TAP, but the onchip debug module has selected) */
248
int gdb_chain = -1;
249
 
250
/*! Data structure for RSP buffers. Can't be null terminated, since it may
251
  include zero bytes */
252
struct rsp_buf
253
{
254
  char  data[GDB_BUF_MAX];
255
  int   len;
256
};
257
 
258
/*! Enumeration of different types of matchpoint. These have explicit values
259
    matching the second digit of 'z' and 'Z' packets. */
260
enum mp_type {
261
  BP_MEMORY   = 0,               // software-breakpoint Z0  break 
262
  BP_HARDWARE = 1,              // hardware-breakpoint Z1  hbreak 
263
  WP_WRITE    = 2,              // write-watchpoint    Z2  watch  
264
  WP_READ     = 3,              // read-watchpoint     Z3  rwatch  
265
  WP_ACCESS   = 4                       // access-watchpoint   Z4  awatch
266
};
267
 
268
/*! Data structure for a matchpoint hash table entry */
269
struct mp_entry
270
{
271
  enum mp_type       type;              /*!< Type of matchpoint */
272
  uint32_t  addr;               /*!< Address with the matchpoint */
273
  uint32_t  instr;              /*!< Substituted instruction */
274
  struct mp_entry   *next;              /*!< Next entry with this hash */
275
};
276
 
277
/*! Central data for the RSP connection */
278
static struct
279
{
280
  int                           client_waiting; /*!< Is client waiting a response? */
281
// Not used  int                proto_num;              /*!< Number of the protocol used */
282
  int                client_fd;         /*!< FD for talking to GDB */
283
  int               sigval;                     /*!< GDB signal for any exception */
284
  uint32_t start_addr;  /*!< Start of last run */
285
  struct mp_entry   *mp_hash[MP_HASH_SIZE];     /*!< Matchpoint hash table */
286
} rsp;
287
 
288
/* Forward declarations of static functions */
289
static char *printTime(void);
290
static int gdb_read(void*, int);
291
static int gdb_write(void*, int);
292
static void rsp_interrupt();
293
static char rsp_peek();
294
static struct rsp_buf *get_packet (void);
295
static void rsp_init (void);
296
static void set_npc (uint32_t  addr);
297
static uint32_t get_npc();
298
static void rsp_check_for_exception();
299
static int check_for_exception_vector(uint32_t ppc);
300
static void rsp_exception (uint32_t  except);
301
static int get_rsp_char (void);
302
static int hex (int  c);
303
static void rsp_get_client (void);
304
static void rsp_client_request (void);
305
static void rsp_client_close (void);
306
static void client_close (char err);
307 46 julius
static void put_str_packet (const char *str);
308 39 julius
static void rsp_report_exception (void);
309
static void put_packet (struct rsp_buf *p_buf);
310
static void send_rsp_str (unsigned char *data, int len);
311
static void rsp_query (struct rsp_buf *p_buf);
312
static void rsp_vpkt (struct rsp_buf *p_buf);
313 46 julius
static void rsp_step (struct rsp_buf *p_buf);
314
static void rsp_step_with_signal (struct rsp_buf *p_buf);
315
static void rsp_step_generic (uint32_t  addr, uint32_t  except);
316 39 julius
static void rsp_continue (struct rsp_buf *p_buf);
317 46 julius
static void rsp_continue_with_signal (struct rsp_buf *p_buf);
318
static void rsp_continue_generic (uint32_t  addr, uint32_t  except);
319 39 julius
static void rsp_read_all_regs (void);
320
static void rsp_write_all_regs (struct rsp_buf *p_buf);
321
static void rsp_read_mem (struct rsp_buf *p_buf);
322
static void rsp_write_mem (struct rsp_buf *p_buf);
323
static void rsp_write_mem_bin (struct rsp_buf *p_buf);
324
static int rsp_unescape (char *data, int len);
325
static void rsp_read_reg (struct rsp_buf *p_buf);
326
static void rsp_write_reg (struct rsp_buf *p_buf);
327
static void mp_hash_init (void);
328 46 julius
static void mp_hash_add (enum mp_type type, uint32_t  addr, uint32_t  instr);
329 39 julius
static struct mp_entry * mp_hash_lookup (enum mp_type type, uint32_t  addr);
330
static struct mp_entry * mp_hash_delete (enum mp_type type,     uint32_t  addr);
331
static void rsp_remove_matchpoint (struct rsp_buf *p_buf);
332
static void rsp_insert_matchpoint (struct rsp_buf *p_buf);
333
static void rsp_command (struct rsp_buf *p_buf);
334
static void rsp_set (struct rsp_buf *p_buf);
335
static void rsp_restart (void);
336
static void  ascii2hex (char *dest,char *src);
337
static void  hex2ascii (char *dest,     char *src);
338
static uint32_t hex2reg (char *p_buf);
339
static void     reg2hex (uint32_t  val, char *p_buf);
340
static void swap_buf(char* p_buf, int len);
341
static void set_stall_state (int state);
342
static void reset_or1k (void);
343
static void gdb_ensure_or1k_stalled();
344
static int gdb_set_chain(int chain);
345 46 julius
static int gdb_write_byte(uint32_t adr, uint8_t data);
346 39 julius
static int gdb_write_reg(uint32_t adr, uint32_t data);
347
static int gdb_read_reg(uint32_t adr, uint32_t *data);
348
static int gdb_write_block(uint32_t adr, uint32_t *data, int len);
349
static int gdb_read_block(uint32_t adr, uint32_t *data, int len);
350
 
351
char *printTime(void)
352
{
353
  time_t tid;
354
  struct tm *strtm;
355
  static char timeBuf[20];
356
 
357
  time(&tid);
358
  strtm = localtime(&tid);
359
  sprintf(timeBuf,"[%.02d:%.02d:%.02d] ",strtm->tm_hour,strtm->tm_min,strtm->tm_sec);
360
  return timeBuf;
361
}
362
 
363
/*---------------------------------------------------------------------------*/
364
/*!Initialize the Remote Serial Protocol connection
365
 
366
   Set up the central data structures.                                       */
367
/*---------------------------------------------------------------------------*/
368
void
369
rsp_init (void)
370
{
371
  /* Clear out the central data structure */
372
  rsp.client_waiting =  0;               /* GDB client is not waiting for us */
373
  rsp.client_fd      = -1;              /* i.e. invalid */
374
  rsp.sigval         = 0;                /* No exception */
375
  rsp.start_addr     = EXCEPT_RESET;    /* Default restart point */
376
 
377
  /* Set up the matchpoint hash table */
378
  mp_hash_init ();
379
 
380
  /* RSP always starts stalled as though we have just reset the processor. */
381
  rsp_exception (EXCEPT_TRAP);
382
 
383
  /* Setup the NPC caching variables */
384
  stallState = STALLED;
385
  // Force a caching of the NPC
386
  npcIsCached = 0;
387
  get_npc();
388
 
389
}       /* rsp_init () */
390
 
391
/*---------------------------------------------------------------------------*/
392
/*!Look for action on RSP
393
 
394
   This function is called when the processor has stalled, which, except for
395
   initialization, must be due to an interrupt.
396
 
397
   If we have no RSP client, we get one. We can make no progress until the
398
   client is available.
399
 
400
   Then if the cause is an exception following a step or continue command, and
401
   the exception not been notified to GDB, a packet reporting the cause of the
402
   exception is sent.
403
 
404
   The next client request is then processed.                                */
405
/*---------------------------------------------------------------------------*/
406
void
407
handle_rsp (void)
408
{
409
  uint32_t              temp_uint32;
410
 
411
  rsp_init();
412
 
413
  while (1){
414
    /* If we have no RSP client, wait until we get one. */
415
    while (-1 == rsp.client_fd)
416
      {
417
        rsp_get_client ();
418
        rsp.client_waiting = 0;          /* No longer waiting */
419
      }
420
 
421
    /* If we have an unacknowledged exception tell the GDB client. If this
422
       exception was a trap due to a memory breakpoint, then adjust the NPC. */
423
    if (rsp.client_waiting)
424
      {
425
 
426
        // Check for exception
427
        rsp_check_for_exception();
428
 
429
        if(stallState == STALLED)
430
          // Get the PPC if we're stalled
431
          gdb_read_reg(PPC_CPU_REG_ADD, &temp_uint32);
432
 
433
 
434
        if ((TARGET_SIGNAL_TRAP == rsp.sigval) && (NULL != mp_hash_lookup (BP_MEMORY, temp_uint32)))
435
          {
436
            if (stallState != STALLED)
437
              // This is a quick fix for a strange situation seen in some of the simulators where
438
              // the sw bp would be detected, but the stalled state variable wasn't updated correctly
439
              // indicating that last time it checked, it wasn't set but the processor has now hit the
440
              // breakpoint. So run rsp_check_for_exception() to bring everything up to date.
441
              rsp_check_for_exception();
442
 
443
            if(DEBUG_GDB) printf("Software breakpoint hit at 0x%08x. Rolling back NPC to this instruction\n", temp_uint32);
444
 
445
            set_npc (temp_uint32);
446
 
447
            rsp_report_exception();
448
            rsp.client_waiting = 0;              /* No longer waiting */
449
          }
450
        else if(stallState == STALLED) {
451
          // If we're here, the thing has stalled, but not because of a breakpoint we set
452
          // report back the exception
453
 
454
          rsp_report_exception();
455
          rsp.client_waiting = 0;                /* No longer waiting */
456
 
457
        }
458
 
459
        if (rsp.client_waiting)
460
#ifdef CYGWIN_COMPILE
461
          sleep(1);
462
#else
463 46 julius
        usleep(10000);
464 39 julius
        sched_yield();
465
#endif
466
 
467
 
468
      }
469
 
470
    // See if there's any incoming data from the client by peeking at the socket
471
    if (rsp_peek() > 0)
472
      {
473
        if (rsp_peek() == 0x03 && (stallState != STALLED)) // ETX, end of text control char
474
          {
475
            // Got an interrupt command from GDB, this function should
476
            // pull the packet off the socket and stall the processor.
477
            // and then send a stop reply packet with signal TARGET_SIGNAL_NONE
478
            rsp_interrupt();
479
            rsp.client_waiting = 0;
480
          }
481
        else if (rsp.client_waiting == 0)
482
          {
483
            // Default handling of data from the client:
484
            /* Get a RSP client request */
485
            rsp_client_request ();
486
          }
487
      } /* end if (rsp_peek() > 0) */
488
    else
489
#ifdef CYGWIN_COMPILE
490
      sleep(1);
491
#else
492
    {
493 46 julius
      usleep(10000);
494 39 julius
      sched_yield();
495
    }
496
#endif
497
 
498
  }
499
 
500
}       /* handle_rsp () */
501
 
502
 
503
/*
504
  Check if processor is stalled - if it is, read the DRR
505
  and return the target signal code
506
*/
507
static void rsp_check_for_exception()
508
{
509
 
510
  unsigned char stalled;
511
  uint32_t drr;
512 46 julius
  err = dbg_cpu0_read_ctrl(0, &stalled); /* check if we're stalled */
513
 
514 39 julius
  if (!(stalled & 0x01))
515
    {
516
      // Processor not stalled. Just return;
517
      return;
518
    }
519
 
520
  if (DEBUG_GDB) printf("rsp_check_for_exception() detected processor was stalled\nChecking DRR\n");
521
 
522
  // We're stalled
523
  stallState = STALLED;
524
  npcIsCached = 0;
525
 
526
  gdb_set_chain(SC_RISC_DEBUG);
527
 
528
  // Now read the DRR (Debug Reason Register)
529
  gdb_read_reg(DRR_CPU_REG_ADD, &drr);
530
 
531
  if (DEBUG_GDB) printf("DRR: 0x%08x\n", drr);
532
 
533
  switch ((int)(drr&0xffffffff))
534
    {
535
    case SPR_DRR_RSTE:  rsp.sigval = TARGET_SIGNAL_PWR;  break;
536
    case SPR_DRR_BUSEE: rsp.sigval = TARGET_SIGNAL_BUS;  break;
537
    case SPR_DRR_DPFE:  rsp.sigval = TARGET_SIGNAL_SEGV; break;
538
    case SPR_DRR_IPFE:  rsp.sigval = TARGET_SIGNAL_SEGV; break;
539
    case SPR_DRR_TTE:   rsp.sigval = TARGET_SIGNAL_ALRM; break;
540
    case SPR_DRR_AE:    rsp.sigval = TARGET_SIGNAL_BUS;  break;
541
    case SPR_DRR_IIE:   rsp.sigval = TARGET_SIGNAL_ILL;  break;
542
    case SPR_DRR_IE:    rsp.sigval = TARGET_SIGNAL_INT;  break;
543
    case SPR_DRR_DME:   rsp.sigval = TARGET_SIGNAL_SEGV; break;
544
    case SPR_DRR_IME:   rsp.sigval = TARGET_SIGNAL_SEGV; break;
545
    case SPR_DRR_RE:    rsp.sigval = TARGET_SIGNAL_FPE;  break;
546
    case SPR_DRR_SCE:   rsp.sigval = TARGET_SIGNAL_USR2; break;
547
    case SPR_DRR_FPE:   rsp.sigval = TARGET_SIGNAL_FPE;  break;
548
    case SPR_DRR_TE:    rsp.sigval = TARGET_SIGNAL_TRAP; break;
549
 
550
    default:
551
      // This must be the case of single step (which does not set DRR)
552
      rsp.sigval = TARGET_SIGNAL_TRAP; break;
553
    }
554
 
555
  if (DEBUG_GDB) printf("rsp.sigval: 0x%x\n", rsp.sigval);
556
 
557
  return;
558
}
559
 
560
/*---------------------------------------------------------------------------*/
561
/*!Check if PPC is in an exception vector that halts program flow
562
 
563
Compare the provided PPC with known exception vectors that are fatal
564
to a program's execution. Call rsp_exception(ppc) to set the appropriate
565
sigval and return.
566
 
567
@param[in] ppc  Value of current PPC, as read from debug unit
568
@return: 1 if we set a sigval and should return control to GDB, else 0       */
569
/*---------------------------------------------------------------------------*/
570
static int
571
check_for_exception_vector(uint32_t ppc)
572
{
573
  switch(ppc)
574
    {
575
      // The following should return sigvals to GDB for processing
576
    case EXCEPT_BUSERR:
577
    case EXCEPT_ALIGN:
578
    case EXCEPT_ILLEGAL:
579
    case EXCEPT_TRAP:     if(DEBUG_GDB)
580
                            printf("PPC at exception address\n");
581
                          rsp_exception(ppc);
582
                          return 1;
583
 
584
    default:
585
      return 0;
586
    }
587
  return 1;
588
}
589
 
590
/*---------------------------------------------------------------------------*/
591
/*!Note an exception for future processing
592
 
593
   The simulator has encountered an exception. Record it here, so that a
594
   future call to handle_exception will report it back to the client. The
595
   signal is supplied in Or1ksim form and recorded in GDB form.
596
 
597
   We flag up a warning if an exception is already pending, and ignore the
598
   earlier exception.
599
 
600
   @param[in] except  The exception (Or1ksim form)                           */
601
/*---------------------------------------------------------------------------*/
602
void
603
rsp_exception (uint32_t  except)
604
{
605
  int  sigval;                  /* GDB signal equivalent to exception */
606
 
607
  switch (except)
608
    {
609
    case EXCEPT_RESET:    sigval = TARGET_SIGNAL_PWR;  break;
610
    case EXCEPT_BUSERR:   sigval = TARGET_SIGNAL_BUS;  break;
611
    case EXCEPT_DPF:      sigval = TARGET_SIGNAL_SEGV; break;
612
    case EXCEPT_IPF:      sigval = TARGET_SIGNAL_SEGV; break;
613
    case EXCEPT_TICK:     sigval = TARGET_SIGNAL_ALRM; break;
614
    case EXCEPT_ALIGN:    sigval = TARGET_SIGNAL_BUS;  break;
615
    case EXCEPT_ILLEGAL:  sigval = TARGET_SIGNAL_ILL;  break;
616
    case EXCEPT_INT:      sigval = TARGET_SIGNAL_INT;  break;
617
    case EXCEPT_DTLBMISS: sigval = TARGET_SIGNAL_SEGV; break;
618
    case EXCEPT_ITLBMISS: sigval = TARGET_SIGNAL_SEGV; break;
619
    case EXCEPT_RANGE:    sigval = TARGET_SIGNAL_FPE;  break;
620
    case EXCEPT_SYSCALL:  sigval = TARGET_SIGNAL_USR2; break;
621
    case EXCEPT_FPE:      sigval = TARGET_SIGNAL_FPE;  break;
622
    case EXCEPT_TRAP:     sigval = TARGET_SIGNAL_TRAP; break;
623
 
624
    default:
625
      fprintf (stderr, "Warning: Unknown RSP exception %u: Ignored\n", except);
626
      return;
627
    }
628
 
629
  if ((0 != rsp.sigval) && (sigval != rsp.sigval))
630
    {
631
      fprintf (stderr, "Warning: RSP signal %d received while signal "
632
               "%d pending: Pending exception replaced\n", sigval, rsp.sigval);
633
    }
634
 
635
  rsp.sigval         = sigval;          /* Save the signal value */
636
 
637
}       /* rsp_exception () */
638
 
639
 
640
/*---------------------------------------------------------------------------*/
641
/*!Get a new client connection.
642
 
643
   Blocks until the client connection is available.
644
 
645
   A lot of this code is copied from remote_open in gdbserver remote-utils.c.
646
 
647
   This involves setting up a socket to listen on a socket for attempted
648
   connections from a single GDB instance (we couldn't be talking to multiple
649
   GDBs at once!).
650
 
651
   The service is specified either as a port number in the Or1ksim configuration
652
   (parameter rsp_port in section debug, default 51000) or as a service name
653
   in the constant OR1KSIM_RSP_SERVICE.
654
 
655
   The protocol used for communication is specified in OR1KSIM_RSP_PROTOCOL. */
656
/*---------------------------------------------------------------------------*/
657
static void
658
rsp_get_client (void)
659
{
660
  int                 tmp_fd;           /* Temporary descriptor for socket */
661
  int                 optval;           /* Socket options */
662
  struct sockaddr_in  sock_addr;        /* Socket address */
663
  socklen_t           len;              /* Size of the socket address */
664
 
665
  /* 0 is used as the RSP port number to indicate that we should use the
666
     service name instead. */
667
  if (0 == serverPort)
668
  {
669
    struct servent *service = getservbyname (OR1KSIM_RSP_SERVICE, "tcp");
670
    if (NULL == service)
671
      {
672
        fprintf (stderr, "Warning: RSP unable to find service \"%s\": %s\n",
673
                 OR1KSIM_RSP_SERVICE, strerror (errno));
674
        return;
675
      }
676
    serverPort = ntohs (service->s_port);
677
  }
678
 
679
  /* Open a socket on which we'll listen for clients */
680
  tmp_fd = socket (PF_INET, SOCK_STREAM, IPPROTO_TCP);
681
  if (tmp_fd < 0)
682
    {
683
      fprintf (stderr, "ERROR: Cannot open RSP socket\n");
684
      exit (0);
685
    }
686
 
687
  /* Allow rapid reuse of the port on this socket */
688
  optval = 1;
689
  setsockopt (tmp_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&optval,
690
              sizeof (optval));
691
 
692
  /* Bind the port to the socket */
693
  sock_addr.sin_family      = PF_INET;
694
  sock_addr.sin_port        = htons (serverPort);
695
  sock_addr.sin_addr.s_addr = INADDR_ANY;
696
  if (bind (tmp_fd, (struct sockaddr *) &sock_addr, sizeof (sock_addr)))
697
    {
698
      fprintf (stderr, "ERROR: Cannot bind to RSP socket\n");
699
      exit (0);
700
    }
701
 
702
  /* Listen for (at most one) client */
703
  if (0 != listen (tmp_fd, 1))
704
    {
705
      fprintf (stderr, "ERROR: Cannot listen on RSP socket\n");
706
      exit (0);
707
    }
708
 
709
  printf("Waiting for gdb connection on localhost:%d\n", serverPort);
710
  fflush (stdout);
711
 
712
  printf("Press CTRL+c to exit.\n");
713
  fflush (stdout);
714
 
715
  /* Accept a client which connects */
716
  len = sizeof (sock_addr);
717
  rsp.client_fd = accept (tmp_fd, (struct sockaddr *)&sock_addr, &len);
718
 
719
  if (-1 == rsp.client_fd)
720
  {
721
    fprintf (stderr, "Warning: Failed to accept RSP client\n");
722
    return;
723
  }
724
 
725
  /* Enable TCP keep alive process */
726
  optval = 1;
727
  setsockopt (rsp.client_fd, SOL_SOCKET, SO_KEEPALIVE, (char *)&optval,
728
              sizeof (optval));
729
 
730
  int flags;
731
 
732
  /* If they have O_NONBLOCK, use the Posix way to do it */
733
 
734
#if defined(O_NONBLOCK)
735
  /* Fixme: O_NONBLOCK is defined but broken on SunOS 4.1.x and AIX 3.2.5. */
736
  if (-1 == (flags = fcntl(rsp.client_fd, F_GETFL, 0)))
737
    flags = 0;
738
 
739
  fcntl(rsp.client_fd, F_SETFL, flags | O_NONBLOCK);
740
#else
741
  /* Otherwise, use the old way of doing it */
742
  flags = 1;
743
  ioctl(fd, FIOBIO, &flags);
744
#endif
745
 
746
 
747
 
748
  /* Set socket to be non-blocking */
749
 
750
  /* We do this because when we're given a continue, or step
751
     instruction,command we set the processor stall off, then instnatly check
752
     if it's stopped. If it hasn't then we drop through and wait for input
753
     from GDB. Obviously this will cause problems when it will stop after we
754
     do the check. So now, rsp_peek() has been implemented to simply check if
755
     there's an incoming command from GDB (only interested in interrupt
756
     commands), otherwise it returns back to and poll the processor's PPC and
757
     stall bit. It can only do this if the socket is non-blocking.
758
 
759
     At first test, simply adding this line appeared to give no problems with
760
     the existing code. No "simulation" of blocking behaviour on the
761
     non-blocking socket was required (in the event that a read/write throws
762
     back a EWOULDBLOCK error, as was looked to be the case in the previous
763
     GDB handling code) -- Julius
764
  */
765
  if (ioctl(rsp.client_fd, FIONBIO, (char *)&optval) > 0 )
766
    {
767
      perror("ioctl() failed");
768
      close(rsp.client_fd);
769
      close(tmp_fd);
770
      exit(0);
771
    }
772
 
773
 
774
  /* Don't delay small packets, for better interactive response (disable
775
     Nagel's algorithm) */
776
  optval = 1;
777
  setsockopt (rsp.client_fd, IPPROTO_TCP, TCP_NODELAY, (char *)&optval,
778
              sizeof (optval));
779
 
780
  /* Socket is no longer needed */
781
  close (tmp_fd);                       /* No longer need this */
782
  signal (SIGPIPE, SIG_IGN);            /* So we don't exit if client dies */
783
 
784
  printf ("Remote debugging from host %s\n", inet_ntoa (sock_addr.sin_addr));
785
}       /* rsp_get_client () */
786
 
787
 
788
/*---------------------------------------------------------------------------*/
789
/*!Deal with a request from the GDB client session
790
 
791
   In general, apart from the simplest requests, this function replies on
792
   other functions to implement the functionality.                           */
793
/*---------------------------------------------------------------------------*/
794
static void rsp_client_request (void)
795
{
796
  struct rsp_buf *p_buf = get_packet ();        /* Message sent to us */
797
 
798
  // Null packet means we hit EOF or the link was closed for some other
799
  // reason. Close the client and return
800
  if (NULL == p_buf)
801
    {
802
      rsp_client_close ();
803
      return;
804
    }
805
 
806
  if (DEBUG_GDB){
807
    printf("%s-----------------------------------------------------\n", printTime());
808
    printf ("Packet received %s: %d chars\n", p_buf->data, p_buf->len );
809
    fflush (stdout);
810
  }
811
 
812
  switch (p_buf->data[0])
813
    {
814
    case '!':
815
      /* Request for extended remote mode */
816
      put_str_packet ("OK"); // OK = supports and has enabled extended mode.
817
      return;
818
 
819
    case '?':
820
      /* Return last signal ID */
821
      rsp_report_exception();
822
      return;
823
 
824
    case 'A':
825
      /* Initialization of argv not supported */
826
      fprintf (stderr, "Warning: RSP 'A' packet not supported: ignored\n");
827
      put_str_packet ("E01");
828
      return;
829
 
830
    case 'b':
831
      /* Setting baud rate is deprecated */
832
      fprintf (stderr, "Warning: RSP 'b' packet is deprecated and not "
833
               "supported: ignored\n");
834
      return;
835
 
836
    case 'B':
837
      /* Breakpoints should be set using Z packets */
838
      fprintf (stderr, "Warning: RSP 'B' packet is deprecated (use 'Z'/'z' "
839
               "packets instead): ignored\n");
840
      return;
841
 
842
    case 'c':
843
      /* Continue */
844
      rsp_continue (p_buf);
845
      return;
846
 
847
    case 'C':
848
      /* Continue with signal */
849
      rsp_continue_with_signal (p_buf);
850
      return;
851
 
852
    case 'd':
853
      /* Disable debug using a general query */
854
      fprintf (stderr, "Warning: RSP 'd' packet is deprecated (define a 'Q' "
855
               "packet instead: ignored\n");
856
      return;
857
 
858
    case 'D':
859
      /* Detach GDB. Do this by closing the client. The rules say that
860
         execution should continue. TODO. Is this really then intended
861
         meaning? Or does it just mean that only vAttach will be recognized
862
         after this? */
863
      put_str_packet ("OK");
864
      rsp_client_close ();
865
      reset_or1k ();
866
      set_stall_state (0);
867
      return;
868
 
869
    case 'F':
870
      /* File I/O is not currently supported */
871
      fprintf (stderr, "Warning: RSP file I/O not currently supported: 'F' "
872
               "packet ignored\n");
873
      return;
874
 
875
    case 'g':
876
      rsp_read_all_regs ();
877
      return;
878
 
879
    case 'G':
880
      rsp_write_all_regs (p_buf);
881
      return;
882
 
883
    case 'H':
884
      /* Set the thread number of subsequent operations. For now ignore
885
            silently and just reply "OK" */
886
      put_str_packet ("OK");
887
      return;
888
 
889
    case 'i':
890
      /* Single instruction step */
891
      fprintf (stderr, "Warning: RSP cycle stepping not supported: target "
892
               "stopped immediately\n");
893
      rsp.client_waiting = 1;                   /* Stop reply will be sent */
894
      return;
895
 
896
    case 'I':
897
      /* Single instruction step with signal */
898
      fprintf (stderr, "Warning: RSP cycle stepping not supported: target "
899
               "stopped immediately\n");
900
      rsp.client_waiting = 1;                   /* Stop reply will be sent */
901
      return;
902
 
903
    case 'k':
904
      /* Kill request. Do nothing for now. */
905
      return;
906
 
907
    case 'm':
908
      /* Read memory (symbolic) */
909
      rsp_read_mem (p_buf);
910
      return;
911
 
912
    case 'M':
913
      /* Write memory (symbolic) */
914
      rsp_write_mem (p_buf);
915
      return;
916
 
917
    case 'p':
918
      /* Read a register */
919
      rsp_read_reg (p_buf);
920
      return;
921
 
922
    case 'P':
923
      /* Write a register */
924
      rsp_write_reg (p_buf);
925
      return;
926
 
927
    case 'q':
928
      /* Any one of a number of query packets */
929
      rsp_query (p_buf);
930
      return;
931
 
932
    case 'Q':
933
      /* Any one of a number of set packets */
934
      rsp_set (p_buf);
935
      return;
936
 
937
    case 'r':
938
      /* Reset the system. Deprecated (use 'R' instead) */
939
      fprintf (stderr, "Warning: RSP 'r' packet is deprecated (use 'R' "
940
               "packet instead): ignored\n");
941
      return;
942
 
943
    case 'R':
944
      /* Restart the program being debugged. */
945
      rsp_restart ();
946
      return;
947
 
948
    case 's':
949
      /* Single step (one high level instruction). This could be hard without
950
                        DWARF2 info */
951
      rsp_step (p_buf);
952
      return;
953
 
954
    case 'S':
955
      /* Single step (one high level instruction) with signal. This could be
956
                        hard without DWARF2 info */
957
      rsp_step_with_signal (p_buf);
958
      return;
959
 
960
    case 't':
961
      /* Search. This is not well defined in the manual and for now we don't
962
                        support it. No response is defined. */
963
      fprintf (stderr, "Warning: RSP 't' packet not supported: ignored\n");
964
      return;
965
 
966
    case 'T':
967
      /* Is the thread alive. We are bare metal, so don't have a thread
968
                        context. The answer is always "OK". */
969
      put_str_packet ("OK");
970
      return;
971
 
972
    case 'v':
973
      /* Any one of a number of packets to control execution */
974
      rsp_vpkt (p_buf);
975
      return;
976
 
977
    case 'X':
978
      /* Write memory (binary) */
979
      rsp_write_mem_bin (p_buf);
980
      return;
981
 
982
    case 'z':
983
      /* Remove a breakpoint/watchpoint. */
984
      rsp_remove_matchpoint (p_buf);
985
      return;
986
 
987
    case 'Z':
988
      /* Insert a breakpoint/watchpoint. */
989
      rsp_insert_matchpoint (p_buf);
990
      return;
991
 
992
    default:
993
      /* Unknown commands are ignored */
994
      fprintf (stderr, "Warning: Unknown RSP request %s\n", p_buf->data);
995
      return;
996
    }
997
}       /* rsp_client_request () */
998
 
999
 
1000
/*---------------------------------------------------------------------------*/
1001
/*!Close the connection to the client if it is open                          */
1002
/*---------------------------------------------------------------------------*/
1003
static void
1004
rsp_client_close (void)
1005
{
1006
  if (-1 != rsp.client_fd)
1007
    {
1008
      close (rsp.client_fd);
1009
      rsp.client_fd = -1;
1010
    }
1011
}       /* rsp_client_close () */
1012
 
1013
 
1014
/*---------------------------------------------------------------------------*/
1015
/*!Send a packet to the GDB client
1016
 
1017
   Modeled on the stub version supplied with GDB. Put out the data preceded by
1018
   a '$', followed by a '#' and a one byte checksum. '$', '#', '*' and '}' are
1019
   escaped by preceding them with '}' and then XORing the character with
1020
   0x20.
1021
 
1022
   @param[in] p_buf  The data to send                                          */
1023
/*---------------------------------------------------------------------------*/
1024
static void
1025
put_packet (struct rsp_buf *p_buf)
1026
{
1027
  unsigned char  data[GDB_BUF_MAX * 2];
1028
  int   len;
1029
  int   ch;                             /* Ack char */
1030
 
1031
  /* Construct $<packet info>#<checksum>. Repeat until the GDB client
1032
     acknowledges satisfactory receipt. */
1033
  do
1034
  {
1035
    unsigned char checksum = 0;  /* Computed checksum */
1036
    int           count    = 0;  /* Index into the buffer */
1037
 
1038
    if (DEBUG_GDB_DUMP_DATA){
1039
      printf ("Putting %s\n\n", p_buf->data);
1040
      fflush (stdout);
1041
    }
1042
 
1043
    len = 0;
1044
    data[len++] =  '$';                 /* Start char */
1045
 
1046
    /* Body of the packet */
1047
    for (count = 0; count < p_buf->len; count++)
1048
                {
1049
                  unsigned char  ch = p_buf->data[count];
1050
 
1051
                  /* Check for escaped chars */
1052
                  if (('$' == ch) || ('#' == ch) || ('*' == ch) || ('}' == ch))
1053
                    {
1054
                      ch       ^= 0x20;
1055
                      checksum += (unsigned char)'}';
1056
                                        data[len++] =  '}';
1057
                    }
1058
 
1059
                  checksum += ch;
1060
                        data[len++] =  ch;
1061
                }
1062
 
1063
                data[len++] =  '#';                     /* End char */
1064
 
1065
    /* Computed checksum */
1066
    data[len++] =       (hexchars[checksum >> 4]);
1067
                data[len++] =   (hexchars[checksum % 16]);
1068
 
1069
                send_rsp_str ((unsigned char *) &data, len);
1070
 
1071
    /* Check for ack of connection failure */
1072
    ch = get_rsp_char ();
1073
    if (0 > ch)
1074
                {
1075
                  return;                       /* Fail the put silently. */
1076
                }
1077
  }
1078
  while ('+' != ch);
1079
 
1080
}       /* put_packet () */
1081
 
1082
 
1083
/*---------------------------------------------------------------------------*/
1084
/*!Convenience to put a constant string packet
1085
 
1086
   param[in] str  The text of the packet                                     */
1087
/*---------------------------------------------------------------------------*/
1088
static void
1089
put_str_packet (const char *str)
1090
{
1091
  struct rsp_buf  buffer;
1092
  int    len = strlen (str);
1093
 
1094
  /* Construct the packet to send, so long as string is not too big,
1095
     otherwise truncate. Add EOS at the end for convenient debug printout */
1096
 
1097
  if (len >= GDB_BUF_MAX)
1098
    {
1099
      fprintf (stderr, "Warning: String %s too large for RSP packet: "
1100
               "truncated\n", str);
1101
      len = GDB_BUF_MAX - 1;
1102
    }
1103
 
1104
  strncpy (buffer.data, str, len);
1105
  buffer.data[len] = 0;
1106
  buffer.len       = len;
1107
 
1108
  put_packet (&buffer);
1109
 
1110
}       /* put_str_packet () */
1111
 
1112
 
1113
/*---------------------------------------------------------------------------*/
1114
/*!Get a packet from the GDB client
1115
 
1116
   Modeled on the stub version supplied with GDB. The data is in a static
1117
   buffer. The data should be copied elsewhere if it is to be preserved across
1118
   a subsequent call to get_packet().
1119
 
1120
   Unlike the reference implementation, we don't deal with sequence
1121
   numbers. GDB has never used them, and this implementation is only intended
1122
   for use with GDB 6.8 or later. Sequence numbers were removed from the RSP
1123
   standard at GDB 5.0.
1124
 
1125
   @return  A pointer to the static buffer containing the data                */
1126
/*---------------------------------------------------------------------------*/
1127
static struct rsp_buf *
1128
get_packet (void)
1129
{
1130
  static struct rsp_buf  buf;           /* Survives the return */
1131
 
1132
  /* Keep getting packets, until one is found with a valid checksum */
1133
  while (1)
1134
        {
1135
                unsigned char checksum;         /* The checksum we have computed */
1136
                int           count;                    /* Index into the buffer */
1137
                int                     ch;                                     /* Current character */
1138
 
1139
    /* Wait around for the start character ('$'). Ignore all other
1140
          characters */
1141
    ch = get_rsp_char ();
1142
 
1143
    while (ch != '$')
1144
                {
1145
                  if (-1 == ch)
1146
                    {
1147
                      return  NULL;             /* Connection failed */
1148
                    }
1149
 
1150
                  ch = get_rsp_char ();
1151
 
1152
                  // Potentially handle an interrupt character (0x03) here                
1153
                }
1154
 
1155
    /* Read until a '#' or end of buffer is found */
1156
    checksum =  0;
1157
    count    =  0;
1158
    while (count < GDB_BUF_MAX - 1)
1159
                {
1160
                  ch = get_rsp_char ();
1161
 
1162
                  if(rsp.client_waiting && DEBUG_GDB)
1163
                    {
1164
                      printf("%x\n",ch);
1165
                    }
1166
 
1167
 
1168
                  /* Check for connection failure */
1169
                  if (0 > ch)
1170
                    {
1171
                      return  NULL;
1172
                    }
1173
 
1174
                  /* If we hit a start of line char begin all over again */
1175
                  if ('$' == ch)
1176
                    {
1177
                      checksum =  0;
1178
                      count    =  0;
1179
 
1180
                      continue;
1181
                    }
1182
 
1183
                  /* Break out if we get the end of line char */
1184
                  if ('#' == ch)
1185
                    {
1186
                      break;
1187
                    }
1188
 
1189
                  /* Update the checksum and add the char to the buffer */
1190
 
1191
                  checksum        = checksum + (unsigned char)ch;
1192
                  buf.data[count] = (char)ch;
1193
                  count           = count + 1;
1194
                }
1195
 
1196
    /* Mark the end of the buffer with EOS - it's convenient for non-binary
1197
          data to be valid strings. */
1198
    buf.data[count] = 0;
1199
    buf.len         = count;
1200
 
1201
    /* If we have a valid end of packet char, validate the checksum */
1202
    if ('#' == ch)
1203
                {
1204
                  unsigned char  xmitcsum;      /* The checksum in the packet */
1205
 
1206
                  ch = get_rsp_char ();
1207
                  if (0 > ch)
1208
                    {
1209
                      return  NULL;             /* Connection failed */
1210
                    }
1211
                  xmitcsum = hex (ch) << 4;
1212
 
1213
                  ch = get_rsp_char ();
1214
                  if (0 > ch)
1215
                    {
1216
                      return  NULL;             /* Connection failed */
1217
                    }
1218
 
1219
                  xmitcsum += hex (ch);
1220
 
1221
                  /* If the checksums don't match print a warning, and put the
1222
                     negative ack back to the client. Otherwise put a positive ack. */
1223
                  if (checksum != xmitcsum)
1224
                    {
1225
                      fprintf (stderr, "Warning: Bad RSP checksum: Computed "
1226
                               "0x%02x, received 0x%02x\n", checksum, xmitcsum);
1227
 
1228
                                        ch = '-';
1229
                      send_rsp_str ((unsigned char *) &ch, 1);  /* Failed checksum */
1230
                    }
1231
                  else
1232
                    {
1233
                                        ch = '+';
1234
                      send_rsp_str ((unsigned char *) &ch, 1);  /* successful transfer */
1235
                      break;
1236
                    }
1237
                }
1238
    else
1239
                {
1240
                  fprintf (stderr, "Warning: RSP packet overran buffer\n");
1241
                }
1242
  }
1243
  return &buf;                          /* Success */
1244
}       /* get_packet () */
1245
 
1246
 
1247
/*---------------------------------------------------------------------------*/
1248
/*!Put a single character out onto the client socket
1249
 
1250
   This should only be called if the client is open, but we check for safety.
1251
 
1252
   @param[in] c  The character to put out                                    */
1253
/*---------------------------------------------------------------------------*/
1254
static void
1255
send_rsp_str (unsigned char *data, int len)
1256
{
1257
  if (-1 == rsp.client_fd)
1258
    {
1259
      fprintf (stderr, "Warning: Attempt to write '%s' to unopened RSP "
1260
               "client: Ignored\n", data);
1261
      return;
1262
    }
1263
 
1264
  /* Write until successful (we retry after interrupts) or catastrophic
1265
     failure. */
1266
  while (1)
1267
    {
1268
      switch (write (rsp.client_fd, data, len))
1269
                        {
1270
                        case -1:
1271
                          /* Error: only allow interrupts or would block */
1272
                          if ((EAGAIN != errno) && (EINTR != errno))
1273
                            {
1274
                              fprintf (stderr, "Warning: Failed to write to RSP client: "
1275
                                       "Closing client connection: %s\n",
1276
                                       strerror (errno));
1277
                              rsp_client_close ();
1278
                              return;
1279
                            }
1280
 
1281
                          break;
1282
 
1283
                        case 0:
1284
                          break;                /* Nothing written! Try again */
1285
 
1286
                        default:
1287
                          return;               /* Success, we can return */
1288
                        }
1289
    }
1290
}       /* send_rsp_str () */
1291
 
1292
 
1293
/*---------------------------------------------------------------------------*/
1294
/*!Get a single character from the client socket
1295
 
1296
   This should only be called if the client is open, but we check for safety.
1297
 
1298
   @return  The character read, or -1 on failure                             */
1299
/*---------------------------------------------------------------------------*/
1300
static int
1301
get_rsp_char ()
1302
{
1303
  if (-1 == rsp.client_fd)
1304
    {
1305
      fprintf (stderr, "Warning: Attempt to read from unopened RSP "
1306
               "client: Ignored\n");
1307
      return  -1;
1308
    }
1309
 
1310
  /* Non-blocking read until successful (we retry after interrupts) or
1311
     catastrophic failure. */
1312
  while (1)
1313
    {
1314
      unsigned char  c;
1315
 
1316
      switch (read (rsp.client_fd, &c, sizeof (c)))
1317
        {
1318
        case -1:
1319
          /* Error: only allow interrupts */
1320
          if ((EAGAIN != errno) && (EINTR != errno))
1321
            {
1322
              fprintf (stderr, "Warning: Failed to read from RSP client: "
1323
                       "Closing client connection: %s\n",
1324
                       strerror (errno));
1325
              rsp_client_close ();
1326
              return  -1;
1327
            }
1328
 
1329
          break;
1330
 
1331
        case 0:
1332
          // EOF
1333
          rsp_client_close ();
1334
          return  -1;
1335
 
1336
        default:
1337
          return  c & 0xff; /* Success, we can return (no sign extend!) */
1338
        }
1339
    }
1340
}       /* get_rsp_char () */
1341
 
1342
/*---------------------------------------------------------------------------*/
1343
/* !Peek at data coming into server from GDB
1344
 
1345
   Useful for polling for ETX (0x3) chars being sent when GDB wants to
1346
   interrupt
1347
 
1348
   @return the char we peeked, 0 otherwise                                   */
1349
/*---------------------------------------------------------------------------*/
1350
static char
1351
rsp_peek()
1352
{
1353
  /*
1354
  if (-1 == rsp.client_fd)
1355
    {
1356
      fprintf (stderr, "Warning: Attempt to read from unopened RSP "
1357
               "client: Ignored\n");
1358
      return  -1;
1359
    }
1360
  */
1361
  char  c;
1362
  int n;
1363
  // Using recv here instead of read becuase we can pass the MSG_PEEK
1364
  // flag, which lets us look at what's on the socket, without actually
1365
  // taking it off
1366
 
1367
  //if (DEBUG_GDB) 
1368
  //  printf("peeking at GDB socket...\n");
1369
 
1370
  n = recv (rsp.client_fd, &c, sizeof (c), MSG_PEEK);
1371
 
1372
  //if (DEBUG_GDB) 
1373
  //  printf("peeked, got n=%d, c=0x%x\n",n, c);
1374
 
1375
  if (n > 0)
1376
    return c;
1377
  else
1378
    return '\0';
1379
 
1380
}
1381
 
1382
/*---------------------------------------------------------------------------*/
1383
/*!Handle an interrupt from GDB
1384
 
1385
 Detect an interrupt from GDB and stall the processor                        */
1386
/*---------------------------------------------------------------------------*/
1387
static void
1388
rsp_interrupt()
1389
{
1390
  unsigned char  c;
1391
 
1392
  if (read (rsp.client_fd, &c, sizeof (c)) <= 0)
1393
    {
1394
      // Had issues, just return
1395
      return;
1396
    }
1397
 
1398
  // Ensure this is a ETX control char (0x3), currently, we only call this
1399
  // function when we've peeked and seen it, otherwise, ignore, return and pray
1400
  // things go back to normal...
1401
  if (c != 0x03)
1402
    {
1403
      printf("Warning: Interrupt character expected but not found on socket.\n");
1404
      return;
1405
    }
1406
 
1407
  // Otherwise, it's an interrupt packet, stall the processor, and upon return
1408
  // to the main handle_rsp() loop, it will inform GDB.
1409
 
1410
  if (DEBUG_GDB) printf("Interrupt received from GDB. Stalling processor.\n");
1411
 
1412
  set_stall_state (1);
1413
 
1414
  // Send a stop reply response, manually set rsp.sigval to TARGET_SIGNAL_NONE
1415
  rsp.sigval = TARGET_SIGNAL_NONE;
1416
  rsp_report_exception();
1417
  rsp.client_waiting = 0;                /* No longer waiting */
1418
 
1419
  return;
1420
 
1421
}
1422
 
1423
 
1424
/*---------------------------------------------------------------------------*/
1425
/*!"Unescape" RSP binary data
1426
 
1427
   '#', '$' and '}' are escaped by preceding them by '}' and oring with 0x20.
1428
 
1429
   This function reverses that, modifying the data in place.
1430
 
1431
   @param[in] data  The array of bytes to convert
1432
   @para[in]  len   The number of bytes to be converted
1433
 
1434
   @return  The number of bytes AFTER conversion                             */
1435
/*---------------------------------------------------------------------------*/
1436
static int
1437
rsp_unescape (char *data,
1438
              int   len)
1439
{
1440
  int  from_off = 0;             /* Offset to source char */
1441
  int  to_off   = 0;             /* Offset to dest char */
1442
 
1443
  while (from_off < len)
1444
    {
1445
      /* Is it escaped */
1446
      if ( '}' == data[from_off])
1447
                        {
1448
                          from_off++;
1449
                          data[to_off] = data[from_off] ^ 0x20;
1450
                        }
1451
                  else
1452
                        {
1453
                          data[to_off] = data[from_off];
1454
                        }
1455
 
1456
      from_off++;
1457
      to_off++;
1458
    }
1459
 
1460
  return  to_off;
1461
 
1462
}       /* rsp_unescape () */
1463
 
1464
 
1465
/*---------------------------------------------------------------------------*/
1466
/*!Initialize the matchpoint hash table
1467
 
1468
   This is an open hash table, so this function clears all the links to
1469
   NULL.                                                                     */
1470
/*---------------------------------------------------------------------------*/
1471
static void
1472
mp_hash_init (void)
1473
{
1474
  int  i;
1475
 
1476
  for (i = 0; i < MP_HASH_SIZE; i++)
1477
    {
1478
      rsp.mp_hash[i] = NULL;
1479
    }
1480
}       /* mp_hash_init () */
1481
 
1482
 
1483
/*---------------------------------------------------------------------------*/
1484
/*!Add an entry to the matchpoint hash table
1485
 
1486
   Add the entry if it wasn't already there. If it was there do nothing. The
1487
   match just be on type and addr. The instr need not match, since if this is
1488
   a duplicate insertion (perhaps due to a lost packet) they will be
1489
   different.
1490
 
1491
   @param[in] type   The type of matchpoint
1492
   @param[in] addr   The address of the matchpoint
1493
   @para[in]  instr  The instruction to associate with the address           */
1494
/*---------------------------------------------------------------------------*/
1495
static void
1496
mp_hash_add (enum mp_type type,
1497
             uint32_t  addr,
1498
             uint32_t  instr)
1499
{
1500
  int              hv    = addr % MP_HASH_SIZE;
1501
  struct mp_entry *curr;
1502
 
1503
  /* See if we already have the entry */
1504
  for(curr = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1505
  {
1506
    if ((type == curr->type) && (addr == curr->addr))
1507
                {
1508
                  return;               /* We already have the entry */
1509
                }
1510
  }
1511
 
1512
  /* Insert the new entry at the head of the chain */
1513
  curr = (struct mp_entry*) malloc (sizeof (*curr));
1514
 
1515
  curr->type  = type;
1516
  curr->addr  = addr;
1517
  curr->instr = instr;
1518
  curr->next  = rsp.mp_hash[hv];
1519
 
1520
  rsp.mp_hash[hv] = curr;
1521
 
1522
}       /* mp_hash_add () */
1523
 
1524
 
1525
/*---------------------------------------------------------------------------*/
1526
/*!Look up an entry in the matchpoint hash table
1527
 
1528
   The match must be on type AND addr.
1529
 
1530
   @param[in] type   The type of matchpoint
1531
   @param[in] addr   The address of the matchpoint
1532
 
1533
   @return  The entry deleted, or NULL if the entry was not found            */
1534
/*---------------------------------------------------------------------------*/
1535
static struct mp_entry * mp_hash_lookup (enum mp_type type,     uint32_t addr)
1536
{
1537
  int    hv = addr % MP_HASH_SIZE;
1538
  struct mp_entry *curr;
1539
 
1540
  /* Search */
1541
  for (curr = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1542
  {
1543
    if ((type == curr->type) && (addr == curr->addr))
1544
                {
1545
                  return  curr;         /* The entry found */
1546
                }
1547
  }
1548
 
1549
  /* Not found */
1550
  return  NULL;
1551
 
1552
}       /* mp_hash_lookup () */
1553
 
1554
 
1555
/*---------------------------------------------------------------------------*/
1556
/*!Delete an entry from the matchpoint hash table
1557
 
1558
   If it is there the entry is deleted from the hash table. If it is not
1559
   there, no action is taken. The match must be on type AND addr.
1560
 
1561
   The usual fun and games tracking the previous entry, so we can delete
1562
   things.
1563
 
1564
   @note  The deletion DOES NOT free the memory associated with the entry,
1565
          since that is returned. The caller should free the memory when they
1566
          have used the information.
1567
 
1568
   @param[in] type   The type of matchpoint
1569
   @param[in] addr   The address of the matchpoint
1570
 
1571
   @return  The entry deleted, or NULL if the entry was not found            */
1572
/*---------------------------------------------------------------------------*/
1573
static struct mp_entry *
1574
mp_hash_delete (enum mp_type       type,
1575
                uint32_t  addr)
1576
{
1577
  int              hv   = addr % MP_HASH_SIZE;
1578
  struct mp_entry *prev = NULL;
1579
  struct mp_entry *curr;
1580
 
1581
  /* Search */
1582
  for (curr  = rsp.mp_hash[hv]; NULL != curr; curr = curr->next)
1583
    {
1584
      if ((type == curr->type) && (addr == curr->addr))
1585
        {
1586
          /* Found - delete. Method depends on whether we are the head of
1587
             chain. */
1588
          if (NULL == prev)
1589
            {
1590
              rsp.mp_hash[hv] = curr->next;
1591
            }
1592
          else
1593
            {
1594
              prev->next = curr->next;
1595
            }
1596
 
1597
          return  curr;         /* The entry deleted */
1598
        }
1599
 
1600
      prev = curr;
1601
    }
1602
 
1603
  /* Not found */
1604
  return  NULL;
1605
 
1606
}       /* mp_hash_delete () */
1607
 
1608
 
1609
/*---------------------------------------------------------------------------*/
1610
/*!Utility to give the value of a hex char
1611
 
1612
   @param[in] ch  A character representing a hexadecimal digit. Done as -1,
1613
                  for consistency with other character routines, which can use
1614
                  -1 as EOF.
1615
 
1616
   @return  The value of the hex character, or -1 if the character is
1617
            invalid.                                                         */
1618
/*---------------------------------------------------------------------------*/
1619
static int hex (int  c)
1620
{
1621
  return  ((c >= 'a') && (c <= 'f')) ? c - 'a' + 10 :
1622
          ((c >= '0') && (c <= '9')) ? c - '0' :
1623
          ((c >= 'A') && (c <= 'F')) ? c - 'A' + 10 : -1;
1624
 
1625
}       /* hex () */
1626
 
1627
 
1628
/*---------------------------------------------------------------------------*/
1629
/*!Convert a register to a hex digit string
1630
 
1631
   The supplied 32-bit value is converted to an 8 digit hex string according
1632
   the target endianism. It is null terminated for convenient printing.
1633
 
1634
   @param[in]  val  The value to convert
1635
   @param[out] p_buf  The buffer for the text string                           */
1636
/*---------------------------------------------------------------------------*/
1637
static void
1638
reg2hex (uint32_t  val, char *p_buf)
1639
{
1640
  int  n;                       /* Counter for digits */
1641
        int  nyb_shift;
1642
 
1643
  for (n = 0; n < 8; n++)
1644
    {
1645
#ifdef WORDSBIGENDIAN
1646
      if(n%2==0){
1647
        nyb_shift = n * 4 + 4;
1648
                        }
1649
                        else{
1650
                                nyb_shift = n * 4 - 4;
1651
                        }
1652
#else
1653
      nyb_shift = 28 - (n * 4);
1654
#endif
1655
      p_buf[n] = hexchars[(val >> nyb_shift) & 0xf];
1656
    }
1657
 
1658
  p_buf[8] = 0;                  /* Useful to terminate as string */
1659
 
1660
}       /* reg2hex () */
1661
 
1662
 
1663
/*---------------------------------------------------------------------------*/
1664
/*!Convert a hex digit string to a register value
1665
 
1666
   The supplied 8 digit hex string is converted to a 32-bit value according
1667
   the target endianism
1668
 
1669
   @param[in] p_buf  The buffer with the hex string
1670
 
1671
   @return  The value to convert                                             */
1672
/*---------------------------------------------------------------------------*/
1673
static uint32_t
1674
hex2reg (char *p_buf)
1675
{
1676
  int                n;         /* Counter for digits */
1677
  uint32_t  val = 0;     /* The result */
1678
 
1679
  for (n = 0; n < 8; n++)
1680
    {
1681
#ifdef WORDSBIGENDIAN
1682
      int  nyb_shift = n * 4;
1683
#else
1684
      int  nyb_shift = 28 - (n * 4);
1685
#endif
1686
      val |= hex (p_buf[n]) << nyb_shift;
1687
    }
1688
 
1689
  return val;
1690
 
1691
}       /* hex2reg () */
1692
 
1693
 
1694
/*---------------------------------------------------------------------------*/
1695
/*!Convert an ASCII character string to pairs of hex digits
1696
 
1697
   Both source and destination are null terminated.
1698
 
1699
   @param[out] dest  Buffer to store the hex digit pairs (null terminated)
1700
   @param[in]  src   The ASCII string (null terminated)                      */
1701
/*---------------------------------------------------------------------------*/
1702
static void  ascii2hex (char *dest,
1703
                        char *src)
1704
{
1705
  int  i;
1706
 
1707
  /* Step through converting the source string */
1708
  for (i = 0; src[i] != '\0'; i++)
1709
    {
1710
      char  ch = src[i];
1711
 
1712
      dest[i * 2]     = hexchars[ch >> 4 & 0xf];
1713
      dest[i * 2 + 1] = hexchars[ch      & 0xf];
1714
    }
1715
 
1716
  dest[i * 2] = '\0';
1717
 
1718
}       /* ascii2hex () */
1719
 
1720
 
1721
/*---------------------------------------------------------------------------*/
1722
/*!Convert pairs of hex digits to an ASCII character string
1723
 
1724
   Both source and destination are null terminated.
1725
 
1726
   @param[out] dest  The ASCII string (null terminated)
1727
   @param[in]  src   Buffer holding the hex digit pairs (null terminated)    */
1728
/*---------------------------------------------------------------------------*/
1729
static void  hex2ascii (char *dest,
1730
                        char *src)
1731
{
1732
  int  i;
1733
 
1734
  /* Step through convering the source hex digit pairs */
1735
  for (i = 0; src[i * 2] != '\0' && src[i * 2 + 1] != '\0'; i++)
1736
    {
1737
      dest[i] = ((hex (src[i * 2]) & 0xf) << 4) | (hex (src[i * 2 + 1]) & 0xf);
1738
    }
1739
 
1740
  dest[i] = '\0';
1741
 
1742
}       /* hex2ascii () */
1743
 
1744
 
1745
/*---------------------------------------------------------------------------*/
1746
/*!Set the program counter
1747
 
1748
   This sets the value in the NPC SPR. Not completely trivial, since this is
1749
   actually cached in cpu_state.pc. Any reset of the NPC also involves
1750
   clearing the delay state and setting the pcnext global.
1751
 
1752
   Only actually do this if the requested address is different to the current
1753
   NPC (avoids clearing the delay pipe).
1754
 
1755
   @param[in] addr  The address to use                                       */
1756
/*---------------------------------------------------------------------------*/
1757
static void
1758
set_npc (uint32_t  addr)
1759
{
1760
 
1761
  // First set the chain 
1762
  gdb_set_chain(SC_RISC_DEBUG); /* 1 RISC Debug Interface chain */
1763
 
1764
 
1765
  if (addr != get_npc())
1766
  {
1767
 
1768
    gdb_write_reg(NPC_CPU_REG_ADD, addr);
1769
 
1770
    if (STALLED == stallState)
1771
      {
1772
        if (DEBUG_GDB) printf("set_npc(): New NPC value (0x%08x) written and locally cached \n", addr);
1773
        npcCachedValue = addr;
1774
        npcIsCached = 1;
1775
      }
1776
    else
1777
      {
1778
        if (DEBUG_GDB) printf("set_npc(): New NPC value (0x%08x) written \n", addr);
1779
        npcIsCached = 0;
1780
      }
1781
 
1782
 
1783
  }
1784
  else
1785
    return;
1786
 
1787
 
1788
}       /* set_npc () */
1789
 
1790
 
1791
//! Read the value of the Next Program Counter (a SPR)
1792
 
1793
//! Setting the NPC flushes the pipeline, so subsequent reads will return
1794
//! zero until the processor has refilled the pipeline. This will not be
1795
//! happening if the processor is stalled (as it is when GDB had control),
1796
//! so we must cache the NPC. As soon as the processor is unstalled, this
1797
//! cached value becomes invalid.
1798
 
1799
//! If we are stalled and the value has been cached, use it. If we are stalled
1800
//! and the value has not been cached, cache it (for efficiency) and use
1801
//! it. Otherwise read the corresponding SPR.
1802
 
1803
//! @return  The value of the NPC
1804
static uint32_t get_npc ()
1805
{
1806
  uint32_t current_npc;
1807
 
1808
  if (STALLED == stallState)
1809
    {
1810
      if (npcIsCached == 0)
1811
        {
1812
          err = gdb_set_chain(SC_RISC_DEBUG);
1813
          err = gdb_read_reg(NPC_CPU_REG_ADD, &npcCachedValue);
1814
          if(err > 0){
1815
            printf("Error %d reading NPC\n", err);
1816
            rsp_client_close ();
1817
            return 0;
1818
          }
1819
          if (DEBUG_GDB) printf("get_npc(): caching newly read NPC value (0x%08x)\n",npcCachedValue);
1820
 
1821
 
1822
          npcIsCached    = 1;
1823
        }
1824
      else
1825
        if (DEBUG_GDB) printf("get_npc(): reading cached NPC value (0x%08x)\n",npcCachedValue);
1826
 
1827
      return  npcCachedValue;
1828
    }
1829
  else
1830
    {
1831
      err = gdb_read_reg(NPC_CPU_REG_ADD, &current_npc);
1832
      if(err > 0){
1833
        printf("Error %d reading NPC\n", err);
1834
        rsp_client_close ();
1835
        return 0;
1836
      }
1837
      return current_npc;
1838
 
1839
    }
1840
}       // get_npc ()
1841
 
1842
 
1843
 
1844
/*---------------------------------------------------------------------------*/
1845
/*!Send a packet acknowledging an exception has occurred
1846
 
1847
   This is only called if there is a client FD to talk to                    */
1848
/*---------------------------------------------------------------------------*/
1849
static void
1850
rsp_report_exception (void)
1851
{
1852
  struct rsp_buf  buffer;
1853
 
1854
  /* Construct a signal received packet */
1855
  buffer.data[0] = 'S';
1856
  buffer.data[1] = hexchars[rsp.sigval >> 4];
1857
  buffer.data[2] = hexchars[rsp.sigval % 16];
1858
  buffer.data[3] = 0;
1859
  buffer.len     = strlen (buffer.data);
1860
 
1861
  put_packet (&buffer);
1862
 
1863
}       /* rsp_report_exception () */
1864
 
1865
 
1866
/*---------------------------------------------------------------------------*/
1867
/*!Handle a RSP continue request
1868
 
1869
   Parse the command to see if there is an address. Uses the underlying
1870
   generic continue function, with EXCEPT_NONE.
1871
 
1872
   @param[in] p_buf  The full continue packet                                  */
1873
/*---------------------------------------------------------------------------*/
1874
static void
1875
rsp_continue (struct rsp_buf *p_buf)
1876
{
1877
  uint32_t  addr;               /* Address to continue from, if any */
1878
 
1879
  // First set the chain 
1880
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
1881
 
1882
  // Make sure the processor is stalled
1883
  gdb_ensure_or1k_stalled();
1884
 
1885
  if(err > 0){
1886
    printf("Error %d to set RISC Debug Interface chain in the CONTINUE command 'c'\n", err);
1887
    rsp_client_close ();
1888
    return;
1889
  }
1890
 
1891
  if (0 == strcmp ("c", p_buf->data))
1892
  {
1893
    // Arc Sim Code -->   addr = cpu_state.pc;  /* Default uses current NPC */
1894
    /* ---------- NPC ---------- */
1895
    addr = get_npc();
1896
  }
1897
  else if (1 != sscanf (p_buf->data, "c%x", &addr))
1898
  {
1899
    fprintf (stderr,
1900
       "Warning: RSP continue address %s not recognized: ignored\n",
1901
       p_buf->data);
1902
 
1903
    // Arc Sim Code -->   addr = cpu_state.pc;  /* Default uses current NPC */
1904
    /* ---------- NPC ---------- */
1905
    addr = get_npc();
1906
  }
1907
 
1908
  if (DEBUG_GDB) printf("rsp_continue() --> Read NPC = 0x%08x\n", addr);
1909
 
1910
  rsp_continue_generic (addr, EXCEPT_NONE);
1911
 
1912
}       /* rsp_continue () */
1913
 
1914
 
1915
/*---------------------------------------------------------------------------*/
1916
/*!Handle a RSP continue with signal request
1917
 
1918
   Currently null. Will use the underlying generic continue function.
1919
 
1920
   @param[in] p_buf  The full continue with signal packet                      */
1921
/*---------------------------------------------------------------------------*/
1922
static void
1923
rsp_continue_with_signal (struct rsp_buf *p_buf)
1924
{
1925
  printf ("RSP continue with signal '%s' received\n", p_buf->data);
1926
 
1927
}       /* rsp_continue_with_signal () */
1928
 
1929
 
1930
/*---------------------------------------------------------------------------*/
1931
/*!Generic processing of a continue request
1932
 
1933
   The signal may be EXCEPT_NONE if there is no exception to be
1934
   handled. Currently the exception is ignored.
1935
 
1936
   The single step flag is cleared in the debug registers and then the
1937
   processor is unstalled.
1938
 
1939
   @param[in] addr    Address from which to step
1940
   @param[in] except  The exception to use (if any)                          */
1941
/*---------------------------------------------------------------------------*/
1942
static void
1943
rsp_continue_generic (uint32_t  addr,
1944
                      uint32_t  except)
1945
{
1946
  uint32_t              temp_uint32;
1947
 
1948
  /* Set the address as the value of the next program counter */
1949
  set_npc (addr);
1950
 
1951
  /* Clear Debug Reason Register (DRR) 0x3015 */
1952
  // Arc sim --> cpu_state.sprs[SPR_DRR]   = 0;
1953
  if(gdb_write_reg(DRR_CPU_REG_ADD, 0)) printf("Error write to DRR register\n");
1954
 
1955
  /* Clear watchpoint break generation in Debug Mode Register 2 (DMR2) 0x3011 */
1956
  // Arc sim --> cpu_state.sprs[SPR_DMR2] &= ~SPR_DMR2_WGB;
1957
  if(gdb_read_reg(DMR2_CPU_REG_ADD, &temp_uint32)) printf("Error read from DMR2 register\n");
1958
  temp_uint32 &= ~SPR_DMR2_WGB;
1959
  if(gdb_write_reg(DMR2_CPU_REG_ADD, temp_uint32)) printf("Error write to DMR2 register\n");
1960
 
1961
  /* Clear the single step trigger in Debug Mode Register 1 (DMR1) Register 0x3010 */
1962
  // Arc sim --> cpu_state.sprs[SPR_DMR1] &= ~SPR_DMR1_ST;
1963
  if(gdb_read_reg(DMR1_CPU_REG_ADD, &temp_uint32)) printf("Error read from DMR1 register\n");
1964
  temp_uint32 &= ~SPR_DMR1_ST;
1965
  if(gdb_write_reg(DMR1_CPU_REG_ADD, temp_uint32)) printf("Error write to DMR1 register\n");
1966
 
1967
  /* Set traps to be handled by the debug unit in the Debug Stop Register (DSR) Register 0x3014 */
1968
  // Arc sim --> cpu_state.sprs[SPR_DSR]  |= SPR_DSR_TE;
1969
  if(gdb_read_reg(DSR_CPU_REG_ADD, &temp_uint32)) printf("Error read from DSR register\n");
1970
  temp_uint32 |= SPR_DSR_TE;
1971
  if(gdb_write_reg(DSR_CPU_REG_ADD, temp_uint32)) printf("Error write to DSR register\n");
1972
 
1973
  /* Unstall the processor */
1974
  set_stall_state (0);
1975
 
1976
  /* Note the GDB client is now waiting for a reply. */
1977
  rsp.client_waiting = 1;
1978
 
1979
}       /* rsp_continue_generic () */
1980
 
1981
 
1982
/*---------------------------------------------------------------------------*/
1983
/*!Handle a RSP read all registers request
1984
 
1985
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PPC
1986
   (i.e. SPR PPC), NPC (i.e. SPR NPC) and SR (i.e. SPR SR). Each register is
1987
   returned as a sequence of bytes in target endian order.
1988
 
1989
   Each byte is packed as a pair of hex digits.                              */
1990
/*---------------------------------------------------------------------------*/
1991
static void
1992
rsp_read_all_regs (void)
1993
{
1994
  struct rsp_buf  buffer;                       /* Buffer for the reply */
1995
  int             r;                            /* Register index */
1996
  uint32_t   temp_uint32;
1997
 
1998
  // Make sure the processor is stalled
1999
  gdb_ensure_or1k_stalled();
2000
 
2001
  // First set the chain 
2002
  gdb_set_chain(SC_RISC_DEBUG); /* 1 RISC Debug Interface chain */
2003
 
2004
 
2005
  // Read all GPRs
2006
  for (r = 0; r < MAX_GPRS; r++){
2007
 
2008
    err = gdb_read_reg(0x400 + r, &temp_uint32);
2009
    if(err > 0){
2010
      if (DEBUG_GDB) printf("Error %d in gdb_read_reg at reg. %d\n", err, r);
2011
      put_str_packet ("E01");
2012
      return;
2013
    }
2014
    reg2hex (temp_uint32, &(buffer.data[r * 8]));
2015
 
2016
    if (DEBUG_GDB_DUMP_DATA){
2017
      switch(r % 4)
2018
        {
2019
        case 0:
2020
          printf("gpr%02d   0x%08x  ", r, temp_uint32);
2021
          break;
2022
        case 1:
2023
        case 2:
2024
          printf("0x%08x  ", temp_uint32);
2025
          break;
2026
        case 3:
2027
          printf("0x%08x\n", temp_uint32);
2028
          break;
2029
        default:
2030
          break;
2031
        }
2032
    }
2033
 
2034
  }
2035
  /* ---------- PPC ---------- */
2036
  err = gdb_read_reg(PPC_CPU_REG_ADD, &temp_uint32);
2037
  if(err > 0){
2038
        if (DEBUG_GDB) printf("Error %d in gdb_read_reg read --> PPC\n", err);
2039
        put_str_packet ("E01");
2040
        return;
2041
  }
2042
  reg2hex (temp_uint32, &(buffer.data[PPC_REGNUM * 8]));
2043
  if (DEBUG_GDB_DUMP_DATA)      printf("PPC     0x%08x\n", temp_uint32);
2044
  /* ---------- NPC ---------- */
2045
  temp_uint32 = get_npc();
2046
  /*
2047
  err = gdb_read_reg(NPC_CPU_REG_ADD, &temp_uint32);
2048
  if(err > 0){
2049
        if (DEBUG_GDB) printf("Error %d in gdb_read_reg read --> NPC\n", err);
2050
        put_str_packet ("E01");
2051
        return;
2052
  }
2053
  */
2054
  reg2hex (temp_uint32, &(buffer.data[NPC_REGNUM * 8]));
2055
  if (DEBUG_GDB_DUMP_DATA)      printf("NPC     0x%08x\n", temp_uint32);
2056
  /* ---------- SR ---------- */
2057
  err = gdb_read_reg(SR_CPU_REG_ADD, &temp_uint32);
2058
  if(err > 0){
2059
        if (DEBUG_GDB) printf("Error %d in gdb_read_reg read --> SP\n", err);
2060
        put_str_packet ("E01");
2061
        return;
2062
  }
2063
  reg2hex (temp_uint32, &(buffer.data[SR_REGNUM * 8]));
2064
        if (DEBUG_GDB_DUMP_DATA)        printf("SR      0x%08x\n", temp_uint32);
2065
 
2066
  /* Finalize the packet and send it */
2067
  buffer.data[NUM_REGS * 8] = 0;
2068
  buffer.len                = NUM_REGS * 8;
2069
 
2070
  put_packet (&buffer);
2071
        return;
2072
}       /* rsp_read_all_regs () */
2073
 
2074
 
2075
/*---------------------------------------------------------------------------*/
2076
/*!Handle a RSP write all registers request
2077
 
2078
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PPC
2079
   (i.e. SPR PPC), NPC (i.e. SPR NPC) and SR (i.e. SPR SR). Each register is
2080
   supplied as a sequence of bytes in target endian order.
2081
 
2082
   Each byte is packed as a pair of hex digits.
2083
 
2084
   @todo There is no error checking at present. Non-hex chars will generate a
2085
         warning message, but there is no other check that the right amount
2086
         of data is present. The result is always "OK".
2087
 
2088
   @param[in] p_buf  The original packet request.                              */
2089
/*---------------------------------------------------------------------------*/
2090
static void
2091
rsp_write_all_regs (struct rsp_buf *p_buf)
2092
{
2093
  uint32_t  regnum;                             /* Register index */
2094
  // char          valstr[9];           /* Allow for EOS on the string */
2095
 
2096
  // /* Check for valid data */
2097
  // if (0 != (strcmp ("G", p_buf->data)) && (GDB_BUF_MAX != strlen(p_buf->data)))
2098
  // {
2099
  //   fprintf (stderr, "Warning: Failed to recognize RSP write register "
2100
  //      "command: %s\n", p_buf->data);
2101
  //   // put_str_packet ("E01");
2102
  //   return;
2103
  // }
2104
 
2105
  // Make sure the processor is stalled
2106
  gdb_ensure_or1k_stalled();
2107
 
2108
  // First set the chain 
2109
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2110
  if(err > 0){
2111
        if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2112
    put_str_packet ("E01");
2113
        return;
2114
        }
2115
 
2116
  /* ---------- GPRS ---------- */
2117
  for (regnum = 0; regnum < MAX_GPRS; regnum++)
2118
  {
2119
    err = gdb_write_reg(0x400 + regnum, hex2reg (&p_buf->data[regnum * 8 + 1]));
2120
          if(err > 0){
2121
                if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> GPRS\n", err);
2122
            put_str_packet ("E01");
2123
                return;
2124
          }
2125
  }
2126
 
2127
  /* ---------- PPC ---------- */
2128
  err = gdb_write_reg(PPC_CPU_REG_ADD, hex2reg (&p_buf->data[PPC_REGNUM * 8 + 1]));
2129
  if(err > 0){
2130
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> PPC\n", err);
2131
    put_str_packet ("E01");
2132
        return;
2133
  }
2134
  /* ---------- SR ---------- */
2135
  err = gdb_write_reg(SR_CPU_REG_ADD, hex2reg (&p_buf->data[SR_REGNUM * 8 + 1]));
2136
  if(err > 0){
2137
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> SR\n", err);
2138
    put_str_packet ("E01");
2139
        return;
2140
  }
2141
  /* ---------- NPC ---------- */
2142
  set_npc(hex2reg (&p_buf->data[NPC_REGNUM * 8 + 1]));
2143
  /*
2144
  err = gdb_write_reg(NPC_CPU_REG_ADD, hex2reg (&p_buf->data[NPC_REGNUM * 8 + 1]));
2145
  if(err > 0){
2146
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> NPC\n", err);
2147
    put_str_packet ("E01");
2148
        return;
2149
  }
2150
  */
2151
  /* Acknowledge. TODO: We always succeed at present, even if the data was
2152
     defective. */
2153
  put_str_packet ("OK");
2154
}       /* rsp_write_all_regs () */
2155
 
2156
 
2157
/*---------------------------------------------------------------------------*/
2158
/* Handle a RSP read memory (symbolic) request
2159
 
2160
   Syntax is:
2161
 
2162
     m<addr>,<length>:
2163
 
2164
   The response is the bytes, lowest address first, encoded as pairs of hex
2165
   digits.
2166
 
2167
   The length given is the number of bytes to be read.
2168
 
2169
   @note This function reuses p_buf, so trashes the original command.
2170
 
2171
   @param[in] p_buf  The command received                                      */
2172
/*---------------------------------------------------------------------------*/
2173
static void rsp_read_mem (struct rsp_buf *p_buf)
2174
{
2175
  unsigned int    addr;                 /* Where to read the memory */
2176
  int             len;                  /* Number of bytes to read */
2177
  int             off;                  /* Offset into the memory */
2178 46 julius
  uint32_t              temp_uint32 = 0;
2179
  char                                          *rec_buf, *rec_buf_ptr;
2180
  int bytes_per_word = 4; /* Current OR implementation is 4-byte words */
2181
  int i;
2182
  int len_cpy;
2183
  /* Couple of temps we might need when doing aligning/leftover accesses */
2184
  uint32_t tmp_word;
2185
  char *tmp_word_ptr = (char*) &tmp_word;
2186
 
2187 39 julius
 
2188
 
2189
  if (2 != sscanf (p_buf->data, "m%x,%x:", &addr, &len))
2190
  {
2191
    fprintf (stderr, "Warning: Failed to recognize RSP read memory "
2192
       "command: %s\n", p_buf->data);
2193
    put_str_packet ("E01");
2194
    return;
2195
  }
2196
 
2197
  /* Make sure we won't overflow the buffer (2 chars per byte) */
2198
  if ((len * 2) >= GDB_BUF_MAX)
2199
  {
2200
    fprintf (stderr, "Warning: Memory read %s too large for RSP packet: "
2201
       "truncated\n", p_buf->data);
2202
    len = (GDB_BUF_MAX - 1) / 2;
2203
        }
2204
 
2205
  if(!(rec_buf = (char*)malloc(len))) {
2206
    put_str_packet ("E01");
2207
    return;
2208
  }
2209
 
2210
  // Make sure the processor is stalled
2211
  gdb_ensure_or1k_stalled();
2212
 
2213
  // Set chain 5 --> Wishbone Memory chain
2214
  err = gdb_set_chain(SC_WISHBONE);
2215
  if(err){
2216 46 julius
    if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2217
    put_str_packet ("E01");
2218 39 julius
    return;
2219 46 julius
  }
2220
 
2221
  len_cpy = len;
2222
  rec_buf_ptr = rec_buf; // Need to save a copy of pointer
2223
 
2224
  if (addr & 0x3)  // address not aligned at the start
2225
    {
2226
      // Have to read from the word-aligned address first and fetch the bytes
2227
      // we need.
2228
      if (DEBUG_GDB)
2229
        printf("rsp_read_mem: unaligned address read - reading before bytes\n",
2230
               err);
2231
      int num_bytes_to_align = bytes_per_word - (addr & 0x3);
2232
      uint32_t aligned_addr = addr & ~0x3;
2233
 
2234
      if (DEBUG_GDB)
2235
        printf("rsp_read_mem: reading first %d of %d overall, from 0x%.8x\n",
2236
               num_bytes_to_align, len_cpy, aligned_addr);
2237
 
2238
      err = gdb_read_reg(aligned_addr, &tmp_word);
2239
 
2240
      if (DEBUG_GDB) printf("rsp_read_mem: first word 0x%.8x\n", tmp_word);
2241
 
2242
      if(err){
2243
        put_str_packet ("E01");
2244
        return;
2245
      }
2246
 
2247
      // Pack these bytes in first
2248
      if (num_bytes_to_align > len_cpy) num_bytes_to_align = len_cpy;
2249
 
2250
      // Data returns back in big endian format.
2251
      if (DEBUG_GDB_BLOCK_DATA)printf("rsp_read_mem: packing first bytes ");
2252
      i=addr&0x3; int buf_ctr = 0;
2253
      while (buf_ctr < num_bytes_to_align)
2254
        {
2255
          rec_buf_ptr[buf_ctr] = tmp_word_ptr[i];
2256
          if (DEBUG_GDB_BLOCK_DATA)printf("i=%d=0x%x, ", i,
2257
                                          tmp_word_ptr[bytes_per_word-1-i]);
2258
          i++;
2259
          buf_ctr++;
2260 39 julius
        }
2261 46 julius
 
2262
      if (DEBUG_GDB_BLOCK_DATA)printf("\n");
2263
 
2264
      // Adjust our status
2265
      len_cpy -= num_bytes_to_align; addr += num_bytes_to_align;
2266
      rec_buf_ptr += num_bytes_to_align;
2267
    }
2268
  if (len_cpy/bytes_per_word) // Now perform all full word accesses
2269
    {
2270
      int words_to_read = len_cpy/bytes_per_word; // Full words to read
2271
      if (DEBUG_GDB) printf("rsp_read_mem: reading %d words from 0x%.8x\n",
2272
                            words_to_read, addr);
2273
      // Read full data words from Wishbone Memory chain
2274
      err = gdb_read_block(addr, (uint32_t*)rec_buf_ptr,
2275
                           words_to_read*bytes_per_word);
2276
 
2277
      if(err){
2278
        put_str_packet ("E01");
2279
        return;
2280
      }
2281 39 julius
 
2282 46 julius
      // Adjust our status
2283
      len_cpy -= (words_to_read*bytes_per_word);
2284
      addr += (words_to_read*bytes_per_word);
2285
      rec_buf_ptr += (words_to_read*bytes_per_word);
2286
    }
2287
  if (len_cpy) // Leftover bytes
2288
    {
2289
      if (DEBUG_GDB)
2290
        printf("rsp_read_mem: reading %d left-over bytes from 0x%.8x\n",
2291
               len_cpy, addr);
2292
 
2293
      err = gdb_read_reg(addr, &tmp_word);
2294
 
2295
      // Big endian - top byte first!
2296
      for(i=0;i<len_cpy;i++)
2297
        rec_buf_ptr[i] = tmp_word_ptr[i];
2298
 
2299
    }
2300
 
2301
  if (DEBUG_GDB)
2302
    printf("rsp_read_mem: err: %d\n",err);
2303
 
2304
 
2305 39 julius
  if(err){
2306 46 julius
    put_str_packet ("E01");
2307
    return;
2308
  }
2309 39 julius
 
2310
  /* Refill the buffer with the reply */
2311
  for( off = 0 ; off < len ; off ++ ) {
2312 46 julius
    ;
2313
    p_buf->data[(2*off)] = hexchars[((rec_buf[off]&0xf0)>>4)];
2314
    p_buf->data[(2*off)+1] = hexchars[(rec_buf[off]&0x0f)];
2315 39 julius
  }
2316 46 julius
 
2317 39 julius
  if (DEBUG_GDB && (err > 0)) printf("\nError %x\n", err);fflush (stdout);
2318
        free(rec_buf);
2319
  p_buf->data[off * 2] = 0;                      /* End of string */
2320
  p_buf->len           = strlen (p_buf->data);
2321 46 julius
  if (DEBUG_GDB_BLOCK_DATA){
2322
    printf("rsp_read_mem: adr 0x%.8x data: ", addr);
2323
    for(i=0;i<len*2;i++)
2324
      printf("%c",p_buf->data[i]);
2325
    printf("\n");
2326
  }
2327
 
2328 39 julius
  put_packet (p_buf);
2329 46 julius
 
2330 39 julius
}       /* rsp_read_mem () */
2331
 
2332
 
2333
/*---------------------------------------------------------------------------*/
2334
/*!Handle a RSP write memory (symbolic) request  ("M")
2335
 
2336
   Syntax is:
2337
 
2338
     M<addr>,<length>:<data>
2339
 
2340
        Example: M4015cc,2:c320#
2341
          (Write the value 0xc320 to address 0x4015cc.)
2342
 
2343
                An example target response:
2344
                + $OK#
2345
 
2346
   The data is the bytes, lowest address first, encoded as pairs of hex
2347
   digits.
2348
 
2349
   The length given is the number of bytes to be written.
2350
 
2351
   @note This function reuses p_buf, so trashes the original command.
2352
 
2353
   @param[in] p_buf  The command received                                      */
2354
/*---------------------------------------------------------------------------*/
2355
static void
2356
rsp_write_mem (struct rsp_buf *p_buf)
2357
{
2358
  unsigned int    addr;                 /* Where to write the memory */
2359
  int             len;                  /* Number of bytes to write */
2360
  char           *symdat;               /* Pointer to the symboli data */
2361
  int             datlen;               /* Number of digits in symbolic data */
2362
  int             off;                  /* Offset into the memory */
2363
  int             nibc;                 /* Nibbel counter */
2364
        uint32_t   val;
2365
 
2366
  if (2 != sscanf (p_buf->data, "M%x,%x:", &addr, &len))
2367
  {
2368
    fprintf (stderr, "Warning: Failed to recognize RSP write memory "
2369
       "command: %s\n", p_buf->data);
2370
    put_str_packet ("E01");
2371
    return;
2372
  }
2373
 
2374
  /* Find the start of the data and check there is the amount we expect. */
2375
  symdat = (char*) memchr ((const void *)p_buf->data, ':', GDB_BUF_MAX) + 1;
2376
  datlen = p_buf->len - (symdat - p_buf->data);
2377
 
2378
  /* Sanity check */
2379
  if (len * 2 != datlen)
2380
  {
2381
    fprintf (stderr, "Warning: Write of %d digits requested, but %d digits "
2382
       "supplied: packet ignored\n", len * 2, datlen );
2383
    put_str_packet ("E01");
2384
    return;
2385
  }
2386
 
2387
  // Make sure the processor is stalled
2388
  gdb_ensure_or1k_stalled();
2389
 
2390
 
2391
  // Set chain 5 --> Wishbone Memory chain
2392
  err = gdb_set_chain(SC_WISHBONE);
2393
        if(err){
2394
    put_str_packet ("E01");
2395
    return;
2396
        }
2397
 
2398
        val = 0;
2399
        off = 0;
2400
  /* Write the bytes to memory */
2401
  for (nibc = 0; nibc < datlen; nibc++)
2402
  {
2403
                val |= 0x0000000f & hex (symdat[nibc]);
2404
                if(nibc % 8 == 7){
2405
                        err = gdb_write_block(addr + off, &val, 4);
2406
                        if (DEBUG_GDB) printf("Error %x\n", err);fflush (stdout);
2407
                        if(err){
2408
                                put_str_packet ("E01");
2409
                                return;
2410
                        }
2411
                  val = 0;
2412
                        off += 4;
2413
                }
2414
          val <<= 4;
2415
  }
2416
  put_str_packet ("OK");
2417
}       /* rsp_write_mem () */
2418
 
2419
 
2420
/*---------------------------------------------------------------------------*/
2421
/*!Read a single register
2422
 
2423
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PC
2424
   (i.e. SPR NPC) and SR (i.e. SPR SR). The register is returned as a
2425
   sequence of bytes in target endian order.
2426
 
2427
   Each byte is packed as a pair of hex digits.
2428
 
2429
   @param[in] p_buf  The original packet request. Reused for the reply.        */
2430
/*---------------------------------------------------------------------------*/
2431
static void
2432
rsp_read_reg (struct rsp_buf *p_buf)
2433
{
2434
  unsigned int          regnum;
2435
        uint32_t        temp_uint32;
2436
 
2437
  /* Break out the fields from the data */
2438
  if (1 != sscanf (p_buf->data, "p%x", &regnum))
2439
    {
2440
      fprintf (stderr, "Warning: Failed to recognize RSP read register "
2441
               "command: %s\n", p_buf->data);
2442
      put_str_packet ("E01");
2443
      return;
2444
    }
2445
 
2446
  // Make sure the processor is stalled
2447
  gdb_ensure_or1k_stalled();
2448
 
2449
  // First set the chain 
2450
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2451
  if(err > 0){
2452
        if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2453
    put_str_packet ("E01");
2454
        return;
2455
        }
2456 46 julius
 
2457 39 julius
  /* Get the relevant register */
2458
  if (regnum < MAX_GPRS)
2459
    {
2460 46 julius
      err = gdb_read_reg(0x400 + regnum, &temp_uint32);
2461
      if(err > 0){
2462
        if (DEBUG_GDB) printf("Error %d in rsp_read_reg at reg. %d \n", err, regnum);
2463
        put_str_packet ("E01");
2464
        return;
2465
      }
2466
      reg2hex (temp_uint32, p_buf->data);
2467 39 julius
    }
2468
  else if (PPC_REGNUM == regnum)        /* ---------- PPC ---------- */
2469
    {
2470 46 julius
      err = gdb_read_reg(PPC_CPU_REG_ADD, &temp_uint32);
2471
      if(err > 0){
2472
        if (DEBUG_GDB) printf("Error %d in rsp_read_reg read --> PPC\n", err);
2473
        put_str_packet ("E01");
2474
        return;
2475
      }
2476
      reg2hex (temp_uint32, p_buf->data);
2477 39 julius
    }
2478
  else if (NPC_REGNUM == regnum)        /* ---------- NPC ---------- */
2479
    {
2480
      temp_uint32 = get_npc();
2481
      /*
2482 46 julius
        err = gdb_read_reg(NPC_CPU_REG_ADD, &temp_uint32);
2483
        if(err > 0){
2484 39 julius
        if (DEBUG_GDB) printf("Error %d in rsp_read_reg read --> PPC\n", err);
2485
        put_str_packet ("E01");
2486
        return;
2487 46 julius
        }
2488 39 julius
      */
2489
      reg2hex (temp_uint32, p_buf->data);
2490
    }
2491
  else if (SR_REGNUM == regnum)         /* ---------- SR ---------- */
2492
    {
2493
      err = gdb_read_reg(SR_CPU_REG_ADD, &temp_uint32);
2494
      if(err > 0){
2495
        if (DEBUG_GDB) printf("Error %d in rsp_read_reg read --> PPC\n", err);
2496
        put_str_packet ("E01");
2497
        return;
2498
      }
2499
      reg2hex (temp_uint32, p_buf->data);
2500
    }
2501
  else
2502
    {
2503
      /* Error response if we don't know the register */
2504
      fprintf (stderr, "Warning: Attempt to read unknown register 0x%x: "
2505
               "ignored\n", regnum);
2506
      put_str_packet ("E01");
2507
      return;
2508
    }
2509
 
2510
  p_buf->len = strlen (p_buf->data);
2511
  put_packet (p_buf);
2512
 
2513
}       /* rsp_read_reg () */
2514
 
2515
 
2516
/*---------------------------------------------------------------------------*/
2517
/*!Write a single register
2518
 
2519
   The registers follow the GDB sequence for OR1K: GPR0 through GPR31, PC
2520
   (i.e. SPR NPC) and SR (i.e. SPR SR). The register is specified as a
2521
   sequence of bytes in target endian order.
2522
 
2523
   Each byte is packed as a pair of hex digits.
2524
 
2525
   @param[in] p_buf  The original packet request.                              */
2526
/*---------------------------------------------------------------------------*/
2527
static void
2528
rsp_write_reg (struct rsp_buf *p_buf)
2529
{
2530
  unsigned int  regnum;
2531
  char          valstr[9];              /* Allow for EOS on the string */
2532
        // int           err = 0;
2533
 
2534
  /* Break out the fields from the data */
2535
  if (2 != sscanf (p_buf->data, "P%x=%8s", &regnum, valstr))
2536
    {
2537
      fprintf (stderr, "Warning: Failed to recognize RSP write register "
2538
               "command: %s\n", p_buf->data);
2539
      put_str_packet ("E01");
2540
      return;
2541
    }
2542
 
2543
  // Make sure the processor is stalled
2544
  gdb_ensure_or1k_stalled();
2545
 
2546
  // First set the chain 
2547
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2548
  if(err > 0){
2549
        if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2550
    put_str_packet ("E01");
2551
        return;
2552
        }
2553
 
2554
  /* Set the relevant register */
2555
  if (regnum < MAX_GPRS)                                 /* ---------- GPRS ---------- */
2556 46 julius
    {
2557 39 julius
      err = gdb_write_reg(0x400 + regnum, hex2reg (valstr));
2558 46 julius
      if(err > 0){
2559
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> GPRS\n", err);
2560
        put_str_packet ("E01");
2561
        return;
2562
      }
2563 39 julius
    }
2564
  else if (PPC_REGNUM == regnum) /* ---------- PPC ---------- */
2565
    {
2566
      err = gdb_write_reg(PPC_CPU_REG_ADD, hex2reg (valstr));
2567 46 julius
      if(err > 0){
2568
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> PPC\n", err);
2569
        put_str_packet ("E01");
2570
        return;
2571
      }
2572 39 julius
    }
2573
  else if (NPC_REGNUM == regnum) /* ---------- NPC ---------- */
2574
    {
2575
      set_npc(hex2reg (valstr));
2576
      /*
2577
      err = gdb_write_reg(NPC_CPU_REG_ADD, hex2reg (valstr));
2578
      if(err > 0){
2579 46 julius
      if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> NPC\n", err);
2580
      put_str_packet ("E01");
2581
      return;
2582 39 julius
      }
2583
      */
2584
    }
2585
  else if (SR_REGNUM == regnum)  /* ---------- SR ---------- */
2586
    {
2587
      err = gdb_write_reg(SR_CPU_REG_ADD, hex2reg (valstr));
2588 46 julius
      if(err > 0){
2589
        if (DEBUG_GDB) printf("Error %d in rsp_write_reg write --> SR\n", err);
2590
        put_str_packet ("E01");
2591
        return;
2592 39 julius
                  }
2593
    }
2594
  else
2595
    {
2596
      /* Error response if we don't know the register */
2597
      fprintf (stderr, "Warning: Attempt to write unknown register 0x%x: "
2598
               "ignored\n", regnum);
2599
      put_str_packet ("E01");
2600
      return;
2601
    }
2602 46 julius
 
2603 39 julius
  put_str_packet ("OK");
2604 46 julius
 
2605 39 julius
}       /* rsp_write_reg () */
2606
 
2607
 
2608
/*---------------------------------------------------------------------------*/
2609
/*!Handle a RSP query request
2610
 
2611
   @param[in] p_buf  The request                                               */
2612
/*---------------------------------------------------------------------------*/
2613
static void
2614
rsp_query (struct rsp_buf *p_buf)
2615
{
2616
  if (0 == strcmp ("qC", p_buf->data))
2617
    {
2618
      /* Return the current thread ID (unsigned hex). A null response
2619
         indicates to use the previously selected thread. Since we do not
2620
         support a thread concept, this is the appropriate response. */
2621
      put_str_packet ("");
2622
    }
2623
  else if (0 == strncmp ("qCRC", p_buf->data, strlen ("qCRC")))
2624
    {
2625
      /* Return CRC of memory area */
2626
      fprintf (stderr, "Warning: RSP CRC query not supported\n");
2627
      put_str_packet ("E01");
2628
    }
2629
  else if (0 == strcmp ("qfThreadInfo", p_buf->data))
2630
    {
2631
      /* Return info about active threads. We return just '-1' */
2632
      put_str_packet ("m-1");
2633
    }
2634
  else if (0 == strcmp ("qsThreadInfo", p_buf->data))
2635
    {
2636
      /* Return info about more active threads. We have no more, so return the
2637
         end of list marker, 'l' */
2638
      put_str_packet ("l");
2639
    }
2640
  else if (0 == strncmp ("qGetTLSAddr:", p_buf->data, strlen ("qGetTLSAddr:")))
2641
    {
2642
      /* We don't support this feature */
2643
      put_str_packet ("");
2644
    }
2645
  else if (0 == strncmp ("qL", p_buf->data, strlen ("qL")))
2646
    {
2647
      /* Deprecated and replaced by 'qfThreadInfo' */
2648
      fprintf (stderr, "Warning: RSP qL deprecated: no info returned\n");
2649
      put_str_packet ("qM001");
2650
    }
2651
  else if (0 == strcmp ("qOffsets", p_buf->data))
2652
    {
2653
      /* Report any relocation */
2654
      put_str_packet ("Text=0;Data=0;Bss=0");
2655
    }
2656
  else if (0 == strncmp ("qP", p_buf->data, strlen ("qP")))
2657
    {
2658
      /* Deprecated and replaced by 'qThreadExtraInfo' */
2659
      fprintf (stderr, "Warning: RSP qP deprecated: no info returned\n");
2660
      put_str_packet ("");
2661
    }
2662
  else if (0 == strncmp ("qRcmd,", p_buf->data, strlen ("qRcmd,")))
2663
    {
2664
      /* This is used to interface to commands to do "stuff" */
2665
      rsp_command (p_buf);
2666
    }
2667
  else if (0 == strncmp ("qSupported", p_buf->data, strlen ("qSupported")))
2668
    {
2669
      /* Report a list of the features we support. For now we just ignore any
2670
                                 supplied specific feature queries, but in the future these may be
2671
                                 supported as well. Note that the packet size allows for 'G' + all the
2672
                                 registers sent to us, or a reply to 'g' with all the registers and an
2673
                                 EOS so the buffer is a well formed string. */
2674
      setup_or32();     // setup cpu
2675
      char  reply[GDB_BUF_MAX];
2676
      sprintf (reply, "PacketSize=%x", GDB_BUF_MAX);
2677
      put_str_packet (reply);
2678
    }
2679
  else if (0 == strncmp ("qSymbol:", p_buf->data, strlen ("qSymbol:")))
2680
    {
2681
      /* Offer to look up symbols. Nothing we want (for now). TODO. This just
2682
         ignores any replies to symbols we looked up, but we didn't want to
2683
         do that anyway! */
2684
      put_str_packet ("OK");
2685
    }
2686
  else if (0 == strncmp ("qThreadExtraInfo,", p_buf->data,
2687
                         strlen ("qThreadExtraInfo,")))
2688
    {
2689
      /* Report that we are runnable, but the text must be hex ASCI
2690
         digits. For now do this by steam, reusing the original packet */
2691
      sprintf (p_buf->data, "%02x%02x%02x%02x%02x%02x%02x%02x%02x",
2692
               'R', 'u', 'n', 'n', 'a', 'b', 'l', 'e', 0);
2693
      p_buf->len = strlen (p_buf->data);
2694
      put_packet (p_buf);
2695
    }
2696
  else if (0 == strncmp ("qXfer:", p_buf->data, strlen ("qXfer:")))
2697
    {
2698
      /* For now we support no 'qXfer' requests, but these should not be
2699
         expected, since they were not reported by 'qSupported' */
2700
      fprintf (stderr, "Warning: RSP 'qXfer' not supported: ignored\n");
2701
      put_str_packet ("");
2702
    }
2703
  else
2704
    {
2705
      fprintf (stderr, "Unrecognized RSP query: ignored\n");
2706
    }
2707
}       /* rsp_query () */
2708
 
2709
 
2710
/*---------------------------------------------------------------------------*/
2711
/*!Handle a RSP qRcmd request
2712
 
2713
  The actual command follows the "qRcmd," in ASCII encoded to hex
2714
 
2715
   @param[in] p_buf  The request in full                                       */
2716
/*---------------------------------------------------------------------------*/
2717
static void
2718
rsp_command (struct rsp_buf *p_buf)
2719
{
2720
  char  cmd[GDB_BUF_MAX];
2721
  unsigned int      regno;
2722
  uint32_t              temp_uint32;
2723
 
2724
  hex2ascii (cmd, &(p_buf->data[strlen ("qRcmd,")]));
2725
 
2726
  /* Work out which command it is */
2727
  if (0 == strncmp ("readspr ", cmd, strlen ("readspr")))
2728
  {
2729
    /* Parse and return error if we fail */
2730
    if( 1 != sscanf (cmd, "readspr %4x", &regno))
2731
      {
2732
        fprintf (stderr, "Warning: qRcmd %s not recognized: ignored\n", cmd);
2733
        put_str_packet ("E01");
2734
        return;
2735
      }
2736
 
2737
    /* SPR out of range */
2738
    if (regno > MAX_SPRS)
2739
      {
2740
        fprintf (stderr, "Warning: qRcmd readspr %x too large: ignored\n",
2741
                 regno);
2742
        put_str_packet ("E01");
2743
        return;
2744
      }
2745
 
2746
    /* Construct the reply */
2747
 
2748
    // Make sure the processor is stalled
2749
    gdb_ensure_or1k_stalled();
2750
 
2751
    // First set the chain 
2752
    gdb_set_chain(SC_RISC_DEBUG);       /* 1 RISC Debug Interface chain */
2753
 
2754
    // special case for NPC
2755
    if(regno == NPC_CPU_REG_ADD)
2756
      temp_uint32 = get_npc();
2757
    else
2758
      {
2759
        err = gdb_read_reg(regno, &temp_uint32);
2760
        if(err > 0){
2761
          if (DEBUG_GDB) printf("Error %d in rsp_command at reg. %x \n", err, regno);
2762
        }
2763
        else{
2764
          reg2hex (temp_uint32, cmd);
2765
          if (DEBUG_GDB) printf("Error %d Command readspr Read reg. %x = 0x%08x\n", err, regno, temp_uint32);
2766
        }
2767
      }
2768
 
2769
    // pack the result into the buffer to send back
2770
    sprintf (cmd, "%8x", (unsigned int)temp_uint32);
2771
    ascii2hex (p_buf->data, cmd);
2772
    p_buf->len = strlen (p_buf->data);
2773
    put_packet (p_buf);
2774
  }
2775
  else if (0 == strncmp ("writespr ", cmd, strlen ("writespr")))
2776
    {
2777
      unsigned int       regno;
2778
      uint32_t  val;
2779
 
2780
      /* Parse and return error if we fail */
2781
      if( 2 != sscanf (cmd, "writespr %4x %8x", &regno, &val))
2782
        {
2783
          fprintf (stderr, "Warning: qRcmd %s not recognized: ignored\n",
2784
                   cmd);
2785
          put_str_packet ("E01");
2786
          return;
2787
        }
2788
 
2789
      /* SPR out of range */
2790
      if (regno > MAX_SPRS)
2791
        {
2792
          fprintf (stderr, "Warning: qRcmd writespr %x too large: ignored\n",
2793
                   regno);
2794
          put_str_packet ("E01");
2795
          return;
2796
        }
2797
 
2798
      // Make sure the processor is stalled
2799
      gdb_ensure_or1k_stalled();
2800
 
2801
      // First set the chain 
2802
      gdb_set_chain(SC_RISC_DEBUG);     /* 1 RISC Debug Interface chain */
2803
 
2804
      /* set the relevant register */
2805
      // special case for NPC
2806
      if(regno == NPC_CPU_REG_ADD)
2807
        set_npc(val);
2808
      else
2809
        {
2810
 
2811
          err = gdb_write_reg(regno, val);
2812
          if(err > 0){
2813
            if (DEBUG_GDB) printf("Error %d in rsp_command write Reg. %x = 0x%08x\n", err, regno, val);
2814
            put_str_packet ("E01");
2815
            return;
2816
          }
2817
          else{
2818
            if (DEBUG_GDB) printf("Error %d Command writespr Write reg. %x = 0x%08x\n", err, regno, val);
2819
          }
2820
        }
2821
      put_str_packet ("OK");
2822
    }
2823
}       /* rsp_command () */
2824
 
2825
 
2826
/*---------------------------------------------------------------------------*/
2827
/*!Handle a RSP set request
2828
 
2829
   @param[in] p_buf  The request                                               */
2830
/*---------------------------------------------------------------------------*/
2831
static void
2832
rsp_set (struct rsp_buf *p_buf)
2833
{
2834
  if (0 == strncmp ("QPassSignals:", p_buf->data, strlen ("QPassSignals:")))
2835
    {
2836
      /* Passing signals not supported */
2837
      put_str_packet ("");
2838
    }
2839
  else if ((0 == strncmp ("QTDP",    p_buf->data, strlen ("QTDP")))   ||
2840
           (0 == strncmp ("QFrame",  p_buf->data, strlen ("QFrame"))) ||
2841
           (0 == strcmp  ("QTStart", p_buf->data))                    ||
2842
           (0 == strcmp  ("QTStop",  p_buf->data))                    ||
2843
           (0 == strcmp  ("QTinit",  p_buf->data))                    ||
2844
           (0 == strncmp ("QTro",    p_buf->data, strlen ("QTro"))))
2845
    {
2846
      /* All tracepoint features are not supported. This reply is really only
2847
         needed to 'QTDP', since with that the others should not be
2848
         generated. */
2849
      put_str_packet ("");
2850
    }
2851
  else
2852
    {
2853
      fprintf (stderr, "Unrecognized RSP set request: ignored\n");
2854
    }
2855
}       /* rsp_set () */
2856
 
2857
 
2858
/*---------------------------------------------------------------------------*/
2859
/*!Handle a RSP restart request
2860
 
2861
   For now we just put the program counter back to the one used with the last
2862
   vRun request. There is no point in unstalling the processor, since we'll
2863
   never get control back.                                                   */
2864
/*---------------------------------------------------------------------------*/
2865
static void
2866
rsp_restart (void)
2867
{
2868
  // Make sure the processor is stalled
2869
  gdb_ensure_or1k_stalled();
2870
 
2871
  // First set the chain 
2872
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2873
  if(err > 0){
2874
        if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
2875
    put_str_packet ("E01");
2876
        return;
2877
        }
2878
        // OR32 Arc sim equivalent --> set_npc (rsp.start_addr);
2879
  /* Set NPC to reset vector 0x100 */
2880
  set_npc(rsp.start_addr);
2881
  /*
2882
  err = gdb_write_reg(NPC_CPU_REG_ADD, rsp.start_addr);
2883
  if(err > 0){
2884
        if (DEBUG_GDB) printf("Error %d in rsp_restart write Reg. %x = 0x%08x\n", err, NPC_CPU_REG_ADD, rsp.start_addr);
2885
    put_str_packet ("E01");
2886
        return;
2887
  }
2888
 
2889
  else{
2890
  if (DEBUG_GDB) printf("Error %d Command Reset. Set NPC to Start vector %x = 0x%08x\n", err, NPC_CPU_REG_ADD, rsp.start_addr);
2891
  }
2892
  */
2893
}       /* rsp_restart () */
2894
 
2895
 
2896
/*---------------------------------------------------------------------------*/
2897
/*!Handle a RSP step request
2898
 
2899
   Parse the command to see if there is an address. Uses the underlying
2900
   generic step function, with EXCEPT_NONE.
2901
 
2902
   @param[in] p_buf  The full step packet                          */
2903
/*---------------------------------------------------------------------------*/
2904
static void
2905
rsp_step (struct rsp_buf *p_buf)
2906
{
2907
  uint32_t  addr;               /* The address to step from, if any */
2908
 
2909
  // Make sure the processor is stalled
2910
  gdb_ensure_or1k_stalled();
2911
 
2912
  // First set the chain 
2913
  err = gdb_set_chain(SC_RISC_DEBUG);   /* 1 RISC Debug Interface chain */
2914
  if(err > 0){
2915
        printf("Error %d to set RISC Debug Interface chain in the STEP command 's'\n", err);
2916
    rsp_client_close ();
2917
    return;
2918
        }
2919
 
2920
  if (0 == strcmp ("s", p_buf->data))
2921
  {
2922
    // Arc Sim Code -->   addr = cpu_state.pc;  /* Default uses current NPC */
2923
    /* ---------- Npc ---------- */
2924
    addr = get_npc();
2925
    /*
2926
    err = gdb_read_reg(NPC_CPU_REG_ADD, &addr);
2927
    if(err > 0){
2928
      printf("Error %d to read NPC in the STEP command 's'\n", err);
2929
      rsp_client_close ();
2930
      return;
2931
    }
2932
    */
2933
  }
2934
  else if (1 != sscanf (p_buf->data, "s%x", &addr))
2935
  {
2936
    fprintf (stderr,
2937
             "Warning: RSP step address %s not recognized: ignored\n",
2938
             p_buf->data);
2939
 
2940
    // Arc Sim Code -->   addr = cpu_state.pc;  /* Default uses current NPC */
2941
    /* ---------- NPC ---------- */
2942
    addr = get_npc();
2943
    /*
2944
    err = gdb_read_reg(NPC_CPU_REG_ADD, &addr);
2945
    if(err > 0){
2946
      printf("Error %d to read NPC in the STEP command 's'\n", err);
2947
      rsp_client_close ();
2948
      return;
2949
    }
2950
    */
2951
  }
2952
 
2953
  //if (DEBUG_GDB) printf("rsp_step() --> Read NPC = 0x%08x\n", addr);
2954
  rsp_step_generic (addr, EXCEPT_NONE);
2955
 
2956
}       /* rsp_step () */
2957
 
2958
 
2959
/*---------------------------------------------------------------------------*/
2960
/*!Handle a RSP step with signal request
2961
 
2962
   Currently null. Will use the underlying generic step function.
2963
 
2964
   @param[in] p_buf  The full step with signal packet              */
2965
/*---------------------------------------------------------------------------*/
2966
static void
2967
rsp_step_with_signal (struct rsp_buf *p_buf)
2968
{
2969
  printf ("RSP step with signal '%s' received\n", p_buf->data);
2970
 
2971
}       /* rsp_step_with_signal () */
2972
 
2973
 
2974
/*---------------------------------------------------------------------------*/
2975
/*!Generic processing of a step request
2976
 
2977
   The signal may be EXCEPT_NONE if there is no exception to be
2978
   handled. Currently the exception is ignored.
2979
 
2980
   The single step flag is set in the debug registers and then the processor
2981
   is unstalled.
2982
 
2983
   @param[in] addr    Address from which to step
2984
   @param[in] except  The exception to use (if any)                          */
2985
/*---------------------------------------------------------------------------*/
2986
static void
2987
rsp_step_generic (uint32_t  addr,
2988
                  uint32_t  except)
2989
{
2990
  uint32_t              temp_uint32;
2991
 
2992
  /* Set the address as the value of the next program counter */
2993
 
2994
  set_npc (addr);
2995
 
2996
  /* Clear Debug Reason Register (DRR) 0x3015 */
2997
  // Arc sim --> cpu_state.sprs[SPR_DRR]   = 0;
2998
  if(gdb_write_reg(DRR_CPU_REG_ADD, 0)) printf("Error write to DRR register\n");
2999
 
3000
  /* Clear watchpoint break generation in Debug Mode Register 2 (DMR2) 0x3011 */
3001
  // Arc sim --> cpu_state.sprs[SPR_DMR2] &= ~SPR_DMR2_WGB;
3002
  if(gdb_read_reg(DMR2_CPU_REG_ADD, &temp_uint32)) printf("Error read from DMR2 register\n");
3003
  temp_uint32 &= ~SPR_DMR2_WGB;
3004
  if(gdb_write_reg(DMR2_CPU_REG_ADD, temp_uint32)) printf("Error write to DMR2 register\n");
3005
 
3006
  /* Set the single step trigger in Debug Mode Register 1 (DMR1) Register 0x3010 */
3007
  // Arc sim --> cpu_state.sprs[SPR_DMR1] |= SPR_DMR1_ST;
3008
  if(gdb_read_reg(DMR1_CPU_REG_ADD, &temp_uint32)) printf("Error read from DMR1 register\n");
3009
  temp_uint32 |= SPR_DMR1_ST;
3010
  if(gdb_write_reg(DMR1_CPU_REG_ADD, temp_uint32)) printf("Error write to DMR1 register\n");
3011
 
3012
  /* Set traps to be handled by the debug unit in the Debug Stop Register (DSR) Register 0x3014 */
3013
  // Arc sim --> cpu_state.sprs[SPR_DSR]  |= SPR_DSR_TE;
3014
  if(gdb_read_reg(DSR_CPU_REG_ADD, &temp_uint32)) printf("Error read from DSR register\n");
3015
  temp_uint32 |= SPR_DSR_TE;
3016
  if(gdb_write_reg(DSR_CPU_REG_ADD, temp_uint32)) printf("Error write to DSR register\n");
3017
 
3018
  /* Unstall the processor */
3019
  set_stall_state (0);
3020
 
3021
  /* Note the GDB client is now waiting for a reply. */
3022
  rsp.client_waiting = 1;
3023
 
3024
}       /* rsp_step_generic () */
3025
 
3026
 
3027
/*---------------------------------------------------------------------------*/
3028
/*!Handle a RSP 'v' packet
3029
 
3030
   These are commands associated with executing the code on the target
3031
 
3032
   @param[in] p_buf  The request                                               */
3033
/*---------------------------------------------------------------------------*/
3034
static void
3035
rsp_vpkt (struct rsp_buf *p_buf)
3036
{
3037
  if (0 == strncmp ("vAttach;", p_buf->data, strlen ("vAttach;")))
3038
    {
3039
      /* Attaching is a null action, since we have no other process. We just
3040
         return a stop packet (using TRAP) to indicate we are stopped. */
3041
      put_str_packet ("S05");
3042
      return;
3043
    }
3044
  else if (0 == strcmp ("vCont?", p_buf->data))
3045
    {
3046
      /* For now we don't support this. */
3047
      put_str_packet ("");
3048
      return;
3049
    }
3050
  else if (0 == strncmp ("vCont", p_buf->data, strlen ("vCont")))
3051
    {
3052
      /* This shouldn't happen, because we've reported non-support via vCont?
3053
         above */
3054
      fprintf (stderr, "Warning: RSP vCont not supported: ignored\n" );
3055
      return;
3056
    }
3057
  else if (0 == strncmp ("vFile:", p_buf->data, strlen ("vFile:")))
3058
    {
3059
      /* For now we don't support this. */
3060
      fprintf (stderr, "Warning: RSP vFile not supported: ignored\n" );
3061
      put_str_packet ("");
3062
      return;
3063
    }
3064
  else if (0 == strncmp ("vFlashErase:", p_buf->data, strlen ("vFlashErase:")))
3065
    {
3066
      /* For now we don't support this. */
3067
      fprintf (stderr, "Warning: RSP vFlashErase not supported: ignored\n" );
3068
      put_str_packet ("E01");
3069
      return;
3070
    }
3071
  else if (0 == strncmp ("vFlashWrite:", p_buf->data, strlen ("vFlashWrite:")))
3072
    {
3073
      /* For now we don't support this. */
3074
      fprintf (stderr, "Warning: RSP vFlashWrite not supported: ignored\n" );
3075
      put_str_packet ("E01");
3076
      return;
3077
    }
3078
  else if (0 == strcmp ("vFlashDone", p_buf->data))
3079
    {
3080
      /* For now we don't support this. */
3081
      fprintf (stderr, "Warning: RSP vFlashDone not supported: ignored\n" );
3082
      put_str_packet ("E01");
3083
      return;
3084
    }
3085
  else if (0 == strncmp ("vRun;", p_buf->data, strlen ("vRun;")))
3086
    {
3087
      /* We shouldn't be given any args, but check for this */
3088
      if (p_buf->len > (int) strlen ("vRun;"))
3089
        {
3090
          fprintf (stderr, "Warning: Unexpected arguments to RSP vRun "
3091
                   "command: ignored\n");
3092
        }
3093
 
3094
      /* Restart the current program. However unlike a "R" packet, "vRun"
3095
         should behave as though it has just stopped. We use signal
3096
         5 (TRAP). */
3097
      rsp_restart ();
3098
      put_str_packet ("S05");
3099
    }
3100
  else
3101
    {
3102
      fprintf (stderr, "Warning: Unknown RSP 'v' packet type %s: ignored\n",
3103
               p_buf->data);
3104
      put_str_packet ("E01");
3105
      return;
3106
    }
3107
}       /* rsp_vpkt () */
3108
 
3109
 
3110
/*---------------------------------------------------------------------------*/
3111
/*!Handle a RSP write memory (binary) request
3112
 
3113
   Syntax is:
3114
 
3115
     X<addr>,<length>:
3116
 
3117
   Followed by the specified number of bytes as raw binary. Response should be
3118
   "OK" if all copied OK, E<nn> if error <nn> has occurred.
3119
 
3120
   The length given is the number of bytes to be written. However the number
3121
   of data bytes may be greater, since '#', '$' and '}' are escaped by
3122
   preceding them by '}' and oring with 0x20.
3123
 
3124
   @param[in] p_buf  The command received                                      */
3125
/*---------------------------------------------------------------------------*/
3126
static void
3127
rsp_write_mem_bin (struct rsp_buf *p_buf)
3128
{
3129
  unsigned int  addr;                   /* Where to write the memory */
3130
  int           len;                    /* Number of bytes to write */
3131 46 julius
  char          *bindat, *bindat_ptr;   /* Pointer to the binary data */
3132 39 julius
  int           off = 0; /* Offset to start of binary data */
3133
  int           newlen;         /* Number of bytes in bin data */
3134 46 julius
  int i;
3135
  int bytes_per_word = 4; /* Current OR implementation is 4-byte words */
3136 39 julius
 
3137
  if (2 != sscanf (p_buf->data, "X%x,%x:", &addr, &len))
3138
    {
3139
      fprintf (stderr, "Warning: Failed to recognize RSP write memory "
3140
               "command: %s\n", p_buf->data);
3141
      put_str_packet ("E01");
3142
      return;
3143
    }
3144
 
3145
  /* Find the start of the data and "unescape" it */
3146
  bindat = p_buf->data;
3147
  while(off < GDB_BUF_MAX){
3148
        if(bindat[off] == ':'){
3149
                        bindat = bindat + off + 1;
3150
                        off++;
3151
                        break;
3152
                }
3153
                off++;
3154
  }
3155
        if(off >= GDB_BUF_MAX){
3156
          put_str_packet ("E01");
3157
                return;
3158
        }
3159
 
3160
  newlen = rsp_unescape (bindat, p_buf->len - off);
3161
 
3162
  /* Sanity check */
3163
  if (newlen != len)
3164
  {
3165
    int  minlen = len < newlen ? len : newlen;
3166
 
3167
    fprintf (stderr, "Warning: Write of %d bytes requested, but %d bytes "
3168
       "supplied. %d will be written\n", len, newlen, minlen);
3169
    len = minlen;
3170
  }
3171
 
3172
  // Make sure the processor is stalled
3173
  gdb_ensure_or1k_stalled();
3174
 
3175
  // Set chain 5 --> Wishbone Memory chain
3176
  err = gdb_set_chain(SC_WISHBONE);
3177
  if(err){
3178
    put_str_packet ("E01");
3179
    return;
3180
  }
3181
 
3182
  /* Write the bytes to memory */
3183
  if (len)
3184
    {
3185 46 julius
      //swap_buf(bindat, len);
3186 39 julius
      if (DEBUG_GDB_BLOCK_DATA){
3187
        uint32_t  temp_uint32;
3188
        for (off = 0; off < len; off++){
3189
          temp_uint32 = (temp_uint32 << 8) | (0x000000ff & bindat[off]);
3190
          if((off %4 ) == 3){
3191 46 julius
            //temp_uint32 = htonl(temp_uint32);
3192 39 julius
          }
3193
          switch(off % 16)
3194
            {
3195
            case 3:
3196
              printf("Add 0x%08x   Data 0x%08x  ", addr + off - 3, temp_uint32);
3197
              break;
3198
            case 7:
3199
            case 11:
3200
              printf("0x%08x  ", temp_uint32);
3201
              break;
3202
            case 15:
3203
              printf("0x%08x\n", temp_uint32);
3204
              break;
3205
            default:
3206
              break;
3207
            }
3208
          if ((len - off == 1) && (off % 16) < 15) printf("\n");
3209
        }
3210
      }
3211
 
3212 46 julius
      bindat_ptr = bindat; // Copy pointer so we don't trash the original.
3213
      if (addr & 0x3) // not perfectly aligned at beginning - fix
3214
        {
3215
          if (DEBUG_GDB) {
3216
            printf("rsp_write_mem_bin: address not word aligned: 0x%.8x\n",
3217
                   addr);fflush (stdout);
3218
          }
3219
          // Write enough to align us
3220
          int bytes_to_write = bytes_per_word - (addr&0x3);
3221
          if (bytes_to_write > len) bytes_to_write = len; // case of writing 1 byte to adr 0x1
3222
          if (DEBUG_GDB) {
3223
            printf("rsp_write_mem_bin: writing %d bytes of len (%d)\n",
3224
                   bytes_to_write, len);fflush (stdout);
3225
          }
3226
 
3227
          for (i=0;i<bytes_to_write;i++)
3228
            err = gdb_write_byte(addr+i, (uint8_t) bindat_ptr[i]);
3229
 
3230
          addr += bytes_to_write; bindat_ptr += bytes_to_write;
3231
          len -= bytes_to_write;
3232
          if (DEBUG_GDB){
3233
            printf("rsp_write_mem_bin: address should now be word aligned: 0x%.8x\n", addr);
3234
            fflush (stdout);
3235
          }
3236
        }
3237
      if ((len > 3) && !err) // now write full words, if we can
3238
        {
3239
          int words_to_write = len/bytes_per_word;
3240
          if (DEBUG_GDB)
3241
            printf("rsp_write_mem_bin: writing %d words from 0x%x, len %d bytes\n",
3242
                   words_to_write, addr, len);fflush (stdout);
3243
 
3244
          err = gdb_write_block(addr, (uint32_t*)bindat_ptr,
3245
                                (words_to_write*bytes_per_word));
3246
 
3247
          addr+=(words_to_write*bytes_per_word);
3248
          bindat_ptr+=(words_to_write*bytes_per_word);
3249
          len-=(words_to_write*bytes_per_word);
3250
        }
3251
      if (len && !err)  // leftover words. Write them out
3252
        {
3253
          if (DEBUG_GDB)
3254
            printf("rsp_write_mem_bin: writing remainder %d bytes to 0x%.8x\n",
3255
                   len, addr);fflush (stdout);
3256
 
3257
          for (i=0;i<len;i++)
3258
            err = gdb_write_byte(addr+i, (uint8_t) bindat_ptr[i]);
3259
        }
3260
 
3261 39 julius
      if(err){
3262
        put_str_packet ("E01");
3263
        return;
3264
      }
3265 46 julius
 
3266 39 julius
      if (DEBUG_GDB) printf("Error %x\n", err);fflush (stdout);
3267
    }
3268
  put_str_packet ("OK");
3269 46 julius
 
3270 39 julius
}       /* rsp_write_mem_bin () */
3271
 
3272
 
3273
/*---------------------------------------------------------------------------*/
3274
/*!Handle a RSP remove breakpoint or matchpoint request
3275
 
3276
   For now only memory breakpoints are implemented, which are implemented by
3277
   substituting a breakpoint at the specified address. The implementation must
3278
   cope with the possibility of duplicate packets.
3279
 
3280
   @todo This doesn't work with icache/immu yet
3281
 
3282
   @param[in] p_buf  The command received                                      */
3283
/*---------------------------------------------------------------------------*/
3284
static void
3285
rsp_remove_matchpoint (struct rsp_buf *p_buf)
3286
{
3287
  enum mp_type       type;              /* What sort of matchpoint */
3288
  uint32_t  addr;               /* Address specified */
3289
  int                len;                       /* Matchpoint length (not used) */
3290
  struct mp_entry   *mpe;                       /* Info about the replaced instr */
3291
 
3292
  /* Break out the instruction */
3293
  if (3 != sscanf (p_buf->data, "z%1d,%x,%1d", (int *)&type, &addr, &len))
3294
    {
3295
      fprintf (stderr, "Warning: RSP matchpoint deletion request not "
3296
               "recognized: ignored\n");
3297
      put_str_packet ("E01");
3298
      return;
3299
    }
3300
 
3301
  /* Sanity check that the length is 4 */
3302
  if (4 != len)
3303
    {
3304
      fprintf (stderr, "Warning: RSP matchpoint deletion length %d not "
3305
               "valid: 4 assumed\n", len);
3306
      len = 4;
3307
    }
3308
 
3309
  /* Sort out the type of matchpoint */
3310
  switch (type)
3311
    {
3312
    case BP_MEMORY:
3313
      /* Memory breakpoint - replace the original instruction. */
3314
      mpe = mp_hash_delete (type, addr);
3315
 
3316
      /* If the BP hasn't yet been deleted, put the original instruction
3317
                         back. Don't forget to free the hash table entry afterwards. */
3318
                        if (NULL != mpe)
3319
                        {
3320
                          // Arc Sim Code -->   set_program32 (addr, mpe->instr);
3321
                          // Make sure the processor is stalled
3322
                          gdb_ensure_or1k_stalled();
3323
 
3324
                          // Set chain 5 --> Wishbone Memory chain
3325
                          err = gdb_set_chain(SC_WISHBONE);
3326
                                if(err){
3327
                                        put_str_packet ("E01");
3328
                                  return;
3329
                                }
3330
                                err = gdb_write_block(addr, &mpe->instr, 4);
3331
                                if(err){
3332
                                        put_str_packet ("E01");
3333
                                  return;
3334
                                }
3335
                          free (mpe);
3336
                        }
3337
 
3338
      put_str_packet ("OK");
3339
      return;
3340
 
3341
    case BP_HARDWARE:
3342
      put_str_packet ("");              /* Not supported */
3343
      return;
3344
 
3345
    case WP_WRITE:
3346
      put_str_packet ("");              /* Not supported */
3347
      return;
3348
 
3349
    case WP_READ:
3350
      put_str_packet ("");              /* Not supported */
3351
      return;
3352
 
3353
    case WP_ACCESS:
3354
      put_str_packet ("");              /* Not supported */
3355
      return;
3356
 
3357
    default:
3358
      fprintf (stderr, "Warning: RSP matchpoint type %d not "
3359
               "recognized: ignored\n", type);
3360
      put_str_packet ("E01");
3361
      return;
3362
 
3363
    }
3364
}       /* rsp_remove_matchpoint () */
3365
 
3366
 
3367
/*---------------------------------------------------------------------------*/
3368
/*!Handle a RSP insert breakpoint or matchpoint request
3369
 
3370
   For now only memory breakpoints are implemented, which are implemented by
3371
   substituting a breakpoint at the specified address. The implementation must
3372
   cope with the possibility of duplicate packets.
3373
 
3374
   @todo This doesn't work with icache/immu yet
3375
 
3376
   @param[in] p_buf  The command received                                      */
3377
/*---------------------------------------------------------------------------*/
3378
static void
3379
rsp_insert_matchpoint (struct rsp_buf *p_buf)
3380
{
3381
  enum mp_type       type;              /* What sort of matchpoint */
3382
  uint32_t  addr;               /* Address specified */
3383
  int                len;               /* Matchpoint length (not used) */
3384
        uint32_t      instr;
3385
 
3386
  /* Break out the instruction */
3387
  if (3 != sscanf (p_buf->data, "Z%1d,%x,%1d", (int *)&type, &addr, &len))
3388
    {
3389
      fprintf (stderr, "Warning: RSP matchpoint insertion request not "
3390
               "recognized: ignored\n");
3391
      put_str_packet ("E01");
3392
      return;
3393
    }
3394
 
3395
  /* Sanity check that the length is 4 */
3396
  if (4 != len)
3397
    {
3398
      fprintf (stderr, "Warning: RSP matchpoint insertion length %d not "
3399
               "valid: 4 assumed\n", len);
3400
      len = 4;
3401
    }
3402
 
3403
  /* Sort out the type of matchpoint */
3404
  switch (type)
3405
    {
3406
    case BP_MEMORY:             // software-breakpoint Z0  break
3407
      /* Memory breakpoint - substitute a TRAP instruction */
3408
      // Make sure the processor is stalled
3409
      gdb_ensure_or1k_stalled();
3410
 
3411
      // Set chain 5 --> Wishbone Memory chain
3412
      gdb_set_chain(SC_WISHBONE);
3413
 
3414
      // Read the data from Wishbone Memory chain
3415
      // Arc Sim Code -->   mp_hash_add (type, addr, eval_direct32 (addr, 0, 0));
3416
      gdb_read_block(addr, &instr, 4);
3417
 
3418
      mp_hash_add (type, addr, instr);
3419 46 julius
 
3420
      //instr = OR1K_TRAP_INSTR;
3421
      instr = ntohl(OR1K_TRAP_INSTR);// Endianess of this needs to be swapped -jb
3422 39 julius
      err = gdb_write_block(addr, &instr, 4);
3423
      if(err){
3424
        put_str_packet ("E01");
3425
        return;
3426
      }
3427
      put_str_packet ("OK");
3428
      return;
3429
 
3430
    case BP_HARDWARE:   // hardware-breakpoint Z1  hbreak
3431
      put_str_packet ("");              /* Not supported */
3432
      return;
3433
 
3434
    case WP_WRITE:              // write-watchpoint    Z2  watch                                                                                                                                                        
3435
      put_str_packet ("");              /* Not supported */
3436
      return;
3437
 
3438
    case WP_READ:                       // read-watchpoint     Z3  rwatch
3439
      put_str_packet ("");              /* Not supported */
3440
      return;
3441
 
3442
    case WP_ACCESS:             // access-watchpoint   Z4  awatch
3443
      put_str_packet ("");              /* Not supported */
3444
      return;
3445
 
3446
    default:
3447
      fprintf (stderr, "Warning: RSP matchpoint type %d not "
3448
               "recognized: ignored\n", type);
3449
      put_str_packet ("E01");
3450
      return;
3451
    }
3452
}       /* rsp_insert_matchpoint () */
3453
 
3454
 
3455
/*---------------------------------------------------------------------------
3456
Setup the or32 to init state
3457
 
3458
---------------------------------------------------------------------------*/
3459
void setup_or32(void)
3460
{
3461
  uint32_t              temp_uint32;
3462
  // First set the chain 
3463
  err = gdb_set_chain(SC_REGISTER);     /* 4 Register Chain */
3464
  if(err > 0){
3465
    if (DEBUG_GDB) printf("Error %d in gdb_set_chain\n", err);
3466
  }
3467
  if(gdb_read_reg(0x04, &temp_uint32)) printf("Error read from register\n");
3468
  if (DEBUG_GDB) printf("Read from chain 4 SC_REGISTER at add 0x00000004 = 0x%08x\n", temp_uint32 );
3469
 
3470
  if(gdb_write_reg(0x04, 0x00000001)) printf("Error write to register\n");
3471
  if (DEBUG_GDB) printf("Write to chain 4 SC_REGISTER at add 0x00000004 = 0x00000001\n");
3472
 
3473
  //    if(gdb_read_reg(0x04, &temp_uint32)) printf("Error read from register\n");
3474
  //    if (DEBUG_GDB) printf("Read from chain 4 SC_REGISTER at add 0x00000004 = 0x%08x\n", temp_uint32 );                      
3475
 
3476
  //    if(gdb_read_reg(0x04, &temp_uint32)) printf("Error read from register\n");
3477
  //    if (DEBUG_GDB) printf("Read from chain 4 SC_REGISTER at add 0x00000004 = 0x%08x\n", temp_uint32 );                      
3478
 
3479
  if(gdb_write_reg(0x00, 0x01000001)) printf("Error write to register\n");
3480
  if (DEBUG_GDB) printf("Write to chain 4 SC_REGISTER at add 0x00000000 = 0x01000001\n");
3481
}
3482
 
3483
// Function to check if the processor is stalled - if not then stall it.
3484
// this is useful in the event that GDB thinks the processor is stalled, but has, in fact
3485
// been hard reset on the board and is running.
3486
static void gdb_ensure_or1k_stalled()
3487
{
3488
  unsigned char stalled;
3489
  dbg_cpu0_read_ctrl(0, &stalled);
3490
  if ((stalled & 0x1) != 0x1)
3491
    {
3492
      if (DEBUG_GDB)
3493
        printf("Processor not stalled, like we thought\n");
3494
 
3495
      // Set the TAP controller to its OR1k chain
3496
      dbg_set_tap_ir(JI_DEBUG);
3497
      gdb_chain = -1;
3498
 
3499
      // Processor isn't stalled, contrary to what we though, so stall it
3500
      printf("Stalling or1k\n");
3501
      dbg_cpu0_write_ctrl(0, 0x01);      // stall or1k
3502
    }
3503
}
3504
 
3505 46 julius
int gdb_write_byte(uint32_t adr, uint8_t data) {
3506
 
3507
#ifdef OR32_KERNEL_DBG_COMPAT
3508
  if (IS_VM_ADDR(adr))
3509
    adr = adr & ~OR32_LINUX_VM_MASK;
3510
#endif
3511 39 julius
 
3512 46 julius
  if (DEBUG_CMDS) printf("wbyte %d\n", gdb_chain);
3513 39 julius
  switch (gdb_chain) {
3514 46 julius
  case SC_WISHBONE:   return dbg_wb_write8(adr, data) ?
3515 39 julius
      ERR_CRC : ERR_NONE;
3516
  default:            return JTAG_PROXY_INVALID_CHAIN;
3517
  }
3518
}
3519
 
3520
int gdb_write_reg(uint32_t adr, uint32_t data) {
3521 46 julius
 
3522
#ifdef OR32_KERNEL_DBG_COMPAT
3523
  if (IS_VM_ADDR(adr))
3524
    adr = adr & ~OR32_LINUX_VM_MASK;
3525
#endif
3526
 
3527
 
3528 39 julius
  switch (gdb_chain) { /* remap registers, to be compatible with jp1 */
3529
  case SC_RISC_DEBUG: if (adr == JTAG_RISCOP) adr = 0x00;
3530
    return dbg_cpu0_write(adr, data) ? ERR_CRC : ERR_NONE;
3531
  case SC_REGISTER:   return dbg_cpu0_write_ctrl(adr, data) ? ERR_CRC : ERR_NONE;
3532
  case SC_WISHBONE:   return dbg_wb_write32(adr, data) ? ERR_CRC : ERR_NONE;
3533
  case SC_TRACE:      return 0;
3534
  default:            return JTAG_PROXY_INVALID_CHAIN;
3535
  }
3536
}
3537 46 julius
int gdb_read_reg(uint32_t adr, uint32_t *data) {
3538 39 julius
 
3539 46 julius
#ifdef OR32_KERNEL_DBG_COMPAT
3540
  if (IS_VM_ADDR(adr))
3541
    adr = adr & ~OR32_LINUX_VM_MASK;
3542
#endif
3543
 
3544
  switch (gdb_chain) {
3545
  case SC_RISC_DEBUG: return dbg_cpu0_read(adr, data) ? ERR_CRC : ERR_NONE;
3546
  case SC_REGISTER:   return dbg_cpu0_read_ctrl(adr, (unsigned char*)data) ?
3547
      ERR_CRC : ERR_NONE;
3548
  case SC_WISHBONE:   return dbg_wb_read32(adr, data) ? ERR_CRC : ERR_NONE;
3549
  case SC_TRACE:      *data = 0; return 0;
3550
  default:            return JTAG_PROXY_INVALID_CHAIN;
3551
  }
3552
}
3553
 
3554
 
3555 39 julius
int gdb_read_block(uint32_t adr, uint32_t *data, int len) {
3556 46 julius
 
3557
#ifdef OR32_KERNEL_DBG_COMPAT
3558
  if (IS_VM_ADDR(adr))
3559
    adr = adr & ~OR32_LINUX_VM_MASK;
3560
#endif
3561
 
3562 39 julius
  if (DEBUG_CMDS) printf("rb %d\n", gdb_chain);
3563 46 julius
 
3564 39 julius
  switch (gdb_chain) {
3565 46 julius
  case SC_WISHBONE:
3566
    {
3567
      return dbg_wb_read_block32(adr, data, len) ? ERR_CRC : ERR_NONE;
3568
    }
3569 39 julius
  default:            return JTAG_PROXY_INVALID_CHAIN;
3570
  }
3571
}
3572
 
3573
int gdb_write_block(uint32_t adr, uint32_t *data, int len) {
3574 46 julius
 
3575
#ifdef OR32_KERNEL_DBG_COMPAT
3576
  if (IS_VM_ADDR(adr))
3577
    adr = adr & ~OR32_LINUX_VM_MASK;
3578
#endif
3579
 
3580 39 julius
  if (DEBUG_CMDS) printf("wb %d\n", gdb_chain);
3581
  switch (gdb_chain) {
3582
  case SC_WISHBONE:   return dbg_wb_write_block32(adr, data, len) ?
3583
      ERR_CRC : ERR_NONE;
3584
  default:            return JTAG_PROXY_INVALID_CHAIN;
3585
  }
3586
}
3587
 
3588
int gdb_set_chain(int chain) {
3589
  switch (chain) {
3590
  case SC_RISC_DEBUG:
3591
  case SC_REGISTER:
3592
  case SC_TRACE:
3593
  case SC_WISHBONE:   gdb_chain = chain;
3594
    return ERR_NONE;
3595
  default:            return JTAG_PROXY_INVALID_CHAIN;
3596
  }
3597
}
3598
 
3599
/*****************************************************************************
3600
* Close the connection to the client if it is open
3601
******************************************************************************/
3602
static void client_close (char err)
3603
{
3604
  if(gdb_fd) {
3605
    perror("gdb socket - " + err);
3606
    close(gdb_fd);
3607
    gdb_fd = 0;
3608
  }
3609
}       /* client_close () */
3610
 
3611
 
3612
/*---------------------------------------------------------------------------*/
3613
/* Swap a buffer of 4-byte from 1234 to 4321
3614
 
3615
   parameter[in]  p_buf and len
3616
   parameter[out] none                                                                                                                                                                                                                   */
3617
/*---------------------------------------------------------------------------*/
3618
static void swap_buf(char* p_buf, int len)
3619
{
3620
        int temp;
3621
        int n = 0;
3622
 
3623
  if (len > 2)
3624
  {
3625
    while(n < len){
3626
        // swap 0 and 3
3627
        temp = p_buf[n];
3628
                        p_buf[n] = p_buf[n + 3];
3629
                        p_buf[n + 3] = temp;
3630
        // swap 1 and 2
3631
        temp = p_buf[n + 1];
3632
                        p_buf[n + 1] = p_buf[n + 2];
3633
                        p_buf[n + 2] = temp;
3634
 
3635
                        n += 4;
3636
    }
3637
        }
3638
}
3639
 
3640
 
3641
/*---------------------------------------------------------------------------*/
3642
/*!Set the stall state of the processor
3643
 
3644
   @param[in] state  If non-zero stall the processor.                        */
3645
/*---------------------------------------------------------------------------*/
3646
static void
3647
set_stall_state (int state)
3648
{
3649
 
3650
  if(state == 0)
3651
    {
3652
      err = dbg_cpu0_write_ctrl(0, 0);     /* unstall or1k */
3653
      stallState = UNSTALLED;
3654
      npcIsCached = 0;
3655
      rsp.sigval = TARGET_SIGNAL_NONE;
3656
    }
3657
  else
3658
    {
3659
      err = dbg_cpu0_write_ctrl(0, 0x01);                                 /* stall or1k */
3660
      stallState = STALLED;
3661
    }
3662
 
3663
  if(err > 0 && DEBUG_GDB)printf("Error %d in set_stall_state Stall state = %d\n", err, state);
3664
 
3665
}       /* set_stall_state () */
3666
 
3667
 
3668
/*---------------------------------------------------------------------------*/
3669
/*!Set the reset bit of the processor's control reg in debug interface
3670
 */
3671
/*---------------------------------------------------------------------------*/
3672
static void
3673
reset_or1k (void)
3674
{
3675
 
3676
  err = dbg_cpu0_write_ctrl(0, 0x02);  /* reset or1k */
3677
 
3678
  if(err > 0 && DEBUG_GDB)printf("Error %d in reset_or1k()\n", err);
3679
 
3680
}       /* reset_or1k () */
3681
 
3682
 
3683
/*---------------------------------------------------------------------------*/
3684
/*!Close down the connection with GDB in the event of a kill signal
3685
 
3686
 */
3687
/*---------------------------------------------------------------------------*/
3688
void gdb_close()
3689
{
3690
  if (gdb_fd) close(gdb_fd);
3691
  // Maybe do other things here!
3692
}

powered by: WebSVN 2.1.0

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