xref: /openbmc/qemu/hw/i386/vmport.c (revision 2fd2f799f874a4d4156c4c7287c92cfbbae5bdb2)
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/qdev-properties.h"
34 #include "sysemu/hw_accel.h"
35 #include "qemu/log.h"
36 #include "vmport.h"
37 #include "cpu.h"
38 #include "trace.h"
39 
40 #define VMPORT_CMD_GETVERSION 0x0a
41 #define VMPORT_CMD_GETRAMSIZE 0x14
42 
43 #define VMPORT_ENTRIES 0x2c
44 #define VMPORT_MAGIC   0x564D5868
45 
46 /* Compatibility flags for migration */
47 #define VMPORT_COMPAT_READ_SET_EAX_BIT              0
48 #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT    1
49 #define VMPORT_COMPAT_READ_SET_EAX              \
50     (1 << VMPORT_COMPAT_READ_SET_EAX_BIT)
51 #define VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD    \
52     (1 << VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT)
53 
54 #define VMPORT(obj) OBJECT_CHECK(VMPortState, (obj), TYPE_VMPORT)
55 
56 typedef struct VMPortState {
57     ISADevice parent_obj;
58 
59     MemoryRegion io;
60     VMPortReadFunc *func[VMPORT_ENTRIES];
61     void *opaque[VMPORT_ENTRIES];
62 
63     uint32_t vmware_vmx_version;
64 
65     uint32_t compat_flags;
66 } VMPortState;
67 
68 static VMPortState *port_state;
69 
70 void vmport_register(unsigned char command, VMPortReadFunc *func, void *opaque)
71 {
72     if (command >= VMPORT_ENTRIES) {
73         return;
74     }
75 
76     trace_vmport_register(command, func, opaque);
77     port_state->func[command] = func;
78     port_state->opaque[command] = opaque;
79 }
80 
81 static uint64_t vmport_ioport_read(void *opaque, hwaddr addr,
82                                    unsigned size)
83 {
84     VMPortState *s = opaque;
85     CPUState *cs = current_cpu;
86     X86CPU *cpu = X86_CPU(cs);
87     CPUX86State *env = &cpu->env;
88     unsigned char command;
89     uint32_t eax;
90 
91     cpu_synchronize_state(cs);
92 
93     eax = env->regs[R_EAX];
94     if (eax != VMPORT_MAGIC) {
95         goto err;
96     }
97 
98     command = env->regs[R_ECX];
99     trace_vmport_command(command);
100     if (command >= VMPORT_ENTRIES || !s->func[command]) {
101         qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command);
102         goto err;
103     }
104 
105     eax = s->func[command](s->opaque[command], addr);
106     goto out;
107 
108 err:
109     if (s->compat_flags & VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD) {
110         eax = UINT32_MAX;
111     }
112 
113 out:
114     /*
115      * The call above to cpu_synchronize_state() gets vCPU registers values
116      * to QEMU but also cause QEMU to write QEMU vCPU registers values to
117      * vCPU implementation (e.g. Accelerator such as KVM) just before
118      * resuming guest.
119      *
120      * Therefore, in order to make IOPort return value propagate to
121      * guest EAX, we need to explicitly update QEMU EAX register value.
122      */
123     if (s->compat_flags & VMPORT_COMPAT_READ_SET_EAX) {
124         cpu->env.regs[R_EAX] = eax;
125     }
126 
127     return eax;
128 }
129 
130 static void vmport_ioport_write(void *opaque, hwaddr addr,
131                                 uint64_t val, unsigned size)
132 {
133     X86CPU *cpu = X86_CPU(current_cpu);
134 
135     cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4);
136 }
137 
138 static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr)
139 {
140     X86CPU *cpu = X86_CPU(current_cpu);
141 
142     cpu->env.regs[R_EBX] = VMPORT_MAGIC;
143     return port_state->vmware_vmx_version;
144 }
145 
146 static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr)
147 {
148     X86CPU *cpu = X86_CPU(current_cpu);
149 
150     cpu->env.regs[R_EBX] = 0x1177;
151     return ram_size;
152 }
153 
154 static const MemoryRegionOps vmport_ops = {
155     .read = vmport_ioport_read,
156     .write = vmport_ioport_write,
157     .impl = {
158         .min_access_size = 4,
159         .max_access_size = 4,
160     },
161     .endianness = DEVICE_LITTLE_ENDIAN,
162 };
163 
164 static void vmport_realizefn(DeviceState *dev, Error **errp)
165 {
166     ISADevice *isadev = ISA_DEVICE(dev);
167     VMPortState *s = VMPORT(dev);
168 
169     memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1);
170     isa_register_ioport(isadev, &s->io, 0x5658);
171 
172     port_state = s;
173     /* Register some generic port commands */
174     vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL);
175     vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL);
176 }
177 
178 static Property vmport_properties[] = {
179     /* Used to enforce compatibility for migration */
180     DEFINE_PROP_BIT("x-read-set-eax", VMPortState, compat_flags,
181                     VMPORT_COMPAT_READ_SET_EAX_BIT, true),
182     DEFINE_PROP_BIT("x-signal-unsupported-cmd", VMPortState, compat_flags,
183                     VMPORT_COMPAT_SIGNAL_UNSUPPORTED_CMD_BIT, true),
184 
185     /* Default value taken from open-vm-tools code VERSION_MAGIC definition */
186     DEFINE_PROP_UINT32("vmware-vmx-version", VMPortState,
187                        vmware_vmx_version, 6),
188 
189     DEFINE_PROP_END_OF_LIST(),
190 };
191 
192 static void vmport_class_initfn(ObjectClass *klass, void *data)
193 {
194     DeviceClass *dc = DEVICE_CLASS(klass);
195 
196     dc->realize = vmport_realizefn;
197     /* Reason: realize sets global port_state */
198     dc->user_creatable = false;
199     device_class_set_props(dc, vmport_properties);
200 }
201 
202 static const TypeInfo vmport_info = {
203     .name          = TYPE_VMPORT,
204     .parent        = TYPE_ISA_DEVICE,
205     .instance_size = sizeof(VMPortState),
206     .class_init    = vmport_class_initfn,
207 };
208 
209 static void vmport_register_types(void)
210 {
211     type_register_static(&vmport_info);
212 }
213 
214 type_init(vmport_register_types)
215