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.


Added shadow page handling, IO and MSR bitmap allocation, and started
[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
27
28 static int inline check_vmcs_write(vmcs_field_t field, addr_t val)
29 {
30     int ret = 0;
31     ret = vmcs_write(field,val);
32
33     if (ret != VMX_SUCCESS) {
34         PrintError("VMWRITE error on %s!: %d\n", v3_vmcs_field_to_str(field), ret);
35         return 1;
36     }
37
38     return 0;
39 }
40
41 static int inline check_vmcs_read(vmcs_field_t field, void * val)
42 {
43     int ret = 0;
44     ret = vmcs_read(field,val);
45
46     if(ret != VMX_SUCCESS) {
47         PrintError("VMREAD error on %s!: %d\n", v3_vmcs_field_to_str(field), ret);
48         return ret;
49     }
50
51     return 0;
52 }
53
54 int v3_handle_vmx_exit(struct v3_gprs * gprs, struct guest_info * info)
55 {
56     uint32_t exit_reason;
57     ulong_t exit_qual;
58
59     check_vmcs_read(VMCS_EXIT_REASON, &exit_reason);
60     check_vmcs_read(VMCS_EXIT_QUAL, &exit_qual);
61
62     PrintDebug("VMX Exit taken, id-qual: %d-%ld\n", exit_reason, exit_qual);
63
64     /* Update guest state */
65     check_vmcs_read(VMCS_GUEST_RIP, &(info->rip));
66     check_vmcs_read(VMCS_GUEST_RSP, &(info->vm_regs.rsp));
67     check_vmcs_read(VMCS_GUEST_CR0, &(info->ctrl_regs.cr0));
68     check_vmcs_read(VMCS_GUEST_CR3, &(info->ctrl_regs.cr3));
69     check_vmcs_read(VMCS_GUEST_CR4, &(info->ctrl_regs.cr4));
70
71     // read out segments
72
73     switch(exit_reason)
74     {
75         case VMEXIT_INFO_EXCEPTION_OR_NMI:
76         {
77             uint32_t int_info;
78             pf_error_t error_code;
79             check_vmcs_read(VMCS_EXIT_INT_INFO, &int_info);
80             check_vmcs_read(VMCS_EXIT_INT_ERR, &error_code);
81
82             if((uint8_t)int_info == 0x0e) {
83                 PrintDebug("Page Fault at %p\n", (void*)exit_qual);
84                 if(info->shdw_pg_mode == SHADOW_PAGING) {
85                     if(v3_handle_shadow_pagefault(info, (addr_t)exit_qual, error_code) == -1) {
86                         return -1;
87                     }
88                 } else {
89                     PrintError("Page fault in unimplemented paging mode\n");
90                     return -1;
91                 }
92             }
93             break;
94         }
95
96         case VMEXIT_IO_INSTR: 
97         {
98             struct vmcs_io_qual * io_qual = (struct vmcs_io_qual *)&exit_qual;
99
100             if(io_qual->dir == 0) {
101                 if(io_qual->string) {
102                     if(v3_handle_vmx_io_outs(info) == -1) {
103                         return -1;
104                     }
105                 } else {
106                     if(v3_handle_vmx_io_out(info) == -1) {
107                         return -1;
108                     }
109                 }
110             } else {
111                 if(io_qual->string) {
112                     if(v3_handle_vmx_io_ins(info) == -1) {
113                         return -1;
114                     }
115                 } else {
116                     if(v3_handle_vmx_io_in(info) == -1) {
117                         return -1;
118                     }
119                 }
120             }
121             break;
122         }
123
124         default:
125             PrintError("Unhandled VMEXIT\n");
126             return -1;
127     }
128
129     check_vmcs_write(VMCS_GUEST_CR0, info->ctrl_regs.cr0);
130     check_vmcs_write(VMCS_GUEST_CR3, info->ctrl_regs.cr3);
131     check_vmcs_write(VMCS_GUEST_CR4, info->ctrl_regs.cr4);
132     check_vmcs_write(VMCS_GUEST_RIP, info->rip);
133     check_vmcs_write(VMCS_GUEST_RSP, info->vm_regs.rsp);
134
135     PrintDebug("Executing VMRESUME\n");
136     return 0;
137 }