1 /* Northwestern University */
2 /* (c) 2008, Jack Lange <jarusl@cs.northwestern.edu> */
4 #include <palacios/vmm_mem.h>
5 #include <palacios/vmm.h>
6 #include <palacios/vmm_util.h>
7 #include <palacios/vmm_decoder.h>
11 void init_shadow_region(struct shadow_region * entry,
12 addr_t guest_addr_start,
13 addr_t guest_addr_end,
14 guest_region_type_t guest_region_type,
15 host_region_type_t host_region_type)
17 entry->guest_type = guest_region_type;
18 entry->guest_start = guest_addr_start;
19 entry->guest_end = guest_addr_end;
20 entry->host_type = host_region_type;
22 entry->next=entry->prev = NULL;
25 int add_shadow_region_passthrough( struct guest_info * guest_info,
26 addr_t guest_addr_start,
27 addr_t guest_addr_end,
30 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
32 init_shadow_region(entry, guest_addr_start, guest_addr_end,
33 GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_PHYSICAL_MEMORY);
34 entry->host_addr = host_addr;
36 return add_shadow_region(&(guest_info->mem_map), entry);
39 int hook_guest_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
40 int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data),
41 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
44 struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region));
45 struct vmm_mem_hook * hook = (struct vmm_mem_hook *)V3_Malloc(sizeof(struct vmm_mem_hook));
47 memset(hook, 0, sizeof(struct vmm_mem_hook));
52 hook->priv_data = priv_data;
55 init_shadow_region(entry, guest_addr_start, guest_addr_end,
56 GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_HOOK);
58 entry->host_addr = (addr_t)hook;
60 return add_shadow_region(&(info->mem_map), entry);
64 struct vmm_mem_hook * get_mem_hook(struct guest_info * info, addr_t guest_addr) {
65 struct shadow_region * region = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
68 PrintDebug("Could not find shadow region for addr: %x\n", guest_addr);
72 return (struct vmm_mem_hook *)(region->host_addr);
76 /* mem_addr is the guest physical memory address */
77 static int mem_hook_dispatch(struct guest_info * info,
78 addr_t fault_gva, addr_t fault_gpa,
79 pf_error_t access_info, struct vmm_mem_hook * hook)
82 // emulate and then dispatch
83 // or dispatch and emulate
86 if (access_info.write == 1) {
87 if (v3_emulate_memory_write(info, fault_gva, hook->write, fault_gpa, hook->priv_data) == -1) {
88 PrintError("Memory write emulation failed\n");
93 if (v3_emulate_memory_read(info, fault_gva, hook->read, fault_gpa, hook->priv_data) == -1) {
94 PrintError("Memory read emulation failed\n");
103 int handle_special_page_fault(struct guest_info * info,
104 addr_t fault_gva, addr_t fault_gpa,
105 pf_error_t access_info)
107 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), fault_gpa);
109 switch (reg->host_type) {
110 case HOST_REGION_HOOK:
111 return mem_hook_dispatch(info, fault_gva, fault_gpa, access_info, (struct vmm_mem_hook *)(reg->host_addr));
122 void init_shadow_map(struct guest_info * info) {
123 struct shadow_map * map = &(info->mem_map);
125 map->num_regions = 0;
131 void free_shadow_map(struct shadow_map * map) {
132 struct shadow_region * cursor = map->head;
133 struct shadow_region * tmp = NULL;
137 cursor = cursor->next;
147 int add_shadow_region(struct shadow_map * map,
148 struct shadow_region * region)
150 struct shadow_region * cursor = map->head;
152 PrintDebug("Adding Shadow Region: (0x%x-0x%x)\n", region->guest_start, region->guest_end);
154 if ((!cursor) || (cursor->guest_start >= region->guest_end)) {
156 region->next = cursor;
163 // Check if it overlaps with the current cursor
164 if ((cursor->guest_end > region->guest_start) && (cursor->guest_start < region->guest_start)) {
165 // overlaps not allowed
169 if (!(cursor->next)) {
170 // add to the end of the list
171 cursor->next = region;
172 region->prev = cursor;
176 } else if (cursor->next->guest_start >= region->guest_end) {
178 region->next = cursor->next;
179 region->prev = cursor;
181 cursor->next->prev = region;
182 cursor->next = region;
187 } else if (cursor->next->guest_end <= region->guest_start) {
188 cursor = cursor->next;
190 // This cannot happen!
191 // we should panic here
196 // This cannot happen
197 // We should panic here
202 int delete_shadow_region(struct shadow_map * map,
210 struct shadow_region *get_shadow_region_by_index(struct shadow_map * map,
212 struct shadow_region * reg = map->head;
226 struct shadow_region * get_shadow_region_by_addr(struct shadow_map * map,
228 struct shadow_region * reg = map->head;
231 if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
233 } else if (reg->guest_start > addr) {
243 host_region_type_t get_shadow_addr_type(struct guest_info * info, addr_t guest_addr) {
244 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
247 return HOST_REGION_INVALID;
249 return reg->host_type;
253 addr_t get_shadow_addr(struct guest_info * info, addr_t guest_addr) {
254 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
259 return (guest_addr - reg->guest_start) + reg->host_addr;
264 host_region_type_t lookup_shadow_map_addr(struct shadow_map * map, addr_t guest_addr, addr_t * host_addr) {
265 struct shadow_region * reg = get_shadow_region_by_addr(map, guest_addr);
269 return HOST_REGION_INVALID;
271 switch (reg->host_type) {
272 case HOST_REGION_PHYSICAL_MEMORY:
273 *host_addr = (guest_addr - reg->guest_start) + reg->host_addr;
274 return reg->host_type;
275 case HOST_REGION_MEMORY_MAPPED_DEVICE:
276 case HOST_REGION_UNALLOCATED:
280 return reg->host_type;
286 void print_shadow_map(struct shadow_map * map) {
287 struct shadow_region * cur = map->head;
290 PrintDebug("Memory Layout (regions: %d) \n", map->num_regions);
293 PrintDebug("%d: 0x%x - 0x%x (%s) -> ", i, cur->guest_start, cur->guest_end - 1,
294 cur->guest_type == GUEST_REGION_PHYSICAL_MEMORY ? "GUEST_REGION_PHYSICAL_MEMORY" :
295 cur->guest_type == GUEST_REGION_NOTHING ? "GUEST_REGION_NOTHING" :
296 cur->guest_type == GUEST_REGION_MEMORY_MAPPED_DEVICE ? "GUEST_REGION_MEMORY_MAPPED_DEVICE" :
298 if (cur->host_type == HOST_REGION_PHYSICAL_MEMORY ||
299 cur->host_type == HOST_REGION_UNALLOCATED ||
300 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) {
301 PrintDebug("0x%x", cur->host_addr);
304 cur->host_type == HOST_REGION_PHYSICAL_MEMORY ? "HOST_REGION_PHYSICAL_MEMORY" :
305 cur->host_type == HOST_REGION_UNALLOCATED ? "HOST_REGION_UNALLOACTED" :
306 cur->host_type == HOST_REGION_HOOK ? "HOST_REGION_HOOK" :
307 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE ? "HOST_REGION_MEMORY_MAPPED_DEVICE" :
308 cur->host_type == HOST_REGION_REMOTE ? "HOST_REGION_REMOTE" :
309 cur->host_type == HOST_REGION_SWAPPED ? "HOST_REGION_SWAPPED" :
336 struct vmm_os_hooks * os_hooks;
338 void * TestMalloc(uint_t size) {
342 void * TestAllocatePages(int size) {
343 return malloc(4096 * size);
347 void TestPrint(const char * fmt, ...) {
355 int mem_list_add_test_1( vmm_mem_list_t * list) {
359 PrintDebug("\n\nTesting Memory List\n");
363 offset = PAGE_SIZE * 6;
364 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 10));
365 add_mem_list_pages(list, offset, 10);
366 print_mem_list(list);
370 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + PAGE_SIZE * 4);
371 add_mem_list_pages(list, offset, 4);
372 print_mem_list(list);
374 offset = PAGE_SIZE * 20;
375 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
376 add_mem_list_pages(list, offset, 1);
377 print_mem_list(list);
379 offset = PAGE_SIZE * 21;
380 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 3));
381 add_mem_list_pages(list, offset, 3);
382 print_mem_list(list);
385 offset = PAGE_SIZE * 10;
386 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 30));
387 add_mem_list_pages(list, offset, 30);
388 print_mem_list(list);
391 offset = PAGE_SIZE * 5;
392 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
393 add_mem_list_pages(list, offset, 1);
394 print_mem_list(list);
402 int mem_layout_add_test_1(vmm_mem_layout_t * layout) {
408 PrintDebug("\n\nTesting Memory Layout\n");
410 init_mem_layout(layout);
414 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
415 add_guest_mem_range(layout, start, end);
416 print_mem_layout(layout);
421 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
422 add_guest_mem_range(layout, start, end);
423 print_mem_layout(layout);
427 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
428 add_guest_mem_range(layout, start, end);
429 print_mem_layout(layout);
433 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
434 add_guest_mem_range(layout, start, end);
435 print_mem_layout(layout);
440 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
441 add_guest_mem_range(layout, start, end);
442 print_mem_layout(layout);
452 int main(int argc, char ** argv) {
453 struct vmm_os_hooks dummy_hooks;
454 os_hooks = &dummy_hooks;
456 vmm_mem_layout_t layout;
459 os_hooks->malloc = &TestMalloc;
460 os_hooks->free = &free;
461 os_hooks->print_debug = &TestPrint;
462 os_hooks->allocate_pages = &TestAllocatePages;
466 printf("mem_list_add_test_1: %d\n", mem_list_add_test_1(&list));
467 printf("layout_add_test_1: %d\n", mem_layout_add_test_1(&layout));