1 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
2 /* (c) 2008, The V3VEE Project <http://www.v3vee.org> */
5 #include <palacios/vmm_mem.h>
6 #include <palacios/vmm.h>
7 #include <palacios/vmm_util.h>
8 #include <palacios/vmm_decoder.h>
12 void init_shadow_region(struct shadow_region * entry,
13 addr_t guest_addr_start,
14 addr_t guest_addr_end,
15 guest_region_type_t guest_region_type,
16 host_region_type_t host_region_type)
18 entry->guest_type = guest_region_type;
19 entry->guest_start = guest_addr_start;
20 entry->guest_end = guest_addr_end;
21 entry->host_type = host_region_type;
23 entry->next=entry->prev = NULL;
26 int add_shadow_region_passthrough( struct guest_info * guest_info,
27 addr_t guest_addr_start,
28 addr_t guest_addr_end,
31 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
33 init_shadow_region(entry, guest_addr_start, guest_addr_end,
34 GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_PHYSICAL_MEMORY);
35 entry->host_addr = host_addr;
37 return add_shadow_region(&(guest_info->mem_map), entry);
40 int hook_guest_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
41 int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data),
42 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
45 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
46 struct vmm_mem_hook * hook = (struct vmm_mem_hook *)V3_Malloc(sizeof(struct vmm_mem_hook));
48 memset(hook, 0, sizeof(struct vmm_mem_hook));
53 hook->priv_data = priv_data;
56 init_shadow_region(entry, guest_addr_start, guest_addr_end,
57 GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_HOOK);
59 entry->host_addr = (addr_t)hook;
61 return add_shadow_region(&(info->mem_map), entry);
65 struct vmm_mem_hook * get_mem_hook(struct guest_info * info, addr_t guest_addr) {
66 struct shadow_region * region = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
69 PrintDebug("Could not find shadow region for addr: %x\n", guest_addr);
73 return (struct vmm_mem_hook *)(region->host_addr);
77 /* mem_addr is the guest physical memory address */
78 static int mem_hook_dispatch(struct guest_info * info,
79 addr_t fault_gva, addr_t fault_gpa,
80 pf_error_t access_info, struct vmm_mem_hook * hook)
83 // emulate and then dispatch
84 // or dispatch and emulate
87 if (access_info.write == 1) {
88 if (v3_emulate_memory_write(info, fault_gva, hook->write, fault_gpa, hook->priv_data) == -1) {
89 PrintError("Memory write emulation failed\n");
94 if (v3_emulate_memory_read(info, fault_gva, hook->read, fault_gpa, hook->priv_data) == -1) {
95 PrintError("Memory read emulation failed\n");
104 int handle_special_page_fault(struct guest_info * info,
105 addr_t fault_gva, addr_t fault_gpa,
106 pf_error_t access_info)
108 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), fault_gpa);
110 switch (reg->host_type) {
111 case HOST_REGION_HOOK:
112 return mem_hook_dispatch(info, fault_gva, fault_gpa, access_info, (struct vmm_mem_hook *)(reg->host_addr));
123 void init_shadow_map(struct guest_info * info) {
124 struct shadow_map * map = &(info->mem_map);
126 map->num_regions = 0;
132 void free_shadow_map(struct shadow_map * map) {
133 struct shadow_region * cursor = map->head;
134 struct shadow_region * tmp = NULL;
138 cursor = cursor->next;
148 int add_shadow_region(struct shadow_map * map,
149 struct shadow_region * region)
151 struct shadow_region * cursor = map->head;
153 PrintDebug("Adding Shadow Region: (0x%x-0x%x)\n", region->guest_start, region->guest_end);
155 if ((!cursor) || (cursor->guest_start >= region->guest_end)) {
157 region->next = cursor;
164 // Check if it overlaps with the current cursor
165 if ((cursor->guest_end > region->guest_start) && (cursor->guest_start < region->guest_start)) {
166 // overlaps not allowed
170 if (!(cursor->next)) {
171 // add to the end of the list
172 cursor->next = region;
173 region->prev = cursor;
177 } else if (cursor->next->guest_start >= region->guest_end) {
179 region->next = cursor->next;
180 region->prev = cursor;
182 cursor->next->prev = region;
183 cursor->next = region;
188 } else if (cursor->next->guest_end <= region->guest_start) {
189 cursor = cursor->next;
191 // This cannot happen!
192 // we should panic here
197 // This cannot happen
198 // We should panic here
203 int delete_shadow_region(struct shadow_map * map,
211 struct shadow_region *get_shadow_region_by_index(struct shadow_map * map,
213 struct shadow_region * reg = map->head;
227 struct shadow_region * get_shadow_region_by_addr(struct shadow_map * map,
229 struct shadow_region * reg = map->head;
232 if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
234 } else if (reg->guest_start > addr) {
244 host_region_type_t get_shadow_addr_type(struct guest_info * info, addr_t guest_addr) {
245 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
248 return HOST_REGION_INVALID;
250 return reg->host_type;
254 addr_t get_shadow_addr(struct guest_info * info, addr_t guest_addr) {
255 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
260 return (guest_addr - reg->guest_start) + reg->host_addr;
265 host_region_type_t lookup_shadow_map_addr(struct shadow_map * map, addr_t guest_addr, addr_t * host_addr) {
266 struct shadow_region * reg = get_shadow_region_by_addr(map, guest_addr);
270 return HOST_REGION_INVALID;
272 switch (reg->host_type) {
273 case HOST_REGION_PHYSICAL_MEMORY:
274 *host_addr = (guest_addr - reg->guest_start) + reg->host_addr;
275 return reg->host_type;
276 case HOST_REGION_MEMORY_MAPPED_DEVICE:
277 case HOST_REGION_UNALLOCATED:
281 return reg->host_type;
287 void print_shadow_map(struct shadow_map * map) {
288 struct shadow_region * cur = map->head;
291 PrintDebug("Memory Layout (regions: %d) \n", map->num_regions);
294 PrintDebug("%d: 0x%x - 0x%x (%s) -> ", i, cur->guest_start, cur->guest_end - 1,
295 cur->guest_type == GUEST_REGION_PHYSICAL_MEMORY ? "GUEST_REGION_PHYSICAL_MEMORY" :
296 cur->guest_type == GUEST_REGION_NOTHING ? "GUEST_REGION_NOTHING" :
297 cur->guest_type == GUEST_REGION_MEMORY_MAPPED_DEVICE ? "GUEST_REGION_MEMORY_MAPPED_DEVICE" :
299 if (cur->host_type == HOST_REGION_PHYSICAL_MEMORY ||
300 cur->host_type == HOST_REGION_UNALLOCATED ||
301 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) {
302 PrintDebug("0x%x", cur->host_addr);
305 cur->host_type == HOST_REGION_PHYSICAL_MEMORY ? "HOST_REGION_PHYSICAL_MEMORY" :
306 cur->host_type == HOST_REGION_UNALLOCATED ? "HOST_REGION_UNALLOACTED" :
307 cur->host_type == HOST_REGION_HOOK ? "HOST_REGION_HOOK" :
308 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE ? "HOST_REGION_MEMORY_MAPPED_DEVICE" :
309 cur->host_type == HOST_REGION_REMOTE ? "HOST_REGION_REMOTE" :
310 cur->host_type == HOST_REGION_SWAPPED ? "HOST_REGION_SWAPPED" :
337 struct vmm_os_hooks * os_hooks;
339 void * TestMalloc(uint_t size) {
343 void * TestAllocatePages(int size) {
344 return malloc(4096 * size);
348 void TestPrint(const char * fmt, ...) {
356 int mem_list_add_test_1( vmm_mem_list_t * list) {
360 PrintDebug("\n\nTesting Memory List\n");
364 offset = PAGE_SIZE * 6;
365 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 10));
366 add_mem_list_pages(list, offset, 10);
367 print_mem_list(list);
371 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + PAGE_SIZE * 4);
372 add_mem_list_pages(list, offset, 4);
373 print_mem_list(list);
375 offset = PAGE_SIZE * 20;
376 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
377 add_mem_list_pages(list, offset, 1);
378 print_mem_list(list);
380 offset = PAGE_SIZE * 21;
381 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 3));
382 add_mem_list_pages(list, offset, 3);
383 print_mem_list(list);
386 offset = PAGE_SIZE * 10;
387 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 30));
388 add_mem_list_pages(list, offset, 30);
389 print_mem_list(list);
392 offset = PAGE_SIZE * 5;
393 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
394 add_mem_list_pages(list, offset, 1);
395 print_mem_list(list);
403 int mem_layout_add_test_1(vmm_mem_layout_t * layout) {
409 PrintDebug("\n\nTesting Memory Layout\n");
411 init_mem_layout(layout);
415 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
416 add_guest_mem_range(layout, start, end);
417 print_mem_layout(layout);
422 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
423 add_guest_mem_range(layout, start, end);
424 print_mem_layout(layout);
428 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
429 add_guest_mem_range(layout, start, end);
430 print_mem_layout(layout);
434 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
435 add_guest_mem_range(layout, start, end);
436 print_mem_layout(layout);
441 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
442 add_guest_mem_range(layout, start, end);
443 print_mem_layout(layout);
453 int main(int argc, char ** argv) {
454 struct vmm_os_hooks dummy_hooks;
455 os_hooks = &dummy_hooks;
457 vmm_mem_layout_t layout;
460 os_hooks->malloc = &TestMalloc;
461 os_hooks->free = &free;
462 os_hooks->print_debug = &TestPrint;
463 os_hooks->allocate_pages = &TestAllocatePages;
467 printf("mem_list_add_test_1: %d\n", mem_list_add_test_1(&list));
468 printf("layout_add_test_1: %d\n", mem_layout_add_test_1(&layout));