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