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 int mem_hook_dispatch(struct guest_info * info, addr_t mem_addr, pf_error_t access_info, struct vmm_mem_hook * hook) {
75 // emulate and then dispatch
76 // or dispatch and emulate
79 if (access_info.write == 1) {
82 PrintDebug("Memory hook write\n");
85 if (hook->write(mem_addr, src, length, hook->priv_data) != length) {
89 PrintDebug("Memory hook read\n");
97 int handle_special_page_fault(struct guest_info * info, addr_t mem_addr, pf_error_t access_info) {
98 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), mem_addr);
100 switch (reg->host_type) {
101 case HOST_REGION_HOOK:
102 return mem_hook_dispatch(info, mem_addr, access_info, (struct vmm_mem_hook *)(reg->host_addr));
113 void init_shadow_map(struct guest_info * info) {
114 struct shadow_map * map = &(info->mem_map);
116 map->num_regions = 0;
122 void free_shadow_map(struct shadow_map * map) {
123 struct shadow_region * cursor = map->head;
124 struct shadow_region * tmp = NULL;
128 cursor = cursor->next;
138 int add_shadow_region(struct shadow_map * map,
139 struct shadow_region * region)
141 struct shadow_region * cursor = map->head;
143 PrintDebug("Adding Shadow Region: (0x%x-0x%x)\n", region->guest_start, region->guest_end);
145 if ((!cursor) || (cursor->guest_start >= region->guest_end)) {
147 region->next = cursor;
154 // Check if it overlaps with the current cursor
155 if ((cursor->guest_end > region->guest_start) && (cursor->guest_start < region->guest_start)) {
156 // overlaps not allowed
160 if (!(cursor->next)) {
161 // add to the end of the list
162 cursor->next = region;
163 region->prev = cursor;
167 } else if (cursor->next->guest_start >= region->guest_end) {
169 region->next = cursor->next;
170 region->prev = cursor;
172 cursor->next->prev = region;
173 cursor->next = region;
178 } else if (cursor->next->guest_end <= region->guest_start) {
179 cursor = cursor->next;
181 // This cannot happen!
182 // we should panic here
187 // This cannot happen
188 // We should panic here
193 int delete_shadow_region(struct shadow_map * map,
201 struct shadow_region *get_shadow_region_by_index(struct shadow_map * map,
203 struct shadow_region * reg = map->head;
217 struct shadow_region * get_shadow_region_by_addr(struct shadow_map * map,
219 struct shadow_region * reg = map->head;
222 if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
224 } else if (reg->guest_start > addr) {
234 host_region_type_t get_shadow_addr_type(struct guest_info * info, addr_t guest_addr) {
235 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
238 return HOST_REGION_INVALID;
240 return reg->host_type;
244 addr_t get_shadow_addr(struct guest_info * info, addr_t guest_addr) {
245 struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr);
250 return (guest_addr - reg->guest_start) + reg->host_addr;
255 host_region_type_t lookup_shadow_map_addr(struct shadow_map * map, addr_t guest_addr, addr_t * host_addr) {
256 struct shadow_region * reg = get_shadow_region_by_addr(map, guest_addr);
260 return HOST_REGION_INVALID;
262 switch (reg->host_type) {
263 case HOST_REGION_PHYSICAL_MEMORY:
264 *host_addr = (guest_addr - reg->guest_start) + reg->host_addr;
265 return reg->host_type;
266 case HOST_REGION_MEMORY_MAPPED_DEVICE:
267 case HOST_REGION_UNALLOCATED:
271 return reg->host_type;
277 void print_shadow_map(struct shadow_map * map) {
278 struct shadow_region * cur = map->head;
281 PrintDebug("Memory Layout (regions: %d) \n", map->num_regions);
284 PrintDebug("%d: 0x%x - 0x%x (%s) -> ", i, cur->guest_start, cur->guest_end - 1,
285 cur->guest_type == GUEST_REGION_PHYSICAL_MEMORY ? "GUEST_REGION_PHYSICAL_MEMORY" :
286 cur->guest_type == GUEST_REGION_NOTHING ? "GUEST_REGION_NOTHING" :
287 cur->guest_type == GUEST_REGION_MEMORY_MAPPED_DEVICE ? "GUEST_REGION_MEMORY_MAPPED_DEVICE" :
289 if (cur->host_type == HOST_REGION_PHYSICAL_MEMORY ||
290 cur->host_type == HOST_REGION_UNALLOCATED ||
291 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) {
292 PrintDebug("0x%x", cur->host_addr);
295 cur->host_type == HOST_REGION_PHYSICAL_MEMORY ? "HOST_REGION_PHYSICAL_MEMORY" :
296 cur->host_type == HOST_REGION_UNALLOCATED ? "HOST_REGION_UNALLOACTED" :
297 cur->host_type == HOST_REGION_HOOK ? "HOST_REGION_HOOK" :
298 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE ? "HOST_REGION_MEMORY_MAPPED_DEVICE" :
299 cur->host_type == HOST_REGION_REMOTE ? "HOST_REGION_REMOTE" :
300 cur->host_type == HOST_REGION_SWAPPED ? "HOST_REGION_SWAPPED" :
327 struct vmm_os_hooks * os_hooks;
329 void * TestMalloc(uint_t size) {
333 void * TestAllocatePages(int size) {
334 return malloc(4096 * size);
338 void TestPrint(const char * fmt, ...) {
346 int mem_list_add_test_1( vmm_mem_list_t * list) {
350 PrintDebug("\n\nTesting Memory List\n");
354 offset = PAGE_SIZE * 6;
355 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 10));
356 add_mem_list_pages(list, offset, 10);
357 print_mem_list(list);
361 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + PAGE_SIZE * 4);
362 add_mem_list_pages(list, offset, 4);
363 print_mem_list(list);
365 offset = PAGE_SIZE * 20;
366 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
367 add_mem_list_pages(list, offset, 1);
368 print_mem_list(list);
370 offset = PAGE_SIZE * 21;
371 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 3));
372 add_mem_list_pages(list, offset, 3);
373 print_mem_list(list);
376 offset = PAGE_SIZE * 10;
377 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 30));
378 add_mem_list_pages(list, offset, 30);
379 print_mem_list(list);
382 offset = PAGE_SIZE * 5;
383 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
384 add_mem_list_pages(list, offset, 1);
385 print_mem_list(list);
393 int mem_layout_add_test_1(vmm_mem_layout_t * layout) {
399 PrintDebug("\n\nTesting Memory Layout\n");
401 init_mem_layout(layout);
405 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
406 add_guest_mem_range(layout, start, end);
407 print_mem_layout(layout);
412 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
413 add_guest_mem_range(layout, start, end);
414 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);
431 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
432 add_guest_mem_range(layout, start, end);
433 print_mem_layout(layout);
443 int main(int argc, char ** argv) {
444 struct vmm_os_hooks dummy_hooks;
445 os_hooks = &dummy_hooks;
447 vmm_mem_layout_t layout;
450 os_hooks->malloc = &TestMalloc;
451 os_hooks->free = &free;
452 os_hooks->print_debug = &TestPrint;
453 os_hooks->allocate_pages = &TestAllocatePages;
457 printf("mem_list_add_test_1: %d\n", mem_list_add_test_1(&list));
458 printf("layout_add_test_1: %d\n", mem_layout_add_test_1(&layout));