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