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