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