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 "qapi/error.h"
15 #include "hw/xen/xen_native.h"
16 #include "hw/xen/xen-legacy-backend.h"
17 #include "hw/xen/xen_pt.h"
18 #include "hw/xen/xen_igd.h"
19 #include "chardev/char.h"
20 #include "qemu/accel.h"
21 #include "accel/dummy-cpus.h"
22 #include "accel/accel-ops.h"
23 #include "accel/accel-cpu-ops.h"
24 #include "system/cpus.h"
25 #include "system/xen.h"
26 #include "system/runstate.h"
27 #include "migration/misc.h"
28 #include "migration/global_state.h"
29 #include "hw/boards.h"
30
31 bool xen_allowed;
32
33 xc_interface *xen_xc;
34 xenforeignmemory_handle *xen_fmem;
35 xendevicemodel_handle *xen_dmod;
36
xenstore_record_dm_state(const char * state)37 static void xenstore_record_dm_state(const char *state)
38 {
39 char path[50];
40
41 snprintf(path, sizeof (path), "device-model/%u/state", xen_domid);
42 if (!qemu_xen_xs_write(xenstore, XBT_NULL, path, state, strlen(state))) {
43 error_report("error recording dm state");
44 exit(1);
45 }
46 }
47
48
xen_change_state_handler(void * opaque,bool running,RunState state)49 static void xen_change_state_handler(void *opaque, bool running,
50 RunState state)
51 {
52 if (running) {
53 /* record state running */
54 xenstore_record_dm_state("running");
55 }
56 }
57
xen_get_igd_gfx_passthru(Object * obj,Error ** errp)58 static bool xen_get_igd_gfx_passthru(Object *obj, Error **errp)
59 {
60 return xen_igd_gfx_pt_enabled();
61 }
62
xen_set_igd_gfx_passthru(Object * obj,bool value,Error ** errp)63 static void xen_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
64 {
65 xen_igd_gfx_pt_set(value, errp);
66 }
67
xen_setup_post(AccelState * as)68 static void xen_setup_post(AccelState *as)
69 {
70 int rc;
71
72 if (xen_domid_restrict) {
73 rc = xen_restrict(xen_domid);
74 if (rc < 0) {
75 perror("xen: failed to restrict");
76 exit(1);
77 }
78 }
79 }
80
xen_init(AccelState * as,MachineState * ms)81 static int xen_init(AccelState *as, MachineState *ms)
82 {
83 MachineClass *mc = MACHINE_GET_CLASS(ms);
84
85 xen_xc = xc_interface_open(0, 0, 0);
86 if (xen_xc == NULL) {
87 xen_pv_printf(NULL, 0, "can't open xen interface\n");
88 return -1;
89 }
90 xen_fmem = xenforeignmemory_open(0, 0);
91 if (xen_fmem == NULL) {
92 xen_pv_printf(NULL, 0, "can't open xen fmem interface\n");
93 xc_interface_close(xen_xc);
94 return -1;
95 }
96 xen_dmod = xendevicemodel_open(0, 0);
97 if (xen_dmod == NULL) {
98 xen_pv_printf(NULL, 0, "can't open xen devicemodel interface\n");
99 xenforeignmemory_close(xen_fmem);
100 xc_interface_close(xen_xc);
101 return -1;
102 }
103
104 /*
105 * The XenStore write would fail when running restricted so don't attempt
106 * it in that case. Toolstacks should instead use QMP to listen for state
107 * changes.
108 */
109 if (!xen_domid_restrict) {
110 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
111 }
112 /*
113 * opt out of system RAM being allocated by generic code
114 */
115 mc->default_ram_id = NULL;
116
117 xen_mode = XEN_ATTACH;
118 return 0;
119 }
120
xen_accel_class_init(ObjectClass * oc,const void * data)121 static void xen_accel_class_init(ObjectClass *oc, const void *data)
122 {
123 AccelClass *ac = ACCEL_CLASS(oc);
124 static GlobalProperty compat[] = {
125 { "migration", "store-global-state", "off" },
126 { "migration", "send-configuration", "off" },
127 { "migration", "send-section-footer", "off" },
128 };
129
130 ac->name = "Xen";
131 ac->init_machine = xen_init;
132 ac->setup_post = xen_setup_post;
133 ac->allowed = &xen_allowed;
134 ac->compat_props = g_ptr_array_new();
135
136 compat_props_add(ac->compat_props, compat, G_N_ELEMENTS(compat));
137
138 object_class_property_add_bool(oc, "igd-passthru",
139 xen_get_igd_gfx_passthru, xen_set_igd_gfx_passthru);
140 object_class_property_set_description(oc, "igd-passthru",
141 "Set on/off to enable/disable igd passthrou");
142 }
143
144 #define TYPE_XEN_ACCEL ACCEL_CLASS_NAME("xen")
145
146 static const TypeInfo xen_accel_type = {
147 .name = TYPE_XEN_ACCEL,
148 .parent = TYPE_ACCEL,
149 .class_init = xen_accel_class_init,
150 };
151
xen_accel_ops_class_init(ObjectClass * oc,const void * data)152 static void xen_accel_ops_class_init(ObjectClass *oc, const void *data)
153 {
154 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc);
155
156 ops->create_vcpu_thread = dummy_start_vcpu_thread;
157 ops->handle_interrupt = generic_handle_interrupt;
158 }
159
160 static const TypeInfo xen_accel_ops_type = {
161 .name = ACCEL_OPS_NAME("xen"),
162
163 .parent = TYPE_ACCEL_OPS,
164 .class_init = xen_accel_ops_class_init,
165 .abstract = true,
166 };
167
xen_type_init(void)168 static void xen_type_init(void)
169 {
170 type_register_static(&xen_accel_type);
171 type_register_static(&xen_accel_ops_type);
172 }
173 type_init(xen_type_init);
174