Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Corrected RTC timer bug (nvram)
[palacios.git] / palacios / include / palacios / vmx_lowlevel.h
1 /* 
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.  
5  *
6  * The V3VEE Project is a joint project between Northwestern University
7  * and the University of New Mexico.  You can find out more at 
8  * http://www.v3vee.org
9  *
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.
13  *
14  * Author: Jack Lange <jarusl@cs.northwestern.edu>
15  *
16  * This is free software.  You are permitted to use,
17  * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
18  */
19
20 #ifndef __VMX_LOWLEVEL_H__
21 #define __VMX_LOWLEVEL_H__
22
23 #ifdef __V3VEE__
24
25 #include <palacios/vmcs.h>
26
27 #define VMX_SUCCESS         0 
28 #define VMX_FAIL_INVALID    1
29 #define VMX_FAIL_VALID      2
30
31 // vmfail macro
32 #define CHECK_VMXFAIL(ret_valid, ret_invalid)   \
33     if (ret_valid) {                            \
34         return VMX_FAIL_VALID;                  \
35     } else if (ret_invalid) {                   \
36         return VMX_FAIL_INVALID;                \
37     }
38
39 /* Opcode definitions for all the VM instructions */
40
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 */
49
50
51 /* Mod/rm definitions for intel registers/memory */
52 #define EAX_ECX_MODRM   ".byte 0xc1;"
53 // %eax with /6 reg
54 #define EAX_06_MODRM    ".byte 0x30;"
55 // %eax with /7 reg
56 #define EAX_07_MODRM    ".byte 0x38;"
57
58
59
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;
63
64     __asm__ __volatile__ (
65                 VMXON_OPCODE
66                 EAX_06_MODRM
67                 "setnaeb %0;" // fail invalid (CF=1)
68                 : "=q"(ret_invalid)
69                 : "a"(&vmxon_ptr_64),"0"(ret_invalid)
70                 : "memory");
71
72     if (ret_invalid) {
73         return VMX_FAIL_INVALID;
74     } else {
75         return VMX_SUCCESS;
76     }
77 }
78
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;
83
84     __asm__ __volatile__ (
85             VMCLEAR_OPCODE
86             EAX_06_MODRM
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)
91             : "memory");
92
93     CHECK_VMXFAIL(ret_valid, ret_invalid);
94   
95     return VMX_SUCCESS;
96 }
97
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;
102     
103     __asm__ __volatile__ (
104                 VMPTRLD_OPCODE
105                 EAX_06_MODRM
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)
110                 : "memory");
111     
112     CHECK_VMXFAIL(ret_valid, ret_invalid);
113
114     return VMX_SUCCESS;
115 }
116
117 static inline int vmcs_store(addr_t vmcs_ptr) {
118     uint64_t vmcs_ptr_64 = (uint64_t)vmcs_ptr;
119
120     __asm__ __volatile__ (
121                VMPTRST_OPCODE
122                EAX_07_MODRM
123                :
124                : "a"(&vmcs_ptr_64)
125                : "memory");
126
127     return VMX_SUCCESS;
128 }
129
130 static inline int vmcs_read(vmcs_field_t vmcs_field, void * dst) {
131     addr_t val = 0;
132     uint8_t ret_valid = 0;
133     uint8_t ret_invalid = 0;
134
135     __asm__ __volatile__ (  
136                 VMREAD_OPCODE
137                 EAX_ECX_MODRM
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)
142                 : "memory"
143                 );
144
145     CHECK_VMXFAIL(ret_valid, ret_invalid);
146
147     switch(v3_vmcs_get_field_len(vmcs_field))
148     {
149         case 2:
150             *((uint16_t*)dst) = (uint16_t)val;
151             break;
152         case 4:
153             *((uint32_t*)dst) = (uint32_t)val;
154             break;
155         case 8:
156             *((uint64_t*)dst) = (uint64_t)val;
157             break;
158         default:
159              return -1;
160     }
161
162
163     return VMX_SUCCESS;
164 }
165
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;
169
170     __asm__ __volatile__ (
171                 VMWRITE_OPCODE
172                 EAX_ECX_MODRM
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)
177                 : "memory");
178
179     CHECK_VMXFAIL(ret_valid, ret_invalid);
180
181     return VMX_SUCCESS;
182 }
183
184 static inline int vmx_off() {
185     uint8_t ret_valid = 0;
186     uint8_t ret_invalid = 0;
187
188     __asm__ __volatile__ (
189                 VMXOFF_OPCODE
190                 "seteb %0;"
191                 "setnaeb %1;"
192                 : "=q"(ret_valid), "=q"(ret_invalid)
193                 : "0"(ret_valid), "1"(ret_invalid)
194                 : "memory");
195
196     CHECK_VMXFAIL(ret_valid, ret_invalid);
197
198     return VMX_SUCCESS;
199 }
200
201 #endif
202
203 #endif