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_decoder.h>
32 struct v3_shadow_region * insert_shadow_region(struct guest_info * info,
33 struct v3_shadow_region * region);
37 void v3_init_shadow_map(struct guest_info * info) {
38 info->mem_map.rb_node = NULL;
41 void v3_delete_shadow_map(struct guest_info * info) {
42 struct rb_node * node = v3_rb_first(&(info->mem_map));
43 struct v3_shadow_region * reg;
44 struct rb_node * tmp_node = NULL;
47 reg = rb_entry(node, struct v3_shadow_region, tree_node);
49 node = v3_rb_next(node);
51 v3_delete_shadow_region(info, reg);
58 int v3_add_shadow_mem( struct guest_info * info,
59 addr_t guest_addr_start,
60 addr_t guest_addr_end,
63 struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
65 entry->guest_start = guest_addr_start;
66 entry->guest_end = guest_addr_end;
67 entry->host_type = SHDW_REGION_ALLOCATED;
68 entry->host_addr = host_addr;
69 entry->write_hook = NULL;
70 entry->read_hook = NULL;
71 entry->priv_data = NULL;
73 if (insert_shadow_region(info, entry)) {
83 int v3_hook_write_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
85 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
88 struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
91 entry->guest_start = guest_addr_start;
92 entry->guest_end = guest_addr_end;
93 entry->host_type = SHDW_REGION_WRITE_HOOK;
94 entry->host_addr = host_addr;
95 entry->write_hook = write;
96 entry->read_hook = NULL;
97 entry->priv_data = priv_data;
99 if (insert_shadow_region(info, entry)) {
107 int v3_hook_full_mem(struct guest_info * info, addr_t guest_addr_start, addr_t guest_addr_end,
108 int (*read)(addr_t guest_addr, void * dst, uint_t length, void * priv_data),
109 int (*write)(addr_t guest_addr, void * src, uint_t length, void * priv_data),
112 struct v3_shadow_region * entry = (struct v3_shadow_region *)V3_Malloc(sizeof(struct v3_shadow_region));
114 entry->guest_start = guest_addr_start;
115 entry->guest_end = guest_addr_end;
116 entry->host_type = SHDW_REGION_FULL_HOOK;
117 entry->host_addr = (addr_t)NULL;
118 entry->write_hook = write;
119 entry->read_hook = read;
120 entry->priv_data = priv_data;
122 if (insert_shadow_region(info, entry)) {
134 struct v3_shadow_region * __insert_shadow_region(struct guest_info * info,
135 struct v3_shadow_region * region) {
136 struct rb_node ** p = &(info->mem_map.rb_node);
137 struct rb_node * parent = NULL;
138 struct v3_shadow_region * tmp_region;
142 tmp_region = rb_entry(parent, struct v3_shadow_region, tree_node);
144 if (region->guest_end <= tmp_region->guest_start) {
146 } else if (region->guest_start >= tmp_region->guest_end) {
153 rb_link_node(&(region->tree_node), parent, p);
160 struct v3_shadow_region * insert_shadow_region(struct guest_info * info,
161 struct v3_shadow_region * region) {
162 struct v3_shadow_region * ret;
164 if ((ret = __insert_shadow_region(info, region))) {
168 v3_rb_insert_color(&(region->tree_node), &(info->mem_map));
180 int handle_special_page_fault(struct guest_info * info,
181 addr_t fault_gva, addr_t fault_gpa,
182 pf_error_t access_info)
184 struct v3_shadow_region * reg = v3_get_shadow_region(info, fault_gpa);
186 PrintDebug("Handling Special Page Fault\n");
188 switch (reg->host_type) {
189 case SHDW_REGION_WRITE_HOOK:
190 return v3_handle_mem_wr_hook(info, fault_gva, fault_gpa, reg, access_info);
191 case SHDW_REGION_FULL_HOOK:
192 return v3_handle_mem_full_hook(info, fault_gva, fault_gpa, reg, access_info);
201 int v3_handle_mem_wr_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa,
202 struct v3_shadow_region * reg, pf_error_t access_info) {
204 addr_t write_src_addr = 0;
206 int write_len = v3_emulate_write_op(info, guest_va, guest_pa, &write_src_addr);
208 if (write_len == -1) {
209 PrintError("Emulation failure in write hook\n");
214 if (reg->write_hook(guest_pa, (void *)write_src_addr, write_len, reg->priv_data) != write_len) {
215 PrintError("Memory write hook did not return correct value\n");
222 int v3_handle_mem_full_hook(struct guest_info * info, addr_t guest_va, addr_t guest_pa,
223 struct v3_shadow_region * reg, pf_error_t access_info) {
229 struct v3_shadow_region * v3_get_shadow_region(struct guest_info * info, addr_t guest_addr) {
230 struct rb_node * n = info->mem_map.rb_node;
231 struct v3_shadow_region * reg = NULL;
234 reg = rb_entry(n, struct v3_shadow_region, tree_node);
236 if (guest_addr < reg->guest_start) {
238 } else if (guest_addr >= reg->guest_end) {
250 addr_t v3_get_shadow_addr(struct v3_shadow_region * reg, addr_t guest_addr) {
252 (reg->host_type != SHDW_REGION_FULL_HOOK) &&
253 (reg->host_type != SHDW_REGION_INVALID)) {
254 return (guest_addr - reg->guest_start) + reg->host_addr;
256 PrintError("MEM Region Invalid\n");
263 void v3_delete_shadow_region(struct guest_info * info, struct v3_shadow_region * reg) {
265 v3_rb_erase(&(reg->tree_node), &(info->mem_map));
274 void print_shadow_map(struct guest_info * info) {
275 struct rb_node * node = v3_rb_first(&(info->mem_map));
276 struct v3_shadow_region * reg;
279 PrintDebug("Memory Layout:\n");
282 reg = rb_entry(node, struct v3_shadow_region, tree_node);
284 PrintDebug("%d: 0x%p - 0x%p -> 0x%p\n", i,
285 (void *)(reg->guest_start),
286 (void *)(reg->guest_end - 1),
287 (void *)(reg->host_addr));
289 PrintDebug("\t(%s) (WriteHook = 0x%p) (ReadHook = 0x%p)\n",
290 v3_shdw_region_type_to_str(reg->host_type),
291 (void *)(reg->write_hook),
292 (void *)(reg->read_hook));
295 } while ((node = v3_rb_next(node)));
299 static const uchar_t SHDW_REGION_INVALID_STR[] = "SHDW_REGION_INVALID";
300 static const uchar_t SHDW_REGION_WRITE_HOOK_STR[] = "SHDW_REGION_WRITE_HOOK";
301 static const uchar_t SHDW_REGION_FULL_HOOK_STR[] = "SHDW_REGION_FULL_HOOK";
302 static const uchar_t SHDW_REGION_ALLOCATED_STR[] = "SHDW_REGION_ALLOCATED";
306 const uchar_t * v3_shdw_region_type_to_str(v3_shdw_region_type_t type) {
308 case SHDW_REGION_WRITE_HOOK:
309 return SHDW_REGION_WRITE_HOOK_STR;
310 case SHDW_REGION_FULL_HOOK:
311 return SHDW_REGION_FULL_HOOK_STR;
312 case SHDW_REGION_ALLOCATED:
313 return SHDW_REGION_ALLOCATED_STR;
315 return SHDW_REGION_INVALID_STR;