2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #ifndef __VMX_LOWLEVEL_H__
21 #define __VMX_LOWLEVEL_H__
25 #include <palacios/vmcs.h>
28 #define VMX_FAIL_INVALID 1
29 #define VMX_FAIL_VALID 2
32 #define CHECK_VMXFAIL(ret_valid, ret_invalid) \
34 return VMX_FAIL_VALID; \
35 } else if (ret_invalid) { \
36 return VMX_FAIL_INVALID; \
39 /* Opcode definitions for all the VM instructions */
41 #define VMCLEAR_OPCODE ".byte 0x66,0xf,0xc7;" /* reg=/6 */
42 #define VMRESUME_OPCODE ".byte 0x0f,0x01,0xc3;"
43 #define VMPTRLD_OPCODE ".byte 0x0f,0xc7;" /* reg=/6 */
44 #define VMPTRST_OPCODE ".byte 0x0f,0xc7;" /* reg=/7 */
45 #define VMREAD_OPCODE ".byte 0x0f,0x78;"
46 #define VMWRITE_OPCODE ".byte 0x0f,0x79;"
47 #define VMXOFF_OPCODE ".byte 0x0f,0x01,0xc4;"
48 #define VMXON_OPCODE ".byte 0xf3,0x0f,0xc7;" /* reg=/6 */
51 /* Mod/rm definitions for intel registers/memory */
52 #define EAX_ECX_MODRM ".byte 0xc1;"
54 #define EAX_06_MODRM ".byte 0x30;"
56 #define EAX_07_MODRM ".byte 0x38;"
60 static inline int v3_enable_vmx(addr_t vmxon_ptr) {
61 uint64_t vmxon_ptr_64 __attribute__((aligned(8))) = (uint64_t)vmxon_ptr;
62 uint8_t ret_invalid = 0;
64 __asm__ __volatile__ (
67 "setnaeb %0;" // fail invalid (CF=1)
69 : "a"(&vmxon_ptr_64),"0"(ret_invalid)
73 return VMX_FAIL_INVALID;
79 static inline int vmcs_clear(addr_t vmcs_ptr) {
80 uint64_t vmcs_ptr_64 __attribute__ ((aligned(8))) = (uint64_t)vmcs_ptr;
81 uint8_t ret_valid = 0;
82 uint8_t ret_invalid = 0;
84 __asm__ __volatile__ (
87 "seteb %0;" // fail valid (ZF=1)
88 "setnaeb %1;" // fail invalid (CF=1)
89 : "=q"(ret_valid), "=q"(ret_invalid)
90 : "a"(&vmcs_ptr_64), "0"(ret_valid), "1"(ret_invalid)
93 CHECK_VMXFAIL(ret_valid, ret_invalid);
98 static inline int vmcs_load(addr_t vmcs_ptr) {
99 uint64_t vmcs_ptr_64 = (uint64_t)vmcs_ptr;
100 uint8_t ret_valid = 0;
101 uint8_t ret_invalid = 0;
103 __asm__ __volatile__ (
106 "seteb %0;" // fail valid (ZF=1)
107 "setnaeb %1;" // fail invalid (CF=1)
108 : "=q"(ret_valid), "=q"(ret_invalid)
109 : "a"(&vmcs_ptr_64), "0"(ret_valid), "1"(ret_invalid)
112 CHECK_VMXFAIL(ret_valid, ret_invalid);
117 static inline int vmcs_store(addr_t vmcs_ptr) {
118 uint64_t vmcs_ptr_64 = (uint64_t)vmcs_ptr;
120 __asm__ __volatile__ (
130 static inline int vmcs_read(vmcs_field_t vmcs_field, void * dst) {
132 uint8_t ret_valid = 0;
133 uint8_t ret_invalid = 0;
135 __asm__ __volatile__ (
138 "seteb %1;" // fail valid
139 "setnaeb %1;" // fail invalid
140 : "=c"(val), "=d"(ret_valid) //, "=r"(ret_invalid) // Use ECX
141 : "a" (vmcs_field), "0"(0), "1"(ret_valid)
145 CHECK_VMXFAIL(ret_valid, ret_invalid);
147 switch(v3_vmcs_get_field_len(vmcs_field))
150 *((uint16_t*)dst) = (uint16_t)val;
153 *((uint32_t*)dst) = (uint32_t)val;
156 *((uint64_t*)dst) = (uint64_t)val;
166 static inline int vmcs_write(vmcs_field_t vmcs_field, addr_t value) {
167 uint8_t ret_valid = 0;
168 uint8_t ret_invalid = 0;
170 __asm__ __volatile__ (
173 "seteb %0;" // fail valid (ZF=1)
174 "setnaeb %1;" // fail invalid (CF=1)
175 : "=q" (ret_valid), "=q" (ret_invalid)
176 : "a" (vmcs_field), "c"(value)
179 CHECK_VMXFAIL(ret_valid, ret_invalid);
184 static inline int vmx_off() {
185 uint8_t ret_valid = 0;
186 uint8_t ret_invalid = 0;
188 __asm__ __volatile__ (
192 : "=q"(ret_valid), "=q"(ret_invalid)
193 : "0"(ret_valid), "1"(ret_invalid)
196 CHECK_VMXFAIL(ret_valid, ret_invalid);