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.


memory lookup refactorization
[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
35
36 #ifdef CONFIG_SHADOW_PAGING_TELEMETRY
37 #include <palacios/vmm_telemetry.h>
38 #endif
39
40 #ifdef CONFIG_SYMBIOTIC_SWAP
41 #include <palacios/vmm_sym_swap.h>
42 #endif
43
44 #ifndef CONFIG_DEBUG_SHADOW_PAGING
45 #undef PrintDebug
46 #define PrintDebug(fmt, args...)
47 #endif
48
49
50
51 static struct hashtable * master_shdw_pg_table = NULL;
52
53 static uint_t shdw_pg_hash_fn(addr_t key) {
54     char * name = (char *)key;
55     return v3_hash_buffer((uint8_t *)name, strlen(name));
56 }
57
58 static int shdw_pg_eq_fn(addr_t key1, addr_t key2) {
59     char * name1 = (char *)key1;
60     char * name2 = (char *)key2;
61
62     return (strcmp(name1, name2) == 0);
63 }
64
65
66 int V3_init_shdw_paging() {
67     extern struct v3_shdw_pg_impl * __start__v3_shdw_pg_impls[];
68     extern struct v3_shdw_pg_impl * __stop__v3_shdw_pg_impls[];
69     struct v3_shdw_pg_impl ** tmp_impl = __start__v3_shdw_pg_impls;
70     int i = 0;
71
72     master_shdw_pg_table = v3_create_htable(0, shdw_pg_hash_fn, shdw_pg_eq_fn);
73
74
75     while (tmp_impl != __stop__v3_shdw_pg_impls) {
76         V3_Print("Registering Shadow Paging Impl (%s)\n", (*tmp_impl)->name);
77
78         if (v3_htable_search(master_shdw_pg_table, (addr_t)((*tmp_impl)->name))) {
79             PrintError("Multiple instances of shadow paging impl (%s)\n", (*tmp_impl)->name);
80             return -1;
81         }
82
83         if (v3_htable_insert(master_shdw_pg_table, 
84                              (addr_t)((*tmp_impl)->name),
85                              (addr_t)(*tmp_impl)) == 0) {
86             PrintError("Could not register shadow paging impl (%s)\n", (*tmp_impl)->name);
87             return -1;
88         }
89
90         tmp_impl = &(__start__v3_shdw_pg_impls[++i]);
91     }
92
93     return 0;
94 }
95
96
97
98 /*** 
99  ***  There be dragons
100  ***/
101
102
103 #ifdef CONFIG_SHADOW_PAGING_TELEMETRY
104 static void telemetry_cb(struct v3_vm_info * vm, void * private_data, char * hdr) {
105     int i = 0;
106     for (i = 0; i < vm->num_cores; i++) {
107         struct guest_info * core = &(vm->cores[i]);
108
109         V3_Print("%s Guest Page faults: %d\n", hdr, core->shdw_pg_state.guest_faults);
110     }
111 }
112 #endif
113
114
115
116 int v3_init_shdw_pg_state(struct guest_info * core) {
117     struct v3_shdw_pg_state * state = &(core->shdw_pg_state);
118     struct v3_shdw_pg_impl * impl = core->vm_info->shdw_impl.current_impl;
119   
120
121     state->guest_cr3 = 0;
122     state->guest_cr0 = 0;
123     state->guest_efer.value = 0x0LL;
124
125
126     if (impl->local_init(core) == -1) {
127         PrintError("Error in Shadow paging local initialization (%s)\n", impl->name);
128         return -1;
129     }
130
131
132 #ifdef CONFIG_SHADOW_PAGING_TELEMETRY
133     v3_add_telemetry_cb(core->vm_info, telemetry_cb, NULL);
134 #endif
135   
136     return 0;
137 }
138
139
140
141 int v3_init_shdw_impl(struct v3_vm_info * vm) {
142     struct v3_shdw_impl_state * impl_state = &(vm->shdw_impl);
143     v3_cfg_tree_t * pg_cfg = v3_cfg_subtree(vm->cfg_data->cfg, "paging");
144     char * impl_name = v3_cfg_val(pg_cfg, "mode");
145     struct v3_shdw_pg_impl * impl = NULL;
146    
147     V3_Print("Initialization of Shadow Paging implementation\n");
148
149     impl = (struct v3_shdw_pg_impl *)v3_htable_search(master_shdw_pg_table, (addr_t)impl_name);
150
151     if (impl == NULL) {
152         PrintError("Could not find shadow paging impl (%s)\n", impl_name);
153         return -1;
154     }
155    
156     impl_state->current_impl = impl;
157
158     if (impl->init(vm, pg_cfg) == -1) {
159         PrintError("Could not initialize Shadow paging implemenation (%s)\n", impl->name);
160         return -1;
161     }
162
163     
164
165
166     return 0;
167 }
168
169
170 // Reads the guest CR3 register
171 // creates new shadow page tables
172 // updates the shadow CR3 register to point to the new pts
173 int v3_activate_shadow_pt(struct guest_info * core) {
174     struct v3_shdw_impl_state * state = &(core->vm_info->shdw_impl);
175     struct v3_shdw_pg_impl * impl = state->current_impl;
176     return impl->activate_shdw_pt(core);
177 }
178
179
180
181 // This must flush any caches
182 // and reset the cr3 value to the correct value
183 int v3_invalidate_shadow_pts(struct guest_info * core) {
184     struct v3_shdw_impl_state * state = &(core->vm_info->shdw_impl);
185     struct v3_shdw_pg_impl * impl = state->current_impl;
186     return impl->invalidate_shdw_pt(core);
187 }
188
189
190 int v3_handle_shadow_pagefault(struct guest_info * core, addr_t fault_addr, pf_error_t error_code) {
191   
192     if (v3_get_vm_mem_mode(core) == PHYSICAL_MEM) {
193         // If paging is not turned on we need to handle the special cases
194         return v3_handle_passthrough_pagefault(core, fault_addr, error_code);
195     } else if (v3_get_vm_mem_mode(core) == VIRTUAL_MEM) {
196         struct v3_shdw_impl_state * state = &(core->vm_info->shdw_impl);
197         struct v3_shdw_pg_impl * impl = state->current_impl;
198
199         return impl->handle_pagefault(core, fault_addr, error_code);
200     } else {
201         PrintError("Invalid Memory mode\n");
202         return -1;
203     }
204 }
205
206
207 int v3_handle_shadow_invlpg(struct guest_info * core) {
208     uchar_t instr[15];
209     struct x86_instr dec_instr;
210     int ret = 0;
211     addr_t vaddr = 0;
212
213     if (v3_get_vm_mem_mode(core) != VIRTUAL_MEM) {
214         // Paging must be turned on...
215         // should handle with some sort of fault I think
216         PrintError("ERROR: INVLPG called in non paged mode\n");
217         return -1;
218     }
219
220     if (v3_get_vm_mem_mode(core) == PHYSICAL_MEM) { 
221         ret = v3_read_gpa_memory(core, get_addr_linear(core, core->rip, &(core->segments.cs)), 15, instr);
222     } else { 
223         ret = v3_read_gva_memory(core, get_addr_linear(core, core->rip, &(core->segments.cs)), 15, instr);
224     }
225
226     if (ret == -1) {
227         PrintError("Could not read instruction into buffer\n");
228         return -1;
229     }
230
231     if (v3_decode(core, (addr_t)instr, &dec_instr) == -1) {
232         PrintError("Decoding Error\n");
233         return -1;
234     }
235   
236     if ((dec_instr.op_type != V3_OP_INVLPG) || 
237         (dec_instr.num_operands != 1) ||
238         (dec_instr.dst_operand.type != MEM_OPERAND)) {
239         PrintError("Decoder Error: Not a valid INVLPG instruction...\n");
240         return -1;
241     }
242
243     vaddr = dec_instr.dst_operand.operand;
244
245     core->rip += dec_instr.instr_length;
246
247     {
248         struct v3_shdw_impl_state * state = &(core->vm_info->shdw_impl);
249         struct v3_shdw_pg_impl * impl = state->current_impl;
250
251         return impl->handle_invlpg(core, vaddr);
252     }
253 }
254
255
256
257
258
259
260 int v3_inject_guest_pf(struct guest_info * core, addr_t fault_addr, pf_error_t error_code) {
261     core->ctrl_regs.cr2 = fault_addr;
262
263 #ifdef CONFIG_SHADOW_PAGING_TELEMETRY
264     core->shdw_pg_state.guest_faults++;
265 #endif
266
267     return v3_raise_exception_with_error(core, PF_EXCEPTION, *(uint_t *)&error_code);
268 }
269
270
271 int v3_is_guest_pf(pt_access_status_t guest_access, pt_access_status_t shadow_access) {
272     /* basically the reasoning is that there can be multiple reasons for a page fault:
273        If there is a permissions failure for a page present in the guest _BUT_
274        the reason for the fault was that the page is not present in the shadow,
275        _THEN_ we have to map the shadow page in and reexecute, this will generate
276        a permissions fault which is _THEN_ valid to send to the guest
277        _UNLESS_ both the guest and shadow have marked the page as not present
278
279        whew...
280     */
281     if (guest_access != PT_ACCESS_OK) {
282         // Guest Access Error
283
284         if ((shadow_access != PT_ACCESS_NOT_PRESENT) &&
285             (guest_access != PT_ACCESS_NOT_PRESENT)) {
286             // aka (guest permission error)
287             return 1;
288         }
289
290         /*
291           if ((shadow_access == PT_ACCESS_NOT_PRESENT) &&
292           (guest_access == PT_ACCESS_NOT_PRESENT)) {
293           // Page tables completely blank, handle guest first
294           return 1;
295           }
296         */
297
298         if (guest_access == PT_ACCESS_NOT_PRESENT) {
299             // Page tables completely blank, handle guest first
300             return 1;
301         }
302         
303         // Otherwise we'll handle the guest fault later...?
304     }
305
306     return 0;
307 }
308
309