linux-arm-low.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  1. /* GNU/Linux/ARM specific low level interface, for the remote server for GDB.
  2. Copyright (C) 1995-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "server.h"
  15. #include "linux-low.h"
  16. #include "arch/arm.h"
  17. #include "arch/arm-linux.h"
  18. #include "arch/arm-get-next-pcs.h"
  19. #include "linux-aarch32-low.h"
  20. #include "linux-aarch32-tdesc.h"
  21. #include "linux-arm-tdesc.h"
  22. #include <sys/uio.h>
  23. /* Don't include elf.h if linux/elf.h got included by gdb_proc_service.h.
  24. On Bionic elf.h and linux/elf.h have conflicting definitions. */
  25. #ifndef ELFMAG0
  26. #include <elf.h>
  27. #endif
  28. #include "nat/gdb_ptrace.h"
  29. #include <signal.h>
  30. #include <sys/syscall.h>
  31. #ifndef PTRACE_GET_THREAD_AREA
  32. #define PTRACE_GET_THREAD_AREA 22
  33. #endif
  34. #ifndef PTRACE_GETWMMXREGS
  35. # define PTRACE_GETWMMXREGS 18
  36. # define PTRACE_SETWMMXREGS 19
  37. #endif
  38. #ifndef PTRACE_GETVFPREGS
  39. # define PTRACE_GETVFPREGS 27
  40. # define PTRACE_SETVFPREGS 28
  41. #endif
  42. #ifndef PTRACE_GETHBPREGS
  43. #define PTRACE_GETHBPREGS 29
  44. #define PTRACE_SETHBPREGS 30
  45. #endif
  46. /* Linux target op definitions for the ARM architecture. */
  47. class arm_target : public linux_process_target
  48. {
  49. public:
  50. const regs_info *get_regs_info () override;
  51. int breakpoint_kind_from_pc (CORE_ADDR *pcptr) override;
  52. int breakpoint_kind_from_current_state (CORE_ADDR *pcptr) override;
  53. const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
  54. bool supports_software_single_step () override;
  55. bool supports_z_point_type (char z_type) override;
  56. bool supports_hardware_single_step () override;
  57. protected:
  58. void low_arch_setup () override;
  59. bool low_cannot_fetch_register (int regno) override;
  60. bool low_cannot_store_register (int regno) override;
  61. bool low_supports_breakpoints () override;
  62. CORE_ADDR low_get_pc (regcache *regcache) override;
  63. void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
  64. std::vector<CORE_ADDR> low_get_next_pcs (regcache *regcache) override;
  65. bool low_breakpoint_at (CORE_ADDR pc) override;
  66. int low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
  67. int size, raw_breakpoint *bp) override;
  68. int low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
  69. int size, raw_breakpoint *bp) override;
  70. bool low_stopped_by_watchpoint () override;
  71. CORE_ADDR low_stopped_data_address () override;
  72. arch_process_info *low_new_process () override;
  73. void low_delete_process (arch_process_info *info) override;
  74. void low_new_thread (lwp_info *) override;
  75. void low_delete_thread (arch_lwp_info *) override;
  76. void low_new_fork (process_info *parent, process_info *child) override;
  77. void low_prepare_to_resume (lwp_info *lwp) override;
  78. bool low_supports_catch_syscall () override;
  79. void low_get_syscall_trapinfo (regcache *regcache, int *sysno) override;
  80. };
  81. /* The singleton target ops object. */
  82. static arm_target the_arm_target;
  83. bool
  84. arm_target::low_supports_breakpoints ()
  85. {
  86. return true;
  87. }
  88. CORE_ADDR
  89. arm_target::low_get_pc (regcache *regcache)
  90. {
  91. return linux_get_pc_32bit (regcache);
  92. }
  93. void
  94. arm_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
  95. {
  96. linux_set_pc_32bit (regcache, pc);
  97. }
  98. int
  99. arm_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
  100. {
  101. return arm_breakpoint_kind_from_pc (pcptr);
  102. }
  103. int
  104. arm_target::breakpoint_kind_from_current_state (CORE_ADDR *pcptr)
  105. {
  106. return arm_breakpoint_kind_from_current_state (pcptr);
  107. }
  108. const gdb_byte *
  109. arm_target::sw_breakpoint_from_kind (int kind, int *size)
  110. {
  111. return arm_sw_breakpoint_from_kind (kind, size);
  112. }
  113. bool
  114. arm_target::low_breakpoint_at (CORE_ADDR pc)
  115. {
  116. return arm_breakpoint_at (pc);
  117. }
  118. /* Information describing the hardware breakpoint capabilities. */
  119. static struct
  120. {
  121. unsigned char arch;
  122. unsigned char max_wp_length;
  123. unsigned char wp_count;
  124. unsigned char bp_count;
  125. } arm_linux_hwbp_cap;
  126. /* Enum describing the different types of ARM hardware break-/watch-points. */
  127. typedef enum
  128. {
  129. arm_hwbp_break = 0,
  130. arm_hwbp_load = 1,
  131. arm_hwbp_store = 2,
  132. arm_hwbp_access = 3
  133. } arm_hwbp_type;
  134. /* Type describing an ARM Hardware Breakpoint Control register value. */
  135. typedef unsigned int arm_hwbp_control_t;
  136. /* Structure used to keep track of hardware break-/watch-points. */
  137. struct arm_linux_hw_breakpoint
  138. {
  139. /* Address to break on, or being watched. */
  140. unsigned int address;
  141. /* Control register for break-/watch- point. */
  142. arm_hwbp_control_t control;
  143. };
  144. /* Since we cannot dynamically allocate subfields of arch_process_info,
  145. assume a maximum number of supported break-/watchpoints. */
  146. #define MAX_BPTS 32
  147. #define MAX_WPTS 32
  148. /* Per-process arch-specific data we want to keep. */
  149. struct arch_process_info
  150. {
  151. /* Hardware breakpoints for this process. */
  152. struct arm_linux_hw_breakpoint bpts[MAX_BPTS];
  153. /* Hardware watchpoints for this process. */
  154. struct arm_linux_hw_breakpoint wpts[MAX_WPTS];
  155. };
  156. /* Per-thread arch-specific data we want to keep. */
  157. struct arch_lwp_info
  158. {
  159. /* Non-zero if our copy differs from what's recorded in the thread. */
  160. char bpts_changed[MAX_BPTS];
  161. char wpts_changed[MAX_WPTS];
  162. /* Cached stopped data address. */
  163. CORE_ADDR stopped_data_address;
  164. };
  165. /* These are in <asm/elf.h> in current kernels. */
  166. #define HWCAP_VFP 64
  167. #define HWCAP_IWMMXT 512
  168. #define HWCAP_NEON 4096
  169. #define HWCAP_VFPv3 8192
  170. #define HWCAP_VFPv3D16 16384
  171. #ifdef HAVE_SYS_REG_H
  172. #include <sys/reg.h>
  173. #endif
  174. #define arm_num_regs 26
  175. static int arm_regmap[] = {
  176. 0, 4, 8, 12, 16, 20, 24, 28,
  177. 32, 36, 40, 44, 48, 52, 56, 60,
  178. -1, -1, -1, -1, -1, -1, -1, -1, -1,
  179. 64
  180. };
  181. /* Forward declarations needed for get_next_pcs ops. */
  182. static ULONGEST get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr,
  183. int len,
  184. int byte_order);
  185. static CORE_ADDR get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self,
  186. CORE_ADDR val);
  187. static CORE_ADDR get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self);
  188. static int get_next_pcs_is_thumb (struct arm_get_next_pcs *self);
  189. /* get_next_pcs operations. */
  190. static struct arm_get_next_pcs_ops get_next_pcs_ops = {
  191. get_next_pcs_read_memory_unsigned_integer,
  192. get_next_pcs_syscall_next_pc,
  193. get_next_pcs_addr_bits_remove,
  194. get_next_pcs_is_thumb,
  195. arm_linux_get_next_pcs_fixup,
  196. };
  197. bool
  198. arm_target::low_cannot_store_register (int regno)
  199. {
  200. return (regno >= arm_num_regs);
  201. }
  202. bool
  203. arm_target::low_cannot_fetch_register (int regno)
  204. {
  205. return (regno >= arm_num_regs);
  206. }
  207. static void
  208. arm_fill_wmmxregset (struct regcache *regcache, void *buf)
  209. {
  210. if (arm_linux_get_tdesc_fp_type (regcache->tdesc) != ARM_FP_TYPE_IWMMXT)
  211. return;
  212. for (int i = 0; i < 16; i++)
  213. collect_register (regcache, arm_num_regs + i, (char *) buf + i * 8);
  214. /* We only have access to wcssf, wcasf, and wcgr0-wcgr3. */
  215. for (int i = 0; i < 6; i++)
  216. collect_register (regcache, arm_num_regs + i + 16,
  217. (char *) buf + 16 * 8 + i * 4);
  218. }
  219. static void
  220. arm_store_wmmxregset (struct regcache *regcache, const void *buf)
  221. {
  222. if (arm_linux_get_tdesc_fp_type (regcache->tdesc) != ARM_FP_TYPE_IWMMXT)
  223. return;
  224. for (int i = 0; i < 16; i++)
  225. supply_register (regcache, arm_num_regs + i, (char *) buf + i * 8);
  226. /* We only have access to wcssf, wcasf, and wcgr0-wcgr3. */
  227. for (int i = 0; i < 6; i++)
  228. supply_register (regcache, arm_num_regs + i + 16,
  229. (char *) buf + 16 * 8 + i * 4);
  230. }
  231. static void
  232. arm_fill_vfpregset (struct regcache *regcache, void *buf)
  233. {
  234. int num;
  235. if (is_aarch32_linux_description (regcache->tdesc))
  236. num = 32;
  237. else
  238. {
  239. arm_fp_type fp_type = arm_linux_get_tdesc_fp_type (regcache->tdesc);
  240. if (fp_type == ARM_FP_TYPE_VFPV3)
  241. num = 32;
  242. else if (fp_type == ARM_FP_TYPE_VFPV2)
  243. num = 16;
  244. else
  245. return;
  246. }
  247. arm_fill_vfpregset_num (regcache, buf, num);
  248. }
  249. /* Wrapper of UNMAKE_THUMB_ADDR for get_next_pcs. */
  250. static CORE_ADDR
  251. get_next_pcs_addr_bits_remove (struct arm_get_next_pcs *self, CORE_ADDR val)
  252. {
  253. return UNMAKE_THUMB_ADDR (val);
  254. }
  255. static void
  256. arm_store_vfpregset (struct regcache *regcache, const void *buf)
  257. {
  258. int num;
  259. if (is_aarch32_linux_description (regcache->tdesc))
  260. num = 32;
  261. else
  262. {
  263. arm_fp_type fp_type = arm_linux_get_tdesc_fp_type (regcache->tdesc);
  264. if (fp_type == ARM_FP_TYPE_VFPV3)
  265. num = 32;
  266. else if (fp_type == ARM_FP_TYPE_VFPV2)
  267. num = 16;
  268. else
  269. return;
  270. }
  271. arm_store_vfpregset_num (regcache, buf, num);
  272. }
  273. /* Wrapper of arm_is_thumb_mode for get_next_pcs. */
  274. static int
  275. get_next_pcs_is_thumb (struct arm_get_next_pcs *self)
  276. {
  277. return arm_is_thumb_mode ();
  278. }
  279. /* Read memory from the inferior.
  280. BYTE_ORDER is ignored and there to keep compatiblity with GDB's
  281. read_memory_unsigned_integer. */
  282. static ULONGEST
  283. get_next_pcs_read_memory_unsigned_integer (CORE_ADDR memaddr,
  284. int len,
  285. int byte_order)
  286. {
  287. ULONGEST res;
  288. res = 0;
  289. target_read_memory (memaddr, (unsigned char *) &res, len);
  290. return res;
  291. }
  292. /* Fetch the thread-local storage pointer for libthread_db. */
  293. ps_err_e
  294. ps_get_thread_area (struct ps_prochandle *ph,
  295. lwpid_t lwpid, int idx, void **base)
  296. {
  297. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  298. return PS_ERR;
  299. /* IDX is the bias from the thread pointer to the beginning of the
  300. thread descriptor. It has to be subtracted due to implementation
  301. quirks in libthread_db. */
  302. *base = (void *) ((char *)*base - idx);
  303. return PS_OK;
  304. }
  305. /* Query Hardware Breakpoint information for the target we are attached to
  306. (using PID as ptrace argument) and set up arm_linux_hwbp_cap. */
  307. static void
  308. arm_linux_init_hwbp_cap (int pid)
  309. {
  310. unsigned int val;
  311. if (ptrace (PTRACE_GETHBPREGS, pid, 0, &val) < 0)
  312. return;
  313. arm_linux_hwbp_cap.arch = (unsigned char)((val >> 24) & 0xff);
  314. if (arm_linux_hwbp_cap.arch == 0)
  315. return;
  316. arm_linux_hwbp_cap.max_wp_length = (unsigned char)((val >> 16) & 0xff);
  317. arm_linux_hwbp_cap.wp_count = (unsigned char)((val >> 8) & 0xff);
  318. arm_linux_hwbp_cap.bp_count = (unsigned char)(val & 0xff);
  319. if (arm_linux_hwbp_cap.wp_count > MAX_WPTS)
  320. internal_error (__FILE__, __LINE__, "Unsupported number of watchpoints");
  321. if (arm_linux_hwbp_cap.bp_count > MAX_BPTS)
  322. internal_error (__FILE__, __LINE__, "Unsupported number of breakpoints");
  323. }
  324. /* How many hardware breakpoints are available? */
  325. static int
  326. arm_linux_get_hw_breakpoint_count (void)
  327. {
  328. return arm_linux_hwbp_cap.bp_count;
  329. }
  330. /* How many hardware watchpoints are available? */
  331. static int
  332. arm_linux_get_hw_watchpoint_count (void)
  333. {
  334. return arm_linux_hwbp_cap.wp_count;
  335. }
  336. /* Maximum length of area watched by hardware watchpoint. */
  337. static int
  338. arm_linux_get_hw_watchpoint_max_length (void)
  339. {
  340. return arm_linux_hwbp_cap.max_wp_length;
  341. }
  342. /* Initialize an ARM hardware break-/watch-point control register value.
  343. BYTE_ADDRESS_SELECT is the mask of bytes to trigger on; HWBP_TYPE is the
  344. type of break-/watch-point; ENABLE indicates whether the point is enabled.
  345. */
  346. static arm_hwbp_control_t
  347. arm_hwbp_control_initialize (unsigned byte_address_select,
  348. arm_hwbp_type hwbp_type,
  349. int enable)
  350. {
  351. gdb_assert ((byte_address_select & ~0xffU) == 0);
  352. gdb_assert (hwbp_type != arm_hwbp_break
  353. || ((byte_address_select & 0xfU) != 0));
  354. return (byte_address_select << 5) | (hwbp_type << 3) | (3 << 1) | enable;
  355. }
  356. /* Does the breakpoint control value CONTROL have the enable bit set? */
  357. static int
  358. arm_hwbp_control_is_enabled (arm_hwbp_control_t control)
  359. {
  360. return control & 0x1;
  361. }
  362. /* Is the breakpoint control value CONTROL initialized? */
  363. static int
  364. arm_hwbp_control_is_initialized (arm_hwbp_control_t control)
  365. {
  366. return control != 0;
  367. }
  368. /* Change a breakpoint control word so that it is in the disabled state. */
  369. static arm_hwbp_control_t
  370. arm_hwbp_control_disable (arm_hwbp_control_t control)
  371. {
  372. return control & ~0x1;
  373. }
  374. /* Are two break-/watch-points equal? */
  375. static int
  376. arm_linux_hw_breakpoint_equal (const struct arm_linux_hw_breakpoint *p1,
  377. const struct arm_linux_hw_breakpoint *p2)
  378. {
  379. return p1->address == p2->address && p1->control == p2->control;
  380. }
  381. /* Convert a raw breakpoint type to an enum arm_hwbp_type. */
  382. static arm_hwbp_type
  383. raw_bkpt_type_to_arm_hwbp_type (enum raw_bkpt_type raw_type)
  384. {
  385. switch (raw_type)
  386. {
  387. case raw_bkpt_type_hw:
  388. return arm_hwbp_break;
  389. case raw_bkpt_type_write_wp:
  390. return arm_hwbp_store;
  391. case raw_bkpt_type_read_wp:
  392. return arm_hwbp_load;
  393. case raw_bkpt_type_access_wp:
  394. return arm_hwbp_access;
  395. default:
  396. gdb_assert_not_reached ("unhandled raw type");
  397. }
  398. }
  399. /* Initialize the hardware breakpoint structure P for a breakpoint or
  400. watchpoint at ADDR to LEN. The type of watchpoint is given in TYPE.
  401. Returns -1 if TYPE is unsupported, or -2 if the particular combination
  402. of ADDR and LEN cannot be implemented. Otherwise, returns 0 if TYPE
  403. represents a breakpoint and 1 if type represents a watchpoint. */
  404. static int
  405. arm_linux_hw_point_initialize (enum raw_bkpt_type raw_type, CORE_ADDR addr,
  406. int len, struct arm_linux_hw_breakpoint *p)
  407. {
  408. arm_hwbp_type hwbp_type;
  409. unsigned mask;
  410. hwbp_type = raw_bkpt_type_to_arm_hwbp_type (raw_type);
  411. if (hwbp_type == arm_hwbp_break)
  412. {
  413. /* For breakpoints, the length field encodes the mode. */
  414. switch (len)
  415. {
  416. case 2: /* 16-bit Thumb mode breakpoint */
  417. case 3: /* 32-bit Thumb mode breakpoint */
  418. mask = 0x3;
  419. addr &= ~1;
  420. break;
  421. case 4: /* 32-bit ARM mode breakpoint */
  422. mask = 0xf;
  423. addr &= ~3;
  424. break;
  425. default:
  426. /* Unsupported. */
  427. return -2;
  428. }
  429. }
  430. else
  431. {
  432. CORE_ADDR max_wp_length = arm_linux_get_hw_watchpoint_max_length ();
  433. CORE_ADDR aligned_addr;
  434. /* Can not set watchpoints for zero or negative lengths. */
  435. if (len <= 0)
  436. return -2;
  437. /* The current ptrace interface can only handle watchpoints that are a
  438. power of 2. */
  439. if ((len & (len - 1)) != 0)
  440. return -2;
  441. /* Test that the range [ADDR, ADDR + LEN) fits into the largest address
  442. range covered by a watchpoint. */
  443. aligned_addr = addr & ~(max_wp_length - 1);
  444. if (aligned_addr + max_wp_length < addr + len)
  445. return -2;
  446. mask = (1 << len) - 1;
  447. }
  448. p->address = (unsigned int) addr;
  449. p->control = arm_hwbp_control_initialize (mask, hwbp_type, 1);
  450. return hwbp_type != arm_hwbp_break;
  451. }
  452. /* Callback to mark a watch-/breakpoint to be updated in all threads of
  453. the current process. */
  454. static void
  455. update_registers_callback (thread_info *thread, int watch, int i)
  456. {
  457. struct lwp_info *lwp = get_thread_lwp (thread);
  458. /* The actual update is done later just before resuming the lwp,
  459. we just mark that the registers need updating. */
  460. if (watch)
  461. lwp->arch_private->wpts_changed[i] = 1;
  462. else
  463. lwp->arch_private->bpts_changed[i] = 1;
  464. /* If the lwp isn't stopped, force it to momentarily pause, so
  465. we can update its breakpoint registers. */
  466. if (!lwp->stopped)
  467. linux_stop_lwp (lwp);
  468. }
  469. bool
  470. arm_target::supports_z_point_type (char z_type)
  471. {
  472. switch (z_type)
  473. {
  474. case Z_PACKET_SW_BP:
  475. case Z_PACKET_HW_BP:
  476. case Z_PACKET_WRITE_WP:
  477. case Z_PACKET_READ_WP:
  478. case Z_PACKET_ACCESS_WP:
  479. return true;
  480. default:
  481. /* Leave the handling of sw breakpoints with the gdb client. */
  482. return false;
  483. }
  484. }
  485. /* Insert hardware break-/watchpoint. */
  486. int
  487. arm_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
  488. int len, raw_breakpoint *bp)
  489. {
  490. struct process_info *proc = current_process ();
  491. struct arm_linux_hw_breakpoint p, *pts;
  492. int watch, i, count;
  493. watch = arm_linux_hw_point_initialize (type, addr, len, &p);
  494. if (watch < 0)
  495. {
  496. /* Unsupported. */
  497. return watch == -1 ? 1 : -1;
  498. }
  499. if (watch)
  500. {
  501. count = arm_linux_get_hw_watchpoint_count ();
  502. pts = proc->priv->arch_private->wpts;
  503. }
  504. else
  505. {
  506. count = arm_linux_get_hw_breakpoint_count ();
  507. pts = proc->priv->arch_private->bpts;
  508. }
  509. for (i = 0; i < count; i++)
  510. if (!arm_hwbp_control_is_enabled (pts[i].control))
  511. {
  512. pts[i] = p;
  513. /* Only update the threads of the current process. */
  514. for_each_thread (current_thread->id.pid (), [&] (thread_info *thread)
  515. {
  516. update_registers_callback (thread, watch, i);
  517. });
  518. return 0;
  519. }
  520. /* We're out of watchpoints. */
  521. return -1;
  522. }
  523. /* Remove hardware break-/watchpoint. */
  524. int
  525. arm_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
  526. int len, raw_breakpoint *bp)
  527. {
  528. struct process_info *proc = current_process ();
  529. struct arm_linux_hw_breakpoint p, *pts;
  530. int watch, i, count;
  531. watch = arm_linux_hw_point_initialize (type, addr, len, &p);
  532. if (watch < 0)
  533. {
  534. /* Unsupported. */
  535. return -1;
  536. }
  537. if (watch)
  538. {
  539. count = arm_linux_get_hw_watchpoint_count ();
  540. pts = proc->priv->arch_private->wpts;
  541. }
  542. else
  543. {
  544. count = arm_linux_get_hw_breakpoint_count ();
  545. pts = proc->priv->arch_private->bpts;
  546. }
  547. for (i = 0; i < count; i++)
  548. if (arm_linux_hw_breakpoint_equal (&p, pts + i))
  549. {
  550. pts[i].control = arm_hwbp_control_disable (pts[i].control);
  551. /* Only update the threads of the current process. */
  552. for_each_thread (current_thread->id.pid (), [&] (thread_info *thread)
  553. {
  554. update_registers_callback (thread, watch, i);
  555. });
  556. return 0;
  557. }
  558. /* No watchpoint matched. */
  559. return -1;
  560. }
  561. /* Return whether current thread is stopped due to a watchpoint. */
  562. bool
  563. arm_target::low_stopped_by_watchpoint ()
  564. {
  565. struct lwp_info *lwp = get_thread_lwp (current_thread);
  566. siginfo_t siginfo;
  567. /* We must be able to set hardware watchpoints. */
  568. if (arm_linux_get_hw_watchpoint_count () == 0)
  569. return false;
  570. /* Retrieve siginfo. */
  571. errno = 0;
  572. ptrace (PTRACE_GETSIGINFO, lwpid_of (current_thread), 0, &siginfo);
  573. if (errno != 0)
  574. return false;
  575. /* This must be a hardware breakpoint. */
  576. if (siginfo.si_signo != SIGTRAP
  577. || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
  578. return false;
  579. /* If we are in a positive slot then we're looking at a breakpoint and not
  580. a watchpoint. */
  581. if (siginfo.si_errno >= 0)
  582. return false;
  583. /* Cache stopped data address for use by arm_stopped_data_address. */
  584. lwp->arch_private->stopped_data_address
  585. = (CORE_ADDR) (uintptr_t) siginfo.si_addr;
  586. return true;
  587. }
  588. /* Return data address that triggered watchpoint. Called only if
  589. low_stopped_by_watchpoint returned true. */
  590. CORE_ADDR
  591. arm_target::low_stopped_data_address ()
  592. {
  593. struct lwp_info *lwp = get_thread_lwp (current_thread);
  594. return lwp->arch_private->stopped_data_address;
  595. }
  596. /* Called when a new process is created. */
  597. arch_process_info *
  598. arm_target::low_new_process ()
  599. {
  600. struct arch_process_info *info = XCNEW (struct arch_process_info);
  601. return info;
  602. }
  603. /* Called when a process is being deleted. */
  604. void
  605. arm_target::low_delete_process (arch_process_info *info)
  606. {
  607. xfree (info);
  608. }
  609. /* Called when a new thread is detected. */
  610. void
  611. arm_target::low_new_thread (lwp_info *lwp)
  612. {
  613. struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
  614. int i;
  615. for (i = 0; i < MAX_BPTS; i++)
  616. info->bpts_changed[i] = 1;
  617. for (i = 0; i < MAX_WPTS; i++)
  618. info->wpts_changed[i] = 1;
  619. lwp->arch_private = info;
  620. }
  621. /* Function to call when a thread is being deleted. */
  622. void
  623. arm_target::low_delete_thread (arch_lwp_info *arch_lwp)
  624. {
  625. xfree (arch_lwp);
  626. }
  627. void
  628. arm_target::low_new_fork (process_info *parent, process_info *child)
  629. {
  630. struct arch_process_info *parent_proc_info;
  631. struct arch_process_info *child_proc_info;
  632. struct lwp_info *child_lwp;
  633. struct arch_lwp_info *child_lwp_info;
  634. int i;
  635. /* These are allocated by linux_add_process. */
  636. gdb_assert (parent->priv != NULL
  637. && parent->priv->arch_private != NULL);
  638. gdb_assert (child->priv != NULL
  639. && child->priv->arch_private != NULL);
  640. parent_proc_info = parent->priv->arch_private;
  641. child_proc_info = child->priv->arch_private;
  642. /* Linux kernel before 2.6.33 commit
  643. 72f674d203cd230426437cdcf7dd6f681dad8b0d
  644. will inherit hardware debug registers from parent
  645. on fork/vfork/clone. Newer Linux kernels create such tasks with
  646. zeroed debug registers.
  647. GDB core assumes the child inherits the watchpoints/hw
  648. breakpoints of the parent, and will remove them all from the
  649. forked off process. Copy the debug registers mirrors into the
  650. new process so that all breakpoints and watchpoints can be
  651. removed together. The debug registers mirror will become zeroed
  652. in the end before detaching the forked off process, thus making
  653. this compatible with older Linux kernels too. */
  654. *child_proc_info = *parent_proc_info;
  655. /* Mark all the hardware breakpoints and watchpoints as changed to
  656. make sure that the registers will be updated. */
  657. child_lwp = find_lwp_pid (ptid_t (child->pid));
  658. child_lwp_info = child_lwp->arch_private;
  659. for (i = 0; i < MAX_BPTS; i++)
  660. child_lwp_info->bpts_changed[i] = 1;
  661. for (i = 0; i < MAX_WPTS; i++)
  662. child_lwp_info->wpts_changed[i] = 1;
  663. }
  664. /* Called when resuming a thread.
  665. If the debug regs have changed, update the thread's copies. */
  666. void
  667. arm_target::low_prepare_to_resume (lwp_info *lwp)
  668. {
  669. struct thread_info *thread = get_lwp_thread (lwp);
  670. int pid = lwpid_of (thread);
  671. struct process_info *proc = find_process_pid (pid_of (thread));
  672. struct arch_process_info *proc_info = proc->priv->arch_private;
  673. struct arch_lwp_info *lwp_info = lwp->arch_private;
  674. int i;
  675. for (i = 0; i < arm_linux_get_hw_breakpoint_count (); i++)
  676. if (lwp_info->bpts_changed[i])
  677. {
  678. errno = 0;
  679. if (arm_hwbp_control_is_enabled (proc_info->bpts[i].control))
  680. if (ptrace (PTRACE_SETHBPREGS, pid,
  681. (PTRACE_TYPE_ARG3) ((i << 1) + 1),
  682. &proc_info->bpts[i].address) < 0)
  683. perror_with_name ("Unexpected error setting breakpoint address");
  684. if (arm_hwbp_control_is_initialized (proc_info->bpts[i].control))
  685. if (ptrace (PTRACE_SETHBPREGS, pid,
  686. (PTRACE_TYPE_ARG3) ((i << 1) + 2),
  687. &proc_info->bpts[i].control) < 0)
  688. perror_with_name ("Unexpected error setting breakpoint");
  689. lwp_info->bpts_changed[i] = 0;
  690. }
  691. for (i = 0; i < arm_linux_get_hw_watchpoint_count (); i++)
  692. if (lwp_info->wpts_changed[i])
  693. {
  694. errno = 0;
  695. if (arm_hwbp_control_is_enabled (proc_info->wpts[i].control))
  696. if (ptrace (PTRACE_SETHBPREGS, pid,
  697. (PTRACE_TYPE_ARG3) -((i << 1) + 1),
  698. &proc_info->wpts[i].address) < 0)
  699. perror_with_name ("Unexpected error setting watchpoint address");
  700. if (arm_hwbp_control_is_initialized (proc_info->wpts[i].control))
  701. if (ptrace (PTRACE_SETHBPREGS, pid,
  702. (PTRACE_TYPE_ARG3) -((i << 1) + 2),
  703. &proc_info->wpts[i].control) < 0)
  704. perror_with_name ("Unexpected error setting watchpoint");
  705. lwp_info->wpts_changed[i] = 0;
  706. }
  707. }
  708. /* Find the next pc for a sigreturn or rt_sigreturn syscall. In
  709. addition, set IS_THUMB depending on whether we will return to ARM
  710. or Thumb code.
  711. See arm-linux.h for stack layout details. */
  712. static CORE_ADDR
  713. arm_sigreturn_next_pc (struct regcache *regcache, int svc_number,
  714. int *is_thumb)
  715. {
  716. unsigned long sp;
  717. unsigned long sp_data;
  718. /* Offset of PC register. */
  719. int pc_offset = 0;
  720. CORE_ADDR next_pc = 0;
  721. uint32_t cpsr;
  722. gdb_assert (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn);
  723. collect_register_by_name (regcache, "sp", &sp);
  724. the_target->read_memory (sp, (unsigned char *) &sp_data, 4);
  725. pc_offset = arm_linux_sigreturn_next_pc_offset
  726. (sp, sp_data, svc_number, __NR_sigreturn == svc_number ? 1 : 0);
  727. the_target->read_memory (sp + pc_offset, (unsigned char *) &next_pc, 4);
  728. /* Set IS_THUMB according the CPSR saved on the stack. */
  729. the_target->read_memory (sp + pc_offset + 4, (unsigned char *) &cpsr, 4);
  730. *is_thumb = ((cpsr & CPSR_T) != 0);
  731. return next_pc;
  732. }
  733. /* When PC is at a syscall instruction, return the PC of the next
  734. instruction to be executed. */
  735. static CORE_ADDR
  736. get_next_pcs_syscall_next_pc (struct arm_get_next_pcs *self)
  737. {
  738. CORE_ADDR next_pc = 0;
  739. CORE_ADDR pc = regcache_read_pc (self->regcache);
  740. int is_thumb = arm_is_thumb_mode ();
  741. ULONGEST svc_number = 0;
  742. struct regcache *regcache = self->regcache;
  743. if (is_thumb)
  744. {
  745. collect_register (regcache, 7, &svc_number);
  746. next_pc = pc + 2;
  747. }
  748. else
  749. {
  750. unsigned long this_instr;
  751. unsigned long svc_operand;
  752. target_read_memory (pc, (unsigned char *) &this_instr, 4);
  753. svc_operand = (0x00ffffff & this_instr);
  754. if (svc_operand) /* OABI. */
  755. {
  756. svc_number = svc_operand - 0x900000;
  757. }
  758. else /* EABI. */
  759. {
  760. collect_register (regcache, 7, &svc_number);
  761. }
  762. next_pc = pc + 4;
  763. }
  764. /* This is a sigreturn or sigreturn_rt syscall. */
  765. if (svc_number == __NR_sigreturn || svc_number == __NR_rt_sigreturn)
  766. {
  767. /* SIGRETURN or RT_SIGRETURN may affect the arm thumb mode, so
  768. update IS_THUMB. */
  769. next_pc = arm_sigreturn_next_pc (regcache, svc_number, &is_thumb);
  770. }
  771. /* Addresses for calling Thumb functions have the bit 0 set. */
  772. if (is_thumb)
  773. next_pc = MAKE_THUMB_ADDR (next_pc);
  774. return next_pc;
  775. }
  776. static const struct target_desc *
  777. arm_read_description (void)
  778. {
  779. unsigned long arm_hwcap = linux_get_hwcap (4);
  780. if (arm_hwcap & HWCAP_IWMMXT)
  781. return arm_linux_read_description (ARM_FP_TYPE_IWMMXT);
  782. if (arm_hwcap & HWCAP_VFP)
  783. {
  784. /* Make sure that the kernel supports reading VFP registers. Support was
  785. added in 2.6.30. */
  786. int pid = lwpid_of (current_thread);
  787. errno = 0;
  788. char *buf = (char *) alloca (ARM_VFP3_REGS_SIZE);
  789. if (ptrace (PTRACE_GETVFPREGS, pid, 0, buf) < 0 && errno == EIO)
  790. return arm_linux_read_description (ARM_FP_TYPE_NONE);
  791. /* NEON implies either no VFP, or VFPv3-D32. We only support
  792. it with VFP. */
  793. if (arm_hwcap & HWCAP_NEON)
  794. return aarch32_linux_read_description ();
  795. else if ((arm_hwcap & (HWCAP_VFPv3 | HWCAP_VFPv3D16)) == HWCAP_VFPv3)
  796. return arm_linux_read_description (ARM_FP_TYPE_VFPV3);
  797. else
  798. return arm_linux_read_description (ARM_FP_TYPE_VFPV2);
  799. }
  800. /* The default configuration uses legacy FPA registers, probably
  801. simulated. */
  802. return arm_linux_read_description (ARM_FP_TYPE_NONE);
  803. }
  804. void
  805. arm_target::low_arch_setup ()
  806. {
  807. int tid = lwpid_of (current_thread);
  808. int gpregs[18];
  809. struct iovec iov;
  810. /* Query hardware watchpoint/breakpoint capabilities. */
  811. arm_linux_init_hwbp_cap (tid);
  812. current_process ()->tdesc = arm_read_description ();
  813. iov.iov_base = gpregs;
  814. iov.iov_len = sizeof (gpregs);
  815. /* Check if PTRACE_GETREGSET works. */
  816. if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS, &iov) == 0)
  817. have_ptrace_getregset = 1;
  818. else
  819. have_ptrace_getregset = 0;
  820. }
  821. bool
  822. arm_target::supports_software_single_step ()
  823. {
  824. return true;
  825. }
  826. /* Fetch the next possible PCs after the current instruction executes. */
  827. std::vector<CORE_ADDR>
  828. arm_target::low_get_next_pcs (regcache *regcache)
  829. {
  830. struct arm_get_next_pcs next_pcs_ctx;
  831. arm_get_next_pcs_ctor (&next_pcs_ctx,
  832. &get_next_pcs_ops,
  833. /* Byte order is ignored assumed as host. */
  834. 0,
  835. 0,
  836. 1,
  837. regcache);
  838. return arm_get_next_pcs (&next_pcs_ctx);
  839. }
  840. /* Support for hardware single step. */
  841. bool
  842. arm_target::supports_hardware_single_step ()
  843. {
  844. return false;
  845. }
  846. bool
  847. arm_target::low_supports_catch_syscall ()
  848. {
  849. return true;
  850. }
  851. /* Implementation of linux target ops method "low_get_syscall_trapinfo". */
  852. void
  853. arm_target::low_get_syscall_trapinfo (regcache *regcache, int *sysno)
  854. {
  855. if (arm_is_thumb_mode ())
  856. collect_register_by_name (regcache, "r7", sysno);
  857. else
  858. {
  859. unsigned long pc;
  860. unsigned long insn;
  861. collect_register_by_name (regcache, "pc", &pc);
  862. if (read_memory (pc - 4, (unsigned char *) &insn, 4))
  863. *sysno = UNKNOWN_SYSCALL;
  864. else
  865. {
  866. unsigned long svc_operand = (0x00ffffff & insn);
  867. if (svc_operand)
  868. {
  869. /* OABI */
  870. *sysno = svc_operand - 0x900000;
  871. }
  872. else
  873. {
  874. /* EABI */
  875. collect_register_by_name (regcache, "r7", sysno);
  876. }
  877. }
  878. }
  879. }
  880. /* Register sets without using PTRACE_GETREGSET. */
  881. static struct regset_info arm_regsets[] = {
  882. { PTRACE_GETREGS, PTRACE_SETREGS, 0,
  883. ARM_CORE_REGS_SIZE + ARM_INT_REGISTER_SIZE, GENERAL_REGS,
  884. arm_fill_gregset, arm_store_gregset },
  885. { PTRACE_GETWMMXREGS, PTRACE_SETWMMXREGS, 0, IWMMXT_REGS_SIZE, EXTENDED_REGS,
  886. arm_fill_wmmxregset, arm_store_wmmxregset },
  887. { PTRACE_GETVFPREGS, PTRACE_SETVFPREGS, 0, ARM_VFP3_REGS_SIZE, EXTENDED_REGS,
  888. arm_fill_vfpregset, arm_store_vfpregset },
  889. NULL_REGSET
  890. };
  891. static struct regsets_info arm_regsets_info =
  892. {
  893. arm_regsets, /* regsets */
  894. 0, /* num_regsets */
  895. NULL, /* disabled_regsets */
  896. };
  897. static struct usrregs_info arm_usrregs_info =
  898. {
  899. arm_num_regs,
  900. arm_regmap,
  901. };
  902. static struct regs_info regs_info_arm =
  903. {
  904. NULL, /* regset_bitmap */
  905. &arm_usrregs_info,
  906. &arm_regsets_info
  907. };
  908. const regs_info *
  909. arm_target::get_regs_info ()
  910. {
  911. const struct target_desc *tdesc = current_process ()->tdesc;
  912. if (have_ptrace_getregset == 1
  913. && (is_aarch32_linux_description (tdesc)
  914. || arm_linux_get_tdesc_fp_type (tdesc) == ARM_FP_TYPE_VFPV3))
  915. return &regs_info_aarch32;
  916. return &regs_info_arm;
  917. }
  918. /* The linux target ops object. */
  919. linux_process_target *the_linux_target = &the_arm_target;
  920. void
  921. initialize_low_arch (void)
  922. {
  923. initialize_low_arch_aarch32 ();
  924. initialize_regsets_info (&arm_regsets_info);
  925. }