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#include <machine/asm.h>
32#include <machine/pcb.h>
33#include <machine/frame.h>
34#include <err.h>
35#include <kvm.h>
36#include <string.h>
37
38#include <defs.h>
39#include <target.h>
40#include <gdbthread.h>
41#include <inferior.h>
42#include <regcache.h>
43#include <frame-unwind.h>
44#include <sparc-tdep.h>
45#include <sparc64-tdep.h>
46
47#include "kgdb.h"
48
49void
50kgdb_trgt_fetch_registers(int regno __unused)
51{
52	struct kthr *kt;
53	struct pcb pcb;
54
55	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
56	if (kt == NULL)
57		return;
58	if (kvm_read(kvm, kt->pcb, &pcb, sizeof(pcb)) != sizeof(pcb)) {
59		warnx("kvm_read: %s", kvm_geterr(kvm));
60		memset(&pcb, 0, sizeof(pcb));
61	}
62
63	supply_register(SPARC_SP_REGNUM, (char *)&pcb.pcb_sp);
64	sparc_supply_rwindow(current_regcache, pcb.pcb_sp, -1);
65	supply_register(SPARC64_PC_REGNUM, (char *)&pcb.pcb_pc);
66	pcb.pcb_pc += 4;
67	supply_register(SPARC64_NPC_REGNUM, (char *)&pcb.pcb_pc);
68}
69
70void
71kgdb_trgt_store_registers(int regno __unused)
72{
73	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
74}
75
76void
77kgdb_trgt_new_objfile(struct objfile *objfile)
78{
79}
80
81struct kgdb_frame_cache {
82	CORE_ADDR	pc;
83	CORE_ADDR	sp;
84	CORE_ADDR	fp;
85};
86
87static struct kgdb_frame_cache *
88kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
89{
90	char buf[MAX_REGISTER_SIZE];
91	struct kgdb_frame_cache *cache;
92
93	cache = *this_cache;
94	if (cache == NULL) {
95		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
96		*this_cache = cache;
97		cache->pc = frame_func_unwind(next_frame);
98		frame_unwind_register(next_frame, SPARC_SP_REGNUM, buf);
99		cache->sp = extract_unsigned_integer(buf,
100		    register_size(current_gdbarch, SPARC_SP_REGNUM));
101		frame_unwind_register(next_frame, SPARC_FP_REGNUM, buf);
102		cache->fp = extract_unsigned_integer(buf,
103		    register_size(current_gdbarch, SPARC_FP_REGNUM));
104		cache->fp += BIAS - sizeof(struct trapframe);
105	}
106	return (cache);
107}
108
109static void
110kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
111    struct frame_id *this_id)
112{
113	struct kgdb_frame_cache *cache;
114
115	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
116	*this_id = frame_id_build(cache->sp, cache->pc);
117}
118
119static void
120kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
121    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
122    CORE_ADDR *addrp, int *realnump, void *valuep)
123{
124	char dummy_valuep[MAX_REGISTER_SIZE];
125	struct kgdb_frame_cache *cache;
126	int ofs, regsz;
127
128	regsz = register_size(current_gdbarch, regnum);
129
130	if (valuep == NULL)
131		valuep = dummy_valuep;
132	memset(valuep, 0, regsz);
133	*optimizedp = 0;
134	*addrp = 0;
135	*lvalp = not_lval;
136	*realnump = -1;
137
138	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
139
140	switch (regnum) {
141	case SPARC_SP_REGNUM:
142		ofs = offsetof(struct trapframe, tf_sp);
143		break;
144	case SPARC64_PC_REGNUM:
145		ofs = offsetof(struct trapframe, tf_tpc);
146		break;
147	case SPARC64_NPC_REGNUM:
148		ofs = offsetof(struct trapframe, tf_tnpc);
149		break;
150	case SPARC_O0_REGNUM:
151	case SPARC_O1_REGNUM:
152	case SPARC_O2_REGNUM:
153	case SPARC_O3_REGNUM:
154	case SPARC_O4_REGNUM:
155	case SPARC_O5_REGNUM:
156	case SPARC_O7_REGNUM:
157		ofs = offsetof(struct trapframe, tf_out) +
158		    (regnum - SPARC_O0_REGNUM) * 8;
159		break;
160	default:
161		if (regnum >= SPARC_L0_REGNUM && regnum <= SPARC_I7_REGNUM) {
162			ofs = (regnum - SPARC_L0_REGNUM) * 8;
163			*addrp = cache->sp + BIAS + ofs;
164			*lvalp = lval_memory;
165			target_read_memory(*addrp, valuep, regsz);
166		}
167		return;
168	}
169
170	*addrp = cache->fp + ofs;
171	*lvalp = lval_memory;
172	target_read_memory(*addrp, valuep, regsz);
173}
174
175static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
176        UNKNOWN_FRAME,
177        &kgdb_trgt_trapframe_this_id,
178        &kgdb_trgt_trapframe_prev_register
179};
180
181const struct frame_unwind *
182kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
183{
184	char *pname;
185	CORE_ADDR pc;
186
187	pc = frame_func_unwind(next_frame);
188	pname = NULL;
189	find_pc_partial_function(pc, &pname, NULL, NULL);
190	if (pname == NULL)
191		return (NULL);
192	if (strcmp(pname, "tl0_intr") == 0 ||
193	    strcmp(pname, "tl0_trap") == 0 ||
194	    strcmp(pname, "tl1_intr") == 0 ||
195	    strcmp(pname, "tl1_trap") == 0)
196		return (&kgdb_trgt_trapframe_unwind);
197	/* printf("%s: %lx =%s\n", __func__, pc, pname); */
198	return (NULL);
199}
200