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"
7
8#include <xenguest.h>
9
10static int xenstore_domain_mkdir(char *path)
11{
12 struct xs_permissions perms_ro[] = {{
13 .id = 0,
14 },{
15 .id = xen_domid,
16 .perms = XS_PERM_READ,
17 }};
18 struct xs_permissions perms_rw[] = {{
19 .id = 0,
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;
27
28 if (!xs_mkdir(xenstore, 0, path)) {
29 fprintf(stderr, "%s: xs_mkdir %s: failed\n", __func__, path);
30 return -1;
31 }
32 if (!xs_set_permissions(xenstore, 0, path, perms_ro, 2)) {
33 fprintf(stderr, "%s: xs_set_permissions failed\n", __func__);
34 return -1;
35 }
36
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;
42 }
43 if (!xs_set_permissions(xenstore, 0, subpath, perms_rw, 2)) {
44 fprintf(stderr, "%s: xs_set_permissions failed\n", __func__);
45 return -1;
46 }
47 }
48 return 0;
49}
50
51int xenstore_domain_init1(const char *kernel, const char *ramdisk,
52 const char *cmdline)
53{
54 char *dom, uuid_string[42], vm[256], path[256];
55 int i;
56
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);
60
61 xenstore_domain_mkdir(dom);
62
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);
70
71
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);
77
78
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);
82
83
84 for (i = 0; i < smp_cpus; i++) {
85 snprintf(path, sizeof(path), "cpu/%d/availability",i);
86 xenstore_write_str(dom, path, "online");
87 }
88 xenstore_write_int(vm, "vcpu_avail", smp_cpus);
89 xenstore_write_int(vm, "vcpus", smp_cpus);
90
91
92 xenstore_write_str(vm, "vncpassword", "" );
93
94 free(dom);
95 return 0;
96}
97
98int xenstore_domain_init2(int xenstore_port, int xenstore_mfn,
99 int console_port, int console_mfn)
100{
101 char *dom;
102
103 dom = xs_get_domain_path(xenstore, xen_domid);
104
105
106 xs_introduce_domain(xenstore,
107 xen_domid,
108 xenstore_mfn,
109 xenstore_port);
110
111
112 xenstore_write_int(dom, "store/ring-ref", xenstore_mfn);
113 xenstore_write_int(dom, "store/port", xenstore_port);
114
115
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);
121
122 free(dom);
123 return 0;
124}
125
126
127
128static QEMUTimer *xen_poll;
129
130
131static void xen_domain_poll(void *opaque)
132{
133 struct xc_dominfo info;
134 int rc;
135
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;
140 }
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;
146 }
147
148 timer_mod(xen_poll, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) + 1000);
149 return;
150
151quit:
152 qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
153}
154
155static int xen_domain_watcher(void)
156{
157 int qemu_running = 1;
158 int fd[2], i, n, rc;
159 char byte;
160
161 if (pipe(fd) != 0) {
162 qemu_log("%s: Huh? pipe error: %s\n", __func__, strerror(errno));
163 return -1;
164 }
165 if (fork() != 0)
166 return 0;
167
168
169
170 n = getdtablesize();
171 for (i = 3; i < n; i++) {
172 if (i == fd[0])
173 continue;
174 close(i);
175 }
176
177
178
179
180
181 xen_xc = xc_interface_open(0, 0, 0);
182
183
184 signal(SIGINT, SIG_IGN);
185 signal(SIGTERM, SIG_IGN);
186
187
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
199 qemu_running = 0;
200 break;
201 default:
202 qemu_log("%s: Huh? data on the watch pipe?\n", __func__);
203 break;
204 }
205 }
206
207
208 qemu_log("%s: destroy domain %d\n", __func__, xen_domid);
209 xc_domain_destroy(xen_xc, xen_domid);
210 _exit(0);
211}
212
213
214static void xen_domain_cleanup(void)
215{
216 char *dom;
217
218 dom = xs_get_domain_path(xenstore, xen_domid);
219 if (dom) {
220 xs_rm(xenstore, 0, dom);
221 free(dom);
222 }
223 xs_release_domain(xenstore, xen_domid);
224}
225
226int xen_domain_build_pv(const char *kernel, const char *ramdisk,
227 const char *cmdline)
228{
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;
235
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;
241 }
242 qemu_log("xen: created domain %d\n", xen_domid);
243 atexit(xen_domain_cleanup);
244 if (xen_domain_watcher() == -1) {
245 goto err;
246 }
247
248 xenstore_domain_init1(kernel, ramdisk, cmdline);
249
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;
254 }
255
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;
261 }
262#endif
263
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;
268 }
269
270 xenstore_port = xc_evtchn_alloc_unbound(xen_xc, xen_domid, 0);
271 console_port = xc_evtchn_alloc_unbound(xen_xc, xen_domid, 0);
272
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;
281 }
282
283 xenstore_domain_init2(xenstore_port, xenstore_mfn,
284 console_port, console_mfn);
285
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;
291 }
292
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;
296
297err:
298 return -1;
299}
300