/src/qemu/hw/i386/vmport.c
Line | Count | Source |
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/core/qdev-properties.h" |
35 | | #include "hw/core/boards.h" |
36 | | #include "system/system.h" |
37 | | #include "system/hw_accel.h" |
38 | | #include "system/qtest.h" |
39 | | #include "qemu/log.h" |
40 | | #include "trace.h" |
41 | | #include "qom/object.h" |
42 | | |
43 | 0 | #define VMPORT_MAGIC 0x564D5868 |
44 | | |
45 | | /* Compatibility flags for migration */ |
46 | 0 | #define VMPORT_COMPAT_READ_SET_EAX_BIT 0 |
47 | 0 | #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT 1 |
48 | 0 | #define VMPORT_COMPAT_REPORT_VMX_TYPE_BIT 2 |
49 | 63 | #define VMPORT_COMPAT_CMDS_V2_BIT 3 |
50 | | #define VMPORT_COMPAT_READ_SET_EAX \ |
51 | 0 | (1 << VMPORT_COMPAT_READ_SET_EAX_BIT) |
52 | | #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD \ |
53 | 0 | (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT) |
54 | | #define VMPORT_COMPAT_REPORT_VMX_TYPE \ |
55 | 0 | (1 << VMPORT_COMPAT_REPORT_VMX_TYPE_BIT) |
56 | | #define VMPORT_COMPAT_CMDS_V2 \ |
57 | 63 | (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 | 0 | #define VCPU_INFO_LEGACY_X2APIC_BIT 3 |
64 | | #define VCPU_INFO_RESERVED_BIT 31 |
65 | | |
66 | | OBJECT_DECLARE_SIMPLE_TYPE(VMPortState, VMPORT) |
67 | | |
68 | | struct VMPortState { |
69 | | ISADevice parent_obj; |
70 | | |
71 | | MemoryRegion io; |
72 | | VMPortReadFunc *func[VMPORT_ENTRIES]; |
73 | | void *opaque[VMPORT_ENTRIES]; |
74 | | |
75 | | uint32_t vmware_vmx_version; |
76 | | uint8_t vmware_vmx_type; |
77 | | |
78 | | uint32_t compat_flags; |
79 | | }; |
80 | | |
81 | | static VMPortState *port_state; |
82 | | |
83 | | void vmport_register(VMPortCommand command, VMPortReadFunc *func, void *opaque) |
84 | 504 | { |
85 | 504 | assert(command < VMPORT_ENTRIES); |
86 | 504 | assert(port_state); |
87 | | |
88 | 504 | trace_vmport_register(command, func, opaque); |
89 | 504 | port_state->func[command] = func; |
90 | 504 | port_state->opaque[command] = opaque; |
91 | 504 | } |
92 | | |
93 | | static uint64_t vmport_ioport_read(void *opaque, hwaddr addr, |
94 | | unsigned size) |
95 | 0 | { |
96 | 0 | VMPortState *s = opaque; |
97 | 0 | CPUState *cs = current_cpu; |
98 | 0 | X86CPU *cpu = X86_CPU(cs); |
99 | 0 | CPUX86State *env; |
100 | 0 | unsigned char command; |
101 | 0 | uint32_t eax; |
102 | |
|
103 | 0 | if (qtest_enabled()) { |
104 | 0 | return -1; |
105 | 0 | } |
106 | 0 | env = &cpu->env; |
107 | 0 | cpu_synchronize_state(cs); |
108 | |
|
109 | 0 | eax = env->regs[R_EAX]; |
110 | 0 | if (eax != VMPORT_MAGIC) { |
111 | 0 | goto err; |
112 | 0 | } |
113 | | |
114 | 0 | command = env->regs[R_ECX]; |
115 | 0 | trace_vmport_command(command); |
116 | 0 | if (command >= VMPORT_ENTRIES || !s->func[command]) { |
117 | 0 | qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command); |
118 | 0 | goto err; |
119 | 0 | } |
120 | | |
121 | 0 | eax = s->func[command](s->opaque[command], addr); |
122 | 0 | goto out; |
123 | | |
124 | 0 | err: |
125 | 0 | if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) { |
126 | 0 | eax = UINT32_MAX; |
127 | 0 | } |
128 | |
|
129 | 0 | out: |
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 | 0 | if (s->compat_flags & VMPORT_COMPAT_READ_SET_EAX) { |
140 | 0 | cpu->env.regs[R_EAX] = eax; |
141 | 0 | } |
142 | |
|
143 | 0 | return eax; |
144 | 0 | } |
145 | | |
146 | | static void vmport_ioport_write(void *opaque, hwaddr addr, |
147 | | uint64_t val, unsigned size) |
148 | 0 | { |
149 | 0 | X86CPU *cpu = X86_CPU(current_cpu); |
150 | |
|
151 | 0 | if (qtest_enabled()) { |
152 | 0 | return; |
153 | 0 | } |
154 | 0 | cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4); |
155 | 0 | } |
156 | | |
157 | | static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr) |
158 | 0 | { |
159 | 0 | X86CPU *cpu = X86_CPU(current_cpu); |
160 | |
|
161 | 0 | if (qtest_enabled()) { |
162 | 0 | return -1; |
163 | 0 | } |
164 | 0 | cpu->env.regs[R_EBX] = VMPORT_MAGIC; |
165 | 0 | if (port_state->compat_flags & VMPORT_COMPAT_REPORT_VMX_TYPE) { |
166 | 0 | cpu->env.regs[R_ECX] = port_state->vmware_vmx_type; |
167 | 0 | } |
168 | 0 | return port_state->vmware_vmx_version; |
169 | 0 | } |
170 | | |
171 | | static uint32_t vmport_cmd_get_bios_uuid(void *opaque, uint32_t addr) |
172 | 0 | { |
173 | 0 | X86CPU *cpu = X86_CPU(current_cpu); |
174 | 0 | uint32_t *uuid_parts = (uint32_t *)(qemu_uuid.data); |
175 | |
|
176 | 0 | cpu->env.regs[R_EAX] = le32_to_cpu(uuid_parts[0]); |
177 | 0 | cpu->env.regs[R_EBX] = le32_to_cpu(uuid_parts[1]); |
178 | 0 | cpu->env.regs[R_ECX] = le32_to_cpu(uuid_parts[2]); |
179 | 0 | cpu->env.regs[R_EDX] = le32_to_cpu(uuid_parts[3]); |
180 | 0 | return cpu->env.regs[R_EAX]; |
181 | 0 | } |
182 | | |
183 | | static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr) |
184 | 0 | { |
185 | 0 | X86CPU *cpu = X86_CPU(current_cpu); |
186 | |
|
187 | 0 | if (qtest_enabled()) { |
188 | 0 | return -1; |
189 | 0 | } |
190 | 0 | cpu->env.regs[R_EBX] = 0x1177; |
191 | 0 | return current_machine->ram_size; |
192 | 0 | } |
193 | | |
194 | | static uint32_t vmport_cmd_get_hz(void *opaque, uint32_t addr) |
195 | 0 | { |
196 | 0 | X86CPU *cpu = X86_CPU(current_cpu); |
197 | |
|
198 | 0 | if (cpu->env.tsc_khz && cpu->env.apic_bus_freq) { |
199 | 0 | uint64_t tsc_freq = (uint64_t)cpu->env.tsc_khz * 1000; |
200 | |
|
201 | 0 | cpu->env.regs[R_ECX] = cpu->env.apic_bus_freq; |
202 | 0 | cpu->env.regs[R_EBX] = (uint32_t)(tsc_freq >> 32); |
203 | 0 | cpu->env.regs[R_EAX] = (uint32_t)tsc_freq; |
204 | 0 | } else { |
205 | | /* Signal cmd as not supported */ |
206 | 0 | cpu->env.regs[R_EBX] = UINT32_MAX; |
207 | 0 | } |
208 | |
|
209 | 0 | return cpu->env.regs[R_EAX]; |
210 | 0 | } |
211 | | |
212 | | static uint32_t vmport_cmd_get_vcpu_info(void *opaque, uint32_t addr) |
213 | 0 | { |
214 | 0 | X86CPU *cpu = X86_CPU(current_cpu); |
215 | 0 | uint32_t ret = 0; |
216 | |
|
217 | 0 | if (cpu->env.features[FEAT_1_ECX] & CPUID_EXT_X2APIC) { |
218 | 0 | ret |= 1 << VCPU_INFO_LEGACY_X2APIC_BIT; |
219 | 0 | } |
220 | |
|
221 | 0 | return ret; |
222 | 0 | } |
223 | | |
224 | | static const MemoryRegionOps vmport_ops = { |
225 | | .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, |
232 | | }; |
233 | | |
234 | | static void vmport_realizefn(DeviceState *dev, Error **errp) |
235 | 63 | { |
236 | 63 | ISADevice *isadev = ISA_DEVICE(dev); |
237 | 63 | VMPortState *s = VMPORT(dev); |
238 | | |
239 | 63 | memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1); |
240 | 63 | isa_register_ioport(isadev, &s->io, 0x5658); |
241 | | |
242 | 63 | port_state = s; |
243 | | |
244 | | /* Register some generic port commands */ |
245 | 63 | vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL); |
246 | 63 | vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL); |
247 | 63 | if (s->compat_flags & VMPORT_COMPAT_CMDS_V2) { |
248 | 63 | vmport_register(VMPORT_CMD_GETBIOSUUID, vmport_cmd_get_bios_uuid, NULL); |
249 | 63 | vmport_register(VMPORT_CMD_GETHZ, vmport_cmd_get_hz, NULL); |
250 | 63 | vmport_register(VMPORT_CMD_GET_VCPU_INFO, vmport_cmd_get_vcpu_info, |
251 | 63 | NULL); |
252 | 63 | } |
253 | 63 | } |
254 | | |
255 | | static const Property vmport_properties[] = { |
256 | | /* 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), |
259 | | DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags, |
260 | | VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true), |
261 | | DEFINE_PROP_BIT("x-report-vmx-type", VMPortState, compat_flags, |
262 | | VMPORT_COMPAT_REPORT_VMX_TYPE_BIT, true), |
263 | | DEFINE_PROP_BIT("x-cmds-v2", VMPortState, compat_flags, |
264 | | VMPORT_COMPAT_CMDS_V2_BIT, true), |
265 | | |
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), |
269 | | /* |
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), |
287 | | }; |
288 | | |
289 | | static void vmport_class_initfn(ObjectClass *klass, const void *data) |
290 | 63 | { |
291 | 63 | DeviceClass *dc = DEVICE_CLASS(klass); |
292 | | |
293 | 63 | dc->realize = vmport_realizefn; |
294 | | /* Reason: realize sets global port_state */ |
295 | 63 | dc->user_creatable = false; |
296 | 63 | device_class_set_props(dc, vmport_properties); |
297 | 63 | } |
298 | | |
299 | | static const TypeInfo vmport_info = { |
300 | | .name = TYPE_VMPORT, |
301 | | .parent = TYPE_ISA_DEVICE, |
302 | | .instance_size = sizeof(VMPortState), |
303 | | .class_init = vmport_class_initfn, |
304 | | .secure = true, |
305 | | }; |
306 | | |
307 | | static void vmport_register_types(void) |
308 | 63 | { |
309 | 63 | type_register_static(&vmport_info); |
310 | 63 | } |
311 | | |
312 | | type_init(vmport_register_types) |