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