PageRenderTime 34ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

/bench/verilog/vpi/c/gdb.c

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