xref: /netbsd-src/external/gpl3/gdb/dist/gdb/riscv-netbsd-nat.c (revision 26ff2d23d52ec7cccb99764698504f64357c54d3)
1f5a9565aSchristos /* Native-dependent code for NetBSD/riscv.
2f5a9565aSchristos 
3f5a9565aSchristos    Copyright (C) 2018-2020 Free Software Foundation, Inc.
4f5a9565aSchristos 
5f5a9565aSchristos    This file is part of GDB.
6f5a9565aSchristos 
7f5a9565aSchristos    This program is free software; you can redistribute it and/or modify
8f5a9565aSchristos    it under the terms of the GNU General Public License as published by
9f5a9565aSchristos    the Free Software Foundation; either version 3 of the License, or
10f5a9565aSchristos    (at your option) any later version.
11f5a9565aSchristos 
12f5a9565aSchristos    This program is distributed in the hope that it will be useful,
13f5a9565aSchristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
14f5a9565aSchristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15f5a9565aSchristos    GNU General Public License for more details.
16f5a9565aSchristos 
17f5a9565aSchristos    You should have received a copy of the GNU General Public License
18f5a9565aSchristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
19f5a9565aSchristos 
20f5a9565aSchristos #include "defs.h"
21f5a9565aSchristos #include "regcache.h"
22f5a9565aSchristos #include "target.h"
23f5a9565aSchristos 
24f5a9565aSchristos #include <sys/types.h>
25f5a9565aSchristos #include <sys/ptrace.h>
26f5a9565aSchristos #include <machine/reg.h>
27f5a9565aSchristos 
28f5a9565aSchristos #include "netbsd-nat.h"
29f5a9565aSchristos #include "riscv-tdep.h"
30f5a9565aSchristos #include "riscv-netbsd-tdep.h"
31f5a9565aSchristos #include "inf-ptrace.h"
32f5a9565aSchristos 
33f5a9565aSchristos struct riscv_nbsd_nat_target final : public nbsd_nat_target
34f5a9565aSchristos {
35f5a9565aSchristos   void fetch_registers (struct regcache *, int) override;
36f5a9565aSchristos   void store_registers (struct regcache *, int) override;
37f5a9565aSchristos };
38f5a9565aSchristos 
39f5a9565aSchristos static riscv_nbsd_nat_target the_riscv_nbsd_nat_target;
40f5a9565aSchristos 
41f5a9565aSchristos /* Determine if PT_GETREGS fetches REGNUM.  */
42f5a9565aSchristos 
43f5a9565aSchristos static bool
getregs_supplies(int regnum)44f5a9565aSchristos getregs_supplies (int regnum)
45f5a9565aSchristos {
46f5a9565aSchristos   return regnum >= RISCV_RA_REGNUM && regnum <= RISCV_PC_REGNUM;
47f5a9565aSchristos }
48f5a9565aSchristos 
49f5a9565aSchristos /* Determine if PT_GETFPREGS fetches REGNUM.  */
50f5a9565aSchristos 
51f5a9565aSchristos static bool
getfpregs_supplies(int regnum)52f5a9565aSchristos getfpregs_supplies (int regnum)
53f5a9565aSchristos {
54f5a9565aSchristos   return ((regnum >= RISCV_FIRST_FP_REGNUM && regnum <= RISCV_LAST_FP_REGNUM)
55f5a9565aSchristos 	  || regnum == RISCV_CSR_FCSR_REGNUM);
56f5a9565aSchristos }
57f5a9565aSchristos 
58f5a9565aSchristos /* Fetch register REGNUM from the inferior.  If REGNUM is -1, do this
59f5a9565aSchristos    for all registers.  */
60f5a9565aSchristos 
61f5a9565aSchristos void
fetch_registers(struct regcache * regcache,int regnum)62f5a9565aSchristos riscv_nbsd_nat_target::fetch_registers (struct regcache *regcache,
63f5a9565aSchristos 					int regnum)
64f5a9565aSchristos {
65f5a9565aSchristos   pid_t pid = regcache->ptid ().pid ();
66f5a9565aSchristos   int lwp = regcache->ptid ().lwp ();
67f5a9565aSchristos 
68f5a9565aSchristos   if (regnum == -1 || regnum == RISCV_ZERO_REGNUM)
69f5a9565aSchristos     regcache->raw_supply_zeroed (RISCV_ZERO_REGNUM);
70f5a9565aSchristos   if (regnum == -1 || getregs_supplies (regnum))
71f5a9565aSchristos     {
72f5a9565aSchristos       struct reg regs;
73f5a9565aSchristos 
74*26ff2d23Srin       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
75f5a9565aSchristos 	perror_with_name (_("Couldn't get registers"));
76f5a9565aSchristos 
77f5a9565aSchristos       regcache->supply_regset (&riscv_nbsd_gregset, regnum, &regs,
78f5a9565aSchristos 			       sizeof (regs));
79f5a9565aSchristos     }
80f5a9565aSchristos 
81f5a9565aSchristos   if (regnum == -1 || getfpregs_supplies (regnum))
82f5a9565aSchristos     {
83f5a9565aSchristos       struct fpreg fpregs;
84f5a9565aSchristos 
85f5a9565aSchristos       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, 0) == -1)
86f5a9565aSchristos 	perror_with_name (_("Couldn't get floating point status"));
87f5a9565aSchristos 
88f5a9565aSchristos       regcache->supply_regset (&riscv_nbsd_fpregset, regnum, &fpregs,
89f5a9565aSchristos 			       sizeof (fpregs));
90f5a9565aSchristos     }
91f5a9565aSchristos }
92f5a9565aSchristos 
93f5a9565aSchristos /* Store register REGNUM back into the inferior.  If REGNUM is -1, do
94f5a9565aSchristos    this for all registers.  */
95f5a9565aSchristos 
96f5a9565aSchristos void
store_registers(struct regcache * regcache,int regnum)97f5a9565aSchristos riscv_nbsd_nat_target::store_registers (struct regcache *regcache,
98f5a9565aSchristos 					int regnum)
99f5a9565aSchristos {
100f5a9565aSchristos   pid_t pid = regcache->ptid ().pid ();
101f5a9565aSchristos   int lwp = regcache->ptid ().lwp ();
102f5a9565aSchristos 
103f5a9565aSchristos   if (regnum == -1 || getregs_supplies (regnum))
104f5a9565aSchristos     {
105f5a9565aSchristos       struct reg regs;
106f5a9565aSchristos 
107*26ff2d23Srin       if (ptrace (PT_GETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
108f5a9565aSchristos 	perror_with_name (_("Couldn't get registers"));
109f5a9565aSchristos 
110f5a9565aSchristos       regcache->collect_regset (&riscv_nbsd_gregset, regnum, &regs,
111f5a9565aSchristos 			       sizeof (regs));
112f5a9565aSchristos 
113*26ff2d23Srin       if (ptrace (PT_SETREGS, pid, (PTRACE_TYPE_ARG3) &regs, lwp) == -1)
114f5a9565aSchristos 	perror_with_name (_("Couldn't write registers"));
115f5a9565aSchristos     }
116f5a9565aSchristos 
117f5a9565aSchristos   if (regnum == -1 || getfpregs_supplies (regnum))
118f5a9565aSchristos     {
119f5a9565aSchristos       struct fpreg fpregs;
120f5a9565aSchristos 
121*26ff2d23Srin       if (ptrace (PT_GETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
122f5a9565aSchristos 	perror_with_name (_("Couldn't get floating point status"));
123f5a9565aSchristos 
124f5a9565aSchristos       regcache->collect_regset (&riscv_nbsd_fpregset, regnum, &fpregs,
125f5a9565aSchristos 				sizeof (fpregs));
126f5a9565aSchristos 
127*26ff2d23Srin       if (ptrace (PT_SETFPREGS, pid, (PTRACE_TYPE_ARG3) &fpregs, lwp) == -1)
128f5a9565aSchristos 	perror_with_name (_("Couldn't write floating point status"));
129f5a9565aSchristos     }
130f5a9565aSchristos }
131f5a9565aSchristos 
132f5a9565aSchristos /* Initialize RISC-V NetBSD native support.  */
133f5a9565aSchristos 
134f5a9565aSchristos void _initialize_riscv_nbsd_nat ();
135f5a9565aSchristos void
_initialize_riscv_nbsd_nat()136f5a9565aSchristos _initialize_riscv_nbsd_nat ()
137f5a9565aSchristos {
138f5a9565aSchristos   add_inf_child_target (&the_riscv_nbsd_nat_target);
139f5a9565aSchristos 
140f5a9565aSchristos //  bsd_kvm_add_target (riscv_nbsd_supply_pcb);
141f5a9565aSchristos }
142