1*2c2f96dcSApple OSS Distributions #include <darwintest.h> 2*2c2f96dcSApple OSS Distributions 3*2c2f96dcSApple OSS Distributions #include <errno.h> 4*2c2f96dcSApple OSS Distributions #include <stdio.h> 5*2c2f96dcSApple OSS Distributions #include <stdlib.h> 6*2c2f96dcSApple OSS Distributions #include <string.h> 7*2c2f96dcSApple OSS Distributions 8*2c2f96dcSApple OSS Distributions #include <sys/mman.h> 9*2c2f96dcSApple OSS Distributions 10*2c2f96dcSApple OSS Distributions #include <mach/mach_error.h> 11*2c2f96dcSApple OSS Distributions #include <mach/mach_init.h> 12*2c2f96dcSApple OSS Distributions #include <mach/mach_vm.h> 13*2c2f96dcSApple OSS Distributions 14*2c2f96dcSApple OSS Distributions #define ANON_MAX_PAGES 0xFFFFFFFFULL 15*2c2f96dcSApple OSS Distributions #define ANON_MAX_SIZE (ANON_MAX_PAGES * vm_kernel_page_size) 16*2c2f96dcSApple OSS Distributions 17*2c2f96dcSApple OSS Distributions T_DECL(anon_max_size, "Test an ALLOC_MAX_SIZE allocation", 18*2c2f96dcSApple OSS Distributions T_META_NAMESPACE("xnu.vm"), 19*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 20*2c2f96dcSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("VM"), 21*2c2f96dcSApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.development", 1)) 22*2c2f96dcSApple OSS Distributions { 23*2c2f96dcSApple OSS Distributions kern_return_t kr; 24*2c2f96dcSApple OSS Distributions mach_vm_address_t vm_addr; 25*2c2f96dcSApple OSS Distributions mach_vm_size_t vm_size; 26*2c2f96dcSApple OSS Distributions unsigned char *cp; 27*2c2f96dcSApple OSS Distributions int ret; 28*2c2f96dcSApple OSS Distributions unsigned char vec; 29*2c2f96dcSApple OSS Distributions 30*2c2f96dcSApple OSS Distributions /* allocate the largest anonymous size possible */ 31*2c2f96dcSApple OSS Distributions vm_size = ANON_MAX_SIZE; 32*2c2f96dcSApple OSS Distributions /* truncate to avoid going over actual max size when rounding up */ 33*2c2f96dcSApple OSS Distributions vm_size &= ~PAGE_MASK; 34*2c2f96dcSApple OSS Distributions vm_addr = 0; 35*2c2f96dcSApple OSS Distributions kr = mach_vm_allocate(mach_task_self(), 36*2c2f96dcSApple OSS Distributions &vm_addr, 37*2c2f96dcSApple OSS Distributions vm_size, 38*2c2f96dcSApple OSS Distributions VM_FLAGS_ANYWHERE | VM_FLAGS_PURGABLE); 39*2c2f96dcSApple OSS Distributions if (kr == KERN_NO_SPACE) { 40*2c2f96dcSApple OSS Distributions T_SKIP("not enough address space..."); 41*2c2f96dcSApple OSS Distributions } 42*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_MACH_SUCCESS(kr, "vm_allocate(0x%llx)", ANON_MAX_SIZE); 43*2c2f96dcSApple OSS Distributions 44*2c2f96dcSApple OSS Distributions /* dirty the first and last pages */ 45*2c2f96dcSApple OSS Distributions cp = (unsigned char *)(uintptr_t)vm_addr; 46*2c2f96dcSApple OSS Distributions cp[0] = 'a'; 47*2c2f96dcSApple OSS Distributions cp[vm_size - 1] = 'z'; 48*2c2f96dcSApple OSS Distributions 49*2c2f96dcSApple OSS Distributions /* trigger the VM compressor for that VM object */ 50*2c2f96dcSApple OSS Distributions ret = madvise(cp, (size_t)vm_size, MADV_PAGEOUT); 51*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "madvise(MADV_PAGEOUT)"); 52*2c2f96dcSApple OSS Distributions 53*2c2f96dcSApple OSS Distributions /* wait for the pages to be (asynchronously) compressed */ 54*2c2f96dcSApple OSS Distributions T_QUIET; T_LOG("waiting for first page to be paged out..."); 55*2c2f96dcSApple OSS Distributions do { 56*2c2f96dcSApple OSS Distributions ret = mincore(&cp[0], 1, (char *)&vec); 57*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mincore(1st)"); 58*2c2f96dcSApple OSS Distributions } while (vec & MINCORE_INCORE); 59*2c2f96dcSApple OSS Distributions T_QUIET; T_LOG("waiting for last page to be paged out..."); 60*2c2f96dcSApple OSS Distributions do { 61*2c2f96dcSApple OSS Distributions ret = mincore(&cp[vm_size - 1], 1, (char *)&vec); 62*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "mincore(last)"); 63*2c2f96dcSApple OSS Distributions } while (vec & MINCORE_INCORE); 64*2c2f96dcSApple OSS Distributions 65*2c2f96dcSApple OSS Distributions /* trigger pageins and check the contents */ 66*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_EQ(cp[0], 'a', "first page intact"); 67*2c2f96dcSApple OSS Distributions T_QUIET; T_ASSERT_EQ(cp[vm_size - 1], 'z', "last page intact"); 68*2c2f96dcSApple OSS Distributions 69*2c2f96dcSApple OSS Distributions /* success */ 70*2c2f96dcSApple OSS Distributions return; 71*2c2f96dcSApple OSS Distributions } 72