2 * Copyright (c) 2000-2004 Apple Computer, 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_host.h>
67 #include <mach/mach_types.h>
68 #include <mach/boolean.h>
69 #include <mach/host_info.h>
70 #include <mach/host_special_ports.h>
71 #include <mach/kern_return.h>
72 #include <mach/machine.h>
73 #include <mach/port.h>
74 #include <mach/processor_info.h>
75 #include <mach/vm_param.h>
76 #include <mach/processor.h>
77 #include <mach/mach_host_server.h>
78 #include <mach/host_priv_server.h>
79 #include <mach/vm_map.h>
81 #include <kern/kern_types.h>
82 #include <kern/assert.h>
83 #include <kern/kalloc.h>
84 #include <kern/host.h>
85 #include <kern/host_statistics.h>
86 #include <kern/ipc_host.h>
87 #include <kern/misc_protos.h>
88 #include <kern/sched.h>
89 #include <kern/processor.h>
91 #include <vm/vm_map.h>
94 #include <dipc/dipc_funcs.h>
95 #include <dipc/special_ports.h>
102 host_priv_t host_priv
,
103 processor_array_t
*out_array
,
104 mach_msg_type_number_t
*countp
)
106 register processor_t processor
, *tp
;
108 unsigned int count
, i
;
110 if (host_priv
== HOST_PRIV_NULL
)
111 return (KERN_INVALID_ARGUMENT
);
113 assert(host_priv
== &realhost
);
115 count
= processor_count
;
118 addr
= kalloc((vm_size_t
) (count
* sizeof(mach_port_t
)));
120 return (KERN_RESOURCE_SHORTAGE
);
122 tp
= (processor_t
*) addr
;
123 *tp
++ = processor
= processor_list
;
126 simple_lock(&processor_list_lock
);
128 for (i
= 1; i
< count
; i
++)
129 *tp
++ = processor
= processor
->processor_list
;
131 simple_unlock(&processor_list_lock
);
135 *out_array
= (processor_array_t
)addr
;
137 /* do the conversion that Mig should handle */
139 tp
= (processor_t
*) addr
;
140 for (i
= 0; i
< count
; i
++)
141 ((mach_port_t
*) tp
)[i
] =
142 (mach_port_t
)convert_processor_to_port(tp
[i
]);
144 return (KERN_SUCCESS
);
150 host_flavor_t flavor
,
152 mach_msg_type_number_t
*count
)
155 if (host
== HOST_NULL
)
156 return (KERN_INVALID_ARGUMENT
);
160 case HOST_BASIC_INFO
:
162 register host_basic_info_t basic_info
;
163 register int master_slot
;
166 * Basic information about this host.
168 if (*count
< HOST_BASIC_INFO_OLD_COUNT
)
169 return (KERN_FAILURE
);
171 basic_info
= (host_basic_info_t
) info
;
173 basic_info
->max_cpus
= machine_info
.max_cpus
;
174 basic_info
->avail_cpus
= machine_info
.avail_cpus
;
175 basic_info
->memory_size
= machine_info
.memory_size
;
176 master_slot
= PROCESSOR_DATA(master_processor
, slot_num
);
177 basic_info
->cpu_type
= slot_type(master_slot
);
178 basic_info
->cpu_subtype
= slot_subtype(master_slot
);
180 if (*count
>= HOST_BASIC_INFO_COUNT
) {
181 basic_info
->cpu_threadtype
= slot_threadtype(master_slot
);
182 basic_info
->physical_cpu
= machine_info
.physical_cpu
;
183 basic_info
->physical_cpu_max
= machine_info
.physical_cpu_max
;
184 basic_info
->logical_cpu
= machine_info
.logical_cpu
;
185 basic_info
->logical_cpu_max
= machine_info
.logical_cpu_max
;
186 basic_info
->max_mem
= machine_info
.max_mem
;
188 *count
= HOST_BASIC_INFO_COUNT
;
190 *count
= HOST_BASIC_INFO_OLD_COUNT
;
193 return (KERN_SUCCESS
);
196 case HOST_SCHED_INFO
:
198 register host_sched_info_t sched_info
;
201 * Return scheduler information.
203 if (*count
< HOST_SCHED_INFO_COUNT
)
204 return (KERN_FAILURE
);
206 sched_info
= (host_sched_info_t
) info
;
208 sched_info
->min_timeout
=
209 sched_info
->min_quantum
= std_quantum_us
/ 1000;
211 *count
= HOST_SCHED_INFO_COUNT
;
213 return (KERN_SUCCESS
);
216 case HOST_RESOURCE_SIZES
:
219 * Return sizes of kernel data structures
221 if (*count
< HOST_RESOURCE_SIZES_COUNT
)
222 return (KERN_FAILURE
);
224 /* XXX Fail until ledgers are implemented */
225 return (KERN_INVALID_ARGUMENT
);
228 case HOST_PRIORITY_INFO
:
230 register host_priority_info_t priority_info
;
232 if (*count
< HOST_PRIORITY_INFO_COUNT
)
233 return (KERN_FAILURE
);
235 priority_info
= (host_priority_info_t
) info
;
237 priority_info
->kernel_priority
= MINPRI_KERNEL
;
238 priority_info
->system_priority
= MINPRI_KERNEL
;
239 priority_info
->server_priority
= MINPRI_RESERVED
;
240 priority_info
->user_priority
= BASEPRI_DEFAULT
;
241 priority_info
->depress_priority
= DEPRESSPRI
;
242 priority_info
->idle_priority
= IDLEPRI
;
243 priority_info
->minimum_priority
= MINPRI_USER
;
244 priority_info
->maximum_priority
= MAXPRI_RESERVED
;
246 *count
= HOST_PRIORITY_INFO_COUNT
;
248 return (KERN_SUCCESS
);
252 * Gestalt for various trap facilities.
254 case HOST_MACH_MSG_TRAP
:
255 case HOST_SEMAPHORE_TRAPS
:
258 return (KERN_SUCCESS
);
262 return (KERN_INVALID_ARGUMENT
);
269 host_flavor_t flavor
,
271 mach_msg_type_number_t
*count
)
274 if (host
== HOST_NULL
)
275 return (KERN_INVALID_HOST
);
281 host_load_info_t load_info
;
283 if (*count
< HOST_LOAD_INFO_COUNT
)
284 return (KERN_FAILURE
);
286 load_info
= (host_load_info_t
) info
;
288 bcopy((char *) avenrun
,
289 (char *) load_info
->avenrun
, sizeof avenrun
);
290 bcopy((char *) mach_factor
,
291 (char *) load_info
->mach_factor
, sizeof mach_factor
);
293 *count
= HOST_LOAD_INFO_COUNT
;
294 return (KERN_SUCCESS
);
299 register processor_t processor
;
300 register vm_statistics_t stat
;
301 vm_statistics_data_t host_vm_stat
;
303 if (*count
< HOST_VM_INFO_REV0_COUNT
)
304 return (KERN_FAILURE
);
306 processor
= processor_list
;
307 stat
= &PROCESSOR_DATA(processor
, vm_stat
);
308 host_vm_stat
= *stat
;
310 if (processor_count
> 1) {
311 simple_lock(&processor_list_lock
);
313 while ((processor
= processor
->processor_list
) != NULL
) {
314 stat
= &PROCESSOR_DATA(processor
, vm_stat
);
316 host_vm_stat
.zero_fill_count
+= stat
->zero_fill_count
;
317 host_vm_stat
.reactivations
+= stat
->reactivations
;
318 host_vm_stat
.pageins
+= stat
->pageins
;
319 host_vm_stat
.pageouts
+= stat
->pageouts
;
320 host_vm_stat
.faults
+= stat
->faults
;
321 host_vm_stat
.cow_faults
+= stat
->cow_faults
;
322 host_vm_stat
.lookups
+= stat
->lookups
;
323 host_vm_stat
.hits
+= stat
->hits
;
326 simple_unlock(&processor_list_lock
);
329 stat
= (vm_statistics_t
) info
;
331 stat
->free_count
= vm_page_free_count
;
332 stat
->active_count
= vm_page_active_count
;
333 stat
->inactive_count
= vm_page_inactive_count
;
334 stat
->wire_count
= vm_page_wire_count
;
335 stat
->zero_fill_count
= host_vm_stat
.zero_fill_count
;
336 stat
->reactivations
= host_vm_stat
.reactivations
;
337 stat
->pageins
= host_vm_stat
.pageins
;
338 stat
->pageouts
= host_vm_stat
.pageouts
;
339 stat
->faults
= host_vm_stat
.faults
;
340 stat
->cow_faults
= host_vm_stat
.cow_faults
;
341 stat
->lookups
= host_vm_stat
.lookups
;
342 stat
->hits
= host_vm_stat
.hits
;
344 if (*count
>= HOST_VM_INFO_COUNT
) {
345 /* info that was not in revision 0 of that interface */
346 stat
->purgeable_count
= vm_page_purgeable_count
;
347 stat
->purges
= vm_page_purged_count
;
348 *count
= HOST_VM_INFO_COUNT
;
350 *count
= HOST_VM_INFO_REV0_COUNT
;
353 return (KERN_SUCCESS
);
356 case HOST_CPU_LOAD_INFO
:
358 register processor_t processor
;
359 host_cpu_load_info_t cpu_load_info
;
360 unsigned long ticks_value1
, ticks_value2
;
362 if (*count
< HOST_CPU_LOAD_INFO_COUNT
)
363 return (KERN_FAILURE
);
365 #define GET_TICKS_VALUE(processor, state) \
368 ticks_value1 = *(volatile integer_t *) \
369 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
370 ticks_value2 = *(volatile integer_t *) \
371 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
372 } while (ticks_value1 != ticks_value2); \
374 cpu_load_info->cpu_ticks[(state)] += ticks_value1; \
377 cpu_load_info
= (host_cpu_load_info_t
)info
;
378 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = 0;
379 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
380 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
381 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] = 0;
383 processor
= processor_list
;
384 GET_TICKS_VALUE(processor
, CPU_STATE_USER
);
385 GET_TICKS_VALUE(processor
, CPU_STATE_NICE
);
386 GET_TICKS_VALUE(processor
, CPU_STATE_SYSTEM
);
387 GET_TICKS_VALUE(processor
, CPU_STATE_IDLE
);
389 if (processor_count
> 1) {
390 simple_lock(&processor_list_lock
);
392 while ((processor
= processor
->processor_list
) != NULL
) {
393 GET_TICKS_VALUE(processor
, CPU_STATE_USER
);
394 GET_TICKS_VALUE(processor
, CPU_STATE_NICE
);
395 GET_TICKS_VALUE(processor
, CPU_STATE_SYSTEM
);
396 GET_TICKS_VALUE(processor
, CPU_STATE_IDLE
);
399 simple_unlock(&processor_list_lock
);
402 *count
= HOST_CPU_LOAD_INFO_COUNT
;
404 return (KERN_SUCCESS
);
408 return (KERN_INVALID_ARGUMENT
);
413 * Get host statistics that require privilege.
414 * None for now, just call the un-privileged version.
417 host_priv_statistics(
418 host_priv_t host_priv
,
419 host_flavor_t flavor
,
421 mach_msg_type_number_t
*count
)
423 return(host_statistics((host_t
)host_priv
, flavor
, info
, count
));
430 vm_size_t
*out_page_size
)
432 if (host
== HOST_NULL
)
433 return(KERN_INVALID_ARGUMENT
);
435 *out_page_size
= PAGE_SIZE
;
437 return(KERN_SUCCESS
);
441 * Return kernel version string (more than you ever
442 * wanted to know about what version of the kernel this is).
444 extern char version
[];
449 kernel_version_t out_version
)
452 if (host
== HOST_NULL
)
453 return(KERN_INVALID_ARGUMENT
);
455 (void) strncpy(out_version
, version
, sizeof(kernel_version_t
));
457 return(KERN_SUCCESS
);
461 * host_processor_sets:
463 * List all processor sets on the host.
467 host_priv_t host_priv
,
468 processor_set_name_array_t
*pset_list
,
469 mach_msg_type_number_t
*count
)
473 if (host_priv
== HOST_PRIV_NULL
)
474 return KERN_INVALID_ARGUMENT
;
477 * Allocate memory. Can be pageable because it won't be
478 * touched while holding a lock.
481 addr
= kalloc((vm_size_t
) sizeof(mach_port_t
));
483 return KERN_RESOURCE_SHORTAGE
;
485 /* take ref for convert_pset_name_to_port */
486 pset_reference(&default_pset
);
487 /* do the conversion that Mig should handle */
488 *((ipc_port_t
*) addr
) = convert_pset_name_to_port(&default_pset
);
490 *pset_list
= (processor_set_array_t
)addr
;
497 * host_processor_set_priv:
499 * Return control port for given processor set.
502 host_processor_set_priv(
503 host_priv_t host_priv
,
504 processor_set_t pset_name
,
505 processor_set_t
*pset
)
507 if ((host_priv
== HOST_PRIV_NULL
) || (pset_name
== PROCESSOR_SET_NULL
)) {
508 *pset
= PROCESSOR_SET_NULL
;
509 return(KERN_INVALID_ARGUMENT
);
513 pset_reference(*pset
);
514 return(KERN_SUCCESS
);
518 * host_processor_info
520 * Return info about the processors on this host. It will return
521 * the number of processors, and the specific type of info requested
527 processor_flavor_t flavor
,
528 natural_t
*out_pcount
,
529 processor_info_array_t
*out_array
,
530 mach_msg_type_number_t
*out_array_count
)
532 kern_return_t result
;
533 processor_t processor
;
535 processor_info_t info
;
536 unsigned int icount
, tcount
;
537 unsigned int pcount
, i
;
542 if (host
== HOST_NULL
)
543 return (KERN_INVALID_ARGUMENT
);
545 result
= processor_info_count(flavor
, &icount
);
546 if (result
!= KERN_SUCCESS
)
549 pcount
= processor_count
;
552 size
= round_page(pcount
* icount
* sizeof(natural_t
));
553 result
= kmem_alloc(ipc_kernel_map
, &addr
, size
);
554 if (result
!= KERN_SUCCESS
)
555 return (KERN_RESOURCE_SHORTAGE
);
557 info
= (processor_info_t
) addr
;
558 processor
= processor_list
;
561 result
= processor_info(processor
, flavor
, &thost
, info
, &tcount
);
562 if (result
!= KERN_SUCCESS
) {
563 kmem_free(ipc_kernel_map
, addr
, size
);
568 for (i
= 1; i
< pcount
; i
++) {
569 simple_lock(&processor_list_lock
);
570 processor
= processor
->processor_list
;
571 simple_unlock(&processor_list_lock
);
575 result
= processor_info(processor
, flavor
, &thost
, info
, &tcount
);
576 if (result
!= KERN_SUCCESS
) {
577 kmem_free(ipc_kernel_map
, addr
, size
);
583 result
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
584 vm_map_round_page(addr
+ size
), FALSE
);
585 assert(result
== KERN_SUCCESS
);
586 result
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
587 (vm_map_size_t
)size
, TRUE
, ©
);
588 assert(result
== KERN_SUCCESS
);
590 *out_pcount
= pcount
;
591 *out_array
= (processor_info_array_t
) copy
;
592 *out_array_count
= pcount
* icount
;
594 return (KERN_SUCCESS
);
598 * Kernel interface for setting a special port.
601 kernel_set_special_port(
602 host_priv_t host_priv
,
608 host_lock(host_priv
);
609 old_port
= host_priv
->special
[id
];
610 host_priv
->special
[id
] = port
;
611 host_unlock(host_priv
);
612 if (IP_VALID(old_port
))
613 ipc_port_release_send(old_port
);
618 * User interface for setting a special port.
620 * Only permits the user to set a user-owned special port
621 * ID, rejecting a kernel-owned special port ID.
623 * A special kernel port cannot be set up using this
624 * routine; use kernel_set_special_port() instead.
627 host_set_special_port(
628 host_priv_t host_priv
,
632 if (host_priv
== HOST_PRIV_NULL
||
633 id
<= HOST_MAX_SPECIAL_KERNEL_PORT
|| id
> HOST_MAX_SPECIAL_PORT
) {
635 ipc_port_release_send(port
);
636 return KERN_INVALID_ARGUMENT
;
639 return kernel_set_special_port(host_priv
, id
, port
);
644 * User interface for retrieving a special port.
646 * Note that there is nothing to prevent a user special
647 * port from disappearing after it has been discovered by
648 * the caller; thus, using a special port can always result
649 * in a "port not valid" error.
653 host_get_special_port(
654 host_priv_t host_priv
,
661 if (host_priv
== HOST_PRIV_NULL
||
662 id
== HOST_SECURITY_PORT
|| id
> HOST_MAX_SPECIAL_PORT
)
663 return KERN_INVALID_ARGUMENT
;
666 if (node
!= HOST_LOCAL_NODE
)
667 return norma_get_special_port(host_priv
, node
, id
, portp
);
670 host_lock(host_priv
);
671 port
= realhost
.special
[id
];
672 *portp
= ipc_port_copy_send(port
);
673 host_unlock(host_priv
);
682 * Return the IO master access port for this host.
687 io_master_t
*io_masterp
)
689 if (host
== HOST_NULL
)
690 return KERN_INVALID_ARGUMENT
;
692 return (host_get_io_master_port(host_priv_self(), io_masterp
));
708 host_security_self(void)