1 /*
2 * Copyright (C) 2014 Citrix Systems UK Ltd.
3 *
4 * This work is licensed under the terms of the GNU GPL, version 2. See
5 * the COPYING file in the top-level directory.
6 *
7 * Contributions after 2012-01-13 are licensed under the terms of the
8 * GNU GPL, version 2 or (at your option) any later version.
9 */
10
11 #include "qemu/osdep.h"
12 #include "qemu/error-report.h"
13 #include "qemu/module.h"
14 #include "hw/xen/xen-legacy-backend.h"
15 #include "chardev/char.h"
16 #include "sysemu/accel.h"
17 #include "sysemu/runstate.h"
18 #include "migration/misc.h"
19 #include "migration/global_state.h"
20
21 //#define DEBUG_XEN
22
23 #ifdef DEBUG_XEN
24 #define DPRINTF(fmt, ...) \
25 do { fprintf(stderr, "xen: " fmt, ## __VA_ARGS__); } while (0)
26 #else
27 #define DPRINTF(fmt, ...) \
28 do { } while (0)
29 #endif
30
31 xc_interface *xen_xc;
32 xenforeignmemory_handle *xen_fmem;
33 xendevicemodel_handle *xen_dmod;
34
store_dev_info(int domid,Chardev * cs,const char * string)35 static int store_dev_info(int domid, Chardev *cs, const char *string)
36 {
37 struct xs_handle *xs = NULL;
38 char *path = NULL;
39 char *newpath = NULL;
40 char *pts = NULL;
41 int ret = -1;
42
43 /* Only continue if we're talking to a pty. */
44 if (!CHARDEV_IS_PTY(cs)) {
45 return 0;
46 }
47 pts = cs->filename + 4;
48
49 /* We now have everything we need to set the xenstore entry. */
50 xs = xs_open(0);
51 if (xs == NULL) {
52 fprintf(stderr, "Could not contact XenStore\n");
53 goto out;
54 }
55
56 path = xs_get_domain_path(xs, domid);
57 if (path == NULL) {
58 fprintf(stderr, "xs_get_domain_path() error\n");
59 goto out;
60 }
61 newpath = realloc(path, (strlen(path) + strlen(string) +
62 strlen("/tty") + 1));
63 if (newpath == NULL) {
64 fprintf(stderr, "realloc error\n");
65 goto out;
66 }
67 path = newpath;
68
69 strcat(path, string);
70 strcat(path, "/tty");
71 if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) {
72 fprintf(stderr, "xs_write for '%s' fail", string);
73 goto out;
74 }
75 ret = 0;
76
77 out:
78 free(path);
79 xs_close(xs);
80
81 return ret;
82 }
83
xenstore_store_pv_console_info(int i,Chardev * chr)84 void xenstore_store_pv_console_info(int i, Chardev *chr)
85 {
86 if (i == 0) {
87 store_dev_info(xen_domid, chr, "/console");
88 } else {
89 char buf[32];
90 snprintf(buf, sizeof(buf), "/device/console/%d", i);
91 store_dev_info(xen_domid, chr, buf);
92 }
93 }
94
95
xenstore_record_dm_state(struct xs_handle * xs,const char * state)96 static void xenstore_record_dm_state(struct xs_handle *xs, const char *state)
97 {
98 char path[50];
99
100 if (xs == NULL) {
101 error_report("xenstore connection not initialized");
102 exit(1);
103 }
104
105 snprintf(path, sizeof (path), "device-model/%u/state", xen_domid);
106 /*
107 * This call may fail when running restricted so don't make it fatal in
108 * that case. Toolstacks should instead use QMP to listen for state changes.
109 */
110 if (!xs_write(xs, XBT_NULL, path, state, strlen(state)) &&
111 !xen_domid_restrict) {
112 error_report("error recording dm state");
113 exit(1);
114 }
115 }
116
117
xen_change_state_handler(void * opaque,int running,RunState state)118 static void xen_change_state_handler(void *opaque, int running,
119 RunState state)
120 {
121 if (running) {
122 /* record state running */
123 xenstore_record_dm_state(xenstore, "running");
124 }
125 }
126
xen_setup_post(MachineState * ms,AccelState * accel)127 static void xen_setup_post(MachineState *ms, AccelState *accel)
128 {
129 int rc;
130
131 if (xen_domid_restrict) {
132 rc = xen_restrict(xen_domid);
133 if (rc < 0) {
134 perror("xen: failed to restrict");
135 exit(1);
136 }
137 }
138 }
139
xen_init(MachineState * ms)140 static int xen_init(MachineState *ms)
141 {
142 xen_xc = xc_interface_open(0, 0, 0);
143 if (xen_xc == NULL) {
144 xen_pv_printf(NULL, 0, "can't open xen interface\n");
145 return -1;
146 }
147 xen_fmem = xenforeignmemory_open(0, 0);
148 if (xen_fmem == NULL) {
149 xen_pv_printf(NULL, 0, "can't open xen fmem interface\n");
150 xc_interface_close(xen_xc);
151 return -1;
152 }
153 xen_dmod = xendevicemodel_open(0, 0);
154 if (xen_dmod == NULL) {
155 xen_pv_printf(NULL, 0, "can't open xen devicemodel interface\n");
156 xenforeignmemory_close(xen_fmem);
157 xc_interface_close(xen_xc);
158 return -1;
159 }
160 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
161 return 0;
162 }
163
xen_accel_class_init(ObjectClass * oc,void * data)164 static void xen_accel_class_init(ObjectClass *oc, void *data)
165 {
166 AccelClass *ac = ACCEL_CLASS(oc);
167 static GlobalProperty compat[] = {
168 { "migration", "store-global-state", "off" },
169 { "migration", "send-configuration", "off" },
170 { "migration", "send-section-footer", "off" },
171 };
172
173 ac->name = "Xen";
174 ac->init_machine = xen_init;
175 ac->setup_post = xen_setup_post;
176 ac->allowed = &xen_allowed;
177 ac->compat_props = g_ptr_array_new();
178
179 compat_props_add(ac->compat_props, compat, G_N_ELEMENTS(compat));
180 }
181
182 #define TYPE_XEN_ACCEL ACCEL_CLASS_NAME("xen")
183
184 static const TypeInfo xen_accel_type = {
185 .name = TYPE_XEN_ACCEL,
186 .parent = TYPE_ACCEL,
187 .class_init = xen_accel_class_init,
188 };
189
xen_type_init(void)190 static void xen_type_init(void)
191 {
192 type_register_static(&xen_accel_type);
193 }
194
195 type_init(xen_type_init);
196