xen_internal.c 38.3 KB
Newer Older
1 2 3
/*
 * xen_internal.c: direct access to Xen hypervisor level
 *
D
Daniel Veillard 已提交
4
 * Copyright (C) 2005, 2006 Red Hat, Inc.
5 6 7 8 9 10 11 12
 *
 * See COPYING.LIB for the License of this software
 *
 * Daniel Veillard <veillard@redhat.com>
 */

#include <stdio.h>
#include <string.h>
13
/* required for uint8_t, uint32_t, etc ... */
14 15 16 17 18 19 20
#include <stdint.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
21
#include <limits.h>
22 23 24
#include <stdint.h>

/* required for dom0_getdomaininfo_t */
25
#include <xen/dom0_ops.h>
26
#include <xen/version.h>
27
#include <xen/xen.h>
28
#include <xen/linux/privcmd.h>
29

30 31 32 33 34 35 36
/* #define DEBUG */
/*
 * so far there is 2 versions of the structures usable for doing 
 * hypervisor calls.
 */
/* the old one */
typedef struct v0_hypercall_struct {
37 38
    unsigned long op;
    unsigned long arg[5];
39 40 41 42 43 44 45 46 47 48 49 50
} v0_hypercall_t;
#define XEN_V0_IOCTL_HYPERCALL_CMD \
        _IOC(_IOC_NONE, 'P', 0, sizeof(v0_hypercall_t))

/* the new one */
typedef struct v1_hypercall_struct
{
    uint64_t op;
    uint64_t arg[5];
} v1_hypercall_t;
#define XEN_V1_IOCTL_HYPERCALL_CMD \
	 _IOC(_IOC_NONE, 'P', 0, sizeof(v1_hypercall_t))
51

52 53 54 55 56 57 58 59
typedef v1_hypercall_t hypercall_t;

#ifndef __HYPERVISOR_sysctl
#define __HYPERVISOR_sysctl 35
#endif
#ifndef __HYPERVISOR_domctl
#define __HYPERVISOR_domctl 36
#endif
60

61 62
static int xen_ioctl_hypercall_cmd = 0;
static int initialized = 0;
63
static int in_init = 0;
64
static int hv_version = 0;
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
static int hypervisor_version = 2;
static int sys_interface_version = -1;
static int dom_interface_version = -1;

/*
 * The content of the structures for a getdomaininfolist system hypercall
 */
#ifndef DOMFLAGS_DYING
#define DOMFLAGS_DYING     (1<<0) /* Domain is scheduled to die.             */
#define DOMFLAGS_SHUTDOWN  (1<<2) /* The guest OS has shut down.             */
#define DOMFLAGS_PAUSED    (1<<3) /* Currently paused by control software.   */
#define DOMFLAGS_BLOCKED   (1<<4) /* Currently blocked pending an event.     */
#define DOMFLAGS_RUNNING   (1<<5) /* Domain is currently running.            */
#define DOMFLAGS_CPUMASK      255 /* CPU to which this domain is bound.      */
#define DOMFLAGS_CPUSHIFT       8
#define DOMFLAGS_SHUTDOWNMASK 255 /* DOMFLAGS_SHUTDOWN guest-supplied code.  */
#define DOMFLAGS_SHUTDOWNSHIFT 16
#endif

#define XEN_V0_OP_GETDOMAININFOLIST	38
#define XEN_V1_OP_GETDOMAININFOLIST	38
#define XEN_V2_OP_GETDOMAININFOLIST	6

struct xen_v0_getdomaininfo {
    domid_t  domain;	/* the domain number */
    uint32_t flags;	/* falgs, see before */
    uint64_t tot_pages;	/* total number of pages used */
    uint64_t max_pages;	/* maximum number of pages allowed */
    uint64_t shared_info_frame;  /* MFN of shared_info struct */
    uint64_t cpu_time;  /* CPU time used */
    uint32_t nr_online_vcpus;  /* Number of VCPUs currently online. */
    uint32_t max_vcpu_id; /* Maximum VCPUID in use by this domain. */
    uint32_t ssidref;
    xen_domain_handle_t handle;
};
typedef struct xen_v0_getdomaininfo xen_v0_getdomaininfo;

struct xen_v0_getdomaininfolist {
    domid_t   first_domain;
    uint32_t  max_domains;
    struct xen_v0_getdomaininfo *buffer;
    uint32_t  num_domains;
};
typedef struct xen_v0_getdomaininfolist xen_v0_getdomaininfolist;

struct xen_v0_domainop {
    domid_t   domain;
};
typedef struct xen_v0_domainop xen_v0_domainop;

/*
 * The informations for a destroydomain system hypercall
 */
#define XEN_V0_OP_DESTROYDOMAIN	9
#define XEN_V1_OP_DESTROYDOMAIN	9
#define XEN_V2_OP_DESTROYDOMAIN	2

/*
 * The informations for a pausedomain system hypercall
 */
#define XEN_V0_OP_PAUSEDOMAIN	10
#define XEN_V1_OP_PAUSEDOMAIN	10
#define XEN_V2_OP_PAUSEDOMAIN	3

/*
 * The informations for an unpausedomain system hypercall
 */
#define XEN_V0_OP_UNPAUSEDOMAIN	11
#define XEN_V1_OP_UNPAUSEDOMAIN	11
#define XEN_V2_OP_UNPAUSEDOMAIN	4

/*
 * The informations for an setmaxmem system hypercall
 */
#define XEN_V0_OP_SETMAXMEM	28
#define XEN_V1_OP_SETMAXMEM	28
#define XEN_V2_OP_SETMAXMEM	14

struct xen_v0_setmaxmem {
    domid_t	domain;
    uint64_t	maxmem;
};
typedef struct xen_v0_setmaxmem xen_v0_setmaxmem;
typedef struct xen_v0_setmaxmem xen_v1_setmaxmem;

struct xen_v2_setmaxmem {
    uint64_t	maxmem;
};
typedef struct xen_v2_setmaxmem xen_v2_setmaxmem;

/*
 * The informations for an setmaxvcpu system hypercall
 */
#define XEN_V0_OP_SETMAXVCPU	41
#define XEN_V1_OP_SETMAXVCPU	41
#define XEN_V2_OP_SETMAXVCPU	15

struct xen_v0_setmaxvcpu {
    domid_t	domain;
    uint32_t	maxvcpu;
};
typedef struct xen_v0_setmaxvcpu xen_v0_setmaxvcpu;
typedef struct xen_v0_setmaxvcpu xen_v1_setmaxvcpu;

struct xen_v2_setmaxvcpu {
    uint32_t	maxvcpu;
};
typedef struct xen_v2_setmaxvcpu xen_v2_setmaxvcpu;

/*
 * The informations for an setvcpumap system hypercall
 * Note that between 1 and 2 the limitation to 64 physical CPU was lifted
 * hence the difference in structures
 */
#define XEN_V0_OP_SETVCPUMAP	20
#define XEN_V1_OP_SETVCPUMAP	20
#define XEN_V2_OP_SETVCPUMAP	9

struct xen_v0_setvcpumap {
    domid_t	domain;
    uint32_t	vcpu;
    cpumap_t    cpumap;
};
typedef struct xen_v0_setvcpumap xen_v0_setvcpumap;
typedef struct xen_v0_setvcpumap xen_v1_setvcpumap;

struct xen_v2_cpumap {
    uint8_t    *bitmap;
    uint32_t    nr_cpus;
};
struct xen_v2_setvcpumap {
    uint32_t	vcpu;
    struct xen_v2_cpumap cpumap;
};
typedef struct xen_v2_setvcpumap xen_v2_setvcpumap;

/*
 * The hypercall operation structures also have changed on
 * changeset 86d26e6ec89b
 */
/* the old structure */
struct xen_op_v0 {
    uint32_t cmd;
    uint32_t interface_version;
    union {
        xen_v0_getdomaininfolist getdomaininfolist;
	xen_v0_domainop          domain;
	xen_v0_setmaxmem         setmaxmem;
	xen_v0_setmaxvcpu        setmaxvcpu;
	xen_v0_setvcpumap        setvcpumap;
	uint8_t padding[128];
    } u;
};
typedef struct xen_op_v0 xen_op_v0;
typedef struct xen_op_v0 xen_op_v1;

/* the new structure for systems operations */
struct xen_op_v2_sys {
    uint32_t cmd;
    uint32_t interface_version;
    union {
        xen_v0_getdomaininfolist getdomaininfolist;
	uint8_t padding[128];
    } u;
};
typedef struct xen_op_v2_sys xen_op_v2_sys;

/* the new structure for domains operation */
struct xen_op_v2_dom {
    uint32_t cmd;
    uint32_t interface_version;
    domid_t  domain;
    union {
	xen_v2_setmaxmem         setmaxmem;
	xen_v2_setmaxvcpu        setmaxvcpu;
	xen_v2_setvcpumap        setvcpumap;
	uint8_t padding[128];
    } u;
};
typedef struct xen_op_v2_dom xen_op_v2_dom;
245 246

#include "internal.h"
247
#include "driver.h"
248 249 250 251
#include "xen_internal.h"

#define XEN_HYPERVISOR_SOCKET "/proc/xen/privcmd"

252
#ifndef PROXY
253
static const char * xenHypervisorGetType(virConnectPtr conn);
254
static unsigned long xenHypervisorGetMaxMemory(virDomainPtr domain);
255
#endif
256
static int xenHypervisorInit(void);
257

258
#ifndef PROXY
259
static virDriver xenHypervisorDriver = {
260
    VIR_DRV_XEN_HYPERVISOR,
261
    "Xen",
262 263 264
    (DOM0_INTERFACE_VERSION >> 24) * 1000000 +
    ((DOM0_INTERFACE_VERSION >> 16) & 0xFF) * 1000 +
    (DOM0_INTERFACE_VERSION & 0xFFFF),
265
    xenHypervisorInit, /* init */
266 267
    xenHypervisorOpen, /* open */
    xenHypervisorClose, /* close */
268
    xenHypervisorGetType, /* type */
269
    xenHypervisorGetVersion, /* version */
270
    NULL, /* nodeGetInfo */
271 272
    xenHypervisorListDomains, /* listDomains */
    xenHypervisorNumOfDomains, /* numOfDomains */
273 274 275 276 277 278 279
    NULL, /* domainCreateLinux */
    NULL, /* domainLookupByID */
    NULL, /* domainLookupByUUID */
    NULL, /* domainLookupByName */
    xenHypervisorPauseDomain, /* domainSuspend */
    xenHypervisorResumeDomain, /* domainResume */
    NULL, /* domainShutdown */
280
    NULL, /* domainReboot */
281 282 283 284 285 286
    xenHypervisorDestroyDomain, /* domainDestroy */
    NULL, /* domainFree */
    NULL, /* domainGetName */
    NULL, /* domainGetID */
    NULL, /* domainGetUUID */
    NULL, /* domainGetOSType */
287
    xenHypervisorGetMaxMemory, /* domainGetMaxMemory */
288
    xenHypervisorSetMaxMemory, /* domainSetMaxMemory */
289
    NULL, /* domainSetMemory */
290 291
    xenHypervisorGetDomainInfo, /* domainGetInfo */
    NULL, /* domainSave */
292 293 294
    NULL, /* domainRestore */
    xenHypervisorSetVcpus, /* domainSetVcpus */
    xenHypervisorPinVcpu, /* domainPinVcpu */
295 296
    xenHypervisorGetVcpus, /* domainGetVcpus */
    NULL, /* domainDumpXML */
297 298 299 300 301
    NULL, /* listDefinedDomains */
    NULL, /* numOfDefinedDomains */
    NULL, /* domainCreate */
    NULL, /* domainDefineXML */
    NULL, /* domainUndefine */
302
};
303
#endif /* !PROXY */
304

305 306 307 308 309 310 311 312 313
/**
 * virXenError:
 * @conn: the connection if available
 * @error: the error number
 * @info: extra information string
 *
 * Handle an error at the xend daemon interface
 */
static void
314 315
virXenError(virErrorNumber error, const char *info, int value)
{
316
    const char *errmsg;
317

318
    if ((error == VIR_ERR_OK) || (in_init != 0))
319 320 321 322
        return;

    errmsg = __virErrorMsg(error, info);
    __virRaiseError(NULL, NULL, VIR_FROM_XEN, error, VIR_ERR_ERROR,
323
                    errmsg, info, NULL, value, 0, errmsg, info, value);
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 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
/**
 * xenHypervisorDoV0Op:
 * @handle: the handle to the Xen hypervisor
 * @op: pointer to the hyperviros operation structure
 *
 * Do an hypervisor operation though the old interface,
 * this leads to an hypervisor call through ioctl.
 *
 * Returns 0 in case of success and -1 in case of error.
 */
static int
xenHypervisorDoV0Op(int handle, xen_op_v0 * op)
{
    int ret;
    v0_hypercall_t hc;

    memset(&hc, 0, sizeof(hc));
    op->interface_version = hv_version << 8;
    hc.op = __HYPERVISOR_dom0_op;
    hc.arg[0] = (unsigned long) op;

    if (mlock(op, sizeof(dom0_op_t)) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " locking", sizeof(*op));
        return (-1);
    }

    ret = ioctl(handle, xen_ioctl_hypercall_cmd, (unsigned long) &hc);
    if (ret < 0) {
        virXenError(VIR_ERR_XEN_CALL, " ioctl ", xen_ioctl_hypercall_cmd);
    }

    if (munlock(op, sizeof(dom0_op_t)) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " releasing", sizeof(*op));
        ret = -1;
    }

    if (ret < 0)
        return (-1);

    return (0);
}
/**
 * xenHypervisorDoV1Op:
 * @handle: the handle to the Xen hypervisor
 * @op: pointer to the hyperviros operation structure
 *
 * Do an hypervisor v1 operation, this leads to an hypervisor call through
 * ioctl.
 *
 * Returns 0 in case of success and -1 in case of error.
 */
static int
xenHypervisorDoV1Op(int handle, xen_op_v1* op)
{
    int ret;
    hypercall_t hc;

    memset(&hc, 0, sizeof(hc));
    op->interface_version = DOM0_INTERFACE_VERSION;
    hc.op = __HYPERVISOR_dom0_op;
    hc.arg[0] = (unsigned long) op;

    if (mlock(op, sizeof(dom0_op_t)) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " locking", sizeof(*op));
        return (-1);
    }

    ret = ioctl(handle, xen_ioctl_hypercall_cmd, (unsigned long) &hc);
    if (ret < 0) {
        virXenError(VIR_ERR_XEN_CALL, " ioctl ", xen_ioctl_hypercall_cmd);
    }

    if (munlock(op, sizeof(dom0_op_t)) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " releasing", sizeof(*op));
        ret = -1;
    }

    if (ret < 0)
        return (-1);

    return (0);
}

/**
 * xenHypervisorDoV2Sys:
 * @handle: the handle to the Xen hypervisor
 * @op: pointer to the hypervisor operation structure
 *
 * Do an hypervisor v2 stsyem operation, this leads to an hypervisor
 * call through ioctl.
 *
 * Returns 0 in case of success and -1 in case of error.
 */
static int
xenHypervisorDoV2Sys(int handle, xen_op_v2_sys* op)
{
    int ret;
    hypercall_t hc;

    memset(&hc, 0, sizeof(hc));
    op->interface_version = sys_interface_version;
    hc.op = __HYPERVISOR_sysctl;
    hc.arg[0] = (unsigned long) op;

    if (mlock(op, sizeof(dom0_op_t)) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " locking", sizeof(*op));
        return (-1);
    }

    ret = ioctl(handle, xen_ioctl_hypercall_cmd, (unsigned long) &hc);
    if (ret < 0) {
        virXenError(VIR_ERR_XEN_CALL, " ioctl ", xen_ioctl_hypercall_cmd);
    }

    if (munlock(op, sizeof(dom0_op_t)) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " releasing", sizeof(*op));
        ret = -1;
    }

    if (ret < 0)
        return (-1);

    return (0);
}

/**
 * xenHypervisorDoV2Dom:
 * @handle: the handle to the Xen hypervisor
 * @op: pointer to the hypervisor domain operation structure
 *
 * Do an hypervisor v2 domain operation, this leads to an hypervisor
 * call through ioctl.
 *
 * Returns 0 in case of success and -1 in case of error.
 */
static int
xenHypervisorDoV2Dom(int handle, xen_op_v2_dom* op)
{
    int ret;
    hypercall_t hc;

    memset(&hc, 0, sizeof(hc));
    op->interface_version = dom_interface_version;
    hc.op = __HYPERVISOR_domctl;
    hc.arg[0] = (unsigned long) op;

    if (mlock(op, sizeof(dom0_op_t)) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " locking", sizeof(*op));
        return (-1);
    }

    ret = ioctl(handle, xen_ioctl_hypercall_cmd, (unsigned long) &hc);
    if (ret < 0) {
        virXenError(VIR_ERR_XEN_CALL, " ioctl ", xen_ioctl_hypercall_cmd);
    }

    if (munlock(op, sizeof(dom0_op_t)) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " releasing", sizeof(*op));
        ret = -1;
    }

    if (ret < 0)
        return (-1);

    return (0);
}

/**
 * virXen_getdomaininfolist:
 * @handle: the hypervisor handle
 * @first_domain: first domain in the range
 * @maxids: maximum number of domains to list
 * @dominfos: output structures
 *
 * Do a low level hypercall to list existing domains informations
 *
 * Returns the number of domains or -1 in case of failure
 */
static int
virXen_getdomaininfolist(int handle, int first_domain, int maxids,
                         xen_v0_getdomaininfo *dominfos)
{
    int ret = -1;

    if (mlock(dominfos, sizeof(xen_v0_getdomaininfo) * maxids) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " locking",
                    sizeof(xen_v0_getdomaininfo) * maxids);
        return (-1);
    }
    if (hypervisor_version > 1) {
        xen_op_v2_sys op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V2_OP_GETDOMAININFOLIST;
	op.u.getdomaininfolist.first_domain = (domid_t) first_domain;
	op.u.getdomaininfolist.max_domains = maxids;
	op.u.getdomaininfolist.buffer = dominfos;
	op.u.getdomaininfolist.num_domains = maxids;
	ret = xenHypervisorDoV2Sys(handle, &op);
	if (ret == 0)
	    ret = op.u.getdomaininfolist.num_domains;
    } else if (hypervisor_version == 1) {
        xen_op_v1 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V1_OP_GETDOMAININFOLIST;
	op.u.getdomaininfolist.first_domain = (domid_t) first_domain;
	op.u.getdomaininfolist.max_domains = maxids;
	op.u.getdomaininfolist.buffer = dominfos;
	op.u.getdomaininfolist.num_domains = maxids;
	ret = xenHypervisorDoV1Op(handle, &op);
	if (ret == 0)
	    ret = op.u.getdomaininfolist.num_domains;
    } else if (hypervisor_version == 0) {
        xen_op_v0 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V0_OP_GETDOMAININFOLIST;
	op.u.getdomaininfolist.first_domain = (domid_t) first_domain;
	op.u.getdomaininfolist.max_domains = maxids;
	op.u.getdomaininfolist.buffer = dominfos;
	op.u.getdomaininfolist.num_domains = maxids;
	ret = xenHypervisorDoV0Op(handle, &op);
	if (ret == 0)
	    ret = op.u.getdomaininfolist.num_domains;
    }
    if (munlock(dominfos, sizeof(xen_v0_getdomaininfo) * maxids) < 0) {
        virXenError(VIR_ERR_XEN_CALL, " release",
                    sizeof(xen_v0_getdomaininfo));
        ret = -1;
    }
    return(ret);
}

/**
 * virXen_pausedomain:
 * @handle: the hypervisor handle
 * @id: the domain id
 *
 * Do a low level hypercall to pause the domain
 *
 * Returns 0 or -1 in case of failure
 */
static int
virXen_pausedomain(int handle, int id) 
{
    int ret = -1;

    if (hypervisor_version > 1) {
        xen_op_v2_dom op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V2_OP_PAUSEDOMAIN;
	op.domain = (domid_t) id;
	ret = xenHypervisorDoV2Dom(handle, &op);
    } else if (hypervisor_version == 1) {
        xen_op_v1 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V1_OP_PAUSEDOMAIN;
	op.u.domain.domain = (domid_t) id;
	ret = xenHypervisorDoV1Op(handle, &op);
    } else if (hypervisor_version == 0) {
        xen_op_v0 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V0_OP_PAUSEDOMAIN;
	op.u.domain.domain = (domid_t) id;
	ret = xenHypervisorDoV0Op(handle, &op);
    }
    return(ret);
}

/**
 * virXen_unpausedomain:
 * @handle: the hypervisor handle
 * @id: the domain id
 *
 * Do a low level hypercall to unpause the domain
 *
 * Returns 0 or -1 in case of failure
 */
static int
virXen_unpausedomain(int handle, int id) 
{
    int ret = -1;

    if (hypervisor_version > 1) {
        xen_op_v2_dom op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V2_OP_UNPAUSEDOMAIN;
	op.domain = (domid_t) id;
	ret = xenHypervisorDoV2Dom(handle, &op);
    } else if (hypervisor_version == 1) {
        xen_op_v1 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V1_OP_UNPAUSEDOMAIN;
	op.u.domain.domain = (domid_t) id;
	ret = xenHypervisorDoV1Op(handle, &op);
    } else if (hypervisor_version == 0) {
        xen_op_v0 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V0_OP_UNPAUSEDOMAIN;
	op.u.domain.domain = (domid_t) id;
	ret = xenHypervisorDoV0Op(handle, &op);
    }
    return(ret);
}

/**
 * virXen_destroydomain:
 * @handle: the hypervisor handle
 * @id: the domain id
 *
 * Do a low level hypercall to destroy the domain
 *
 * Returns 0 or -1 in case of failure
 */
static int
virXen_destroydomain(int handle, int id) 
{
    int ret = -1;

    if (hypervisor_version > 1) {
        xen_op_v2_dom op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V2_OP_DESTROYDOMAIN;
	op.domain = (domid_t) id;
	ret = xenHypervisorDoV2Dom(handle, &op);
    } else if (hypervisor_version == 1) {
        xen_op_v1 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V1_OP_DESTROYDOMAIN;
	op.u.domain.domain = (domid_t) id;
	ret = xenHypervisorDoV1Op(handle, &op);
    } else if (hypervisor_version == 0) {
        xen_op_v0 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V0_OP_DESTROYDOMAIN;
	op.u.domain.domain = (domid_t) id;
	ret = xenHypervisorDoV0Op(handle, &op);
    }
    return(ret);
}

/**
 * virXen_setmaxmem:
 * @handle: the hypervisor handle
 * @id: the domain id
 * @memory: the amount of memory in kilobytes
 *
 * Do a low level hypercall to change the max memory amount
 *
 * Returns 0 or -1 in case of failure
 */
static int
virXen_setmaxmem(int handle, int id, unsigned long memory) 
{
    int ret = -1;

    if (hypervisor_version > 1) {
        xen_op_v2_dom op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V2_OP_SETMAXMEM;
	op.domain = (domid_t) id;
	op.u.setmaxmem.maxmem = memory;
	ret = xenHypervisorDoV2Dom(handle, &op);
    } else if (hypervisor_version == 1) {
        xen_op_v1 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V1_OP_SETMAXMEM;
	op.u.setmaxmem.domain = (domid_t) id;
	op.u.setmaxmem.maxmem = memory;
	ret = xenHypervisorDoV1Op(handle, &op);
    } else if (hypervisor_version == 0) {
        xen_op_v1 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V0_OP_SETMAXMEM;
	op.u.setmaxmem.domain = (domid_t) id;
	op.u.setmaxmem.maxmem = memory;
	ret = xenHypervisorDoV0Op(handle, &op);
    }
    return(ret);
}

/**
 * virXen_setmaxvcpus:
 * @handle: the hypervisor handle
 * @id: the domain id
 * @vcpus: the numbers of vcpus
 *
 * Do a low level hypercall to change the max vcpus amount
 *
 * Returns 0 or -1 in case of failure
 */
static int
virXen_setmaxvcpus(int handle, int id, unsigned int vcpus) 
{
    int ret = -1;

    if (hypervisor_version > 1) {
        xen_op_v2_dom op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V2_OP_SETMAXVCPU;
	op.domain = (domid_t) id;
	op.u.setmaxvcpu.maxvcpu = vcpus;
	ret = xenHypervisorDoV2Dom(handle, &op);
    } else if (hypervisor_version == 1) {
        xen_op_v1 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V1_OP_SETMAXVCPU;
	op.u.setmaxvcpu.domain = (domid_t) id;
	op.u.setmaxvcpu.maxvcpu = vcpus;
	ret = xenHypervisorDoV1Op(handle, &op);
    } else if (hypervisor_version == 0) {
        xen_op_v1 op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V0_OP_SETMAXVCPU;
	op.u.setmaxvcpu.domain = (domid_t) id;
	op.u.setmaxvcpu.maxvcpu = vcpus;
	ret = xenHypervisorDoV0Op(handle, &op);
    }
    return(ret);
}

/**
 * virXen_setvcpumap:
 * @handle: the hypervisor handle
 * @id: the domain id
 * @vcpu: the vcpu to map
 * @cpumap: the bitmap for this vcpu
 *
 * Do a low level hypercall to change the pinning for vcpu
 *
 * Returns 0 or -1 in case of failure
 */
static int
virXen_setvcpumap(int handle, int id, unsigned int vcpu,
                  unsigned char * cpumap, int maplen)
{
    int ret = -1;

    if (hypervisor_version > 1) {
        xen_op_v2_dom op;

        memset(&op, 0, sizeof(op));
	op.cmd = XEN_V2_OP_SETVCPUMAP;
	op.domain = (domid_t) id;
	op.u.setvcpumap.vcpu = vcpu;
	op.u.setvcpumap.cpumap.bitmap = cpumap;
	op.u.setvcpumap.cpumap.nr_cpus = maplen * 8;
	ret = xenHypervisorDoV2Dom(handle, &op);
    } else {
	cpumap_t xen_cpumap; /* limited to 64 CPUs in old hypervisors */
	uint64_t *pm = &xen_cpumap;
	int j;

	if ((maplen > (int)sizeof(cpumap_t)) || (sizeof(cpumap_t) & 7))
	    return (-1);

	memset(pm, 0, sizeof(cpumap_t));
	for (j = 0; j < maplen; j++)
	    *(pm + (j / 8)) |= cpumap[j] << (8 * (j & 7));

        if (hypervisor_version == 1) {
	    xen_op_v1 op;

	    memset(&op, 0, sizeof(op));
	    op.cmd = XEN_V1_OP_SETVCPUMAP;
	    op.u.setvcpumap.domain = (domid_t) id;
	    op.u.setvcpumap.vcpu = vcpu;
	    op.u.setvcpumap.cpumap = xen_cpumap;
	    ret = xenHypervisorDoV1Op(handle, &op);
	} else if (hypervisor_version == 0) {
	    xen_op_v1 op;

	    memset(&op, 0, sizeof(op));
	    op.cmd = XEN_V0_OP_SETVCPUMAP;
	    op.u.setvcpumap.domain = (domid_t) id;
	    op.u.setvcpumap.vcpu = vcpu;
	    op.u.setvcpumap.cpumap = xen_cpumap;
	    ret = xenHypervisorDoV0Op(handle, &op);
	}
    }
    return(ret);
}

825 826 827 828 829 830 831 832 833 834
/**
 * xenHypervisorInit:
 *
 * Initialize the hypervisor layer. Try to detect the kind of interface
 * used i.e. pre or post changeset 10277
 */
int xenHypervisorInit(void)
{
    int fd, ret, cmd;
    hypercall_t hc;
835 836
    v0_hypercall_t v0_hc;
    xen_v0_getdomaininfo info;
837 838

    if (initialized) {
839
        if (hypervisor_version == -1)
840 841 842 843
	    return(-1);
	return(0);
    }
    initialized = 1;
844
    in_init = 1;
845 846 847

    ret = open(XEN_HYPERVISOR_SOCKET, O_RDWR);
    if (ret < 0) {
848
	hypervisor_version = -1;
849 850 851 852
        return (-1);
    }
    fd = ret;

853 854 855 856
    /*
     * The size of the hypervisor call block changed July 2006
     * this detect if we are using the new or old hypercall_t structure
     */
857 858 859 860 861 862 863 864
    hc.op = __HYPERVISOR_xen_version;
    hc.arg[0] = (unsigned long) XENVER_version;
    hc.arg[1] = 0;

    cmd = IOCTL_PRIVCMD_HYPERCALL;
    ret = ioctl(fd, cmd, (unsigned long) &hc);

    if ((ret != -1) && (ret != 0)) {
865
        fprintf(stderr, "Using new hypervisor call: %X\n", ret);
866 867
	hv_version = ret;
	xen_ioctl_hypercall_cmd = cmd;
868
	goto detect_v2;
869 870
    }
    
871 872 873 874 875 876 877 878
    /*
     * check if the old hypercall are actually working
     */
    v0_hc.op = __HYPERVISOR_xen_version;
    v0_hc.arg[0] = (unsigned long) XENVER_version;
    v0_hc.arg[1] = 0;
    cmd = _IOC(_IOC_NONE, 'P', 0, sizeof(v0_hypercall_t));
    ret = ioctl(fd, cmd, (unsigned long) &v0_hc);
879
    if ((ret != -1) && (ret != 0)) {
880
        fprintf(stderr, "Using old hypervisor call: %X\n", ret);
881 882
	hv_version = ret;
	xen_ioctl_hypercall_cmd = cmd;
883
        hypervisor_version = 0;
884 885 886
	goto done;
    }

887 888 889 890 891
    /*
     * we faild to make any hypercall
     */

    hypervisor_version = -1;
892 893
    virXenError(VIR_ERR_XEN_CALL, " ioctl ", IOCTL_PRIVCMD_HYPERCALL);
    close(fd);
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925
    in_init = 0;
    return(-1);

detect_v2:
    /*
     * The hypercalls were refactored into 3 different section in August 2006
     * Try to detect if we are running a version post 3.0.2 with the new ones
     * or the old ones
     */
    hypervisor_version = 2;
    /* TODO: one probably will need to autodetect thse subversions too */
    sys_interface_version = 2; /* XEN_SYSCTL_INTERFACE_VERSION */
    dom_interface_version = 3; /* XEN_DOMCTL_INTERFACE_VERSION */
    if (virXen_getdomaininfolist(fd, 0, 1, &info) == 1) {
        fprintf(stderr, "Using hypervisor call v2, sys version 2\n");
	goto done;
    }
    hypervisor_version = 1;
    sys_interface_version = -1;
    if (virXen_getdomaininfolist(fd, 0, 1, &info) == 1) {
        fprintf(stderr, "Using hypervisor call v1\n");
	goto done;
    }

    /*
     * we faild to make the getdomaininfolist hypercall
     */

    hypervisor_version = -1;
    virXenError(VIR_ERR_XEN_CALL, " ioctl ", IOCTL_PRIVCMD_HYPERCALL);
    close(fd);
    in_init = 0;
926 927 928 929
    return(-1);

done:
    close(fd);
930
    in_init = 0;
931 932 933 934
    return(0);

}

935
#ifndef PROXY
936 937 938 939 940 941 942 943 944 945 946 947
/**
 * xenHypervisorRegister:
 *
 * Registers the xenHypervisor driver
 */
void xenHypervisorRegister(void)
{
    if (initialized == 0)
        xenHypervisorInit();

    virRegisterDriver(&xenHypervisorDriver);
}
948
#endif /* !PROXY */
949

950 951
/**
 * xenHypervisorOpen:
952 953 954
 * @conn: pointer to the connection block
 * @name: URL for the target, NULL for local
 * @flags: combination of virDrvOpenFlag(s)
955 956 957
 *
 * Connects to the Xen hypervisor.
 *
958
 * Returns 0 or -1 in case of error.
959
 */
960
int
961
xenHypervisorOpen(virConnectPtr conn, const char *name, int flags)
962
{
963 964
    int ret;

965 966 967
    if (initialized == 0)
        xenHypervisorInit();

968
    if ((name != NULL) && (strcasecmp(name, "xen")))
969 970 971 972
        return(-1);

    conn->handle = -1;

973
    ret = open(XEN_HYPERVISOR_SOCKET, O_RDWR);
974
    if (ret < 0) {
975
        if (!(flags & VIR_DRV_OPEN_QUIET))
976 977
            virXenError(VIR_ERR_NO_XEN, XEN_HYPERVISOR_SOCKET, 0);
        return (-1);
978
    }
979
    conn->handle = ret;
980

981
    return(0);
982 983 984 985
}

/**
 * xenHypervisorClose:
986
 * @conn: pointer to the connection block
987 988 989 990 991
 *
 * Close the connection to the Xen hypervisor.
 *
 * Returns 0 in case of success or -1 in case of error.
 */
992
int
993
xenHypervisorClose(virConnectPtr conn)
994
{
995 996
    int ret;

997
    if ((conn == NULL) || (conn->handle < 0))
998
        return (-1);
999

1000
    ret = close(conn->handle);
1001
    if (ret < 0)
1002 1003
        return (-1);
    return (0);
1004 1005 1006
}


1007
#ifndef PROXY
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026
/**
 * xenHypervisorGetType:
 * @conn: pointer to the Xen Hypervisor block
 *
 * Get the version level of the Hypervisor running.
 *
 * Returns -1 in case of error, 0 otherwise. if the version can't be
 *    extracted by lack of capacities returns 0 and @hvVer is 0, otherwise
 *    @hvVer value is major * 1,000,000 + minor * 1,000 + release
 */
static const char *
xenHypervisorGetType(virConnectPtr conn)
{
    if (!VIR_IS_CONNECT(conn)) {
        virXenError(VIR_ERR_INVALID_CONN, __FUNCTION__, 0);
        return (NULL);
    }
    return("Xen");
}
1027
#endif
1028

1029 1030
/**
 * xenHypervisorGetVersion:
1031 1032
 * @conn: pointer to the connection block
 * @hvVer: where to store the version
1033 1034 1035
 *
 * Call the hypervisor to extracts his own internal API version
 *
1036
 * Returns 0 in case of success, -1 in case of error
1037
 */
1038 1039
int
xenHypervisorGetVersion(virConnectPtr conn, unsigned long *hvVer)
1040
{
1041 1042
    if ((conn == NULL) || (conn->handle < 0) || (hvVer == NULL))
        return (-1);
1043
    *hvVer = (hv_version >> 16) * 1000000 + (hv_version & 0xFFFF) * 1000;
1044
    return(0);
1045 1046
}

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
/**
 * xenHypervisorNumOfDomains:
 * @conn: pointer to the connection block
 *
 * Provides the number of active domains.
 *
 * Returns the number of domain found or -1 in case of error
 */
int
xenHypervisorNumOfDomains(virConnectPtr conn)
{
1058
    xen_v0_getdomaininfo *dominfos;
1059 1060 1061 1062 1063 1064 1065 1066
    int ret, nbids;
    static int last_maxids = 2;
    int maxids = last_maxids;

    if ((conn == NULL) || (conn->handle < 0))
        return (-1);

retry:
1067
    dominfos = malloc(maxids * sizeof(xen_v0_getdomaininfo));
1068 1069 1070 1071 1072 1073
    if (dominfos == NULL) {
        virXenError(VIR_ERR_NO_MEMORY, "failed to allocate %d domain info",
	            maxids);
	return(-1);
    }
    
1074
    memset(dominfos, 0, sizeof(xen_v0_getdomaininfo) * maxids);
1075

1076
    ret = virXen_getdomaininfolist(conn->handle, 0, maxids, dominfos);
1077 1078 1079 1080 1081 1082

    free(dominfos);

    if (ret < 0)
        return (-1);

1083
    nbids = ret;
1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
    if (nbids == maxids) {
        last_maxids *= 2;
        maxids *= 2;
	goto retry;
    }
    if ((nbids < 0) || (nbids > maxids))
        return(-1);
    return(nbids);
}

/**
 * xenHypervisorListDomains:
 * @conn: pointer to the connection block
 * @ids: array to collect the list of IDs of active domains
 * @maxids: size of @ids
 *
 * Collect the list of active domains, and store their ID in @maxids
 *
 * Returns the number of domain found or -1 in case of error
 */
int
xenHypervisorListDomains(virConnectPtr conn, int *ids, int maxids)
{
1107
    xen_v0_getdomaininfo *dominfos;
1108 1109 1110 1111 1112 1113
    int ret, nbids, i;

    if ((conn == NULL) || (conn->handle < 0) ||
        (ids == NULL) || (maxids < 1))
        return (-1);

1114
    dominfos = malloc(maxids * sizeof(xen_v0_getdomaininfo));
1115 1116 1117 1118 1119 1120
    if (dominfos == NULL) {
        virXenError(VIR_ERR_NO_MEMORY, "failed to allocate %d domain info",
	            maxids);
	return(-1);
    }
    
1121
    memset(dominfos, 0, sizeof(xen_v0_getdomaininfo) * maxids);
1122 1123
    memset(ids, 0, maxids * sizeof(int));

1124
    ret = virXen_getdomaininfolist(conn->handle, 0, maxids, dominfos);
1125 1126 1127 1128 1129 1130

    if (ret < 0) {
	free(dominfos);
        return (-1);
    }

1131
    nbids = ret;
1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
    if ((nbids < 0) || (nbids > maxids)) {
	free(dominfos);
        return(-1);
    }

    for (i = 0;i < nbids;i++) {
        ids[i] = dominfos[i].domain;
    }

    free(dominfos);
    return (nbids);
}

1145
/**
1146 1147 1148
 * xenHypervisorGetDomMaxMemory:
 * @conn: connection data
 * @id: domain id
1149 1150
 * 
 * Retrieve the maximum amount of physical memory allocated to a
1151
 * domain.
1152 1153 1154
 *
 * Returns the memory size in kilobytes or 0 in case of error.
 */
1155 1156
unsigned long
xenHypervisorGetDomMaxMemory(virConnectPtr conn, int id)
1157
{
1158
    xen_v0_getdomaininfo dominfo;
1159 1160
    int ret;

1161
    if ((conn == NULL) || (conn->handle < 0))
1162 1163
        return (0);

1164
    memset(&dominfo, 0, sizeof(xen_v0_getdomaininfo));
1165

1166
    dominfo.domain = id;
1167
    ret = virXen_getdomaininfolist(conn->handle, id, 1, &dominfo);
1168

1169
    if ((ret < 0) || (dominfo.domain != id))
1170 1171 1172 1173 1174
        return (0);

    return((unsigned long) dominfo.max_pages * 4);
}

1175
#ifndef PROXY
1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
/**
 * xenHypervisorGetMaxMemory:
 * @domain: a domain object or NULL
 * 
 * Retrieve the maximum amount of physical memory allocated to a
 * domain. If domain is NULL, then this get the amount of memory reserved
 * to Domain0 i.e. the domain where the application runs.
 *
 * Returns the memory size in kilobytes or 0 in case of error.
 */
static unsigned long
xenHypervisorGetMaxMemory(virDomainPtr domain)
{
    if ((domain == NULL) || (domain->conn == NULL) ||
        (domain->conn->handle < 0))
        return (0);

    return(xenHypervisorGetDomMaxMemory(domain->conn, domain->handle));
}
1195
#endif
1196

1197
/**
1198 1199 1200
 * xenHypervisorGetDomInfo:
 * @conn: connection data
 * @id: the domain ID
1201
 * @info: the place where information should be stored
1202
 *
1203
 * Do an hypervisor call to get the related set of domain information.
1204 1205 1206 1207
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
1208
xenHypervisorGetDomInfo(virConnectPtr conn, int id, virDomainInfoPtr info)
1209
{
1210
    xen_v0_getdomaininfo dominfo;
1211 1212
    int ret;

1213
    if ((conn == NULL) || (conn->handle < 0) || (info == NULL))
1214
        return (-1);
1215

1216
    memset(info, 0, sizeof(virDomainInfo));
1217
    memset(&dominfo, 0, sizeof(xen_v0_getdomaininfo));
1218

1219
    ret = virXen_getdomaininfolist(conn->handle, id, 1, &dominfo);
1220

1221
    if ((ret < 0) || (dominfo.domain != id))
1222
        return (-1);
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252

    switch (dominfo.flags & 0xFF) {
	case DOMFLAGS_DYING:
	    info->state = VIR_DOMAIN_SHUTDOWN;
	    break;
	case DOMFLAGS_SHUTDOWN:
	    info->state = VIR_DOMAIN_SHUTOFF;
	    break;
	case DOMFLAGS_PAUSED:
	    info->state = VIR_DOMAIN_PAUSED;
	    break;
	case DOMFLAGS_BLOCKED:
	    info->state = VIR_DOMAIN_BLOCKED;
	    break;
	case DOMFLAGS_RUNNING:
	    info->state = VIR_DOMAIN_RUNNING;
	    break;
	default:
	    info->state = VIR_DOMAIN_NONE;
    }

    /*
     * the API brings back the cpu time in nanoseconds,
     * convert to microseconds, same thing convert to
     * kilobytes from page counts
     */
    info->cpuTime = dominfo.cpu_time;
    info->memory = dominfo.tot_pages * 4;
    info->maxMem = dominfo.max_pages * 4;
    info->nrVirtCpu = dominfo.nr_online_vcpus;
1253
    return (0);
1254 1255
}

1256 1257 1258
/**
 * xenHypervisorGetDomainInfo:
 * @domain: pointer to the domain block
1259
 * @info: the place where information should be stored
1260
 *
1261
 * Do an hypervisor call to get the related set of domain information.
1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
xenHypervisorGetDomainInfo(virDomainPtr domain, virDomainInfoPtr info)
{
    if ((domain == NULL) || (domain->conn == NULL) ||
        (domain->conn->handle < 0) || (info == NULL) ||
	(domain->handle < 0))
        return (-1);
    return(xenHypervisorGetDomInfo(domain->conn, domain->handle, info));

}

1276
#ifndef PROXY
1277 1278
/**
 * xenHypervisorPauseDomain:
1279
 * @domain: pointer to the domain block
1280 1281 1282 1283 1284 1285
 *
 * Do an hypervisor call to pause the given domain
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
1286
xenHypervisorPauseDomain(virDomainPtr domain)
1287
{
1288 1289
    int ret;

1290 1291 1292 1293
    if ((domain == NULL) || (domain->conn == NULL) ||
        (domain->conn->handle < 0))
        return (-1);

1294
    ret = virXen_pausedomain(domain->conn->handle, domain->handle);
1295
    if (ret < 0)
1296 1297
        return (-1);
    return (0);
1298 1299 1300 1301
}

/**
 * xenHypervisorResumeDomain:
1302
 * @domain: pointer to the domain block
1303 1304 1305 1306 1307 1308
 *
 * Do an hypervisor call to resume the given domain
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
1309
xenHypervisorResumeDomain(virDomainPtr domain)
1310
{
1311 1312
    int ret;

1313 1314 1315 1316
    if ((domain == NULL) || (domain->conn == NULL) ||
        (domain->conn->handle < 0))
        return (-1);

1317
    ret = virXen_unpausedomain(domain->conn->handle, domain->handle);
1318
    if (ret < 0)
1319 1320
        return (-1);
    return (0);
1321 1322 1323 1324
}

/**
 * xenHypervisorDestroyDomain:
1325
 * @domain: pointer to the domain block
1326 1327 1328 1329 1330 1331
 *
 * Do an hypervisor call to destroy the given domain
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
1332
xenHypervisorDestroyDomain(virDomainPtr domain)
1333
{
1334 1335
    int ret;

1336 1337 1338 1339
    if ((domain == NULL) || (domain->conn == NULL) ||
        (domain->conn->handle < 0))
        return (-1);

1340
    ret = virXen_destroydomain(domain->conn->handle, domain->handle);
1341
    if (ret < 0)
1342 1343
        return (-1);
    return (0);
1344 1345
}

1346 1347
/**
 * xenHypervisorSetMaxMemory:
1348
 * @domain: pointer to the domain block
1349 1350 1351 1352 1353 1354 1355
 * @memory: the max memory size in kilobytes.
 *
 * Do an hypervisor call to change the maximum amount of memory used
 *
 * Returns 0 in case of success, -1 in case of error.
 */
int
1356
xenHypervisorSetMaxMemory(virDomainPtr domain, unsigned long memory)
1357
{
1358 1359
    int ret;

1360 1361 1362 1363
    if ((domain == NULL) || (domain->conn == NULL) ||
        (domain->conn->handle < 0))
        return (-1);

1364
    ret = virXen_setmaxmem(domain->conn->handle, domain->handle, memory);
1365
    if (ret < 0)
1366 1367
        return (-1);
    return (0);
1368
}
1369
#endif /* PROXY */
1370

1371
#ifndef PROXY
1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
/**
 * xenHypervisorSetVcpus:
 * @domain: pointer to domain object
 * @nvcpus: the new number of virtual CPUs for this domain
 *
 * Dynamically change the number of virtual CPUs used by the domain.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */

int
xenHypervisorSetVcpus(virDomainPtr domain, unsigned int nvcpus)
{
1385
    int ret;
1386

1387 1388
    if ((domain == NULL) || (domain->conn == NULL) ||
        (domain->conn->handle < 0) || (nvcpus < 1))
1389
        return (-1);
1390 1391 1392

    ret = virXen_setmaxvcpus(domain->conn->handle, domain->handle, nvcpus);
    if (ret < 0)
1393
        return (-1);
1394
    return (0);
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
}

/**
 * xenHypervisorPinVcpu:
 * @domain: pointer to domain object
 * @vcpu: virtual CPU number
 * @cpumap: pointer to a bit map of real CPUs (in 8-bit bytes)
 * @maplen: length of cpumap in bytes
 * 
 * Dynamically change the real CPUs which can be allocated to a virtual CPU.
 *
 * Returns 0 in case of success, -1 in case of failure.
 */

int
xenHypervisorPinVcpu(virDomainPtr domain, unsigned int vcpu,
                     unsigned char *cpumap, int maplen)
{
1413
    int ret;
1414

1415 1416 1417 1418 1419
    if ((domain == NULL) || (domain->conn == NULL) ||
        (domain->conn->handle < 0) || (cpumap == NULL) || (maplen < 1))
    ret = virXen_setvcpumap(domain->conn->handle, domain->handle, vcpu,
                            cpumap, maplen);
    if (ret < 0)
1420
        return (-1);
1421
    return (0);
1422
}
1423
#endif
1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447

/**
 * virDomainGetVcpus:
 * @domain: pointer to domain object, or NULL for Domain0
 * @info: pointer to an array of virVcpuInfo structures (OUT)
 * @maxinfo: number of structures in info array
 * @cpumaps: pointer to an bit map of real CPUs for all vcpus of this domain (in 8-bit bytes) (OUT)
 *	If cpumaps is NULL, then no cupmap information is returned by the API.
 *	It's assumed there is <maxinfo> cpumap in cpumaps array.
 *	The memory allocated to cpumaps must be (maxinfo * maplen) bytes
 *	(ie: calloc(maxinfo, maplen)).
 *	One cpumap inside cpumaps has the format described in virDomainPinVcpu() API.
 * @maplen: number of bytes in one cpumap, from 1 up to size of CPU map in
 *	underlying virtualization system (Xen...).
 * 
 * Extract information about virtual CPUs of domain, store it in info array
 * and also in cpumaps if this pointer is'nt NULL.
 *
 * Returns the number of info filled in case of success, -1 in case of failure.
 */
int
xenHypervisorGetVcpus(virDomainPtr domain, virVcpuInfoPtr info, int maxinfo,
		      unsigned char *cpumaps, int maplen)
{
1448
#ifdef TO_DO
1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
    dom0_op_t op;
    uint64_t *pm = (uint64_t *)&op.u.getvcpuinfo.cpumap; 
    virVcpuInfoPtr ipt;
    int nbinfo, mapl, i;
    unsigned char *cpumap;
    int vcpu, cpu;

    if ((domain == NULL) || (domain->conn == NULL) || (domain->conn->handle < 0)
     || (info == NULL) || (maxinfo < 1)
     || (sizeof(cpumap_t) & 7))
        return (-1);
    if (cpumaps != NULL && maplen < 1)
	return -1;

    /* first get the number of virtual CPUs in this domain */
    op.cmd = DOM0_GETDOMAININFO;
    op.u.getdomaininfo.domain = (domid_t) domain->handle;
    if (xenHypervisorDoOp(domain->conn->handle, &op) < 0)
        return (-1);
    nbinfo = (int)op.u.getdomaininfo.max_vcpu_id + 1;
    if (nbinfo > maxinfo) nbinfo = maxinfo;

    if (cpumaps != NULL)
	memset(cpumaps, 0, maxinfo * maplen);

    op.cmd = DOM0_GETVCPUINFO;
    for (i=0, ipt=info; i < nbinfo; i++, ipt++) {
        vcpu = op.u.getvcpuinfo.vcpu = i;
        if (xenHypervisorDoOp(domain->conn->handle, &op) < 0)
            return (-1);
        ipt->number = i;
        if (op.u.getvcpuinfo.online) {
            if (op.u.getvcpuinfo.running) ipt->state = VIR_VCPU_RUNNING;
            if (op.u.getvcpuinfo.blocked) ipt->state = VIR_VCPU_BLOCKED;
        }
        else ipt->state = VIR_VCPU_OFFLINE;
        ipt->cpuTime = op.u.getvcpuinfo.cpu_time;
        ipt->cpu = op.u.getvcpuinfo.online ? (int)op.u.getvcpuinfo.cpu : -1;
	if (cpumaps != NULL && vcpu >= 0 && vcpu < maxinfo) {
	    cpumap = (unsigned char *)VIR_GET_CPUMAP(cpumaps, maplen, vcpu);
	    mapl = (maplen > (int)sizeof(cpumap_t)) ? (int)sizeof(cpumap_t) : maplen;
            for (cpu = 0; cpu < (mapl * CHAR_BIT); cpu++) {
		if (*pm & ((uint64_t)1<<cpu))
		    VIR_USE_CPU(cpumap, cpu);
	    }
	}
    }
    return nbinfo;
1497
#endif
1498
}