ia64-linux-nat.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. /* Functions specific to running gdb native on IA-64 running
  2. GNU/Linux.
  3. Copyright (C) 1999-2022 Free Software Foundation, Inc.
  4. This file is part of GDB.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. #include "defs.h"
  16. #include "inferior.h"
  17. #include "target.h"
  18. #include "gdbarch.h"
  19. #include "gdbcore.h"
  20. #include "regcache.h"
  21. #include "ia64-tdep.h"
  22. #include "linux-nat.h"
  23. #include <signal.h>
  24. #include "nat/gdb_ptrace.h"
  25. #include "gdbsupport/gdb_wait.h"
  26. #ifdef HAVE_SYS_REG_H
  27. #include <sys/reg.h>
  28. #endif
  29. #include <sys/syscall.h>
  30. #include <sys/user.h>
  31. #include <asm/ptrace_offsets.h>
  32. #include <sys/procfs.h>
  33. /* Prototypes for supply_gregset etc. */
  34. #include "gregset.h"
  35. #include "inf-ptrace.h"
  36. class ia64_linux_nat_target final : public linux_nat_target
  37. {
  38. public:
  39. /* Add our register access methods. */
  40. void fetch_registers (struct regcache *, int) override;
  41. void store_registers (struct regcache *, int) override;
  42. enum target_xfer_status xfer_partial (enum target_object object,
  43. const char *annex,
  44. gdb_byte *readbuf,
  45. const gdb_byte *writebuf,
  46. ULONGEST offset, ULONGEST len,
  47. ULONGEST *xfered_len) override;
  48. /* Override watchpoint routines. */
  49. /* The IA-64 architecture can step over a watch point (without
  50. triggering it again) if the "dd" (data debug fault disable) bit
  51. in the processor status word is set.
  52. This PSR bit is set in
  53. ia64_linux_nat_target::stopped_by_watchpoint when the code there
  54. has determined that a hardware watchpoint has indeed been hit.
  55. The CPU will then be able to execute one instruction without
  56. triggering a watchpoint. */
  57. bool have_steppable_watchpoint () override { return true; }
  58. int can_use_hw_breakpoint (enum bptype, int, int) override;
  59. bool stopped_by_watchpoint () override;
  60. bool stopped_data_address (CORE_ADDR *) override;
  61. int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  62. struct expression *) override;
  63. int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  64. struct expression *) override;
  65. /* Override linux_nat_target low methods. */
  66. void low_new_thread (struct lwp_info *lp) override;
  67. bool low_status_is_event (int status) override;
  68. void enable_watchpoints_in_psr (ptid_t ptid);
  69. };
  70. static ia64_linux_nat_target the_ia64_linux_nat_target;
  71. /* These must match the order of the register names.
  72. Some sort of lookup table is needed because the offsets associated
  73. with the registers are all over the board. */
  74. static int u_offsets[] =
  75. {
  76. /* general registers */
  77. -1, /* gr0 not available; i.e, it's always zero. */
  78. PT_R1,
  79. PT_R2,
  80. PT_R3,
  81. PT_R4,
  82. PT_R5,
  83. PT_R6,
  84. PT_R7,
  85. PT_R8,
  86. PT_R9,
  87. PT_R10,
  88. PT_R11,
  89. PT_R12,
  90. PT_R13,
  91. PT_R14,
  92. PT_R15,
  93. PT_R16,
  94. PT_R17,
  95. PT_R18,
  96. PT_R19,
  97. PT_R20,
  98. PT_R21,
  99. PT_R22,
  100. PT_R23,
  101. PT_R24,
  102. PT_R25,
  103. PT_R26,
  104. PT_R27,
  105. PT_R28,
  106. PT_R29,
  107. PT_R30,
  108. PT_R31,
  109. /* gr32 through gr127 not directly available via the ptrace interface. */
  110. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  111. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  112. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  113. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  114. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  115. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  116. /* Floating point registers */
  117. -1, -1, /* f0 and f1 not available (f0 is +0.0 and f1 is +1.0). */
  118. PT_F2,
  119. PT_F3,
  120. PT_F4,
  121. PT_F5,
  122. PT_F6,
  123. PT_F7,
  124. PT_F8,
  125. PT_F9,
  126. PT_F10,
  127. PT_F11,
  128. PT_F12,
  129. PT_F13,
  130. PT_F14,
  131. PT_F15,
  132. PT_F16,
  133. PT_F17,
  134. PT_F18,
  135. PT_F19,
  136. PT_F20,
  137. PT_F21,
  138. PT_F22,
  139. PT_F23,
  140. PT_F24,
  141. PT_F25,
  142. PT_F26,
  143. PT_F27,
  144. PT_F28,
  145. PT_F29,
  146. PT_F30,
  147. PT_F31,
  148. PT_F32,
  149. PT_F33,
  150. PT_F34,
  151. PT_F35,
  152. PT_F36,
  153. PT_F37,
  154. PT_F38,
  155. PT_F39,
  156. PT_F40,
  157. PT_F41,
  158. PT_F42,
  159. PT_F43,
  160. PT_F44,
  161. PT_F45,
  162. PT_F46,
  163. PT_F47,
  164. PT_F48,
  165. PT_F49,
  166. PT_F50,
  167. PT_F51,
  168. PT_F52,
  169. PT_F53,
  170. PT_F54,
  171. PT_F55,
  172. PT_F56,
  173. PT_F57,
  174. PT_F58,
  175. PT_F59,
  176. PT_F60,
  177. PT_F61,
  178. PT_F62,
  179. PT_F63,
  180. PT_F64,
  181. PT_F65,
  182. PT_F66,
  183. PT_F67,
  184. PT_F68,
  185. PT_F69,
  186. PT_F70,
  187. PT_F71,
  188. PT_F72,
  189. PT_F73,
  190. PT_F74,
  191. PT_F75,
  192. PT_F76,
  193. PT_F77,
  194. PT_F78,
  195. PT_F79,
  196. PT_F80,
  197. PT_F81,
  198. PT_F82,
  199. PT_F83,
  200. PT_F84,
  201. PT_F85,
  202. PT_F86,
  203. PT_F87,
  204. PT_F88,
  205. PT_F89,
  206. PT_F90,
  207. PT_F91,
  208. PT_F92,
  209. PT_F93,
  210. PT_F94,
  211. PT_F95,
  212. PT_F96,
  213. PT_F97,
  214. PT_F98,
  215. PT_F99,
  216. PT_F100,
  217. PT_F101,
  218. PT_F102,
  219. PT_F103,
  220. PT_F104,
  221. PT_F105,
  222. PT_F106,
  223. PT_F107,
  224. PT_F108,
  225. PT_F109,
  226. PT_F110,
  227. PT_F111,
  228. PT_F112,
  229. PT_F113,
  230. PT_F114,
  231. PT_F115,
  232. PT_F116,
  233. PT_F117,
  234. PT_F118,
  235. PT_F119,
  236. PT_F120,
  237. PT_F121,
  238. PT_F122,
  239. PT_F123,
  240. PT_F124,
  241. PT_F125,
  242. PT_F126,
  243. PT_F127,
  244. /* Predicate registers - we don't fetch these individually. */
  245. -1, -1, -1, -1, -1, -1, -1, -1,
  246. -1, -1, -1, -1, -1, -1, -1, -1,
  247. -1, -1, -1, -1, -1, -1, -1, -1,
  248. -1, -1, -1, -1, -1, -1, -1, -1,
  249. -1, -1, -1, -1, -1, -1, -1, -1,
  250. -1, -1, -1, -1, -1, -1, -1, -1,
  251. -1, -1, -1, -1, -1, -1, -1, -1,
  252. -1, -1, -1, -1, -1, -1, -1, -1,
  253. /* branch registers */
  254. PT_B0,
  255. PT_B1,
  256. PT_B2,
  257. PT_B3,
  258. PT_B4,
  259. PT_B5,
  260. PT_B6,
  261. PT_B7,
  262. /* Virtual frame pointer and virtual return address pointer. */
  263. -1, -1,
  264. /* other registers */
  265. PT_PR,
  266. PT_CR_IIP, /* ip */
  267. PT_CR_IPSR, /* psr */
  268. PT_CFM, /* cfm */
  269. /* kernel registers not visible via ptrace interface (?) */
  270. -1, -1, -1, -1, -1, -1, -1, -1,
  271. /* hole */
  272. -1, -1, -1, -1, -1, -1, -1, -1,
  273. PT_AR_RSC,
  274. PT_AR_BSP,
  275. PT_AR_BSPSTORE,
  276. PT_AR_RNAT,
  277. -1,
  278. -1, /* Not available: FCR, IA32 floating control register. */
  279. -1, -1,
  280. -1, /* Not available: EFLAG */
  281. -1, /* Not available: CSD */
  282. -1, /* Not available: SSD */
  283. -1, /* Not available: CFLG */
  284. -1, /* Not available: FSR */
  285. -1, /* Not available: FIR */
  286. -1, /* Not available: FDR */
  287. -1,
  288. PT_AR_CCV,
  289. -1, -1, -1,
  290. PT_AR_UNAT,
  291. -1, -1, -1,
  292. PT_AR_FPSR,
  293. -1, -1, -1,
  294. -1, /* Not available: ITC */
  295. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  296. -1, -1, -1, -1, -1, -1, -1, -1, -1,
  297. PT_AR_PFS,
  298. PT_AR_LC,
  299. PT_AR_EC,
  300. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  301. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  302. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  303. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  304. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  305. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  306. -1,
  307. /* nat bits - not fetched directly; instead we obtain these bits from
  308. either rnat or unat or from memory. */
  309. -1, -1, -1, -1, -1, -1, -1, -1,
  310. -1, -1, -1, -1, -1, -1, -1, -1,
  311. -1, -1, -1, -1, -1, -1, -1, -1,
  312. -1, -1, -1, -1, -1, -1, -1, -1,
  313. -1, -1, -1, -1, -1, -1, -1, -1,
  314. -1, -1, -1, -1, -1, -1, -1, -1,
  315. -1, -1, -1, -1, -1, -1, -1, -1,
  316. -1, -1, -1, -1, -1, -1, -1, -1,
  317. -1, -1, -1, -1, -1, -1, -1, -1,
  318. -1, -1, -1, -1, -1, -1, -1, -1,
  319. -1, -1, -1, -1, -1, -1, -1, -1,
  320. -1, -1, -1, -1, -1, -1, -1, -1,
  321. -1, -1, -1, -1, -1, -1, -1, -1,
  322. -1, -1, -1, -1, -1, -1, -1, -1,
  323. -1, -1, -1, -1, -1, -1, -1, -1,
  324. -1, -1, -1, -1, -1, -1, -1, -1,
  325. };
  326. static CORE_ADDR
  327. ia64_register_addr (struct gdbarch *gdbarch, int regno)
  328. {
  329. CORE_ADDR addr;
  330. if (regno < 0 || regno >= gdbarch_num_regs (gdbarch))
  331. error (_("Invalid register number %d."), regno);
  332. if (u_offsets[regno] == -1)
  333. addr = 0;
  334. else
  335. addr = (CORE_ADDR) u_offsets[regno];
  336. return addr;
  337. }
  338. static int
  339. ia64_cannot_fetch_register (struct gdbarch *gdbarch, int regno)
  340. {
  341. return regno < 0
  342. || regno >= gdbarch_num_regs (gdbarch)
  343. || u_offsets[regno] == -1;
  344. }
  345. static int
  346. ia64_cannot_store_register (struct gdbarch *gdbarch, int regno)
  347. {
  348. /* Rationale behind not permitting stores to bspstore...
  349. The IA-64 architecture provides bspstore and bsp which refer
  350. memory locations in the RSE's backing store. bspstore is the
  351. next location which will be written when the RSE needs to write
  352. to memory. bsp is the address at which r32 in the current frame
  353. would be found if it were written to the backing store.
  354. The IA-64 architecture provides read-only access to bsp and
  355. read/write access to bspstore (but only when the RSE is in
  356. the enforced lazy mode). It should be noted that stores
  357. to bspstore also affect the value of bsp. Changing bspstore
  358. does not affect the number of dirty entries between bspstore
  359. and bsp, so changing bspstore by N words will also cause bsp
  360. to be changed by (roughly) N as well. (It could be N-1 or N+1
  361. depending upon where the NaT collection bits fall.)
  362. OTOH, the Linux kernel provides read/write access to bsp (and
  363. currently read/write access to bspstore as well). But it
  364. is definitely the case that if you change one, the other
  365. will change at the same time. It is more useful to gdb to
  366. be able to change bsp. So in order to prevent strange and
  367. undesirable things from happening when a dummy stack frame
  368. is popped (after calling an inferior function), we allow
  369. bspstore to be read, but not written. (Note that popping
  370. a (generic) dummy stack frame causes all registers that
  371. were previously read from the inferior process to be written
  372. back.) */
  373. return regno < 0
  374. || regno >= gdbarch_num_regs (gdbarch)
  375. || u_offsets[regno] == -1
  376. || regno == IA64_BSPSTORE_REGNUM;
  377. }
  378. void
  379. supply_gregset (struct regcache *regcache, const gregset_t *gregsetp)
  380. {
  381. int regi;
  382. const greg_t *regp = (const greg_t *) gregsetp;
  383. for (regi = IA64_GR0_REGNUM; regi <= IA64_GR31_REGNUM; regi++)
  384. {
  385. regcache->raw_supply (regi, regp + (regi - IA64_GR0_REGNUM));
  386. }
  387. /* FIXME: NAT collection bits are at index 32; gotta deal with these
  388. somehow... */
  389. regcache->raw_supply (IA64_PR_REGNUM, regp + 33);
  390. for (regi = IA64_BR0_REGNUM; regi <= IA64_BR7_REGNUM; regi++)
  391. {
  392. regcache->raw_supply (regi, regp + 34 + (regi - IA64_BR0_REGNUM));
  393. }
  394. regcache->raw_supply (IA64_IP_REGNUM, regp + 42);
  395. regcache->raw_supply (IA64_CFM_REGNUM, regp + 43);
  396. regcache->raw_supply (IA64_PSR_REGNUM, regp + 44);
  397. regcache->raw_supply (IA64_RSC_REGNUM, regp + 45);
  398. regcache->raw_supply (IA64_BSP_REGNUM, regp + 46);
  399. regcache->raw_supply (IA64_BSPSTORE_REGNUM, regp + 47);
  400. regcache->raw_supply (IA64_RNAT_REGNUM, regp + 48);
  401. regcache->raw_supply (IA64_CCV_REGNUM, regp + 49);
  402. regcache->raw_supply (IA64_UNAT_REGNUM, regp + 50);
  403. regcache->raw_supply (IA64_FPSR_REGNUM, regp + 51);
  404. regcache->raw_supply (IA64_PFS_REGNUM, regp + 52);
  405. regcache->raw_supply (IA64_LC_REGNUM, regp + 53);
  406. regcache->raw_supply (IA64_EC_REGNUM, regp + 54);
  407. }
  408. void
  409. fill_gregset (const struct regcache *regcache, gregset_t *gregsetp, int regno)
  410. {
  411. int regi;
  412. greg_t *regp = (greg_t *) gregsetp;
  413. #define COPY_REG(_idx_,_regi_) \
  414. if ((regno == -1) || regno == _regi_) \
  415. regcache->raw_collect (_regi_, regp + _idx_)
  416. for (regi = IA64_GR0_REGNUM; regi <= IA64_GR31_REGNUM; regi++)
  417. {
  418. COPY_REG (regi - IA64_GR0_REGNUM, regi);
  419. }
  420. /* FIXME: NAT collection bits at index 32? */
  421. COPY_REG (33, IA64_PR_REGNUM);
  422. for (regi = IA64_BR0_REGNUM; regi <= IA64_BR7_REGNUM; regi++)
  423. {
  424. COPY_REG (34 + (regi - IA64_BR0_REGNUM), regi);
  425. }
  426. COPY_REG (42, IA64_IP_REGNUM);
  427. COPY_REG (43, IA64_CFM_REGNUM);
  428. COPY_REG (44, IA64_PSR_REGNUM);
  429. COPY_REG (45, IA64_RSC_REGNUM);
  430. COPY_REG (46, IA64_BSP_REGNUM);
  431. COPY_REG (47, IA64_BSPSTORE_REGNUM);
  432. COPY_REG (48, IA64_RNAT_REGNUM);
  433. COPY_REG (49, IA64_CCV_REGNUM);
  434. COPY_REG (50, IA64_UNAT_REGNUM);
  435. COPY_REG (51, IA64_FPSR_REGNUM);
  436. COPY_REG (52, IA64_PFS_REGNUM);
  437. COPY_REG (53, IA64_LC_REGNUM);
  438. COPY_REG (54, IA64_EC_REGNUM);
  439. }
  440. /* Given a pointer to a floating point register set in /proc format
  441. (fpregset_t *), unpack the register contents and supply them as gdb's
  442. idea of the current floating point register values. */
  443. void
  444. supply_fpregset (struct regcache *regcache, const fpregset_t *fpregsetp)
  445. {
  446. int regi;
  447. const char *from;
  448. const gdb_byte f_zero[16] = { 0 };
  449. const gdb_byte f_one[16] =
  450. { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0 };
  451. /* Kernel generated cores have fr1==0 instead of 1.0. Older GDBs
  452. did the same. So ignore whatever might be recorded in fpregset_t
  453. for fr0/fr1 and always supply their expected values. */
  454. /* fr0 is always read as zero. */
  455. regcache->raw_supply (IA64_FR0_REGNUM, f_zero);
  456. /* fr1 is always read as one (1.0). */
  457. regcache->raw_supply (IA64_FR1_REGNUM, f_one);
  458. for (regi = IA64_FR2_REGNUM; regi <= IA64_FR127_REGNUM; regi++)
  459. {
  460. from = (const char *) &((*fpregsetp)[regi - IA64_FR0_REGNUM]);
  461. regcache->raw_supply (regi, from);
  462. }
  463. }
  464. /* Given a pointer to a floating point register set in /proc format
  465. (fpregset_t *), update the register specified by REGNO from gdb's idea
  466. of the current floating point register set. If REGNO is -1, update
  467. them all. */
  468. void
  469. fill_fpregset (const struct regcache *regcache,
  470. fpregset_t *fpregsetp, int regno)
  471. {
  472. int regi;
  473. for (regi = IA64_FR0_REGNUM; regi <= IA64_FR127_REGNUM; regi++)
  474. {
  475. if ((regno == -1) || (regno == regi))
  476. regcache->raw_collect (regi, &((*fpregsetp)[regi - IA64_FR0_REGNUM]));
  477. }
  478. }
  479. #define IA64_PSR_DB (1UL << 24)
  480. #define IA64_PSR_DD (1UL << 39)
  481. void
  482. ia64_linux_nat_target::enable_watchpoints_in_psr (ptid_t ptid)
  483. {
  484. struct regcache *regcache = get_thread_regcache (this, ptid);
  485. ULONGEST psr;
  486. regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr);
  487. if (!(psr & IA64_PSR_DB))
  488. {
  489. psr |= IA64_PSR_DB; /* Set the db bit - this enables hardware
  490. watchpoints and breakpoints. */
  491. regcache_cooked_write_unsigned (regcache, IA64_PSR_REGNUM, psr);
  492. }
  493. }
  494. static long debug_registers[8];
  495. static void
  496. store_debug_register (ptid_t ptid, int idx, long val)
  497. {
  498. int tid;
  499. tid = ptid.lwp ();
  500. if (tid == 0)
  501. tid = ptid.pid ();
  502. (void) ptrace (PT_WRITE_U, tid, (PTRACE_TYPE_ARG3) (PT_DBR + 8 * idx), val);
  503. }
  504. static void
  505. store_debug_register_pair (ptid_t ptid, int idx, long *dbr_addr,
  506. long *dbr_mask)
  507. {
  508. if (dbr_addr)
  509. store_debug_register (ptid, 2 * idx, *dbr_addr);
  510. if (dbr_mask)
  511. store_debug_register (ptid, 2 * idx + 1, *dbr_mask);
  512. }
  513. static int
  514. is_power_of_2 (int val)
  515. {
  516. int i, onecount;
  517. onecount = 0;
  518. for (i = 0; i < 8 * sizeof (val); i++)
  519. if (val & (1 << i))
  520. onecount++;
  521. return onecount <= 1;
  522. }
  523. int
  524. ia64_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
  525. enum target_hw_bp_type type,
  526. struct expression *cond)
  527. {
  528. int idx;
  529. long dbr_addr, dbr_mask;
  530. int max_watchpoints = 4;
  531. if (len <= 0 || !is_power_of_2 (len))
  532. return -1;
  533. for (idx = 0; idx < max_watchpoints; idx++)
  534. {
  535. dbr_mask = debug_registers[idx * 2 + 1];
  536. if ((dbr_mask & (0x3UL << 62)) == 0)
  537. {
  538. /* Exit loop if both r and w bits clear. */
  539. break;
  540. }
  541. }
  542. if (idx == max_watchpoints)
  543. return -1;
  544. dbr_addr = (long) addr;
  545. dbr_mask = (~(len - 1) & 0x00ffffffffffffffL); /* construct mask to match */
  546. dbr_mask |= 0x0800000000000000L; /* Only match privilege level 3 */
  547. switch (type)
  548. {
  549. case hw_write:
  550. dbr_mask |= (1L << 62); /* Set w bit */
  551. break;
  552. case hw_read:
  553. dbr_mask |= (1L << 63); /* Set r bit */
  554. break;
  555. case hw_access:
  556. dbr_mask |= (3L << 62); /* Set both r and w bits */
  557. break;
  558. default:
  559. return -1;
  560. }
  561. debug_registers[2 * idx] = dbr_addr;
  562. debug_registers[2 * idx + 1] = dbr_mask;
  563. for (const lwp_info *lp : all_lwps ())
  564. {
  565. store_debug_register_pair (lp->ptid, idx, &dbr_addr, &dbr_mask);
  566. enable_watchpoints_in_psr (lp->ptid);
  567. }
  568. return 0;
  569. }
  570. int
  571. ia64_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
  572. enum target_hw_bp_type type,
  573. struct expression *cond)
  574. {
  575. int idx;
  576. long dbr_addr, dbr_mask;
  577. int max_watchpoints = 4;
  578. if (len <= 0 || !is_power_of_2 (len))
  579. return -1;
  580. for (idx = 0; idx < max_watchpoints; idx++)
  581. {
  582. dbr_addr = debug_registers[2 * idx];
  583. dbr_mask = debug_registers[2 * idx + 1];
  584. if ((dbr_mask & (0x3UL << 62)) && addr == (CORE_ADDR) dbr_addr)
  585. {
  586. debug_registers[2 * idx] = 0;
  587. debug_registers[2 * idx + 1] = 0;
  588. dbr_addr = 0;
  589. dbr_mask = 0;
  590. for (const lwp_info *lp : all_lwps ())
  591. store_debug_register_pair (lp->ptid, idx, &dbr_addr, &dbr_mask);
  592. return 0;
  593. }
  594. }
  595. return -1;
  596. }
  597. void
  598. ia64_linux_nat_target::low_new_thread (struct lwp_info *lp)
  599. {
  600. int i, any;
  601. any = 0;
  602. for (i = 0; i < 8; i++)
  603. {
  604. if (debug_registers[i] != 0)
  605. any = 1;
  606. store_debug_register (lp->ptid, i, debug_registers[i]);
  607. }
  608. if (any)
  609. enable_watchpoints_in_psr (lp->ptid);
  610. }
  611. bool
  612. ia64_linux_nat_target::stopped_data_address (CORE_ADDR *addr_p)
  613. {
  614. CORE_ADDR psr;
  615. siginfo_t siginfo;
  616. struct regcache *regcache = get_current_regcache ();
  617. if (!linux_nat_get_siginfo (inferior_ptid, &siginfo))
  618. return false;
  619. if (siginfo.si_signo != SIGTRAP
  620. || (siginfo.si_code & 0xffff) != 0x0004 /* TRAP_HWBKPT */)
  621. return false;
  622. regcache_cooked_read_unsigned (regcache, IA64_PSR_REGNUM, &psr);
  623. psr |= IA64_PSR_DD; /* Set the dd bit - this will disable the watchpoint
  624. for the next instruction. */
  625. regcache_cooked_write_unsigned (regcache, IA64_PSR_REGNUM, psr);
  626. *addr_p = (CORE_ADDR) siginfo.si_addr;
  627. return true;
  628. }
  629. bool
  630. ia64_linux_nat_target::stopped_by_watchpoint ()
  631. {
  632. CORE_ADDR addr;
  633. return stopped_data_address (&addr);
  634. }
  635. int
  636. ia64_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
  637. int cnt, int othertype)
  638. {
  639. return 1;
  640. }
  641. /* Fetch register REGNUM from the inferior. */
  642. static void
  643. ia64_linux_fetch_register (struct regcache *regcache, int regnum)
  644. {
  645. struct gdbarch *gdbarch = regcache->arch ();
  646. CORE_ADDR addr;
  647. size_t size;
  648. PTRACE_TYPE_RET *buf;
  649. pid_t pid;
  650. int i;
  651. /* r0 cannot be fetched but is always zero. */
  652. if (regnum == IA64_GR0_REGNUM)
  653. {
  654. const gdb_byte zero[8] = { 0 };
  655. gdb_assert (sizeof (zero) == register_size (gdbarch, regnum));
  656. regcache->raw_supply (regnum, zero);
  657. return;
  658. }
  659. /* fr0 cannot be fetched but is always zero. */
  660. if (regnum == IA64_FR0_REGNUM)
  661. {
  662. const gdb_byte f_zero[16] = { 0 };
  663. gdb_assert (sizeof (f_zero) == register_size (gdbarch, regnum));
  664. regcache->raw_supply (regnum, f_zero);
  665. return;
  666. }
  667. /* fr1 cannot be fetched but is always one (1.0). */
  668. if (regnum == IA64_FR1_REGNUM)
  669. {
  670. const gdb_byte f_one[16] =
  671. { 0, 0, 0, 0, 0, 0, 0, 0x80, 0xff, 0xff, 0, 0, 0, 0, 0, 0 };
  672. gdb_assert (sizeof (f_one) == register_size (gdbarch, regnum));
  673. regcache->raw_supply (regnum, f_one);
  674. return;
  675. }
  676. if (ia64_cannot_fetch_register (gdbarch, regnum))
  677. {
  678. regcache->raw_supply (regnum, NULL);
  679. return;
  680. }
  681. pid = get_ptrace_pid (regcache->ptid ());
  682. /* This isn't really an address, but ptrace thinks of it as one. */
  683. addr = ia64_register_addr (gdbarch, regnum);
  684. size = register_size (gdbarch, regnum);
  685. gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
  686. buf = (PTRACE_TYPE_RET *) alloca (size);
  687. /* Read the register contents from the inferior a chunk at a time. */
  688. for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
  689. {
  690. errno = 0;
  691. buf[i] = ptrace (PT_READ_U, pid, (PTRACE_TYPE_ARG3)addr, 0);
  692. if (errno != 0)
  693. error (_("Couldn't read register %s (#%d): %s."),
  694. gdbarch_register_name (gdbarch, regnum),
  695. regnum, safe_strerror (errno));
  696. addr += sizeof (PTRACE_TYPE_RET);
  697. }
  698. regcache->raw_supply (regnum, buf);
  699. }
  700. /* Fetch register REGNUM from the inferior. If REGNUM is -1, do this
  701. for all registers. */
  702. void
  703. ia64_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  704. {
  705. if (regnum == -1)
  706. for (regnum = 0;
  707. regnum < gdbarch_num_regs (regcache->arch ());
  708. regnum++)
  709. ia64_linux_fetch_register (regcache, regnum);
  710. else
  711. ia64_linux_fetch_register (regcache, regnum);
  712. }
  713. /* Store register REGNUM into the inferior. */
  714. static void
  715. ia64_linux_store_register (const struct regcache *regcache, int regnum)
  716. {
  717. struct gdbarch *gdbarch = regcache->arch ();
  718. CORE_ADDR addr;
  719. size_t size;
  720. PTRACE_TYPE_RET *buf;
  721. pid_t pid;
  722. int i;
  723. if (ia64_cannot_store_register (gdbarch, regnum))
  724. return;
  725. pid = get_ptrace_pid (regcache->ptid ());
  726. /* This isn't really an address, but ptrace thinks of it as one. */
  727. addr = ia64_register_addr (gdbarch, regnum);
  728. size = register_size (gdbarch, regnum);
  729. gdb_assert ((size % sizeof (PTRACE_TYPE_RET)) == 0);
  730. buf = (PTRACE_TYPE_RET *) alloca (size);
  731. /* Write the register contents into the inferior a chunk at a time. */
  732. regcache->raw_collect (regnum, buf);
  733. for (i = 0; i < size / sizeof (PTRACE_TYPE_RET); i++)
  734. {
  735. errno = 0;
  736. ptrace (PT_WRITE_U, pid, (PTRACE_TYPE_ARG3)addr, buf[i]);
  737. if (errno != 0)
  738. error (_("Couldn't write register %s (#%d): %s."),
  739. gdbarch_register_name (gdbarch, regnum),
  740. regnum, safe_strerror (errno));
  741. addr += sizeof (PTRACE_TYPE_RET);
  742. }
  743. }
  744. /* Store register REGNUM back into the inferior. If REGNUM is -1, do
  745. this for all registers. */
  746. void
  747. ia64_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
  748. {
  749. if (regnum == -1)
  750. for (regnum = 0;
  751. regnum < gdbarch_num_regs (regcache->arch ());
  752. regnum++)
  753. ia64_linux_store_register (regcache, regnum);
  754. else
  755. ia64_linux_store_register (regcache, regnum);
  756. }
  757. /* Implement the xfer_partial target_ops method. */
  758. enum target_xfer_status
  759. ia64_linux_nat_target::xfer_partial (enum target_object object,
  760. const char *annex,
  761. gdb_byte *readbuf, const gdb_byte *writebuf,
  762. ULONGEST offset, ULONGEST len,
  763. ULONGEST *xfered_len)
  764. {
  765. if (object == TARGET_OBJECT_UNWIND_TABLE && readbuf != NULL)
  766. {
  767. static long gate_table_size;
  768. gdb_byte *tmp_buf;
  769. long res;
  770. /* Probe for the table size once. */
  771. if (gate_table_size == 0)
  772. gate_table_size = syscall (__NR_getunwind, NULL, 0);
  773. if (gate_table_size < 0)
  774. return TARGET_XFER_E_IO;
  775. if (offset >= gate_table_size)
  776. return TARGET_XFER_EOF;
  777. tmp_buf = (gdb_byte *) alloca (gate_table_size);
  778. res = syscall (__NR_getunwind, tmp_buf, gate_table_size);
  779. if (res < 0)
  780. return TARGET_XFER_E_IO;
  781. gdb_assert (res == gate_table_size);
  782. if (offset + len > gate_table_size)
  783. len = gate_table_size - offset;
  784. memcpy (readbuf, tmp_buf + offset, len);
  785. *xfered_len = len;
  786. return TARGET_XFER_OK;
  787. }
  788. return linux_nat_target::xfer_partial (object, annex, readbuf, writebuf,
  789. offset, len, xfered_len);
  790. }
  791. /* For break.b instruction ia64 CPU forgets the immediate value and generates
  792. SIGILL with ILL_ILLOPC instead of more common SIGTRAP with TRAP_BRKPT.
  793. ia64 does not use gdbarch_decr_pc_after_break so we do not have to make any
  794. difference for the signals here. */
  795. bool
  796. ia64_linux_nat_target::low_status_is_event (int status)
  797. {
  798. return WIFSTOPPED (status) && (WSTOPSIG (status) == SIGTRAP
  799. || WSTOPSIG (status) == SIGILL);
  800. }
  801. void _initialize_ia64_linux_nat ();
  802. void
  803. _initialize_ia64_linux_nat ()
  804. {
  805. /* Register the target. */
  806. linux_target = &the_ia64_linux_nat_target;
  807. add_inf_child_target (&the_ia64_linux_nat_target);
  808. }