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

Subversion Repositories openrisc_me

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

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

powered by: WebSVN 2.1.0

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