]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/vmm/vmm_ioport.c
Merge from head
[FreeBSD/FreeBSD.git] / sys / amd64 / vmm / vmm_ioport.c
1 /*-
2  * Copyright (c) 2014 Tycho Nightingale <tycho.nightingale@pluribusnetworks.com>
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 THE AUTHOR ``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 THE AUTHOR 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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/queue.h>
33 #include <sys/cpuset.h>
34 #include <sys/systm.h>
35
36 #include <vm/vm.h>
37
38 #include <machine/vmm.h>
39 #include <machine/vmm_instruction_emul.h>
40 #include <x86/psl.h>
41
42 #include "vatpic.h"
43 #include "vatpit.h"
44 #include "vpmtmr.h"
45 #include "vmm_ioport.h"
46 #include "vmm_ktr.h"
47
48 #define MAX_IOPORTS             1280
49
50 ioport_handler_func_t ioport_handler[MAX_IOPORTS] = {
51         [TIMER_MODE] = vatpit_handler,
52         [TIMER_CNTR0] = vatpit_handler,
53         [TIMER_CNTR1] = vatpit_handler,
54         [TIMER_CNTR2] = vatpit_handler,
55         [NMISC_PORT] = vatpit_nmisc_handler,
56         [IO_ICU1] = vatpic_master_handler,
57         [IO_ICU1 + ICU_IMR_OFFSET] = vatpic_master_handler,
58         [IO_ICU2] = vatpic_slave_handler,
59         [IO_ICU2 + ICU_IMR_OFFSET] = vatpic_slave_handler,
60         [IO_ELCR1] = vatpic_elc_handler,
61         [IO_ELCR2] = vatpic_elc_handler,
62         [IO_PMTMR] = vpmtmr_handler,
63 };
64
65 #ifdef KTR
66 static const char *
67 inout_instruction(struct vm_exit *vmexit)
68 {
69         int index;
70
71         static const char *iodesc[] = {
72                 "outb", "outw", "outl",
73                 "inb", "inw", "inl",
74                 "outsb", "outsw", "outsd"
75                 "insb", "insw", "insd",
76         };
77
78         switch (vmexit->u.inout.bytes) {
79         case 1:
80                 index = 0;
81                 break;
82         case 2:
83                 index = 1;
84                 break;
85         default:
86                 index = 2;
87                 break;
88         }
89
90         if (vmexit->u.inout.in)
91                 index += 3;
92
93         if (vmexit->u.inout.string)
94                 index += 6;
95
96         KASSERT(index < nitems(iodesc), ("%s: invalid index %d",
97             __func__, index));
98
99         return (iodesc[index]);
100 }
101 #endif  /* KTR */
102
103 static int
104 emulate_inout_port(struct vm *vm, int vcpuid, struct vm_exit *vmexit,
105     bool *retu)
106 {
107         ioport_handler_func_t handler;
108         uint32_t mask, val;
109         int error;
110
111         /*
112          * If there is no handler for the I/O port then punt to userspace.
113          */
114         if (vmexit->u.inout.port >= MAX_IOPORTS ||
115             (handler = ioport_handler[vmexit->u.inout.port]) == NULL) {
116                 *retu = true;
117                 return (0);
118         }
119
120         mask = vie_size2mask(vmexit->u.inout.bytes);
121
122         if (!vmexit->u.inout.in) {
123                 val = vmexit->u.inout.eax & mask;
124         }
125
126         error = (*handler)(vm, vcpuid, vmexit->u.inout.in,
127             vmexit->u.inout.port, vmexit->u.inout.bytes, &val);
128         if (error) {
129                 /*
130                  * The value returned by this function is also the return value
131                  * of vm_run(). This needs to be a positive number otherwise it
132                  * can be interpreted as a "pseudo-error" like ERESTART.
133                  *
134                  * Enforce this by mapping all errors to EIO.
135                  */
136                 return (EIO);
137         }
138
139         if (vmexit->u.inout.in) {
140                 vmexit->u.inout.eax &= ~mask;
141                 vmexit->u.inout.eax |= val & mask;
142                 error = vm_set_register(vm, vcpuid, VM_REG_GUEST_RAX,
143                     vmexit->u.inout.eax);
144                 KASSERT(error == 0, ("emulate_ioport: error %d setting guest "
145                     "rax register", error));
146         }
147         *retu = false;
148         return (0);
149 }
150
151 static int
152 emulate_inout_str(struct vm *vm, int vcpuid, struct vm_exit *vmexit, bool *retu)
153 {
154         *retu = true;
155         return (0);     /* Return to userspace to finish emulation */
156 }
157
158 int
159 vm_handle_inout(struct vm *vm, int vcpuid, struct vm_exit *vmexit, bool *retu)
160 {
161         int bytes, error;
162
163         bytes = vmexit->u.inout.bytes;
164         KASSERT(bytes == 1 || bytes == 2 || bytes == 4,
165             ("vm_handle_inout: invalid operand size %d", bytes));
166
167         if (vmexit->u.inout.string)
168                 error = emulate_inout_str(vm, vcpuid, vmexit, retu);
169         else
170                 error = emulate_inout_port(vm, vcpuid, vmexit, retu);
171
172         VCPU_CTR4(vm, vcpuid, "%s%s 0x%04x: %s",
173             vmexit->u.inout.rep ? "rep " : "",
174             inout_instruction(vmexit),
175             vmexit->u.inout.port,
176             error ? "error" : (*retu ? "userspace" : "handled"));
177
178         return (error);
179 }