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