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 #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>
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>
35 #ifndef CONFIG_DEBUG_VMX
37 #define PrintDebug(fmt, args...)
40 #ifdef CONFIG_TELEMETRY
41 #include <palacios/vmm_telemetry.h>
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) {
47 PrintError("Handling VMEXIT: %s (%u), %lu (0x%lx)\n",
48 v3_vmx_exit_code_to_str(exit_info->exit_reason),
49 exit_info->exit_reason,
50 exit_info->exit_qual, exit_info->exit_qual);
55 #ifdef CONFIG_TELEMETRY
56 if (info->vm_info->enable_telemetry) {
57 v3_telemetry_start_exit(info);
61 switch (exit_info->exit_reason) {
62 case VMEXIT_INFO_EXCEPTION_OR_NMI: {
63 pf_error_t error_code = *(pf_error_t *)&(exit_info->int_err);
66 // JRL: Change "0x0e" to a macro value
67 if ((uint8_t)exit_info->int_info == 0x0e) {
68 #ifdef CONFIG_DEBUG_SHADOW_PAGING
69 PrintDebug("Page Fault at %p error_code=%x\n", (void *)exit_info->exit_qual, *(uint32_t *)&error_code);
72 if (info->shdw_pg_mode == SHADOW_PAGING) {
73 if (v3_handle_shadow_pagefault(info, (addr_t)exit_info->exit_qual, error_code) == -1) {
74 PrintError("Error handling shadow page fault\n");
78 PrintError("Page fault in unimplemented paging mode\n");
82 PrintError("Unknown exception: 0x%x\n", (uint8_t)exit_info->int_info);
90 if (info->shdw_pg_mode == SHADOW_PAGING) {
91 if (v3_handle_shadow_invlpg(info) == -1) {
92 PrintError("Error handling INVLPG\n");
100 #ifdef CONFIG_DEBUG_TIME
101 PrintDebug("RDTSC\n");
103 if (v3_handle_rdtsc(info) == -1) {
104 PrintError("Error Handling RDTSC instruction\n");
111 if (v3_handle_cpuid(info) == -1) {
112 PrintError("Error Handling CPUID instruction\n");
118 if (v3_handle_msr_read(info) == -1) {
119 PrintError("Error handling MSR Read\n");
125 if (v3_handle_msr_write(info) == -1) {
126 PrintError("Error handling MSR Write\n");
136 // VMCALL is a 3 byte op
137 // We do this early because some hypercalls can change the rip...
140 if (v3_handle_hypercall(info) == -1) {
144 case VMEXIT_IO_INSTR: {
145 struct vmx_exit_io_qual * io_qual = (struct vmx_exit_io_qual *)&(exit_info->exit_qual);
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");
154 if (v3_handle_vmx_io_out(info, exit_info) == -1) {
155 PrintError("Error in out IO handler\n");
160 if (io_qual->string) {
161 if(v3_handle_vmx_io_ins(info, exit_info) == -1) {
162 PrintError("Error in ins IO handler\n");
166 if (v3_handle_vmx_io_in(info, exit_info) == -1) {
167 PrintError("Error in in IO handler\n");
174 case VMEXIT_CR_REG_ACCESSES: {
175 struct vmx_exit_cr_qual * cr_qual = (struct vmx_exit_cr_qual *)&(exit_info->exit_qual);
177 // PrintDebug("Control register: %d\n", cr_qual->access_type);
178 switch(cr_qual->cr_id) {
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");
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");
194 PrintError("Unhandled CR access: %d\n", cr_qual->cr_id);
198 info->rip += exit_info->instr_len;
203 PrintDebug("Guest halted\n");
205 if (v3_handle_halt(info) == -1) {
206 PrintError("Error handling halt instruction\n");
216 case VMEXIT_EXTERNAL_INTR:
217 // Interrupts are handled outside switch
219 case VMEXIT_INTR_WINDOW:
220 // This is handled in the atomic part of the vmx code,
221 // not in the generic (interruptable) vmx handler
224 PrintError("Unhandled VMEXIT: %s (%u), %lu (0x%lx)\n",
225 v3_vmx_exit_code_to_str(exit_info->exit_reason),
226 exit_info->exit_reason,
227 exit_info->exit_qual, exit_info->exit_qual);
232 #ifdef CONFIG_TELEMETRY
233 if (info->vm_info->enable_telemetry) {
234 v3_telemetry_end_exit(info, exit_info->exit_reason);
242 static const char VMEXIT_INFO_EXCEPTION_OR_NMI_STR[] = "VMEXIT_INFO_EXCEPTION_OR_NMI";
243 static const char VMEXIT_EXTERNAL_INTR_STR[] = "VMEXIT_EXTERNAL_INTR";
244 static const char VMEXIT_TRIPLE_FAULT_STR[] = "VMEXIT_TRIPLE_FAULT";
245 static const char VMEXIT_INIT_SIGNAL_STR[] = "VMEXIT_INIT_SIGNAL";
246 static const char VMEXIT_STARTUP_IPI_STR[] = "VMEXIT_STARTUP_IPI";
247 static const char VMEXIT_IO_SMI_STR[] = "VMEXIT_IO_SMI";
248 static const char VMEXIT_OTHER_SMI_STR[] = "VMEXIT_OTHER_SMI";
249 static const char VMEXIT_INTR_WINDOW_STR[] = "VMEXIT_INTR_WINDOW";
250 static const char VMEXIT_NMI_WINDOW_STR[] = "VMEXIT_NMI_WINDOW";
251 static const char VMEXIT_TASK_SWITCH_STR[] = "VMEXIT_TASK_SWITCH";
252 static const char VMEXIT_CPUID_STR[] = "VMEXIT_CPUID";
253 static const char VMEXIT_HLT_STR[] = "VMEXIT_HLT";
254 static const char VMEXIT_INVD_STR[] = "VMEXIT_INVD";
255 static const char VMEXIT_INVLPG_STR[] = "VMEXIT_INVLPG";
256 static const char VMEXIT_RDPMC_STR[] = "VMEXIT_RDPMC";
257 static const char VMEXIT_RDTSC_STR[] = "VMEXIT_RDTSC";
258 static const char VMEXIT_RSM_STR[] = "VMEXIT_RSM";
259 static const char VMEXIT_VMCALL_STR[] = "VMEXIT_VMCALL";
260 static const char VMEXIT_VMCLEAR_STR[] = "VMEXIT_VMCLEAR";
261 static const char VMEXIT_VMLAUNCH_STR[] = "VMEXIT_VMLAUNCH";
262 static const char VMEXIT_VMPTRLD_STR[] = "VMEXIT_VMPTRLD";
263 static const char VMEXIT_VMPTRST_STR[] = "VMEXIT_VMPTRST";
264 static const char VMEXIT_VMREAD_STR[] = "VMEXIT_VMREAD";
265 static const char VMEXIT_VMRESUME_STR[] = "VMEXIT_VMRESUME";
266 static const char VMEXIT_VMWRITE_STR[] = "VMEXIT_VMWRITE";
267 static const char VMEXIT_VMXOFF_STR[] = "VMEXIT_VMXOFF";
268 static const char VMEXIT_VMXON_STR[] = "VMEXIT_VMXON";
269 static const char VMEXIT_CR_REG_ACCESSES_STR[] = "VMEXIT_CR_REG_ACCESSES";
270 static const char VMEXIT_MOV_DR_STR[] = "VMEXIT_MOV_DR";
271 static const char VMEXIT_IO_INSTR_STR[] = "VMEXIT_IO_INSTR";
272 static const char VMEXIT_RDMSR_STR[] = "VMEXIT_RDMSR";
273 static const char VMEXIT_WRMSR_STR[] = "VMEXIT_WRMSR";
274 static const char VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE_STR[] = "VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE";
275 static const char VMEXIT_ENTRY_FAIL_MSR_LOAD_STR[] = "VMEXIT_ENTRY_FAIL_MSR_LOAD";
276 static const char VMEXIT_MWAIT_STR[] = "VMEXIT_MWAIT";
277 static const char VMEXIT_MONITOR_STR[] = "VMEXIT_MONITOR";
278 static const char VMEXIT_PAUSE_STR[] = "VMEXIT_PAUSE";
279 static const char VMEXIT_ENTRY_FAILURE_MACHINE_CHECK_STR[] = "VMEXIT_ENTRY_FAILURE_MACHINE_CHECK";
280 static const char VMEXIT_TPR_BELOW_THRESHOLD_STR[] = "VMEXIT_TPR_BELOW_THRESHOLD";
281 static const char VMEXIT_APIC_STR[] = "VMEXIT_APIC";
282 static const char VMEXIT_GDTR_IDTR_STR[] = "VMEXIT_GDTR_IDTR";
283 static const char VMEXIT_LDTR_TR_STR[] = "VMEXIT_LDTR_TR";
284 static const char VMEXIT_EPT_VIOLATION_STR[] = "VMEXIT_EPT_VIOLATION";
285 static const char VMEXIT_EPT_CONFIG_STR[] = "VMEXIT_EPT_CONFIG";
286 static const char VMEXIT_INVEPT_STR[] = "VMEXIT_INVEPT";
287 static const char VMEXIT_RDTSCP_STR[] = "VMEXIT_RDTSCP";
288 static const char VMEXIT_EXPIRED_PREEMPT_TIMER_STR[] = "VMEXIT_EXPIRED_PREEMPT_TIMER";
289 static const char VMEXIT_INVVPID_STR[] = "VMEXIT_INVVPID";
290 static const char VMEXIT_WBINVD_STR[] = "VMEXIT_WBINVD";
291 static const char VMEXIT_XSETBV_STR[] = "VMEXIT_XSETBV";
293 const char * v3_vmx_exit_code_to_str(vmx_exit_t exit)
296 case VMEXIT_INFO_EXCEPTION_OR_NMI:
297 return VMEXIT_INFO_EXCEPTION_OR_NMI_STR;
298 case VMEXIT_EXTERNAL_INTR:
299 return VMEXIT_EXTERNAL_INTR_STR;
300 case VMEXIT_TRIPLE_FAULT:
301 return VMEXIT_TRIPLE_FAULT_STR;
302 case VMEXIT_INIT_SIGNAL:
303 return VMEXIT_INIT_SIGNAL_STR;
304 case VMEXIT_STARTUP_IPI:
305 return VMEXIT_STARTUP_IPI_STR;
307 return VMEXIT_IO_SMI_STR;
308 case VMEXIT_OTHER_SMI:
309 return VMEXIT_OTHER_SMI_STR;
310 case VMEXIT_INTR_WINDOW:
311 return VMEXIT_INTR_WINDOW_STR;
312 case VMEXIT_NMI_WINDOW:
313 return VMEXIT_NMI_WINDOW_STR;
314 case VMEXIT_TASK_SWITCH:
315 return VMEXIT_TASK_SWITCH_STR;
317 return VMEXIT_CPUID_STR;
319 return VMEXIT_HLT_STR;
321 return VMEXIT_INVD_STR;
323 return VMEXIT_INVLPG_STR;
325 return VMEXIT_RDPMC_STR;
327 return VMEXIT_RDTSC_STR;
329 return VMEXIT_RSM_STR;
331 return VMEXIT_VMCALL_STR;
333 return VMEXIT_VMCLEAR_STR;
334 case VMEXIT_VMLAUNCH:
335 return VMEXIT_VMLAUNCH_STR;
337 return VMEXIT_VMPTRLD_STR;
339 return VMEXIT_VMPTRST_STR;
341 return VMEXIT_VMREAD_STR;
342 case VMEXIT_VMRESUME:
343 return VMEXIT_VMRESUME_STR;
345 return VMEXIT_VMWRITE_STR;
347 return VMEXIT_VMXOFF_STR;
349 return VMEXIT_VMXON_STR;
350 case VMEXIT_CR_REG_ACCESSES:
351 return VMEXIT_CR_REG_ACCESSES_STR;
353 return VMEXIT_MOV_DR_STR;
354 case VMEXIT_IO_INSTR:
355 return VMEXIT_IO_INSTR_STR;
357 return VMEXIT_RDMSR_STR;
359 return VMEXIT_WRMSR_STR;
360 case VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE:
361 return VMEXIT_ENTRY_FAIL_INVALID_GUEST_STATE_STR;
362 case VMEXIT_ENTRY_FAIL_MSR_LOAD:
363 return VMEXIT_ENTRY_FAIL_MSR_LOAD_STR;
365 return VMEXIT_MWAIT_STR;
367 return VMEXIT_MONITOR_STR;
369 return VMEXIT_PAUSE_STR;
370 case VMEXIT_ENTRY_FAILURE_MACHINE_CHECK:
371 return VMEXIT_ENTRY_FAILURE_MACHINE_CHECK_STR;
372 case VMEXIT_TPR_BELOW_THRESHOLD:
373 return VMEXIT_TPR_BELOW_THRESHOLD_STR;
375 return VMEXIT_APIC_STR;
376 case VMEXIT_GDTR_IDTR:
377 return VMEXIT_GDTR_IDTR_STR;
379 return VMEXIT_LDTR_TR_STR;
380 case VMEXIT_EPT_VIOLATION:
381 return VMEXIT_EPT_VIOLATION_STR;
382 case VMEXIT_EPT_CONFIG:
383 return VMEXIT_EPT_CONFIG_STR;
385 return VMEXIT_INVEPT_STR;
387 return VMEXIT_RDTSCP_STR;
388 case VMEXIT_EXPIRED_PREEMPT_TIMER:
389 return VMEXIT_EXPIRED_PREEMPT_TIMER_STR;
391 return VMEXIT_INVVPID_STR;
393 return VMEXIT_WBINVD_STR;
395 return VMEXIT_XSETBV_STR;