summaryrefslogtreecommitdiff
path: root/usr/src/cmd/mdb/i86xpv/modules/xpv/xpv.c
blob: 8912e80b89a2d2fae9fb1c11546533ff0444c600 (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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <mdb/mdb_modapi.h>
#include <mdb/mdb_ks.h>
#include <mdb/mdb_ctf.h>
#include <mdb/mdb_gelf.h>
#include <mdb/mdb_target_impl.h>
#include <mdb/mdb_kvm.h>
#include <mdb/mdb.h>
#include <xen/public/xen.h>
#include <xen/public/arch-x86/xen.h>
#include <errno.h>

static mdb_ctf_id_t domain_type;

/*
 * Some constants found in the non-public sched.h header file
 */
#define	MAX_EVTCHNS		NR_EVENT_CHANNELS
#define	EVTCHNS_PER_BUCKET	128
#define	NR_EVTCHN_BUCKETS	(MAX_EVTCHNS / EVTCHNS_PER_BUCKET)

/*
 * "struct domain" is an internal Xen structure.  Rather than trying to
 * keep the mdb source in sync with Xen, we use CTF to extract the
 * interesting bits from the binary, and stash them in the structure
 * defined below.
 */
typedef struct domain {
	short		domain_id;
	int		tot_pages;
	int		max_pages;
	int		xenheap_pages;
	ulong_t		domain_flags;
	char		is_hvm;
	struct vcpu	*vcpu[MAX_VIRT_CPUS];
	struct evtchn   *evtchn[NR_EVTCHN_BUCKETS];
	struct domain 	*next_in_list;
} domain_t;

static uintptr_t
get_dom0_addr()
{
	GElf_Sym sym;
	uintptr_t addr;

	if ((mdb_lookup_by_obj(MDB_TGT_OBJ_EVERY, "dom0", &sym)) == 1) {
		mdb_warn("can't find symbol 'dom0'");
		return (0);
	}

	if (sym.st_size != sizeof (uintptr_t)) {
		mdb_printf("Symbol 'dom0' found, but with the wrong size\n");
		return (0);
	}

	if (mdb_vread(&addr, sym.st_size, sym.st_value) == -1) {
		mdb_warn("can't read data for symbol 'dom0'");
		return (0);
	}

	return (addr);
}

typedef struct domain_walk {
	uint_t dw_step;
} domain_walk_t;

int
domain_walk_init(mdb_walk_state_t *wsp)
{
	domain_walk_t *dwp;

	if (wsp->walk_addr == NULL)
		if ((wsp->walk_addr = get_dom0_addr()) == NULL)
			return (WALK_ERR);

	dwp = mdb_alloc(sizeof (domain_walk_t), UM_SLEEP);
	dwp->dw_step = FALSE;
	wsp->walk_data = dwp;
	return (WALK_NEXT);
}

int
domain_walk_step(mdb_walk_state_t *wsp)
{
	domain_walk_t *dwp = (domain_walk_t *)wsp->walk_data;
	struct domain dom;
	int status;

	if (wsp->walk_addr == NULL)
		return (WALK_DONE);

	status = wsp->walk_callback(wsp->walk_addr, (void *)wsp->walk_addr,
	    wsp->walk_cbdata);

	if (mdb_ctf_vread(&dom, "struct domain", wsp->walk_addr,
	    MDB_CTF_VREAD_IGNORE_ABSENT) != 0) {
		mdb_warn("can't find next domain");
		return (WALK_ERR);
	}
	wsp->walk_addr = (uintptr_t)dom.next_in_list;

	dwp->dw_step = TRUE;
	return (status);
}

void
domain_walk_fini(mdb_walk_state_t *wsp)
{
	domain_walk_t *dwp = (domain_walk_t *)wsp->walk_data;

	mdb_free(dwp, sizeof (domain_walk_t));
}

typedef struct vcpu_walk {
	uint_t vw_count;
	uint_t vw_step;
} vcpu_walk_t;

int
vcpu_walk_init(mdb_walk_state_t *wsp)
{
	vcpu_walk_t *vwp;
	uintptr_t off;

	if (wsp->walk_addr == NULL)
		if ((wsp->walk_addr = get_dom0_addr()) == NULL)
			return (WALK_ERR);

	if (mdb_ctf_offsetof(domain_type, "vcpu", &off)) {
		mdb_warn("can't find per-domain vcpu information");
		return (WALK_ERR);
	}

	wsp->walk_addr = wsp->walk_addr + (off / NBBY);
	vwp = mdb_alloc(sizeof (vcpu_walk_t), UM_SLEEP);
	vwp->vw_step = FALSE;
	vwp->vw_count = 0;
	wsp->walk_data = vwp;
	return (WALK_NEXT);
}

int
vcpu_walk_step(mdb_walk_state_t *wsp)
{
	vcpu_walk_t *vwp = (vcpu_walk_t *)wsp->walk_data;
	uintptr_t vcpu_ptr;
	int status;

	if (vwp->vw_count++ >= MAX_VIRT_CPUS)
		return (WALK_DONE);
	if ((wsp->walk_addr == NULL) ||
	    (mdb_vread(&vcpu_ptr, sizeof (uintptr_t), wsp->walk_addr) == -1) ||
	    (vcpu_ptr == 0))
		return (WALK_DONE);

	status = wsp->walk_callback(vcpu_ptr, (void *)vcpu_ptr,
	    wsp->walk_cbdata);

	wsp->walk_addr = wsp->walk_addr + sizeof (uintptr_t);
	vwp->vw_step = TRUE;
	return (status);
}

void
vcpu_walk_fini(mdb_walk_state_t *wsp)
{
	vcpu_walk_t *vwp = (vcpu_walk_t *)wsp->walk_data;

	mdb_free(vwp, sizeof (vcpu_walk_t));
}

int
domain(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
	domain_t dom;
	uintptr_t off, vcpu_addr, evtchn_addr;

	if (!mdb_ctf_type_valid(domain_type)) {
		mdb_warn("Can't parse Xen domain info.\n");
		return (DCMD_ERR);
	}

	if (!(flags & DCMD_ADDRSPEC)) {
		if (mdb_walk_dcmd("domain", "domain", argc, argv) == -1) {
			mdb_warn("can't walk domains");
			return (DCMD_ERR);
		}
		return (DCMD_OK);
	}

	if (DCMD_HDRSPEC(flags))
		mdb_printf("%?s %3s %8s %8s %8s %3s %?s %?s\n",
		    "ADDR", "ID", "TPAGES", "MPAGES", "FLAGS", "HVM",
		    "VCPU", "EVTCHN");

	if (mdb_ctf_vread(&dom, "struct domain", addr,
	    MDB_CTF_VREAD_IGNORE_ABSENT) != 0) {
		mdb_warn("can't read domain information");
		return (DCMD_ERR);
	}

	if (mdb_ctf_offsetof(domain_type, "vcpu", &off)) {
		mdb_warn("can't find per-domain vcpu information");
		return (DCMD_ERR);
	}
	vcpu_addr = addr + (off / NBBY);
	if (mdb_ctf_offsetof(domain_type, "evtchn", &off)) {
		mdb_warn("can't find per-domain event channel information");
		return (DCMD_ERR);
	}
	evtchn_addr = addr + (off / NBBY);
	mdb_printf("%?lx %3d %8x %8x %8x %3d %?lx %?lx\n",
	    addr, dom.domain_id, dom.tot_pages, dom.max_pages, dom.domain_flags,
	    dom.is_hvm, vcpu_addr, evtchn_addr);

	return (DCMD_OK);
}

static const mdb_dcmd_t dcmds[] = {
	{ "domain", ":", "display Xen domain info", domain },
	{ NULL }
};

static const mdb_walker_t walkers[] = {
	{ "domain", "walk list of Xen domains",
		domain_walk_init, domain_walk_step, domain_walk_fini },
	{ "vcpu", "walk a Xen domain's vcpus",
		vcpu_walk_init, vcpu_walk_step, vcpu_walk_fini },
	{ NULL }
};

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

const mdb_modinfo_t *
_mdb_init(void)
{
	GElf_Sym sym;
	uintptr_t pip;
	struct panic_info pi;

	if (mdb_lookup_by_name("xpv_panic_info", &sym) < 0)
		return (NULL);

	if (mdb_ctf_vread(&pip, "uintptr_t", sym.st_value, 0) == -1) {
		mdb_warn("failed to read xpv panic_info pointer");
		return (NULL);
	}
	if (mdb_ctf_vread(&pi, "struct panic_info", pip, 0) == -1) {
		mdb_warn("failed to read xpv panic_info");
		return (NULL);
	}

	if (pi.pi_version != PANIC_INFO_VERSION) {
		mdb_warn("unrecognized hypervisor panic format");
		return (NULL);
	}

	if (mdb_ctf_lookup_by_name("struct domain", &domain_type) != 0) {
		mdb_warn("Can't parse Xen domain info: "
		    "'struct domain' not found.\n");
		mdb_ctf_type_invalidate(&domain_type);
	}

	return (&modinfo);
}