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

Subversion Repositories openrisc

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

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

powered by: WebSVN 2.1.0

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