summaryrefslogtreecommitdiff
path: root/kvm_mdb.c
blob: 2b698af2ae3daf87324de727d1959ac4a1615269 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/*
 * Copyright (c) 2011, Joyent, Inc. All rights reserved.
 */

#include <sys/types.h>
#include <sys/param.h>
#include <sys/errno.h>
#include <sys/uio.h>
#include <sys/buf.h>
#include <sys/modctl.h>
#include <sys/open.h>
#include <sys/kmem.h>
#include <sys/poll.h>
#include <sys/conf.h>
#include <sys/cmn_err.h>
#include <sys/stat.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/atomic.h>
#include <sys/spl.h>
#include <sys/cpuvar.h>
#include <sys/segments.h>
#include <sys/mdb_modapi.h>

#include "kvm_msr.h"
#include "kvm_vmx.h"
#include "kvm_iodev.h"
#include "kvm_host.h"
#include "kvm_x86host.h"
#include "kvm.h"

int
kvm_mdb_memory_slot_init(mdb_walk_state_t *wsp)
{
	struct kvm_memslots *memslots;
	struct kvm kvm;
	uintptr_t addr;

	if (wsp->walk_addr == NULL) {
		mdb_warn("kvm_memory_slot does not support global walks");
		return (WALK_ERR);
	}

	if (mdb_vread(&kvm, sizeof (kvm), wsp->walk_addr) == -1) {
		mdb_warn("couldn't read kvm at %p", wsp->walk_addr);
		return (DCMD_ERR);
	}

	addr = (uintptr_t)kvm.memslots;
	memslots = mdb_alloc(sizeof (struct kvm_memslots), UM_SLEEP | UM_GC);

	if (mdb_vread(memslots, sizeof (struct kvm_memslots), addr) == -1) {
		mdb_warn("couldn't read memslots at %p", addr);
		return (DCMD_ERR);
	}

	wsp->walk_addr = addr + offsetof(struct kvm_memslots, memslots);
	wsp->walk_arg = 0;
	wsp->walk_data = memslots;

	return (WALK_NEXT);
}

int
kvm_mdb_memory_slot_step(mdb_walk_state_t *wsp)
{
	struct kvm_memslots *memslots = wsp->walk_data;
	uintptr_t ndx = (uintptr_t)wsp->walk_arg;

	if (ndx >= KVM_MEMORY_SLOTS)
		return (WALK_DONE);

	wsp->walk_arg = (void *)(ndx + 1);

	return (wsp->walk_callback(wsp->walk_addr +
	    ndx * sizeof (struct kvm_memory_slot), &memslots->memslots[ndx],
	    wsp->walk_cbdata));
}

int
kvm_mdb_mem_alias_init(mdb_walk_state_t *wsp)
{
	struct kvm_mem_aliases *aliases;
	struct kvm kvm;
	uintptr_t addr;

	if (wsp->walk_addr == NULL) {
		mdb_warn("kvm_mem_alias does not support global walks");
		return (WALK_ERR);
	}

	if (mdb_vread(&kvm, sizeof (kvm), wsp->walk_addr) == -1) {
		mdb_warn("couldn't read kvm at %p", wsp->walk_addr);
		return (DCMD_ERR);
	}

	addr = (uintptr_t)kvm.arch.aliases;
	aliases = mdb_alloc(sizeof (struct kvm_mem_aliases), UM_SLEEP | UM_GC);

	if (mdb_vread(aliases, sizeof (struct kvm_mem_aliases), addr) == -1) {
		mdb_warn("couldn't read aliases at %p", addr);
		return (DCMD_ERR);
	}

	wsp->walk_addr = addr + offsetof(struct kvm_mem_aliases, aliases);
	wsp->walk_arg = 0;
	wsp->walk_data = aliases;

	return (WALK_NEXT);
}

int
kvm_mdb_mem_alias_step(mdb_walk_state_t *wsp)
{
	struct kvm_mem_aliases *aliases = wsp->walk_data;
	uintptr_t ndx = (uintptr_t)wsp->walk_arg;

	if (ndx >= aliases->naliases)
		return (WALK_DONE);

	wsp->walk_arg = (void *)(ndx + 1);

	return (wsp->walk_callback(wsp->walk_addr +
	    ndx * sizeof (struct kvm_mem_alias), &aliases->aliases[ndx],
	    wsp->walk_cbdata));
}

static int
kvm_mdb_gpa2qva_walk_alias(uintptr_t addr,
    const struct kvm_mem_alias *alias, uintptr_t *gfn)
{
	if (alias->flags & KVM_ALIAS_INVALID)
		return (WALK_NEXT);

	if (*gfn < alias->base_gfn || *gfn >= alias->base_gfn + alias->npages)
		return (WALK_NEXT);

	*gfn = alias->target_gfn + *gfn - alias->base_gfn;

	return (WALK_DONE);
}

static int
kvm_mdb_gpa2qva_walk_slot(uintptr_t addr,
    const struct kvm_memory_slot *memslot, uintptr_t *gpa)
{
	uintptr_t gfn = *gpa >> PAGESHIFT;

	if (gfn < memslot->base_gfn)
		return (WALK_NEXT);

	if (gfn >= memslot->base_gfn + memslot->npages)
		return (WALK_NEXT);

	mdb_printf("%p\n", memslot->userspace_addr +
	    ((gfn - memslot->base_gfn) << PAGESHIFT) + (*gpa & PAGEOFFSET));

	*gpa = -1;

	return (WALK_DONE);
}

static int
kvm_mdb_gpa2qva(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	struct kvm kvm;
	uintptr_t gpa = addr, gfn, kaddr;
	int i;

	if (!(flags & DCMD_ADDRSPEC) || argc < 1)
		return (DCMD_USAGE);

	switch (argv[0].a_type) {
	case MDB_TYPE_STRING:
		kaddr = mdb_strtoull(argv[0].a_un.a_str);
		break;

	case MDB_TYPE_IMMEDIATE:
		kaddr = argv[0].a_un.a_val;
		break;

	default:
		return (DCMD_USAGE);
	}

	if (mdb_vread(&kvm, sizeof (kvm), kaddr) == -1) {
		mdb_warn("couldn't read kvm at %p", kaddr);
		return (DCMD_ERR);
	}

	gfn = gpa >> PAGESHIFT;

	/*
	 * First unalias our guest PFN...
	 */
	if (mdb_pwalk("kvm_mem_alias",
	    (mdb_walk_cb_t)kvm_mdb_gpa2qva_walk_alias, &gfn, kaddr) == -1) {
		mdb_warn("failed to walk 'kvm_memory_slot' for %p", kaddr);
		return (DCMD_ERR);
	}

	gpa = (gfn << PAGESHIFT) | (gpa & PAGEOFFSET);

	/*
	 * Now walk memory slots looking for a match.
	 */
	if (mdb_pwalk("kvm_memory_slot",
	    (mdb_walk_cb_t)kvm_mdb_gpa2qva_walk_slot, &gpa, kaddr) == -1) {
		mdb_warn("failed to walk 'kvm_memory_slot' for %p", kaddr);
		return (DCMD_ERR);
	}

	if (gpa != -1) {
		mdb_warn("0x%p is unknown for kvm 0x%p", addr, kaddr);
		return (DCMD_ERR);
	}

	return (DCMD_OK);
}

static int
kvm_mdb_gsiroutes(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	struct kvm kvm;
	struct kvm_irq_routing_table *table;
	int ii, jj;

	if (argc > 1)
		return (DCMD_USAGE);

	if (mdb_vread(&kvm, sizeof (struct kvm), addr) == -1) {
		mdb_warn("couldn't read kvm at %p", addr);
		return (DCMD_ERR);
	}

	table = mdb_alloc(sizeof (struct kvm_irq_routing_table),
	    UM_SLEEP | UM_GC);

	if (mdb_vread(table, sizeof (struct kvm_irq_routing_table),
	    (uintptr_t)kvm.irq_routing) == -1) {
		mdb_warn("couldn't read kvm irq routing table at %p",
		    kvm.irq_routing);
		return (DCMD_ERR);
	}

	if (DCMD_HDRSPEC(flags))
		mdb_printf("%s %7s %5s\n", "CHIP", "PORT", "GSI");

	for (ii = 0; ii < KVM_NR_IRQCHIPS; ii++) {
		for (jj = 0; jj < KVM_IOAPIC_NUM_PINS; jj++)
			mdb_printf("%3d %7d    0x%x\n", ii, jj,
			    table->chip[ii][jj]);
	}

	return (DCMD_OK);
}

static int
kvm_mdb_kvm_walk_init(mdb_walk_state_t *wsp)
{
	list_t list;
	GElf_Sym sym;
	if (wsp->walk_addr != NULL) {
		mdb_warn("kvm does not support non-global walks\n");
		return (WALK_ERR);
	}

	if (mdb_lookup_by_name("vm_list", &sym) != 0) {
		mdb_warn("unable to locate vm_list\n");
		return (WALK_ERR);
	}

	wsp->walk_addr = sym.st_value;

	if (mdb_vread(&list, sizeof (list_t), wsp->walk_addr) == -1) {
		mdb_warn("failed to read vm_list\n");
		return (WALK_ERR);
	}

	if (mdb_layered_walk("list", wsp) == -1) {
		mdb_warn("failed to walk 'list'\n");
		return (WALK_ERR);
	}

	return (WALK_NEXT);
}

static int
kvm_mdb_kvm_walk_step(mdb_walk_state_t *wsp)
{
	return (wsp->walk_callback(wsp->walk_addr, wsp->walk_layer,
	    wsp->walk_cbdata));
}

static const mdb_dcmd_t dcmds[] = {
	{ "kvm_gpa2qva", "?[address of kvm]", "translate a guest physical "
	    "to a QEMU virtual address", kvm_mdb_gpa2qva },
	{ "kvm_gsiroutes", NULL, "print out the global system "
	    "interrupt (GSI) routing table", kvm_mdb_gsiroutes },
	{ NULL }
};

static const mdb_walker_t walkers[] = {
	{ "kvm_memory_slot", "walk kvm_memory_slot structures for a given kvm",
	    kvm_mdb_memory_slot_init, kvm_mdb_memory_slot_step },
	{ "kvm_mem_alias", "walk kvm_mem_alias structures for a given kvm",
	    kvm_mdb_mem_alias_init, kvm_mdb_mem_alias_step },
	{ "kvm", "walk all the kvm structures",
	    kvm_mdb_kvm_walk_init, kvm_mdb_kvm_walk_step },
	{ NULL }
};

static const mdb_modinfo_t modinfo = { MDB_API_VERSION, dcmds, walkers };

const mdb_modinfo_t *
_mdb_init(void)
{
	return (&modinfo);
}