2 ** Copyright 2001, Travis Geiselbrecht. All rights reserved.
3 ** Distributed under the terms of the NewOS License.
5 #include <kernel/kernel.h>
7 #include <kernel/debug.h>
8 #include <kernel/arch/cpu.h>
16 // region_id region, region2, region3;
17 // addr_t region_addr;
20 dprintf("vm_test: entry\n");
22 dprintf("vm_test 1: creating anonymous region and writing to it\n");
27 region
= vm_create_anonymous_region(vm_get_kernel_aspace_id(), "test_region", (void **)®ion_addr
,
28 REGION_ADDR_ANY_ADDRESS
, PAGE_SIZE
* 16, REGION_WIRING_LAZY
, LOCK_RW
|LOCK_KERNEL
);
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");
42 dprintf("vm_test 2: creating physical region and writing and reading from it\n");
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);
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");
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");
68 dprintf("vm_test 3: testing some functionality of vm_get_page_mapping(), vm_get/put_physical_page()\n");
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
);
95 dprintf("vm_test 4: cloning vidmem and testing if it compares\n");
97 region_id region
, region2
;
102 region
= vm_find_region_by_name(vm_get_kernel_aspace_id(), "vid_mem");
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
);
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
);
116 panic("vm_test 4: regions are not identical\n");
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");
124 dprintf("vm_test 5: cloning vidmem in RO and testing if it compares\n");
126 region_id region
, region2
;
131 region
= vm_find_region_by_name(vm_get_kernel_aspace_id(), "vid_mem");
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
);
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
);
145 panic("vm_test 5: regions are not identical\n");
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");
154 dprintf("vm_test 6: creating anonymous region, cloning it, and testing if it compares\n");
156 region_id region
, region2
;
161 region
= vm_create_anonymous_region(vm_get_kernel_aspace_id(), "test_region", ®ion_addr
,
162 REGION_ADDR_ANY_ADDRESS
, PAGE_SIZE
* 16, REGION_WIRING_LAZY
, LOCK_RW
|LOCK_KERNEL
);
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
);
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);
179 panic("vm_test 6: regions are not identical\n");
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");
191 dprintf("vm_test 7: mmaping a known file a few times and verifying they see the same data\n");
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");
216 dprintf("vm_test 7: passed\n");
220 dprintf("vm_test 8: creating anonymous region, cloning it with private mapping\n");
222 region_id region
, region2
;
227 dprintf("vm_test 8: creating test region...\n");
229 region
= vm_create_anonymous_region(vm_get_kernel_aspace_id(), "test_region", ®ion_addr
,
230 REGION_ADDR_ANY_ADDRESS
, PAGE_SIZE
* 16, REGION_WIRING_LAZY
, LOCK_RW
|LOCK_KERNEL
);
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
);
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);
251 panic("vm_test 8: regions are not identical\n");
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");
259 rc
= memcmp(region_addr
, ptr
, PAGE_SIZE
);
261 dprintf("vm_test 8: regions are not identical, good\n");
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);
269 panic("vm_test 8: regions are not identical\n");
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);
279 panic("vm_test 8: regions are not identical\n");
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);
290 dprintf("vm_test 8: regions are not identical, good\n");
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");
302 dprintf("vm_test 9: mmaping a known file a few times, one with private mappings\n");
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
);
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
);
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
);
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
);
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");
354 dprintf("vm_test: done\n");