1 #include <palacios/vmm_mem.h>
2 #include <palacios/vmm.h>
3 #include <palacios/vmm_util.h>
5 extern struct vmm_os_hooks * os_hooks;
8 void init_shadow_region(shadow_region_t * 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;
18 entry->next=entry->prev = NULL;
21 void init_shadow_region_physical(shadow_region_t * entry,
22 addr_t guest_addr_start,
23 addr_t guest_addr_end,
24 guest_region_type_t guest_region_type,
25 addr_t host_addr_start,
26 host_region_type_t host_region_type)
28 init_shadow_region(entry, guest_addr_start, guest_addr_end, guest_region_type, host_region_type);
29 entry->host_addr.phys_addr.host_start = host_addr_start;
34 void init_shadow_map(shadow_map_t * map) {
41 void free_shadow_map(shadow_map_t * map) {
42 shadow_region_t * cursor = map->head;
43 shadow_region_t * tmp = NULL;
47 cursor = cursor->next;
57 int add_shadow_region(shadow_map_t * map,
58 shadow_region_t * region)
60 shadow_region_t * cursor = map->head;
62 if ((!cursor) || (cursor->guest_start >= region->guest_end)) {
64 region->next = cursor;
71 // Check if it overlaps with the current cursor
72 if ((cursor->guest_end > region->guest_start) && (cursor->guest_start < region->guest_start)) {
73 // overlaps not allowed
77 if (!(cursor->next)) {
78 // add to the end of the list
79 cursor->next = region;
80 region->prev = cursor;
84 } else if (cursor->next->guest_start >= region->guest_end) {
86 region->next = cursor->next;
87 region->prev = cursor;
89 cursor->next->prev = region;
90 cursor->next = region;
95 } else if (cursor->next->guest_end < region->guest_start) {
96 cursor = cursor->next;
98 // This cannot happen!
99 // we should panic here
104 // This cannot happen
105 // We should panic here
110 int delete_shadow_region(shadow_map_t * map,
118 shadow_region_t *get_shadow_region_by_index(shadow_map_t * map,
120 shadow_region_t * reg = map->head;
134 shadow_region_t * get_shadow_region_by_addr(shadow_map_t * map,
136 shadow_region_t * reg = map->head;
139 if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
141 } else if (reg->guest_start > addr) {
152 host_region_type_t lookup_shadow_map_addr(shadow_map_t * map, addr_t guest_addr, addr_t * host_addr) {
153 shadow_region_t * reg = get_shadow_region_by_addr(map, guest_addr);
157 return HOST_REGION_INVALID;
159 switch (reg->host_type) {
160 case HOST_REGION_PHYSICAL_MEMORY:
161 *host_addr = (guest_addr - reg->guest_start) + reg->host_addr.phys_addr.host_start;
162 return reg->host_type;
163 case HOST_REGION_MEMORY_MAPPED_DEVICE:
164 case HOST_REGION_UNALLOCATED:
168 return reg->host_type;
174 void print_shadow_map(shadow_map_t * map) {
175 shadow_region_t * cur = map->head;
178 PrintDebug("Memory Layout (regions: %d) \n", map->num_regions);
181 PrintDebug("%d: 0x%x - 0x%x (%s) -> ", i, cur->guest_start, cur->guest_end - 1,
182 cur->guest_type == GUEST_REGION_PHYSICAL_MEMORY ? "GUEST_REGION_PHYSICAL_MEMORY" :
183 cur->guest_type == GUEST_REGION_NOTHING ? "GUEST_REGION_NOTHING" :
184 cur->guest_type == GUEST_REGION_MEMORY_MAPPED_DEVICE ? "GUEST_REGION_MEMORY_MAPPED_DEVICE" :
186 if (cur->host_type == HOST_REGION_PHYSICAL_MEMORY ||
187 cur->host_type == HOST_REGION_UNALLOCATED ||
188 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) {
189 PrintDebug("0x%x", cur->host_addr.phys_addr.host_start);
192 cur->host_type == HOST_REGION_PHYSICAL_MEMORY ? "HOST_REGION_PHYSICAL_MEMORY" :
193 cur->host_type == HOST_REGION_UNALLOCATED ? "HOST_REGION_UNALLOACTED" :
194 cur->host_type == HOST_REGION_NOTHING ? "HOST_REGION_NOTHING" :
195 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE ? "HOST_REGION_MEMORY_MAPPED_DEVICE" :
196 cur->host_type == HOST_REGION_REMOTE ? "HOST_REGION_REMOTE" :
197 cur->host_type == HOST_REGION_SWAPPED ? "HOST_REGION_SWAPPED" :
224 struct vmm_os_hooks * os_hooks;
226 void * TestMalloc(uint_t size) {
230 void * TestAllocatePages(int size) {
231 return malloc(4096 * size);
235 void TestPrint(const char * fmt, ...) {
243 int mem_list_add_test_1( vmm_mem_list_t * list) {
247 PrintDebug("\n\nTesting Memory List\n");
251 offset = PAGE_SIZE * 6;
252 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 10));
253 add_mem_list_pages(list, offset, 10);
254 print_mem_list(list);
258 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + PAGE_SIZE * 4);
259 add_mem_list_pages(list, offset, 4);
260 print_mem_list(list);
262 offset = PAGE_SIZE * 20;
263 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
264 add_mem_list_pages(list, offset, 1);
265 print_mem_list(list);
267 offset = PAGE_SIZE * 21;
268 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 3));
269 add_mem_list_pages(list, offset, 3);
270 print_mem_list(list);
273 offset = PAGE_SIZE * 10;
274 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 30));
275 add_mem_list_pages(list, offset, 30);
276 print_mem_list(list);
279 offset = PAGE_SIZE * 5;
280 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
281 add_mem_list_pages(list, offset, 1);
282 print_mem_list(list);
290 int mem_layout_add_test_1(vmm_mem_layout_t * layout) {
296 PrintDebug("\n\nTesting Memory Layout\n");
298 init_mem_layout(layout);
302 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
303 add_guest_mem_range(layout, start, end);
304 print_mem_layout(layout);
309 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
310 add_guest_mem_range(layout, start, end);
311 print_mem_layout(layout);
315 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
316 add_guest_mem_range(layout, start, end);
317 print_mem_layout(layout);
321 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
322 add_guest_mem_range(layout, start, end);
323 print_mem_layout(layout);
328 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
329 add_guest_mem_range(layout, start, end);
330 print_mem_layout(layout);
340 int main(int argc, char ** argv) {
341 struct vmm_os_hooks dummy_hooks;
342 os_hooks = &dummy_hooks;
344 vmm_mem_layout_t layout;
347 os_hooks->malloc = &TestMalloc;
348 os_hooks->free = &free;
349 os_hooks->print_debug = &TestPrint;
350 os_hooks->allocate_pages = &TestAllocatePages;
354 printf("mem_list_add_test_1: %d\n", mem_list_add_test_1(&list));
355 printf("layout_add_test_1: %d\n", mem_layout_add_test_1(&layout));