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