2 * This file is part of the Palacios Virtual Machine Monitor developed
3 * by the V3VEE Project with funding from the United States National
4 * Science Foundation and the Department of Energy.
6 * The V3VEE Project is a joint project between Northwestern University
7 * and the University of New Mexico. You can find out more at
10 * Copyright (c) 2008, Jack Lange <jarusl@cs.northwestern.edu>
11 * Copyright (c) 2008, The V3VEE Project <http://www.v3vee.org>
12 * All rights reserved.
14 * Author: Jack Lange <jarusl@cs.northwestern.edu>
16 * This is free software. You are permitted to use,
17 * redistribute, and modify it as specified in the file "V3VEE_LICENSE".
20 #include <palacios/vmm_mem.h>
21 #include <palacios/vmm.h>
22 #include <palacios/vmm_util.h>
23 #include <palacios/vmm_emulator.h>
24 #include <palacios/vm_guest.h>
26 #include <palacios/vmm_shadow_paging.h>
27 #include <palacios/vmm_direct_paging.h>
32 static int mem_offset_hypercall(struct guest_info * info, uint_t hcall_id, void * private_data) {
33 PrintDebug("V3Vee: Memory offset hypercall (offset=%p)\n",
34 (void *)(info->vm_info->mem_map.base_region.host_addr));
36 info->vm_regs.rbx = info->vm_info->mem_map.base_region.host_addr;
41 static int unhandled_err(struct guest_info * core, addr_t guest_va, addr_t guest_pa,
42 struct v3_mem_region * reg, pf_error_t access_info) {
44 PrintError("Unhandled memory access error\n");
46 v3_print_mem_map(core->vm_info);
48 v3_print_guest_state(core);
55 int v3_init_mem_map(struct v3_vm_info * vm) {
56 struct v3_mem_map * map = &(vm->mem_map);
57 addr_t mem_pages = vm->mem_size >> 12;
59 memset(&(map->base_region), 0, sizeof(struct v3_mem_region));
61 map->mem_regions.rb_node = NULL;
64 // There is an underlying region that contains all of the guest memory
65 // PrintDebug("Mapping %d pages of memory (%u bytes)\n", (int)mem_pages, (uint_t)info->mem_size);
67 map->base_region.guest_start = 0;
68 map->base_region.guest_end = mem_pages * PAGE_SIZE_4KB;
69 map->base_region.host_addr = (addr_t)V3_AllocPages(mem_pages);
71 map->base_region.flags.read = 1;
72 map->base_region.flags.write = 1;
73 map->base_region.flags.exec = 1;
74 map->base_region.flags.base = 1;
75 map->base_region.flags.alloced = 1;
77 map->base_region.unhandled = unhandled_err;
79 if ((void *)map->base_region.host_addr == NULL) {
80 PrintError("Could not allocate Guest memory\n");
84 //memset(V3_VAddr((void *)map->base_region.host_addr), 0xffffffff, map->base_region.guest_end);
86 v3_register_hypercall(vm, MEM_OFFSET_HCALL, mem_offset_hypercall, NULL);
92 void v3_delete_mem_map(struct v3_vm_info * vm) {
93 struct rb_node * node = v3_rb_first(&(vm->mem_map.mem_regions));
94 struct v3_mem_region * reg;
95 struct rb_node * tmp_node = NULL;
98 reg = rb_entry(node, struct v3_mem_region, tree_node);
100 node = v3_rb_next(node);
102 v3_delete_mem_region(vm, reg);
105 V3_FreePage((void *)(vm->mem_map.base_region.host_addr));
109 struct v3_mem_region * v3_create_mem_region(struct v3_vm_info * vm, uint16_t core_id,
110 addr_t guest_addr_start, addr_t guest_addr_end) {
112 struct v3_mem_region * entry = (struct v3_mem_region *)V3_Malloc(sizeof(struct v3_mem_region));
113 memset(entry, 0, sizeof(struct v3_mem_region));
115 entry->guest_start = guest_addr_start;
116 entry->guest_end = guest_addr_end;
117 entry->core_id = core_id;
118 entry->unhandled = unhandled_err;
126 int v3_add_shadow_mem( struct v3_vm_info * vm, uint16_t core_id,
127 addr_t guest_addr_start,
128 addr_t guest_addr_end,
131 struct v3_mem_region * entry = NULL;
133 entry = v3_create_mem_region(vm, core_id,
137 entry->host_addr = host_addr;
140 entry->flags.read = 1;
141 entry->flags.write = 1;
142 entry->flags.exec = 1;
143 entry->flags.alloced = 1;
145 if (v3_insert_mem_region(vm, entry) == -1) {
156 struct v3_mem_region * __insert_mem_region(struct v3_vm_info * vm,
157 struct v3_mem_region * region) {
158 struct rb_node ** p = &(vm->mem_map.mem_regions.rb_node);
159 struct rb_node * parent = NULL;
160 struct v3_mem_region * tmp_region;
164 tmp_region = rb_entry(parent, struct v3_mem_region, tree_node);
166 if (region->guest_end <= tmp_region->guest_start) {
168 } else if (region->guest_start >= tmp_region->guest_end) {
171 if ((region->guest_end != tmp_region->guest_end) ||
172 (region->guest_start != tmp_region->guest_start)) {
173 PrintError("Trying to map a partial overlapped core specific page...\n");
174 return tmp_region; // This is ugly...
175 } else if (region->core_id == tmp_region->core_id) {
177 } else if (region->core_id < tmp_region->core_id) {
185 rb_link_node(&(region->tree_node), parent, p);
192 int v3_insert_mem_region(struct v3_vm_info * vm,
193 struct v3_mem_region * region) {
194 struct v3_mem_region * ret;
197 if ((ret = __insert_mem_region(vm, region))) {
201 v3_rb_insert_color(&(region->tree_node), &(vm->mem_map.mem_regions));
205 for (i = 0; i < vm->num_cores; i++) {
206 struct guest_info * info = &(vm->cores[i]);
208 // flush virtual page tables
209 // 3 cases shadow, shadow passthrough, and nested
211 if (info->shdw_pg_mode == SHADOW_PAGING) {
212 v3_mem_mode_t mem_mode = v3_get_vm_mem_mode(info);
214 if (mem_mode == PHYSICAL_MEM) {
217 for (cur_addr = region->guest_start;
218 cur_addr < region->guest_end;
219 cur_addr += PAGE_SIZE_4KB) {
220 v3_invalidate_passthrough_addr(info, cur_addr);
223 v3_invalidate_shadow_pts(info);
226 } else if (info->shdw_pg_mode == NESTED_PAGING) {
229 for (cur_addr = region->guest_start;
230 cur_addr < region->guest_end;
231 cur_addr += PAGE_SIZE_4KB) {
233 v3_invalidate_nested_addr(info, cur_addr);
244 struct v3_mem_region * v3_get_mem_region(struct v3_vm_info * vm, uint16_t core_id, addr_t guest_addr) {
245 struct rb_node * n = vm->mem_map.mem_regions.rb_node;
246 struct v3_mem_region * reg = NULL;
249 reg = rb_entry(n, struct v3_mem_region, tree_node);
251 if (guest_addr < reg->guest_start) {
253 } else if (guest_addr >= reg->guest_end) {
256 if ((core_id == reg->core_id) ||
257 (reg->core_id == V3_MEM_CORE_ANY)) {
266 // There is not registered region, so we check if its a valid address in the base region
268 if (guest_addr > vm->mem_map.base_region.guest_end) {
269 PrintError("Guest Address Exceeds Base Memory Size (ga=%p), (limit=%p)\n",
270 (void *)guest_addr, (void *)vm->mem_map.base_region.guest_end);
271 v3_print_mem_map(vm);
276 return &(vm->mem_map.base_region);
282 void v3_delete_mem_region(struct v3_vm_info * vm, struct v3_mem_region * reg) {
289 for (i = 0; i < vm->num_cores; i++) {
290 struct guest_info * info = &(vm->cores[i]);
292 // flush virtual page tables
293 // 3 cases shadow, shadow passthrough, and nested
295 if (info->shdw_pg_mode == SHADOW_PAGING) {
296 v3_mem_mode_t mem_mode = v3_get_vm_mem_mode(info);
298 if (mem_mode == PHYSICAL_MEM) {
301 for (cur_addr = reg->guest_start;
302 cur_addr < reg->guest_end;
303 cur_addr += PAGE_SIZE_4KB) {
304 v3_invalidate_passthrough_addr(info, cur_addr);
307 v3_invalidate_shadow_pts(info);
310 } else if (info->shdw_pg_mode == NESTED_PAGING) {
313 for (cur_addr = reg->guest_start;
314 cur_addr < reg->guest_end;
315 cur_addr += PAGE_SIZE_4KB) {
317 v3_invalidate_nested_addr(info, cur_addr);
322 v3_rb_erase(&(reg->tree_node), &(vm->mem_map.mem_regions));
326 // flush virtual page tables
327 // 3 cases shadow, shadow passthrough, and nested
333 void v3_print_mem_map(struct v3_vm_info * vm) {
334 struct rb_node * node = v3_rb_first(&(vm->mem_map.mem_regions));
335 struct v3_mem_region * reg = &(vm->mem_map.base_region);
338 V3_Print("Memory Layout:\n");
341 V3_Print("Base Region: 0x%p - 0x%p -> 0x%p\n",
342 (void *)(reg->guest_start),
343 (void *)(reg->guest_end - 1),
344 (void *)(reg->host_addr));
347 // If the memory map is empty, don't print it
353 reg = rb_entry(node, struct v3_mem_region, tree_node);
355 V3_Print("%d: 0x%p - 0x%p -> 0x%p\n", i,
356 (void *)(reg->guest_start),
357 (void *)(reg->guest_end - 1),
358 (void *)(reg->host_addr));
360 V3_Print("\t(flags=%x) (unhandled = 0x%p)\n",
365 } while ((node = v3_rb_next(node)));