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