xref: /openbmc/qemu/hw/i386/vmport.c (revision c9ab24cef8ab680e3d24d7332599b2b857bd107b)
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 #define VMPORT(obj) OBJECT_CHECK(VMPortState, (obj), TYPE_VMPORT)
47 
48 typedef struct VMPortState {
49     ISADevice parent_obj;
50 
51     MemoryRegion io;
52     VMPortReadFunc *func[VMPORT_ENTRIES];
53     void *opaque[VMPORT_ENTRIES];
54 } VMPortState;
55 
56 static VMPortState *port_state;
57 
58 void vmport_register(unsigned char command, VMPortReadFunc *func, void *opaque)
59 {
60     if (command >= VMPORT_ENTRIES) {
61         return;
62     }
63 
64     trace_vmport_register(command, func, opaque);
65     port_state->func[command] = func;
66     port_state->opaque[command] = opaque;
67 }
68 
69 static uint64_t vmport_ioport_read(void *opaque, hwaddr addr,
70                                    unsigned size)
71 {
72     VMPortState *s = opaque;
73     CPUState *cs = current_cpu;
74     X86CPU *cpu = X86_CPU(cs);
75     CPUX86State *env = &cpu->env;
76     unsigned char command;
77     uint32_t eax;
78 
79     cpu_synchronize_state(cs);
80 
81     eax = env->regs[R_EAX];
82     if (eax != VMPORT_MAGIC) {
83         return eax;
84     }
85 
86     command = env->regs[R_ECX];
87     trace_vmport_command(command);
88     if (command >= VMPORT_ENTRIES || !s->func[command]) {
89         qemu_log_mask(LOG_UNIMP, "vmport: unknown command %x\n", command);
90         return eax;
91     }
92 
93     return s->func[command](s->opaque[command], addr);
94 }
95 
96 static void vmport_ioport_write(void *opaque, hwaddr addr,
97                                 uint64_t val, unsigned size)
98 {
99     X86CPU *cpu = X86_CPU(current_cpu);
100 
101     cpu->env.regs[R_EAX] = vmport_ioport_read(opaque, addr, 4);
102 }
103 
104 static uint32_t vmport_cmd_get_version(void *opaque, uint32_t addr)
105 {
106     X86CPU *cpu = X86_CPU(current_cpu);
107 
108     cpu->env.regs[R_EBX] = VMPORT_MAGIC;
109     return 6;
110 }
111 
112 static uint32_t vmport_cmd_ram_size(void *opaque, uint32_t addr)
113 {
114     X86CPU *cpu = X86_CPU(current_cpu);
115 
116     cpu->env.regs[R_EBX] = 0x1177;
117     return ram_size;
118 }
119 
120 static const MemoryRegionOps vmport_ops = {
121     .read = vmport_ioport_read,
122     .write = vmport_ioport_write,
123     .impl = {
124         .min_access_size = 4,
125         .max_access_size = 4,
126     },
127     .endianness = DEVICE_LITTLE_ENDIAN,
128 };
129 
130 static void vmport_realizefn(DeviceState *dev, Error **errp)
131 {
132     ISADevice *isadev = ISA_DEVICE(dev);
133     VMPortState *s = VMPORT(dev);
134 
135     memory_region_init_io(&s->io, OBJECT(s), &vmport_ops, s, "vmport", 1);
136     isa_register_ioport(isadev, &s->io, 0x5658);
137 
138     port_state = s;
139     /* Register some generic port commands */
140     vmport_register(VMPORT_CMD_GETVERSION, vmport_cmd_get_version, NULL);
141     vmport_register(VMPORT_CMD_GETRAMSIZE, vmport_cmd_ram_size, NULL);
142 }
143 
144 static Property vmport_properties[] = {
145     DEFINE_PROP_END_OF_LIST(),
146 };
147 
148 static void vmport_class_initfn(ObjectClass *klass, void *data)
149 {
150     DeviceClass *dc = DEVICE_CLASS(klass);
151 
152     dc->realize = vmport_realizefn;
153     /* Reason: realize sets global port_state */
154     dc->user_creatable = false;
155     device_class_set_props(dc, vmport_properties);
156 }
157 
158 static const TypeInfo vmport_info = {
159     .name          = TYPE_VMPORT,
160     .parent        = TYPE_ISA_DEVICE,
161     .instance_size = sizeof(VMPortState),
162     .class_init    = vmport_class_initfn,
163 };
164 
165 static void vmport_register_types(void)
166 {
167     type_register_static(&vmport_info);
168 }
169 
170 type_init(vmport_register_types)
171