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
76	supply_register(tdep->ppc_lr_regnum, (char *)&pcb.pcb_lr);
77	supply_register(tdep->ppc_cr_regnum, (char *)&pcb.pcb_cr);
78}
79
80void
81kgdb_trgt_store_registers(int regno __unused)
82{
83	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
84}
85
86void
87kgdb_trgt_new_objfile(struct objfile *objfile)
88{
89}
90
91struct kgdb_frame_cache {
92	CORE_ADDR	pc;
93	CORE_ADDR	sp;
94};
95
96static struct kgdb_frame_cache *
97kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
98{
99	char buf[MAX_REGISTER_SIZE];
100	struct kgdb_frame_cache *cache;
101
102	cache = *this_cache;
103	if (cache == NULL) {
104		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
105		*this_cache = cache;
106		cache->pc = frame_func_unwind(next_frame);
107		frame_unwind_register(next_frame, SP_REGNUM, buf);
108		cache->sp = extract_unsigned_integer(buf,
109		    register_size(current_gdbarch, SP_REGNUM));
110	}
111	return (cache);
112}
113
114static void
115kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
116    struct frame_id *this_id)
117{
118	struct kgdb_frame_cache *cache;
119
120	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
121	*this_id = frame_id_build(cache->sp, cache->pc);
122}
123
124static void
125kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
126    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
127    CORE_ADDR *addrp, int *realnump, void *valuep)
128{
129	char dummy_valuep[MAX_REGISTER_SIZE];
130	struct gdbarch_tdep *tdep;
131	struct kgdb_frame_cache *cache;
132	int ofs, regsz;
133
134	tdep = gdbarch_tdep(current_gdbarch);
135	regsz = register_size(current_gdbarch, regnum);
136
137	if (valuep == NULL)
138		valuep = dummy_valuep;
139	memset(valuep, 0, regsz);
140	*optimizedp = 0;
141	*addrp = 0;
142	*lvalp = not_lval;
143	*realnump = -1;
144
145	if (regnum >= tdep->ppc_gp0_regnum &&
146	    regnum <= tdep->ppc_gplast_regnum)
147		ofs = offsetof(struct trapframe,
148		    fixreg[regnum - tdep->ppc_gp0_regnum]);
149	else if (regnum == tdep->ppc_lr_regnum)
150		ofs = offsetof(struct trapframe, lr);
151	else if (regnum == tdep->ppc_cr_regnum)
152		ofs = offsetof(struct trapframe, cr);
153	else if (regnum == tdep->ppc_xer_regnum)
154		ofs = offsetof(struct trapframe, xer);
155	else if (regnum == tdep->ppc_ctr_regnum)
156		ofs = offsetof(struct trapframe, ctr);
157	else if (regnum == PC_REGNUM)
158		ofs = offsetof(struct trapframe, srr0);
159	else
160		return;
161
162	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
163	*addrp = cache->sp + 8 + ofs;
164	*lvalp = lval_memory;
165	target_read_memory(*addrp, valuep, regsz);
166}
167
168static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
169        UNKNOWN_FRAME,
170        &kgdb_trgt_trapframe_this_id,
171        &kgdb_trgt_trapframe_prev_register
172};
173
174const struct frame_unwind *
175kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
176{
177	char *pname;
178	CORE_ADDR pc;
179
180	pc = frame_pc_unwind(next_frame);
181	pname = NULL;
182	find_pc_partial_function(pc, &pname, NULL, NULL);
183	if (pname == NULL)
184		return (NULL);
185	if (strcmp(pname, "asttrapexit") == 0 ||
186	    strcmp(pname, "trapexit") == 0)
187		return (&kgdb_trgt_trapframe_unwind);
188	/* printf("%s: %llx =%s\n", __func__, pc, pname); */
189	return (NULL);
190}
191