1 #include "qemu/osdep.h"
2 #include "hw/xen/xen_backend.h"
3 #include "xen_domainbuild.h"
4 #include "qemu/timer.h"
9 static int xenstore_domain_mkdir(char *path
)
11 struct xs_permissions perms_ro
[] = {{
12 .id
= 0, /* set owner: dom0 */
15 .perms
= XS_PERM_READ
,
17 struct xs_permissions perms_rw
[] = {{
18 .id
= 0, /* set owner: dom0 */
21 .perms
= XS_PERM_READ
| XS_PERM_WRITE
,
23 const char *writable
[] = { "device", "control", "error", NULL
};
27 if (!xs_mkdir(xenstore
, 0, path
)) {
28 fprintf(stderr
, "%s: xs_mkdir %s: failed\n", __FUNCTION__
, path
);
31 if (!xs_set_permissions(xenstore
, 0, path
, perms_ro
, 2)) {
32 fprintf(stderr
, "%s: xs_set_permissions failed\n", __FUNCTION__
);
36 for (i
= 0; writable
[i
]; i
++) {
37 snprintf(subpath
, sizeof(subpath
), "%s/%s", path
, writable
[i
]);
38 if (!xs_mkdir(xenstore
, 0, subpath
)) {
39 fprintf(stderr
, "%s: xs_mkdir %s: failed\n", __FUNCTION__
, subpath
);
42 if (!xs_set_permissions(xenstore
, 0, subpath
, perms_rw
, 2)) {
43 fprintf(stderr
, "%s: xs_set_permissions failed\n", __FUNCTION__
);
50 int xenstore_domain_init1(const char *kernel
, const char *ramdisk
,
53 char *dom
, uuid_string
[42], vm
[256], path
[256];
56 qemu_uuid_unparse(&qemu_uuid
, uuid_string
);
57 dom
= xs_get_domain_path(xenstore
, xen_domid
);
58 snprintf(vm
, sizeof(vm
), "/vm/%s", uuid_string
);
60 xenstore_domain_mkdir(dom
);
62 xenstore_write_str(vm
, "image/ostype", "linux");
64 xenstore_write_str(vm
, "image/kernel", kernel
);
66 xenstore_write_str(vm
, "image/ramdisk", ramdisk
);
68 xenstore_write_str(vm
, "image/cmdline", cmdline
);
71 xenstore_write_str(vm
, "name", qemu_name
? qemu_name
: "no-name");
72 xenstore_write_str(vm
, "uuid", uuid_string
);
73 xenstore_write_str(dom
, "name", qemu_name
? qemu_name
: "no-name");
74 xenstore_write_int(dom
, "domid", xen_domid
);
75 xenstore_write_str(dom
, "vm", vm
);
78 xenstore_write_int(dom
, "memory/target", ram_size
>> 10); // kB
79 xenstore_write_int(vm
, "memory", ram_size
>> 20); // MB
80 xenstore_write_int(vm
, "maxmem", ram_size
>> 20); // MB
83 for (i
= 0; i
< smp_cpus
; i
++) {
84 snprintf(path
, sizeof(path
), "cpu/%d/availability",i
);
85 xenstore_write_str(dom
, path
, "online");
87 xenstore_write_int(vm
, "vcpu_avail", smp_cpus
);
88 xenstore_write_int(vm
, "vcpus", smp_cpus
);
91 xenstore_write_str(vm
, "vncpassword", "" /* FIXME */);
97 int xenstore_domain_init2(int xenstore_port
, int xenstore_mfn
,
98 int console_port
, int console_mfn
)
102 dom
= xs_get_domain_path(xenstore
, xen_domid
);
104 /* signal new domain */
105 xs_introduce_domain(xenstore
,
111 xenstore_write_int(dom
, "store/ring-ref", xenstore_mfn
);
112 xenstore_write_int(dom
, "store/port", xenstore_port
);
115 xenstore_write_str(dom
, "console/type", "ioemu");
116 xenstore_write_int(dom
, "console/limit", 128 * 1024);
117 xenstore_write_int(dom
, "console/ring-ref", console_mfn
);
118 xenstore_write_int(dom
, "console/port", console_port
);
119 xen_config_dev_console(0);
125 /* ------------------------------------------------------------- */
127 static QEMUTimer
*xen_poll
;
129 /* check domain state once per second */
130 static void xen_domain_poll(void *opaque
)
132 struct xc_dominfo info
;
135 rc
= xc_domain_getinfo(xen_xc
, xen_domid
, 1, &info
);
136 if ((rc
!= 1) || (info
.domid
!= xen_domid
)) {
137 qemu_log("xen: domain %d is gone\n", xen_domid
);
141 qemu_log("xen: domain %d is dying (%s%s)\n", xen_domid
,
142 info
.crashed
? "crashed" : "",
143 info
.shutdown
? "shutdown" : "");
147 timer_mod(xen_poll
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);
151 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN
);
154 static int xen_domain_watcher(void)
156 int qemu_running
= 1;
161 qemu_log("%s: Huh? pipe error: %s\n", __FUNCTION__
, strerror(errno
));
165 return 0; /* not child */
167 /* close all file handles, except stdio/out/err,
168 * our watch pipe and the xen interface handle */
170 for (i
= 3; i
< n
; i
++) {
177 * Reopen xc interface, since the original is unsafe after fork
178 * and was closed above.
180 xen_xc
= xc_interface_open(0, 0, 0);
182 /* ignore term signals */
183 signal(SIGINT
, SIG_IGN
);
184 signal(SIGTERM
, SIG_IGN
);
186 /* wait for qemu exiting */
187 while (qemu_running
) {
188 rc
= read(fd
[0], &byte
, 1);
193 qemu_log("%s: Huh? read error: %s\n", __FUNCTION__
, strerror(errno
));
197 /* EOF -> qemu exited */
201 qemu_log("%s: Huh? data on the watch pipe?\n", __FUNCTION__
);
207 qemu_log("%s: destroy domain %d\n", __FUNCTION__
, xen_domid
);
208 xc_domain_destroy(xen_xc
, xen_domid
);
213 static void xen_domain_cleanup(void)
217 dom
= xs_get_domain_path(xenstore
, xen_domid
);
219 xs_rm(xenstore
, 0, dom
);
222 xs_release_domain(xenstore
, xen_domid
);
225 int xen_domain_build_pv(const char *kernel
, const char *ramdisk
,
228 uint32_t ssidref
= 0;
230 xen_domain_handle_t uuid
;
231 unsigned int xenstore_port
= 0, console_port
= 0;
232 unsigned long xenstore_mfn
= 0, console_mfn
= 0;
235 memcpy(uuid
, &qemu_uuid
, sizeof(uuid
));
236 rc
= xen_domain_create(xen_xc
, ssidref
, uuid
, flags
, &xen_domid
);
238 fprintf(stderr
, "xen: xc_domain_create() failed\n");
241 qemu_log("xen: created domain %d\n", xen_domid
);
242 atexit(xen_domain_cleanup
);
243 if (xen_domain_watcher() == -1) {
247 xenstore_domain_init1(kernel
, ramdisk
, cmdline
);
249 rc
= xc_domain_max_vcpus(xen_xc
, xen_domid
, smp_cpus
);
251 fprintf(stderr
, "xen: xc_domain_max_vcpus() failed\n");
256 rc
= xc_domain_setcpuweight(xen_xc
, xen_domid
, 256);
258 fprintf(stderr
, "xen: xc_domain_setcpuweight() failed\n");
263 rc
= xc_domain_setmaxmem(xen_xc
, xen_domid
, ram_size
>> 10);
265 fprintf(stderr
, "xen: xc_domain_setmaxmem() failed\n");
269 xenstore_port
= xc_evtchn_alloc_unbound(xen_xc
, xen_domid
, 0);
270 console_port
= xc_evtchn_alloc_unbound(xen_xc
, xen_domid
, 0);
272 rc
= xc_linux_build(xen_xc
, xen_domid
, ram_size
>> 20,
273 kernel
, ramdisk
, cmdline
,
275 xenstore_port
, &xenstore_mfn
,
276 console_port
, &console_mfn
);
278 fprintf(stderr
, "xen: xc_linux_build() failed\n");
282 xenstore_domain_init2(xenstore_port
, xenstore_mfn
,
283 console_port
, console_mfn
);
285 qemu_log("xen: unpausing domain %d\n", xen_domid
);
286 rc
= xc_domain_unpause(xen_xc
, xen_domid
);
288 fprintf(stderr
, "xen: xc_domain_unpause() failed\n");
292 xen_poll
= timer_new_ms(QEMU_CLOCK_REALTIME
, xen_domain_poll
, NULL
);
293 timer_mod(xen_poll
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) + 1000);