trgt_arm.c revision 278614
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: stable/10/gnu/usr.bin/gdb/kgdb/trgt_arm.c 278614 2015-02-12 04:15:55Z ian $");
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
50CORE_ADDR
51kgdb_trgt_core_pcb(u_int cpuid)
52{
53	return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb)));
54}
55
56void
57kgdb_trgt_fetch_registers(int regno __unused)
58{
59#ifndef CROSS_DEBUGGER
60	struct kthr *kt;
61	struct pcb pcb;
62	int i, reg;
63
64	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
65	if (kt == NULL)
66		return;
67	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
68		warnx("kvm_read: %s", kvm_geterr(kvm));
69		memset(&pcb, 0, sizeof(pcb));
70	}
71	for (i = ARM_A1_REGNUM + 4; i <= ARM_SP_REGNUM; i++) {
72		supply_register(i, (char *)&pcb.pcb_regs.sf_r4 +
73		    (i - (ARM_A1_REGNUM + 4 )) * 4);
74	}
75	if (pcb.pcb_regs.sf_sp != 0) {
76		if (kvm_read(kvm, pcb.pcb_regs.sf_sp + 4 * 4, &reg, 4) != 4)
77			warnx("kvm_read :%s", kvm_geterr(kvm));
78		else
79			supply_register(ARM_PC_REGNUM, (char *)&reg);
80	}
81#endif
82}
83
84void
85kgdb_trgt_store_registers(int regno __unused)
86{
87	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
88}
89
90void
91kgdb_trgt_new_objfile(struct objfile *objfile)
92{
93}
94
95#ifndef CROSS_DEBUGGER
96struct kgdb_frame_cache {
97	CORE_ADDR	fp;
98	CORE_ADDR	sp;
99};
100
101static int kgdb_trgt_frame_offset[26] = {
102	offsetof(struct trapframe, tf_r0),
103	offsetof(struct trapframe, tf_r1),
104	offsetof(struct trapframe, tf_r2),
105	offsetof(struct trapframe, tf_r3),
106	offsetof(struct trapframe, tf_r4),
107	offsetof(struct trapframe, tf_r5),
108	offsetof(struct trapframe, tf_r6),
109	offsetof(struct trapframe, tf_r7),
110	offsetof(struct trapframe, tf_r8),
111	offsetof(struct trapframe, tf_r9),
112	offsetof(struct trapframe, tf_r10),
113	offsetof(struct trapframe, tf_r11),
114	offsetof(struct trapframe, tf_r12),
115	offsetof(struct trapframe, tf_svc_sp),
116	offsetof(struct trapframe, tf_svc_lr),
117	offsetof(struct trapframe, tf_pc),
118	-1, -1, -1, -1, -1, -1, -1, -1, -1,
119	offsetof(struct trapframe, tf_spsr)
120};
121
122static struct kgdb_frame_cache *
123kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
124{
125	char buf[MAX_REGISTER_SIZE];
126	struct kgdb_frame_cache *cache;
127
128	cache = *this_cache;
129	if (cache == NULL) {
130		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
131		*this_cache = cache;
132		frame_unwind_register(next_frame, ARM_SP_REGNUM, buf);
133		cache->sp = extract_unsigned_integer(buf,
134		    register_size(current_gdbarch, ARM_SP_REGNUM));
135		frame_unwind_register(next_frame, ARM_FP_REGNUM, buf);
136		cache->fp = extract_unsigned_integer(buf,
137		    register_size(current_gdbarch, ARM_FP_REGNUM));
138	}
139	return (cache);
140}
141
142static int is_undef;
143
144static void
145kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
146    struct frame_id *this_id)
147{
148	struct kgdb_frame_cache *cache;
149
150	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
151	*this_id = frame_id_build(cache->fp, 0);
152}
153
154static void
155kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
156    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
157    CORE_ADDR *addrp, int *realnump, void *valuep)
158{
159	char dummy_valuep[MAX_REGISTER_SIZE];
160	struct kgdb_frame_cache *cache;
161	int ofs, regsz;
162	int is_undefined = 0;
163
164	regsz = register_size(current_gdbarch, regnum);
165
166	if (valuep == NULL)
167		valuep = dummy_valuep;
168	memset(valuep, 0, regsz);
169	*optimizedp = 0;
170	*addrp = 0;
171	*lvalp = not_lval;
172	*realnump = -1;
173
174	ofs = (regnum >= 0 && regnum <= ARM_PS_REGNUM)
175	    ? kgdb_trgt_frame_offset[regnum] : -1;
176	if (ofs == -1)
177		return;
178
179	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
180
181	if (is_undef && (regnum == ARM_SP_REGNUM || regnum == ARM_PC_REGNUM)) {
182		*addrp = cache->sp + offsetof(struct trapframe, tf_spsr);
183		target_read_memory(*addrp, valuep, regsz);
184		is_undefined = 1;
185		ofs = kgdb_trgt_frame_offset[ARM_SP_REGNUM];
186
187	}
188	*addrp = cache->sp + ofs;
189	*lvalp = lval_memory;
190	target_read_memory(*addrp, valuep, regsz);
191
192	if (is_undefined) {
193		*addrp = *(unsigned int *)valuep + (regnum == ARM_SP_REGNUM ?
194		    0 : 8);
195		target_read_memory(*addrp, valuep, regsz);
196
197	}
198}
199
200static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
201        UNKNOWN_FRAME,
202        &kgdb_trgt_trapframe_this_id,
203        &kgdb_trgt_trapframe_prev_register
204};
205#endif
206
207const struct frame_unwind *
208kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
209{
210#ifndef CROSS_DEBUGGER
211	char *pname;
212	CORE_ADDR pc;
213
214	pc = frame_pc_unwind(next_frame);
215	pname = NULL;
216	find_pc_partial_function(pc, &pname, NULL, NULL);
217	if (pname == NULL) {
218		is_undef = 0;
219		return (NULL);
220	}
221	if (!strcmp(pname, "undefinedinstruction"))
222		is_undef = 1;
223	if (strcmp(pname, "Laddress_exception_entry") == 0 ||
224	    strcmp(pname, "undefined_entry") == 0 ||
225	    strcmp(pname, "exception_exit") == 0 ||
226	    strcmp(pname, "Laddress_exception_msg") == 0 ||
227	    strcmp(pname, "irq_entry") == 0)
228		return (&kgdb_trgt_trapframe_unwind);
229	if (!strcmp(pname, "undefinedinstruction"))
230		is_undef = 1;
231	else
232		is_undef = 0;
233#endif
234	return (NULL);
235}
236