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