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 T_META_TAG_VM_PREFERRED);
52
53 static void
check_game_mode(bool expected_mode)54 check_game_mode(bool expected_mode)
55 {
56 int game_mode = getpriority(PRIO_DARWIN_GAME_MODE, 0);
57
58 T_QUIET;
59 T_ASSERT_POSIX_SUCCESS(game_mode, "getpriority(PRIO_DARWIN_GAME_MODE)");
60
61 T_LOG("pid %d: game mode is: %d", getpid(), game_mode);
62
63 if (expected_mode) {
64 T_QUIET;
65 T_ASSERT_EQ(game_mode, PRIO_DARWIN_GAME_MODE_ON, "should be on");
66 } else {
67 T_QUIET;
68 T_ASSERT_EQ(game_mode, PRIO_DARWIN_GAME_MODE_OFF, "should be off");
69 }
70 }
71
72 T_DECL(entitled_game_mode, "game mode bit should be settable while entitled")
73 {
74 T_LOG("uid: %d", getuid());
75
76 check_game_mode(false);
77
78 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
79 "setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON)");
80
81 check_game_mode(true);
82
83 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
84 "setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF)");
85
86 check_game_mode(false);
87 }
88
89 T_DECL(entitled_game_mode_read_root, "game mode bit should be readable as root",
90 T_META_ASROOT(true))
91 {
92 T_LOG("uid: %d", getuid());
93
94 check_game_mode(false);
95 }
96
97 T_DECL(entitled_game_mode_read_notroot, "game mode bit should be readable as not root but entitled",
98 T_META_ASROOT(false))
99 {
100 T_LOG("uid: %d", getuid());
101
102 check_game_mode(false);
103 }
104
105 static struct coalinfo_debuginfo
get_coal_debuginfo(uint64_t coal_id,char * prefix)106 get_coal_debuginfo(uint64_t coal_id, char* prefix)
107 {
108 struct coalinfo_debuginfo coaldebuginfo = {};
109
110 int ret = coalition_info_debug_info(coal_id, &coaldebuginfo, sizeof(coaldebuginfo));
111 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_debug_info");
112
113 T_LOG("coal(%s): %lld, game count %d, flags 0x%x (group 0x%llx, rec %d, focal: %d, nonfocal %d)",
114 prefix, coal_id,
115 coaldebuginfo.game_task_count, coaldebuginfo.thread_group_flags,
116 coaldebuginfo.thread_group_id, coaldebuginfo.thread_group_recommendation,
117 coaldebuginfo.focal_task_count, coaldebuginfo.nonfocal_task_count);
118
119 return coaldebuginfo;
120 }
121
122 static void
check_game_mode_count(uint32_t expected_count)123 check_game_mode_count(uint32_t expected_count)
124 {
125 struct proc_pidcoalitioninfo idinfo = {};
126
127 int ret = proc_pidinfo(getpid(), PROC_PIDCOALITIONINFO, 0,
128 &idinfo, sizeof(idinfo));
129 T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "proc_pidinfo(... PROC_PIDCOALITIONINFO ...)");
130
131 uint64_t res_id = idinfo.coalition_id[COALITION_TYPE_RESOURCE];
132 uint64_t jet_id = idinfo.coalition_id[COALITION_TYPE_JETSAM];
133
134 struct coalinfo_debuginfo coaldebuginfo_res = get_coal_debuginfo(res_id, "res");
135 struct coalinfo_debuginfo coaldebuginfo_jet = get_coal_debuginfo(jet_id, "jet");
136
137 if (expected_count) {
138 // see COALITION_FOCAL_TASKS_ACCOUNTING for this difference
139 #if TARGET_OS_OSX
140 T_ASSERT_EQ(coaldebuginfo_res.game_task_count, expected_count, "should have game in res coalition");
141 T_QUIET; T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
142 #else
143 T_QUIET; T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
144 T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, expected_count, "should have game in jet coalition");
145 #endif
146 T_ASSERT_BITS_SET(coaldebuginfo_jet.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
147 "should have game mode flag in jet coalition"); \
148 } else {
149 #if TARGET_OS_OSX
150 T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
151 T_QUIET; T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
152 #else
153 T_QUIET; T_ASSERT_EQ(coaldebuginfo_res.game_task_count, 0, "should not have game in res coalition");
154 T_ASSERT_EQ(coaldebuginfo_jet.game_task_count, 0, "should not have game in jet coalition");
155 #endif
156 T_ASSERT_BITS_NOTSET(coaldebuginfo_jet.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
157 "should not have game mode flag in jet coalition"); \
158 }
159
160 T_QUIET; T_ASSERT_BITS_NOTSET(coaldebuginfo_res.thread_group_flags, THREAD_GROUP_FLAGS_GAME_MODE,
161 "should never have game mode flag in res coalition"); \
162 }
163
164 static void
skip_if_unsupported(void)165 skip_if_unsupported(void)
166 {
167 int r;
168 int supported = 0;
169 size_t supported_size = sizeof(supported);
170
171 r = sysctlbyname("kern.thread_groups_supported", &supported, &supported_size,
172 NULL, 0);
173 if (r < 0) {
174 T_WITH_ERRNO;
175 T_SKIP("could not find \"kern.thread_groups_supported\" sysctl");
176 }
177
178 if (!supported) {
179 T_SKIP("test was run even though kern.thread_groups_supported is not 1, see rdar://111297938");
180 }
181
182 r = sysctlbyname("kern.development", &supported, &supported_size,
183 NULL, 0);
184 if (r < 0) {
185 T_WITH_ERRNO;
186 T_SKIP("could not find \"kern.development\" sysctl");
187 }
188
189 if (!supported) {
190 T_SKIP("test was run even though kern.development is not 1, see rdar://111297938");
191 }
192 }
193
194 T_DECL(entitled_game_mode_check_count, "game mode bit should affect coalition thread group flags",
195 T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
196 {
197 T_LOG("uid: %d", getuid());
198 skip_if_unsupported();
199
200 check_game_mode(false);
201 check_game_mode_count(0);
202
203 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
204 "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
205
206 check_game_mode(true);
207 check_game_mode_count(1);
208
209 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
210 "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
211
212 check_game_mode(false);
213 check_game_mode_count(0);
214 }
215
216 T_DECL(game_mode_child_exit, "game mode bit should disappear when child exits",
217 T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
218 {
219 T_LOG("uid: %d", getuid());
220 skip_if_unsupported();
221
222 check_game_mode(false);
223 check_game_mode_count(0);
224
225 T_LOG("Spawning child");
226
227 pid_t child_pid = fork();
228
229 if (child_pid == 0) {
230 /* child process */
231
232 check_game_mode(false);
233 check_game_mode_count(0);
234
235 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
236 "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
237
238 check_game_mode(true);
239 check_game_mode_count(1);
240
241 T_LOG("Exit pid %d with the game mode bit on", getpid());
242
243 exit(0);
244 } else {
245 T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
246
247 /* wait for child process to exit */
248 int exit_status = 0, signum = 0;
249
250 T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
251 "wait for child (%d) complete", child_pid);
252
253 T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
254 T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
255 }
256
257 check_game_mode(false);
258 check_game_mode_count(0);
259 }
260
261
262 T_DECL(game_mode_double_set_and_child_exit, "game mode bit on parent should stay when game mode child exits",
263 T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
264 {
265 T_LOG("uid: %d", getuid());
266 skip_if_unsupported();
267
268 check_game_mode(false);
269 check_game_mode_count(0);
270
271 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
272 "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
273
274 check_game_mode(true);
275 check_game_mode_count(1);
276
277 pid_t child_pid = fork();
278
279 if (child_pid == 0) {
280 /* child process */
281
282 check_game_mode(false);
283 check_game_mode_count(1);
284
285 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
286 "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
287
288 check_game_mode(true);
289 check_game_mode_count(2);
290
291 T_LOG("Exit pid %d with the game mode bit on", getpid());
292 exit(0);
293 } else {
294 T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
295
296 /* wait for child process to exit */
297 int exit_status = 0, signum = 0;
298
299 T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
300 "wait for child (%d) complete", child_pid);
301
302 T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
303 T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
304 }
305
306 check_game_mode(true);
307 check_game_mode_count(1);
308
309 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
310 "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
311
312 check_game_mode(false);
313 check_game_mode_count(0);
314 }
315
316 T_DECL(game_mode_double_set_and_child_unset, "game mode bit on parent should stay when game mode child unsets",
317 T_META_REQUIRES_SYSCTL_EQ("kern.development", 1), T_META_REQUIRES_SYSCTL_EQ("kern.thread_groups_supported", 1))
318 {
319 T_LOG("uid: %d", getuid());
320 skip_if_unsupported();
321
322 check_game_mode(false);
323 check_game_mode_count(0);
324
325 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
326 "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
327
328 check_game_mode(true);
329 check_game_mode_count(1);
330
331 pid_t child_pid = fork();
332
333 if (child_pid == 0) {
334 /* child process */
335
336 check_game_mode(false);
337 check_game_mode_count(1);
338
339 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_ON),
340 "setpriority(PRIO_DARWIN_GAME_MODE_ON)");
341
342 check_game_mode(true);
343 check_game_mode_count(2);
344
345 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
346 "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
347
348 check_game_mode(false);
349 check_game_mode_count(1);
350
351 T_LOG("Exit pid %d with the game mode bit off", getpid());
352 exit(0);
353 } else {
354 T_ASSERT_POSIX_SUCCESS(child_pid, "fork, pid %d", child_pid);
355
356 /* wait for child process to exit */
357 int exit_status = 0, signum = 0;
358
359 T_ASSERT_TRUE(dt_waitpid(child_pid, &exit_status, &signum, 5),
360 "wait for child (%d) complete", child_pid);
361
362 T_QUIET; T_ASSERT_EQ(exit_status, 0, "dt_waitpid: exit_status");
363 T_QUIET; T_ASSERT_EQ(signum, 0, "dt_waitpid: signum");
364 }
365
366 check_game_mode(true);
367 check_game_mode_count(1);
368
369 T_ASSERT_POSIX_SUCCESS(setpriority(PRIO_DARWIN_GAME_MODE, 0, PRIO_DARWIN_GAME_MODE_OFF),
370 "setpriority(PRIO_DARWIN_GAME_MODE_OFF)");
371
372 check_game_mode(false);
373 check_game_mode_count(0);
374 }
375