xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/or1k-linux-nat.c (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* Native-dependent code for GNU/Linux OpenRISC.
2*6881a400Schristos    Copyright (C) 2021-2023 Free Software Foundation, Inc.
3*6881a400Schristos 
4*6881a400Schristos    This file is part of GDB.
5*6881a400Schristos 
6*6881a400Schristos    This program is free software; you can redistribute it and/or modify
7*6881a400Schristos    it under the terms of the GNU General Public License as published by
8*6881a400Schristos    the Free Software Foundation; either version 3 of the License, or
9*6881a400Schristos    (at your option) any later version.
10*6881a400Schristos 
11*6881a400Schristos    This program is distributed in the hope that it will be useful,
12*6881a400Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*6881a400Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*6881a400Schristos    GNU General Public License for more details.
15*6881a400Schristos 
16*6881a400Schristos    You should have received a copy of the GNU General Public License
17*6881a400Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18*6881a400Schristos 
19*6881a400Schristos #include "defs.h"
20*6881a400Schristos #include "regcache.h"
21*6881a400Schristos #include "gregset.h"
22*6881a400Schristos #include "linux-nat.h"
23*6881a400Schristos #include "or1k-tdep.h"
24*6881a400Schristos #include "or1k-linux-tdep.h"
25*6881a400Schristos #include "inferior.h"
26*6881a400Schristos 
27*6881a400Schristos #include "elf/common.h"
28*6881a400Schristos 
29*6881a400Schristos #include <sys/ptrace.h>
30*6881a400Schristos 
31*6881a400Schristos /* OpenRISC Linux native additions to the default linux support.  */
32*6881a400Schristos 
33*6881a400Schristos class or1k_linux_nat_target final : public linux_nat_target
34*6881a400Schristos {
35*6881a400Schristos public:
36*6881a400Schristos   /* Add our register access methods.  */
37*6881a400Schristos   void fetch_registers (struct regcache *regcache, int regnum) override;
38*6881a400Schristos   void store_registers (struct regcache *regcache, int regnum) override;
39*6881a400Schristos 
40*6881a400Schristos   /* Read suitable target description.  */
41*6881a400Schristos   const struct target_desc *read_description () override;
42*6881a400Schristos };
43*6881a400Schristos 
44*6881a400Schristos static or1k_linux_nat_target the_or1k_linux_nat_target;
45*6881a400Schristos 
46*6881a400Schristos /* Copy general purpose register REGNUM (or all gp regs if REGNUM == -1)
47*6881a400Schristos    from regset GREGS into REGCACHE.  */
48*6881a400Schristos 
49*6881a400Schristos static void
50*6881a400Schristos supply_gregset_regnum (struct regcache *regcache, const prgregset_t *gregs,
51*6881a400Schristos 		       int regnum)
52*6881a400Schristos {
53*6881a400Schristos   int i;
54*6881a400Schristos   const elf_greg_t *regp = *gregs;
55*6881a400Schristos 
56*6881a400Schristos   /* Access all registers */
57*6881a400Schristos   if (regnum == -1)
58*6881a400Schristos     {
59*6881a400Schristos       /* We fill the general purpose registers.  */
60*6881a400Schristos       for (i = OR1K_ZERO_REGNUM + 1; i < OR1K_MAX_GPR_REGS; i++)
61*6881a400Schristos 	regcache->raw_supply (i, regp + i);
62*6881a400Schristos 
63*6881a400Schristos       /* Supply OR1K_NPC_REGNUM from index 32.  */
64*6881a400Schristos       regcache->raw_supply (OR1K_NPC_REGNUM, regp + 32);
65*6881a400Schristos 
66*6881a400Schristos       /* Fill the inaccessible zero register with zero.  */
67*6881a400Schristos       regcache->raw_supply_zeroed (0);
68*6881a400Schristos     }
69*6881a400Schristos   else if (regnum == OR1K_ZERO_REGNUM)
70*6881a400Schristos     regcache->raw_supply_zeroed (0);
71*6881a400Schristos   else if (regnum == OR1K_NPC_REGNUM)
72*6881a400Schristos     regcache->raw_supply (OR1K_NPC_REGNUM, regp + 32);
73*6881a400Schristos   else if (regnum > OR1K_ZERO_REGNUM && regnum < OR1K_MAX_GPR_REGS)
74*6881a400Schristos     regcache->raw_supply (regnum, regp + regnum);
75*6881a400Schristos }
76*6881a400Schristos 
77*6881a400Schristos /* Copy all general purpose registers from regset GREGS into REGCACHE.  */
78*6881a400Schristos 
79*6881a400Schristos void
80*6881a400Schristos supply_gregset (struct regcache *regcache, const prgregset_t *gregs)
81*6881a400Schristos {
82*6881a400Schristos   supply_gregset_regnum (regcache, gregs, -1);
83*6881a400Schristos }
84*6881a400Schristos 
85*6881a400Schristos /* Copy general purpose register REGNUM (or all gp regs if REGNUM == -1)
86*6881a400Schristos    from REGCACHE into regset GREGS.  */
87*6881a400Schristos 
88*6881a400Schristos void
89*6881a400Schristos fill_gregset (const struct regcache *regcache, prgregset_t *gregs, int regnum)
90*6881a400Schristos {
91*6881a400Schristos   elf_greg_t *regp = *gregs;
92*6881a400Schristos 
93*6881a400Schristos   if (regnum == -1)
94*6881a400Schristos     {
95*6881a400Schristos       /* We fill the general purpose registers.  */
96*6881a400Schristos       for (int i = OR1K_ZERO_REGNUM + 1; i < OR1K_MAX_GPR_REGS; i++)
97*6881a400Schristos 	regcache->raw_collect (i, regp + i);
98*6881a400Schristos 
99*6881a400Schristos       regcache->raw_collect (OR1K_NPC_REGNUM, regp + 32);
100*6881a400Schristos     }
101*6881a400Schristos   else if (regnum == OR1K_ZERO_REGNUM)
102*6881a400Schristos     /* Nothing to do here.  */
103*6881a400Schristos     ;
104*6881a400Schristos   else if (regnum > OR1K_ZERO_REGNUM && regnum < OR1K_MAX_GPR_REGS)
105*6881a400Schristos     regcache->raw_collect (regnum, regp + regnum);
106*6881a400Schristos   else if (regnum == OR1K_NPC_REGNUM)
107*6881a400Schristos     regcache->raw_collect (OR1K_NPC_REGNUM, regp + 32);
108*6881a400Schristos }
109*6881a400Schristos 
110*6881a400Schristos /* Transfering floating-point registers between GDB, inferiors and cores.
111*6881a400Schristos    Since OpenRISC floating-point registers are the same as GPRs these do
112*6881a400Schristos    nothing.  */
113*6881a400Schristos 
114*6881a400Schristos void
115*6881a400Schristos supply_fpregset (struct regcache *regcache, const gdb_fpregset_t *fpregs)
116*6881a400Schristos {
117*6881a400Schristos }
118*6881a400Schristos 
119*6881a400Schristos void
120*6881a400Schristos fill_fpregset (const struct regcache *regcache,
121*6881a400Schristos 	       gdb_fpregset_t *fpregs, int regno)
122*6881a400Schristos {
123*6881a400Schristos }
124*6881a400Schristos 
125*6881a400Schristos /* Return a target description for the current target.  */
126*6881a400Schristos 
127*6881a400Schristos const struct target_desc *
128*6881a400Schristos or1k_linux_nat_target::read_description ()
129*6881a400Schristos {
130*6881a400Schristos   return tdesc_or1k_linux;
131*6881a400Schristos }
132*6881a400Schristos 
133*6881a400Schristos /* Fetch REGNUM (or all registers if REGNUM == -1) from the target
134*6881a400Schristos    into REGCACHE using PTRACE_GETREGSET.  */
135*6881a400Schristos 
136*6881a400Schristos void
137*6881a400Schristos or1k_linux_nat_target::fetch_registers (struct regcache *regcache, int regnum)
138*6881a400Schristos {
139*6881a400Schristos   int tid;
140*6881a400Schristos 
141*6881a400Schristos   tid = get_ptrace_pid (regcache->ptid());
142*6881a400Schristos 
143*6881a400Schristos   if ((regnum >= OR1K_ZERO_REGNUM && regnum < OR1K_MAX_GPR_REGS)
144*6881a400Schristos       || (regnum == OR1K_NPC_REGNUM)
145*6881a400Schristos       || (regnum == -1))
146*6881a400Schristos     {
147*6881a400Schristos       struct iovec iov;
148*6881a400Schristos       elf_gregset_t regs;
149*6881a400Schristos 
150*6881a400Schristos       iov.iov_base = &regs;
151*6881a400Schristos       iov.iov_len = sizeof (regs);
152*6881a400Schristos 
153*6881a400Schristos       if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS,
154*6881a400Schristos 		  (PTRACE_TYPE_ARG3) &iov) == -1)
155*6881a400Schristos 	perror_with_name (_("Couldn't get registers"));
156*6881a400Schristos       else
157*6881a400Schristos 	supply_gregset_regnum (regcache, &regs, regnum);
158*6881a400Schristos     }
159*6881a400Schristos 
160*6881a400Schristos   /* Access to other SPRs has potential security issues, don't support them for
161*6881a400Schristos      now.  */
162*6881a400Schristos }
163*6881a400Schristos 
164*6881a400Schristos /* Store REGNUM (or all registers if REGNUM == -1) to the target
165*6881a400Schristos    from REGCACHE using PTRACE_SETREGSET.  */
166*6881a400Schristos 
167*6881a400Schristos void
168*6881a400Schristos or1k_linux_nat_target::store_registers (struct regcache *regcache, int regnum)
169*6881a400Schristos {
170*6881a400Schristos   int tid;
171*6881a400Schristos 
172*6881a400Schristos   tid = get_ptrace_pid (regcache->ptid ());
173*6881a400Schristos 
174*6881a400Schristos   if ((regnum >= OR1K_ZERO_REGNUM && regnum < OR1K_MAX_GPR_REGS)
175*6881a400Schristos       || (regnum == OR1K_NPC_REGNUM)
176*6881a400Schristos       || (regnum == -1))
177*6881a400Schristos     {
178*6881a400Schristos       struct iovec iov;
179*6881a400Schristos       elf_gregset_t regs;
180*6881a400Schristos 
181*6881a400Schristos       iov.iov_base = &regs;
182*6881a400Schristos       iov.iov_len = sizeof (regs);
183*6881a400Schristos 
184*6881a400Schristos       if (ptrace (PTRACE_GETREGSET, tid, NT_PRSTATUS,
185*6881a400Schristos 		  (PTRACE_TYPE_ARG3) &iov) == -1)
186*6881a400Schristos 	perror_with_name (_("Couldn't get registers"));
187*6881a400Schristos       else
188*6881a400Schristos 	{
189*6881a400Schristos 	  fill_gregset (regcache, &regs, regnum);
190*6881a400Schristos 
191*6881a400Schristos 	  if (ptrace (PTRACE_SETREGSET, tid, NT_PRSTATUS,
192*6881a400Schristos 		      (PTRACE_TYPE_ARG3) &iov) == -1)
193*6881a400Schristos 	    perror_with_name (_("Couldn't set registers"));
194*6881a400Schristos 	}
195*6881a400Schristos     }
196*6881a400Schristos 
197*6881a400Schristos   /* Access to SPRs has potential security issues, don't support them for
198*6881a400Schristos      now.  */
199*6881a400Schristos }
200*6881a400Schristos 
201*6881a400Schristos /* Initialize OpenRISC Linux native support.  */
202*6881a400Schristos 
203*6881a400Schristos void _initialize_or1k_linux_nat ();
204*6881a400Schristos void
205*6881a400Schristos _initialize_or1k_linux_nat ()
206*6881a400Schristos {
207*6881a400Schristos   /* Register the target.  */
208*6881a400Schristos   linux_target = &the_or1k_linux_nat_target;
209*6881a400Schristos   add_inf_child_target (&the_or1k_linux_nat_target);
210*6881a400Schristos }
211