Commit | Line | Data |
---|---|---|
5769d3cd | 1 | /* S390 native-dependent code for GDB, the GNU debugger. |
32d0add0 | 2 | Copyright (C) 2001-2015 Free Software Foundation, Inc. |
d0f54f9d | 3 | |
5769d3cd AC |
4 | Contributed by D.J. Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com) |
5 | for IBM Deutschland Entwicklung GmbH, IBM Corporation. | |
d0f54f9d | 6 | |
5769d3cd AC |
7 | This file is part of GDB. |
8 | ||
9 | This program is free software; you can redistribute it and/or modify | |
10 | it under the terms of the GNU General Public License as published by | |
a9762ec7 | 11 | the Free Software Foundation; either version 3 of the License, or |
5769d3cd AC |
12 | (at your option) any later version. |
13 | ||
14 | This program is distributed in the hope that it will be useful, | |
15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | GNU General Public License for more details. | |
18 | ||
19 | You should have received a copy of the GNU General Public License | |
a9762ec7 | 20 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
5769d3cd AC |
21 | |
22 | #include "defs.h" | |
3ecc0ae2 | 23 | #include "regcache.h" |
d0f54f9d | 24 | #include "inferior.h" |
10d6c8cd DJ |
25 | #include "target.h" |
26 | #include "linux-nat.h" | |
7803799a | 27 | #include "auxv.h" |
5538f557 | 28 | #include "gregset.h" |
99b7da5d | 29 | #include "regset.h" |
d0f54f9d | 30 | |
0e5fae36 | 31 | #include "s390-linux-tdep.h" |
c642a434 | 32 | #include "elf/common.h" |
d0f54f9d | 33 | |
5769d3cd AC |
34 | #include <asm/ptrace.h> |
35 | #include <sys/ptrace.h> | |
2d0c7962 | 36 | #include <asm/types.h> |
5769d3cd | 37 | #include <sys/procfs.h> |
5769d3cd | 38 | #include <sys/ucontext.h> |
7803799a UW |
39 | #include <elf.h> |
40 | ||
c642a434 UW |
41 | #ifndef PTRACE_GETREGSET |
42 | #define PTRACE_GETREGSET 0x4204 | |
43 | #endif | |
44 | ||
45 | #ifndef PTRACE_SETREGSET | |
46 | #define PTRACE_SETREGSET 0x4205 | |
47 | #endif | |
48 | ||
49 | static int have_regset_last_break = 0; | |
50 | static int have_regset_system_call = 0; | |
4ac33720 | 51 | static int have_regset_tdb = 0; |
550bdf96 | 52 | static int have_regset_vxrs = 0; |
5769d3cd | 53 | |
99b7da5d AA |
54 | /* Register map for 32-bit executables running under a 64-bit |
55 | kernel. */ | |
d0f54f9d JB |
56 | |
57 | #ifdef __s390x__ | |
99b7da5d AA |
58 | static const struct regcache_map_entry s390_64_regmap_gregset[] = |
59 | { | |
60 | /* Skip PSWM and PSWA, since they must be handled specially. */ | |
61 | { 2, REGCACHE_MAP_SKIP, 8 }, | |
62 | { 1, S390_R0_UPPER_REGNUM, 4 }, { 1, S390_R0_REGNUM, 4 }, | |
63 | { 1, S390_R1_UPPER_REGNUM, 4 }, { 1, S390_R1_REGNUM, 4 }, | |
64 | { 1, S390_R2_UPPER_REGNUM, 4 }, { 1, S390_R2_REGNUM, 4 }, | |
65 | { 1, S390_R3_UPPER_REGNUM, 4 }, { 1, S390_R3_REGNUM, 4 }, | |
66 | { 1, S390_R4_UPPER_REGNUM, 4 }, { 1, S390_R4_REGNUM, 4 }, | |
67 | { 1, S390_R5_UPPER_REGNUM, 4 }, { 1, S390_R5_REGNUM, 4 }, | |
68 | { 1, S390_R6_UPPER_REGNUM, 4 }, { 1, S390_R6_REGNUM, 4 }, | |
69 | { 1, S390_R7_UPPER_REGNUM, 4 }, { 1, S390_R7_REGNUM, 4 }, | |
70 | { 1, S390_R8_UPPER_REGNUM, 4 }, { 1, S390_R8_REGNUM, 4 }, | |
71 | { 1, S390_R9_UPPER_REGNUM, 4 }, { 1, S390_R9_REGNUM, 4 }, | |
72 | { 1, S390_R10_UPPER_REGNUM, 4 }, { 1, S390_R10_REGNUM, 4 }, | |
73 | { 1, S390_R11_UPPER_REGNUM, 4 }, { 1, S390_R11_REGNUM, 4 }, | |
74 | { 1, S390_R12_UPPER_REGNUM, 4 }, { 1, S390_R12_REGNUM, 4 }, | |
75 | { 1, S390_R13_UPPER_REGNUM, 4 }, { 1, S390_R13_REGNUM, 4 }, | |
76 | { 1, S390_R14_UPPER_REGNUM, 4 }, { 1, S390_R14_REGNUM, 4 }, | |
77 | { 1, S390_R15_UPPER_REGNUM, 4 }, { 1, S390_R15_REGNUM, 4 }, | |
78 | { 16, S390_A0_REGNUM, 4 }, | |
79 | { 1, REGCACHE_MAP_SKIP, 4 }, { 1, S390_ORIG_R2_REGNUM, 4 }, | |
80 | { 0 } | |
81 | }; | |
82 | ||
83 | static const struct regset s390_64_gregset = | |
84 | { | |
85 | s390_64_regmap_gregset, | |
86 | regcache_supply_regset, | |
87 | regcache_collect_regset | |
88 | }; | |
89 | ||
90 | #define S390_PSWM_OFFSET 0 | |
91 | #define S390_PSWA_OFFSET 8 | |
5769d3cd | 92 | #endif |
d0f54f9d | 93 | |
2ccd1468 UW |
94 | /* Fill GDB's register array with the general-purpose register values |
95 | in *REGP. | |
96 | ||
97 | When debugging a 32-bit executable running under a 64-bit kernel, | |
98 | we have to fix up the 64-bit registers we get from the kernel to | |
99 | make them look like 32-bit registers. */ | |
d6db1fab | 100 | |
2ccd1468 UW |
101 | void |
102 | supply_gregset (struct regcache *regcache, const gregset_t *regp) | |
103 | { | |
9cbd5950 | 104 | #ifdef __s390x__ |
d6db1fab | 105 | struct gdbarch *gdbarch = get_regcache_arch (regcache); |
2ccd1468 | 106 | if (gdbarch_ptr_bit (gdbarch) == 32) |
d6db1fab UW |
107 | { |
108 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); | |
99b7da5d | 109 | ULONGEST pswm, pswa; |
2ccd1468 | 110 | gdb_byte buf[4]; |
d6db1fab | 111 | |
99b7da5d AA |
112 | regcache_supply_regset (&s390_64_gregset, regcache, -1, |
113 | regp, sizeof (gregset_t)); | |
114 | pswm = extract_unsigned_integer ((const gdb_byte *) regp | |
115 | + S390_PSWM_OFFSET, 8, byte_order); | |
116 | pswa = extract_unsigned_integer ((const gdb_byte *) regp | |
117 | + S390_PSWA_OFFSET, 8, byte_order); | |
2ccd1468 UW |
118 | store_unsigned_integer (buf, 4, byte_order, (pswm >> 32) | 0x80000); |
119 | regcache_raw_supply (regcache, S390_PSWM_REGNUM, buf); | |
120 | store_unsigned_integer (buf, 4, byte_order, | |
121 | (pswa & 0x7fffffff) | (pswm & 0x80000000)); | |
122 | regcache_raw_supply (regcache, S390_PSWA_REGNUM, buf); | |
123 | return; | |
d6db1fab UW |
124 | } |
125 | #endif | |
126 | ||
99b7da5d AA |
127 | regcache_supply_regset (&s390_gregset, regcache, -1, regp, |
128 | sizeof (gregset_t)); | |
d6db1fab UW |
129 | } |
130 | ||
2ccd1468 UW |
131 | /* Fill register REGNO (if it is a general-purpose register) in |
132 | *REGP with the value in GDB's register array. If REGNO is -1, | |
133 | do this for all registers. */ | |
d6db1fab | 134 | |
2ccd1468 UW |
135 | void |
136 | fill_gregset (const struct regcache *regcache, gregset_t *regp, int regno) | |
137 | { | |
d6db1fab UW |
138 | #ifdef __s390x__ |
139 | struct gdbarch *gdbarch = get_regcache_arch (regcache); | |
2ccd1468 | 140 | if (gdbarch_ptr_bit (gdbarch) == 32) |
d6db1fab | 141 | { |
99b7da5d AA |
142 | regcache_collect_regset (&s390_64_gregset, regcache, regno, |
143 | regp, sizeof (gregset_t)); | |
d6db1fab | 144 | |
2ccd1468 UW |
145 | if (regno == -1 |
146 | || regno == S390_PSWM_REGNUM || regno == S390_PSWA_REGNUM) | |
d6db1fab | 147 | { |
2ccd1468 UW |
148 | enum bfd_endian byte_order = gdbarch_byte_order (gdbarch); |
149 | ULONGEST pswa, pswm; | |
d6db1fab UW |
150 | gdb_byte buf[4]; |
151 | ||
2ccd1468 UW |
152 | regcache_raw_collect (regcache, S390_PSWM_REGNUM, buf); |
153 | pswm = extract_unsigned_integer (buf, 4, byte_order); | |
154 | regcache_raw_collect (regcache, S390_PSWA_REGNUM, buf); | |
d6db1fab UW |
155 | pswa = extract_unsigned_integer (buf, 4, byte_order); |
156 | ||
2ccd1468 | 157 | if (regno == -1 || regno == S390_PSWM_REGNUM) |
99b7da5d AA |
158 | store_unsigned_integer ((gdb_byte *) regp + S390_PSWM_OFFSET, 8, |
159 | byte_order, ((pswm & 0xfff7ffff) << 32) | | |
2ccd1468 UW |
160 | (pswa & 0x80000000)); |
161 | if (regno == -1 || regno == S390_PSWA_REGNUM) | |
99b7da5d AA |
162 | store_unsigned_integer ((gdb_byte *) regp + S390_PSWA_OFFSET, 8, |
163 | byte_order, pswa & 0x7fffffff); | |
d6db1fab | 164 | } |
2ccd1468 | 165 | return; |
d6db1fab | 166 | } |
9cbd5950 JB |
167 | #endif |
168 | ||
99b7da5d AA |
169 | regcache_collect_regset (&s390_gregset, regcache, regno, regp, |
170 | sizeof (gregset_t)); | |
d0f54f9d JB |
171 | } |
172 | ||
173 | /* Fill GDB's register array with the floating-point register values | |
174 | in *REGP. */ | |
175 | void | |
7f7fe91e | 176 | supply_fpregset (struct regcache *regcache, const fpregset_t *regp) |
d0f54f9d | 177 | { |
99b7da5d AA |
178 | regcache_supply_regset (&s390_fpregset, regcache, -1, regp, |
179 | sizeof (fpregset_t)); | |
d0f54f9d JB |
180 | } |
181 | ||
182 | /* Fill register REGNO (if it is a general-purpose register) in | |
183 | *REGP with the value in GDB's register array. If REGNO is -1, | |
184 | do this for all registers. */ | |
185 | void | |
7f7fe91e | 186 | fill_fpregset (const struct regcache *regcache, fpregset_t *regp, int regno) |
d0f54f9d | 187 | { |
99b7da5d AA |
188 | regcache_collect_regset (&s390_fpregset, regcache, regno, regp, |
189 | sizeof (fpregset_t)); | |
d0f54f9d JB |
190 | } |
191 | ||
192 | /* Find the TID for the current inferior thread to use with ptrace. */ | |
193 | static int | |
194 | s390_inferior_tid (void) | |
195 | { | |
196 | /* GNU/Linux LWP ID's are process ID's. */ | |
dfd4cc63 | 197 | int tid = ptid_get_lwp (inferior_ptid); |
d0f54f9d | 198 | if (tid == 0) |
dfd4cc63 | 199 | tid = ptid_get_pid (inferior_ptid); /* Not a threaded program. */ |
d0f54f9d JB |
200 | |
201 | return tid; | |
202 | } | |
203 | ||
204 | /* Fetch all general-purpose registers from process/thread TID and | |
205 | store their values in GDB's register cache. */ | |
206 | static void | |
56be3814 | 207 | fetch_regs (struct regcache *regcache, int tid) |
d0f54f9d JB |
208 | { |
209 | gregset_t regs; | |
210 | ptrace_area parea; | |
211 | ||
212 | parea.len = sizeof (regs); | |
213 | parea.process_addr = (addr_t) ®s; | |
214 | parea.kernel_addr = offsetof (struct user_regs_struct, psw); | |
215 | if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0) | |
e2e0b3e5 | 216 | perror_with_name (_("Couldn't get registers")); |
d0f54f9d | 217 | |
56be3814 | 218 | supply_gregset (regcache, (const gregset_t *) ®s); |
d0f54f9d JB |
219 | } |
220 | ||
221 | /* Store all valid general-purpose registers in GDB's register cache | |
222 | into the process/thread specified by TID. */ | |
223 | static void | |
56be3814 | 224 | store_regs (const struct regcache *regcache, int tid, int regnum) |
d0f54f9d JB |
225 | { |
226 | gregset_t regs; | |
227 | ptrace_area parea; | |
228 | ||
229 | parea.len = sizeof (regs); | |
230 | parea.process_addr = (addr_t) ®s; | |
231 | parea.kernel_addr = offsetof (struct user_regs_struct, psw); | |
232 | if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0) | |
e2e0b3e5 | 233 | perror_with_name (_("Couldn't get registers")); |
d0f54f9d | 234 | |
56be3814 | 235 | fill_gregset (regcache, ®s, regnum); |
d0f54f9d JB |
236 | |
237 | if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0) | |
e2e0b3e5 | 238 | perror_with_name (_("Couldn't write registers")); |
d0f54f9d JB |
239 | } |
240 | ||
241 | /* Fetch all floating-point registers from process/thread TID and store | |
242 | their values in GDB's register cache. */ | |
243 | static void | |
56be3814 | 244 | fetch_fpregs (struct regcache *regcache, int tid) |
d0f54f9d JB |
245 | { |
246 | fpregset_t fpregs; | |
247 | ptrace_area parea; | |
248 | ||
249 | parea.len = sizeof (fpregs); | |
250 | parea.process_addr = (addr_t) &fpregs; | |
251 | parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs); | |
252 | if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0) | |
e2e0b3e5 | 253 | perror_with_name (_("Couldn't get floating point status")); |
d0f54f9d | 254 | |
56be3814 | 255 | supply_fpregset (regcache, (const fpregset_t *) &fpregs); |
d0f54f9d JB |
256 | } |
257 | ||
258 | /* Store all valid floating-point registers in GDB's register cache | |
259 | into the process/thread specified by TID. */ | |
260 | static void | |
56be3814 | 261 | store_fpregs (const struct regcache *regcache, int tid, int regnum) |
d0f54f9d JB |
262 | { |
263 | fpregset_t fpregs; | |
264 | ptrace_area parea; | |
265 | ||
266 | parea.len = sizeof (fpregs); | |
267 | parea.process_addr = (addr_t) &fpregs; | |
268 | parea.kernel_addr = offsetof (struct user_regs_struct, fp_regs); | |
269 | if (ptrace (PTRACE_PEEKUSR_AREA, tid, (long) &parea) < 0) | |
e2e0b3e5 | 270 | perror_with_name (_("Couldn't get floating point status")); |
d0f54f9d | 271 | |
56be3814 | 272 | fill_fpregset (regcache, &fpregs, regnum); |
d0f54f9d JB |
273 | |
274 | if (ptrace (PTRACE_POKEUSR_AREA, tid, (long) &parea) < 0) | |
e2e0b3e5 | 275 | perror_with_name (_("Couldn't write floating point status")); |
d0f54f9d JB |
276 | } |
277 | ||
99b7da5d AA |
278 | /* Fetch all registers in the kernel's register set whose number is |
279 | REGSET_ID, whose size is REGSIZE, and whose layout is described by | |
280 | REGSET, from process/thread TID and store their values in GDB's | |
281 | register cache. */ | |
c642a434 UW |
282 | static void |
283 | fetch_regset (struct regcache *regcache, int tid, | |
99b7da5d | 284 | int regset_id, int regsize, const struct regset *regset) |
c642a434 | 285 | { |
c642a434 UW |
286 | gdb_byte *buf = alloca (regsize); |
287 | struct iovec iov; | |
c642a434 UW |
288 | |
289 | iov.iov_base = buf; | |
290 | iov.iov_len = regsize; | |
291 | ||
99b7da5d | 292 | if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0) |
4ac33720 UW |
293 | { |
294 | if (errno == ENODATA) | |
99b7da5d | 295 | regcache_supply_regset (regset, regcache, -1, NULL, regsize); |
4ac33720 UW |
296 | else |
297 | perror_with_name (_("Couldn't get register set")); | |
298 | } | |
299 | else | |
99b7da5d | 300 | regcache_supply_regset (regset, regcache, -1, buf, regsize); |
c642a434 UW |
301 | } |
302 | ||
99b7da5d AA |
303 | /* Store all registers in the kernel's register set whose number is |
304 | REGSET_ID, whose size is REGSIZE, and whose layout is described by | |
305 | REGSET, from GDB's register cache back to process/thread TID. */ | |
c642a434 UW |
306 | static void |
307 | store_regset (struct regcache *regcache, int tid, | |
99b7da5d | 308 | int regset_id, int regsize, const struct regset *regset) |
c642a434 | 309 | { |
c642a434 UW |
310 | gdb_byte *buf = alloca (regsize); |
311 | struct iovec iov; | |
c642a434 UW |
312 | |
313 | iov.iov_base = buf; | |
314 | iov.iov_len = regsize; | |
315 | ||
99b7da5d | 316 | if (ptrace (PTRACE_GETREGSET, tid, (long) regset_id, (long) &iov) < 0) |
c642a434 UW |
317 | perror_with_name (_("Couldn't get register set")); |
318 | ||
99b7da5d | 319 | regcache_collect_regset (regset, regcache, -1, buf, regsize); |
c642a434 | 320 | |
99b7da5d | 321 | if (ptrace (PTRACE_SETREGSET, tid, (long) regset_id, (long) &iov) < 0) |
c642a434 UW |
322 | perror_with_name (_("Couldn't set register set")); |
323 | } | |
324 | ||
325 | /* Check whether the kernel provides a register set with number REGSET | |
326 | of size REGSIZE for process/thread TID. */ | |
327 | static int | |
328 | check_regset (int tid, int regset, int regsize) | |
329 | { | |
330 | gdb_byte *buf = alloca (regsize); | |
331 | struct iovec iov; | |
332 | ||
333 | iov.iov_base = buf; | |
334 | iov.iov_len = regsize; | |
335 | ||
4ac33720 UW |
336 | if (ptrace (PTRACE_GETREGSET, tid, (long) regset, (long) &iov) >= 0 |
337 | || errno == ENODATA) | |
c642a434 | 338 | return 1; |
4ac33720 | 339 | return 0; |
c642a434 UW |
340 | } |
341 | ||
d0f54f9d JB |
342 | /* Fetch register REGNUM from the child process. If REGNUM is -1, do |
343 | this for all registers. */ | |
10d6c8cd | 344 | static void |
28439f5e PA |
345 | s390_linux_fetch_inferior_registers (struct target_ops *ops, |
346 | struct regcache *regcache, int regnum) | |
d0f54f9d JB |
347 | { |
348 | int tid = s390_inferior_tid (); | |
349 | ||
2ccd1468 | 350 | if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum)) |
56be3814 | 351 | fetch_regs (regcache, tid); |
d0f54f9d | 352 | |
2ccd1468 | 353 | if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum)) |
56be3814 | 354 | fetch_fpregs (regcache, tid); |
c642a434 UW |
355 | |
356 | if (have_regset_last_break) | |
357 | if (regnum == -1 || regnum == S390_LAST_BREAK_REGNUM) | |
358 | fetch_regset (regcache, tid, NT_S390_LAST_BREAK, 8, | |
359 | (gdbarch_ptr_bit (get_regcache_arch (regcache)) == 32 | |
99b7da5d | 360 | ? &s390_last_break_regset : &s390x_last_break_regset)); |
c642a434 UW |
361 | |
362 | if (have_regset_system_call) | |
363 | if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM) | |
364 | fetch_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4, | |
99b7da5d | 365 | &s390_system_call_regset); |
4ac33720 UW |
366 | |
367 | if (have_regset_tdb) | |
368 | if (regnum == -1 || S390_IS_TDBREGSET_REGNUM (regnum)) | |
369 | fetch_regset (regcache, tid, NT_S390_TDB, s390_sizeof_tdbregset, | |
99b7da5d | 370 | &s390_tdb_regset); |
550bdf96 AA |
371 | |
372 | if (have_regset_vxrs) | |
373 | { | |
374 | if (regnum == -1 || (regnum >= S390_V0_LOWER_REGNUM | |
375 | && regnum <= S390_V15_LOWER_REGNUM)) | |
376 | fetch_regset (regcache, tid, NT_S390_VXRS_LOW, 16 * 8, | |
377 | &s390_vxrs_low_regset); | |
378 | if (regnum == -1 || (regnum >= S390_V16_REGNUM | |
379 | && regnum <= S390_V31_REGNUM)) | |
380 | fetch_regset (regcache, tid, NT_S390_VXRS_HIGH, 16 * 16, | |
381 | &s390_vxrs_high_regset); | |
382 | } | |
d0f54f9d JB |
383 | } |
384 | ||
385 | /* Store register REGNUM back into the child process. If REGNUM is | |
386 | -1, do this for all registers. */ | |
10d6c8cd | 387 | static void |
28439f5e PA |
388 | s390_linux_store_inferior_registers (struct target_ops *ops, |
389 | struct regcache *regcache, int regnum) | |
d0f54f9d JB |
390 | { |
391 | int tid = s390_inferior_tid (); | |
392 | ||
2ccd1468 | 393 | if (regnum == -1 || S390_IS_GREGSET_REGNUM (regnum)) |
56be3814 | 394 | store_regs (regcache, tid, regnum); |
d0f54f9d | 395 | |
2ccd1468 | 396 | if (regnum == -1 || S390_IS_FPREGSET_REGNUM (regnum)) |
56be3814 | 397 | store_fpregs (regcache, tid, regnum); |
c642a434 UW |
398 | |
399 | /* S390_LAST_BREAK_REGNUM is read-only. */ | |
400 | ||
401 | if (have_regset_system_call) | |
402 | if (regnum == -1 || regnum == S390_SYSTEM_CALL_REGNUM) | |
403 | store_regset (regcache, tid, NT_S390_SYSTEM_CALL, 4, | |
99b7da5d | 404 | &s390_system_call_regset); |
550bdf96 AA |
405 | |
406 | if (have_regset_vxrs) | |
407 | { | |
408 | if (regnum == -1 || (regnum >= S390_V0_LOWER_REGNUM | |
409 | && regnum <= S390_V15_LOWER_REGNUM)) | |
410 | store_regset (regcache, tid, NT_S390_VXRS_LOW, 16 * 8, | |
411 | &s390_vxrs_low_regset); | |
412 | if (regnum == -1 || (regnum >= S390_V16_REGNUM | |
413 | && regnum <= S390_V31_REGNUM)) | |
414 | store_regset (regcache, tid, NT_S390_VXRS_HIGH, 16 * 16, | |
415 | &s390_vxrs_high_regset); | |
416 | } | |
5769d3cd AC |
417 | } |
418 | ||
d0f54f9d | 419 | |
e1457d83 JB |
420 | /* Hardware-assisted watchpoint handling. */ |
421 | ||
422 | /* We maintain a list of all currently active watchpoints in order | |
423 | to properly handle watchpoint removal. | |
424 | ||
425 | The only thing we actually need is the total address space area | |
426 | spanned by the watchpoints. */ | |
427 | ||
5769d3cd AC |
428 | struct watch_area |
429 | { | |
e1457d83 | 430 | struct watch_area *next; |
5769d3cd AC |
431 | CORE_ADDR lo_addr; |
432 | CORE_ADDR hi_addr; | |
433 | }; | |
434 | ||
e1457d83 | 435 | static struct watch_area *watch_base = NULL; |
5769d3cd | 436 | |
fd7979d1 | 437 | static int |
6a109b6b | 438 | s390_stopped_by_watchpoint (struct target_ops *ops) |
5769d3cd AC |
439 | { |
440 | per_lowcore_bits per_lowcore; | |
441 | ptrace_area parea; | |
9f0bdab8 | 442 | int result; |
5769d3cd | 443 | |
e1457d83 JB |
444 | /* Speed up common case. */ |
445 | if (!watch_base) | |
446 | return 0; | |
447 | ||
5769d3cd AC |
448 | parea.len = sizeof (per_lowcore); |
449 | parea.process_addr = (addr_t) & per_lowcore; | |
450 | parea.kernel_addr = offsetof (struct user_regs_struct, per_info.lowcore); | |
e1457d83 | 451 | if (ptrace (PTRACE_PEEKUSR_AREA, s390_inferior_tid (), &parea) < 0) |
e2e0b3e5 | 452 | perror_with_name (_("Couldn't retrieve watchpoint status")); |
5769d3cd | 453 | |
9f0bdab8 DJ |
454 | result = (per_lowcore.perc_storage_alteration == 1 |
455 | && per_lowcore.perc_store_real_address == 0); | |
456 | ||
457 | if (result) | |
458 | { | |
459 | /* Do not report this watchpoint again. */ | |
460 | memset (&per_lowcore, 0, sizeof (per_lowcore)); | |
461 | if (ptrace (PTRACE_POKEUSR_AREA, s390_inferior_tid (), &parea) < 0) | |
462 | perror_with_name (_("Couldn't clear watchpoint status")); | |
463 | } | |
464 | ||
465 | return result; | |
e1457d83 | 466 | } |
5769d3cd | 467 | |
e1457d83 | 468 | static void |
7b50312a | 469 | s390_fix_watch_points (struct lwp_info *lp) |
5769d3cd | 470 | { |
9f0bdab8 | 471 | int tid; |
e1457d83 | 472 | |
5769d3cd AC |
473 | per_struct per_info; |
474 | ptrace_area parea; | |
475 | ||
e1457d83 JB |
476 | CORE_ADDR watch_lo_addr = (CORE_ADDR)-1, watch_hi_addr = 0; |
477 | struct watch_area *area; | |
478 | ||
dfd4cc63 | 479 | tid = ptid_get_lwp (lp->ptid); |
9f0bdab8 | 480 | if (tid == 0) |
dfd4cc63 | 481 | tid = ptid_get_pid (lp->ptid); |
9f0bdab8 | 482 | |
e1457d83 JB |
483 | for (area = watch_base; area; area = area->next) |
484 | { | |
485 | watch_lo_addr = min (watch_lo_addr, area->lo_addr); | |
486 | watch_hi_addr = max (watch_hi_addr, area->hi_addr); | |
487 | } | |
488 | ||
5769d3cd AC |
489 | parea.len = sizeof (per_info); |
490 | parea.process_addr = (addr_t) & per_info; | |
e1457d83 JB |
491 | parea.kernel_addr = offsetof (struct user_regs_struct, per_info); |
492 | if (ptrace (PTRACE_PEEKUSR_AREA, tid, &parea) < 0) | |
e2e0b3e5 | 493 | perror_with_name (_("Couldn't retrieve watchpoint status")); |
e1457d83 JB |
494 | |
495 | if (watch_base) | |
5769d3cd AC |
496 | { |
497 | per_info.control_regs.bits.em_storage_alteration = 1; | |
498 | per_info.control_regs.bits.storage_alt_space_ctl = 1; | |
499 | } | |
500 | else | |
501 | { | |
502 | per_info.control_regs.bits.em_storage_alteration = 0; | |
503 | per_info.control_regs.bits.storage_alt_space_ctl = 0; | |
504 | } | |
505 | per_info.starting_addr = watch_lo_addr; | |
506 | per_info.ending_addr = watch_hi_addr; | |
e1457d83 JB |
507 | |
508 | if (ptrace (PTRACE_POKEUSR_AREA, tid, &parea) < 0) | |
e2e0b3e5 | 509 | perror_with_name (_("Couldn't modify watchpoint status")); |
5769d3cd AC |
510 | } |
511 | ||
fd7979d1 | 512 | static int |
7bb99c53 TT |
513 | s390_insert_watchpoint (struct target_ops *self, |
514 | CORE_ADDR addr, int len, int type, | |
0cf6dd15 | 515 | struct expression *cond) |
5769d3cd | 516 | { |
9f0bdab8 | 517 | struct lwp_info *lp; |
e1457d83 | 518 | struct watch_area *area = xmalloc (sizeof (struct watch_area)); |
9f0bdab8 | 519 | |
e1457d83 | 520 | if (!area) |
34201ae3 | 521 | return -1; |
e1457d83 JB |
522 | |
523 | area->lo_addr = addr; | |
524 | area->hi_addr = addr + len - 1; | |
34201ae3 | 525 | |
e1457d83 JB |
526 | area->next = watch_base; |
527 | watch_base = area; | |
528 | ||
4c38200f | 529 | ALL_LWPS (lp) |
3e290cb1 | 530 | s390_fix_watch_points (lp); |
e1457d83 | 531 | return 0; |
5769d3cd AC |
532 | } |
533 | ||
fd7979d1 | 534 | static int |
11b5219a TT |
535 | s390_remove_watchpoint (struct target_ops *self, |
536 | CORE_ADDR addr, int len, int type, | |
0cf6dd15 | 537 | struct expression *cond) |
5769d3cd | 538 | { |
9f0bdab8 | 539 | struct lwp_info *lp; |
e1457d83 JB |
540 | struct watch_area *area, **parea; |
541 | ||
542 | for (parea = &watch_base; *parea; parea = &(*parea)->next) | |
543 | if ((*parea)->lo_addr == addr | |
544 | && (*parea)->hi_addr == addr + len - 1) | |
545 | break; | |
546 | ||
547 | if (!*parea) | |
5769d3cd AC |
548 | { |
549 | fprintf_unfiltered (gdb_stderr, | |
e1457d83 | 550 | "Attempt to remove nonexistent watchpoint.\n"); |
5769d3cd AC |
551 | return -1; |
552 | } | |
e1457d83 JB |
553 | |
554 | area = *parea; | |
555 | *parea = area->next; | |
556 | xfree (area); | |
557 | ||
4c38200f | 558 | ALL_LWPS (lp) |
3e290cb1 | 559 | s390_fix_watch_points (lp); |
e1457d83 | 560 | return 0; |
5769d3cd AC |
561 | } |
562 | ||
fd7979d1 | 563 | static int |
5461485a TT |
564 | s390_can_use_hw_breakpoint (struct target_ops *self, |
565 | int type, int cnt, int othertype) | |
fd7979d1 | 566 | { |
b1798462 | 567 | return type == bp_hardware_watchpoint; |
fd7979d1 | 568 | } |
e1457d83 | 569 | |
fd7979d1 | 570 | static int |
31568a15 TT |
571 | s390_region_ok_for_hw_watchpoint (struct target_ops *self, |
572 | CORE_ADDR addr, int cnt) | |
5769d3cd | 573 | { |
fd7979d1 | 574 | return 1; |
5769d3cd AC |
575 | } |
576 | ||
7803799a UW |
577 | static int |
578 | s390_target_wordsize (void) | |
579 | { | |
580 | int wordsize = 4; | |
581 | ||
582 | /* Check for 64-bit inferior process. This is the case when the host is | |
583 | 64-bit, and in addition bit 32 of the PSW mask is set. */ | |
584 | #ifdef __s390x__ | |
585 | long pswm; | |
586 | ||
587 | errno = 0; | |
588 | pswm = (long) ptrace (PTRACE_PEEKUSER, s390_inferior_tid (), PT_PSWMASK, 0); | |
589 | if (errno == 0 && (pswm & 0x100000000ul) != 0) | |
590 | wordsize = 8; | |
591 | #endif | |
592 | ||
593 | return wordsize; | |
594 | } | |
595 | ||
596 | static int | |
597 | s390_auxv_parse (struct target_ops *ops, gdb_byte **readptr, | |
598 | gdb_byte *endptr, CORE_ADDR *typep, CORE_ADDR *valp) | |
599 | { | |
600 | int sizeof_auxv_field = s390_target_wordsize (); | |
f5656ead | 601 | enum bfd_endian byte_order = gdbarch_byte_order (target_gdbarch ()); |
7803799a UW |
602 | gdb_byte *ptr = *readptr; |
603 | ||
604 | if (endptr == ptr) | |
605 | return 0; | |
606 | ||
607 | if (endptr - ptr < sizeof_auxv_field * 2) | |
608 | return -1; | |
609 | ||
610 | *typep = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order); | |
611 | ptr += sizeof_auxv_field; | |
612 | *valp = extract_unsigned_integer (ptr, sizeof_auxv_field, byte_order); | |
613 | ptr += sizeof_auxv_field; | |
614 | ||
615 | *readptr = ptr; | |
616 | return 1; | |
617 | } | |
618 | ||
7803799a UW |
619 | static const struct target_desc * |
620 | s390_read_description (struct target_ops *ops) | |
621 | { | |
c642a434 UW |
622 | int tid = s390_inferior_tid (); |
623 | ||
624 | have_regset_last_break | |
625 | = check_regset (tid, NT_S390_LAST_BREAK, 8); | |
626 | have_regset_system_call | |
627 | = check_regset (tid, NT_S390_SYSTEM_CALL, 4); | |
628 | ||
7803799a UW |
629 | /* If GDB itself is compiled as 64-bit, we are running on a machine in |
630 | z/Architecture mode. If the target is running in 64-bit addressing | |
631 | mode, report s390x architecture. If the target is running in 31-bit | |
632 | addressing mode, but the kernel supports using 64-bit registers in | |
633 | that mode, report s390 architecture with 64-bit GPRs. */ | |
550bdf96 AA |
634 | #ifdef __s390x__ |
635 | { | |
636 | CORE_ADDR hwcap = 0; | |
637 | ||
638 | target_auxv_search (¤t_target, AT_HWCAP, &hwcap); | |
639 | have_regset_tdb = (hwcap & HWCAP_S390_TE) | |
640 | && check_regset (tid, NT_S390_TDB, s390_sizeof_tdbregset); | |
641 | ||
642 | have_regset_vxrs = (hwcap & HWCAP_S390_VX) | |
643 | && check_regset (tid, NT_S390_VXRS_LOW, 16 * 8) | |
644 | && check_regset (tid, NT_S390_VXRS_HIGH, 16 * 16); | |
645 | ||
646 | if (s390_target_wordsize () == 8) | |
647 | return (have_regset_vxrs ? | |
648 | (have_regset_tdb ? tdesc_s390x_tevx_linux64 : | |
649 | tdesc_s390x_vx_linux64) : | |
650 | have_regset_tdb ? tdesc_s390x_te_linux64 : | |
651 | have_regset_system_call ? tdesc_s390x_linux64v2 : | |
652 | have_regset_last_break ? tdesc_s390x_linux64v1 : | |
653 | tdesc_s390x_linux64); | |
654 | ||
655 | if (hwcap & HWCAP_S390_HIGH_GPRS) | |
656 | return (have_regset_vxrs ? | |
657 | (have_regset_tdb ? tdesc_s390_tevx_linux64 : | |
658 | tdesc_s390_vx_linux64) : | |
659 | have_regset_tdb ? tdesc_s390_te_linux64 : | |
660 | have_regset_system_call ? tdesc_s390_linux64v2 : | |
661 | have_regset_last_break ? tdesc_s390_linux64v1 : | |
662 | tdesc_s390_linux64); | |
663 | } | |
7803799a UW |
664 | #endif |
665 | ||
666 | /* If GDB itself is compiled as 31-bit, or if we're running a 31-bit inferior | |
667 | on a 64-bit kernel that does not support using 64-bit registers in 31-bit | |
668 | mode, report s390 architecture with 32-bit GPRs. */ | |
c642a434 UW |
669 | return (have_regset_system_call? tdesc_s390_linux32v2 : |
670 | have_regset_last_break? tdesc_s390_linux32v1 : | |
671 | tdesc_s390_linux32); | |
7803799a | 672 | } |
fd7979d1 | 673 | |
10d6c8cd DJ |
674 | void _initialize_s390_nat (void); |
675 | ||
676 | void | |
677 | _initialize_s390_nat (void) | |
678 | { | |
679 | struct target_ops *t; | |
680 | ||
681 | /* Fill in the generic GNU/Linux methods. */ | |
682 | t = linux_target (); | |
683 | ||
684 | /* Add our register access methods. */ | |
685 | t->to_fetch_registers = s390_linux_fetch_inferior_registers; | |
686 | t->to_store_registers = s390_linux_store_inferior_registers; | |
687 | ||
fd7979d1 UW |
688 | /* Add our watchpoint methods. */ |
689 | t->to_can_use_hw_breakpoint = s390_can_use_hw_breakpoint; | |
2a3cdf79 | 690 | t->to_region_ok_for_hw_watchpoint = s390_region_ok_for_hw_watchpoint; |
fd7979d1 UW |
691 | t->to_have_continuable_watchpoint = 1; |
692 | t->to_stopped_by_watchpoint = s390_stopped_by_watchpoint; | |
693 | t->to_insert_watchpoint = s390_insert_watchpoint; | |
694 | t->to_remove_watchpoint = s390_remove_watchpoint; | |
695 | ||
7803799a UW |
696 | /* Detect target architecture. */ |
697 | t->to_read_description = s390_read_description; | |
698 | t->to_auxv_parse = s390_auxv_parse; | |
699 | ||
10d6c8cd | 700 | /* Register the target. */ |
f973ed9c | 701 | linux_nat_add_target (t); |
9f0bdab8 | 702 | linux_nat_set_new_thread (t, s390_fix_watch_points); |
10d6c8cd | 703 | } |