1 /* 2 * QEMU VMPort emulation 3 * 4 * Copyright (C) 2007 Hervé Poussineau 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a copy 7 * of this software and associated documentation files (the "Software"), to deal 8 * in the Software without restriction, including without limitation the rights 9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10 * copies of the Software, and to permit persons to whom the Software is 11 * furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22 * THE SOFTWARE. 23 */ 24 25 /* 26 * Guest code that interacts with this virtual device can be found 27 * in VMware open-vm-tools open-source project: 28 * https://github.com/vmware/open-vm-tools 29 */ 30 31 #include "qemu/osdep.h" 32 #include "hw/isa/isa.h" 33 #include "hw/i386/vmport.h" 34 #include "hw/qdev-properties.h" 35 #include "hw/boards.h" 36 #include "sysemu/sysemu.h" 37 #include "sysemu/hw_accel.h" 38 #include "sysemu/qtest.h" 39 #include "qemu/log.h" 40 #include "cpu.h" 41 #include "trace.h" 42 #include "qom/object.h" 43 44 #define VMPORT_MAGIC 0x564D5868 45 46 /* Compatibility flags for migration */ 47 #define VMPORT_COMPAT_READ_SET_EAX_BIT 0 48 #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT 1 49 #define VMPORT_COMPAT_REPORT_VMX_TYPE_BIT 2 50 #define VMPORT_COMPAT_CMDS_V2_BIT 3 51 #define VMPORT_COMPAT_READ_SET_EAX \ 52 (1 << VMPORT_COMPAT_READ_SET_EAX_BIT) 53 #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD \ 54 (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT) 55 #define VMPORT_COMPAT_REPORT_VMX_TYPE \ 56 (1 << VMPORT_COMPAT_REPORT_VMX_TYPE_BIT) 57 #define VMPORT_COMPAT_CMDS_V2 \ 58 (1 << VMPORT_COMPAT_CMDS_V2_BIT) 59 60 /* vCPU features reported by CMD_GET_VCPU_INFO */ 61 #define VCPU_INFO_SLC64_BIT 0 62 #define VCPU_INFO_SYNC_VTSCS_BIT 1 63 #define VCPU_INFO_HV_REPLAY_OK_BIT 2 64 #define VCPU_INFO_LEGACY_X2APIC_BIT 3 65 #define VCPU_INFO_RESERVED_BIT 31 66 67 OBJECT_DECLARE_SIMPLE_TYPE(VMPortState, VMPORT) 68 69 struct VMPortState { 70 ISADevice parent_obj; 71 72 MemoryRegion io; 73 VMPortReadFunc *func[VMPORT_ENTRIES]; 74 void *opaque[VMPORT_ENTRIES]; 75 76 uint32_t vmware_vmx_version; 77 uint8_t vmware_vmx_type; 78 79 uint32_t compat_flags; 80 }; 81 82 static VMPortState *port_state; 83 84 void vmport_register(VMPortCommand command, VMPortReadFunc *func, void *opaque) 85 { 86 assert(command < VMPORT_ENTRIES); 87 assert(port_state); 88 89 trace_vmport_register(command, func, opaque); 90 port_state->func[command] = func; 91 port_state->opaque[command] = opaque; 92 } 93 94 static uint64_t vmport_ioport_read(void *opaque, hwaddr addr, 95 unsigned size) 96 { 97 VMPortState *s = opaque; 98 CPUState *cs = current_cpu; 99 X86CPU *cpu = X86_CPU(cs); 100 CPUX86State *env; 101 unsigned char command; 102 uint32_t eax; 103 104 if (qtest_enabled()) { 105 return -1; 106 } 107 env = &cpu->env; 108 cpu_synchronize_state(cs); 109 110 eax = env->regs[R_EAX]; 111 if (eax != VMPORT_MAGIC) { 112 goto err; 113 } 114 115 command = env->regs[R_ECX]; 116 trace_vmport_command(command); 117 if (command >= VMPORT_ENTRIES || !s->func[command]) { 118 qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command); 119 goto err; 120 } 121 122 eax = s->func[command](s->opaque[command], addr); 123 goto out; 124 125 err: 126 if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) { 127 eax = UINT32_MAX; 128 } 129 130 out: 131 /* 132 * The call above to cpu_synchronize_state() gets vCPU registers values 133 * to QEMU but also cause QEMU to write QEMU vCPU registers values to 134 * vCPU implementation (e.g. Accelerator such as KVM) just before 135 * resuming guest. 136 * 137 * Therefore, in order to make IOPort return value propagate to 138 * guest EAX, we need to explicitly update QEMU EAX register value. 139 */ 140 if (s->compat_flags & VMPORT_COMPAT_READ_SET_EAX) { 141 cpu->env.regs[R_EAX] = eax; 142 } 143 144 return eax; 145 } 146 147 static void vmport_ioport_write(void *opaque, hwaddr addr, 148 uint64_t val, unsigned size) 149 { 150 X86CPU *cpu = X86_CPU(current_cpu); 151 152 if (qtest_enabled()) { 153 return; 154 } 155 cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4); 156 } 157 158 static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr) 159 { 160 X86CPU *cpu = X86_CPU(current_cpu); 161 162 if (qtest_enabled()) { 163 return -1; 164 } 165 cpu->env.regs[R_EBX] = VMPORT_MAGIC; 166 if (port_state->compat_flags & VMPORT_COMPAT_REPORT_VMX_TYPE) { 167 cpu->env.regs[R_ECX] = port_state->vmware_vmx_type; 168 } 169 return port_state->vmware_vmx_version; 170 } 171 172 static uint32_t vmport_cmd_get_bios_uuid(void *opaque, uint32_t addr) 173 { 174 X86CPU *cpu = X86_CPU(current_cpu); 175 uint32_t *uuid_parts = (uint32_t *)(qemu_uuid.data); 176 177 cpu->env.regs[R_EAX] = le32_to_cpu(uuid_parts[0]); 178 cpu->env.regs[R_EBX] = le32_to_cpu(uuid_parts[1]); 179 cpu->env.regs[R_ECX] = le32_to_cpu(uuid_parts[2]); 180 cpu->env.regs[R_EDX] = le32_to_cpu(uuid_parts[3]); 181 return cpu->env.regs[R_EAX]; 182 } 183 184 static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr) 185 { 186 X86CPU *cpu = X86_CPU(current_cpu); 187 188 if (qtest_enabled()) { 189 return -1; 190 } 191 cpu->env.regs[R_EBX] = 0x1177; 192 return current_machine->ram_size; 193 } 194 195 static uint32_t vmport_cmd_get_hz(void *opaque, uint32_t addr) 196 { 197 X86CPU *cpu = X86_CPU(current_cpu); 198 199 if (cpu->env.tsc_khz && cpu->env.apic_bus_freq) { 200 uint64_t tsc_freq = (uint64_t)cpu->env.tsc_khz * 1000; 201 202 cpu->env.regs[R_ECX] = cpu->env.apic_bus_freq; 203 cpu->env.regs[R_EBX] = (uint32_t)(tsc_freq >> 32); 204 cpu->env.regs[R_EAX] = (uint32_t)tsc_freq; 205 } else { 206 /* Signal cmd as not supported */ 207 cpu->env.regs[R_EBX] = UINT32_MAX; 208 } 209 210 return cpu->env.regs[R_EAX]; 211 } 212 213 static uint32_t vmport_cmd_get_vcpu_info(void *opaque, uint32_t addr) 214 { 215 X86CPU *cpu = X86_CPU(current_cpu); 216 uint32_t ret = 0; 217 218 if (cpu->env.features[FEAT_1_ECX] & CPUID_EXT_X2APIC) { 219 ret |= 1 << VCPU_INFO_LEGACY_X2APIC_BIT; 220 } 221 222 return ret; 223 } 224 225 static const MemoryRegionOps vmport_ops = { 226 .read = vmport_ioport_read, 227 .write = vmport_ioport_write, 228 .impl = { 229 .min_access_size = 4, 230 .max_access_size = 4, 231 }, 232 .endianness = DEVICE_LITTLE_ENDIAN, 233 }; 234 235 static void vmport_realizefn(DeviceState *dev, Error **errp) 236 { 237 ISADevice *isadev = ISA_DEVICE(dev); 238 VMPortState *s = VMPORT(dev); 239 240 memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1); 241 isa_register_ioport(isadev, &s->io, 0x5658); 242 243 port_state = s; 244 245 /* Register some generic port commands */ 246 vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL); 247 vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL); 248 if (s->compat_flags & VMPORT_COMPAT_CMDS_V2) { 249 vmport_register(VMPORT_CMD_GETBIOSUUID, vmport_cmd_get_bios_uuid, NULL); 250 vmport_register(VMPORT_CMD_GETHZ, vmport_cmd_get_hz, NULL); 251 vmport_register(VMPORT_CMD_GET_VCPU_INFO, vmport_cmd_get_vcpu_info, 252 NULL); 253 } 254 } 255 256 static Property vmport_properties[] = { 257 /* Used to enforce compatibility for migration */ 258 DEFINE_PROP_BIT("x-read-set-eax", VMPortState, compat_flags, 259 VMPORT_COMPAT_READ_SET_EAX_BIT, true), 260 DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags, 261 VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true), 262 DEFINE_PROP_BIT("x-report-vmx-type", VMPortState, compat_flags, 263 VMPORT_COMPAT_REPORT_VMX_TYPE_BIT, true), 264 DEFINE_PROP_BIT("x-cmds-v2", VMPortState, compat_flags, 265 VMPORT_COMPAT_CMDS_V2_BIT, true), 266 267 /* Default value taken from open-vm-tools code VERSION_MAGIC definition */ 268 DEFINE_PROP_UINT32("vmware-vmx-version", VMPortState, 269 vmware_vmx_version, 6), 270 /* 271 * Value determines which VMware product type host report itself to guest. 272 * 273 * Most guests are fine with exposing host as VMware ESX server. 274 * Some legacy/proprietary guests hard-code a given type. 275 * 276 * For a complete list of values, refer to enum VMXType at open-vm-tools 277 * project (Defined at lib/include/vm_vmx_type.h). 278 * 279 * Reasonable options: 280 * 0 - Unset 281 * 1 - VMware Express (deprecated) 282 * 2 - VMware ESX Server 283 * 3 - VMware Server (Deprecated) 284 * 4 - VMware Workstation 285 * 5 - ACE 1.x (Deprecated) 286 */ 287 DEFINE_PROP_UINT8("vmware-vmx-type", VMPortState, vmware_vmx_type, 2), 288 289 DEFINE_PROP_END_OF_LIST(), 290 }; 291 292 static void vmport_class_initfn(ObjectClass *klass, void *data) 293 { 294 DeviceClass *dc = DEVICE_CLASS(klass); 295 296 dc->realize = vmport_realizefn; 297 /* Reason: realize sets global port_state */ 298 dc->user_creatable = false; 299 device_class_set_props(dc, vmport_properties); 300 } 301 302 static const TypeInfo vmport_info = { 303 .name = TYPE_VMPORT, 304 .parent = TYPE_ISA_DEVICE, 305 .instance_size = sizeof(VMPortState), 306 .class_init = vmport_class_initfn, 307 }; 308 309 static void vmport_register_types(void) 310 { 311 type_register_static(&vmport_info); 312 } 313 314 type_init(vmport_register_types) 315