131cf78c9SMark Johnston /*-
231cf78c9SMark Johnston * SPDX-License-Identifier: BSD-2-Clause
331cf78c9SMark Johnston *
431cf78c9SMark Johnston * Copyright (c) 2011 NetApp, Inc.
531cf78c9SMark Johnston * All rights reserved.
631cf78c9SMark Johnston *
731cf78c9SMark Johnston * Redistribution and use in source and binary forms, with or without
831cf78c9SMark Johnston * modification, are permitted provided that the following conditions
931cf78c9SMark Johnston * are met:
1031cf78c9SMark Johnston * 1. Redistributions of source code must retain the above copyright
1131cf78c9SMark Johnston * notice, this list of conditions and the following disclaimer.
1231cf78c9SMark Johnston * 2. Redistributions in binary form must reproduce the above copyright
1331cf78c9SMark Johnston * notice, this list of conditions and the following disclaimer in the
1431cf78c9SMark Johnston * documentation and/or other materials provided with the distribution.
1531cf78c9SMark Johnston *
1631cf78c9SMark Johnston * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
1731cf78c9SMark Johnston * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1831cf78c9SMark Johnston * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1931cf78c9SMark Johnston * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
2031cf78c9SMark Johnston * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2131cf78c9SMark Johnston * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2231cf78c9SMark Johnston * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2331cf78c9SMark Johnston * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2431cf78c9SMark Johnston * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2531cf78c9SMark Johnston * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2631cf78c9SMark Johnston * SUCH DAMAGE.
2731cf78c9SMark Johnston */
2831cf78c9SMark Johnston
2931cf78c9SMark Johnston #include <sys/param.h>
3031cf78c9SMark Johnston #include <sys/linker_set.h>
3131cf78c9SMark Johnston #include <sys/_iovec.h>
3231cf78c9SMark Johnston #include <sys/mman.h>
3331cf78c9SMark Johnston
3431cf78c9SMark Johnston #include <x86/psl.h>
3531cf78c9SMark Johnston
3631cf78c9SMark Johnston #include <machine/vmm.h>
3731cf78c9SMark Johnston #include <machine/vmm_instruction_emul.h>
3831cf78c9SMark Johnston #include <vmmapi.h>
3931cf78c9SMark Johnston
4031cf78c9SMark Johnston #include <stdio.h>
4131cf78c9SMark Johnston #include <string.h>
4231cf78c9SMark Johnston #include <assert.h>
4331cf78c9SMark Johnston
4431cf78c9SMark Johnston #include "bhyverun.h"
4531cf78c9SMark Johnston #include "config.h"
4631cf78c9SMark Johnston #include "inout.h"
4731cf78c9SMark Johnston
4831cf78c9SMark Johnston SET_DECLARE(inout_port_set, struct inout_port);
4931cf78c9SMark Johnston
5031cf78c9SMark Johnston #define MAX_IOPORTS (1 << 16)
5131cf78c9SMark Johnston
5231cf78c9SMark Johnston #define VERIFY_IOPORT(port, size) \
5331cf78c9SMark Johnston assert((port) >= 0 && (size) > 0 && ((port) + (size)) <= MAX_IOPORTS)
5431cf78c9SMark Johnston
5531cf78c9SMark Johnston static struct {
5631cf78c9SMark Johnston const char *name;
5731cf78c9SMark Johnston int flags;
5831cf78c9SMark Johnston inout_func_t handler;
5931cf78c9SMark Johnston void *arg;
6031cf78c9SMark Johnston } inout_handlers[MAX_IOPORTS];
6131cf78c9SMark Johnston
6231cf78c9SMark Johnston static int
default_inout(struct vmctx * ctx __unused,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)6331cf78c9SMark Johnston default_inout(struct vmctx *ctx __unused, int in,
6431cf78c9SMark Johnston int port __unused, int bytes, uint32_t *eax, void *arg __unused)
6531cf78c9SMark Johnston {
6631cf78c9SMark Johnston if (in) {
6731cf78c9SMark Johnston switch (bytes) {
6831cf78c9SMark Johnston case 4:
6931cf78c9SMark Johnston *eax = 0xffffffff;
7031cf78c9SMark Johnston break;
7131cf78c9SMark Johnston case 2:
7231cf78c9SMark Johnston *eax = 0xffff;
7331cf78c9SMark Johnston break;
7431cf78c9SMark Johnston case 1:
7531cf78c9SMark Johnston *eax = 0xff;
7631cf78c9SMark Johnston break;
7731cf78c9SMark Johnston }
7831cf78c9SMark Johnston }
7931cf78c9SMark Johnston
8031cf78c9SMark Johnston return (0);
8131cf78c9SMark Johnston }
8231cf78c9SMark Johnston
8331cf78c9SMark Johnston static void
register_default_iohandler(int start,int size)8431cf78c9SMark Johnston register_default_iohandler(int start, int size)
8531cf78c9SMark Johnston {
8631cf78c9SMark Johnston struct inout_port iop;
8731cf78c9SMark Johnston
8831cf78c9SMark Johnston VERIFY_IOPORT(start, size);
8931cf78c9SMark Johnston
9031cf78c9SMark Johnston bzero(&iop, sizeof(iop));
9131cf78c9SMark Johnston iop.name = "default";
9231cf78c9SMark Johnston iop.port = start;
9331cf78c9SMark Johnston iop.size = size;
9431cf78c9SMark Johnston iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT;
9531cf78c9SMark Johnston iop.handler = default_inout;
9631cf78c9SMark Johnston
9731cf78c9SMark Johnston register_inout(&iop);
9831cf78c9SMark Johnston }
9931cf78c9SMark Johnston
10031cf78c9SMark Johnston int
emulate_inout(struct vmctx * ctx,struct vcpu * vcpu,struct vm_exit * vmexit)10131cf78c9SMark Johnston emulate_inout(struct vmctx *ctx, struct vcpu *vcpu, struct vm_exit *vmexit)
10231cf78c9SMark Johnston {
10331cf78c9SMark Johnston int addrsize, bytes, flags, in, port, prot, rep;
10431cf78c9SMark Johnston uint32_t eax, val;
10531cf78c9SMark Johnston inout_func_t handler;
10631cf78c9SMark Johnston void *arg;
10731cf78c9SMark Johnston int error, fault, retval;
10831cf78c9SMark Johnston enum vm_reg_name idxreg;
10931cf78c9SMark Johnston uint64_t gla, index, iterations, count;
11031cf78c9SMark Johnston struct vm_inout_str *vis;
11131cf78c9SMark Johnston struct iovec iov[2];
11231cf78c9SMark Johnston
11331cf78c9SMark Johnston bytes = vmexit->u.inout.bytes;
11431cf78c9SMark Johnston in = vmexit->u.inout.in;
11531cf78c9SMark Johnston port = vmexit->u.inout.port;
11631cf78c9SMark Johnston
11731cf78c9SMark Johnston assert(port < MAX_IOPORTS);
11831cf78c9SMark Johnston assert(bytes == 1 || bytes == 2 || bytes == 4);
11931cf78c9SMark Johnston
12031cf78c9SMark Johnston handler = inout_handlers[port].handler;
12131cf78c9SMark Johnston
12231cf78c9SMark Johnston if (handler == default_inout &&
12331cf78c9SMark Johnston get_config_bool_default("x86.strictio", false))
12431cf78c9SMark Johnston return (-1);
12531cf78c9SMark Johnston
12631cf78c9SMark Johnston flags = inout_handlers[port].flags;
12731cf78c9SMark Johnston arg = inout_handlers[port].arg;
12831cf78c9SMark Johnston
12931cf78c9SMark Johnston if (in) {
13031cf78c9SMark Johnston if (!(flags & IOPORT_F_IN))
13131cf78c9SMark Johnston return (-1);
13231cf78c9SMark Johnston } else {
13331cf78c9SMark Johnston if (!(flags & IOPORT_F_OUT))
13431cf78c9SMark Johnston return (-1);
13531cf78c9SMark Johnston }
13631cf78c9SMark Johnston
13731cf78c9SMark Johnston retval = 0;
13831cf78c9SMark Johnston if (vmexit->u.inout.string) {
13931cf78c9SMark Johnston vis = &vmexit->u.inout_str;
14031cf78c9SMark Johnston rep = vis->inout.rep;
14131cf78c9SMark Johnston addrsize = vis->addrsize;
14231cf78c9SMark Johnston prot = in ? PROT_WRITE : PROT_READ;
14331cf78c9SMark Johnston assert(addrsize == 2 || addrsize == 4 || addrsize == 8);
14431cf78c9SMark Johnston
14531cf78c9SMark Johnston /* Index register */
14631cf78c9SMark Johnston idxreg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
14731cf78c9SMark Johnston index = vis->index & vie_size2mask(addrsize);
14831cf78c9SMark Johnston
14931cf78c9SMark Johnston /* Count register */
15031cf78c9SMark Johnston count = vis->count & vie_size2mask(addrsize);
15131cf78c9SMark Johnston
15231cf78c9SMark Johnston /* Limit number of back-to-back in/out emulations to 16 */
15331cf78c9SMark Johnston iterations = MIN(count, 16);
15431cf78c9SMark Johnston while (iterations > 0) {
15531cf78c9SMark Johnston assert(retval == 0);
15631cf78c9SMark Johnston if (vie_calculate_gla(vis->paging.cpu_mode,
15731cf78c9SMark Johnston vis->seg_name, &vis->seg_desc, index, bytes,
15831cf78c9SMark Johnston addrsize, prot, &gla)) {
15931cf78c9SMark Johnston vm_inject_gp(vcpu);
16031cf78c9SMark Johnston break;
16131cf78c9SMark Johnston }
16231cf78c9SMark Johnston
16331cf78c9SMark Johnston error = vm_copy_setup(vcpu, &vis->paging, gla,
16431cf78c9SMark Johnston bytes, prot, iov, nitems(iov), &fault);
16531cf78c9SMark Johnston if (error) {
16631cf78c9SMark Johnston retval = -1; /* Unrecoverable error */
16731cf78c9SMark Johnston break;
16831cf78c9SMark Johnston } else if (fault) {
16931cf78c9SMark Johnston retval = 0; /* Resume guest to handle fault */
17031cf78c9SMark Johnston break;
17131cf78c9SMark Johnston }
17231cf78c9SMark Johnston
17331cf78c9SMark Johnston if (vie_alignment_check(vis->paging.cpl, bytes,
17431cf78c9SMark Johnston vis->cr0, vis->rflags, gla)) {
17531cf78c9SMark Johnston vm_inject_ac(vcpu, 0);
17631cf78c9SMark Johnston break;
17731cf78c9SMark Johnston }
17831cf78c9SMark Johnston
17931cf78c9SMark Johnston val = 0;
18031cf78c9SMark Johnston if (!in)
18131cf78c9SMark Johnston vm_copyin(iov, &val, bytes);
18231cf78c9SMark Johnston
18331cf78c9SMark Johnston retval = handler(ctx, in, port, bytes, &val, arg);
18431cf78c9SMark Johnston if (retval != 0)
18531cf78c9SMark Johnston break;
18631cf78c9SMark Johnston
18731cf78c9SMark Johnston if (in)
18831cf78c9SMark Johnston vm_copyout(&val, iov, bytes);
18931cf78c9SMark Johnston
19031cf78c9SMark Johnston /* Update index */
19131cf78c9SMark Johnston if (vis->rflags & PSL_D)
19231cf78c9SMark Johnston index -= bytes;
19331cf78c9SMark Johnston else
19431cf78c9SMark Johnston index += bytes;
19531cf78c9SMark Johnston
19631cf78c9SMark Johnston count--;
19731cf78c9SMark Johnston iterations--;
19831cf78c9SMark Johnston }
19931cf78c9SMark Johnston
20031cf78c9SMark Johnston /* Update index register */
20131cf78c9SMark Johnston error = vie_update_register(vcpu, idxreg, index, addrsize);
20231cf78c9SMark Johnston assert(error == 0);
20331cf78c9SMark Johnston
20431cf78c9SMark Johnston /*
20531cf78c9SMark Johnston * Update count register only if the instruction had a repeat
20631cf78c9SMark Johnston * prefix.
20731cf78c9SMark Johnston */
20831cf78c9SMark Johnston if (rep) {
20931cf78c9SMark Johnston error = vie_update_register(vcpu, VM_REG_GUEST_RCX,
21031cf78c9SMark Johnston count, addrsize);
21131cf78c9SMark Johnston assert(error == 0);
21231cf78c9SMark Johnston }
21331cf78c9SMark Johnston
21431cf78c9SMark Johnston /* Restart the instruction if more iterations remain */
21531cf78c9SMark Johnston if (retval == 0 && count != 0) {
21631cf78c9SMark Johnston error = vm_restart_instruction(vcpu);
21731cf78c9SMark Johnston assert(error == 0);
21831cf78c9SMark Johnston }
21931cf78c9SMark Johnston } else {
22031cf78c9SMark Johnston eax = vmexit->u.inout.eax;
22131cf78c9SMark Johnston val = eax & vie_size2mask(bytes);
22231cf78c9SMark Johnston retval = handler(ctx, in, port, bytes, &val, arg);
22331cf78c9SMark Johnston if (retval == 0 && in) {
22431cf78c9SMark Johnston eax &= ~vie_size2mask(bytes);
22531cf78c9SMark Johnston eax |= val & vie_size2mask(bytes);
22631cf78c9SMark Johnston error = vm_set_register(vcpu, VM_REG_GUEST_RAX,
22731cf78c9SMark Johnston eax);
22831cf78c9SMark Johnston assert(error == 0);
22931cf78c9SMark Johnston }
23031cf78c9SMark Johnston }
23131cf78c9SMark Johnston return (retval);
23231cf78c9SMark Johnston }
23331cf78c9SMark Johnston
23431cf78c9SMark Johnston void
init_inout(void)23531cf78c9SMark Johnston init_inout(void)
23631cf78c9SMark Johnston {
23731cf78c9SMark Johnston struct inout_port **iopp, *iop;
23831cf78c9SMark Johnston
23931cf78c9SMark Johnston /*
24031cf78c9SMark Johnston * Set up the default handler for all ports
24131cf78c9SMark Johnston */
24231cf78c9SMark Johnston register_default_iohandler(0, MAX_IOPORTS);
24331cf78c9SMark Johnston
24431cf78c9SMark Johnston /*
24531cf78c9SMark Johnston * Overwrite with specified handlers
24631cf78c9SMark Johnston */
24731cf78c9SMark Johnston SET_FOREACH(iopp, inout_port_set) {
24831cf78c9SMark Johnston iop = *iopp;
24931cf78c9SMark Johnston assert(iop->port < MAX_IOPORTS);
25031cf78c9SMark Johnston inout_handlers[iop->port].name = iop->name;
25131cf78c9SMark Johnston inout_handlers[iop->port].flags = iop->flags;
25231cf78c9SMark Johnston inout_handlers[iop->port].handler = iop->handler;
25331cf78c9SMark Johnston inout_handlers[iop->port].arg = NULL;
25431cf78c9SMark Johnston }
25531cf78c9SMark Johnston }
25631cf78c9SMark Johnston
25731cf78c9SMark Johnston int
register_inout(struct inout_port * iop)25831cf78c9SMark Johnston register_inout(struct inout_port *iop)
25931cf78c9SMark Johnston {
26031cf78c9SMark Johnston int i;
26131cf78c9SMark Johnston
26231cf78c9SMark Johnston VERIFY_IOPORT(iop->port, iop->size);
26331cf78c9SMark Johnston
26431cf78c9SMark Johnston /*
26531cf78c9SMark Johnston * Verify that the new registration is not overwriting an already
26631cf78c9SMark Johnston * allocated i/o range.
26731cf78c9SMark Johnston */
26831cf78c9SMark Johnston if ((iop->flags & IOPORT_F_DEFAULT) == 0) {
26931cf78c9SMark Johnston for (i = iop->port; i < iop->port + iop->size; i++) {
27031cf78c9SMark Johnston if ((inout_handlers[i].flags & IOPORT_F_DEFAULT) == 0)
27131cf78c9SMark Johnston return (-1);
27231cf78c9SMark Johnston }
27331cf78c9SMark Johnston }
27431cf78c9SMark Johnston
27531cf78c9SMark Johnston for (i = iop->port; i < iop->port + iop->size; i++) {
27631cf78c9SMark Johnston inout_handlers[i].name = iop->name;
27731cf78c9SMark Johnston inout_handlers[i].flags = iop->flags;
27831cf78c9SMark Johnston inout_handlers[i].handler = iop->handler;
27931cf78c9SMark Johnston inout_handlers[i].arg = iop->arg;
28031cf78c9SMark Johnston }
28131cf78c9SMark Johnston
28231cf78c9SMark Johnston return (0);
28331cf78c9SMark Johnston }
28431cf78c9SMark Johnston
28531cf78c9SMark Johnston int
unregister_inout(struct inout_port * iop)28631cf78c9SMark Johnston unregister_inout(struct inout_port *iop)
28731cf78c9SMark Johnston {
28831cf78c9SMark Johnston
28931cf78c9SMark Johnston VERIFY_IOPORT(iop->port, iop->size);
29031cf78c9SMark Johnston assert(inout_handlers[iop->port].name == iop->name);
29131cf78c9SMark Johnston
29231cf78c9SMark Johnston register_default_iohandler(iop->port, iop->size);
29331cf78c9SMark Johnston
29431cf78c9SMark Johnston return (0);
29531cf78c9SMark Johnston }
296