blob: 7cc75dbc6da6d6e62adbf72a83965864e80561fb [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"
Paolo Bonzini86378b22020-10-28 06:22:23 -040035#include "hw/boards.h"
Liran Alonaaacf1c2020-03-12 18:54:24 +020036#include "sysemu/sysemu.h"
Vincent Palatinb3946622017-01-10 11:59:55 +010037#include "sysemu/hw_accel.h"
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +020038#include "sysemu/qtest.h"
Laurent Vivier43ab9a52017-12-21 22:11:03 +010039#include "qemu/log.h"
Philippe Mathieu-Daudé7299e1a2017-12-15 00:43:55 -030040#include "trace.h"
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040041#include "qom/object.h"
aliguori61ada152008-08-19 19:13:06 +000042
ths0975c302007-08-26 17:48:12 +000043#define VMPORT_MAGIC 0x564D5868
44
Liran Alonb8892122020-03-12 18:54:18 +020045/* Compatibility flags for migration */
Liran Alon0342ee72020-03-12 18:54:19 +020046#define VMPORT_COMPAT_READ_SET_EAX_BIT 0
47#define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT 1
Liran Alonf8bdc552020-03-12 18:54:21 +020048#define VMPORT_COMPAT_REPORT_VMX_TYPE_BIT 2
Liran Alonaaacf1c2020-03-12 18:54:24 +020049#define VMPORT_COMPAT_CMDS_V2_BIT 3
Liran Alon0342ee72020-03-12 18:54:19 +020050#define VMPORT_COMPAT_READ_SET_EAX \
Liran Alonb8892122020-03-12 18:54:18 +020051 (1 << VMPORT_COMPAT_READ_SET_EAX_BIT)
Liran Alon0342ee72020-03-12 18:54:19 +020052#define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD \
53 (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT)
Liran Alonf8bdc552020-03-12 18:54:21 +020054#define VMPORT_COMPAT_REPORT_VMX_TYPE \
55 (1 << VMPORT_COMPAT_REPORT_VMX_TYPE_BIT)
Liran Alonaaacf1c2020-03-12 18:54:24 +020056#define VMPORT_COMPAT_CMDS_V2 \
57 (1 << VMPORT_COMPAT_CMDS_V2_BIT)
Liran Alonb8892122020-03-12 18:54:18 +020058
Liran Alonacacd352020-03-12 18:54:27 +020059/* 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
Eduardo Habkost80633962020-09-16 14:25:19 -040066OBJECT_DECLARE_SIMPLE_TYPE(VMPortState, VMPORT)
Andreas Färberf02317a2013-04-27 22:18:54 +020067
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040068struct VMPortState {
Andreas Färberf02317a2013-04-27 22:18:54 +020069 ISADevice parent_obj;
70
Richard Hendersonf75317b2011-08-16 08:38:14 -070071 MemoryRegion io;
Jan Kiszkad67f6792013-06-22 08:07:06 +020072 VMPortReadFunc *func[VMPORT_ENTRIES];
ths0975c302007-08-26 17:48:12 +000073 void *opaque[VMPORT_ENTRIES];
Liran Alonb8892122020-03-12 18:54:18 +020074
Liran Alon2fd2f792020-03-12 18:54:20 +020075 uint32_t vmware_vmx_version;
Liran Alonf8bdc552020-03-12 18:54:21 +020076 uint8_t vmware_vmx_type;
Liran Alon2fd2f792020-03-12 18:54:20 +020077
Liran Alonb8892122020-03-12 18:54:18 +020078 uint32_t compat_flags;
Eduardo Habkostdb1015e2020-09-03 16:43:22 -040079};
ths0975c302007-08-26 17:48:12 +000080
Marcelo Tosattie14da0a2011-02-17 01:27:19 -020081static VMPortState *port_state;
ths0975c302007-08-26 17:48:12 +000082
Liran Alondcd938f2020-03-12 18:54:23 +020083void vmport_register(VMPortCommand command, VMPortReadFunc *func, void *opaque)
ths0975c302007-08-26 17:48:12 +000084{
Liran Alondcd938f2020-03-12 18:54:23 +020085 assert(command < VMPORT_ENTRIES);
Liran Alon23accdf2020-03-12 18:54:31 +020086 assert(port_state);
87
Philippe Mathieu-Daudé7299e1a2017-12-15 00:43:55 -030088 trace_vmport_register(command, func, opaque);
Marcelo Tosattie14da0a2011-02-17 01:27:19 -020089 port_state->func[command] = func;
90 port_state->opaque[command] = opaque;
ths0975c302007-08-26 17:48:12 +000091}
92
Alexander Graf360d6132012-10-08 13:44:24 +020093static uint64_t vmport_ioport_read(void *opaque, hwaddr addr,
94 unsigned size)
ths0975c302007-08-26 17:48:12 +000095{
96 VMPortState *s = opaque;
Andreas Färber4917cf42013-05-27 05:17:50 +020097 CPUState *cs = current_cpu;
98 X86CPU *cpu = X86_CPU(cs);
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +020099 CPUX86State *env;
ths0975c302007-08-26 17:48:12 +0000100 unsigned char command;
ths6dab28d2007-10-09 03:19:01 +0000101 uint32_t eax;
ths0975c302007-08-26 17:48:12 +0000102
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +0200103 if (qtest_enabled()) {
104 return -1;
105 }
106 env = &cpu->env;
Andreas Färber4917cf42013-05-27 05:17:50 +0200107 cpu_synchronize_state(cs);
Jan Kiszka03c63b92010-02-03 21:16:41 +0100108
aurel3226fb5e42008-04-07 19:47:25 +0000109 eax = env->regs[R_EAX];
Philippe Mathieu-Daudé323d7d12017-10-17 13:44:24 -0300110 if (eax != VMPORT_MAGIC) {
Liran Alon0342ee72020-03-12 18:54:19 +0200111 goto err;
Philippe Mathieu-Daudé323d7d12017-10-17 13:44:24 -0300112 }
ths0975c302007-08-26 17:48:12 +0000113
aurel3226fb5e42008-04-07 19:47:25 +0000114 command = env->regs[R_ECX];
Philippe Mathieu-Daudé7299e1a2017-12-15 00:43:55 -0300115 trace_vmport_command(command);
116 if (command >= VMPORT_ENTRIES || !s->func[command]) {
117 qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command);
Liran Alon0342ee72020-03-12 18:54:19 +0200118 goto err;
ths0975c302007-08-26 17:48:12 +0000119 }
120
Liran Alonb8892122020-03-12 18:54:18 +0200121 eax = s->func[command](s->opaque[command], addr);
Liran Alon0342ee72020-03-12 18:54:19 +0200122 goto out;
123
124err:
125 if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) {
126 eax = UINT32_MAX;
127 }
Liran Alonb8892122020-03-12 18:54:18 +0200128
129out:
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;
141 }
142
143 return eax;
ths0975c302007-08-26 17:48:12 +0000144}
145
Alexander Graf360d6132012-10-08 13:44:24 +0200146static void vmport_ioport_write(void *opaque, hwaddr addr,
147 uint64_t val, unsigned size)
aliguorie9396bd2008-07-28 18:58:02 +0000148{
Andreas Färber4917cf42013-05-27 05:17:50 +0200149 X86CPU *cpu = X86_CPU(current_cpu);
aliguorie9396bd2008-07-28 18:58:02 +0000150
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +0200151 if (qtest_enabled()) {
152 return;
153 }
Andreas Färber4917cf42013-05-27 05:17:50 +0200154 cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4);
aliguorie9396bd2008-07-28 18:58:02 +0000155}
156
ths0975c302007-08-26 17:48:12 +0000157static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr)
158{
Andreas Färber4917cf42013-05-27 05:17:50 +0200159 X86CPU *cpu = X86_CPU(current_cpu);
160
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +0200161 if (qtest_enabled()) {
162 return -1;
163 }
Andreas Färber4917cf42013-05-27 05:17:50 +0200164 cpu->env.regs[R_EBX] = VMPORT_MAGIC;
Liran Alonf8bdc552020-03-12 18:54:21 +0200165 if (port_state->compat_flags & VMPORT_COMPAT_REPORT_VMX_TYPE) {
166 cpu->env.regs[R_ECX] = port_state->vmware_vmx_type;
167 }
Liran Alon2fd2f792020-03-12 18:54:20 +0200168 return port_state->vmware_vmx_version;
ths0975c302007-08-26 17:48:12 +0000169}
170
Liran Alonaaacf1c2020-03-12 18:54:24 +0200171static 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
ths0975c302007-08-26 17:48:12 +0000183static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr)
184{
Andreas Färber4917cf42013-05-27 05:17:50 +0200185 X86CPU *cpu = X86_CPU(current_cpu);
186
Philippe Mathieu-Daudéc781a2c2020-05-18 12:31:13 +0200187 if (qtest_enabled()) {
188 return -1;
189 }
Andreas Färber4917cf42013-05-27 05:17:50 +0200190 cpu->env.regs[R_EBX] = 0x1177;
Paolo Bonzini86378b22020-10-28 06:22:23 -0400191 return current_machine->ram_size;
ths0975c302007-08-26 17:48:12 +0000192}
193
Liran Alond6048bf2020-03-12 18:54:30 +0200194static 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
Liran Alonacacd352020-03-12 18:54:27 +0200212static uint32_t vmport_cmd_get_vcpu_info(void *opaque, uint32_t addr)
213{
Liran Alon7f9114b2020-03-12 18:54:28 +0200214 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;
Liran Alonacacd352020-03-12 18:54:27 +0200222}
223
Richard Hendersonf75317b2011-08-16 08:38:14 -0700224static const MemoryRegionOps vmport_ops = {
Alexander Graf360d6132012-10-08 13:44:24 +0200225 .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,
Richard Hendersonf75317b2011-08-16 08:38:14 -0700232};
233
Andreas Färberdb895a12012-11-25 02:37:14 +0100234static void vmport_realizefn(DeviceState *dev, Error **errp)
ths0975c302007-08-26 17:48:12 +0000235{
Andreas Färberdb895a12012-11-25 02:37:14 +0100236 ISADevice *isadev = ISA_DEVICE(dev);
Andreas Färberf02317a2013-04-27 22:18:54 +0200237 VMPortState *s = VMPORT(dev);
ths0975c302007-08-26 17:48:12 +0000238
Paolo Bonzini3c161542013-06-06 21:25:08 -0400239 memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1);
Andreas Färberdb895a12012-11-25 02:37:14 +0100240 isa_register_ioport(isadev, &s->io, 0x5658);
Richard Hendersonf75317b2011-08-16 08:38:14 -0700241
Marcelo Tosattie14da0a2011-02-17 01:27:19 -0200242 port_state = s;
Liran Alonaaacf1c2020-03-12 18:54:24 +0200243
ths0975c302007-08-26 17:48:12 +0000244 /* Register some generic port commands */
aurel3226fb5e42008-04-07 19:47:25 +0000245 vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL);
246 vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL);
Liran Alonaaacf1c2020-03-12 18:54:24 +0200247 if (s->compat_flags & VMPORT_COMPAT_CMDS_V2) {
248 vmport_register(VMPORT_CMD_GETBIOSUUID, vmport_cmd_get_bios_uuid, NULL);
Liran Alond6048bf2020-03-12 18:54:30 +0200249 vmport_register(VMPORT_CMD_GETHZ, vmport_cmd_get_hz, NULL);
Liran Alonacacd352020-03-12 18:54:27 +0200250 vmport_register(VMPORT_CMD_GET_VCPU_INFO, vmport_cmd_get_vcpu_info,
251 NULL);
Liran Alonaaacf1c2020-03-12 18:54:24 +0200252 }
ths0975c302007-08-26 17:48:12 +0000253}
Blue Swirl6872ef62011-02-05 14:34:41 +0000254
Liran Alonc9ab24c2020-03-12 18:54:17 +0200255static Property vmport_properties[] = {
Liran Alonb8892122020-03-12 18:54:18 +0200256 /* 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),
Liran Alon0342ee72020-03-12 18:54:19 +0200259 DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags,
260 VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true),
Liran Alonf8bdc552020-03-12 18:54:21 +0200261 DEFINE_PROP_BIT("x-report-vmx-type", VMPortState, compat_flags,
262 VMPORT_COMPAT_REPORT_VMX_TYPE_BIT, true),
Liran Alonaaacf1c2020-03-12 18:54:24 +0200263 DEFINE_PROP_BIT("x-cmds-v2", VMPortState, compat_flags,
264 VMPORT_COMPAT_CMDS_V2_BIT, true),
Liran Alon2fd2f792020-03-12 18:54:20 +0200265
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),
Liran Alonf8bdc552020-03-12 18:54:21 +0200269 /*
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),
Liran Alon2fd2f792020-03-12 18:54:20 +0200287
Liran Alonc9ab24c2020-03-12 18:54:17 +0200288 DEFINE_PROP_END_OF_LIST(),
289};
290
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600291static void vmport_class_initfn(ObjectClass *klass, void *data)
292{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600293 DeviceClass *dc = DEVICE_CLASS(klass);
Andreas Färberdb895a12012-11-25 02:37:14 +0100294
295 dc->realize = vmport_realizefn;
Markus Armbrusterf3b17642013-11-28 17:27:02 +0100296 /* Reason: realize sets global port_state */
Eduardo Habkoste90f2a82017-05-03 17:35:44 -0300297 dc->user_creatable = false;
Liran Alonc9ab24c2020-03-12 18:54:17 +0200298 device_class_set_props(dc, vmport_properties);
Anthony Liguori8f04ee02011-12-04 11:52:49 -0600299}
300
Andreas Färber8c43a6f2013-01-10 16:19:07 +0100301static const TypeInfo vmport_info = {
Andreas Färberf02317a2013-04-27 22:18:54 +0200302 .name = TYPE_VMPORT,
Anthony Liguori39bffca2011-12-07 21:34:16 -0600303 .parent = TYPE_ISA_DEVICE,
304 .instance_size = sizeof(VMPortState),
305 .class_init = vmport_class_initfn,
Blue Swirl6872ef62011-02-05 14:34:41 +0000306};
307
Andreas Färber83f7d432012-02-09 15:20:55 +0100308static void vmport_register_types(void)
Blue Swirl6872ef62011-02-05 14:34:41 +0000309{
Anthony Liguori39bffca2011-12-07 21:34:16 -0600310 type_register_static(&vmport_info);
Blue Swirl6872ef62011-02-05 14:34:41 +0000311}
Andreas Färber83f7d432012-02-09 15:20:55 +0100312
313type_init(vmport_register_types)