2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
36 #include <sys/_iovec.h>
40 #include <x86/segments.h>
42 #include <machine/vmm.h>
43 #include <machine/vmm_instruction_emul.h>
53 SET_DECLARE(inout_port_set, struct inout_port);
55 #define MAX_IOPORTS (1 << 16)
57 #define VERIFY_IOPORT(port, size) \
58 assert((port) >= 0 && (size) > 0 && ((port) + (size)) <= MAX_IOPORTS)
65 } inout_handlers[MAX_IOPORTS];
68 default_inout(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
69 uint32_t *eax, void *arg)
89 register_default_iohandler(int start, int size)
91 struct inout_port iop;
93 VERIFY_IOPORT(start, size);
95 bzero(&iop, sizeof(iop));
99 iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT;
100 iop.handler = default_inout;
102 register_inout(&iop);
106 emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit, int strict)
108 int addrsize, bytes, flags, in, port, prot, rep;
110 inout_func_t handler;
112 int error, fault, retval;
113 enum vm_reg_name idxreg;
114 uint64_t gla, index, iterations, count;
115 struct vm_inout_str *vis;
118 bytes = vmexit->u.inout.bytes;
119 in = vmexit->u.inout.in;
120 port = vmexit->u.inout.port;
122 assert(port < MAX_IOPORTS);
123 assert(bytes == 1 || bytes == 2 || bytes == 4);
125 handler = inout_handlers[port].handler;
127 if (strict && handler == default_inout)
130 flags = inout_handlers[port].flags;
131 arg = inout_handlers[port].arg;
134 if (!(flags & IOPORT_F_IN))
137 if (!(flags & IOPORT_F_OUT))
142 if (vmexit->u.inout.string) {
143 vis = &vmexit->u.inout_str;
144 rep = vis->inout.rep;
145 addrsize = vis->addrsize;
146 prot = in ? PROT_WRITE : PROT_READ;
147 assert(addrsize == 2 || addrsize == 4 || addrsize == 8);
150 idxreg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
151 index = vis->index & vie_size2mask(addrsize);
154 count = vis->count & vie_size2mask(addrsize);
156 /* Limit number of back-to-back in/out emulations to 16 */
157 iterations = MIN(count, 16);
158 while (iterations > 0) {
160 if (vie_calculate_gla(vis->paging.cpu_mode,
161 vis->seg_name, &vis->seg_desc, index, bytes,
162 addrsize, prot, &gla)) {
163 vm_inject_gp(ctx, vcpu);
167 error = vm_copy_setup(ctx, vcpu, &vis->paging, gla,
168 bytes, prot, iov, nitems(iov), &fault);
170 retval = -1; /* Unrecoverable error */
173 retval = 0; /* Resume guest to handle fault */
177 if (vie_alignment_check(vis->paging.cpl, bytes,
178 vis->cr0, vis->rflags, gla)) {
179 vm_inject_ac(ctx, vcpu, 0);
185 vm_copyin(ctx, vcpu, iov, &val, bytes);
187 retval = handler(ctx, vcpu, in, port, bytes, &val, arg);
192 vm_copyout(ctx, vcpu, &val, iov, bytes);
195 if (vis->rflags & PSL_D)
204 /* Update index register */
205 error = vie_update_register(ctx, vcpu, idxreg, index, addrsize);
209 * Update count register only if the instruction had a repeat
213 error = vie_update_register(ctx, vcpu, VM_REG_GUEST_RCX,
218 /* Restart the instruction if more iterations remain */
219 if (retval == 0 && count != 0) {
220 error = vm_restart_instruction(ctx, vcpu);
224 eax = vmexit->u.inout.eax;
225 val = eax & vie_size2mask(bytes);
226 retval = handler(ctx, vcpu, in, port, bytes, &val, arg);
227 if (retval == 0 && in) {
228 eax &= ~vie_size2mask(bytes);
229 eax |= val & vie_size2mask(bytes);
230 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX,
241 struct inout_port **iopp, *iop;
244 * Set up the default handler for all ports
246 register_default_iohandler(0, MAX_IOPORTS);
249 * Overwrite with specified handlers
251 SET_FOREACH(iopp, inout_port_set) {
253 assert(iop->port < MAX_IOPORTS);
254 inout_handlers[iop->port].name = iop->name;
255 inout_handlers[iop->port].flags = iop->flags;
256 inout_handlers[iop->port].handler = iop->handler;
257 inout_handlers[iop->port].arg = NULL;
262 register_inout(struct inout_port *iop)
266 VERIFY_IOPORT(iop->port, iop->size);
269 * Verify that the new registration is not overwriting an already
270 * allocated i/o range.
272 if ((iop->flags & IOPORT_F_DEFAULT) == 0) {
273 for (i = iop->port; i < iop->port + iop->size; i++) {
274 if ((inout_handlers[i].flags & IOPORT_F_DEFAULT) == 0)
279 for (i = iop->port; i < iop->port + iop->size; i++) {
280 inout_handlers[i].name = iop->name;
281 inout_handlers[i].flags = iop->flags;
282 inout_handlers[i].handler = iop->handler;
283 inout_handlers[i].arg = iop->arg;
290 unregister_inout(struct inout_port *iop)
293 VERIFY_IOPORT(iop->port, iop->size);
294 assert(inout_handlers[iop->port].name == iop->name);
296 register_default_iohandler(iop->port, iop->size);