2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2011 NetApp, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #ifndef _VMX_CPUFUNC_H_
32 #define _VMX_CPUFUNC_H_
37 * Section 5.2 "Conventions" from Intel Architecture Manual 2B.
42 * VMFailValid 2 see also VMCS VM-Instruction Error Field
45 #define VM_FAIL_INVALID 1
46 #define VM_FAIL_VALID 2
47 #define VMX_SET_ERROR_CODE \
49 " mov $1, %[error];" /* CF: error = 1 */ \
52 " mov $2, %[error];" /* ZF: error = 2 */ \
54 "2: mov $0, %[error];" \
57 /* returns 0 on success and non-zero on failure */
64 addr = vtophys(region);
65 __asm __volatile("vmxon %[addr];"
67 : [error] "=r" (error)
68 : [addr] "m" (*(uint64_t *)&addr)
74 /* returns 0 on success and non-zero on failure */
76 vmclear(struct vmcs *vmcs)
82 __asm __volatile("vmclear %[addr];"
84 : [error] "=r" (error)
85 : [addr] "m" (*(uint64_t *)&addr)
94 __asm __volatile("vmxoff");
98 vmptrst(uint64_t *addr)
101 __asm __volatile("vmptrst %[addr]" :: [addr]"m" (*addr) : "memory");
105 vmptrld(struct vmcs *vmcs)
110 addr = vtophys(vmcs);
111 __asm __volatile("vmptrld %[addr];"
113 : [error] "=r" (error)
114 : [addr] "m" (*(uint64_t *)&addr)
120 vmwrite(uint64_t reg, uint64_t val)
124 __asm __volatile("vmwrite %[val], %[reg];"
126 : [error] "=r" (error)
127 : [val] "r" (val), [reg] "r" (reg)
134 vmread(uint64_t r, uint64_t *addr)
138 __asm __volatile("vmread %[r], %[addr];"
140 : [error] "=r" (error)
141 : [r] "r" (r), [addr] "m" (*addr)
148 VMCLEAR(struct vmcs *vmcs)
154 panic("%s: vmclear(%p) error %d", __func__, vmcs, err);
160 VMPTRLD(struct vmcs *vmcs)
168 panic("%s: vmptrld(%p) error %d", __func__, vmcs, err);
171 #define INVVPID_TYPE_ADDRESS 0UL
172 #define INVVPID_TYPE_SINGLE_CONTEXT 1UL
173 #define INVVPID_TYPE_ALL_CONTEXTS 2UL
175 struct invvpid_desc {
179 uint64_t linear_addr;
181 CTASSERT(sizeof(struct invvpid_desc) == 16);
184 invvpid(uint64_t type, struct invvpid_desc desc)
188 __asm __volatile("invvpid %[desc], %[type];"
190 : [error] "=r" (error)
191 : [desc] "m" (desc), [type] "r" (type)
195 panic("invvpid error %d", error);
198 #define INVEPT_TYPE_SINGLE_CONTEXT 1UL
199 #define INVEPT_TYPE_ALL_CONTEXTS 2UL
204 CTASSERT(sizeof(struct invept_desc) == 16);
207 invept(uint64_t type, struct invept_desc desc)
211 __asm __volatile("invept %[desc], %[type];"
213 : [error] "=r" (error)
214 : [desc] "m" (desc), [type] "r" (type)
218 panic("invept error %d", error);