xref: /illumos-gate/usr/src/uts/intel/io/vmm/sys/vmm_instruction_emul.h (revision 076ad4c710ebdb269f6341db447a83b5781f0b05)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2012 NetApp, Inc.
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 NETAPP, INC ``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 NETAPP, INC 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  * $FreeBSD$
29  */
30 /*
31  * This file and its contents are supplied under the terms of the
32  * Common Development and Distribution License ("CDDL"), version 1.0.
33  * You may only use this file in accordance with the terms of version
34  * 1.0 of the CDDL.
35  *
36  * A full copy of the text of the CDDL should have accompanied this
37  * source.  A copy of the CDDL is also available via the Internet at
38  * http://www.illumos.org/license/CDDL.
39  *
40  * Copyright 2020 Oxide Computer Company
41  */
42 
43 #ifndef	_VMM_INSTRUCTION_EMUL_H_
44 #define	_VMM_INSTRUCTION_EMUL_H_
45 
46 #include <sys/mman.h>
47 #include <machine/vmm.h>
48 
49 struct vie;
50 
51 struct vie *vie_alloc();
52 void vie_free(struct vie *);
53 
54 enum vm_reg_name vie_regnum_map(uint8_t);
55 
56 void vie_init_mmio(struct vie *vie, const char *inst_bytes, uint8_t inst_length,
57     const struct vm_guest_paging *paging, uint64_t gpa);
58 void vie_init_inout(struct vie *vie, const struct vm_inout *inout,
59     uint8_t inst_len, const struct vm_guest_paging *paging);
60 void vie_init_other(struct vie *vie, const struct vm_guest_paging *paging);
61 
62 int vie_fulfill_mmio(struct vie *vie, const struct vm_mmio *res);
63 int vie_fulfill_inout(struct vie *vie, const struct vm_inout *res);
64 
65 bool vie_needs_fetch(const struct vie *vie);
66 bool vie_pending(const struct vie *vie);
67 uint64_t vie_mmio_gpa(const struct vie *vie);
68 void vie_exitinfo(const struct vie *vie, struct vm_exit *vme);
69 void vie_fallback_exitinfo(const struct vie *vie, struct vm_exit *vme);
70 void vie_cs_info(const struct vie *vie, struct vm *vm, int vcpuid,
71     uint64_t *cs_base, int *cs_d);
72 
73 void vie_reset(struct vie *vie);
74 void vie_advance_pc(struct vie *vie, uint64_t *nextrip);
75 
76 int vie_emulate_mmio(struct vie *vie, struct vm *vm, int vcpuid);
77 int vie_emulate_inout(struct vie *vie, struct vm *vm, int vcpuid);
78 int vie_emulate_other(struct vie *vie, struct vm *vm, int vcpuid);
79 
80 /*
81  * APIs to fetch and decode the instruction from nested page fault handler.
82  *
83  * 'vie' must be initialized before calling 'vie_fetch_instruction()'
84  */
85 int vie_fetch_instruction(struct vie *vie, struct vm *vm, int cpuid,
86     uint64_t rip, int *is_fault);
87 
88 /*
89  * Translate the guest linear address 'gla' to a guest physical address.
90  *
91  * retval	is_fault	Interpretation
92  *   0		   0		'gpa' contains result of the translation
93  *   0		   1		An exception was injected into the guest
94  * EFAULT	  N/A		An unrecoverable hypervisor error occurred
95  */
96 int vm_gla2gpa(struct vm *vm, int vcpuid, struct vm_guest_paging *paging,
97     uint64_t gla, int prot, uint64_t *gpa, int *is_fault);
98 
99 /*
100  * Like vm_gla2gpa, but no exceptions are injected into the guest and
101  * PTEs are not changed.
102  */
103 int vm_gla2gpa_nofault(struct vm *vm, int vcpuid,
104     struct vm_guest_paging *paging, uint64_t gla, int prot, uint64_t *gpa,
105     int *is_fault);
106 
107 int vie_verify_gla(struct vie *vie, struct vm *vm, int cpuid, uint64_t gla);
108 /*
109  * Decode the instruction fetched into 'vie' so it can be emulated.
110  *
111  * 'gla' is the guest linear address provided by the hardware assist
112  * that caused the nested page table fault. It is used to verify that
113  * the software instruction decoding is in agreement with the hardware.
114  *
115  * Some hardware assists do not provide the 'gla' to the hypervisor.
116  * To skip the 'gla' verification for this or any other reason pass
117  * in VIE_INVALID_GLA instead.
118  */
119 #define	VIE_INVALID_GLA		(1UL << 63)	/* a non-canonical address */
120 int vie_decode_instruction(struct vie *vie, struct vm *vm, int cpuid, int csd);
121 
122 #endif	/* _VMM_INSTRUCTION_EMUL_H_ */
123