trgt_amd64.c revision 331323
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: stable/11/gnu/usr.bin/gdb/kgdb/trgt_amd64.c 331323 2018-03-21 21:10:49Z jhb $");
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 <amd64-tdep.h>
44
45#include "kgdb.h"
46
47CORE_ADDR
48kgdb_trgt_core_pcb(u_int cpuid)
49{
50	return (kgdb_trgt_stop_pcb(cpuid, sizeof(struct pcb)));
51}
52
53void
54kgdb_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
67	supply_register(AMD64_RBX_REGNUM, (char *)&pcb.pcb_rbx);
68	supply_register(AMD64_RBP_REGNUM, (char *)&pcb.pcb_rbp);
69	supply_register(AMD64_RSP_REGNUM, (char *)&pcb.pcb_rsp);
70	supply_register(AMD64_R8_REGNUM + 4, (char *)&pcb.pcb_r12);
71	supply_register(AMD64_R8_REGNUM + 5, (char *)&pcb.pcb_r13);
72	supply_register(AMD64_R8_REGNUM + 6, (char *)&pcb.pcb_r14);
73	supply_register(AMD64_R15_REGNUM, (char *)&pcb.pcb_r15);
74	supply_register(AMD64_RIP_REGNUM, (char *)&pcb.pcb_rip);
75}
76
77void
78kgdb_trgt_store_registers(int regno __unused)
79{
80	fprintf_unfiltered(gdb_stderr, "XXX: %s\n", __func__);
81}
82
83void
84kgdb_trgt_new_objfile(struct objfile *objfile)
85{
86}
87
88struct kgdb_frame_cache {
89	CORE_ADDR	pc;
90	CORE_ADDR	sp;
91};
92
93static int kgdb_trgt_frame_offset[20] = {
94	offsetof(struct trapframe, tf_rax),
95	offsetof(struct trapframe, tf_rbx),
96	offsetof(struct trapframe, tf_rcx),
97	offsetof(struct trapframe, tf_rdx),
98	offsetof(struct trapframe, tf_rsi),
99	offsetof(struct trapframe, tf_rdi),
100	offsetof(struct trapframe, tf_rbp),
101	offsetof(struct trapframe, tf_rsp),
102	offsetof(struct trapframe, tf_r8),
103	offsetof(struct trapframe, tf_r9),
104	offsetof(struct trapframe, tf_r10),
105	offsetof(struct trapframe, tf_r11),
106	offsetof(struct trapframe, tf_r12),
107	offsetof(struct trapframe, tf_r13),
108	offsetof(struct trapframe, tf_r14),
109	offsetof(struct trapframe, tf_r15),
110	offsetof(struct trapframe, tf_rip),
111	offsetof(struct trapframe, tf_rflags),
112	offsetof(struct trapframe, tf_cs),
113	offsetof(struct trapframe, tf_ss)
114};
115
116static struct kgdb_frame_cache *
117kgdb_trgt_frame_cache(struct frame_info *next_frame, void **this_cache)
118{
119	char buf[MAX_REGISTER_SIZE];
120	struct kgdb_frame_cache *cache;
121
122	cache = *this_cache;
123	if (cache == NULL) {
124		cache = FRAME_OBSTACK_ZALLOC(struct kgdb_frame_cache);
125		*this_cache = cache;
126		cache->pc = frame_func_unwind(next_frame);
127		frame_unwind_register(next_frame, SP_REGNUM, buf);
128		cache->sp = extract_unsigned_integer(buf,
129		    register_size(current_gdbarch, SP_REGNUM));
130	}
131	return (cache);
132}
133
134static void
135kgdb_trgt_trapframe_this_id(struct frame_info *next_frame, void **this_cache,
136    struct frame_id *this_id)
137{
138	struct kgdb_frame_cache *cache;
139
140	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
141	*this_id = frame_id_build(cache->sp, cache->pc);
142}
143
144static void
145kgdb_trgt_trapframe_prev_register(struct frame_info *next_frame,
146    void **this_cache, int regnum, int *optimizedp, enum lval_type *lvalp,
147    CORE_ADDR *addrp, int *realnump, void *valuep)
148{
149	char dummy_valuep[MAX_REGISTER_SIZE];
150	struct kgdb_frame_cache *cache;
151	int ofs, regsz;
152
153	regsz = register_size(current_gdbarch, regnum);
154
155	if (valuep == NULL)
156		valuep = dummy_valuep;
157	memset(valuep, 0, regsz);
158	*optimizedp = 0;
159	*addrp = 0;
160	*lvalp = not_lval;
161	*realnump = -1;
162
163	ofs = (regnum >= AMD64_RAX_REGNUM && regnum <= AMD64_EFLAGS_REGNUM + 2)
164	    ? kgdb_trgt_frame_offset[regnum] : -1;
165	if (ofs == -1)
166		return;
167
168	cache = kgdb_trgt_frame_cache(next_frame, this_cache);
169	*addrp = cache->sp + ofs;
170	*lvalp = lval_memory;
171	target_read_memory(*addrp, valuep, regsz);
172}
173
174static const struct frame_unwind kgdb_trgt_trapframe_unwind = {
175        UNKNOWN_FRAME,
176        &kgdb_trgt_trapframe_this_id,
177        &kgdb_trgt_trapframe_prev_register
178};
179
180const struct frame_unwind *
181kgdb_trgt_trapframe_sniffer(struct frame_info *next_frame)
182{
183	char *pname;
184	CORE_ADDR pc;
185
186	pc = frame_pc_unwind(next_frame);
187	pname = NULL;
188	find_pc_partial_function(pc, &pname, NULL, NULL);
189	if (pname == NULL)
190		return (NULL);
191	if (strcmp(pname, "calltrap") == 0 ||
192	    strcmp(pname, "fast_syscall_common") == 0 ||
193	    strcmp(pname, "mchk_calltrap") == 0 ||
194	    strcmp(pname, "nmi_calltrap") == 0 ||
195	    (pname[0] == 'X' && pname[1] != '_'))
196		return (&kgdb_trgt_trapframe_unwind);
197	/* printf("%s: %lx =%s\n", __func__, pc, pname); */
198	return (NULL);
199}
200
201/*
202 * This function ensures, that the PC is inside the
203 * function section which is understood by GDB.
204 *
205 * Return 0 when fixup is necessary, -1 otherwise.
206 */
207int
208kgdb_trgt_pc_fixup(CORE_ADDR *pc __unused)
209{
210
211	return (-1);
212}
213