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.


First attempt at speculative paging around the address that faulted
[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
34 // set this to 1 if you want us to attempt to
35 // fetch multiple entries on a page fault
36 #define SPECULATIVE_PAGING 1
37
38 #define REGULAR_PAGE_FAULT 0
39 #define SPECULATIVE_PAGE_FAULT 1
40
41 #ifndef DEBUG_SHADOW_PAGING
42 #undef PrintDebug
43 #define PrintDebug(fmt, args...)
44 #endif
45
46
47 /*** 
48  ***  There be dragons
49  ***/
50
51
52 struct shadow_page_data {
53     v3_reg_t cr3;
54     addr_t page_pa;
55   
56     struct list_head page_list_node;
57 };
58
59
60 DEFINE_HASHTABLE_INSERT(add_pte_map, addr_t, addr_t);
61 DEFINE_HASHTABLE_SEARCH(find_pte_map, addr_t, addr_t);
62 //DEFINE_HASHTABLE_REMOVE(del_pte_map, addr_t, addr_t, 0);
63
64
65
66 static uint_t pte_hash_fn(addr_t key) {
67     return hash_long(key, 32);
68 }
69
70 static int pte_equals(addr_t key1, addr_t key2) {
71     return (key1 == key2);
72 }
73
74 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info);
75 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code);
76 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access);
77
78
79 #include "vmm_shadow_paging_32.h"
80 #include "vmm_shadow_paging_32pae.h"
81 #include "vmm_shadow_paging_64.h"
82
83
84
85 int v3_init_shadow_page_state(struct guest_info * info) {
86     struct shadow_page_state * state = &(info->shdw_pg_state);
87   
88     state->guest_cr3 = 0;
89     state->guest_cr0 = 0;
90
91     INIT_LIST_HEAD(&(state->page_list));
92
93     state->cached_ptes = NULL;
94     state->cached_cr3 = 0;
95   
96     return 0;
97 }
98
99
100
101 // Reads the guest CR3 register
102 // creates new shadow page tables
103 // updates the shadow CR3 register to point to the new pts
104 int v3_activate_shadow_pt(struct guest_info * info) {
105     switch (v3_get_cpu_mode(info)) {
106
107         case PROTECTED:
108             return activate_shadow_pt_32(info);
109         case PROTECTED_PAE:
110             return activate_shadow_pt_32pae(info);
111         case LONG:
112         case LONG_32_COMPAT:
113         case LONG_16_COMPAT:
114             return activate_shadow_pt_64(info);
115         default:
116             PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
117             return -1;
118     }
119
120     return 0;
121 }
122
123
124 int v3_activate_passthrough_pt(struct guest_info * info) {
125     // For now... But we need to change this....
126     // As soon as shadow paging becomes active the passthrough tables are hosed
127     // So this will cause chaos if it is called at that time
128
129     info->ctrl_regs.cr3 = *(addr_t*)&(info->direct_map_pt);
130     //PrintError("Activate Passthrough Page tables not implemented\n");
131     return 0;
132 }
133
134
135
136 int v3_handle_shadow_pagefault(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
137   int rc;
138   
139     if (v3_get_mem_mode(info) == PHYSICAL_MEM) {
140         // If paging is not turned on we need to handle the special cases
141         return v3_handle_passthrough_pagefault(info, fault_addr, error_code);
142     } else if (v3_get_mem_mode(info) == VIRTUAL_MEM) {
143
144         switch (v3_get_cpu_mode(info)) {
145             case PROTECTED:
146               return handle_shadow_pagefault_32(info, fault_addr, error_code);
147                 break;
148             case PROTECTED_PAE:
149                 return handle_shadow_pagefault_32pae(info, fault_addr, error_code);
150             case LONG:
151             case LONG_32_COMPAT:
152         case LONG_16_COMPAT: {
153               addr_t curr_addr;
154               addr_t fault_addr_base;
155               // first, we will handle the actual fault, non-speculatively
156               rc=handle_shadow_pagefault_64(info, fault_addr, error_code, REGULAR_PAGE_FAULT);
157               if (rc) {
158                 return -1;
159               } 
160               if (!SPECULATIVE_PAGING) { 
161                 return 0;
162               }
163               fault_addr_base=PAGE_ADDR_4KB(fault_addr);
164               PrintDebug("Attempting speculative paging around %p\n",(void*)fault_addr_base);
165               for (curr_addr = (fault_addr_base & (~0x1fffffLL)) ; 
166                    curr_addr < (fault_addr_base | (0x1fffffLL)) ;
167                    curr_addr+=PAGE_SIZE) {
168                 if (curr_addr!=fault_addr_base) { 
169                   rc=handle_shadow_pagefault_64(info, curr_addr, error_code, SPECULATIVE_PAGE_FAULT);
170                   if (rc) {
171                     PrintDebug("Speculative page fault handler failed at %p\n",(void*)curr_addr);
172                     return -1;
173                   } 
174                 }
175               }
176               return 0;
177             }
178                 break;
179             default:
180                 PrintError("Unhandled CPU Mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
181                 return -1;
182         }
183     } else {
184         PrintError("Invalid Memory mode\n");
185         return -1;
186     }
187 }
188
189
190 int v3_handle_shadow_invlpg(struct guest_info * info) {
191     uchar_t instr[15];
192     struct x86_instr dec_instr;
193     int ret = 0;
194     addr_t vaddr = 0;
195
196     if (v3_get_mem_mode(info) != VIRTUAL_MEM) {
197         // Paging must be turned on...
198         // should handle with some sort of fault I think
199         PrintError("ERROR: INVLPG called in non paged mode\n");
200         return -1;
201     }
202
203     if (v3_get_mem_mode(info) == PHYSICAL_MEM) { 
204         ret = read_guest_pa_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
205     } else { 
206         ret = read_guest_va_memory(info, get_addr_linear(info, info->rip, &(info->segments.cs)), 15, instr);
207     }
208
209     if (ret == -1) {
210         PrintError("Could not read instruction into buffer\n");
211         return -1;
212     }
213
214     if (v3_decode(info, (addr_t)instr, &dec_instr) == -1) {
215         PrintError("Decoding Error\n");
216         return -1;
217     }
218   
219     if ((dec_instr.op_type != V3_OP_INVLPG) || 
220         (dec_instr.num_operands != 1) ||
221         (dec_instr.dst_operand.type != MEM_OPERAND)) {
222         PrintError("Decoder Error: Not a valid INVLPG instruction...\n");
223         return -1;
224     }
225
226     vaddr = dec_instr.dst_operand.operand;
227
228     info->rip += dec_instr.instr_length;
229
230     switch (v3_get_cpu_mode(info)) {
231         case PROTECTED:
232             return handle_shadow_invlpg_32(info, vaddr);
233         case PROTECTED_PAE:
234             return handle_shadow_invlpg_32pae(info, vaddr);
235         case LONG:
236         case LONG_32_COMPAT:
237         case LONG_16_COMPAT:
238             return handle_shadow_invlpg_64(info, vaddr);
239         default:
240             PrintError("Invalid CPU mode: %s\n", v3_cpu_mode_to_str(v3_get_cpu_mode(info)));
241             return -1;
242     }
243 }
244
245
246
247
248 static struct shadow_page_data * create_new_shadow_pt(struct guest_info * info) {
249     struct shadow_page_state * state = &(info->shdw_pg_state);
250     v3_reg_t cur_cr3 = info->ctrl_regs.cr3;
251     struct shadow_page_data * page_tail = NULL;
252     addr_t shdw_page = 0;
253
254     if (!list_empty(&(state->page_list))) {
255         page_tail = list_tail_entry(&(state->page_list), struct shadow_page_data, page_list_node);
256     
257         if (page_tail->cr3 != cur_cr3) {
258             PrintDebug("Reusing old shadow Page: %p (cur_CR3=%p)(page_cr3=%p) \n",
259                        (void *)(addr_t)page_tail->page_pa, 
260                        (void *)(addr_t)cur_cr3, 
261                        (void *)(addr_t)(page_tail->cr3));
262
263             list_move(&(page_tail->page_list_node), &(state->page_list));
264
265             memset(V3_VAddr((void *)(page_tail->page_pa)), 0, PAGE_SIZE_4KB);
266
267
268             return page_tail;
269         }
270     }
271
272     // else  
273
274     page_tail = (struct shadow_page_data *)V3_Malloc(sizeof(struct shadow_page_data));
275     page_tail->page_pa = (addr_t)V3_AllocPages(1);
276
277     PrintDebug("Allocating new shadow Page: %p (cur_cr3=%p)\n", 
278                (void *)(addr_t)page_tail->page_pa, 
279                (void *)(addr_t)cur_cr3);
280
281     page_tail->cr3 = cur_cr3;
282     list_add(&(page_tail->page_list_node), &(state->page_list));
283
284     shdw_page = (addr_t)V3_VAddr((void *)(page_tail->page_pa));
285     memset((void *)shdw_page, 0, PAGE_SIZE_4KB);
286
287     return page_tail;
288 }
289
290
291 static void inject_guest_pf(struct guest_info * info, addr_t fault_addr, pf_error_t error_code) {
292     if (info->enable_profiler) {
293         info->profiler.guest_pf_cnt++;
294     }
295
296     info->ctrl_regs.cr2 = fault_addr;
297     v3_raise_exception_with_error(info, PF_EXCEPTION, *(uint_t *)&error_code);
298 }
299
300
301 static int is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
302     /* basically the reasoning is that there can be multiple reasons for a page fault:
303        If there is a permissions failure for a page present in the guest _BUT_
304        the reason for the fault was that the page is not present in the shadow,
305        _THEN_ we have to map the shadow page in and reexecute, this will generate
306        a permissions fault which is _THEN_ valid to send to the guest
307        _UNLESS_ both the guest and shadow have marked the page as not present
308
309        whew...
310     */
311     if (guest_access != PT_ACCESS_OK) {
312         // Guest Access Error
313
314         if ((shadow_access != PT_ACCESS_NOT_PRESENT) &&
315             (guest_access != PT_ACCESS_NOT_PRESENT)) {
316             // aka (guest permission error)
317             return 1;
318         }
319
320         if ((shadow_access == PT_ACCESS_NOT_PRESENT) &&
321             (guest_access == PT_ACCESS_NOT_PRESENT)) {
322             // Page tables completely blank, handle guest first
323             return 1;
324         }
325
326         // Otherwise we'll handle the guest fault later...?
327     }
328
329     return 0;
330 }
331
332