]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/host.c
xnu-3247.10.11.tar.gz
[apple/xnu.git] / osfmk / kern / host.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
3e170ce0 5 *
2d21ac55
A
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.
3e170ce0 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
3e170ce0 17 *
2d21ac55
A
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
3e170ce0 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
3e170ce0 31/*
1c79356b
A
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
3e170ce0 35 *
1c79356b
A
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.
3e170ce0 41 *
1c79356b
A
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.
3e170ce0 45 *
1c79356b 46 * Carnegie Mellon requests users of this software to return to
3e170ce0 47 *
1c79356b
A
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
3e170ce0 52 *
1c79356b
A
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
91447636 65#include <mach/mach_types.h>
1c79356b 66#include <mach/boolean.h>
1c79356b 67#include <mach/host_info.h>
55e303ae 68#include <mach/host_special_ports.h>
1c79356b
A
69#include <mach/kern_return.h>
70#include <mach/machine.h>
71#include <mach/port.h>
1c79356b
A
72#include <mach/processor_info.h>
73#include <mach/vm_param.h>
91447636 74#include <mach/processor.h>
1c79356b 75#include <mach/mach_host_server.h>
91447636
A
76#include <mach/host_priv_server.h>
77#include <mach/vm_map.h>
39236c6e 78#include <mach/task_info.h>
91447636
A
79
80#include <kern/kern_types.h>
81#include <kern/assert.h>
82#include <kern/kalloc.h>
83#include <kern/host.h>
84#include <kern/host_statistics.h>
85#include <kern/ipc_host.h>
86#include <kern/misc_protos.h>
87#include <kern/sched.h>
88#include <kern/processor.h>
89
90#include <vm/vm_map.h>
39236c6e
A
91#include <vm/vm_purgeable_internal.h>
92#include <vm/vm_pageout.h>
91447636 93
3e170ce0
A
94#if CONFIG_ATM
95#include <atm/atm_internal.h>
96#endif
97
98#if CONFIG_MACF
99#include <security/mac_mach_internal.h>
100#endif
101
102host_data_t realhost;
1c79356b 103
6d2010ae
A
104vm_extmod_statistics_data_t host_extmod_statistics;
105
1c79356b 106kern_return_t
3e170ce0 107host_processors(host_priv_t host_priv, processor_array_t * out_array, mach_msg_type_number_t * countp)
1c79356b 108{
3e170ce0
A
109 register processor_t processor, *tp;
110 void * addr;
111 unsigned int count, i;
1c79356b
A
112
113 if (host_priv == HOST_PRIV_NULL)
91447636 114 return (KERN_INVALID_ARGUMENT);
1c79356b
A
115
116 assert(host_priv == &realhost);
117
91447636
A
118 count = processor_count;
119 assert(count != 0);
1c79356b 120
3e170ce0 121 addr = kalloc((vm_size_t)(count * sizeof(mach_port_t)));
1c79356b 122 if (addr == 0)
91447636 123 return (KERN_RESOURCE_SHORTAGE);
1c79356b 124
3e170ce0 125 tp = (processor_t *)addr;
91447636
A
126 *tp++ = processor = processor_list;
127
128 if (count > 1) {
129 simple_lock(&processor_list_lock);
130
131 for (i = 1; i < count; i++)
132 *tp++ = processor = processor->processor_list;
133
134 simple_unlock(&processor_list_lock);
135 }
1c79356b
A
136
137 *countp = count;
91447636 138 *out_array = (processor_array_t)addr;
1c79356b
A
139
140 /* do the conversion that Mig should handle */
3e170ce0 141 tp = (processor_t *)addr;
1c79356b 142 for (i = 0; i < count; i++)
3e170ce0 143 ((mach_port_t *)tp)[i] = (mach_port_t)convert_processor_to_port(tp[i]);
1c79356b 144
91447636 145 return (KERN_SUCCESS);
1c79356b
A
146}
147
148kern_return_t
3e170ce0 149host_info(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count)
1c79356b 150{
1c79356b 151 if (host == HOST_NULL)
91447636 152 return (KERN_INVALID_ARGUMENT);
1c79356b 153
3e170ce0
A
154 switch (flavor) {
155 case HOST_BASIC_INFO: {
156 register host_basic_info_t basic_info;
157 register int master_id;
1c79356b
A
158
159 /*
160 * Basic information about this host.
161 */
91447636
A
162 if (*count < HOST_BASIC_INFO_OLD_COUNT)
163 return (KERN_FAILURE);
1c79356b 164
3e170ce0 165 basic_info = (host_basic_info_t)info;
1c79356b 166
1c79356b 167 basic_info->memory_size = machine_info.memory_size;
2d21ac55
A
168 basic_info->max_cpus = machine_info.max_cpus;
169 basic_info->avail_cpus = processor_avail_count;
b0d623f7
A
170 master_id = master_processor->cpu_id;
171 basic_info->cpu_type = slot_type(master_id);
172 basic_info->cpu_subtype = slot_subtype(master_id);
91447636
A
173
174 if (*count >= HOST_BASIC_INFO_COUNT) {
b0d623f7 175 basic_info->cpu_threadtype = slot_threadtype(master_id);
91447636
A
176 basic_info->physical_cpu = machine_info.physical_cpu;
177 basic_info->physical_cpu_max = machine_info.physical_cpu_max;
178 basic_info->logical_cpu = machine_info.logical_cpu;
179 basic_info->logical_cpu_max = machine_info.logical_cpu_max;
180 basic_info->max_mem = machine_info.max_mem;
181
182 *count = HOST_BASIC_INFO_COUNT;
183 } else {
184 *count = HOST_BASIC_INFO_OLD_COUNT;
185 }
1c79356b 186
91447636 187 return (KERN_SUCCESS);
1c79356b
A
188 }
189
3e170ce0
A
190 case HOST_SCHED_INFO: {
191 register host_sched_info_t sched_info;
6d2010ae
A
192 uint32_t quantum_time;
193 uint64_t quantum_ns;
1c79356b
A
194
195 /*
196 * Return scheduler information.
197 */
198 if (*count < HOST_SCHED_INFO_COUNT)
91447636 199 return (KERN_FAILURE);
1c79356b 200
3e170ce0 201 sched_info = (host_sched_info_t)info;
1c79356b 202
6d2010ae
A
203 quantum_time = SCHED(initial_quantum_size)(THREAD_NULL);
204 absolutetime_to_nanoseconds(quantum_time, &quantum_ns);
205
3e170ce0 206 sched_info->min_timeout = sched_info->min_quantum = (uint32_t)(quantum_ns / 1000 / 1000);
1c79356b
A
207
208 *count = HOST_SCHED_INFO_COUNT;
209
91447636 210 return (KERN_SUCCESS);
1c79356b
A
211 }
212
3e170ce0 213 case HOST_RESOURCE_SIZES: {
1c79356b
A
214 /*
215 * Return sizes of kernel data structures
216 */
217 if (*count < HOST_RESOURCE_SIZES_COUNT)
91447636 218 return (KERN_FAILURE);
1c79356b
A
219
220 /* XXX Fail until ledgers are implemented */
91447636 221 return (KERN_INVALID_ARGUMENT);
1c79356b 222 }
3e170ce0
A
223
224 case HOST_PRIORITY_INFO: {
225 register host_priority_info_t priority_info;
1c79356b
A
226
227 if (*count < HOST_PRIORITY_INFO_COUNT)
91447636 228 return (KERN_FAILURE);
1c79356b 229
3e170ce0 230 priority_info = (host_priority_info_t)info;
1c79356b 231
3e170ce0
A
232 priority_info->kernel_priority = MINPRI_KERNEL;
233 priority_info->system_priority = MINPRI_KERNEL;
234 priority_info->server_priority = MINPRI_RESERVED;
235 priority_info->user_priority = BASEPRI_DEFAULT;
236 priority_info->depress_priority = DEPRESSPRI;
237 priority_info->idle_priority = IDLEPRI;
238 priority_info->minimum_priority = MINPRI_USER;
239 priority_info->maximum_priority = MAXPRI_RESERVED;
1c79356b
A
240
241 *count = HOST_PRIORITY_INFO_COUNT;
242
91447636 243 return (KERN_SUCCESS);
1c79356b
A
244 }
245
246 /*
9bccf70c 247 * Gestalt for various trap facilities.
1c79356b 248 */
9bccf70c 249 case HOST_MACH_MSG_TRAP:
3e170ce0 250 case HOST_SEMAPHORE_TRAPS: {
1c79356b 251 *count = 0;
91447636 252 return (KERN_SUCCESS);
1c79356b
A
253 }
254
3e170ce0 255 case HOST_VM_PURGABLE: {
39236c6e
A
256 if (*count < HOST_VM_PURGABLE_COUNT)
257 return (KERN_FAILURE);
258
3e170ce0 259 vm_purgeable_stats((vm_purgeable_info_t)info, NULL);
39236c6e
A
260
261 *count = HOST_VM_PURGABLE_COUNT;
262 return (KERN_SUCCESS);
263 }
264
3e170ce0
A
265 case HOST_DEBUG_INFO_INTERNAL: {
266#if DEVELOPMENT || DEBUG
267 if (*count < HOST_DEBUG_INFO_INTERNAL_COUNT)
268 return (KERN_FAILURE);
269
270 host_debug_info_internal_t debug_info = (host_debug_info_internal_t)info;
271 bzero(debug_info, sizeof(host_debug_info_internal_data_t));
272 *count = HOST_DEBUG_INFO_INTERNAL_COUNT;
273
274#if CONFIG_COALITIONS
275 debug_info->config_coalitions = 1;
276#endif
277#if CONFIG_BANK
278 debug_info->config_bank = 1;
279#endif
280#if CONFIG_ATM
281 debug_info->config_atm = 1;
282#endif
283#if CONFIG_CSR
284 debug_info->config_csr = 1;
285#endif
286 return (KERN_SUCCESS);
287#else /* DEVELOPMENT || DEBUG */
288 return (KERN_NOT_SUPPORTED);
289#endif
290 }
291
292 default: return (KERN_INVALID_ARGUMENT);
1c79356b
A
293 }
294}
295
296kern_return_t
3e170ce0 297host_statistics(host_t host, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count)
1c79356b 298{
3e170ce0 299 uint32_t i;
1c79356b
A
300
301 if (host == HOST_NULL)
91447636 302 return (KERN_INVALID_HOST);
1c79356b 303
3e170ce0
A
304 switch (flavor) {
305 case HOST_LOAD_INFO: {
306 host_load_info_t load_info;
1c79356b
A
307
308 if (*count < HOST_LOAD_INFO_COUNT)
91447636 309 return (KERN_FAILURE);
1c79356b 310
3e170ce0 311 load_info = (host_load_info_t)info;
1c79356b 312
3e170ce0
A
313 bcopy((char *)avenrun, (char *)load_info->avenrun, sizeof avenrun);
314 bcopy((char *)mach_factor, (char *)load_info->mach_factor, sizeof mach_factor);
1c79356b
A
315
316 *count = HOST_LOAD_INFO_COUNT;
91447636
A
317 return (KERN_SUCCESS);
318 }
319
3e170ce0
A
320 case HOST_VM_INFO: {
321 register processor_t processor;
322 register vm_statistics64_t stat;
323 vm_statistics64_data_t host_vm_stat;
324 vm_statistics_t stat32;
325 mach_msg_type_number_t original_count;
326
91447636
A
327 if (*count < HOST_VM_INFO_REV0_COUNT)
328 return (KERN_FAILURE);
1c79356b 329
91447636
A
330 processor = processor_list;
331 stat = &PROCESSOR_DATA(processor, vm_stat);
1c79356b 332 host_vm_stat = *stat;
91447636
A
333
334 if (processor_count > 1) {
335 simple_lock(&processor_list_lock);
336
337 while ((processor = processor->processor_list) != NULL) {
338 stat = &PROCESSOR_DATA(processor, vm_stat);
339
3e170ce0 340 host_vm_stat.zero_fill_count += stat->zero_fill_count;
91447636 341 host_vm_stat.reactivations += stat->reactivations;
1c79356b
A
342 host_vm_stat.pageins += stat->pageins;
343 host_vm_stat.pageouts += stat->pageouts;
344 host_vm_stat.faults += stat->faults;
345 host_vm_stat.cow_faults += stat->cow_faults;
346 host_vm_stat.lookups += stat->lookups;
347 host_vm_stat.hits += stat->hits;
348 }
91447636
A
349
350 simple_unlock(&processor_list_lock);
1c79356b 351 }
1c79356b 352
3e170ce0 353 stat32 = (vm_statistics_t)info;
b0d623f7
A
354
355 stat32->free_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_free_count + vm_page_speculative_count);
356 stat32->active_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_active_count);
3e170ce0 357
b0d623f7
A
358 if (vm_page_local_q) {
359 for (i = 0; i < vm_page_local_q_count; i++) {
3e170ce0 360 struct vpl * lq;
b0d623f7
A
361
362 lq = &vm_page_local_q[i].vpl_un.vpl;
363
364 stat32->active_count += VM_STATISTICS_TRUNCATE_TO_32_BIT(lq->vpl_count);
365 }
366 }
367 stat32->inactive_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_inactive_count);
0b4c1975 368 stat32->wire_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_wire_count + vm_page_throttled_count + vm_lopage_free_count);
b0d623f7
A
369 stat32->zero_fill_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.zero_fill_count);
370 stat32->reactivations = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.reactivations);
371 stat32->pageins = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.pageins);
372 stat32->pageouts = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.pageouts);
373 stat32->faults = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.faults);
374 stat32->cow_faults = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.cow_faults);
375 stat32->lookups = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.lookups);
376 stat32->hits = VM_STATISTICS_TRUNCATE_TO_32_BIT(host_vm_stat.hits);
91447636 377
2d21ac55
A
378 /*
379 * Fill in extra info added in later revisions of the
380 * vm_statistics data structure. Fill in only what can fit
381 * in the data structure the caller gave us !
382 */
383 original_count = *count;
384 *count = HOST_VM_INFO_REV0_COUNT; /* rev0 already filled in */
385 if (original_count >= HOST_VM_INFO_REV1_COUNT) {
386 /* rev1 added "purgeable" info */
b0d623f7
A
387 stat32->purgeable_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_purgeable_count);
388 stat32->purges = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_purged_count);
2d21ac55
A
389 *count = HOST_VM_INFO_REV1_COUNT;
390 }
b0d623f7 391
2d21ac55
A
392 if (original_count >= HOST_VM_INFO_REV2_COUNT) {
393 /* rev2 added "speculative" info */
b0d623f7 394 stat32->speculative_count = VM_STATISTICS_TRUNCATE_TO_32_BIT(vm_page_speculative_count);
2d21ac55 395 *count = HOST_VM_INFO_REV2_COUNT;
91447636
A
396 }
397
b0d623f7
A
398 /* rev3 changed some of the fields to be 64-bit*/
399
91447636
A
400 return (KERN_SUCCESS);
401 }
3e170ce0
A
402
403 case HOST_CPU_LOAD_INFO: {
404 register processor_t processor;
405 host_cpu_load_info_t cpu_load_info;
1c79356b
A
406
407 if (*count < HOST_CPU_LOAD_INFO_COUNT)
91447636
A
408 return (KERN_FAILURE);
409
3e170ce0
A
410#define GET_TICKS_VALUE(state, ticks) \
411 MACRO_BEGIN cpu_load_info->cpu_ticks[(state)] += (uint32_t)(ticks / hz_tick_interval); \
412 MACRO_END
413#define GET_TICKS_VALUE_FROM_TIMER(processor, state, timer) \
414 MACRO_BEGIN GET_TICKS_VALUE(state, timer_grab(&PROCESSOR_DATA(processor, timer))); \
415 MACRO_END
1c79356b 416
91447636 417 cpu_load_info = (host_cpu_load_info_t)info;
1c79356b 418 cpu_load_info->cpu_ticks[CPU_STATE_USER] = 0;
1c79356b
A
419 cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] = 0;
420 cpu_load_info->cpu_ticks[CPU_STATE_IDLE] = 0;
2d21ac55 421 cpu_load_info->cpu_ticks[CPU_STATE_NICE] = 0;
91447636 422
6d2010ae 423 simple_lock(&processor_list_lock);
91447636 424
6d2010ae 425 for (processor = processor_list; processor != NULL; processor = processor->processor_list) {
3e170ce0
A
426 timer_t idle_state;
427 uint64_t idle_time_snapshot1, idle_time_snapshot2;
428 uint64_t idle_time_tstamp1, idle_time_tstamp2;
429
39236c6e 430 /* See discussion in processor_info(PROCESSOR_CPU_LOAD_INFO) */
91447636 431
39236c6e 432 GET_TICKS_VALUE_FROM_TIMER(processor, CPU_STATE_USER, user_state);
316670eb 433 if (precise_user_kernel_time) {
39236c6e 434 GET_TICKS_VALUE_FROM_TIMER(processor, CPU_STATE_SYSTEM, system_state);
316670eb
A
435 } else {
436 /* system_state may represent either sys or user */
39236c6e 437 GET_TICKS_VALUE_FROM_TIMER(processor, CPU_STATE_USER, system_state);
316670eb 438 }
6d2010ae
A
439
440 idle_state = &PROCESSOR_DATA(processor, idle_state);
39236c6e
A
441 idle_time_snapshot1 = timer_grab(idle_state);
442 idle_time_tstamp1 = idle_state->tstamp;
3e170ce0 443
39236c6e
A
444 if (PROCESSOR_DATA(processor, current_state) != idle_state) {
445 /* Processor is non-idle, so idle timer should be accurate */
446 GET_TICKS_VALUE_FROM_TIMER(processor, CPU_STATE_IDLE, idle_state);
447 } else if ((idle_time_snapshot1 != (idle_time_snapshot2 = timer_grab(idle_state))) ||
3e170ce0 448 (idle_time_tstamp1 != (idle_time_tstamp2 = idle_state->tstamp))) {
39236c6e
A
449 /* Idle timer is being updated concurrently, second stamp is good enough */
450 GET_TICKS_VALUE(CPU_STATE_IDLE, idle_time_snapshot2);
451 } else {
452 /*
453 * Idle timer may be very stale. Fortunately we have established
454 * that idle_time_snapshot1 and idle_time_tstamp1 are unchanging
455 */
456 idle_time_snapshot1 += mach_absolute_time() - idle_time_tstamp1;
3e170ce0 457
39236c6e 458 GET_TICKS_VALUE(CPU_STATE_IDLE, idle_time_snapshot1);
6d2010ae 459 }
1c79356b 460 }
6d2010ae 461 simple_unlock(&processor_list_lock);
316670eb 462
1c79356b 463 *count = HOST_CPU_LOAD_INFO_COUNT;
91447636
A
464
465 return (KERN_SUCCESS);
466 }
1c79356b 467
3e170ce0 468 case HOST_EXPIRED_TASK_INFO: {
4b17d6b6
A
469 if (*count < TASK_POWER_INFO_COUNT) {
470 return (KERN_FAILURE);
471 }
472
473 task_power_info_t tinfo = (task_power_info_t)info;
474
475 tinfo->task_interrupt_wakeups = dead_task_statistics.task_interrupt_wakeups;
476 tinfo->task_platform_idle_wakeups = dead_task_statistics.task_platform_idle_wakeups;
477
478 tinfo->task_timer_wakeups_bin_1 = dead_task_statistics.task_timer_wakeups_bin_1;
39236c6e 479
4b17d6b6
A
480 tinfo->task_timer_wakeups_bin_2 = dead_task_statistics.task_timer_wakeups_bin_2;
481
482 tinfo->total_user = dead_task_statistics.total_user_time;
483 tinfo->total_system = dead_task_statistics.total_system_time;
484
485 return (KERN_SUCCESS);
39236c6e 486 }
3e170ce0 487 default: return (KERN_INVALID_ARGUMENT);
1c79356b
A
488 }
489}
490
3e170ce0 491extern uint32_t c_segment_pages_compressed;
b0d623f7
A
492
493kern_return_t
3e170ce0 494host_statistics64(host_t host, host_flavor_t flavor, host_info64_t info, mach_msg_type_number_t * count)
b0d623f7 495{
3e170ce0
A
496 uint32_t i;
497
b0d623f7
A
498 if (host == HOST_NULL)
499 return (KERN_INVALID_HOST);
b0d623f7 500
3e170ce0
A
501 switch (flavor) {
502 case HOST_VM_INFO64: /* We were asked to get vm_statistics64 */
503 {
504 register processor_t processor;
505 register vm_statistics64_t stat;
506 vm_statistics64_data_t host_vm_stat;
507 mach_msg_type_number_t original_count;
508 unsigned int local_q_internal_count;
509 unsigned int local_q_external_count;
510
511 if (*count < HOST_VM_INFO64_REV0_COUNT)
512 return (KERN_FAILURE);
513
514 processor = processor_list;
515 stat = &PROCESSOR_DATA(processor, vm_stat);
516 host_vm_stat = *stat;
517
518 if (processor_count > 1) {
519 simple_lock(&processor_list_lock);
520
521 while ((processor = processor->processor_list) != NULL) {
522 stat = &PROCESSOR_DATA(processor, vm_stat);
523
524 host_vm_stat.zero_fill_count += stat->zero_fill_count;
525 host_vm_stat.reactivations += stat->reactivations;
526 host_vm_stat.pageins += stat->pageins;
527 host_vm_stat.pageouts += stat->pageouts;
528 host_vm_stat.faults += stat->faults;
529 host_vm_stat.cow_faults += stat->cow_faults;
530 host_vm_stat.lookups += stat->lookups;
531 host_vm_stat.hits += stat->hits;
532 host_vm_stat.compressions += stat->compressions;
533 host_vm_stat.decompressions += stat->decompressions;
534 host_vm_stat.swapins += stat->swapins;
535 host_vm_stat.swapouts += stat->swapouts;
39236c6e 536 }
b0d623f7 537
3e170ce0 538 simple_unlock(&processor_list_lock);
b0d623f7
A
539 }
540
3e170ce0
A
541 stat = (vm_statistics64_t)info;
542
543 stat->free_count = vm_page_free_count + vm_page_speculative_count;
544 stat->active_count = vm_page_active_count;
6d2010ae 545
3e170ce0
A
546 local_q_internal_count = 0;
547 local_q_external_count = 0;
548 if (vm_page_local_q) {
549 for (i = 0; i < vm_page_local_q_count; i++) {
550 struct vpl * lq;
6d2010ae 551
3e170ce0 552 lq = &vm_page_local_q[i].vpl_un.vpl;
6d2010ae 553
3e170ce0
A
554 stat->active_count += lq->vpl_count;
555 local_q_internal_count += lq->vpl_internal_count;
556 local_q_external_count += lq->vpl_external_count;
557 }
558 }
559 stat->inactive_count = vm_page_inactive_count;
560 stat->wire_count = vm_page_wire_count + vm_page_throttled_count + vm_lopage_free_count;
561 stat->zero_fill_count = host_vm_stat.zero_fill_count;
562 stat->reactivations = host_vm_stat.reactivations;
563 stat->pageins = host_vm_stat.pageins;
564 stat->pageouts = host_vm_stat.pageouts;
565 stat->faults = host_vm_stat.faults;
566 stat->cow_faults = host_vm_stat.cow_faults;
567 stat->lookups = host_vm_stat.lookups;
568 stat->hits = host_vm_stat.hits;
569
570 stat->purgeable_count = vm_page_purgeable_count;
571 stat->purges = vm_page_purged_count;
572
573 stat->speculative_count = vm_page_speculative_count;
6d2010ae 574
3e170ce0
A
575 /*
576 * Fill in extra info added in later revisions of the
577 * vm_statistics data structure. Fill in only what can fit
578 * in the data structure the caller gave us !
579 */
580 original_count = *count;
581 *count = HOST_VM_INFO64_REV0_COUNT; /* rev0 already filled in */
582 if (original_count >= HOST_VM_INFO64_REV1_COUNT) {
583 /* rev1 added "throttled count" */
584 stat->throttled_count = vm_page_throttled_count;
585 /* rev1 added "compression" info */
586 stat->compressor_page_count = VM_PAGE_COMPRESSOR_COUNT;
587 stat->compressions = host_vm_stat.compressions;
588 stat->decompressions = host_vm_stat.decompressions;
589 stat->swapins = host_vm_stat.swapins;
590 stat->swapouts = host_vm_stat.swapouts;
591 /* rev1 added:
592 * "external page count"
593 * "anonymous page count"
594 * "total # of pages (uncompressed) held in the compressor"
595 */
596 stat->external_page_count = (vm_page_pageable_external_count + local_q_external_count);
597 stat->internal_page_count = (vm_page_pageable_internal_count + local_q_internal_count);
598 stat->total_uncompressed_pages_in_compressor = c_segment_pages_compressed;
599 *count = HOST_VM_INFO64_REV1_COUNT;
6d2010ae
A
600 }
601
3e170ce0 602 return (KERN_SUCCESS);
b0d623f7 603 }
b0d623f7 604
3e170ce0
A
605 case HOST_EXTMOD_INFO64: /* We were asked to get vm_statistics64 */
606 {
607 vm_extmod_statistics_t out_extmod_statistics;
608
609 if (*count < HOST_EXTMOD_INFO64_COUNT)
610 return (KERN_FAILURE);
611
612 out_extmod_statistics = (vm_extmod_statistics_t)info;
613 *out_extmod_statistics = host_extmod_statistics;
614
615 *count = HOST_EXTMOD_INFO64_COUNT;
616
617 return (KERN_SUCCESS);
618 }
619
620 default: /* If we didn't recognize the flavor, send to host_statistics */
621 return (host_statistics(host, flavor, (host_info_t)info, count));
622 }
623}
b0d623f7 624
1c79356b
A
625/*
626 * Get host statistics that require privilege.
627 * None for now, just call the un-privileged version.
628 */
629kern_return_t
3e170ce0 630host_priv_statistics(host_priv_t host_priv, host_flavor_t flavor, host_info_t info, mach_msg_type_number_t * count)
1c79356b 631{
3e170ce0 632 return (host_statistics((host_t)host_priv, flavor, info, count));
1c79356b
A
633}
634
6d2010ae 635kern_return_t
3e170ce0 636set_sched_stats_active(boolean_t active)
6d2010ae
A
637{
638 sched_stats_active = active;
3e170ce0 639 return (KERN_SUCCESS);
6d2010ae
A
640}
641
6d2010ae 642kern_return_t
3e170ce0 643get_sched_statistics(struct _processor_statistics_np * out, uint32_t * count)
6d2010ae
A
644{
645 processor_t processor;
646
647 if (!sched_stats_active) {
3e170ce0 648 return (KERN_FAILURE);
6d2010ae
A
649 }
650
651 simple_lock(&processor_list_lock);
3e170ce0
A
652
653 if (*count < (processor_count + 1) * sizeof(struct _processor_statistics_np)) { /* One for RT */
6d2010ae 654 simple_unlock(&processor_list_lock);
3e170ce0 655 return (KERN_FAILURE);
6d2010ae
A
656 }
657
658 processor = processor_list;
659 while (processor) {
3e170ce0
A
660 struct processor_sched_statistics * stats = &processor->processor_data.sched_stats;
661
662 out->ps_cpuid = processor->cpu_id;
663 out->ps_csw_count = stats->csw_count;
664 out->ps_preempt_count = stats->preempt_count;
665 out->ps_preempted_rt_count = stats->preempted_rt_count;
666 out->ps_preempted_by_rt_count = stats->preempted_by_rt_count;
667 out->ps_rt_sched_count = stats->rt_sched_count;
668 out->ps_interrupt_count = stats->interrupt_count;
669 out->ps_ipi_count = stats->ipi_count;
670 out->ps_timer_pop_count = stats->timer_pop_count;
671 out->ps_runq_count_sum = SCHED(processor_runq_stats_count_sum)(processor);
672 out->ps_idle_transitions = stats->idle_transitions;
673 out->ps_quantum_timer_expirations = stats->quantum_timer_expirations;
6d2010ae
A
674
675 out++;
676 processor = processor->processor_list;
677 }
678
3e170ce0 679 *count = (uint32_t)(processor_count * sizeof(struct _processor_statistics_np));
6d2010ae
A
680
681 simple_unlock(&processor_list_lock);
682
683 /* And include RT Queue information */
684 bzero(out, sizeof(*out));
685 out->ps_cpuid = (-1);
686 out->ps_runq_count_sum = rt_runq.runq_stats.count_sum;
687 out++;
688 *count += (uint32_t)sizeof(struct _processor_statistics_np);
689
3e170ce0 690 return (KERN_SUCCESS);
6d2010ae
A
691}
692
1c79356b 693kern_return_t
3e170ce0 694host_page_size(host_t host, vm_size_t * out_page_size)
1c79356b
A
695{
696 if (host == HOST_NULL)
3e170ce0 697 return (KERN_INVALID_ARGUMENT);
1c79356b 698
3e170ce0 699 *out_page_size = PAGE_SIZE;
1c79356b 700
3e170ce0 701 return (KERN_SUCCESS);
1c79356b
A
702}
703
704/*
705 * Return kernel version string (more than you ever
706 * wanted to know about what version of the kernel this is).
707 */
3e170ce0 708extern char version[];
1c79356b
A
709
710kern_return_t
3e170ce0 711host_kernel_version(host_t host, kernel_version_t out_version)
1c79356b 712{
1c79356b 713 if (host == HOST_NULL)
3e170ce0 714 return (KERN_INVALID_ARGUMENT);
1c79356b 715
3e170ce0 716 (void)strncpy(out_version, version, sizeof(kernel_version_t));
1c79356b 717
3e170ce0 718 return (KERN_SUCCESS);
1c79356b
A
719}
720
721/*
722 * host_processor_sets:
723 *
724 * List all processor sets on the host.
725 */
726kern_return_t
3e170ce0 727host_processor_sets(host_priv_t host_priv, processor_set_name_array_t * pset_list, mach_msg_type_number_t * count)
1c79356b 728{
3e170ce0 729 void * addr;
1c79356b
A
730
731 if (host_priv == HOST_PRIV_NULL)
2d21ac55 732 return (KERN_INVALID_ARGUMENT);
1c79356b
A
733
734 /*
735 * Allocate memory. Can be pageable because it won't be
736 * touched while holding a lock.
737 */
738
3e170ce0 739 addr = kalloc((vm_size_t)sizeof(mach_port_t));
1c79356b 740 if (addr == 0)
2d21ac55 741 return (KERN_RESOURCE_SHORTAGE);
1c79356b 742
1c79356b 743 /* do the conversion that Mig should handle */
3e170ce0 744 *((ipc_port_t *)addr) = convert_pset_name_to_port(&pset0);
1c79356b
A
745
746 *pset_list = (processor_set_array_t)addr;
747 *count = 1;
748
2d21ac55 749 return (KERN_SUCCESS);
1c79356b
A
750}
751
752/*
753 * host_processor_set_priv:
754 *
755 * Return control port for given processor set.
756 */
757kern_return_t
3e170ce0 758host_processor_set_priv(host_priv_t host_priv, processor_set_t pset_name, processor_set_t * pset)
1c79356b 759{
3e170ce0 760 if (host_priv == HOST_PRIV_NULL || pset_name == PROCESSOR_SET_NULL) {
2d21ac55
A
761 *pset = PROCESSOR_SET_NULL;
762
763 return (KERN_INVALID_ARGUMENT);
3e170ce0 764 }
1c79356b 765
3e170ce0 766 *pset = pset_name;
2d21ac55 767
3e170ce0 768 return (KERN_SUCCESS);
1c79356b
A
769}
770
771/*
772 * host_processor_info
773 *
774 * Return info about the processors on this host. It will return
775 * the number of processors, and the specific type of info requested
776 * in an OOL array.
777 */
778kern_return_t
3e170ce0
A
779host_processor_info(host_t host,
780 processor_flavor_t flavor,
781 natural_t * out_pcount,
782 processor_info_array_t * out_array,
783 mach_msg_type_number_t * out_array_count)
1c79356b 784{
3e170ce0
A
785 kern_return_t result;
786 processor_t processor;
787 host_t thost;
788 processor_info_t info;
789 unsigned int icount, tcount;
790 unsigned int pcount, i;
791 vm_offset_t addr;
792 vm_size_t size, needed;
793 vm_map_copy_t copy;
1c79356b
A
794
795 if (host == HOST_NULL)
91447636 796 return (KERN_INVALID_ARGUMENT);
1c79356b 797
91447636
A
798 result = processor_info_count(flavor, &icount);
799 if (result != KERN_SUCCESS)
800 return (result);
1c79356b 801
91447636
A
802 pcount = processor_count;
803 assert(pcount != 0);
1c79356b 804
6601e61a 805 needed = pcount * icount * sizeof(natural_t);
3e170ce0
A
806 size = vm_map_round_page(needed, VM_MAP_PAGE_MASK(ipc_kernel_map));
807 result = kmem_alloc(ipc_kernel_map, &addr, size, VM_KERN_MEMORY_IPC);
91447636
A
808 if (result != KERN_SUCCESS)
809 return (KERN_RESOURCE_SHORTAGE);
810
3e170ce0 811 info = (processor_info_t)addr;
91447636
A
812 processor = processor_list;
813 tcount = icount;
1c79356b 814
91447636
A
815 result = processor_info(processor, flavor, &thost, info, &tcount);
816 if (result != KERN_SUCCESS) {
1c79356b 817 kmem_free(ipc_kernel_map, addr, size);
91447636 818 return (result);
1c79356b
A
819 }
820
91447636
A
821 if (pcount > 1) {
822 for (i = 1; i < pcount; i++) {
823 simple_lock(&processor_list_lock);
824 processor = processor->processor_list;
825 simple_unlock(&processor_list_lock);
826
827 info += icount;
828 tcount = icount;
829 result = processor_info(processor, flavor, &thost, info, &tcount);
830 if (result != KERN_SUCCESS) {
1c79356b 831 kmem_free(ipc_kernel_map, addr, size);
91447636 832 return (result);
1c79356b 833 }
1c79356b
A
834 }
835 }
836
3e170ce0
A
837 if (size != needed)
838 bzero((char *)addr + needed, size - needed);
6601e61a 839
3e170ce0
A
840 result = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr, VM_MAP_PAGE_MASK(ipc_kernel_map)),
841 vm_map_round_page(addr + size, VM_MAP_PAGE_MASK(ipc_kernel_map)), FALSE);
91447636 842 assert(result == KERN_SUCCESS);
2dced7af 843 result = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr, (vm_map_size_t)needed, TRUE, &copy);
91447636 844 assert(result == KERN_SUCCESS);
1c79356b 845
91447636 846 *out_pcount = pcount;
3e170ce0 847 *out_array = (processor_info_array_t)copy;
91447636
A
848 *out_array_count = pcount * icount;
849
850 return (KERN_SUCCESS);
1c79356b
A
851}
852
1c79356b 853/*
55e303ae 854 * Kernel interface for setting a special port.
1c79356b
A
855 */
856kern_return_t
3e170ce0 857kernel_set_special_port(host_priv_t host_priv, int id, ipc_port_t port)
1c79356b 858{
55e303ae
A
859 ipc_port_t old_port;
860
861 host_lock(host_priv);
862 old_port = host_priv->special[id];
863 host_priv->special[id] = port;
864 host_unlock(host_priv);
865 if (IP_VALID(old_port))
866 ipc_port_release_send(old_port);
3e170ce0 867 return (KERN_SUCCESS);
1c79356b
A
868}
869
870/*
871 * User interface for setting a special port.
872 *
873 * Only permits the user to set a user-owned special port
874 * ID, rejecting a kernel-owned special port ID.
875 *
876 * A special kernel port cannot be set up using this
877 * routine; use kernel_set_special_port() instead.
878 */
879kern_return_t
3e170ce0 880host_set_special_port(host_priv_t host_priv, int id, ipc_port_t port)
1c79356b 881{
3e170ce0
A
882 if (host_priv == HOST_PRIV_NULL || id <= HOST_MAX_SPECIAL_KERNEL_PORT || id > HOST_MAX_SPECIAL_PORT)
883 return (KERN_INVALID_ARGUMENT);
55e303ae 884
3e170ce0
A
885#if CONFIG_MACF
886 if (mac_task_check_set_host_special_port(current_task(), id, port) != 0)
887 return (KERN_NO_ACCESS);
888#endif
1c79356b 889
3e170ce0
A
890 return (kernel_set_special_port(host_priv, id, port));
891}
1c79356b
A
892
893/*
894 * User interface for retrieving a special port.
895 *
1c79356b
A
896 * Note that there is nothing to prevent a user special
897 * port from disappearing after it has been discovered by
898 * the caller; thus, using a special port can always result
899 * in a "port not valid" error.
900 */
901
902kern_return_t
3e170ce0 903host_get_special_port(host_priv_t host_priv, __unused int node, int id, ipc_port_t * portp)
1c79356b 904{
3e170ce0 905 ipc_port_t port;
55e303ae 906
3e170ce0
A
907 if (host_priv == HOST_PRIV_NULL || id == HOST_SECURITY_PORT || id > HOST_MAX_SPECIAL_PORT || id < 0)
908 return (KERN_INVALID_ARGUMENT);
55e303ae 909
55e303ae
A
910 host_lock(host_priv);
911 port = realhost.special[id];
912 *portp = ipc_port_copy_send(port);
913 host_unlock(host_priv);
914
3e170ce0 915 return (KERN_SUCCESS);
55e303ae
A
916}
917
55e303ae 918/*
3e170ce0 919 * host_get_io_master
55e303ae
A
920 *
921 * Return the IO master access port for this host.
922 */
923kern_return_t
3e170ce0 924host_get_io_master(host_t host, io_master_t * io_masterp)
55e303ae
A
925{
926 if (host == HOST_NULL)
3e170ce0 927 return (KERN_INVALID_ARGUMENT);
55e303ae
A
928
929 return (host_get_io_master_port(host_priv_self(), io_masterp));
1c79356b
A
930}
931
932host_t
933host_self(void)
934{
3e170ce0 935 return (&realhost);
1c79356b
A
936}
937
938host_priv_t
939host_priv_self(void)
940{
3e170ce0 941 return (&realhost);
1c79356b
A
942}
943
944host_security_t
945host_security_self(void)
946{
3e170ce0
A
947 return (&realhost);
948}
949
950kern_return_t
951host_set_atm_diagnostic_flag(host_priv_t host_priv, uint32_t diagnostic_flag)
952{
953 if (host_priv == HOST_PRIV_NULL)
954 return (KERN_INVALID_ARGUMENT);
955
956 assert(host_priv == &realhost);
957
958#if CONFIG_ATM
959 return (atm_set_diagnostic_config(diagnostic_flag));
960#else
961 (void)diagnostic_flag;
962 return (KERN_NOT_SUPPORTED);
963#endif
1c79356b 964}