X-Git-Url: http://v3vee.org/palacios/gitweb/gitweb.cgi?a=blobdiff_plain;f=palacios%2Fsrc%2Fpalacios%2Fvmm_mem.c;h=fa17e9761cd5791daa2936082e0c24cb69cb773a;hb=da0f0deecf22754656bad2a95640461ec3ac4f1d;hp=c4747ee986b2a217701a6099ae195e8413545107;hpb=b527f44a71d32952d7b129a7ce5dbeb3969fb8d2;p=palacios.git diff --git a/palacios/src/palacios/vmm_mem.c b/palacios/src/palacios/vmm_mem.c index c4747ee..fa17e97 100644 --- a/palacios/src/palacios/vmm_mem.c +++ b/palacios/src/palacios/vmm_mem.c @@ -1,22 +1,39 @@ +/* + * This file is part of the Palacios Virtual Machine Monitor developed + * by the V3VEE Project with funding from the United States National + * Science Foundation and the Department of Energy. + * + * The V3VEE Project is a joint project between Northwestern University + * and the University of New Mexico. You can find out more at + * http://www.v3vee.org + * + * Copyright (c) 2008, Jack Lange + * Copyright (c) 2008, The V3VEE Project + * All rights reserved. + * + * Author: Jack Lange + * + * This is free software. You are permitted to use, + * redistribute, and modify it as specified in the file "V3VEE_LICENSE". + */ + #include #include #include -#include +#include void init_shadow_region(struct shadow_region * entry, addr_t guest_addr_start, addr_t guest_addr_end, - guest_region_type_t guest_region_type, - host_region_type_t host_region_type) + shdw_region_type_t shdw_region_type) { - entry->guest_type = guest_region_type; entry->guest_start = guest_addr_start; entry->guest_end = guest_addr_end; - entry->host_type = host_region_type; + entry->host_type = shdw_region_type; entry->host_addr = 0; - entry->next=entry->prev = NULL; + entry->next = entry->prev = NULL; } int add_shadow_region_passthrough( struct guest_info * guest_info, @@ -27,86 +44,120 @@ int add_shadow_region_passthrough( struct guest_info * guest_info, struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region)); init_shadow_region(entry, guest_addr_start, guest_addr_end, - GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_PHYSICAL_MEMORY); + SHDW_REGION_ALLOCATED); entry->host_addr = host_addr; return add_shadow_region(&(guest_info->mem_map), entry); } -int hook_guest_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end, - int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data), - int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data), - void * priv_data) { - +int v3_hook_write_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end, + addr_t host_addr, + int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data), + void * priv_data) { + struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region)); - struct vmm_mem_hook * hook = (struct vmm_mem_hook *)V3_Malloc(sizeof(struct vmm_mem_hook)); - memset(hook, 0, sizeof(struct vmm_mem_hook)); + init_shadow_region(entry, guest_addr_start, guest_addr_end, + SHDW_REGION_WRITE_HOOK); - hook->read = read; - hook->write = write; - hook->region = entry; - hook->priv_data = priv_data; + entry->write_hook = write; + entry->read_hook = NULL; + entry->host_addr = host_addr; + entry->priv_data = priv_data; + return add_shadow_region(&(info->mem_map), entry); +} + +int v3_hook_full_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end, + int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data), + int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data), + void * priv_data) { + + struct shadow_region * entry = (struct shadow_region *)V3_Malloc(sizeof(struct shadow_region)); init_shadow_region(entry, guest_addr_start, guest_addr_end, - GUEST_REGION_PHYSICAL_MEMORY, HOST_REGION_HOOK); + SHDW_REGION_FULL_HOOK); + + entry->write_hook = write; + entry->read_hook = read; + entry->priv_data = priv_data; - entry->host_addr = (addr_t)hook; + entry->host_addr = 0; return add_shadow_region(&(info->mem_map), entry); } -struct vmm_mem_hook * get_mem_hook(struct guest_info * info, addr_t guest_addr) { - struct shadow_region * region = get_shadow_region_by_addr(&(info->mem_map), guest_addr); - if (region == NULL) { - PrintDebug("Could not find shadow region for addr: %x\n", guest_addr); - return NULL; + +int handle_special_page_fault(struct guest_info * info, + addr_t fault_gva, addr_t fault_gpa, + pf_error_t access_info) +{ + struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), fault_gpa); + + PrintDebug("Handling Special Page Fault\n"); + + switch (reg->host_type) { + case SHDW_REGION_WRITE_HOOK: + return v3_handle_mem_wr_hook(info, fault_gva, fault_gpa, reg, access_info); + case SHDW_REGION_FULL_HOOK: + return v3_handle_mem_full_hook(info, fault_gva, fault_gpa, reg, access_info); + default: + return -1; } - return (struct vmm_mem_hook *)(region->host_addr); + return 0; + } +int v3_handle_mem_wr_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa, + struct shadow_region * reg, pf_error_t access_info) { -int mem_hook_dispatch(struct guest_info * info, addr_t mem_addr, pf_error_t access_info, struct vmm_mem_hook * hook) { + addr_t write_src_addr = 0; - if (access_info.write == 1) { - void * src = NULL; - uint_t length = 0; - PrintDebug("Memory hook write\n"); + int write_len = v3_emulate_write_op(info, guest_va, guest_pa, &write_src_addr); + + if (write_len == -1) { + PrintError("Emulation failure in write hook\n"); return -1; + } - if (hook->write(mem_addr, src, length, hook->priv_data) != length) { - return -1; - } - } else { - PrintDebug("Memory hook read\n"); + + if (reg->write_hook(guest_pa, (void *)write_src_addr, write_len, reg->priv_data) != write_len) { + PrintError("Memory write hook did not return correct value\n"); return -1; - } + } + + return 0; +} +int v3_handle_mem_full_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa, + struct shadow_region * reg, pf_error_t access_info) { return -1; } -int handle_special_page_fault(struct guest_info * info, addr_t mem_addr, pf_error_t access_info) { - struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), mem_addr); - switch (reg->host_type) { - case HOST_REGION_HOOK: - return mem_hook_dispatch(info, mem_addr, access_info, (struct vmm_mem_hook *)(reg->host_addr)); - default: - return -1; - } - - return 0; +struct shadow_region * v3_get_shadow_region(struct guest_info * info, addr_t addr) { + struct shadow_region * reg = info->mem_map.head; + while (reg) { + if ((reg->guest_start <= addr) && (reg->guest_end > addr)) { + return reg; + } else if (reg->guest_start > addr) { + return NULL; + } else { + reg = reg->next; + } + } + return NULL; } +void init_shadow_map(struct guest_info * info) { + struct shadow_map * map = &(info->mem_map); -void init_shadow_map(struct shadow_map * map) { map->num_regions = 0; map->head = NULL; @@ -134,7 +185,8 @@ int add_shadow_region(struct shadow_map * map, { struct shadow_region * cursor = map->head; - PrintDebug("Adding Shadow Region: (0x%x-0x%x)\n", region->guest_start, region->guest_end); + PrintDebug("Adding Shadow Region: (0x%p-0x%p)\n", + (void *)region->guest_start, (void *)region->guest_end); if ((!cursor) || (cursor->guest_start >= region->guest_end)) { region->prev = NULL; @@ -172,7 +224,6 @@ int add_shadow_region(struct shadow_map * map, } else if (cursor->next->guest_end <= region->guest_start) { cursor = cursor->next; } else { - PrintDebug("WTF?\n"); // This cannot happen! // we should panic here return -1; @@ -194,7 +245,7 @@ int delete_shadow_region(struct shadow_map * map, struct shadow_region *get_shadow_region_by_index(struct shadow_map * map, - uint_t index) { + uint_t index) { struct shadow_region * reg = map->head; uint_t i = 0; @@ -210,7 +261,7 @@ struct shadow_region *get_shadow_region_by_index(struct shadow_map * map, struct shadow_region * get_shadow_region_by_addr(struct shadow_map * map, - addr_t addr) { + addr_t addr) { struct shadow_region * reg = map->head; while (reg) { @@ -226,11 +277,11 @@ struct shadow_region * get_shadow_region_by_addr(struct shadow_map * map, } -host_region_type_t get_shadow_addr_type(struct guest_info * info, addr_t guest_addr) { +shdw_region_type_t get_shadow_addr_type(struct guest_info * info, addr_t guest_addr) { struct shadow_region * reg = get_shadow_region_by_addr(&(info->mem_map), guest_addr); if (!reg) { - return HOST_REGION_INVALID; + return SHDW_REGION_INVALID; } else { return reg->host_type; } @@ -247,19 +298,20 @@ addr_t get_shadow_addr(struct guest_info * info, addr_t guest_addr) { } -host_region_type_t lookup_shadow_map_addr(struct shadow_map * map, addr_t guest_addr, addr_t * host_addr) { +shdw_region_type_t lookup_shadow_map_addr(struct shadow_map * map, addr_t guest_addr, addr_t * host_addr) { struct shadow_region * reg = get_shadow_region_by_addr(map, guest_addr); if (!reg) { // No mapping exists - return HOST_REGION_INVALID; + return SHDW_REGION_INVALID; } else { switch (reg->host_type) { - case HOST_REGION_PHYSICAL_MEMORY: + case SHDW_REGION_ALLOCATED: + case SHDW_REGION_WRITE_HOOK: *host_addr = (guest_addr - reg->guest_start) + reg->host_addr; return reg->host_type; - case HOST_REGION_MEMORY_MAPPED_DEVICE: - case HOST_REGION_UNALLOCATED: + case SHDW_REGION_UNALLOCATED: + case SHDW_REGION_FULL_HOOK: // ... default: *host_addr = 0; @@ -276,31 +328,43 @@ void print_shadow_map(struct shadow_map * map) { PrintDebug("Memory Layout (regions: %d) \n", map->num_regions); while (cur) { - PrintDebug("%d: 0x%x - 0x%x (%s) -> ", i, cur->guest_start, cur->guest_end - 1, - cur->guest_type == GUEST_REGION_PHYSICAL_MEMORY ? "GUEST_REGION_PHYSICAL_MEMORY" : - cur->guest_type == GUEST_REGION_NOTHING ? "GUEST_REGION_NOTHING" : - cur->guest_type == GUEST_REGION_MEMORY_MAPPED_DEVICE ? "GUEST_REGION_MEMORY_MAPPED_DEVICE" : - "UNKNOWN"); - if (cur->host_type == HOST_REGION_PHYSICAL_MEMORY || - cur->host_type == HOST_REGION_UNALLOCATED || - cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) { - PrintDebug("0x%x", cur->host_addr); + PrintDebug("%d: 0x%p - 0x%p -> ", i, + (void *)cur->guest_start, (void *)(cur->guest_end - 1)); + if (cur->host_type == SHDW_REGION_ALLOCATED || + cur->host_type == SHDW_REGION_UNALLOCATED) { + PrintDebug("0x%p", (void *)(cur->host_addr)); } - PrintDebug("(%s)\n", - cur->host_type == HOST_REGION_PHYSICAL_MEMORY ? "HOST_REGION_PHYSICAL_MEMORY" : - cur->host_type == HOST_REGION_UNALLOCATED ? "HOST_REGION_UNALLOACTED" : - cur->host_type == HOST_REGION_HOOK ? "HOST_REGION_HOOK" : - cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE ? "HOST_REGION_MEMORY_MAPPED_DEVICE" : - cur->host_type == HOST_REGION_REMOTE ? "HOST_REGION_REMOTE" : - cur->host_type == HOST_REGION_SWAPPED ? "HOST_REGION_SWAPPED" : - "UNKNOWN"); + PrintDebug("(%s)\n", shdw_region_type_to_str(cur->host_type)); cur = cur->next; i++; } } +static const uchar_t SHDW_REGION_INVALID_STR[] = "SHDW_REGION_INVALID"; +static const uchar_t SHDW_REGION_WRITE_HOOK_STR[] = "SHDW_REGION_WRITE_HOOK"; +static const uchar_t SHDW_REGION_FULL_HOOK_STR[] = "SHDW_REGION_FULL_HOOK"; +static const uchar_t SHDW_REGION_ALLOCATED_STR[] = "SHDW_REGION_ALLOCATED"; +static const uchar_t SHDW_REGION_UNALLOCATED_STR[] = "SHDW_REGION_UNALLOCATED"; + + +const uchar_t * shdw_region_type_to_str(shdw_region_type_t type) { + switch (type) { + case SHDW_REGION_INVALID: + return SHDW_REGION_INVALID_STR; + case SHDW_REGION_WRITE_HOOK: + return SHDW_REGION_WRITE_HOOK_STR; + case SHDW_REGION_FULL_HOOK: + return SHDW_REGION_FULL_HOOK_STR; + case SHDW_REGION_ALLOCATED: + return SHDW_REGION_ALLOCATED_STR; + case SHDW_REGION_UNALLOCATED: + return SHDW_REGION_UNALLOCATED_STR; + default: + return SHDW_REGION_INVALID_STR; + } +}