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