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-legacy-backend.h" 16 #include "hw/xen/xen_pt.h" 17 #include "chardev/char.h" 18 #include "qemu/accel.h" 19 #include "sysemu/cpus.h" 20 #include "sysemu/xen.h" 21 #include "sysemu/runstate.h" 22 #include "migration/misc.h" 23 #include "migration/global_state.h" 24 #include "hw/boards.h" 25 26 bool xen_allowed; 27 28 xc_interface *xen_xc; 29 xenforeignmemory_handle *xen_fmem; 30 xendevicemodel_handle *xen_dmod; 31 32 static int store_dev_info(int domid, Chardev *cs, const char *string) 33 { 34 struct xs_handle *xs = NULL; 35 char *path = NULL; 36 char *newpath = NULL; 37 char *pts = NULL; 38 int ret = -1; 39 40 /* Only continue if we're talking to a pty. */ 41 if (!CHARDEV_IS_PTY(cs)) { 42 return 0; 43 } 44 pts = cs->filename + 4; 45 46 /* We now have everything we need to set the xenstore entry. */ 47 xs = xs_open(0); 48 if (xs == NULL) { 49 fprintf(stderr, "Could not contact XenStore\n"); 50 goto out; 51 } 52 53 path = xs_get_domain_path(xs, domid); 54 if (path == NULL) { 55 fprintf(stderr, "xs_get_domain_path() error\n"); 56 goto out; 57 } 58 newpath = realloc(path, (strlen(path) + strlen(string) + 59 strlen("/tty") + 1)); 60 if (newpath == NULL) { 61 fprintf(stderr, "realloc error\n"); 62 goto out; 63 } 64 path = newpath; 65 66 strcat(path, string); 67 strcat(path, "/tty"); 68 if (!xs_write(xs, XBT_NULL, path, pts, strlen(pts))) { 69 fprintf(stderr, "xs_write for '%s' fail", string); 70 goto out; 71 } 72 ret = 0; 73 74 out: 75 free(path); 76 xs_close(xs); 77 78 return ret; 79 } 80 81 void xenstore_store_pv_console_info(int i, Chardev *chr) 82 { 83 if (i == 0) { 84 store_dev_info(xen_domid, chr, "/console"); 85 } else { 86 char buf[32]; 87 snprintf(buf, sizeof(buf), "/device/console/%d", i); 88 store_dev_info(xen_domid, chr, buf); 89 } 90 } 91 92 93 static void xenstore_record_dm_state(struct xs_handle *xs, const char *state) 94 { 95 char path[50]; 96 97 if (xs == NULL) { 98 error_report("xenstore connection not initialized"); 99 exit(1); 100 } 101 102 snprintf(path, sizeof (path), "device-model/%u/state", xen_domid); 103 /* 104 * This call may fail when running restricted so don't make it fatal in 105 * that case. Toolstacks should instead use QMP to listen for state changes. 106 */ 107 if (!xs_write(xs, XBT_NULL, path, state, strlen(state)) && 108 !xen_domid_restrict) { 109 error_report("error recording dm state"); 110 exit(1); 111 } 112 } 113 114 115 static void xen_change_state_handler(void *opaque, bool running, 116 RunState state) 117 { 118 if (running) { 119 /* record state running */ 120 xenstore_record_dm_state(xenstore, "running"); 121 } 122 } 123 124 static bool xen_get_igd_gfx_passthru(Object *obj, Error **errp) 125 { 126 return xen_igd_gfx_pt_enabled(); 127 } 128 129 static void xen_set_igd_gfx_passthru(Object *obj, bool value, Error **errp) 130 { 131 xen_igd_gfx_pt_set(value, errp); 132 } 133 134 static void xen_setup_post(MachineState *ms, AccelState *accel) 135 { 136 int rc; 137 138 if (xen_domid_restrict) { 139 rc = xen_restrict(xen_domid); 140 if (rc < 0) { 141 perror("xen: failed to restrict"); 142 exit(1); 143 } 144 } 145 } 146 147 static int xen_init(MachineState *ms) 148 { 149 MachineClass *mc = MACHINE_GET_CLASS(ms); 150 151 xen_xc = xc_interface_open(0, 0, 0); 152 if (xen_xc == NULL) { 153 xen_pv_printf(NULL, 0, "can't open xen interface\n"); 154 return -1; 155 } 156 xen_fmem = xenforeignmemory_open(0, 0); 157 if (xen_fmem == NULL) { 158 xen_pv_printf(NULL, 0, "can't open xen fmem interface\n"); 159 xc_interface_close(xen_xc); 160 return -1; 161 } 162 xen_dmod = xendevicemodel_open(0, 0); 163 if (xen_dmod == NULL) { 164 xen_pv_printf(NULL, 0, "can't open xen devicemodel interface\n"); 165 xenforeignmemory_close(xen_fmem); 166 xc_interface_close(xen_xc); 167 return -1; 168 } 169 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL); 170 /* 171 * opt out of system RAM being allocated by generic code 172 */ 173 mc->default_ram_id = NULL; 174 175 xen_mode = XEN_ATTACH; 176 return 0; 177 } 178 179 static void xen_accel_class_init(ObjectClass *oc, void *data) 180 { 181 AccelClass *ac = ACCEL_CLASS(oc); 182 static GlobalProperty compat[] = { 183 { "migration", "store-global-state", "off" }, 184 { "migration", "send-configuration", "off" }, 185 { "migration", "send-section-footer", "off" }, 186 }; 187 188 ac->name = "Xen"; 189 ac->init_machine = xen_init; 190 ac->setup_post = xen_setup_post; 191 ac->allowed = &xen_allowed; 192 ac->compat_props = g_ptr_array_new(); 193 194 compat_props_add(ac->compat_props, compat, G_N_ELEMENTS(compat)); 195 196 object_class_property_add_bool(oc, "igd-passthru", 197 xen_get_igd_gfx_passthru, xen_set_igd_gfx_passthru); 198 object_class_property_set_description(oc, "igd-passthru", 199 "Set on/off to enable/disable igd passthrou"); 200 } 201 202 #define TYPE_XEN_ACCEL ACCEL_CLASS_NAME("xen") 203 204 static const TypeInfo xen_accel_type = { 205 .name = TYPE_XEN_ACCEL, 206 .parent = TYPE_ACCEL, 207 .class_init = xen_accel_class_init, 208 }; 209 210 static void xen_accel_ops_class_init(ObjectClass *oc, void *data) 211 { 212 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc); 213 214 ops->create_vcpu_thread = dummy_start_vcpu_thread; 215 } 216 217 static const TypeInfo xen_accel_ops_type = { 218 .name = ACCEL_OPS_NAME("xen"), 219 220 .parent = TYPE_ACCEL_OPS, 221 .class_init = xen_accel_ops_class_init, 222 .abstract = true, 223 }; 224 225 static void xen_type_init(void) 226 { 227 type_register_static(&xen_accel_type); 228 type_register_static(&xen_accel_ops_type); 229 } 230 type_init(xen_type_init); 231