479b65b26b0064ba8663d39ff9054c4a13ecc946
[platform/upstream/gdb.git] / gdb / gdbserver / linux-xtensa-low.c
1 /* GNU/Linux/Xtensa specific low level interface, for the remote server for GDB.
2    Copyright (C) 2007-2014 Free Software Foundation, Inc.
3
4    This file is part of GDB.
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18
19
20 #include "server.h"
21 #include "linux-low.h"
22
23 /* Defined in auto-generated file reg-xtensa.c.  */
24 void init_registers_xtensa (void);
25 extern const struct target_desc *tdesc_xtensa;
26
27 #include <asm/ptrace.h>
28 #include <xtensa-config.h>
29
30 #include "xtensa-xtregs.c"
31
32 enum regnum {
33         R_PC=0, R_PS,
34         R_LBEG, R_LEND, R_LCOUNT,
35         R_SAR,
36         R_WS, R_WB,
37         R_A0 = 64
38 };
39
40 static void
41 xtensa_fill_gregset (struct regcache *regcache, void *buf)
42 {
43   elf_greg_t* rset = (elf_greg_t*)buf;
44   const struct target_desc *tdesc = regcache->tdesc;
45   int ar0_regnum;
46   char *ptr;
47   int i;
48
49   /* Take care of AR registers.  */
50
51   ar0_regnum = find_regno (tdesc, "ar0");
52   ptr = (char*)&rset[R_A0];
53
54   for (i = ar0_regnum; i < ar0_regnum + XCHAL_NUM_AREGS; i++)
55     {
56       collect_register (regcache, i, ptr);
57       ptr += register_size (tdesc, i);
58     }
59
60   /* Loop registers, if hardware has it.  */
61
62 #if XCHAL_HAVE_LOOP
63   collect_register_by_name (regcache, "lbeg", (char*)&rset[R_LBEG]);
64   collect_register_by_name (regcache, "lend", (char*)&rset[R_LEND]);
65   collect_register_by_name (regcache, "lcount", (char*)&rset[R_LCOUNT]);
66 #endif
67
68   collect_register_by_name (regcache, "sar", (char*)&rset[R_SAR]);
69   collect_register_by_name (regcache, "pc", (char*)&rset[R_PC]);
70   collect_register_by_name (regcache, "ps", (char*)&rset[R_PS]);
71   collect_register_by_name (regcache, "windowbase", (char*)&rset[R_WB]);
72   collect_register_by_name (regcache, "windowstart", (char*)&rset[R_WS]);
73 }
74
75 static void
76 xtensa_store_gregset (struct regcache *regcache, const void *buf)
77 {
78   const elf_greg_t* rset = (const elf_greg_t*)buf;
79   const struct target_desc *tdesc = regcache->tdesc;
80   int ar0_regnum;
81   char *ptr;
82   int i;
83
84   /* Take care of AR registers.  */
85
86   ar0_regnum = find_regno (tdesc, "ar0");
87   ptr = (char *)&rset[R_A0];
88
89   for (i = ar0_regnum; i < ar0_regnum + XCHAL_NUM_AREGS; i++)
90     {
91       supply_register (regcache, i, ptr);
92       ptr += register_size (tdesc, i);
93     }
94
95   /* Loop registers, if hardware has it.  */
96
97 #if XCHAL_HAVE_LOOP
98   supply_register_by_name (regcache, "lbeg", (char*)&rset[R_LBEG]);
99   supply_register_by_name (regcache, "lend", (char*)&rset[R_LEND]);
100   supply_register_by_name (regcache, "lcount", (char*)&rset[R_LCOUNT]);
101 #endif
102
103   supply_register_by_name (regcache, "sar", (char*)&rset[R_SAR]);
104   supply_register_by_name (regcache, "pc", (char*)&rset[R_PC]);
105   supply_register_by_name (regcache, "ps", (char*)&rset[R_PS]);
106   supply_register_by_name (regcache, "windowbase", (char*)&rset[R_WB]);
107   supply_register_by_name (regcache, "windowstart", (char*)&rset[R_WS]);
108 }
109
110 /* Xtensa GNU/Linux PTRACE interface includes extended register set.  */
111
112 static void
113 xtensa_fill_xtregset (struct regcache *regcache, void *buf)
114 {
115   const xtensa_regtable_t *ptr;
116
117   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
118     {
119       collect_register_by_name (regcache, ptr->name,
120                                 (char*)buf + ptr->ptrace_offset);
121     }
122 }
123
124 static void
125 xtensa_store_xtregset (struct regcache *regcache, const void *buf)
126 {
127   const xtensa_regtable_t *ptr;
128
129   for (ptr = xtensa_regmap_table; ptr->name; ptr++)
130     {
131       supply_register_by_name (regcache, ptr->name,
132                                 (char*)buf + ptr->ptrace_offset);
133     }
134 }
135
136 static struct regset_info xtensa_regsets[] = {
137   { PTRACE_GETREGS, PTRACE_SETREGS, 0, sizeof (elf_gregset_t),
138     GENERAL_REGS,
139     xtensa_fill_gregset, xtensa_store_gregset },
140   { PTRACE_GETXTREGS, PTRACE_SETXTREGS, 0, XTENSA_ELF_XTREG_SIZE,
141     EXTENDED_REGS,
142     xtensa_fill_xtregset, xtensa_store_xtregset },
143   { 0, 0, 0, -1, -1, NULL, NULL }
144 };
145
146 #if XCHAL_HAVE_BE
147 #define XTENSA_BREAKPOINT {0xd2,0x0f}
148 #else
149 #define XTENSA_BREAKPOINT {0x2d,0xf0}
150 #endif
151
152 static const unsigned char xtensa_breakpoint[] = XTENSA_BREAKPOINT;
153 #define xtensa_breakpoint_len 2
154
155 static CORE_ADDR
156 xtensa_get_pc (struct regcache *regcache)
157 {
158   unsigned long pc;
159
160   collect_register_by_name (regcache, "pc", &pc);
161   return pc;
162 }
163
164 static void
165 xtensa_set_pc (struct regcache *regcache, CORE_ADDR pc)
166 {
167   unsigned long newpc = pc;
168   supply_register_by_name (regcache, "pc", &newpc);
169 }
170
171 static int
172 xtensa_breakpoint_at (CORE_ADDR where)
173 {
174     unsigned long insn;
175
176     (*the_target->read_memory) (where, (unsigned char *) &insn,
177                                 xtensa_breakpoint_len);
178     return memcmp((char *) &insn,
179                   xtensa_breakpoint, xtensa_breakpoint_len) == 0;
180 }
181
182 static struct regsets_info xtensa_regsets_info =
183   {
184     xtensa_regsets, /* regsets */
185     0, /* num_regsets */
186     NULL, /* disabled_regsets */
187   };
188
189 static struct usrregs_info xtensa_usrregs_info =
190   {
191     xtensa_num_regs,
192     xtensa_regmap,
193   };
194
195 static struct regs_info regs_info =
196   {
197     NULL, /* regset_bitmap */
198     &xtensa_usrregs_info,
199     &xtensa_regsets_info
200   };
201
202 static void
203 xtensa_arch_setup (void)
204 {
205   current_process ()->tdesc = tdesc_xtensa;
206 }
207
208 static const struct regs_info *
209 xtensa_regs_info (void)
210 {
211   return &regs_info;
212 }
213
214 struct linux_target_ops the_low_target = {
215   xtensa_arch_setup,
216   xtensa_regs_info,
217   0,
218   0,
219   NULL, /* fetch_register */
220   xtensa_get_pc,
221   xtensa_set_pc,
222   xtensa_breakpoint,
223   xtensa_breakpoint_len,
224   NULL,
225   0,
226   xtensa_breakpoint_at,
227 };
228
229
230 void
231 initialize_low_arch (void)
232 {
233   /* Initialize the Linux target descriptions.  */
234   init_registers_xtensa ();
235
236   initialize_regsets_info (&xtensa_regsets_info);
237 }