]>
Commit | Line | Data |
---|---|---|
e5ab6af5 YT |
1 | /* GNU/Linux/LoongArch specific low level interface, for the remote server |
2 | for GDB. | |
1d506c26 | 3 | Copyright (C) 2022-2024 Free Software Foundation, Inc. |
e5ab6af5 YT |
4 | |
5 | This file is part of GDB. | |
6 | ||
7 | This program is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "server.h" | |
21 | #include "linux-low.h" | |
22 | #include "tdesc.h" | |
23 | #include "elf/common.h" | |
24 | #include "arch/loongarch.h" | |
25 | ||
26 | /* Linux target ops definitions for the LoongArch architecture. */ | |
27 | ||
28 | class loongarch_target : public linux_process_target | |
29 | { | |
30 | public: | |
31 | ||
32 | const regs_info *get_regs_info () override; | |
33 | ||
34 | int breakpoint_kind_from_pc (CORE_ADDR *pcptr) override; | |
35 | ||
36 | const gdb_byte *sw_breakpoint_from_kind (int kind, int *size) override; | |
37 | ||
38 | protected: | |
39 | ||
40 | void low_arch_setup () override; | |
41 | ||
42 | bool low_cannot_fetch_register (int regno) override; | |
43 | ||
44 | bool low_cannot_store_register (int regno) override; | |
45 | ||
46 | bool low_fetch_register (regcache *regcache, int regno) override; | |
47 | ||
48 | bool low_supports_breakpoints () override; | |
49 | ||
50 | CORE_ADDR low_get_pc (regcache *regcache) override; | |
51 | ||
52 | void low_set_pc (regcache *regcache, CORE_ADDR newpc) override; | |
53 | ||
54 | bool low_breakpoint_at (CORE_ADDR pc) override; | |
55 | }; | |
56 | ||
57 | /* The singleton target ops object. */ | |
58 | ||
59 | static loongarch_target the_loongarch_target; | |
60 | ||
61 | bool | |
62 | loongarch_target::low_cannot_fetch_register (int regno) | |
63 | { | |
64 | gdb_assert_not_reached ("linux target op low_cannot_fetch_register " | |
65 | "is not implemented by the target"); | |
66 | } | |
67 | ||
68 | bool | |
69 | loongarch_target::low_cannot_store_register (int regno) | |
70 | { | |
71 | gdb_assert_not_reached ("linux target op low_cannot_store_register " | |
72 | "is not implemented by the target"); | |
73 | } | |
74 | ||
75 | /* Implementation of linux target ops method "low_arch_setup". */ | |
76 | ||
77 | void | |
78 | loongarch_target::low_arch_setup () | |
79 | { | |
80 | static const char *expedite_regs[] = { "r3", "pc", NULL }; | |
81 | loongarch_gdbarch_features features; | |
82 | target_desc_up tdesc; | |
83 | ||
84 | features.xlen = sizeof (elf_greg_t); | |
85 | tdesc = loongarch_create_target_description (features); | |
86 | ||
dd6a9ec1 AB |
87 | if (tdesc->expedite_regs.empty ()) |
88 | { | |
89 | init_target_desc (tdesc.get (), expedite_regs); | |
90 | gdb_assert (!tdesc->expedite_regs.empty ()); | |
91 | } | |
e5ab6af5 YT |
92 | current_process ()->tdesc = tdesc.release (); |
93 | } | |
94 | ||
95 | /* Collect GPRs from REGCACHE into BUF. */ | |
96 | ||
97 | static void | |
98 | loongarch_fill_gregset (struct regcache *regcache, void *buf) | |
99 | { | |
e5ab6af5 | 100 | elf_gregset_t *regset = (elf_gregset_t *) buf; |
e5ab6af5 YT |
101 | int i; |
102 | ||
103 | for (i = 1; i < 32; i++) | |
3eba4833 | 104 | collect_register (regcache, i, *regset + i); |
74baa6cd | 105 | collect_register (regcache, LOONGARCH_ORIG_A0_REGNUM, *regset + LOONGARCH_ORIG_A0_REGNUM); |
3eba4833 YT |
106 | collect_register (regcache, LOONGARCH_PC_REGNUM, *regset + LOONGARCH_PC_REGNUM); |
107 | collect_register (regcache, LOONGARCH_BADV_REGNUM, *regset + LOONGARCH_BADV_REGNUM); | |
e5ab6af5 YT |
108 | } |
109 | ||
110 | /* Supply GPRs from BUF into REGCACHE. */ | |
111 | ||
112 | static void | |
113 | loongarch_store_gregset (struct regcache *regcache, const void *buf) | |
114 | { | |
e5ab6af5 | 115 | const elf_gregset_t *regset = (const elf_gregset_t *) buf; |
e5ab6af5 YT |
116 | int i; |
117 | ||
3eba4833 | 118 | supply_register_zeroed (regcache, 0); |
e5ab6af5 | 119 | for (i = 1; i < 32; i++) |
3eba4833 | 120 | supply_register (regcache, i, *regset + i); |
74baa6cd | 121 | supply_register (regcache, LOONGARCH_ORIG_A0_REGNUM, *regset + LOONGARCH_ORIG_A0_REGNUM); |
3eba4833 YT |
122 | supply_register (regcache, LOONGARCH_PC_REGNUM, *regset + LOONGARCH_PC_REGNUM); |
123 | supply_register (regcache, LOONGARCH_BADV_REGNUM, *regset + LOONGARCH_BADV_REGNUM); | |
e5ab6af5 YT |
124 | } |
125 | ||
3f6227c2 TY |
126 | /* Collect FPRs from REGCACHE into BUF. */ |
127 | ||
128 | static void | |
129 | loongarch_fill_fpregset (struct regcache *regcache, void *buf) | |
130 | { | |
131 | gdb_byte *regbuf = nullptr; | |
132 | int fprsize = register_size (regcache->tdesc, LOONGARCH_FIRST_FP_REGNUM); | |
ea335217 | 133 | int fccsize = register_size (regcache->tdesc, LOONGARCH_FIRST_FCC_REGNUM); |
3f6227c2 TY |
134 | |
135 | for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++) | |
136 | { | |
137 | regbuf = (gdb_byte *)buf + fprsize * i; | |
138 | collect_register (regcache, LOONGARCH_FIRST_FP_REGNUM + i, regbuf); | |
139 | } | |
ea335217 FC |
140 | |
141 | for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++) | |
142 | { | |
143 | regbuf = (gdb_byte *)buf + fprsize * LOONGARCH_LINUX_NUM_FPREGSET + | |
144 | fccsize * i; | |
145 | collect_register (regcache, LOONGARCH_FIRST_FCC_REGNUM + i, regbuf); | |
146 | } | |
147 | ||
148 | regbuf = (gdb_byte *)buf + fprsize * LOONGARCH_LINUX_NUM_FPREGSET + | |
149 | fccsize * LOONGARCH_LINUX_NUM_FCC; | |
150 | collect_register (regcache, LOONGARCH_FCSR_REGNUM, regbuf); | |
3f6227c2 TY |
151 | } |
152 | ||
153 | /* Supply FPRs from BUF into REGCACHE. */ | |
154 | ||
155 | static void | |
156 | loongarch_store_fpregset (struct regcache *regcache, const void *buf) | |
157 | { | |
158 | const gdb_byte *regbuf = nullptr; | |
159 | int fprsize = register_size (regcache->tdesc, LOONGARCH_FIRST_FP_REGNUM); | |
ea335217 | 160 | int fccsize = register_size (regcache->tdesc, LOONGARCH_FIRST_FCC_REGNUM); |
3f6227c2 TY |
161 | |
162 | for (int i = 0; i < LOONGARCH_LINUX_NUM_FPREGSET; i++) | |
163 | { | |
164 | regbuf = (const gdb_byte *)buf + fprsize * i; | |
165 | supply_register (regcache, LOONGARCH_FIRST_FP_REGNUM + i, regbuf); | |
166 | } | |
ea335217 FC |
167 | |
168 | for (int i = 0; i < LOONGARCH_LINUX_NUM_FCC; i++) | |
169 | { | |
170 | regbuf = (const gdb_byte *)buf + fprsize * LOONGARCH_LINUX_NUM_FPREGSET + | |
171 | fccsize * i; | |
172 | supply_register (regcache, LOONGARCH_FIRST_FCC_REGNUM + i, regbuf); | |
173 | } | |
174 | ||
175 | regbuf = (const gdb_byte *)buf + fprsize * LOONGARCH_LINUX_NUM_FPREGSET + | |
176 | fccsize * LOONGARCH_LINUX_NUM_FCC; | |
177 | supply_register (regcache, LOONGARCH_FCSR_REGNUM, regbuf); | |
3f6227c2 TY |
178 | } |
179 | ||
1e9569f3 HL |
180 | /* Collect lsx regs from REGCACHE into BUF. */ |
181 | ||
182 | static void | |
183 | loongarch_fill_lsxregset (struct regcache *regcache, void *buf) | |
184 | { | |
185 | elf_lsxregset_t *regset = (elf_lsxregset_t *) buf; | |
186 | int i; | |
187 | ||
188 | for (i = 0; i < LOONGARCH_LINUX_NUM_LSXREGSET; i++) | |
189 | collect_register (regcache, LOONGARCH_FIRST_LSX_REGNUM + i, *regset + i); | |
190 | } | |
191 | ||
192 | /* Supply lsx regs from BUF into REGCACHE. */ | |
193 | ||
194 | static void | |
195 | loongarch_store_lsxregset (struct regcache *regcache, const void *buf) | |
196 | { | |
197 | const elf_lsxregset_t *regset = (const elf_lsxregset_t *) buf; | |
198 | int i; | |
199 | ||
200 | for (i = 0; i < LOONGARCH_LINUX_NUM_LSXREGSET; i++) | |
201 | supply_register (regcache, LOONGARCH_FIRST_LSX_REGNUM + i, *regset + i); | |
202 | } | |
203 | ||
204 | /* Collect lasx regs from REGCACHE into BUF. */ | |
205 | ||
206 | static void | |
207 | loongarch_fill_lasxregset (struct regcache *regcache, void *buf) | |
208 | { | |
209 | elf_lasxregset_t *regset = (elf_lasxregset_t *) buf; | |
210 | int i; | |
211 | ||
212 | for (i = 0; i < LOONGARCH_LINUX_NUM_LASXREGSET; i++) | |
213 | collect_register (regcache, LOONGARCH_FIRST_LASX_REGNUM + i, *regset + i); | |
214 | } | |
215 | ||
216 | /* Supply lasx regs from BUF into REGCACHE. */ | |
217 | ||
218 | static void | |
219 | loongarch_store_lasxregset (struct regcache *regcache, const void *buf) | |
220 | { | |
221 | const elf_lasxregset_t *regset = (const elf_lasxregset_t *) buf; | |
222 | int i; | |
223 | ||
224 | for (i = 0; i < LOONGARCH_LINUX_NUM_LASXREGSET; i++) | |
225 | supply_register (regcache, LOONGARCH_FIRST_LASX_REGNUM + i, *regset + i); | |
226 | } | |
227 | ||
e4d74c01 FC |
228 | /* Collect lbt regs from REGCACHE into BUF. */ |
229 | ||
230 | static void | |
231 | loongarch_fill_lbtregset (struct regcache *regcache, void *buf) | |
232 | { | |
233 | gdb_byte *regbuf = (gdb_byte*)buf; | |
234 | int scrsize = register_size (regcache->tdesc, LOONGARCH_FIRST_SCR_REGNUM); | |
235 | int eflagssize = register_size (regcache->tdesc, LOONGARCH_EFLAGS_REGNUM); | |
236 | int i; | |
237 | ||
238 | for (i = 0; i < LOONGARCH_LINUX_NUM_SCR; i++) | |
239 | collect_register (regcache, LOONGARCH_FIRST_SCR_REGNUM + i, regbuf + scrsize * i); | |
240 | ||
241 | collect_register (regcache, LOONGARCH_EFLAGS_REGNUM, | |
242 | regbuf + LOONGARCH_LINUX_NUM_SCR * scrsize); | |
243 | collect_register (regcache, LOONGARCH_FTOP_REGNUM, | |
244 | regbuf + LOONGARCH_LINUX_NUM_SCR * scrsize + eflagssize); | |
245 | ||
246 | } | |
247 | ||
248 | /* Supply lbt regs from BUF into REGCACHE. */ | |
249 | ||
250 | static void | |
251 | loongarch_store_lbtregset (struct regcache *regcache, const void *buf) | |
252 | { | |
253 | ||
254 | gdb_byte *regbuf = (gdb_byte*)buf; | |
255 | int scrsize = register_size (regcache->tdesc, LOONGARCH_FIRST_SCR_REGNUM); | |
256 | int eflagssize = register_size (regcache->tdesc, LOONGARCH_EFLAGS_REGNUM); | |
257 | int i; | |
258 | ||
259 | for (i = 0; i < LOONGARCH_LINUX_NUM_SCR; i++) | |
260 | supply_register (regcache, LOONGARCH_FIRST_SCR_REGNUM + i, regbuf + scrsize * i); | |
261 | ||
262 | supply_register (regcache, LOONGARCH_EFLAGS_REGNUM, | |
263 | regbuf + LOONGARCH_LINUX_NUM_SCR * scrsize); | |
264 | supply_register (regcache, LOONGARCH_FTOP_REGNUM, | |
265 | regbuf + LOONGARCH_LINUX_NUM_SCR * scrsize + eflagssize); | |
266 | ||
267 | } | |
268 | ||
e5ab6af5 YT |
269 | /* LoongArch/Linux regsets. */ |
270 | static struct regset_info loongarch_regsets[] = { | |
271 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS, sizeof (elf_gregset_t), | |
272 | GENERAL_REGS, loongarch_fill_gregset, loongarch_store_gregset }, | |
3f6227c2 TY |
273 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_FPREGSET, sizeof (elf_fpregset_t), |
274 | FP_REGS, loongarch_fill_fpregset, loongarch_store_fpregset }, | |
1e9569f3 HL |
275 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_LARCH_LSX, sizeof (elf_lsxregset_t), |
276 | OPTIONAL_REGS, loongarch_fill_lsxregset, loongarch_store_lsxregset }, | |
277 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_LARCH_LASX, sizeof (elf_lasxregset_t), | |
278 | OPTIONAL_REGS, loongarch_fill_lasxregset, loongarch_store_lasxregset }, | |
e4d74c01 FC |
279 | { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_LARCH_LBT, LOONGARCH_LBT_REGS_SIZE, |
280 | OPTIONAL_REGS, loongarch_fill_lbtregset, loongarch_store_lbtregset }, | |
e5ab6af5 YT |
281 | NULL_REGSET |
282 | }; | |
283 | ||
284 | /* LoongArch/Linux regset information. */ | |
285 | static struct regsets_info loongarch_regsets_info = | |
286 | { | |
287 | loongarch_regsets, /* regsets */ | |
288 | 0, /* num_regsets */ | |
289 | NULL, /* disabled_regsets */ | |
290 | }; | |
291 | ||
292 | /* Definition of linux_target_ops data member "regs_info". */ | |
293 | static struct regs_info loongarch_regs = | |
294 | { | |
295 | NULL, /* regset_bitmap */ | |
296 | NULL, /* usrregs */ | |
297 | &loongarch_regsets_info, | |
298 | }; | |
299 | ||
300 | /* Implementation of linux target ops method "get_regs_info". */ | |
301 | ||
302 | const regs_info * | |
303 | loongarch_target::get_regs_info () | |
304 | { | |
305 | return &loongarch_regs; | |
306 | } | |
307 | ||
308 | /* Implementation of linux target ops method "low_fetch_register". */ | |
309 | ||
310 | bool | |
311 | loongarch_target::low_fetch_register (regcache *regcache, int regno) | |
312 | { | |
3eba4833 | 313 | if (regno != 0) |
e5ab6af5 | 314 | return false; |
3eba4833 | 315 | supply_register_zeroed (regcache, 0); |
e5ab6af5 YT |
316 | return true; |
317 | } | |
318 | ||
319 | bool | |
320 | loongarch_target::low_supports_breakpoints () | |
321 | { | |
322 | return true; | |
323 | } | |
324 | ||
325 | /* Implementation of linux target ops method "low_get_pc". */ | |
326 | ||
327 | CORE_ADDR | |
328 | loongarch_target::low_get_pc (regcache *regcache) | |
329 | { | |
330 | if (register_size (regcache->tdesc, 0) == 8) | |
331 | return linux_get_pc_64bit (regcache); | |
332 | else | |
333 | return linux_get_pc_32bit (regcache); | |
334 | } | |
335 | ||
336 | /* Implementation of linux target ops method "low_set_pc". */ | |
337 | ||
338 | void | |
339 | loongarch_target::low_set_pc (regcache *regcache, CORE_ADDR newpc) | |
340 | { | |
341 | if (register_size (regcache->tdesc, 0) == 8) | |
342 | linux_set_pc_64bit (regcache, newpc); | |
343 | else | |
344 | linux_set_pc_32bit (regcache, newpc); | |
345 | } | |
346 | ||
347 | #define loongarch_breakpoint_len 4 | |
348 | ||
349 | /* LoongArch BRK software debug mode instruction. | |
350 | This instruction needs to match gdb/loongarch-tdep.c | |
351 | (loongarch_default_breakpoint). */ | |
352 | static const gdb_byte loongarch_breakpoint[] = {0x05, 0x00, 0x2a, 0x00}; | |
353 | ||
354 | /* Implementation of target ops method "breakpoint_kind_from_pc". */ | |
355 | ||
356 | int | |
357 | loongarch_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr) | |
358 | { | |
359 | return loongarch_breakpoint_len; | |
360 | } | |
361 | ||
362 | /* Implementation of target ops method "sw_breakpoint_from_kind". */ | |
363 | ||
364 | const gdb_byte * | |
365 | loongarch_target::sw_breakpoint_from_kind (int kind, int *size) | |
366 | { | |
367 | *size = loongarch_breakpoint_len; | |
368 | return (const gdb_byte *) &loongarch_breakpoint; | |
369 | } | |
370 | ||
371 | /* Implementation of linux target ops method "low_breakpoint_at". */ | |
372 | ||
373 | bool | |
374 | loongarch_target::low_breakpoint_at (CORE_ADDR pc) | |
375 | { | |
376 | gdb_byte insn[loongarch_breakpoint_len]; | |
377 | ||
378 | read_memory (pc, (unsigned char *) &insn, loongarch_breakpoint_len); | |
379 | if (memcmp (insn, loongarch_breakpoint, loongarch_breakpoint_len) == 0) | |
380 | return true; | |
381 | ||
382 | return false; | |
383 | } | |
384 | ||
385 | /* The linux target ops object. */ | |
386 | ||
387 | linux_process_target *the_linux_target = &the_loongarch_target; | |
388 | ||
389 | /* Initialize the LoongArch/Linux target. */ | |
390 | ||
391 | void | |
392 | initialize_low_arch () | |
393 | { | |
394 | initialize_regsets_info (&loongarch_regsets_info); | |
395 | } |