]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - gnu/usr.bin/gdb/kgdb/trgt_arm.c
Import DTS files from Linux 5.4
[FreeBSD/FreeBSD.git] / gnu / usr.bin / gdb / kgdb / trgt_arm.c
1 /*
2  * Copyright (c) 2004 Marcel Moolenaar
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/types.h>
31 #ifndef CROSS_DEBUGGER
32 #include <machine/pcb.h>
33 #include <machine/frame.h>
34 #include <machine/armreg.h>
35 #endif
36 #include <err.h>
37 #include <kvm.h>
38 #include <string.h>
39
40 #include <defs.h>
41 #include <target.h>
42 #include <gdbthread.h>
43 #include <inferior.h>
44 #include <regcache.h>
45 #include <frame-unwind.h>
46 #include <arm-tdep.h>
47
48 #include "kgdb.h"
49
50 CORE_ADDR
51 kgdb_trgt_core_pcb(u_int cpuid)
52 {
53 #ifndef CROSS_DEBUGGER
54         return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb)));
55 #else
56         return -1;
57 #endif
58 }
59
60 void
61 kgdb_trgt_fetch_registers(int regno __unused)
62 {
63 #ifndef CROSS_DEBUGGER
64         struct kthr *kt;
65         struct pcb pcb;
66         int i;
67
68         kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
69         if (kt == NULL)
70                 return;
71         if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
72                 warnx("kvm_read: %s", kvm_geterr(kvm));
73                 memset(&pcb, 0, sizeof(pcb));
74         }
75         for (i = ARM_A1_REGNUM + 4; i <= ARM_SP_REGNUM; i++) {
76                 supply_register(i, (char *)&pcb.pcb_regs.sf_r4 +
77                     (i - (ARM_A1_REGNUM + 4 )) * 4);
78         }
79         supply_register(ARM_PC_REGNUM, (char *)&pcb.pcb_regs.sf_pc);
80         supply_register(ARM_LR_REGNUM, (char *)&pcb.pcb_regs.sf_lr);
81 #endif
82 }
83
84 void
85 kgdb_trgt_store_registers(int regno __unused)
86 {
87         fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
88 }
89
90 void
91 kgdb_trgt_new_objfile(struct objfile *objfile)
92 {
93 }
94
95 #ifndef CROSS_DEBUGGER
96 struct kgdb_frame_cache {
97         CORE_ADDR       fp;
98         CORE_ADDR       sp;
99         CORE_ADDR       pc;
100 };
101
102 static int kgdb_trgt_frame_offset[26] = {
103         offsetof(struct trapframe, tf_r0),
104         offsetof(struct trapframe, tf_r1),
105         offsetof(struct trapframe, tf_r2),
106         offsetof(struct trapframe, tf_r3),
107         offsetof(struct trapframe, tf_r4),
108         offsetof(struct trapframe, tf_r5),
109         offsetof(struct trapframe, tf_r6),
110         offsetof(struct trapframe, tf_r7),
111         offsetof(struct trapframe, tf_r8),
112         offsetof(struct trapframe, tf_r9),
113         offsetof(struct trapframe, tf_r10),
114         offsetof(struct trapframe, tf_r11),
115         offsetof(struct trapframe, tf_r12),
116         offsetof(struct trapframe, tf_svc_sp),
117         offsetof(struct trapframe, tf_svc_lr),
118         offsetof(struct trapframe, tf_pc),
119         -1, -1, -1, -1, -1, -1, -1, -1, -1,
120         offsetof(struct trapframe, tf_spsr)
121 };
122
123 static struct kgdb_frame_cache *
124 kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
125 {
126         char buf[MAX_REGISTER_SIZE];
127         struct kgdb_frame_cache *cache;
128
129         cache = *this_cache;
130         if (cache == NULL) {
131                 cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
132                 *this_cache = cache;
133                 frame_unwind_register(next_frame, ARM_SP_REGNUM, buf);
134                 cache->sp = extract_unsigned_integer(buf,
135                     register_size(current_gdbarch, ARM_SP_REGNUM));
136                 frame_unwind_register(next_frame, ARM_FP_REGNUM, buf);
137                 cache->fp = extract_unsigned_integer(buf,
138                     register_size(current_gdbarch, ARM_FP_REGNUM));
139                 cache->pc = frame_func_unwind(next_frame);
140         }
141         return (cache);
142 }
143
144 static int is_undef;
145
146 static void
147 kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
148     struct frame_id *this_id)
149 {
150         struct kgdb_frame_cache *cache;
151
152         cache = kgdb_trgt_frame_cache(next_frame, this_cache);
153         *this_id = frame_id_build(cache->sp, cache->pc);
154 }
155
156 static void
157 kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
158     void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
159     CORE_ADDR *addrp, int *realnump, void *valuep)
160 {
161         char dummy_valuep[MAX_REGISTER_SIZE];
162         struct kgdb_frame_cache *cache;
163         int ofs, regsz;
164         CORE_ADDR sp;
165
166         regsz = register_size(current_gdbarch, regnum);
167
168         if (valuep == NULL)
169                 valuep = dummy_valuep;
170         memset(valuep, 0, regsz);
171         *optimizedp = 0;
172         *addrp = 0;
173         *lvalp = not_lval;
174         *realnump = -1;
175
176         ofs = (regnum >= 0 && regnum <= ARM_PS_REGNUM)
177             ? kgdb_trgt_frame_offset[regnum] : -1;
178         if (ofs == -1)
179                 return;
180
181         cache = kgdb_trgt_frame_cache(next_frame, this_cache);
182         sp = cache->sp;
183
184         ofs = kgdb_trgt_frame_offset[regnum];
185         *addrp = sp + ofs;
186         *lvalp = lval_memory;
187         target_read_memory(*addrp, valuep, regsz);
188 }
189
190 static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
191         UNKNOWN_FRAME,
192         &kgdb_trgt_trapframe_this_id,
193         &kgdb_trgt_trapframe_prev_register
194 };
195 #endif
196
197 const struct frame_unwind *
198 kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
199 {
200 #ifndef CROSS_DEBUGGER
201         char *pname;
202         CORE_ADDR pc;
203
204         pc = frame_pc_unwind(next_frame);
205         pname = NULL;
206         find_pc_partial_function(pc, &pname, NULL, NULL);
207         if (pname == NULL) {
208                 is_undef = 0;
209                 return (NULL);
210         }
211         if (!strcmp(pname, "undefinedinstruction"))
212                 is_undef = 1;
213         if (strcmp(pname, "Laddress_exception_entry") == 0 ||
214             strcmp(pname, "undefined_entry") == 0 ||
215             strcmp(pname, "exception_exit") == 0 ||
216             strcmp(pname, "Laddress_exception_msg") == 0 ||
217             strcmp(pname, "irq_entry") == 0)
218                 return (&kgdb_trgt_trapframe_unwind);
219         if (!strcmp(pname, "undefinedinstruction"))
220                 is_undef = 1;
221         else
222                 is_undef = 0;
223 #endif
224         return (NULL);
225 }
226
227 /*
228  * This function ensures, that the PC is inside the
229  * function section which is understood by GDB.
230  *
231  * Return 0 when fixup is necessary, -1 otherwise.
232  */
233 int
234 kgdb_trgt_pc_fixup(CORE_ADDR *pc)
235 {
236 #ifndef CROSS_DEBUGGER
237         struct minimal_symbol *msymbol;
238         int valpc;
239
240         /*
241          * exception_exit and swi_exit are special. These functions
242          * are artificially injected into the stack to be executed
243          * as the last entry in calling chain when all functions exit.
244          * Treat them differently.
245          */
246         msymbol = lookup_minimal_symbol_by_pc(*pc);
247         if (msymbol != NULL) {
248                 if (strcmp(DEPRECATED_SYMBOL_NAME(msymbol), "exception_exit") == 0)
249                         return (0);
250                 if (strcmp(DEPRECATED_SYMBOL_NAME(msymbol), "swi_exit") == 0)
251                         return (0);
252         }
253
254         /*
255          * kdb_enter contains an invalid instruction which is supposed
256          * to generate a trap. BFD does not understand it and treats
257          * this part of function as a separate function. Move PC
258          * two instruction earlier to be inside kdb_enter section.
259          */
260         target_read_memory(*pc - 4, (char*)&valpc, 4);
261         if (valpc == 0xe7ffffff) {
262                 *pc = *pc - 8;
263                 return (0);
264         }
265
266         /*
267          * When the panic/vpanic is the last (noreturn) function,
268          * the bottom of the calling function looks as below.
269          *   mov lr, pc
270          *   b panic
271          * Normally, GDB is not able to detect function boundaries,
272          * so move the PC two instruction earlier where it can deal
273          * with it.
274          * Match this pair of instructions: mov lr, pc followed with
275          * non-linked branch.
276          */
277         if ((valpc & 0xff000000) == 0xea000000) {
278                 target_read_memory(*pc - 8, (char*)&valpc, 4);
279                 if (valpc == 0xe1a0e00f) {
280                         *pc -= 8;
281                         return (0);
282                 }
283         }
284 #endif
285         return (-1);
286 }