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
50void
51kgdb_trgt_fetch_registers(int regno __unused)
52{
53#ifndef CROSS_DEBUGGER
54	struct kthr *kt;
55	struct pcb pcb;
56	int i, reg;
57
58	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
59	if (kt == NULL)
60		return;
61	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
62		warnx("kvm_read: %s", kvm_geterr(kvm));
63		memset(&pcb, 0, sizeof(pcb));
64	}
65	for (i = ARM_A1_REGNUM + 8; i <= ARM_SP_REGNUM; i++) {
66		supply_register(i, (char *)&pcb.un_32.pcb32_r8 +
67		    (i - (ARM_A1_REGNUM + 8 )) * 4);
68	}
69	if (pcb.un_32.pcb32_sp != 0) {
70		for (i = 0; i < 4; i++) {
71			if (kvm_read(kvm, pcb.un_32.pcb32_sp + (i) * 4,
72			    &reg, 4) != 4) {
73				warnx("kvm_read: %s", kvm_geterr(kvm));
74				break;
75			}
76			supply_register(ARM_A1_REGNUM + 4 + i, (char *)&reg);
77		}
78		if (kvm_read(kvm, pcb.un_32.pcb32_sp + 4 * 4, &reg, 4) != 4)
79			warnx("kvm_read :%s", kvm_geterr(kvm));
80		else
81			supply_register(ARM_PC_REGNUM, (char *)&reg);
82	}
83#endif
84}
85
86void
87kgdb_trgt_store_registers(int regno __unused)
88{
89	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
90}
91
92void
93kgdb_trgt_new_objfile(struct objfile *objfile)
94{
95}
96
97#ifndef CROSS_DEBUGGER
98struct kgdb_frame_cache {
99	CORE_ADDR	fp;
100	CORE_ADDR	sp;
101};
102
103static int kgdb_trgt_frame_offset[26] = {
104	offsetof(struct trapframe, tf_r0),
105	offsetof(struct trapframe, tf_r1),
106	offsetof(struct trapframe, tf_r2),
107	offsetof(struct trapframe, tf_r3),
108	offsetof(struct trapframe, tf_r4),
109	offsetof(struct trapframe, tf_r5),
110	offsetof(struct trapframe, tf_r6),
111	offsetof(struct trapframe, tf_r7),
112	offsetof(struct trapframe, tf_r8),
113	offsetof(struct trapframe, tf_r9),
114	offsetof(struct trapframe, tf_r10),
115	offsetof(struct trapframe, tf_r11),
116	offsetof(struct trapframe, tf_r12),
117	offsetof(struct trapframe, tf_svc_sp),
118	offsetof(struct trapframe, tf_svc_lr),
119	offsetof(struct trapframe, tf_pc),
120	-1, -1, -1, -1, -1, -1, -1, -1, -1,
121	offsetof(struct trapframe, tf_spsr)
122};
123
124static struct kgdb_frame_cache *
125kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
126{
127	char buf[MAX_REGISTER_SIZE];
128	struct kgdb_frame_cache *cache;
129
130	cache = *this_cache;
131	if (cache == NULL) {
132		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
133		*this_cache = cache;
134		frame_unwind_register(next_frame, ARM_SP_REGNUM, buf);
135		cache->sp = extract_unsigned_integer(buf,
136		    register_size(current_gdbarch, ARM_SP_REGNUM));
137		frame_unwind_register(next_frame, ARM_FP_REGNUM, buf);
138		cache->fp = extract_unsigned_integer(buf,
139		    register_size(current_gdbarch, ARM_FP_REGNUM));
140	}
141	return (cache);
142}
143
144static int is_undef;
145
146static void
147kgdb_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->fp, 0);
154}
155
156static void
157kgdb_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	int is_undefined = 0;
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
183	if (is_undef && (regnum == ARM_SP_REGNUM || regnum == ARM_PC_REGNUM)) {
184		*addrp = cache->sp + offsetof(struct trapframe, tf_spsr);
185		target_read_memory(*addrp, valuep, regsz);
186		is_undefined = 1;
187		ofs = kgdb_trgt_frame_offset[ARM_SP_REGNUM];
188
189	}
190	*addrp = cache->sp + ofs;
191	*lvalp = lval_memory;
192	target_read_memory(*addrp, valuep, regsz);
193
194	if (is_undefined) {
195		*addrp = *(unsigned int *)valuep + (regnum == ARM_SP_REGNUM ?
196		    0 : 8);
197		target_read_memory(*addrp, valuep, regsz);
198
199	}
200}
201
202static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
203        UNKNOWN_FRAME,
204        &kgdb_trgt_trapframe_this_id,
205        &kgdb_trgt_trapframe_prev_register
206};
207#endif
208
209const struct frame_unwind *
210kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
211{
212#ifndef CROSS_DEBUGGER
213	char *pname;
214	CORE_ADDR pc;
215
216	pc = frame_pc_unwind(next_frame);
217	pname = NULL;
218	find_pc_partial_function(pc, &pname, NULL, NULL);
219	if (pname == NULL) {
220		is_undef = 0;
221		return (NULL);
222	}
223	if (!strcmp(pname, "undefinedinstruction"))
224		is_undef = 1;
225	if (strcmp(pname, "Laddress_exception_entry") == 0 ||
226	    strcmp(pname, "undefined_entry") == 0 ||
227	    strcmp(pname, "exception_exit") == 0 ||
228	    strcmp(pname, "Laddress_exception_msg") == 0 ||
229	    strcmp(pname, "irq_entry") == 0)
230		return (&kgdb_trgt_trapframe_unwind);
231	if (!strcmp(pname, "undefinedinstruction"))
232		is_undef = 1;
233	else
234		is_undef = 0;
235#endif
236	return (NULL);
237}
238