xref: /xnu-8796.121.2/tests/vm_kern_count_wired_kernelcache.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions #include <unistd.h>
2*c54f35caSApple OSS Distributions #include <errno.h>
3*c54f35caSApple OSS Distributions 
4*c54f35caSApple OSS Distributions #include <vm_statistics.h>
5*c54f35caSApple OSS Distributions #include <mach/mach.h>
6*c54f35caSApple OSS Distributions #include <mach_debug/mach_debug.h>
7*c54f35caSApple OSS Distributions 
8*c54f35caSApple OSS Distributions #include <darwintest.h>
9*c54f35caSApple OSS Distributions 
10*c54f35caSApple OSS Distributions /*
11*c54f35caSApple OSS Distributions  * Ensure that mach_memory_info includes a counter for the kernelcache size.
12*c54f35caSApple OSS Distributions  */
13*c54f35caSApple OSS Distributions 
14*c54f35caSApple OSS Distributions T_GLOBAL_META(
15*c54f35caSApple OSS Distributions 	T_META_NAMESPACE("xnu.vm"),
16*c54f35caSApple OSS Distributions 	T_META_RADAR_COMPONENT_NAME("xnu"),
17*c54f35caSApple OSS Distributions 	T_META_RADAR_COMPONENT_VERSION("VM"));
18*c54f35caSApple OSS Distributions 
19*c54f35caSApple OSS Distributions T_DECL(vm_kern_count_wired_kernelcache,
20*c54f35caSApple OSS Distributions     "mach_memory_info returns a counter for for kernelcache",
21*c54f35caSApple OSS Distributions     T_META_ASROOT(true))
22*c54f35caSApple OSS Distributions {
23*c54f35caSApple OSS Distributions 	kern_return_t kr;
24*c54f35caSApple OSS Distributions 	uint64_t i;
25*c54f35caSApple OSS Distributions 	mach_zone_name_t *name = NULL;
26*c54f35caSApple OSS Distributions 	unsigned int nameCnt = 0;
27*c54f35caSApple OSS Distributions 	mach_zone_info_t *info = NULL;
28*c54f35caSApple OSS Distributions 	unsigned int infoCnt = 0;
29*c54f35caSApple OSS Distributions 	mach_memory_info_t *wiredInfo = NULL;
30*c54f35caSApple OSS Distributions 	unsigned int wiredInfoCnt = 0;
31*c54f35caSApple OSS Distributions 
32*c54f35caSApple OSS Distributions 	kr = mach_memory_info(mach_host_self(), &name, &nameCnt, &info, &infoCnt,
33*c54f35caSApple OSS Distributions 	    &wiredInfo, &wiredInfoCnt);
34*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "mach_memory_info");
35*c54f35caSApple OSS Distributions 
36*c54f35caSApple OSS Distributions 	bool found_kernelcache_counter = false;
37*c54f35caSApple OSS Distributions 	uint64_t static_kernelcache_size = 0;
38*c54f35caSApple OSS Distributions 	uint64_t wired_memory_boot = 0;
39*c54f35caSApple OSS Distributions 	for (i = 0; i < wiredInfoCnt; i++) {
40*c54f35caSApple OSS Distributions 		const mach_memory_info_t *curr = &wiredInfo[i];
41*c54f35caSApple OSS Distributions 		uint32_t type = curr->flags & VM_KERN_SITE_TYPE;
42*c54f35caSApple OSS Distributions 		if (type == VM_KERN_SITE_COUNTER) {
43*c54f35caSApple OSS Distributions 			if (curr->site == VM_KERN_COUNT_WIRED_STATIC_KERNELCACHE) {
44*c54f35caSApple OSS Distributions 				found_kernelcache_counter = true;
45*c54f35caSApple OSS Distributions 				static_kernelcache_size = curr->size;
46*c54f35caSApple OSS Distributions 			} else if (curr->site == VM_KERN_COUNT_WIRED_BOOT) {
47*c54f35caSApple OSS Distributions 				wired_memory_boot = curr->size;
48*c54f35caSApple OSS Distributions 			}
49*c54f35caSApple OSS Distributions 		}
50*c54f35caSApple OSS Distributions 	}
51*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_TRUE(found_kernelcache_counter, "mach_memory_info returned kernelcache counter.");
52*c54f35caSApple OSS Distributions 	// Sanity check that the counter isn't 0.
53*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_GT(static_kernelcache_size, 0ULL, "kernelcache counter > 0");
54*c54f35caSApple OSS Distributions 	// Sanity check that the counter is less than the amount of wired memory
55*c54f35caSApple OSS Distributions 	// at boot.
56*c54f35caSApple OSS Distributions 	T_QUIET; T_ASSERT_LE(static_kernelcache_size, wired_memory_boot, "kernelcache counter <= VM_KERN_COUNT_WIRED_BOOT");
57*c54f35caSApple OSS Distributions 
58*c54f35caSApple OSS Distributions 	// Cleanup
59*c54f35caSApple OSS Distributions 	if ((name != NULL) && (nameCnt != 0)) {
60*c54f35caSApple OSS Distributions 		kr = vm_deallocate(mach_task_self(), (vm_address_t) name,
61*c54f35caSApple OSS Distributions 		    (vm_size_t) (nameCnt * sizeof *name));
62*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate name");
63*c54f35caSApple OSS Distributions 	}
64*c54f35caSApple OSS Distributions 
65*c54f35caSApple OSS Distributions 	if ((info != NULL) && (infoCnt != 0)) {
66*c54f35caSApple OSS Distributions 		kr = vm_deallocate(mach_task_self(), (vm_address_t) info,
67*c54f35caSApple OSS Distributions 		    (vm_size_t) (infoCnt * sizeof *info));
68*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate info");
69*c54f35caSApple OSS Distributions 	}
70*c54f35caSApple OSS Distributions 
71*c54f35caSApple OSS Distributions 	if ((wiredInfo != NULL) && (wiredInfoCnt != 0)) {
72*c54f35caSApple OSS Distributions 		kr = vm_deallocate(mach_task_self(), (vm_address_t) wiredInfo,
73*c54f35caSApple OSS Distributions 		    (vm_size_t) (wiredInfoCnt * sizeof *wiredInfo));
74*c54f35caSApple OSS Distributions 		T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_deallocate wiredInfo");
75*c54f35caSApple OSS Distributions 	}
76*c54f35caSApple OSS Distributions }
77