m68k-linux-nat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* Motorola m68k native support for GNU/Linux.
  2. Copyright (C) 1996-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 "defs.h"
  15. #include "frame.h"
  16. #include "inferior.h"
  17. #include "language.h"
  18. #include "gdbcore.h"
  19. #include "regcache.h"
  20. #include "target.h"
  21. #include "linux-nat.h"
  22. #include "gdbarch.h"
  23. #include "m68k-tdep.h"
  24. #include <sys/dir.h>
  25. #include <signal.h>
  26. #include "nat/gdb_ptrace.h"
  27. #include <sys/user.h>
  28. #include <sys/ioctl.h>
  29. #include <fcntl.h>
  30. #include <sys/procfs.h>
  31. #ifdef HAVE_SYS_REG_H
  32. #include <sys/reg.h>
  33. #endif
  34. #include <sys/file.h>
  35. #include <sys/stat.h>
  36. #include "floatformat.h"
  37. /* Prototypes for supply_gregset etc. */
  38. #include "gregset.h"
  39. /* Defines ps_err_e, struct ps_prochandle. */
  40. #include "gdb_proc_service.h"
  41. #include "inf-ptrace.h"
  42. #ifndef PTRACE_GET_THREAD_AREA
  43. #define PTRACE_GET_THREAD_AREA 25
  44. #endif
  45. class m68k_linux_nat_target final : public linux_nat_target
  46. {
  47. public:
  48. /* Add our register access methods. */
  49. void fetch_registers (struct regcache *, int) override;
  50. void store_registers (struct regcache *, int) override;
  51. };
  52. static m68k_linux_nat_target the_m68k_linux_nat_target;
  53. /* This table must line up with gdbarch_register_name in "m68k-tdep.c". */
  54. static const int regmap[] =
  55. {
  56. PT_D0, PT_D1, PT_D2, PT_D3, PT_D4, PT_D5, PT_D6, PT_D7,
  57. PT_A0, PT_A1, PT_A2, PT_A3, PT_A4, PT_A5, PT_A6, PT_USP,
  58. PT_SR, PT_PC,
  59. /* PT_FP0, ..., PT_FP7 */
  60. 21, 24, 27, 30, 33, 36, 39, 42,
  61. /* PT_FPCR, PT_FPSR, PT_FPIAR */
  62. 45, 46, 47
  63. };
  64. /* Which ptrace request retrieves which registers?
  65. These apply to the corresponding SET requests as well. */
  66. #define NUM_GREGS (18)
  67. #define MAX_NUM_REGS (NUM_GREGS + 11)
  68. static int
  69. getregs_supplies (int regno)
  70. {
  71. return 0 <= regno && regno < NUM_GREGS;
  72. }
  73. static int
  74. getfpregs_supplies (int regno)
  75. {
  76. return M68K_FP0_REGNUM <= regno && regno <= M68K_FPI_REGNUM;
  77. }
  78. /* Does the current host support the GETREGS request? */
  79. static int have_ptrace_getregs =
  80. #ifdef HAVE_PTRACE_GETREGS
  81. 1
  82. #else
  83. 0
  84. #endif
  85. ;
  86. /* Fetching registers directly from the U area, one at a time. */
  87. /* Fetch one register. */
  88. static void
  89. fetch_register (struct regcache *regcache, int regno)
  90. {
  91. struct gdbarch *gdbarch = regcache->arch ();
  92. long regaddr, val;
  93. int i;
  94. gdb_byte buf[M68K_MAX_REGISTER_SIZE];
  95. pid_t tid = get_ptrace_pid (regcache->ptid ());
  96. regaddr = 4 * regmap[regno];
  97. for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
  98. {
  99. errno = 0;
  100. val = ptrace (PTRACE_PEEKUSER, tid, regaddr, 0);
  101. memcpy (&buf[i], &val, sizeof (long));
  102. regaddr += sizeof (long);
  103. if (errno != 0)
  104. error (_("Couldn't read register %s (#%d): %s."),
  105. gdbarch_register_name (gdbarch, regno),
  106. regno, safe_strerror (errno));
  107. }
  108. regcache->raw_supply (regno, buf);
  109. }
  110. /* Fetch register values from the inferior.
  111. If REGNO is negative, do this for all registers.
  112. Otherwise, REGNO specifies which register (so we can save time). */
  113. static void
  114. old_fetch_inferior_registers (struct regcache *regcache, int regno)
  115. {
  116. if (regno >= 0)
  117. {
  118. fetch_register (regcache, regno);
  119. }
  120. else
  121. {
  122. for (regno = 0;
  123. regno < gdbarch_num_regs (regcache->arch ());
  124. regno++)
  125. {
  126. fetch_register (regcache, regno);
  127. }
  128. }
  129. }
  130. /* Store one register. */
  131. static void
  132. store_register (const struct regcache *regcache, int regno)
  133. {
  134. struct gdbarch *gdbarch = regcache->arch ();
  135. long regaddr, val;
  136. int i;
  137. gdb_byte buf[M68K_MAX_REGISTER_SIZE];
  138. pid_t tid = get_ptrace_pid (regcache->ptid ());
  139. regaddr = 4 * regmap[regno];
  140. /* Put the contents of regno into a local buffer. */
  141. regcache->raw_collect (regno, buf);
  142. /* Store the local buffer into the inferior a chunk at the time. */
  143. for (i = 0; i < register_size (gdbarch, regno); i += sizeof (long))
  144. {
  145. errno = 0;
  146. memcpy (&val, &buf[i], sizeof (long));
  147. ptrace (PTRACE_POKEUSER, tid, regaddr, val);
  148. regaddr += sizeof (long);
  149. if (errno != 0)
  150. error (_("Couldn't write register %s (#%d): %s."),
  151. gdbarch_register_name (gdbarch, regno),
  152. regno, safe_strerror (errno));
  153. }
  154. }
  155. /* Store our register values back into the inferior.
  156. If REGNO is negative, do this for all registers.
  157. Otherwise, REGNO specifies which register (so we can save time). */
  158. static void
  159. old_store_inferior_registers (const struct regcache *regcache, int regno)
  160. {
  161. if (regno >= 0)
  162. {
  163. store_register (regcache, regno);
  164. }
  165. else
  166. {
  167. for (regno = 0;
  168. regno < gdbarch_num_regs (regcache->arch ());
  169. regno++)
  170. {
  171. store_register (regcache, regno);
  172. }
  173. }
  174. }
  175. /* Given a pointer to a general register set in /proc format
  176. (elf_gregset_t *), unpack the register contents and supply
  177. them as gdb's idea of the current register values. */
  178. void
  179. supply_gregset (struct regcache *regcache, const elf_gregset_t *gregsetp)
  180. {
  181. struct gdbarch *gdbarch = regcache->arch ();
  182. const elf_greg_t *regp = (const elf_greg_t *) gregsetp;
  183. int regi;
  184. for (regi = M68K_D0_REGNUM;
  185. regi <= gdbarch_sp_regnum (gdbarch);
  186. regi++)
  187. regcache->raw_supply (regi, &regp[regmap[regi]]);
  188. regcache->raw_supply (gdbarch_ps_regnum (gdbarch), &regp[PT_SR]);
  189. regcache->raw_supply (gdbarch_pc_regnum (gdbarch), &regp[PT_PC]);
  190. }
  191. /* Fill register REGNO (if it is a general-purpose register) in
  192. *GREGSETPS with the value in GDB's register array. If REGNO is -1,
  193. do this for all registers. */
  194. void
  195. fill_gregset (const struct regcache *regcache,
  196. elf_gregset_t *gregsetp, int regno)
  197. {
  198. elf_greg_t *regp = (elf_greg_t *) gregsetp;
  199. int i;
  200. for (i = 0; i < NUM_GREGS; i++)
  201. if (regno == -1 || regno == i)
  202. regcache->raw_collect (i, regp + regmap[i]);
  203. }
  204. #ifdef HAVE_PTRACE_GETREGS
  205. /* Fetch all general-purpose registers from process/thread TID and
  206. store their values in GDB's register array. */
  207. static void
  208. fetch_regs (struct regcache *regcache, int tid)
  209. {
  210. elf_gregset_t regs;
  211. if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
  212. {
  213. if (errno == EIO)
  214. {
  215. /* The kernel we're running on doesn't support the GETREGS
  216. request. Reset `have_ptrace_getregs'. */
  217. have_ptrace_getregs = 0;
  218. return;
  219. }
  220. perror_with_name (_("Couldn't get registers"));
  221. }
  222. supply_gregset (regcache, (const elf_gregset_t *) &regs);
  223. }
  224. /* Store all valid general-purpose registers in GDB's register array
  225. into the process/thread specified by TID. */
  226. static void
  227. store_regs (const struct regcache *regcache, int tid, int regno)
  228. {
  229. elf_gregset_t regs;
  230. if (ptrace (PTRACE_GETREGS, tid, 0, (int) &regs) < 0)
  231. perror_with_name (_("Couldn't get registers"));
  232. fill_gregset (regcache, &regs, regno);
  233. if (ptrace (PTRACE_SETREGS, tid, 0, (int) &regs) < 0)
  234. perror_with_name (_("Couldn't write registers"));
  235. }
  236. #else
  237. static void fetch_regs (struct regcache *regcache, int tid)
  238. {
  239. }
  240. static void store_regs (const struct regcache *regcache, int tid, int regno)
  241. {
  242. }
  243. #endif
  244. /* Transfering floating-point registers between GDB, inferiors and cores. */
  245. /* What is the address of fpN within the floating-point register set F? */
  246. #define FPREG_ADDR(f, n) (&(f)->fpregs[(n) * 3])
  247. /* Fill GDB's register array with the floating-point register values in
  248. *FPREGSETP. */
  249. void
  250. supply_fpregset (struct regcache *regcache, const elf_fpregset_t *fpregsetp)
  251. {
  252. struct gdbarch *gdbarch = regcache->arch ();
  253. int regi;
  254. for (regi = gdbarch_fp0_regnum (gdbarch);
  255. regi < gdbarch_fp0_regnum (gdbarch) + 8; regi++)
  256. regcache->raw_supply
  257. (regi, FPREG_ADDR (fpregsetp, regi - gdbarch_fp0_regnum (gdbarch)));
  258. regcache->raw_supply (M68K_FPC_REGNUM, &fpregsetp->fpcntl[0]);
  259. regcache->raw_supply (M68K_FPS_REGNUM, &fpregsetp->fpcntl[1]);
  260. regcache->raw_supply (M68K_FPI_REGNUM, &fpregsetp->fpcntl[2]);
  261. }
  262. /* Fill register REGNO (if it is a floating-point register) in
  263. *FPREGSETP with the value in GDB's register array. If REGNO is -1,
  264. do this for all registers. */
  265. void
  266. fill_fpregset (const struct regcache *regcache,
  267. elf_fpregset_t *fpregsetp, int regno)
  268. {
  269. struct gdbarch *gdbarch = regcache->arch ();
  270. int i;
  271. /* Fill in the floating-point registers. */
  272. for (i = gdbarch_fp0_regnum (gdbarch);
  273. i < gdbarch_fp0_regnum (gdbarch) + 8; i++)
  274. if (regno == -1 || regno == i)
  275. regcache->raw_collect
  276. (i, FPREG_ADDR (fpregsetp, i - gdbarch_fp0_regnum (gdbarch)));
  277. /* Fill in the floating-point control registers. */
  278. for (i = M68K_FPC_REGNUM; i <= M68K_FPI_REGNUM; i++)
  279. if (regno == -1 || regno == i)
  280. regcache->raw_collect (i, &fpregsetp->fpcntl[i - M68K_FPC_REGNUM]);
  281. }
  282. #ifdef HAVE_PTRACE_GETREGS
  283. /* Fetch all floating-point registers from process/thread TID and store
  284. thier values in GDB's register array. */
  285. static void
  286. fetch_fpregs (struct regcache *regcache, int tid)
  287. {
  288. elf_fpregset_t fpregs;
  289. if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
  290. perror_with_name (_("Couldn't get floating point status"));
  291. supply_fpregset (regcache, (const elf_fpregset_t *) &fpregs);
  292. }
  293. /* Store all valid floating-point registers in GDB's register array
  294. into the process/thread specified by TID. */
  295. static void
  296. store_fpregs (const struct regcache *regcache, int tid, int regno)
  297. {
  298. elf_fpregset_t fpregs;
  299. if (ptrace (PTRACE_GETFPREGS, tid, 0, (int) &fpregs) < 0)
  300. perror_with_name (_("Couldn't get floating point status"));
  301. fill_fpregset (regcache, &fpregs, regno);
  302. if (ptrace (PTRACE_SETFPREGS, tid, 0, (int) &fpregs) < 0)
  303. perror_with_name (_("Couldn't write floating point status"));
  304. }
  305. #else
  306. static void fetch_fpregs (struct regcache *regcache, int tid)
  307. {
  308. }
  309. static void store_fpregs (const struct regcache *regcache, int tid, int regno)
  310. {
  311. }
  312. #endif
  313. /* Transferring arbitrary registers between GDB and inferior. */
  314. /* Fetch register REGNO from the child process. If REGNO is -1, do
  315. this for all registers (including the floating point and SSE
  316. registers). */
  317. void
  318. m68k_linux_nat_target::fetch_registers (struct regcache *regcache, int regno)
  319. {
  320. pid_t tid;
  321. /* Use the old method of peeking around in `struct user' if the
  322. GETREGS request isn't available. */
  323. if (! have_ptrace_getregs)
  324. {
  325. old_fetch_inferior_registers (regcache, regno);
  326. return;
  327. }
  328. tid = get_ptrace_pid (regcache->ptid ());
  329. /* Use the PTRACE_GETFPXREGS request whenever possible, since it
  330. transfers more registers in one system call, and we'll cache the
  331. results. But remember that fetch_fpxregs can fail, and return
  332. zero. */
  333. if (regno == -1)
  334. {
  335. fetch_regs (regcache, tid);
  336. /* The call above might reset `have_ptrace_getregs'. */
  337. if (! have_ptrace_getregs)
  338. {
  339. old_fetch_inferior_registers (regcache, -1);
  340. return;
  341. }
  342. fetch_fpregs (regcache, tid);
  343. return;
  344. }
  345. if (getregs_supplies (regno))
  346. {
  347. fetch_regs (regcache, tid);
  348. return;
  349. }
  350. if (getfpregs_supplies (regno))
  351. {
  352. fetch_fpregs (regcache, tid);
  353. return;
  354. }
  355. internal_error (__FILE__, __LINE__,
  356. _("Got request for bad register number %d."), regno);
  357. }
  358. /* Store register REGNO back into the child process. If REGNO is -1,
  359. do this for all registers (including the floating point and SSE
  360. registers). */
  361. void
  362. m68k_linux_nat_target::store_registers (struct regcache *regcache, int regno)
  363. {
  364. pid_t tid;
  365. /* Use the old method of poking around in `struct user' if the
  366. SETREGS request isn't available. */
  367. if (! have_ptrace_getregs)
  368. {
  369. old_store_inferior_registers (regcache, regno);
  370. return;
  371. }
  372. tid = get_ptrace_pid (regcache->ptid ());
  373. /* Use the PTRACE_SETFPREGS requests whenever possible, since it
  374. transfers more registers in one system call. But remember that
  375. store_fpregs can fail, and return zero. */
  376. if (regno == -1)
  377. {
  378. store_regs (regcache, tid, regno);
  379. store_fpregs (regcache, tid, regno);
  380. return;
  381. }
  382. if (getregs_supplies (regno))
  383. {
  384. store_regs (regcache, tid, regno);
  385. return;
  386. }
  387. if (getfpregs_supplies (regno))
  388. {
  389. store_fpregs (regcache, tid, regno);
  390. return;
  391. }
  392. internal_error (__FILE__, __LINE__,
  393. _("Got request to store bad register number %d."), regno);
  394. }
  395. /* Fetch the thread-local storage pointer for libthread_db. */
  396. ps_err_e
  397. ps_get_thread_area (struct ps_prochandle *ph,
  398. lwpid_t lwpid, int idx, void **base)
  399. {
  400. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) < 0)
  401. return PS_ERR;
  402. /* IDX is the bias from the thread pointer to the beginning of the
  403. thread descriptor. It has to be subtracted due to implementation
  404. quirks in libthread_db. */
  405. *base = (char *) *base - idx;
  406. return PS_OK;
  407. }
  408. void _initialize_m68k_linux_nat ();
  409. void
  410. _initialize_m68k_linux_nat ()
  411. {
  412. /* Register the target. */
  413. linux_target = &the_m68k_linux_nat_target;
  414. add_inf_child_target (&the_m68k_linux_nat_target);
  415. }