1 /* GNU/Linux/Nios II specific low level interface, for the remote server for
3 Copyright (C) 2008-2020 Free Software Foundation, Inc.
5 Contributed by Mentor Graphics, Inc.
7 This file is part of GDB.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
23 #include "linux-low.h"
24 #include "elf/common.h"
25 #include "nat/gdb_ptrace.h"
27 #include "gdb_proc_service.h"
28 #include <asm/ptrace.h>
30 #ifndef PTRACE_GET_THREAD_AREA
31 #define PTRACE_GET_THREAD_AREA 25
34 /* Linux target op definitions for the NIOS II architecture. */
36 class nios2_target
: public linux_process_target
40 const regs_info
*get_regs_info () override
;
44 void low_arch_setup () override
;
46 bool low_cannot_fetch_register (int regno
) override
;
48 bool low_cannot_store_register (int regno
) override
;
51 /* The singleton target ops object. */
53 static nios2_target the_nios2_target
;
55 /* The following definition must agree with the number of registers
56 defined in "struct user_regs" in GLIBC
57 (sysdeps/unix/sysv/linux/nios2/sys/user.h), and also with
58 NIOS2_NUM_REGS in GDB proper. */
60 #define nios2_num_regs 49
62 /* Defined in auto-generated file nios2-linux.c. */
64 void init_registers_nios2_linux (void);
65 extern const struct target_desc
*tdesc_nios2_linux
;
67 /* This union is used to convert between int and byte buffer
68 representations of register contents. */
76 /* Return the ptrace ``address'' of register REGNO. */
78 static int nios2_regmap
[] = {
79 -1, 1, 2, 3, 4, 5, 6, 7,
80 8, 9, 10, 11, 12, 13, 14, 15,
81 16, 17, 18, 19, 20, 21, 22, 23,
82 24, 25, 26, 27, 28, 29, 30, 31,
83 32, 33, 34, 35, 36, 37, 38, 39,
84 40, 41, 42, 43, 44, 45, 46, 47,
89 /* Implement the low_arch_setup linux target ops method. */
92 nios2_target::low_arch_setup ()
94 current_process ()->tdesc
= tdesc_nios2_linux
;
97 /* Implement the low_cannot_fetch_register linux target ops method. */
100 nios2_target::low_cannot_fetch_register (int regno
)
102 return (nios2_regmap
[regno
] == -1);
105 /* Implement the low_cannot_store_register linux target ops method. */
108 nios2_target::low_cannot_store_register (int regno
)
110 return (nios2_regmap
[regno
] == -1);
113 /* Breakpoint support. Also see comments on nios2_breakpoint_from_pc
116 #if defined(__nios2_arch__) && __nios2_arch__ == 2
117 #define NIOS2_BREAKPOINT 0xb7fd0020
118 #define CDX_BREAKPOINT 0xd7c9
120 #define NIOS2_BREAKPOINT 0x003b6ffa
123 /* We only register the 4-byte breakpoint, even on R2 targets which also
124 support 2-byte breakpoints. Since there is no supports_z_point_type
125 function provided, gdbserver never inserts software breakpoints itself
126 and instead relies on GDB to insert the breakpoint of the correct length
127 via a memory write. */
128 static const unsigned int nios2_breakpoint
= NIOS2_BREAKPOINT
;
129 #define nios2_breakpoint_len 4
131 /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */
133 static const gdb_byte
*
134 nios2_sw_breakpoint_from_kind (int kind
, int *size
)
136 *size
= nios2_breakpoint_len
;
137 return (const gdb_byte
*) &nios2_breakpoint
;
140 /* Implement the breakpoint_at linux_target_ops method. */
143 nios2_breakpoint_at (CORE_ADDR where
)
147 /* For R2, first check for the 2-byte CDX trap.n breakpoint encoding. */
148 #if defined(__nios2_arch__) && __nios2_arch__ == 2
149 the_target
->read_memory (where
, (unsigned char *) &insn
, 2);
150 if (insn
== CDX_BREAKPOINT
)
154 the_target
->read_memory (where
, (unsigned char *) &insn
, 4);
155 if (insn
== nios2_breakpoint
)
160 /* Fetch the thread-local storage pointer for libthread_db. */
163 ps_get_thread_area (struct ps_prochandle
*ph
,
164 lwpid_t lwpid
, int idx
, void **base
)
166 if (ptrace (PTRACE_GET_THREAD_AREA
, lwpid
, NULL
, base
) != 0)
169 /* IDX is the bias from the thread pointer to the beginning of the
170 thread descriptor. It has to be subtracted due to implementation
171 quirks in libthread_db. */
172 *base
= (void *) ((char *) *base
- idx
);
177 /* Helper functions to collect/supply a single register REGNO. */
180 nios2_collect_register (struct regcache
*regcache
, int regno
,
181 union nios2_register
*reg
)
183 union nios2_register tmp_reg
;
185 collect_register (regcache
, regno
, &tmp_reg
.reg32
);
186 reg
->reg32
= tmp_reg
.reg32
;
190 nios2_supply_register (struct regcache
*regcache
, int regno
,
191 const union nios2_register
*reg
)
193 supply_register (regcache
, regno
, reg
->buf
);
196 /* We have only a single register set on Nios II. */
199 nios2_fill_gregset (struct regcache
*regcache
, void *buf
)
201 union nios2_register
*regset
= (union nios2_register
*) buf
;
204 for (i
= 1; i
< nios2_num_regs
; i
++)
205 nios2_collect_register (regcache
, i
, regset
+ i
);
209 nios2_store_gregset (struct regcache
*regcache
, const void *buf
)
211 const union nios2_register
*regset
= (union nios2_register
*) buf
;
214 for (i
= 0; i
< nios2_num_regs
; i
++)
215 nios2_supply_register (regcache
, i
, regset
+ i
);
218 static struct regset_info nios2_regsets
[] =
220 { PTRACE_GETREGSET
, PTRACE_SETREGSET
, NT_PRSTATUS
,
221 nios2_num_regs
* 4, GENERAL_REGS
,
222 nios2_fill_gregset
, nios2_store_gregset
},
226 static struct regsets_info nios2_regsets_info
=
228 nios2_regsets
, /* regsets */
230 NULL
, /* disabled_regsets */
233 static struct usrregs_info nios2_usrregs_info
=
239 static struct regs_info myregs_info
=
241 NULL
, /* regset_bitmap */
247 nios2_target::get_regs_info ()
252 struct linux_target_ops the_low_target
=
256 NULL
, /* breakpoint_kind_from_pc */
257 nios2_sw_breakpoint_from_kind
,
258 NULL
, /* get_next_pcs */
263 /* The linux target ops object. */
265 linux_process_target
*the_linux_target
= &the_nios2_target
;
268 initialize_low_arch (void)
270 init_registers_nios2_linux ();
272 initialize_regsets_info (&nios2_regsets_info
);