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

Subversion Repositories openrisc

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

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

powered by: WebSVN 2.1.0

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