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