trgt_i386.c revision 178880
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: head/gnu/usr.bin/gdb/kgdb/trgt_i386.c 178880 2008-05-09 19:00:40Z jhb $");
29
30#include <sys/param.h>
31#include <sys/proc.h>
32#include <machine/pcb.h>
33#include <machine/frame.h>
34#include <machine/segments.h>
35#include <machine/tss.h>
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 <i386-tdep.h>
47
48#include "kgdb.h"
49
50static int ofs_fix;
51
52void
53kgdb_trgt_fetch_registers(int regno __unused)
54{
55	struct kthr *kt;
56	struct pcb pcb;
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	supply_register(I386_EBX_REGNUM, (char *)&pcb.pcb_ebx);
66	supply_register(I386_ESP_REGNUM, (char *)&pcb.pcb_esp);
67	supply_register(I386_EBP_REGNUM, (char *)&pcb.pcb_ebp);
68	supply_register(I386_ESI_REGNUM, (char *)&pcb.pcb_esi);
69	supply_register(I386_EDI_REGNUM, (char *)&pcb.pcb_edi);
70	supply_register(I386_EIP_REGNUM, (char *)&pcb.pcb_eip);
71}
72
73void
74kgdb_trgt_store_registers(int regno __unused)
75{
76	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
77}
78
79void
80kgdb_trgt_new_objfile(struct objfile *objfile)
81{
82
83	/*
84	 * In revision 1.117 of i386/i386/exception.S trap handlers
85	 * were changed to pass trapframes by reference rather than
86	 * by value.  Detect this by seeing if the first instruction
87	 * at the 'calltrap' label is a "push %esp" which has the
88	 * opcode 0x54.
89	 */
90	if (kgdb_parse("((char *)calltrap)[0]") == 0x54)
91		ofs_fix = 4;
92	else
93		ofs_fix = 0;
94}
95
96struct kgdb_tss_cache {
97	CORE_ADDR	pc;
98	CORE_ADDR	sp;
99	CORE_ADDR	tss;
100};
101
102static int kgdb_trgt_tss_offset[15] = {
103	offsetof(struct i386tss, tss_eax),
104	offsetof(struct i386tss, tss_ecx),
105	offsetof(struct i386tss, tss_edx),
106	offsetof(struct i386tss, tss_ebx),
107	offsetof(struct i386tss, tss_esp),
108	offsetof(struct i386tss, tss_ebp),
109	offsetof(struct i386tss, tss_esi),
110	offsetof(struct i386tss, tss_edi),
111	offsetof(struct i386tss, tss_eip),
112	offsetof(struct i386tss, tss_eflags),
113	offsetof(struct i386tss, tss_cs),
114	offsetof(struct i386tss, tss_ss),
115	offsetof(struct i386tss, tss_ds),
116	offsetof(struct i386tss, tss_es),
117	offsetof(struct i386tss, tss_fs)
118};
119
120/*
121 * If the current thread is executing on a CPU, fetch the common_tss
122 * for that CPU.
123 *
124 * This is painful because 'struct pcpu' is variant sized, so we can't
125 * use it.  Instead, we lookup the GDT selector for this CPU and
126 * extract the base of the TSS from there.
127 */
128static CORE_ADDR
129kgdb_trgt_fetch_tss(void)
130{
131	struct kthr *kt;
132	struct segment_descriptor sd;
133	uintptr_t addr, cpu0prvpage, tss;
134
135	kt = kgdb_thr_lookup_tid(ptid_get_pid(inferior_ptid));
136	if (kt == NULL || kt->cpu == NOCPU)
137		return (0);
138
139	addr = kgdb_lookup("_gdt");
140	if (addr == 0)
141		return (0);
142	addr += (kt->cpu * NGDT + GPROC0_SEL) * sizeof(sd);
143	if (kvm_read(kvm, addr, &sd, sizeof(sd)) != sizeof(sd)) {
144		warnx("kvm_read: %s", kvm_geterr(kvm));
145		return (0);
146	}
147	if (sd.sd_type != SDT_SYS386BSY) {
148		warnx("descriptor is not a busy TSS");
149		return (0);
150	}
151	tss = sd.sd_hibase << 24 | sd.sd_lobase;
152
153	/*
154	 * In SMP kernels, the TSS is stored as part of the per-CPU
155	 * data.  On older kernels, the CPU0's private page
156	 * is stored at an address that isn't mapped in minidumps.
157	 * However, the data is mapped at the alternate cpu0prvpage
158	 * address.  Thus, if the TSS is at the invalid address,
159	 * change it to be relative to cpu0prvpage instead.
160	 */
161	if (trunc_page(tss) == 0xffc00000) {
162		addr = kgdb_lookup("_cpu0prvpage");
163		if (addr == 0) {
164			warnx("kvm_nlist(_cpu0prvpage): %s", kvm_geterr(kvm));
165			return (0);
166		}
167		if (kvm_read(kvm, addr, &cpu0prvpage, sizeof(cpu0prvpage)) !=
168		    sizeof(cpu0prvpage)) {
169			warnx("kvm_read: %s", kvm_geterr(kvm));
170			return (0);
171		}
172		tss = cpu0prvpage + (tss & PAGE_MASK);
173	}
174	return ((CORE_ADDR)tss);
175}
176
177static struct kgdb_tss_cache *
178kgdb_trgt_tss_cache(struct frame_info *next_frame, void **this_cache)
179{
180	char buf[MAX_REGISTER_SIZE];
181	struct kgdb_tss_cache *cache;
182
183	cache = *this_cache;
184	if (cache == NULL) {
185		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_tss_cache);
186		*this_cache = cache;
187		cache->pc = frame_func_unwind(next_frame);
188		frame_unwind_register(next_frame, SP_REGNUM, buf);
189		cache->sp = extract_unsigned_integer(buf,
190		    register_size(current_gdbarch, SP_REGNUM));
191		cache->tss = kgdb_trgt_fetch_tss();
192	}
193	return (cache);
194}
195
196static void
197kgdb_trgt_dblfault_this_id(struct frame_info *next_frame, void **this_cache,
198    struct frame_id *this_id)
199{
200	struct kgdb_tss_cache *cache;
201
202	cache = kgdb_trgt_tss_cache(next_frame, this_cache);
203	*this_id = frame_id_build(cache->sp, cache->pc);
204}
205
206static void
207kgdb_trgt_dblfault_prev_register(struct frame_info *next_frame,
208    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
209    CORE_ADDR *addrp, int *realnump, void *valuep)
210{
211	char dummy_valuep[MAX_REGISTER_SIZE];
212	struct kgdb_tss_cache *cache;
213	int ofs, regsz;
214
215	regsz = register_size(current_gdbarch, regnum);
216
217	if (valuep == NULL)
218		valuep = dummy_valuep;
219	memset(valuep, 0, regsz);
220	*optimizedp = 0;
221	*addrp = 0;
222	*lvalp = not_lval;
223	*realnump = -1;
224
225	ofs = (regnum >= I386_EAX_REGNUM && regnum <= I386_FS_REGNUM)
226	    ? kgdb_trgt_tss_offset[regnum] : -1;
227	if (ofs == -1)
228		return;
229
230	cache = kgdb_trgt_tss_cache(next_frame, this_cache);
231	if (cache->tss == 0)
232		return;
233	*addrp = cache->tss + ofs;
234	*lvalp = lval_memory;
235	target_read_memory(*addrp, valuep, regsz);
236}
237
238static const struct frame_unwind kgdb_trgt_dblfault_unwind = {
239        UNKNOWN_FRAME,
240        &kgdb_trgt_dblfault_this_id,
241        &kgdb_trgt_dblfault_prev_register
242};
243
244struct kgdb_frame_cache {
245	int		intrframe;
246	CORE_ADDR	pc;
247	CORE_ADDR	sp;
248};
249
250static int kgdb_trgt_frame_offset[15] = {
251	offsetof(struct trapframe, tf_eax),
252	offsetof(struct trapframe, tf_ecx),
253	offsetof(struct trapframe, tf_edx),
254	offsetof(struct trapframe, tf_ebx),
255	offsetof(struct trapframe, tf_esp),
256	offsetof(struct trapframe, tf_ebp),
257	offsetof(struct trapframe, tf_esi),
258	offsetof(struct trapframe, tf_edi),
259	offsetof(struct trapframe, tf_eip),
260	offsetof(struct trapframe, tf_eflags),
261	offsetof(struct trapframe, tf_cs),
262	offsetof(struct trapframe, tf_ss),
263	offsetof(struct trapframe, tf_ds),
264	offsetof(struct trapframe, tf_es),
265	offsetof(struct trapframe, tf_fs)
266};
267
268static struct kgdb_frame_cache *
269kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
270{
271	char buf[MAX_REGISTER_SIZE];
272	struct kgdb_frame_cache *cache;
273	char *pname;
274
275	cache = *this_cache;
276	if (cache == NULL) {
277		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
278		*this_cache = cache;
279		cache->pc = frame_func_unwind(next_frame);
280		find_pc_partial_function(cache->pc, &pname, NULL, NULL);
281		cache->intrframe = (pname[0] == 'X') ? 1 : 0;
282		frame_unwind_register(next_frame, SP_REGNUM, buf);
283		cache->sp = extract_unsigned_integer(buf,
284		    register_size(current_gdbarch, SP_REGNUM));
285	}
286	return (cache);
287}
288
289static void
290kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
291    struct frame_id *this_id)
292{
293	struct kgdb_frame_cache *cache;
294
295	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
296	*this_id = frame_id_build(cache->sp, cache->pc);
297}
298
299static void
300kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
301    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
302    CORE_ADDR *addrp, int *realnump, void *valuep)
303{
304	char dummy_valuep[MAX_REGISTER_SIZE];
305	struct kgdb_frame_cache *cache;
306	int ofs, regsz;
307
308	regsz = register_size(current_gdbarch, regnum);
309
310	if (valuep == NULL)
311		valuep = dummy_valuep;
312	memset(valuep, 0, regsz);
313	*optimizedp = 0;
314	*addrp = 0;
315	*lvalp = not_lval;
316	*realnump = -1;
317
318	ofs = (regnum >= I386_EAX_REGNUM && regnum <= I386_FS_REGNUM)
319	    ? kgdb_trgt_frame_offset[regnum] + ofs_fix : -1;
320	if (ofs == -1)
321		return;
322
323	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
324	*addrp = cache->sp + ofs + (cache->intrframe ? 4 : 0);
325	*lvalp = lval_memory;
326	target_read_memory(*addrp, valuep, regsz);
327}
328
329static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
330        UNKNOWN_FRAME,
331        &kgdb_trgt_trapframe_this_id,
332        &kgdb_trgt_trapframe_prev_register
333};
334
335const struct frame_unwind *
336kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
337{
338	char *pname;
339	CORE_ADDR pc;
340
341	pc = frame_pc_unwind(next_frame);
342	pname = NULL;
343	find_pc_partial_function(pc, &pname, NULL, NULL);
344	if (pname == NULL)
345		return (NULL);
346	if (strcmp(pname, "dblfault_handler") == 0)
347		return (&kgdb_trgt_dblfault_unwind);
348	if (strcmp(pname, "calltrap") == 0 ||
349	    (pname[0] == 'X' && pname[1] != '_'))
350		return (&kgdb_trgt_trapframe_unwind);
351	/* printf("%s: %llx =%s\n", __func__, pc, pname); */
352	return (NULL);
353}
354