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