]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdbserver/linux-loongarch-low.cc
gdbserver: Add LoongArch/Linux support
[thirdparty/binutils-gdb.git] / gdbserver / linux-loongarch-low.cc
CommitLineData
e5ab6af5
YT
1/* GNU/Linux/LoongArch specific low level interface, for the remote server
2 for GDB.
3 Copyright (C) 2022 Free Software Foundation, Inc.
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
28class loongarch_target : public linux_process_target
29{
30public:
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
38protected:
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
59static loongarch_target the_loongarch_target;
60
61bool
62loongarch_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
68bool
69loongarch_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
77void
78loongarch_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
87 if (!tdesc->expedite_regs)
88 init_target_desc (tdesc.get (), expedite_regs);
89 current_process ()->tdesc = tdesc.release ();
90}
91
92/* Collect GPRs from REGCACHE into BUF. */
93
94static void
95loongarch_fill_gregset (struct regcache *regcache, void *buf)
96{
97 const struct target_desc *tdesc = regcache->tdesc;
98 elf_gregset_t *regset = (elf_gregset_t *) buf;
99 int regno = find_regno (tdesc, "r0");
100 int i;
101
102 for (i = 1; i < 32; i++)
103 collect_register (regcache, regno + i, *regset + i);
104 collect_register_by_name (regcache, "pc", *regset + 32);
105 collect_register_by_name (regcache, "badv", *regset + 33);
106}
107
108/* Supply GPRs from BUF into REGCACHE. */
109
110static void
111loongarch_store_gregset (struct regcache *regcache, const void *buf)
112{
113 const struct target_desc *tdesc = regcache->tdesc;
114 const elf_gregset_t *regset = (const elf_gregset_t *) buf;
115 int regno = find_regno (tdesc, "r0");
116 int i;
117
118 supply_register_zeroed (regcache, regno);
119 for (i = 1; i < 32; i++)
120 supply_register (regcache, regno + i, *regset + i);
121 supply_register_by_name (regcache, "pc", *regset + 32);
122 supply_register_by_name (regcache, "badv", *regset + 33);
123}
124
125/* LoongArch/Linux regsets. */
126static struct regset_info loongarch_regsets[] = {
127 { PTRACE_GETREGSET, PTRACE_SETREGSET, NT_PRSTATUS, sizeof (elf_gregset_t),
128 GENERAL_REGS, loongarch_fill_gregset, loongarch_store_gregset },
129 NULL_REGSET
130};
131
132/* LoongArch/Linux regset information. */
133static struct regsets_info loongarch_regsets_info =
134 {
135 loongarch_regsets, /* regsets */
136 0, /* num_regsets */
137 NULL, /* disabled_regsets */
138 };
139
140/* Definition of linux_target_ops data member "regs_info". */
141static struct regs_info loongarch_regs =
142 {
143 NULL, /* regset_bitmap */
144 NULL, /* usrregs */
145 &loongarch_regsets_info,
146 };
147
148/* Implementation of linux target ops method "get_regs_info". */
149
150const regs_info *
151loongarch_target::get_regs_info ()
152{
153 return &loongarch_regs;
154}
155
156/* Implementation of linux target ops method "low_fetch_register". */
157
158bool
159loongarch_target::low_fetch_register (regcache *regcache, int regno)
160{
161 const struct target_desc *tdesc = regcache->tdesc;
162
163 if (regno != find_regno (tdesc, "r0"))
164 return false;
165 supply_register_zeroed (regcache, regno);
166 return true;
167}
168
169bool
170loongarch_target::low_supports_breakpoints ()
171{
172 return true;
173}
174
175/* Implementation of linux target ops method "low_get_pc". */
176
177CORE_ADDR
178loongarch_target::low_get_pc (regcache *regcache)
179{
180 if (register_size (regcache->tdesc, 0) == 8)
181 return linux_get_pc_64bit (regcache);
182 else
183 return linux_get_pc_32bit (regcache);
184}
185
186/* Implementation of linux target ops method "low_set_pc". */
187
188void
189loongarch_target::low_set_pc (regcache *regcache, CORE_ADDR newpc)
190{
191 if (register_size (regcache->tdesc, 0) == 8)
192 linux_set_pc_64bit (regcache, newpc);
193 else
194 linux_set_pc_32bit (regcache, newpc);
195}
196
197#define loongarch_breakpoint_len 4
198
199/* LoongArch BRK software debug mode instruction.
200 This instruction needs to match gdb/loongarch-tdep.c
201 (loongarch_default_breakpoint). */
202static const gdb_byte loongarch_breakpoint[] = {0x05, 0x00, 0x2a, 0x00};
203
204/* Implementation of target ops method "breakpoint_kind_from_pc". */
205
206int
207loongarch_target::breakpoint_kind_from_pc (CORE_ADDR *pcptr)
208{
209 return loongarch_breakpoint_len;
210}
211
212/* Implementation of target ops method "sw_breakpoint_from_kind". */
213
214const gdb_byte *
215loongarch_target::sw_breakpoint_from_kind (int kind, int *size)
216{
217 *size = loongarch_breakpoint_len;
218 return (const gdb_byte *) &loongarch_breakpoint;
219}
220
221/* Implementation of linux target ops method "low_breakpoint_at". */
222
223bool
224loongarch_target::low_breakpoint_at (CORE_ADDR pc)
225{
226 gdb_byte insn[loongarch_breakpoint_len];
227
228 read_memory (pc, (unsigned char *) &insn, loongarch_breakpoint_len);
229 if (memcmp (insn, loongarch_breakpoint, loongarch_breakpoint_len) == 0)
230 return true;
231
232 return false;
233}
234
235/* The linux target ops object. */
236
237linux_process_target *the_linux_target = &the_loongarch_target;
238
239/* Initialize the LoongArch/Linux target. */
240
241void
242initialize_low_arch ()
243{
244 initialize_regsets_info (&loongarch_regsets_info);
245}