PageRenderTime 63ms CodeModel.GetById 15ms RepoModel.GetById 1ms 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

Large files files are truncated, but you can click here to view the full 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 …

Large files files are truncated, but you can click here to view the full file