]>
Commit | Line | Data |
---|---|---|
0975c304 TS |
1 | /* |
2 | * QEMU VMPort emulation | |
3 | * | |
bcc4e41f | 4 | * Copyright (C) 2007 Hervé Poussineau |
0975c304 TS |
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 | */ | |
29282253 LA |
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 | ||
0d1c9782 | 31 | #include "qemu/osdep.h" |
0d09e41a | 32 | #include "hw/isa/isa.h" |
d8f23d61 | 33 | #include "hw/i386/vmport.h" |
c9ab24ce | 34 | #include "hw/qdev-properties.h" |
aaacf1c1 | 35 | #include "sysemu/sysemu.h" |
b3946626 | 36 | #include "sysemu/hw_accel.h" |
43ab9a53 | 37 | #include "qemu/log.h" |
d8a05995 | 38 | #include "cpu.h" |
7299e1a4 | 39 | #include "trace.h" |
61ada15d | 40 | |
0975c304 TS |
41 | #define VMPORT_MAGIC 0x564D5868 |
42 | ||
b8892129 | 43 | /* Compatibility flags for migration */ |
0342ee76 LA |
44 | #define VMPORT_COMPAT_READ_SET_EAX_BIT 0 |
45 | #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT 1 | |
f8bdc550 | 46 | #define VMPORT_COMPAT_REPORT_VMX_TYPE_BIT 2 |
aaacf1c1 | 47 | #define VMPORT_COMPAT_CMDS_V2_BIT 3 |
0342ee76 | 48 | #define VMPORT_COMPAT_READ_SET_EAX \ |
b8892129 | 49 | (1 << VMPORT_COMPAT_READ_SET_EAX_BIT) |
0342ee76 LA |
50 | #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD \ |
51 | (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT) | |
f8bdc550 LA |
52 | #define VMPORT_COMPAT_REPORT_VMX_TYPE \ |
53 | (1 << VMPORT_COMPAT_REPORT_VMX_TYPE_BIT) | |
aaacf1c1 LA |
54 | #define VMPORT_COMPAT_CMDS_V2 \ |
55 | (1 << VMPORT_COMPAT_CMDS_V2_BIT) | |
b8892129 | 56 | |
acacd355 LA |
57 | /* vCPU features reported by CMD_GET_VCPU_INFO */ |
58 | #define VCPU_INFO_SLC64_BIT 0 | |
59 | #define VCPU_INFO_SYNC_VTSCS_BIT 1 | |
60 | #define VCPU_INFO_HV_REPLAY_OK_BIT 2 | |
61 | #define VCPU_INFO_LEGACY_X2APIC_BIT 3 | |
62 | #define VCPU_INFO_RESERVED_BIT 31 | |
63 | ||
f02317ad AF |
64 | #define VMPORT(obj) OBJECT_CHECK(VMPortState, (obj), TYPE_VMPORT) |
65 | ||
323d7d1d | 66 | typedef struct VMPortState { |
f02317ad AF |
67 | ISADevice parent_obj; |
68 | ||
f75317b4 | 69 | MemoryRegion io; |
d67f679d | 70 | VMPortReadFunc *func[VMPORT_ENTRIES]; |
0975c304 | 71 | void *opaque[VMPORT_ENTRIES]; |
b8892129 | 72 | |
2fd2f799 | 73 | uint32_t vmware_vmx_version; |
f8bdc550 | 74 | uint8_t vmware_vmx_type; |
2fd2f799 | 75 | |
b8892129 | 76 | uint32_t compat_flags; |
0975c304 TS |
77 | } VMPortState; |
78 | ||
e14da0af | 79 | static VMPortState *port_state; |
0975c304 | 80 | |
dcd938f0 | 81 | void vmport_register(VMPortCommand command, VMPortReadFunc *func, void *opaque) |
0975c304 | 82 | { |
dcd938f0 | 83 | assert(command < VMPORT_ENTRIES); |
23accdf1 LA |
84 | assert(port_state); |
85 | ||
7299e1a4 | 86 | trace_vmport_register(command, func, opaque); |
e14da0af MT |
87 | port_state->func[command] = func; |
88 | port_state->opaque[command] = opaque; | |
0975c304 TS |
89 | } |
90 | ||
360d613e AG |
91 | static uint64_t vmport_ioport_read(void *opaque, hwaddr addr, |
92 | unsigned size) | |
0975c304 TS |
93 | { |
94 | VMPortState *s = opaque; | |
4917cf44 AF |
95 | CPUState *cs = current_cpu; |
96 | X86CPU *cpu = X86_CPU(cs); | |
97 | CPUX86State *env = &cpu->env; | |
0975c304 | 98 | unsigned char command; |
6dab28d5 | 99 | uint32_t eax; |
0975c304 | 100 | |
4917cf44 | 101 | cpu_synchronize_state(cs); |
03c63b94 | 102 | |
26fb5e48 | 103 | eax = env->regs[R_EAX]; |
323d7d1d | 104 | if (eax != VMPORT_MAGIC) { |
0342ee76 | 105 | goto err; |
323d7d1d | 106 | } |
0975c304 | 107 | |
26fb5e48 | 108 | command = env->regs[R_ECX]; |
7299e1a4 PMD |
109 | trace_vmport_command(command); |
110 | if (command >= VMPORT_ENTRIES || !s->func[command]) { | |
111 | qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command); | |
0342ee76 | 112 | goto err; |
b8892129 LA |
113 | } |
114 | ||
115 | eax = s->func[command](s->opaque[command], addr); | |
0342ee76 LA |
116 | goto out; |
117 | ||
118 | err: | |
119 | if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) { | |
120 | eax = UINT32_MAX; | |
121 | } | |
b8892129 LA |
122 | |
123 | out: | |
124 | /* | |
125 | * The call above to cpu_synchronize_state() gets vCPU registers values | |
126 | * to QEMU but also cause QEMU to write QEMU vCPU registers values to | |
127 | * vCPU implementation (e.g. Accelerator such as KVM) just before | |
128 | * resuming guest. | |
129 | * | |
130 | * Therefore, in order to make IOPort return value propagate to | |
131 | * guest EAX, we need to explicitly update QEMU EAX register value. | |
132 | */ | |
133 | if (s->compat_flags & VMPORT_COMPAT_READ_SET_EAX) { | |
134 | cpu->env.regs[R_EAX] = eax; | |
0975c304 TS |
135 | } |
136 | ||
b8892129 | 137 | return eax; |
0975c304 TS |
138 | } |
139 | ||
360d613e AG |
140 | static void vmport_ioport_write(void *opaque, hwaddr addr, |
141 | uint64_t val, unsigned size) | |
e9396bde | 142 | { |
4917cf44 | 143 | X86CPU *cpu = X86_CPU(current_cpu); |
e9396bde | 144 | |
4917cf44 | 145 | cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4); |
e9396bde AL |
146 | } |
147 | ||
0975c304 TS |
148 | static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr) |
149 | { | |
4917cf44 AF |
150 | X86CPU *cpu = X86_CPU(current_cpu); |
151 | ||
152 | cpu->env.regs[R_EBX] = VMPORT_MAGIC; | |
f8bdc550 LA |
153 | if (port_state->compat_flags & VMPORT_COMPAT_REPORT_VMX_TYPE) { |
154 | cpu->env.regs[R_ECX] = port_state->vmware_vmx_type; | |
155 | } | |
2fd2f799 | 156 | return port_state->vmware_vmx_version; |
0975c304 TS |
157 | } |
158 | ||
aaacf1c1 LA |
159 | static uint32_t vmport_cmd_get_bios_uuid(void *opaque, uint32_t addr) |
160 | { | |
161 | X86CPU *cpu = X86_CPU(current_cpu); | |
162 | uint32_t *uuid_parts = (uint32_t *)(qemu_uuid.data); | |
163 | ||
164 | cpu->env.regs[R_EAX] = le32_to_cpu(uuid_parts[0]); | |
165 | cpu->env.regs[R_EBX] = le32_to_cpu(uuid_parts[1]); | |
166 | cpu->env.regs[R_ECX] = le32_to_cpu(uuid_parts[2]); | |
167 | cpu->env.regs[R_EDX] = le32_to_cpu(uuid_parts[3]); | |
168 | return cpu->env.regs[R_EAX]; | |
169 | } | |
170 | ||
0975c304 TS |
171 | static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr) |
172 | { | |
4917cf44 AF |
173 | X86CPU *cpu = X86_CPU(current_cpu); |
174 | ||
175 | cpu->env.regs[R_EBX] = 0x1177; | |
0975c304 TS |
176 | return ram_size; |
177 | } | |
178 | ||
d6048bfd LA |
179 | static uint32_t vmport_cmd_get_hz(void *opaque, uint32_t addr) |
180 | { | |
181 | X86CPU *cpu = X86_CPU(current_cpu); | |
182 | ||
183 | if (cpu->env.tsc_khz && cpu->env.apic_bus_freq) { | |
184 | uint64_t tsc_freq = (uint64_t)cpu->env.tsc_khz * 1000; | |
185 | ||
186 | cpu->env.regs[R_ECX] = cpu->env.apic_bus_freq; | |
187 | cpu->env.regs[R_EBX] = (uint32_t)(tsc_freq >> 32); | |
188 | cpu->env.regs[R_EAX] = (uint32_t)tsc_freq; | |
189 | } else { | |
190 | /* Signal cmd as not supported */ | |
191 | cpu->env.regs[R_EBX] = UINT32_MAX; | |
192 | } | |
193 | ||
194 | return cpu->env.regs[R_EAX]; | |
195 | } | |
196 | ||
acacd355 LA |
197 | static uint32_t vmport_cmd_get_vcpu_info(void *opaque, uint32_t addr) |
198 | { | |
7f9114b7 LA |
199 | X86CPU *cpu = X86_CPU(current_cpu); |
200 | uint32_t ret = 0; | |
201 | ||
202 | if (cpu->env.features[FEAT_1_ECX] & CPUID_EXT_X2APIC) { | |
203 | ret |= 1 << VCPU_INFO_LEGACY_X2APIC_BIT; | |
204 | } | |
205 | ||
206 | return ret; | |
acacd355 LA |
207 | } |
208 | ||
f75317b4 | 209 | static const MemoryRegionOps vmport_ops = { |
360d613e AG |
210 | .read = vmport_ioport_read, |
211 | .write = vmport_ioport_write, | |
212 | .impl = { | |
213 | .min_access_size = 4, | |
214 | .max_access_size = 4, | |
215 | }, | |
216 | .endianness = DEVICE_LITTLE_ENDIAN, | |
f75317b4 RH |
217 | }; |
218 | ||
db895a1e | 219 | static void vmport_realizefn(DeviceState *dev, Error **errp) |
0975c304 | 220 | { |
db895a1e | 221 | ISADevice *isadev = ISA_DEVICE(dev); |
f02317ad | 222 | VMPortState *s = VMPORT(dev); |
0975c304 | 223 | |
3c161542 | 224 | memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1); |
db895a1e | 225 | isa_register_ioport(isadev, &s->io, 0x5658); |
f75317b4 | 226 | |
e14da0af | 227 | port_state = s; |
aaacf1c1 | 228 | |
0975c304 | 229 | /* Register some generic port commands */ |
26fb5e48 AJ |
230 | vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL); |
231 | vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL); | |
aaacf1c1 LA |
232 | if (s->compat_flags & VMPORT_COMPAT_CMDS_V2) { |
233 | vmport_register(VMPORT_CMD_GETBIOSUUID, vmport_cmd_get_bios_uuid, NULL); | |
d6048bfd | 234 | vmport_register(VMPORT_CMD_GETHZ, vmport_cmd_get_hz, NULL); |
acacd355 LA |
235 | vmport_register(VMPORT_CMD_GET_VCPU_INFO, vmport_cmd_get_vcpu_info, |
236 | NULL); | |
aaacf1c1 | 237 | } |
0975c304 | 238 | } |
6872ef61 | 239 | |
c9ab24ce | 240 | static Property vmport_properties[] = { |
b8892129 LA |
241 | /* Used to enforce compatibility for migration */ |
242 | DEFINE_PROP_BIT("x-read-set-eax", VMPortState, compat_flags, | |
243 | VMPORT_COMPAT_READ_SET_EAX_BIT, true), | |
0342ee76 LA |
244 | DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags, |
245 | VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true), | |
f8bdc550 LA |
246 | DEFINE_PROP_BIT("x-report-vmx-type", VMPortState, compat_flags, |
247 | VMPORT_COMPAT_REPORT_VMX_TYPE_BIT, true), | |
aaacf1c1 LA |
248 | DEFINE_PROP_BIT("x-cmds-v2", VMPortState, compat_flags, |
249 | VMPORT_COMPAT_CMDS_V2_BIT, true), | |
2fd2f799 LA |
250 | |
251 | /* Default value taken from open-vm-tools code VERSION_MAGIC definition */ | |
252 | DEFINE_PROP_UINT32("vmware-vmx-version", VMPortState, | |
253 | vmware_vmx_version, 6), | |
f8bdc550 LA |
254 | /* |
255 | * Value determines which VMware product type host report itself to guest. | |
256 | * | |
257 | * Most guests are fine with exposing host as VMware ESX server. | |
258 | * Some legacy/proprietary guests hard-code a given type. | |
259 | * | |
260 | * For a complete list of values, refer to enum VMXType at open-vm-tools | |
261 | * project (Defined at lib/include/vm_vmx_type.h). | |
262 | * | |
263 | * Reasonable options: | |
264 | * 0 - Unset | |
265 | * 1 - VMware Express (deprecated) | |
266 | * 2 - VMware ESX Server | |
267 | * 3 - VMware Server (Deprecated) | |
268 | * 4 - VMware Workstation | |
269 | * 5 - ACE 1.x (Deprecated) | |
270 | */ | |
271 | DEFINE_PROP_UINT8("vmware-vmx-type", VMPortState, vmware_vmx_type, 2), | |
2fd2f799 | 272 | |
c9ab24ce LA |
273 | DEFINE_PROP_END_OF_LIST(), |
274 | }; | |
275 | ||
8f04ee08 AL |
276 | static void vmport_class_initfn(ObjectClass *klass, void *data) |
277 | { | |
39bffca2 | 278 | DeviceClass *dc = DEVICE_CLASS(klass); |
db895a1e AF |
279 | |
280 | dc->realize = vmport_realizefn; | |
f3b17640 | 281 | /* Reason: realize sets global port_state */ |
e90f2a8c | 282 | dc->user_creatable = false; |
c9ab24ce | 283 | device_class_set_props(dc, vmport_properties); |
8f04ee08 AL |
284 | } |
285 | ||
8c43a6f0 | 286 | static const TypeInfo vmport_info = { |
f02317ad | 287 | .name = TYPE_VMPORT, |
39bffca2 AL |
288 | .parent = TYPE_ISA_DEVICE, |
289 | .instance_size = sizeof(VMPortState), | |
290 | .class_init = vmport_class_initfn, | |
6872ef61 BS |
291 | }; |
292 | ||
83f7d43a | 293 | static void vmport_register_types(void) |
6872ef61 | 294 | { |
39bffca2 | 295 | type_register_static(&vmport_info); |
6872ef61 | 296 | } |
83f7d43a AF |
297 | |
298 | type_init(vmport_register_types) |