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