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