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