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.


Update on VNET
[palacios.git] / palacios / src / palacios / vmx_handler.c
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 #include <palacios/vmx_handler.h>
21 #include <palacios/vmm_types.h>
22 #include <palacios/vmm.h>
23 #include <palacios/vmcs.h>
24 #include <palacios/vmx_lowlevel.h>
25 #include <palacios/vmx_io.h>
26 #include <palacios/vmm_cpuid.h>
27
28 #include <palacios/vmx.h>
29 #include <palacios/vmm_ctrl_regs.h>
30 #include <palacios/vmm_lowlevel.h>
31 #include <palacios/vmx_ctrl_regs.h>
32 #include <palacios/vmx_assist.h>
33 #include <palacios/vmm_halt.h>
34
35 #ifdef CONFIG_TELEMETRY
36 #include <palacios/vmm_telemetry.h>
37 #endif
38
39
40 #ifdef CONFIG_VNET
41 #include <palacios/vmm_vnet.h>
42 #endif
43
44 /* At this point the GPRs are already copied into the guest_info state */
45 int v3_handle_vmx_exit(struct guest_info * info, struct vmx_exit_info * exit_info) {
46     struct vmx_data * vmx_info = (struct vmx_data *)(info->vmm_data);
47
48     /*
49       PrintError("Handling VMEXIT: %s (%u), %lu (0x%lx)\n", 
50       v3_vmx_exit_code_to_str(exit_info->exit_reason),
51       exit_info->exit_reason, 
52       exit_info->exit_qual, exit_info->exit_qual);
53       
54       v3_print_vmcs();
55     */
56
57 #ifdef CONFIG_TELEMETRY
58     if (info->vm_info->enable_telemetry) {
59         v3_telemetry_start_exit(info);
60     }
61 #endif
62
63     switch (exit_info->exit_reason) {
64         case VMEXIT_INFO_EXCEPTION_OR_NMI: {
65             pf_error_t error_code = *(pf_error_t *)&(exit_info->int_err);
66
67
68             // JRL: Change "0x0e" to a macro value
69             if ((uint8_t)exit_info->int_info == 0x0e) {
70 #ifdef CONFIG_DEBUG_SHADOW_PAGING
71                 PrintDebug("Page Fault at %p error_code=%x\n", (void *)exit_info->exit_qual, *(uint32_t *)&error_code);
72 #endif
73
74                 if (info->shdw_pg_mode == SHADOW_PAGING) {
75                     if (v3_handle_shadow_pagefault(info, (addr_t)exit_info->exit_qual, error_code) == -1) {
76                         PrintError("Error handling shadow page fault\n");
77                         return -1;
78                     }
79                 } else {
80                     PrintError("Page fault in unimplemented paging mode\n");
81                     return -1;
82                 }
83             } else {
84                 PrintError("Unknown exception: 0x%x\n", (uint8_t)exit_info->int_info);
85                 v3_print_GPRs(info);
86                 return -1;
87             }
88             break;
89         }
90
91         case VMEXIT_INVLPG:
92             if (info->shdw_pg_mode == SHADOW_PAGING) {
93                 if (v3_handle_shadow_invlpg(info) == -1) {
94                     PrintError("Error handling INVLPG\n");
95                     return -1;
96                 }
97             }
98
99             break;
100         case VMEXIT_CPUID:
101             if (v3_handle_cpuid(info) == -1) {
102                 PrintError("Error Handling CPUID instruction\n");
103                 return -1;
104             }
105
106             break;
107         case VMEXIT_RDMSR: 
108             if (v3_handle_msr_read(info) == -1) {
109                 PrintError("Error handling MSR Read\n");
110                 return -1;
111             }
112
113             break;
114         case VMEXIT_WRMSR:
115             if (v3_handle_msr_write(info) == -1) {
116                 PrintError("Error handling MSR Write\n");
117                 return -1;
118             }
119
120             break;
121         case VMEXIT_VMCALL:
122             /* 
123              * Hypercall 
124              */
125
126             // VMCALL is a 3 byte op
127             // We do this early because some hypercalls can change the rip...
128             info->rip += 3;         
129
130             if (v3_handle_hypercall(info) == -1) {
131                 return -1;
132             }
133             break;
134         case VMEXIT_IO_INSTR: {
135             struct vmx_exit_io_qual * io_qual = (struct vmx_exit_io_qual *)&(exit_info->exit_qual);
136
137             if (io_qual->dir == 0) {
138                 if (io_qual->string) {
139                     if (v3_handle_vmx_io_outs(info, exit_info) == -1) {
140                         PrintError("Error in outs IO handler\n");
141                         return -1;
142                     }
143                 } else {
144                     if (v3_handle_vmx_io_out(info, exit_info) == -1) {
145                         PrintError("Error in out IO handler\n");
146                         return -1;
147                     }
148                 }
149             } else {
150                 if (io_qual->string) {
151                     if(v3_handle_vmx_io_ins(info, exit_info) == -1) {
152                         PrintError("Error in ins IO handler\n");
153                         return -1;
154                     }
155                 } else {
156                     if (v3_handle_vmx_io_in(info, exit_info) == -1) {
157                         PrintError("Error in in IO handler\n");
158                         return -1;
159                     }
160                 }
161             }
162             break;
163         }
164         case VMEXIT_CR_REG_ACCESSES: {
165             struct vmx_exit_cr_qual * cr_qual = (struct vmx_exit_cr_qual *)&(exit_info->exit_qual);
166             
167             // PrintDebug("Control register: %d\n", cr_qual->access_type);
168             switch(cr_qual->cr_id) {
169                 case 0:
170                     //PrintDebug("Handling CR0 Access\n");
171                     if (v3_vmx_handle_cr0_access(info, cr_qual, exit_info) == -1) {
172                         PrintError("Error in CR0 access handler\n");
173                         return -1;
174                     }
175                     break;
176                 case 3:
177                     //PrintDebug("Handling CR3 Access\n");
178                     if (v3_vmx_handle_cr3_access(info, cr_qual) == -1) {
179                         PrintError("Error in CR3 access handler\n");
180                         return -1;
181                     }
182                     break;
183                 default:
184                     PrintError("Unhandled CR access: %d\n", cr_qual->cr_id);
185                     return -1;
186             }
187             
188             info->rip += exit_info->instr_len;
189
190             break;
191         }
192         case VMEXIT_HLT:
193             PrintDebug("Guest halted\n");
194
195             if (v3_handle_halt(info) == -1) {
196                 PrintError("Error handling halt instruction\n");
197                 return -1;
198             }
199
200             break;
201         case VMEXIT_PAUSE:
202             // Handled as NOP
203             info->rip += 2;
204
205             break;
206         case VMEXIT_EXTERNAL_INTR:
207             // Interrupts are handled outside switch
208             break;
209         case VMEXIT_INTR_WINDOW:
210
211             vmcs_read(VMCS_PROC_CTRLS, &(vmx_info->pri_proc_ctrls.value));
212             vmx_info->pri_proc_ctrls.int_wndw_exit = 0;
213             vmcs_write(VMCS_PROC_CTRLS, vmx_info->pri_proc_ctrls.value);
214
215 #ifdef CONFIG_DEBUG_INTERRUPTS
216             PrintDebug("Interrupts available again! (RIP=%llx)\n", info->rip);
217 #endif
218
219             break;
220         default:
221             PrintError("Unhandled VMEXIT: %s (%u), %lu (0x%lx)\n", 
222                        v3_vmx_exit_code_to_str(exit_info->exit_reason),
223                        exit_info->exit_reason, 
224                        exit_info->exit_qual, exit_info->exit_qual);
225             return -1;
226     }
227
228
229 #ifdef CONFIG_VNET
230     v3_vnet_heartbeat(info);
231 #endif
232
233
234 #ifdef CONFIG_TELEMETRY
235     if (info->vm_info->enable_telemetry) {
236         v3_telemetry_end_exit(info, exit_info->exit_reason);
237     }
238 #endif
239
240
241     return 0;
242 }
243
244 static const char VMEXIT_INFO_EXCEPTION_OR_NMI_STR[] = "VMEXIT_INFO_EXCEPTION_OR_NMI";
245 static const char VMEXIT_EXTERNAL_INTR_STR[] = "VMEXIT_EXTERNAL_INTR";
246 static const char VMEXIT_TRIPLE_FAULT_STR[] = "VMEXIT_TRIPLE_FAULT";
247 static const char VMEXIT_INIT_SIGNAL_STR[] = "VMEXIT_INIT_SIGNAL";
248 static const char VMEXIT_STARTUP_IPI_STR[] = "VMEXIT_STARTUP_IPI";
249 static const char VMEXIT_IO_SMI_STR[] = "VMEXIT_IO_SMI";
250 static const char VMEXIT_OTHER_SMI_STR[] = "VMEXIT_OTHER_SMI";
251 static const char VMEXIT_INTR_WINDOW_STR[] = "VMEXIT_INTR_WINDOW";
252 static const char VMEXIT_NMI_WINDOW_STR[] = "VMEXIT_NMI_WINDOW";
253 static const char VMEXIT_TASK_SWITCH_STR[] = "VMEXIT_TASK_SWITCH";
254 static const char VMEXIT_CPUID_STR[] = "VMEXIT_CPUID";
255 static const char VMEXIT_HLT_STR[] = "VMEXIT_HLT";
256 static const char VMEXIT_INVD_STR[] = "VMEXIT_INVD";
257 static const char VMEXIT_INVLPG_STR[] = "VMEXIT_INVLPG";
258 static const char VMEXIT_RDPMC_STR[] = "VMEXIT_RDPMC";
259 static const char VMEXIT_RDTSC_STR[] = "VMEXIT_RDTSC";
260 static const char VMEXIT_RSM_STR[] = "VMEXIT_RSM";
261 static const char VMEXIT_VMCALL_STR[] = "VMEXIT_VMCALL";
262 static const char VMEXIT_VMCLEAR_STR[] = "VMEXIT_VMCLEAR";
263 static const char VMEXIT_VMLAUNCH_STR[] = "VMEXIT_VMLAUNCH";
264 static const char VMEXIT_VMPTRLD_STR[] = "VMEXIT_VMPTRLD";
265 static const char VMEXIT_VMPTRST_STR[] = "VMEXIT_VMPTRST";
266 static const char VMEXIT_VMREAD_STR[] = "VMEXIT_VMREAD";
267 static const char VMEXIT_VMRESUME_STR[] = "VMEXIT_VMRESUME";
268 static const char VMEXIT_VMWRITE_STR[] = "VMEXIT_VMWRITE";
269 static const char VMEXIT_VMXOFF_STR[] = "VMEXIT_VMXOFF";
270 static const char VMEXIT_VMXON_STR[] = "VMEXIT_VMXON";
271 static const char VMEXIT_CR_REG_ACCESSES_STR[] = "VMEXIT_CR_REG_ACCESSES";
272 static const char VMEXIT_MOV_DR_STR[] = "VMEXIT_MOV_DR";
273 static const char VMEXIT_IO_INSTR_STR[] = "VMEXIT_IO_INSTR";
274 static const char VMEXIT_RDMSR_STR[] = "VMEXIT_RDMSR";
275 static const char VMEXIT_WRMSR_STR[] = "VMEXIT_WRMSR";
276 static const char VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE_STR[] = "VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE";
277 static const char VMEXIT_ENTRY_FAIL_MSR_LOAD_STR[] = "VMEXIT_ENTRY_FAIL_MSR_LOAD";
278 static const char VMEXIT_MWAIT_STR[] = "VMEXIT_MWAIT";
279 static const char VMEXIT_MONITOR_STR[] = "VMEXIT_MONITOR";
280 static const char VMEXIT_PAUSE_STR[] = "VMEXIT_PAUSE";
281 static const char VMEXIT_ENTRY_FAILURE_MACHINE_CHECK_STR[] = "VMEXIT_ENTRY_FAILURE_MACHINE_CHECK";
282 static const char VMEXIT_TPR_BELOW_THRESHOLD_STR[] = "VMEXIT_TPR_BELOW_THRESHOLD";
283 static const char VMEXIT_APIC_STR[] = "VMEXIT_APIC";
284 static const char VMEXIT_GDTR_IDTR_STR[] = "VMEXIT_GDTR_IDTR";
285 static const char VMEXIT_LDTR_TR_STR[] = "VMEXIT_LDTR_TR";
286 static const char VMEXIT_EPT_VIOLATION_STR[] = "VMEXIT_EPT_VIOLATION";
287 static const char VMEXIT_EPT_CONFIG_STR[] = "VMEXIT_EPT_CONFIG";
288 static const char VMEXIT_INVEPT_STR[] = "VMEXIT_INVEPT";
289 static const char VMEXIT_RDTSCP_STR[] = "VMEXIT_RDTSCP";
290 static const char VMEXIT_EXPIRED_PREEMPT_TIMER_STR[] = "VMEXIT_EXPIRED_PREEMPT_TIMER";
291 static const char VMEXIT_INVVPID_STR[] = "VMEXIT_INVVPID";
292 static const char VMEXIT_WBINVD_STR[] = "VMEXIT_WBINVD";
293 static const char VMEXIT_XSETBV_STR[] = "VMEXIT_XSETBV";
294
295 const char * v3_vmx_exit_code_to_str(vmx_exit_t exit)
296 {
297     switch(exit) {
298         case VMEXIT_INFO_EXCEPTION_OR_NMI:
299             return VMEXIT_INFO_EXCEPTION_OR_NMI_STR;
300         case VMEXIT_EXTERNAL_INTR:
301             return VMEXIT_EXTERNAL_INTR_STR;
302         case VMEXIT_TRIPLE_FAULT:
303             return VMEXIT_TRIPLE_FAULT_STR;
304         case VMEXIT_INIT_SIGNAL:
305             return VMEXIT_INIT_SIGNAL_STR;
306         case VMEXIT_STARTUP_IPI:
307             return VMEXIT_STARTUP_IPI_STR;
308         case VMEXIT_IO_SMI:
309             return VMEXIT_IO_SMI_STR;
310         case VMEXIT_OTHER_SMI:
311             return VMEXIT_OTHER_SMI_STR;
312         case VMEXIT_INTR_WINDOW:
313             return VMEXIT_INTR_WINDOW_STR;
314         case VMEXIT_NMI_WINDOW:
315             return VMEXIT_NMI_WINDOW_STR;
316         case VMEXIT_TASK_SWITCH:
317             return VMEXIT_TASK_SWITCH_STR;
318         case VMEXIT_CPUID:
319             return VMEXIT_CPUID_STR;
320         case VMEXIT_HLT:
321             return VMEXIT_HLT_STR;
322         case VMEXIT_INVD:
323             return VMEXIT_INVD_STR;
324         case VMEXIT_INVLPG:
325             return VMEXIT_INVLPG_STR;
326         case VMEXIT_RDPMC:
327             return VMEXIT_RDPMC_STR;
328         case VMEXIT_RDTSC:
329             return VMEXIT_RDTSC_STR;
330         case VMEXIT_RSM:
331             return VMEXIT_RSM_STR;
332         case VMEXIT_VMCALL:
333             return VMEXIT_VMCALL_STR;
334         case VMEXIT_VMCLEAR:
335             return VMEXIT_VMCLEAR_STR;
336         case VMEXIT_VMLAUNCH:
337             return VMEXIT_VMLAUNCH_STR;
338         case VMEXIT_VMPTRLD:
339             return VMEXIT_VMPTRLD_STR;
340         case VMEXIT_VMPTRST:
341             return VMEXIT_VMPTRST_STR;
342         case VMEXIT_VMREAD:
343             return VMEXIT_VMREAD_STR;
344         case VMEXIT_VMRESUME:
345             return VMEXIT_VMRESUME_STR;
346         case VMEXIT_VMWRITE:
347             return VMEXIT_VMWRITE_STR;
348         case VMEXIT_VMXOFF:
349             return VMEXIT_VMXOFF_STR;
350         case VMEXIT_VMXON:
351             return VMEXIT_VMXON_STR;
352         case VMEXIT_CR_REG_ACCESSES:
353             return VMEXIT_CR_REG_ACCESSES_STR;
354         case VMEXIT_MOV_DR:
355             return VMEXIT_MOV_DR_STR;
356         case VMEXIT_IO_INSTR:
357             return VMEXIT_IO_INSTR_STR;
358         case VMEXIT_RDMSR:
359             return VMEXIT_RDMSR_STR;
360         case VMEXIT_WRMSR:
361             return VMEXIT_WRMSR_STR;
362         case VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE:
363             return VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE_STR;
364         case VMEXIT_ENTRY_FAIL_MSR_LOAD:
365             return VMEXIT_ENTRY_FAIL_MSR_LOAD_STR;
366         case VMEXIT_MWAIT:
367             return VMEXIT_MWAIT_STR;
368         case VMEXIT_MONITOR:
369             return VMEXIT_MONITOR_STR;
370         case VMEXIT_PAUSE:
371             return VMEXIT_PAUSE_STR;
372         case VMEXIT_ENTRY_FAILURE_MACHINE_CHECK:
373             return VMEXIT_ENTRY_FAILURE_MACHINE_CHECK_STR;
374         case VMEXIT_TPR_BELOW_THRESHOLD:
375             return VMEXIT_TPR_BELOW_THRESHOLD_STR;
376         case VMEXIT_APIC:
377             return VMEXIT_APIC_STR;
378         case VMEXIT_GDTR_IDTR:
379             return VMEXIT_GDTR_IDTR_STR;
380         case VMEXIT_LDTR_TR:
381             return VMEXIT_LDTR_TR_STR;
382         case VMEXIT_EPT_VIOLATION:
383             return VMEXIT_EPT_VIOLATION_STR;
384         case VMEXIT_EPT_CONFIG:
385             return VMEXIT_EPT_CONFIG_STR;
386         case VMEXIT_INVEPT:
387             return VMEXIT_INVEPT_STR;
388         case VMEXIT_RDTSCP:
389             return VMEXIT_RDTSCP_STR;
390         case VMEXIT_EXPIRED_PREEMPT_TIMER:
391             return VMEXIT_EXPIRED_PREEMPT_TIMER_STR;
392         case VMEXIT_INVVPID:
393             return VMEXIT_INVVPID_STR;
394         case VMEXIT_WBINVD:
395             return VMEXIT_WBINVD_STR;
396         case VMEXIT_XSETBV:
397             return VMEXIT_XSETBV_STR;
398     }
399     return NULL;
400 }
401