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