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