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/vmm_mem.h>
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_util.h>
23 #include <palacios/vmm_emulator.h>
25 #include <palacios/vmm_shadow_paging.h>
26 #include <palacios/vmm_direct_paging.h>
29 #define MEM_OFFSET_HCALL 0x1000
33 struct v3_shadow_region * insert_shadow_region(struct guest_info * info,
34 struct v3_shadow_region * region);
37 static int mem_offset_hypercall(struct guest_info * info, uint_t hcall_id, void * private_data) {
38 info->vm_regs.rbx = info->mem_map.base_region.host_addr;
44 void v3_init_shadow_map(struct guest_info * info) {
45 v3_shdw_map_t * map = &(info->mem_map);
46 addr_t mem_pages = info->mem_size >> 12;
48 map->shdw_regions.rb_node = NULL;
49 map->hook_hva = (addr_t)V3_VAddr(V3_AllocPages(1));
51 // There is an underlying region that contains all of the guest memory
52 // PrintDebug("Mapping %d pages of memory (%u bytes)\n", (int)mem_pages, (uint_t)info->mem_size);
54 map->base_region.guest_start = 0;
55 map->base_region.guest_end = mem_pages * PAGE_SIZE_4KB;
56 map->base_region.host_type = SHDW_REGION_ALLOCATED;
57 map->base_region.host_addr = (addr_t)V3_AllocPages(mem_pages);
59 //memset(V3_VAddr((void *)map->base_region.host_addr), 0xffffffff, map->base_region.guest_end);
61 v3_register_hypercall(info, MEM_OFFSET_HCALL, mem_offset_hypercall, NULL);
64 void v3_delete_shadow_map(struct guest_info * info) {
65 struct rb_node * node = v3_rb_first(&(info->mem_map.shdw_regions));
66 struct v3_shadow_region * reg;
67 struct rb_node * tmp_node = NULL;
70 reg = rb_entry(node, struct v3_shadow_region, tree_node);
72 node = v3_rb_next(node);
74 v3_delete_shadow_region(info, reg);
77 V3_FreePage((void *)(info->mem_map.base_region.host_addr));
78 V3_FreePage(V3_PAddr((void *)(info->mem_map.hook_hva)));
84 int v3_add_shadow_mem( struct guest_info * info,
85 addr_t guest_addr_start,
86 addr_t guest_addr_end,
89 struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
91 entry->guest_start = guest_addr_start;
92 entry->guest_end = guest_addr_end;
93 entry->host_type = SHDW_REGION_ALLOCATED;
94 entry->host_addr = host_addr;
95 entry->write_hook = NULL;
96 entry->read_hook = NULL;
97 entry->priv_data = NULL;
99 if (insert_shadow_region(info, entry)) {
109 int v3_hook_write_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
111 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
114 struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
117 entry->guest_start = guest_addr_start;
118 entry->guest_end = guest_addr_end;
119 entry->host_type = SHDW_REGION_WRITE_HOOK;
120 entry->host_addr = host_addr;
121 entry->write_hook = write;
122 entry->read_hook = NULL;
123 entry->priv_data = priv_data;
125 if (insert_shadow_region(info, entry)) {
133 int v3_hook_full_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
134 int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data),
135 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
138 struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
140 entry->guest_start = guest_addr_start;
141 entry->guest_end = guest_addr_end;
142 entry->host_type = SHDW_REGION_FULL_HOOK;
143 entry->host_addr = (addr_t)NULL;
144 entry->write_hook = write;
145 entry->read_hook = read;
146 entry->priv_data = priv_data;
148 if (insert_shadow_region(info, entry)) {
157 // This will unhook the memory hook registered at start address
158 // We do not support unhooking subregions
159 int v3_unhook_mem(struct guest_info * info, addr_t guest_addr_start) {
160 struct v3_shadow_region * reg = v3_get_shadow_region(info, guest_addr_start);
162 if ((reg->host_type != SHDW_REGION_FULL_HOOK) ||
163 (reg->host_type != SHDW_REGION_WRITE_HOOK)) {
164 PrintError("Trying to unhook a non hooked memory region (addr=%p)\n", (void *)guest_addr_start);
168 v3_delete_shadow_region(info, reg);
176 struct v3_shadow_region * __insert_shadow_region(struct guest_info * info,
177 struct v3_shadow_region * region) {
178 struct rb_node ** p = &(info->mem_map.shdw_regions.rb_node);
179 struct rb_node * parent = NULL;
180 struct v3_shadow_region * tmp_region;
184 tmp_region = rb_entry(parent, struct v3_shadow_region, tree_node);
186 if (region->guest_end <= tmp_region->guest_start) {
188 } else if (region->guest_start >= tmp_region->guest_end) {
195 rb_link_node(&(region->tree_node), parent, p);
202 struct v3_shadow_region * insert_shadow_region(struct guest_info * info,
203 struct v3_shadow_region * region) {
204 struct v3_shadow_region * ret;
206 if ((ret = __insert_shadow_region(info, region))) {
210 v3_rb_insert_color(&(region->tree_node), &(info->mem_map.shdw_regions));
214 // flush virtual page tables
215 // 3 cases shadow, shadow passthrough, and nested
216 if (info->shdw_pg_mode == SHADOW_PAGING) {
217 v3_mem_mode_t mem_mode = v3_get_vm_mem_mode(info);
219 if (mem_mode == PHYSICAL_MEM) {
222 for (cur_addr = region->guest_start;
223 cur_addr < region->guest_end;
224 cur_addr += PAGE_SIZE_4KB) {
225 v3_invalidate_passthrough_addr(info, cur_addr);
228 v3_invalidate_shadow_pts(info);
231 } else if (info->shdw_pg_mode == NESTED_PAGING) {
234 for (cur_addr = region->guest_start;
235 cur_addr < region->guest_end;
236 cur_addr += PAGE_SIZE_4KB) {
238 v3_invalidate_nested_addr(info, cur_addr);
248 int handle_special_page_fault(struct guest_info * info,
249 addr_t fault_gva, addr_t fault_gpa,
250 pf_error_t access_info)
252 struct v3_shadow_region * reg = v3_get_shadow_region(info, fault_gpa);
254 PrintDebug("Handling Special Page Fault\n");
256 switch (reg->host_type) {
257 case SHDW_REGION_WRITE_HOOK:
258 return v3_handle_mem_wr_hook(info, fault_gva, fault_gpa, reg, access_info);
259 case SHDW_REGION_FULL_HOOK:
260 return v3_handle_mem_full_hook(info, fault_gva, fault_gpa, reg, access_info);
269 int v3_handle_mem_wr_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa,
270 struct v3_shadow_region * reg, pf_error_t access_info) {
272 addr_t dst_addr = (addr_t)V3_VAddr((void *)v3_get_shadow_addr(reg, guest_pa));
274 if (v3_emulate_write_op(info, guest_va, guest_pa, dst_addr,
275 reg->write_hook, reg->priv_data) == -1) {
276 PrintError("Write hook emulation failed\n");
283 int v3_handle_mem_full_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa,
284 struct v3_shadow_region * reg, pf_error_t access_info) {
286 addr_t op_addr = info->mem_map.hook_hva;
288 if (access_info.write == 1) {
289 if (v3_emulate_write_op(info, guest_va, guest_pa, op_addr,
290 reg->write_hook, reg->priv_data) == -1) {
291 PrintError("Write Full Hook emulation failed\n");
295 if (v3_emulate_read_op(info, guest_va, guest_pa, op_addr,
296 reg->read_hook, reg->write_hook,
297 reg->priv_data) == -1) {
298 PrintError("Read Full Hook emulation failed\n");
308 struct v3_shadow_region * v3_get_shadow_region(struct guest_info * info, addr_t guest_addr) {
309 struct rb_node * n = info->mem_map.shdw_regions.rb_node;
310 struct v3_shadow_region * reg = NULL;
313 reg = rb_entry(n, struct v3_shadow_region, tree_node);
315 if (guest_addr < reg->guest_start) {
317 } else if (guest_addr >= reg->guest_end) {
325 // There is not registered region, so we check if its a valid address in the base region
327 if (guest_addr > info->mem_map.base_region.guest_end) {
328 PrintError("Guest Address Exceeds Base Memory Size (ga=%p), (limit=%p)\n",
329 (void *)guest_addr, (void *)info->mem_map.base_region.guest_end);
333 return &(info->mem_map.base_region);
337 void v3_delete_shadow_region(struct guest_info * info, struct v3_shadow_region * reg) {
342 // flush virtual page tables
343 // 3 cases shadow, shadow passthrough, and nested
344 if (info->shdw_pg_mode == SHADOW_PAGING) {
345 v3_mem_mode_t mem_mode = v3_get_vm_mem_mode(info);
347 if (mem_mode == PHYSICAL_MEM) {
350 for (cur_addr = reg->guest_start;
351 cur_addr < reg->guest_end;
352 cur_addr += PAGE_SIZE_4KB) {
353 v3_invalidate_passthrough_addr(info, cur_addr);
356 v3_invalidate_shadow_pts(info);
359 } else if (info->shdw_pg_mode == NESTED_PAGING) {
362 for (cur_addr = reg->guest_start;
363 cur_addr < reg->guest_end;
364 cur_addr += PAGE_SIZE_4KB) {
366 v3_invalidate_nested_addr(info, cur_addr);
371 v3_rb_erase(&(reg->tree_node), &(info->mem_map.shdw_regions));
375 // flush virtual page tables
376 // 3 cases shadow, shadow passthrough, and nested
383 addr_t v3_get_shadow_addr(struct v3_shadow_region * reg, addr_t guest_addr) {
385 (reg->host_type != SHDW_REGION_FULL_HOOK)) {
386 return (guest_addr - reg->guest_start) + reg->host_addr;
388 PrintError("MEM Region Invalid\n");
396 void print_shadow_map(struct guest_info * info) {
397 struct rb_node * node = v3_rb_first(&(info->mem_map.shdw_regions));
398 struct v3_shadow_region * reg = &(info->mem_map.base_region);
401 PrintDebug("Memory Layout:\n");
404 PrintDebug("Base Region: 0x%p - 0x%p -> 0x%p\n",
405 (void *)(reg->guest_start),
406 (void *)(reg->guest_end - 1),
407 (void *)(reg->host_addr));
410 reg = rb_entry(node, struct v3_shadow_region, tree_node);
412 PrintDebug("%d: 0x%p - 0x%p -> 0x%p\n", i,
413 (void *)(reg->guest_start),
414 (void *)(reg->guest_end - 1),
415 (void *)(reg->host_addr));
417 PrintDebug("\t(%s) (WriteHook = 0x%p) (ReadHook = 0x%p)\n",
418 v3_shdw_region_type_to_str(reg->host_type),
419 (void *)(reg->write_hook),
420 (void *)(reg->read_hook));
423 } while ((node = v3_rb_next(node)));
427 static const uchar_t SHDW_REGION_WRITE_HOOK_STR[] = "SHDW_REGION_WRITE_HOOK";
428 static const uchar_t SHDW_REGION_FULL_HOOK_STR[] = "SHDW_REGION_FULL_HOOK";
429 static const uchar_t SHDW_REGION_ALLOCATED_STR[] = "SHDW_REGION_ALLOCATED";
431 const uchar_t * v3_shdw_region_type_to_str(v3_shdw_region_type_t type) {
433 case SHDW_REGION_WRITE_HOOK:
434 return SHDW_REGION_WRITE_HOOK_STR;
435 case SHDW_REGION_FULL_HOOK:
436 return SHDW_REGION_FULL_HOOK_STR;
437 case SHDW_REGION_ALLOCATED:
438 return SHDW_REGION_ALLOCATED_STR;
440 return (uchar_t *)"SHDW_REGION_INVALID";