1/*-
2 * Copyright (c) 2006 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 AUTHOR ``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 AUTHOR 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#ifdef CROSS_DEBUGGER
32#include <sys/powerpc/include/pcb.h>
33#include <sys/powerpc/include/frame.h>
34#else
35#include <machine/pcb.h>
36#include <machine/frame.h>
37#endif
38#include <err.h>
39#include <kvm.h>
40#include <string.h>
41
42#include <defs.h>
43#include <target.h>
44#include <gdbthread.h>
45#include <inferior.h>
46#include <regcache.h>
47#include <frame-unwind.h>
48#include <ppc-tdep.h>
49
50#include "kgdb.h"
51
52CORE_ADDR
53kgdb_trgt_core_pcb(u_int cpuid)
54{
55	return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb)));
56}
57
58void
59kgdb_trgt_fetch_registers(int regno __unused)
60{
61	struct kthr *kt;
62	struct pcb pcb;
63	struct gdbarch_tdep *tdep;
64	int i;
65
66	tdep = gdbarch_tdep (current_gdbarch);
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
76	/*
77	 * r14-r31 are saved in the pcb
78	 */
79	for (i = 14; i <= 31; i++) {
80		supply_register(tdep->ppc_gp0_regnum + i,
81		    (char *)&pcb.pcb_context[i]);
82	}
83
84	/* r1 is saved in the sp field */
85	supply_register(tdep->ppc_gp0_regnum + 1, (char *)&pcb.pcb_sp);
86
87	supply_register(tdep->ppc_lr_regnum, (char *)&pcb.pcb_lr);
88	supply_register(tdep->ppc_cr_regnum, (char *)&pcb.pcb_cr);
89}
90
91void
92kgdb_trgt_store_registers(int regno __unused)
93{
94	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
95}
96
97void
98kgdb_trgt_new_objfile(struct objfile *objfile)
99{
100}
101
102struct kgdb_frame_cache {
103	CORE_ADDR	pc;
104	CORE_ADDR	sp;
105};
106
107static struct kgdb_frame_cache *
108kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
109{
110	char buf[MAX_REGISTER_SIZE];
111	struct kgdb_frame_cache *cache;
112
113	cache = *this_cache;
114	if (cache == NULL) {
115		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
116		*this_cache = cache;
117		cache->pc = frame_func_unwind(next_frame);
118		frame_unwind_register(next_frame, SP_REGNUM, buf);
119		cache->sp = extract_unsigned_integer(buf,
120		    register_size(current_gdbarch, SP_REGNUM));
121	}
122	return (cache);
123}
124
125static void
126kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
127    struct frame_id *this_id)
128{
129	struct kgdb_frame_cache *cache;
130
131	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
132	*this_id = frame_id_build(cache->sp, cache->pc);
133}
134
135static void
136kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
137    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
138    CORE_ADDR *addrp, int *realnump, void *valuep)
139{
140	char dummy_valuep[MAX_REGISTER_SIZE];
141	struct gdbarch_tdep *tdep;
142	struct kgdb_frame_cache *cache;
143	int ofs, regsz;
144
145	tdep = gdbarch_tdep(current_gdbarch);
146	regsz = register_size(current_gdbarch, regnum);
147
148	if (valuep == NULL)
149		valuep = dummy_valuep;
150	memset(valuep, 0, regsz);
151	*optimizedp = 0;
152	*addrp = 0;
153	*lvalp = not_lval;
154	*realnump = -1;
155
156	if (regnum >= tdep->ppc_gp0_regnum &&
157	    regnum <= tdep->ppc_gplast_regnum)
158		ofs = offsetof(struct trapframe,
159		    fixreg[regnum - tdep->ppc_gp0_regnum]);
160	else if (regnum == tdep->ppc_lr_regnum)
161		ofs = offsetof(struct trapframe, lr);
162	else if (regnum == tdep->ppc_cr_regnum)
163		ofs = offsetof(struct trapframe, cr);
164	else if (regnum == tdep->ppc_xer_regnum)
165		ofs = offsetof(struct trapframe, xer);
166	else if (regnum == tdep->ppc_ctr_regnum)
167		ofs = offsetof(struct trapframe, ctr);
168	else if (regnum == PC_REGNUM)
169		ofs = offsetof(struct trapframe, srr0);
170	else
171		return;
172
173	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
174	*addrp = cache->sp + 8 + ofs;
175	*lvalp = lval_memory;
176	target_read_memory(*addrp, valuep, regsz);
177}
178
179static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
180        UNKNOWN_FRAME,
181        &kgdb_trgt_trapframe_this_id,
182        &kgdb_trgt_trapframe_prev_register
183};
184
185const struct frame_unwind *
186kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
187{
188	char *pname;
189	CORE_ADDR pc;
190
191	pc = frame_pc_unwind(next_frame);
192	pname = NULL;
193	find_pc_partial_function(pc, &pname, NULL, NULL);
194	if (pname == NULL)
195		return (NULL);
196	if (strcmp(pname, "asttrapexit") == 0 ||
197	    strcmp(pname, "trapexit") == 0)
198		return (&kgdb_trgt_trapframe_unwind);
199	/* printf("%s: %llx =%s\n", __func__, pc, pname); */
200	return (NULL);
201}
202