xref: /xnu-11215.41.3/osfmk/kern/host.c (revision 33de042d024d46de5ff4e89f2471de6608e37fa4)
1 /*
2  * Copyright (c) 2000-2009 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  * @OSF_COPYRIGHT@
30  */
31 /*
32  * Mach Operating System
33  * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34  * All Rights Reserved.
35  *
36  * Permission to use, copy, modify and distribute this software and its
37  * documentation is hereby granted, provided that both the copyright
38  * notice and this permission notice appear in all copies of the
39  * software, derivative works or modified versions, and any portions
40  * thereof, and that both notices appear in supporting documentation.
41  *
42  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45  *
46  * Carnegie Mellon requests users of this software to return to
47  *
48  *  Software Distribution Coordinator  or  [email protected]
49  *  School of Computer Science
50  *  Carnegie Mellon University
51  *  Pittsburgh PA 15213-3890
52  *
53  * any improvements or extensions that they make and grant Carnegie Mellon
54  * the rights to redistribute these changes.
55  */
56 /*
57  */
58 
59 /*
60  *	host.c
61  *
62  *	Non-ipc host functions.
63  */
64 
65 #include <mach/mach_types.h>
66 #include <mach/boolean.h>
67 #include <mach/host_info.h>
68 #include <mach/host_special_ports.h>
69 #include <mach/kern_return.h>
70 #include <mach/machine.h>
71 #include <mach/port.h>
72 #include <mach/processor_info.h>
73 #include <mach/vm_param.h>
74 #include <mach/processor.h>
75 #include <mach/mach_host_server.h>
76 #include <mach/host_priv_server.h>
77 #include <mach/vm_map.h>
78 #include <mach/task_info.h>
79 
80 #include <machine/commpage.h>
81 #include <machine/cpu_capabilities.h>
82 
83 #include <device/device_port.h>
84 
85 #include <kern/kern_types.h>
86 #include <kern/assert.h>
87 #include <kern/kalloc.h>
88 #include <kern/ecc.h>
89 #include <kern/host.h>
90 #include <kern/host_statistics.h>
91 #include <kern/ipc_host.h>
92 #include <kern/misc_protos.h>
93 #include <kern/sched.h>
94 #include <kern/processor.h>
95 #include <kern/mach_node.h>     // mach_node_port_changed()
96 
97 #include <vm/vm_map_xnu.h>
98 #include <vm/vm_purgeable_xnu.h>
99 #include <vm/vm_pageout.h>
100 #include <vm/vm_kern_xnu.h>
101 
102 #include <IOKit/IOBSD.h> // IOTaskHasEntitlement
103 #include <IOKit/IOKitKeys.h> // DriverKit entitlement strings
104 
105 
106 #if CONFIG_ATM
107 #include <atm/atm_internal.h>
108 #endif
109 
110 #if CONFIG_MACF
111 #include <security/mac_mach_internal.h>
112 #endif
113 
114 #if CONFIG_CSR
115 #include <sys/csr.h>
116 #endif
117 
118 #include <pexpert/pexpert.h>
119 
120 SCALABLE_COUNTER_DEFINE(vm_statistics_zero_fill_count);        /* # of zero fill pages */
121 SCALABLE_COUNTER_DEFINE(vm_statistics_reactivations);          /* # of pages reactivated */
122 SCALABLE_COUNTER_DEFINE(vm_statistics_pageins);                /* # of pageins */
123 SCALABLE_COUNTER_DEFINE(vm_statistics_pageouts);               /* # of pageouts */
124 SCALABLE_COUNTER_DEFINE(vm_statistics_faults);                 /* # of faults */
125 SCALABLE_COUNTER_DEFINE(vm_statistics_cow_faults);             /* # of copy-on-writes */
126 SCALABLE_COUNTER_DEFINE(vm_statistics_lookups);                /* object cache lookups */
127 SCALABLE_COUNTER_DEFINE(vm_statistics_hits);                   /* object cache hits */
128 SCALABLE_COUNTER_DEFINE(vm_statistics_purges);                 /* # of pages purged */
129 SCALABLE_COUNTER_DEFINE(vm_statistics_decompressions);         /* # of pages decompressed */
130 SCALABLE_COUNTER_DEFINE(vm_statistics_compressions);           /* # of pages compressed */
131 SCALABLE_COUNTER_DEFINE(vm_statistics_swapins);                /* # of pages swapped in (via compression segments) */
132 SCALABLE_COUNTER_DEFINE(vm_statistics_swapouts);               /* # of pages swapped out (via compression segments) */
133 SCALABLE_COUNTER_DEFINE(vm_statistics_total_uncompressed_pages_in_compressor); /* # of pages (uncompressed) held within the compressor. */
134 SCALABLE_COUNTER_DEFINE(vm_page_grab_count);
135 
136 host_data_t realhost;
137 
138 static void
get_host_vm_stats(vm_statistics64_t out)139 get_host_vm_stats(vm_statistics64_t out)
140 {
141 	out->zero_fill_count = counter_load(&vm_statistics_zero_fill_count);
142 	out->reactivations = counter_load(&vm_statistics_reactivations);
143 	out->pageins = counter_load(&vm_statistics_pageins);
144 	out->pageouts = counter_load(&vm_statistics_pageouts);
145 	out->faults = counter_load(&vm_statistics_faults);
146 	out->cow_faults = counter_load(&vm_statistics_cow_faults);
147 	out->lookups = counter_load(&vm_statistics_lookups);
148 	out->hits = counter_load(&vm_statistics_hits);
149 	out->compressions = counter_load(&vm_statistics_compressions);
150 	out->decompressions = counter_load(&vm_statistics_decompressions);
151 	out->swapins = counter_load(&vm_statistics_swapins);
152 	out->swapouts = counter_load(&vm_statistics_swapouts);
153 }
154 vm_extmod_statistics_data_t host_extmod_statistics;
155 
156 kern_return_t
host_processors(host_priv_t host_priv,processor_array_t * out_array,mach_msg_type_number_t * countp)157 host_processors(host_priv_t host_priv, processor_array_t * out_array, mach_msg_type_number_t * countp)
158 {
159 	if (host_priv == HOST_PRIV_NULL) {
160 		return KERN_INVALID_ARGUMENT;
161 	}
162 
163 	unsigned int count = processor_count;
164 	assert(count != 0);
165 
166 	static_assert(sizeof(mach_port_t) == sizeof(processor_t));
167 
168 	mach_port_array_t ports = mach_port_array_alloc(count, Z_WAITOK);
169 	if (!ports) {
170 		return KERN_RESOURCE_SHORTAGE;
171 	}
172 
173 	for (unsigned int i = 0; i < count; i++) {
174 		processor_t processor = processor_array[i];
175 		assert(processor != PROCESSOR_NULL);
176 
177 		/* do the conversion that Mig should handle */
178 		ports[i].port = convert_processor_to_port(processor);
179 	}
180 
181 	*countp = count;
182 	*out_array = ports;
183 
184 	return KERN_SUCCESS;
185 }
186 
187 extern int sched_allow_NO_SMT_threads;
188 
189 kern_return_t
host_info(host_t host,host_flavor_t flavor,host_info_t info,mach_msg_type_number_t * count)190 host_info(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count)
191 {
192 	if (host == HOST_NULL) {
193 		return KERN_INVALID_ARGUMENT;
194 	}
195 
196 	switch (flavor) {
197 	case HOST_BASIC_INFO: {
198 		host_basic_info_t basic_info;
199 		int master_id = master_processor->cpu_id;
200 
201 		/*
202 		 *	Basic information about this host.
203 		 */
204 		if (*count < HOST_BASIC_INFO_OLD_COUNT) {
205 			return KERN_FAILURE;
206 		}
207 
208 		basic_info = (host_basic_info_t)info;
209 
210 		basic_info->memory_size = machine_info.memory_size;
211 		basic_info->cpu_type = slot_type(master_id);
212 		basic_info->cpu_subtype = slot_subtype(master_id);
213 		basic_info->max_cpus = machine_info.max_cpus;
214 #if defined(__x86_64__)
215 		if (sched_allow_NO_SMT_threads && current_task()->t_flags & TF_NO_SMT) {
216 			basic_info->avail_cpus = primary_processor_avail_count_user;
217 		} else {
218 			basic_info->avail_cpus = processor_avail_count_user;
219 		}
220 #else
221 		basic_info->avail_cpus = processor_avail_count;
222 #endif
223 
224 
225 		if (*count >= HOST_BASIC_INFO_COUNT) {
226 			basic_info->cpu_threadtype = slot_threadtype(master_id);
227 			basic_info->physical_cpu = machine_info.physical_cpu;
228 			basic_info->physical_cpu_max = machine_info.physical_cpu_max;
229 #if defined(__x86_64__)
230 			basic_info->logical_cpu = basic_info->avail_cpus;
231 #else
232 			basic_info->logical_cpu = machine_info.logical_cpu;
233 #endif
234 			basic_info->logical_cpu_max = machine_info.logical_cpu_max;
235 			basic_info->max_mem = machine_info.max_mem;
236 
237 			*count = HOST_BASIC_INFO_COUNT;
238 		} else {
239 			*count = HOST_BASIC_INFO_OLD_COUNT;
240 		}
241 
242 		return KERN_SUCCESS;
243 	}
244 
245 	case HOST_SCHED_INFO: {
246 		host_sched_info_t sched_info;
247 		uint32_t quantum_time;
248 		uint64_t quantum_ns;
249 
250 		/*
251 		 *	Return scheduler information.
252 		 */
253 		if (*count < HOST_SCHED_INFO_COUNT) {
254 			return KERN_FAILURE;
255 		}
256 
257 		sched_info = (host_sched_info_t)info;
258 
259 		quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
260 		absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
261 
262 		sched_info->min_timeout = sched_info->min_quantum = (uint32_t)(quantum_ns / 1000 / 1000);
263 
264 		*count = HOST_SCHED_INFO_COUNT;
265 
266 		return KERN_SUCCESS;
267 	}
268 
269 	case HOST_RESOURCE_SIZES: {
270 		/*
271 		 * Return sizes of kernel data structures
272 		 */
273 		if (*count < HOST_RESOURCE_SIZES_COUNT) {
274 			return KERN_FAILURE;
275 		}
276 
277 		/* XXX Fail until ledgers are implemented */
278 		return KERN_INVALID_ARGUMENT;
279 	}
280 
281 	case HOST_PRIORITY_INFO: {
282 		host_priority_info_t priority_info;
283 
284 		if (*count < HOST_PRIORITY_INFO_COUNT) {
285 			return KERN_FAILURE;
286 		}
287 
288 		priority_info = (host_priority_info_t)info;
289 
290 		priority_info->kernel_priority = MINPRI_KERNEL;
291 		priority_info->system_priority = MINPRI_KERNEL;
292 		priority_info->server_priority = MINPRI_RESERVED;
293 		priority_info->user_priority = BASEPRI_DEFAULT;
294 		priority_info->depress_priority = DEPRESSPRI;
295 		priority_info->idle_priority = IDLEPRI;
296 		priority_info->minimum_priority = MINPRI_USER;
297 		priority_info->maximum_priority = MAXPRI_RESERVED;
298 
299 		*count = HOST_PRIORITY_INFO_COUNT;
300 
301 		return KERN_SUCCESS;
302 	}
303 
304 	/*
305 	 * Gestalt for various trap facilities.
306 	 */
307 	case HOST_MACH_MSG_TRAP:
308 	case HOST_SEMAPHORE_TRAPS: {
309 		*count = 0;
310 		return KERN_SUCCESS;
311 	}
312 
313 	case HOST_CAN_HAS_DEBUGGER: {
314 		host_can_has_debugger_info_t can_has_debugger_info;
315 
316 		if (*count < HOST_CAN_HAS_DEBUGGER_COUNT) {
317 			return KERN_FAILURE;
318 		}
319 
320 		can_has_debugger_info = (host_can_has_debugger_info_t)info;
321 		can_has_debugger_info->can_has_debugger = PE_i_can_has_debugger(NULL);
322 		*count = HOST_CAN_HAS_DEBUGGER_COUNT;
323 
324 		return KERN_SUCCESS;
325 	}
326 
327 	case HOST_VM_PURGABLE: {
328 		if (*count < HOST_VM_PURGABLE_COUNT) {
329 			return KERN_FAILURE;
330 		}
331 
332 		vm_purgeable_stats((vm_purgeable_info_t)info, NULL);
333 
334 		*count = HOST_VM_PURGABLE_COUNT;
335 		return KERN_SUCCESS;
336 	}
337 
338 	case HOST_DEBUG_INFO_INTERNAL: {
339 #if DEVELOPMENT || DEBUG
340 		if (*count < HOST_DEBUG_INFO_INTERNAL_COUNT) {
341 			return KERN_FAILURE;
342 		}
343 
344 		host_debug_info_internal_t debug_info = (host_debug_info_internal_t)info;
345 		bzero(debug_info, sizeof(host_debug_info_internal_data_t));
346 		*count = HOST_DEBUG_INFO_INTERNAL_COUNT;
347 
348 #if CONFIG_COALITIONS
349 		debug_info->config_coalitions = 1;
350 #endif
351 		debug_info->config_bank = 1;
352 #if CONFIG_ATM
353 		debug_info->config_atm = 1;
354 #endif
355 #if CONFIG_CSR
356 		debug_info->config_csr = 1;
357 #endif
358 		return KERN_SUCCESS;
359 #else /* DEVELOPMENT || DEBUG */
360 		return KERN_NOT_SUPPORTED;
361 #endif
362 	}
363 
364 	case HOST_PREFERRED_USER_ARCH: {
365 		host_preferred_user_arch_t user_arch_info;
366 
367 		/*
368 		 *	Basic information about this host.
369 		 */
370 		if (*count < HOST_PREFERRED_USER_ARCH_COUNT) {
371 			return KERN_FAILURE;
372 		}
373 
374 		user_arch_info = (host_preferred_user_arch_t)info;
375 
376 #if defined(PREFERRED_USER_CPU_TYPE) && defined(PREFERRED_USER_CPU_SUBTYPE)
377 		cpu_type_t preferred_cpu_type;
378 		cpu_subtype_t preferred_cpu_subtype;
379 		if (!PE_get_default("kern.preferred_cpu_type", &preferred_cpu_type, sizeof(cpu_type_t))) {
380 			preferred_cpu_type = PREFERRED_USER_CPU_TYPE;
381 		}
382 		if (!PE_get_default("kern.preferred_cpu_subtype", &preferred_cpu_subtype, sizeof(cpu_subtype_t))) {
383 			preferred_cpu_subtype = PREFERRED_USER_CPU_SUBTYPE;
384 		}
385 		user_arch_info->cpu_type    = preferred_cpu_type;
386 		user_arch_info->cpu_subtype = preferred_cpu_subtype;
387 #else
388 		int master_id               = master_processor->cpu_id;
389 		user_arch_info->cpu_type    = slot_type(master_id);
390 		user_arch_info->cpu_subtype = slot_subtype(master_id);
391 #endif
392 
393 
394 		*count = HOST_PREFERRED_USER_ARCH_COUNT;
395 
396 		return KERN_SUCCESS;
397 	}
398 
399 	default: return KERN_INVALID_ARGUMENT;
400 	}
401 }
402 
403 kern_return_t host_statistics(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count);
404 
405 kern_return_t
host_statistics(host_t host,host_flavor_t flavor,host_info_t info,mach_msg_type_number_t * count)406 host_statistics(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count)
407 {
408 	if (host == HOST_NULL) {
409 		return KERN_INVALID_HOST;
410 	}
411 
412 	switch (flavor) {
413 	case HOST_LOAD_INFO: {
414 		host_load_info_t load_info;
415 
416 		if (*count < HOST_LOAD_INFO_COUNT) {
417 			return KERN_FAILURE;
418 		}
419 
420 		load_info = (host_load_info_t)info;
421 
422 		bcopy((char *)avenrun, (char *)load_info->avenrun, sizeof avenrun);
423 		bcopy((char *)mach_factor, (char *)load_info->mach_factor, sizeof mach_factor);
424 
425 		*count = HOST_LOAD_INFO_COUNT;
426 		return KERN_SUCCESS;
427 	}
428 
429 	case HOST_VM_INFO: {
430 		vm_statistics64_data_t host_vm_stat;
431 		vm_statistics_t stat32;
432 		mach_msg_type_number_t original_count;
433 		natural_t speculative_count = vm_page_speculative_count;
434 
435 		if (*count < HOST_VM_INFO_REV0_COUNT) {
436 			return KERN_FAILURE;
437 		}
438 
439 		get_host_vm_stats(&host_vm_stat);
440 
441 		stat32 = (vm_statistics_t)info;
442 
443 		stat32->free_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_free_count + speculative_count);
444 		stat32->active_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_active_count);
445 
446 		if (vm_page_local_q) {
447 			zpercpu_foreach(lq, vm_page_local_q) {
448 				stat32->active_count += VM_STATISTICS_TRUNCATE_TO_32_BIT(lq->vpl_count);
449 			}
450 		}
451 		stat32->inactive_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_inactive_count);
452 #if !XNU_TARGET_OS_OSX
453 		stat32->wire_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_wire_count);
454 #else /* !XNU_TARGET_OS_OSX */
455 		stat32->wire_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_wire_count + vm_page_throttled_count + vm_lopage_free_count);
456 #endif /* !XNU_TARGET_OS_OSX */
457 		stat32->zero_fill_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.zero_fill_count);
458 		stat32->reactivations = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.reactivations);
459 		stat32->pageins = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.pageins);
460 		stat32->pageouts = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.pageouts);
461 		stat32->faults = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.faults);
462 		stat32->cow_faults = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.cow_faults);
463 		stat32->lookups = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.lookups);
464 		stat32->hits = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.hits);
465 
466 		/*
467 		 * Fill in extra info added in later revisions of the
468 		 * vm_statistics data structure.  Fill in only what can fit
469 		 * in the data structure the caller gave us !
470 		 */
471 		original_count = *count;
472 		*count = HOST_VM_INFO_REV0_COUNT; /* rev0 already filled in */
473 		if (original_count >= HOST_VM_INFO_REV1_COUNT) {
474 			/* rev1 added "purgeable" info */
475 			stat32->purgeable_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_purgeable_count);
476 			stat32->purges = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_purged_count);
477 			*count = HOST_VM_INFO_REV1_COUNT;
478 		}
479 
480 		if (original_count >= HOST_VM_INFO_REV2_COUNT) {
481 			/* rev2 added "speculative" info */
482 			stat32->speculative_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(speculative_count);
483 			*count = HOST_VM_INFO_REV2_COUNT;
484 		}
485 
486 		/* rev3 changed some of the fields to be 64-bit*/
487 
488 		return KERN_SUCCESS;
489 	}
490 
491 	case HOST_CPU_LOAD_INFO: {
492 		host_cpu_load_info_t cpu_load_info;
493 
494 		if (*count < HOST_CPU_LOAD_INFO_COUNT) {
495 			return KERN_FAILURE;
496 		}
497 
498 #define GET_TICKS_VALUE(state, ticks)                                                      \
499 	MACRO_BEGIN cpu_load_info->cpu_ticks[(state)] += (uint32_t)(ticks / hz_tick_interval); \
500 	MACRO_END
501 #define GET_TICKS_VALUE_FROM_TIMER(processor, state, timer)                            \
502 	MACRO_BEGIN GET_TICKS_VALUE(state, timer_grab(&(processor)->timer)); \
503 	MACRO_END
504 
505 		cpu_load_info = (host_cpu_load_info_t)info;
506 		cpu_load_info->cpu_ticks[CPU_STATE_USER] = 0;
507 		cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] = 0;
508 		cpu_load_info->cpu_ticks[CPU_STATE_IDLE] = 0;
509 		cpu_load_info->cpu_ticks[CPU_STATE_NICE] = 0;
510 
511 		simple_lock(&processor_list_lock, LCK_GRP_NULL);
512 
513 		unsigned int pcount = processor_count;
514 
515 		for (unsigned int i = 0; i < pcount; i++) {
516 			processor_t processor = processor_array[i];
517 			assert(processor != PROCESSOR_NULL);
518 			processor_cpu_load_info(processor, cpu_load_info->cpu_ticks);
519 		}
520 		simple_unlock(&processor_list_lock);
521 
522 		*count = HOST_CPU_LOAD_INFO_COUNT;
523 
524 		return KERN_SUCCESS;
525 	}
526 
527 	case HOST_EXPIRED_TASK_INFO: {
528 		if (*count < TASK_POWER_INFO_COUNT) {
529 			return KERN_FAILURE;
530 		}
531 
532 		task_power_info_t tinfo1 = (task_power_info_t)info;
533 		task_power_info_v2_t tinfo2 = (task_power_info_v2_t)info;
534 
535 		tinfo1->task_interrupt_wakeups = dead_task_statistics.task_interrupt_wakeups;
536 		tinfo1->task_platform_idle_wakeups = dead_task_statistics.task_platform_idle_wakeups;
537 
538 		tinfo1->task_timer_wakeups_bin_1 = dead_task_statistics.task_timer_wakeups_bin_1;
539 
540 		tinfo1->task_timer_wakeups_bin_2 = dead_task_statistics.task_timer_wakeups_bin_2;
541 
542 		tinfo1->total_user = dead_task_statistics.total_user_time;
543 		tinfo1->total_system = dead_task_statistics.total_system_time;
544 		if (*count < TASK_POWER_INFO_V2_COUNT) {
545 			*count = TASK_POWER_INFO_COUNT;
546 		} else if (*count >= TASK_POWER_INFO_V2_COUNT) {
547 			tinfo2->gpu_energy.task_gpu_utilisation = dead_task_statistics.task_gpu_ns;
548 #if defined(__arm64__)
549 			tinfo2->task_energy = dead_task_statistics.task_energy;
550 			tinfo2->task_ptime = dead_task_statistics.total_ptime;
551 			tinfo2->task_pset_switches = dead_task_statistics.total_pset_switches;
552 #endif
553 			*count = TASK_POWER_INFO_V2_COUNT;
554 		}
555 
556 		return KERN_SUCCESS;
557 	}
558 	default: return KERN_INVALID_ARGUMENT;
559 	}
560 }
561 
562 extern uint32_t c_segment_pages_compressed;
563 
564 #define HOST_STATISTICS_TIME_WINDOW 1 /* seconds */
565 #define HOST_STATISTICS_MAX_REQUESTS 10 /* maximum number of requests per window */
566 #define HOST_STATISTICS_MIN_REQUESTS 2 /* minimum number of requests per window */
567 
568 uint64_t host_statistics_time_window;
569 
570 static LCK_GRP_DECLARE(host_statistics_lck_grp, "host_statistics");
571 static LCK_MTX_DECLARE(host_statistics_lck, &host_statistics_lck_grp);
572 
573 #define HOST_VM_INFO64_REV0             0
574 #define HOST_VM_INFO64_REV1             1
575 #define HOST_EXTMOD_INFO64_REV0         2
576 #define HOST_LOAD_INFO_REV0             3
577 #define HOST_VM_INFO_REV0               4
578 #define HOST_VM_INFO_REV1               5
579 #define HOST_VM_INFO_REV2               6
580 #define HOST_CPU_LOAD_INFO_REV0         7
581 #define HOST_EXPIRED_TASK_INFO_REV0     8
582 #define HOST_EXPIRED_TASK_INFO_REV1     9
583 #define HOST_VM_COMPRESSOR_Q_LEN_REV0   10
584 #define NUM_HOST_INFO_DATA_TYPES        11
585 
586 static vm_statistics64_data_t host_vm_info64_rev0 = {};
587 static vm_statistics64_data_t host_vm_info64_rev1 = {};
588 static vm_extmod_statistics_data_t host_extmod_info64 = {};
589 static host_load_info_data_t host_load_info = {};
590 static vm_statistics_data_t host_vm_info_rev0 = {};
591 static vm_statistics_data_t host_vm_info_rev1 = {};
592 static vm_statistics_data_t host_vm_info_rev2 = {};
593 static host_cpu_load_info_data_t host_cpu_load_info = {};
594 static task_power_info_data_t host_expired_task_info = {};
595 static task_power_info_v2_data_t host_expired_task_info2 = {};
596 static vm_compressor_q_lens_data_t host_vm_compressor_q_lens = {};
597 
598 struct host_stats_cache {
599 	uint64_t last_access;
600 	uint64_t current_requests;
601 	uint64_t max_requests;
602 	uintptr_t data;
603 	mach_msg_type_number_t count; //NOTE count is in sizeof(integer_t)
604 };
605 
606 static struct host_stats_cache g_host_stats_cache[NUM_HOST_INFO_DATA_TYPES] = {
607 	[HOST_VM_INFO64_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_vm_info64_rev0, .count = HOST_VM_INFO64_REV0_COUNT },
608 	[HOST_VM_INFO64_REV1] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_vm_info64_rev1, .count = HOST_VM_INFO64_REV1_COUNT },
609 	[HOST_EXTMOD_INFO64_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_extmod_info64, .count = HOST_EXTMOD_INFO64_COUNT },
610 	[HOST_LOAD_INFO_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_load_info, .count = HOST_LOAD_INFO_COUNT },
611 	[HOST_VM_INFO_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_vm_info_rev0, .count = HOST_VM_INFO_REV0_COUNT },
612 	[HOST_VM_INFO_REV1] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_vm_info_rev1, .count = HOST_VM_INFO_REV1_COUNT },
613 	[HOST_VM_INFO_REV2] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_vm_info_rev2, .count = HOST_VM_INFO_REV2_COUNT },
614 	[HOST_CPU_LOAD_INFO_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_cpu_load_info, .count = HOST_CPU_LOAD_INFO_COUNT },
615 	[HOST_EXPIRED_TASK_INFO_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_expired_task_info, .count = TASK_POWER_INFO_COUNT },
616 	[HOST_EXPIRED_TASK_INFO_REV1] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_expired_task_info2, .count = TASK_POWER_INFO_V2_COUNT},
617 	[HOST_VM_COMPRESSOR_Q_LEN_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_vm_compressor_q_lens, .count = VM_COMPRESSOR_Q_LENS_COUNT},
618 };
619 
620 
621 void
host_statistics_init(void)622 host_statistics_init(void)
623 {
624 	nanoseconds_to_absolutetime((HOST_STATISTICS_TIME_WINDOW * NSEC_PER_SEC), &host_statistics_time_window);
625 }
626 
627 static void
cache_host_statistics(int index,host_info64_t info)628 cache_host_statistics(int index, host_info64_t info)
629 {
630 	if (index < 0 || index >= NUM_HOST_INFO_DATA_TYPES) {
631 		return;
632 	}
633 
634 	if (task_get_platform_binary(current_task())) {
635 		return;
636 	}
637 
638 	memcpy((void *)g_host_stats_cache[index].data, info, g_host_stats_cache[index].count * sizeof(integer_t));
639 	return;
640 }
641 
642 static void
get_cached_info(int index,host_info64_t info,mach_msg_type_number_t * count)643 get_cached_info(int index, host_info64_t info, mach_msg_type_number_t* count)
644 {
645 	if (index < 0 || index >= NUM_HOST_INFO_DATA_TYPES) {
646 		*count = 0;
647 		return;
648 	}
649 
650 	*count = g_host_stats_cache[index].count;
651 	memcpy(info, (void *)g_host_stats_cache[index].data, g_host_stats_cache[index].count * sizeof(integer_t));
652 }
653 
654 static int
get_host_info_data_index(bool is_stat64,host_flavor_t flavor,mach_msg_type_number_t * count,kern_return_t * ret)655 get_host_info_data_index(bool is_stat64, host_flavor_t flavor, mach_msg_type_number_t* count, kern_return_t* ret)
656 {
657 	switch (flavor) {
658 	case HOST_VM_INFO64:
659 		if (!is_stat64) {
660 			*ret = KERN_INVALID_ARGUMENT;
661 			return -1;
662 		}
663 		if (*count < HOST_VM_INFO64_REV0_COUNT) {
664 			*ret = KERN_FAILURE;
665 			return -1;
666 		}
667 		if (*count >= HOST_VM_INFO64_REV1_COUNT) {
668 			return HOST_VM_INFO64_REV1;
669 		}
670 		return HOST_VM_INFO64_REV0;
671 
672 	case HOST_EXTMOD_INFO64:
673 		if (!is_stat64) {
674 			*ret = KERN_INVALID_ARGUMENT;
675 			return -1;
676 		}
677 		if (*count < HOST_EXTMOD_INFO64_COUNT) {
678 			*ret = KERN_FAILURE;
679 			return -1;
680 		}
681 		return HOST_EXTMOD_INFO64_REV0;
682 
683 	case HOST_LOAD_INFO:
684 		if (*count < HOST_LOAD_INFO_COUNT) {
685 			*ret = KERN_FAILURE;
686 			return -1;
687 		}
688 		return HOST_LOAD_INFO_REV0;
689 
690 	case HOST_VM_INFO:
691 		if (*count < HOST_VM_INFO_REV0_COUNT) {
692 			*ret = KERN_FAILURE;
693 			return -1;
694 		}
695 		if (*count >= HOST_VM_INFO_REV2_COUNT) {
696 			return HOST_VM_INFO_REV2;
697 		}
698 		if (*count >= HOST_VM_INFO_REV1_COUNT) {
699 			return HOST_VM_INFO_REV1;
700 		}
701 		return HOST_VM_INFO_REV0;
702 
703 	case HOST_CPU_LOAD_INFO:
704 		if (*count < HOST_CPU_LOAD_INFO_COUNT) {
705 			*ret = KERN_FAILURE;
706 			return -1;
707 		}
708 		return HOST_CPU_LOAD_INFO_REV0;
709 
710 	case HOST_EXPIRED_TASK_INFO:
711 		if (*count < TASK_POWER_INFO_COUNT) {
712 			*ret = KERN_FAILURE;
713 			return -1;
714 		}
715 		if (*count >= TASK_POWER_INFO_V2_COUNT) {
716 			return HOST_EXPIRED_TASK_INFO_REV1;
717 		}
718 		return HOST_EXPIRED_TASK_INFO_REV0;
719 
720 	case HOST_VM_COMPRESSOR_Q_LENS:
721 		if (*count < VM_COMPRESSOR_Q_LENS_COUNT) {
722 			*ret = KERN_FAILURE;
723 			return -1;
724 		}
725 		return HOST_VM_COMPRESSOR_Q_LEN_REV0;
726 
727 	default:
728 		*ret = KERN_INVALID_ARGUMENT;
729 		return -1;
730 	}
731 }
732 
733 static bool
rate_limit_host_statistics(bool is_stat64,host_flavor_t flavor,host_info64_t info,mach_msg_type_number_t * count,kern_return_t * ret,int * pindex)734 rate_limit_host_statistics(bool is_stat64, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t* count, kern_return_t* ret, int *pindex)
735 {
736 	task_t task = current_task();
737 
738 	assert(task != kernel_task);
739 
740 	*ret = KERN_SUCCESS;
741 	*pindex = -1;
742 
743 	/* Access control only for third party applications */
744 	if (task_get_platform_binary(task)) {
745 		return FALSE;
746 	}
747 
748 	/* Rate limit to HOST_STATISTICS_MAX_REQUESTS queries for each HOST_STATISTICS_TIME_WINDOW window of time */
749 	bool rate_limited = FALSE;
750 	bool set_last_access = TRUE;
751 
752 	/* there is a cache for every flavor */
753 	int index = get_host_info_data_index(is_stat64, flavor, count, ret);
754 	if (index == -1) {
755 		goto out;
756 	}
757 
758 	*pindex = index;
759 	lck_mtx_lock(&host_statistics_lck);
760 	if (g_host_stats_cache[index].last_access > mach_continuous_time() - host_statistics_time_window) {
761 		set_last_access = FALSE;
762 		if (g_host_stats_cache[index].current_requests++ >= g_host_stats_cache[index].max_requests) {
763 			rate_limited = TRUE;
764 			get_cached_info(index, info, count);
765 		}
766 	}
767 	if (set_last_access) {
768 		g_host_stats_cache[index].current_requests = 1;
769 		/*
770 		 * select a random number of requests (included between HOST_STATISTICS_MIN_REQUESTS and HOST_STATISTICS_MAX_REQUESTS)
771 		 * to let query host_statistics.
772 		 * In this way it is not possible to infer looking at when the a cached copy changes if host_statistics was called on
773 		 * the provious window.
774 		 */
775 		g_host_stats_cache[index].max_requests = (mach_absolute_time() % (HOST_STATISTICS_MAX_REQUESTS - HOST_STATISTICS_MIN_REQUESTS + 1)) + HOST_STATISTICS_MIN_REQUESTS;
776 		g_host_stats_cache[index].last_access = mach_continuous_time();
777 	}
778 	lck_mtx_unlock(&host_statistics_lck);
779 out:
780 	return rate_limited;
781 }
782 
783 kern_return_t
vm_stats(void * info,unsigned int * count)784 vm_stats(void *info, unsigned int *count)
785 {
786 	vm_statistics64_data_t host_vm_stat;
787 	mach_msg_type_number_t original_count;
788 	unsigned int local_q_internal_count;
789 	unsigned int local_q_external_count;
790 	natural_t speculative_count = vm_page_speculative_count;
791 	natural_t throttled_count = vm_page_throttled_count;
792 
793 	if (*count < HOST_VM_INFO64_REV0_COUNT) {
794 		return KERN_FAILURE;
795 	}
796 	get_host_vm_stats(&host_vm_stat);
797 
798 	vm_statistics64_t stat = (vm_statistics64_t)info;
799 
800 	stat->free_count = vm_page_free_count + speculative_count;
801 	stat->active_count = vm_page_active_count;
802 
803 	local_q_internal_count = 0;
804 	local_q_external_count = 0;
805 	if (vm_page_local_q) {
806 		zpercpu_foreach(lq, vm_page_local_q) {
807 			stat->active_count += lq->vpl_count;
808 			local_q_internal_count += lq->vpl_internal_count;
809 			local_q_external_count += lq->vpl_external_count;
810 		}
811 	}
812 	stat->inactive_count = vm_page_inactive_count;
813 #if !XNU_TARGET_OS_OSX
814 	stat->wire_count = vm_page_wire_count;
815 #else /* !XNU_TARGET_OS_OSX */
816 	stat->wire_count = vm_page_wire_count + throttled_count + vm_lopage_free_count;
817 #endif /* !XNU_TARGET_OS_OSX */
818 	stat->zero_fill_count = host_vm_stat.zero_fill_count;
819 	stat->reactivations = host_vm_stat.reactivations;
820 	stat->pageins = host_vm_stat.pageins;
821 	stat->pageouts = host_vm_stat.pageouts;
822 	stat->faults = host_vm_stat.faults;
823 	stat->cow_faults = host_vm_stat.cow_faults;
824 	stat->lookups = host_vm_stat.lookups;
825 	stat->hits = host_vm_stat.hits;
826 
827 	stat->purgeable_count = vm_page_purgeable_count;
828 	stat->purges = vm_page_purged_count;
829 
830 	stat->speculative_count = speculative_count;
831 
832 	/*
833 	 * Fill in extra info added in later revisions of the
834 	 * vm_statistics data structure.  Fill in only what can fit
835 	 * in the data structure the caller gave us !
836 	 */
837 	original_count = *count;
838 	*count = HOST_VM_INFO64_REV0_COUNT; /* rev0 already filled in */
839 	if (original_count >= HOST_VM_INFO64_REV1_COUNT) {
840 		/* rev1 added "throttled count" */
841 		stat->throttled_count = throttled_count;
842 		/* rev1 added "compression" info */
843 		stat->compressor_page_count = VM_PAGE_COMPRESSOR_COUNT;
844 		stat->compressions = host_vm_stat.compressions;
845 		stat->decompressions = host_vm_stat.decompressions;
846 		stat->swapins = host_vm_stat.swapins;
847 		stat->swapouts = host_vm_stat.swapouts;
848 		/* rev1 added:
849 		 * "external page count"
850 		 * "anonymous page count"
851 		 * "total # of pages (uncompressed) held in the compressor"
852 		 */
853 		stat->external_page_count = (vm_page_pageable_external_count + local_q_external_count);
854 		stat->internal_page_count = (vm_page_pageable_internal_count + local_q_internal_count);
855 		stat->total_uncompressed_pages_in_compressor = c_segment_pages_compressed;
856 		*count = HOST_VM_INFO64_REV1_COUNT;
857 	}
858 
859 	return KERN_SUCCESS;
860 }
861 
862 #if DEVELOPMENT || DEBUG
863 extern uint32_t        c_segment_count;
864 extern uint32_t        c_age_count;
865 extern uint32_t        c_early_swappedin_count, c_regular_swappedin_count, c_late_swappedin_count;
866 extern uint32_t        c_early_swapout_count, c_regular_swapout_count, c_late_swapout_count;
867 extern uint32_t        c_swapio_count;
868 extern uint32_t        c_swappedout_count;
869 extern uint32_t        c_swappedout_sparse_count;
870 extern uint32_t        c_major_count;
871 extern uint32_t        c_filling_count;
872 extern uint32_t        c_empty_count;
873 extern uint32_t        c_bad_count;
874 extern uint32_t        c_minor_count;
875 extern uint32_t        c_segments_available;
876 
877 static kern_return_t
vm_compressor_queue_lens(void * info,unsigned int * count)878 vm_compressor_queue_lens(void *info, unsigned int *count)
879 {
880 	if (*count < VM_COMPRESSOR_Q_LENS_COUNT) {
881 		return KERN_NO_SPACE;
882 	}
883 
884 	struct vm_compressor_q_lens *qc = (struct vm_compressor_q_lens *)info;
885 	qc->qcc_segments_available = c_segments_available;
886 	qc->qcc_segment_count = c_segment_count;
887 	qc->qcc_age_count = c_age_count;
888 	qc->qcc_early_swappedin_count = c_early_swappedin_count;
889 	qc->qcc_regular_swappedin_count = c_regular_swappedin_count;
890 	qc->qcc_late_swappedin_count = c_late_swappedin_count;
891 	qc->qcc_early_swapout_count = c_early_swapout_count;
892 	qc->qcc_regular_swapout_count = c_regular_swapout_count;
893 	qc->qcc_late_swapout_count = c_late_swapout_count;
894 	qc->qcc_swapio_count = c_swapio_count;
895 	qc->qcc_swappedout_count = c_swappedout_count;
896 	qc->qcc_swappedout_sparse_count = c_swappedout_sparse_count;
897 	qc->qcc_major_count = c_major_count;
898 	qc->qcc_filling_count = c_filling_count;
899 	qc->qcc_empty_count = c_empty_count;
900 	qc->qcc_bad_count = c_bad_count;
901 	qc->qcc_minor_count = c_minor_count;
902 
903 	*count = VM_COMPRESSOR_Q_LENS_COUNT;
904 
905 	return KERN_SUCCESS;
906 }
907 
908 #endif /* DEVELOPMENT || DEBUG */
909 
910 kern_return_t host_statistics64(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count);
911 
912 kern_return_t
host_statistics64(host_t host,host_flavor_t flavor,host_info64_t info,mach_msg_type_number_t * count)913 host_statistics64(host_t host, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t * count)
914 {
915 	if (host == HOST_NULL) {
916 		return KERN_INVALID_HOST;
917 	}
918 
919 	switch (flavor) {
920 	case HOST_VM_INFO64: /* We were asked to get vm_statistics64 */
921 		return vm_stats(info, count);
922 
923 	case HOST_EXTMOD_INFO64: /* We were asked to get vm_statistics64 */
924 	{
925 		vm_extmod_statistics_t out_extmod_statistics;
926 
927 		if (*count < HOST_EXTMOD_INFO64_COUNT) {
928 			return KERN_FAILURE;
929 		}
930 
931 		out_extmod_statistics = (vm_extmod_statistics_t)info;
932 		*out_extmod_statistics = host_extmod_statistics;
933 
934 		*count = HOST_EXTMOD_INFO64_COUNT;
935 
936 		return KERN_SUCCESS;
937 	}
938 
939 	case HOST_VM_COMPRESSOR_Q_LENS:
940 #if DEVELOPMENT || DEBUG
941 		return vm_compressor_queue_lens(info, count);
942 #else
943 		return KERN_NOT_SUPPORTED;
944 #endif
945 
946 	default: /* If we didn't recognize the flavor, send to host_statistics */
947 		return host_statistics(host, flavor, (host_info_t)info, count);
948 	}
949 }
950 
951 kern_return_t
host_statistics64_from_user(host_t host,host_flavor_t flavor,host_info64_t info,mach_msg_type_number_t * count)952 host_statistics64_from_user(host_t host, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t * count)
953 {
954 	kern_return_t ret = KERN_SUCCESS;
955 	int index;
956 
957 	if (host == HOST_NULL) {
958 		return KERN_INVALID_HOST;
959 	}
960 
961 	if (rate_limit_host_statistics(TRUE, flavor, info, count, &ret, &index)) {
962 		return ret;
963 	}
964 
965 	if (ret != KERN_SUCCESS) {
966 		return ret;
967 	}
968 
969 	ret = host_statistics64(host, flavor, info, count);
970 
971 	if (ret == KERN_SUCCESS) {
972 		cache_host_statistics(index, info);
973 	}
974 
975 	return ret;
976 }
977 
978 kern_return_t
host_statistics_from_user(host_t host,host_flavor_t flavor,host_info64_t info,mach_msg_type_number_t * count)979 host_statistics_from_user(host_t host, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t * count)
980 {
981 	kern_return_t ret = KERN_SUCCESS;
982 	int index;
983 
984 	if (host == HOST_NULL) {
985 		return KERN_INVALID_HOST;
986 	}
987 
988 	if (rate_limit_host_statistics(FALSE, flavor, info, count, &ret, &index)) {
989 		return ret;
990 	}
991 
992 	if (ret != KERN_SUCCESS) {
993 		return ret;
994 	}
995 
996 	ret = host_statistics(host, flavor, info, count);
997 
998 	if (ret == KERN_SUCCESS) {
999 		cache_host_statistics(index, info);
1000 	}
1001 
1002 	return ret;
1003 }
1004 
1005 /*
1006  * Get host statistics that require privilege.
1007  * None for now, just call the un-privileged version.
1008  */
1009 kern_return_t
host_priv_statistics(host_priv_t host_priv,host_flavor_t flavor,host_info_t info,mach_msg_type_number_t * count)1010 host_priv_statistics(host_priv_t host_priv, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count)
1011 {
1012 	return host_statistics((host_t)host_priv, flavor, info, count);
1013 }
1014 
1015 kern_return_t
set_sched_stats_active(boolean_t active)1016 set_sched_stats_active(boolean_t active)
1017 {
1018 	sched_stats_active = active;
1019 	return KERN_SUCCESS;
1020 }
1021 
1022 kern_return_t
get_sched_statistics(struct _processor_statistics_np * out,uint32_t * count)1023 get_sched_statistics(struct _processor_statistics_np * out, uint32_t * count)
1024 {
1025 	uint32_t pos = 0;
1026 
1027 	if (!sched_stats_active) {
1028 		return KERN_FAILURE;
1029 	}
1030 
1031 	percpu_foreach_base(pcpu_base) {
1032 		struct sched_statistics stats;
1033 		processor_t processor;
1034 
1035 		pos += sizeof(struct _processor_statistics_np);
1036 		if (pos > *count) {
1037 			return KERN_FAILURE;
1038 		}
1039 
1040 		stats = *PERCPU_GET_WITH_BASE(pcpu_base, sched_stats);
1041 		processor = PERCPU_GET_WITH_BASE(pcpu_base, processor);
1042 
1043 		out->ps_cpuid = processor->cpu_id;
1044 		out->ps_csw_count = stats.csw_count;
1045 		out->ps_preempt_count = stats.preempt_count;
1046 		out->ps_preempted_rt_count = stats.preempted_rt_count;
1047 		out->ps_preempted_by_rt_count = stats.preempted_by_rt_count;
1048 		out->ps_rt_sched_count = stats.rt_sched_count;
1049 		out->ps_interrupt_count = stats.interrupt_count;
1050 		out->ps_ipi_count = stats.ipi_count;
1051 		out->ps_timer_pop_count = stats.timer_pop_count;
1052 		out->ps_runq_count_sum = SCHED(processor_runq_stats_count_sum)(processor);
1053 		out->ps_idle_transitions = stats.idle_transitions;
1054 		out->ps_quantum_timer_expirations = stats.quantum_timer_expirations;
1055 
1056 		out++;
1057 	}
1058 
1059 	/* And include RT Queue information */
1060 	pos += sizeof(struct _processor_statistics_np);
1061 	if (pos > *count) {
1062 		return KERN_FAILURE;
1063 	}
1064 
1065 	bzero(out, sizeof(*out));
1066 	out->ps_cpuid = (-1);
1067 	out->ps_runq_count_sum = SCHED(rt_runq_count_sum)();
1068 	out++;
1069 
1070 	*count = pos;
1071 
1072 	return KERN_SUCCESS;
1073 }
1074 
1075 kern_return_t
host_page_size(host_t host,vm_size_t * out_page_size)1076 host_page_size(host_t host, vm_size_t * out_page_size)
1077 {
1078 	if (host == HOST_NULL) {
1079 		return KERN_INVALID_ARGUMENT;
1080 	}
1081 
1082 	*out_page_size = PAGE_SIZE;
1083 
1084 	return KERN_SUCCESS;
1085 }
1086 
1087 /*
1088  *	Return kernel version string (more than you ever
1089  *	wanted to know about what version of the kernel this is).
1090  */
1091 extern char version[];
1092 
1093 kern_return_t
host_kernel_version(host_t host,kernel_version_t out_version)1094 host_kernel_version(host_t host, kernel_version_t out_version)
1095 {
1096 	if (host == HOST_NULL) {
1097 		return KERN_INVALID_ARGUMENT;
1098 	}
1099 
1100 	(void)strncpy(out_version, version, sizeof(kernel_version_t));
1101 
1102 	return KERN_SUCCESS;
1103 }
1104 
1105 /*
1106  *	host_processor_sets:
1107  *
1108  *	List all processor sets on the host.
1109  */
1110 kern_return_t
host_processor_sets(host_priv_t host_priv,processor_set_name_array_t * pset_list,mach_msg_type_number_t * count)1111 host_processor_sets(host_priv_t host_priv, processor_set_name_array_t * pset_list, mach_msg_type_number_t * count)
1112 {
1113 	mach_port_array_t ports;
1114 
1115 	if (host_priv == HOST_PRIV_NULL) {
1116 		return KERN_INVALID_ARGUMENT;
1117 	}
1118 
1119 	/*
1120 	 *	Allocate memory.  Can be pageable because it won't be
1121 	 *	touched while holding a lock.
1122 	 */
1123 
1124 	ports = mach_port_array_alloc(1, Z_WAITOK | Z_NOFAIL);
1125 
1126 	/* do the conversion that Mig should handle */
1127 	ports[0].port = convert_pset_name_to_port(&pset0);
1128 
1129 	*pset_list = ports;
1130 	*count = 1;
1131 
1132 	return KERN_SUCCESS;
1133 }
1134 
1135 /*
1136  *	host_processor_set_priv:
1137  *
1138  *	Return control port for given processor set.
1139  */
1140 kern_return_t
host_processor_set_priv(host_priv_t host_priv,processor_set_t pset_name,processor_set_t * pset)1141 host_processor_set_priv(host_priv_t host_priv, processor_set_t pset_name, processor_set_t * pset)
1142 {
1143 	if (host_priv == HOST_PRIV_NULL || pset_name == PROCESSOR_SET_NULL) {
1144 		*pset = PROCESSOR_SET_NULL;
1145 
1146 		return KERN_INVALID_ARGUMENT;
1147 	}
1148 
1149 	*pset = pset_name;
1150 
1151 	return KERN_SUCCESS;
1152 }
1153 
1154 /*
1155  *	host_processor_info
1156  *
1157  *	Return info about the processors on this host.  It will return
1158  *	the number of processors, and the specific type of info requested
1159  *	in an OOL array.
1160  */
1161 kern_return_t
host_processor_info(host_t host,processor_flavor_t flavor,natural_t * out_pcount,processor_info_array_t * out_array,mach_msg_type_number_t * out_array_count)1162 host_processor_info(host_t host,
1163     processor_flavor_t flavor,
1164     natural_t * out_pcount,
1165     processor_info_array_t * out_array,
1166     mach_msg_type_number_t * out_array_count)
1167 {
1168 	kern_return_t result;
1169 	host_t thost;
1170 	processor_info_t info;
1171 	unsigned int icount;
1172 	unsigned int pcount;
1173 	vm_offset_t addr;
1174 	vm_size_t size, needed;
1175 	vm_map_copy_t copy;
1176 
1177 	if (host == HOST_NULL) {
1178 		return KERN_INVALID_ARGUMENT;
1179 	}
1180 
1181 	result = processor_info_count(flavor, &icount);
1182 	if (result != KERN_SUCCESS) {
1183 		return result;
1184 	}
1185 
1186 	pcount = processor_count;
1187 	assert(pcount != 0);
1188 
1189 	needed = pcount * icount * sizeof(natural_t);
1190 	size = vm_map_round_page(needed, VM_MAP_PAGE_MASK(ipc_kernel_map));
1191 	result = kmem_alloc(ipc_kernel_map, &addr, size, KMA_DATA, VM_KERN_MEMORY_IPC);
1192 	if (result != KERN_SUCCESS) {
1193 		return KERN_RESOURCE_SHORTAGE;
1194 	}
1195 
1196 	info = (processor_info_t)addr;
1197 
1198 	for (unsigned int i = 0; i < pcount; i++) {
1199 		processor_t processor = processor_array[i];
1200 		assert(processor != PROCESSOR_NULL);
1201 
1202 		unsigned int tcount = icount;
1203 
1204 		result = processor_info(processor, flavor, &thost, info, &tcount);
1205 		if (result != KERN_SUCCESS) {
1206 			kmem_free(ipc_kernel_map, addr, size);
1207 			return result;
1208 		}
1209 		info += icount;
1210 	}
1211 
1212 	if (size != needed) {
1213 		bzero((char *)addr + needed, size - needed);
1214 	}
1215 
1216 	result = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr, VM_MAP_PAGE_MASK(ipc_kernel_map)),
1217 	    vm_map_round_page(addr + size, VM_MAP_PAGE_MASK(ipc_kernel_map)), FALSE);
1218 	assert(result == KERN_SUCCESS);
1219 	result = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr, (vm_map_size_t)needed, TRUE, &copy);
1220 	assert(result == KERN_SUCCESS);
1221 
1222 	*out_pcount = pcount;
1223 	*out_array = (processor_info_array_t)copy;
1224 	*out_array_count = pcount * icount;
1225 
1226 	return KERN_SUCCESS;
1227 }
1228 
1229 static bool
is_valid_host_special_port(int id)1230 is_valid_host_special_port(int id)
1231 {
1232 	return (id <= HOST_MAX_SPECIAL_PORT) &&
1233 	       (id >= HOST_MIN_SPECIAL_PORT) &&
1234 	       ((id <= HOST_LAST_SPECIAL_KERNEL_PORT) || (id > HOST_MAX_SPECIAL_KERNEL_PORT));
1235 }
1236 
1237 extern void * XNU_PTRAUTH_SIGNED_PTR("initproc") initproc;
1238 
1239 /*
1240  *      Kernel interface for setting a special port.
1241  */
1242 kern_return_t
kernel_set_special_port(host_priv_t host_priv,int id,ipc_port_t port)1243 kernel_set_special_port(host_priv_t host_priv, int id, ipc_port_t port)
1244 {
1245 	ipc_port_t old_port;
1246 
1247 	if (!is_valid_host_special_port(id)) {
1248 		panic("attempted to set invalid special port %d", id);
1249 	}
1250 
1251 #if !MACH_FLIPC
1252 	if (id == HOST_NODE_PORT) {
1253 		return KERN_NOT_SUPPORTED;
1254 	}
1255 #endif
1256 
1257 	host_lock(host_priv);
1258 	old_port = host_priv->special[id];
1259 	host_priv->special[id] = port;
1260 	host_unlock(host_priv);
1261 
1262 #if MACH_FLIPC
1263 	if (id == HOST_NODE_PORT) {
1264 		mach_node_port_changed();
1265 	}
1266 #endif
1267 
1268 	if (IP_VALID(old_port)) {
1269 		ipc_port_release_send(old_port);
1270 	}
1271 
1272 
1273 	return KERN_SUCCESS;
1274 }
1275 
1276 /*
1277  *      Kernel interface for retrieving a special port.
1278  */
1279 kern_return_t
kernel_get_special_port(host_priv_t host_priv,int id,ipc_port_t * portp)1280 kernel_get_special_port(host_priv_t host_priv, int id, ipc_port_t * portp)
1281 {
1282 	if (!is_valid_host_special_port(id)) {
1283 		panic("attempted to get invalid special port %d", id);
1284 	}
1285 
1286 	host_lock(host_priv);
1287 	*portp = host_priv->special[id];
1288 	host_unlock(host_priv);
1289 	return KERN_SUCCESS;
1290 }
1291 
1292 /*
1293  *      User interface for setting a special port.
1294  *
1295  *      Only permits the user to set a user-owned special port
1296  *      ID, rejecting a kernel-owned special port ID.
1297  *
1298  *      A special kernel port cannot be set up using this
1299  *      routine; use kernel_set_special_port() instead.
1300  */
1301 kern_return_t
host_set_special_port_from_user(host_priv_t host_priv,int id,ipc_port_t port)1302 host_set_special_port_from_user(host_priv_t host_priv, int id, ipc_port_t port)
1303 {
1304 	if (host_priv == HOST_PRIV_NULL || id <= HOST_MAX_SPECIAL_KERNEL_PORT || id > HOST_MAX_SPECIAL_PORT) {
1305 		return KERN_INVALID_ARGUMENT;
1306 	}
1307 
1308 	if (task_is_driver(current_task())) {
1309 		return KERN_NO_ACCESS;
1310 	}
1311 
1312 	if (IP_VALID(port) && (port->ip_immovable_receive || port->ip_immovable_send)) {
1313 		return KERN_INVALID_RIGHT;
1314 	}
1315 
1316 	return host_set_special_port(host_priv, id, port);
1317 }
1318 
1319 kern_return_t
host_set_special_port(host_priv_t host_priv,int id,ipc_port_t port)1320 host_set_special_port(host_priv_t host_priv, int id, ipc_port_t port)
1321 {
1322 	if (host_priv == HOST_PRIV_NULL || id <= HOST_MAX_SPECIAL_KERNEL_PORT || id > HOST_MAX_SPECIAL_PORT) {
1323 		return KERN_INVALID_ARGUMENT;
1324 	}
1325 
1326 	if (current_task() != kernel_task && get_bsdtask_info(current_task()) != initproc) {
1327 		bool allowed = (id == HOST_TELEMETRY_PORT &&
1328 		    IOTaskHasEntitlement(current_task(), "com.apple.private.xpc.launchd.event-monitor"));
1329 #if CONFIG_CSR
1330 		if (!allowed) {
1331 			allowed = (csr_check(CSR_ALLOW_TASK_FOR_PID) == 0);
1332 		}
1333 #endif
1334 		if (!allowed) {
1335 			return KERN_NO_ACCESS;
1336 		}
1337 	}
1338 
1339 #if CONFIG_MACF
1340 	if (mac_task_check_set_host_special_port(current_task(), id, port) != 0) {
1341 		return KERN_NO_ACCESS;
1342 	}
1343 #endif
1344 
1345 	return kernel_set_special_port(host_priv, id, port);
1346 }
1347 
1348 /*
1349  *      User interface for retrieving a special port.
1350  *
1351  *      Note that there is nothing to prevent a user special
1352  *      port from disappearing after it has been discovered by
1353  *      the caller; thus, using a special port can always result
1354  *      in a "port not valid" error.
1355  */
1356 
1357 kern_return_t
host_get_special_port_from_user(host_priv_t host_priv,__unused int node,int id,ipc_port_t * portp)1358 host_get_special_port_from_user(host_priv_t host_priv, __unused int node, int id, ipc_port_t * portp)
1359 {
1360 	if (host_priv == HOST_PRIV_NULL || id == HOST_SECURITY_PORT || id > HOST_MAX_SPECIAL_PORT || id < HOST_MIN_SPECIAL_PORT) {
1361 		return KERN_INVALID_ARGUMENT;
1362 	}
1363 
1364 	task_t task = current_task();
1365 	if (task && task_is_driver(task) && id > HOST_MAX_SPECIAL_KERNEL_PORT) {
1366 		/* allow HID drivers to get the sysdiagnose port for keychord handling */
1367 		if (id == HOST_SYSDIAGNOSE_PORT &&
1368 		    IOCurrentTaskHasEntitlement(kIODriverKitHIDFamilyEventServiceEntitlementKey)) {
1369 			goto get_special_port;
1370 		}
1371 		return KERN_NO_ACCESS;
1372 	}
1373 get_special_port:
1374 	return host_get_special_port(host_priv, node, id, portp);
1375 }
1376 
1377 kern_return_t
host_get_special_port(host_priv_t host_priv,__unused int node,int id,ipc_port_t * portp)1378 host_get_special_port(host_priv_t host_priv, __unused int node, int id, ipc_port_t * portp)
1379 {
1380 	ipc_port_t port;
1381 
1382 	if (host_priv == HOST_PRIV_NULL || id == HOST_SECURITY_PORT || id > HOST_MAX_SPECIAL_PORT || id < HOST_MIN_SPECIAL_PORT) {
1383 		return KERN_INVALID_ARGUMENT;
1384 	}
1385 
1386 	host_lock(host_priv);
1387 	port = realhost.special[id];
1388 	switch (id) {
1389 	case HOST_PORT:
1390 		*portp = ipc_kobject_copy_send(port, &realhost, IKOT_HOST);
1391 		break;
1392 	case HOST_PRIV_PORT:
1393 		*portp = ipc_kobject_copy_send(port, &realhost, IKOT_HOST_PRIV);
1394 		break;
1395 	case HOST_IO_MAIN_PORT:
1396 		*portp = ipc_port_copy_send_any(main_device_port);
1397 		break;
1398 	default:
1399 		*portp = ipc_port_copy_send_mqueue(port);
1400 		break;
1401 	}
1402 	host_unlock(host_priv);
1403 
1404 	return KERN_SUCCESS;
1405 }
1406 
1407 /*
1408  *	host_get_io_main
1409  *
1410  *	Return the IO main access port for this host.
1411  */
1412 kern_return_t
host_get_io_main(host_t host,io_main_t * io_mainp)1413 host_get_io_main(host_t host, io_main_t * io_mainp)
1414 {
1415 	if (host == HOST_NULL) {
1416 		return KERN_INVALID_ARGUMENT;
1417 	}
1418 
1419 	return host_get_io_main_port(host_priv_self(), io_mainp);
1420 }
1421 
1422 host_t
host_self(void)1423 host_self(void)
1424 {
1425 	return &realhost;
1426 }
1427 
1428 host_priv_t
host_priv_self(void)1429 host_priv_self(void)
1430 {
1431 	return &realhost;
1432 }
1433 
1434 kern_return_t
host_set_atm_diagnostic_flag(host_t host,uint32_t diagnostic_flag)1435 host_set_atm_diagnostic_flag(host_t host, uint32_t diagnostic_flag)
1436 {
1437 	if (host == HOST_NULL) {
1438 		return KERN_INVALID_ARGUMENT;
1439 	}
1440 
1441 	if (!IOCurrentTaskHasEntitlement("com.apple.private.set-atm-diagnostic-flag")) {
1442 		return KERN_NO_ACCESS;
1443 	}
1444 
1445 #if CONFIG_ATM
1446 	return atm_set_diagnostic_config(diagnostic_flag);
1447 #else
1448 	(void)diagnostic_flag;
1449 	return KERN_NOT_SUPPORTED;
1450 #endif
1451 }
1452 
1453 kern_return_t
host_set_multiuser_config_flags(host_priv_t host_priv,uint32_t multiuser_config)1454 host_set_multiuser_config_flags(host_priv_t host_priv, uint32_t multiuser_config)
1455 {
1456 #if !defined(XNU_TARGET_OS_OSX)
1457 	if (host_priv == HOST_PRIV_NULL) {
1458 		return KERN_INVALID_ARGUMENT;
1459 	}
1460 
1461 	/*
1462 	 * multiuser bit is extensively used for sharedIpad mode.
1463 	 * Caller sets the sharedIPad or other mutiuser modes.
1464 	 * Any override during commpage setting is not suitable anymore.
1465 	 */
1466 	commpage_update_multiuser_config(multiuser_config);
1467 	return KERN_SUCCESS;
1468 #else
1469 	(void)host_priv;
1470 	(void)multiuser_config;
1471 	return KERN_NOT_SUPPORTED;
1472 #endif
1473 }
1474