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