2013-05-06 Sandra Loosemore <sandra@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / gdbserver / linux-nios2-low.c
1 /* GNU/Linux/Nios II specific low level interface, for the remote server for
2 GDB.
3 Copyright (C) 2008-2013 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 <sys/ptrace.h>
25 #include <endian.h>
26 #include "gdb_proc_service.h"
27 #include <asm/ptrace.h>
28
29 #ifndef PTRACE_GET_THREAD_AREA
30 #define PTRACE_GET_THREAD_AREA 25
31 #endif
32
33 /* The following definition must agree with the number of registers
34 defined in "struct user_regs" in GLIBC
35 (ports/sysdeps/unix/sysv/linux/nios2/sys/user.h), and also with
36 NIOS2_NUM_REGS in GDB proper. */
37
38 #define nios2_num_regs 49
39
40 /* Defined in auto-generated file nios2-linux.c. */
41
42 void init_registers_nios2_linux (void);
43
44 /* This union is used to convert between int and byte buffer
45 representations of register contents. */
46
47 union nios2_register
48 {
49 unsigned char buf[4];
50 int reg32;
51 };
52
53 /* Return the ptrace ``address'' of register REGNO. */
54
55 static int nios2_regmap[] = {
56 -1, 1, 2, 3, 4, 5, 6, 7,
57 8, 9, 10, 11, 12, 13, 14, 15,
58 16, 17, 18, 19, 20, 21, 22, 23,
59 24, 25, 26, 27, 28, 29, 30, 31,
60 32, 33, 34, 35, 36, 37, 38, 39,
61 40, 41, 42, 43, 44, 45, 46, 47,
62 48,
63 0
64 };
65
66 /* Implement the arch_setup linux_target_ops method. */
67
68 static void
69 nios2_arch_setup (void)
70 {
71 init_registers_nios2_linux ();
72 }
73
74 /* Implement the cannot_fetch_register linux_target_ops method. */
75
76 static int
77 nios2_cannot_fetch_register (int regno)
78 {
79 if (nios2_regmap[regno] == -1)
80 return 1;
81
82 return 0;
83 }
84
85 /* Implement the cannot_store_register linux_target_ops method. */
86
87 static int
88 nios2_cannot_store_register (int regno)
89 {
90 if (nios2_regmap[regno] == -1)
91 return 1;
92
93 return 0;
94 }
95
96 /* Implement the get_pc linux_target_ops method. */
97
98 static CORE_ADDR
99 nios2_get_pc (struct regcache *regcache)
100 {
101 union nios2_register pc;
102
103 collect_register_by_name (regcache, "pc", pc.buf);
104 return pc.reg32;
105 }
106
107 /* Implement the set_pc linux_target_ops method. */
108
109 static void
110 nios2_set_pc (struct regcache *regcache, CORE_ADDR pc)
111 {
112 union nios2_register newpc;
113
114 newpc.reg32 = pc;
115 supply_register_by_name (regcache, "pc", newpc.buf);
116 }
117
118 /* Breakpoint support. */
119
120 static const unsigned int nios2_breakpoint = 0x003b6ffa;
121 #define nios2_breakpoint_len 4
122
123 /* Implement the breakpoint_reinsert_addr linux_target_ops method. */
124
125 static CORE_ADDR
126 nios2_reinsert_addr (void)
127 {
128 union nios2_register ra;
129 struct regcache *regcache = get_thread_regcache (current_inferior, 1);
130
131 collect_register_by_name (regcache, "ra", ra.buf);
132 return ra.reg32;
133 }
134
135 /* Implement the breakpoint_at linux_target_ops method. */
136
137 static int
138 nios2_breakpoint_at (CORE_ADDR where)
139 {
140 unsigned int insn;
141
142 (*the_target->read_memory) (where, (unsigned char *) &insn, 4);
143 if (insn == nios2_breakpoint)
144 return 1;
145 return 0;
146 }
147
148 /* Fetch the thread-local storage pointer for libthread_db. */
149
150 ps_err_e
151 ps_get_thread_area (const struct ps_prochandle *ph,
152 lwpid_t lwpid, int idx, void **base)
153 {
154 if (ptrace (PTRACE_GET_THREAD_AREA, lwpid, NULL, base) != 0)
155 return PS_ERR;
156
157 /* IDX is the bias from the thread pointer to the beginning of the
158 thread descriptor. It has to be subtracted due to implementation
159 quirks in libthread_db. */
160 *base = (void *) ((char *) *base - idx);
161
162 return PS_OK;
163 }
164
165 #ifdef HAVE_PTRACE_GETREGS
166
167 /* Helper functions to collect/supply a single register REGNO. */
168
169 static void
170 nios2_collect_register (struct regcache *regcache, int regno,
171 union nios2_register *reg)
172 {
173 union nios2_register tmp_reg;
174
175 collect_register (regcache, regno, &tmp_reg.reg32);
176 reg->reg32 = tmp_reg.reg32;
177 }
178
179 static void
180 nios2_supply_register (struct regcache *regcache, int regno,
181 const union nios2_register *reg)
182 {
183 supply_register (regcache, regno, reg->buf);
184 }
185
186 /* We have only a single register set on Nios II. */
187
188 static void
189 nios2_fill_gregset (struct regcache *regcache, void *buf)
190 {
191 union nios2_register *regset = buf;
192 int i;
193
194 for (i = 1; i < nios2_num_regs; i++)
195 nios2_collect_register (regcache, i, regset + i);
196 }
197
198 static void
199 nios2_store_gregset (struct regcache *regcache, const void *buf)
200 {
201 const union nios2_register *regset = buf;
202 int i;
203
204 for (i = 0; i < nios2_num_regs; i++)
205 nios2_supply_register (regcache, i, regset + i);
206 }
207 #endif /* HAVE_PTRACE_GETREGS */
208
209 struct regset_info target_regsets[] =
210 {
211 #ifdef HAVE_PTRACE_GETREGS
212 { PTRACE_GETREGS, PTRACE_SETREGS, 0, nios2_num_regs * 4, GENERAL_REGS,
213 nios2_fill_gregset, nios2_store_gregset },
214 #endif /* HAVE_PTRACE_GETREGS */
215 { 0, 0, 0, -1, -1, NULL, NULL }
216 };
217
218 struct linux_target_ops the_low_target =
219 {
220 nios2_arch_setup,
221 nios2_num_regs,
222 nios2_regmap,
223 NULL,
224 nios2_cannot_fetch_register,
225 nios2_cannot_store_register,
226 NULL,
227 nios2_get_pc,
228 nios2_set_pc,
229 (const unsigned char *) &nios2_breakpoint,
230 nios2_breakpoint_len,
231 nios2_reinsert_addr,
232 0,
233 nios2_breakpoint_at,
234 };
This page took 0.035759 seconds and 5 git commands to generate.