xref: /netbsd-src/external/gpl3/gdb/dist/gdbserver/linux-csky-low.cc (revision 2be465b09aca4bf6e67814eb0e0f409087138d90)
1 /* GNU/Linux/MIPS specific low level interface, for the remote server for GDB.
2    Copyright (C) 2022-2024 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 #include "tdesc.h"
20 #include "linux-low.h"
21 #include <sys/ptrace.h>
22 #include "gdb_proc_service.h"
23 #include <asm/ptrace.h>
24 #include <elf.h>
25 #include "arch/csky.h"
26 
27 /* Linux target op definitions for the CSKY architecture.  */
28 
29 class csky_target : public linux_process_target
30 {
31 public:
32 
33   const regs_info *get_regs_info () override;
34 
35   const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override;
36 
37   bool supports_z_point_type (char z_type) override;
38 
39   bool supports_hardware_single_step () override;
40 
41 protected:
42 
43   void low_arch_setup () override;
44 
45   bool low_cannot_fetch_register (int regno) override;
46 
47   bool low_cannot_store_register (int regno) override;
48 
49   CORE_ADDR low_get_pc (regcache *regcache) override;
50 
51   void low_set_pc (regcache *regcache, CORE_ADDR newpc) override;
52 
53   bool low_breakpoint_at (CORE_ADDR pc) override;
54 };
55 
56 /* The singleton target ops object.  */
57 
58 static csky_target the_csky_target;
59 
60 /* Return the ptrace "address" of register REGNO.  */
61 static int csky_regmap[] = {
62    0*4,  1*4,  2*4,  3*4,  4*4,  5*4,  6*4,  7*4,
63    8*4,  9*4, 10*4, 11*4, 12*4, 13*4, 14*4, 15*4,
64   16*4, 17*4, 18*4, 19*4, 20*4, 21*4, 22*4, 23*4,
65   24*4, 25*4, 26*4, 27*4, 28*4, 29*4, 30*4, 31*4,
66     -1,   -1,   -1,   -1, 34*4, 35*4,   -1,   -1,
67   40*4, 42*4, 44*4, 46*4, 48*4, 50*4, 52*4, 54*4, /* fr0 ~ fr15, 64bit  */
68   56*4, 58*4, 60*4, 62*4, 64*4, 66*4, 68*4, 70*4,
69   72*4, 76*4, 80*4, 84*4, 88*4, 92*4, 96*4,100*4, /* vr0 ~ vr15, 128bit  */
70  104*4,108*4,112*4,116*4,120*4,124*4,128*4,132*4,
71   33*4,  /* pc  */
72     -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
73     -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
74   32*4,   -1,   -1,   -1,   -1,   -1,   -1,   -1, /* psr  */
75     -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
76     -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
77     -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
78   73*4, 72*4, 74*4,   -1,   -1,   -1, 14*4,  /* fcr, fid, fesr, usp  */
79 };
80 
81 /* CSKY software breakpoint instruction code.  */
82 
83 /* When the kernel code version is behind v4.x,
84    illegal insn 0x1464 will be a software bkpt trigger.
85    When an illegal insn exception happens, the case
86    that insn at EPC is 0x1464 will be recognized as SIGTRAP.  */
87 static unsigned short csky_breakpoint_illegal_2_v2 = 0x1464;
88 static unsigned int csky_breakpoint_illegal_4_v2 = 0x14641464;
89 
90 bool
91 csky_target::low_cannot_fetch_register (int regno)
92 {
93   if (csky_regmap[regno] == -1)
94     return true;
95 
96   return false;
97 }
98 
99 bool
100 csky_target::low_cannot_store_register (int regno)
101 {
102   if (csky_regmap[regno] == -1)
103     return true;
104 
105   return false;
106 }
107 
108 /* Get the value of pc register.  */
109 
110 CORE_ADDR
111 csky_target::low_get_pc (struct regcache *regcache)
112 {
113   unsigned long pc;
114   collect_register_by_name (regcache, "pc", &pc);
115   return pc;
116 }
117 
118 /* Set pc register.  */
119 
120 void
121 csky_target::low_set_pc (struct regcache *regcache, CORE_ADDR pc)
122 {
123   unsigned long new_pc = pc;
124   supply_register_by_name (regcache, "pc", &new_pc);
125 }
126 
127 
128 void
129 csky_target::low_arch_setup ()
130 {
131   static const char *expedite_regs[] = { "r14", "pc", NULL };
132   target_desc_up tdesc = csky_create_target_description ();
133 
134   if (tdesc->expedite_regs.empty ())
135     {
136       init_target_desc (tdesc.get (), expedite_regs);
137       gdb_assert (!tdesc->expedite_regs.empty ());
138     }
139 
140   current_process ()->tdesc = tdesc.release ();
141 
142   return;
143 }
144 
145 /* Fetch the thread-local storage pointer for libthread_db.  */
146 
147 ps_err_e
148 ps_get_thread_area (struct ps_prochandle *ph,
149 		    lwpid_t lwpid, int idx, void **base)
150 {
151   struct pt_regs regset;
152   if (ptrace (PTRACE_GETREGSET, lwpid,
153 	      (PTRACE_TYPE_ARG3) (long) NT_PRSTATUS, &regset) != 0)
154     return PS_ERR;
155 
156   *base = (void *) regset.tls;
157 
158   /* IDX is the bias from the thread pointer to the beginning of the
159      thread descriptor.  It has to be subtracted due to implementation
160      quirks in libthread_db.  */
161   *base = (void *) ((char *)*base - idx);
162 
163   return PS_OK;
164 }
165 
166 /* Gdbserver uses PTRACE_GET/SET_RGESET.  */
167 
168 static void
169 csky_fill_pt_gregset (struct regcache *regcache, void *buf)
170 {
171   int i, base;
172   struct pt_regs *regset = (struct pt_regs *)buf;
173 
174   collect_register_by_name (regcache, "r15",  &regset->lr);
175   collect_register_by_name (regcache, "pc", &regset->pc);
176   collect_register_by_name (regcache, "psr", &regset->sr);
177   collect_register_by_name (regcache, "r14", &regset->usp);
178 
179   collect_register_by_name (regcache, "r0", &regset->a0);
180   collect_register_by_name (regcache, "r1", &regset->a1);
181   collect_register_by_name (regcache, "r2", &regset->a2);
182   collect_register_by_name (regcache, "r3", &regset->a3);
183 
184   base = find_regno (regcache->tdesc, "r4");
185 
186   for (i = 0; i < 10; i++)
187     collect_register (regcache, base + i,   &regset->regs[i]);
188 
189   base = find_regno (regcache->tdesc, "r16");
190   for (i = 0; i < 16; i++)
191     collect_register (regcache, base + i,   &regset->exregs[i]);
192 
193   collect_register_by_name (regcache, "hi", &regset->rhi);
194   collect_register_by_name (regcache, "lo", &regset->rlo);
195 }
196 
197 static void
198 csky_store_pt_gregset (struct regcache *regcache, const void *buf)
199 {
200   int i, base;
201   const struct pt_regs *regset = (const struct pt_regs *) buf;
202 
203   supply_register_by_name (regcache, "r15",  &regset->lr);
204   supply_register_by_name (regcache, "pc", &regset->pc);
205   supply_register_by_name (regcache, "psr", &regset->sr);
206   supply_register_by_name (regcache, "r14", &regset->usp);
207 
208   supply_register_by_name (regcache, "r0", &regset->a0);
209   supply_register_by_name (regcache, "r1", &regset->a1);
210   supply_register_by_name (regcache, "r2", &regset->a2);
211   supply_register_by_name (regcache, "r3", &regset->a3);
212 
213   base = find_regno (regcache->tdesc, "r4");
214 
215   for (i = 0; i < 10; i++)
216     supply_register (regcache, base + i,   &regset->regs[i]);
217 
218   base = find_regno (regcache->tdesc, "r16");
219   for (i = 0; i < 16; i++)
220     supply_register (regcache, base + i,   &regset->exregs[i]);
221 
222   supply_register_by_name (regcache, "hi", &regset->rhi);
223   supply_register_by_name (regcache, "lo", &regset->rlo);
224 }
225 
226 static void
227 csky_fill_pt_vrregset (struct regcache *regcache, void *buf)
228 {
229   int i, base;
230   struct user_fp *regset = (struct user_fp *)buf;
231 
232   base = find_regno (regcache->tdesc, "vr0");
233 
234   for (i = 0; i < 16; i++)
235     collect_register (regcache, base + i, &regset->vr[i * 4]);
236   collect_register_by_name (regcache, "fcr", &regset->fcr);
237   collect_register_by_name (regcache, "fesr", &regset->fesr);
238   collect_register_by_name (regcache, "fid", &regset->fid);
239 }
240 
241 
242 static void
243 csky_store_pt_vrregset (struct regcache *regcache, const void *buf)
244 {
245   int i, base;
246   const struct user_fp *regset = (const struct user_fp *)buf;
247 
248   base = find_regno (regcache->tdesc, "vr0");
249 
250   for (i = 0; i < 16; i++)
251     supply_register (regcache, base + i, &regset->vr[i * 4]);
252 
253   base = find_regno (regcache->tdesc, "fr0");
254 
255   for (i = 0; i < 16; i++)
256     supply_register (regcache, base + i, &regset->vr[i * 4]);
257   supply_register_by_name (regcache, "fcr", &regset->fcr);
258   supply_register_by_name (regcache, "fesr", &regset->fesr);
259   supply_register_by_name (regcache, "fid", &regset->fid);
260 }
261 
262 struct regset_info csky_regsets[] = {
263   { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS, sizeof(struct pt_regs),
264     GENERAL_REGS, csky_fill_pt_gregset, csky_store_pt_gregset},
265 
266   { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET, sizeof(struct user_fp),
267     FP_REGS, csky_fill_pt_vrregset, csky_store_pt_vrregset},
268   NULL_REGSET
269 };
270 
271 
272 static struct regsets_info csky_regsets_info =
273 {
274   csky_regsets, /* Regsets  */
275   0,            /* Num_regsets  */
276   NULL,         /* Disabled_regsets  */
277 };
278 
279 
280 static struct regs_info csky_regs_info =
281 {
282   NULL, /* FIXME: what's this  */
283   NULL, /* PEEKUSER/POKEUSR isn't supported by kernel > 4.x */
284   &csky_regsets_info
285 };
286 
287 
288 const regs_info *
289 csky_target::get_regs_info ()
290 {
291   return &csky_regs_info;
292 }
293 
294 /* Implementation of linux_target_ops method "sw_breakpoint_from_kind".  */
295 
296 const gdb_byte *
297 csky_target::sw_breakpoint_from_kind (int kind, int *size)
298 {
299   if (kind == 4)
300     {
301       *size = 4;
302       return (const gdb_byte *) &csky_breakpoint_illegal_4_v2;
303     }
304   else
305     {
306       *size = 2;
307       return (const gdb_byte *) &csky_breakpoint_illegal_2_v2;
308     }
309 }
310 
311 bool
312 csky_target::supports_z_point_type (char z_type)
313 {
314   /* FIXME: hardware breakpoint support ??  */
315   if (z_type == Z_PACKET_SW_BP)
316     return true;
317 
318   return false;
319 }
320 
321 bool
322 csky_target::low_breakpoint_at (CORE_ADDR where)
323 {
324   unsigned int insn;
325 
326   /* Here just read 2 bytes, as csky_breakpoint_illegal_4_v2
327      is double of csky_breakpoint_illegal_2_v2, csky_breakpoint_bkpt_4
328      is double of csky_breakpoint_bkpt_2. Others are 2 bytes bkpt.  */
329   read_memory (where, (unsigned char *) &insn, 2);
330 
331   if (insn == csky_breakpoint_illegal_2_v2)
332     return true;
333 
334   return false;
335 }
336 
337 /* Support for hardware single step.  */
338 
339 bool
340 csky_target::supports_hardware_single_step ()
341 {
342   return true;
343 }
344 
345 /* The linux target ops object.  */
346 
347 linux_process_target *the_linux_target = &the_csky_target;
348 
349 void
350 initialize_low_arch (void)
351 {
352   initialize_regsets_info (&csky_regsets_info);
353 }
354