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_decoder.h>
27 void init_shadow_region(struct shadow_region * entry,
28 addr_t guest_addr_start,
29 addr_t guest_addr_end,
30 shdw_region_type_t shdw_region_type)
32 entry->guest_start = guest_addr_start;
33 entry->guest_end = guest_addr_end;
34 entry->host_type = shdw_region_type;
36 entry->next = entry->prev = NULL;
39 int add_shadow_region_passthrough( struct guest_info * guest_info,
40 addr_t guest_addr_start,
41 addr_t guest_addr_end,
44 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
46 init_shadow_region(entry, guest_addr_start, guest_addr_end,
47 SHDW_REGION_ALLOCATED);
48 entry->host_addr = host_addr;
50 return add_shadow_region(&(guest_info->mem_map), entry);
53 int v3_hook_write_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
55 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
58 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
60 init_shadow_region(entry, guest_addr_start, guest_addr_end,
61 SHDW_REGION_WRITE_HOOK);
63 entry->write_hook = write;
64 entry->read_hook = NULL;
65 entry->host_addr = host_addr;
66 entry->priv_data = priv_data;
68 return add_shadow_region(&(info->mem_map), entry);
71 int v3_hook_full_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
72 int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data),
73 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
76 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
78 init_shadow_region(entry, guest_addr_start, guest_addr_end,
79 SHDW_REGION_FULL_HOOK);
81 entry->write_hook = write;
82 entry->read_hook = read;
83 entry->priv_data = priv_data;
87 return add_shadow_region(&(info->mem_map), entry);
93 int handle_special_page_fault(struct guest_info * info,
94 addr_t fault_gva, addr_t fault_gpa,
95 pf_error_t access_info)
97 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), fault_gpa);
99 PrintDebug("Handling Special Page Fault\n");
101 switch (reg->host_type) {
102 case SHDW_REGION_WRITE_HOOK:
103 return v3_handle_mem_wr_hook(info, fault_gva, fault_gpa, reg, access_info);
104 case SHDW_REGION_FULL_HOOK:
105 return v3_handle_mem_full_hook(info, fault_gva, fault_gpa, reg, access_info);
114 int v3_handle_mem_wr_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa,
115 struct shadow_region * reg, pf_error_t access_info) {
117 addr_t write_src_addr = 0;
119 int write_len = v3_emulate_write_op(info, guest_va, guest_pa, &write_src_addr);
121 if (write_len == -1) {
122 PrintError("Emulation failure in write hook\n");
127 if (reg->write_hook(guest_pa, (void *)write_src_addr, write_len, reg->priv_data) != write_len) {
128 PrintError("Memory write hook did not return correct value\n");
135 int v3_handle_mem_full_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa,
136 struct shadow_region * reg, pf_error_t access_info) {
142 struct shadow_region * v3_get_shadow_region(struct guest_info * info, addr_t addr) {
143 struct shadow_region * reg = info->mem_map.head;
146 if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
148 } else if (reg->guest_start > addr) {
158 void init_shadow_map(struct guest_info * info) {
159 struct shadow_map * map = &(info->mem_map);
161 map->num_regions = 0;
167 void free_shadow_map(struct shadow_map * map) {
168 struct shadow_region * cursor = map->head;
169 struct shadow_region * tmp = NULL;
173 cursor = cursor->next;
183 int add_shadow_region(struct shadow_map * map,
184 struct shadow_region * region)
186 struct shadow_region * cursor = map->head;
188 PrintDebug("Adding Shadow Region: (0x%p-0x%p)\n",
189 (void *)region->guest_start, (void *)region->guest_end);
191 if ((!cursor) || (cursor->guest_start >= region->guest_end)) {
193 region->next = cursor;
200 // Check if it overlaps with the current cursor
201 if ((cursor->guest_end > region->guest_start) && (cursor->guest_start < region->guest_start)) {
202 // overlaps not allowed
206 if (!(cursor->next)) {
207 // add to the end of the list
208 cursor->next = region;
209 region->prev = cursor;
213 } else if (cursor->next->guest_start >= region->guest_end) {
215 region->next = cursor->next;
216 region->prev = cursor;
218 cursor->next->prev = region;
219 cursor->next = region;
224 } else if (cursor->next->guest_end <= region->guest_start) {
225 cursor = cursor->next;
227 // This cannot happen!
228 // we should panic here
233 // This cannot happen
234 // We should panic here
239 int delete_shadow_region(struct shadow_map * map,
247 struct shadow_region *get_shadow_region_by_index(struct shadow_map * map,
249 struct shadow_region * reg = map->head;
263 struct shadow_region * get_shadow_region_by_addr(struct shadow_map * map,
265 struct shadow_region * reg = map->head;
268 if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
270 } else if (reg->guest_start > addr) {
280 shdw_region_type_t get_shadow_addr_type(struct guest_info * info, addr_t guest_addr) {
281 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
284 return SHDW_REGION_INVALID;
286 return reg->host_type;
290 addr_t get_shadow_addr(struct guest_info * info, addr_t guest_addr) {
291 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
296 return (guest_addr - reg->guest_start) + reg->host_addr;
301 shdw_region_type_t lookup_shadow_map_addr(struct shadow_map * map, addr_t guest_addr, addr_t * host_addr) {
302 struct shadow_region * reg = get_shadow_region_by_addr(map, guest_addr);
306 return SHDW_REGION_INVALID;
308 switch (reg->host_type) {
309 case SHDW_REGION_ALLOCATED:
310 case SHDW_REGION_WRITE_HOOK:
311 *host_addr = (guest_addr - reg->guest_start) + reg->host_addr;
312 return reg->host_type;
313 case SHDW_REGION_UNALLOCATED:
314 case SHDW_REGION_FULL_HOOK:
318 return reg->host_type;
324 void print_shadow_map(struct shadow_map * map) {
325 struct shadow_region * cur = map->head;
328 PrintDebug("Memory Layout (regions: %d) \n", map->num_regions);
331 PrintDebug("%d: 0x%p - 0x%p -> ", i,
332 (void *)cur->guest_start, (void *)(cur->guest_end - 1));
333 if (cur->host_type == SHDW_REGION_ALLOCATED ||
334 cur->host_type == SHDW_REGION_UNALLOCATED) {
335 PrintDebug("0x%p", (void *)(cur->host_addr));
337 PrintDebug("(%s)\n", shdw_region_type_to_str(cur->host_type));
344 static const uchar_t SHDW_REGION_INVALID_STR[] = "SHDW_REGION_INVALID";
345 static const uchar_t SHDW_REGION_WRITE_HOOK_STR[] = "SHDW_REGION_WRITE_HOOK";
346 static const uchar_t SHDW_REGION_FULL_HOOK_STR[] = "SHDW_REGION_FULL_HOOK";
347 static const uchar_t SHDW_REGION_ALLOCATED_STR[] = "SHDW_REGION_ALLOCATED";
348 static const uchar_t SHDW_REGION_UNALLOCATED_STR[] = "SHDW_REGION_UNALLOCATED";
352 const uchar_t * shdw_region_type_to_str(shdw_region_type_t type) {
354 case SHDW_REGION_INVALID:
355 return SHDW_REGION_INVALID_STR;
356 case SHDW_REGION_WRITE_HOOK:
357 return SHDW_REGION_WRITE_HOOK_STR;
358 case SHDW_REGION_FULL_HOOK:
359 return SHDW_REGION_FULL_HOOK_STR;
360 case SHDW_REGION_ALLOCATED:
361 return SHDW_REGION_ALLOCATED_STR;
362 case SHDW_REGION_UNALLOCATED:
363 return SHDW_REGION_UNALLOCATED_STR;
365 return SHDW_REGION_INVALID_STR;
386 struct vmm_os_hooks * os_hooks;
388 void * TestMalloc(uint_t size) {
392 void * TestAllocatePages(int size) {
393 return malloc(4096 * size);
397 void TestPrint(const char * fmt, ...) {
405 int mem_list_add_test_1( vmm_mem_list_t * list) {
409 PrintDebug("\n\nTesting Memory List\n");
413 offset = PAGE_SIZE * 6;
414 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 10));
415 add_mem_list_pages(list, offset, 10);
416 print_mem_list(list);
420 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + PAGE_SIZE * 4);
421 add_mem_list_pages(list, offset, 4);
422 print_mem_list(list);
424 offset = PAGE_SIZE * 20;
425 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
426 add_mem_list_pages(list, offset, 1);
427 print_mem_list(list);
429 offset = PAGE_SIZE * 21;
430 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 3));
431 add_mem_list_pages(list, offset, 3);
432 print_mem_list(list);
435 offset = PAGE_SIZE * 10;
436 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 30));
437 add_mem_list_pages(list, offset, 30);
438 print_mem_list(list);
441 offset = PAGE_SIZE * 5;
442 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
443 add_mem_list_pages(list, offset, 1);
444 print_mem_list(list);
452 int mem_layout_add_test_1(vmm_mem_layout_t * layout) {
458 PrintDebug("\n\nTesting Memory Layout\n");
460 init_mem_layout(layout);
464 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
465 add_guest_mem_range(layout, start, end);
466 print_mem_layout(layout);
471 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
472 add_guest_mem_range(layout, start, end);
473 print_mem_layout(layout);
477 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
478 add_guest_mem_range(layout, start, end);
479 print_mem_layout(layout);
483 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
484 add_guest_mem_range(layout, start, end);
485 print_mem_layout(layout);
490 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
491 add_guest_mem_range(layout, start, end);
492 print_mem_layout(layout);
502 int main(int argc, char ** argv) {
503 struct vmm_os_hooks dummy_hooks;
504 os_hooks = &dummy_hooks;
506 vmm_mem_layout_t layout;
509 os_hooks->malloc = &TestMalloc;
510 os_hooks->free = &free;
511 os_hooks->print_debug = &TestPrint;
512 os_hooks->allocate_pages = &TestAllocatePages;
516 printf("mem_list_add_test_1: %d\n", mem_list_add_test_1(&list));
517 printf("layout_add_test_1: %d\n", mem_layout_add_test_1(&layout));