aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxc/xc_dom_boot.c
blob: d8bfe485d84783bda8d6d1e3dc64812e3983ade6 (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
/*
 * Xen domain builder -- xen booter.
 *
 * This is the code which actually boots a fresh
 * prepared domain image as xen guest domain.
 *
 * ==>  this is the only domain bilder code piece
 *          where xen hypercalls are allowed        <==
 *
 * This code is licenced under the GPL.
 * written 2006 by Gerd Hoffmann <kraxel@suse.de>.
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <zlib.h>

#include "xg_private.h"
#include "xc_dom.h"
#include <xen/hvm/params.h>

/* ------------------------------------------------------------------------ */

static int setup_hypercall_page(struct xc_dom_image *dom)
{
    DECLARE_DOMCTL;
    xen_pfn_t pfn;
    int rc;

    if (-1 == dom->parms.virt_hypercall)
	return 0;
    pfn = (dom->parms.virt_hypercall - dom->parms.virt_base)
	>> XC_DOM_PAGE_SHIFT(dom);

    xc_dom_printf("%s: vaddr=0x%" PRIx64 " pfn=0x%" PRIpfn "\n", __FUNCTION__,
		  dom->parms.virt_hypercall, pfn);
    domctl.cmd = XEN_DOMCTL_hypercall_init;
    domctl.domain = dom->guest_domid;
    domctl.u.hypercall_init.gmfn = xc_dom_p2m_guest(dom, pfn);
    rc = do_domctl(dom->guest_xc, &domctl);
    if (0 != rc)
	xc_dom_panic(XC_INTERNAL_ERROR, "%s: HYPERCALL_INIT failed (rc=%d)\n",
		     __FUNCTION__, rc);
    return rc;
}

static int launch_vm(int xc, domid_t domid, void *ctxt)
{
    DECLARE_DOMCTL;
    int rc;

    xc_dom_printf("%s: called, ctxt=%p\n", __FUNCTION__, ctxt);
    memset(&domctl, 0, sizeof(domctl));
    domctl.cmd = XEN_DOMCTL_setvcpucontext;
    domctl.domain = domid;
    domctl.u.vcpucontext.vcpu = 0;
    set_xen_guest_handle(domctl.u.vcpucontext.ctxt, ctxt);
    rc = do_domctl(xc, &domctl);
    if (0 != rc)
	xc_dom_panic(XC_INTERNAL_ERROR,
		     "%s: SETVCPUCONTEXT failed (rc=%d)\n", __FUNCTION__, rc);
    return rc;
}

static int clear_page(struct xc_dom_image *dom, xen_pfn_t pfn)
{
    xen_pfn_t dst;
    int rc;

    if (0 == pfn)
	return 0;

    dst = xc_dom_p2m_host(dom, pfn);
    xc_dom_printf("%s: pfn 0x%" PRIpfn ", mfn 0x%" PRIpfn "\n",
		  __FUNCTION__, pfn, dst);
    rc = xc_clear_domain_page(dom->guest_xc, dom->guest_domid, dst);
    if (0 != rc)
	xc_dom_panic(XC_INTERNAL_ERROR,
		     "%s: xc_clear_domain_page failed (pfn 0x%" PRIpfn
		     ", rc=%d)\n", __FUNCTION__, pfn, rc);
    return rc;
}

/* ------------------------------------------------------------------------ */
/* arch stuff: x86 bits                                                     */

#if defined(__i386__) || defined(__x86_64__)


static int x86_compat(int xc, domid_t domid, char *guest_type)
{
    static const struct {
	char           *guest;
	uint32_t        size;
    } types[] = {
	{ "xen-3.0-x86_32p", 32 },
	{ "xen-3.0-x86_64",  64 },
    };
    DECLARE_DOMCTL;
    int i,rc;

    memset(&domctl, 0, sizeof(domctl));
    domctl.domain = domid;
    domctl.cmd    = XEN_DOMCTL_set_address_size;
    for (i = 0; i < sizeof(types)/sizeof(types[0]); i++)
	if (0 == strcmp(types[i].guest, guest_type))
	    domctl.u.address_size.size = types[i].size;
    if (0 == domctl.u.address_size.size)
	/* nothing to do */
	return 0;

    xc_dom_printf("%s: guest %s, address size %" PRId32 "\n", __FUNCTION__,
		  guest_type, domctl.u.address_size.size);
    rc = do_domctl(xc, &domctl);
    if (0 != rc)
	xc_dom_printf("%s: warning: failed (rc=%d)\n",
		      __FUNCTION__, rc);
    return rc;
}


static int x86_shadow(int xc, domid_t domid)
{
    int rc, mode;

    xc_dom_printf("%s: called\n", __FUNCTION__);

    mode = XEN_DOMCTL_SHADOW_ENABLE_REFCOUNT |
	XEN_DOMCTL_SHADOW_ENABLE_TRANSLATE;

    rc = xc_shadow_control(xc, domid,
			   XEN_DOMCTL_SHADOW_OP_ENABLE,
			   NULL, 0, NULL, mode, NULL);
    if (0 != rc)
    {
	xc_dom_panic(XC_INTERNAL_ERROR,
		     "%s: SHADOW_OP_ENABLE (mode=0x%x) failed (rc=%d)\n",
		     __FUNCTION__, mode, rc);
	return rc;
    }
    xc_dom_printf("%s: shadow enabled (mode=0x%x)\n", __FUNCTION__, mode);
    return rc;
}

static int arch_setup_early(struct xc_dom_image *dom)
{
    int rc = 0;

    x86_compat(dom->guest_xc, dom->guest_domid, dom->guest_type);
    if (xc_dom_feature_translated(dom))
    {
	dom->shadow_enabled = 1;
	rc = x86_shadow(dom->guest_xc, dom->guest_domid);
    }
    return rc;
}

static int arch_setup_middle(struct xc_dom_image *dom)
{
    xc_dom_printf("%s: doing nothing\n", __FUNCTION__);
    return 0;
}

static int arch_setup_late(struct xc_dom_image *dom)
{
    static const struct {
	char *guest;
	unsigned long pgd_type;
    } types[] = {
	{ "xen-3.0-x86_32",  MMUEXT_PIN_L2_TABLE},
	{ "xen-3.0-x86_32p", MMUEXT_PIN_L3_TABLE},
	{ "xen-3.0-x86_64",  MMUEXT_PIN_L4_TABLE},
    };
    unsigned long pgd_type = 0;
    shared_info_t *shared_info;
    xen_pfn_t shinfo;
    int i, rc;

    for (i = 0; i < sizeof(types) / sizeof(types[0]); i++)
	if (0 == strcmp(types[i].guest, dom->guest_type))
	    pgd_type = types[i].pgd_type;

    if (!xc_dom_feature_translated(dom))
    {
	/* paravirtualized guest */
	xc_dom_unmap_one(dom, dom->pgtables_seg.pfn);
	rc = pin_table(dom->guest_xc, pgd_type,
		       xc_dom_p2m_host(dom, dom->pgtables_seg.pfn),
		       dom->guest_domid);
	if (0 != rc)
	{
	    xc_dom_panic(XC_INTERNAL_ERROR,
			 "%s: pin_table failed (pfn 0x%" PRIpfn ", rc=%d)\n",
			 __FUNCTION__, dom->pgtables_seg.pfn, rc);
	    return rc;
	}
	shinfo = dom->shared_info_mfn;
    }
    else
    {
	/* paravirtualized guest with auto-translation */
	struct xen_add_to_physmap xatp;
	int i;

	/* Map shared info frame into guest physmap. */
	xatp.domid = dom->guest_domid;
	xatp.space = XENMAPSPACE_shared_info;
	xatp.idx = 0;
	xatp.gpfn = dom->shared_info_pfn;
	rc = xc_memory_op(dom->guest_xc, XENMEM_add_to_physmap, &xatp);
	if (rc != 0)
	{
	    xc_dom_panic(XC_INTERNAL_ERROR, "%s: mapping shared_info failed "
			 "(pfn=0x%" PRIpfn ", rc=%d)\n",
			 __FUNCTION__, xatp.gpfn, rc);
	    return rc;
	}

	/* Map grant table frames into guest physmap. */
	for (i = 0;; i++)
	{
	    xatp.domid = dom->guest_domid;
	    xatp.space = XENMAPSPACE_grant_table;
	    xatp.idx = i;
	    xatp.gpfn = dom->total_pages + i;
	    rc = xc_memory_op(dom->guest_xc, XENMEM_add_to_physmap, &xatp);
	    if (rc != 0)
	    {
		if (i > 0 && errno == EINVAL)
		{
		    xc_dom_printf("%s: %d grant tables mapped\n", __FUNCTION__,
				  i);
		    break;
		}
		xc_dom_panic(XC_INTERNAL_ERROR,
			     "%s: mapping grant tables failed " "(pfn=0x%"
			     PRIpfn ", rc=%d)\n", __FUNCTION__, xatp.gpfn, rc);
		return rc;
	    }
	}
	shinfo = dom->shared_info_pfn;
    }

    /* setup shared_info page */
    xc_dom_printf("%s: shared_info: pfn 0x%" PRIpfn ", mfn 0x%" PRIpfn "\n",
		  __FUNCTION__, dom->shared_info_pfn, dom->shared_info_mfn);
    shared_info = xc_map_foreign_range(dom->guest_xc, dom->guest_domid,
				       PAGE_SIZE_X86,
				       PROT_READ | PROT_WRITE,
				       shinfo);
    if (NULL == shared_info)
	return -1;
    dom->arch_hooks->shared_info(dom, shared_info);
    munmap(shared_info, PAGE_SIZE_X86);

    return 0;
}

/* ------------------------------------------------------------------------ */
/* arch stuff: ia64                                                         */

#elif defined(__ia64__)

static int arch_setup_early(struct xc_dom_image *dom)
{
    xc_dom_printf("%s: doing nothing\n", __FUNCTION__);
    return 0;
}

static int arch_setup_middle(struct xc_dom_image *dom)
{
    DECLARE_DOMCTL;
    int rc;

    xc_dom_printf("%s: setup firmware\n", __FUNCTION__);

    memset(&domctl, 0, sizeof(domctl));
    domctl.cmd = XEN_DOMCTL_arch_setup;
    domctl.domain = dom->guest_domid;
    domctl.u.arch_setup.flags = 0;

    /* dom->start_info_pfn should be initialized by alloc_magic_pages().
     * However it is called later. So we initialize here.
     */
    dom->start_info_pfn = dom->total_pages - 3;
    domctl.u.arch_setup.bp = (dom->start_info_pfn << PAGE_SHIFT)
	+ sizeof(start_info_t);
    /* 3 = start info page, xenstore page and console page */
    domctl.u.arch_setup.maxmem = (dom->total_pages - 3) << PAGE_SHIFT;
    rc = do_domctl(dom->guest_xc, &domctl);
    return rc;
}

static int arch_setup_late(struct xc_dom_image *dom)
{
    unsigned int page_size = XC_DOM_PAGE_SIZE(dom);
    shared_info_t *shared_info;

    /* setup shared_info page */
    xc_dom_printf("%s: shared_info: mfn 0x%" PRIpfn "\n",
		  __FUNCTION__, dom->shared_info_mfn);
    shared_info = xc_map_foreign_range(dom->guest_xc, dom->guest_domid,
				       page_size,
				       PROT_READ | PROT_WRITE,
				       dom->shared_info_mfn);
    if (NULL == shared_info)
	return -1;
    dom->arch_hooks->shared_info(dom, shared_info);
    munmap(shared_info, page_size);
    return 0;
}

/* ------------------------------------------------------------------------ */
/* arch stuff: powerpc                                                      */

#elif defined(__powerpc64__)

static int arch_setup_early(struct xc_dom_image *dom)
{
    xc_dom_printf("%s: doing nothing\n", __FUNCTION__);
    return 0;
}

static int arch_setup_middle(struct xc_dom_image *dom)
{
    xc_dom_printf("%s: doing nothing\n", __FUNCTION__);
    return 0;
}

static int arch_setup_late(struct xc_dom_image *dom)
{
    start_info_t *si =
	xc_dom_pfn_to_ptr(dom, dom->start_info_pfn, 1);

    xc_dom_printf("%s: TODO: setup devtree\n", __FUNCTION__);

#if 0
    load_devtree(dom->guest_xc,
		 dom->guest_domid,
		 dom->p2m_host,
		 devtree,           // FIXME
		 devtree_addr,      // FIXME
		 dom->ramdisk_seg.vstart,
		 dom->ramdisk_seg.vend - dom->ramdisk_seg.vstart,
		 si,
		 dom->start_info_pfn << PAGE_SHIFT);
#endif
    return rc;
}

/* ------------------------------------------------------------------------ */
/* arch stuff: other                                                        */

#else

static int arch_setup_early(struct xc_dom_image *dom)
{
    xc_dom_printf("%s: doing nothing\n", __FUNCTION__);
    return 0;
}

static int arch_setup_middle(struct xc_dom_image *dom)
{
    xc_dom_printf("%s: doing nothing\n", __FUNCTION__);
    return 0;
}

static int arch_setup_late(struct xc_dom_image *dom)
{
    xc_dom_printf("%s: doing nothing\n", __FUNCTION__);
    return 0;
}

#endif /* arch stuff */

/* ------------------------------------------------------------------------ */

int xc_dom_compat_check(struct xc_dom_image *dom)
{
    xen_capabilities_info_t xen_caps;
    char *item, *ptr;
    int match, found = 0;

    strcpy(xen_caps, dom->xen_caps);
    for (item = strtok_r(xen_caps, " ", &ptr);
	 NULL != item; item = strtok_r(NULL, " ", &ptr))
    {
	match = (0 == strcmp(dom->guest_type, item));
	xc_dom_printf("%s: supported guest type: %s%s\n", __FUNCTION__,
		      item, match ? " <= matches" : "");
	if (match)
	    found++;
    }
    if (!found)
	xc_dom_panic(XC_INVALID_KERNEL,
		     "%s: guest type %s not supported by xen kernel, sorry\n",
		     __FUNCTION__, dom->guest_type);

    return found;
}

int xc_dom_boot_xen_init(struct xc_dom_image *dom, int xc, domid_t domid)
{
    dom->guest_xc = xc;
    dom->guest_domid = domid;

    dom->xen_version = xc_version(dom->guest_xc, XENVER_version, NULL);
    if (xc_version(xc, XENVER_capabilities, &dom->xen_caps) < 0) {
	xc_dom_panic(XC_INTERNAL_ERROR, "can't get xen capabilities");
	return -1;
    }
    xc_dom_printf("%s: ver %d.%d, caps %s\n", __FUNCTION__,
		  dom->xen_version >> 16, dom->xen_version & 0xff,
		  dom->xen_caps);
    return 0;
}

int xc_dom_boot_mem_init(struct xc_dom_image *dom)
{
    long rc;

    xc_dom_printf("%s: called\n", __FUNCTION__);

    if (0 != (rc = arch_setup_early(dom)))
	return rc;

    /* allocate guest memory */
    rc = xc_domain_memory_populate_physmap(dom->guest_xc, dom->guest_domid,
					   dom->total_pages, 0, 0,
					   dom->p2m_host);
    if (0 != rc)
    {
	xc_dom_panic(XC_OUT_OF_MEMORY,
		     "%s: can't allocate low memory for domain\n",
		     __FUNCTION__);
	return rc;
    }

    if (0 != (rc = arch_setup_middle(dom)))
        return rc;

    return 0;
}

void *xc_dom_boot_domU_map(struct xc_dom_image *dom, xen_pfn_t pfn,
			   xen_pfn_t count)
{
    int page_shift = XC_DOM_PAGE_SHIFT(dom);
    privcmd_mmap_entry_t *entries;
    void *ptr;
    int i, rc;

    entries = xc_dom_malloc(dom, count * sizeof(privcmd_mmap_entry_t));
    if (NULL == entries)
    {
	xc_dom_panic(XC_INTERNAL_ERROR,
		     "%s: failed to mmap domU pages 0x%" PRIpfn "+0x%" PRIpfn
		     " [malloc]\n", __FUNCTION__, pfn, count);
	return NULL;
    }

    ptr = mmap(NULL, count << page_shift, PROT_READ | PROT_WRITE,
	       MAP_SHARED, dom->guest_xc, 0);
    if (MAP_FAILED == ptr)
    {
	xc_dom_panic(XC_INTERNAL_ERROR,
		     "%s: failed to mmap domU pages 0x%" PRIpfn "+0x%" PRIpfn
		     " [mmap]\n", __FUNCTION__, pfn, count);
	return NULL;
    }

    for (i = 0; i < count; i++)
    {
	entries[i].va = (uintptr_t) ptr + (i << page_shift);
	entries[i].mfn = xc_dom_p2m_host(dom, pfn + i);
	entries[i].npages = 1;
    }

    rc = xc_map_foreign_ranges(dom->guest_xc, dom->guest_domid, entries, count);
    if (rc < 0)
    {
	xc_dom_panic(XC_INTERNAL_ERROR,
		     "%s: failed to mmap domU pages 0x%" PRIpfn "+0x%" PRIpfn
		     " [xenctl, rc=%d]\n", __FUNCTION__, pfn, count, rc);
	return NULL;
    }
    return ptr;
}

int xc_dom_boot_image(struct xc_dom_image *dom)
{
    DECLARE_DOMCTL;
    void *ctxt;
    int rc;

    xc_dom_printf("%s: called\n", __FUNCTION__);

    /* collect some info */
    domctl.cmd = XEN_DOMCTL_getdomaininfo;
    domctl.domain = dom->guest_domid;
    rc = do_domctl(dom->guest_xc, &domctl);
    if (0 != rc)
    {
	xc_dom_panic(XC_INTERNAL_ERROR,
		     "%s: getdomaininfo failed (rc=%d)\n", __FUNCTION__, rc);
	return rc;
    }
    if (domctl.domain != dom->guest_domid)
    {
	xc_dom_panic(XC_INTERNAL_ERROR,
		     "%s: Huh? domid mismatch (%d != %d)\n", __FUNCTION__,
		     domctl.domain, dom->guest_domid);
	return -1;
    }
    dom->shared_info_mfn = domctl.u.getdomaininfo.shared_info_frame;

    /* sanity checks */
    if (!xc_dom_compat_check(dom))
	return -1;

    /* initial mm setup */
    if (0 != (rc = xc_dom_update_guest_p2m(dom)))
	return rc;
    if (dom->arch_hooks->setup_pgtables)
	if (0 != (rc = dom->arch_hooks->setup_pgtables(dom)))
	    return rc;

    if (0 != (rc = clear_page(dom, dom->console_pfn)))
	return rc;
    if (0 != (rc = clear_page(dom, dom->xenstore_pfn)))
	return rc;

    /* start info page */
    if (dom->arch_hooks->start_info)
	dom->arch_hooks->start_info(dom);

    /* hypercall page */
    if (0 != (rc = setup_hypercall_page(dom)))
	return rc;
    xc_dom_log_memory_footprint(dom);

    /* misc x86 stuff */
    if (0 != (rc = arch_setup_late(dom)))
	return rc;

    /* let the vm run */
    ctxt = xc_dom_malloc(dom, PAGE_SIZE * 2 /* FIXME */ );
    memset(ctxt, 0, PAGE_SIZE * 2);
    if (0 != (rc = dom->arch_hooks->vcpu(dom, ctxt)))
	return rc;
    xc_dom_unmap_all(dom);
    rc = launch_vm(dom->guest_xc, dom->guest_domid, ctxt);

    return rc;
}