blob: 89bda9108eba49bd64abf2f5ac89ce9ba7db6a57 [file] [log] [blame]
ths0975c302007-08-26 17:48:12 +00001/*
2 * QEMU VMPort emulation
3 *
Stefan Weilbcc4e412011-12-02 10:30:41 +01004 * Copyright (C) 2007 Hervé Poussineau
ths0975c302007-08-26 17:48:12 +00005 *
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 */
Liran Alon29282252020-03-12 18:54:16 +020024
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
Peter Maydell0d1c9782016-01-26 18:17:17 +000031#include "qemu/osdep.h"
Paolo Bonzini0d09e412013-02-05 17:06:20 +010032#include "hw/isa/isa.h"
Liran Alond8f23d62020-03-12 18:54:22 +020033#include "hw/i386/vmport.h"
Liran Alonc9ab24c2020-03-12 18:54:17 +020034#include "hw/qdev-properties.h"
Liran Alonaaacf1c2020-03-12 18:54:24 +020035#include "sysemu/sysemu.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010036#include "sysemu/hw_accel.h"
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +020037#include "sysemu/qtest.h"
Laurent Vivier43ab9a52017-12-21 22:11:03 +010038#include "qemu/log.h"
Philippe Mathieu-Daudéd8a05992020-05-04 10:33:42 +020039#include "cpu.h"
Philippe Mathieu-Daudé7299e1a2017-12-15 00:43:55 -030040#include "trace.h"
aliguori61ada152008-08-19 19:13:06 +000041
ths0975c302007-08-26 17:48:12 +000042#define VMPORT_MAGIC 0x564D5868
43
Liran Alonb8892122020-03-12 18:54:18 +020044/* Compatibility flags for migration */
Liran Alon0342ee72020-03-12 18:54:19 +020045#define VMPORT_COMPAT_READ_SET_EAX_BIT 0
46#define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT 1
Liran Alonf8bdc552020-03-12 18:54:21 +020047#define VMPORT_COMPAT_REPORT_VMX_TYPE_BIT 2
Liran Alonaaacf1c2020-03-12 18:54:24 +020048#define VMPORT_COMPAT_CMDS_V2_BIT 3
Liran Alon0342ee72020-03-12 18:54:19 +020049#define VMPORT_COMPAT_READ_SET_EAX \
Liran Alonb8892122020-03-12 18:54:18 +020050 (1 << VMPORT_COMPAT_READ_SET_EAX_BIT)
Liran Alon0342ee72020-03-12 18:54:19 +020051#define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD \
52 (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT)
Liran Alonf8bdc552020-03-12 18:54:21 +020053#define VMPORT_COMPAT_REPORT_VMX_TYPE \
54 (1 << VMPORT_COMPAT_REPORT_VMX_TYPE_BIT)
Liran Alonaaacf1c2020-03-12 18:54:24 +020055#define VMPORT_COMPAT_CMDS_V2 \
56 (1 << VMPORT_COMPAT_CMDS_V2_BIT)
Liran Alonb8892122020-03-12 18:54:18 +020057
Liran Alonacacd352020-03-12 18:54:27 +020058/* 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
Andreas Färberf02317a2013-04-27 22:18:54 +020065#define VMPORT(obj) OBJECT_CHECK(VMPortState, (obj), TYPE_VMPORT)
66
Philippe Mathieu-Daudé323d7d12017-10-17 13:44:24 -030067typedef struct VMPortState {
Andreas Färberf02317a2013-04-27 22:18:54 +020068 ISADevice parent_obj;
69
Richard Hendersonf75317b2011-08-16 08:38:14 -070070 MemoryRegion io;
Jan Kiszkad67f6792013-06-22 08:07:06 +020071 VMPortReadFunc *func[VMPORT_ENTRIES];
ths0975c302007-08-26 17:48:12 +000072 void *opaque[VMPORT_ENTRIES];
Liran Alonb8892122020-03-12 18:54:18 +020073
Liran Alon2fd2f792020-03-12 18:54:20 +020074 uint32_t vmware_vmx_version;
Liran Alonf8bdc552020-03-12 18:54:21 +020075 uint8_t vmware_vmx_type;
Liran Alon2fd2f792020-03-12 18:54:20 +020076
Liran Alonb8892122020-03-12 18:54:18 +020077 uint32_t compat_flags;
ths0975c302007-08-26 17:48:12 +000078} VMPortState;
79
Marcelo Tosattie14da0a2011-02-17 01:27:19 -020080static VMPortState *port_state;
ths0975c302007-08-26 17:48:12 +000081
Liran Alondcd938f2020-03-12 18:54:23 +020082void vmport_register(VMPortCommand command, VMPortReadFunc *func, void *opaque)
ths0975c302007-08-26 17:48:12 +000083{
Liran Alondcd938f2020-03-12 18:54:23 +020084 assert(command < VMPORT_ENTRIES);
Liran Alon23accdf2020-03-12 18:54:31 +020085 assert(port_state);
86
Philippe Mathieu-Daudé7299e1a2017-12-15 00:43:55 -030087 trace_vmport_register(command, func, opaque);
Marcelo Tosattie14da0a2011-02-17 01:27:19 -020088 port_state->func[command] = func;
89 port_state->opaque[command] = opaque;
ths0975c302007-08-26 17:48:12 +000090}
91
Alexander Graf360d6132012-10-08 13:44:24 +020092static uint64_t vmport_ioport_read(void *opaque, hwaddr addr,
93 unsigned size)
ths0975c302007-08-26 17:48:12 +000094{
95 VMPortState *s = opaque;
Andreas Färber4917cf42013-05-27 05:17:50 +020096 CPUState *cs = current_cpu;
97 X86CPU *cpu = X86_CPU(cs);
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +020098 CPUX86State *env;
ths0975c302007-08-26 17:48:12 +000099 unsigned char command;
ths6dab28d2007-10-09 03:19:01 +0000100 uint32_t eax;
ths0975c302007-08-26 17:48:12 +0000101
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +0200102 if (qtest_enabled()) {
103 return -1;
104 }
105 env = &cpu->env;
Andreas Färber4917cf42013-05-27 05:17:50 +0200106 cpu_synchronize_state(cs);
Jan Kiszka03c63b92010-02-03 21:16:41 +0100107
aurel3226fb5e42008-04-07 19:47:25 +0000108 eax = env->regs[R_EAX];
Philippe Mathieu-Daudé323d7d12017-10-17 13:44:24 -0300109 if (eax != VMPORT_MAGIC) {
Liran Alon0342ee72020-03-12 18:54:19 +0200110 goto err;
Philippe Mathieu-Daudé323d7d12017-10-17 13:44:24 -0300111 }
ths0975c302007-08-26 17:48:12 +0000112
aurel3226fb5e42008-04-07 19:47:25 +0000113 command = env->regs[R_ECX];
Philippe Mathieu-Daudé7299e1a2017-12-15 00:43:55 -0300114 trace_vmport_command(command);
115 if (command >= VMPORT_ENTRIES || !s->func[command]) {
116 qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command);
Liran Alon0342ee72020-03-12 18:54:19 +0200117 goto err;
ths0975c302007-08-26 17:48:12 +0000118 }
119
Liran Alonb8892122020-03-12 18:54:18 +0200120 eax = s->func[command](s->opaque[command], addr);
Liran Alon0342ee72020-03-12 18:54:19 +0200121 goto out;
122
123err:
124 if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) {
125 eax = UINT32_MAX;
126 }
Liran Alonb8892122020-03-12 18:54:18 +0200127
128out:
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;
ths0975c302007-08-26 17:48:12 +0000143}
144
Alexander Graf360d6132012-10-08 13:44:24 +0200145static void vmport_ioport_write(void *opaque, hwaddr addr,
146 uint64_t val, unsigned size)
aliguorie9396bd2008-07-28 18:58:02 +0000147{
Andreas Färber4917cf42013-05-27 05:17:50 +0200148 X86CPU *cpu = X86_CPU(current_cpu);
aliguorie9396bd2008-07-28 18:58:02 +0000149
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +0200150 if (qtest_enabled()) {
151 return;
152 }
Andreas Färber4917cf42013-05-27 05:17:50 +0200153 cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4);
aliguorie9396bd2008-07-28 18:58:02 +0000154}
155
ths0975c302007-08-26 17:48:12 +0000156static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr)
157{
Andreas Färber4917cf42013-05-27 05:17:50 +0200158 X86CPU *cpu = X86_CPU(current_cpu);
159
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +0200160 if (qtest_enabled()) {
161 return -1;
162 }
Andreas Färber4917cf42013-05-27 05:17:50 +0200163 cpu->env.regs[R_EBX] = VMPORT_MAGIC;
Liran Alonf8bdc552020-03-12 18:54:21 +0200164 if (port_state->compat_flags & VMPORT_COMPAT_REPORT_VMX_TYPE) {
165 cpu->env.regs[R_ECX] = port_state->vmware_vmx_type;
166 }
Liran Alon2fd2f792020-03-12 18:54:20 +0200167 return port_state->vmware_vmx_version;
ths0975c302007-08-26 17:48:12 +0000168}
169
Liran Alonaaacf1c2020-03-12 18:54:24 +0200170static 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
ths0975c302007-08-26 17:48:12 +0000182static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr)
183{
Andreas Färber4917cf42013-05-27 05:17:50 +0200184 X86CPU *cpu = X86_CPU(current_cpu);
185
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +0200186 if (qtest_enabled()) {
187 return -1;
188 }
Andreas Färber4917cf42013-05-27 05:17:50 +0200189 cpu->env.regs[R_EBX] = 0x1177;
ths0975c302007-08-26 17:48:12 +0000190 return ram_size;
191}
192
Liran Alond6048bf2020-03-12 18:54:30 +0200193static 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
Liran Alonacacd352020-03-12 18:54:27 +0200211static uint32_t vmport_cmd_get_vcpu_info(void *opaque, uint32_t addr)
212{
Liran Alon7f9114b2020-03-12 18:54:28 +0200213 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;
Liran Alonacacd352020-03-12 18:54:27 +0200221}
222
Richard Hendersonf75317b2011-08-16 08:38:14 -0700223static const MemoryRegionOps vmport_ops = {
Alexander Graf360d6132012-10-08 13:44:24 +0200224 .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,
Richard Hendersonf75317b2011-08-16 08:38:14 -0700231};
232
Andreas Färberdb895a12012-11-25 02:37:14 +0100233static void vmport_realizefn(DeviceState *dev, Error **errp)
ths0975c302007-08-26 17:48:12 +0000234{
Andreas Färberdb895a12012-11-25 02:37:14 +0100235 ISADevice *isadev = ISA_DEVICE(dev);
Andreas Färberf02317a2013-04-27 22:18:54 +0200236 VMPortState *s = VMPORT(dev);
ths0975c302007-08-26 17:48:12 +0000237
Paolo Bonzini3c161542013-06-06 21:25:08 -0400238 memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1);
Andreas Färberdb895a12012-11-25 02:37:14 +0100239 isa_register_ioport(isadev, &s->io, 0x5658);
Richard Hendersonf75317b2011-08-16 08:38:14 -0700240
Marcelo Tosattie14da0a2011-02-17 01:27:19 -0200241 port_state = s;
Liran Alonaaacf1c2020-03-12 18:54:24 +0200242
ths0975c302007-08-26 17:48:12 +0000243 /* Register some generic port commands */
aurel3226fb5e42008-04-07 19:47:25 +0000244 vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL);
245 vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL);
Liran Alonaaacf1c2020-03-12 18:54:24 +0200246 if (s->compat_flags & VMPORT_COMPAT_CMDS_V2) {
247 vmport_register(VMPORT_CMD_GETBIOSUUID, vmport_cmd_get_bios_uuid, NULL);
Liran Alond6048bf2020-03-12 18:54:30 +0200248 vmport_register(VMPORT_CMD_GETHZ, vmport_cmd_get_hz, NULL);
Liran Alonacacd352020-03-12 18:54:27 +0200249 vmport_register(VMPORT_CMD_GET_VCPU_INFO, vmport_cmd_get_vcpu_info,
250 NULL);
Liran Alonaaacf1c2020-03-12 18:54:24 +0200251 }
ths0975c302007-08-26 17:48:12 +0000252}
Blue Swirl6872ef62011-02-05 14:34:41 +0000253
Liran Alonc9ab24c2020-03-12 18:54:17 +0200254static Property vmport_properties[] = {
Liran Alonb8892122020-03-12 18:54:18 +0200255 /* 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),
Liran Alon0342ee72020-03-12 18:54:19 +0200258 DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags,
259 VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true),
Liran Alonf8bdc552020-03-12 18:54:21 +0200260 DEFINE_PROP_BIT("x-report-vmx-type", VMPortState, compat_flags,
261 VMPORT_COMPAT_REPORT_VMX_TYPE_BIT, true),
Liran Alonaaacf1c2020-03-12 18:54:24 +0200262 DEFINE_PROP_BIT("x-cmds-v2", VMPortState, compat_flags,
263 VMPORT_COMPAT_CMDS_V2_BIT, true),
Liran Alon2fd2f792020-03-12 18:54:20 +0200264
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),
Liran Alonf8bdc552020-03-12 18:54:21 +0200268 /*
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),
Liran Alon2fd2f792020-03-12 18:54:20 +0200286
Liran Alonc9ab24c2020-03-12 18:54:17 +0200287 DEFINE_PROP_END_OF_LIST(),
288};
289
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600290static void vmport_class_initfn(ObjectClass *klass, void *data)
291{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600292 DeviceClass *dc = DEVICE_CLASS(klass);
Andreas Färberdb895a12012-11-25 02:37:14 +0100293
294 dc->realize = vmport_realizefn;
Markus Armbrusterf3b17642013-11-28 17:27:02 +0100295 /* Reason: realize sets global port_state */
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300296 dc->user_creatable = false;
Liran Alonc9ab24c2020-03-12 18:54:17 +0200297 device_class_set_props(dc, vmport_properties);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600298}
299
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100300static const TypeInfo vmport_info = {
Andreas Färberf02317a2013-04-27 22:18:54 +0200301 .name = TYPE_VMPORT,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600302 .parent = TYPE_ISA_DEVICE,
303 .instance_size = sizeof(VMPortState),
304 .class_init = vmport_class_initfn,
Blue Swirl6872ef62011-02-05 14:34:41 +0000305};
306
Andreas Färber83f7d432012-02-09 15:20:55 +0100307static void vmport_register_types(void)
Blue Swirl6872ef62011-02-05 14:34:41 +0000308{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600309 type_register_static(&vmport_info);
Blue Swirl6872ef62011-02-05 14:34:41 +0000310}
Andreas Färber83f7d432012-02-09 15:20:55 +0100311
312type_init(vmport_register_types)