1*4d495c6eSApple OSS Distributions /* 2*4d495c6eSApple OSS Distributions * Copyright (c) 2022 Apple Inc. All rights reserved. 3*4d495c6eSApple OSS Distributions * 4*4d495c6eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 5*4d495c6eSApple OSS Distributions * 6*4d495c6eSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code 7*4d495c6eSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License 8*4d495c6eSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in 9*4d495c6eSApple OSS Distributions * compliance with the License. The rights granted to you under the License 10*4d495c6eSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of, 11*4d495c6eSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to 12*4d495c6eSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any 13*4d495c6eSApple OSS Distributions * terms of an Apple operating system software license agreement. 14*4d495c6eSApple OSS Distributions * 15*4d495c6eSApple OSS Distributions * Please obtain a copy of the License at 16*4d495c6eSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file. 17*4d495c6eSApple OSS Distributions * 18*4d495c6eSApple OSS Distributions * The Original Code and all software distributed under the License are 19*4d495c6eSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 20*4d495c6eSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 21*4d495c6eSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 22*4d495c6eSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 23*4d495c6eSApple OSS Distributions * Please see the License for the specific language governing rights and 24*4d495c6eSApple OSS Distributions * limitations under the License. 25*4d495c6eSApple OSS Distributions * 26*4d495c6eSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 27*4d495c6eSApple OSS Distributions */ 28*4d495c6eSApple OSS Distributions #include <darwintest.h> 29*4d495c6eSApple OSS Distributions #include <sys/sysctl.h> 30*4d495c6eSApple OSS Distributions #include <assert.h> 31*4d495c6eSApple OSS Distributions #include <pthread.h> 32*4d495c6eSApple OSS Distributions #include "test_utils.h" 33*4d495c6eSApple OSS Distributions 34*4d495c6eSApple OSS Distributions T_GLOBAL_META( 35*4d495c6eSApple OSS Distributions T_META_NAMESPACE("xnu.arm"), 36*4d495c6eSApple OSS Distributions T_META_RADAR_COMPONENT_NAME("xnu"), 37*4d495c6eSApple OSS Distributions T_META_RADAR_COMPONENT_VERSION("arm"), 38*4d495c6eSApple OSS Distributions T_META_OWNER("jharmening"), 39*4d495c6eSApple OSS Distributions T_META_RUN_CONCURRENTLY(true), 40*4d495c6eSApple OSS Distributions XNU_T_META_SOC_SPECIFIC); 41*4d495c6eSApple OSS Distributions 42*4d495c6eSApple OSS Distributions T_DECL(pmap_enter_disconnect, 43*4d495c6eSApple OSS Distributions "Test that a physical page can be safely mapped concurrently with a disconnect of the same page", T_META_TAG_VM_NOT_ELIGIBLE) 44*4d495c6eSApple OSS Distributions { 45*4d495c6eSApple OSS Distributions int num_loops = 10000; 46*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.pmap_enter_disconnect_test", NULL, NULL, &num_loops, sizeof(num_loops)), 47*4d495c6eSApple OSS Distributions "kern.pmap_enter_disconnect_test, %d loops", num_loops); 48*4d495c6eSApple OSS Distributions } 49*4d495c6eSApple OSS Distributions 50*4d495c6eSApple OSS Distributions T_DECL(pmap_exec_remove_test, 51*4d495c6eSApple OSS Distributions "Test that an executable mapping can be created while another mapping of the same physical page is removed", T_META_TAG_VM_NOT_ELIGIBLE) 52*4d495c6eSApple OSS Distributions { 53*4d495c6eSApple OSS Distributions int num_loops = 10000; 54*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.pmap_exec_remove_test", NULL, NULL, &num_loops, sizeof(num_loops)), 55*4d495c6eSApple OSS Distributions "kern.pmap_exec_remove_test, %d loops", num_loops); 56*4d495c6eSApple OSS Distributions } 57*4d495c6eSApple OSS Distributions 58*4d495c6eSApple OSS Distributions T_DECL(pmap_compress_remove_test, 59*4d495c6eSApple OSS Distributions "Test that a page can be disconnected for compression while concurrently unmapping the same page", T_META_TAG_VM_NOT_ELIGIBLE) 60*4d495c6eSApple OSS Distributions { 61*4d495c6eSApple OSS Distributions int num_loops = 1000000; 62*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.pmap_compress_remove_test", NULL, NULL, &num_loops, sizeof(num_loops)), 63*4d495c6eSApple OSS Distributions "kern.pmap_compress_remove_test, %d loops", num_loops); 64*4d495c6eSApple OSS Distributions } 65*4d495c6eSApple OSS Distributions 66*4d495c6eSApple OSS Distributions T_DECL(pmap_nesting_test, 67*4d495c6eSApple OSS Distributions "Test that pmap_nest() and pmap_unnest() work reliably when concurrently invoked from multiple threads", T_META_TAG_VM_NOT_ELIGIBLE) 68*4d495c6eSApple OSS Distributions { 69*4d495c6eSApple OSS Distributions int num_loops = 5; 70*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.pmap_nesting_test", NULL, NULL, &num_loops, sizeof(num_loops)), 71*4d495c6eSApple OSS Distributions "kern.pmap_nesting_test, %d loops", num_loops); 72*4d495c6eSApple OSS Distributions } 73*4d495c6eSApple OSS Distributions 74*4d495c6eSApple OSS Distributions T_DECL(pmap_iommu_disconnect_test, 75*4d495c6eSApple OSS Distributions "Test that CPU mappings of a physical page can safely be disconnected in the presence of IOMMU mappings", T_META_TAG_VM_NOT_ELIGIBLE) 76*4d495c6eSApple OSS Distributions { 77*4d495c6eSApple OSS Distributions int run = 1; 78*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.pmap_iommu_disconnect_test", NULL, NULL, &run, sizeof(run)), 79*4d495c6eSApple OSS Distributions "kern.pmap_iommu_disconnect_test"); 80*4d495c6eSApple OSS Distributions } 81*4d495c6eSApple OSS Distributions 82*4d495c6eSApple OSS Distributions T_DECL(pmap_extended_test, 83*4d495c6eSApple OSS Distributions "Test various pmap lifecycle calls in the presence of special configurations such as 4K and stage-2", T_META_TAG_VM_NOT_ELIGIBLE) 84*4d495c6eSApple OSS Distributions { 85*4d495c6eSApple OSS Distributions int run = 1; 86*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.pmap_extended_test", NULL, NULL, &run, sizeof(run)), 87*4d495c6eSApple OSS Distributions "kern.pmap_extended_test"); 88*4d495c6eSApple OSS Distributions } 89*4d495c6eSApple OSS Distributions 90*4d495c6eSApple OSS Distributions T_DECL(pmap_huge_pv_list_test, 91*4d495c6eSApple OSS Distributions "Test that extremely large PV lists can be managed without spinlock timeouts or other panics", 92*4d495c6eSApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.page_protection_type", 2), T_META_TAG_VM_NOT_ELIGIBLE) 93*4d495c6eSApple OSS Distributions { 94*4d495c6eSApple OSS Distributions struct { 95*4d495c6eSApple OSS Distributions unsigned int num_loops; 96*4d495c6eSApple OSS Distributions unsigned int num_mappings; 97*4d495c6eSApple OSS Distributions } hugepv_in; 98*4d495c6eSApple OSS Distributions hugepv_in.num_loops = 500; 99*4d495c6eSApple OSS Distributions hugepv_in.num_mappings = 500000; 100*4d495c6eSApple OSS Distributions 101*4d495c6eSApple OSS Distributions /** 102*4d495c6eSApple OSS Distributions * This test spawns a number of long-running and CPU-intensive kernel worker threads 103*4d495c6eSApple OSS Distributions * which inherit the main thread's priority. Temporarily drop our priority down to 104*4d495c6eSApple OSS Distributions * the default (low) userspace priority to avoid producing a bunch of foreground- 105*4d495c6eSApple OSS Distributions * priority kernel threads that may starve other threads on smaller/slower devices. 106*4d495c6eSApple OSS Distributions */ 107*4d495c6eSApple OSS Distributions qos_class_t prev_qos; 108*4d495c6eSApple OSS Distributions pthread_get_qos_class_np(pthread_self(), &prev_qos, NULL); 109*4d495c6eSApple OSS Distributions pthread_set_qos_class_self_np(QOS_CLASS_DEFAULT, 0); 110*4d495c6eSApple OSS Distributions 111*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.pmap_huge_pv_list_test", NULL, NULL, 112*4d495c6eSApple OSS Distributions &hugepv_in, sizeof(hugepv_in)), "kern.pmap_huge_pv_list_test"); 113*4d495c6eSApple OSS Distributions 114*4d495c6eSApple OSS Distributions pthread_set_qos_class_self_np(prev_qos, 0); 115*4d495c6eSApple OSS Distributions } 116*4d495c6eSApple OSS Distributions 117*4d495c6eSApple OSS Distributions T_DECL(pmap_reentrance_test, 118*4d495c6eSApple OSS Distributions "Test that the pmap can be reentered by an async exception handler", 119*4d495c6eSApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.page_protection_type", 2), T_META_TAG_VM_NOT_ELIGIBLE) 120*4d495c6eSApple OSS Distributions { 121*4d495c6eSApple OSS Distributions int num_loops = 10000; 122*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.pmap_reentrance_test", NULL, NULL, &num_loops, sizeof(num_loops)), 123*4d495c6eSApple OSS Distributions "kern.pmap_reentrance_test, %d loops", num_loops); 124*4d495c6eSApple OSS Distributions } 125*4d495c6eSApple OSS Distributions 126*4d495c6eSApple OSS Distributions T_DECL(surt_test, 127*4d495c6eSApple OSS Distributions "Test that surt can handle a surge of SURT requests", 128*4d495c6eSApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.page_protection_type", 2), 129*4d495c6eSApple OSS Distributions T_META_REQUIRES_SYSCTL_EQ("kern.surt_ready", 1), 130*4d495c6eSApple OSS Distributions T_META_TAG_VM_NOT_ELIGIBLE) 131*4d495c6eSApple OSS Distributions { 132*4d495c6eSApple OSS Distributions /* Use maxproc to get the theoretical upper bound on the sizeof a SURT request surge. */ 133*4d495c6eSApple OSS Distributions int maxproc; 134*4d495c6eSApple OSS Distributions size_t maxproc_size = sizeof(maxproc); 135*4d495c6eSApple OSS Distributions sysctlbyname("kern.maxproc", &maxproc, &maxproc_size, NULL, 0); 136*4d495c6eSApple OSS Distributions 137*4d495c6eSApple OSS Distributions int num_surts = maxproc; 138*4d495c6eSApple OSS Distributions const int num_loops = 100; 139*4d495c6eSApple OSS Distributions 140*4d495c6eSApple OSS Distributions for (int i = 0; i < num_loops; i++) { 141*4d495c6eSApple OSS Distributions T_ASSERT_POSIX_SUCCESS(sysctlbyname("kern.surt_test", NULL, NULL, &num_surts, sizeof(num_surts)), 142*4d495c6eSApple OSS Distributions "kern.surt_test, %d surts", num_surts); 143*4d495c6eSApple OSS Distributions } 144*4d495c6eSApple OSS Distributions } 145