2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
28 * All Rights Reserved.
30 * Permission to use, copy, modify and distribute this software and its
31 * documentation is hereby granted, provided that both the copyright
32 * notice and this permission notice appear in all copies of the
33 * software, derivative works or modified versions, and any portions
34 * thereof, and that both notices appear in supporting documentation.
36 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
37 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
38 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
40 * Carnegie Mellon requests users of this software to return to
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
56 * Non-ipc host functions.
59 #include <mach_host.h>
61 #include <mach/mach_types.h>
62 #include <mach/boolean.h>
63 #include <mach/host_info.h>
64 #include <mach/host_special_ports.h>
65 #include <mach/kern_return.h>
66 #include <mach/machine.h>
67 #include <mach/port.h>
68 #include <mach/processor_info.h>
69 #include <mach/vm_param.h>
70 #include <mach/processor.h>
71 #include <mach/mach_host_server.h>
72 #include <mach/host_priv_server.h>
73 #include <mach/vm_map.h>
75 #include <kern/kern_types.h>
76 #include <kern/assert.h>
77 #include <kern/kalloc.h>
78 #include <kern/host.h>
79 #include <kern/host_statistics.h>
80 #include <kern/ipc_host.h>
81 #include <kern/misc_protos.h>
82 #include <kern/sched.h>
83 #include <kern/processor.h>
85 #include <vm/vm_map.h>
88 #include <dipc/dipc_funcs.h>
89 #include <dipc/special_ports.h>
96 host_priv_t host_priv
,
97 processor_array_t
*out_array
,
98 mach_msg_type_number_t
*countp
)
100 register processor_t processor
, *tp
;
102 unsigned int count
, i
;
104 if (host_priv
== HOST_PRIV_NULL
)
105 return (KERN_INVALID_ARGUMENT
);
107 assert(host_priv
== &realhost
);
109 count
= processor_count
;
112 addr
= kalloc((vm_size_t
) (count
* sizeof(mach_port_t
)));
114 return (KERN_RESOURCE_SHORTAGE
);
116 tp
= (processor_t
*) addr
;
117 *tp
++ = processor
= processor_list
;
120 simple_lock(&processor_list_lock
);
122 for (i
= 1; i
< count
; i
++)
123 *tp
++ = processor
= processor
->processor_list
;
125 simple_unlock(&processor_list_lock
);
129 *out_array
= (processor_array_t
)addr
;
131 /* do the conversion that Mig should handle */
133 tp
= (processor_t
*) addr
;
134 for (i
= 0; i
< count
; i
++)
135 ((mach_port_t
*) tp
)[i
] =
136 (mach_port_t
)convert_processor_to_port(tp
[i
]);
138 return (KERN_SUCCESS
);
144 host_flavor_t flavor
,
146 mach_msg_type_number_t
*count
)
149 if (host
== HOST_NULL
)
150 return (KERN_INVALID_ARGUMENT
);
154 case HOST_BASIC_INFO
:
156 register host_basic_info_t basic_info
;
157 register int master_slot
;
160 * Basic information about this host.
162 if (*count
< HOST_BASIC_INFO_OLD_COUNT
)
163 return (KERN_FAILURE
);
165 basic_info
= (host_basic_info_t
) info
;
167 basic_info
->max_cpus
= machine_info
.max_cpus
;
168 basic_info
->avail_cpus
= machine_info
.avail_cpus
;
169 basic_info
->memory_size
= machine_info
.memory_size
;
170 master_slot
= PROCESSOR_DATA(master_processor
, slot_num
);
171 basic_info
->cpu_type
= slot_type(master_slot
);
172 basic_info
->cpu_subtype
= slot_subtype(master_slot
);
174 if (*count
>= HOST_BASIC_INFO_COUNT
) {
175 basic_info
->cpu_threadtype
= slot_threadtype(master_slot
);
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
;
182 *count
= HOST_BASIC_INFO_COUNT
;
184 *count
= HOST_BASIC_INFO_OLD_COUNT
;
187 return (KERN_SUCCESS
);
190 case HOST_SCHED_INFO
:
192 register host_sched_info_t sched_info
;
195 * Return scheduler information.
197 if (*count
< HOST_SCHED_INFO_COUNT
)
198 return (KERN_FAILURE
);
200 sched_info
= (host_sched_info_t
) info
;
202 sched_info
->min_timeout
=
203 sched_info
->min_quantum
= std_quantum_us
/ 1000;
205 *count
= HOST_SCHED_INFO_COUNT
;
207 return (KERN_SUCCESS
);
210 case HOST_RESOURCE_SIZES
:
213 * Return sizes of kernel data structures
215 if (*count
< HOST_RESOURCE_SIZES_COUNT
)
216 return (KERN_FAILURE
);
218 /* XXX Fail until ledgers are implemented */
219 return (KERN_INVALID_ARGUMENT
);
222 case HOST_PRIORITY_INFO
:
224 register host_priority_info_t priority_info
;
226 if (*count
< HOST_PRIORITY_INFO_COUNT
)
227 return (KERN_FAILURE
);
229 priority_info
= (host_priority_info_t
) info
;
231 priority_info
->kernel_priority
= MINPRI_KERNEL
;
232 priority_info
->system_priority
= MINPRI_KERNEL
;
233 priority_info
->server_priority
= MINPRI_RESERVED
;
234 priority_info
->user_priority
= BASEPRI_DEFAULT
;
235 priority_info
->depress_priority
= DEPRESSPRI
;
236 priority_info
->idle_priority
= IDLEPRI
;
237 priority_info
->minimum_priority
= MINPRI_USER
;
238 priority_info
->maximum_priority
= MAXPRI_RESERVED
;
240 *count
= HOST_PRIORITY_INFO_COUNT
;
242 return (KERN_SUCCESS
);
246 * Gestalt for various trap facilities.
248 case HOST_MACH_MSG_TRAP
:
249 case HOST_SEMAPHORE_TRAPS
:
252 return (KERN_SUCCESS
);
256 return (KERN_INVALID_ARGUMENT
);
263 host_flavor_t flavor
,
265 mach_msg_type_number_t
*count
)
268 if (host
== HOST_NULL
)
269 return (KERN_INVALID_HOST
);
275 host_load_info_t load_info
;
277 if (*count
< HOST_LOAD_INFO_COUNT
)
278 return (KERN_FAILURE
);
280 load_info
= (host_load_info_t
) info
;
282 bcopy((char *) avenrun
,
283 (char *) load_info
->avenrun
, sizeof avenrun
);
284 bcopy((char *) mach_factor
,
285 (char *) load_info
->mach_factor
, sizeof mach_factor
);
287 *count
= HOST_LOAD_INFO_COUNT
;
288 return (KERN_SUCCESS
);
293 register processor_t processor
;
294 register vm_statistics_t stat
;
295 vm_statistics_data_t host_vm_stat
;
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
;
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
;
338 if (*count
>= HOST_VM_INFO_COUNT
) {
339 /* info that was not in revision 0 of that interface */
340 stat
->purgeable_count
= vm_page_purgeable_count
;
341 stat
->purges
= vm_page_purged_count
;
342 *count
= HOST_VM_INFO_COUNT
;
344 *count
= HOST_VM_INFO_REV0_COUNT
;
347 return (KERN_SUCCESS
);
350 case HOST_CPU_LOAD_INFO
:
352 register processor_t processor
;
353 host_cpu_load_info_t cpu_load_info
;
354 unsigned long ticks_value1
, ticks_value2
;
356 if (*count
< HOST_CPU_LOAD_INFO_COUNT
)
357 return (KERN_FAILURE
);
359 #define GET_TICKS_VALUE(processor, state) \
362 ticks_value1 = *(volatile integer_t *) \
363 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
364 ticks_value2 = *(volatile integer_t *) \
365 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
366 } while (ticks_value1 != ticks_value2); \
368 cpu_load_info->cpu_ticks[(state)] += ticks_value1; \
371 cpu_load_info
= (host_cpu_load_info_t
)info
;
372 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = 0;
373 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
374 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
375 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] = 0;
377 processor
= processor_list
;
378 GET_TICKS_VALUE(processor
, CPU_STATE_USER
);
379 GET_TICKS_VALUE(processor
, CPU_STATE_NICE
);
380 GET_TICKS_VALUE(processor
, CPU_STATE_SYSTEM
);
381 GET_TICKS_VALUE(processor
, CPU_STATE_IDLE
);
383 if (processor_count
> 1) {
384 simple_lock(&processor_list_lock
);
386 while ((processor
= processor
->processor_list
) != NULL
) {
387 GET_TICKS_VALUE(processor
, CPU_STATE_USER
);
388 GET_TICKS_VALUE(processor
, CPU_STATE_NICE
);
389 GET_TICKS_VALUE(processor
, CPU_STATE_SYSTEM
);
390 GET_TICKS_VALUE(processor
, CPU_STATE_IDLE
);
393 simple_unlock(&processor_list_lock
);
396 *count
= HOST_CPU_LOAD_INFO_COUNT
;
398 return (KERN_SUCCESS
);
402 return (KERN_INVALID_ARGUMENT
);
407 * Get host statistics that require privilege.
408 * None for now, just call the un-privileged version.
411 host_priv_statistics(
412 host_priv_t host_priv
,
413 host_flavor_t flavor
,
415 mach_msg_type_number_t
*count
)
417 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 /* take ref for convert_pset_name_to_port */
480 pset_reference(&default_pset
);
481 /* do the conversion that Mig should handle */
482 *((ipc_port_t
*) addr
) = convert_pset_name_to_port(&default_pset
);
484 *pset_list
= (processor_set_array_t
)addr
;
491 * host_processor_set_priv:
493 * Return control port for given processor set.
496 host_processor_set_priv(
497 host_priv_t host_priv
,
498 processor_set_t pset_name
,
499 processor_set_t
*pset
)
501 if ((host_priv
== HOST_PRIV_NULL
) || (pset_name
== PROCESSOR_SET_NULL
)) {
502 *pset
= PROCESSOR_SET_NULL
;
503 return(KERN_INVALID_ARGUMENT
);
507 pset_reference(*pset
);
508 return(KERN_SUCCESS
);
512 * host_processor_info
514 * Return info about the processors on this host. It will return
515 * the number of processors, and the specific type of info requested
521 processor_flavor_t flavor
,
522 natural_t
*out_pcount
,
523 processor_info_array_t
*out_array
,
524 mach_msg_type_number_t
*out_array_count
)
526 kern_return_t result
;
527 processor_t processor
;
529 processor_info_t info
;
530 unsigned int icount
, tcount
;
531 unsigned int pcount
, i
;
533 vm_size_t size
, needed
;
536 if (host
== HOST_NULL
)
537 return (KERN_INVALID_ARGUMENT
);
539 result
= processor_info_count(flavor
, &icount
);
540 if (result
!= KERN_SUCCESS
)
543 pcount
= processor_count
;
546 needed
= pcount
* icount
* sizeof(natural_t
);
547 size
= round_page(needed
);
548 result
= kmem_alloc(ipc_kernel_map
, &addr
, size
);
549 if (result
!= KERN_SUCCESS
)
550 return (KERN_RESOURCE_SHORTAGE
);
552 info
= (processor_info_t
) addr
;
553 processor
= processor_list
;
556 result
= processor_info(processor
, flavor
, &thost
, info
, &tcount
);
557 if (result
!= KERN_SUCCESS
) {
558 kmem_free(ipc_kernel_map
, addr
, size
);
563 for (i
= 1; i
< pcount
; i
++) {
564 simple_lock(&processor_list_lock
);
565 processor
= processor
->processor_list
;
566 simple_unlock(&processor_list_lock
);
570 result
= processor_info(processor
, flavor
, &thost
, info
, &tcount
);
571 if (result
!= KERN_SUCCESS
) {
572 kmem_free(ipc_kernel_map
, addr
, size
);
579 bzero((char *) addr
+ needed
, size
- needed
);
581 result
= vm_map_unwire(ipc_kernel_map
, vm_map_trunc_page(addr
),
582 vm_map_round_page(addr
+ size
), FALSE
);
583 assert(result
== KERN_SUCCESS
);
584 result
= vm_map_copyin(ipc_kernel_map
, (vm_map_address_t
)addr
,
585 (vm_map_size_t
)size
, TRUE
, ©
);
586 assert(result
== KERN_SUCCESS
);
588 *out_pcount
= pcount
;
589 *out_array
= (processor_info_array_t
) copy
;
590 *out_array_count
= pcount
* icount
;
592 return (KERN_SUCCESS
);
596 * Kernel interface for setting a special port.
599 kernel_set_special_port(
600 host_priv_t host_priv
,
606 host_lock(host_priv
);
607 old_port
= host_priv
->special
[id
];
608 host_priv
->special
[id
] = port
;
609 host_unlock(host_priv
);
610 if (IP_VALID(old_port
))
611 ipc_port_release_send(old_port
);
616 * User interface for setting a special port.
618 * Only permits the user to set a user-owned special port
619 * ID, rejecting a kernel-owned special port ID.
621 * A special kernel port cannot be set up using this
622 * routine; use kernel_set_special_port() instead.
625 host_set_special_port(
626 host_priv_t host_priv
,
630 if (host_priv
== HOST_PRIV_NULL
||
631 id
<= HOST_MAX_SPECIAL_KERNEL_PORT
|| id
> HOST_MAX_SPECIAL_PORT
) {
633 ipc_port_release_send(port
);
634 return KERN_INVALID_ARGUMENT
;
637 return kernel_set_special_port(host_priv
, id
, port
);
642 * User interface for retrieving a special port.
644 * Note that there is nothing to prevent a user special
645 * port from disappearing after it has been discovered by
646 * the caller; thus, using a special port can always result
647 * in a "port not valid" error.
651 host_get_special_port(
652 host_priv_t host_priv
,
659 if (host_priv
== HOST_PRIV_NULL
||
660 id
== HOST_SECURITY_PORT
|| id
> HOST_MAX_SPECIAL_PORT
)
661 return KERN_INVALID_ARGUMENT
;
664 if (node
!= HOST_LOCAL_NODE
)
665 return norma_get_special_port(host_priv
, node
, id
, portp
);
668 host_lock(host_priv
);
669 port
= realhost
.special
[id
];
670 *portp
= ipc_port_copy_send(port
);
671 host_unlock(host_priv
);
680 * Return the IO master access port for this host.
685 io_master_t
*io_masterp
)
687 if (host
== HOST_NULL
)
688 return KERN_INVALID_ARGUMENT
;
690 return (host_get_io_master_port(host_priv_self(), io_masterp
));
706 host_security_self(void)