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