1*31cf78c9SMark Johnston /*-
2*31cf78c9SMark Johnston * SPDX-License-Identifier: BSD-2-Clause
3*31cf78c9SMark Johnston *
4*31cf78c9SMark Johnston * Copyright (c) 2011 NetApp, Inc.
5*31cf78c9SMark Johnston * All rights reserved.
6*31cf78c9SMark Johnston *
7*31cf78c9SMark Johnston * Redistribution and use in source and binary forms, with or without
8*31cf78c9SMark Johnston * modification, are permitted provided that the following conditions
9*31cf78c9SMark Johnston * are met:
10*31cf78c9SMark Johnston * 1. Redistributions of source code must retain the above copyright
11*31cf78c9SMark Johnston * notice, this list of conditions and the following disclaimer.
12*31cf78c9SMark Johnston * 2. Redistributions in binary form must reproduce the above copyright
13*31cf78c9SMark Johnston * notice, this list of conditions and the following disclaimer in the
14*31cf78c9SMark Johnston * documentation and/or other materials provided with the distribution.
15*31cf78c9SMark Johnston *
16*31cf78c9SMark Johnston * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND
17*31cf78c9SMark Johnston * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*31cf78c9SMark Johnston * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*31cf78c9SMark Johnston * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE
20*31cf78c9SMark Johnston * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21*31cf78c9SMark Johnston * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22*31cf78c9SMark Johnston * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23*31cf78c9SMark Johnston * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24*31cf78c9SMark Johnston * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25*31cf78c9SMark Johnston * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26*31cf78c9SMark Johnston * SUCH DAMAGE.
27*31cf78c9SMark Johnston */
28*31cf78c9SMark Johnston
29*31cf78c9SMark Johnston #include <sys/param.h>
30*31cf78c9SMark Johnston #include <sys/linker_set.h>
31*31cf78c9SMark Johnston #include <sys/_iovec.h>
32*31cf78c9SMark Johnston #include <sys/mman.h>
33*31cf78c9SMark Johnston
34*31cf78c9SMark Johnston #include <x86/psl.h>
35*31cf78c9SMark Johnston
36*31cf78c9SMark Johnston #include <machine/vmm.h>
37*31cf78c9SMark Johnston #include <machine/vmm_instruction_emul.h>
38*31cf78c9SMark Johnston #include <vmmapi.h>
39*31cf78c9SMark Johnston
40*31cf78c9SMark Johnston #include <stdio.h>
41*31cf78c9SMark Johnston #include <string.h>
42*31cf78c9SMark Johnston #include <assert.h>
43*31cf78c9SMark Johnston
44*31cf78c9SMark Johnston #include "bhyverun.h"
45*31cf78c9SMark Johnston #include "config.h"
46*31cf78c9SMark Johnston #include "inout.h"
47*31cf78c9SMark Johnston
48*31cf78c9SMark Johnston SET_DECLARE(inout_port_set, struct inout_port);
49*31cf78c9SMark Johnston
50*31cf78c9SMark Johnston #define MAX_IOPORTS (1 << 16)
51*31cf78c9SMark Johnston
52*31cf78c9SMark Johnston #define VERIFY_IOPORT(port, size) \
53*31cf78c9SMark Johnston assert((port) >= 0 && (size) > 0 && ((port) + (size)) <= MAX_IOPORTS)
54*31cf78c9SMark Johnston
55*31cf78c9SMark Johnston static struct {
56*31cf78c9SMark Johnston const char *name;
57*31cf78c9SMark Johnston int flags;
58*31cf78c9SMark Johnston inout_func_t handler;
59*31cf78c9SMark Johnston void *arg;
60*31cf78c9SMark Johnston } inout_handlers[MAX_IOPORTS];
61*31cf78c9SMark Johnston
62*31cf78c9SMark Johnston static int
default_inout(struct vmctx * ctx __unused,int in,int port __unused,int bytes,uint32_t * eax,void * arg __unused)63*31cf78c9SMark Johnston default_inout(struct vmctx *ctx __unused, int in,
64*31cf78c9SMark Johnston int port __unused, int bytes, uint32_t *eax, void *arg __unused)
65*31cf78c9SMark Johnston {
66*31cf78c9SMark Johnston if (in) {
67*31cf78c9SMark Johnston switch (bytes) {
68*31cf78c9SMark Johnston case 4:
69*31cf78c9SMark Johnston *eax = 0xffffffff;
70*31cf78c9SMark Johnston break;
71*31cf78c9SMark Johnston case 2:
72*31cf78c9SMark Johnston *eax = 0xffff;
73*31cf78c9SMark Johnston break;
74*31cf78c9SMark Johnston case 1:
75*31cf78c9SMark Johnston *eax = 0xff;
76*31cf78c9SMark Johnston break;
77*31cf78c9SMark Johnston }
78*31cf78c9SMark Johnston }
79*31cf78c9SMark Johnston
80*31cf78c9SMark Johnston return (0);
81*31cf78c9SMark Johnston }
82*31cf78c9SMark Johnston
83*31cf78c9SMark Johnston static void
register_default_iohandler(int start,int size)84*31cf78c9SMark Johnston register_default_iohandler(int start, int size)
85*31cf78c9SMark Johnston {
86*31cf78c9SMark Johnston struct inout_port iop;
87*31cf78c9SMark Johnston
88*31cf78c9SMark Johnston VERIFY_IOPORT(start, size);
89*31cf78c9SMark Johnston
90*31cf78c9SMark Johnston bzero(&iop, sizeof(iop));
91*31cf78c9SMark Johnston iop.name = "default";
92*31cf78c9SMark Johnston iop.port = start;
93*31cf78c9SMark Johnston iop.size = size;
94*31cf78c9SMark Johnston iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT;
95*31cf78c9SMark Johnston iop.handler = default_inout;
96*31cf78c9SMark Johnston
97*31cf78c9SMark Johnston register_inout(&iop);
98*31cf78c9SMark Johnston }
99*31cf78c9SMark Johnston
100*31cf78c9SMark Johnston int
emulate_inout(struct vmctx * ctx,struct vcpu * vcpu,struct vm_exit * vmexit)101*31cf78c9SMark Johnston emulate_inout(struct vmctx *ctx, struct vcpu *vcpu, struct vm_exit *vmexit)
102*31cf78c9SMark Johnston {
103*31cf78c9SMark Johnston int addrsize, bytes, flags, in, port, prot, rep;
104*31cf78c9SMark Johnston uint32_t eax, val;
105*31cf78c9SMark Johnston inout_func_t handler;
106*31cf78c9SMark Johnston void *arg;
107*31cf78c9SMark Johnston int error, fault, retval;
108*31cf78c9SMark Johnston enum vm_reg_name idxreg;
109*31cf78c9SMark Johnston uint64_t gla, index, iterations, count;
110*31cf78c9SMark Johnston struct vm_inout_str *vis;
111*31cf78c9SMark Johnston struct iovec iov[2];
112*31cf78c9SMark Johnston
113*31cf78c9SMark Johnston bytes = vmexit->u.inout.bytes;
114*31cf78c9SMark Johnston in = vmexit->u.inout.in;
115*31cf78c9SMark Johnston port = vmexit->u.inout.port;
116*31cf78c9SMark Johnston
117*31cf78c9SMark Johnston assert(port < MAX_IOPORTS);
118*31cf78c9SMark Johnston assert(bytes == 1 || bytes == 2 || bytes == 4);
119*31cf78c9SMark Johnston
120*31cf78c9SMark Johnston handler = inout_handlers[port].handler;
121*31cf78c9SMark Johnston
122*31cf78c9SMark Johnston if (handler == default_inout &&
123*31cf78c9SMark Johnston get_config_bool_default("x86.strictio", false))
124*31cf78c9SMark Johnston return (-1);
125*31cf78c9SMark Johnston
126*31cf78c9SMark Johnston flags = inout_handlers[port].flags;
127*31cf78c9SMark Johnston arg = inout_handlers[port].arg;
128*31cf78c9SMark Johnston
129*31cf78c9SMark Johnston if (in) {
130*31cf78c9SMark Johnston if (!(flags & IOPORT_F_IN))
131*31cf78c9SMark Johnston return (-1);
132*31cf78c9SMark Johnston } else {
133*31cf78c9SMark Johnston if (!(flags & IOPORT_F_OUT))
134*31cf78c9SMark Johnston return (-1);
135*31cf78c9SMark Johnston }
136*31cf78c9SMark Johnston
137*31cf78c9SMark Johnston retval = 0;
138*31cf78c9SMark Johnston if (vmexit->u.inout.string) {
139*31cf78c9SMark Johnston vis = &vmexit->u.inout_str;
140*31cf78c9SMark Johnston rep = vis->inout.rep;
141*31cf78c9SMark Johnston addrsize = vis->addrsize;
142*31cf78c9SMark Johnston prot = in ? PROT_WRITE : PROT_READ;
143*31cf78c9SMark Johnston assert(addrsize == 2 || addrsize == 4 || addrsize == 8);
144*31cf78c9SMark Johnston
145*31cf78c9SMark Johnston /* Index register */
146*31cf78c9SMark Johnston idxreg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
147*31cf78c9SMark Johnston index = vis->index & vie_size2mask(addrsize);
148*31cf78c9SMark Johnston
149*31cf78c9SMark Johnston /* Count register */
150*31cf78c9SMark Johnston count = vis->count & vie_size2mask(addrsize);
151*31cf78c9SMark Johnston
152*31cf78c9SMark Johnston /* Limit number of back-to-back in/out emulations to 16 */
153*31cf78c9SMark Johnston iterations = MIN(count, 16);
154*31cf78c9SMark Johnston while (iterations > 0) {
155*31cf78c9SMark Johnston assert(retval == 0);
156*31cf78c9SMark Johnston if (vie_calculate_gla(vis->paging.cpu_mode,
157*31cf78c9SMark Johnston vis->seg_name, &vis->seg_desc, index, bytes,
158*31cf78c9SMark Johnston addrsize, prot, &gla)) {
159*31cf78c9SMark Johnston vm_inject_gp(vcpu);
160*31cf78c9SMark Johnston break;
161*31cf78c9SMark Johnston }
162*31cf78c9SMark Johnston
163*31cf78c9SMark Johnston error = vm_copy_setup(vcpu, &vis->paging, gla,
164*31cf78c9SMark Johnston bytes, prot, iov, nitems(iov), &fault);
165*31cf78c9SMark Johnston if (error) {
166*31cf78c9SMark Johnston retval = -1; /* Unrecoverable error */
167*31cf78c9SMark Johnston break;
168*31cf78c9SMark Johnston } else if (fault) {
169*31cf78c9SMark Johnston retval = 0; /* Resume guest to handle fault */
170*31cf78c9SMark Johnston break;
171*31cf78c9SMark Johnston }
172*31cf78c9SMark Johnston
173*31cf78c9SMark Johnston if (vie_alignment_check(vis->paging.cpl, bytes,
174*31cf78c9SMark Johnston vis->cr0, vis->rflags, gla)) {
175*31cf78c9SMark Johnston vm_inject_ac(vcpu, 0);
176*31cf78c9SMark Johnston break;
177*31cf78c9SMark Johnston }
178*31cf78c9SMark Johnston
179*31cf78c9SMark Johnston val = 0;
180*31cf78c9SMark Johnston if (!in)
181*31cf78c9SMark Johnston vm_copyin(iov, &val, bytes);
182*31cf78c9SMark Johnston
183*31cf78c9SMark Johnston retval = handler(ctx, in, port, bytes, &val, arg);
184*31cf78c9SMark Johnston if (retval != 0)
185*31cf78c9SMark Johnston break;
186*31cf78c9SMark Johnston
187*31cf78c9SMark Johnston if (in)
188*31cf78c9SMark Johnston vm_copyout(&val, iov, bytes);
189*31cf78c9SMark Johnston
190*31cf78c9SMark Johnston /* Update index */
191*31cf78c9SMark Johnston if (vis->rflags & PSL_D)
192*31cf78c9SMark Johnston index -= bytes;
193*31cf78c9SMark Johnston else
194*31cf78c9SMark Johnston index += bytes;
195*31cf78c9SMark Johnston
196*31cf78c9SMark Johnston count--;
197*31cf78c9SMark Johnston iterations--;
198*31cf78c9SMark Johnston }
199*31cf78c9SMark Johnston
200*31cf78c9SMark Johnston /* Update index register */
201*31cf78c9SMark Johnston error = vie_update_register(vcpu, idxreg, index, addrsize);
202*31cf78c9SMark Johnston assert(error == 0);
203*31cf78c9SMark Johnston
204*31cf78c9SMark Johnston /*
205*31cf78c9SMark Johnston * Update count register only if the instruction had a repeat
206*31cf78c9SMark Johnston * prefix.
207*31cf78c9SMark Johnston */
208*31cf78c9SMark Johnston if (rep) {
209*31cf78c9SMark Johnston error = vie_update_register(vcpu, VM_REG_GUEST_RCX,
210*31cf78c9SMark Johnston count, addrsize);
211*31cf78c9SMark Johnston assert(error == 0);
212*31cf78c9SMark Johnston }
213*31cf78c9SMark Johnston
214*31cf78c9SMark Johnston /* Restart the instruction if more iterations remain */
215*31cf78c9SMark Johnston if (retval == 0 && count != 0) {
216*31cf78c9SMark Johnston error = vm_restart_instruction(vcpu);
217*31cf78c9SMark Johnston assert(error == 0);
218*31cf78c9SMark Johnston }
219*31cf78c9SMark Johnston } else {
220*31cf78c9SMark Johnston eax = vmexit->u.inout.eax;
221*31cf78c9SMark Johnston val = eax & vie_size2mask(bytes);
222*31cf78c9SMark Johnston retval = handler(ctx, in, port, bytes, &val, arg);
223*31cf78c9SMark Johnston if (retval == 0 && in) {
224*31cf78c9SMark Johnston eax &= ~vie_size2mask(bytes);
225*31cf78c9SMark Johnston eax |= val & vie_size2mask(bytes);
226*31cf78c9SMark Johnston error = vm_set_register(vcpu, VM_REG_GUEST_RAX,
227*31cf78c9SMark Johnston eax);
228*31cf78c9SMark Johnston assert(error == 0);
229*31cf78c9SMark Johnston }
230*31cf78c9SMark Johnston }
231*31cf78c9SMark Johnston return (retval);
232*31cf78c9SMark Johnston }
233*31cf78c9SMark Johnston
234*31cf78c9SMark Johnston void
init_inout(void)235*31cf78c9SMark Johnston init_inout(void)
236*31cf78c9SMark Johnston {
237*31cf78c9SMark Johnston struct inout_port **iopp, *iop;
238*31cf78c9SMark Johnston
239*31cf78c9SMark Johnston /*
240*31cf78c9SMark Johnston * Set up the default handler for all ports
241*31cf78c9SMark Johnston */
242*31cf78c9SMark Johnston register_default_iohandler(0, MAX_IOPORTS);
243*31cf78c9SMark Johnston
244*31cf78c9SMark Johnston /*
245*31cf78c9SMark Johnston * Overwrite with specified handlers
246*31cf78c9SMark Johnston */
247*31cf78c9SMark Johnston SET_FOREACH(iopp, inout_port_set) {
248*31cf78c9SMark Johnston iop = *iopp;
249*31cf78c9SMark Johnston assert(iop->port < MAX_IOPORTS);
250*31cf78c9SMark Johnston inout_handlers[iop->port].name = iop->name;
251*31cf78c9SMark Johnston inout_handlers[iop->port].flags = iop->flags;
252*31cf78c9SMark Johnston inout_handlers[iop->port].handler = iop->handler;
253*31cf78c9SMark Johnston inout_handlers[iop->port].arg = NULL;
254*31cf78c9SMark Johnston }
255*31cf78c9SMark Johnston }
256*31cf78c9SMark Johnston
257*31cf78c9SMark Johnston int
register_inout(struct inout_port * iop)258*31cf78c9SMark Johnston register_inout(struct inout_port *iop)
259*31cf78c9SMark Johnston {
260*31cf78c9SMark Johnston int i;
261*31cf78c9SMark Johnston
262*31cf78c9SMark Johnston VERIFY_IOPORT(iop->port, iop->size);
263*31cf78c9SMark Johnston
264*31cf78c9SMark Johnston /*
265*31cf78c9SMark Johnston * Verify that the new registration is not overwriting an already
266*31cf78c9SMark Johnston * allocated i/o range.
267*31cf78c9SMark Johnston */
268*31cf78c9SMark Johnston if ((iop->flags & IOPORT_F_DEFAULT) == 0) {
269*31cf78c9SMark Johnston for (i = iop->port; i < iop->port + iop->size; i++) {
270*31cf78c9SMark Johnston if ((inout_handlers[i].flags & IOPORT_F_DEFAULT) == 0)
271*31cf78c9SMark Johnston return (-1);
272*31cf78c9SMark Johnston }
273*31cf78c9SMark Johnston }
274*31cf78c9SMark Johnston
275*31cf78c9SMark Johnston for (i = iop->port; i < iop->port + iop->size; i++) {
276*31cf78c9SMark Johnston inout_handlers[i].name = iop->name;
277*31cf78c9SMark Johnston inout_handlers[i].flags = iop->flags;
278*31cf78c9SMark Johnston inout_handlers[i].handler = iop->handler;
279*31cf78c9SMark Johnston inout_handlers[i].arg = iop->arg;
280*31cf78c9SMark Johnston }
281*31cf78c9SMark Johnston
282*31cf78c9SMark Johnston return (0);
283*31cf78c9SMark Johnston }
284*31cf78c9SMark Johnston
285*31cf78c9SMark Johnston int
unregister_inout(struct inout_port * iop)286*31cf78c9SMark Johnston unregister_inout(struct inout_port *iop)
287*31cf78c9SMark Johnston {
288*31cf78c9SMark Johnston
289*31cf78c9SMark Johnston VERIFY_IOPORT(iop->port, iop->size);
290*31cf78c9SMark Johnston assert(inout_handlers[iop->port].name == iop->name);
291*31cf78c9SMark Johnston
292*31cf78c9SMark Johnston register_default_iohandler(iop->port, iop->size);
293*31cf78c9SMark Johnston
294*31cf78c9SMark Johnston return (0);
295*31cf78c9SMark Johnston }
296