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(const char *state) 94 { 95 struct xs_handle *xs; 96 char path[50]; 97 98 /* We now have everything we need to set the xenstore entry. */ 99 xs = xs_open(0); 100 if (xs == NULL) { 101 fprintf(stderr, "Could not contact XenStore\n"); 102 exit(1); 103 } 104 105 snprintf(path, sizeof (path), "device-model/%u/state", xen_domid); 106 /* 107 * This call may fail when running restricted so don't make it fatal in 108 * that case. Toolstacks should instead use QMP to listen for state changes. 109 */ 110 if (!xs_write(xs, XBT_NULL, path, state, strlen(state)) && 111 !xen_domid_restrict) { 112 error_report("error recording dm state"); 113 exit(1); 114 } 115 116 xs_close(xs); 117 } 118 119 120 static void xen_change_state_handler(void *opaque, bool running, 121 RunState state) 122 { 123 if (running) { 124 /* record state running */ 125 xenstore_record_dm_state("running"); 126 } 127 } 128 129 static bool xen_get_igd_gfx_passthru(Object *obj, Error **errp) 130 { 131 return xen_igd_gfx_pt_enabled(); 132 } 133 134 static void xen_set_igd_gfx_passthru(Object *obj, bool value, Error **errp) 135 { 136 xen_igd_gfx_pt_set(value, errp); 137 } 138 139 static void xen_setup_post(MachineState *ms, AccelState *accel) 140 { 141 int rc; 142 143 if (xen_domid_restrict) { 144 rc = xen_restrict(xen_domid); 145 if (rc < 0) { 146 perror("xen: failed to restrict"); 147 exit(1); 148 } 149 } 150 } 151 152 static int xen_init(MachineState *ms) 153 { 154 MachineClass *mc = MACHINE_GET_CLASS(ms); 155 156 xen_xc = xc_interface_open(0, 0, 0); 157 if (xen_xc == NULL) { 158 xen_pv_printf(NULL, 0, "can't open xen interface\n"); 159 return -1; 160 } 161 xen_fmem = xenforeignmemory_open(0, 0); 162 if (xen_fmem == NULL) { 163 xen_pv_printf(NULL, 0, "can't open xen fmem interface\n"); 164 xc_interface_close(xen_xc); 165 return -1; 166 } 167 xen_dmod = xendevicemodel_open(0, 0); 168 if (xen_dmod == NULL) { 169 xen_pv_printf(NULL, 0, "can't open xen devicemodel interface\n"); 170 xenforeignmemory_close(xen_fmem); 171 xc_interface_close(xen_xc); 172 return -1; 173 } 174 qemu_add_vm_change_state_handler(xen_change_state_handler, NULL); 175 /* 176 * opt out of system RAM being allocated by generic code 177 */ 178 mc->default_ram_id = NULL; 179 180 xen_mode = XEN_ATTACH; 181 return 0; 182 } 183 184 static void xen_accel_class_init(ObjectClass *oc, void *data) 185 { 186 AccelClass *ac = ACCEL_CLASS(oc); 187 static GlobalProperty compat[] = { 188 { "migration", "store-global-state", "off" }, 189 { "migration", "send-configuration", "off" }, 190 { "migration", "send-section-footer", "off" }, 191 }; 192 193 ac->name = "Xen"; 194 ac->init_machine = xen_init; 195 ac->setup_post = xen_setup_post; 196 ac->allowed = &xen_allowed; 197 ac->compat_props = g_ptr_array_new(); 198 199 compat_props_add(ac->compat_props, compat, G_N_ELEMENTS(compat)); 200 201 object_class_property_add_bool(oc, "igd-passthru", 202 xen_get_igd_gfx_passthru, xen_set_igd_gfx_passthru); 203 object_class_property_set_description(oc, "igd-passthru", 204 "Set on/off to enable/disable igd passthrou"); 205 } 206 207 #define TYPE_XEN_ACCEL ACCEL_CLASS_NAME("xen") 208 209 static const TypeInfo xen_accel_type = { 210 .name = TYPE_XEN_ACCEL, 211 .parent = TYPE_ACCEL, 212 .class_init = xen_accel_class_init, 213 }; 214 215 static void xen_accel_ops_class_init(ObjectClass *oc, void *data) 216 { 217 AccelOpsClass *ops = ACCEL_OPS_CLASS(oc); 218 219 ops->create_vcpu_thread = dummy_start_vcpu_thread; 220 } 221 222 static const TypeInfo xen_accel_ops_type = { 223 .name = ACCEL_OPS_NAME("xen"), 224 225 .parent = TYPE_ACCEL_OPS, 226 .class_init = xen_accel_ops_class_init, 227 .abstract = true, 228 }; 229 230 static void xen_type_init(void) 231 { 232 type_register_static(&xen_accel_type); 233 type_register_static(&xen_accel_ops_type); 234 } 235 type_init(xen_type_init); 236