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/qdev-properties.h" 34 #include "sysemu/hw_accel.h" 35 #include "qemu/log.h" 36 #include "vmport.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_READ_SET_EAX \ 50 (1 << VMPORT_COMPAT_READ_SET_EAX_BIT) 51 #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD \ 52 (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT) 53 54 #define VMPORT(obj) OBJECT_CHECK(VMPortState, (obj), TYPE_VMPORT) 55 56 typedef struct VMPortState { 57 ISADevice parent_obj; 58 59 MemoryRegion io; 60 VMPortReadFunc *func[VMPORT_ENTRIES]; 61 void *opaque[VMPORT_ENTRIES]; 62 63 uint32_t compat_flags; 64 } VMPortState; 65 66 static VMPortState *port_state; 67 68 void vmport_register(unsigned char command, VMPortReadFunc *func, void *opaque) 69 { 70 if (command >= VMPORT_ENTRIES) { 71 return; 72 } 73 74 trace_vmport_register(command, func, opaque); 75 port_state->func[command] = func; 76 port_state->opaque[command] = opaque; 77 } 78 79 static uint64_t vmport_ioport_read(void *opaque, hwaddr addr, 80 unsigned size) 81 { 82 VMPortState *s = opaque; 83 CPUState *cs = current_cpu; 84 X86CPU *cpu = X86_CPU(cs); 85 CPUX86State *env = &cpu->env; 86 unsigned char command; 87 uint32_t eax; 88 89 cpu_synchronize_state(cs); 90 91 eax = env->regs[R_EAX]; 92 if (eax != VMPORT_MAGIC) { 93 goto err; 94 } 95 96 command = env->regs[R_ECX]; 97 trace_vmport_command(command); 98 if (command >= VMPORT_ENTRIES || !s->func[command]) { 99 qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command); 100 goto err; 101 } 102 103 eax = s->func[command](s->opaque[command], addr); 104 goto out; 105 106 err: 107 if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) { 108 eax = UINT32_MAX; 109 } 110 111 out: 112 /* 113 * The call above to cpu_synchronize_state() gets vCPU registers values 114 * to QEMU but also cause QEMU to write QEMU vCPU registers values to 115 * vCPU implementation (e.g. Accelerator such as KVM) just before 116 * resuming guest. 117 * 118 * Therefore, in order to make IOPort return value propagate to 119 * guest EAX, we need to explicitly update QEMU EAX register value. 120 */ 121 if (s->compat_flags & VMPORT_COMPAT_READ_SET_EAX) { 122 cpu->env.regs[R_EAX] = eax; 123 } 124 125 return eax; 126 } 127 128 static void vmport_ioport_write(void *opaque, hwaddr addr, 129 uint64_t val, unsigned size) 130 { 131 X86CPU *cpu = X86_CPU(current_cpu); 132 133 cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4); 134 } 135 136 static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr) 137 { 138 X86CPU *cpu = X86_CPU(current_cpu); 139 140 cpu->env.regs[R_EBX] = VMPORT_MAGIC; 141 return 6; 142 } 143 144 static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr) 145 { 146 X86CPU *cpu = X86_CPU(current_cpu); 147 148 cpu->env.regs[R_EBX] = 0x1177; 149 return ram_size; 150 } 151 152 static const MemoryRegionOps vmport_ops = { 153 .read = vmport_ioport_read, 154 .write = vmport_ioport_write, 155 .impl = { 156 .min_access_size = 4, 157 .max_access_size = 4, 158 }, 159 .endianness = DEVICE_LITTLE_ENDIAN, 160 }; 161 162 static void vmport_realizefn(DeviceState *dev, Error **errp) 163 { 164 ISADevice *isadev = ISA_DEVICE(dev); 165 VMPortState *s = VMPORT(dev); 166 167 memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1); 168 isa_register_ioport(isadev, &s->io, 0x5658); 169 170 port_state = s; 171 /* Register some generic port commands */ 172 vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL); 173 vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL); 174 } 175 176 static Property vmport_properties[] = { 177 /* Used to enforce compatibility for migration */ 178 DEFINE_PROP_BIT("x-read-set-eax", VMPortState, compat_flags, 179 VMPORT_COMPAT_READ_SET_EAX_BIT, true), 180 DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags, 181 VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true), 182 DEFINE_PROP_END_OF_LIST(), 183 }; 184 185 static void vmport_class_initfn(ObjectClass *klass, void *data) 186 { 187 DeviceClass *dc = DEVICE_CLASS(klass); 188 189 dc->realize = vmport_realizefn; 190 /* Reason: realize sets global port_state */ 191 dc->user_creatable = false; 192 device_class_set_props(dc, vmport_properties); 193 } 194 195 static const TypeInfo vmport_info = { 196 .name = TYPE_VMPORT, 197 .parent = TYPE_ISA_DEVICE, 198 .instance_size = sizeof(VMPortState), 199 .class_init = vmport_class_initfn, 200 }; 201 202 static void vmport_register_types(void) 203 { 204 type_register_static(&vmport_info); 205 } 206 207 type_init(vmport_register_types) 208