1 #include <geekos/vmm_mem.h>
2 #include <geekos/vmm.h>
3 #include <geekos/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;
56 /* This is slightly different semantically from the mem list, in that
57 * we don't allow overlaps we could probably allow overlappig regions
58 * of the same type... but I'll let someone else deal with that
60 int add_shadow_region(shadow_map_t * map,
61 shadow_region_t * region)
63 shadow_region_t * cursor = map->head;
65 if ((!cursor) || (cursor->guest_start >= region->guest_end)) {
67 region->next = cursor;
74 // Check if it overlaps with the current cursor
75 if ((cursor->guest_end > region->guest_start) && (cursor->guest_start < region->guest_start)) {
76 // overlaps not allowed
80 if (!(cursor->next)) {
81 // add to the end of the list
82 cursor->next = region;
83 region->prev = cursor;
87 } else if (cursor->next->guest_start >= region->guest_end) {
89 region->next = cursor->next;
90 region->prev = cursor;
92 cursor->next->prev = region;
93 cursor->next = region;
98 } else if (cursor->next->guest_end < region->guest_start) {
99 cursor = cursor->next;
101 // This cannot happen!
102 // we should panic here
107 // This cannot happen
108 // We should panic here
113 int delete_shadow_region(shadow_map_t * map,
121 shadow_region_t *get_shadow_region_by_index(shadow_map_t * map,
123 shadow_region_t * reg = map->head;
137 shadow_region_t * get_shadow_region_by_addr(shadow_map_t * map,
139 shadow_region_t * reg = map->head;
142 if ((reg->guest_start <= addr) && (reg->guest_end > addr)) {
144 } else if (reg->guest_start > addr) {
155 int guest_paddr_to_host_paddr(shadow_region_t * entry,
157 addr_t * host_addr) {
159 if (!((guest_addr >= entry->guest_start) &&
160 (guest_addr < entry->guest_end))) {
164 switch (entry->host_type) {
165 case HOST_REGION_PHYSICAL_MEMORY:
166 case HOST_REGION_MEMORY_MAPPED_DEVICE:
167 case HOST_REGION_UNALLOCATED:
168 *host_addr = (guest_addr-entry->guest_start) + entry->host_addr.phys_addr.host_start;
178 void print_shadow_map(shadow_map_t * map) {
179 shadow_region_t * cur = map->head;
182 PrintDebug("Memory Layout (regions: %d) \n", map->num_regions);
185 PrintDebug("%d: 0x%x - 0x%x (%s) -> ", i, cur->guest_start, cur->guest_end - 1,
186 cur->guest_type == GUEST_REGION_PHYSICAL_MEMORY ? "GUEST_REGION_PHYSICAL_MEMORY" :
187 cur->guest_type == GUEST_REGION_NOTHING ? "GUEST_REGION_NOTHING" :
188 cur->guest_type == GUEST_REGION_MEMORY_MAPPED_DEVICE ? "GUEST_REGION_MEMORY_MAPPED_DEVICE" :
190 if (cur->host_type == HOST_REGION_PHYSICAL_MEMORY ||
191 cur->host_type == HOST_REGION_UNALLOCATED ||
192 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE) {
193 PrintDebug("0x%x", cur->host_addr.phys_addr.host_start);
196 cur->host_type == HOST_REGION_PHYSICAL_MEMORY ? "HOST_REGION_PHYSICAL_MEMORY" :
197 cur->host_type == HOST_REGION_UNALLOCATED ? "HOST_REGION_UNALLOACTED" :
198 cur->host_type == HOST_REGION_NOTHING ? "HOST_REGION_NOTHING" :
199 cur->host_type == HOST_REGION_MEMORY_MAPPED_DEVICE ? "HOST_REGION_MEMORY_MAPPED_DEVICE" :
200 cur->host_type == HOST_REGION_REMOTE ? "HOST_REGION_REMOTE" :
201 cur->host_type == HOST_REGION_SWAPPED ? "HOST_REGION_SWAPPED" :
228 struct vmm_os_hooks * os_hooks;
230 void * TestMalloc(uint_t size) {
234 void * TestAllocatePages(int size) {
235 return malloc(4096 * size);
239 void TestPrint(const char * fmt, ...) {
247 int mem_list_add_test_1( vmm_mem_list_t * list) {
251 PrintDebug("\n\nTesting Memory List\n");
255 offset = PAGE_SIZE * 6;
256 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 10));
257 add_mem_list_pages(list, offset, 10);
258 print_mem_list(list);
262 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + PAGE_SIZE * 4);
263 add_mem_list_pages(list, offset, 4);
264 print_mem_list(list);
266 offset = PAGE_SIZE * 20;
267 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
268 add_mem_list_pages(list, offset, 1);
269 print_mem_list(list);
271 offset = PAGE_SIZE * 21;
272 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 3));
273 add_mem_list_pages(list, offset, 3);
274 print_mem_list(list);
277 offset = PAGE_SIZE * 10;
278 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 30));
279 add_mem_list_pages(list, offset, 30);
280 print_mem_list(list);
283 offset = PAGE_SIZE * 5;
284 PrintDebug("Adding 0x%x - 0x%x\n", offset, offset + (PAGE_SIZE * 1));
285 add_mem_list_pages(list, offset, 1);
286 print_mem_list(list);
294 int mem_layout_add_test_1(vmm_mem_layout_t * layout) {
300 PrintDebug("\n\nTesting Memory Layout\n");
302 init_mem_layout(layout);
306 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
307 add_guest_mem_range(layout, start, end);
308 print_mem_layout(layout);
313 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
314 add_guest_mem_range(layout, start, end);
315 print_mem_layout(layout);
319 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
320 add_guest_mem_range(layout, start, end);
321 print_mem_layout(layout);
325 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
326 add_guest_mem_range(layout, start, end);
327 print_mem_layout(layout);
332 PrintDebug("Adding 0x%x - 0x%x\n", start, end);
333 add_guest_mem_range(layout, start, end);
334 print_mem_layout(layout);
344 int main(int argc, char ** argv) {
345 struct vmm_os_hooks dummy_hooks;
346 os_hooks = &dummy_hooks;
348 vmm_mem_layout_t layout;
351 os_hooks->malloc = &TestMalloc;
352 os_hooks->free = &free;
353 os_hooks->print_debug = &TestPrint;
354 os_hooks->allocate_pages = &TestAllocatePages;
358 printf("mem_list_add_test_1: %d\n", mem_list_add_test_1(&list));
359 printf("layout_add_test_1: %d\n", mem_layout_add_test_1(&layout));