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