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.


Add HVM configuration capability, init/deinit, etc
[palacios.git] / palacios / src / palacios / vmx_lowlevel.S
1 #;  -*- fundamental -*-
2
3 #define VMX_SUCCESS         0
4 #define VMX_FAIL_INVALID    1
5 #define VMX_FAIL_VALID      2
6 #define VMM_FAILURE         3
7
8 #define VMCS_HOST_RSP       0x00006C14
9 #define VMCS_HOST_RIP       0x00006C16
10
11 #if defined(__V3_64BIT__)
12
13 #define save_registers(location)        \
14         pushq   %rax;                   \
15         movq    location, %rax;         \
16         movq    %rdi, (%rax);           \
17         movq    %rsi, 8(%rax);          \
18         movq    %rbp, 16(%rax);         \
19         movq    $0, 24(%rax);           \
20         movq    %rbx, 32(%rax);         \
21         movq    %rdx, 40(%rax);         \
22         movq    %rcx, 48(%rax);         \
23         pushq   %rbx;                   \
24         movq    8(%rsp), %rbx;          \
25         movq    %rbx, 56(%rax);         \
26         popq    %rbx;                   \
27                                         \
28         movq    %r8, 64(%rax);          \
29         movq    %r9, 72(%rax);          \
30         movq    %r10, 80(%rax);         \
31         movq    %r11, 88(%rax);         \
32         movq    %r12, 96(%rax);         \
33         movq    %r13, 104(%rax);        \
34         movq    %r14, 112(%rax);        \
35         movq    %r15, 120(%rax);        \
36         popq    %rax;                   
37         
38 #define restore_registers(location) \
39         mov     location, %rax;         \
40         mov     (%rax), %rdi;           \
41         mov     8(%rax), %rsi;          \
42         mov     16(%rax), %rbp;         \
43         mov     32(%rax), %rbx;         \
44         mov     40(%rax), %rdx;         \
45         mov 48(%rax), %rcx;             \
46                                         \
47         mov     64(%rax), %r8;          \
48         mov     72(%rax), %r9;          \
49         mov     80(%rax), %r10;         \
50         mov     88(%rax), %r11;         \
51         mov     96(%rax), %r12;         \
52         mov     104(%rax), %r13;        \
53         mov     112(%rax), %r14;        \
54         mov     120(%rax), %r15;        \
55         pushq %rbx;                     \
56         movq 56(%rax), %rbx;            \
57         movq %rbx, %rax;                \
58         popq %rbx;
59
60 #define save_ctrl_regs(location)  \
61     pushq %rax;              \
62     pushq %rbx;              \
63     movq location, %rax;     \
64     movq %cr2, %rbx;         \
65     movq %rbx, 8(%rax);      \
66     popq %rbx;               \
67     popq %rax
68
69 #define restore_ctrl_regs(location)  \
70     pushq %rax;              \
71     pushq %rbx;              \
72     movq location, %rax;     \
73     movq 8(%rax), %rbx;      \
74     movq %rbx, %cr2;         \
75     popq %rbx;               \
76     popq %rax
77
78 #define PUSHA    \
79     push %rax;   \
80     push %rbx;   \
81     push %rcx;   \
82     push %rdx;   \
83     push %rbp;   \
84     push %rdi;   \
85     push %rsi;   \
86     push %r8 ;   \
87     push %r9 ;   \
88     push %r10;   \
89     push %r11;   \
90     push %r12;   \
91     push %r13;   \
92     push %r14;   \
93     push %r15;   
94
95 #define POPA     \
96     pop %r15;    \
97     pop %r14;    \
98     pop %r13;    \
99     pop %r12;    \
100     pop %r11;    \
101     pop %r10;    \
102     pop %r9 ;    \
103     pop %r8 ;    \
104     pop %rsi;    \
105     pop %rdi;    \
106     pop %rbp;    \
107     pop %rdx;    \
108     pop %rcx;    \
109     pop %rbx;    \
110     pop %rax;    
111
112
113 #define PRE_LAUNCH(return_target)       \
114     pushf;                              \
115     PUSHA;                              \
116     pushq %rdi;                         \
117     pushq %rdx;                         \
118                                         \
119     movq %rsp, %rax;                    \
120     movq $VMCS_HOST_RSP, %rbx;          \
121     vmwrite %rax, %rbx;                 \
122     jz .Lfail_valid;                    \
123     jc .Lfail_invalid;                  \
124                                         \
125     movq return_target, %rax;           \
126     movq $VMCS_HOST_RIP, %rbx;          \
127     vmwrite %rax, %rbx;                 \
128     jz .Lfail_valid;                    \
129     jc .Lfail_invalid;                  \
130                                         \
131     restore_ctrl_regs(%rdx);            \
132     restore_registers(%rdi);
133
134
135
136
137 .align 8
138 .globl v3_vmx_resume
139 // vm_regs = %rdi, guest_info * = %rsi, ctrl_regs = %rdx
140 v3_vmx_resume:
141
142     PRE_LAUNCH($vmx_resume_ret);
143
144     vmresume
145
146 vmx_resume_ret:
147     jz .Lfail_valid
148     jc .Lfail_invalid
149     jmp .Lnormal_exit
150
151
152 .align 8
153 .globl v3_vmx_launch
154 // vm_regs = %rdi, guest_info * = %rsi, ctrl_regs = %rdx
155 v3_vmx_launch:
156
157     PRE_LAUNCH($vmx_launch_ret);
158
159     vmlaunch
160
161 vmx_launch_ret:
162     jz .Lfail_valid
163     jc .Lfail_invalid
164     jmp .Lnormal_exit
165
166
167
168
169 .Lfail_valid:
170     addq $16, %rsp
171     POPA
172     popf
173     movq $VMX_FAIL_VALID, %rax
174     jmp .Lreturn
175
176 .Lfail_invalid:
177     addq $16, %rsp
178     POPA
179     popf
180     movq $VMX_FAIL_INVALID, %rax
181     jmp .Lreturn
182
183 .Lvmm_failure:
184     addq $24, %rsp
185     movq $VMM_FAILURE, %rax
186     jmp .Lreturn
187
188
189 .Lnormal_exit:
190     save_registers(16(%rsp));
191     save_ctrl_regs(16(%rsp));
192     addq $16, %rsp
193     POPA
194     popf
195     xorq %rax, %rax
196     jmp .Lreturn
197
198
199 .Lreturn:
200     ret
201     
202 #else
203
204 #define save_registers(location)        \
205         pushl   %eax;                   \
206         movl    location, %eax;         \
207         movl    %edi, (%eax);           \
208         movl    %esi, 8(%eax);          \
209         movl    %ebp, 16(%eax);         \
210         movl    $0, 24(%eax);           \
211         movl    %ebx, 32(%eax);         \
212         movl    %edx, 40(%eax);         \
213         movl    %ecx, 48(%eax);         \
214         pushl   %ebx;                   \
215         movl    8(%esp), %ebx;          \
216         movl    %ebx, 56(%eax);         \
217         popl    %ebx;                   \
218         popl    %eax;                   
219         
220
221 #define restore_registers(location) \
222         pushl   %eax;                   \
223         movl    location, %eax;         \
224         movl    (%eax), %edi;           \
225         movl    8(%eax), %esi;          \
226         movl    16(%eax), %ebp;         \
227         movl    32(%eax), %ebx;         \
228         movl    40(%eax), %edx;         \
229         movl    48(%eax), %ecx;         \
230         popl    %eax;
231  
232
233 #define save_ctrl_regs(location)  \
234     push %eax;              \
235     push %ebx;              \
236     movl location, %eax;     \
237     movl %cr2, %ebx;         \
238     movl %ebx, 8(%eax);      \
239     popl %ebx;               \
240     popl %eax
241
242 #define restore_ctrl_regs(location)  \
243     push %eax;              \
244     push %ebx;              \
245     movl location, %eax;     \
246     movl 8(%eax), %ebx;      \
247     movl %ebx, %cr2;         \
248     popl %ebx;               \
249     popl %eax
250
251 #define PUSHA    \
252     push %eax;   \
253     push %ebx;   \
254     push %ecx;   \
255     push %edx;   \
256     push %ebp;   \
257     push %edi;   \
258     push %esi;
259
260 #define POPA     \
261     pop %esi;    \
262     pop %edi;    \
263     pop %ebp;    \
264     pop %edx;    \
265     pop %ecx;    \
266     pop %ebx;    \
267     pop %eax;
268
269
270 #define PRE_LAUNCH(return_target)       \
271     push %ebp;                          \
272     movl %esp, %ebp;                    \
273     pushf;                              \
274     PUSHA;                              \
275                                         \
276     movl 8(%ebp),  %edi;                \
277     movl 12(%ebp), %esi;                \
278     movl 16(%ebp), %edx;                \
279                                         \
280     movl %esp, %eax;                    \
281     movl $VMCS_HOST_RSP, %ebx;          \
282     vmwrite %eax, %ebx;                 \
283     jz .Lfail_valid;                    \
284     jc .Lfail_invalid;                  \
285                                         \
286     movl return_target, %eax;           \
287     movl $VMCS_HOST_RIP, %ebx;          \
288     vmwrite %eax, %ebx;                 \
289     jz .Lfail_valid;                    \
290     jc .Lfail_invalid;                  \
291                                         \
292     restore_ctrl_regs(%edx);            \
293     restore_registers(%edi);
294
295
296 // 32 bit GCC passes arguments via stack
297
298 .align 4
299 .globl v3_vmx_resume
300 v3_vmx_resume:
301
302     PRE_LAUNCH($vmx_resume_ret);
303
304     vmresume
305
306 vmx_resume_ret:
307     jz .Lfail_valid
308     jc .Lfail_invalid
309     jmp .Lnormal_exit
310
311 .align 4
312 .globl v3_vmx_launch
313 // vm_regs = %edi
314 v3_vmx_launch:
315
316     PRE_LAUNCH($vmx_launch_ret);
317
318     vmlaunch
319
320 vmx_launch_ret:
321     jz .Lfail_valid
322     jc .Lfail_invalid
323     jmp .Lnormal_exit
324
325 .Lfail_valid:
326     addl $8, %esp
327     POPA
328     popf
329     movl $VMX_FAIL_VALID, %eax
330     jmp .Lreturn
331
332 .Lfail_invalid:
333     addl $8, %esp
334     POPA
335     popf
336     movl $VMX_FAIL_INVALID, %eax
337     jmp .Lreturn
338
339 .Lvmm_failure:
340     addl $12, %esp
341     movl $VMM_FAILURE, %eax
342     jmp .Lreturn
343
344
345 .Lnormal_exit:
346     save_registers(8(%esp));
347     save_ctrl_regs(8(%esp));
348     addl $8, %esp
349     POPA
350     popf
351     xorl %eax, %eax
352     jmp .Lreturn
353
354 .Lreturn:
355     pop  %ebp;
356     ret
357
358 #endif
359