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>
54 SET_DECLARE(inout_port_set, struct inout_port);
56 #define MAX_IOPORTS (1 << 16)
58 #define VERIFY_IOPORT(port, size) \
59 assert((port) >= 0 && (size) > 0 && ((port) + (size)) <= MAX_IOPORTS)
66 } inout_handlers[MAX_IOPORTS];
69 default_inout(struct vmctx *ctx __unused, int vcpu __unused, int in,
70 int port __unused, int bytes, uint32_t *eax, void *arg __unused)
90 register_default_iohandler(int start, int size)
92 struct inout_port iop;
94 VERIFY_IOPORT(start, size);
96 bzero(&iop, sizeof(iop));
100 iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT;
101 iop.handler = default_inout;
103 register_inout(&iop);
107 emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit)
109 int addrsize, bytes, flags, in, port, prot, rep;
111 inout_func_t handler;
113 int error, fault, retval;
114 enum vm_reg_name idxreg;
115 uint64_t gla, index, iterations, count;
116 struct vm_inout_str *vis;
119 bytes = vmexit->u.inout.bytes;
120 in = vmexit->u.inout.in;
121 port = vmexit->u.inout.port;
123 assert(port < MAX_IOPORTS);
124 assert(bytes == 1 || bytes == 2 || bytes == 4);
126 handler = inout_handlers[port].handler;
128 if (handler == default_inout &&
129 get_config_bool_default("x86.strictio", false))
132 flags = inout_handlers[port].flags;
133 arg = inout_handlers[port].arg;
136 if (!(flags & IOPORT_F_IN))
139 if (!(flags & IOPORT_F_OUT))
144 if (vmexit->u.inout.string) {
145 vis = &vmexit->u.inout_str;
146 rep = vis->inout.rep;
147 addrsize = vis->addrsize;
148 prot = in ? PROT_WRITE : PROT_READ;
149 assert(addrsize == 2 || addrsize == 4 || addrsize == 8);
152 idxreg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
153 index = vis->index & vie_size2mask(addrsize);
156 count = vis->count & vie_size2mask(addrsize);
158 /* Limit number of back-to-back in/out emulations to 16 */
159 iterations = MIN(count, 16);
160 while (iterations > 0) {
162 if (vie_calculate_gla(vis->paging.cpu_mode,
163 vis->seg_name, &vis->seg_desc, index, bytes,
164 addrsize, prot, &gla)) {
165 vm_inject_gp(ctx, vcpu);
169 error = vm_copy_setup(ctx, vcpu, &vis->paging, gla,
170 bytes, prot, iov, nitems(iov), &fault);
172 retval = -1; /* Unrecoverable error */
175 retval = 0; /* Resume guest to handle fault */
179 if (vie_alignment_check(vis->paging.cpl, bytes,
180 vis->cr0, vis->rflags, gla)) {
181 vm_inject_ac(ctx, vcpu, 0);
187 vm_copyin(ctx, vcpu, iov, &val, bytes);
189 retval = handler(ctx, vcpu, in, port, bytes, &val, arg);
194 vm_copyout(ctx, vcpu, &val, iov, bytes);
197 if (vis->rflags & PSL_D)
206 /* Update index register */
207 error = vie_update_register(ctx, vcpu, idxreg, index, addrsize);
211 * Update count register only if the instruction had a repeat
215 error = vie_update_register(ctx, vcpu, VM_REG_GUEST_RCX,
220 /* Restart the instruction if more iterations remain */
221 if (retval == 0 && count != 0) {
222 error = vm_restart_instruction(ctx, vcpu);
226 eax = vmexit->u.inout.eax;
227 val = eax & vie_size2mask(bytes);
228 retval = handler(ctx, vcpu, in, port, bytes, &val, arg);
229 if (retval == 0 && in) {
230 eax &= ~vie_size2mask(bytes);
231 eax |= val & vie_size2mask(bytes);
232 error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX,
243 struct inout_port **iopp, *iop;
246 * Set up the default handler for all ports
248 register_default_iohandler(0, MAX_IOPORTS);
251 * Overwrite with specified handlers
253 SET_FOREACH(iopp, inout_port_set) {
255 assert(iop->port < MAX_IOPORTS);
256 inout_handlers[iop->port].name = iop->name;
257 inout_handlers[iop->port].flags = iop->flags;
258 inout_handlers[iop->port].handler = iop->handler;
259 inout_handlers[iop->port].arg = NULL;
264 register_inout(struct inout_port *iop)
268 VERIFY_IOPORT(iop->port, iop->size);
271 * Verify that the new registration is not overwriting an already
272 * allocated i/o range.
274 if ((iop->flags & IOPORT_F_DEFAULT) == 0) {
275 for (i = iop->port; i < iop->port + iop->size; i++) {
276 if ((inout_handlers[i].flags & IOPORT_F_DEFAULT) == 0)
281 for (i = iop->port; i < iop->port + iop->size; i++) {
282 inout_handlers[i].name = iop->name;
283 inout_handlers[i].flags = iop->flags;
284 inout_handlers[i].handler = iop->handler;
285 inout_handlers[i].arg = iop->arg;
292 unregister_inout(struct inout_port *iop)
295 VERIFY_IOPORT(iop->port, iop->size);
296 assert(inout_handlers[iop->port].name == iop->name);
298 register_default_iohandler(iop->port, iop->size);