xref: /xnu-8796.121.2/osfmk/kern/host.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
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.h>
98 #include <vm/vm_purgeable_internal.h>
99 #include <vm/vm_pageout.h>
100 
101 #include <IOKit/IOBSD.h> // IOTaskHasEntitlement
102 #include <IOKit/IOKitKeys.h> // DriverKit entitlement strings
103 
104 
105 #if CONFIG_ATM
106 #include <atm/atm_internal.h>
107 #endif
108 
109 #if CONFIG_MACF
110 #include <security/mac_mach_internal.h>
111 #endif
112 
113 #if CONFIG_CSR
114 #include <sys/csr.h>
115 #endif
116 
117 #include <pexpert/pexpert.h>
118 
119 SCALABLE_COUNTER_DEFINE(vm_statistics_zero_fill_count);        /* # of zero fill pages */
120 SCALABLE_COUNTER_DEFINE(vm_statistics_reactivations);          /* # of pages reactivated */
121 SCALABLE_COUNTER_DEFINE(vm_statistics_pageins);                /* # of pageins */
122 SCALABLE_COUNTER_DEFINE(vm_statistics_pageouts);               /* # of pageouts */
123 SCALABLE_COUNTER_DEFINE(vm_statistics_faults);                 /* # of faults */
124 SCALABLE_COUNTER_DEFINE(vm_statistics_cow_faults);             /* # of copy-on-writes */
125 SCALABLE_COUNTER_DEFINE(vm_statistics_lookups);                /* object cache lookups */
126 SCALABLE_COUNTER_DEFINE(vm_statistics_hits);                   /* object cache hits */
127 SCALABLE_COUNTER_DEFINE(vm_statistics_purges);                 /* # of pages purged */
128 SCALABLE_COUNTER_DEFINE(vm_statistics_decompressions);         /* # of pages decompressed */
129 SCALABLE_COUNTER_DEFINE(vm_statistics_compressions);           /* # of pages compressed */
130 SCALABLE_COUNTER_DEFINE(vm_statistics_swapins);                /* # of pages swapped in (via compression segments) */
131 SCALABLE_COUNTER_DEFINE(vm_statistics_swapouts);               /* # of pages swapped out (via compression segments) */
132 SCALABLE_COUNTER_DEFINE(vm_statistics_total_uncompressed_pages_in_compressor); /* # of pages (uncompressed) held within the compressor. */
133 SCALABLE_COUNTER_DEFINE(vm_page_grab_count);
134 
135 host_data_t realhost;
136 
137 static void
get_host_vm_stats(vm_statistics64_t out)138 get_host_vm_stats(vm_statistics64_t out)
139 {
140 	out->zero_fill_count = counter_load(&vm_statistics_zero_fill_count);
141 	out->reactivations = counter_load(&vm_statistics_reactivations);
142 	out->pageins = counter_load(&vm_statistics_pageins);
143 	out->pageouts = counter_load(&vm_statistics_pageouts);
144 	out->faults = counter_load(&vm_statistics_faults);
145 	out->cow_faults = counter_load(&vm_statistics_cow_faults);
146 	out->lookups = counter_load(&vm_statistics_lookups);
147 	out->hits = counter_load(&vm_statistics_hits);
148 	out->compressions = counter_load(&vm_statistics_compressions);
149 	out->decompressions = counter_load(&vm_statistics_decompressions);
150 	out->swapins = counter_load(&vm_statistics_swapins);
151 	out->swapouts = counter_load(&vm_statistics_swapouts);
152 }
153 vm_extmod_statistics_data_t host_extmod_statistics;
154 
155 kern_return_t
host_processors(host_priv_t host_priv,processor_array_t * out_array,mach_msg_type_number_t * countp)156 host_processors(host_priv_t host_priv, processor_array_t * out_array, mach_msg_type_number_t * countp)
157 {
158 	if (host_priv == HOST_PRIV_NULL) {
159 		return KERN_INVALID_ARGUMENT;
160 	}
161 
162 	unsigned int count = processor_count;
163 	assert(count != 0);
164 
165 	static_assert(sizeof(mach_port_t) == sizeof(processor_t));
166 
167 	mach_port_t *ports = kalloc_type(mach_port_t, count, Z_WAITOK);
168 	if (!ports) {
169 		return KERN_RESOURCE_SHORTAGE;
170 	}
171 
172 	for (unsigned int i = 0; i < count; i++) {
173 		processor_t processor = processor_array[i];
174 		assert(processor != PROCESSOR_NULL);
175 
176 		/* do the conversion that Mig should handle */
177 		ipc_port_t processor_port = convert_processor_to_port(processor);
178 		ports[i] = processor_port;
179 	}
180 
181 	*countp = count;
182 	*out_array = (processor_array_t)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 
236 			basic_info->max_mem = machine_info.max_mem;
237 
238 			*count = HOST_BASIC_INFO_COUNT;
239 		} else {
240 			*count = HOST_BASIC_INFO_OLD_COUNT;
241 		}
242 
243 		return KERN_SUCCESS;
244 	}
245 
246 	case HOST_SCHED_INFO: {
247 		host_sched_info_t sched_info;
248 		uint32_t quantum_time;
249 		uint64_t quantum_ns;
250 
251 		/*
252 		 *	Return scheduler information.
253 		 */
254 		if (*count < HOST_SCHED_INFO_COUNT) {
255 			return KERN_FAILURE;
256 		}
257 
258 		sched_info = (host_sched_info_t)info;
259 
260 		quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
261 		absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
262 
263 		sched_info->min_timeout = sched_info->min_quantum = (uint32_t)(quantum_ns / 1000 / 1000);
264 
265 		*count = HOST_SCHED_INFO_COUNT;
266 
267 		return KERN_SUCCESS;
268 	}
269 
270 	case HOST_RESOURCE_SIZES: {
271 		/*
272 		 * Return sizes of kernel data structures
273 		 */
274 		if (*count < HOST_RESOURCE_SIZES_COUNT) {
275 			return KERN_FAILURE;
276 		}
277 
278 		/* XXX Fail until ledgers are implemented */
279 		return KERN_INVALID_ARGUMENT;
280 	}
281 
282 	case HOST_PRIORITY_INFO: {
283 		host_priority_info_t priority_info;
284 
285 		if (*count < HOST_PRIORITY_INFO_COUNT) {
286 			return KERN_FAILURE;
287 		}
288 
289 		priority_info = (host_priority_info_t)info;
290 
291 		priority_info->kernel_priority = MINPRI_KERNEL;
292 		priority_info->system_priority = MINPRI_KERNEL;
293 		priority_info->server_priority = MINPRI_RESERVED;
294 		priority_info->user_priority = BASEPRI_DEFAULT;
295 		priority_info->depress_priority = DEPRESSPRI;
296 		priority_info->idle_priority = IDLEPRI;
297 		priority_info->minimum_priority = MINPRI_USER;
298 		priority_info->maximum_priority = MAXPRI_RESERVED;
299 
300 		*count = HOST_PRIORITY_INFO_COUNT;
301 
302 		return KERN_SUCCESS;
303 	}
304 
305 	/*
306 	 * Gestalt for various trap facilities.
307 	 */
308 	case HOST_MACH_MSG_TRAP:
309 	case HOST_SEMAPHORE_TRAPS: {
310 		*count = 0;
311 		return KERN_SUCCESS;
312 	}
313 
314 	case HOST_CAN_HAS_DEBUGGER: {
315 		host_can_has_debugger_info_t can_has_debugger_info;
316 
317 		if (*count < HOST_CAN_HAS_DEBUGGER_COUNT) {
318 			return KERN_FAILURE;
319 		}
320 
321 		can_has_debugger_info = (host_can_has_debugger_info_t)info;
322 		can_has_debugger_info->can_has_debugger = PE_i_can_has_debugger(NULL);
323 		*count = HOST_CAN_HAS_DEBUGGER_COUNT;
324 
325 		return KERN_SUCCESS;
326 	}
327 
328 	case HOST_VM_PURGABLE: {
329 		if (*count < HOST_VM_PURGABLE_COUNT) {
330 			return KERN_FAILURE;
331 		}
332 
333 		vm_purgeable_stats((vm_purgeable_info_t)info, NULL);
334 
335 		*count = HOST_VM_PURGABLE_COUNT;
336 		return KERN_SUCCESS;
337 	}
338 
339 	case HOST_DEBUG_INFO_INTERNAL: {
340 #if DEVELOPMENT || DEBUG
341 		if (*count < HOST_DEBUG_INFO_INTERNAL_COUNT) {
342 			return KERN_FAILURE;
343 		}
344 
345 		host_debug_info_internal_t debug_info = (host_debug_info_internal_t)info;
346 		bzero(debug_info, sizeof(host_debug_info_internal_data_t));
347 		*count = HOST_DEBUG_INFO_INTERNAL_COUNT;
348 
349 #if CONFIG_COALITIONS
350 		debug_info->config_coalitions = 1;
351 #endif
352 		debug_info->config_bank = 1;
353 #if CONFIG_ATM
354 		debug_info->config_atm = 1;
355 #endif
356 #if CONFIG_CSR
357 		debug_info->config_csr = 1;
358 #endif
359 		return KERN_SUCCESS;
360 #else /* DEVELOPMENT || DEBUG */
361 		return KERN_NOT_SUPPORTED;
362 #endif
363 	}
364 
365 	case HOST_PREFERRED_USER_ARCH: {
366 		host_preferred_user_arch_t user_arch_info;
367 
368 		/*
369 		 *	Basic information about this host.
370 		 */
371 		if (*count < HOST_PREFERRED_USER_ARCH_COUNT) {
372 			return KERN_FAILURE;
373 		}
374 
375 		user_arch_info = (host_preferred_user_arch_t)info;
376 
377 #if defined(PREFERRED_USER_CPU_TYPE) && defined(PREFERRED_USER_CPU_SUBTYPE)
378 		cpu_type_t preferred_cpu_type;
379 		cpu_subtype_t preferred_cpu_subtype;
380 		if (!PE_get_default("kern.preferred_cpu_type", &preferred_cpu_type, sizeof(cpu_type_t))) {
381 			preferred_cpu_type = PREFERRED_USER_CPU_TYPE;
382 		}
383 		if (!PE_get_default("kern.preferred_cpu_subtype", &preferred_cpu_subtype, sizeof(cpu_subtype_t))) {
384 			preferred_cpu_subtype = PREFERRED_USER_CPU_SUBTYPE;
385 		}
386 		user_arch_info->cpu_type    = preferred_cpu_type;
387 		user_arch_info->cpu_subtype = preferred_cpu_subtype;
388 #else
389 		int master_id               = master_processor->cpu_id;
390 		user_arch_info->cpu_type    = slot_type(master_id);
391 		user_arch_info->cpu_subtype = slot_subtype(master_id);
392 #endif
393 
394 
395 		*count = HOST_PREFERRED_USER_ARCH_COUNT;
396 
397 		return KERN_SUCCESS;
398 	}
399 
400 	default: return KERN_INVALID_ARGUMENT;
401 	}
402 }
403 
404 kern_return_t host_statistics(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count);
405 
406 kern_return_t
host_statistics(host_t host,host_flavor_t flavor,host_info_t info,mach_msg_type_number_t * count)407 host_statistics(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count)
408 {
409 	if (host == HOST_NULL) {
410 		return KERN_INVALID_HOST;
411 	}
412 
413 	switch (flavor) {
414 	case HOST_LOAD_INFO: {
415 		host_load_info_t load_info;
416 
417 		if (*count < HOST_LOAD_INFO_COUNT) {
418 			return KERN_FAILURE;
419 		}
420 
421 		load_info = (host_load_info_t)info;
422 
423 		bcopy((char *)avenrun, (char *)load_info->avenrun, sizeof avenrun);
424 		bcopy((char *)mach_factor, (char *)load_info->mach_factor, sizeof mach_factor);
425 
426 		*count = HOST_LOAD_INFO_COUNT;
427 		return KERN_SUCCESS;
428 	}
429 
430 	case HOST_VM_INFO: {
431 		vm_statistics64_data_t host_vm_stat;
432 		vm_statistics_t stat32;
433 		mach_msg_type_number_t original_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 + vm_page_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(vm_page_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 NUM_HOST_INFO_DATA_TYPES        10
584 
585 static vm_statistics64_data_t host_vm_info64_rev0 = {};
586 static vm_statistics64_data_t host_vm_info64_rev1 = {};
587 static vm_extmod_statistics_data_t host_extmod_info64 = {};
588 static host_load_info_data_t host_load_info = {};
589 static vm_statistics_data_t host_vm_info_rev0 = {};
590 static vm_statistics_data_t host_vm_info_rev1 = {};
591 static vm_statistics_data_t host_vm_info_rev2 = {};
592 static host_cpu_load_info_data_t host_cpu_load_info = {};
593 static task_power_info_data_t host_expired_task_info = {};
594 static task_power_info_v2_data_t host_expired_task_info2 = {};
595 
596 struct host_stats_cache {
597 	uint64_t last_access;
598 	uint64_t current_requests;
599 	uint64_t max_requests;
600 	uintptr_t data;
601 	mach_msg_type_number_t count; //NOTE count is in sizeof(integer_t)
602 };
603 
604 static struct host_stats_cache g_host_stats_cache[NUM_HOST_INFO_DATA_TYPES] = {
605 	[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 },
606 	[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 },
607 	[HOST_EXTMOD_INFO64_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_extmod_info64, .count = HOST_EXTMOD_INFO64_COUNT },
608 	[HOST_LOAD_INFO_REV0] = { .last_access = 0, .current_requests = 0, .max_requests = 0, .data = (uintptr_t)&host_load_info, .count = HOST_LOAD_INFO_COUNT },
609 	[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 },
610 	[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 },
611 	[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 },
612 	[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 },
613 	[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 },
614 	[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},
615 };
616 
617 
618 void
host_statistics_init(void)619 host_statistics_init(void)
620 {
621 	nanoseconds_to_absolutetime((HOST_STATISTICS_TIME_WINDOW * NSEC_PER_SEC), &host_statistics_time_window);
622 }
623 
624 static void
cache_host_statistics(int index,host_info64_t info)625 cache_host_statistics(int index, host_info64_t info)
626 {
627 	if (index < 0 || index >= NUM_HOST_INFO_DATA_TYPES) {
628 		return;
629 	}
630 
631 	if (task_get_platform_binary(current_task())) {
632 		return;
633 	}
634 
635 	memcpy((void *)g_host_stats_cache[index].data, info, g_host_stats_cache[index].count * sizeof(integer_t));
636 	return;
637 }
638 
639 static void
get_cached_info(int index,host_info64_t info,mach_msg_type_number_t * count)640 get_cached_info(int index, host_info64_t info, mach_msg_type_number_t* count)
641 {
642 	if (index < 0 || index >= NUM_HOST_INFO_DATA_TYPES) {
643 		*count = 0;
644 		return;
645 	}
646 
647 	*count = g_host_stats_cache[index].count;
648 	memcpy(info, (void *)g_host_stats_cache[index].data, g_host_stats_cache[index].count * sizeof(integer_t));
649 }
650 
651 static int
get_host_info_data_index(bool is_stat64,host_flavor_t flavor,mach_msg_type_number_t * count,kern_return_t * ret)652 get_host_info_data_index(bool is_stat64, host_flavor_t flavor, mach_msg_type_number_t* count, kern_return_t* ret)
653 {
654 	switch (flavor) {
655 	case HOST_VM_INFO64:
656 		if (!is_stat64) {
657 			*ret = KERN_INVALID_ARGUMENT;
658 			return -1;
659 		}
660 		if (*count < HOST_VM_INFO64_REV0_COUNT) {
661 			*ret = KERN_FAILURE;
662 			return -1;
663 		}
664 		if (*count >= HOST_VM_INFO64_REV1_COUNT) {
665 			return HOST_VM_INFO64_REV1;
666 		}
667 		return HOST_VM_INFO64_REV0;
668 
669 	case HOST_EXTMOD_INFO64:
670 		if (!is_stat64) {
671 			*ret = KERN_INVALID_ARGUMENT;
672 			return -1;
673 		}
674 		if (*count < HOST_EXTMOD_INFO64_COUNT) {
675 			*ret = KERN_FAILURE;
676 			return -1;
677 		}
678 		return HOST_EXTMOD_INFO64_REV0;
679 
680 	case HOST_LOAD_INFO:
681 		if (*count < HOST_LOAD_INFO_COUNT) {
682 			*ret = KERN_FAILURE;
683 			return -1;
684 		}
685 		return HOST_LOAD_INFO_REV0;
686 
687 	case HOST_VM_INFO:
688 		if (*count < HOST_VM_INFO_REV0_COUNT) {
689 			*ret = KERN_FAILURE;
690 			return -1;
691 		}
692 		if (*count >= HOST_VM_INFO_REV2_COUNT) {
693 			return HOST_VM_INFO_REV2;
694 		}
695 		if (*count >= HOST_VM_INFO_REV1_COUNT) {
696 			return HOST_VM_INFO_REV1;
697 		}
698 		return HOST_VM_INFO_REV0;
699 
700 	case HOST_CPU_LOAD_INFO:
701 		if (*count < HOST_CPU_LOAD_INFO_COUNT) {
702 			*ret = KERN_FAILURE;
703 			return -1;
704 		}
705 		return HOST_CPU_LOAD_INFO_REV0;
706 
707 	case HOST_EXPIRED_TASK_INFO:
708 		if (*count < TASK_POWER_INFO_COUNT) {
709 			*ret = KERN_FAILURE;
710 			return -1;
711 		}
712 		if (*count >= TASK_POWER_INFO_V2_COUNT) {
713 			return HOST_EXPIRED_TASK_INFO_REV1;
714 		}
715 		return HOST_EXPIRED_TASK_INFO_REV0;
716 
717 	default:
718 		*ret = KERN_INVALID_ARGUMENT;
719 		return -1;
720 	}
721 }
722 
723 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)724 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)
725 {
726 	task_t task = current_task();
727 
728 	assert(task != kernel_task);
729 
730 	*ret = KERN_SUCCESS;
731 	*pindex = -1;
732 
733 	/* Access control only for third party applications */
734 	if (task_get_platform_binary(task)) {
735 		return FALSE;
736 	}
737 
738 	/* Rate limit to HOST_STATISTICS_MAX_REQUESTS queries for each HOST_STATISTICS_TIME_WINDOW window of time */
739 	bool rate_limited = FALSE;
740 	bool set_last_access = TRUE;
741 
742 	/* there is a cache for every flavor */
743 	int index = get_host_info_data_index(is_stat64, flavor, count, ret);
744 	if (index == -1) {
745 		goto out;
746 	}
747 
748 	*pindex = index;
749 	lck_mtx_lock(&host_statistics_lck);
750 	if (g_host_stats_cache[index].last_access > mach_continuous_time() - host_statistics_time_window) {
751 		set_last_access = FALSE;
752 		if (g_host_stats_cache[index].current_requests++ >= g_host_stats_cache[index].max_requests) {
753 			rate_limited = TRUE;
754 			get_cached_info(index, info, count);
755 		}
756 	}
757 	if (set_last_access) {
758 		g_host_stats_cache[index].current_requests = 1;
759 		/*
760 		 * select a random number of requests (included between HOST_STATISTICS_MIN_REQUESTS and HOST_STATISTICS_MAX_REQUESTS)
761 		 * to let query host_statistics.
762 		 * In this way it is not possible to infer looking at when the a cached copy changes if host_statistics was called on
763 		 * the provious window.
764 		 */
765 		g_host_stats_cache[index].max_requests = (mach_absolute_time() % (HOST_STATISTICS_MAX_REQUESTS - HOST_STATISTICS_MIN_REQUESTS + 1)) + HOST_STATISTICS_MIN_REQUESTS;
766 		g_host_stats_cache[index].last_access = mach_continuous_time();
767 	}
768 	lck_mtx_unlock(&host_statistics_lck);
769 out:
770 	return rate_limited;
771 }
772 
773 kern_return_t
vm_stats(void * info,unsigned int * count)774 vm_stats(void *info, unsigned int *count)
775 {
776 	vm_statistics64_data_t host_vm_stat;
777 	mach_msg_type_number_t original_count;
778 	unsigned int local_q_internal_count;
779 	unsigned int local_q_external_count;
780 
781 	if (*count < HOST_VM_INFO64_REV0_COUNT) {
782 		return KERN_FAILURE;
783 	}
784 	get_host_vm_stats(&host_vm_stat);
785 
786 	vm_statistics64_t stat = (vm_statistics64_t)info;
787 
788 	stat->free_count = vm_page_free_count + vm_page_speculative_count;
789 	stat->active_count = vm_page_active_count;
790 
791 	local_q_internal_count = 0;
792 	local_q_external_count = 0;
793 	if (vm_page_local_q) {
794 		zpercpu_foreach(lq, vm_page_local_q) {
795 			stat->active_count += lq->vpl_count;
796 			local_q_internal_count += lq->vpl_internal_count;
797 			local_q_external_count += lq->vpl_external_count;
798 		}
799 	}
800 	stat->inactive_count = vm_page_inactive_count;
801 #if !XNU_TARGET_OS_OSX
802 	stat->wire_count = vm_page_wire_count;
803 #else /* !XNU_TARGET_OS_OSX */
804 	stat->wire_count = vm_page_wire_count + vm_page_throttled_count + vm_lopage_free_count;
805 #endif /* !XNU_TARGET_OS_OSX */
806 	stat->zero_fill_count = host_vm_stat.zero_fill_count;
807 	stat->reactivations = host_vm_stat.reactivations;
808 	stat->pageins = host_vm_stat.pageins;
809 	stat->pageouts = host_vm_stat.pageouts;
810 	stat->faults = host_vm_stat.faults;
811 	stat->cow_faults = host_vm_stat.cow_faults;
812 	stat->lookups = host_vm_stat.lookups;
813 	stat->hits = host_vm_stat.hits;
814 
815 	stat->purgeable_count = vm_page_purgeable_count;
816 	stat->purges = vm_page_purged_count;
817 
818 	stat->speculative_count = vm_page_speculative_count;
819 
820 	/*
821 	 * Fill in extra info added in later revisions of the
822 	 * vm_statistics data structure.  Fill in only what can fit
823 	 * in the data structure the caller gave us !
824 	 */
825 	original_count = *count;
826 	*count = HOST_VM_INFO64_REV0_COUNT; /* rev0 already filled in */
827 	if (original_count >= HOST_VM_INFO64_REV1_COUNT) {
828 		/* rev1 added "throttled count" */
829 		stat->throttled_count = vm_page_throttled_count;
830 		/* rev1 added "compression" info */
831 		stat->compressor_page_count = VM_PAGE_COMPRESSOR_COUNT;
832 		stat->compressions = host_vm_stat.compressions;
833 		stat->decompressions = host_vm_stat.decompressions;
834 		stat->swapins = host_vm_stat.swapins;
835 		stat->swapouts = host_vm_stat.swapouts;
836 		/* rev1 added:
837 		 * "external page count"
838 		 * "anonymous page count"
839 		 * "total # of pages (uncompressed) held in the compressor"
840 		 */
841 		stat->external_page_count = (vm_page_pageable_external_count + local_q_external_count);
842 		stat->internal_page_count = (vm_page_pageable_internal_count + local_q_internal_count);
843 		stat->total_uncompressed_pages_in_compressor = c_segment_pages_compressed;
844 		*count = HOST_VM_INFO64_REV1_COUNT;
845 	}
846 
847 	return KERN_SUCCESS;
848 }
849 
850 kern_return_t host_statistics64(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count);
851 
852 kern_return_t
host_statistics64(host_t host,host_flavor_t flavor,host_info64_t info,mach_msg_type_number_t * count)853 host_statistics64(host_t host, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t * count)
854 {
855 	if (host == HOST_NULL) {
856 		return KERN_INVALID_HOST;
857 	}
858 
859 	switch (flavor) {
860 	case HOST_VM_INFO64: /* We were asked to get vm_statistics64 */
861 		return vm_stats(info, count);
862 
863 	case HOST_EXTMOD_INFO64: /* We were asked to get vm_statistics64 */
864 	{
865 		vm_extmod_statistics_t out_extmod_statistics;
866 
867 		if (*count < HOST_EXTMOD_INFO64_COUNT) {
868 			return KERN_FAILURE;
869 		}
870 
871 		out_extmod_statistics = (vm_extmod_statistics_t)info;
872 		*out_extmod_statistics = host_extmod_statistics;
873 
874 		*count = HOST_EXTMOD_INFO64_COUNT;
875 
876 		return KERN_SUCCESS;
877 	}
878 
879 	default: /* If we didn't recognize the flavor, send to host_statistics */
880 		return host_statistics(host, flavor, (host_info_t)info, count);
881 	}
882 }
883 
884 kern_return_t
host_statistics64_from_user(host_t host,host_flavor_t flavor,host_info64_t info,mach_msg_type_number_t * count)885 host_statistics64_from_user(host_t host, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t * count)
886 {
887 	kern_return_t ret = KERN_SUCCESS;
888 	int index;
889 
890 	if (host == HOST_NULL) {
891 		return KERN_INVALID_HOST;
892 	}
893 
894 	if (rate_limit_host_statistics(TRUE, flavor, info, count, &ret, &index)) {
895 		return ret;
896 	}
897 
898 	if (ret != KERN_SUCCESS) {
899 		return ret;
900 	}
901 
902 	ret = host_statistics64(host, flavor, info, count);
903 
904 	if (ret == KERN_SUCCESS) {
905 		cache_host_statistics(index, info);
906 	}
907 
908 	return ret;
909 }
910 
911 kern_return_t
host_statistics_from_user(host_t host,host_flavor_t flavor,host_info64_t info,mach_msg_type_number_t * count)912 host_statistics_from_user(host_t host, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t * count)
913 {
914 	kern_return_t ret = KERN_SUCCESS;
915 	int index;
916 
917 	if (host == HOST_NULL) {
918 		return KERN_INVALID_HOST;
919 	}
920 
921 	if (rate_limit_host_statistics(FALSE, flavor, info, count, &ret, &index)) {
922 		return ret;
923 	}
924 
925 	if (ret != KERN_SUCCESS) {
926 		return ret;
927 	}
928 
929 	ret = host_statistics(host, flavor, info, count);
930 
931 	if (ret == KERN_SUCCESS) {
932 		cache_host_statistics(index, info);
933 	}
934 
935 	return ret;
936 }
937 
938 /*
939  * Get host statistics that require privilege.
940  * None for now, just call the un-privileged version.
941  */
942 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)943 host_priv_statistics(host_priv_t host_priv, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count)
944 {
945 	return host_statistics((host_t)host_priv, flavor, info, count);
946 }
947 
948 kern_return_t
set_sched_stats_active(boolean_t active)949 set_sched_stats_active(boolean_t active)
950 {
951 	sched_stats_active = active;
952 	return KERN_SUCCESS;
953 }
954 
955 kern_return_t
get_sched_statistics(struct _processor_statistics_np * out,uint32_t * count)956 get_sched_statistics(struct _processor_statistics_np * out, uint32_t * count)
957 {
958 	uint32_t pos = 0;
959 
960 	if (!sched_stats_active) {
961 		return KERN_FAILURE;
962 	}
963 
964 	percpu_foreach_base(pcpu_base) {
965 		struct sched_statistics stats;
966 		processor_t processor;
967 
968 		pos += sizeof(struct _processor_statistics_np);
969 		if (pos > *count) {
970 			return KERN_FAILURE;
971 		}
972 
973 		stats = *PERCPU_GET_WITH_BASE(pcpu_base, sched_stats);
974 		processor = PERCPU_GET_WITH_BASE(pcpu_base, processor);
975 
976 		out->ps_cpuid = processor->cpu_id;
977 		out->ps_csw_count = stats.csw_count;
978 		out->ps_preempt_count = stats.preempt_count;
979 		out->ps_preempted_rt_count = stats.preempted_rt_count;
980 		out->ps_preempted_by_rt_count = stats.preempted_by_rt_count;
981 		out->ps_rt_sched_count = stats.rt_sched_count;
982 		out->ps_interrupt_count = stats.interrupt_count;
983 		out->ps_ipi_count = stats.ipi_count;
984 		out->ps_timer_pop_count = stats.timer_pop_count;
985 		out->ps_runq_count_sum = SCHED(processor_runq_stats_count_sum)(processor);
986 		out->ps_idle_transitions = stats.idle_transitions;
987 		out->ps_quantum_timer_expirations = stats.quantum_timer_expirations;
988 
989 		out++;
990 	}
991 
992 	/* And include RT Queue information */
993 	pos += sizeof(struct _processor_statistics_np);
994 	if (pos > *count) {
995 		return KERN_FAILURE;
996 	}
997 
998 	bzero(out, sizeof(*out));
999 	out->ps_cpuid = (-1);
1000 	out->ps_runq_count_sum = SCHED(rt_runq_count_sum)();
1001 	out++;
1002 
1003 	*count = pos;
1004 
1005 	return KERN_SUCCESS;
1006 }
1007 
1008 kern_return_t
host_page_size(host_t host,vm_size_t * out_page_size)1009 host_page_size(host_t host, vm_size_t * out_page_size)
1010 {
1011 	if (host == HOST_NULL) {
1012 		return KERN_INVALID_ARGUMENT;
1013 	}
1014 
1015 	*out_page_size = PAGE_SIZE;
1016 
1017 	return KERN_SUCCESS;
1018 }
1019 
1020 /*
1021  *	Return kernel version string (more than you ever
1022  *	wanted to know about what version of the kernel this is).
1023  */
1024 extern char version[];
1025 
1026 kern_return_t
host_kernel_version(host_t host,kernel_version_t out_version)1027 host_kernel_version(host_t host, kernel_version_t out_version)
1028 {
1029 	if (host == HOST_NULL) {
1030 		return KERN_INVALID_ARGUMENT;
1031 	}
1032 
1033 	(void)strncpy(out_version, version, sizeof(kernel_version_t));
1034 
1035 	return KERN_SUCCESS;
1036 }
1037 
1038 /*
1039  *	host_processor_sets:
1040  *
1041  *	List all processor sets on the host.
1042  */
1043 kern_return_t
host_processor_sets(host_priv_t host_priv,processor_set_name_array_t * pset_list,mach_msg_type_number_t * count)1044 host_processor_sets(host_priv_t host_priv, processor_set_name_array_t * pset_list, mach_msg_type_number_t * count)
1045 {
1046 	mach_port_t *ports;
1047 
1048 	if (host_priv == HOST_PRIV_NULL) {
1049 		return KERN_INVALID_ARGUMENT;
1050 	}
1051 
1052 	/*
1053 	 *	Allocate memory.  Can be pageable because it won't be
1054 	 *	touched while holding a lock.
1055 	 */
1056 
1057 	ports = kalloc_type(mach_port_t, 1, Z_WAITOK | Z_ZERO | Z_NOFAIL);
1058 
1059 	/* do the conversion that Mig should handle */
1060 	ports[0] = convert_pset_name_to_port(&pset0);
1061 
1062 	*pset_list = (processor_set_array_t)ports;
1063 	*count = 1;
1064 
1065 	return KERN_SUCCESS;
1066 }
1067 
1068 /*
1069  *	host_processor_set_priv:
1070  *
1071  *	Return control port for given processor set.
1072  */
1073 kern_return_t
host_processor_set_priv(host_priv_t host_priv,processor_set_t pset_name,processor_set_t * pset)1074 host_processor_set_priv(host_priv_t host_priv, processor_set_t pset_name, processor_set_t * pset)
1075 {
1076 	if (host_priv == HOST_PRIV_NULL || pset_name == PROCESSOR_SET_NULL) {
1077 		*pset = PROCESSOR_SET_NULL;
1078 
1079 		return KERN_INVALID_ARGUMENT;
1080 	}
1081 
1082 	*pset = pset_name;
1083 
1084 	return KERN_SUCCESS;
1085 }
1086 
1087 /*
1088  *	host_processor_info
1089  *
1090  *	Return info about the processors on this host.  It will return
1091  *	the number of processors, and the specific type of info requested
1092  *	in an OOL array.
1093  */
1094 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)1095 host_processor_info(host_t host,
1096     processor_flavor_t flavor,
1097     natural_t * out_pcount,
1098     processor_info_array_t * out_array,
1099     mach_msg_type_number_t * out_array_count)
1100 {
1101 	kern_return_t result;
1102 	host_t thost;
1103 	processor_info_t info;
1104 	unsigned int icount;
1105 	unsigned int pcount;
1106 	vm_offset_t addr;
1107 	vm_size_t size, needed;
1108 	vm_map_copy_t copy;
1109 
1110 	if (host == HOST_NULL) {
1111 		return KERN_INVALID_ARGUMENT;
1112 	}
1113 
1114 	result = processor_info_count(flavor, &icount);
1115 	if (result != KERN_SUCCESS) {
1116 		return result;
1117 	}
1118 
1119 	pcount = processor_count;
1120 	assert(pcount != 0);
1121 
1122 	needed = pcount * icount * sizeof(natural_t);
1123 	size = vm_map_round_page(needed, VM_MAP_PAGE_MASK(ipc_kernel_map));
1124 	result = kmem_alloc(ipc_kernel_map, &addr, size, KMA_DATA, VM_KERN_MEMORY_IPC);
1125 	if (result != KERN_SUCCESS) {
1126 		return KERN_RESOURCE_SHORTAGE;
1127 	}
1128 
1129 	info = (processor_info_t)addr;
1130 
1131 	for (unsigned int i = 0; i < pcount; i++) {
1132 		processor_t processor = processor_array[i];
1133 		assert(processor != PROCESSOR_NULL);
1134 
1135 		unsigned int tcount = icount;
1136 
1137 		result = processor_info(processor, flavor, &thost, info, &tcount);
1138 		if (result != KERN_SUCCESS) {
1139 			kmem_free(ipc_kernel_map, addr, size);
1140 			return result;
1141 		}
1142 		info += icount;
1143 	}
1144 
1145 	if (size != needed) {
1146 		bzero((char *)addr + needed, size - needed);
1147 	}
1148 
1149 	result = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr, VM_MAP_PAGE_MASK(ipc_kernel_map)),
1150 	    vm_map_round_page(addr + size, VM_MAP_PAGE_MASK(ipc_kernel_map)), FALSE);
1151 	assert(result == KERN_SUCCESS);
1152 	result = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr, (vm_map_size_t)needed, TRUE, &copy);
1153 	assert(result == KERN_SUCCESS);
1154 
1155 	*out_pcount = pcount;
1156 	*out_array = (processor_info_array_t)copy;
1157 	*out_array_count = pcount * icount;
1158 
1159 	return KERN_SUCCESS;
1160 }
1161 
1162 static bool
is_valid_host_special_port(int id)1163 is_valid_host_special_port(int id)
1164 {
1165 	return (id <= HOST_MAX_SPECIAL_PORT) &&
1166 	       (id >= HOST_MIN_SPECIAL_PORT) &&
1167 	       ((id <= HOST_LAST_SPECIAL_KERNEL_PORT) || (id > HOST_MAX_SPECIAL_KERNEL_PORT));
1168 }
1169 
1170 extern void * XNU_PTRAUTH_SIGNED_PTR("initproc") initproc;
1171 
1172 /*
1173  *      Kernel interface for setting a special port.
1174  */
1175 kern_return_t
kernel_set_special_port(host_priv_t host_priv,int id,ipc_port_t port)1176 kernel_set_special_port(host_priv_t host_priv, int id, ipc_port_t port)
1177 {
1178 	ipc_port_t old_port;
1179 
1180 	if (!is_valid_host_special_port(id)) {
1181 		panic("attempted to set invalid special port %d", id);
1182 	}
1183 
1184 #if !MACH_FLIPC
1185 	if (id == HOST_NODE_PORT) {
1186 		return KERN_NOT_SUPPORTED;
1187 	}
1188 #endif
1189 
1190 	host_lock(host_priv);
1191 	old_port = host_priv->special[id];
1192 	host_priv->special[id] = port;
1193 	host_unlock(host_priv);
1194 
1195 #if MACH_FLIPC
1196 	if (id == HOST_NODE_PORT) {
1197 		mach_node_port_changed();
1198 	}
1199 #endif
1200 
1201 	if (IP_VALID(old_port)) {
1202 		ipc_port_release_send(old_port);
1203 	}
1204 
1205 
1206 	return KERN_SUCCESS;
1207 }
1208 
1209 /*
1210  *      Kernel interface for retrieving a special port.
1211  */
1212 kern_return_t
kernel_get_special_port(host_priv_t host_priv,int id,ipc_port_t * portp)1213 kernel_get_special_port(host_priv_t host_priv, int id, ipc_port_t * portp)
1214 {
1215 	if (!is_valid_host_special_port(id)) {
1216 		panic("attempted to get invalid special port %d", id);
1217 	}
1218 
1219 	host_lock(host_priv);
1220 	*portp = host_priv->special[id];
1221 	host_unlock(host_priv);
1222 	return KERN_SUCCESS;
1223 }
1224 
1225 /*
1226  *      User interface for setting a special port.
1227  *
1228  *      Only permits the user to set a user-owned special port
1229  *      ID, rejecting a kernel-owned special port ID.
1230  *
1231  *      A special kernel port cannot be set up using this
1232  *      routine; use kernel_set_special_port() instead.
1233  */
1234 kern_return_t
host_set_special_port_from_user(host_priv_t host_priv,int id,ipc_port_t port)1235 host_set_special_port_from_user(host_priv_t host_priv, int id, ipc_port_t port)
1236 {
1237 	if (host_priv == HOST_PRIV_NULL || id <= HOST_MAX_SPECIAL_KERNEL_PORT || id > HOST_MAX_SPECIAL_PORT) {
1238 		return KERN_INVALID_ARGUMENT;
1239 	}
1240 
1241 	if (task_is_driver(current_task())) {
1242 		return KERN_NO_ACCESS;
1243 	}
1244 
1245 	if (IP_VALID(port) && (port->ip_immovable_receive || port->ip_immovable_send)) {
1246 		return KERN_INVALID_RIGHT;
1247 	}
1248 
1249 	return host_set_special_port(host_priv, id, port);
1250 }
1251 
1252 kern_return_t
host_set_special_port(host_priv_t host_priv,int id,ipc_port_t port)1253 host_set_special_port(host_priv_t host_priv, int id, ipc_port_t port)
1254 {
1255 	if (host_priv == HOST_PRIV_NULL || id <= HOST_MAX_SPECIAL_KERNEL_PORT || id > HOST_MAX_SPECIAL_PORT) {
1256 		return KERN_INVALID_ARGUMENT;
1257 	}
1258 
1259 	if (current_task() != kernel_task && get_bsdtask_info(current_task()) != initproc) {
1260 		bool allowed = (id == HOST_TELEMETRY_PORT &&
1261 		    IOTaskHasEntitlement(current_task(), "com.apple.private.xpc.launchd.event-monitor"));
1262 #if CONFIG_CSR
1263 		if (!allowed) {
1264 			allowed = (csr_check(CSR_ALLOW_TASK_FOR_PID) == 0);
1265 		}
1266 #endif
1267 		if (!allowed) {
1268 			return KERN_NO_ACCESS;
1269 		}
1270 	}
1271 
1272 #if CONFIG_MACF
1273 	if (mac_task_check_set_host_special_port(current_task(), id, port) != 0) {
1274 		return KERN_NO_ACCESS;
1275 	}
1276 #endif
1277 
1278 	return kernel_set_special_port(host_priv, id, port);
1279 }
1280 
1281 /*
1282  *      User interface for retrieving a special port.
1283  *
1284  *      Note that there is nothing to prevent a user special
1285  *      port from disappearing after it has been discovered by
1286  *      the caller; thus, using a special port can always result
1287  *      in a "port not valid" error.
1288  */
1289 
1290 kern_return_t
host_get_special_port_from_user(host_priv_t host_priv,__unused int node,int id,ipc_port_t * portp)1291 host_get_special_port_from_user(host_priv_t host_priv, __unused int node, int id, ipc_port_t * portp)
1292 {
1293 	if (host_priv == HOST_PRIV_NULL || id == HOST_SECURITY_PORT || id > HOST_MAX_SPECIAL_PORT || id < HOST_MIN_SPECIAL_PORT) {
1294 		return KERN_INVALID_ARGUMENT;
1295 	}
1296 
1297 	task_t task = current_task();
1298 	if (task && task_is_driver(task) && id > HOST_MAX_SPECIAL_KERNEL_PORT) {
1299 		/* allow HID drivers to get the sysdiagnose port for keychord handling */
1300 		if (id == HOST_SYSDIAGNOSE_PORT &&
1301 		    IOCurrentTaskHasEntitlement(kIODriverKitHIDFamilyEventServiceEntitlementKey)) {
1302 			goto get_special_port;
1303 		}
1304 		return KERN_NO_ACCESS;
1305 	}
1306 get_special_port:
1307 	return host_get_special_port(host_priv, node, id, portp);
1308 }
1309 
1310 kern_return_t
host_get_special_port(host_priv_t host_priv,__unused int node,int id,ipc_port_t * portp)1311 host_get_special_port(host_priv_t host_priv, __unused int node, int id, ipc_port_t * portp)
1312 {
1313 	ipc_port_t port;
1314 
1315 	if (host_priv == HOST_PRIV_NULL || id == HOST_SECURITY_PORT || id > HOST_MAX_SPECIAL_PORT || id < HOST_MIN_SPECIAL_PORT) {
1316 		return KERN_INVALID_ARGUMENT;
1317 	}
1318 
1319 	host_lock(host_priv);
1320 	port = realhost.special[id];
1321 	switch (id) {
1322 	case HOST_PORT:
1323 		*portp = ipc_kobject_copy_send(port, &realhost, IKOT_HOST);
1324 		break;
1325 	case HOST_PRIV_PORT:
1326 		*portp = ipc_kobject_copy_send(port, &realhost, IKOT_HOST_PRIV);
1327 		break;
1328 	case HOST_IO_MAIN_PORT:
1329 		*portp = ipc_port_copy_send_any(main_device_port);
1330 		break;
1331 	default:
1332 		*portp = ipc_port_copy_send_mqueue(port);
1333 		break;
1334 	}
1335 	host_unlock(host_priv);
1336 
1337 	return KERN_SUCCESS;
1338 }
1339 
1340 /*
1341  *	host_get_io_main
1342  *
1343  *	Return the IO main access port for this host.
1344  */
1345 kern_return_t
host_get_io_main(host_t host,io_main_t * io_mainp)1346 host_get_io_main(host_t host, io_main_t * io_mainp)
1347 {
1348 	if (host == HOST_NULL) {
1349 		return KERN_INVALID_ARGUMENT;
1350 	}
1351 
1352 	return host_get_io_main_port(host_priv_self(), io_mainp);
1353 }
1354 
1355 host_t
host_self(void)1356 host_self(void)
1357 {
1358 	return &realhost;
1359 }
1360 
1361 host_priv_t
host_priv_self(void)1362 host_priv_self(void)
1363 {
1364 	return &realhost;
1365 }
1366 
1367 kern_return_t
host_set_atm_diagnostic_flag(host_t host,uint32_t diagnostic_flag)1368 host_set_atm_diagnostic_flag(host_t host, uint32_t diagnostic_flag)
1369 {
1370 	if (host == HOST_NULL) {
1371 		return KERN_INVALID_ARGUMENT;
1372 	}
1373 
1374 	if (!IOCurrentTaskHasEntitlement("com.apple.private.set-atm-diagnostic-flag")) {
1375 		return KERN_NO_ACCESS;
1376 	}
1377 
1378 #if CONFIG_ATM
1379 	return atm_set_diagnostic_config(diagnostic_flag);
1380 #else
1381 	(void)diagnostic_flag;
1382 	return KERN_NOT_SUPPORTED;
1383 #endif
1384 }
1385 
1386 kern_return_t
host_set_multiuser_config_flags(host_priv_t host_priv,uint32_t multiuser_config)1387 host_set_multiuser_config_flags(host_priv_t host_priv, uint32_t multiuser_config)
1388 {
1389 #if !defined(XNU_TARGET_OS_OSX)
1390 	if (host_priv == HOST_PRIV_NULL) {
1391 		return KERN_INVALID_ARGUMENT;
1392 	}
1393 
1394 	/*
1395 	 * multiuser bit is extensively used for sharedIpad mode.
1396 	 * Caller sets the sharedIPad or other mutiuser modes.
1397 	 * Any override during commpage setting is not suitable anymore.
1398 	 */
1399 	commpage_update_multiuser_config(multiuser_config);
1400 	return KERN_SUCCESS;
1401 #else
1402 	(void)host_priv;
1403 	(void)multiuser_config;
1404 	return KERN_NOT_SUPPORTED;
1405 #endif
1406 }
1407