1 #include <palacios/vmm_mem.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_util.h>
4 #include <palacios/vmm_decoder.h>
8 void init_shadow_region(struct shadow_region * entry,
9 addr_t guest_addr_start,
10 addr_t guest_addr_end,
11 guest_region_type_t guest_region_type,
12 host_region_type_t host_region_type)
14 entry->guest_type = guest_region_type;
15 entry->guest_start = guest_addr_start;
16 entry->guest_end = guest_addr_end;
17 entry->host_type = host_region_type;
19 entry->next=entry->prev = NULL;
22 int add_shadow_region_passthrough( struct guest_info * guest_info,
23 addr_t guest_addr_start,
24 addr_t guest_addr_end,
27 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
29 init_shadow_region(entry, guest_addr_start, guest_addr_end,
30 GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_PHYSICAL_MEMORY);
31 entry->host_addr = host_addr;
33 return add_shadow_region(&(guest_info->mem_map), entry);
36 int hook_guest_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
37 int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data),
38 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
41 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
42 struct vmm_mem_hook * hook = (struct vmm_mem_hook *)V3_Malloc(sizeof(struct vmm_mem_hook));
44 memset(hook, 0, sizeof(struct vmm_mem_hook));
49 hook->priv_data = priv_data;
52 init_shadow_region(entry, guest_addr_start, guest_addr_end,
53 GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_HOOK);
55 entry->host_addr = (addr_t)hook;
57 return add_shadow_region(&(info->mem_map), entry);
61 struct vmm_mem_hook * get_mem_hook(struct guest_info * info, addr_t guest_addr) {
62 struct shadow_region * region = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
65 PrintDebug("Could not find shadow region for addr: %x\n", guest_addr);
69 return (struct vmm_mem_hook *)(region->host_addr);
73 /* mem_addr is the guest physical memory address */
74 static int mem_hook_dispatch(struct guest_info * info,
75 addr_t fault_gva, addr_t fault_gpa,
76 pf_error_t access_info, struct vmm_mem_hook * hook)
79 // emulate and then dispatch
80 // or dispatch and emulate
83 if (access_info.write == 1) {
84 if (v3_emulate_memory_write(info, fault_gva, hook->write, fault_gpa, hook->priv_data) == -1) {
85 PrintError("Memory write emulation failed\n");
90 if (v3_emulate_memory_read(info, fault_gva, hook->read, fault_gpa, hook->priv_data) == -1) {
91 PrintError("Memory read emulation failed\n");
100 int handle_special_page_fault(struct guest_info * info,
101 addr_t fault_gva, addr_t fault_gpa,
102 pf_error_t access_info)
104 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), fault_gpa);
106 switch (reg->host_type) {
107 case HOST_REGION_HOOK:
108 return mem_hook_dispatch(info, fault_gva, fault_gpa, access_info, (struct vmm_mem_hook *)(reg->host_addr));
119 void init_shadow_map(struct guest_info * info) {
120 struct shadow_map * map = &(info->mem_map);
122 map->num_regions = 0;
128 void free_shadow_map(struct shadow_map * map) {
129 struct shadow_region * cursor = map->head;
130 struct shadow_region * tmp = NULL;
134 cursor = cursor->next;
144 int add_shadow_region(struct shadow_map * map,
145 struct shadow_region * region)
147 struct shadow_region * cursor = map->head;
149 PrintDebug("Adding Shadow Region: (0x%x-0x%x)\n", region->guest_start, region->guest_end);
151 if ((!cursor) || (cursor->guest_start >= region->guest_end)) {
153 region->next = cursor;
160 // Check if it overlaps with the current cursor
161 if ((cursor->guest_end > region->guest_start) && (cursor->guest_start < region->guest_start)) {
162 // overlaps not allowed
166 if (!(cursor->next)) {
167 // add to the end of the list
168 cursor->next = region;
169 region->prev = cursor;
173 } else if (cursor->next->guest_start >= region->guest_end) {
175 region->next = cursor->next;
176 region->prev = cursor;
178 cursor->next->prev = region;
179 cursor->next = region;
184 } else if (cursor->next->guest_end <= region->guest_start) {
185 cursor = cursor->next;
187 // This cannot happen!
188 // we should panic here
193 // This cannot happen
194 // We should panic here
199 int delete_shadow_region(struct shadow_map * map,
207 struct shadow_region *get_shadow_region_by_index(struct shadow_map * map,
209 struct shadow_region * reg = map->head;
223 struct shadow_region * get_shadow_region_by_addr(struct shadow_map * map,
225 struct shadow_region * reg = map->head;
228 if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
230 } else if (reg->guest_start > addr) {
240 host_region_type_t get_shadow_addr_type(struct guest_info * info, addr_t guest_addr) {
241 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
244 return HOST_REGION_INVALID;
246 return reg->host_type;
250 addr_t get_shadow_addr(struct guest_info * info, addr_t guest_addr) {
251 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
256 return (guest_addr - reg->guest_start) + reg->host_addr;
261 host_region_type_t lookup_shadow_map_addr(struct shadow_map * map, addr_t guest_addr, addr_t * host_addr) {
262 struct shadow_region * reg = get_shadow_region_by_addr(map, guest_addr);
266 return HOST_REGION_INVALID;
268 switch (reg->host_type) {
269 case HOST_REGION_PHYSICAL_MEMORY:
270 *host_addr = (guest_addr - reg->guest_start) + reg->host_addr;
271 return reg->host_type;
272 case HOST_REGION_MEMORY_MAPPED_DEVICE:
273 case HOST_REGION_UNALLOCATED:
277 return reg->host_type;
283 void print_shadow_map(struct shadow_map * map) {
284 struct shadow_region * cur = map->head;
287 PrintDebug("Memory Layout (regions: %d) \n", map->num_regions);
290 PrintDebug("%d: 0x%x - 0x%x (%s) -> ", i, cur->guest_start, cur->guest_end - 1,
291 cur->guest_type == GUEST_REGION_PHYSICAL_MEMORY ? "GUEST_REGION_PHYSICAL_MEMORY" :
292 cur->guest_type == GUEST_REGION_NOTHING ? "GUEST_REGION_NOTHING" :
293 cur->guest_type == GUEST_REGION_MEMORY_MAPPED_DEVICE ? "GUEST_REGION_MEMORY_MAPPED_DEVICE" :
295 if (cur->host_type == HOST_REGION_PHYSICAL_MEMORY ||
296 cur->host_type == HOST_REGION_UNALLOCATED ||
297 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) {
298 PrintDebug("0x%x", cur->host_addr);
301 cur->host_type == HOST_REGION_PHYSICAL_MEMORY ? "HOST_REGION_PHYSICAL_MEMORY" :
302 cur->host_type == HOST_REGION_UNALLOCATED ? "HOST_REGION_UNALLOACTED" :
303 cur->host_type == HOST_REGION_HOOK ? "HOST_REGION_HOOK" :
304 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE ? "HOST_REGION_MEMORY_MAPPED_DEVICE" :
305 cur->host_type == HOST_REGION_REMOTE ? "HOST_REGION_REMOTE" :
306 cur->host_type == HOST_REGION_SWAPPED ? "HOST_REGION_SWAPPED" :
333 struct vmm_os_hooks * os_hooks;
335 void * TestMalloc(uint_t size) {
339 void * TestAllocatePages(int size) {
340 return malloc(4096 * size);
344 void TestPrint(const char * fmt, ...) {
352 int mem_list_add_test_1( vmm_mem_list_t * list) {
356 PrintDebug("\n\nTesting Memory List\n");
360 offset = PAGE_SIZE * 6;
361 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 10));
362 add_mem_list_pages(list, offset, 10);
363 print_mem_list(list);
367 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + PAGE_SIZE * 4);
368 add_mem_list_pages(list, offset, 4);
369 print_mem_list(list);
371 offset = PAGE_SIZE * 20;
372 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
373 add_mem_list_pages(list, offset, 1);
374 print_mem_list(list);
376 offset = PAGE_SIZE * 21;
377 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 3));
378 add_mem_list_pages(list, offset, 3);
379 print_mem_list(list);
382 offset = PAGE_SIZE * 10;
383 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 30));
384 add_mem_list_pages(list, offset, 30);
385 print_mem_list(list);
388 offset = PAGE_SIZE * 5;
389 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
390 add_mem_list_pages(list, offset, 1);
391 print_mem_list(list);
399 int mem_layout_add_test_1(vmm_mem_layout_t * layout) {
405 PrintDebug("\n\nTesting Memory Layout\n");
407 init_mem_layout(layout);
411 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
412 add_guest_mem_range(layout, start, end);
413 print_mem_layout(layout);
418 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
419 add_guest_mem_range(layout, start, end);
420 print_mem_layout(layout);
424 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
425 add_guest_mem_range(layout, start, end);
426 print_mem_layout(layout);
430 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
431 add_guest_mem_range(layout, start, end);
432 print_mem_layout(layout);
437 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
438 add_guest_mem_range(layout, start, end);
439 print_mem_layout(layout);
449 int main(int argc, char ** argv) {
450 struct vmm_os_hooks dummy_hooks;
451 os_hooks = &dummy_hooks;
453 vmm_mem_layout_t layout;
456 os_hooks->malloc = &TestMalloc;
457 os_hooks->free = &free;
458 os_hooks->print_debug = &TestPrint;
459 os_hooks->allocate_pages = &TestAllocatePages;
463 printf("mem_list_add_test_1: %d\n", mem_list_add_test_1(&list));
464 printf("layout_add_test_1: %d\n", mem_layout_add_test_1(&layout));