]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/bhyve/inout.c
libcbor: update to 0.10.2
[FreeBSD/FreeBSD.git] / usr.sbin / bhyve / inout.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2011 NetApp, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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.
15  *
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
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/param.h>
35 #include <sys/linker_set.h>
36 #include <sys/_iovec.h>
37 #include <sys/mman.h>
38
39 #include <x86/psl.h>
40 #include <x86/segments.h>
41
42 #include <machine/vmm.h>
43 #include <machine/vmm_instruction_emul.h>
44 #include <vmmapi.h>
45
46 #include <stdio.h>
47 #include <string.h>
48 #include <assert.h>
49
50 #include "bhyverun.h"
51 #include "config.h"
52 #include "inout.h"
53
54 SET_DECLARE(inout_port_set, struct inout_port);
55
56 #define MAX_IOPORTS     (1 << 16)
57
58 #define VERIFY_IOPORT(port, size) \
59         assert((port) >= 0 && (size) > 0 && ((port) + (size)) <= MAX_IOPORTS)
60
61 static struct {
62         const char      *name;
63         int             flags;
64         inout_func_t    handler;
65         void            *arg;
66 } inout_handlers[MAX_IOPORTS];
67
68 static int
69 default_inout(struct vmctx *ctx __unused, int in,
70     int port __unused, int bytes, uint32_t *eax, void *arg __unused)
71 {
72         if (in) {
73                 switch (bytes) {
74                 case 4:
75                         *eax = 0xffffffff;
76                         break;
77                 case 2:
78                         *eax = 0xffff;
79                         break;
80                 case 1:
81                         *eax = 0xff;
82                         break;
83                 }
84         }
85
86         return (0);
87 }
88
89 static void
90 register_default_iohandler(int start, int size)
91 {
92         struct inout_port iop;
93
94         VERIFY_IOPORT(start, size);
95
96         bzero(&iop, sizeof(iop));
97         iop.name = "default";
98         iop.port = start;
99         iop.size = size;
100         iop.flags = IOPORT_F_INOUT | IOPORT_F_DEFAULT;
101         iop.handler = default_inout;
102
103         register_inout(&iop);
104 }
105
106 int
107 emulate_inout(struct vmctx *ctx, struct vcpu *vcpu, struct vm_exit *vmexit)
108 {
109         int addrsize, bytes, flags, in, port, prot, rep;
110         uint32_t eax, val;
111         inout_func_t handler;
112         void *arg;
113         int error, fault, retval;
114         enum vm_reg_name idxreg;
115         uint64_t gla, index, iterations, count;
116         struct vm_inout_str *vis;
117         struct iovec iov[2];
118
119         bytes = vmexit->u.inout.bytes;
120         in = vmexit->u.inout.in;
121         port = vmexit->u.inout.port;
122
123         assert(port < MAX_IOPORTS);
124         assert(bytes == 1 || bytes == 2 || bytes == 4);
125
126         handler = inout_handlers[port].handler;
127
128         if (handler == default_inout &&
129             get_config_bool_default("x86.strictio", false))
130                 return (-1);
131
132         flags = inout_handlers[port].flags;
133         arg = inout_handlers[port].arg;
134
135         if (in) {
136                 if (!(flags & IOPORT_F_IN))
137                         return (-1);
138         } else {
139                 if (!(flags & IOPORT_F_OUT))
140                         return (-1);
141         }
142
143         retval = 0;
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);
150
151                 /* Index register */
152                 idxreg = in ? VM_REG_GUEST_RDI : VM_REG_GUEST_RSI;
153                 index = vis->index & vie_size2mask(addrsize);
154
155                 /* Count register */
156                 count = vis->count & vie_size2mask(addrsize);
157
158                 /* Limit number of back-to-back in/out emulations to 16 */
159                 iterations = MIN(count, 16);
160                 while (iterations > 0) {
161                         assert(retval == 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(vcpu);
166                                 break;
167                         }
168
169                         error = vm_copy_setup(vcpu, &vis->paging, gla,
170                             bytes, prot, iov, nitems(iov), &fault);
171                         if (error) {
172                                 retval = -1;  /* Unrecoverable error */
173                                 break;
174                         } else if (fault) {
175                                 retval = 0;  /* Resume guest to handle fault */
176                                 break;
177                         }
178
179                         if (vie_alignment_check(vis->paging.cpl, bytes,
180                             vis->cr0, vis->rflags, gla)) {
181                                 vm_inject_ac(vcpu, 0);
182                                 break;
183                         }
184
185                         val = 0;
186                         if (!in)
187                                 vm_copyin(iov, &val, bytes);
188
189                         retval = handler(ctx, in, port, bytes, &val, arg);
190                         if (retval != 0)
191                                 break;
192
193                         if (in)
194                                 vm_copyout(&val, iov, bytes);
195
196                         /* Update index */
197                         if (vis->rflags & PSL_D)
198                                 index -= bytes;
199                         else
200                                 index += bytes;
201
202                         count--;
203                         iterations--;
204                 }
205
206                 /* Update index register */
207                 error = vie_update_register(vcpu, idxreg, index, addrsize);
208                 assert(error == 0);
209
210                 /*
211                  * Update count register only if the instruction had a repeat
212                  * prefix.
213                  */
214                 if (rep) {
215                         error = vie_update_register(vcpu, VM_REG_GUEST_RCX,
216                             count, addrsize);
217                         assert(error == 0);
218                 }
219
220                 /* Restart the instruction if more iterations remain */
221                 if (retval == 0 && count != 0) {
222                         error = vm_restart_instruction(vcpu);
223                         assert(error == 0);
224                 }
225         } else {
226                 eax = vmexit->u.inout.eax;
227                 val = eax & vie_size2mask(bytes);
228                 retval = handler(ctx, 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(vcpu, VM_REG_GUEST_RAX,
233                             eax);
234                         assert(error == 0);
235                 }
236         }
237         return (retval);
238 }
239
240 void
241 init_inout(void)
242 {
243         struct inout_port **iopp, *iop;
244
245         /*
246          * Set up the default handler for all ports
247          */
248         register_default_iohandler(0, MAX_IOPORTS);
249
250         /*
251          * Overwrite with specified handlers
252          */
253         SET_FOREACH(iopp, inout_port_set) {
254                 iop = *iopp;
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;
260         }
261 }
262
263 int
264 register_inout(struct inout_port *iop)
265 {
266         int i;
267
268         VERIFY_IOPORT(iop->port, iop->size);
269
270         /*
271          * Verify that the new registration is not overwriting an already
272          * allocated i/o range.
273          */
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)
277                                 return (-1);
278                 }
279         }
280
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;
286         }
287
288         return (0);
289 }
290
291 int
292 unregister_inout(struct inout_port *iop)
293 {
294
295         VERIFY_IOPORT(iop->port, iop->size);
296         assert(inout_handlers[iop->port].name == iop->name);
297
298         register_default_iohandler(iop->port, iop->size);
299
300         return (0);
301 }