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 "sysemu/hw_accel.h" 36 #include "qemu/log.h" 37 #include "cpu.h" 38 #include "trace.h" 39 40 #define VMPORT_CMD_GETVERSION 0x0a 41 #define VMPORT_CMD_GETRAMSIZE 0x14 42 43 #define VMPORT_ENTRIES 0x2c 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_READ_SET_EAX \ 51 (1 << VMPORT_COMPAT_READ_SET_EAX_BIT) 52 #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD \ 53 (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT) 54 #define VMPORT_COMPAT_REPORT_VMX_TYPE \ 55 (1 << VMPORT_COMPAT_REPORT_VMX_TYPE_BIT) 56 57 #define VMPORT(obj) OBJECT_CHECK(VMPortState, (obj), TYPE_VMPORT) 58 59 typedef struct VMPortState { 60 ISADevice parent_obj; 61 62 MemoryRegion io; 63 VMPortReadFunc *func[VMPORT_ENTRIES]; 64 void *opaque[VMPORT_ENTRIES]; 65 66 uint32_t vmware_vmx_version; 67 uint8_t vmware_vmx_type; 68 69 uint32_t compat_flags; 70 } VMPortState; 71 72 static VMPortState *port_state; 73 74 void vmport_register(unsigned char command, VMPortReadFunc *func, void *opaque) 75 { 76 if (command >= VMPORT_ENTRIES) { 77 return; 78 } 79 80 trace_vmport_register(command, func, opaque); 81 port_state->func[command] = func; 82 port_state->opaque[command] = opaque; 83 } 84 85 static uint64_t vmport_ioport_read(void *opaque, hwaddr addr, 86 unsigned size) 87 { 88 VMPortState *s = opaque; 89 CPUState *cs = current_cpu; 90 X86CPU *cpu = X86_CPU(cs); 91 CPUX86State *env = &cpu->env; 92 unsigned char command; 93 uint32_t eax; 94 95 cpu_synchronize_state(cs); 96 97 eax = env->regs[R_EAX]; 98 if (eax != VMPORT_MAGIC) { 99 goto err; 100 } 101 102 command = env->regs[R_ECX]; 103 trace_vmport_command(command); 104 if (command >= VMPORT_ENTRIES || !s->func[command]) { 105 qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command); 106 goto err; 107 } 108 109 eax = s->func[command](s->opaque[command], addr); 110 goto out; 111 112 err: 113 if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) { 114 eax = UINT32_MAX; 115 } 116 117 out: 118 /* 119 * The call above to cpu_synchronize_state() gets vCPU registers values 120 * to QEMU but also cause QEMU to write QEMU vCPU registers values to 121 * vCPU implementation (e.g. Accelerator such as KVM) just before 122 * resuming guest. 123 * 124 * Therefore, in order to make IOPort return value propagate to 125 * guest EAX, we need to explicitly update QEMU EAX register value. 126 */ 127 if (s->compat_flags & VMPORT_COMPAT_READ_SET_EAX) { 128 cpu->env.regs[R_EAX] = eax; 129 } 130 131 return eax; 132 } 133 134 static void vmport_ioport_write(void *opaque, hwaddr addr, 135 uint64_t val, unsigned size) 136 { 137 X86CPU *cpu = X86_CPU(current_cpu); 138 139 cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4); 140 } 141 142 static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr) 143 { 144 X86CPU *cpu = X86_CPU(current_cpu); 145 146 cpu->env.regs[R_EBX] = VMPORT_MAGIC; 147 if (port_state->compat_flags & VMPORT_COMPAT_REPORT_VMX_TYPE) { 148 cpu->env.regs[R_ECX] = port_state->vmware_vmx_type; 149 } 150 return port_state->vmware_vmx_version; 151 } 152 153 static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr) 154 { 155 X86CPU *cpu = X86_CPU(current_cpu); 156 157 cpu->env.regs[R_EBX] = 0x1177; 158 return ram_size; 159 } 160 161 static const MemoryRegionOps vmport_ops = { 162 .read = vmport_ioport_read, 163 .write = vmport_ioport_write, 164 .impl = { 165 .min_access_size = 4, 166 .max_access_size = 4, 167 }, 168 .endianness = DEVICE_LITTLE_ENDIAN, 169 }; 170 171 static void vmport_realizefn(DeviceState *dev, Error **errp) 172 { 173 ISADevice *isadev = ISA_DEVICE(dev); 174 VMPortState *s = VMPORT(dev); 175 176 memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1); 177 isa_register_ioport(isadev, &s->io, 0x5658); 178 179 port_state = s; 180 /* Register some generic port commands */ 181 vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL); 182 vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL); 183 } 184 185 static Property vmport_properties[] = { 186 /* Used to enforce compatibility for migration */ 187 DEFINE_PROP_BIT("x-read-set-eax", VMPortState, compat_flags, 188 VMPORT_COMPAT_READ_SET_EAX_BIT, true), 189 DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags, 190 VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true), 191 DEFINE_PROP_BIT("x-report-vmx-type", VMPortState, compat_flags, 192 VMPORT_COMPAT_REPORT_VMX_TYPE_BIT, true), 193 194 /* Default value taken from open-vm-tools code VERSION_MAGIC definition */ 195 DEFINE_PROP_UINT32("vmware-vmx-version", VMPortState, 196 vmware_vmx_version, 6), 197 /* 198 * Value determines which VMware product type host report itself to guest. 199 * 200 * Most guests are fine with exposing host as VMware ESX server. 201 * Some legacy/proprietary guests hard-code a given type. 202 * 203 * For a complete list of values, refer to enum VMXType at open-vm-tools 204 * project (Defined at lib/include/vm_vmx_type.h). 205 * 206 * Reasonable options: 207 * 0 - Unset 208 * 1 - VMware Express (deprecated) 209 * 2 - VMware ESX Server 210 * 3 - VMware Server (Deprecated) 211 * 4 - VMware Workstation 212 * 5 - ACE 1.x (Deprecated) 213 */ 214 DEFINE_PROP_UINT8("vmware-vmx-type", VMPortState, vmware_vmx_type, 2), 215 216 DEFINE_PROP_END_OF_LIST(), 217 }; 218 219 static void vmport_class_initfn(ObjectClass *klass, void *data) 220 { 221 DeviceClass *dc = DEVICE_CLASS(klass); 222 223 dc->realize = vmport_realizefn; 224 /* Reason: realize sets global port_state */ 225 dc->user_creatable = false; 226 device_class_set_props(dc, vmport_properties); 227 } 228 229 static const TypeInfo vmport_info = { 230 .name = TYPE_VMPORT, 231 .parent = TYPE_ISA_DEVICE, 232 .instance_size = sizeof(VMPortState), 233 .class_init = vmport_class_initfn, 234 }; 235 236 static void vmport_register_types(void) 237 { 238 type_register_static(&vmport_info); 239 } 240 241 type_init(vmport_register_types) 242