xref: /xnu-11417.101.15/tests/game_mode.c (revision e3723e1f17661b24996789d8afc084c0c3303b26)
1*e3723e1fSApple OSS Distributions /*
2*e3723e1fSApple OSS Distributions  * Copyright (c) 2023 Apple Inc. All rights reserved.
3*e3723e1fSApple OSS Distributions  *
4*e3723e1fSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*e3723e1fSApple OSS Distributions  *
6*e3723e1fSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*e3723e1fSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*e3723e1fSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*e3723e1fSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*e3723e1fSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*e3723e1fSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*e3723e1fSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*e3723e1fSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*e3723e1fSApple OSS Distributions  *
15*e3723e1fSApple OSS Distributions  * Please obtain a copy of the License at
16*e3723e1fSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*e3723e1fSApple OSS Distributions  *
18*e3723e1fSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*e3723e1fSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*e3723e1fSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*e3723e1fSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*e3723e1fSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*e3723e1fSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*e3723e1fSApple OSS Distributions  * limitations under the License.
25*e3723e1fSApple OSS Distributions  *
26*e3723e1fSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*e3723e1fSApple OSS Distributions  */
28*e3723e1fSApple OSS Distributions 
29*e3723e1fSApple OSS Distributions /* test that the header doesn't implicitly depend on others */
30*e3723e1fSApple OSS Distributions #include <sys/resource_private.h>
31*e3723e1fSApple OSS Distributions #include <sys/resource.h>
32*e3723e1fSApple OSS Distributions 
33*e3723e1fSApple OSS Distributions #include <mach/coalition.h>
34*e3723e1fSApple OSS Distributions #include <sys/coalition.h>
35*e3723e1fSApple OSS Distributions #include <libproc.h>
36*e3723e1fSApple OSS Distributions 
37*e3723e1fSApple OSS Distributions #include <sys/types.h>
38*e3723e1fSApple OSS Distributions #include <unistd.h>
39*e3723e1fSApple OSS Distributions 
40*e3723e1fSApple OSS Distributions #include <darwintest.h>
41*e3723e1fSApple OSS Distributions #include <darwintest_utils.h>
42*e3723e1fSApple OSS Distributions 
43*e3723e1fSApple OSS Distributions /* TODO: can this come from the right header? */
44*e3723e1fSApple OSS Distributions #define THREAD_GROUP_FLAGS_GAME_MODE            0x800
45*e3723e1fSApple OSS Distributions 
46*e3723e1fSApple OSS Distributions T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
47*e3723e1fSApple OSS Distributions     T_META_RADAR_COMPONENT_NAME("xnu"),
48*e3723e1fSApple OSS Distributions     T_META_RADAR_COMPONENT_VERSION("scheduler"),
49*e3723e1fSApple OSS Distributions     T_META_OWNER("chimene"),
50*e3723e1fSApple OSS Distributions     T_META_RUN_CONCURRENTLY(false),
51*e3723e1fSApple OSS Distributions     T_META_TAG_VM_PREFERRED);
52*e3723e1fSApple OSS Distributions 
53*e3723e1fSApple OSS Distributions static void
check_game_mode(bool expected_mode)54*e3723e1fSApple OSS Distributions check_game_mode(bool expected_mode)
55*e3723e1fSApple OSS Distributions {
56*e3723e1fSApple OSS Distributions 	int game_mode = getpriority(PRIO_DARWIN_GAME_MODE, 0);
57*e3723e1fSApple OSS Distributions 
58*e3723e1fSApple OSS Distributions 	T_QUIET;
59*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(game_mode, "getpriority(PRIO_DARWIN_GAME_MODE)");
60*e3723e1fSApple OSS Distributions 
61*e3723e1fSApple OSS Distributions 	T_LOG("pid %d: game mode is: %d", getpid(), game_mode);
62*e3723e1fSApple OSS Distributions 
63*e3723e1fSApple OSS Distributions 	if (expected_mode) {
64*e3723e1fSApple OSS Distributions 		T_QUIET;
65*e3723e1fSApple OSS Distributions 		T_ASSERT_EQ(game_mode, PRIO_DARWIN_GAME_MODE_ON, "should be on");
66*e3723e1fSApple OSS Distributions 	} else {
67*e3723e1fSApple OSS Distributions 		T_QUIET;
68*e3723e1fSApple OSS Distributions 		T_ASSERT_EQ(game_mode, PRIO_DARWIN_GAME_MODE_OFF, "should be off");
69*e3723e1fSApple OSS Distributions 	}
70*e3723e1fSApple OSS Distributions }
71*e3723e1fSApple OSS Distributions 
72*e3723e1fSApple OSS Distributions T_DECL(entitled_game_mode, "game mode bit should be settable while entitled")
73*e3723e1fSApple OSS Distributions {
74*e3723e1fSApple OSS Distributions 	T_LOG("uid: %d", getuid());
75*e3723e1fSApple OSS Distributions 
76*e3723e1fSApple OSS Distributions 	check_game_mode(false);
77*e3723e1fSApple OSS Distributions 
78*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
79*e3723e1fSApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON)");
80*e3723e1fSApple OSS Distributions 
81*e3723e1fSApple OSS Distributions 	check_game_mode(true);
82*e3723e1fSApple OSS Distributions 
83*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
84*e3723e1fSApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF)");
85*e3723e1fSApple OSS Distributions 
86*e3723e1fSApple OSS Distributions 	check_game_mode(false);
87*e3723e1fSApple OSS Distributions }
88*e3723e1fSApple OSS Distributions 
89*e3723e1fSApple OSS Distributions T_DECL(entitled_game_mode_read_root, "game mode bit should be readable as root",
90*e3723e1fSApple OSS Distributions     T_META_ASROOT(true))
91*e3723e1fSApple OSS Distributions {
92*e3723e1fSApple OSS Distributions 	T_LOG("uid: %d", getuid());
93*e3723e1fSApple OSS Distributions 
94*e3723e1fSApple OSS Distributions 	check_game_mode(false);
95*e3723e1fSApple OSS Distributions }
96*e3723e1fSApple OSS Distributions 
97*e3723e1fSApple OSS Distributions T_DECL(entitled_game_mode_read_notroot, "game mode bit should be readable as not root but entitled",
98*e3723e1fSApple OSS Distributions     T_META_ASROOT(false))
99*e3723e1fSApple OSS Distributions {
100*e3723e1fSApple OSS Distributions 	T_LOG("uid: %d", getuid());
101*e3723e1fSApple OSS Distributions 
102*e3723e1fSApple OSS Distributions 	check_game_mode(false);
103*e3723e1fSApple OSS Distributions }
104*e3723e1fSApple OSS Distributions 
105*e3723e1fSApple OSS Distributions static struct coalinfo_debuginfo
get_coal_debuginfo(uint64_t coal_id,char * prefix)106*e3723e1fSApple OSS Distributions get_coal_debuginfo(uint64_t coal_id, char* prefix)
107*e3723e1fSApple OSS Distributions {
108*e3723e1fSApple OSS Distributions 	struct coalinfo_debuginfo coaldebuginfo = {};
109*e3723e1fSApple OSS Distributions 
110*e3723e1fSApple OSS Distributions 	int ret = coalition_info_debug_info(coal_id, &coaldebuginfo, sizeof(coaldebuginfo));
111*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_debug_info");
112*e3723e1fSApple OSS Distributions 
113*e3723e1fSApple OSS Distributions 	T_LOG("coal(%s): %lld, game count %d, flags 0x%x (group 0x%llx, rec %d, focal: %d, nonfocal %d)",
114*e3723e1fSApple OSS Distributions 	    prefix, coal_id,
115*e3723e1fSApple OSS Distributions 	    coaldebuginfo.game_task_count, coaldebuginfo.thread_group_flags,
116*e3723e1fSApple OSS Distributions 	    coaldebuginfo.thread_group_id, coaldebuginfo.thread_group_recommendation,
117*e3723e1fSApple OSS Distributions 	    coaldebuginfo.focal_task_count, coaldebuginfo.nonfocal_task_count);
118*e3723e1fSApple OSS Distributions 
119*e3723e1fSApple OSS Distributions 	return coaldebuginfo;
120*e3723e1fSApple OSS Distributions }
121*e3723e1fSApple OSS Distributions 
122*e3723e1fSApple OSS Distributions static void
check_game_mode_count(uint32_t expected_count)123*e3723e1fSApple OSS Distributions check_game_mode_count(uint32_t expected_count)
124*e3723e1fSApple OSS Distributions {
125*e3723e1fSApple OSS Distributions 	struct proc_pidcoalitioninfo idinfo = {};
126*e3723e1fSApple OSS Distributions 
127*e3723e1fSApple OSS Distributions 	int ret = proc_pidinfo(getpid(), PROC_PIDCOALITIONINFO, 0,
128*e3723e1fSApple OSS Distributions 	    &idinfo, sizeof(idinfo));
129*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "proc_pidinfo(... PROC_PIDCOALITIONINFO ...)");
130*e3723e1fSApple OSS Distributions 
131*e3723e1fSApple OSS Distributions 	uint64_t res_id = idinfo.coalition_id[COALITION_TYPE_RESOURCE];
132*e3723e1fSApple OSS Distributions 	uint64_t jet_id = idinfo.coalition_id[COALITION_TYPE_JETSAM];
133*e3723e1fSApple OSS Distributions 
134*e3723e1fSApple OSS Distributions 	struct coalinfo_debuginfo coaldebuginfo_res = get_coal_debuginfo(res_id, "res");
135*e3723e1fSApple OSS Distributions 	struct coalinfo_debuginfo coaldebuginfo_jet = get_coal_debuginfo(jet_id, "jet");
136*e3723e1fSApple OSS Distributions 
137*e3723e1fSApple OSS Distributions 	if (expected_count) {
138*e3723e1fSApple OSS Distributions 		// see COALITION_FOCAL_TASKS_ACCOUNTING for this difference
139*e3723e1fSApple OSS Distributions #if TARGET_OS_OSX
140*e3723e1fSApple OSS Distributions 		T_ASSERT_EQ(coaldebuginfo_res.game_task_count, expected_count, "should have game in res coalition");
141*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
142*e3723e1fSApple OSS Distributions #else
143*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
144*e3723e1fSApple OSS Distributions 		T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, expected_count, "should have game in jet coalition");
145*e3723e1fSApple OSS Distributions #endif
146*e3723e1fSApple OSS Distributions 		T_ASSERT_BITS_SET(coaldebuginfo_jet.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
147*e3723e1fSApple OSS Distributions 		    "should have game mode flag in jet coalition"); \
148*e3723e1fSApple OSS Distributions 	} else {
149*e3723e1fSApple OSS Distributions #if TARGET_OS_OSX
150*e3723e1fSApple OSS Distributions 		T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
151*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
152*e3723e1fSApple OSS Distributions #else
153*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
154*e3723e1fSApple OSS Distributions 		T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
155*e3723e1fSApple OSS Distributions #endif
156*e3723e1fSApple OSS Distributions 		T_ASSERT_BITS_NOTSET(coaldebuginfo_jet.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
157*e3723e1fSApple OSS Distributions 		    "should not have game mode flag in jet coalition"); \
158*e3723e1fSApple OSS Distributions 	}
159*e3723e1fSApple OSS Distributions 
160*e3723e1fSApple OSS Distributions 	T_QUIET; T_ASSERT_BITS_NOTSET(coaldebuginfo_res.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
161*e3723e1fSApple OSS Distributions 	    "should never have game mode flag in res coalition"); \
162*e3723e1fSApple OSS Distributions }
163*e3723e1fSApple OSS Distributions 
164*e3723e1fSApple OSS Distributions static void
skip_if_unsupported(void)165*e3723e1fSApple OSS Distributions skip_if_unsupported(void)
166*e3723e1fSApple OSS Distributions {
167*e3723e1fSApple OSS Distributions 	int r;
168*e3723e1fSApple OSS Distributions 	int supported = 0;
169*e3723e1fSApple OSS Distributions 	size_t supported_size = sizeof(supported);
170*e3723e1fSApple OSS Distributions 
171*e3723e1fSApple OSS Distributions 	r = sysctlbyname("kern.thread_groups_supported", &supported, &supported_size,
172*e3723e1fSApple OSS Distributions 	    NULL, 0);
173*e3723e1fSApple OSS Distributions 	if (r < 0) {
174*e3723e1fSApple OSS Distributions 		T_WITH_ERRNO;
175*e3723e1fSApple OSS Distributions 		T_SKIP("could not find \"kern.thread_groups_supported\" sysctl");
176*e3723e1fSApple OSS Distributions 	}
177*e3723e1fSApple OSS Distributions 
178*e3723e1fSApple OSS Distributions 	if (!supported) {
179*e3723e1fSApple OSS Distributions 		T_SKIP("test was run even though kern.thread_groups_supported is not 1, see rdar://111297938");
180*e3723e1fSApple OSS Distributions 	}
181*e3723e1fSApple OSS Distributions 
182*e3723e1fSApple OSS Distributions 	r = sysctlbyname("kern.development", &supported, &supported_size,
183*e3723e1fSApple OSS Distributions 	    NULL, 0);
184*e3723e1fSApple OSS Distributions 	if (r < 0) {
185*e3723e1fSApple OSS Distributions 		T_WITH_ERRNO;
186*e3723e1fSApple OSS Distributions 		T_SKIP("could not find \"kern.development\" sysctl");
187*e3723e1fSApple OSS Distributions 	}
188*e3723e1fSApple OSS Distributions 
189*e3723e1fSApple OSS Distributions 	if (!supported) {
190*e3723e1fSApple OSS Distributions 		T_SKIP("test was run even though kern.development is not 1, see rdar://111297938");
191*e3723e1fSApple OSS Distributions 	}
192*e3723e1fSApple OSS Distributions }
193*e3723e1fSApple OSS Distributions 
194*e3723e1fSApple OSS Distributions T_DECL(entitled_game_mode_check_count, "game mode bit should affect coalition thread group flags",
195*e3723e1fSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
196*e3723e1fSApple OSS Distributions {
197*e3723e1fSApple OSS Distributions 	T_LOG("uid: %d", getuid());
198*e3723e1fSApple OSS Distributions 	skip_if_unsupported();
199*e3723e1fSApple OSS Distributions 
200*e3723e1fSApple OSS Distributions 	check_game_mode(false);
201*e3723e1fSApple OSS Distributions 	check_game_mode_count(0);
202*e3723e1fSApple OSS Distributions 
203*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
204*e3723e1fSApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
205*e3723e1fSApple OSS Distributions 
206*e3723e1fSApple OSS Distributions 	check_game_mode(true);
207*e3723e1fSApple OSS Distributions 	check_game_mode_count(1);
208*e3723e1fSApple OSS Distributions 
209*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
210*e3723e1fSApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
211*e3723e1fSApple OSS Distributions 
212*e3723e1fSApple OSS Distributions 	check_game_mode(false);
213*e3723e1fSApple OSS Distributions 	check_game_mode_count(0);
214*e3723e1fSApple OSS Distributions }
215*e3723e1fSApple OSS Distributions 
216*e3723e1fSApple OSS Distributions T_DECL(game_mode_child_exit, "game mode bit should disappear when child exits",
217*e3723e1fSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
218*e3723e1fSApple OSS Distributions {
219*e3723e1fSApple OSS Distributions 	T_LOG("uid: %d", getuid());
220*e3723e1fSApple OSS Distributions 	skip_if_unsupported();
221*e3723e1fSApple OSS Distributions 
222*e3723e1fSApple OSS Distributions 	check_game_mode(false);
223*e3723e1fSApple OSS Distributions 	check_game_mode_count(0);
224*e3723e1fSApple OSS Distributions 
225*e3723e1fSApple OSS Distributions 	T_LOG("Spawning child");
226*e3723e1fSApple OSS Distributions 
227*e3723e1fSApple OSS Distributions 	pid_t child_pid = fork();
228*e3723e1fSApple OSS Distributions 
229*e3723e1fSApple OSS Distributions 	if (child_pid == 0) {
230*e3723e1fSApple OSS Distributions 		/* child process */
231*e3723e1fSApple OSS Distributions 
232*e3723e1fSApple OSS Distributions 		check_game_mode(false);
233*e3723e1fSApple OSS Distributions 		check_game_mode_count(0);
234*e3723e1fSApple OSS Distributions 
235*e3723e1fSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
236*e3723e1fSApple OSS Distributions 		    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
237*e3723e1fSApple OSS Distributions 
238*e3723e1fSApple OSS Distributions 		check_game_mode(true);
239*e3723e1fSApple OSS Distributions 		check_game_mode_count(1);
240*e3723e1fSApple OSS Distributions 
241*e3723e1fSApple OSS Distributions 		T_LOG("Exit pid %d with the game mode bit on", getpid());
242*e3723e1fSApple OSS Distributions 
243*e3723e1fSApple OSS Distributions 		exit(0);
244*e3723e1fSApple OSS Distributions 	} else {
245*e3723e1fSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
246*e3723e1fSApple OSS Distributions 
247*e3723e1fSApple OSS Distributions 		/* wait for child process to exit */
248*e3723e1fSApple OSS Distributions 		int exit_status = 0, signum = 0;
249*e3723e1fSApple OSS Distributions 
250*e3723e1fSApple OSS Distributions 		T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
251*e3723e1fSApple OSS Distributions 		    "wait for child (%d) complete", child_pid);
252*e3723e1fSApple OSS Distributions 
253*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
254*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
255*e3723e1fSApple OSS Distributions 	}
256*e3723e1fSApple OSS Distributions 
257*e3723e1fSApple OSS Distributions 	check_game_mode(false);
258*e3723e1fSApple OSS Distributions 	check_game_mode_count(0);
259*e3723e1fSApple OSS Distributions }
260*e3723e1fSApple OSS Distributions 
261*e3723e1fSApple OSS Distributions 
262*e3723e1fSApple OSS Distributions T_DECL(game_mode_double_set_and_child_exit, "game mode bit on parent should stay when game mode child exits",
263*e3723e1fSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
264*e3723e1fSApple OSS Distributions {
265*e3723e1fSApple OSS Distributions 	T_LOG("uid: %d", getuid());
266*e3723e1fSApple OSS Distributions 	skip_if_unsupported();
267*e3723e1fSApple OSS Distributions 
268*e3723e1fSApple OSS Distributions 	check_game_mode(false);
269*e3723e1fSApple OSS Distributions 	check_game_mode_count(0);
270*e3723e1fSApple OSS Distributions 
271*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
272*e3723e1fSApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
273*e3723e1fSApple OSS Distributions 
274*e3723e1fSApple OSS Distributions 	check_game_mode(true);
275*e3723e1fSApple OSS Distributions 	check_game_mode_count(1);
276*e3723e1fSApple OSS Distributions 
277*e3723e1fSApple OSS Distributions 	pid_t child_pid = fork();
278*e3723e1fSApple OSS Distributions 
279*e3723e1fSApple OSS Distributions 	if (child_pid == 0) {
280*e3723e1fSApple OSS Distributions 		/* child process */
281*e3723e1fSApple OSS Distributions 
282*e3723e1fSApple OSS Distributions 		check_game_mode(false);
283*e3723e1fSApple OSS Distributions 		check_game_mode_count(1);
284*e3723e1fSApple OSS Distributions 
285*e3723e1fSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
286*e3723e1fSApple OSS Distributions 		    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
287*e3723e1fSApple OSS Distributions 
288*e3723e1fSApple OSS Distributions 		check_game_mode(true);
289*e3723e1fSApple OSS Distributions 		check_game_mode_count(2);
290*e3723e1fSApple OSS Distributions 
291*e3723e1fSApple OSS Distributions 		T_LOG("Exit pid %d with the game mode bit on", getpid());
292*e3723e1fSApple OSS Distributions 		exit(0);
293*e3723e1fSApple OSS Distributions 	} else {
294*e3723e1fSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
295*e3723e1fSApple OSS Distributions 
296*e3723e1fSApple OSS Distributions 		/* wait for child process to exit */
297*e3723e1fSApple OSS Distributions 		int exit_status = 0, signum = 0;
298*e3723e1fSApple OSS Distributions 
299*e3723e1fSApple OSS Distributions 		T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
300*e3723e1fSApple OSS Distributions 		    "wait for child (%d) complete", child_pid);
301*e3723e1fSApple OSS Distributions 
302*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
303*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
304*e3723e1fSApple OSS Distributions 	}
305*e3723e1fSApple OSS Distributions 
306*e3723e1fSApple OSS Distributions 	check_game_mode(true);
307*e3723e1fSApple OSS Distributions 	check_game_mode_count(1);
308*e3723e1fSApple OSS Distributions 
309*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
310*e3723e1fSApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
311*e3723e1fSApple OSS Distributions 
312*e3723e1fSApple OSS Distributions 	check_game_mode(false);
313*e3723e1fSApple OSS Distributions 	check_game_mode_count(0);
314*e3723e1fSApple OSS Distributions }
315*e3723e1fSApple OSS Distributions 
316*e3723e1fSApple OSS Distributions T_DECL(game_mode_double_set_and_child_unset, "game mode bit on parent should stay when game mode child unsets",
317*e3723e1fSApple OSS Distributions     T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
318*e3723e1fSApple OSS Distributions {
319*e3723e1fSApple OSS Distributions 	T_LOG("uid: %d", getuid());
320*e3723e1fSApple OSS Distributions 	skip_if_unsupported();
321*e3723e1fSApple OSS Distributions 
322*e3723e1fSApple OSS Distributions 	check_game_mode(false);
323*e3723e1fSApple OSS Distributions 	check_game_mode_count(0);
324*e3723e1fSApple OSS Distributions 
325*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
326*e3723e1fSApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
327*e3723e1fSApple OSS Distributions 
328*e3723e1fSApple OSS Distributions 	check_game_mode(true);
329*e3723e1fSApple OSS Distributions 	check_game_mode_count(1);
330*e3723e1fSApple OSS Distributions 
331*e3723e1fSApple OSS Distributions 	pid_t child_pid = fork();
332*e3723e1fSApple OSS Distributions 
333*e3723e1fSApple OSS Distributions 	if (child_pid == 0) {
334*e3723e1fSApple OSS Distributions 		/* child process */
335*e3723e1fSApple OSS Distributions 
336*e3723e1fSApple OSS Distributions 		check_game_mode(false);
337*e3723e1fSApple OSS Distributions 		check_game_mode_count(1);
338*e3723e1fSApple OSS Distributions 
339*e3723e1fSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
340*e3723e1fSApple OSS Distributions 		    "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
341*e3723e1fSApple OSS Distributions 
342*e3723e1fSApple OSS Distributions 		check_game_mode(true);
343*e3723e1fSApple OSS Distributions 		check_game_mode_count(2);
344*e3723e1fSApple OSS Distributions 
345*e3723e1fSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
346*e3723e1fSApple OSS Distributions 		    "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
347*e3723e1fSApple OSS Distributions 
348*e3723e1fSApple OSS Distributions 		check_game_mode(false);
349*e3723e1fSApple OSS Distributions 		check_game_mode_count(1);
350*e3723e1fSApple OSS Distributions 
351*e3723e1fSApple OSS Distributions 		T_LOG("Exit pid %d with the game mode bit off", getpid());
352*e3723e1fSApple OSS Distributions 		exit(0);
353*e3723e1fSApple OSS Distributions 	} else {
354*e3723e1fSApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
355*e3723e1fSApple OSS Distributions 
356*e3723e1fSApple OSS Distributions 		/* wait for child process to exit */
357*e3723e1fSApple OSS Distributions 		int exit_status = 0, signum = 0;
358*e3723e1fSApple OSS Distributions 
359*e3723e1fSApple OSS Distributions 		T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
360*e3723e1fSApple OSS Distributions 		    "wait for child (%d) complete", child_pid);
361*e3723e1fSApple OSS Distributions 
362*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
363*e3723e1fSApple OSS Distributions 		T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
364*e3723e1fSApple OSS Distributions 	}
365*e3723e1fSApple OSS Distributions 
366*e3723e1fSApple OSS Distributions 	check_game_mode(true);
367*e3723e1fSApple OSS Distributions 	check_game_mode_count(1);
368*e3723e1fSApple OSS Distributions 
369*e3723e1fSApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
370*e3723e1fSApple OSS Distributions 	    "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
371*e3723e1fSApple OSS Distributions 
372*e3723e1fSApple OSS Distributions 	check_game_mode(false);
373*e3723e1fSApple OSS Distributions 	check_game_mode_count(0);
374*e3723e1fSApple OSS Distributions }
375