1 /* Native-dependent code for GNU/Linux OpenRISC.
2 Copyright (C) 2021-2024 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/>. */
21 #include "linux-nat.h"
22 #include "or1k-tdep.h"
23 #include "or1k-linux-tdep.h"
26 #include "elf/common.h"
28 #include <sys/ptrace.h>
30 /* OpenRISC Linux native additions to the default linux support. */
32 class or1k_linux_nat_target final
: public linux_nat_target
35 /* Add our register access methods. */
36 void fetch_registers (struct regcache
*regcache
, int regnum
) override
;
37 void store_registers (struct regcache
*regcache
, int regnum
) override
;
39 /* Read suitable target description. */
40 const struct target_desc
*read_description () override
;
43 static or1k_linux_nat_target the_or1k_linux_nat_target
;
45 /* Copy general purpose register REGNUM (or all gp regs if REGNUM == -1)
46 from regset GREGS into REGCACHE. */
49 supply_gregset_regnum (struct regcache
*regcache
, const prgregset_t
*gregs
,
53 const elf_greg_t
*regp
= *gregs
;
55 /* Access all registers */
58 /* We fill the general purpose registers. */
59 for (i
= OR1K_ZERO_REGNUM
+ 1; i
< OR1K_MAX_GPR_REGS
; i
++)
60 regcache
->raw_supply (i
, regp
+ i
);
62 /* Supply OR1K_NPC_REGNUM from index 32. */
63 regcache
->raw_supply (OR1K_NPC_REGNUM
, regp
+ 32);
65 /* Fill the inaccessible zero register with zero. */
66 regcache
->raw_supply_zeroed (0);
68 else if (regnum
== OR1K_ZERO_REGNUM
)
69 regcache
->raw_supply_zeroed (0);
70 else if (regnum
== OR1K_NPC_REGNUM
)
71 regcache
->raw_supply (OR1K_NPC_REGNUM
, regp
+ 32);
72 else if (regnum
> OR1K_ZERO_REGNUM
&& regnum
< OR1K_MAX_GPR_REGS
)
73 regcache
->raw_supply (regnum
, regp
+ regnum
);
76 /* Copy all general purpose registers from regset GREGS into REGCACHE. */
79 supply_gregset (struct regcache
*regcache
, const prgregset_t
*gregs
)
81 supply_gregset_regnum (regcache
, gregs
, -1);
84 /* Copy general purpose register REGNUM (or all gp regs if REGNUM == -1)
85 from REGCACHE into regset GREGS. */
88 fill_gregset (const struct regcache
*regcache
, prgregset_t
*gregs
, int regnum
)
90 elf_greg_t
*regp
= *gregs
;
94 /* We fill the general purpose registers. */
95 for (int i
= OR1K_ZERO_REGNUM
+ 1; i
< OR1K_MAX_GPR_REGS
; i
++)
96 regcache
->raw_collect (i
, regp
+ i
);
98 regcache
->raw_collect (OR1K_NPC_REGNUM
, regp
+ 32);
100 else if (regnum
== OR1K_ZERO_REGNUM
)
101 /* Nothing to do here. */
103 else if (regnum
> OR1K_ZERO_REGNUM
&& regnum
< OR1K_MAX_GPR_REGS
)
104 regcache
->raw_collect (regnum
, regp
+ regnum
);
105 else if (regnum
== OR1K_NPC_REGNUM
)
106 regcache
->raw_collect (OR1K_NPC_REGNUM
, regp
+ 32);
109 /* Transfering floating-point registers between GDB, inferiors and cores.
110 Since OpenRISC floating-point registers are the same as GPRs these do
114 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregs
)
119 fill_fpregset (const struct regcache
*regcache
,
120 gdb_fpregset_t
*fpregs
, int regno
)
124 /* Return a target description for the current target. */
126 const struct target_desc
*
127 or1k_linux_nat_target::read_description ()
129 return tdesc_or1k_linux
;
132 /* Fetch REGNUM (or all registers if REGNUM == -1) from the target
133 into REGCACHE using PTRACE_GETREGSET. */
136 or1k_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regnum
)
140 tid
= get_ptrace_pid (regcache
->ptid());
142 if ((regnum
>= OR1K_ZERO_REGNUM
&& regnum
< OR1K_MAX_GPR_REGS
)
143 || (regnum
== OR1K_NPC_REGNUM
)
149 iov
.iov_base
= ®s
;
150 iov
.iov_len
= sizeof (regs
);
152 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
,
153 (PTRACE_TYPE_ARG3
) &iov
) == -1)
154 perror_with_name (_("Couldn't get registers"));
156 supply_gregset_regnum (regcache
, ®s
, regnum
);
159 /* Access to other SPRs has potential security issues, don't support them for
163 /* Store REGNUM (or all registers if REGNUM == -1) to the target
164 from REGCACHE using PTRACE_SETREGSET. */
167 or1k_linux_nat_target::store_registers (struct regcache
*regcache
, int regnum
)
171 tid
= get_ptrace_pid (regcache
->ptid ());
173 if ((regnum
>= OR1K_ZERO_REGNUM
&& regnum
< OR1K_MAX_GPR_REGS
)
174 || (regnum
== OR1K_NPC_REGNUM
)
180 iov
.iov_base
= ®s
;
181 iov
.iov_len
= sizeof (regs
);
183 if (ptrace (PTRACE_GETREGSET
, tid
, NT_PRSTATUS
,
184 (PTRACE_TYPE_ARG3
) &iov
) == -1)
185 perror_with_name (_("Couldn't get registers"));
188 fill_gregset (regcache
, ®s
, regnum
);
190 if (ptrace (PTRACE_SETREGSET
, tid
, NT_PRSTATUS
,
191 (PTRACE_TYPE_ARG3
) &iov
) == -1)
192 perror_with_name (_("Couldn't set registers"));
196 /* Access to SPRs has potential security issues, don't support them for
200 /* Initialize OpenRISC Linux native support. */
202 void _initialize_or1k_linux_nat ();
204 _initialize_or1k_linux_nat ()
206 /* Register the target. */
207 linux_target
= &the_or1k_linux_nat_target
;
208 add_inf_child_target (&the_or1k_linux_nat_target
);