GDBserver
linux-nios2-low.c
Go to the documentation of this file.
1 /* GNU/Linux/Nios II specific low level interface, for the remote server for
2  GDB.
3  Copyright (C) 2008-2018 Free Software Foundation, Inc.
4 
5  Contributed by Mentor Graphics, Inc.
6 
7  This file is part of GDB.
8 
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.
13 
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.
18 
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/>. */
21 
22 #include "server.h"
23 #include "linux-low.h"
24 #include "elf/common.h"
25 #include "nat/gdb_ptrace.h"
26 #include <endian.h>
27 #include "gdb_proc_service.h"
28 #include <asm/ptrace.h>
29 
30 #ifndef PTRACE_GET_THREAD_AREA
31 #define PTRACE_GET_THREAD_AREA 25
32 #endif
33 
34 /* The following definition must agree with the number of registers
35  defined in "struct user_regs" in GLIBC
36  (sysdeps/unix/sysv/linux/nios2/sys/user.h), and also with
37  NIOS2_NUM_REGS in GDB proper. */
38 
39 #define nios2_num_regs 49
40 
41 /* Defined in auto-generated file nios2-linux.c. */
42 
43 void init_registers_nios2_linux (void);
44 extern const struct target_desc *tdesc_nios2_linux;
45 
46 /* This union is used to convert between int and byte buffer
47  representations of register contents. */
48 
50 {
51  unsigned char buf[4];
52  int reg32;
53 };
54 
55 /* Return the ptrace ``address'' of register REGNO. */
56 
57 static int nios2_regmap[] = {
58  -1, 1, 2, 3, 4, 5, 6, 7,
59  8, 9, 10, 11, 12, 13, 14, 15,
60  16, 17, 18, 19, 20, 21, 22, 23,
61  24, 25, 26, 27, 28, 29, 30, 31,
62  32, 33, 34, 35, 36, 37, 38, 39,
63  40, 41, 42, 43, 44, 45, 46, 47,
64  48,
65  0
66 };
67 
68 /* Implement the arch_setup linux_target_ops method. */
69 
70 static void
72 {
74 }
75 
76 /* Implement the cannot_fetch_register linux_target_ops method. */
77 
78 static int
80 {
81  if (nios2_regmap[regno] == -1)
82  return 1;
83 
84  return 0;
85 }
86 
87 /* Implement the cannot_store_register linux_target_ops method. */
88 
89 static int
91 {
92  if (nios2_regmap[regno] == -1)
93  return 1;
94 
95  return 0;
96 }
97 
98 /* Breakpoint support. Also see comments on nios2_breakpoint_from_pc
99  in nios2-tdep.c. */
100 
101 #if defined(__nios2_arch__) && __nios2_arch__ == 2
102 #define NIOS2_BREAKPOINT 0xb7fd0020
103 #define CDX_BREAKPOINT 0xd7c9
104 #else
105 #define NIOS2_BREAKPOINT 0x003b6ffa
106 #endif
107 
108 /* We only register the 4-byte breakpoint, even on R2 targets which also
109  support 2-byte breakpoints. Since there is no supports_z_point_type
110  function provided, gdbserver never inserts software breakpoints itself
111  and instead relies on GDB to insert the breakpoint of the correct length
112  via a memory write. */
113 static const unsigned int nios2_breakpoint = NIOS2_BREAKPOINT;
114 #define nios2_breakpoint_len 4
115 
116 /* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */
117 
118 static const gdb_byte *
119 nios2_sw_breakpoint_from_kind (int kind, int *size)
120 {
121  *size = nios2_breakpoint_len;
122  return (const gdb_byte *) &nios2_breakpoint;
123 }
124 
125 /* Implement the breakpoint_at linux_target_ops method. */
126 
127 static int
129 {
130  unsigned int insn;
131 
132  /* For R2, first check for the 2-byte CDX trap.n breakpoint encoding. */
133 #if defined(__nios2_arch__) && __nios2_arch__ == 2
134  (*the_target->read_memory) (where, (unsigned char *) &insn, 2);
135  if (insn == CDX_BREAKPOINT)
136  return 1;
137 #endif
138 
139  (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
140  if (insn == nios2_breakpoint)
141  return 1;
142  return 0;
143 }
144 
145 /* Fetch the thread-local storage pointer for libthread_db. */
146 
147 ps_err_e
149  lwpid_t lwpid, int idx, void **base)
150 {
151  if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
152  return PS_ERR;
153 
154  /* IDX is the bias from the thread pointer to the beginning of the
155  thread descriptor. It has to be subtracted due to implementation
156  quirks in libthread_db. */
157  *base = (void *) ((char *) *base - idx);
158 
159  return PS_OK;
160 }
161 
162 /* Helper functions to collect/supply a single register REGNO. */
163 
164 static void
166  union nios2_register *reg)
167 {
168  union nios2_register tmp_reg;
169 
170  collect_register (regcache, regno, &tmp_reg.reg32);
171  reg->reg32 = tmp_reg.reg32;
172 }
173 
174 static void
176  const union nios2_register *reg)
177 {
178  supply_register (regcache, regno, reg->buf);
179 }
180 
181 /* We have only a single register set on Nios II. */
182 
183 static void
185 {
186  union nios2_register *regset = (union nios2_register *) buf;
187  int i;
188 
189  for (i = 1; i < nios2_num_regs; i++)
190  nios2_collect_register (regcache, i, regset + i);
191 }
192 
193 static void
194 nios2_store_gregset (struct regcache *regcache, const void *buf)
195 {
196  const union nios2_register *regset = (union nios2_register *) buf;
197  int i;
198 
199  for (i = 0; i < nios2_num_regs; i++)
200  nios2_supply_register (regcache, i, regset + i);
201 }
202 
203 static struct regset_info nios2_regsets[] =
204 {
205  { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
206  nios2_num_regs * 4, GENERAL_REGS,
208  NULL_REGSET
209 };
210 
211 static struct regsets_info nios2_regsets_info =
212  {
213  nios2_regsets, /* regsets */
214  0, /* num_regsets */
215  NULL, /* disabled_regsets */
216  };
217 
219  {
221  nios2_regmap,
222  };
223 
224 static struct regs_info regs_info =
225  {
226  NULL, /* regset_bitmap */
229  };
230 
231 static const struct regs_info *
233 {
234  return &regs_info;
235 }
236 
238 {
243  NULL,
246  NULL, /* breakpoint_kind_from_pc */
248  NULL, /* get_next_pcs */
249  0,
251 };
252 
253 void
255 {
257 
258  initialize_regsets_info (&nios2_regsets_info);
259 }
unsigned int lwpid_t
void init_registers_nios2_linux(void)
void collect_register(struct regcache *regcache, int n, void *buf)
Definition: regcache.c:402
static struct regset_info nios2_regsets[]
#define nios2_breakpoint_len
bfd_vma CORE_ADDR
Definition: common-types.h:41
#define nios2_num_regs
#define NIOS2_BREAKPOINT
static const unsigned int nios2_breakpoint
void initialize_low_arch(void)
ps_err_e
struct target_ops * the_target
Definition: target.c:24
const struct target_desc * tdesc
Definition: inferiors.h:67
PTRACE_TYPE_RET ptrace()
CORE_ADDR linux_get_pc_32bit(struct regcache *regcache)
Definition: linux-low.c:7411
static void nios2_supply_register(struct regcache *regcache, int regno, const union nios2_register *reg)
struct linux_target_ops the_low_target
unsigned char buf[4]
static int nios2_cannot_store_register(int regno)
#define PTRACE_GET_THREAD_AREA
static const gdb_byte * nios2_sw_breakpoint_from_kind(int kind, int *size)
#define PTRACE_SETREGSET
Definition: linux-ptrace.h:51
struct process_info * current_process(void)
Definition: inferiors.c:210
#define PTRACE_GETREGSET
Definition: linux-ptrace.h:47
void linux_set_pc_32bit(struct regcache *regcache, CORE_ADDR pc)
Definition: linux-low.c:7400
static void nios2_collect_register(struct regcache *regcache, int regno, union nios2_register *reg)
static int nios2_regmap[]
bfd_byte gdb_byte
Definition: common-types.h:38
ps_err_e ps_get_thread_area(struct ps_prochandle *ph, lwpid_t lwpid, int idx, void **base)
static void nios2_store_gregset(struct regcache *regcache, const void *buf)
int(* read_memory)(CORE_ADDR memaddr, unsigned char *myaddr, int len)
Definition: target.h:166
static int nios2_breakpoint_at(CORE_ADDR where)
static struct usrregs_info nios2_usrregs_info
const struct target_desc * tdesc_nios2_linux
static int nios2_cannot_fetch_register(int regno)
static void nios2_arch_setup(void)
static struct regsets_info nios2_regsets_info
void supply_register(struct regcache *regcache, int n, const void *buf)
Definition: regcache.c:318
static void nios2_fill_gregset(struct regcache *regcache, void *buf)
static struct regs_info regs_info
static const struct regs_info * nios2_regs_info(void)