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