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