Add the target_ops needed for software breakpoints in GDBServer.
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-nios2-low.c
CommitLineData
68f5f838
SL
1/* GNU/Linux/Nios II specific low level interface, for the remote server for
2 GDB.
32d0add0 3 Copyright (C) 2008-2015 Free Software Foundation, Inc.
68f5f838
SL
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"
21e94bd9 24#include "elf/common.h"
5826e159 25#include "nat/gdb_ptrace.h"
68f5f838
SL
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
21e94bd9 36 (sysdeps/unix/sysv/linux/nios2/sys/user.h), and also with
68f5f838
SL
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
43void init_registers_nios2_linux (void);
3aee8918 44extern const struct target_desc *tdesc_nios2_linux;
68f5f838
SL
45
46/* This union is used to convert between int and byte buffer
47 representations of register contents. */
48
49union nios2_register
50{
51 unsigned char buf[4];
52 int reg32;
53};
54
55/* Return the ptrace ``address'' of register REGNO. */
56
57static 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
70static void
71nios2_arch_setup (void)
72{
3aee8918 73 current_process ()->tdesc = tdesc_nios2_linux;
68f5f838
SL
74}
75
76/* Implement the cannot_fetch_register linux_target_ops method. */
77
78static int
79nios2_cannot_fetch_register (int regno)
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
89static int
90nios2_cannot_store_register (int regno)
91{
92 if (nios2_regmap[regno] == -1)
93 return 1;
94
95 return 0;
96}
97
98/* Implement the get_pc linux_target_ops method. */
99
100static CORE_ADDR
101nios2_get_pc (struct regcache *regcache)
102{
103 union nios2_register pc;
104
105 collect_register_by_name (regcache, "pc", pc.buf);
106 return pc.reg32;
107}
108
109/* Implement the set_pc linux_target_ops method. */
110
111static void
112nios2_set_pc (struct regcache *regcache, CORE_ADDR pc)
113{
114 union nios2_register newpc;
115
116 newpc.reg32 = pc;
117 supply_register_by_name (regcache, "pc", newpc.buf);
118}
119
af60a1ef
SL
120/* Breakpoint support. Also see comments on nios2_breakpoint_from_pc
121 in nios2-tdep.c. */
122
123#if defined(__nios2_arch__) && __nios2_arch__ == 2
124#define NIOS2_BREAKPOINT 0xb7fd0020
125#define CDX_BREAKPOINT 0xd7c9
126#else
127#define NIOS2_BREAKPOINT 0x003b6ffa
128#endif
68f5f838 129
dd373349
AT
130/* We only register the 4-byte breakpoint, even on R2 targets which also
131 support 2-byte breakpoints. Since there is no supports_z_point_type
132 function provided, gdbserver never inserts software breakpoints itself
133 and instead relies on GDB to insert the breakpoint of the correct length
134 via a memory write. */
af60a1ef 135static const unsigned int nios2_breakpoint = NIOS2_BREAKPOINT;
68f5f838
SL
136#define nios2_breakpoint_len 4
137
dd373349
AT
138/* Implementation of linux_target_ops method "sw_breakpoint_from_kind". */
139
140static const gdb_byte *
141nios2_sw_breakpoint_from_kind (int kind, int *size)
142{
143 *size = nios2_breakpoint_len;
144 return (const gdb_byte *) &nios2_breakpoint;
145}
146
68f5f838
SL
147/* Implement the breakpoint_reinsert_addr linux_target_ops method. */
148
149static CORE_ADDR
150nios2_reinsert_addr (void)
151{
152 union nios2_register ra;
0bfdf32f 153 struct regcache *regcache = get_thread_regcache (current_thread, 1);
68f5f838
SL
154
155 collect_register_by_name (regcache, "ra", ra.buf);
156 return ra.reg32;
157}
158
159/* Implement the breakpoint_at linux_target_ops method. */
160
161static int
162nios2_breakpoint_at (CORE_ADDR where)
163{
164 unsigned int insn;
165
af60a1ef
SL
166 /* For R2, first check for the 2-byte CDX trap.n breakpoint encoding. */
167#if defined(__nios2_arch__) && __nios2_arch__ == 2
168 (*the_target->read_memory) (where, (unsigned char *) &insn, 2);
169 if (insn == CDX_BREAKPOINT)
170 return 1;
171#endif
172
68f5f838
SL
173 (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
174 if (insn == nios2_breakpoint)
175 return 1;
176 return 0;
177}
178
179/* Fetch the thread-local storage pointer for libthread_db. */
180
181ps_err_e
182ps_get_thread_area (const struct ps_prochandle *ph,
183 lwpid_t lwpid, int idx, void **base)
184{
185 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
186 return PS_ERR;
187
188 /* IDX is the bias from the thread pointer to the beginning of the
189 thread descriptor. It has to be subtracted due to implementation
190 quirks in libthread_db. */
191 *base = (void *) ((char *) *base - idx);
192
193 return PS_OK;
194}
195
68f5f838
SL
196/* Helper functions to collect/supply a single register REGNO. */
197
198static void
199nios2_collect_register (struct regcache *regcache, int regno,
200 union nios2_register *reg)
201{
202 union nios2_register tmp_reg;
203
204 collect_register (regcache, regno, &tmp_reg.reg32);
205 reg->reg32 = tmp_reg.reg32;
206}
207
208static void
209nios2_supply_register (struct regcache *regcache, int regno,
210 const union nios2_register *reg)
211{
212 supply_register (regcache, regno, reg->buf);
213}
214
215/* We have only a single register set on Nios II. */
216
217static void
218nios2_fill_gregset (struct regcache *regcache, void *buf)
219{
220 union nios2_register *regset = buf;
221 int i;
222
223 for (i = 1; i < nios2_num_regs; i++)
224 nios2_collect_register (regcache, i, regset + i);
225}
226
227static void
228nios2_store_gregset (struct regcache *regcache, const void *buf)
229{
230 const union nios2_register *regset = buf;
231 int i;
232
233 for (i = 0; i < nios2_num_regs; i++)
234 nios2_supply_register (regcache, i, regset + i);
235}
68f5f838 236
3aee8918 237static struct regset_info nios2_regsets[] =
68f5f838 238{
21e94bd9
SL
239 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS,
240 nios2_num_regs * 4, GENERAL_REGS,
68f5f838 241 nios2_fill_gregset, nios2_store_gregset },
68f5f838
SL
242 { 0, 0, 0, -1, -1, NULL, NULL }
243};
244
3aee8918
PA
245static struct regsets_info nios2_regsets_info =
246 {
247 nios2_regsets, /* regsets */
248 0, /* num_regsets */
249 NULL, /* disabled_regsets */
250 };
251
252static struct usrregs_info nios2_usrregs_info =
253 {
254 nios2_num_regs,
255 nios2_regmap,
256 };
257
258static struct regs_info regs_info =
259 {
260 NULL, /* regset_bitmap */
261 &nios2_usrregs_info,
262 &nios2_regsets_info
263 };
264
265static const struct regs_info *
266nios2_regs_info (void)
267{
268 return &regs_info;
269}
270
68f5f838
SL
271struct linux_target_ops the_low_target =
272{
273 nios2_arch_setup,
3aee8918 274 nios2_regs_info,
68f5f838
SL
275 nios2_cannot_fetch_register,
276 nios2_cannot_store_register,
277 NULL,
278 nios2_get_pc,
279 nios2_set_pc,
dd373349
AT
280 NULL, /* breakpoint_kind_from_pc */
281 nios2_sw_breakpoint_from_kind,
68f5f838
SL
282 nios2_reinsert_addr,
283 0,
284 nios2_breakpoint_at,
285};
3aee8918
PA
286
287void
288initialize_low_arch (void)
289{
290 init_registers_nios2_linux ();
291
292 initialize_regsets_info (&nios2_regsets_info);
293}
This page took 0.242735 seconds and 4 git commands to generate.