1 /* Native-dependent code for GNU/Linux m32r.
3 Copyright (C) 2004-2024 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
23 #include "linux-nat.h"
25 #include "nat/gdb_ptrace.h"
27 #include <sys/procfs.h>
28 #include "inf-ptrace.h"
30 /* Prototypes for supply_gregset etc. */
33 #include "m32r-tdep.h"
36 class m32r_linux_nat_target final
: public linux_nat_target
39 /* Add our register access methods. */
40 void fetch_registers (struct regcache
*, int) override
;
41 void store_registers (struct regcache
*, int) override
;
44 static m32r_linux_nat_target the_m32r_linux_nat_target
;
46 /* Since EVB register is not available for native debug, we reduce
47 the number of registers. */
48 #define M32R_LINUX_NUM_REGS (M32R_NUM_REGS - 1)
50 /* Mapping between the general-purpose registers in `struct user'
51 format and GDB's register array layout. */
52 static int regmap
[] = {
53 4, 5, 6, 7, 0, 1, 2, 8,
54 9, 10, 11, 12, 13, 24, 25, 23,
55 19, 19, 26, 23, 22, 20, 16, 15
59 #define BBPSW_REGMAP 21
63 /* Doee (??) apply to the corresponding SET requests as well. */
64 #define GETREGS_SUPPLIES(regno) (0 <= (regno) \
65 && (regno) <= M32R_LINUX_NUM_REGS)
69 /* Transferring the general-purpose registers between GDB, inferiors
72 /* Fill GDB's register array with the general-purpose register values
76 supply_gregset (struct regcache
*regcache
, const elf_gregset_t
* gregsetp
)
78 const elf_greg_t
*regp
= (const elf_greg_t
*) gregsetp
;
80 unsigned long psw
, bbpsw
;
82 psw
= *(regp
+ PSW_REGMAP
);
83 bbpsw
= *(regp
+ BBPSW_REGMAP
);
85 for (i
= 0; i
< M32R_LINUX_NUM_REGS
; i
++)
92 regval
= ((0x00c1 & bbpsw
) << 8) | ((0xc100 & psw
) >> 8);
95 regval
= ((psw
>> 8) & 1);
98 regval
= *(regp
+ regmap
[i
]);
102 if (i
!= M32R_SP_REGNUM
)
103 regcache
->raw_supply (i
, ®val
);
104 else if (psw
& 0x8000)
105 regcache
->raw_supply (i
, regp
+ SPU_REGMAP
);
107 regcache
->raw_supply (i
, regp
+ SPI_REGMAP
);
111 /* Fetch all general-purpose registers from process/thread TID and
112 store their values in GDB's register array. */
115 fetch_regs (struct regcache
*regcache
, int tid
)
119 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
120 perror_with_name (_("Couldn't get registers"));
122 supply_gregset (regcache
, (const elf_gregset_t
*) ®s
);
125 /* Fill register REGNO (if it is a general-purpose register) in
126 *GREGSETPS with the value in GDB's register array. If REGNO is -1,
127 do this for all registers. */
130 fill_gregset (const struct regcache
*regcache
,
131 elf_gregset_t
* gregsetp
, int regno
)
133 elf_greg_t
*regp
= (elf_greg_t
*) gregsetp
;
135 unsigned long psw
, bbpsw
, tmp
;
137 psw
= *(regp
+ PSW_REGMAP
);
138 bbpsw
= *(regp
+ BBPSW_REGMAP
);
140 for (i
= 0; i
< M32R_LINUX_NUM_REGS
; i
++)
142 if (regno
!= -1 && regno
!= i
)
145 if (i
== CBR_REGNUM
|| i
== PSW_REGNUM
)
148 if (i
== SPU_REGNUM
|| i
== SPI_REGNUM
)
151 if (i
!= M32R_SP_REGNUM
)
152 regcache
->raw_collect (i
, regp
+ regmap
[i
]);
153 else if (psw
& 0x8000)
154 regcache
->raw_collect (i
, regp
+ SPU_REGMAP
);
156 regcache
->raw_collect (i
, regp
+ SPI_REGMAP
);
160 /* Store all valid general-purpose registers in GDB's register array
161 into the process/thread specified by TID. */
164 store_regs (const struct regcache
*regcache
, int tid
, int regno
)
168 if (ptrace (PTRACE_GETREGS
, tid
, 0, (int) ®s
) < 0)
169 perror_with_name (_("Couldn't get registers"));
171 fill_gregset (regcache
, ®s
, regno
);
173 if (ptrace (PTRACE_SETREGS
, tid
, 0, (int) ®s
) < 0)
174 perror_with_name (_("Couldn't write registers"));
179 /* Transferring floating-point registers between GDB, inferiors and cores.
180 Since M32R has no floating-point registers, these functions do nothing. */
183 supply_fpregset (struct regcache
*regcache
, const gdb_fpregset_t
*fpregs
)
188 fill_fpregset (const struct regcache
*regcache
,
189 gdb_fpregset_t
*fpregs
, int regno
)
195 /* Transferring arbitrary registers between GDB and inferior. */
197 /* Fetch register REGNO from the child process. If REGNO is -1, do
198 this for all registers (including the floating point and SSE
202 m32r_linux_nat_target::fetch_registers (struct regcache
*regcache
, int regno
)
204 pid_t tid
= get_ptrace_pid (regcache
->ptid ());
206 /* Use the PTRACE_GETREGS request whenever possible, since it
207 transfers more registers in one system call, and we'll cache the
209 if (regno
== -1 || GETREGS_SUPPLIES (regno
))
211 fetch_regs (regcache
, tid
);
215 internal_error (_("Got request for bad register number %d."), regno
);
218 /* Store register REGNO back into the child process. If REGNO is -1,
219 do this for all registers (including the floating point and SSE
222 m32r_linux_nat_target::store_registers (struct regcache
*regcache
, int regno
)
224 pid_t tid
= get_ptrace_pid (regcache
->ptid ());
226 /* Use the PTRACE_SETREGS request whenever possible, since it
227 transfers more registers in one system call. */
228 if (regno
== -1 || GETREGS_SUPPLIES (regno
))
230 store_regs (regcache
, tid
, regno
);
234 internal_error (_("Got request to store bad register number %d."), regno
);
237 void _initialize_m32r_linux_nat ();
239 _initialize_m32r_linux_nat ()
241 /* Register the target. */
242 linux_target
= &the_m32r_linux_nat_target
;
243 add_inf_child_target (&the_m32r_linux_nat_target
);