1 /* Native-dependent code for GNU/Linux OpenRISC.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
22 #include "linux-nat.h"
23 #include "or1k-tdep.h"
24 #include "or1k-linux-tdep.h"
27 #include "elf/common.h"
29 #include <sys/ptrace.h>
31 /* OpenRISC Linux native additions to the default linux support. */
33 class or1k_linux_nat_target final
: public linux_nat_target
36 /* Add our register access methods. */
37 void fetch_registers (struct regcache
*regcache
, int regnum
) override
;
38 void store_registers (struct regcache
*regcache
, int regnum
) override
;
40 /* Read suitable target description. */
41 const struct target_desc
*read_description () override
;
44 static or1k_linux_nat_target the_or1k_linux_nat_target
;
46 /* Copy general purpose register REGNUM (or all gp regs if REGNUM == -1)
47 from regset GREGS into REGCACHE. */
50 supply_gregset_regnum (struct regcache
*regcache
, const prgregset_t
*gregs
,
54 const elf_greg_t
*regp
= *gregs
;
56 /* Access all registers */
59 /* We fill the general purpose registers. */
60 for (i
= OR1K_ZERO_REGNUM
+ 1; i
< OR1K_MAX_GPR_REGS
; i
++)
61 regcache
->raw_supply (i
, regp
+ i
);
63 /* Supply OR1K_NPC_REGNUM from index 32. */
64 regcache
->raw_supply (OR1K_NPC_REGNUM
, regp
+ 32);
66 /* Fill the inaccessible zero register with zero. */
67 regcache
->raw_supply_zeroed (0);
69 else if (regnum
== OR1K_ZERO_REGNUM
)
70 regcache
->raw_supply_zeroed (0);
71 else if (regnum
== OR1K_NPC_REGNUM
)
72 regcache
->raw_supply (OR1K_NPC_REGNUM
, regp
+ 32);
73 else if (regnum
> OR1K_ZERO_REGNUM
&& regnum
< OR1K_MAX_GPR_REGS
)
74 regcache
->raw_supply (regnum
, regp
+ regnum
);
77 /* Copy all general purpose registers from regset GREGS into REGCACHE. */
80 supply_gregset (struct regcache
*regcache
, const prgregset_t
*gregs
)
82 supply_gregset_regnum (regcache
, gregs
, -1);
85 /* Copy general purpose register REGNUM (or all gp regs if REGNUM == -1)
86 from REGCACHE into regset GREGS. */
89 fill_gregset (const struct regcache
*regcache
, prgregset_t
*gregs
, int regnum
)
91 elf_greg_t
*regp
= *gregs
;
95 /* We fill the general purpose registers. */
96 for (int i
= OR1K_ZERO_REGNUM
+ 1; i
< OR1K_MAX_GPR_REGS
; i
++)
97 regcache
->raw_collect (i
, regp
+ i
);
99 regcache
->raw_collect (OR1K_NPC_REGNUM
, regp
+ 32);
101 else if (regnum
== OR1K_ZERO_REGNUM
)
102 /* Nothing to do here. */
104 else if (regnum
> OR1K_ZERO_REGNUM
&& regnum
< OR1K_MAX_GPR_REGS
)
105 regcache
->raw_collect (regnum
, regp
+ regnum
);
106 else if (regnum
== OR1K_NPC_REGNUM
)
107 regcache
->raw_collect (OR1K_NPC_REGNUM
, regp
+ 32);
110 /* Transfering floating-point registers between GDB, inferiors and cores.
111 Since OpenRISC floating-point registers are the same as GPRs these do
115 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregs
)
120 fill_fpregset (const struct regcache
*regcache
,
121 gdb_fpregset_t
*fpregs
, int regno
)
125 /* Return a target description for the current target. */
127 const struct target_desc
*
128 or1k_linux_nat_target::read_description ()
130 return tdesc_or1k_linux
;
133 /* Fetch REGNUM (or all registers if REGNUM == -1) from the target
134 into REGCACHE using PTRACE_GETREGSET. */
137 or1k_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regnum
)
141 tid
= get_ptrace_pid (regcache
->ptid());
143 if ((regnum
>= OR1K_ZERO_REGNUM
&& regnum
< OR1K_MAX_GPR_REGS
)
144 || (regnum
== OR1K_NPC_REGNUM
)
150 iov
.iov_base
= ®s
;
151 iov
.iov_len
= sizeof (regs
);
153 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
,
154 (PTRACE_TYPE_ARG3
) &iov
) == -1)
155 perror_with_name (_("Couldn't get registers"));
157 supply_gregset_regnum (regcache
, ®s
, regnum
);
160 /* Access to other SPRs has potential security issues, don't support them for
164 /* Store REGNUM (or all registers if REGNUM == -1) to the target
165 from REGCACHE using PTRACE_SETREGSET. */
168 or1k_linux_nat_target::store_registers (struct regcache
*regcache
, int regnum
)
172 tid
= get_ptrace_pid (regcache
->ptid ());
174 if ((regnum
>= OR1K_ZERO_REGNUM
&& regnum
< OR1K_MAX_GPR_REGS
)
175 || (regnum
== OR1K_NPC_REGNUM
)
181 iov
.iov_base
= ®s
;
182 iov
.iov_len
= sizeof (regs
);
184 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
,
185 (PTRACE_TYPE_ARG3
) &iov
) == -1)
186 perror_with_name (_("Couldn't get registers"));
189 fill_gregset (regcache
, ®s
, regnum
);
191 if (ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
,
192 (PTRACE_TYPE_ARG3
) &iov
) == -1)
193 perror_with_name (_("Couldn't set registers"));
197 /* Access to SPRs has potential security issues, don't support them for
201 /* Initialize OpenRISC Linux native support. */
203 void _initialize_or1k_linux_nat ();
205 _initialize_or1k_linux_nat ()
207 /* Register the target. */
208 linux_target
= &the_or1k_linux_nat_target
;
209 add_inf_child_target (&the_or1k_linux_nat_target
);