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.


cleaned up the memory handing implementation
[palacios.git] / palacios / src / palacios / vmm_shadow_paging.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
21 #include <palacios/vmm_shadow_paging.h>
22
23
24 #include <palacios/vmm.h>
25 #include <palacios/vm_guest_mem.h>
26 #include <palacios/vmm_decoder.h>
27 #include <palacios/vmm_ctrl_regs.h>
28
29 #include <palacios/vmm_hashtable.h>
30
31 #include <palacios/vmm_direct_paging.h>
32
33 #ifndef DEBUG_SHADOW_PAGING
34 #undef PrintDebug
35 #define PrintDebug(fmt, args...)
36 #endif
37
38
39 /*** 
40  ***  There be dragons
41  ***/
42
43
44 struct shadow_page_data {
45     v3_reg_t cr3;
46     addr_t page_pa;
47   
48     struct list_head page_list_node;
49 };
50
51
52
53 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info);
54 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code);
55 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access);
56
57
58 #include "vmm_shadow_paging_32.h"
59 #include "vmm_shadow_paging_32pae.h"
60 #include "vmm_shadow_paging_64.h"
61
62
63
64 int v3_init_shadow_page_state(struct guest_info * info) {
65     struct shadow_page_state * state = &(info->shdw_pg_state);
66   
67     state->guest_cr3 = 0;
68     state->guest_cr0 = 0;
69
70     INIT_LIST_HEAD(&(state->page_list));
71   
72     return 0;
73 }
74
75
76
77 // Reads the guest CR3 register
78 // creates new shadow page tables
79 // updates the shadow CR3 register to point to the new pts
80 int v3_activate_shadow_pt(struct guest_info * info) {
81     switch (v3_get_cpu_mode(info)) {
82
83         case PROTECTED:
84             return activate_shadow_pt_32(info);
85         case PROTECTED_PAE:
86             return activate_shadow_pt_32pae(info);
87         case LONG:
88         case LONG_32_COMPAT:
89         case LONG_16_COMPAT:
90             return activate_shadow_pt_64(info);
91         default:
92             PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
93             return -1;
94     }
95
96     return 0;
97 }
98
99
100
101
102
103 int v3_handle_shadow_pagefault(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
104   
105     if (v3_get_mem_mode(info) == PHYSICAL_MEM) {
106         // If paging is not turned on we need to handle the special cases
107         return v3_handle_passthrough_pagefault(info, fault_addr, error_code);
108     } else if (v3_get_mem_mode(info) == VIRTUAL_MEM) {
109
110         switch (v3_get_cpu_mode(info)) {
111             case PROTECTED:
112                 return handle_shadow_pagefault_32(info, fault_addr, error_code);
113                 break;
114             case PROTECTED_PAE:
115                 return handle_shadow_pagefault_32pae(info, fault_addr, error_code);
116             case LONG:
117             case LONG_32_COMPAT:
118             case LONG_16_COMPAT:
119                 return handle_shadow_pagefault_64(info, fault_addr, error_code);
120                 break;
121             default:
122                 PrintError("Unhandled CPU Mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
123                 return -1;
124         }
125     } else {
126         PrintError("Invalid Memory mode\n");
127         return -1;
128     }
129 }
130
131
132 int v3_handle_shadow_invlpg(struct guest_info * info) {
133     uchar_t instr[15];
134     struct x86_instr dec_instr;
135     int ret = 0;
136     addr_t vaddr = 0;
137
138     if (v3_get_mem_mode(info) != VIRTUAL_MEM) {
139         // Paging must be turned on...
140         // should handle with some sort of fault I think
141         PrintError("ERROR: INVLPG called in non paged mode\n");
142         return -1;
143     }
144
145     if (v3_get_mem_mode(info) == PHYSICAL_MEM) { 
146         ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
147     } else { 
148         ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
149     }
150
151     if (ret == -1) {
152         PrintError("Could not read instruction into buffer\n");
153         return -1;
154     }
155
156     if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
157         PrintError("Decoding Error\n");
158         return -1;
159     }
160   
161     if ((dec_instr.op_type != V3_OP_INVLPG) || 
162         (dec_instr.num_operands != 1) ||
163         (dec_instr.dst_operand.type != MEM_OPERAND)) {
164         PrintError("Decoder Error: Not a valid INVLPG instruction...\n");
165         return -1;
166     }
167
168     vaddr = dec_instr.dst_operand.operand;
169
170     info->rip += dec_instr.instr_length;
171
172     switch (v3_get_cpu_mode(info)) {
173         case PROTECTED:
174             return handle_shadow_invlpg_32(info, vaddr);
175         case PROTECTED_PAE:
176             return handle_shadow_invlpg_32pae(info, vaddr);
177         case LONG:
178         case LONG_32_COMPAT:
179         case LONG_16_COMPAT:
180             return handle_shadow_invlpg_64(info, vaddr);
181         default:
182             PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
183             return -1;
184     }
185 }
186
187
188
189
190 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info) {
191     struct shadow_page_state * state = &(info->shdw_pg_state);
192     v3_reg_t cur_cr3 = info->ctrl_regs.cr3;
193     struct shadow_page_data * page_tail = NULL;
194     addr_t shdw_page = 0;
195
196     if (!list_empty(&(state->page_list))) {
197         page_tail = list_tail_entry(&(state->page_list), struct shadow_page_data, page_list_node);
198     
199         if (page_tail->cr3 != cur_cr3) {
200             PrintDebug("Reusing old shadow Page: %p (cur_CR3=%p)(page_cr3=%p) \n",
201                        (void *)(addr_t)page_tail->page_pa, 
202                        (void *)(addr_t)cur_cr3, 
203                        (void *)(addr_t)(page_tail->cr3));
204
205             list_move(&(page_tail->page_list_node), &(state->page_list));
206
207             memset(V3_VAddr((void *)(page_tail->page_pa)), 0, PAGE_SIZE_4KB);
208
209
210             return page_tail;
211         }
212     }
213
214     // else  
215
216     page_tail = (struct shadow_page_data *)V3_Malloc(sizeof(struct shadow_page_data));
217     page_tail->page_pa = (addr_t)V3_AllocPages(1);
218
219     PrintDebug("Allocating new shadow Page: %p (cur_cr3=%p)\n", 
220                (void *)(addr_t)page_tail->page_pa, 
221                (void *)(addr_t)cur_cr3);
222
223     page_tail->cr3 = cur_cr3;
224     list_add(&(page_tail->page_list_node), &(state->page_list));
225
226     shdw_page = (addr_t)V3_VAddr((void *)(page_tail->page_pa));
227     memset((void *)shdw_page, 0, PAGE_SIZE_4KB);
228
229     return page_tail;
230 }
231
232
233 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
234     if (info->enable_profiler) {
235         info->profiler.guest_pf_cnt++;
236     }
237
238     info->ctrl_regs.cr2 = fault_addr;
239     v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
240 }
241
242
243 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
244     /* basically the reasoning is that there can be multiple reasons for a page fault:
245        If there is a permissions failure for a page present in the guest _BUT_
246        the reason for the fault was that the page is not present in the shadow,
247        _THEN_ we have to map the shadow page in and reexecute, this will generate
248        a permissions fault which is _THEN_ valid to send to the guest
249        _UNLESS_ both the guest and shadow have marked the page as not present
250
251        whew...
252     */
253     if (guest_access != PT_ACCESS_OK) {
254         // Guest Access Error
255
256         if ((shadow_access != PT_ACCESS_NOT_PRESENT) &&
257             (guest_access != PT_ACCESS_NOT_PRESENT)) {
258             // aka (guest permission error)
259             return 1;
260         }
261
262         if ((shadow_access == PT_ACCESS_NOT_PRESENT) &&
263             (guest_access == PT_ACCESS_NOT_PRESENT)) {
264             // Page tables completely blank, handle guest first
265             return 1;
266         }
267
268         // Otherwise we'll handle the guest fault later...?
269     }
270
271     return 0;
272 }
273
274