2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
34 * Mach Operating System
35 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
36 * All Rights Reserved.
38 * Permission to use, copy, modify and distribute this software and its
39 * documentation is hereby granted, provided that both the copyright
40 * notice and this permission notice appear in all copies of the
41 * software, derivative works or modified versions, and any portions
42 * thereof, and that both notices appear in supporting documentation.
44 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
45 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
46 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
48 * Carnegie Mellon requests users of this software to return to
50 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
51 * School of Computer Science
52 * Carnegie Mellon University
53 * Pittsburgh PA 15213-3890
55 * any improvements or extensions that they make and grant Carnegie Mellon
56 * the rights to redistribute these changes.
64 * Non-ipc host functions.
67 #include <mach_host.h>
69 #include <mach/mach_types.h>
70 #include <mach/boolean.h>
71 #include <mach/host_info.h>
72 #include <mach/host_special_ports.h>
73 #include <mach/kern_return.h>
74 #include <mach/machine.h>
75 #include <mach/port.h>
76 #include <mach/processor_info.h>
77 #include <mach/vm_param.h>
78 #include <mach/processor.h>
79 #include <mach/mach_host_server.h>
80 #include <mach/host_priv_server.h>
81 #include <mach/vm_map.h>
83 #include <kern/kern_types.h>
84 #include <kern/assert.h>
85 #include <kern/kalloc.h>
86 #include <kern/host.h>
87 #include <kern/host_statistics.h>
88 #include <kern/ipc_host.h>
89 #include <kern/misc_protos.h>
90 #include <kern/sched.h>
91 #include <kern/processor.h>
93 #include <vm/vm_map.h>
96 #include <dipc/dipc_funcs.h>
97 #include <dipc/special_ports.h>
100 host_data_t realhost
;
104 host_priv_t host_priv
,
105 processor_array_t
*out_array
,
106 mach_msg_type_number_t
*countp
)
108 register processor_t processor
, *tp
;
110 unsigned int count
, i
;
112 if (host_priv
== HOST_PRIV_NULL
)
113 return (KERN_INVALID_ARGUMENT
);
115 assert(host_priv
== &realhost
);
117 count
= processor_count
;
120 addr
= kalloc((vm_size_t
) (count
* sizeof(mach_port_t
)));
122 return (KERN_RESOURCE_SHORTAGE
);
124 tp
= (processor_t
*) addr
;
125 *tp
++ = processor
= processor_list
;
128 simple_lock(&processor_list_lock
);
130 for (i
= 1; i
< count
; i
++)
131 *tp
++ = processor
= processor
->processor_list
;
133 simple_unlock(&processor_list_lock
);
137 *out_array
= (processor_array_t
)addr
;
139 /* do the conversion that Mig should handle */
141 tp
= (processor_t
*) addr
;
142 for (i
= 0; i
< count
; i
++)
143 ((mach_port_t
*) tp
)[i
] =
144 (mach_port_t
)convert_processor_to_port(tp
[i
]);
146 return (KERN_SUCCESS
);
152 host_flavor_t flavor
,
154 mach_msg_type_number_t
*count
)
157 if (host
== HOST_NULL
)
158 return (KERN_INVALID_ARGUMENT
);
162 case HOST_BASIC_INFO
:
164 register host_basic_info_t basic_info
;
165 register int master_slot
;
168 * Basic information about this host.
170 if (*count
< HOST_BASIC_INFO_OLD_COUNT
)
171 return (KERN_FAILURE
);
173 basic_info
= (host_basic_info_t
) info
;
175 basic_info
->max_cpus
= machine_info
.max_cpus
;
176 basic_info
->avail_cpus
= machine_info
.avail_cpus
;
177 basic_info
->memory_size
= machine_info
.memory_size
;
178 master_slot
= PROCESSOR_DATA(master_processor
, slot_num
);
179 basic_info
->cpu_type
= slot_type(master_slot
);
180 basic_info
->cpu_subtype
= slot_subtype(master_slot
);
182 if (*count
>= HOST_BASIC_INFO_COUNT
) {
183 basic_info
->cpu_threadtype
= slot_threadtype(master_slot
);
184 basic_info
->physical_cpu
= machine_info
.physical_cpu
;
185 basic_info
->physical_cpu_max
= machine_info
.physical_cpu_max
;
186 basic_info
->logical_cpu
= machine_info
.logical_cpu
;
187 basic_info
->logical_cpu_max
= machine_info
.logical_cpu_max
;
188 basic_info
->max_mem
= machine_info
.max_mem
;
190 *count
= HOST_BASIC_INFO_COUNT
;
192 *count
= HOST_BASIC_INFO_OLD_COUNT
;
195 return (KERN_SUCCESS
);
198 case HOST_SCHED_INFO
:
200 register host_sched_info_t sched_info
;
203 * Return scheduler information.
205 if (*count
< HOST_SCHED_INFO_COUNT
)
206 return (KERN_FAILURE
);
208 sched_info
= (host_sched_info_t
) info
;
210 sched_info
->min_timeout
=
211 sched_info
->min_quantum
= std_quantum_us
/ 1000;
213 *count
= HOST_SCHED_INFO_COUNT
;
215 return (KERN_SUCCESS
);
218 case HOST_RESOURCE_SIZES
:
221 * Return sizes of kernel data structures
223 if (*count
< HOST_RESOURCE_SIZES_COUNT
)
224 return (KERN_FAILURE
);
226 /* XXX Fail until ledgers are implemented */
227 return (KERN_INVALID_ARGUMENT
);
230 case HOST_PRIORITY_INFO
:
232 register host_priority_info_t priority_info
;
234 if (*count
< HOST_PRIORITY_INFO_COUNT
)
235 return (KERN_FAILURE
);
237 priority_info
= (host_priority_info_t
) info
;
239 priority_info
->kernel_priority
= MINPRI_KERNEL
;
240 priority_info
->system_priority
= MINPRI_KERNEL
;
241 priority_info
->server_priority
= MINPRI_RESERVED
;
242 priority_info
->user_priority
= BASEPRI_DEFAULT
;
243 priority_info
->depress_priority
= DEPRESSPRI
;
244 priority_info
->idle_priority
= IDLEPRI
;
245 priority_info
->minimum_priority
= MINPRI_USER
;
246 priority_info
->maximum_priority
= MAXPRI_RESERVED
;
248 *count
= HOST_PRIORITY_INFO_COUNT
;
250 return (KERN_SUCCESS
);
254 * Gestalt for various trap facilities.
256 case HOST_MACH_MSG_TRAP
:
257 case HOST_SEMAPHORE_TRAPS
:
260 return (KERN_SUCCESS
);
264 return (KERN_INVALID_ARGUMENT
);
271 host_flavor_t flavor
,
273 mach_msg_type_number_t
*count
)
276 if (host
== HOST_NULL
)
277 return (KERN_INVALID_HOST
);
283 host_load_info_t load_info
;
285 if (*count
< HOST_LOAD_INFO_COUNT
)
286 return (KERN_FAILURE
);
288 load_info
= (host_load_info_t
) info
;
290 bcopy((char *) avenrun
,
291 (char *) load_info
->avenrun
, sizeof avenrun
);
292 bcopy((char *) mach_factor
,
293 (char *) load_info
->mach_factor
, sizeof mach_factor
);
295 *count
= HOST_LOAD_INFO_COUNT
;
296 return (KERN_SUCCESS
);
301 register processor_t processor
;
302 register vm_statistics_t stat
;
303 vm_statistics_data_t host_vm_stat
;
305 if (*count
< HOST_VM_INFO_REV0_COUNT
)
306 return (KERN_FAILURE
);
308 processor
= processor_list
;
309 stat
= &PROCESSOR_DATA(processor
, vm_stat
);
310 host_vm_stat
= *stat
;
312 if (processor_count
> 1) {
313 simple_lock(&processor_list_lock
);
315 while ((processor
= processor
->processor_list
) != NULL
) {
316 stat
= &PROCESSOR_DATA(processor
, vm_stat
);
318 host_vm_stat
.zero_fill_count
+= stat
->zero_fill_count
;
319 host_vm_stat
.reactivations
+= stat
->reactivations
;
320 host_vm_stat
.pageins
+= stat
->pageins
;
321 host_vm_stat
.pageouts
+= stat
->pageouts
;
322 host_vm_stat
.faults
+= stat
->faults
;
323 host_vm_stat
.cow_faults
+= stat
->cow_faults
;
324 host_vm_stat
.lookups
+= stat
->lookups
;
325 host_vm_stat
.hits
+= stat
->hits
;
328 simple_unlock(&processor_list_lock
);
331 stat
= (vm_statistics_t
) info
;
333 stat
->free_count
= vm_page_free_count
;
334 stat
->active_count
= vm_page_active_count
;
335 stat
->inactive_count
= vm_page_inactive_count
;
336 stat
->wire_count
= vm_page_wire_count
;
337 stat
->zero_fill_count
= host_vm_stat
.zero_fill_count
;
338 stat
->reactivations
= host_vm_stat
.reactivations
;
339 stat
->pageins
= host_vm_stat
.pageins
;
340 stat
->pageouts
= host_vm_stat
.pageouts
;
341 stat
->faults
= host_vm_stat
.faults
;
342 stat
->cow_faults
= host_vm_stat
.cow_faults
;
343 stat
->lookups
= host_vm_stat
.lookups
;
344 stat
->hits
= host_vm_stat
.hits
;
346 if (*count
>= HOST_VM_INFO_COUNT
) {
347 /* info that was not in revision 0 of that interface */
348 stat
->purgeable_count
= vm_page_purgeable_count
;
349 stat
->purges
= vm_page_purged_count
;
350 *count
= HOST_VM_INFO_COUNT
;
352 *count
= HOST_VM_INFO_REV0_COUNT
;
355 return (KERN_SUCCESS
);
358 case HOST_CPU_LOAD_INFO
:
360 register processor_t processor
;
361 host_cpu_load_info_t cpu_load_info
;
362 unsigned long ticks_value1
, ticks_value2
;
364 if (*count
< HOST_CPU_LOAD_INFO_COUNT
)
365 return (KERN_FAILURE
);
367 #define GET_TICKS_VALUE(processor, state) \
370 ticks_value1 = *(volatile integer_t *) \
371 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
372 ticks_value2 = *(volatile integer_t *) \
373 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
374 } while (ticks_value1 != ticks_value2); \
376 cpu_load_info->cpu_ticks[(state)] += ticks_value1; \
379 cpu_load_info
= (host_cpu_load_info_t
)info
;
380 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = 0;
381 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
382 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
383 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] = 0;
385 processor
= processor_list
;
386 GET_TICKS_VALUE(processor
, CPU_STATE_USER
);
387 GET_TICKS_VALUE(processor
, CPU_STATE_NICE
);
388 GET_TICKS_VALUE(processor
, CPU_STATE_SYSTEM
);
389 GET_TICKS_VALUE(processor
, CPU_STATE_IDLE
);
391 if (processor_count
> 1) {
392 simple_lock(&processor_list_lock
);
394 while ((processor
= processor
->processor_list
) != NULL
) {
395 GET_TICKS_VALUE(processor
, CPU_STATE_USER
);
396 GET_TICKS_VALUE(processor
, CPU_STATE_NICE
);
397 GET_TICKS_VALUE(processor
, CPU_STATE_SYSTEM
);
398 GET_TICKS_VALUE(processor
, CPU_STATE_IDLE
);
401 simple_unlock(&processor_list_lock
);
404 *count
= HOST_CPU_LOAD_INFO_COUNT
;
406 return (KERN_SUCCESS
);
410 return (KERN_INVALID_ARGUMENT
);
415 * Get host statistics that require privilege.
416 * None for now, just call the un-privileged version.
419 host_priv_statistics(
420 host_priv_t host_priv
,
421 host_flavor_t flavor
,
423 mach_msg_type_number_t
*count
)
425 return(host_statistics((host_t
)host_priv
, flavor
, info
, count
));
432 vm_size_t
*out_page_size
)
434 if (host
== HOST_NULL
)
435 return(KERN_INVALID_ARGUMENT
);
437 *out_page_size
= PAGE_SIZE
;
439 return(KERN_SUCCESS
);
443 * Return kernel version string (more than you ever
444 * wanted to know about what version of the kernel this is).
446 extern char version
[];
451 kernel_version_t out_version
)
454 if (host
== HOST_NULL
)
455 return(KERN_INVALID_ARGUMENT
);
457 (void) strncpy(out_version
, version
, sizeof(kernel_version_t
));
459 return(KERN_SUCCESS
);
463 * host_processor_sets:
465 * List all processor sets on the host.
469 host_priv_t host_priv
,
470 processor_set_name_array_t
*pset_list
,
471 mach_msg_type_number_t
*count
)
475 if (host_priv
== HOST_PRIV_NULL
)
476 return KERN_INVALID_ARGUMENT
;
479 * Allocate memory. Can be pageable because it won't be
480 * touched while holding a lock.
483 addr
= kalloc((vm_size_t
) sizeof(mach_port_t
));
485 return KERN_RESOURCE_SHORTAGE
;
487 /* take ref for convert_pset_name_to_port */
488 pset_reference(&default_pset
);
489 /* do the conversion that Mig should handle */
490 *((ipc_port_t
*) addr
) = convert_pset_name_to_port(&default_pset
);
492 *pset_list
= (processor_set_array_t
)addr
;
499 * host_processor_set_priv:
501 * Return control port for given processor set.
504 host_processor_set_priv(
505 host_priv_t host_priv
,
506 processor_set_t pset_name
,
507 processor_set_t
*pset
)
509 if ((host_priv
== HOST_PRIV_NULL
) || (pset_name
== PROCESSOR_SET_NULL
)) {
510 *pset
= PROCESSOR_SET_NULL
;
511 return(KERN_INVALID_ARGUMENT
);
515 pset_reference(*pset
);
516 return(KERN_SUCCESS
);
520 * host_processor_info
522 * Return info about the processors on this host. It will return
523 * the number of processors, and the specific type of info requested
529 processor_flavor_t flavor
,
530 natural_t
*out_pcount
,
531 processor_info_array_t
*out_array
,
532 mach_msg_type_number_t
*out_array_count
)
534 kern_return_t result
;
535 processor_t processor
;
537 processor_info_t info
;
538 unsigned int icount
, tcount
;
539 unsigned int pcount
, i
;
544 if (host
== HOST_NULL
)
545 return (KERN_INVALID_ARGUMENT
);
547 result
= processor_info_count(flavor
, &icount
);
548 if (result
!= KERN_SUCCESS
)
551 pcount
= processor_count
;
554 size
= round_page(pcount
* icount
* sizeof(natural_t
));
555 result
= kmem_alloc(ipc_kernel_map
, &addr
, size
);
556 if (result
!= KERN_SUCCESS
)
557 return (KERN_RESOURCE_SHORTAGE
);
559 info
= (processor_info_t
) addr
;
560 processor
= processor_list
;
563 result
= processor_info(processor
, flavor
, &thost
, info
, &tcount
);
564 if (result
!= KERN_SUCCESS
) {
565 kmem_free(ipc_kernel_map
, addr
, size
);
570 for (i
= 1; i
< pcount
; i
++) {
571 simple_lock(&processor_list_lock
);
572 processor
= processor
->processor_list
;
573 simple_unlock(&processor_list_lock
);
577 result
= processor_info(processor
, flavor
, &thost
, info
, &tcount
);
578 if (result
!= KERN_SUCCESS
) {
579 kmem_free(ipc_kernel_map
, addr
, size
);
585 result
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
586 vm_map_round_page(addr
+ size
), FALSE
);
587 assert(result
== KERN_SUCCESS
);
588 result
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
589 (vm_map_size_t
)size
, TRUE
, ©
);
590 assert(result
== KERN_SUCCESS
);
592 *out_pcount
= pcount
;
593 *out_array
= (processor_info_array_t
) copy
;
594 *out_array_count
= pcount
* icount
;
596 return (KERN_SUCCESS
);
600 * Kernel interface for setting a special port.
603 kernel_set_special_port(
604 host_priv_t host_priv
,
610 host_lock(host_priv
);
611 old_port
= host_priv
->special
[id
];
612 host_priv
->special
[id
] = port
;
613 host_unlock(host_priv
);
614 if (IP_VALID(old_port
))
615 ipc_port_release_send(old_port
);
620 * User interface for setting a special port.
622 * Only permits the user to set a user-owned special port
623 * ID, rejecting a kernel-owned special port ID.
625 * A special kernel port cannot be set up using this
626 * routine; use kernel_set_special_port() instead.
629 host_set_special_port(
630 host_priv_t host_priv
,
634 if (host_priv
== HOST_PRIV_NULL
||
635 id
<= HOST_MAX_SPECIAL_KERNEL_PORT
|| id
> HOST_MAX_SPECIAL_PORT
) {
637 ipc_port_release_send(port
);
638 return KERN_INVALID_ARGUMENT
;
641 return kernel_set_special_port(host_priv
, id
, port
);
646 * User interface for retrieving a special port.
648 * Note that there is nothing to prevent a user special
649 * port from disappearing after it has been discovered by
650 * the caller; thus, using a special port can always result
651 * in a "port not valid" error.
655 host_get_special_port(
656 host_priv_t host_priv
,
663 if (host_priv
== HOST_PRIV_NULL
||
664 id
== HOST_SECURITY_PORT
|| id
> HOST_MAX_SPECIAL_PORT
)
665 return KERN_INVALID_ARGUMENT
;
668 if (node
!= HOST_LOCAL_NODE
)
669 return norma_get_special_port(host_priv
, node
, id
, portp
);
672 host_lock(host_priv
);
673 port
= realhost
.special
[id
];
674 *portp
= ipc_port_copy_send(port
);
675 host_unlock(host_priv
);
684 * Return the IO master access port for this host.
689 io_master_t
*io_masterp
)
691 if (host
== HOST_NULL
)
692 return KERN_INVALID_ARGUMENT
;
694 return (host_get_io_master_port(host_priv_self(), io_masterp
));
710 host_security_self(void)