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