xref: /xnu-10063.141.1/tests/task_info.c (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1*d8b80295SApple OSS Distributions #include <darwintest.h>
2*d8b80295SApple OSS Distributions #include <darwintest_utils.h>
3*d8b80295SApple OSS Distributions #include <errno.h>
4*d8b80295SApple OSS Distributions #include <mach/mach.h>
5*d8b80295SApple OSS Distributions #include <mach/mach_error.h>
6*d8b80295SApple OSS Distributions #include <mach/policy.h>
7*d8b80295SApple OSS Distributions #include <mach/task_info.h>
8*d8b80295SApple OSS Distributions #include <mach/thread_info.h>
9*d8b80295SApple OSS Distributions #include <signal.h>
10*d8b80295SApple OSS Distributions #include <stdio.h>
11*d8b80295SApple OSS Distributions #include <stdlib.h>
12*d8b80295SApple OSS Distributions #include <sys/mman.h>
13*d8b80295SApple OSS Distributions #include <sys/sysctl.h>
14*d8b80295SApple OSS Distributions #include <unistd.h>
15*d8b80295SApple OSS Distributions 
16*d8b80295SApple OSS Distributions #include "test_utils.h"
17*d8b80295SApple OSS Distributions 
18*d8b80295SApple OSS Distributions T_GLOBAL_META(T_META_RUN_CONCURRENTLY(true));
19*d8b80295SApple OSS Distributions 
20*d8b80295SApple OSS Distributions /* *************************************************************************************
21*d8b80295SApple OSS Distributions  * Test the task_info API.
22*d8b80295SApple OSS Distributions  *
23*d8b80295SApple OSS Distributions  * This is a functional test of the following APIs:
24*d8b80295SApple OSS Distributions  * TASK_BASIC_INFO_32
25*d8b80295SApple OSS Distributions  * TASK_BASIC2_INFO_32
26*d8b80295SApple OSS Distributions  * TASK_BASIC_INFO_64
27*d8b80295SApple OSS Distributions  * TASK_BASIC_INFO_64_2
28*d8b80295SApple OSS Distributions  * TASK_POWER_INFO_V2
29*d8b80295SApple OSS Distributions  * TASK_FLAGS_INFO
30*d8b80295SApple OSS Distributions  * TASK_AFFINITY_TAG_INFO
31*d8b80295SApple OSS Distributions  * TASK_THREAD_TIMES_INFO
32*d8b80295SApple OSS Distributions  * TASK_ABSOLUTE_TIME_INFO
33*d8b80295SApple OSS Distributions  * <rdar://problem/22242021> Add tests to increase code coverage for the task_info API
34*d8b80295SApple OSS Distributions  * *************************************************************************************
35*d8b80295SApple OSS Distributions  */
36*d8b80295SApple OSS Distributions #define TESTPHYSFOOTPRINTVAL 5
37*d8b80295SApple OSS Distributions #define CANARY 0x0f0f0f0f0f0f0f0fULL
38*d8b80295SApple OSS Distributions #if !defined(CONFIG_EMBEDDED)
39*d8b80295SApple OSS Distributions #define ABSOLUTE_MIN_USER_TIME_DIFF 150
40*d8b80295SApple OSS Distributions #define ABSOLUTE_MIN_SYSTEM_TIME_DIFF 300
41*d8b80295SApple OSS Distributions #endif
42*d8b80295SApple OSS Distributions 
43*d8b80295SApple OSS Distributions enum info_kind { INFO_32, INFO_64, INFO_32_2, INFO_64_2, INFO_MACH, INFO_MAX };
44*d8b80295SApple OSS Distributions 
45*d8b80295SApple OSS Distributions enum info_get { GET_SUSPEND_COUNT, GET_RESIDENT_SIZE, GET_VIRTUAL_SIZE, GET_USER_TIME, GET_SYS_TIME, GET_POLICY, GET_MAX_RES };
46*d8b80295SApple OSS Distributions 
47*d8b80295SApple OSS Distributions /*
48*d8b80295SApple OSS Distributions  * This function uses CPU cycles by doing a factorial computation.
49*d8b80295SApple OSS Distributions  */
50*d8b80295SApple OSS Distributions static void do_factorial_task(void);
51*d8b80295SApple OSS Distributions 
52*d8b80295SApple OSS Distributions void test_task_basic_info_32(void);
53*d8b80295SApple OSS Distributions void test_task_basic_info_64(void);
54*d8b80295SApple OSS Distributions void task_basic_info_32_debug(void);
55*d8b80295SApple OSS Distributions void task_basic2_info_32_warmup(void);
56*d8b80295SApple OSS Distributions void test_task_basic_info(enum info_kind kind);
57*d8b80295SApple OSS Distributions uint64_t info_get(enum info_kind kind, enum info_get get, void * data);
58*d8b80295SApple OSS Distributions 
59*d8b80295SApple OSS Distributions T_DECL(task_vm_info, "tests task vm info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
60*d8b80295SApple OSS Distributions {
61*d8b80295SApple OSS Distributions 	kern_return_t err;
62*d8b80295SApple OSS Distributions 	task_vm_info_data_t vm_info;
63*d8b80295SApple OSS Distributions 
64*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = TASK_VM_INFO_COUNT;
65*d8b80295SApple OSS Distributions 
66*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
67*d8b80295SApple OSS Distributions 
68*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
69*d8b80295SApple OSS Distributions 
70*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info return value !=0 for virtual_size\n");
71*d8b80295SApple OSS Distributions 
72*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.phys_footprint, 0ULL, "task_info return value !=0 for phys_footprint\n");
73*d8b80295SApple OSS Distributions 
74*d8b80295SApple OSS Distributions 	/*
75*d8b80295SApple OSS Distributions 	 * Test the REV0 version of TASK_VM_INFO. It should not change the value of phys_footprint.
76*d8b80295SApple OSS Distributions 	 */
77*d8b80295SApple OSS Distributions 
78*d8b80295SApple OSS Distributions 	count                  = TASK_VM_INFO_REV0_COUNT;
79*d8b80295SApple OSS Distributions 	vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
80*d8b80295SApple OSS Distributions 	vm_info.min_address    = CANARY;
81*d8b80295SApple OSS Distributions 	vm_info.max_address    = CANARY;
82*d8b80295SApple OSS Distributions 
83*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
84*d8b80295SApple OSS Distributions 
85*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
86*d8b80295SApple OSS Distributions 
87*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(count, TASK_VM_INFO_REV0_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV0_COUNT", count);
88*d8b80295SApple OSS Distributions 
89*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev0 call does not return 0 for virtual_size");
90*d8b80295SApple OSS Distributions 
91*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
92*d8b80295SApple OSS Distributions 	    "task_info --rev0 call returned value %llu for vm_info.phys_footprint.  Expected %u since this value should not be "
93*d8b80295SApple OSS Distributions 	    "modified by rev0",
94*d8b80295SApple OSS Distributions 	    vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
95*d8b80295SApple OSS Distributions 
96*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(vm_info.min_address, CANARY,
97*d8b80295SApple OSS Distributions 	    "task_info --rev0 call returned value 0x%llx for vm_info.min_address. Expected 0x%llx since this value should not "
98*d8b80295SApple OSS Distributions 	    "be modified by rev0",
99*d8b80295SApple OSS Distributions 	    vm_info.min_address, CANARY);
100*d8b80295SApple OSS Distributions 
101*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(vm_info.max_address, CANARY,
102*d8b80295SApple OSS Distributions 	    "task_info --rev0 call returned value 0x%llx for vm_info.max_address. Expected 0x%llx since this value should not "
103*d8b80295SApple OSS Distributions 	    "be modified by rev0",
104*d8b80295SApple OSS Distributions 	    vm_info.max_address, CANARY);
105*d8b80295SApple OSS Distributions 
106*d8b80295SApple OSS Distributions 	/*
107*d8b80295SApple OSS Distributions 	 * Test the REV1 version of TASK_VM_INFO.
108*d8b80295SApple OSS Distributions 	 */
109*d8b80295SApple OSS Distributions 
110*d8b80295SApple OSS Distributions 	count                  = TASK_VM_INFO_REV1_COUNT;
111*d8b80295SApple OSS Distributions 	vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
112*d8b80295SApple OSS Distributions 	vm_info.min_address    = CANARY;
113*d8b80295SApple OSS Distributions 	vm_info.max_address    = CANARY;
114*d8b80295SApple OSS Distributions 
115*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
116*d8b80295SApple OSS Distributions 
117*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
118*d8b80295SApple OSS Distributions 
119*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(count, TASK_VM_INFO_REV1_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV1_COUNT", count);
120*d8b80295SApple OSS Distributions 
121*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev1 call does not return 0 for virtual_size");
122*d8b80295SApple OSS Distributions 
123*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
124*d8b80295SApple OSS Distributions 	    "task_info --rev1 call returned value %llu for vm_info.phys_footprint.  Expected value is anything other than %u "
125*d8b80295SApple OSS Distributions 	    "since this value should not be modified by rev1",
126*d8b80295SApple OSS Distributions 	    vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
127*d8b80295SApple OSS Distributions 
128*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(vm_info.min_address, CANARY,
129*d8b80295SApple OSS Distributions 	    "task_info --rev1 call returned value 0x%llx for vm_info.min_address. Expected 0x%llx since this value should not "
130*d8b80295SApple OSS Distributions 	    "be modified by rev1",
131*d8b80295SApple OSS Distributions 	    vm_info.min_address, CANARY);
132*d8b80295SApple OSS Distributions 
133*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(vm_info.max_address, CANARY,
134*d8b80295SApple OSS Distributions 	    "task_info --rev1 call returned value 0x%llx for vm_info.max_address. Expected 0x%llx since this value should not "
135*d8b80295SApple OSS Distributions 	    "be modified by rev1",
136*d8b80295SApple OSS Distributions 	    vm_info.max_address, CANARY);
137*d8b80295SApple OSS Distributions 
138*d8b80295SApple OSS Distributions 	/*
139*d8b80295SApple OSS Distributions 	 * Test the REV2 version of TASK_VM_INFO.
140*d8b80295SApple OSS Distributions 	 */
141*d8b80295SApple OSS Distributions 
142*d8b80295SApple OSS Distributions 	count                  = TASK_VM_INFO_REV2_COUNT;
143*d8b80295SApple OSS Distributions 	vm_info.phys_footprint = TESTPHYSFOOTPRINTVAL;
144*d8b80295SApple OSS Distributions 	vm_info.min_address    = CANARY;
145*d8b80295SApple OSS Distributions 	vm_info.max_address    = CANARY;
146*d8b80295SApple OSS Distributions 
147*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
148*d8b80295SApple OSS Distributions 
149*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
150*d8b80295SApple OSS Distributions 
151*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(count, TASK_VM_INFO_REV2_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV2_COUNT\n", count);
152*d8b80295SApple OSS Distributions 
153*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.virtual_size, 0ULL, "task_info --rev2 call does not return 0 for virtual_size\n");
154*d8b80295SApple OSS Distributions 
155*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
156*d8b80295SApple OSS Distributions 	    "task_info --rev2 call returned value %llu for vm_info.phys_footprint.  Expected anything other than %u since this "
157*d8b80295SApple OSS Distributions 	    "value should be modified by rev2",
158*d8b80295SApple OSS Distributions 	    vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
159*d8b80295SApple OSS Distributions 
160*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.min_address, CANARY,
161*d8b80295SApple OSS Distributions 	    "task_info --rev2 call returned value 0x%llx for vm_info.min_address. Expected anything other than 0x%llx since "
162*d8b80295SApple OSS Distributions 	    "this value should be modified by rev2",
163*d8b80295SApple OSS Distributions 	    vm_info.min_address, CANARY);
164*d8b80295SApple OSS Distributions 
165*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.max_address, CANARY,
166*d8b80295SApple OSS Distributions 	    "task_info --rev2 call returned value 0x%llx for vm_info.max_address. Expected anything other than 0x%llx since "
167*d8b80295SApple OSS Distributions 	    "this value should be modified by rev2",
168*d8b80295SApple OSS Distributions 	    vm_info.max_address, CANARY);
169*d8b80295SApple OSS Distributions 
170*d8b80295SApple OSS Distributions 	/*
171*d8b80295SApple OSS Distributions 	 * Test the REV4 version of TASK_VM_INFO.
172*d8b80295SApple OSS Distributions 	 */
173*d8b80295SApple OSS Distributions 
174*d8b80295SApple OSS Distributions 	count                         = TASK_VM_INFO_REV4_COUNT;
175*d8b80295SApple OSS Distributions 	vm_info.phys_footprint        = TESTPHYSFOOTPRINTVAL;
176*d8b80295SApple OSS Distributions 	vm_info.min_address           = CANARY;
177*d8b80295SApple OSS Distributions 	vm_info.max_address           = CANARY;
178*d8b80295SApple OSS Distributions 	vm_info.limit_bytes_remaining = CANARY;
179*d8b80295SApple OSS Distributions 
180*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_VM_INFO_PURGEABLE, (task_info_t)&vm_info, &count);
181*d8b80295SApple OSS Distributions 
182*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
183*d8b80295SApple OSS Distributions 
184*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(count, TASK_VM_INFO_REV4_COUNT, "task_info count(%d) is equal to TASK_VM_INFO_REV4_COUNT\n", count);
185*d8b80295SApple OSS Distributions 
186*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.phys_footprint, (unsigned long long)TESTPHYSFOOTPRINTVAL,
187*d8b80295SApple OSS Distributions 	    "task_info --rev4 call returned value %llu for vm_info.phys_footprint.  Expected anything other than %u since this "
188*d8b80295SApple OSS Distributions 	    "value should be modified by rev4",
189*d8b80295SApple OSS Distributions 	    vm_info.phys_footprint, TESTPHYSFOOTPRINTVAL);
190*d8b80295SApple OSS Distributions 
191*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.min_address, CANARY,
192*d8b80295SApple OSS Distributions 	    "task_info --rev4 call returned value 0x%llx for vm_info.min_address. Expected anything other than 0x%llx since "
193*d8b80295SApple OSS Distributions 	    "this value should be modified by rev4",
194*d8b80295SApple OSS Distributions 	    vm_info.min_address, CANARY);
195*d8b80295SApple OSS Distributions 
196*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.max_address, CANARY,
197*d8b80295SApple OSS Distributions 	    "task_info --rev4 call returned value 0x%llx for vm_info.max_address. Expected anything other than 0x%llx since "
198*d8b80295SApple OSS Distributions 	    "this value should be modified by rev4",
199*d8b80295SApple OSS Distributions 	    vm_info.max_address, CANARY);
200*d8b80295SApple OSS Distributions 
201*d8b80295SApple OSS Distributions 	T_EXPECT_NE(vm_info.limit_bytes_remaining, CANARY,
202*d8b80295SApple OSS Distributions 	    "task_info --rev4 call returned value 0x%llx for vm_info.limit_bytes_remaining. Expected anything other than 0x%llx since "
203*d8b80295SApple OSS Distributions 	    "this value should be modified by rev4",
204*d8b80295SApple OSS Distributions 	    vm_info.limit_bytes_remaining, CANARY);
205*d8b80295SApple OSS Distributions }
206*d8b80295SApple OSS Distributions 
207*d8b80295SApple OSS Distributions T_DECL(host_debug_info, "tests host debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
208*d8b80295SApple OSS Distributions {
209*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
210*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
211*d8b80295SApple OSS Distributions 	T_QUIET;
212*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
213*d8b80295SApple OSS Distributions 	T_SETUPEND;
214*d8b80295SApple OSS Distributions 
215*d8b80295SApple OSS Distributions 	kern_return_t err;
216*d8b80295SApple OSS Distributions 	mach_port_t host;
217*d8b80295SApple OSS Distributions 	host_debug_info_internal_data_t debug_info;
218*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = HOST_DEBUG_INFO_INTERNAL_COUNT;
219*d8b80295SApple OSS Distributions 	host                         = mach_host_self();
220*d8b80295SApple OSS Distributions 	err                          = host_info(host, HOST_DEBUG_INFO_INTERNAL, (host_info_t)&debug_info, &count);
221*d8b80295SApple OSS Distributions 
222*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify host_info call succeeded");
223*d8b80295SApple OSS Distributions }
224*d8b80295SApple OSS Distributions 
225*d8b80295SApple OSS Distributions T_DECL(task_debug_info, "tests task debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
226*d8b80295SApple OSS Distributions {
227*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
228*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
229*d8b80295SApple OSS Distributions 	T_QUIET;
230*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
231*d8b80295SApple OSS Distributions 	T_SETUPEND;
232*d8b80295SApple OSS Distributions 
233*d8b80295SApple OSS Distributions 	kern_return_t err;
234*d8b80295SApple OSS Distributions 	task_debug_info_internal_data_t debug_info;
235*d8b80295SApple OSS Distributions 
236*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = TASK_DEBUG_INFO_INTERNAL_COUNT;
237*d8b80295SApple OSS Distributions 
238*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &count);
239*d8b80295SApple OSS Distributions 
240*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
241*d8b80295SApple OSS Distributions }
242*d8b80295SApple OSS Distributions 
243*d8b80295SApple OSS Distributions T_DECL(thread_debug_info, "tests thread debug info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
244*d8b80295SApple OSS Distributions {
245*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
246*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
247*d8b80295SApple OSS Distributions 	T_QUIET;
248*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
249*d8b80295SApple OSS Distributions 	T_SETUPEND;
250*d8b80295SApple OSS Distributions 
251*d8b80295SApple OSS Distributions 	kern_return_t err;
252*d8b80295SApple OSS Distributions 	thread_debug_info_internal_data_t debug_info;
253*d8b80295SApple OSS Distributions 
254*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = THREAD_DEBUG_INFO_INTERNAL_COUNT;
255*d8b80295SApple OSS Distributions 
256*d8b80295SApple OSS Distributions 	err = thread_info(mach_thread_self(), THREAD_DEBUG_INFO_INTERNAL, (thread_info_t)&debug_info, &count);
257*d8b80295SApple OSS Distributions 
258*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
259*d8b80295SApple OSS Distributions }
260*d8b80295SApple OSS Distributions 
261*d8b80295SApple OSS Distributions static void
do_factorial_task()262*d8b80295SApple OSS Distributions do_factorial_task()
263*d8b80295SApple OSS Distributions {
264*d8b80295SApple OSS Distributions 	int number    = 20;
265*d8b80295SApple OSS Distributions 	int factorial = 1;
266*d8b80295SApple OSS Distributions 	int i;
267*d8b80295SApple OSS Distributions 	for (i = 1; i <= number; i++) {
268*d8b80295SApple OSS Distributions 		factorial *= i;
269*d8b80295SApple OSS Distributions 	}
270*d8b80295SApple OSS Distributions 
271*d8b80295SApple OSS Distributions 	return;
272*d8b80295SApple OSS Distributions }
273*d8b80295SApple OSS Distributions 
274*d8b80295SApple OSS Distributions T_DECL(task_thread_times_info, "tests task thread times info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
275*d8b80295SApple OSS Distributions {
276*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
277*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
278*d8b80295SApple OSS Distributions 	T_QUIET;
279*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
280*d8b80295SApple OSS Distributions 	T_SETUPEND;
281*d8b80295SApple OSS Distributions 
282*d8b80295SApple OSS Distributions 	kern_return_t err;
283*d8b80295SApple OSS Distributions 	task_thread_times_info_data_t thread_times_info_data;
284*d8b80295SApple OSS Distributions 	task_thread_times_info_data_t thread_times_info_data_new;
285*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = TASK_THREAD_TIMES_INFO_COUNT;
286*d8b80295SApple OSS Distributions 
287*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data, &count);
288*d8b80295SApple OSS Distributions 
289*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
290*d8b80295SApple OSS Distributions 
291*d8b80295SApple OSS Distributions 	do_factorial_task();
292*d8b80295SApple OSS Distributions 
293*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data_new, &count);
294*d8b80295SApple OSS Distributions 
295*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
296*d8b80295SApple OSS Distributions 
297*d8b80295SApple OSS Distributions 	/*
298*d8b80295SApple OSS Distributions 	 * The difference is observed to be less than 30 microseconds for user_time
299*d8b80295SApple OSS Distributions 	 * and less than 50 microseconds for system_time. This observation was done for over
300*d8b80295SApple OSS Distributions 	 * 1000 runs.
301*d8b80295SApple OSS Distributions 	 */
302*d8b80295SApple OSS Distributions 
303*d8b80295SApple OSS Distributions 	T_EXPECT_FALSE((thread_times_info_data_new.user_time.seconds - thread_times_info_data.user_time.seconds) != 0 ||
304*d8b80295SApple OSS Distributions 	    (thread_times_info_data_new.system_time.seconds - thread_times_info_data.system_time.seconds) != 0,
305*d8b80295SApple OSS Distributions 	    "Tests whether the difference between thread times is greater than the allowed limit");
306*d8b80295SApple OSS Distributions 
307*d8b80295SApple OSS Distributions 	/*
308*d8b80295SApple OSS Distributions 	 * This is a negative case.
309*d8b80295SApple OSS Distributions 	 */
310*d8b80295SApple OSS Distributions 
311*d8b80295SApple OSS Distributions 	count--;
312*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_THREAD_TIMES_INFO, (task_info_t)&thread_times_info_data, &count);
313*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
314*d8b80295SApple OSS Distributions 	    "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
315*d8b80295SApple OSS Distributions }
316*d8b80295SApple OSS Distributions 
317*d8b80295SApple OSS Distributions T_DECL(task_absolutetime_info, "tests task absolute time info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
318*d8b80295SApple OSS Distributions {
319*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
320*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
321*d8b80295SApple OSS Distributions 	T_QUIET;
322*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
323*d8b80295SApple OSS Distributions 	T_SETUPEND;
324*d8b80295SApple OSS Distributions 
325*d8b80295SApple OSS Distributions 	kern_return_t err;
326*d8b80295SApple OSS Distributions 	uint64_t user_time_diff, system_time_diff;
327*d8b80295SApple OSS Distributions 	task_absolutetime_info_data_t absolute_time_info_data;
328*d8b80295SApple OSS Distributions 	task_absolutetime_info_data_t absolute_time_info_data_new;
329*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = TASK_ABSOLUTETIME_INFO_COUNT;
330*d8b80295SApple OSS Distributions 
331*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data, &count);
332*d8b80295SApple OSS Distributions 
333*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
334*d8b80295SApple OSS Distributions 
335*d8b80295SApple OSS Distributions 	do_factorial_task();
336*d8b80295SApple OSS Distributions 
337*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count);
338*d8b80295SApple OSS Distributions 
339*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
340*d8b80295SApple OSS Distributions 
341*d8b80295SApple OSS Distributions 	user_time_diff   = absolute_time_info_data_new.total_user - absolute_time_info_data.total_user;
342*d8b80295SApple OSS Distributions 	system_time_diff = absolute_time_info_data_new.total_system - absolute_time_info_data.total_system;
343*d8b80295SApple OSS Distributions 
344*d8b80295SApple OSS Distributions #if !defined(__arm64__)
345*d8b80295SApple OSS Distributions 	/*
346*d8b80295SApple OSS Distributions 	 * On embedded devices the difference is always zero.
347*d8b80295SApple OSS Distributions 	 * On non-embedded devices the difference occurs in this range. This was observed over ~10000 runs.
348*d8b80295SApple OSS Distributions 	 */
349*d8b80295SApple OSS Distributions 
350*d8b80295SApple OSS Distributions 	T_EXPECT_FALSE(user_time_diff < ABSOLUTE_MIN_USER_TIME_DIFF || system_time_diff < ABSOLUTE_MIN_SYSTEM_TIME_DIFF,
351*d8b80295SApple OSS Distributions 	    "Tests whether the difference between thread times is greater than the expected range");
352*d8b80295SApple OSS Distributions #endif
353*d8b80295SApple OSS Distributions 
354*d8b80295SApple OSS Distributions 	if (absolute_time_info_data.threads_user <= 0) {
355*d8b80295SApple OSS Distributions 		int precise_time_val = 0;
356*d8b80295SApple OSS Distributions 		size_t len           = sizeof(size_t);
357*d8b80295SApple OSS Distributions 
358*d8b80295SApple OSS Distributions 		T_LOG("User threads time is zero. This should only happen rarely and when precise_user_time is off");
359*d8b80295SApple OSS Distributions 
360*d8b80295SApple OSS Distributions 		err = sysctlbyname("kern.precise_user_kernel_time", &precise_time_val, &len, NULL, 0);
361*d8b80295SApple OSS Distributions 
362*d8b80295SApple OSS Distributions 		T_EXPECT_POSIX_SUCCESS(err, "performing sysctl to check precise_user_time");
363*d8b80295SApple OSS Distributions 
364*d8b80295SApple OSS Distributions 		T_LOG("kern.precise_user_kernel_time val = %d", precise_time_val);
365*d8b80295SApple OSS Distributions 
366*d8b80295SApple OSS Distributions 		T_EXPECT_FALSE(precise_time_val, "user thread time should only be zero when precise_user_kernel_time is disabled");
367*d8b80295SApple OSS Distributions 	} else {
368*d8b80295SApple OSS Distributions 		T_PASS("task_info should return non-zero value for user threads time = %llu", absolute_time_info_data.threads_user);
369*d8b80295SApple OSS Distributions 	}
370*d8b80295SApple OSS Distributions 
371*d8b80295SApple OSS Distributions #if !defined(__arm64__)
372*d8b80295SApple OSS Distributions 	/*
373*d8b80295SApple OSS Distributions 	 * On iOS, system threads are always zero. On OS X this value can be some large positive number.
374*d8b80295SApple OSS Distributions 	 * There is no real way to estimate the exact amount.
375*d8b80295SApple OSS Distributions 	 */
376*d8b80295SApple OSS Distributions 	T_EXPECT_NE(absolute_time_info_data.threads_system, 0ULL,
377*d8b80295SApple OSS Distributions 	    "task_info should return non-zero value for system threads time = %llu", absolute_time_info_data.threads_system);
378*d8b80295SApple OSS Distributions #endif
379*d8b80295SApple OSS Distributions 
380*d8b80295SApple OSS Distributions 	/*
381*d8b80295SApple OSS Distributions 	 * This is a negative case.
382*d8b80295SApple OSS Distributions 	 */
383*d8b80295SApple OSS Distributions 	count--;
384*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_ABSOLUTETIME_INFO, (task_info_t)&absolute_time_info_data_new, &count);
385*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
386*d8b80295SApple OSS Distributions 	    "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
387*d8b80295SApple OSS Distributions }
388*d8b80295SApple OSS Distributions 
389*d8b80295SApple OSS Distributions T_DECL(task_affinity_tag_info, "tests task_affinity_tag_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
390*d8b80295SApple OSS Distributions {
391*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
392*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
393*d8b80295SApple OSS Distributions 	T_QUIET;
394*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
395*d8b80295SApple OSS Distributions 	T_SETUPEND;
396*d8b80295SApple OSS Distributions 
397*d8b80295SApple OSS Distributions 	kern_return_t err;
398*d8b80295SApple OSS Distributions 	task_affinity_tag_info_data_t affinity_tag_info_data;
399*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = TASK_AFFINITY_TAG_INFO_COUNT;
400*d8b80295SApple OSS Distributions 
401*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count);
402*d8b80295SApple OSS Distributions 
403*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
404*d8b80295SApple OSS Distributions 
405*d8b80295SApple OSS Distributions 	/*
406*d8b80295SApple OSS Distributions 	 * The affinity is not set by default, hence expecting a zero value.
407*d8b80295SApple OSS Distributions 	 */
408*d8b80295SApple OSS Distributions 	T_ASSERT_FALSE(affinity_tag_info_data.min != 0 || affinity_tag_info_data.max != 0,
409*d8b80295SApple OSS Distributions 	    "task_info call returns non-zero min or max value");
410*d8b80295SApple OSS Distributions 
411*d8b80295SApple OSS Distributions 	/*
412*d8b80295SApple OSS Distributions 	 * This is a negative case.
413*d8b80295SApple OSS Distributions 	 */
414*d8b80295SApple OSS Distributions 	count--;
415*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_AFFINITY_TAG_INFO, (task_info_t)&affinity_tag_info_data, &count);
416*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
417*d8b80295SApple OSS Distributions 	    "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
418*d8b80295SApple OSS Distributions }
419*d8b80295SApple OSS Distributions 
420*d8b80295SApple OSS Distributions T_DECL(task_flags_info, "tests task_flags_info", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
421*d8b80295SApple OSS Distributions {
422*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
423*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
424*d8b80295SApple OSS Distributions 	T_QUIET;
425*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
426*d8b80295SApple OSS Distributions 	T_SETUPEND;
427*d8b80295SApple OSS Distributions 
428*d8b80295SApple OSS Distributions 	kern_return_t err;
429*d8b80295SApple OSS Distributions 	task_flags_info_data_t flags_info_data;
430*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = TASK_FLAGS_INFO_COUNT;
431*d8b80295SApple OSS Distributions 
432*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count);
433*d8b80295SApple OSS Distributions 
434*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
435*d8b80295SApple OSS Distributions 
436*d8b80295SApple OSS Distributions 	/* Change for 32-bit arch possibility?*/
437*d8b80295SApple OSS Distributions 	T_ASSERT_EQ((flags_info_data.flags & (unsigned int)(~(TF_LP64 | TF_64B_DATA))), 0U,
438*d8b80295SApple OSS Distributions 	    "task_info should only give out 64-bit addr/data flags");
439*d8b80295SApple OSS Distributions 
440*d8b80295SApple OSS Distributions 	/*
441*d8b80295SApple OSS Distributions 	 * This is a negative case.
442*d8b80295SApple OSS Distributions 	 */
443*d8b80295SApple OSS Distributions 
444*d8b80295SApple OSS Distributions 	count--;
445*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_FLAGS_INFO, (task_info_t)&flags_info_data, &count);
446*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
447*d8b80295SApple OSS Distributions 	    "Negative test case: task_info should verify that count is at least equal to what is defined in API.");
448*d8b80295SApple OSS Distributions }
449*d8b80295SApple OSS Distributions 
450*d8b80295SApple OSS Distributions T_DECL(task_power_info_v2, "tests task_power_info_v2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
451*d8b80295SApple OSS Distributions {
452*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
453*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
454*d8b80295SApple OSS Distributions 	T_QUIET;
455*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
456*d8b80295SApple OSS Distributions 	T_SETUPEND;
457*d8b80295SApple OSS Distributions 
458*d8b80295SApple OSS Distributions 	kern_return_t err;
459*d8b80295SApple OSS Distributions 	task_power_info_v2_data_t power_info_data_v2;
460*d8b80295SApple OSS Distributions 	task_power_info_v2_data_t power_info_data_v2_new;
461*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = TASK_POWER_INFO_V2_COUNT;
462*d8b80295SApple OSS Distributions 
463*d8b80295SApple OSS Distributions 	sleep(1);
464*d8b80295SApple OSS Distributions 
465*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
466*d8b80295SApple OSS Distributions 
467*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
468*d8b80295SApple OSS Distributions 
469*d8b80295SApple OSS Distributions 	T_ASSERT_LE(power_info_data_v2.gpu_energy.task_gpu_utilisation, 0ULL,
470*d8b80295SApple OSS Distributions 	    "verified task_info call shows zero GPU utilization for non-GPU task");
471*d8b80295SApple OSS Distributions 
472*d8b80295SApple OSS Distributions 	do_factorial_task();
473*d8b80295SApple OSS Distributions 
474*d8b80295SApple OSS Distributions 	/*
475*d8b80295SApple OSS Distributions 	 * Verify the cpu_energy parameters.
476*d8b80295SApple OSS Distributions 	 */
477*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2_new, &count);
478*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
479*d8b80295SApple OSS Distributions 
480*d8b80295SApple OSS Distributions #if !defined(__arm64__)
481*d8b80295SApple OSS Distributions 	/*
482*d8b80295SApple OSS Distributions 	 * iOS does not have system_time.
483*d8b80295SApple OSS Distributions 	 */
484*d8b80295SApple OSS Distributions 	T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_user, power_info_data_v2.cpu_energy.total_user,
485*d8b80295SApple OSS Distributions 	    "task_info call returns valid user time");
486*d8b80295SApple OSS Distributions 	T_ASSERT_GT(power_info_data_v2_new.cpu_energy.total_system, power_info_data_v2.cpu_energy.total_system,
487*d8b80295SApple OSS Distributions 	    "task_info call returns valid system time");
488*d8b80295SApple OSS Distributions #endif
489*d8b80295SApple OSS Distributions 
490*d8b80295SApple OSS Distributions 	T_ASSERT_GE(power_info_data_v2.cpu_energy.task_interrupt_wakeups, 1ULL,
491*d8b80295SApple OSS Distributions 	    "verify task_info call returns non-zero value for interrupt_wakeup (ret value = %llu)",
492*d8b80295SApple OSS Distributions 	    power_info_data_v2.cpu_energy.task_interrupt_wakeups);
493*d8b80295SApple OSS Distributions 
494*d8b80295SApple OSS Distributions #if !defined(__arm64__)
495*d8b80295SApple OSS Distributions 	if (power_info_data_v2.cpu_energy.task_platform_idle_wakeups != 0) {
496*d8b80295SApple OSS Distributions 		T_LOG("task_info call returned %llu for platform_idle_wakeup", power_info_data_v2.cpu_energy.task_platform_idle_wakeups);
497*d8b80295SApple OSS Distributions 	}
498*d8b80295SApple OSS Distributions #endif
499*d8b80295SApple OSS Distributions 
500*d8b80295SApple OSS Distributions 	count = TASK_POWER_INFO_V2_COUNT_OLD;
501*d8b80295SApple OSS Distributions 	err   = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
502*d8b80295SApple OSS Distributions 
503*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(err, "verify task_info call succeeded");
504*d8b80295SApple OSS Distributions 
505*d8b80295SApple OSS Distributions 	/*
506*d8b80295SApple OSS Distributions 	 * This is a negative case.
507*d8b80295SApple OSS Distributions 	 */
508*d8b80295SApple OSS Distributions 	count--;
509*d8b80295SApple OSS Distributions 	err = task_info(mach_task_self(), TASK_POWER_INFO_V2, (task_info_t)&power_info_data_v2, &count);
510*d8b80295SApple OSS Distributions 
511*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_ERROR(err, KERN_INVALID_ARGUMENT,
512*d8b80295SApple OSS Distributions 	    "Negative test case: task_info should verify that count is at least equal to what is defined in API. Call "
513*d8b80295SApple OSS Distributions 	    "returns errno %d:%s",
514*d8b80295SApple OSS Distributions 	    err, mach_error_string(err));
515*d8b80295SApple OSS Distributions }
516*d8b80295SApple OSS Distributions 
517*d8b80295SApple OSS Distributions T_DECL(test_task_basic_info_32, "tests TASK_BASIC_INFO_32", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
518*d8b80295SApple OSS Distributions {
519*d8b80295SApple OSS Distributions 	test_task_basic_info(INFO_32);
520*d8b80295SApple OSS Distributions }
521*d8b80295SApple OSS Distributions 
522*d8b80295SApple OSS Distributions T_DECL(test_task_basic_info_32_2, "tests TASK_BASIC_INFO_32_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
523*d8b80295SApple OSS Distributions {
524*d8b80295SApple OSS Distributions 	test_task_basic_info(INFO_32_2);
525*d8b80295SApple OSS Distributions }
526*d8b80295SApple OSS Distributions 
527*d8b80295SApple OSS Distributions #if defined(__arm64__)
528*d8b80295SApple OSS Distributions T_DECL(test_task_basic_info_64i_2, "tests TASK_BASIC_INFO_64_2", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
529*d8b80295SApple OSS Distributions {
530*d8b80295SApple OSS Distributions 	test_task_basic_info(INFO_64_2);
531*d8b80295SApple OSS Distributions }
532*d8b80295SApple OSS Distributions #else
533*d8b80295SApple OSS Distributions T_DECL(test_task_basic_info_64, "tests TASK_BASIC_INFO_64", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
534*d8b80295SApple OSS Distributions {
535*d8b80295SApple OSS Distributions 	test_task_basic_info(INFO_64);
536*d8b80295SApple OSS Distributions }
537*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
538*d8b80295SApple OSS Distributions 
539*d8b80295SApple OSS Distributions T_DECL(test_mach_task_basic_info, "tests MACH_TASK_BASIC_INFO", T_META_ASROOT(true), T_META_LTEPHASE(LTE_POSTINIT))
540*d8b80295SApple OSS Distributions {
541*d8b80295SApple OSS Distributions 	test_task_basic_info(INFO_MACH);
542*d8b80295SApple OSS Distributions }
543*d8b80295SApple OSS Distributions 
544*d8b80295SApple OSS Distributions void
test_task_basic_info(enum info_kind kind)545*d8b80295SApple OSS Distributions test_task_basic_info(enum info_kind kind)
546*d8b80295SApple OSS Distributions {
547*d8b80295SApple OSS Distributions #define BEFORE 0
548*d8b80295SApple OSS Distributions #define AFTER 1
549*d8b80295SApple OSS Distributions 
550*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
551*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
552*d8b80295SApple OSS Distributions 	T_QUIET;
553*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
554*d8b80295SApple OSS Distributions 	T_SETUPEND;
555*d8b80295SApple OSS Distributions 
556*d8b80295SApple OSS Distributions 	task_info_t info_data[2];
557*d8b80295SApple OSS Distributions 	task_basic_info_32_data_t basic_info_32_data[2];
558*d8b80295SApple OSS Distributions #if defined(__arm64__)
559*d8b80295SApple OSS Distributions 	task_basic_info_64_2_data_t basic_info_64_2_data[2];
560*d8b80295SApple OSS Distributions #else
561*d8b80295SApple OSS Distributions 	task_basic_info_64_data_t basic_info_64_data[2];
562*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
563*d8b80295SApple OSS Distributions 	mach_task_basic_info_data_t mach_basic_info_data[2];
564*d8b80295SApple OSS Distributions 
565*d8b80295SApple OSS Distributions 	kern_return_t kr;
566*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count;
567*d8b80295SApple OSS Distributions 	task_flavor_t flavor = 0;
568*d8b80295SApple OSS Distributions 	integer_t suspend_count;
569*d8b80295SApple OSS Distributions 	uint64_t resident_size_diff;
570*d8b80295SApple OSS Distributions 	uint64_t virtual_size_diff;
571*d8b80295SApple OSS Distributions 
572*d8b80295SApple OSS Distributions 	void * tmp_map = NULL;
573*d8b80295SApple OSS Distributions 	pid_t child_pid;
574*d8b80295SApple OSS Distributions 	mach_port_name_t child_task;
575*d8b80295SApple OSS Distributions 	/*for dt_waitpid*/
576*d8b80295SApple OSS Distributions 	int timeout     = 10; // change to max timeout
577*d8b80295SApple OSS Distributions 	int exit_status = 0;
578*d8b80295SApple OSS Distributions 
579*d8b80295SApple OSS Distributions 	switch (kind) {
580*d8b80295SApple OSS Distributions 	case INFO_32:
581*d8b80295SApple OSS Distributions 	case INFO_32_2:
582*d8b80295SApple OSS Distributions 		info_data[BEFORE] = (task_info_t)&basic_info_32_data[BEFORE];
583*d8b80295SApple OSS Distributions 		info_data[AFTER]  = (task_info_t)&basic_info_32_data[AFTER];
584*d8b80295SApple OSS Distributions 		count             = TASK_BASIC_INFO_32_COUNT;
585*d8b80295SApple OSS Distributions 		flavor            = TASK_BASIC_INFO_32;
586*d8b80295SApple OSS Distributions 
587*d8b80295SApple OSS Distributions 		if (kind == INFO_32_2) {
588*d8b80295SApple OSS Distributions 			flavor = TASK_BASIC2_INFO_32;
589*d8b80295SApple OSS Distributions 		}
590*d8b80295SApple OSS Distributions 
591*d8b80295SApple OSS Distributions 		break;
592*d8b80295SApple OSS Distributions #if defined(__arm64__)
593*d8b80295SApple OSS Distributions 	case INFO_64:
594*d8b80295SApple OSS Distributions 		T_ASSERT_FAIL("invalid basic info kind");
595*d8b80295SApple OSS Distributions 		break;
596*d8b80295SApple OSS Distributions 
597*d8b80295SApple OSS Distributions 	case INFO_64_2:
598*d8b80295SApple OSS Distributions 		info_data[BEFORE] = (task_info_t)&basic_info_64_2_data[BEFORE];
599*d8b80295SApple OSS Distributions 		info_data[AFTER]  = (task_info_t)&basic_info_64_2_data[AFTER];
600*d8b80295SApple OSS Distributions 		count             = TASK_BASIC_INFO_64_2_COUNT;
601*d8b80295SApple OSS Distributions 		flavor            = TASK_BASIC_INFO_64_2;
602*d8b80295SApple OSS Distributions 		break;
603*d8b80295SApple OSS Distributions 
604*d8b80295SApple OSS Distributions #else
605*d8b80295SApple OSS Distributions 	case INFO_64:
606*d8b80295SApple OSS Distributions 		info_data[BEFORE] = (task_info_t)&basic_info_64_data[BEFORE];
607*d8b80295SApple OSS Distributions 		info_data[AFTER]  = (task_info_t)&basic_info_64_data[AFTER];
608*d8b80295SApple OSS Distributions 		count             = TASK_BASIC_INFO_64_COUNT;
609*d8b80295SApple OSS Distributions 		flavor            = TASK_BASIC_INFO_64;
610*d8b80295SApple OSS Distributions 		break;
611*d8b80295SApple OSS Distributions 
612*d8b80295SApple OSS Distributions 	case INFO_64_2:
613*d8b80295SApple OSS Distributions 		T_ASSERT_FAIL("invalid basic info kind");
614*d8b80295SApple OSS Distributions 		break;
615*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
616*d8b80295SApple OSS Distributions 	case INFO_MACH:
617*d8b80295SApple OSS Distributions 		info_data[BEFORE] = (task_info_t)&mach_basic_info_data[BEFORE];
618*d8b80295SApple OSS Distributions 		info_data[AFTER]  = (task_info_t)&mach_basic_info_data[AFTER];
619*d8b80295SApple OSS Distributions 		count             = MACH_TASK_BASIC_INFO_COUNT;
620*d8b80295SApple OSS Distributions 		flavor            = MACH_TASK_BASIC_INFO;
621*d8b80295SApple OSS Distributions 		break;
622*d8b80295SApple OSS Distributions 	case INFO_MAX:
623*d8b80295SApple OSS Distributions 	default:
624*d8b80295SApple OSS Distributions 		T_ASSERT_FAIL("invalid basic info kind");
625*d8b80295SApple OSS Distributions 		break;
626*d8b80295SApple OSS Distributions 	}
627*d8b80295SApple OSS Distributions 
628*d8b80295SApple OSS Distributions 	kr = task_info(mach_task_self(), flavor, info_data[BEFORE], &count);
629*d8b80295SApple OSS Distributions 
630*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_info succeeded");
631*d8b80295SApple OSS Distributions 
632*d8b80295SApple OSS Distributions 	do_factorial_task();
633*d8b80295SApple OSS Distributions 
634*d8b80295SApple OSS Distributions 	/*
635*d8b80295SApple OSS Distributions 	 * Allocate virtual and resident memory.
636*d8b80295SApple OSS Distributions 	 */
637*d8b80295SApple OSS Distributions 	tmp_map = mmap(0, PAGE_SIZE, PROT_WRITE, MAP_ANON | MAP_PRIVATE, -1, 0);
638*d8b80295SApple OSS Distributions 
639*d8b80295SApple OSS Distributions 	T_WITH_ERRNO;
640*d8b80295SApple OSS Distributions 	T_EXPECT_NE(tmp_map, MAP_FAILED, "verify mmap call is successful");
641*d8b80295SApple OSS Distributions 
642*d8b80295SApple OSS Distributions 	memset(tmp_map, 'm', PAGE_SIZE);
643*d8b80295SApple OSS Distributions 
644*d8b80295SApple OSS Distributions 	child_pid = fork();
645*d8b80295SApple OSS Distributions 
646*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked");
647*d8b80295SApple OSS Distributions 
648*d8b80295SApple OSS Distributions 	if (child_pid == 0) {
649*d8b80295SApple OSS Distributions 		/*
650*d8b80295SApple OSS Distributions 		 * This will suspend the child process.
651*d8b80295SApple OSS Distributions 		 */
652*d8b80295SApple OSS Distributions 		kr = task_suspend(mach_task_self());
653*d8b80295SApple OSS Distributions 		exit(kr);
654*d8b80295SApple OSS Distributions 	}
655*d8b80295SApple OSS Distributions 
656*d8b80295SApple OSS Distributions 	/*
657*d8b80295SApple OSS Distributions 	 * Wait for the child process to suspend itself.
658*d8b80295SApple OSS Distributions 	 */
659*d8b80295SApple OSS Distributions 	sleep(1);
660*d8b80295SApple OSS Distributions 
661*d8b80295SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), child_pid, &child_task);
662*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded.  check sudo if failed");
663*d8b80295SApple OSS Distributions 
664*d8b80295SApple OSS Distributions 	/*
665*d8b80295SApple OSS Distributions 	 * Verify the suspend_count for child and resume it.
666*d8b80295SApple OSS Distributions 	 */
667*d8b80295SApple OSS Distributions 
668*d8b80295SApple OSS Distributions 	kr = task_info(child_task, flavor, info_data[AFTER], &count);
669*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
670*d8b80295SApple OSS Distributions 
671*d8b80295SApple OSS Distributions 	suspend_count = (integer_t)(info_get(kind, GET_SUSPEND_COUNT, info_data[AFTER]));
672*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct suspend_count");
673*d8b80295SApple OSS Distributions 
674*d8b80295SApple OSS Distributions 	kr = task_resume(child_task);
675*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_resume succeeded");
676*d8b80295SApple OSS Distributions 
677*d8b80295SApple OSS Distributions 	/*
678*d8b80295SApple OSS Distributions 	 * reap kr from task_suspend call in child
679*d8b80295SApple OSS Distributions 	 */
680*d8b80295SApple OSS Distributions 	if (dt_waitpid(child_pid, &exit_status, NULL, timeout)) {
681*d8b80295SApple OSS Distributions 		T_ASSERT_MACH_SUCCESS(exit_status, "verify child task_suspend is successful");
682*d8b80295SApple OSS Distributions 	} else {
683*d8b80295SApple OSS Distributions 		T_FAIL("dt_waitpid failed");
684*d8b80295SApple OSS Distributions 	}
685*d8b80295SApple OSS Distributions 
686*d8b80295SApple OSS Distributions 	kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count);
687*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
688*d8b80295SApple OSS Distributions 
689*d8b80295SApple OSS Distributions 	resident_size_diff = info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]) - info_get(kind, GET_RESIDENT_SIZE, info_data[BEFORE]);
690*d8b80295SApple OSS Distributions 	virtual_size_diff  = info_get(kind, GET_VIRTUAL_SIZE, info_data[AFTER]) - info_get(kind, GET_VIRTUAL_SIZE, info_data[BEFORE]);
691*d8b80295SApple OSS Distributions 
692*d8b80295SApple OSS Distributions 	/*
693*d8b80295SApple OSS Distributions 	 * INFO_32_2 gets the max resident size instead of the current resident size
694*d8b80295SApple OSS Distributions 	 * 32 KB tolerance built into test.  The returned value is generally between 0 and 16384
695*d8b80295SApple OSS Distributions 	 *
696*d8b80295SApple OSS Distributions 	 * max resident size is a discrete field in INFO_MACH, so it's handled differently
697*d8b80295SApple OSS Distributions 	 */
698*d8b80295SApple OSS Distributions 	if (kind == INFO_32_2) {
699*d8b80295SApple OSS Distributions 		T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size");
700*d8b80295SApple OSS Distributions 		T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size");
701*d8b80295SApple OSS Distributions 		T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "verify task_info returns valid virtual_size");
702*d8b80295SApple OSS Distributions 	} else {
703*d8b80295SApple OSS Distributions 		T_EXPECT_GE(resident_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid resident_size");
704*d8b80295SApple OSS Distributions 		T_EXPECT_GE(virtual_size_diff, (unsigned long long)PAGE_SIZE, "task_info returns valid virtual_size");
705*d8b80295SApple OSS Distributions 	}
706*d8b80295SApple OSS Distributions 
707*d8b80295SApple OSS Distributions 	if (kind == INFO_MACH) {
708*d8b80295SApple OSS Distributions 		resident_size_diff = info_get(kind, GET_MAX_RES, info_data[AFTER]) - info_get(kind, GET_MAX_RES, info_data[BEFORE]);
709*d8b80295SApple OSS Distributions 		T_EXPECT_EQ(resident_size_diff % 4096, 0ULL, "verify task_info returns valid max resident_size");
710*d8b80295SApple OSS Distributions 		T_EXPECT_GE(resident_size_diff, 0ULL, "verify task_info returns non-negative max resident_size");
711*d8b80295SApple OSS Distributions 		T_EXPECT_GE(info_get(kind, GET_MAX_RES, info_data[AFTER]), info_get(kind, GET_RESIDENT_SIZE, info_data[AFTER]),
712*d8b80295SApple OSS Distributions 		    "verify max resident size is greater than or equal to curr resident size");
713*d8b80295SApple OSS Distributions 	}
714*d8b80295SApple OSS Distributions 
715*d8b80295SApple OSS Distributions 	do_factorial_task();
716*d8b80295SApple OSS Distributions 
717*d8b80295SApple OSS Distributions 	/*
718*d8b80295SApple OSS Distributions 	 * These counters give time for threads that have terminated. We dont have any, so checking for zero.
719*d8b80295SApple OSS Distributions 	 */
720*d8b80295SApple OSS Distributions 
721*d8b80295SApple OSS Distributions 	time_value_t * user_tv = (time_value_t *)(info_get(kind, GET_USER_TIME, info_data[BEFORE]));
722*d8b80295SApple OSS Distributions 	T_EXPECT_EQ((user_tv->seconds + user_tv->microseconds / 1000000), 0, "verify task_info shows valid user time");
723*d8b80295SApple OSS Distributions 
724*d8b80295SApple OSS Distributions 	time_value_t * sys_tv = (time_value_t *)(info_get(kind, GET_SYS_TIME, info_data[BEFORE]));
725*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(sys_tv->seconds + (sys_tv->microseconds / 1000000), 0, "verify task_info shows valid system time");
726*d8b80295SApple OSS Distributions 
727*d8b80295SApple OSS Distributions 	/*
728*d8b80295SApple OSS Distributions 	 * The default value for non-kernel tasks is TIMESHARE.
729*d8b80295SApple OSS Distributions 	 */
730*d8b80295SApple OSS Distributions 
731*d8b80295SApple OSS Distributions 	policy_t pt = (policy_t)info_get(kind, GET_POLICY, info_data[BEFORE]);
732*d8b80295SApple OSS Distributions 
733*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(pt, POLICY_TIMESHARE, "verify task_info shows valid policy");
734*d8b80295SApple OSS Distributions 
735*d8b80295SApple OSS Distributions 	/*
736*d8b80295SApple OSS Distributions 	 * This is a negative case.
737*d8b80295SApple OSS Distributions 	 */
738*d8b80295SApple OSS Distributions 
739*d8b80295SApple OSS Distributions 	count--;
740*d8b80295SApple OSS Distributions 	kr = task_info(mach_task_self(), flavor, info_data[AFTER], &count);
741*d8b80295SApple OSS Distributions 
742*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_ERROR(kr, KERN_INVALID_ARGUMENT,
743*d8b80295SApple OSS Distributions 	    "Negative test case: task_info should verify that count is at least equal to what is defined in API");
744*d8b80295SApple OSS Distributions 
745*d8b80295SApple OSS Distributions 	/*
746*d8b80295SApple OSS Distributions 	 * deallocate memory
747*d8b80295SApple OSS Distributions 	 */
748*d8b80295SApple OSS Distributions 	munmap(tmp_map, PAGE_SIZE);
749*d8b80295SApple OSS Distributions 
750*d8b80295SApple OSS Distributions 	return;
751*d8b80295SApple OSS Distributions 
752*d8b80295SApple OSS Distributions #undef BEFORE
753*d8b80295SApple OSS Distributions #undef AFTER
754*d8b80295SApple OSS Distributions }
755*d8b80295SApple OSS Distributions 
756*d8b80295SApple OSS Distributions T_DECL(test_sigcont_task_suspend_resume,
757*d8b80295SApple OSS Distributions     "test to verify that SIGCONT on task_suspend()-ed process works",
758*d8b80295SApple OSS Distributions     T_META_ASROOT(true),
759*d8b80295SApple OSS Distributions     T_META_LTEPHASE(LTE_POSTINIT))
760*d8b80295SApple OSS Distributions {
761*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
762*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
763*d8b80295SApple OSS Distributions 	T_QUIET;
764*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
765*d8b80295SApple OSS Distributions 	T_SETUPEND;
766*d8b80295SApple OSS Distributions 
767*d8b80295SApple OSS Distributions 	mach_task_basic_info_data_t mach_basic_info_data;
768*d8b80295SApple OSS Distributions 	task_info_t info_data = (task_info_t)&mach_basic_info_data;
769*d8b80295SApple OSS Distributions 
770*d8b80295SApple OSS Distributions 	task_debug_info_internal_data_t debug_info;
771*d8b80295SApple OSS Distributions 	mach_msg_type_number_t debug_count = TASK_DEBUG_INFO_INTERNAL_COUNT;
772*d8b80295SApple OSS Distributions 
773*d8b80295SApple OSS Distributions 	kern_return_t kr;
774*d8b80295SApple OSS Distributions 	int posix_ret;
775*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count = MACH_TASK_BASIC_INFO_COUNT;
776*d8b80295SApple OSS Distributions 	task_flavor_t flavor         = MACH_TASK_BASIC_INFO;
777*d8b80295SApple OSS Distributions 	integer_t suspend_count;
778*d8b80295SApple OSS Distributions 	integer_t debug_suspend_count;
779*d8b80295SApple OSS Distributions 	pid_t child_pid = 0;
780*d8b80295SApple OSS Distributions 	mach_port_name_t child_task;
781*d8b80295SApple OSS Distributions 	/*for dt_waitpid*/
782*d8b80295SApple OSS Distributions 	int timeout     = 5;
783*d8b80295SApple OSS Distributions 	int exit_status = 0;
784*d8b80295SApple OSS Distributions 	int signal_no   = 0;
785*d8b80295SApple OSS Distributions 
786*d8b80295SApple OSS Distributions 	child_pid = fork();
787*d8b80295SApple OSS Distributions 
788*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked");
789*d8b80295SApple OSS Distributions 
790*d8b80295SApple OSS Distributions 	if (child_pid == 0) {
791*d8b80295SApple OSS Distributions 		/*
792*d8b80295SApple OSS Distributions 		 * This will suspend the child process.
793*d8b80295SApple OSS Distributions 		 */
794*d8b80295SApple OSS Distributions 		kr = task_suspend(mach_task_self());
795*d8b80295SApple OSS Distributions 
796*d8b80295SApple OSS Distributions 		/*
797*d8b80295SApple OSS Distributions 		 * When child resumes, it exits immediately
798*d8b80295SApple OSS Distributions 		 */
799*d8b80295SApple OSS Distributions 
800*d8b80295SApple OSS Distributions 		exit(kr);
801*d8b80295SApple OSS Distributions 	}
802*d8b80295SApple OSS Distributions 
803*d8b80295SApple OSS Distributions 	/*
804*d8b80295SApple OSS Distributions 	 * Wait for the child process to suspend itself.
805*d8b80295SApple OSS Distributions 	 */
806*d8b80295SApple OSS Distributions 	sleep(1);
807*d8b80295SApple OSS Distributions 
808*d8b80295SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), child_pid, &child_task);
809*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded.  check sudo if failed");
810*d8b80295SApple OSS Distributions 
811*d8b80295SApple OSS Distributions 	/*
812*d8b80295SApple OSS Distributions 	 * Verify the suspend_count for child and resume it.
813*d8b80295SApple OSS Distributions 	 */
814*d8b80295SApple OSS Distributions 
815*d8b80295SApple OSS Distributions 	kr = task_info(child_task, flavor, info_data, &count);
816*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
817*d8b80295SApple OSS Distributions 
818*d8b80295SApple OSS Distributions 	suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data));
819*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct suspend_count (1) (actually user stop count) ");
820*d8b80295SApple OSS Distributions 
821*d8b80295SApple OSS Distributions 	kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count);
822*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
823*d8b80295SApple OSS Distributions 
824*d8b80295SApple OSS Distributions 	debug_suspend_count = debug_info.suspend_count;
825*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(debug_info.suspend_count, 1, "verify debug_info shows correct suspend_count(1)");
826*d8b80295SApple OSS Distributions 
827*d8b80295SApple OSS Distributions 	posix_ret = kill(child_pid, SIGCONT);
828*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_ret, "verify signal call succeeded");
829*d8b80295SApple OSS Distributions 
830*d8b80295SApple OSS Distributions 	/*
831*d8b80295SApple OSS Distributions 	 * reap kr from task_suspend call in child
832*d8b80295SApple OSS Distributions 	 */
833*d8b80295SApple OSS Distributions 	dt_waitpid(child_pid, &exit_status, &signal_no, timeout);
834*d8b80295SApple OSS Distributions 
835*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(signal_no, 0, "child should be resumed and exit without signal");
836*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(exit_status, 0, "child should exit with 0");
837*d8b80295SApple OSS Distributions }
838*d8b80295SApple OSS Distributions 
839*d8b80295SApple OSS Distributions T_DECL(test_sigcont_task_suspend2_resume,
840*d8b80295SApple OSS Distributions     "test to verify that SIGCONT on task_suspend2()-ed process doesn't work",
841*d8b80295SApple OSS Distributions     T_META_ASROOT(true),
842*d8b80295SApple OSS Distributions     T_META_LTEPHASE(LTE_POSTINIT))
843*d8b80295SApple OSS Distributions {
844*d8b80295SApple OSS Distributions 	T_SETUPBEGIN;
845*d8b80295SApple OSS Distributions 	int is_dev = is_development_kernel();
846*d8b80295SApple OSS Distributions 	T_QUIET;
847*d8b80295SApple OSS Distributions 	T_ASSERT_TRUE(is_dev, "verify development kernel is running");
848*d8b80295SApple OSS Distributions 	T_SETUPEND;
849*d8b80295SApple OSS Distributions 
850*d8b80295SApple OSS Distributions 	mach_task_basic_info_data_t mach_basic_info_data;
851*d8b80295SApple OSS Distributions 	task_info_t info_data = (task_info_t)&mach_basic_info_data;
852*d8b80295SApple OSS Distributions 
853*d8b80295SApple OSS Distributions 	task_debug_info_internal_data_t debug_info;
854*d8b80295SApple OSS Distributions 	mach_msg_type_number_t debug_count = TASK_DEBUG_INFO_INTERNAL_COUNT;
855*d8b80295SApple OSS Distributions 
856*d8b80295SApple OSS Distributions 	kern_return_t kr;
857*d8b80295SApple OSS Distributions 	int posix_ret;
858*d8b80295SApple OSS Distributions 	mach_msg_type_number_t count  = MACH_TASK_BASIC_INFO_COUNT;
859*d8b80295SApple OSS Distributions 	task_flavor_t flavor          = MACH_TASK_BASIC_INFO;
860*d8b80295SApple OSS Distributions 	integer_t suspend_count       = 0;
861*d8b80295SApple OSS Distributions 	integer_t debug_suspend_count = 0;
862*d8b80295SApple OSS Distributions 	pid_t child_pid               = 0;
863*d8b80295SApple OSS Distributions 	mach_port_name_t child_task;
864*d8b80295SApple OSS Distributions 	task_suspension_token_t child_token = 0xFFFFF;
865*d8b80295SApple OSS Distributions 
866*d8b80295SApple OSS Distributions 	/*
867*d8b80295SApple OSS Distributions 	 * for dt_waitpid
868*d8b80295SApple OSS Distributions 	 * We expect the test to fail right now, so I've set timeout to
869*d8b80295SApple OSS Distributions 	 * be shorter than we may want it to be when the issue is fixed
870*d8b80295SApple OSS Distributions 	 */
871*d8b80295SApple OSS Distributions 	int timeout     = 1;
872*d8b80295SApple OSS Distributions 	int exit_status = 0;
873*d8b80295SApple OSS Distributions 	int signal_no   = 0;
874*d8b80295SApple OSS Distributions 
875*d8b80295SApple OSS Distributions 	/* for pipe */
876*d8b80295SApple OSS Distributions 	int fd[2];
877*d8b80295SApple OSS Distributions 	pipe(fd);
878*d8b80295SApple OSS Distributions 	int pipe_msg = 0;
879*d8b80295SApple OSS Distributions 
880*d8b80295SApple OSS Distributions 	child_pid = fork();
881*d8b80295SApple OSS Distributions 
882*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(child_pid, "verify process can be forked %d", child_pid);
883*d8b80295SApple OSS Distributions 
884*d8b80295SApple OSS Distributions 	if (child_pid == 0) {
885*d8b80295SApple OSS Distributions 		close(fd[1]);
886*d8b80295SApple OSS Distributions 		T_LOG("Waiting to read from parent...");
887*d8b80295SApple OSS Distributions 		read(fd[0], &pipe_msg, sizeof(pipe_msg));
888*d8b80295SApple OSS Distributions 		T_LOG("Done reading from parent, about to exit...");
889*d8b80295SApple OSS Distributions 		exit(0);
890*d8b80295SApple OSS Distributions 	}
891*d8b80295SApple OSS Distributions 	/*
892*d8b80295SApple OSS Distributions 	 * Wait for child to fork and block on read
893*d8b80295SApple OSS Distributions 	 */
894*d8b80295SApple OSS Distributions 	sleep(1);
895*d8b80295SApple OSS Distributions 
896*d8b80295SApple OSS Distributions 	close(fd[0]);
897*d8b80295SApple OSS Distributions 
898*d8b80295SApple OSS Distributions 	kr = task_for_pid(mach_task_self(), child_pid, &child_task);
899*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_for_pid succeeded.  check sudo if failed");
900*d8b80295SApple OSS Distributions 
901*d8b80295SApple OSS Distributions 	kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count);
902*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
903*d8b80295SApple OSS Distributions 
904*d8b80295SApple OSS Distributions 	debug_suspend_count = debug_info.suspend_count;
905*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(debug_suspend_count, 0, "verify debug_info shows correct (true) suspend_count(0)");
906*d8b80295SApple OSS Distributions 
907*d8b80295SApple OSS Distributions 	kr = task_suspend2(child_task, &child_token);
908*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_suspend2 call succeeded");
909*d8b80295SApple OSS Distributions 
910*d8b80295SApple OSS Distributions 	kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count);
911*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
912*d8b80295SApple OSS Distributions 
913*d8b80295SApple OSS Distributions 	debug_suspend_count = debug_info.suspend_count;
914*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(debug_suspend_count, 1, "verify debug_info shows correct (true) suspend_count(1)");
915*d8b80295SApple OSS Distributions 
916*d8b80295SApple OSS Distributions 	/*
917*d8b80295SApple OSS Distributions 	 * Verify the suspend_count for child and resume it.
918*d8b80295SApple OSS Distributions 	 */
919*d8b80295SApple OSS Distributions 
920*d8b80295SApple OSS Distributions 	kr = task_info(child_task, flavor, info_data, &count);
921*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_info call succeeded");
922*d8b80295SApple OSS Distributions 
923*d8b80295SApple OSS Distributions 	suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data));
924*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(suspend_count, 1, "verify task_info shows correct (user_stop_count) suspend_count (1)");
925*d8b80295SApple OSS Distributions 
926*d8b80295SApple OSS Distributions 	posix_ret = kill(child_pid, SIGCONT);
927*d8b80295SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(posix_ret, "verify signal call succeeded");
928*d8b80295SApple OSS Distributions 
929*d8b80295SApple OSS Distributions 	kr = task_info(child_task, TASK_DEBUG_INFO_INTERNAL, (task_info_t)&debug_info, &debug_count);
930*d8b80295SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "verify task_info call succeeded");
931*d8b80295SApple OSS Distributions 
932*d8b80295SApple OSS Distributions 	debug_suspend_count = debug_info.suspend_count;
933*d8b80295SApple OSS Distributions 	T_EXPECTFAIL_WITH_RADAR(33166654);
934*d8b80295SApple OSS Distributions 	T_EXPECT_EQ(debug_suspend_count, 1, "verify debug_info shows correct (true) suspend_count (1)");
935*d8b80295SApple OSS Distributions 
936*d8b80295SApple OSS Distributions 	suspend_count = (integer_t)(info_get(INFO_MACH, GET_SUSPEND_COUNT, info_data));
937*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(suspend_count, 1, "verify task_info shows correct (user_stop_count) suspend_count (1) after SIG_CONT");
938*d8b80295SApple OSS Distributions 
939*d8b80295SApple OSS Distributions 	kr = task_resume(child_task);
940*d8b80295SApple OSS Distributions 	T_EXPECTFAIL_WITH_RADAR(33166654);
941*d8b80295SApple OSS Distributions 	T_EXPECT_MACH_SUCCESS(kr, "verify task_resume succeeded");
942*d8b80295SApple OSS Distributions 
943*d8b80295SApple OSS Distributions 	/*
944*d8b80295SApple OSS Distributions 	 * reap kr from task_suspend call in child
945*d8b80295SApple OSS Distributions 	 */
946*d8b80295SApple OSS Distributions 
947*d8b80295SApple OSS Distributions 	dt_waitpid(child_pid, &exit_status, &signal_no, timeout);
948*d8b80295SApple OSS Distributions 
949*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(signal_no, SIG_DT_TIMEOUT, "dt_waitpid timed out as expected");
950*d8b80295SApple OSS Distributions 
951*d8b80295SApple OSS Distributions 	// Resume properly using token and then wait
952*d8b80295SApple OSS Distributions 
953*d8b80295SApple OSS Distributions 	kr = task_resume2(child_token);
954*d8b80295SApple OSS Distributions 	T_EXPECTFAIL_WITH_RADAR(33166654);
955*d8b80295SApple OSS Distributions 	T_ASSERT_MACH_SUCCESS(kr, "verify task_resume2 succeeded");
956*d8b80295SApple OSS Distributions 
957*d8b80295SApple OSS Distributions 	write(fd[1], &pipe_msg, sizeof(pipe_msg));
958*d8b80295SApple OSS Distributions 
959*d8b80295SApple OSS Distributions 	/*
960*d8b80295SApple OSS Distributions 	 * reap kr from task_suspend call in child
961*d8b80295SApple OSS Distributions 	 */
962*d8b80295SApple OSS Distributions 	dt_waitpid(child_pid, &exit_status, &signal_no, timeout);
963*d8b80295SApple OSS Distributions 
964*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(signal_no, 0, "child should be resumed and no signal should be returned");
965*d8b80295SApple OSS Distributions 	T_ASSERT_EQ(exit_status, 0, "child should exit with 0");
966*d8b80295SApple OSS Distributions }
967*d8b80295SApple OSS Distributions 
968*d8b80295SApple OSS Distributions uint64_t
info_get(enum info_kind kind,enum info_get get,void * data)969*d8b80295SApple OSS Distributions info_get(enum info_kind kind, enum info_get get, void * data)
970*d8b80295SApple OSS Distributions {
971*d8b80295SApple OSS Distributions 	switch (get) {
972*d8b80295SApple OSS Distributions 	case GET_SUSPEND_COUNT:
973*d8b80295SApple OSS Distributions 		switch (kind) {
974*d8b80295SApple OSS Distributions 		case INFO_32:
975*d8b80295SApple OSS Distributions 		case INFO_32_2:
976*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_32_t)data)->suspend_count);
977*d8b80295SApple OSS Distributions #if defined(__arm64__)
978*d8b80295SApple OSS Distributions 		case INFO_64:
979*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
980*d8b80295SApple OSS Distributions 			break;
981*d8b80295SApple OSS Distributions 
982*d8b80295SApple OSS Distributions 		case INFO_64_2:
983*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_64_2_t)data)->suspend_count);
984*d8b80295SApple OSS Distributions #else
985*d8b80295SApple OSS Distributions 		case INFO_64:
986*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_64_t)data)->suspend_count);
987*d8b80295SApple OSS Distributions 
988*d8b80295SApple OSS Distributions 		case INFO_64_2:
989*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
990*d8b80295SApple OSS Distributions 			break;
991*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
992*d8b80295SApple OSS Distributions 		case INFO_MACH:
993*d8b80295SApple OSS Distributions 			return (uint64_t)(((mach_task_basic_info_t)data)->suspend_count);
994*d8b80295SApple OSS Distributions 		case INFO_MAX:
995*d8b80295SApple OSS Distributions 		default:
996*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
997*d8b80295SApple OSS Distributions 		}
998*d8b80295SApple OSS Distributions 	case GET_RESIDENT_SIZE:
999*d8b80295SApple OSS Distributions 		switch (kind) {
1000*d8b80295SApple OSS Distributions 		case INFO_32:
1001*d8b80295SApple OSS Distributions 		case INFO_32_2:
1002*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_32_t)data)->resident_size);
1003*d8b80295SApple OSS Distributions #if defined(__arm64__)
1004*d8b80295SApple OSS Distributions 		case INFO_64:
1005*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1006*d8b80295SApple OSS Distributions 			break;
1007*d8b80295SApple OSS Distributions 
1008*d8b80295SApple OSS Distributions 		case INFO_64_2:
1009*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_64_2_t)data)->resident_size);
1010*d8b80295SApple OSS Distributions #else
1011*d8b80295SApple OSS Distributions 		case INFO_64:
1012*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_64_t)data)->resident_size);
1013*d8b80295SApple OSS Distributions 
1014*d8b80295SApple OSS Distributions 		case INFO_64_2:
1015*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1016*d8b80295SApple OSS Distributions 			break;
1017*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
1018*d8b80295SApple OSS Distributions 		case INFO_MACH:
1019*d8b80295SApple OSS Distributions 			return (uint64_t)(((mach_task_basic_info_t)data)->resident_size);
1020*d8b80295SApple OSS Distributions 		case INFO_MAX:
1021*d8b80295SApple OSS Distributions 		default:
1022*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1023*d8b80295SApple OSS Distributions 		}
1024*d8b80295SApple OSS Distributions 	case GET_VIRTUAL_SIZE:
1025*d8b80295SApple OSS Distributions 		switch (kind) {
1026*d8b80295SApple OSS Distributions 		case INFO_32:
1027*d8b80295SApple OSS Distributions 		case INFO_32_2:
1028*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_32_t)data)->virtual_size);
1029*d8b80295SApple OSS Distributions #if defined(__arm64__)
1030*d8b80295SApple OSS Distributions 		case INFO_64:
1031*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1032*d8b80295SApple OSS Distributions 			break;
1033*d8b80295SApple OSS Distributions 
1034*d8b80295SApple OSS Distributions 		case INFO_64_2:
1035*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_64_2_t)data)->virtual_size);
1036*d8b80295SApple OSS Distributions #else
1037*d8b80295SApple OSS Distributions 		case INFO_64:
1038*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_64_t)data)->virtual_size);
1039*d8b80295SApple OSS Distributions 
1040*d8b80295SApple OSS Distributions 		case INFO_64_2:
1041*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1042*d8b80295SApple OSS Distributions 			break;
1043*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
1044*d8b80295SApple OSS Distributions 		case INFO_MACH:
1045*d8b80295SApple OSS Distributions 			return (uint64_t)(((mach_task_basic_info_t)data)->virtual_size);
1046*d8b80295SApple OSS Distributions 
1047*d8b80295SApple OSS Distributions 		case INFO_MAX:
1048*d8b80295SApple OSS Distributions 		default:
1049*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1050*d8b80295SApple OSS Distributions 		}
1051*d8b80295SApple OSS Distributions 	case GET_USER_TIME:
1052*d8b80295SApple OSS Distributions 		switch (kind) {
1053*d8b80295SApple OSS Distributions 		case INFO_32:
1054*d8b80295SApple OSS Distributions 		case INFO_32_2:
1055*d8b80295SApple OSS Distributions 			return (uint64_t) &(((task_basic_info_32_t)data)->user_time);
1056*d8b80295SApple OSS Distributions #if defined(__arm64__)
1057*d8b80295SApple OSS Distributions 		case INFO_64:
1058*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1059*d8b80295SApple OSS Distributions 			break;
1060*d8b80295SApple OSS Distributions 
1061*d8b80295SApple OSS Distributions 		case INFO_64_2:
1062*d8b80295SApple OSS Distributions 			return (uint64_t) &(((task_basic_info_64_2_t)data)->user_time);
1063*d8b80295SApple OSS Distributions #else
1064*d8b80295SApple OSS Distributions 		case INFO_64:
1065*d8b80295SApple OSS Distributions 			return (uint64_t) &(((task_basic_info_64_t)data)->user_time);
1066*d8b80295SApple OSS Distributions 
1067*d8b80295SApple OSS Distributions 		case INFO_64_2:
1068*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1069*d8b80295SApple OSS Distributions 			break;
1070*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
1071*d8b80295SApple OSS Distributions 		case INFO_MACH:
1072*d8b80295SApple OSS Distributions 			return (uint64_t) &(((mach_task_basic_info_t)data)->user_time);
1073*d8b80295SApple OSS Distributions 
1074*d8b80295SApple OSS Distributions 		case INFO_MAX:
1075*d8b80295SApple OSS Distributions 		default:
1076*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1077*d8b80295SApple OSS Distributions 		}
1078*d8b80295SApple OSS Distributions 	case GET_SYS_TIME:
1079*d8b80295SApple OSS Distributions 		switch (kind) {
1080*d8b80295SApple OSS Distributions 		case INFO_32:
1081*d8b80295SApple OSS Distributions 		case INFO_32_2:
1082*d8b80295SApple OSS Distributions 			return (uint64_t) &(((task_basic_info_32_t)data)->system_time);
1083*d8b80295SApple OSS Distributions #if defined(__arm64__)
1084*d8b80295SApple OSS Distributions 		case INFO_64:
1085*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1086*d8b80295SApple OSS Distributions 			break;
1087*d8b80295SApple OSS Distributions 
1088*d8b80295SApple OSS Distributions 		case INFO_64_2:
1089*d8b80295SApple OSS Distributions 			return (uint64_t) &(((task_basic_info_64_2_t)data)->system_time);
1090*d8b80295SApple OSS Distributions #else
1091*d8b80295SApple OSS Distributions 		case INFO_64:
1092*d8b80295SApple OSS Distributions 			return (uint64_t) &(((task_basic_info_64_t)data)->system_time);
1093*d8b80295SApple OSS Distributions 
1094*d8b80295SApple OSS Distributions 		case INFO_64_2:
1095*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1096*d8b80295SApple OSS Distributions 			break;
1097*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
1098*d8b80295SApple OSS Distributions 		case INFO_MACH:
1099*d8b80295SApple OSS Distributions 			return (uint64_t) &(((mach_task_basic_info_t)data)->user_time);
1100*d8b80295SApple OSS Distributions 		case INFO_MAX:
1101*d8b80295SApple OSS Distributions 		default:
1102*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1103*d8b80295SApple OSS Distributions 		}
1104*d8b80295SApple OSS Distributions 	case GET_POLICY:
1105*d8b80295SApple OSS Distributions 		switch (kind) {
1106*d8b80295SApple OSS Distributions 		case INFO_32:
1107*d8b80295SApple OSS Distributions 		case INFO_32_2:
1108*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_32_t)data)->policy);
1109*d8b80295SApple OSS Distributions #if defined(__arm64__)
1110*d8b80295SApple OSS Distributions 		case INFO_64:
1111*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1112*d8b80295SApple OSS Distributions 			break;
1113*d8b80295SApple OSS Distributions 
1114*d8b80295SApple OSS Distributions 		case INFO_64_2:
1115*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_64_2_t)data)->policy);
1116*d8b80295SApple OSS Distributions #else
1117*d8b80295SApple OSS Distributions 		case INFO_64:
1118*d8b80295SApple OSS Distributions 			return (uint64_t)(((task_basic_info_64_t)data)->policy);
1119*d8b80295SApple OSS Distributions 
1120*d8b80295SApple OSS Distributions 		case INFO_64_2:
1121*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1122*d8b80295SApple OSS Distributions 			break;
1123*d8b80295SApple OSS Distributions #endif /* defined(__arm64__) */
1124*d8b80295SApple OSS Distributions 		case INFO_MACH:
1125*d8b80295SApple OSS Distributions 			return (uint64_t)(((mach_task_basic_info_t)data)->policy);
1126*d8b80295SApple OSS Distributions 
1127*d8b80295SApple OSS Distributions 		case INFO_MAX:
1128*d8b80295SApple OSS Distributions 		default:
1129*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1130*d8b80295SApple OSS Distributions 		}
1131*d8b80295SApple OSS Distributions 	case GET_MAX_RES:
1132*d8b80295SApple OSS Distributions 		switch (kind) {
1133*d8b80295SApple OSS Distributions 		case INFO_32:
1134*d8b80295SApple OSS Distributions 		case INFO_32_2:
1135*d8b80295SApple OSS Distributions 		case INFO_64:
1136*d8b80295SApple OSS Distributions 		case INFO_64_2:
1137*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("illegal info_get %d %d", kind, get);
1138*d8b80295SApple OSS Distributions 		case INFO_MACH:
1139*d8b80295SApple OSS Distributions 			return (uint64_t)(((mach_task_basic_info_t)data)->resident_size_max);
1140*d8b80295SApple OSS Distributions 		case INFO_MAX:
1141*d8b80295SApple OSS Distributions 		default:
1142*d8b80295SApple OSS Distributions 			T_ASSERT_FAIL("unhandled info_get %d %d", kind, get);
1143*d8b80295SApple OSS Distributions 		}
1144*d8b80295SApple OSS Distributions 	}
1145*d8b80295SApple OSS Distributions 
1146*d8b80295SApple OSS Distributions 	__builtin_unreachable();
1147*d8b80295SApple OSS Distributions }
1148