1 /* Native-dependent code for NetBSD/sh.
2 
3    Copyright (C) 2002-2020 Free Software Foundation, Inc.
4 
5    Contributed by Wasabi Systems, Inc.
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 "inferior.h"
24 #include "gdbarch.h"
25 
26 #include <sys/types.h>
27 #include <sys/ptrace.h>
28 #include <machine/reg.h>
29 
30 #include "sh-tdep.h"
31 #include "inf-ptrace.h"
32 #include "nbsd-nat.h"
33 #include "regcache.h"
34 #include "inf-ptrace.h"
35 #include "nbsd-nat.h"
36 
37 struct sh_nbsd_nat_target final : public nbsd_nat_target
38 {
39   void fetch_registers (struct regcache *, int) override;
40   void store_registers (struct regcache *, int) override;
41 };
42 
43 static sh_nbsd_nat_target the_sh_nbsd_nat_target;
44 
45 /* Determine if PT_GETREGS fetches this register.  */
46 #define GETREGS_SUPPLIES(gdbarch, regno) \
47   (((regno) >= R0_REGNUM && (regno) <= (R0_REGNUM + 15)) \
48 || (regno) == gdbarch_pc_regnum (gdbarch) || (regno) == PR_REGNUM \
49 || (regno) == MACH_REGNUM || (regno) == MACL_REGNUM \
50 || (regno) == SR_REGNUM || (regno) == GBR_REGNUM)
51 
52 /* Sizeof `struct reg' in <machine/reg.h>.  */
53 #define SHNBSD_SIZEOF_GREGS	(22 * 4)
54 
55 void
fetch_registers(struct regcache * regcache,int regno)56 sh_nbsd_nat_target::fetch_registers (struct regcache *regcache, int regno)
57 {
58   pid_t pid = regcache->ptid ().pid ();
59   int lwp = regcache->ptid ().lwp ();
60 
61   if (regno == -1 || GETREGS_SUPPLIES (regcache->arch (), regno))
62     {
63       struct reg regs;
64 
65       if (ptrace (PT_GETREGS, pid,
66 		  (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
67 	perror_with_name (_("Couldn't get registers"));
68 
69       sh_corefile_supply_regset (&sh_corefile_gregset, regcache, regno,
70 				 (char *) &regs,
71 				 SHNBSD_SIZEOF_GREGS);
72 
73       if (regno != -1)
74 	return;
75     }
76 }
77 
78 void
store_registers(struct regcache * regcache,int regno)79 sh_nbsd_nat_target::store_registers (struct regcache *regcache, int regno)
80 {
81   pid_t pid = regcache->ptid ().pid ();
82   int lwp = regcache->ptid ().lwp ();
83 
84   if (regno == -1 || GETREGS_SUPPLIES (regcache->arch (), regno))
85     {
86       struct reg regs;
87 
88       if (ptrace (PT_GETREGS, pid,
89 		  (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
90 	perror_with_name (_("Couldn't get registers"));
91 
92       sh_corefile_collect_regset (&sh_corefile_gregset, regcache, regno,
93 				  (char *) &regs,
94 				  SHNBSD_SIZEOF_GREGS);
95 
96       if (ptrace (PT_SETREGS, pid,
97 		  (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
98 	perror_with_name (_("Couldn't set registers"));
99 
100       if (regno != -1)
101 	return;
102     }
103 }
104 
105 void _initialize_shnbsd_nat ();
106 void
_initialize_shnbsd_nat()107 _initialize_shnbsd_nat ()
108 {
109   add_inf_child_target (&the_sh_nbsd_nat_target);
110 }
111