xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/mips-netbsd-nat.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Native-dependent code for MIPS systems running NetBSD.
2*6881a400Schristos 
3*6881a400Schristos    Copyright (C) 2000-2023 Free Software Foundation, Inc.
4*6881a400Schristos 
5*6881a400Schristos    This file is part of GDB.
6*6881a400Schristos 
7*6881a400Schristos    This program is free software; you can redistribute it and/or modify
8*6881a400Schristos    it under the terms of the GNU General Public License as published by
9*6881a400Schristos    the Free Software Foundation; either version 3 of the License, or
10*6881a400Schristos    (at your option) any later version.
11*6881a400Schristos 
12*6881a400Schristos    This program is distributed in the hope that it will be useful,
13*6881a400Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14*6881a400Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*6881a400Schristos    GNU General Public License for more details.
16*6881a400Schristos 
17*6881a400Schristos    You should have received a copy of the GNU General Public License
18*6881a400Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19*6881a400Schristos 
20*6881a400Schristos /* We define this to get types like register_t.  */
21*6881a400Schristos #include "defs.h"
22*6881a400Schristos #include "inferior.h"
23*6881a400Schristos #include "regcache.h"
24*6881a400Schristos #include "target.h"
25*6881a400Schristos 
26*6881a400Schristos #include <sys/types.h>
27*6881a400Schristos #include <sys/ptrace.h>
28*6881a400Schristos #include <machine/reg.h>
29*6881a400Schristos #include <machine/pcb.h>
30*6881a400Schristos 
31*6881a400Schristos #include "mips-tdep.h"
32*6881a400Schristos #include "mips-netbsd-tdep.h"
33*6881a400Schristos #include "netbsd-nat.h"
34*6881a400Schristos #include "inf-ptrace.h"
35*6881a400Schristos #include "bsd-kvm.h"
36*6881a400Schristos 
37*6881a400Schristos class mips_nbsd_nat_target final : public nbsd_nat_target
38*6881a400Schristos {
39*6881a400Schristos   void fetch_registers (struct regcache *, int) override;
40*6881a400Schristos   void store_registers (struct regcache *, int) override;
41*6881a400Schristos };
42*6881a400Schristos 
43*6881a400Schristos static mips_nbsd_nat_target the_mips_nbsd_nat_target;
44*6881a400Schristos 
45*6881a400Schristos /* Determine if PT_GETREGS fetches this register.  */
46*6881a400Schristos static int
47*6881a400Schristos getregs_supplies (struct gdbarch *gdbarch, int regno)
48*6881a400Schristos {
49*6881a400Schristos   return ((regno) >= MIPS_ZERO_REGNUM
50*6881a400Schristos 	  && (regno) <= gdbarch_pc_regnum (gdbarch));
51*6881a400Schristos }
52*6881a400Schristos 
53*6881a400Schristos void
54*6881a400Schristos mips_nbsd_nat_target::fetch_registers (struct regcache *regcache, int regno)
55*6881a400Schristos {
56*6881a400Schristos   pid_t pid = regcache->ptid ().pid ();
57*6881a400Schristos   int lwp = regcache->ptid ().lwp ();
58*6881a400Schristos 
59*6881a400Schristos   struct gdbarch *gdbarch = regcache->arch ();
60*6881a400Schristos   if (regno == -1 || getregs_supplies (gdbarch, regno))
61*6881a400Schristos     {
62*6881a400Schristos       struct reg regs;
63*6881a400Schristos 
64*6881a400Schristos       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
65*6881a400Schristos 	perror_with_name (_("Couldn't get registers"));
66*6881a400Schristos 
67*6881a400Schristos       mipsnbsd_supply_reg (regcache, (char *) &regs, regno);
68*6881a400Schristos       if (regno != -1)
69*6881a400Schristos 	return;
70*6881a400Schristos     }
71*6881a400Schristos 
72*6881a400Schristos   if (regno == -1
73*6881a400Schristos       || regno >= gdbarch_fp0_regnum (regcache->arch ()))
74*6881a400Schristos     {
75*6881a400Schristos       struct fpreg fpregs;
76*6881a400Schristos 
77*6881a400Schristos       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
78*6881a400Schristos 	perror_with_name (_("Couldn't get floating point status"));
79*6881a400Schristos 
80*6881a400Schristos       mipsnbsd_supply_fpreg (regcache, (char *) &fpregs, regno);
81*6881a400Schristos     }
82*6881a400Schristos }
83*6881a400Schristos 
84*6881a400Schristos void
85*6881a400Schristos mips_nbsd_nat_target::store_registers (struct regcache *regcache, int regno)
86*6881a400Schristos {
87*6881a400Schristos   pid_t pid = regcache->ptid ().pid ();
88*6881a400Schristos   int lwp = regcache->ptid ().lwp ();
89*6881a400Schristos 
90*6881a400Schristos   struct gdbarch *gdbarch = regcache->arch ();
91*6881a400Schristos   if (regno == -1 || getregs_supplies (gdbarch, regno))
92*6881a400Schristos     {
93*6881a400Schristos       struct reg regs;
94*6881a400Schristos 
95*6881a400Schristos       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
96*6881a400Schristos 	perror_with_name (_("Couldn't get registers"));
97*6881a400Schristos 
98*6881a400Schristos       mipsnbsd_fill_reg (regcache, (char *) &regs, regno);
99*6881a400Schristos 
100*6881a400Schristos       if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
101*6881a400Schristos 	perror_with_name (_("Couldn't write registers"));
102*6881a400Schristos 
103*6881a400Schristos       if (regno != -1)
104*6881a400Schristos 	return;
105*6881a400Schristos     }
106*6881a400Schristos 
107*6881a400Schristos   if (regno == -1
108*6881a400Schristos       || regno >= gdbarch_fp0_regnum (regcache->arch ()))
109*6881a400Schristos     {
110*6881a400Schristos       struct fpreg fpregs;
111*6881a400Schristos 
112*6881a400Schristos       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
113*6881a400Schristos 	perror_with_name (_("Couldn't get floating point status"));
114*6881a400Schristos 
115*6881a400Schristos       mipsnbsd_fill_fpreg (regcache, (char *) &fpregs, regno);
116*6881a400Schristos 
117*6881a400Schristos       if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
118*6881a400Schristos 	perror_with_name (_("Couldn't write floating point status"));
119*6881a400Schristos     }
120*6881a400Schristos }
121*6881a400Schristos 
122*6881a400Schristos static int
123*6881a400Schristos mipsnbsd_supply_pcb (struct regcache *regcache, struct pcb *pcb)
124*6881a400Schristos {
125*6881a400Schristos   struct label_t sf;
126*6881a400Schristos 
127*6881a400Schristos   sf = pcb->pcb_context;
128*6881a400Schristos 
129*6881a400Schristos   /* really should test for n{32,64} abi for this register
130*6881a400Schristos      unless this is purely the "n" ABI */
131*6881a400Schristos 
132*6881a400Schristos   regcache->raw_supply (MIPS_S0_REGNUM, &sf.val[_L_S0]);
133*6881a400Schristos   regcache->raw_supply (MIPS_S1_REGNUM, &sf.val[_L_S1]);
134*6881a400Schristos   regcache->raw_supply (MIPS_S2_REGNUM, &sf.val[_L_S2]);
135*6881a400Schristos   regcache->raw_supply (MIPS_S3_REGNUM, &sf.val[_L_S3]);
136*6881a400Schristos   regcache->raw_supply (MIPS_S4_REGNUM, &sf.val[_L_S4]);
137*6881a400Schristos   regcache->raw_supply (MIPS_S5_REGNUM, &sf.val[_L_S5]);
138*6881a400Schristos   regcache->raw_supply (MIPS_S6_REGNUM, &sf.val[_L_S6]);
139*6881a400Schristos   regcache->raw_supply (MIPS_S7_REGNUM, &sf.val[_L_S7]);
140*6881a400Schristos 
141*6881a400Schristos   regcache->raw_supply (MIPS_S8_REGNUM, &sf.val[_L_S8]);
142*6881a400Schristos 
143*6881a400Schristos   regcache->raw_supply (MIPS_T8_REGNUM, &sf.val[_L_T8]);
144*6881a400Schristos 
145*6881a400Schristos   regcache->raw_supply (MIPS_GP_REGNUM, &sf.val[_L_GP]);
146*6881a400Schristos 
147*6881a400Schristos   regcache->raw_supply (MIPS_SP_REGNUM, &sf.val[_L_SP]);
148*6881a400Schristos   regcache->raw_supply (MIPS_RA_REGNUM, &sf.val[_L_RA]);
149*6881a400Schristos   regcache->raw_supply (MIPS_PS_REGNUM, &sf.val[_L_SR]);
150*6881a400Schristos 
151*6881a400Schristos   /* provide the return address of the savectx as the current pc */
152*6881a400Schristos   regcache->raw_supply (MIPS_EMBED_PC_REGNUM, &sf.val[_L_RA]);
153*6881a400Schristos 
154*6881a400Schristos   return 0;
155*6881a400Schristos }
156*6881a400Schristos 
157*6881a400Schristos void _initialize_mipsnbsd_nat ();
158*6881a400Schristos void
159*6881a400Schristos _initialize_mipsnbsd_nat ()
160*6881a400Schristos {
161*6881a400Schristos   add_inf_child_target (&the_mips_nbsd_nat_target);
162*6881a400Schristos   bsd_kvm_add_target (mipsnbsd_supply_pcb);
163*6881a400Schristos }
164