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