1ba9b7bf7SNeel Natu /*- 2c49761ddSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3c49761ddSPedro F. Giffuni * 4ba9b7bf7SNeel Natu * Copyright (c) 2012 NetApp, Inc. 5ba9b7bf7SNeel Natu * All rights reserved. 6ba9b7bf7SNeel Natu * 7ba9b7bf7SNeel Natu * Redistribution and use in source and binary forms, with or without 8ba9b7bf7SNeel Natu * modification, are permitted provided that the following conditions 9ba9b7bf7SNeel Natu * are met: 10ba9b7bf7SNeel Natu * 1. Redistributions of source code must retain the above copyright 11ba9b7bf7SNeel Natu * notice, this list of conditions and the following disclaimer. 12ba9b7bf7SNeel Natu * 2. Redistributions in binary form must reproduce the above copyright 13ba9b7bf7SNeel Natu * notice, this list of conditions and the following disclaimer in the 14ba9b7bf7SNeel Natu * documentation and/or other materials provided with the distribution. 15ba9b7bf7SNeel Natu * 16ba9b7bf7SNeel Natu * THIS SOFTWARE IS PROVIDED BY NETAPP, INC ``AS IS'' AND 17ba9b7bf7SNeel Natu * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18ba9b7bf7SNeel Natu * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19ba9b7bf7SNeel Natu * ARE DISCLAIMED. IN NO EVENT SHALL NETAPP, INC OR CONTRIBUTORS BE LIABLE 20ba9b7bf7SNeel Natu * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21ba9b7bf7SNeel Natu * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22ba9b7bf7SNeel Natu * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23ba9b7bf7SNeel Natu * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24ba9b7bf7SNeel Natu * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25ba9b7bf7SNeel Natu * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26ba9b7bf7SNeel Natu * SUCH DAMAGE. 27ba9b7bf7SNeel Natu * 28ba9b7bf7SNeel Natu * $FreeBSD$ 29ba9b7bf7SNeel Natu */ 30ba9b7bf7SNeel Natu 31ba9b7bf7SNeel Natu #ifndef _VMM_INSTRUCTION_EMUL_H_ 32ba9b7bf7SNeel Natu #define _VMM_INSTRUCTION_EMUL_H_ 33ba9b7bf7SNeel Natu 3465ffa035SNeel Natu #include <sys/mman.h> 3565ffa035SNeel Natu 36ba9b7bf7SNeel Natu /* 37ba9b7bf7SNeel Natu * Callback functions to read and write memory regions. 38ba9b7bf7SNeel Natu */ 39ba9b7bf7SNeel Natu typedef int (*mem_region_read_t)(void *vm, int cpuid, uint64_t gpa, 40ba9b7bf7SNeel Natu uint64_t *rval, int rsize, void *arg); 41ba9b7bf7SNeel Natu 42ba9b7bf7SNeel Natu typedef int (*mem_region_write_t)(void *vm, int cpuid, uint64_t gpa, 43ba9b7bf7SNeel Natu uint64_t wval, int wsize, void *arg); 44ba9b7bf7SNeel Natu 45ba9b7bf7SNeel Natu /* 46ba9b7bf7SNeel Natu * Emulate the decoded 'vie' instruction. 47ba9b7bf7SNeel Natu * 48ba9b7bf7SNeel Natu * The callbacks 'mrr' and 'mrw' emulate reads and writes to the memory region 49ba9b7bf7SNeel Natu * containing 'gpa'. 'mrarg' is an opaque argument that is passed into the 50ba9b7bf7SNeel Natu * callback functions. 51ba9b7bf7SNeel Natu * 52ba9b7bf7SNeel Natu * 'void *vm' should be 'struct vm *' when called from kernel context and 53ba9b7bf7SNeel Natu * 'struct vmctx *' when called from user context. 54ba9b7bf7SNeel Natu * s 55ba9b7bf7SNeel Natu */ 56ba9b7bf7SNeel Natu int vmm_emulate_instruction(void *vm, int cpuid, uint64_t gpa, struct vie *vie, 57d665d229SNeel Natu struct vm_guest_paging *paging, mem_region_read_t mrr, 58d665d229SNeel Natu mem_region_write_t mrw, void *mrarg); 59ba9b7bf7SNeel Natu 60d17b5104SNeel Natu int vie_update_register(void *vm, int vcpuid, enum vm_reg_name reg, 61d17b5104SNeel Natu uint64_t val, int size); 62d17b5104SNeel Natu 63a7424861SNeel Natu /* 64a7424861SNeel Natu * Returns 1 if an alignment check exception should be injected and 0 otherwise. 65a7424861SNeel Natu */ 66a7424861SNeel Natu int vie_alignment_check(int cpl, int operand_size, uint64_t cr0, 67a7424861SNeel Natu uint64_t rflags, uint64_t gla); 68a7424861SNeel Natu 69e813a873SNeel Natu /* Returns 1 if the 'gla' is not canonical and 0 otherwise. */ 70e813a873SNeel Natu int vie_canonical_check(enum vm_cpu_mode cpu_mode, uint64_t gla); 71e813a873SNeel Natu 72a7424861SNeel Natu uint64_t vie_size2mask(int size); 73a7424861SNeel Natu 7465ffa035SNeel Natu int vie_calculate_gla(enum vm_cpu_mode cpu_mode, enum vm_reg_name seg, 7565ffa035SNeel Natu struct seg_desc *desc, uint64_t off, int length, int addrsize, int prot, 7665ffa035SNeel Natu uint64_t *gla); 775382c19dSNeel Natu 78ba9b7bf7SNeel Natu #ifdef _KERNEL 79ba9b7bf7SNeel Natu /* 80ba9b7bf7SNeel Natu * APIs to fetch and decode the instruction from nested page fault handler. 81318224bbSNeel Natu * 82318224bbSNeel Natu * 'vie' must be initialized before calling 'vmm_fetch_instruction()' 83ba9b7bf7SNeel Natu */ 84ba9b7bf7SNeel Natu int vmm_fetch_instruction(struct vm *vm, int cpuid, 85e813a873SNeel Natu struct vm_guest_paging *guest_paging, 869c4d5478SNeel Natu uint64_t rip, int inst_length, struct vie *vie, 879c4d5478SNeel Natu int *is_fault); 88ba9b7bf7SNeel Natu 89fd949af6SNeel Natu /* 90fd949af6SNeel Natu * Translate the guest linear address 'gla' to a guest physical address. 91fd949af6SNeel Natu * 929c4d5478SNeel Natu * retval is_fault Interpretation 939c4d5478SNeel Natu * 0 0 'gpa' contains result of the translation 949c4d5478SNeel Natu * 0 1 An exception was injected into the guest 959c4d5478SNeel Natu * EFAULT N/A An unrecoverable hypervisor error occurred 96fd949af6SNeel Natu */ 97ef7c2a82STycho Nightingale int vm_gla2gpa(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, 989c4d5478SNeel Natu uint64_t gla, int prot, uint64_t *gpa, int *is_fault); 99fd949af6SNeel Natu 100*5f8754c0SJohn Baldwin /* 101*5f8754c0SJohn Baldwin * Like vm_gla2gpa, but no exceptions are injected into the guest and 102*5f8754c0SJohn Baldwin * PTEs are not changed. 103*5f8754c0SJohn Baldwin */ 104*5f8754c0SJohn Baldwin int vm_gla2gpa_nofault(struct vm *vm, int vcpuid, struct vm_guest_paging *paging, 105*5f8754c0SJohn Baldwin uint64_t gla, int prot, uint64_t *gpa, int *is_fault); 106*5f8754c0SJohn Baldwin 107c2a875f9SNeel Natu void vie_init(struct vie *vie, const char *inst_bytes, int inst_length); 108318224bbSNeel Natu 10966f71b7dSNeel Natu /* 11066f71b7dSNeel Natu * Decode the instruction fetched into 'vie' so it can be emulated. 11166f71b7dSNeel Natu * 11266f71b7dSNeel Natu * 'gla' is the guest linear address provided by the hardware assist 11366f71b7dSNeel Natu * that caused the nested page table fault. It is used to verify that 11466f71b7dSNeel Natu * the software instruction decoding is in agreement with the hardware. 11566f71b7dSNeel Natu * 11666f71b7dSNeel Natu * Some hardware assists do not provide the 'gla' to the hypervisor. 11766f71b7dSNeel Natu * To skip the 'gla' verification for this or any other reason pass 11866f71b7dSNeel Natu * in VIE_INVALID_GLA instead. 11966f71b7dSNeel Natu */ 12066f71b7dSNeel Natu #define VIE_INVALID_GLA (1UL << 63) /* a non-canonical address */ 12100f3efe1SJohn Baldwin int vmm_decode_instruction(struct vm *vm, int cpuid, uint64_t gla, 122f7a9f178SNeel Natu enum vm_cpu_mode cpu_mode, int csd, struct vie *vie); 123ba9b7bf7SNeel Natu #endif /* _KERNEL */ 124ba9b7bf7SNeel Natu 125ba9b7bf7SNeel Natu #endif /* _VMM_INSTRUCTION_EMUL_H_ */ 126