]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/amd64/vmm/vmm_ioport.c
Add libvmmapi functions vm_copyin() and vm_copyout() to copy into and out
[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 "vmm_ioport.h"
45 #include "vmm_ktr.h"
46
47 #define MAX_IOPORTS             1280
48
49 ioport_handler_func_t ioport_handler[MAX_IOPORTS] = {
50         [TIMER_MODE] = vatpit_handler,
51         [TIMER_CNTR0] = vatpit_handler,
52         [TIMER_CNTR1] = vatpit_handler,
53         [TIMER_CNTR2] = vatpit_handler,
54         [NMISC_PORT] = vatpit_nmisc_handler,
55         [IO_ICU1] = vatpic_master_handler,
56         [IO_ICU1 + ICU_IMR_OFFSET] = vatpic_master_handler,
57         [IO_ICU2] = vatpic_slave_handler,
58         [IO_ICU2 + ICU_IMR_OFFSET] = vatpic_slave_handler,
59         [IO_ELCR1] = vatpic_elc_handler,
60         [IO_ELCR2] = vatpic_elc_handler,
61 };
62
63 #ifdef KTR
64 static const char *
65 inout_instruction(struct vm_exit *vmexit)
66 {
67         int index;
68
69         static const char *iodesc[] = {
70                 "outb", "outw", "outl",
71                 "inb", "inw", "inl",
72                 "outsb", "outsw", "outsd"
73                 "insb", "insw", "insd",
74         };
75
76         switch (vmexit->u.inout.bytes) {
77         case 1:
78                 index = 0;
79                 break;
80         case 2:
81                 index = 1;
82                 break;
83         default:
84                 index = 2;
85                 break;
86         }
87
88         if (vmexit->u.inout.in)
89                 index += 3;
90
91         if (vmexit->u.inout.string)
92                 index += 6;
93
94         KASSERT(index < nitems(iodesc), ("%s: invalid index %d",
95             __func__, index));
96
97         return (iodesc[index]);
98 }
99 #endif  /* KTR */
100
101 static int
102 emulate_inout_port(struct vm *vm, int vcpuid, struct vm_exit *vmexit,
103     bool *retu)
104 {
105         ioport_handler_func_t handler;
106         uint32_t mask, val;
107         int error;
108
109         error = 0;
110         *retu = true;
111
112         if (vmexit->u.inout.port >= MAX_IOPORTS)
113                 goto done;
114
115         handler = ioport_handler[vmexit->u.inout.port];
116         if (handler == NULL)
117                 goto done;
118
119         mask = vie_size2mask(vmexit->u.inout.bytes);
120
121         if (!vmexit->u.inout.in) {
122                 val = vmexit->u.inout.eax & mask;
123         }
124
125         error = (*handler)(vm, vcpuid, vmexit->u.inout.in,
126             vmexit->u.inout.port, vmexit->u.inout.bytes, &val);
127
128         if (!error) {
129                 *retu = false;
130                 if (vmexit->u.inout.in) {
131                         vmexit->u.inout.eax &= ~mask;
132                         vmexit->u.inout.eax |= val & mask;
133                         error = vm_set_register(vm, vcpuid,
134                             VM_REG_GUEST_RAX, vmexit->u.inout.eax);
135                         KASSERT(error == 0, ("emulate_ioport: error %d "
136                             "setting guest rax register", error));
137                 }
138         }
139 done:
140         return (error);
141 }
142
143 static int
144 emulate_inout_str(struct vm *vm, int vcpuid, struct vm_exit *vmexit, bool *retu)
145 {
146         struct vm_inout_str *vis;
147         uint64_t gla, index, segbase;
148         int in;
149
150         vis = &vmexit->u.inout_str;
151         in = vis->inout.in;
152
153         /*
154          * ins/outs VM exit takes precedence over the following error
155          * conditions that would ordinarily be checked by the processor:
156          *
157          * - #GP(0) due to segment being unusable.
158          * - #GP(0) due to memory operand effective address outside the limit
159          *   of the segment.
160          * - #AC(0) if alignment checking is enabled and an unaligned memory
161          *   reference is made at CPL=3
162          */
163
164         /*
165          * XXX
166          * inout string emulation only supported in 64-bit mode.
167          *
168          * The #GP(0) fault conditions described above don't apply in
169          * 64-bit mode.
170          */
171         if (vis->paging.cpu_mode != CPU_MODE_64BIT) { 
172                 VCPU_CTR1(vm, vcpuid, "ins/outs not emulated in cpu mode %d",
173                     vis->paging.cpu_mode);
174                 return (EINVAL);
175         }
176
177         /*
178          * XXX insb/insw/insd instructions not emulated at this time.
179          */
180         if (in) {
181                 VCPU_CTR0(vm, vcpuid, "ins emulation not implemented");
182                 return (EINVAL);
183         }
184
185         segbase = vie_segbase(vis->seg_name, vis->paging.cpu_mode,
186             &vis->seg_desc);
187         index = vis->index & vie_size2mask(vis->addrsize);
188         gla = segbase + index;
189
190         /*
191          * Verify that the computed linear address matches with the one
192          * provided by hardware.
193          */
194         if (vis->gla != VIE_INVALID_GLA) {
195                 KASSERT(gla == vis->gla, ("%s: gla mismatch "
196                     "%#lx/%#lx", __func__, gla, vis->gla));
197         }
198         vis->gla = gla;
199
200         *retu = true;
201         return (0);     /* Return to userspace to finish emulation */
202 }
203
204 int
205 vm_handle_inout(struct vm *vm, int vcpuid, struct vm_exit *vmexit, bool *retu)
206 {
207         int bytes, error;
208
209         bytes = vmexit->u.inout.bytes;
210         KASSERT(bytes == 1 || bytes == 2 || bytes == 4,
211             ("vm_handle_inout: invalid operand size %d", bytes));
212
213         if (vmexit->u.inout.string)
214                 error = emulate_inout_str(vm, vcpuid, vmexit, retu);
215         else
216                 error = emulate_inout_port(vm, vcpuid, vmexit, retu);
217
218         VCPU_CTR4(vm, vcpuid, "%s%s 0x%04x: %s",
219             vmexit->u.inout.rep ? "rep " : "",
220             inout_instruction(vmexit),
221             vmexit->u.inout.port,
222             error ? "error" : (*retu ? "userspace" : "handled"));
223
224         return (error);
225 }