fix a warning fredf pointed out on irc
[newos.git] / kernel / vm / vm_tests.c
blobe58d71b4d7a66de5eed466bbdb630c41f2be6ca5
1 /*
2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
4 */
5 #include <kernel/kernel.h>
6 #include <kernel/vm.h>
7 #include <kernel/debug.h>
8 #include <kernel/arch/cpu.h>
10 #include <string.h>
11 #include <ctype.h>
12 #include <stdio.h>
14 void vm_test()
16 // region_id region, region2, region3;
17 // addr_t region_addr;
18 // int i;
20 dprintf("vm_test: entry\n");
21 #if 1
22 dprintf("vm_test 1: creating anonymous region and writing to it\n");
24 region_id region;
25 addr_t region_addr;
27 region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "test_region", (void **)&region_addr,
28 REGION_ADDR_ANY_ADDRESS, PAGE_SIZE * 16, REGION_WIRING_LAZY, LOCK_RW|LOCK_KERNEL);
29 if(region < 0)
30 panic("vm_test 1: failed to create test region\n");
31 dprintf("region = 0x%x, addr = 0x%lx\n", region, region_addr);
33 memset((void *)region_addr, 0, PAGE_SIZE * 16);
35 dprintf("memsetted the region\n");
36 if(vm_delete_region(vm_get_kernel_aspace_id(), region) < 0)
37 panic("vm_test 1: error deleting test region\n");
38 dprintf("deleted the region\n");
40 #endif
41 #if 1
42 dprintf("vm_test 2: creating physical region and writing and reading from it\n");
44 region_id region;
45 vm_region_info info;
46 char *ptr;
47 int i;
49 region = vm_map_physical_memory(vm_get_kernel_aspace_id(), "test_physical_region", (void **)&ptr,
50 REGION_ADDR_ANY_ADDRESS, PAGE_SIZE * 16, LOCK_RW|LOCK_KERNEL, 0xb8000);
51 if(region < 0)
52 panic("vm_test 2: failed to create test region\n");
54 vm_get_region_info(region, &info);
55 dprintf("region = 0x%x, addr = %p, region->base = 0x%lx\n", region, ptr, info.base);
56 if((addr_t)ptr != info.base)
57 panic("vm_test 2: info returned about region does not match pointer returned\n");
59 for(i=0; i<64; i++) {
60 ptr[i] = 'a';
62 if(vm_delete_region(vm_get_kernel_aspace_id(), region) < 0)
63 panic("vm_test 2: error deleting test region\n");
64 dprintf("deleted the region\n");
66 #endif
67 #if 1
68 dprintf("vm_test 3: testing some functionality of vm_get_page_mapping(), vm_get/put_physical_page()\n");
70 addr_t va, pa;
71 addr_t va2;
73 vm_get_page_mapping(vm_get_kernel_aspace_id(), 0x80000000, &pa);
74 vm_get_physical_page(pa, &va, PHYSICAL_PAGE_CAN_WAIT);
75 dprintf("pa 0x%lx va 0x%lx\n", pa, va);
76 dprintf("%d\n", memcmp((void *)0x80000000, (void *)va, PAGE_SIZE));
78 vm_get_page_mapping(vm_get_kernel_aspace_id(), 0x80001000, &pa);
79 vm_get_physical_page(pa, &va2, PHYSICAL_PAGE_CAN_WAIT);
80 dprintf("pa 0x%lx va 0x%lx\n", pa, va2);
81 dprintf("%d\n", memcmp((void *)0x80001000, (void *)va2, PAGE_SIZE));
83 vm_put_physical_page(va);
84 vm_put_physical_page(va2);
86 vm_get_page_mapping(vm_get_kernel_aspace_id(), 0x80000000, &pa);
87 vm_get_physical_page(pa, &va, PHYSICAL_PAGE_CAN_WAIT);
88 dprintf("pa 0x%lx va 0x%lx\n", pa, va);
89 dprintf("%d\n", memcmp((void *)0x80000000, (void *)va, PAGE_SIZE));
91 vm_put_physical_page(va);
93 #endif
94 #if 1
95 dprintf("vm_test 4: cloning vidmem and testing if it compares\n");
97 region_id region, region2;
98 vm_region_info info;
99 void *ptr;
100 int rc;
102 region = vm_find_region_by_name(vm_get_kernel_aspace_id(), "vid_mem");
103 if(region < 0)
104 panic("vm_test 4: error finding region 'vid_mem'\n");
105 dprintf("vid_mem region = 0x%x\n", region);
107 region2 = vm_clone_region(vm_get_kernel_aspace_id(), "vid_mem2",
108 &ptr, REGION_ADDR_ANY_ADDRESS, region, REGION_NO_PRIVATE_MAP, LOCK_RW|LOCK_KERNEL);
109 if(region2 < 0)
110 panic("vm_test 4: error cloning region 'vid_mem'\n");
111 dprintf("region2 = 0x%x, ptr = %p\n", region2, ptr);
113 vm_get_region_info(region, &info);
114 rc = memcmp(ptr, (void *)info.base, info.size);
115 if(rc != 0)
116 panic("vm_test 4: regions are not identical\n");
117 else
118 dprintf("vm_test 4: comparison ok\n");
119 if(vm_delete_region(vm_get_kernel_aspace_id(), region2) < 0)
120 panic("vm_test 4: error deleting cloned region\n");
122 #endif
123 #if 1
124 dprintf("vm_test 5: cloning vidmem in RO and testing if it compares\n");
126 region_id region, region2;
127 vm_region_info info;
128 void *ptr;
129 int rc;
131 region = vm_find_region_by_name(vm_get_kernel_aspace_id(), "vid_mem");
132 if(region < 0)
133 panic("vm_test 5: error finding region 'vid_mem'\n");
134 dprintf("vid_mem region = 0x%x\n", region);
136 region2 = vm_clone_region(vm_get_kernel_aspace_id(), "vid_mem3",
137 &ptr, REGION_ADDR_ANY_ADDRESS, region, REGION_NO_PRIVATE_MAP, LOCK_RO|LOCK_KERNEL);
138 if(region2 < 0)
139 panic("vm_test 5: error cloning region 'vid_mem'\n");
140 dprintf("region2 = 0x%x, ptr = %p\n", region2, ptr);
142 vm_get_region_info(region, &info);
143 rc = memcmp(ptr, (void *)info.base, info.size);
144 if(rc != 0)
145 panic("vm_test 5: regions are not identical\n");
146 else
147 dprintf("vm_test 5: comparison ok\n");
149 if(vm_delete_region(vm_get_kernel_aspace_id(), region2) < 0)
150 panic("vm_test 5: error deleting cloned region\n");
152 #endif
153 #if 1
154 dprintf("vm_test 6: creating anonymous region, cloning it, and testing if it compares\n");
156 region_id region, region2;
157 void *region_addr;
158 void *ptr;
159 int rc;
161 region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "test_region", &region_addr,
162 REGION_ADDR_ANY_ADDRESS, PAGE_SIZE * 16, REGION_WIRING_LAZY, LOCK_RW|LOCK_KERNEL);
163 if(region < 0)
164 panic("vm_test 6: error creating test region\n");
165 dprintf("region = 0x%x, addr = %p\n", region, region_addr);
167 memset(region_addr, 99, PAGE_SIZE * 16);
169 dprintf("memsetted the region\n");
171 region2 = vm_clone_region(vm_get_kernel_aspace_id(), "test_region2",
172 &ptr, REGION_ADDR_ANY_ADDRESS, region, REGION_NO_PRIVATE_MAP, LOCK_RW|LOCK_KERNEL);
173 if(region2 < 0)
174 panic("vm_test 6: error cloning test region\n");
175 dprintf("region2 = 0x%x, ptr = %p\n", region2, ptr);
177 rc = memcmp(region_addr, ptr, PAGE_SIZE * 16);
178 if(rc != 0)
179 panic("vm_test 6: regions are not identical\n");
180 else
181 dprintf("vm_test 6: comparison ok\n");
183 if(vm_delete_region(vm_get_kernel_aspace_id(), region) < 0)
184 panic("vm_test 6: error deleting test region\n");
186 if(vm_delete_region(vm_get_kernel_aspace_id(), region2) < 0)
187 panic("vm_test 6: error deleting cloned region\n");
189 #endif
190 #if 1
191 dprintf("vm_test 7: mmaping a known file a few times and verifying they see the same data\n");
193 void *ptr, *ptr2;
194 region_id rid, rid2;
195 int fd;
197 fd = sys_open("/boot/kernel", 0);
199 rid = vm_map_file(vm_get_kernel_aspace_id(), "mmap_test", &ptr, REGION_ADDR_ANY_ADDRESS,
200 PAGE_SIZE, LOCK_RW|LOCK_KERNEL, REGION_NO_PRIVATE_MAP, "/boot/kernel", 0);
202 rid2 = vm_map_file(vm_get_kernel_aspace_id(), "mmap_test2", &ptr2, REGION_ADDR_ANY_ADDRESS,
203 PAGE_SIZE, LOCK_RW|LOCK_KERNEL, REGION_NO_PRIVATE_MAP, "/boot/kernel", 0);
205 dprintf("diff %d\n", memcmp(ptr, ptr2, PAGE_SIZE));
207 dprintf("removing regions\n");
209 vm_delete_region(vm_get_kernel_aspace_id(), rid);
210 vm_delete_region(vm_get_kernel_aspace_id(), rid2);
212 dprintf("regions deleted\n");
214 sys_close(fd);
216 dprintf("vm_test 7: passed\n");
218 #endif
219 #if 1
220 dprintf("vm_test 8: creating anonymous region, cloning it with private mapping\n");
222 region_id region, region2;
223 void *region_addr;
224 void *ptr;
225 int rc;
227 dprintf("vm_test 8: creating test region...\n");
229 region = vm_create_anonymous_region(vm_get_kernel_aspace_id(), "test_region", &region_addr,
230 REGION_ADDR_ANY_ADDRESS, PAGE_SIZE * 16, REGION_WIRING_LAZY, LOCK_RW|LOCK_KERNEL);
231 if(region < 0)
232 panic("vm_test 8: error creating test region\n");
233 dprintf("region = 0x%x, addr = %p\n", region, region_addr);
235 dprintf("vm_test 8: memsetting the first 2 pages\n");
237 memset(region_addr, 99, PAGE_SIZE * 2);
239 dprintf("vm_test 8: cloning test region with PRIVATE_MAP\n");
241 region2 = vm_clone_region(vm_get_kernel_aspace_id(), "test_region2",
242 &ptr, REGION_ADDR_ANY_ADDRESS, region, REGION_PRIVATE_MAP, LOCK_RW|LOCK_KERNEL);
243 if(region2 < 0)
244 panic("vm_test 8: error cloning test region\n");
245 dprintf("region2 = 0x%x, ptr = %p\n", region2, ptr);
247 dprintf("vm_test 8: comparing first 2 pages, shoudld be identical\n");
249 rc = memcmp(region_addr, ptr, PAGE_SIZE * 2);
250 if(rc != 0)
251 panic("vm_test 8: regions are not identical\n");
252 else
253 dprintf("vm_test 8: comparison ok\n");
255 dprintf("vm_test 8: changing one page in the clone and comparing, should now differ\n");
257 memset(ptr, 1, 1);
259 rc = memcmp(region_addr, ptr, PAGE_SIZE);
260 if(rc != 0)
261 dprintf("vm_test 8: regions are not identical, good\n");
262 else
263 panic("vm_test 8: comparison shows not private mapping\n");
265 dprintf("vm_test 8: comparing untouched pages in source and clone, should be identical\n");
267 rc = memcmp((char *)region_addr + 2*PAGE_SIZE, (char *)ptr + 2*PAGE_SIZE, PAGE_SIZE * 1);
268 if(rc != 0)
269 panic("vm_test 8: regions are not identical\n");
270 else
271 dprintf("vm_test 8: comparison ok\n");
273 dprintf("vm_test 8: modifying source page and comparing, should be the same\n");
275 memset((char *)region_addr + 3*PAGE_SIZE, 2, 4);
277 rc = memcmp((char *)region_addr + 3*PAGE_SIZE, (char *)ptr + 3*PAGE_SIZE, PAGE_SIZE * 1);
278 if(rc != 0)
279 panic("vm_test 8: regions are not identical\n");
280 else
281 dprintf("vm_test 8: comparison ok\n");
284 dprintf("vm_test 8: modifying new page in clone, should not effect source, which is also untouched\n");
286 memset((char *)ptr + 4*PAGE_SIZE, 2, 4);
288 rc = memcmp((char *)region_addr + 4*PAGE_SIZE, (char *)ptr + 4*PAGE_SIZE, PAGE_SIZE * 1);
289 if(rc != 0)
290 dprintf("vm_test 8: regions are not identical, good\n");
291 else
292 panic("vm_test 8: comparison shows not private mapping\n");
294 if(vm_delete_region(vm_get_kernel_aspace_id(), region) < 0)
295 panic("vm_test 8: error deleting test region\n");
297 if(vm_delete_region(vm_get_kernel_aspace_id(), region2) < 0)
298 panic("vm_test 8: error deleting cloned region\n");
300 #endif
301 #if 1
302 dprintf("vm_test 9: mmaping a known file a few times, one with private mappings\n");
304 void *ptr, *ptr2;
305 region_id rid, rid2;
306 int err;
308 dprintf("vm_test 9: mapping /boot/kernel twice\n");
310 rid = vm_map_file(vm_get_kernel_aspace_id(), "mmap_test", &ptr, REGION_ADDR_ANY_ADDRESS,
311 PAGE_SIZE*4, LOCK_RW|LOCK_KERNEL, REGION_NO_PRIVATE_MAP, "/boot/kernel", 0);
313 rid2 = vm_map_file(vm_get_kernel_aspace_id(), "mmap_test2", &ptr2, REGION_ADDR_ANY_ADDRESS,
314 PAGE_SIZE*4, LOCK_RW|LOCK_KERNEL, REGION_PRIVATE_MAP, "/boot/kernel", 0);
316 err = memcmp(ptr, ptr2, PAGE_SIZE);
317 if(err)
318 panic("vm_test 9: two mappings are different\n");
320 dprintf("vm_test 9: modifying private mapping in section that has been referenced, should be different\n");
322 memset(ptr2, 0xaa, 4);
324 err = memcmp(ptr, ptr2, PAGE_SIZE);
325 if(!err)
326 panic("vm_test 9: two mappings still identical\n");
328 dprintf("vm_test 9: modifying private mapping in section that hasn't been touched, should be different\n");
330 memset((char *)ptr2 + PAGE_SIZE, 0xaa, 4);
332 err = memcmp((char *)ptr + PAGE_SIZE, (char *)ptr2 + PAGE_SIZE, PAGE_SIZE);
333 if(!err)
334 panic("vm_test 9: two mappings still identical\n");
336 dprintf("vm_test 9: modifying non-private mapping in section that hasn't been touched\n");
338 memset((char *)ptr + PAGE_SIZE*2, 0xaa, 4);
340 err = memcmp((char *)ptr + PAGE_SIZE*2, (char *)ptr2 + PAGE_SIZE*2, PAGE_SIZE);
341 if(err)
342 panic("vm_test 9: two mappings are different\n");
344 dprintf("vm_test 9: removing regions\n");
346 vm_delete_region(vm_get_kernel_aspace_id(), rid);
347 vm_delete_region(vm_get_kernel_aspace_id(), rid2);
349 dprintf("vm_test 9: regions deleted\n");
351 dprintf("vm_test 9: passed\n");
353 #endif
354 dprintf("vm_test: done\n");