debuggers.hg

view tools/libxl/libxl.c @ 20819:dda8dc485d67

libxenlight: returns errors if xc and/or xs has not been initialized properly.

fixed segfault when xenstore or xc are not available.

Signed-off-by: Vincent Hanquez <vincent.hanquez@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri Jan 08 11:45:34 2010 +0000 (2010-01-08)
parents 74ca2cb026a5
children 819c21064517
line source
1 /*
2 * Copyright (C) 2009 Citrix Ltd.
3 * Author Vincent Hanquez <vincent.hanquez@eu.citrix.com>
4 * Author Stefano Stabellini <stefano.stabellini@eu.citrix.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation; version 2.1 only. with the special
9 * exception on linking described in file LICENSE.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 */
17 #include "libxl_osdeps.h"
19 #include <stdio.h>
20 #include <string.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <fcntl.h>
24 #include <sys/select.h>
25 #include <sys/wait.h>
26 #include <signal.h>
27 #include <unistd.h> /* for write, unlink and close */
28 #include <stdint.h>
29 #include <inttypes.h>
30 #include <assert.h>
32 #include "libxl.h"
33 #include "libxl_utils.h"
34 #include "libxl_internal.h"
35 #include "flexarray.h"
37 int libxl_ctx_init(struct libxl_ctx *ctx, int version)
38 {
39 if (version != LIBXL_VERSION)
40 return ERROR_VERSION;
41 memset(ctx, 0, sizeof(struct libxl_ctx));
42 ctx->alloc_maxsize = 256;
43 ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
44 if (!ctx->alloc_ptrs)
45 return ERROR_NOMEM;
47 ctx->xch = xc_interface_open();
48 if (ctx->xch == -1) {
49 free(ctx->alloc_ptrs);
50 return ERROR_FAIL;
51 }
53 ctx->xsh = xs_daemon_open();
54 if (!ctx->xsh) {
55 xc_interface_close(ctx->xch);
56 free(ctx->alloc_ptrs);
57 return ERROR_FAIL;
58 }
59 return 0;
60 }
62 int libxl_ctx_free(struct libxl_ctx *ctx)
63 {
64 libxl_free_all(ctx);
65 free(ctx->alloc_ptrs);
66 xc_interface_close(ctx->xch);
67 xs_daemon_close(ctx->xsh);
68 return 0;
69 }
71 int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, void *log_data)
72 {
73 ctx->log_callback = log_callback;
74 ctx->log_userdata = log_data;
75 return 0;
76 }
78 /******************************************************************************/
80 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
81 uint32_t *domid)
82 {
83 int flags, ret, i;
84 char *uuid_string;
85 char *rw_paths[] = { "device", "device/suspend/event-channel" , "data"};
86 char *ro_paths[] = { "cpu", "memory", "device", "error", "drivers",
87 "control", "attr", "messages" };
88 char *dom_path, *vm_path, *vss_path;
89 struct xs_permissions roperm[2];
90 struct xs_permissions rwperm[1];
91 xs_transaction_t t;
92 xen_domain_handle_t handle;
94 uuid_string = string_of_uuid(ctx, info->uuid);
95 if (!uuid_string) {
96 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate uuid string");
97 return ERROR_FAIL;
98 }
100 flags = info->hvm ? XEN_DOMCTL_CDF_hvm_guest : 0;
101 flags |= info->hap ? XEN_DOMCTL_CDF_hap : 0;
102 *domid = 0;
104 /* Ultimately, handle is an array of 16 uint8_t, same as uuid */
105 memcpy(handle, info->uuid, sizeof(xen_domain_handle_t));
107 ret = xc_domain_create(ctx->xch, info->ssidref, handle, flags, domid);
108 if (ret < 0) {
109 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, ret, "domain creation fail");
110 return ERROR_FAIL;
111 }
113 dom_path = libxl_xs_get_dompath(ctx, *domid);
114 if (!dom_path)
115 return ERROR_FAIL;
117 vm_path = libxl_sprintf(ctx, "/vm/%s", uuid_string);
118 vss_path = libxl_sprintf(ctx, "/vss/%s", uuid_string);
119 if (!vm_path || !vss_path) {
120 XL_LOG(ctx, XL_LOG_ERROR, "cannot allocate create paths");
121 return ERROR_FAIL;
122 }
124 roperm[0].id = 0;
125 roperm[0].perms = XS_PERM_NONE;
126 roperm[1].id = *domid;
127 roperm[1].perms = XS_PERM_READ;
128 rwperm[0].id = *domid;
129 rwperm[0].perms = XS_PERM_NONE;
131 retry_transaction:
132 t = xs_transaction_start(ctx->xsh);
133 xs_rm(ctx->xsh, t, dom_path);
134 xs_mkdir(ctx->xsh, t, dom_path);
135 xs_set_permissions(ctx->xsh, t, dom_path, roperm, ARRAY_SIZE(roperm));
137 xs_rm(ctx->xsh, t, vm_path);
138 xs_mkdir(ctx->xsh, t, vm_path);
139 xs_set_permissions(ctx->xsh, t, vm_path, roperm, ARRAY_SIZE(roperm));
141 xs_rm(ctx->xsh, t, vss_path);
142 xs_mkdir(ctx->xsh, t, vss_path);
143 xs_set_permissions(ctx->xsh, t, vss_path, rwperm, ARRAY_SIZE(rwperm));
145 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vm", dom_path), vm_path, strlen(vm_path));
146 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vss", dom_path), vss_path, strlen(vss_path));
147 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", dom_path), info->name, strlen(info->name));
149 for (i = 0; i < ARRAY_SIZE(rw_paths); i++) {
150 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, rw_paths[i]);
151 xs_mkdir(ctx->xsh, t, path);
152 xs_set_permissions(ctx->xsh, t, path, rwperm, ARRAY_SIZE(rwperm));
153 libxl_free(ctx, path);
154 }
155 for (i = 0; i < ARRAY_SIZE(ro_paths); i++) {
156 char *path = libxl_sprintf(ctx, "%s/%s", dom_path, ro_paths[i]);
157 xs_mkdir(ctx->xsh, t, path);
158 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
159 libxl_free(ctx, path);
160 }
162 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/uuid", vm_path), uuid_string, strlen(uuid_string));
163 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/name", vm_path), info->name, strlen(info->name));
165 libxl_xs_writev(ctx, t, dom_path, info->xsdata);
166 libxl_xs_writev(ctx, t, libxl_sprintf(ctx, "%s/platform", dom_path), info->platformdata);
168 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/control/platform-feature-multiprocessor-suspend", dom_path), "1", 1);
170 if (!xs_transaction_end(ctx->xsh, t, 0))
171 if (errno == EAGAIN)
172 goto retry_transaction;
173 return 0;
174 }
176 int libxl_domain_build(struct libxl_ctx *ctx, libxl_domain_build_info *info, uint32_t domid, libxl_domain_build_state *state)
177 {
178 char **vments = NULL, **localents = NULL;
179 int i;
181 build_pre(ctx, domid, info, state);
182 if (info->hvm) {
183 build_hvm(ctx, domid, info, state);
184 vments = libxl_calloc(ctx, 5, sizeof(char *));
185 vments[0] = "rtc/timeoffset";
186 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
187 vments[2] = "image/ostype";
188 vments[3] = "hvm";
189 } else {
190 build_pv(ctx, domid, info, state);
191 vments = libxl_calloc(ctx, 9, sizeof(char *));
192 i = 0;
193 vments[i++] = "image/ostype";
194 vments[i++] = "linux";
195 vments[i++] = "image/kernel";
196 vments[i++] = (char*) info->kernel;
197 if (info->u.pv.ramdisk) {
198 vments[i++] = "image/ramdisk";
199 vments[i++] = (char*) info->u.pv.ramdisk;
200 }
201 if (info->u.pv.cmdline) {
202 vments[i++] = "image/cmdline";
203 vments[i++] = (char*) info->u.pv.cmdline;
204 }
205 }
206 build_post(ctx, domid, info, state, vments, localents);
207 return 0;
208 }
210 int libxl_domain_restore(struct libxl_ctx *ctx, libxl_domain_build_info *info,
211 uint32_t domid, int fd, libxl_domain_build_state *state,
212 libxl_device_model_info *dm_info)
213 {
214 char **vments = NULL, **localents = NULL;
215 int i;
217 build_pre(ctx, domid, info, state);
218 restore_common(ctx, domid, info, state, fd);
219 if (info->hvm) {
220 vments = libxl_calloc(ctx, 5, sizeof(char *));
221 vments[0] = "rtc/timeoffset";
222 vments[1] = (info->u.hvm.timeoffset) ? info->u.hvm.timeoffset : "";
223 vments[2] = "image/ostype";
224 vments[3] = "hvm";
225 } else {
226 vments = libxl_calloc(ctx, 9, sizeof(char *));
227 i = 0;
228 vments[i++] = "image/ostype";
229 vments[i++] = "linux";
230 vments[i++] = "image/kernel";
231 vments[i++] = (char*) info->kernel;
232 if (info->u.pv.ramdisk) {
233 vments[i++] = "image/ramdisk";
234 vments[i++] = (char*) info->u.pv.ramdisk;
235 }
236 if (info->u.pv.cmdline) {
237 vments[i++] = "image/cmdline";
238 vments[i++] = (char*) info->u.pv.cmdline;
239 }
240 }
241 build_post(ctx, domid, info, state, vments, localents);
242 if (info->hvm)
243 asprintf(&(dm_info->saved_state), "/var/lib/xen/qemu-save.%d", domid);
244 else
245 dm_info->saved_state = NULL;
247 return 0;
248 }
250 int libxl_domain_resume(struct libxl_ctx *ctx, uint32_t domid)
251 {
252 if (is_hvm(ctx, domid)) {
253 XL_LOG(ctx, XL_LOG_DEBUG, "Called domain_resume on "
254 "non-cooperative hvm domain %u", domid);
255 return ERROR_NI;
256 }
257 if (xc_domain_resume(ctx->xch, domid, 1)) {
258 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
259 "xc_domain_resume failed for domain %u",
260 domid);
261 return ERROR_FAIL;
262 }
263 if (!xs_resume_domain(ctx->xsh, domid)) {
264 XL_LOG_ERRNO(ctx, XL_LOG_ERROR,
265 "xs_resume_domain failed for domain %u",
266 domid);
267 return ERROR_FAIL;
268 }
269 return 0;
270 }
272 struct libxl_dominfo * libxl_domain_list(struct libxl_ctx *ctx, int *nb_domain)
273 {
274 struct libxl_dominfo *ptr;
275 int index, i, ret, first_domain;
276 xc_domaininfo_t info[16];
277 int size = 16;
279 first_domain = 1;
280 index = 0;
281 ptr = calloc(size, sizeof(struct libxl_dominfo));
282 if (!ptr)
283 return NULL;
284 redo:
285 ret = xc_domain_getinfolist(ctx->xch, first_domain, 16, info);
286 for (i = 0; i < ret; i++) {
287 if (index == size) {
288 struct libxl_dominfo *ptr2;
290 ptr2 = calloc(size * 2, sizeof(struct libxl_dominfo));
291 if (!ptr2) {
292 free(ptr);
293 return NULL;
294 }
295 memcpy(ptr2, ptr, sizeof(struct libxl_dominfo) * size);
296 free(ptr);
297 ptr = ptr2;
298 size *= 2;
299 }
300 memcpy(&(ptr[index].uuid), info[i].handle, sizeof(xen_domain_handle_t));
301 ptr[index].domid = info[i].domain;
303 if (info[i].flags & XEN_DOMINF_dying)
304 ptr[index].dying = 1;
305 else if (info[i].flags & XEN_DOMINF_paused)
306 ptr[index].paused = 1;
307 else if (info[i].flags & XEN_DOMINF_blocked || info[i].flags & XEN_DOMINF_running)
308 ptr[index].running = 1;
310 first_domain = info[i].domain + 1;
311 index++;
312 }
313 if (ret == 16)
314 goto redo;
315 *nb_domain = index;
316 return ptr;
317 }
319 int libxl_domain_suspend(struct libxl_ctx *ctx, libxl_domain_suspend_info *info,
320 uint32_t domid, int fd)
321 {
322 int hvm = is_hvm(ctx, domid);
323 int live = info != NULL && info->flags & XL_SUSPEND_LIVE;
324 int debug = info != NULL && info->flags & XL_SUSPEND_LIVE;
326 core_suspend(ctx, domid, fd, hvm, live, debug);
327 if (hvm)
328 save_device_model(ctx, domid, fd);
329 return 0;
330 }
332 int libxl_domain_pause(struct libxl_ctx *ctx, uint32_t domid)
333 {
334 xc_domain_pause(ctx->xch, domid);
335 return 0;
336 }
338 int libxl_domain_unpause(struct libxl_ctx *ctx, uint32_t domid)
339 {
340 char *path;
341 char *state;
343 if (is_hvm(ctx, domid)) {
344 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
345 state = libxl_xs_read(ctx, XBT_NULL, path);
346 if (state != NULL && !strcmp(state, "paused")) {
347 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid), "continue", strlen("continue"));
348 libxl_wait_for_device_model(ctx, domid, "running", NULL, NULL);
349 }
350 }
351 xc_domain_unpause(ctx->xch, domid);
353 return 0;
354 }
356 static char *req_table[] = {
357 [0] = "poweroff",
358 [1] = "reboot",
359 [2] = "suspend",
360 [3] = "crash",
361 [4] = "halt",
362 };
364 int libxl_domain_shutdown(struct libxl_ctx *ctx, uint32_t domid, int req)
365 {
366 char *shutdown_path;
367 char *dom_path;
369 if (req > ARRAY_SIZE(req_table))
370 return ERROR_INVAL;
372 dom_path = libxl_xs_get_dompath(ctx, domid);
373 if (!dom_path)
374 return ERROR_FAIL;
376 shutdown_path = libxl_sprintf(ctx, "%s/control/shutdown", dom_path);
378 xs_write(ctx->xsh, XBT_NULL, shutdown_path, req_table[req], strlen(req_table[req]));
379 if (/* hvm */ 0) {
380 unsigned long acpi_s_state = 0;
381 unsigned long pvdriver = 0;
382 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_ACPI_S_STATE, &acpi_s_state);
383 xc_get_hvm_param(ctx->xch, domid, HVM_PARAM_CALLBACK_IRQ, &pvdriver);
384 if (!pvdriver && acpi_s_state != 0)
385 xc_domain_shutdown(ctx->xch, domid, req);
386 }
387 return 0;
388 }
390 int libxl_get_wait_fd(struct libxl_ctx *ctx, int *fd)
391 {
392 *fd = xs_fileno(ctx->xsh);
393 return 0;
394 }
396 int libxl_wait_for_domain_death(struct libxl_ctx *ctx, uint32_t domid, libxl_waiter *waiter)
397 {
398 waiter->path = strdup("@releaseDomain");
399 asprintf(&(waiter->token), "%d", DOMAIN_DEATH);
400 if (!xs_watch(ctx->xsh, waiter->path, waiter->token))
401 return -1;
402 return 0;
403 }
405 int libxl_wait_for_disk_ejects(struct libxl_ctx *ctx, uint32_t guest_domid, libxl_device_disk *disks, int num_disks, libxl_waiter *waiter)
406 {
407 int i;
408 uint32_t domid = libxl_get_stubdom_id(ctx, guest_domid);
410 if (!domid)
411 domid = guest_domid;
413 for (i = 0; i < num_disks; i++) {
414 asprintf(&(waiter[i].path), "%s/device/vbd/%d/eject", libxl_xs_get_dompath(ctx, domid), device_disk_dev_number(disks[i].virtpath));
415 asprintf(&(waiter[i].token), "%d", DISK_EJECT);
416 xs_watch(ctx->xsh, waiter->path, waiter->token);
417 }
418 return 0;
419 }
421 int libxl_get_event(struct libxl_ctx *ctx, libxl_event *event)
422 {
423 unsigned int num;
424 char **events = xs_read_watch(ctx->xsh, &num);
425 if (num != 2) {
426 free(events);
427 return -1;
428 }
429 event->path = strdup(events[XS_WATCH_PATH]);
430 event->token = strdup(events[XS_WATCH_TOKEN]);
431 event->type = atoi(event->token);
432 free(events);
433 return 0;
434 }
436 int libxl_stop_waiting(struct libxl_ctx *ctx, libxl_waiter *waiter)
437 {
438 if (!xs_unwatch(ctx->xsh, waiter->path, waiter->token))
439 return -1;
440 else
441 return 0;
442 }
444 int libxl_free_event(libxl_event *event)
445 {
446 free(event->path);
447 free(event->token);
448 return 0;
449 }
451 int libxl_free_waiter(libxl_waiter *waiter)
452 {
453 free(waiter->path);
454 free(waiter->token);
455 return 0;
456 }
458 int libxl_event_get_domain_death_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, xc_domaininfo_t *info)
459 {
460 int rc = 0, ret;
462 if (event && event->type == DOMAIN_DEATH) {
463 ret = xc_domain_getinfolist(ctx->xch, domid, 1, info);
464 if (ret == 1 && info->domain == domid) {
465 if (info->flags & XEN_DOMINF_running ||
466 (!(info->flags & XEN_DOMINF_shutdown) && !(info->flags & XEN_DOMINF_dying)))
467 goto out;
468 rc = 1;
469 goto out;
470 }
471 memset(info, 0, sizeof(xc_dominfo_t));
472 rc = 1;
473 goto out;
474 }
475 out:
476 return rc;
477 }
479 int libxl_event_get_disk_eject_info(struct libxl_ctx *ctx, uint32_t domid, libxl_event *event, libxl_device_disk *disk)
480 {
481 if (event && event->type == DISK_EJECT) {
482 char *path;
483 char *backend;
484 char *value = libxl_xs_read(ctx, XBT_NULL, event->path);
486 if (!value || strcmp(value, "eject"))
487 return 0;
489 path = strdup(event->path);
490 path[strlen(path) - 6] = '\0';
491 backend = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/backend", path));
493 disk->backend_domid = 0;
494 disk->domid = domid;
495 disk->physpath = NULL;
496 disk->phystype = 0;
497 /* this value is returned to the user: do not free right away */
498 disk->virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev", backend));
499 disk->unpluggable = 1;
500 disk->readwrite = 0;
501 disk->is_cdrom = 1;
503 free(path);
504 return 1;
505 }
506 return 0;
507 }
509 static int libxl_destroy_device_model(struct libxl_ctx *ctx, uint32_t domid)
510 {
511 char *pid;
512 int ret;
514 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
515 if (!pid) {
516 int stubdomid = libxl_get_stubdom_id(ctx, domid);
517 if (!stubdomid) {
518 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't find device model's pid");
519 return -1;
520 }
521 XL_LOG(ctx, XL_LOG_ERROR, "Device model is a stubdom, domid=%d\n", stubdomid);
522 return libxl_domain_destroy(ctx, stubdomid, 0);
523 }
524 xs_rm(ctx->xsh, XBT_NULL, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", domid));
526 ret = kill(atoi(pid), SIGHUP);
527 if (ret < 0 && errno == ESRCH) {
528 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model already exited");
529 ret = 0;
530 } else if (ret == 0) {
531 XL_LOG(ctx, XL_LOG_DEBUG, "Device Model signaled");
532 ret = 0;
533 } else {
534 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "failed to kill Device Model [%d]",
535 atoi(pid));
536 }
537 return ret;
538 }
540 int libxl_domain_destroy(struct libxl_ctx *ctx, uint32_t domid, int force)
541 {
542 char *dom_path;
543 char *vm_path, *vss_path, *xapi_path;
544 int rc, dm_present;
546 if (is_hvm(ctx, domid)) {
547 dm_present = 1;
548 } else {
549 char *pid;
550 pid = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/image/device-model-pid", domid));
551 dm_present = (pid != NULL);
552 libxl_free(ctx, pid);
553 }
555 dom_path = libxl_xs_get_dompath(ctx, domid);
556 if (!dom_path)
557 return -1;
559 if (libxl_device_pci_shutdown(ctx, domid) < 0)
560 XL_LOG(ctx, XL_LOG_ERROR, "pci shutdown failed for domid %d", domid);
561 if (dm_present) {
562 xs_write(ctx->xsh, XBT_NULL,
563 libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid),
564 "shutdown", strlen("shutdown"));
565 }
566 rc = xc_domain_pause(ctx->xch, domid);
567 if (rc < 0) {
568 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_pause failed for %d", domid);
569 return -1;
570 }
571 if (dm_present) {
572 if (libxl_destroy_device_model(ctx, domid) < 0)
573 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_device_model failed for %d", domid);
574 }
575 if (libxl_devices_destroy(ctx, domid, force) < 0)
576 XL_LOG(ctx, XL_LOG_ERROR, "libxl_destroy_devices failed for %d", domid);
577 if (!xs_rm(ctx->xsh, XBT_NULL, dom_path))
578 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", dom_path);
580 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", domid));
581 if (!xs_rm(ctx->xsh, XBT_NULL, vm_path))
582 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vm_path);
584 vss_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vss", domid));
585 if (!xs_rm(ctx->xsh, XBT_NULL, vss_path))
586 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", vss_path);
588 xapi_path = libxl_sprintf(ctx, "/xapi/%u", domid);
589 if (!xs_rm(ctx->xsh, XBT_NULL, xapi_path))
590 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "xs_rm failed for %s", xapi_path);
592 rc = xc_domain_destroy(ctx->xch, domid);
593 if (rc < 0) {
594 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_destroy failed for %d", domid);
595 return -1;
596 }
597 return 0;
598 }
600 int libxl_console_attach(struct libxl_ctx *ctx, uint32_t domid, int cons_num)
601 {
602 struct stat st;
603 const char *XENCONSOLE = "/usr/lib/xen/bin/xenconsole";
604 char *cmd;
606 if (stat(XENCONSOLE, &st) != 0) {
607 XL_LOG(ctx, XL_LOG_ERROR, "could not access %s", XENCONSOLE);
608 return ERROR_FAIL;
609 }
611 cmd = libxl_sprintf(ctx, "%s %d --num %d", XENCONSOLE, domid, cons_num);
612 return (system(cmd) != 0) ? ERROR_FAIL : 0;
613 }
615 static char ** libxl_build_device_model_args(struct libxl_ctx *ctx,
616 libxl_device_model_info *info,
617 libxl_device_nic *vifs,
618 int num_vifs)
619 {
620 int num = 0, i;
621 flexarray_t *dm_args;
622 dm_args = flexarray_make(16, 1);
623 if (!dm_args)
624 return NULL;
626 flexarray_set(dm_args, num++, "qemu-dm");
627 flexarray_set(dm_args, num++, "-d");
629 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->domid));
631 if (info->dom_name) {
632 flexarray_set(dm_args, num++, "-domain-name");
633 flexarray_set(dm_args, num++, info->dom_name);
634 }
635 if (info->vnc || info->vncdisplay || info->vnclisten || info->vncunused) {
636 flexarray_set(dm_args, num++, "-vnc");
637 if (info->vncdisplay) {
638 if (info->vnclisten && strchr(info->vnclisten, ':') == NULL) {
639 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:%d", info->vnclisten, info->vncdisplay));
640 } else {
641 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "127.0.0.1:%d", info->vncdisplay));
642 }
643 } else if (info->vnclisten) {
644 if (strchr(info->vnclisten, ':') != NULL) {
645 flexarray_set(dm_args, num++, info->vnclisten);
646 } else {
647 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%s:0", info->vnclisten));
648 }
649 } else {
650 flexarray_set(dm_args, num++, "127.0.0.1:0");
651 }
652 if (info->vncunused) {
653 flexarray_set(dm_args, num++, "-vncunused");
654 }
655 }
656 if (info->sdl || info->opengl) {
657 flexarray_set(dm_args, num++, "-sdl");
658 if (info->opengl) {
659 flexarray_set(dm_args, num++, "-disable-opengl");
660 }
661 }
662 if (info->keymap) {
663 flexarray_set(dm_args, num++, "-k");
664 flexarray_set(dm_args, num++, info->keymap);
665 }
666 if (info->nographic && (!info->sdl && !info->vnc)) {
667 flexarray_set(dm_args, num++, "-nographic");
668 }
669 if (info->serial) {
670 flexarray_set(dm_args, num++, "-serial");
671 flexarray_set(dm_args, num++, info->serial);
672 }
673 if (info->type == XENFV) {
674 if (info->videoram) {
675 flexarray_set(dm_args, num++, "-videoram");
676 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "%d", info->videoram));
677 }
678 if (info->stdvga) {
679 flexarray_set(dm_args, num++, "-std-vga");
680 }
682 if (info->boot) {
683 flexarray_set(dm_args, num++, "-boot");
684 flexarray_set(dm_args, num++, info->boot);
685 }
686 if (info->usb) {
687 flexarray_set(dm_args, num++, "-usb");
688 if (info->usbdevice) {
689 flexarray_set(dm_args, num++, "-usbdevice");
690 flexarray_set(dm_args, num++, info->usbdevice);
691 }
692 }
693 if (info->apic) {
694 flexarray_set(dm_args, num++, "-acpi");
695 }
696 for (i = 0; i < num_vifs; i++) {
697 if (vifs[i].nictype == NICTYPE_IOEMU) {
698 char *smac = libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
699 vifs[i].mac[0], vifs[i].mac[1], vifs[i].mac[2],
700 vifs[i].mac[3], vifs[i].mac[4], vifs[i].mac[5]);
701 if (!vifs[i].ifname)
702 vifs[i].ifname = libxl_sprintf(ctx, "tap%d.%d", info->domid, vifs[i].devid - 1);
703 flexarray_set(dm_args, num++, "-net");
704 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "nic,vlan=%d,macaddr=%s,model=%s",
705 vifs[i].devid, smac, vifs[i].model));
706 flexarray_set(dm_args, num++, "-net");
707 flexarray_set(dm_args, num++, libxl_sprintf(ctx, "tap,vlan=%d,ifname=%s,bridge=%s",
708 vifs[i].devid, vifs[i].ifname, vifs[i].bridge));
709 }
710 }
711 }
712 if (info->saved_state) {
713 flexarray_set(dm_args, num++, "-loadvm");
714 flexarray_set(dm_args, num++, info->saved_state);
715 }
716 for (i = 0; info->extra && info->extra[i] != NULL; i++)
717 flexarray_set(dm_args, num++, info->extra[i]);
718 flexarray_set(dm_args, num++, "-M");
719 if (info->type == XENPV)
720 flexarray_set(dm_args, num++, "xenpv");
721 else
722 flexarray_set(dm_args, num++, "xenfv");
723 flexarray_set(dm_args, num++, NULL);
725 return (char **) flexarray_contents(dm_args);
726 }
728 void dm_xenstore_record_pid(struct libxl_ctx *ctx, void *for_spawn,
729 pid_t innerchild) {
730 struct libxl_device_model_starting *starting = for_spawn;
731 struct libxl_ctx clone;
732 char *kvs[3];
733 int rc, cloned;
735 if (libxl_clone_context_xs(ctx, &clone)) {
736 XL_LOG(ctx, XL_LOG_ERROR, "Out of memory when cloning context");
737 /* Throw a prayer fallback */
738 clone = *ctx;
739 clone.xsh = xs_daemon_open();
740 cloned = 0;
741 } else {
742 cloned = 1;
743 }
744 /* we mustn't use the parent's handle in the child */
746 kvs[0] = "image/device-model-pid";
747 kvs[1] = libxl_sprintf(&clone, "%d", innerchild);
748 kvs[2] = NULL;
749 rc = libxl_xs_writev(&clone, XBT_NULL, starting->dom_path, kvs);
750 if (rc) XL_LOG_ERRNO(&clone, XL_LOG_ERROR,
751 "Couldn't record device model pid %ld at %s/%s",
752 (unsigned long)innerchild, starting->dom_path, kvs);
753 if (cloned) {
754 libxl_discard_cloned_context_xs(&clone);
755 } else {
756 xs_daemon_close(clone.xsh);
757 }
758 }
760 static int libxl_vfb_and_vkb_from_device_model_info(struct libxl_ctx *ctx,
761 libxl_device_model_info *info,
762 libxl_device_vfb *vfb,
763 libxl_device_vkb *vkb)
764 {
765 memset(vfb, 0x00, sizeof(libxl_device_vfb));
766 memset(vkb, 0x00, sizeof(libxl_device_vkb));
768 vfb->backend_domid = 0;
769 vfb->devid = 0;
770 vfb->vnc = info->vnc;
771 vfb->vnclisten = info->vnclisten;
772 vfb->vncdisplay = info->vncdisplay;
773 vfb->vncunused = info->vncunused;
774 vfb->keymap = info->keymap;
775 vfb->sdl = info->sdl;
776 vfb->opengl = info->opengl;
778 vkb->backend_domid = 0;
779 vkb->devid = 0;
780 return 0;
781 }
783 static int libxl_write_dmargs(struct libxl_ctx *ctx, int domid, int guest_domid, char **args)
784 {
785 int i;
786 char *vm_path;
787 char *dmargs, *path;
788 int dmargs_size;
789 struct xs_permissions roperm[2];
790 xs_transaction_t t;
792 roperm[0].id = 0;
793 roperm[0].perms = XS_PERM_NONE;
794 roperm[1].id = domid;
795 roperm[1].perms = XS_PERM_READ;
797 vm_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "/local/domain/%d/vm", guest_domid));
799 i = 0;
800 dmargs_size = 0;
801 while (args[i] != NULL) {
802 dmargs_size = dmargs_size + strlen(args[i]) + 1;
803 i++;
804 }
805 dmargs_size++;
806 dmargs = (char *) malloc(dmargs_size);
807 i = 1;
808 dmargs[0] = '\0';
809 while (args[i] != NULL) {
810 if (strcmp(args[i], "-sdl") && strcmp(args[i], "-M") && strcmp(args[i], "xenfv")) {
811 strcat(dmargs, " ");
812 strcat(dmargs, args[i]);
813 }
814 i++;
815 }
816 path = libxl_sprintf(ctx, "%s/image/dmargs", vm_path);
818 retry_transaction:
819 t = xs_transaction_start(ctx->xsh);
820 xs_write(ctx->xsh, t, path, dmargs, strlen(dmargs));
821 xs_set_permissions(ctx->xsh, t, path, roperm, ARRAY_SIZE(roperm));
822 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "%s/rtc/timeoffset", vm_path), roperm, ARRAY_SIZE(roperm));
823 if (!xs_transaction_end(ctx->xsh, t, 0))
824 if (errno == EAGAIN)
825 goto retry_transaction;
826 free(dmargs);
827 return 0;
828 }
830 static int libxl_create_stubdom(struct libxl_ctx *ctx,
831 libxl_device_model_info *info,
832 libxl_device_disk *disks, int num_disks,
833 libxl_device_nic *vifs, int num_vifs,
834 libxl_device_vfb *vfb,
835 libxl_device_vkb *vkb,
836 libxl_device_model_starting **starting_r)
837 {
838 int i, num_console = 1;
839 libxl_device_console *console;
840 libxl_domain_create_info c_info;
841 libxl_domain_build_info b_info;
842 libxl_domain_build_state state;
843 uint32_t domid;
844 char **args;
845 struct xs_permissions perm[2];
846 xs_transaction_t t;
847 libxl_device_model_starting *dm_starting = 0;
849 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
850 if (!args)
851 return ERROR_FAIL;
853 memset(&c_info, 0x00, sizeof(libxl_domain_create_info));
854 c_info.hvm = 0;
855 c_info.name = libxl_sprintf(ctx, "%s-dm", libxl_domid_to_name(ctx, info->domid));
856 for (i = 0; i < 16; i++)
857 c_info.uuid[i] = info->uuid[i];
859 memset(&b_info, 0x00, sizeof(libxl_domain_build_info));
860 b_info.max_vcpus = 1;
861 b_info.max_memkb = 32 * 1024;
862 b_info.target_memkb = b_info.max_memkb;
863 b_info.kernel = "/usr/lib/xen/boot/ioemu-stubdom.gz";
864 b_info.u.pv.cmdline = libxl_sprintf(ctx, " -d %d", info->domid);
865 b_info.u.pv.ramdisk = "";
866 b_info.u.pv.features = "";
867 b_info.hvm = 0;
869 libxl_domain_make(ctx, &c_info, &domid);
870 libxl_domain_build(ctx, &b_info, domid, &state);
872 libxl_write_dmargs(ctx, domid, info->domid, args);
873 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/image/device-model-domid", libxl_xs_get_dompath(ctx, info->domid)), "%d", domid);
874 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/target", libxl_xs_get_dompath(ctx, domid)), "%d", info->domid);
875 xc_domain_set_target(ctx->xch, domid, info->domid);
876 xs_set_target(ctx->xsh, domid, info->domid);
878 perm[0].id = domid;
879 perm[0].perms = XS_PERM_NONE;
880 perm[1].id = info->domid;
881 perm[1].perms = XS_PERM_READ;
882 retry_transaction:
883 t = xs_transaction_start(ctx->xsh);
884 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid));
885 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid), perm, ARRAY_SIZE(perm));
886 xs_mkdir(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs", domid));
887 xs_set_permissions(ctx->xsh, t, libxl_sprintf(ctx, "/local/domain/%d/device/vfs",domid), perm, ARRAY_SIZE(perm));
888 if (!xs_transaction_end(ctx->xsh, t, 0))
889 if (errno == EAGAIN)
890 goto retry_transaction;
892 for (i = 0; i < num_disks; i++) {
893 disks[i].domid = domid;
894 libxl_device_disk_add(ctx, domid, &disks[i]);
895 }
896 for (i = 0; i < num_vifs; i++) {
897 vifs[i].domid = domid;
898 libxl_device_nic_add(ctx, domid, &vifs[i]);
899 }
900 vfb->domid = domid;
901 libxl_device_vfb_add(ctx, domid, vfb);
902 vkb->domid = domid;
903 libxl_device_vkb_add(ctx, domid, vkb);
905 if (info->serial)
906 num_console++;
907 console = libxl_calloc(ctx, num_console, sizeof(libxl_device_console));
908 for (i = 0; i < num_console; i++) {
909 console[i].devid = i;
910 console[i].constype = CONSTYPE_IOEMU;
911 console[i].domid = domid;
912 if (!i)
913 console[i].build_state = &state;
914 libxl_device_console_add(ctx, domid, &console[i]);
915 }
916 if (libxl_create_xenpv_qemu(ctx, vfb, num_console, console, &dm_starting) < 0) {
917 free(args);
918 return -1;
919 }
920 if (libxl_confirm_device_model_startup(ctx, dm_starting) < 0) {
921 free(args);
922 return -1;
923 }
925 libxl_domain_unpause(ctx, domid);
927 if (starting_r) {
928 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
929 (*starting_r)->domid = info->domid;
930 (*starting_r)->dom_path = libxl_xs_get_dompath(ctx, info->domid);
931 (*starting_r)->for_spawn = NULL;
932 }
934 free(args);
935 return 0;
936 }
938 int libxl_create_device_model(struct libxl_ctx *ctx,
939 libxl_device_model_info *info,
940 libxl_device_disk *disks, int num_disks,
941 libxl_device_nic *vifs, int num_vifs,
942 libxl_device_model_starting **starting_r)
943 {
944 char *path, *logfile;
945 int logfile_w, null;
946 int rc;
947 char **args;
948 struct libxl_device_model_starting buf_starting, *p;
950 if (strstr(info->device_model, "stubdom-dm")) {
951 libxl_device_vfb vfb;
952 libxl_device_vkb vkb;
954 libxl_vfb_and_vkb_from_device_model_info(ctx, info, &vfb, &vkb);
955 return libxl_create_stubdom(ctx, info, disks, num_disks, vifs, num_vifs, &vfb, &vkb, starting_r);
956 }
958 args = libxl_build_device_model_args(ctx, info, vifs, num_vifs);
959 if (!args)
960 return ERROR_FAIL;
962 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
963 xs_mkdir(ctx->xsh, XBT_NULL, path);
965 libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", info->dom_name), &logfile);
966 logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
967 free(logfile);
968 null = open("/dev/null", O_RDONLY);
970 if (starting_r) {
971 rc = ERROR_NOMEM;
972 *starting_r = libxl_calloc(ctx, sizeof(libxl_device_model_starting), 1);
973 if (!*starting_r) goto xit;
974 p = *starting_r;
975 p->for_spawn = libxl_calloc(ctx, sizeof(struct libxl_spawn_starting), 1);
976 } else {
977 p = &buf_starting;
978 p->for_spawn = NULL;
979 }
981 p->domid = info->domid;
982 p->dom_path = libxl_xs_get_dompath(ctx, info->domid);
983 if (!p->dom_path) { libxl_free(ctx, p); return ERROR_FAIL; }
985 rc = libxl_spawn_spawn(ctx, p, "device model", dm_xenstore_record_pid);
986 if (rc < 0) goto xit;
987 if (!rc) { /* inner child */
988 libxl_exec(ctx, null, logfile_w, logfile_w,
989 info->device_model, args);
990 }
992 rc = 0;
993 xit:
994 free(args);
995 close(null);
996 close(logfile_w);
998 return rc;
999 }
1001 int libxl_detach_device_model(struct libxl_ctx *ctx,
1002 libxl_device_model_starting *starting)
1004 int rc;
1005 rc = libxl_spawn_detach(ctx, starting->for_spawn);
1006 if (starting->for_spawn) libxl_free(ctx, starting->for_spawn);
1007 libxl_free(ctx, starting);
1008 return rc;
1012 int libxl_confirm_device_model_startup(struct libxl_ctx *ctx,
1013 libxl_device_model_starting *starting)
1015 int problem = libxl_wait_for_device_model(ctx, starting->domid, "running",
1016 libxl_spawn_check,
1017 starting->for_spawn);
1018 int detach = libxl_detach_device_model(ctx, starting);
1019 return problem ? problem : detach;
1020 return 0;
1024 /******************************************************************************/
1026 static int is_blktap2_supported(void)
1028 char buf[1024];
1029 FILE *f = fopen("/proc/devices", "r");
1032 while (fgets(buf, sizeof(buf), f) != NULL) {
1033 if (strstr(buf, "blktap2")) {
1034 fclose(f);
1035 return 1;
1038 fclose(f);
1039 return 0;
1042 static char *get_blktap2_device(struct libxl_ctx *ctx, char *name, char *type)
1044 char buf[1024];
1045 char *p;
1046 int devnum;
1047 FILE *f = fopen("/sys/class/blktap2/devices", "r");
1050 while (!feof(f)) {
1051 fscanf(f, "%d %s", &devnum, buf);
1052 p = strchr(buf, ':');
1053 if (p == NULL)
1054 continue;
1055 p++;
1056 if (!strcmp(p, name) && !strncmp(buf, type, 3)) {
1057 fclose(f);
1058 return libxl_sprintf(ctx, "/dev/xen/blktap-2/tapdev%d", devnum);
1061 fclose(f);
1062 return NULL;
1065 int libxl_device_disk_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1067 flexarray_t *front;
1068 flexarray_t *back;
1069 char *backend_type;
1070 unsigned int boffset = 0;
1071 unsigned int foffset = 0;
1072 int devid;
1073 libxl_device device;
1074 int major, minor;
1076 front = flexarray_make(16, 1);
1077 if (!front)
1078 return ERROR_NOMEM;
1079 back = flexarray_make(16, 1);
1080 if (!back) /* leaks front if error */
1081 return ERROR_NOMEM;
1083 backend_type = device_disk_backend_type_of_phystype(disk->phystype);
1084 devid = device_disk_dev_number(disk->virtpath);
1086 device.backend_devid = devid;
1087 device.backend_domid = disk->backend_domid;
1088 device.devid = devid;
1089 device.domid = disk->domid;
1090 device.kind = DEVICE_VBD;
1092 switch (disk->phystype) {
1093 case PHYSTYPE_PHY: {
1095 device_physdisk_major_minor(disk->physpath, &major, &minor);
1096 flexarray_set(back, boffset++, "physical-device");
1097 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1099 flexarray_set(back, boffset++, "params");
1100 flexarray_set(back, boffset++, disk->physpath);
1102 device.backend_kind = DEVICE_VBD;
1103 break;
1105 case PHYSTYPE_FILE:
1106 /* let's pretend is tap:aio for the moment */
1107 disk->phystype = PHYSTYPE_AIO;
1108 case PHYSTYPE_AIO: case PHYSTYPE_QCOW: case PHYSTYPE_QCOW2: case PHYSTYPE_VHD:
1109 if (is_blktap2_supported()) {
1110 int rc, c, p[2], tot;
1111 char buf[1024], *dev;
1112 dev = get_blktap2_device(ctx, disk->physpath, device_disk_string_of_phystype(disk->phystype));
1113 if (dev == NULL) {
1114 if (pipe(p) < 0) {
1115 XL_LOG(ctx, XL_LOG_ERROR, "Failed to create a pipe");
1116 return -1;
1118 rc = fork();
1119 if (rc < 0) {
1120 XL_LOG(ctx, XL_LOG_ERROR, "Failed to fork a new process");
1121 return -1;
1122 } else if (!rc) { /* child */
1123 int null_r, null_w;
1124 char *args[4];
1125 args[0] = "tapdisk2";
1126 args[1] = "-n";
1127 args[2] = libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath);
1128 args[3] = NULL;
1130 null_r = open("/dev/null", O_RDONLY);
1131 null_w = open("/dev/null", O_WRONLY);
1132 libxl_exec(ctx, null_r, p[1], null_w, "/usr/sbin/tapdisk2", args);
1133 XL_LOG(ctx, XL_LOG_ERROR, "Error execing tapdisk2");
1135 close(p[1]);
1136 tot = 0;
1137 while ((c = read(p[0], buf + tot, sizeof(buf) - tot)) > 0)
1138 tot = tot + c;
1139 close(p[0]);
1140 buf[tot - 1] = '\0';
1141 dev = buf;
1143 flexarray_set(back, boffset++, "tapdisk-params");
1144 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s", device_disk_string_of_phystype(disk->phystype), disk->physpath));
1145 flexarray_set(back, boffset++, "params");
1146 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s", dev));
1147 backend_type = "phy";
1148 device_physdisk_major_minor(dev, &major, &minor);
1149 flexarray_set(back, boffset++, "physical-device");
1150 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x:%x", major, minor));
1151 device.backend_kind = DEVICE_VBD;
1153 break;
1155 flexarray_set(back, boffset++, "params");
1156 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%s:%s",
1157 device_disk_string_of_phystype(disk->phystype), disk->physpath));
1159 device.backend_kind = DEVICE_TAP;
1160 break;
1163 flexarray_set(back, boffset++, "frontend-id");
1164 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", disk->domid));
1165 flexarray_set(back, boffset++, "online");
1166 flexarray_set(back, boffset++, "1");
1167 flexarray_set(back, boffset++, "removable");
1168 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", (disk->unpluggable) ? 1 : 0));
1169 flexarray_set(back, boffset++, "bootable");
1170 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1171 flexarray_set(back, boffset++, "state");
1172 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1173 flexarray_set(back, boffset++, "dev");
1174 flexarray_set(back, boffset++, disk->virtpath);
1175 flexarray_set(back, boffset++, "type");
1176 flexarray_set(back, boffset++, backend_type);
1177 flexarray_set(back, boffset++, "mode");
1178 flexarray_set(back, boffset++, disk->readwrite ? "w" : "r");
1180 flexarray_set(front, foffset++, "backend-id");
1181 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", disk->backend_domid));
1182 flexarray_set(front, foffset++, "state");
1183 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1184 flexarray_set(front, foffset++, "virtual-device");
1185 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", devid));
1186 flexarray_set(front, foffset++, "device-type");
1187 flexarray_set(front, foffset++, disk->is_cdrom ? "cdrom" : "disk");
1189 if (0 /* protocol != native*/) {
1190 flexarray_set(front, foffset++, "protocol");
1191 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1194 libxl_device_generic_add(ctx, &device,
1195 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1196 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1197 flexarray_free(back);
1198 flexarray_free(front);
1199 return 0;
1202 int libxl_device_disk_del(struct libxl_ctx *ctx,
1203 libxl_device_disk *disk, int wait)
1205 libxl_device device;
1206 int devid;
1208 devid = device_disk_dev_number(disk->virtpath);
1209 device.backend_domid = disk->backend_domid;
1210 device.backend_devid = devid;
1211 device.backend_kind =
1212 (disk->phystype == PHYSTYPE_PHY) ? DEVICE_VBD : DEVICE_TAP;
1213 device.domid = disk->domid;
1214 device.devid = devid;
1215 device.kind = DEVICE_VBD;
1216 return libxl_device_del(ctx, &device, wait);
1219 /******************************************************************************/
1220 int libxl_device_nic_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_nic *nic)
1222 flexarray_t *front;
1223 flexarray_t *back;
1224 unsigned int boffset = 0;
1225 unsigned int foffset = 0;
1226 libxl_device device;
1228 front = flexarray_make(16, 1);
1229 if (!front)
1230 return ERROR_NOMEM;
1231 back = flexarray_make(16, 1);
1232 if (!back)
1233 return ERROR_NOMEM;
1235 device.backend_devid = nic->devid;
1236 device.backend_domid = nic->backend_domid;
1237 device.backend_kind = DEVICE_VIF;
1238 device.devid = nic->devid;
1239 device.domid = nic->domid;
1240 device.kind = DEVICE_VIF;
1242 flexarray_set(back, boffset++, "frontend-id");
1243 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->domid));
1244 flexarray_set(back, boffset++, "online");
1245 flexarray_set(back, boffset++, "1");
1246 flexarray_set(back, boffset++, "state");
1247 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1248 flexarray_set(back, boffset++, "script");
1249 flexarray_set(back, boffset++, nic->script);
1250 flexarray_set(back, boffset++, "mac");
1251 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1252 nic->mac[0], nic->mac[1], nic->mac[2],
1253 nic->mac[3], nic->mac[4], nic->mac[5]));
1254 flexarray_set(back, boffset++, "handle");
1255 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", nic->devid));
1257 flexarray_set(front, foffset++, "backend-id");
1258 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->backend_domid));
1259 flexarray_set(front, foffset++, "state");
1260 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1261 flexarray_set(front, foffset++, "handle");
1262 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", nic->devid));
1263 flexarray_set(front, foffset++, "mac");
1264 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%02x:%02x:%02x:%02x:%02x:%02x",
1265 nic->mac[0], nic->mac[1], nic->mac[2],
1266 nic->mac[3], nic->mac[4], nic->mac[5]));
1267 if (0 /* protocol != native*/) {
1268 flexarray_set(front, foffset++, "protocol");
1269 flexarray_set(front, foffset++, "x86_32-abi"); /* hardcoded ! */
1272 libxl_device_generic_add(ctx, &device,
1273 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1274 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1276 /* FIXME: wait for plug */
1277 flexarray_free(back);
1278 flexarray_free(front);
1279 return 0;
1282 int libxl_device_nic_del(struct libxl_ctx *ctx,
1283 libxl_device_nic *nic, int wait)
1285 libxl_device device;
1287 device.backend_devid = nic->devid;
1288 device.backend_domid = nic->backend_domid;
1289 device.backend_kind = DEVICE_VIF;
1290 device.devid = nic->devid;
1291 device.domid = nic->domid;
1292 device.kind = DEVICE_VIF;
1294 return libxl_device_del(ctx, &device, wait);
1297 /******************************************************************************/
1298 int libxl_device_console_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_console *console)
1300 flexarray_t *front;
1301 flexarray_t *back;
1302 unsigned int boffset = 0;
1303 unsigned int foffset = 0;
1304 libxl_device device;
1306 if (console->build_state) {
1307 xs_transaction_t t;
1308 char **ents = (char **) libxl_calloc(ctx, 9, sizeof(char *));
1309 ents[0] = "console/port";
1310 ents[1] = libxl_sprintf(ctx, "%"PRIu32, console->build_state->console_port);
1311 ents[2] = "console/ring-ref";
1312 ents[3] = libxl_sprintf(ctx, "%lu", console->build_state->console_mfn);
1313 ents[4] = "console/limit";
1314 ents[5] = libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT);
1315 ents[6] = "console/type";
1316 if (console->constype == CONSTYPE_XENCONSOLED)
1317 ents[7] = "xenconsoled";
1318 else
1319 ents[7] = "ioemu";
1320 retry_transaction:
1321 t = xs_transaction_start(ctx->xsh);
1322 libxl_xs_writev(ctx, t, libxl_xs_get_dompath(ctx, console->domid), ents);
1323 if (!xs_transaction_end(ctx->xsh, t, 0))
1324 if (errno == EAGAIN)
1325 goto retry_transaction;
1328 front = flexarray_make(16, 1);
1329 if (!front)
1330 return ERROR_NOMEM;
1331 back = flexarray_make(16, 1);
1332 if (!back)
1333 return ERROR_NOMEM;
1335 device.backend_devid = console->devid;
1336 device.backend_domid = console->backend_domid;
1337 device.backend_kind = DEVICE_CONSOLE;
1338 device.devid = console->devid;
1339 device.domid = console->domid;
1340 device.kind = DEVICE_CONSOLE;
1342 flexarray_set(back, boffset++, "frontend-id");
1343 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", console->domid));
1344 flexarray_set(back, boffset++, "online");
1345 flexarray_set(back, boffset++, "1");
1346 flexarray_set(back, boffset++, "state");
1347 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1348 flexarray_set(back, boffset++, "domain");
1349 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1350 flexarray_set(back, boffset++, "protocol");
1351 flexarray_set(back, boffset++, LIBXL_XENCONSOLE_PROTOCOL);
1353 flexarray_set(front, foffset++, "backend-id");
1354 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", console->backend_domid));
1355 flexarray_set(front, foffset++, "state");
1356 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1357 flexarray_set(front, foffset++, "limit");
1358 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", LIBXL_XENCONSOLE_LIMIT));
1359 flexarray_set(front, foffset++, "protocol");
1360 flexarray_set(front, foffset++, LIBXL_XENCONSOLE_PROTOCOL);
1361 flexarray_set(front, foffset++, "type");
1362 if (console->constype == CONSTYPE_XENCONSOLED)
1363 flexarray_set(front, foffset++, "xenconsoled");
1364 else
1365 flexarray_set(front, foffset++, "ioemu");
1367 libxl_device_generic_add(ctx, &device,
1368 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1369 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1370 flexarray_free(back);
1371 flexarray_free(front);
1373 return 0;
1376 /******************************************************************************/
1377 int libxl_device_vkb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vkb *vkb)
1379 flexarray_t *front;
1380 flexarray_t *back;
1381 unsigned int boffset = 0;
1382 unsigned int foffset = 0;
1383 libxl_device device;
1385 front = flexarray_make(16, 1);
1386 if (!front)
1387 return ERROR_NOMEM;
1388 back = flexarray_make(16, 1);
1389 if (!back)
1390 return ERROR_NOMEM;
1392 device.backend_devid = vkb->devid;
1393 device.backend_domid = vkb->backend_domid;
1394 device.backend_kind = DEVICE_VKBD;
1395 device.devid = vkb->devid;
1396 device.domid = vkb->domid;
1397 device.kind = DEVICE_VKBD;
1399 flexarray_set(back, boffset++, "frontend-id");
1400 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vkb->domid));
1401 flexarray_set(back, boffset++, "online");
1402 flexarray_set(back, boffset++, "1");
1403 flexarray_set(back, boffset++, "state");
1404 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1405 flexarray_set(back, boffset++, "domain");
1406 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1408 flexarray_set(front, foffset++, "backend-id");
1409 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vkb->backend_domid));
1410 flexarray_set(front, foffset++, "state");
1411 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1413 libxl_device_generic_add(ctx, &device,
1414 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1415 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1416 flexarray_free(back);
1417 flexarray_free(front);
1419 return 0;
1422 int libxl_device_vkb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1424 return ERROR_NI;
1427 int libxl_device_vkb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1429 return ERROR_NI;
1432 libxl_device_disk *libxl_device_disk_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
1434 char *be_path_tap, *be_path_vbd;
1435 libxl_device_disk *disks = NULL;
1436 char **l = NULL;
1437 unsigned int numl;
1438 int num_disks = 0, i;
1439 char *type;
1441 be_path_vbd = libxl_sprintf(ctx, "%s/backend/vbd/%d", libxl_xs_get_dompath(ctx, 0), domid);
1442 be_path_tap = libxl_sprintf(ctx, "%s/backend/tap/%d", libxl_xs_get_dompath(ctx, 0), domid);
1444 l = libxl_xs_directory(ctx, XBT_NULL, be_path_vbd, &numl);
1445 if (l) {
1446 num_disks += numl;
1447 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
1448 for (i = 0; i < numl; i++) {
1449 disks[i].backend_domid = 0;
1450 disks[i].domid = domid;
1451 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_vbd, l[i]));
1452 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_vbd, l[i])), &(disks[i].phystype));
1453 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_vbd, l[i]));
1454 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_vbd, l[i])));
1455 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_vbd, l[i])), "w"))
1456 disks[i].readwrite = 1;
1457 else
1458 disks[i].readwrite = 0;
1459 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_vbd, l[i]))));
1460 disks[i].is_cdrom = !strcmp(type, "cdrom");
1462 free(l);
1464 l = libxl_xs_directory(ctx, XBT_NULL, be_path_tap, &numl);
1465 if (l) {
1466 num_disks += numl;
1467 disks = realloc(disks, sizeof(libxl_device_disk) * num_disks);
1468 for (i = 0; i < numl; i++) {
1469 disks[i].backend_domid = 0;
1470 disks[i].domid = domid;
1471 disks[i].physpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/params", be_path_tap, l[i]));
1472 libxl_string_to_phystype(ctx, libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/type", be_path_tap, l[i])), &(disks[i].phystype));
1473 disks[i].virtpath = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/dev", be_path_tap, l[i]));
1474 disks[i].unpluggable = atoi(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/removable", be_path_tap, l[i])));
1475 if (!strcmp(libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/mode", be_path_tap, l[i])), "w"))
1476 disks[i].readwrite = 1;
1477 else
1478 disks[i].readwrite = 0;
1479 type = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/device-type", libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/%s/frontend", be_path_vbd, l[i]))));
1480 disks[i].is_cdrom = !strcmp(type, "cdrom");
1482 free(l);
1484 *num = num_disks;
1485 return disks;
1488 int libxl_cdrom_insert(struct libxl_ctx *ctx, uint32_t domid, libxl_device_disk *disk)
1490 int num, i;
1491 uint32_t stubdomid;
1492 libxl_device_disk *disks;
1494 if (!disk->physpath) {
1495 disk->physpath = "";
1496 disk->phystype = PHYSTYPE_PHY;
1498 disks = libxl_device_disk_list(ctx, domid, &num);
1499 for (i = 0; i < num; i++) {
1500 if (disks[i].is_cdrom && !strcmp(disk->virtpath, disks[i].virtpath))
1501 /* found */
1502 break;
1504 if (i == num) {
1505 XL_LOG(ctx, XL_LOG_ERROR, "Virtual device not found");
1506 return -1;
1508 libxl_device_disk_del(ctx, disks + i, 1);
1509 libxl_device_disk_add(ctx, domid, disk);
1510 stubdomid = libxl_get_stubdom_id(ctx, domid);
1511 if (stubdomid) {
1512 disks[i].domid = stubdomid;
1513 libxl_device_disk_del(ctx, disks + i, 1);
1514 disk->domid = stubdomid;
1515 libxl_device_disk_add(ctx, stubdomid, disk);
1516 disk->domid = domid;
1518 return 0;
1521 /******************************************************************************/
1522 static int libxl_build_xenpv_qemu_args(struct libxl_ctx *ctx,
1523 libxl_device_vfb *vfb,
1524 int num_console,
1525 libxl_device_console *console,
1526 libxl_device_model_info *info) {
1527 int i = 0, j = 0, num = 0;
1528 memset(info, 0x00, sizeof(libxl_device_model_info));
1530 info->vnc = vfb->vnc;
1531 if (vfb->vnclisten)
1532 info->vnclisten = libxl_sprintf(ctx, "%s", vfb->vnclisten);
1533 info->vncdisplay = vfb->vncdisplay;
1534 info->vncunused = vfb->vncunused;
1535 if (vfb->keymap)
1536 info->keymap = libxl_sprintf(ctx, "%s", vfb->keymap);
1537 info->sdl = vfb->sdl;
1538 info->opengl = vfb->opengl;
1539 for (i = 0; i < num_console; i++) {
1540 if (console->constype == CONSTYPE_IOEMU)
1541 num++;
1543 if (num > 0) {
1544 uint32_t guest_domid = libxl_is_stubdom(ctx, vfb->domid);
1545 if (guest_domid) {
1546 char *filename;
1547 char *name = libxl_sprintf(ctx, "qemu-dm-%s", libxl_domid_to_name(ctx, guest_domid));
1548 libxl_create_logfile(ctx, name, &filename);
1549 info->serial = libxl_sprintf(ctx, "file:%s", filename);
1550 free(filename);
1551 } else {
1552 info->serial = "pty";
1554 num--;
1556 if (num > 0) {
1557 info->extra = (char **) libxl_calloc(ctx, num * 2 + 1, sizeof(char *));
1558 for (j = 0; j < num * 2; j = j + 2) {
1559 info->extra[j] = "-serial";
1560 info->extra[j + 1] = "pty";
1562 info->extra[j] = NULL;
1564 info->domid = vfb->domid;
1565 info->dom_name = libxl_domid_to_name(ctx, vfb->domid);
1566 info->device_model = "/usr/lib/xen/bin/qemu-dm";
1567 info->type = XENPV;
1568 return 0;
1571 int libxl_create_xenpv_qemu(struct libxl_ctx *ctx, libxl_device_vfb *vfb,
1572 int num_console, libxl_device_console *console,
1573 struct libxl_device_model_starting **starting_r)
1575 libxl_device_model_info info;
1577 libxl_build_xenpv_qemu_args(ctx, vfb, num_console, console, &info);
1578 libxl_create_device_model(ctx, &info, NULL, 0, NULL, 0, starting_r);
1579 return 0;
1582 int libxl_device_vfb_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_vfb *vfb)
1584 flexarray_t *front;
1585 flexarray_t *back;
1586 unsigned int boffset = 0;
1587 unsigned int foffset = 0;
1588 libxl_device device;
1590 front = flexarray_make(16, 1);
1591 if (!front)
1592 return ERROR_NOMEM;
1593 back = flexarray_make(16, 1);
1594 if (!back)
1595 return ERROR_NOMEM;
1597 device.backend_devid = vfb->devid;
1598 device.backend_domid = vfb->backend_domid;
1599 device.backend_kind = DEVICE_VFB;
1600 device.devid = vfb->devid;
1601 device.domid = vfb->domid;
1602 device.kind = DEVICE_VFB;
1604 flexarray_set(back, boffset++, "frontend-id");
1605 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->domid));
1606 flexarray_set(back, boffset++, "online");
1607 flexarray_set(back, boffset++, "1");
1608 flexarray_set(back, boffset++, "state");
1609 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1610 flexarray_set(back, boffset++, "domain");
1611 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1612 flexarray_set(back, boffset++, "vnc");
1613 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vnc));
1614 flexarray_set(back, boffset++, "vnclisten");
1615 flexarray_set(back, boffset++, vfb->vnclisten);
1616 flexarray_set(back, boffset++, "vncdisplay");
1617 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncdisplay));
1618 flexarray_set(back, boffset++, "vncunused");
1619 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->vncunused));
1620 flexarray_set(back, boffset++, "sdl");
1621 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->sdl));
1622 flexarray_set(back, boffset++, "opengl");
1623 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", vfb->opengl));
1624 if (vfb->xauthority) {
1625 flexarray_set(back, boffset++, "xauthority");
1626 flexarray_set(back, boffset++, vfb->xauthority);
1628 if (vfb->display) {
1629 flexarray_set(back, boffset++, "display");
1630 flexarray_set(back, boffset++, vfb->display);
1633 flexarray_set(front, foffset++, "backend-id");
1634 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", vfb->backend_domid));
1635 flexarray_set(front, foffset++, "state");
1636 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1638 libxl_device_generic_add(ctx, &device,
1639 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1640 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1641 flexarray_free(front);
1642 flexarray_free(back);
1644 return 0;
1647 int libxl_device_vfb_clean_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1649 return ERROR_NI;
1652 int libxl_device_vfb_hard_shutdown(struct libxl_ctx *ctx, uint32_t domid)
1654 return ERROR_NI;
1657 /******************************************************************************/
1659 int libxl_device_pci_init(libxl_device_pci *pcidev, unsigned int domain,
1660 unsigned int bus, unsigned int dev,
1661 unsigned int func, unsigned int vdevfn)
1663 pcidev->domain = domain;
1664 pcidev->bus = bus;
1665 pcidev->dev = dev;
1666 pcidev->func = func;
1667 pcidev->vdevfn = vdevfn;
1668 return 0;
1671 static int libxl_create_pci_backend(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev, int num)
1673 flexarray_t *front;
1674 flexarray_t *back;
1675 unsigned int boffset = 0;
1676 unsigned int foffset = 0;
1677 libxl_device device;
1678 int i;
1680 front = flexarray_make(16, 1);
1681 if (!front)
1682 return ERROR_NOMEM;
1683 back = flexarray_make(16, 1);
1684 if (!back)
1685 return ERROR_NOMEM;
1687 XL_LOG(ctx, XL_LOG_DEBUG, "Creating pci backend");
1689 /* add pci device */
1690 device.backend_devid = 0;
1691 device.backend_domid = 0;
1692 device.backend_kind = DEVICE_PCI;
1693 device.devid = 0;
1694 device.domid = domid;
1695 device.kind = DEVICE_PCI;
1697 flexarray_set(back, boffset++, "frontend-id");
1698 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", domid));
1699 flexarray_set(back, boffset++, "online");
1700 flexarray_set(back, boffset++, "1");
1701 flexarray_set(back, boffset++, "state");
1702 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1703 flexarray_set(back, boffset++, "domain");
1704 flexarray_set(back, boffset++, libxl_domid_to_name(ctx, domid));
1705 for (i = 0; i < num; i++) {
1706 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", i));
1707 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1708 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", i));
1709 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1710 if (pcidev->vdevfn) {
1711 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", i));
1712 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
1714 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", i));
1715 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
1716 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", i));
1717 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1719 flexarray_set(back, boffset++, "num_devs");
1720 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num));
1722 flexarray_set(front, foffset++, "backend-id");
1723 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 0));
1724 flexarray_set(front, foffset++, "state");
1725 flexarray_set(front, foffset++, libxl_sprintf(ctx, "%d", 1));
1727 libxl_device_generic_add(ctx, &device,
1728 libxl_xs_kvs_of_flexarray(ctx, back, boffset),
1729 libxl_xs_kvs_of_flexarray(ctx, front, foffset));
1731 flexarray_free(back);
1732 flexarray_free(front);
1733 return 0;
1736 static int libxl_device_pci_add_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1738 flexarray_t *back;
1739 char *num_devs, *be_path;
1740 int num = 0;
1741 unsigned int boffset = 0;
1742 xs_transaction_t t;
1744 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
1745 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
1746 if (!num_devs)
1747 return libxl_create_pci_backend(ctx, domid, pcidev, 1);
1749 if (!is_hvm(ctx, domid)) {
1750 if (libxl_wait_for_backend(ctx, be_path, "4") < 0)
1751 return -1;
1754 back = flexarray_make(16, 1);
1755 if (!back)
1756 return ERROR_NOMEM;
1758 XL_LOG(ctx, XL_LOG_DEBUG, "Adding new pci device to xenstore");
1759 num = atoi(num_devs);
1760 flexarray_set(back, boffset++, libxl_sprintf(ctx, "key-%d", num));
1761 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1762 flexarray_set(back, boffset++, libxl_sprintf(ctx, "dev-%d", num));
1763 flexarray_set(back, boffset++, libxl_sprintf(ctx, PCI_BDF, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func));
1764 if (pcidev->vdevfn) {
1765 flexarray_set(back, boffset++, libxl_sprintf(ctx, "vdevfn-%d", num));
1766 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%x", pcidev->vdevfn));
1768 flexarray_set(back, boffset++, libxl_sprintf(ctx, "opts-%d", num));
1769 flexarray_set(back, boffset++, libxl_sprintf(ctx, "msitranslate=%d,power_mgmt=%d", pcidev->msitranslate, pcidev->power_mgmt));
1770 flexarray_set(back, boffset++, libxl_sprintf(ctx, "state-%d", num));
1771 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 1));
1772 flexarray_set(back, boffset++, "num_devs");
1773 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", num + 1));
1774 flexarray_set(back, boffset++, "state");
1775 flexarray_set(back, boffset++, libxl_sprintf(ctx, "%d", 7));
1777 retry_transaction:
1778 t = xs_transaction_start(ctx->xsh);
1779 libxl_xs_writev(ctx, t, be_path,
1780 libxl_xs_kvs_of_flexarray(ctx, back, boffset));
1781 if (!xs_transaction_end(ctx->xsh, t, 0))
1782 if (errno == EAGAIN)
1783 goto retry_transaction;
1785 flexarray_free(back);
1786 return 0;
1789 static int libxl_device_pci_remove_xenstore(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1791 char *be_path, *num_devs_path, *num_devs, *xsdev, *tmp, *tmppath;
1792 int num, i, j;
1793 xs_transaction_t t;
1794 unsigned int domain = 0, bus = 0, dev = 0, func = 0;
1796 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
1797 num_devs_path = libxl_sprintf(ctx, "%s/num_devs", be_path);
1798 num_devs = libxl_xs_read(ctx, XBT_NULL, num_devs_path);
1799 if (!num_devs)
1800 return -1;
1801 num = atoi(num_devs);
1803 if (!is_hvm(ctx, domid)) {
1804 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
1805 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
1806 return -1;
1810 for (i = 0; i < num; i++) {
1811 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
1812 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
1813 if (domain == pcidev->domain && bus == pcidev->bus &&
1814 pcidev->dev == dev && pcidev->func == func) {
1815 break;
1818 if (i == num) {
1819 XL_LOG(ctx, XL_LOG_ERROR, "Couldn't find the device on xenstore");
1820 return -1;
1823 retry_transaction:
1824 t = xs_transaction_start(ctx->xsh);
1825 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i), "5", strlen("5"));
1826 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state", be_path), "7", strlen("7"));
1827 if (!xs_transaction_end(ctx->xsh, t, 0))
1828 if (errno == EAGAIN)
1829 goto retry_transaction;
1831 if (!is_hvm(ctx, domid)) {
1832 if (libxl_wait_for_backend(ctx, be_path, "4") < 0) {
1833 XL_LOG(ctx, XL_LOG_DEBUG, "pci backend at %s is not ready", be_path);
1834 return -1;
1838 retry_transaction2:
1839 t = xs_transaction_start(ctx->xsh);
1840 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, i));
1841 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, i));
1842 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
1843 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, i));
1844 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
1845 xs_rm(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
1846 libxl_xs_write(ctx, t, num_devs_path, "%d", num - 1);
1847 for (j = i + 1; j < num; j++) {
1848 tmppath = libxl_sprintf(ctx, "%s/state-%d", be_path, j);
1849 tmp = libxl_xs_read(ctx, t, tmppath);
1850 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/state-%d", be_path, j - 1), tmp, strlen(tmp));
1851 xs_rm(ctx->xsh, t, tmppath);
1852 tmppath = libxl_sprintf(ctx, "%s/dev-%d", be_path, j);
1853 tmp = libxl_xs_read(ctx, t, tmppath);
1854 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/dev-%d", be_path, j - 1), tmp, strlen(tmp));
1855 xs_rm(ctx->xsh, t, tmppath);
1856 tmppath = libxl_sprintf(ctx, "%s/key-%d", be_path, j);
1857 tmp = libxl_xs_read(ctx, t, tmppath);
1858 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/key-%d", be_path, j - 1), tmp, strlen(tmp));
1859 xs_rm(ctx->xsh, t, tmppath);
1860 tmppath = libxl_sprintf(ctx, "%s/vdev-%d", be_path, j);
1861 tmp = libxl_xs_read(ctx, t, tmppath);
1862 if (tmp) {
1863 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdev-%d", be_path, j - 1), tmp, strlen(tmp));
1864 xs_rm(ctx->xsh, t, tmppath);
1866 tmppath = libxl_sprintf(ctx, "%s/opts-%d", be_path, j);
1867 tmp = libxl_xs_read(ctx, t, tmppath);
1868 if (tmp) {
1869 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/opts-%d", be_path, j - 1), tmp, strlen(tmp));
1870 xs_rm(ctx->xsh, t, tmppath);
1872 tmppath = libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j);
1873 tmp = libxl_xs_read(ctx, t, tmppath);
1874 if (tmp) {
1875 xs_write(ctx->xsh, t, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, j - 1), tmp, strlen(tmp));
1876 xs_rm(ctx->xsh, t, tmppath);
1879 if (!xs_transaction_end(ctx->xsh, t, 0))
1880 if (errno == EAGAIN)
1881 goto retry_transaction2;
1883 if (num == 1) {
1884 char *fe_path = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/frontend", be_path));
1885 libxl_device_destroy(ctx, be_path, 1);
1886 xs_rm(ctx->xsh, XBT_NULL, be_path);
1887 xs_rm(ctx->xsh, XBT_NULL, fe_path);
1888 return 0;
1891 return 0;
1894 int libxl_device_pci_add(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1896 char *path;
1897 char *state, *vdevfn;
1898 int rc, hvm;
1899 int stubdomid = 0;
1901 /* TODO: check if the device can be assigned */
1903 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
1905 stubdomid = libxl_get_stubdom_id(ctx, domid);
1906 if (stubdomid != 0) {
1907 libxl_device_pci pcidev_s = *pcidev;
1908 libxl_device_pci_add(ctx, stubdomid, &pcidev_s);
1911 hvm = is_hvm(ctx, domid);
1912 if (hvm) {
1913 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
1914 return -1;
1916 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
1917 state = libxl_xs_read(ctx, XBT_NULL, path);
1918 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
1919 if (pcidev->vdevfn)
1920 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF_VDEVFN, pcidev->domain,
1921 pcidev->bus, pcidev->dev, pcidev->func, pcidev->vdevfn);
1922 else
1923 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
1924 pcidev->bus, pcidev->dev, pcidev->func);
1925 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
1926 xs_write(ctx->xsh, XBT_NULL, path, "pci-ins", strlen("pci-ins"));
1927 if (libxl_wait_for_device_model(ctx, domid, "pci-inserted", 0,0) < 0)
1928 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
1929 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
1930 vdevfn = libxl_xs_read(ctx, XBT_NULL, path);
1931 sscanf(vdevfn + 2, "%x", &pcidev->vdevfn);
1932 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
1933 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
1934 } else {
1935 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
1936 pcidev->bus, pcidev->dev, pcidev->func);
1937 FILE *f = fopen(sysfs_path, "r");
1938 unsigned int start = 0, end = 0, flags = 0, size = 0;
1939 int irq = 0;
1940 int i;
1942 if (f == NULL) {
1943 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
1944 return -1;
1946 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
1947 fscanf(f, "0x%x 0x%x 0x%x", &start, &end, &flags);
1948 size = end - start + 1;
1949 if (start) {
1950 if (flags & PCI_BAR_IO) {
1951 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 1);
1952 if (rc < 0)
1953 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_ioport_permission error 0x%x/0x%x", start, size);
1954 } else {
1955 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
1956 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 1);
1957 if (rc < 0)
1958 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_iomem_permission error 0x%x/0x%x", start, size);
1962 fclose(f);
1963 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
1964 pcidev->bus, pcidev->dev, pcidev->func);
1965 f = fopen(sysfs_path, "r");
1966 if (f == NULL) {
1967 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
1968 goto out;
1970 fscanf(f, "%u", &irq);
1971 if (irq) {
1972 rc = xc_physdev_map_pirq(ctx->xch, domid, irq, &irq);
1973 if (rc < 0) {
1974 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_physdev_map_pirq irq=%d", irq);
1976 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 1);
1977 if (rc < 0) {
1978 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "Error: xc_domain_irq_permission irq=%d", irq);
1981 fclose(f);
1983 out:
1984 if (!libxl_is_stubdom(ctx, domid)) {
1985 rc = xc_assign_device(ctx->xch, domid, pcidev->value);
1986 if (rc < 0)
1987 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_assign_device failed");
1990 libxl_device_pci_add_xenstore(ctx, domid, pcidev);
1991 return 0;
1994 int libxl_device_pci_remove(struct libxl_ctx *ctx, uint32_t domid, libxl_device_pci *pcidev)
1996 char *path;
1997 char *state;
1998 int hvm, rc;
1999 int stubdomid = 0;
2001 /* TODO: check if the device can be detached */
2002 libxl_device_pci_remove_xenstore(ctx, domid, pcidev);
2004 hvm = is_hvm(ctx, domid);
2005 if (hvm) {
2006 if (libxl_wait_for_device_model(ctx, domid, "running", 0,0) < 0) {
2007 return -1;
2009 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
2010 state = libxl_xs_read(ctx, XBT_NULL, path);
2011 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/parameter", domid);
2012 libxl_xs_write(ctx, XBT_NULL, path, PCI_BDF, pcidev->domain,
2013 pcidev->bus, pcidev->dev, pcidev->func);
2014 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/command", domid);
2015 xs_write(ctx->xsh, XBT_NULL, path, "pci-rem", strlen("pci-rem"));
2016 if (libxl_wait_for_device_model(ctx, domid, "pci-removed", 0,0) < 0) {
2017 XL_LOG(ctx, XL_LOG_ERROR, "Device Model didn't respond in time");
2018 return -1;
2020 path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d/state", domid);
2021 xs_write(ctx->xsh, XBT_NULL, path, state, strlen(state));
2022 } else {
2023 char *sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/resource", pcidev->domain,
2024 pcidev->bus, pcidev->dev, pcidev->func);
2025 FILE *f = fopen(sysfs_path, "r");
2026 unsigned int start = 0, end = 0, flags = 0, size = 0;
2027 int irq = 0;
2028 int i;
2030 if (f == NULL) {
2031 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2032 goto skip1;
2034 for (i = 0; i < PROC_PCI_NUM_RESOURCES; i++) {
2035 fscanf(f, "0x%x 0x%x 0x%x\n", &start, &end, &flags);
2036 size = end - start + 1;
2037 if (start) {
2038 if (flags & PCI_BAR_IO) {
2039 rc = xc_domain_ioport_permission(ctx->xch, domid, start, size, 0);
2040 if (rc < 0)
2041 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_ioport_permission error 0x%x/0x%x", start, size);
2042 } else {
2043 rc = xc_domain_iomem_permission(ctx->xch, domid, start>>XC_PAGE_SHIFT,
2044 (size+(XC_PAGE_SIZE-1))>>XC_PAGE_SHIFT, 0);
2045 if (rc < 0)
2046 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_iomem_permission error 0x%x/0x%x", start, size);
2050 fclose(f);
2051 skip1:
2052 sysfs_path = libxl_sprintf(ctx, SYSFS_PCI_DEV"/"PCI_BDF"/irq", pcidev->domain,
2053 pcidev->bus, pcidev->dev, pcidev->func);
2054 f = fopen(sysfs_path, "r");
2055 if (f == NULL) {
2056 XL_LOG_ERRNO(ctx, XL_LOG_ERROR, "Couldn't open %s", sysfs_path);
2057 goto out;
2059 fscanf(f, "%u", &irq);
2060 if (irq) {
2061 rc = xc_physdev_unmap_pirq(ctx->xch, domid, irq);
2062 if (rc < 0) {
2063 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_physdev_map_pirq irq=%d", irq);
2065 rc = xc_domain_irq_permission(ctx->xch, domid, irq, 0);
2066 if (rc < 0) {
2067 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_domain_irq_permission irq=%d", irq);
2070 fclose(f);
2072 out:
2073 libxl_device_pci_flr(ctx, pcidev->domain, pcidev->bus, pcidev->dev, pcidev->func);
2075 if (!libxl_is_stubdom(ctx, domid)) {
2076 rc = xc_deassign_device(ctx->xch, domid, pcidev->value);
2077 if (rc < 0)
2078 XL_LOG_ERRNOVAL(ctx, XL_LOG_ERROR, rc, "xc_deassign_device failed");
2081 stubdomid = libxl_get_stubdom_id(ctx, domid);
2082 if (stubdomid != 0) {
2083 libxl_device_pci pcidev_s = *pcidev;
2084 libxl_device_pci_remove(ctx, stubdomid, &pcidev_s);
2087 return 0;
2090 libxl_device_pci *libxl_device_pci_list(struct libxl_ctx *ctx, uint32_t domid, int *num)
2092 char *be_path, *num_devs, *xsdev, *xsvdevfn, *xsopts;
2093 int n, i;
2094 unsigned int domain = 0, bus = 0, dev = 0, func = 0, vdevfn = 0;
2095 libxl_device_pci *pcidevs;
2097 be_path = libxl_sprintf(ctx, "%s/backend/pci/%d/0", libxl_xs_get_dompath(ctx, 0), domid);
2098 num_devs = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/num_devs", be_path));
2099 if (!num_devs) {
2100 *num = 0;
2101 return NULL;
2103 n = atoi(num_devs);
2104 pcidevs = calloc(n, sizeof(libxl_device_pci));
2105 *num = n;
2107 for (i = 0; i < n; i++) {
2108 xsdev = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/dev-%d", be_path, i));
2109 sscanf(xsdev, PCI_BDF, &domain, &bus, &dev, &func);
2110 xsvdevfn = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/vdevfn-%d", be_path, i));
2111 if (xsvdevfn)
2112 vdevfn = strtol(xsvdevfn, (char **) NULL, 16);
2113 libxl_device_pci_init(pcidevs + i, domain, bus, dev, func, vdevfn);
2114 xsopts = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/opts-%d", be_path, i));
2115 if (xsopts) {
2116 char *saveptr;
2117 char *p = strtok_r(xsopts, ",=", &saveptr);
2118 do {
2119 while (*p == ' ')
2120 p++;
2121 if (!strcmp(p, "msitranslate")) {
2122 p = strtok_r(NULL, ",=", &saveptr);
2123 pcidevs[i].msitranslate = atoi(p);
2124 } else if (!strcmp(p, "power_mgmt")) {
2125 p = strtok_r(NULL, ",=", &saveptr);
2126 pcidevs[i].power_mgmt = atoi(p);
2128 } while ((p = strtok_r(NULL, ",=", &saveptr)) != NULL);
2131 return pcidevs;
2134 int libxl_device_pci_shutdown(struct libxl_ctx *ctx, uint32_t domid)
2136 libxl_device_pci *pcidevs;
2137 int num, i;
2139 pcidevs = libxl_device_pci_list(ctx, domid, &num);
2140 for (i = 0; i < num; i++) {
2141 if (libxl_device_pci_remove(ctx, domid, pcidevs + i) < 0)
2142 return -1;
2144 free(pcidevs);
2145 return 0;
2148 int libxl_set_memory_target(struct libxl_ctx *ctx, uint32_t domid, uint32_t target_memkb)
2150 int rc = 0;
2151 uint32_t videoram;
2152 char *videoram_s = NULL;
2153 char *dompath = libxl_xs_get_dompath(ctx, domid);
2155 videoram_s = libxl_xs_read(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/videoram", dompath));
2156 if (!videoram_s)
2157 return -1;
2158 videoram = atoi(videoram_s);
2160 libxl_xs_write(ctx, XBT_NULL, libxl_sprintf(ctx, "%s/memory/target", dompath), "%lu", target_memkb);
2161 rc = xc_domain_setmaxmem(ctx->xch, domid, target_memkb + LIBXL_MAXMEM_CONSTANT);
2162 if (rc != 0)
2163 return rc;
2164 rc = xc_domain_memory_set_pod_target(ctx->xch, domid, (target_memkb - videoram) / 4, NULL, NULL, NULL);
2165 return rc;