2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
62 * Non-ipc host functions.
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>
79 #include <kern/kern_types.h>
80 #include <kern/assert.h>
81 #include <kern/kalloc.h>
82 #include <kern/host.h>
83 #include <kern/host_statistics.h>
84 #include <kern/ipc_host.h>
85 #include <kern/misc_protos.h>
86 #include <kern/sched.h>
87 #include <kern/processor.h>
89 #include <vm/vm_map.h>
95 host_priv_t host_priv
,
96 processor_array_t
*out_array
,
97 mach_msg_type_number_t
*countp
)
99 register processor_t processor
, *tp
;
101 unsigned int count
, i
;
103 if (host_priv
== HOST_PRIV_NULL
)
104 return (KERN_INVALID_ARGUMENT
);
106 assert(host_priv
== &realhost
);
108 count
= processor_count
;
111 addr
= kalloc((vm_size_t
) (count
* sizeof(mach_port_t
)));
113 return (KERN_RESOURCE_SHORTAGE
);
115 tp
= (processor_t
*) addr
;
116 *tp
++ = processor
= processor_list
;
119 simple_lock(&processor_list_lock
);
121 for (i
= 1; i
< count
; i
++)
122 *tp
++ = processor
= processor
->processor_list
;
124 simple_unlock(&processor_list_lock
);
128 *out_array
= (processor_array_t
)addr
;
130 /* do the conversion that Mig should handle */
132 tp
= (processor_t
*) addr
;
133 for (i
= 0; i
< count
; i
++)
134 ((mach_port_t
*) tp
)[i
] =
135 (mach_port_t
)convert_processor_to_port(tp
[i
]);
137 return (KERN_SUCCESS
);
143 host_flavor_t flavor
,
145 mach_msg_type_number_t
*count
)
148 if (host
== HOST_NULL
)
149 return (KERN_INVALID_ARGUMENT
);
153 case HOST_BASIC_INFO
:
155 register host_basic_info_t basic_info
;
156 register int master_num
;
159 * Basic information about this host.
161 if (*count
< HOST_BASIC_INFO_OLD_COUNT
)
162 return (KERN_FAILURE
);
164 basic_info
= (host_basic_info_t
) info
;
166 basic_info
->memory_size
= machine_info
.memory_size
;
167 basic_info
->max_cpus
= machine_info
.max_cpus
;
168 basic_info
->avail_cpus
= processor_avail_count
;
169 master_num
= master_processor
->cpu_num
;
170 basic_info
->cpu_type
= slot_type(master_num
);
171 basic_info
->cpu_subtype
= slot_subtype(master_num
);
173 if (*count
>= HOST_BASIC_INFO_COUNT
) {
174 basic_info
->cpu_threadtype
= slot_threadtype(master_num
);
175 basic_info
->physical_cpu
= machine_info
.physical_cpu
;
176 basic_info
->physical_cpu_max
= machine_info
.physical_cpu_max
;
177 basic_info
->logical_cpu
= machine_info
.logical_cpu
;
178 basic_info
->logical_cpu_max
= machine_info
.logical_cpu_max
;
179 basic_info
->max_mem
= machine_info
.max_mem
;
181 *count
= HOST_BASIC_INFO_COUNT
;
183 *count
= HOST_BASIC_INFO_OLD_COUNT
;
186 return (KERN_SUCCESS
);
189 case HOST_SCHED_INFO
:
191 register host_sched_info_t sched_info
;
194 * Return scheduler information.
196 if (*count
< HOST_SCHED_INFO_COUNT
)
197 return (KERN_FAILURE
);
199 sched_info
= (host_sched_info_t
) info
;
201 sched_info
->min_timeout
=
202 sched_info
->min_quantum
= std_quantum_us
/ 1000;
204 *count
= HOST_SCHED_INFO_COUNT
;
206 return (KERN_SUCCESS
);
209 case HOST_RESOURCE_SIZES
:
212 * Return sizes of kernel data structures
214 if (*count
< HOST_RESOURCE_SIZES_COUNT
)
215 return (KERN_FAILURE
);
217 /* XXX Fail until ledgers are implemented */
218 return (KERN_INVALID_ARGUMENT
);
221 case HOST_PRIORITY_INFO
:
223 register host_priority_info_t priority_info
;
225 if (*count
< HOST_PRIORITY_INFO_COUNT
)
226 return (KERN_FAILURE
);
228 priority_info
= (host_priority_info_t
) info
;
230 priority_info
->kernel_priority
= MINPRI_KERNEL
;
231 priority_info
->system_priority
= MINPRI_KERNEL
;
232 priority_info
->server_priority
= MINPRI_RESERVED
;
233 priority_info
->user_priority
= BASEPRI_DEFAULT
;
234 priority_info
->depress_priority
= DEPRESSPRI
;
235 priority_info
->idle_priority
= IDLEPRI
;
236 priority_info
->minimum_priority
= MINPRI_USER
;
237 priority_info
->maximum_priority
= MAXPRI_RESERVED
;
239 *count
= HOST_PRIORITY_INFO_COUNT
;
241 return (KERN_SUCCESS
);
245 * Gestalt for various trap facilities.
247 case HOST_MACH_MSG_TRAP
:
248 case HOST_SEMAPHORE_TRAPS
:
251 return (KERN_SUCCESS
);
255 return (KERN_INVALID_ARGUMENT
);
262 host_flavor_t flavor
,
264 mach_msg_type_number_t
*count
)
267 if (host
== HOST_NULL
)
268 return (KERN_INVALID_HOST
);
274 host_load_info_t load_info
;
276 if (*count
< HOST_LOAD_INFO_COUNT
)
277 return (KERN_FAILURE
);
279 load_info
= (host_load_info_t
) info
;
281 bcopy((char *) avenrun
,
282 (char *) load_info
->avenrun
, sizeof avenrun
);
283 bcopy((char *) mach_factor
,
284 (char *) load_info
->mach_factor
, sizeof mach_factor
);
286 *count
= HOST_LOAD_INFO_COUNT
;
287 return (KERN_SUCCESS
);
292 register processor_t processor
;
293 register vm_statistics_t stat
;
294 vm_statistics_data_t host_vm_stat
;
295 mach_msg_type_number_t original_count
;
297 if (*count
< HOST_VM_INFO_REV0_COUNT
)
298 return (KERN_FAILURE
);
300 processor
= processor_list
;
301 stat
= &PROCESSOR_DATA(processor
, vm_stat
);
302 host_vm_stat
= *stat
;
304 if (processor_count
> 1) {
305 simple_lock(&processor_list_lock
);
307 while ((processor
= processor
->processor_list
) != NULL
) {
308 stat
= &PROCESSOR_DATA(processor
, vm_stat
);
310 host_vm_stat
.zero_fill_count
+= stat
->zero_fill_count
;
311 host_vm_stat
.reactivations
+= stat
->reactivations
;
312 host_vm_stat
.pageins
+= stat
->pageins
;
313 host_vm_stat
.pageouts
+= stat
->pageouts
;
314 host_vm_stat
.faults
+= stat
->faults
;
315 host_vm_stat
.cow_faults
+= stat
->cow_faults
;
316 host_vm_stat
.lookups
+= stat
->lookups
;
317 host_vm_stat
.hits
+= stat
->hits
;
320 simple_unlock(&processor_list_lock
);
323 stat
= (vm_statistics_t
) info
;
325 stat
->free_count
= vm_page_free_count
+ vm_page_speculative_count
;
326 stat
->active_count
= vm_page_active_count
;
327 stat
->inactive_count
= vm_page_inactive_count
;
328 stat
->wire_count
= vm_page_wire_count
;
329 stat
->zero_fill_count
= host_vm_stat
.zero_fill_count
;
330 stat
->reactivations
= host_vm_stat
.reactivations
;
331 stat
->pageins
= host_vm_stat
.pageins
;
332 stat
->pageouts
= host_vm_stat
.pageouts
;
333 stat
->faults
= host_vm_stat
.faults
;
334 stat
->cow_faults
= host_vm_stat
.cow_faults
;
335 stat
->lookups
= host_vm_stat
.lookups
;
336 stat
->hits
= host_vm_stat
.hits
;
339 * Fill in extra info added in later revisions of the
340 * vm_statistics data structure. Fill in only what can fit
341 * in the data structure the caller gave us !
343 original_count
= *count
;
344 *count
= HOST_VM_INFO_REV0_COUNT
; /* rev0 already filled in */
345 if (original_count
>= HOST_VM_INFO_REV1_COUNT
) {
346 /* rev1 added "purgeable" info */
347 stat
->purgeable_count
= vm_page_purgeable_count
;
348 stat
->purges
= vm_page_purged_count
;
349 *count
= HOST_VM_INFO_REV1_COUNT
;
351 if (original_count
>= HOST_VM_INFO_REV2_COUNT
) {
352 /* rev2 added "speculative" info */
353 stat
->speculative_count
= vm_page_speculative_count
;
354 *count
= HOST_VM_INFO_REV2_COUNT
;
357 return (KERN_SUCCESS
);
360 case HOST_CPU_LOAD_INFO
:
362 register processor_t processor
;
363 host_cpu_load_info_t cpu_load_info
;
365 if (*count
< HOST_CPU_LOAD_INFO_COUNT
)
366 return (KERN_FAILURE
);
368 #define GET_TICKS_VALUE(processor, state, timer) \
370 cpu_load_info->cpu_ticks[(state)] += \
371 timer_grab(&PROCESSOR_DATA(processor, timer)) / hz_tick_interval; \
374 cpu_load_info
= (host_cpu_load_info_t
)info
;
375 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = 0;
376 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
377 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] = 0;
378 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
380 processor
= processor_list
;
381 GET_TICKS_VALUE(processor
, CPU_STATE_USER
, user_state
);
382 GET_TICKS_VALUE(processor
, CPU_STATE_SYSTEM
, system_state
);
383 GET_TICKS_VALUE(processor
, CPU_STATE_IDLE
, idle_state
);
385 if (processor_count
> 1) {
386 simple_lock(&processor_list_lock
);
388 while ((processor
= processor
->processor_list
) != NULL
) {
389 GET_TICKS_VALUE(processor
, CPU_STATE_USER
, user_state
);
390 GET_TICKS_VALUE(processor
, CPU_STATE_SYSTEM
, system_state
);
391 GET_TICKS_VALUE(processor
, CPU_STATE_IDLE
, idle_state
);
394 simple_unlock(&processor_list_lock
);
397 *count
= HOST_CPU_LOAD_INFO_COUNT
;
399 return (KERN_SUCCESS
);
403 return (KERN_INVALID_ARGUMENT
);
408 * Get host statistics that require privilege.
409 * None for now, just call the un-privileged version.
412 host_priv_statistics(
413 host_priv_t host_priv
,
414 host_flavor_t flavor
,
416 mach_msg_type_number_t
*count
)
418 return(host_statistics((host_t
)host_priv
, flavor
, info
, count
));
424 vm_size_t
*out_page_size
)
426 if (host
== HOST_NULL
)
427 return(KERN_INVALID_ARGUMENT
);
429 *out_page_size
= PAGE_SIZE
;
431 return(KERN_SUCCESS
);
435 * Return kernel version string (more than you ever
436 * wanted to know about what version of the kernel this is).
438 extern char version
[];
443 kernel_version_t out_version
)
446 if (host
== HOST_NULL
)
447 return(KERN_INVALID_ARGUMENT
);
449 (void) strncpy(out_version
, version
, sizeof(kernel_version_t
));
451 return(KERN_SUCCESS
);
455 * host_processor_sets:
457 * List all processor sets on the host.
461 host_priv_t host_priv
,
462 processor_set_name_array_t
*pset_list
,
463 mach_msg_type_number_t
*count
)
467 if (host_priv
== HOST_PRIV_NULL
)
468 return (KERN_INVALID_ARGUMENT
);
471 * Allocate memory. Can be pageable because it won't be
472 * touched while holding a lock.
475 addr
= kalloc((vm_size_t
) sizeof(mach_port_t
));
477 return (KERN_RESOURCE_SHORTAGE
);
479 /* do the conversion that Mig should handle */
480 *((ipc_port_t
*) addr
) = convert_pset_name_to_port(&pset0
);
482 *pset_list
= (processor_set_array_t
)addr
;
485 return (KERN_SUCCESS
);
489 * host_processor_set_priv:
491 * Return control port for given processor set.
494 host_processor_set_priv(
495 host_priv_t host_priv
,
496 processor_set_t pset_name
,
497 processor_set_t
*pset
)
499 if (host_priv
== HOST_PRIV_NULL
|| pset_name
== PROCESSOR_SET_NULL
) {
500 *pset
= PROCESSOR_SET_NULL
;
502 return (KERN_INVALID_ARGUMENT
);
507 return (KERN_SUCCESS
);
511 * host_processor_info
513 * Return info about the processors on this host. It will return
514 * the number of processors, and the specific type of info requested
520 processor_flavor_t flavor
,
521 natural_t
*out_pcount
,
522 processor_info_array_t
*out_array
,
523 mach_msg_type_number_t
*out_array_count
)
525 kern_return_t result
;
526 processor_t processor
;
528 processor_info_t info
;
529 unsigned int icount
, tcount
;
530 unsigned int pcount
, i
;
532 vm_size_t size
, needed
;
535 if (host
== HOST_NULL
)
536 return (KERN_INVALID_ARGUMENT
);
538 result
= processor_info_count(flavor
, &icount
);
539 if (result
!= KERN_SUCCESS
)
542 pcount
= processor_count
;
545 needed
= pcount
* icount
* sizeof(natural_t
);
546 size
= round_page(needed
);
547 result
= kmem_alloc(ipc_kernel_map
, &addr
, size
);
548 if (result
!= KERN_SUCCESS
)
549 return (KERN_RESOURCE_SHORTAGE
);
551 info
= (processor_info_t
) addr
;
552 processor
= processor_list
;
555 result
= processor_info(processor
, flavor
, &thost
, info
, &tcount
);
556 if (result
!= KERN_SUCCESS
) {
557 kmem_free(ipc_kernel_map
, addr
, size
);
562 for (i
= 1; i
< pcount
; i
++) {
563 simple_lock(&processor_list_lock
);
564 processor
= processor
->processor_list
;
565 simple_unlock(&processor_list_lock
);
569 result
= processor_info(processor
, flavor
, &thost
, info
, &tcount
);
570 if (result
!= KERN_SUCCESS
) {
571 kmem_free(ipc_kernel_map
, addr
, size
);
578 bzero((char *) addr
+ needed
, size
- needed
);
580 result
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
581 vm_map_round_page(addr
+ size
), FALSE
);
582 assert(result
== KERN_SUCCESS
);
583 result
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
584 (vm_map_size_t
)size
, TRUE
, ©
);
585 assert(result
== KERN_SUCCESS
);
587 *out_pcount
= pcount
;
588 *out_array
= (processor_info_array_t
) copy
;
589 *out_array_count
= pcount
* icount
;
591 return (KERN_SUCCESS
);
595 * Kernel interface for setting a special port.
598 kernel_set_special_port(
599 host_priv_t host_priv
,
605 host_lock(host_priv
);
606 old_port
= host_priv
->special
[id
];
607 host_priv
->special
[id
] = port
;
608 host_unlock(host_priv
);
609 if (IP_VALID(old_port
))
610 ipc_port_release_send(old_port
);
615 * User interface for setting a special port.
617 * Only permits the user to set a user-owned special port
618 * ID, rejecting a kernel-owned special port ID.
620 * A special kernel port cannot be set up using this
621 * routine; use kernel_set_special_port() instead.
624 host_set_special_port(
625 host_priv_t host_priv
,
629 if (host_priv
== HOST_PRIV_NULL
||
630 id
<= HOST_MAX_SPECIAL_KERNEL_PORT
|| id
> HOST_MAX_SPECIAL_PORT
) {
632 ipc_port_release_send(port
);
633 return KERN_INVALID_ARGUMENT
;
636 return kernel_set_special_port(host_priv
, id
, port
);
641 * User interface for retrieving a special port.
643 * Note that there is nothing to prevent a user special
644 * port from disappearing after it has been discovered by
645 * the caller; thus, using a special port can always result
646 * in a "port not valid" error.
650 host_get_special_port(
651 host_priv_t host_priv
,
658 if (host_priv
== HOST_PRIV_NULL
||
659 id
== HOST_SECURITY_PORT
|| id
> HOST_MAX_SPECIAL_PORT
)
660 return KERN_INVALID_ARGUMENT
;
662 host_lock(host_priv
);
663 port
= realhost
.special
[id
];
664 *portp
= ipc_port_copy_send(port
);
665 host_unlock(host_priv
);
674 * Return the IO master access port for this host.
679 io_master_t
*io_masterp
)
681 if (host
== HOST_NULL
)
682 return KERN_INVALID_ARGUMENT
;
684 return (host_get_io_master_port(host_priv_self(), io_masterp
));
700 host_security_self(void)