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

Subversion Repositories openrisc

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

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

powered by: WebSVN 2.1.0

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