2011-05-13 04:54:01 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause-FreeBSD
|
|
|
|
*
|
2011-05-13 04:54:01 +00:00
|
|
|
* Copyright (c) 2011 NetApp, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/linker_set.h>
|
2014-05-26 18:21:08 +00:00
|
|
|
#include <sys/_iovec.h>
|
|
|
|
#include <sys/mman.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2014-05-23 05:15:17 +00:00
|
|
|
#include <x86/psl.h>
|
2014-05-23 19:59:14 +00:00
|
|
|
#include <x86/segments.h>
|
2014-05-23 05:15:17 +00:00
|
|
|
|
|
|
|
#include <machine/vmm.h>
|
2014-05-24 20:26:57 +00:00
|
|
|
#include <machine/vmm_instruction_emul.h>
|
2014-05-23 05:15:17 +00:00
|
|
|
#include <vmmapi.h>
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <stdio.h>
|
2013-04-10 02:12:39 +00:00
|
|
|
#include <string.h>
|
2011-05-13 04:54:01 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
2014-05-23 05:15:17 +00:00
|
|
|
#include "bhyverun.h"
|
2019-06-26 20:30:41 +00:00
|
|
|
#include "config.h"
|
2011-05-13 04:54:01 +00:00
|
|
|
#include "inout.h"
|
|
|
|
|
|
|
|
SET_DECLARE(inout_port_set, struct inout_port);
|
|
|
|
|
|
|
|
#define MAX_IOPORTS (1 << 16)
|
|
|
|
|
2013-04-10 02:12:39 +00:00
|
|
|
#define VERIFY_IOPORT(port, size) \
|
|
|
|
assert((port) >= 0 && (size) > 0 && ((port) + (size)) <= MAX_IOPORTS)
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
int flags;
|
|
|
|
inout_func_t handler;
|
|
|
|
void *arg;
|
|
|
|
} inout_handlers[MAX_IOPORTS];
|
|
|
|
|
2011-05-19 21:53:25 +00:00
|
|
|
static int
|
|
|
|
default_inout(struct vmctx *ctx, int vcpu, int in, int port, int bytes,
|
|
|
|
uint32_t *eax, void *arg)
|
|
|
|
{
|
2018-06-14 01:34:53 +00:00
|
|
|
if (in) {
|
|
|
|
switch (bytes) {
|
|
|
|
case 4:
|
|
|
|
*eax = 0xffffffff;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*eax = 0xffff;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
*eax = 0xff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
2011-05-19 21:53:25 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 07:52:38 +00:00
|
|
|
static void
|
2013-04-10 02:12:39 +00:00
|
|
|
register_default_iohandler(int start, int size)
|
|
|
|
{
|
|
|
|
struct inout_port iop;
|
2021-12-26 07:52:38 +00:00
|
|
|
|
2013-04-10 02:12:39 +00:00
|
|
|
VERIFY_IOPORT(start, size);
|
|
|
|
|
|
|
|
bzero(&iop, sizeof(iop));
|
|
|
|
iop.name = "default";
|
|
|
|
iop.port = start;
|
|
|
|
iop.size = size;
|
2013-10-29 00:18:11 +00:00
|
|
|
iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT;
|
2013-04-10 02:12:39 +00:00
|
|
|
iop.handler = default_inout;
|
|
|
|
|
|
|
|
register_inout(&iop);
|
|
|
|
}
|
|
|
|
|
2011-05-13 04:54:01 +00:00
|
|
|
int
|
2019-06-26 20:30:41 +00:00
|
|
|
emulate_inout(struct vmctx *ctx, int vcpu, struct vm_exit *vmexit)
|
2011-05-13 04:54:01 +00:00
|
|
|
{
|
2014-05-27 04:26:22 +00:00
|
|
|
int addrsize, bytes, flags, in, port, prot, rep;
|
2015-01-18 03:08:30 +00:00
|
|
|
uint32_t eax, val;
|
2011-05-13 04:54:01 +00:00
|
|
|
inout_func_t handler;
|
|
|
|
void *arg;
|
2015-05-06 16:25:20 +00:00
|
|
|
int error, fault, retval;
|
2014-05-23 05:15:17 +00:00
|
|
|
enum vm_reg_name idxreg;
|
2014-06-01 02:13:07 +00:00
|
|
|
uint64_t gla, index, iterations, count;
|
2014-05-23 05:15:17 +00:00
|
|
|
struct vm_inout_str *vis;
|
2014-05-26 18:21:08 +00:00
|
|
|
struct iovec iov[2];
|
2014-05-23 05:15:17 +00:00
|
|
|
|
|
|
|
bytes = vmexit->u.inout.bytes;
|
|
|
|
in = vmexit->u.inout.in;
|
|
|
|
port = vmexit->u.inout.port;
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
assert(port < MAX_IOPORTS);
|
2014-05-23 05:15:17 +00:00
|
|
|
assert(bytes == 1 || bytes == 2 || bytes == 4);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2011-05-19 21:53:25 +00:00
|
|
|
handler = inout_handlers[port].handler;
|
|
|
|
|
2019-06-26 20:30:41 +00:00
|
|
|
if (handler == default_inout &&
|
|
|
|
get_config_bool_default("x86.strictio", false))
|
2011-05-13 04:54:01 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
flags = inout_handlers[port].flags;
|
|
|
|
arg = inout_handlers[port].arg;
|
|
|
|
|
2014-05-23 05:15:17 +00:00
|
|
|
if (in) {
|
|
|
|
if (!(flags & IOPORT_F_IN))
|
|
|
|
return (-1);
|
|
|
|
} else {
|
|
|
|
if (!(flags & IOPORT_F_OUT))
|
|
|
|
return (-1);
|
2014-04-18 15:22:56 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 05:15:17 +00:00
|
|
|
retval = 0;
|
|
|
|
if (vmexit->u.inout.string) {
|
|
|
|
vis = &vmexit->u.inout_str;
|
|
|
|
rep = vis->inout.rep;
|
|
|
|
addrsize = vis->addrsize;
|
2014-05-27 04:26:22 +00:00
|
|
|
prot = in ? PROT_WRITE : PROT_READ;
|
2014-05-23 05:15:17 +00:00
|
|
|
assert(addrsize == 2 || addrsize == 4 || addrsize == 8);
|
|
|
|
|
|
|
|
/* Index register */
|
|
|
|
idxreg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
|
2014-05-23 19:59:14 +00:00
|
|
|
index = vis->index & vie_size2mask(addrsize);
|
2014-05-23 05:15:17 +00:00
|
|
|
|
|
|
|
/* Count register */
|
2014-05-23 19:59:14 +00:00
|
|
|
count = vis->count & vie_size2mask(addrsize);
|
2014-05-23 05:15:17 +00:00
|
|
|
|
2014-06-01 02:13:07 +00:00
|
|
|
/* Limit number of back-to-back in/out emulations to 16 */
|
2014-06-01 02:47:09 +00:00
|
|
|
iterations = MIN(count, 16);
|
2014-06-01 02:13:07 +00:00
|
|
|
while (iterations > 0) {
|
2014-07-25 20:18:35 +00:00
|
|
|
assert(retval == 0);
|
2014-05-25 00:57:24 +00:00
|
|
|
if (vie_calculate_gla(vis->paging.cpu_mode,
|
2014-05-27 04:26:22 +00:00
|
|
|
vis->seg_name, &vis->seg_desc, index, bytes,
|
|
|
|
addrsize, prot, &gla)) {
|
2014-07-24 01:38:11 +00:00
|
|
|
vm_inject_gp(ctx, vcpu);
|
2014-06-01 02:13:07 +00:00
|
|
|
break;
|
2014-05-25 00:57:24 +00:00
|
|
|
}
|
|
|
|
|
2014-07-23 04:28:51 +00:00
|
|
|
error = vm_copy_setup(ctx, vcpu, &vis->paging, gla,
|
2015-05-06 16:25:20 +00:00
|
|
|
bytes, prot, iov, nitems(iov), &fault);
|
|
|
|
if (error) {
|
2014-07-25 20:18:35 +00:00
|
|
|
retval = -1; /* Unrecoverable error */
|
|
|
|
break;
|
2015-05-06 16:25:20 +00:00
|
|
|
} else if (fault) {
|
2014-07-25 20:18:35 +00:00
|
|
|
retval = 0; /* Resume guest to handle fault */
|
2014-05-26 18:21:08 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-25 00:57:24 +00:00
|
|
|
if (vie_alignment_check(vis->paging.cpl, bytes,
|
|
|
|
vis->cr0, vis->rflags, gla)) {
|
2014-07-23 04:28:51 +00:00
|
|
|
vm_inject_ac(ctx, vcpu, 0);
|
2014-07-25 20:18:35 +00:00
|
|
|
break;
|
2014-05-25 00:57:24 +00:00
|
|
|
}
|
|
|
|
|
2014-05-23 05:15:17 +00:00
|
|
|
val = 0;
|
2014-05-26 18:21:08 +00:00
|
|
|
if (!in)
|
|
|
|
vm_copyin(ctx, vcpu, iov, &val, bytes);
|
2014-05-23 05:15:17 +00:00
|
|
|
|
|
|
|
retval = handler(ctx, vcpu, in, port, bytes, &val, arg);
|
|
|
|
if (retval != 0)
|
|
|
|
break;
|
|
|
|
|
2014-05-26 18:21:08 +00:00
|
|
|
if (in)
|
|
|
|
vm_copyout(ctx, vcpu, &val, iov, bytes);
|
2014-05-23 05:15:17 +00:00
|
|
|
|
|
|
|
/* Update index */
|
|
|
|
if (vis->rflags & PSL_D)
|
|
|
|
index -= bytes;
|
|
|
|
else
|
|
|
|
index += bytes;
|
|
|
|
|
|
|
|
count--;
|
2014-06-01 02:13:07 +00:00
|
|
|
iterations--;
|
2014-05-23 05:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Update index register */
|
|
|
|
error = vie_update_register(ctx, vcpu, idxreg, index, addrsize);
|
|
|
|
assert(error == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update count register only if the instruction had a repeat
|
|
|
|
* prefix.
|
|
|
|
*/
|
|
|
|
if (rep) {
|
|
|
|
error = vie_update_register(ctx, vcpu, VM_REG_GUEST_RCX,
|
|
|
|
count, addrsize);
|
|
|
|
assert(error == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Restart the instruction if more iterations remain */
|
2015-01-18 03:08:30 +00:00
|
|
|
if (retval == 0 && count != 0) {
|
|
|
|
error = vm_restart_instruction(ctx, vcpu);
|
|
|
|
assert(error == 0);
|
2014-05-23 05:15:17 +00:00
|
|
|
}
|
2015-01-18 03:08:30 +00:00
|
|
|
} else {
|
|
|
|
eax = vmexit->u.inout.eax;
|
|
|
|
val = eax & vie_size2mask(bytes);
|
2014-05-23 05:15:17 +00:00
|
|
|
retval = handler(ctx, vcpu, in, port, bytes, &val, arg);
|
|
|
|
if (retval == 0 && in) {
|
2015-01-18 03:08:30 +00:00
|
|
|
eax &= ~vie_size2mask(bytes);
|
|
|
|
eax |= val & vie_size2mask(bytes);
|
|
|
|
error = vm_set_register(ctx, vcpu, VM_REG_GUEST_RAX,
|
|
|
|
eax);
|
|
|
|
assert(error == 0);
|
2014-05-23 05:15:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (retval);
|
2011-05-13 04:54:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
init_inout(void)
|
|
|
|
{
|
|
|
|
struct inout_port **iopp, *iop;
|
2011-05-19 21:53:25 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the default handler for all ports
|
|
|
|
*/
|
2013-04-10 02:12:39 +00:00
|
|
|
register_default_iohandler(0, MAX_IOPORTS);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
2011-05-19 21:53:25 +00:00
|
|
|
/*
|
|
|
|
* Overwrite with specified handlers
|
|
|
|
*/
|
2011-05-13 04:54:01 +00:00
|
|
|
SET_FOREACH(iopp, inout_port_set) {
|
|
|
|
iop = *iopp;
|
|
|
|
assert(iop->port < MAX_IOPORTS);
|
|
|
|
inout_handlers[iop->port].name = iop->name;
|
|
|
|
inout_handlers[iop->port].flags = iop->flags;
|
|
|
|
inout_handlers[iop->port].handler = iop->handler;
|
|
|
|
inout_handlers[iop->port].arg = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
register_inout(struct inout_port *iop)
|
|
|
|
{
|
2013-04-10 02:12:39 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
VERIFY_IOPORT(iop->port, iop->size);
|
2013-10-29 00:18:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Verify that the new registration is not overwriting an already
|
|
|
|
* allocated i/o range.
|
|
|
|
*/
|
|
|
|
if ((iop->flags & IOPORT_F_DEFAULT) == 0) {
|
|
|
|
for (i = iop->port; i < iop->port + iop->size; i++) {
|
|
|
|
if ((inout_handlers[i].flags & IOPORT_F_DEFAULT) == 0)
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-10 02:12:39 +00:00
|
|
|
for (i = iop->port; i < iop->port + iop->size; i++) {
|
|
|
|
inout_handlers[i].name = iop->name;
|
|
|
|
inout_handlers[i].flags = iop->flags;
|
|
|
|
inout_handlers[i].handler = iop->handler;
|
|
|
|
inout_handlers[i].arg = iop->arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
unregister_inout(struct inout_port *iop)
|
|
|
|
{
|
|
|
|
|
|
|
|
VERIFY_IOPORT(iop->port, iop->size);
|
|
|
|
assert(inout_handlers[iop->port].name == iop->name);
|
|
|
|
|
|
|
|
register_default_iohandler(iop->port, iop->size);
|
2011-05-13 04:54:01 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|