s390-linux-nat.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. /* S390 native-dependent code for GDB, the GNU debugger.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com)
  4. for IBM Deutschland Entwicklung GmbH, IBM Corporation.
  5. This file is part of GDB.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include "defs.h"
  17. #include "regcache.h"
  18. #include "inferior.h"
  19. #include "target.h"
  20. #include "linux-nat.h"
  21. #include "auxv.h"
  22. #include "gregset.h"
  23. #include "regset.h"
  24. #include "nat/linux-ptrace.h"
  25. #include "gdbcmd.h"
  26. #include "gdbarch.h"
  27. #include "s390-tdep.h"
  28. #include "s390-linux-tdep.h"
  29. #include "elf/common.h"
  30. #include <asm/ptrace.h>
  31. #include "nat/gdb_ptrace.h"
  32. #include <asm/types.h>
  33. #include <sys/procfs.h>
  34. #include <sys/ucontext.h>
  35. #include <elf.h>
  36. #include <algorithm>
  37. #include "inf-ptrace.h"
  38. #include "linux-tdep.h"
  39. /* Per-thread arch-specific data. */
  40. struct arch_lwp_info
  41. {
  42. /* Non-zero if the thread's PER info must be re-written. */
  43. int per_info_changed;
  44. };
  45. static int have_regset_last_break = 0;
  46. static int have_regset_system_call = 0;
  47. static int have_regset_tdb = 0;
  48. static int have_regset_vxrs = 0;
  49. static int have_regset_gs = 0;
  50. /* Register map for 32-bit executables running under a 64-bit
  51. kernel. */
  52. #ifdef __s390x__
  53. static const struct regcache_map_entry s390_64_regmap_gregset[] =
  54. {
  55. /* Skip PSWM and PSWA, since they must be handled specially. */
  56. { 2, REGCACHE_MAP_SKIP, 8 },
  57. { 1, S390_R0_UPPER_REGNUM, 4 }, { 1, S390_R0_REGNUM, 4 },
  58. { 1, S390_R1_UPPER_REGNUM, 4 }, { 1, S390_R1_REGNUM, 4 },
  59. { 1, S390_R2_UPPER_REGNUM, 4 }, { 1, S390_R2_REGNUM, 4 },
  60. { 1, S390_R3_UPPER_REGNUM, 4 }, { 1, S390_R3_REGNUM, 4 },
  61. { 1, S390_R4_UPPER_REGNUM, 4 }, { 1, S390_R4_REGNUM, 4 },
  62. { 1, S390_R5_UPPER_REGNUM, 4 }, { 1, S390_R5_REGNUM, 4 },
  63. { 1, S390_R6_UPPER_REGNUM, 4 }, { 1, S390_R6_REGNUM, 4 },
  64. { 1, S390_R7_UPPER_REGNUM, 4 }, { 1, S390_R7_REGNUM, 4 },
  65. { 1, S390_R8_UPPER_REGNUM, 4 }, { 1, S390_R8_REGNUM, 4 },
  66. { 1, S390_R9_UPPER_REGNUM, 4 }, { 1, S390_R9_REGNUM, 4 },
  67. { 1, S390_R10_UPPER_REGNUM, 4 }, { 1, S390_R10_REGNUM, 4 },
  68. { 1, S390_R11_UPPER_REGNUM, 4 }, { 1, S390_R11_REGNUM, 4 },
  69. { 1, S390_R12_UPPER_REGNUM, 4 }, { 1, S390_R12_REGNUM, 4 },
  70. { 1, S390_R13_UPPER_REGNUM, 4 }, { 1, S390_R13_REGNUM, 4 },
  71. { 1, S390_R14_UPPER_REGNUM, 4 }, { 1, S390_R14_REGNUM, 4 },
  72. { 1, S390_R15_UPPER_REGNUM, 4 }, { 1, S390_R15_REGNUM, 4 },
  73. { 16, S390_A0_REGNUM, 4 },
  74. { 1, REGCACHE_MAP_SKIP, 4 }, { 1, S390_ORIG_R2_REGNUM, 4 },
  75. { 0 }
  76. };
  77. static const struct regset s390_64_gregset =
  78. {
  79. s390_64_regmap_gregset,
  80. regcache_supply_regset,
  81. regcache_collect_regset
  82. };
  83. #define S390_PSWM_OFFSET 0
  84. #define S390_PSWA_OFFSET 8
  85. #endif
  86. /* PER-event mask bits and PER control bits (CR9). */
  87. #define PER_BIT(n) (1UL << (63 - (n)))
  88. #define PER_EVENT_BRANCH PER_BIT (32)
  89. #define PER_EVENT_IFETCH PER_BIT (33)
  90. #define PER_EVENT_STORE PER_BIT (34)
  91. #define PER_EVENT_NULLIFICATION PER_BIT (39)
  92. #define PER_CONTROL_BRANCH_ADDRESS PER_BIT (40)
  93. #define PER_CONTROL_SUSPENSION PER_BIT (41)
  94. #define PER_CONTROL_ALTERATION PER_BIT (42)
  95. class s390_linux_nat_target final : public linux_nat_target
  96. {
  97. public:
  98. /* Add our register access methods. */
  99. void fetch_registers (struct regcache *, int) override;
  100. void store_registers (struct regcache *, int) override;
  101. /* Add our watchpoint methods. */
  102. int can_use_hw_breakpoint (enum bptype, int, int) override;
  103. int insert_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
  104. override;
  105. int remove_hw_breakpoint (struct gdbarch *, struct bp_target_info *)
  106. override;
  107. int region_ok_for_hw_watchpoint (CORE_ADDR, int) override;
  108. bool stopped_by_watchpoint () override;
  109. int insert_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  110. struct expression *) override;
  111. int remove_watchpoint (CORE_ADDR, int, enum target_hw_bp_type,
  112. struct expression *) override;
  113. /* Detect target architecture. */
  114. const struct target_desc *read_description () override;
  115. int auxv_parse (gdb_byte **readptr,
  116. gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp)
  117. override;
  118. /* Override linux_nat_target low methods. */
  119. void low_new_thread (struct lwp_info *lp) override;
  120. void low_delete_thread (struct arch_lwp_info *lp) override;
  121. void low_prepare_to_resume (struct lwp_info *lp) override;
  122. void low_new_fork (struct lwp_info *parent, pid_t child_pid) override;
  123. void low_forget_process (pid_t pid) override;
  124. };
  125. static s390_linux_nat_target the_s390_linux_nat_target;
  126. /* Fill GDB's register array with the general-purpose register values
  127. in *REGP.
  128. When debugging a 32-bit executable running under a 64-bit kernel,
  129. we have to fix up the 64-bit registers we get from the kernel to
  130. make them look like 32-bit registers. */
  131. void
  132. supply_gregset (struct regcache *regcache, const gregset_t *regp)
  133. {
  134. #ifdef __s390x__
  135. struct gdbarch *gdbarch = regcache->arch ();
  136. if (gdbarch_ptr_bit (gdbarch) == 32)
  137. {
  138. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  139. ULONGEST pswm, pswa;
  140. gdb_byte buf[4];
  141. regcache_supply_regset (&s390_64_gregset, regcache, -1,
  142. regp, sizeof (gregset_t));
  143. pswm = extract_unsigned_integer ((const gdb_byte *) regp
  144. + S390_PSWM_OFFSET, 8, byte_order);
  145. pswa = extract_unsigned_integer ((const gdb_byte *) regp
  146. + S390_PSWA_OFFSET, 8, byte_order);
  147. store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000);
  148. regcache->raw_supply (S390_PSWM_REGNUM, buf);
  149. store_unsigned_integer (buf, 4, byte_order,
  150. (pswa & 0x7fffffff) | (pswm & 0x80000000));
  151. regcache->raw_supply (S390_PSWA_REGNUM, buf);
  152. return;
  153. }
  154. #endif
  155. regcache_supply_regset (&s390_gregset, regcache, -1, regp,
  156. sizeof (gregset_t));
  157. }
  158. /* Fill register REGNO (if it is a general-purpose register) in
  159. *REGP with the value in GDB's register array. If REGNO is -1,
  160. do this for all registers. */
  161. void
  162. fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno)
  163. {
  164. #ifdef __s390x__
  165. struct gdbarch *gdbarch = regcache->arch ();
  166. if (gdbarch_ptr_bit (gdbarch) == 32)
  167. {
  168. regcache_collect_regset (&s390_64_gregset, regcache, regno,
  169. regp, sizeof (gregset_t));
  170. if (regno == -1
  171. || regno == S390_PSWM_REGNUM || regno == S390_PSWA_REGNUM)
  172. {
  173. enum bfd_endian byte_order = gdbarch_byte_order (gdbarch);
  174. ULONGEST pswa, pswm;
  175. gdb_byte buf[4];
  176. gdb_byte *pswm_p = (gdb_byte *) regp + S390_PSWM_OFFSET;
  177. gdb_byte *pswa_p = (gdb_byte *) regp + S390_PSWA_OFFSET;
  178. pswm = extract_unsigned_integer (pswm_p, 8, byte_order);
  179. if (regno == -1 || regno == S390_PSWM_REGNUM)
  180. {
  181. pswm &= 0x80000000;
  182. regcache->raw_collect (S390_PSWM_REGNUM, buf);
  183. pswm |= (extract_unsigned_integer (buf, 4, byte_order)
  184. & 0xfff7ffff) << 32;
  185. }
  186. if (regno == -1 || regno == S390_PSWA_REGNUM)
  187. {
  188. regcache->raw_collect (S390_PSWA_REGNUM, buf);
  189. pswa = extract_unsigned_integer (buf, 4, byte_order);
  190. pswm ^= (pswm ^ pswa) & 0x80000000;
  191. pswa &= 0x7fffffff;
  192. store_unsigned_integer (pswa_p, 8, byte_order, pswa);
  193. }
  194. store_unsigned_integer (pswm_p, 8, byte_order, pswm);
  195. }
  196. return;
  197. }
  198. #endif
  199. regcache_collect_regset (&s390_gregset, regcache, regno, regp,
  200. sizeof (gregset_t));
  201. }
  202. /* Fill GDB's register array with the floating-point register values
  203. in *REGP. */
  204. void
  205. supply_fpregset (struct regcache *regcache, const fpregset_t *regp)
  206. {
  207. regcache_supply_regset (&s390_fpregset, regcache, -1, regp,
  208. sizeof (fpregset_t));
  209. }
  210. /* Fill register REGNO (if it is a general-purpose register) in
  211. *REGP with the value in GDB's register array. If REGNO is -1,
  212. do this for all registers. */
  213. void
  214. fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno)
  215. {
  216. regcache_collect_regset (&s390_fpregset, regcache, regno, regp,
  217. sizeof (fpregset_t));
  218. }
  219. /* Find the TID for the current inferior thread to use with ptrace. */
  220. static int
  221. s390_inferior_tid (void)
  222. {
  223. /* GNU/Linux LWP ID's are process ID's. */
  224. int tid = inferior_ptid.lwp ();
  225. if (tid == 0)
  226. tid = inferior_ptid.pid (); /* Not a threaded program. */
  227. return tid;
  228. }
  229. /* Fetch all general-purpose registers from process/thread TID and
  230. store their values in GDB's register cache. */
  231. static void
  232. fetch_regs (struct regcache *regcache, int tid)
  233. {
  234. gregset_t regs;
  235. ptrace_area parea;
  236. parea.len = sizeof (regs);
  237. parea.process_addr = (addr_t) &regs;
  238. parea.kernel_addr = offsetof (struct user_regs_struct, psw);
  239. if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
  240. perror_with_name (_("Couldn't get registers"));
  241. supply_gregset (regcache, (const gregset_t *) &regs);
  242. }
  243. /* Store all valid general-purpose registers in GDB's register cache
  244. into the process/thread specified by TID. */
  245. static void
  246. store_regs (const struct regcache *regcache, int tid, int regnum)
  247. {
  248. gregset_t regs;
  249. ptrace_area parea;
  250. parea.len = sizeof (regs);
  251. parea.process_addr = (addr_t) &regs;
  252. parea.kernel_addr = offsetof (struct user_regs_struct, psw);
  253. if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
  254. perror_with_name (_("Couldn't get registers"));
  255. fill_gregset (regcache, &regs, regnum);
  256. if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea, 0) < 0)
  257. perror_with_name (_("Couldn't write registers"));
  258. }
  259. /* Fetch all floating-point registers from process/thread TID and store
  260. their values in GDB's register cache. */
  261. static void
  262. fetch_fpregs (struct regcache *regcache, int tid)
  263. {
  264. fpregset_t fpregs;
  265. ptrace_area parea;
  266. parea.len = sizeof (fpregs);
  267. parea.process_addr = (addr_t) &fpregs;
  268. parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
  269. if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
  270. perror_with_name (_("Couldn't get floating point status"));
  271. supply_fpregset (regcache, (const fpregset_t *) &fpregs);
  272. }
  273. /* Store all valid floating-point registers in GDB's register cache
  274. into the process/thread specified by TID. */
  275. static void
  276. store_fpregs (const struct regcache *regcache, int tid, int regnum)
  277. {
  278. fpregset_t fpregs;
  279. ptrace_area parea;
  280. parea.len = sizeof (fpregs);
  281. parea.process_addr = (addr_t) &fpregs;
  282. parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs);
  283. if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea, 0) < 0)
  284. perror_with_name (_("Couldn't get floating point status"));
  285. fill_fpregset (regcache, &fpregs, regnum);
  286. if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea, 0) < 0)
  287. perror_with_name (_("Couldn't write floating point status"));
  288. }
  289. /* Fetch all registers in the kernel's register set whose number is
  290. REGSET_ID, whose size is REGSIZE, and whose layout is described by
  291. REGSET, from process/thread TID and store their values in GDB's
  292. register cache. */
  293. static void
  294. fetch_regset (struct regcache *regcache, int tid,
  295. int regset_id, int regsize, const struct regset *regset)
  296. {
  297. void *buf = alloca (regsize);
  298. struct iovec iov;
  299. iov.iov_base = buf;
  300. iov.iov_len = regsize;
  301. if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
  302. {
  303. if (errno == ENODATA)
  304. regcache_supply_regset (regset, regcache, -1, NULL, regsize);
  305. else
  306. perror_with_name (_("Couldn't get register set"));
  307. }
  308. else
  309. regcache_supply_regset (regset, regcache, -1, buf, regsize);
  310. }
  311. /* Store all registers in the kernel's register set whose number is
  312. REGSET_ID, whose size is REGSIZE, and whose layout is described by
  313. REGSET, from GDB's register cache back to process/thread TID. */
  314. static void
  315. store_regset (struct regcache *regcache, int tid,
  316. int regset_id, int regsize, const struct regset *regset)
  317. {
  318. void *buf = alloca (regsize);
  319. struct iovec iov;
  320. iov.iov_base = buf;
  321. iov.iov_len = regsize;
  322. if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0)
  323. perror_with_name (_("Couldn't get register set"));
  324. regcache_collect_regset (regset, regcache, -1, buf, regsize);
  325. if (ptrace (PTRACE_SETREGSET, tid, (long) regset_id, (long) &iov) < 0)
  326. perror_with_name (_("Couldn't set register set"));
  327. }
  328. /* Check whether the kernel provides a register set with number REGSET
  329. of size REGSIZE for process/thread TID. */
  330. static int
  331. check_regset (int tid, int regset, int regsize)
  332. {
  333. void *buf = alloca (regsize);
  334. struct iovec iov;
  335. iov.iov_base = buf;
  336. iov.iov_len = regsize;
  337. if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) >= 0
  338. || errno == ENODATA)
  339. return 1;
  340. return 0;
  341. }
  342. /* Fetch register REGNUM from the child process. If REGNUM is -1, do
  343. this for all registers. */
  344. void
  345. s390_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
  346. {
  347. pid_t tid = get_ptrace_pid (regcache->ptid ());
  348. if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
  349. fetch_regs (regcache, tid);
  350. if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
  351. fetch_fpregs (regcache, tid);
  352. if (have_regset_last_break)
  353. if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM)
  354. fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8,
  355. (gdbarch_ptr_bit (regcache->arch ()) == 32
  356. ? &s390_last_break_regset : &s390x_last_break_regset));
  357. if (have_regset_system_call)
  358. if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
  359. fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
  360. &s390_system_call_regset);
  361. if (have_regset_tdb)
  362. if (regnum == -1 || S390_IS_TDBREGSET_REGNUM (regnum))
  363. fetch_regset (regcache, tid, NT_S390_TDB, s390_sizeof_tdbregset,
  364. &s390_tdb_regset);
  365. if (have_regset_vxrs)
  366. {
  367. if (regnum == -1 || (regnum >= S390_V0_LOWER_REGNUM
  368. && regnum <= S390_V15_LOWER_REGNUM))
  369. fetch_regset (regcache, tid, NT_S390_VXRS_LOW, 16 * 8,
  370. &s390_vxrs_low_regset);
  371. if (regnum == -1 || (regnum >= S390_V16_REGNUM
  372. && regnum <= S390_V31_REGNUM))
  373. fetch_regset (regcache, tid, NT_S390_VXRS_HIGH, 16 * 16,
  374. &s390_vxrs_high_regset);
  375. }
  376. if (have_regset_gs)
  377. {
  378. if (regnum == -1 || (regnum >= S390_GSD_REGNUM
  379. && regnum <= S390_GSEPLA_REGNUM))
  380. fetch_regset (regcache, tid, NT_S390_GS_CB, 4 * 8,
  381. &s390_gs_regset);
  382. if (regnum == -1 || (regnum >= S390_BC_GSD_REGNUM
  383. && regnum <= S390_BC_GSEPLA_REGNUM))
  384. fetch_regset (regcache, tid, NT_S390_GS_BC, 4 * 8,
  385. &s390_gsbc_regset);
  386. }
  387. }
  388. /* Store register REGNUM back into the child process. If REGNUM is
  389. -1, do this for all registers. */
  390. void
  391. s390_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
  392. {
  393. pid_t tid = get_ptrace_pid (regcache->ptid ());
  394. if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum))
  395. store_regs (regcache, tid, regnum);
  396. if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum))
  397. store_fpregs (regcache, tid, regnum);
  398. /* S390_LAST_BREAK_REGNUM is read-only. */
  399. if (have_regset_system_call)
  400. if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM)
  401. store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4,
  402. &s390_system_call_regset);
  403. if (have_regset_vxrs)
  404. {
  405. if (regnum == -1 || (regnum >= S390_V0_LOWER_REGNUM
  406. && regnum <= S390_V15_LOWER_REGNUM))
  407. store_regset (regcache, tid, NT_S390_VXRS_LOW, 16 * 8,
  408. &s390_vxrs_low_regset);
  409. if (regnum == -1 || (regnum >= S390_V16_REGNUM
  410. && regnum <= S390_V31_REGNUM))
  411. store_regset (regcache, tid, NT_S390_VXRS_HIGH, 16 * 16,
  412. &s390_vxrs_high_regset);
  413. }
  414. }
  415. /* Hardware-assisted watchpoint handling. */
  416. /* For each process we maintain a list of all currently active
  417. watchpoints, in order to properly handle watchpoint removal.
  418. The only thing we actually need is the total address space area
  419. spanned by the watchpoints. */
  420. struct watch_area
  421. {
  422. CORE_ADDR lo_addr;
  423. CORE_ADDR hi_addr;
  424. };
  425. /* Hardware debug state. */
  426. struct s390_debug_reg_state
  427. {
  428. std::vector<watch_area> watch_areas;
  429. std::vector<watch_area> break_areas;
  430. };
  431. /* Per-process data. */
  432. struct s390_process_info
  433. {
  434. struct s390_process_info *next = nullptr;
  435. pid_t pid = 0;
  436. struct s390_debug_reg_state state;
  437. };
  438. static struct s390_process_info *s390_process_list = NULL;
  439. /* Find process data for process PID. */
  440. static struct s390_process_info *
  441. s390_find_process_pid (pid_t pid)
  442. {
  443. struct s390_process_info *proc;
  444. for (proc = s390_process_list; proc; proc = proc->next)
  445. if (proc->pid == pid)
  446. return proc;
  447. return NULL;
  448. }
  449. /* Add process data for process PID. Returns newly allocated info
  450. object. */
  451. static struct s390_process_info *
  452. s390_add_process (pid_t pid)
  453. {
  454. struct s390_process_info *proc = new struct s390_process_info;
  455. proc->pid = pid;
  456. proc->next = s390_process_list;
  457. s390_process_list = proc;
  458. return proc;
  459. }
  460. /* Get data specific info for process PID, creating it if necessary.
  461. Never returns NULL. */
  462. static struct s390_process_info *
  463. s390_process_info_get (pid_t pid)
  464. {
  465. struct s390_process_info *proc;
  466. proc = s390_find_process_pid (pid);
  467. if (proc == NULL)
  468. proc = s390_add_process (pid);
  469. return proc;
  470. }
  471. /* Get hardware debug state for process PID. */
  472. static struct s390_debug_reg_state *
  473. s390_get_debug_reg_state (pid_t pid)
  474. {
  475. return &s390_process_info_get (pid)->state;
  476. }
  477. /* Called whenever GDB is no longer debugging process PID. It deletes
  478. data structures that keep track of hardware debug state. */
  479. void
  480. s390_linux_nat_target::low_forget_process (pid_t pid)
  481. {
  482. struct s390_process_info *proc, **proc_link;
  483. proc = s390_process_list;
  484. proc_link = &s390_process_list;
  485. while (proc != NULL)
  486. {
  487. if (proc->pid == pid)
  488. {
  489. *proc_link = proc->next;
  490. delete proc;
  491. return;
  492. }
  493. proc_link = &proc->next;
  494. proc = *proc_link;
  495. }
  496. }
  497. /* linux_nat_new_fork hook. */
  498. void
  499. s390_linux_nat_target::low_new_fork (struct lwp_info *parent, pid_t child_pid)
  500. {
  501. pid_t parent_pid;
  502. struct s390_debug_reg_state *parent_state;
  503. struct s390_debug_reg_state *child_state;
  504. /* NULL means no watchpoint has ever been set in the parent. In
  505. that case, there's nothing to do. */
  506. if (lwp_arch_private_info (parent) == NULL)
  507. return;
  508. /* GDB core assumes the child inherits the watchpoints/hw breakpoints of
  509. the parent. So copy the debug state from parent to child. */
  510. parent_pid = parent->ptid.pid ();
  511. parent_state = s390_get_debug_reg_state (parent_pid);
  512. child_state = s390_get_debug_reg_state (child_pid);
  513. child_state->watch_areas = parent_state->watch_areas;
  514. child_state->break_areas = parent_state->break_areas;
  515. }
  516. /* Dump PER state. */
  517. static void
  518. s390_show_debug_regs (int tid, const char *where)
  519. {
  520. per_struct per_info;
  521. ptrace_area parea;
  522. parea.len = sizeof (per_info);
  523. parea.process_addr = (addr_t) &per_info;
  524. parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
  525. if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea, 0) < 0)
  526. perror_with_name (_("Couldn't retrieve debug regs"));
  527. debug_printf ("PER (debug) state for %d -- %s\n"
  528. " cr9-11: %lx %lx %lx\n"
  529. " start, end: %lx %lx\n"
  530. " code/ATMID: %x address: %lx PAID: %x\n",
  531. tid,
  532. where,
  533. per_info.control_regs.words.cr[0],
  534. per_info.control_regs.words.cr[1],
  535. per_info.control_regs.words.cr[2],
  536. per_info.starting_addr,
  537. per_info.ending_addr,
  538. per_info.lowcore.words.perc_atmid,
  539. per_info.lowcore.words.address,
  540. per_info.lowcore.words.access_id);
  541. }
  542. bool
  543. s390_linux_nat_target::stopped_by_watchpoint ()
  544. {
  545. struct s390_debug_reg_state *state
  546. = s390_get_debug_reg_state (inferior_ptid.pid ());
  547. per_lowcore_bits per_lowcore;
  548. ptrace_area parea;
  549. if (show_debug_regs)
  550. s390_show_debug_regs (s390_inferior_tid (), "stop");
  551. /* Speed up common case. */
  552. if (state->watch_areas.empty ())
  553. return false;
  554. parea.len = sizeof (per_lowcore);
  555. parea.process_addr = (addr_t) & per_lowcore;
  556. parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore);
  557. if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea, 0) < 0)
  558. perror_with_name (_("Couldn't retrieve watchpoint status"));
  559. bool result = (per_lowcore.perc_storage_alteration == 1
  560. && per_lowcore.perc_store_real_address == 0);
  561. if (result)
  562. {
  563. /* Do not report this watchpoint again. */
  564. memset (&per_lowcore, 0, sizeof (per_lowcore));
  565. if (ptrace (PTRACE_POKEUSR_AREA, s390_inferior_tid (), &parea, 0) < 0)
  566. perror_with_name (_("Couldn't clear watchpoint status"));
  567. }
  568. return result;
  569. }
  570. /* Each time before resuming a thread, update its PER info. */
  571. void
  572. s390_linux_nat_target::low_prepare_to_resume (struct lwp_info *lp)
  573. {
  574. int tid;
  575. pid_t pid = ptid_of_lwp (lp).pid ();
  576. per_struct per_info;
  577. ptrace_area parea;
  578. CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0;
  579. struct arch_lwp_info *lp_priv = lwp_arch_private_info (lp);
  580. struct s390_debug_reg_state *state = s390_get_debug_reg_state (pid);
  581. int step = lwp_is_stepping (lp);
  582. /* Nothing to do if there was never any PER info for this thread. */
  583. if (lp_priv == NULL)
  584. return;
  585. /* If PER info has changed, update it. When single-stepping, disable
  586. hardware breakpoints (if any). Otherwise we're done. */
  587. if (!lp_priv->per_info_changed)
  588. {
  589. if (!step || state->break_areas.empty ())
  590. return;
  591. }
  592. lp_priv->per_info_changed = 0;
  593. tid = ptid_of_lwp (lp).lwp ();
  594. if (tid == 0)
  595. tid = pid;
  596. parea.len = sizeof (per_info);
  597. parea.process_addr = (addr_t) & per_info;
  598. parea.kernel_addr = offsetof (struct user_regs_struct, per_info);
  599. /* Clear PER info, but adjust the single_step field (used by older
  600. kernels only). */
  601. memset (&per_info, 0, sizeof (per_info));
  602. per_info.single_step = (step != 0);
  603. if (!state->watch_areas.empty ())
  604. {
  605. for (const auto &area : state->watch_areas)
  606. {
  607. watch_lo_addr = std::min (watch_lo_addr, area.lo_addr);
  608. watch_hi_addr = std::max (watch_hi_addr, area.hi_addr);
  609. }
  610. /* Enable storage-alteration events. */
  611. per_info.control_regs.words.cr[0] |= (PER_EVENT_STORE
  612. | PER_CONTROL_ALTERATION);
  613. }
  614. if (!state->break_areas.empty ())
  615. {
  616. /* Don't install hardware breakpoints while single-stepping, since
  617. our PER settings (e.g. the nullification bit) might then conflict
  618. with the kernel's. But re-install them afterwards. */
  619. if (step)
  620. lp_priv->per_info_changed = 1;
  621. else
  622. {
  623. for (const auto &area : state->break_areas)
  624. {
  625. watch_lo_addr = std::min (watch_lo_addr, area.lo_addr);
  626. watch_hi_addr = std::max (watch_hi_addr, area.hi_addr);
  627. }
  628. /* If there's just one breakpoint, enable instruction-fetching
  629. nullification events for the breakpoint address (fast).
  630. Otherwise stop after any instruction within the PER area and
  631. after any branch into it (slow). */
  632. if (watch_hi_addr == watch_lo_addr)
  633. per_info.control_regs.words.cr[0] |= (PER_EVENT_NULLIFICATION
  634. | PER_EVENT_IFETCH);
  635. else
  636. {
  637. /* The PER area must include the instruction before the
  638. first breakpoint address. */
  639. watch_lo_addr = watch_lo_addr > 6 ? watch_lo_addr - 6 : 0;
  640. per_info.control_regs.words.cr[0]
  641. |= (PER_EVENT_BRANCH
  642. | PER_EVENT_IFETCH
  643. | PER_CONTROL_BRANCH_ADDRESS);
  644. }
  645. }
  646. }
  647. per_info.starting_addr = watch_lo_addr;
  648. per_info.ending_addr = watch_hi_addr;
  649. if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea, 0) < 0)
  650. perror_with_name (_("Couldn't modify watchpoint status"));
  651. if (show_debug_regs)
  652. s390_show_debug_regs (tid, "resume");
  653. }
  654. /* Mark the PER info as changed, so the next resume will update it. */
  655. static void
  656. s390_mark_per_info_changed (struct lwp_info *lp)
  657. {
  658. if (lwp_arch_private_info (lp) == NULL)
  659. lwp_set_arch_private_info (lp, XCNEW (struct arch_lwp_info));
  660. lwp_arch_private_info (lp)->per_info_changed = 1;
  661. }
  662. /* When attaching to a new thread, mark its PER info as changed. */
  663. void
  664. s390_linux_nat_target::low_new_thread (struct lwp_info *lp)
  665. {
  666. s390_mark_per_info_changed (lp);
  667. }
  668. /* Function to call when a thread is being deleted. */
  669. void
  670. s390_linux_nat_target::low_delete_thread (struct arch_lwp_info *arch_lwp)
  671. {
  672. xfree (arch_lwp);
  673. }
  674. /* Iterator callback for s390_refresh_per_info. */
  675. static int
  676. s390_refresh_per_info_cb (struct lwp_info *lp)
  677. {
  678. s390_mark_per_info_changed (lp);
  679. if (!lwp_is_stopped (lp))
  680. linux_stop_lwp (lp);
  681. return 0;
  682. }
  683. /* Make sure that threads are stopped and mark PER info as changed. */
  684. static int
  685. s390_refresh_per_info (void)
  686. {
  687. ptid_t pid_ptid = ptid_t (current_lwp_ptid ().pid ());
  688. iterate_over_lwps (pid_ptid, s390_refresh_per_info_cb);
  689. return 0;
  690. }
  691. int
  692. s390_linux_nat_target::insert_watchpoint (CORE_ADDR addr, int len,
  693. enum target_hw_bp_type type,
  694. struct expression *cond)
  695. {
  696. watch_area area;
  697. struct s390_debug_reg_state *state
  698. = s390_get_debug_reg_state (inferior_ptid.pid ());
  699. area.lo_addr = addr;
  700. area.hi_addr = addr + len - 1;
  701. state->watch_areas.push_back (area);
  702. return s390_refresh_per_info ();
  703. }
  704. int
  705. s390_linux_nat_target::remove_watchpoint (CORE_ADDR addr, int len,
  706. enum target_hw_bp_type type,
  707. struct expression *cond)
  708. {
  709. unsigned ix;
  710. struct s390_debug_reg_state *state
  711. = s390_get_debug_reg_state (inferior_ptid.pid ());
  712. for (ix = 0; ix < state->watch_areas.size (); ix++)
  713. {
  714. watch_area &area = state->watch_areas[ix];
  715. if (area.lo_addr == addr && area.hi_addr == addr + len - 1)
  716. {
  717. unordered_remove (state->watch_areas, ix);
  718. return s390_refresh_per_info ();
  719. }
  720. }
  721. gdb_printf (gdb_stderr,
  722. "Attempt to remove nonexistent watchpoint.\n");
  723. return -1;
  724. }
  725. /* Implement the "can_use_hw_breakpoint" target_ops method. */
  726. int
  727. s390_linux_nat_target::can_use_hw_breakpoint (enum bptype type,
  728. int cnt, int othertype)
  729. {
  730. if (type == bp_hardware_watchpoint || type == bp_hardware_breakpoint)
  731. return 1;
  732. return 0;
  733. }
  734. /* Implement the "insert_hw_breakpoint" target_ops method. */
  735. int
  736. s390_linux_nat_target::insert_hw_breakpoint (struct gdbarch *gdbarch,
  737. struct bp_target_info *bp_tgt)
  738. {
  739. watch_area area;
  740. struct s390_debug_reg_state *state;
  741. area.lo_addr = bp_tgt->placed_address = bp_tgt->reqstd_address;
  742. area.hi_addr = area.lo_addr;
  743. state = s390_get_debug_reg_state (inferior_ptid.pid ());
  744. state->break_areas.push_back (area);
  745. return s390_refresh_per_info ();
  746. }
  747. /* Implement the "remove_hw_breakpoint" target_ops method. */
  748. int
  749. s390_linux_nat_target::remove_hw_breakpoint (struct gdbarch *gdbarch,
  750. struct bp_target_info *bp_tgt)
  751. {
  752. unsigned ix;
  753. struct s390_debug_reg_state *state;
  754. state = s390_get_debug_reg_state (inferior_ptid.pid ());
  755. for (ix = 0; state->break_areas.size (); ix++)
  756. {
  757. watch_area &area = state->break_areas[ix];
  758. if (area.lo_addr == bp_tgt->placed_address)
  759. {
  760. unordered_remove (state->break_areas, ix);
  761. return s390_refresh_per_info ();
  762. }
  763. }
  764. gdb_printf (gdb_stderr,
  765. "Attempt to remove nonexistent breakpoint.\n");
  766. return -1;
  767. }
  768. int
  769. s390_linux_nat_target::region_ok_for_hw_watchpoint (CORE_ADDR addr, int cnt)
  770. {
  771. return 1;
  772. }
  773. static int
  774. s390_target_wordsize (void)
  775. {
  776. int wordsize = 4;
  777. /* Check for 64-bit inferior process. This is the case when the host is
  778. 64-bit, and in addition bit 32 of the PSW mask is set. */
  779. #ifdef __s390x__
  780. long pswm;
  781. errno = 0;
  782. pswm = (long) ptrace (PTRACE_PEEKUSER, s390_inferior_tid (), PT_PSWMASK, 0);
  783. if (errno == 0 && (pswm & 0x100000000ul) != 0)
  784. wordsize = 8;
  785. #endif
  786. return wordsize;
  787. }
  788. int
  789. s390_linux_nat_target::auxv_parse (gdb_byte **readptr,
  790. gdb_byte *endptr, CORE_ADDR *typep,
  791. CORE_ADDR *valp)
  792. {
  793. int sizeof_auxv_field = s390_target_wordsize ();
  794. enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ());
  795. gdb_byte *ptr = *readptr;
  796. if (endptr == ptr)
  797. return 0;
  798. if (endptr - ptr < sizeof_auxv_field * 2)
  799. return -1;
  800. *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
  801. ptr += sizeof_auxv_field;
  802. *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order);
  803. ptr += sizeof_auxv_field;
  804. *readptr = ptr;
  805. return 1;
  806. }
  807. const struct target_desc *
  808. s390_linux_nat_target::read_description ()
  809. {
  810. int tid = inferior_ptid.pid ();
  811. have_regset_last_break
  812. = check_regset (tid, NT_S390_LAST_BREAK, 8);
  813. have_regset_system_call
  814. = check_regset (tid, NT_S390_SYSTEM_CALL, 4);
  815. /* If GDB itself is compiled as 64-bit, we are running on a machine in
  816. z/Architecture mode. If the target is running in 64-bit addressing
  817. mode, report s390x architecture. If the target is running in 31-bit
  818. addressing mode, but the kernel supports using 64-bit registers in
  819. that mode, report s390 architecture with 64-bit GPRs. */
  820. #ifdef __s390x__
  821. {
  822. CORE_ADDR hwcap = linux_get_hwcap (current_inferior ()->top_target ());
  823. have_regset_tdb = (hwcap & HWCAP_S390_TE)
  824. && check_regset (tid, NT_S390_TDB, s390_sizeof_tdbregset);
  825. have_regset_vxrs = (hwcap & HWCAP_S390_VX)
  826. && check_regset (tid, NT_S390_VXRS_LOW, 16 * 8)
  827. && check_regset (tid, NT_S390_VXRS_HIGH, 16 * 16);
  828. have_regset_gs = (hwcap & HWCAP_S390_GS)
  829. && check_regset (tid, NT_S390_GS_CB, 4 * 8)
  830. && check_regset (tid, NT_S390_GS_BC, 4 * 8);
  831. if (s390_target_wordsize () == 8)
  832. return (have_regset_gs ? tdesc_s390x_gs_linux64 :
  833. have_regset_vxrs ?
  834. (have_regset_tdb ? tdesc_s390x_tevx_linux64 :
  835. tdesc_s390x_vx_linux64) :
  836. have_regset_tdb ? tdesc_s390x_te_linux64 :
  837. have_regset_system_call ? tdesc_s390x_linux64v2 :
  838. have_regset_last_break ? tdesc_s390x_linux64v1 :
  839. tdesc_s390x_linux64);
  840. if (hwcap & HWCAP_S390_HIGH_GPRS)
  841. return (have_regset_gs ? tdesc_s390_gs_linux64 :
  842. have_regset_vxrs ?
  843. (have_regset_tdb ? tdesc_s390_tevx_linux64 :
  844. tdesc_s390_vx_linux64) :
  845. have_regset_tdb ? tdesc_s390_te_linux64 :
  846. have_regset_system_call ? tdesc_s390_linux64v2 :
  847. have_regset_last_break ? tdesc_s390_linux64v1 :
  848. tdesc_s390_linux64);
  849. }
  850. #endif
  851. /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior
  852. on a 64-bit kernel that does not support using 64-bit registers in 31-bit
  853. mode, report s390 architecture with 32-bit GPRs. */
  854. return (have_regset_system_call? tdesc_s390_linux32v2 :
  855. have_regset_last_break? tdesc_s390_linux32v1 :
  856. tdesc_s390_linux32);
  857. }
  858. void _initialize_s390_nat ();
  859. void
  860. _initialize_s390_nat ()
  861. {
  862. /* Register the target. */
  863. linux_target = &the_s390_linux_nat_target;
  864. add_inf_child_target (&the_s390_linux_nat_target);
  865. /* A maintenance command to enable showing the PER state. */
  866. add_setshow_boolean_cmd ("show-debug-regs", class_maintenance,
  867. &show_debug_regs, _("\
  868. Set whether to show the PER (debug) hardware state."), _("\
  869. Show whether to show the PER (debug) hardware state."), _("\
  870. Use \"on\" to enable, \"off\" to disable.\n\
  871. If enabled, the PER state is shown after it is changed by GDB,\n\
  872. and when the inferior triggers a breakpoint or watchpoint."),
  873. NULL,
  874. NULL,
  875. &maintenance_set_cmdlist,
  876. &maintenance_show_cmdlist);
  877. }