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