nbd: Remove x-nbd-server-add-bitmap
[qemu/ar7.git] / hw / xenpv / xen_domainbuild.c
blob2859280a6a6dae5e907ec0c3c773ae50e17c51a5
1 #include "qemu/osdep.h"
2 #include "qemu/units.h"
3 #include "hw/xen/xen_backend.h"
4 #include "xen_domainbuild.h"
5 #include "qemu/timer.h"
6 #include "qemu/log.h"
8 #include <xenguest.h>
10 static int xenstore_domain_mkdir(char *path)
12 struct xs_permissions perms_ro[] = {{
13 .id = 0, /* set owner: dom0 */
14 },{
15 .id = xen_domid,
16 .perms = XS_PERM_READ,
17 }};
18 struct xs_permissions perms_rw[] = {{
19 .id = 0, /* set owner: dom0 */
20 },{
21 .id = xen_domid,
22 .perms = XS_PERM_READ | XS_PERM_WRITE,
23 }};
24 const char *writable[] = { "device", "control", "error", NULL };
25 char subpath[256];
26 int i;
28 if (!xs_mkdir(xenstore, 0, path)) {
29 fprintf(stderr, "%s: xs_mkdir %s: failed\n", __func__, path);
30 return -1;
32 if (!xs_set_permissions(xenstore, 0, path, perms_ro, 2)) {
33 fprintf(stderr, "%s: xs_set_permissions failed\n", __func__);
34 return -1;
37 for (i = 0; writable[i]; i++) {
38 snprintf(subpath, sizeof(subpath), "%s/%s", path, writable[i]);
39 if (!xs_mkdir(xenstore, 0, subpath)) {
40 fprintf(stderr, "%s: xs_mkdir %s: failed\n", __func__, subpath);
41 return -1;
43 if (!xs_set_permissions(xenstore, 0, subpath, perms_rw, 2)) {
44 fprintf(stderr, "%s: xs_set_permissions failed\n", __func__);
45 return -1;
48 return 0;
51 int xenstore_domain_init1(const char *kernel, const char *ramdisk,
52 const char *cmdline)
54 char *dom, uuid_string[42], vm[256], path[256];
55 int i;
57 qemu_uuid_unparse(&qemu_uuid, uuid_string);
58 dom = xs_get_domain_path(xenstore, xen_domid);
59 snprintf(vm, sizeof(vm), "/vm/%s", uuid_string);
61 xenstore_domain_mkdir(dom);
63 xenstore_write_str(vm, "image/ostype", "linux");
64 if (kernel)
65 xenstore_write_str(vm, "image/kernel", kernel);
66 if (ramdisk)
67 xenstore_write_str(vm, "image/ramdisk", ramdisk);
68 if (cmdline)
69 xenstore_write_str(vm, "image/cmdline", cmdline);
71 /* name + id */
72 xenstore_write_str(vm, "name", qemu_name ? qemu_name : "no-name");
73 xenstore_write_str(vm, "uuid", uuid_string);
74 xenstore_write_str(dom, "name", qemu_name ? qemu_name : "no-name");
75 xenstore_write_int(dom, "domid", xen_domid);
76 xenstore_write_str(dom, "vm", vm);
78 /* memory */
79 xenstore_write_int(dom, "memory/target", ram_size / KiB);
80 xenstore_write_int(vm, "memory", ram_size / MiB);
81 xenstore_write_int(vm, "maxmem", ram_size / MiB);
83 /* cpus */
84 for (i = 0; i < smp_cpus; i++) {
85 snprintf(path, sizeof(path), "cpu/%d/availability",i);
86 xenstore_write_str(dom, path, "online");
88 xenstore_write_int(vm, "vcpu_avail", smp_cpus);
89 xenstore_write_int(vm, "vcpus", smp_cpus);
91 /* vnc password */
92 xenstore_write_str(vm, "vncpassword", "" /* FIXME */);
94 free(dom);
95 return 0;
98 int xenstore_domain_init2(int xenstore_port, int xenstore_mfn,
99 int console_port, int console_mfn)
101 char *dom;
103 dom = xs_get_domain_path(xenstore, xen_domid);
105 /* signal new domain */
106 xs_introduce_domain(xenstore,
107 xen_domid,
108 xenstore_mfn,
109 xenstore_port);
111 /* xenstore */
112 xenstore_write_int(dom, "store/ring-ref", xenstore_mfn);
113 xenstore_write_int(dom, "store/port", xenstore_port);
115 /* console */
116 xenstore_write_str(dom, "console/type", "ioemu");
117 xenstore_write_int(dom, "console/limit", 128 * KiB);
118 xenstore_write_int(dom, "console/ring-ref", console_mfn);
119 xenstore_write_int(dom, "console/port", console_port);
120 xen_config_dev_console(0);
122 free(dom);
123 return 0;
126 /* ------------------------------------------------------------- */
128 static QEMUTimer *xen_poll;
130 /* check domain state once per second */
131 static void xen_domain_poll(void *opaque)
133 struct xc_dominfo info;
134 int rc;
136 rc = xc_domain_getinfo(xen_xc, xen_domid, 1, &info);
137 if ((rc != 1) || (info.domid != xen_domid)) {
138 qemu_log("xen: domain %d is gone\n", xen_domid);
139 goto quit;
141 if (info.dying) {
142 qemu_log("xen: domain %d is dying (%s%s)\n", xen_domid,
143 info.crashed ? "crashed" : "",
144 info.shutdown ? "shutdown" : "");
145 goto quit;
148 timer_mod(xen_poll, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
149 return;
151 quit:
152 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
155 static int xen_domain_watcher(void)
157 int qemu_running = 1;
158 int fd[2], i, n, rc;
159 char byte;
161 if (pipe(fd) != 0) {
162 qemu_log("%s: Huh? pipe error: %s\n", __func__, strerror(errno));
163 return -1;
165 if (fork() != 0)
166 return 0; /* not child */
168 /* close all file handles, except stdio/out/err,
169 * our watch pipe and the xen interface handle */
170 n = getdtablesize();
171 for (i = 3; i < n; i++) {
172 if (i == fd[0])
173 continue;
174 close(i);
178 * Reopen xc interface, since the original is unsafe after fork
179 * and was closed above.
181 xen_xc = xc_interface_open(0, 0, 0);
183 /* ignore term signals */
184 signal(SIGINT, SIG_IGN);
185 signal(SIGTERM, SIG_IGN);
187 /* wait for qemu exiting */
188 while (qemu_running) {
189 rc = read(fd[0], &byte, 1);
190 switch (rc) {
191 case -1:
192 if (errno == EINTR)
193 continue;
194 qemu_log("%s: Huh? read error: %s\n", __func__, strerror(errno));
195 qemu_running = 0;
196 break;
197 case 0:
198 /* EOF -> qemu exited */
199 qemu_running = 0;
200 break;
201 default:
202 qemu_log("%s: Huh? data on the watch pipe?\n", __func__);
203 break;
207 /* cleanup */
208 qemu_log("%s: destroy domain %d\n", __func__, xen_domid);
209 xc_domain_destroy(xen_xc, xen_domid);
210 _exit(0);
213 /* normal cleanup */
214 static void xen_domain_cleanup(void)
216 char *dom;
218 dom = xs_get_domain_path(xenstore, xen_domid);
219 if (dom) {
220 xs_rm(xenstore, 0, dom);
221 free(dom);
223 xs_release_domain(xenstore, xen_domid);
226 int xen_domain_build_pv(const char *kernel, const char *ramdisk,
227 const char *cmdline)
229 uint32_t ssidref = 0;
230 uint32_t flags = 0;
231 xen_domain_handle_t uuid;
232 unsigned int xenstore_port = 0, console_port = 0;
233 unsigned long xenstore_mfn = 0, console_mfn = 0;
234 int rc;
236 memcpy(uuid, &qemu_uuid, sizeof(uuid));
237 rc = xen_domain_create(xen_xc, ssidref, uuid, flags, &xen_domid);
238 if (rc < 0) {
239 fprintf(stderr, "xen: xc_domain_create() failed\n");
240 goto err;
242 qemu_log("xen: created domain %d\n", xen_domid);
243 atexit(xen_domain_cleanup);
244 if (xen_domain_watcher() == -1) {
245 goto err;
248 xenstore_domain_init1(kernel, ramdisk, cmdline);
250 rc = xc_domain_max_vcpus(xen_xc, xen_domid, smp_cpus);
251 if (rc < 0) {
252 fprintf(stderr, "xen: xc_domain_max_vcpus() failed\n");
253 goto err;
256 #if 0
257 rc = xc_domain_setcpuweight(xen_xc, xen_domid, 256);
258 if (rc < 0) {
259 fprintf(stderr, "xen: xc_domain_setcpuweight() failed\n");
260 goto err;
262 #endif
264 rc = xc_domain_setmaxmem(xen_xc, xen_domid, ram_size / KiB);
265 if (rc < 0) {
266 fprintf(stderr, "xen: xc_domain_setmaxmem() failed\n");
267 goto err;
270 xenstore_port = xc_evtchn_alloc_unbound(xen_xc, xen_domid, 0);
271 console_port = xc_evtchn_alloc_unbound(xen_xc, xen_domid, 0);
273 rc = xc_linux_build(xen_xc, xen_domid, ram_size / MiB,
274 kernel, ramdisk, cmdline,
275 0, flags,
276 xenstore_port, &xenstore_mfn,
277 console_port, &console_mfn);
278 if (rc < 0) {
279 fprintf(stderr, "xen: xc_linux_build() failed\n");
280 goto err;
283 xenstore_domain_init2(xenstore_port, xenstore_mfn,
284 console_port, console_mfn);
286 qemu_log("xen: unpausing domain %d\n", xen_domid);
287 rc = xc_domain_unpause(xen_xc, xen_domid);
288 if (rc < 0) {
289 fprintf(stderr, "xen: xc_domain_unpause() failed\n");
290 goto err;
293 xen_poll = timer_new_ms(QEMU_CLOCK_REALTIME, xen_domain_poll, NULL);
294 timer_mod(xen_poll, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
295 return 0;
297 err:
298 return -1;