linux-mips-low.cc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  1. /* GNU/Linux/MIPS 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 "nat/gdb_ptrace.h"
  17. #include <endian.h>
  18. #include "nat/mips-linux-watch.h"
  19. #include "gdb_proc_service.h"
  20. /* Linux target op definitions for the MIPS architecture. */
  21. class mips_target : public linux_process_target
  22. {
  23. public:
  24. const regs_info *get_regs_info () override;
  25. const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
  26. bool supports_z_point_type (char z_type) override;
  27. protected:
  28. void low_arch_setup () override;
  29. bool low_cannot_fetch_register (int regno) override;
  30. bool low_cannot_store_register (int regno) override;
  31. bool low_fetch_register (regcache *regcache, int regno) override;
  32. bool low_supports_breakpoints () override;
  33. CORE_ADDR low_get_pc (regcache *regcache) override;
  34. void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
  35. bool low_breakpoint_at (CORE_ADDR pc) override;
  36. int low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
  37. int size, raw_breakpoint *bp) override;
  38. int low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
  39. int size, raw_breakpoint *bp) override;
  40. bool low_stopped_by_watchpoint () override;
  41. CORE_ADDR low_stopped_data_address () override;
  42. void low_collect_ptrace_register (regcache *regcache, int regno,
  43. char *buf) override;
  44. void low_supply_ptrace_register (regcache *regcache, int regno,
  45. const char *buf) override;
  46. arch_process_info *low_new_process () override;
  47. void low_delete_process (arch_process_info *info) override;
  48. void low_new_thread (lwp_info *) override;
  49. void low_delete_thread (arch_lwp_info *) override;
  50. void low_new_fork (process_info *parent, process_info *child) override;
  51. void low_prepare_to_resume (lwp_info *lwp) override;
  52. };
  53. /* The singleton target ops object. */
  54. static mips_target the_mips_target;
  55. /* Defined in auto-generated file mips-linux.c. */
  56. void init_registers_mips_linux (void);
  57. extern const struct target_desc *tdesc_mips_linux;
  58. /* Defined in auto-generated file mips-dsp-linux.c. */
  59. void init_registers_mips_dsp_linux (void);
  60. extern const struct target_desc *tdesc_mips_dsp_linux;
  61. /* Defined in auto-generated file mips64-linux.c. */
  62. void init_registers_mips64_linux (void);
  63. extern const struct target_desc *tdesc_mips64_linux;
  64. /* Defined in auto-generated file mips64-dsp-linux.c. */
  65. void init_registers_mips64_dsp_linux (void);
  66. extern const struct target_desc *tdesc_mips64_dsp_linux;
  67. #ifdef __mips64
  68. #define tdesc_mips_linux tdesc_mips64_linux
  69. #define tdesc_mips_dsp_linux tdesc_mips64_dsp_linux
  70. #endif
  71. #ifndef PTRACE_GET_THREAD_AREA
  72. #define PTRACE_GET_THREAD_AREA 25
  73. #endif
  74. #ifdef HAVE_SYS_REG_H
  75. #include <sys/reg.h>
  76. #endif
  77. #define mips_num_regs 73
  78. #define mips_dsp_num_regs 80
  79. #include <asm/ptrace.h>
  80. #ifndef DSP_BASE
  81. #define DSP_BASE 71
  82. #define DSP_CONTROL 77
  83. #endif
  84. union mips_register
  85. {
  86. unsigned char buf[8];
  87. /* Deliberately signed, for proper sign extension. */
  88. int reg32;
  89. long long reg64;
  90. };
  91. /* Return the ptrace ``address'' of register REGNO. */
  92. #define mips_base_regs \
  93. -1, 1, 2, 3, 4, 5, 6, 7, \
  94. 8, 9, 10, 11, 12, 13, 14, 15, \
  95. 16, 17, 18, 19, 20, 21, 22, 23, \
  96. 24, 25, 26, 27, 28, 29, 30, 31, \
  97. \
  98. -1, MMLO, MMHI, BADVADDR, CAUSE, PC, \
  99. \
  100. FPR_BASE, FPR_BASE + 1, FPR_BASE + 2, FPR_BASE + 3, \
  101. FPR_BASE + 4, FPR_BASE + 5, FPR_BASE + 6, FPR_BASE + 7, \
  102. FPR_BASE + 8, FPR_BASE + 9, FPR_BASE + 10, FPR_BASE + 11, \
  103. FPR_BASE + 12, FPR_BASE + 13, FPR_BASE + 14, FPR_BASE + 15, \
  104. FPR_BASE + 16, FPR_BASE + 17, FPR_BASE + 18, FPR_BASE + 19, \
  105. FPR_BASE + 20, FPR_BASE + 21, FPR_BASE + 22, FPR_BASE + 23, \
  106. FPR_BASE + 24, FPR_BASE + 25, FPR_BASE + 26, FPR_BASE + 27, \
  107. FPR_BASE + 28, FPR_BASE + 29, FPR_BASE + 30, FPR_BASE + 31, \
  108. FPC_CSR, FPC_EIR
  109. #define mips_dsp_regs \
  110. DSP_BASE, DSP_BASE + 1, DSP_BASE + 2, DSP_BASE + 3, \
  111. DSP_BASE + 4, DSP_BASE + 5, \
  112. DSP_CONTROL
  113. static int mips_regmap[mips_num_regs] = {
  114. mips_base_regs,
  115. 0
  116. };
  117. static int mips_dsp_regmap[mips_dsp_num_regs] = {
  118. mips_base_regs,
  119. mips_dsp_regs,
  120. 0
  121. };
  122. /* DSP registers are not in any regset and can only be accessed
  123. individually. */
  124. static unsigned char mips_dsp_regset_bitmap[(mips_dsp_num_regs + 7) / 8] = {
  125. 0xfe, 0xff, 0xff, 0xff, 0xfe, 0xff, 0xff, 0xff, 0xff, 0x80
  126. };
  127. static int have_dsp = -1;
  128. /* Try peeking at an arbitrarily chosen DSP register and pick the available
  129. user register set accordingly. */
  130. static const struct target_desc *
  131. mips_read_description (void)
  132. {
  133. if (have_dsp < 0)
  134. {
  135. int pid = lwpid_of (current_thread);
  136. errno = 0;
  137. ptrace (PTRACE_PEEKUSER, pid, DSP_CONTROL, 0);
  138. switch (errno)
  139. {
  140. case 0:
  141. have_dsp = 1;
  142. break;
  143. case EIO:
  144. have_dsp = 0;
  145. break;
  146. default:
  147. perror_with_name ("ptrace");
  148. break;
  149. }
  150. }
  151. return have_dsp ? tdesc_mips_dsp_linux : tdesc_mips_linux;
  152. }
  153. void
  154. mips_target::low_arch_setup ()
  155. {
  156. current_process ()->tdesc = mips_read_description ();
  157. }
  158. /* Per-process arch-specific data we want to keep. */
  159. struct arch_process_info
  160. {
  161. /* -1 if the kernel and/or CPU do not support watch registers.
  162. 1 if watch_readback is valid and we can read style, num_valid
  163. and the masks.
  164. 0 if we need to read the watch_readback. */
  165. int watch_readback_valid;
  166. /* Cached watch register read values. */
  167. struct pt_watch_regs watch_readback;
  168. /* Current watchpoint requests for this process. */
  169. struct mips_watchpoint *current_watches;
  170. /* The current set of watch register values for writing the
  171. registers. */
  172. struct pt_watch_regs watch_mirror;
  173. };
  174. /* Per-thread arch-specific data we want to keep. */
  175. struct arch_lwp_info
  176. {
  177. /* Non-zero if our copy differs from what's recorded in the thread. */
  178. int watch_registers_changed;
  179. };
  180. /* From mips-linux-nat.c. */
  181. /* Pseudo registers can not be read. ptrace does not provide a way to
  182. read (or set) PS_REGNUM, and there's no point in reading or setting
  183. ZERO_REGNUM, it's always 0. We also can not set BADVADDR, CAUSE,
  184. or FCRIR via ptrace(). */
  185. bool
  186. mips_target::low_cannot_fetch_register (int regno)
  187. {
  188. const struct target_desc *tdesc;
  189. if (get_regs_info ()->usrregs->regmap[regno] == -1)
  190. return true;
  191. tdesc = current_process ()->tdesc;
  192. /* On n32 we can't access 64-bit registers via PTRACE_PEEKUSR. */
  193. if (register_size (tdesc, regno) > sizeof (PTRACE_XFER_TYPE))
  194. return true;
  195. if (find_regno (tdesc, "r0") == regno)
  196. return true;
  197. return false;
  198. }
  199. bool
  200. mips_target::low_cannot_store_register (int regno)
  201. {
  202. const struct target_desc *tdesc;
  203. if (get_regs_info ()->usrregs->regmap[regno] == -1)
  204. return true;
  205. tdesc = current_process ()->tdesc;
  206. /* On n32 we can't access 64-bit registers via PTRACE_POKEUSR. */
  207. if (register_size (tdesc, regno) > sizeof (PTRACE_XFER_TYPE))
  208. return true;
  209. if (find_regno (tdesc, "r0") == regno)
  210. return true;
  211. if (find_regno (tdesc, "cause") == regno)
  212. return true;
  213. if (find_regno (tdesc, "badvaddr") == regno)
  214. return true;
  215. if (find_regno (tdesc, "fir") == regno)
  216. return true;
  217. return false;
  218. }
  219. bool
  220. mips_target::low_fetch_register (regcache *regcache, int regno)
  221. {
  222. const struct target_desc *tdesc = current_process ()->tdesc;
  223. if (find_regno (tdesc, "r0") == regno)
  224. {
  225. supply_register_zeroed (regcache, regno);
  226. return true;
  227. }
  228. return false;
  229. }
  230. bool
  231. mips_target::low_supports_breakpoints ()
  232. {
  233. return true;
  234. }
  235. CORE_ADDR
  236. mips_target::low_get_pc (regcache *regcache)
  237. {
  238. union mips_register pc;
  239. collect_register_by_name (regcache, "pc", pc.buf);
  240. return register_size (regcache->tdesc, 0) == 4 ? pc.reg32 : pc.reg64;
  241. }
  242. void
  243. mips_target::low_set_pc (regcache *regcache, CORE_ADDR pc)
  244. {
  245. union mips_register newpc;
  246. if (register_size (regcache->tdesc, 0) == 4)
  247. newpc.reg32 = pc;
  248. else
  249. newpc.reg64 = pc;
  250. supply_register_by_name (regcache, "pc", newpc.buf);
  251. }
  252. /* Correct in either endianness. */
  253. static const unsigned int mips_breakpoint = 0x0005000d;
  254. #define mips_breakpoint_len 4
  255. /* Implementation of target ops method "sw_breakpoint_from_kind". */
  256. const gdb_byte *
  257. mips_target::sw_breakpoint_from_kind (int kind, int *size)
  258. {
  259. *size = mips_breakpoint_len;
  260. return (const gdb_byte *) &mips_breakpoint;
  261. }
  262. bool
  263. mips_target::low_breakpoint_at (CORE_ADDR where)
  264. {
  265. unsigned int insn;
  266. read_memory (where, (unsigned char *) &insn, 4);
  267. if (insn == mips_breakpoint)
  268. return true;
  269. /* If necessary, recognize more trap instructions here. GDB only uses the
  270. one. */
  271. return false;
  272. }
  273. /* Mark the watch registers of lwp, represented by ENTRY, as changed. */
  274. static void
  275. update_watch_registers_callback (thread_info *thread)
  276. {
  277. struct lwp_info *lwp = get_thread_lwp (thread);
  278. /* The actual update is done later just before resuming the lwp,
  279. we just mark that the registers need updating. */
  280. lwp->arch_private->watch_registers_changed = 1;
  281. /* If the lwp isn't stopped, force it to momentarily pause, so
  282. we can update its watch registers. */
  283. if (!lwp->stopped)
  284. linux_stop_lwp (lwp);
  285. }
  286. /* This is the implementation of linux target ops method
  287. low_new_process. */
  288. arch_process_info *
  289. mips_target::low_new_process ()
  290. {
  291. struct arch_process_info *info = XCNEW (struct arch_process_info);
  292. return info;
  293. }
  294. /* This is the implementation of linux target ops method
  295. low_delete_process. */
  296. void
  297. mips_target::low_delete_process (arch_process_info *info)
  298. {
  299. xfree (info);
  300. }
  301. /* This is the implementation of linux target ops method low_new_thread.
  302. Mark the watch registers as changed, so the threads' copies will
  303. be updated. */
  304. void
  305. mips_target::low_new_thread (lwp_info *lwp)
  306. {
  307. struct arch_lwp_info *info = XCNEW (struct arch_lwp_info);
  308. info->watch_registers_changed = 1;
  309. lwp->arch_private = info;
  310. }
  311. /* Function to call when a thread is being deleted. */
  312. void
  313. mips_target::low_delete_thread (arch_lwp_info *arch_lwp)
  314. {
  315. xfree (arch_lwp);
  316. }
  317. /* Create a new mips_watchpoint and add it to the list. */
  318. static void
  319. mips_add_watchpoint (struct arch_process_info *priv, CORE_ADDR addr, int len,
  320. enum target_hw_bp_type watch_type)
  321. {
  322. struct mips_watchpoint *new_watch;
  323. struct mips_watchpoint **pw;
  324. new_watch = XNEW (struct mips_watchpoint);
  325. new_watch->addr = addr;
  326. new_watch->len = len;
  327. new_watch->type = watch_type;
  328. new_watch->next = NULL;
  329. pw = &priv->current_watches;
  330. while (*pw != NULL)
  331. pw = &(*pw)->next;
  332. *pw = new_watch;
  333. }
  334. /* Hook to call when a new fork is attached. */
  335. void
  336. mips_target::low_new_fork (process_info *parent,
  337. process_info *child)
  338. {
  339. struct arch_process_info *parent_private;
  340. struct arch_process_info *child_private;
  341. struct mips_watchpoint *wp;
  342. /* These are allocated by linux_add_process. */
  343. gdb_assert (parent->priv != NULL
  344. && parent->priv->arch_private != NULL);
  345. gdb_assert (child->priv != NULL
  346. && child->priv->arch_private != NULL);
  347. /* Linux kernel before 2.6.33 commit
  348. 72f674d203cd230426437cdcf7dd6f681dad8b0d
  349. will inherit hardware debug registers from parent
  350. on fork/vfork/clone. Newer Linux kernels create such tasks with
  351. zeroed debug registers.
  352. GDB core assumes the child inherits the watchpoints/hw
  353. breakpoints of the parent, and will remove them all from the
  354. forked off process. Copy the debug registers mirrors into the
  355. new process so that all breakpoints and watchpoints can be
  356. removed together. The debug registers mirror will become zeroed
  357. in the end before detaching the forked off process, thus making
  358. this compatible with older Linux kernels too. */
  359. parent_private = parent->priv->arch_private;
  360. child_private = child->priv->arch_private;
  361. child_private->watch_readback_valid = parent_private->watch_readback_valid;
  362. child_private->watch_readback = parent_private->watch_readback;
  363. for (wp = parent_private->current_watches; wp != NULL; wp = wp->next)
  364. mips_add_watchpoint (child_private, wp->addr, wp->len, wp->type);
  365. child_private->watch_mirror = parent_private->watch_mirror;
  366. }
  367. /* This is the implementation of linux target ops method
  368. low_prepare_to_resume. If the watch regs have changed, update the
  369. thread's copies. */
  370. void
  371. mips_target::low_prepare_to_resume (lwp_info *lwp)
  372. {
  373. ptid_t ptid = ptid_of (get_lwp_thread (lwp));
  374. struct process_info *proc = find_process_pid (ptid.pid ());
  375. struct arch_process_info *priv = proc->priv->arch_private;
  376. if (lwp->arch_private->watch_registers_changed)
  377. {
  378. /* Only update the watch registers if we have set or unset a
  379. watchpoint already. */
  380. if (mips_linux_watch_get_num_valid (&priv->watch_mirror) > 0)
  381. {
  382. /* Write the mirrored watch register values. */
  383. int tid = ptid.lwp ();
  384. if (-1 == ptrace (PTRACE_SET_WATCH_REGS, tid,
  385. &priv->watch_mirror, NULL))
  386. perror_with_name ("Couldn't write watch register");
  387. }
  388. lwp->arch_private->watch_registers_changed = 0;
  389. }
  390. }
  391. bool
  392. mips_target::supports_z_point_type (char z_type)
  393. {
  394. switch (z_type)
  395. {
  396. case Z_PACKET_WRITE_WP:
  397. case Z_PACKET_READ_WP:
  398. case Z_PACKET_ACCESS_WP:
  399. return true;
  400. default:
  401. return false;
  402. }
  403. }
  404. /* This is the implementation of linux target ops method
  405. low_insert_point. */
  406. int
  407. mips_target::low_insert_point (raw_bkpt_type type, CORE_ADDR addr,
  408. int len, raw_breakpoint *bp)
  409. {
  410. struct process_info *proc = current_process ();
  411. struct arch_process_info *priv = proc->priv->arch_private;
  412. struct pt_watch_regs regs;
  413. long lwpid;
  414. enum target_hw_bp_type watch_type;
  415. uint32_t irw;
  416. lwpid = lwpid_of (current_thread);
  417. if (!mips_linux_read_watch_registers (lwpid,
  418. &priv->watch_readback,
  419. &priv->watch_readback_valid,
  420. 0))
  421. return -1;
  422. if (len <= 0)
  423. return -1;
  424. regs = priv->watch_readback;
  425. /* Add the current watches. */
  426. mips_linux_watch_populate_regs (priv->current_watches, &regs);
  427. /* Now try to add the new watch. */
  428. watch_type = raw_bkpt_type_to_target_hw_bp_type (type);
  429. irw = mips_linux_watch_type_to_irw (watch_type);
  430. if (!mips_linux_watch_try_one_watch (&regs, addr, len, irw))
  431. return -1;
  432. /* It fit. Stick it on the end of the list. */
  433. mips_add_watchpoint (priv, addr, len, watch_type);
  434. priv->watch_mirror = regs;
  435. /* Only update the threads of this process. */
  436. for_each_thread (proc->pid, update_watch_registers_callback);
  437. return 0;
  438. }
  439. /* This is the implementation of linux target ops method
  440. low_remove_point. */
  441. int
  442. mips_target::low_remove_point (raw_bkpt_type type, CORE_ADDR addr,
  443. int len, raw_breakpoint *bp)
  444. {
  445. struct process_info *proc = current_process ();
  446. struct arch_process_info *priv = proc->priv->arch_private;
  447. int deleted_one;
  448. enum target_hw_bp_type watch_type;
  449. struct mips_watchpoint **pw;
  450. struct mips_watchpoint *w;
  451. /* Search for a known watch that matches. Then unlink and free it. */
  452. watch_type = raw_bkpt_type_to_target_hw_bp_type (type);
  453. deleted_one = 0;
  454. pw = &priv->current_watches;
  455. while ((w = *pw))
  456. {
  457. if (w->addr == addr && w->len == len && w->type == watch_type)
  458. {
  459. *pw = w->next;
  460. free (w);
  461. deleted_one = 1;
  462. break;
  463. }
  464. pw = &(w->next);
  465. }
  466. if (!deleted_one)
  467. return -1; /* We don't know about it, fail doing nothing. */
  468. /* At this point watch_readback is known to be valid because we
  469. could not have added the watch without reading it. */
  470. gdb_assert (priv->watch_readback_valid == 1);
  471. priv->watch_mirror = priv->watch_readback;
  472. mips_linux_watch_populate_regs (priv->current_watches,
  473. &priv->watch_mirror);
  474. /* Only update the threads of this process. */
  475. for_each_thread (proc->pid, update_watch_registers_callback);
  476. return 0;
  477. }
  478. /* This is the implementation of linux target ops method
  479. low_stopped_by_watchpoint. The watchhi R and W bits indicate
  480. the watch register triggered. */
  481. bool
  482. mips_target::low_stopped_by_watchpoint ()
  483. {
  484. struct process_info *proc = current_process ();
  485. struct arch_process_info *priv = proc->priv->arch_private;
  486. int n;
  487. int num_valid;
  488. long lwpid = lwpid_of (current_thread);
  489. if (!mips_linux_read_watch_registers (lwpid,
  490. &priv->watch_readback,
  491. &priv->watch_readback_valid,
  492. 1))
  493. return 0;
  494. num_valid = mips_linux_watch_get_num_valid (&priv->watch_readback);
  495. for (n = 0; n < MAX_DEBUG_REGISTER && n < num_valid; n++)
  496. if (mips_linux_watch_get_watchhi (&priv->watch_readback, n)
  497. & (R_MASK | W_MASK))
  498. return true;
  499. return false;
  500. }
  501. /* This is the implementation of linux target ops method
  502. low_stopped_data_address. */
  503. CORE_ADDR
  504. mips_target::low_stopped_data_address ()
  505. {
  506. struct process_info *proc = current_process ();
  507. struct arch_process_info *priv = proc->priv->arch_private;
  508. int n;
  509. int num_valid;
  510. long lwpid = lwpid_of (current_thread);
  511. /* On MIPS we don't know the low order 3 bits of the data address.
  512. GDB does not support remote targets that can't report the
  513. watchpoint address. So, make our best guess; return the starting
  514. address of a watchpoint request which overlaps the one that
  515. triggered. */
  516. if (!mips_linux_read_watch_registers (lwpid,
  517. &priv->watch_readback,
  518. &priv->watch_readback_valid,
  519. 0))
  520. return 0;
  521. num_valid = mips_linux_watch_get_num_valid (&priv->watch_readback);
  522. for (n = 0; n < MAX_DEBUG_REGISTER && n < num_valid; n++)
  523. if (mips_linux_watch_get_watchhi (&priv->watch_readback, n)
  524. & (R_MASK | W_MASK))
  525. {
  526. CORE_ADDR t_low, t_hi;
  527. int t_irw;
  528. struct mips_watchpoint *watch;
  529. t_low = mips_linux_watch_get_watchlo (&priv->watch_readback, n);
  530. t_irw = t_low & IRW_MASK;
  531. t_hi = (mips_linux_watch_get_watchhi (&priv->watch_readback, n)
  532. | IRW_MASK);
  533. t_low &= ~(CORE_ADDR)t_hi;
  534. for (watch = priv->current_watches;
  535. watch != NULL;
  536. watch = watch->next)
  537. {
  538. CORE_ADDR addr = watch->addr;
  539. CORE_ADDR last_byte = addr + watch->len - 1;
  540. if ((t_irw & mips_linux_watch_type_to_irw (watch->type)) == 0)
  541. {
  542. /* Different type. */
  543. continue;
  544. }
  545. /* Check for overlap of even a single byte. */
  546. if (last_byte >= t_low && addr <= t_low + t_hi)
  547. return addr;
  548. }
  549. }
  550. /* Shouldn't happen. */
  551. return 0;
  552. }
  553. /* Fetch the thread-local storage pointer for libthread_db. */
  554. ps_err_e
  555. ps_get_thread_area (struct ps_prochandle *ph,
  556. lwpid_t lwpid, int idx, void **base)
  557. {
  558. if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
  559. return PS_ERR;
  560. /* IDX is the bias from the thread pointer to the beginning of the
  561. thread descriptor. It has to be subtracted due to implementation
  562. quirks in libthread_db. */
  563. *base = (void *) ((char *)*base - idx);
  564. return PS_OK;
  565. }
  566. static void
  567. mips_collect_register (struct regcache *regcache,
  568. int use_64bit, int regno, union mips_register *reg)
  569. {
  570. union mips_register tmp_reg;
  571. if (use_64bit)
  572. {
  573. collect_register (regcache, regno, &tmp_reg.reg64);
  574. *reg = tmp_reg;
  575. }
  576. else
  577. {
  578. collect_register (regcache, regno, &tmp_reg.reg32);
  579. reg->reg64 = tmp_reg.reg32;
  580. }
  581. }
  582. static void
  583. mips_supply_register (struct regcache *regcache,
  584. int use_64bit, int regno, const union mips_register *reg)
  585. {
  586. int offset = 0;
  587. /* For big-endian 32-bit targets, ignore the high four bytes of each
  588. eight-byte slot. */
  589. if (__BYTE_ORDER == __BIG_ENDIAN && !use_64bit)
  590. offset = 4;
  591. supply_register (regcache, regno, reg->buf + offset);
  592. }
  593. #ifdef HAVE_PTRACE_GETREGS
  594. static void
  595. mips_collect_register_32bit (struct regcache *regcache,
  596. int use_64bit, int regno, unsigned char *buf)
  597. {
  598. union mips_register tmp_reg;
  599. int reg32;
  600. mips_collect_register (regcache, use_64bit, regno, &tmp_reg);
  601. reg32 = tmp_reg.reg64;
  602. memcpy (buf, &reg32, 4);
  603. }
  604. static void
  605. mips_supply_register_32bit (struct regcache *regcache,
  606. int use_64bit, int regno, const unsigned char *buf)
  607. {
  608. union mips_register tmp_reg;
  609. int reg32;
  610. memcpy (&reg32, buf, 4);
  611. tmp_reg.reg64 = reg32;
  612. mips_supply_register (regcache, use_64bit, regno, &tmp_reg);
  613. }
  614. static void
  615. mips_fill_gregset (struct regcache *regcache, void *buf)
  616. {
  617. union mips_register *regset = (union mips_register *) buf;
  618. int i, use_64bit;
  619. const struct target_desc *tdesc = regcache->tdesc;
  620. use_64bit = (register_size (tdesc, 0) == 8);
  621. for (i = 1; i < 32; i++)
  622. mips_collect_register (regcache, use_64bit, i, regset + i);
  623. mips_collect_register (regcache, use_64bit,
  624. find_regno (tdesc, "lo"), regset + 32);
  625. mips_collect_register (regcache, use_64bit,
  626. find_regno (tdesc, "hi"), regset + 33);
  627. mips_collect_register (regcache, use_64bit,
  628. find_regno (tdesc, "pc"), regset + 34);
  629. mips_collect_register (regcache, use_64bit,
  630. find_regno (tdesc, "badvaddr"), regset + 35);
  631. mips_collect_register (regcache, use_64bit,
  632. find_regno (tdesc, "status"), regset + 36);
  633. mips_collect_register (regcache, use_64bit,
  634. find_regno (tdesc, "cause"), regset + 37);
  635. mips_collect_register (regcache, use_64bit,
  636. find_regno (tdesc, "restart"), regset + 0);
  637. }
  638. static void
  639. mips_store_gregset (struct regcache *regcache, const void *buf)
  640. {
  641. const union mips_register *regset = (const union mips_register *) buf;
  642. int i, use_64bit;
  643. use_64bit = (register_size (regcache->tdesc, 0) == 8);
  644. supply_register_by_name_zeroed (regcache, "r0");
  645. for (i = 1; i < 32; i++)
  646. mips_supply_register (regcache, use_64bit, i, regset + i);
  647. mips_supply_register (regcache, use_64bit,
  648. find_regno (regcache->tdesc, "lo"), regset + 32);
  649. mips_supply_register (regcache, use_64bit,
  650. find_regno (regcache->tdesc, "hi"), regset + 33);
  651. mips_supply_register (regcache, use_64bit,
  652. find_regno (regcache->tdesc, "pc"), regset + 34);
  653. mips_supply_register (regcache, use_64bit,
  654. find_regno (regcache->tdesc, "badvaddr"), regset + 35);
  655. mips_supply_register (regcache, use_64bit,
  656. find_regno (regcache->tdesc, "status"), regset + 36);
  657. mips_supply_register (regcache, use_64bit,
  658. find_regno (regcache->tdesc, "cause"), regset + 37);
  659. mips_supply_register (regcache, use_64bit,
  660. find_regno (regcache->tdesc, "restart"), regset + 0);
  661. }
  662. static void
  663. mips_fill_fpregset (struct regcache *regcache, void *buf)
  664. {
  665. union mips_register *regset = (union mips_register *) buf;
  666. int i, use_64bit, first_fp, big_endian;
  667. use_64bit = (register_size (regcache->tdesc, 0) == 8);
  668. first_fp = find_regno (regcache->tdesc, "f0");
  669. big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
  670. /* See GDB for a discussion of this peculiar layout. */
  671. for (i = 0; i < 32; i++)
  672. if (use_64bit)
  673. collect_register (regcache, first_fp + i, regset[i].buf);
  674. else
  675. collect_register (regcache, first_fp + i,
  676. regset[i & ~1].buf + 4 * (big_endian != (i & 1)));
  677. mips_collect_register_32bit (regcache, use_64bit,
  678. find_regno (regcache->tdesc, "fcsr"), regset[32].buf);
  679. mips_collect_register_32bit (regcache, use_64bit,
  680. find_regno (regcache->tdesc, "fir"),
  681. regset[32].buf + 4);
  682. }
  683. static void
  684. mips_store_fpregset (struct regcache *regcache, const void *buf)
  685. {
  686. const union mips_register *regset = (const union mips_register *) buf;
  687. int i, use_64bit, first_fp, big_endian;
  688. use_64bit = (register_size (regcache->tdesc, 0) == 8);
  689. first_fp = find_regno (regcache->tdesc, "f0");
  690. big_endian = (__BYTE_ORDER == __BIG_ENDIAN);
  691. /* See GDB for a discussion of this peculiar layout. */
  692. for (i = 0; i < 32; i++)
  693. if (use_64bit)
  694. supply_register (regcache, first_fp + i, regset[i].buf);
  695. else
  696. supply_register (regcache, first_fp + i,
  697. regset[i & ~1].buf + 4 * (big_endian != (i & 1)));
  698. mips_supply_register_32bit (regcache, use_64bit,
  699. find_regno (regcache->tdesc, "fcsr"),
  700. regset[32].buf);
  701. mips_supply_register_32bit (regcache, use_64bit,
  702. find_regno (regcache->tdesc, "fir"),
  703. regset[32].buf + 4);
  704. }
  705. #endif /* HAVE_PTRACE_GETREGS */
  706. /* Take care of 32-bit registers with 64-bit ptrace, POKEUSER side. */
  707. void
  708. mips_target::low_collect_ptrace_register (regcache *regcache, int regno,
  709. char *buf)
  710. {
  711. int use_64bit = sizeof (PTRACE_XFER_TYPE) == 8;
  712. if (use_64bit && register_size (regcache->tdesc, regno) == 4)
  713. {
  714. union mips_register reg;
  715. mips_collect_register (regcache, 0, regno, &reg);
  716. memcpy (buf, &reg, sizeof (reg));
  717. }
  718. else
  719. collect_register (regcache, regno, buf);
  720. }
  721. /* Take care of 32-bit registers with 64-bit ptrace, PEEKUSER side. */
  722. void
  723. mips_target::low_supply_ptrace_register (regcache *regcache, int regno,
  724. const char *buf)
  725. {
  726. int use_64bit = sizeof (PTRACE_XFER_TYPE) == 8;
  727. if (use_64bit && register_size (regcache->tdesc, regno) == 4)
  728. {
  729. union mips_register reg;
  730. memcpy (&reg, buf, sizeof (reg));
  731. mips_supply_register (regcache, 0, regno, &reg);
  732. }
  733. else
  734. supply_register (regcache, regno, buf);
  735. }
  736. static struct regset_info mips_regsets[] = {
  737. #ifdef HAVE_PTRACE_GETREGS
  738. { PTRACE_GETREGS, PTRACE_SETREGS, 0, 38 * 8, GENERAL_REGS,
  739. mips_fill_gregset, mips_store_gregset },
  740. { PTRACE_GETFPREGS, PTRACE_SETFPREGS, 0, 33 * 8, FP_REGS,
  741. mips_fill_fpregset, mips_store_fpregset },
  742. #endif /* HAVE_PTRACE_GETREGS */
  743. NULL_REGSET
  744. };
  745. static struct regsets_info mips_regsets_info =
  746. {
  747. mips_regsets, /* regsets */
  748. 0, /* num_regsets */
  749. NULL, /* disabled_regsets */
  750. };
  751. static struct usrregs_info mips_dsp_usrregs_info =
  752. {
  753. mips_dsp_num_regs,
  754. mips_dsp_regmap,
  755. };
  756. static struct usrregs_info mips_usrregs_info =
  757. {
  758. mips_num_regs,
  759. mips_regmap,
  760. };
  761. static struct regs_info dsp_regs_info =
  762. {
  763. mips_dsp_regset_bitmap,
  764. &mips_dsp_usrregs_info,
  765. &mips_regsets_info
  766. };
  767. static struct regs_info myregs_info =
  768. {
  769. NULL, /* regset_bitmap */
  770. &mips_usrregs_info,
  771. &mips_regsets_info
  772. };
  773. const regs_info *
  774. mips_target::get_regs_info ()
  775. {
  776. if (have_dsp)
  777. return &dsp_regs_info;
  778. else
  779. return &myregs_info;
  780. }
  781. /* The linux target ops object. */
  782. linux_process_target *the_linux_target = &the_mips_target;
  783. void
  784. initialize_low_arch (void)
  785. {
  786. /* Initialize the Linux target descriptions. */
  787. init_registers_mips_linux ();
  788. init_registers_mips_dsp_linux ();
  789. init_registers_mips64_linux ();
  790. init_registers_mips64_dsp_linux ();
  791. initialize_regsets_info (&mips_regsets_info);
  792. }