aboutsummaryrefslogtreecommitdiffstats
path: root/tools/libxc/xc_domain.c
blob: 51117117f407d985f2d9b79d556b1df48e24a93f (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
/******************************************************************************
 * xc_domain.c
 *
 * API for manipulating and obtaining information on domains.
 *
 * Copyright (c) 2003, K A Fraser.
 */

#include "xc_private.h"
#include <xen/memory.h>

int xc_domain_create(int xc_handle,
                     uint32_t ssidref,
                     xen_domain_handle_t handle,
                     uint32_t *pdomid)
{
    int err;
    DECLARE_DOM0_OP;

    op.cmd = DOM0_CREATEDOMAIN;
    op.u.createdomain.domain = (domid_t)*pdomid;
    op.u.createdomain.ssidref = ssidref;
    memcpy(op.u.createdomain.handle, handle, sizeof(xen_domain_handle_t));
    if ( (err = do_dom0_op(xc_handle, &op)) != 0 )
        return err;

    *pdomid = (uint16_t)op.u.createdomain.domain;
    return 0;
}


int xc_domain_pause(int xc_handle,
                    uint32_t domid)
{
    DECLARE_DOM0_OP;
    op.cmd = DOM0_PAUSEDOMAIN;
    op.u.pausedomain.domain = (domid_t)domid;
    return do_dom0_op(xc_handle, &op);
}


int xc_domain_unpause(int xc_handle,
                      uint32_t domid)
{
    DECLARE_DOM0_OP;
    op.cmd = DOM0_UNPAUSEDOMAIN;
    op.u.unpausedomain.domain = (domid_t)domid;
    return do_dom0_op(xc_handle, &op);
}


int xc_domain_destroy(int xc_handle,
                      uint32_t domid)
{
    DECLARE_DOM0_OP;
    op.cmd = DOM0_DESTROYDOMAIN;
    op.u.destroydomain.domain = (domid_t)domid;
    return do_dom0_op(xc_handle, &op);
}

int xc_domain_shutdown(int xc_handle,
                       uint32_t domid,
                       int reason)
{
    int ret = -1;
    sched_remote_shutdown_t arg;
    DECLARE_HYPERCALL;

    hypercall.op     = __HYPERVISOR_sched_op;
    hypercall.arg[0] = (unsigned long)SCHEDOP_remote_shutdown;
    hypercall.arg[1] = (unsigned long)&arg;
    arg.domain_id = domid;
    arg.reason = reason;

    if ( mlock(&arg, sizeof(arg)) != 0 )
    {
        PERROR("Could not lock memory for Xen hypercall");
        goto out1;
    }

    ret = do_xen_hypercall(xc_handle, &hypercall);

    safe_munlock(&arg, sizeof(arg));

 out1:
    return ret;
}


int xc_vcpu_setaffinity(int xc_handle,
                        uint32_t domid,
                        int vcpu,
                        cpumap_t cpumap)
{
    DECLARE_DOM0_OP;
    op.cmd = DOM0_SETVCPUAFFINITY;
    op.u.setvcpuaffinity.domain  = (domid_t)domid;
    op.u.setvcpuaffinity.vcpu    = vcpu;
    op.u.setvcpuaffinity.cpumap  = cpumap;
    return do_dom0_op(xc_handle, &op);
}


int xc_domain_getinfo(int xc_handle,
                      uint32_t first_domid,
                      unsigned int max_doms,
                      xc_dominfo_t *info)
{
    unsigned int nr_doms;
    uint32_t next_domid = first_domid;
    DECLARE_DOM0_OP;
    int rc = 0;

    memset(info, 0, max_doms*sizeof(xc_dominfo_t));

    for ( nr_doms = 0; nr_doms < max_doms; nr_doms++ )
    {
        op.cmd = DOM0_GETDOMAININFO;
        op.u.getdomaininfo.domain = (domid_t)next_domid;
        if ( (rc = do_dom0_op(xc_handle, &op)) < 0 )
            break;
        info->domid      = (uint16_t)op.u.getdomaininfo.domain;

        info->dying    = !!(op.u.getdomaininfo.flags & DOMFLAGS_DYING);
        info->shutdown = !!(op.u.getdomaininfo.flags & DOMFLAGS_SHUTDOWN);
        info->paused   = !!(op.u.getdomaininfo.flags & DOMFLAGS_PAUSED);
        info->blocked  = !!(op.u.getdomaininfo.flags & DOMFLAGS_BLOCKED);
        info->running  = !!(op.u.getdomaininfo.flags & DOMFLAGS_RUNNING);

        info->shutdown_reason =
            (op.u.getdomaininfo.flags>>DOMFLAGS_SHUTDOWNSHIFT) &
            DOMFLAGS_SHUTDOWNMASK;

        if ( info->shutdown && (info->shutdown_reason == SHUTDOWN_crash) )
        {
            info->shutdown = 0;
            info->crashed  = 1;
        }

        info->ssidref  = op.u.getdomaininfo.ssidref;
        info->nr_pages = op.u.getdomaininfo.tot_pages;
        info->max_memkb = op.u.getdomaininfo.max_pages << (PAGE_SHIFT - 10);
        info->shared_info_frame = op.u.getdomaininfo.shared_info_frame;
        info->cpu_time = op.u.getdomaininfo.cpu_time;
        info->nr_online_vcpus = op.u.getdomaininfo.nr_online_vcpus;
        info->max_vcpu_id = op.u.getdomaininfo.max_vcpu_id;

        memcpy(info->handle, op.u.getdomaininfo.handle,
               sizeof(xen_domain_handle_t));

        next_domid = (uint16_t)op.u.getdomaininfo.domain + 1;
        info++;
    }

    if( !nr_doms ) return rc;

    return nr_doms;
}

int xc_domain_getinfolist(int xc_handle,
                          uint32_t first_domain,
                          unsigned int max_domains,
                          xc_domaininfo_t *info)
{
    int ret = 0;
    DECLARE_DOM0_OP;

    if ( mlock(info, max_domains*sizeof(xc_domaininfo_t)) != 0 )
        return -1;

    op.cmd = DOM0_GETDOMAININFOLIST;
    op.u.getdomaininfolist.first_domain = first_domain;
    op.u.getdomaininfolist.max_domains  = max_domains;
    set_xen_guest_handle(op.u.getdomaininfolist.buffer, info);

    if ( xc_dom0_op(xc_handle, &op) < 0 )
        ret = -1;
    else
        ret = op.u.getdomaininfolist.num_domains;

    if ( munlock(info, max_domains*sizeof(xc_domaininfo_t)) != 0 )
        ret = -1;

    return ret;
}

int xc_vcpu_getcontext(int xc_handle,
                               uint32_t domid,
                               uint32_t vcpu,
                               vcpu_guest_context_t *ctxt)
{
    int rc;
    DECLARE_DOM0_OP;

    op.cmd = DOM0_GETVCPUCONTEXT;
    op.u.getvcpucontext.domain = (domid_t)domid;
    op.u.getvcpucontext.vcpu   = (uint16_t)vcpu;
    set_xen_guest_handle(op.u.getvcpucontext.ctxt, ctxt);

    if ( (rc = mlock(ctxt, sizeof(*ctxt))) != 0 )
        return rc;

    rc = do_dom0_op(xc_handle, &op);

    safe_munlock(ctxt, sizeof(*ctxt));

    return rc;
}


int xc_shadow_control(int xc_handle,
                      uint32_t domid,
                      unsigned int sop,
                      unsigned long *dirty_bitmap,
                      unsigned long pages,
                      xc_shadow_control_stats_t *stats )
{
    int rc;
    DECLARE_DOM0_OP;
    op.cmd = DOM0_SHADOW_CONTROL;
    op.u.shadow_control.domain = (domid_t)domid;
    op.u.shadow_control.op     = sop;
    set_xen_guest_handle(op.u.shadow_control.dirty_bitmap, dirty_bitmap);
    op.u.shadow_control.pages  = pages;

    rc = do_dom0_op(xc_handle, &op);

    if ( stats )
        memcpy(stats, &op.u.shadow_control.stats,
               sizeof(xc_shadow_control_stats_t));

    return (rc == 0) ? op.u.shadow_control.pages : rc;
}

int xc_domain_setcpuweight(int xc_handle,
                           uint32_t domid,
                           float weight)
{
    int sched_id;
    int ret;

    /* Figure out which scheduler is currently used: */
    if ( (ret = xc_sched_id(xc_handle, &sched_id)) != 0 )
        return ret;

    switch ( sched_id )
    {
        case SCHED_BVT:
        {
            uint32_t mcuadv;
            int warpback;
            int32_t warpvalue;
            long long warpl;
            long long warpu;

            /* Preserve all the scheduling parameters apart
               of MCU advance. */
            if ( (ret = xc_bvtsched_domain_get(
                xc_handle, domid, &mcuadv,
                &warpback, &warpvalue, &warpl, &warpu)) != 0 )
                return ret;

            /* The MCU advance is inverse of the weight.
               Default value of the weight is 1, default mcuadv 10.
               The scaling factor is therefore 10. */
            if ( weight > 0 )
                mcuadv = 10 / weight;

            ret = xc_bvtsched_domain_set(xc_handle, domid, mcuadv,
                                         warpback, warpvalue, warpl, warpu);
            break;
        }
    }

    return ret;
}

int xc_domain_setmaxmem(int xc_handle,
                        uint32_t domid,
                        unsigned int max_memkb)
{
    DECLARE_DOM0_OP;
    op.cmd = DOM0_SETDOMAINMAXMEM;
    op.u.setdomainmaxmem.domain = (domid_t)domid;
    op.u.setdomainmaxmem.max_memkb = max_memkb;
    return do_dom0_op(xc_handle, &op);
}

int xc_domain_set_time_offset(int xc_handle,
                              uint32_t domid,
                              int32_t time_offset_seconds)
{
    DECLARE_DOM0_OP;
    op.cmd = DOM0_SETTIMEOFFSET;
    op.u.settimeoffset.domain = (domid_t)domid;
    op.u.settimeoffset.time_offset_seconds = time_offset_seconds;
    return do_dom0_op(xc_handle, &op);
}

int xc_domain_memory_increase_reservation(int xc_handle,
                                          uint32_t domid,
                                          unsigned long nr_extents,
                                          unsigned int extent_order,
                                          unsigned int address_bits,
                                          xen_pfn_t *extent_start)
{
    int err;
    struct xen_memory_reservation reservation = {
        .nr_extents   = nr_extents,
        .extent_order = extent_order,
        .address_bits = address_bits,
        .domid        = domid
    };

    /* may be NULL */
    set_xen_guest_handle(reservation.extent_start, extent_start);

    err = xc_memory_op(xc_handle, XENMEM_increase_reservation, &reservation);
    if ( err == nr_extents )
        return 0;

    if ( err > 0 )
    {
        DPRINTF("Failed allocation for dom %d: "
                "%ld pages order %d addr_bits %d\n",
                domid, nr_extents, extent_order, address_bits);
        errno = ENOMEM;
        err = -1;
    }

    return err;
}

int xc_domain_memory_decrease_reservation(int xc_handle,
                                          uint32_t domid,
                                          unsigned long nr_extents,
                                          unsigned int extent_order,
                                          xen_pfn_t *extent_start)
{
    int err;
    struct xen_memory_reservation reservation = {
        .nr_extents   = nr_extents,
        .extent_order = extent_order,
        .address_bits = 0,
        .domid        = domid
    };

    set_xen_guest_handle(reservation.extent_start, extent_start);

    if ( extent_start == NULL )
    {
        DPRINTF("decrease_reservation extent_start is NULL!\n");
        errno = EINVAL;
        return -1;
    }

    err = xc_memory_op(xc_handle, XENMEM_decrease_reservation, &reservation);
    if ( err == nr_extents )
        return 0;

    if ( err > 0 )
    {
        DPRINTF("Failed deallocation for dom %d: %ld pages order %d\n",
                domid, nr_extents, extent_order);
        errno = EBUSY;
        err = -1;
    }

    return err;
}

int xc_domain_memory_populate_physmap(int xc_handle,
                                          uint32_t domid,
                                          unsigned long nr_extents,
                                          unsigned int extent_order,
                                          unsigned int address_bits,
                                          xen_pfn_t *extent_start)
{
    int err;
    struct xen_memory_reservation reservation = {
        .nr_extents   = nr_extents,
        .extent_order = extent_order,
        .address_bits = address_bits,
        .domid        = domid
    };
    set_xen_guest_handle(reservation.extent_start, extent_start);

    err = xc_memory_op(xc_handle, XENMEM_populate_physmap, &reservation);
    if ( err == nr_extents )
        return 0;

    if ( err > 0 )
    {
        DPRINTF("Failed deallocation for dom %d: %ld pages order %d\n",
                domid, nr_extents, extent_order);
        errno = EBUSY;
        err = -1;
    }

    return err;
}

int xc_domain_translate_gpfn_list(int xc_handle,
                                  uint32_t domid,
                                  unsigned long nr_gpfns,
                                  xen_pfn_t *gpfn_list,
                                  xen_pfn_t *mfn_list)
{
    struct xen_translate_gpfn_list op = {
        .domid        = domid,
        .nr_gpfns     = nr_gpfns,
    };
    set_xen_guest_handle(op.gpfn_list, gpfn_list);
    set_xen_guest_handle(op.mfn_list, mfn_list);

    return xc_memory_op(xc_handle, XENMEM_translate_gpfn_list, &op);
}

int xc_domain_max_vcpus(int xc_handle, uint32_t domid, unsigned int max)
{
    DECLARE_DOM0_OP;
    op.cmd = DOM0_MAX_VCPUS;
    op.u.max_vcpus.domain = (domid_t)domid;
    op.u.max_vcpus.max    = max;
    return do_dom0_op(xc_handle, &op);
}

int xc_domain_sethandle(int xc_handle, uint32_t domid,
                        xen_domain_handle_t handle)
{
    DECLARE_DOM0_OP;
    op.cmd = DOM0_SETDOMAINHANDLE;
    op.u.setdomainhandle.domain = (domid_t)domid;
    memcpy(op.u.setdomainhandle.handle, handle, sizeof(xen_domain_handle_t));
    return do_dom0_op(xc_handle, &op);
}

int xc_vcpu_getinfo(int xc_handle,
                    uint32_t domid,
                    uint32_t vcpu,
                    xc_vcpuinfo_t *info)
{
    int rc;
    DECLARE_DOM0_OP;
    op.cmd = DOM0_GETVCPUINFO;
    op.u.getvcpuinfo.domain = (domid_t)domid;
    op.u.getvcpuinfo.vcpu   = (uint16_t)vcpu;

    rc = do_dom0_op(xc_handle, &op);

    memcpy(info, &op.u.getvcpuinfo, sizeof(*info));

    return rc;
}

int xc_domain_ioport_permission(int xc_handle,
                                uint32_t domid,
                                uint32_t first_port,
                                uint32_t nr_ports,
                                uint32_t allow_access)
{
    DECLARE_DOM0_OP;

    op.cmd = DOM0_IOPORT_PERMISSION;
    op.u.ioport_permission.domain = (domid_t)domid;
    op.u.ioport_permission.first_port = first_port;
    op.u.ioport_permission.nr_ports = nr_ports;
    op.u.ioport_permission.allow_access = allow_access;

    return do_dom0_op(xc_handle, &op);
}

int xc_vcpu_setcontext(int xc_handle,
                       uint32_t domid,
                       uint32_t vcpu,
                       vcpu_guest_context_t *ctxt)
{
    dom0_op_t op;
    int rc;

    op.cmd = DOM0_SETVCPUCONTEXT;
    op.u.setvcpucontext.domain = domid;
    op.u.setvcpucontext.vcpu = vcpu;
    set_xen_guest_handle(op.u.setvcpucontext.ctxt, ctxt);

    if ( (rc = mlock(ctxt, sizeof(*ctxt))) != 0 )
        return rc;

    rc = do_dom0_op(xc_handle, &op);

    safe_munlock(ctxt, sizeof(*ctxt));

    return rc;

}

int xc_domain_irq_permission(int xc_handle,
                             uint32_t domid,
                             uint8_t pirq,
                             uint8_t allow_access)
{
    dom0_op_t op;

    op.cmd = DOM0_IRQ_PERMISSION;
    op.u.irq_permission.domain = domid;
    op.u.irq_permission.pirq = pirq;
    op.u.irq_permission.allow_access = allow_access;

    return do_dom0_op(xc_handle, &op);
}

int xc_domain_iomem_permission(int xc_handle,
                               uint32_t domid,
                               unsigned long first_mfn,
                               unsigned long nr_mfns,
                               uint8_t allow_access)
{
    dom0_op_t op;

    op.cmd = DOM0_IOMEM_PERMISSION;
    op.u.iomem_permission.domain = domid;
    op.u.iomem_permission.first_mfn = first_mfn;
    op.u.iomem_permission.nr_mfns = nr_mfns;
    op.u.iomem_permission.allow_access = allow_access;

    return do_dom0_op(xc_handle, &op);
}

/*
 * Local variables:
 * mode: C
 * c-set-style: "BSD"
 * c-basic-offset: 4
 * tab-width: 4
 * indent-tabs-mode: nil
 * End:
 */