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