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