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.


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