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