2 * Copyright (c) 2000-2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
29 * Mach Operating System
30 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
31 * All Rights Reserved.
33 * Permission to use, copy, modify and distribute this software and its
34 * documentation is hereby granted, provided that both the copyright
35 * notice and this permission notice appear in all copies of the
36 * software, derivative works or modified versions, and any portions
37 * thereof, and that both notices appear in supporting documentation.
39 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
40 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
41 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
43 * Carnegie Mellon requests users of this software to return to
45 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
46 * School of Computer Science
47 * Carnegie Mellon University
48 * Pittsburgh PA 15213-3890
50 * any improvements or extensions that they make and grant Carnegie Mellon
51 * the rights to redistribute these changes.
59 * Non-ipc host functions.
63 #include <mach_host.h>
65 #include <mach/boolean.h>
66 #include <kern/assert.h>
67 #include <kern/kalloc.h>
68 #include <kern/host.h>
69 #include <kern/host_statistics.h>
70 #include <kern/ipc_host.h>
71 #include <kern/misc_protos.h>
72 #include <mach/host_info.h>
73 #include <mach/host_special_ports.h>
74 #include <mach/kern_return.h>
75 #include <mach/machine.h>
76 #include <mach/port.h>
77 #include <kern/processor.h>
78 #include <mach/processor_info.h>
79 #include <mach/vm_param.h>
80 #include <mach/mach_host_server.h>
82 #include <dipc/dipc_funcs.h>
83 #include <dipc/special_ports.h>
86 vm_statistics_data_t vm_stat
[NCPUS
];
92 host_priv_t host_priv
,
93 processor_array_t
*processor_list
,
94 mach_msg_type_number_t
*countp
)
97 register processor_t
*tp
;
101 if (host_priv
== HOST_PRIV_NULL
)
102 return(KERN_INVALID_ARGUMENT
);
104 assert(host_priv
== &realhost
);
107 * Determine how many processors we have.
108 * (This number shouldn't change.)
112 for (i
= 0; i
< NCPUS
; i
++)
113 if (machine_slot
[i
].is_cpu
)
117 panic("host_processors");
119 addr
= kalloc((vm_size_t
) (count
* sizeof(mach_port_t
)));
121 return KERN_RESOURCE_SHORTAGE
;
123 tp
= (processor_t
*) addr
;
124 for (i
= 0; i
< NCPUS
; i
++)
125 if (machine_slot
[i
].is_cpu
)
126 *tp
++ = cpu_to_processor(i
);
129 *processor_list
= (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
]);
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
;
159 * Basic information about this host.
161 if (*count
< HOST_BASIC_INFO_COUNT
)
162 return(KERN_FAILURE
);
164 basic_info
= (host_basic_info_t
) info
;
166 basic_info
->max_cpus
= machine_info
.max_cpus
;
167 basic_info
->avail_cpus
= machine_info
.avail_cpus
;
168 basic_info
->memory_size
= machine_info
.memory_size
;
169 basic_info
->cpu_type
=
170 machine_slot
[master_processor
->slot_num
].cpu_type
;
171 basic_info
->cpu_subtype
=
172 machine_slot
[master_processor
->slot_num
].cpu_subtype
;
174 *count
= HOST_BASIC_INFO_COUNT
;
176 return(KERN_SUCCESS
);
179 case HOST_SCHED_INFO
:
181 register host_sched_info_t sched_info
;
182 extern int tick
; /* XXX */
185 * Return scheduler information.
187 if (*count
< HOST_SCHED_INFO_COUNT
)
188 return(KERN_FAILURE
);
190 sched_info
= (host_sched_info_t
) info
;
192 sched_info
->min_timeout
= tick
/ 1000; /* XXX */
193 sched_info
->min_quantum
= tick
/ 1000; /* XXX */
195 *count
= HOST_SCHED_INFO_COUNT
;
197 return(KERN_SUCCESS
);
200 case HOST_RESOURCE_SIZES
:
203 * Return sizes of kernel data structures
205 if (*count
< HOST_RESOURCE_SIZES_COUNT
)
206 return(KERN_FAILURE
);
208 /* XXX Fail until ledgers are implemented */
209 return(KERN_INVALID_ARGUMENT
);
212 case HOST_PRIORITY_INFO
:
214 register host_priority_info_t priority_info
;
216 if (*count
< HOST_PRIORITY_INFO_COUNT
)
217 return(KERN_FAILURE
);
219 priority_info
= (host_priority_info_t
) info
;
221 priority_info
->kernel_priority
= MINPRI_KERNEL
;
222 priority_info
->system_priority
= MINPRI_KERNEL
;
223 priority_info
->server_priority
= MINPRI_SYSTEM
;
224 priority_info
->user_priority
= BASEPRI_DEFAULT
;
225 priority_info
->depress_priority
= DEPRESSPRI
;
226 priority_info
->idle_priority
= IDLEPRI
;
227 priority_info
->minimum_priority
= MINPRI_STANDARD
;
228 priority_info
->maximum_priority
= MAXPRI_SYSTEM
;
230 *count
= HOST_PRIORITY_INFO_COUNT
;
232 return(KERN_SUCCESS
);
236 * Gestalt for various trap facilities.
238 case HOST_MACH_MSG_TRAP
:
239 case HOST_SEMAPHORE_TRAPS
:
246 return(KERN_INVALID_ARGUMENT
);
253 host_flavor_t flavor
,
255 mach_msg_type_number_t
*count
)
258 if (host
== HOST_NULL
)
259 return(KERN_INVALID_HOST
);
263 case HOST_LOAD_INFO
: {
264 register host_load_info_t load_info
;
265 extern uint32_t avenrun
[3], mach_factor
[3];
267 if (*count
< HOST_LOAD_INFO_COUNT
)
268 return(KERN_FAILURE
);
270 load_info
= (host_load_info_t
) info
;
272 bcopy((char *) avenrun
,
273 (char *) load_info
->avenrun
,
275 bcopy((char *) mach_factor
,
276 (char *) load_info
->mach_factor
,
279 *count
= HOST_LOAD_INFO_COUNT
;
280 return(KERN_SUCCESS
);
284 register vm_statistics_t stat
;
285 vm_statistics_data_t host_vm_stat
;
286 extern int vm_page_free_count
, vm_page_active_count
,
287 vm_page_inactive_count
, vm_page_wire_count
;
289 if (*count
< HOST_VM_INFO_COUNT
)
290 return(KERN_FAILURE
);
293 host_vm_stat
= *stat
;
298 for (i
= 1; i
< NCPUS
; i
++) {
300 host_vm_stat
.zero_fill_count
+=
301 stat
->zero_fill_count
;
302 host_vm_stat
.reactivations
+=
304 host_vm_stat
.pageins
+= stat
->pageins
;
305 host_vm_stat
.pageouts
+= stat
->pageouts
;
306 host_vm_stat
.faults
+= stat
->faults
;
307 host_vm_stat
.cow_faults
+= stat
->cow_faults
;
308 host_vm_stat
.lookups
+= stat
->lookups
;
309 host_vm_stat
.hits
+= stat
->hits
;
314 stat
= (vm_statistics_t
) info
;
316 stat
->free_count
= vm_page_free_count
;
317 stat
->active_count
= vm_page_active_count
;
318 stat
->inactive_count
= vm_page_inactive_count
;
319 stat
->wire_count
= vm_page_wire_count
;
320 stat
->zero_fill_count
= host_vm_stat
.zero_fill_count
;
321 stat
->reactivations
= host_vm_stat
.reactivations
;
322 stat
->pageins
= host_vm_stat
.pageins
;
323 stat
->pageouts
= host_vm_stat
.pageouts
;
324 stat
->faults
= host_vm_stat
.faults
;
325 stat
->cow_faults
= host_vm_stat
.cow_faults
;
326 stat
->lookups
= host_vm_stat
.lookups
;
327 stat
->hits
= host_vm_stat
.hits
;
329 *count
= HOST_VM_INFO_COUNT
;
330 return(KERN_SUCCESS
);
333 case HOST_CPU_LOAD_INFO
: {
334 host_cpu_load_info_t cpu_load_info
;
335 unsigned long ticks_value1
, ticks_value2
;
338 #define GET_TICKS_VALUE(__cpu,__state) \
341 ticks_value1 = *(volatile integer_t *) \
342 (&machine_slot[(__cpu)].cpu_ticks[(__state)]); \
343 ticks_value2 = *(volatile integer_t *) \
344 (&machine_slot[(__cpu)].cpu_ticks[(__state)]); \
345 } while (ticks_value1 != ticks_value2); \
346 cpu_load_info->cpu_ticks[(__state)] += ticks_value1; \
349 if (*count
< HOST_CPU_LOAD_INFO_COUNT
)
352 cpu_load_info
= (host_cpu_load_info_t
) info
;
354 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = 0;
355 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
356 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
357 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] = 0;
358 for (i
= 0; i
< NCPUS
; i
++) {
359 if (!machine_slot
[i
].is_cpu
||
360 !machine_slot
[i
].running
)
362 GET_TICKS_VALUE(i
, CPU_STATE_USER
);
363 GET_TICKS_VALUE(i
, CPU_STATE_NICE
);
364 GET_TICKS_VALUE(i
, CPU_STATE_SYSTEM
);
365 GET_TICKS_VALUE(i
, CPU_STATE_IDLE
);
368 *count
= HOST_CPU_LOAD_INFO_COUNT
;
373 return(KERN_INVALID_ARGUMENT
);
378 * Get host statistics that require privilege.
379 * None for now, just call the un-privileged version.
382 host_priv_statistics(
383 host_priv_t host_priv
,
384 host_flavor_t flavor
,
386 mach_msg_type_number_t
*count
)
388 return(host_statistics((host_t
)host_priv
, flavor
, info
, count
));
395 vm_size_t
*out_page_size
)
397 if (host
== HOST_NULL
)
398 return(KERN_INVALID_ARGUMENT
);
400 *out_page_size
= PAGE_SIZE
;
402 return(KERN_SUCCESS
);
406 * Return kernel version string (more than you ever
407 * wanted to know about what version of the kernel this is).
413 kernel_version_t out_version
)
415 extern char version
[];
417 if (host
== HOST_NULL
)
418 return(KERN_INVALID_ARGUMENT
);
420 (void) strncpy(out_version
, version
, sizeof(kernel_version_t
));
422 return(KERN_SUCCESS
);
426 * host_processor_sets:
428 * List all processor sets on the host.
432 host_priv_t host_priv
,
433 processor_set_name_array_t
*pset_list
,
434 mach_msg_type_number_t
*count
)
438 if (host_priv
== HOST_PRIV_NULL
)
439 return KERN_INVALID_ARGUMENT
;
442 * Allocate memory. Can be pageable because it won't be
443 * touched while holding a lock.
446 addr
= kalloc((vm_size_t
) sizeof(mach_port_t
));
448 return KERN_RESOURCE_SHORTAGE
;
450 /* take ref for convert_pset_name_to_port */
451 pset_reference(&default_pset
);
452 /* do the conversion that Mig should handle */
453 *((ipc_port_t
*) addr
) = convert_pset_name_to_port(&default_pset
);
455 *pset_list
= (processor_set_array_t
)addr
;
462 * host_processor_set_priv:
464 * Return control port for given processor set.
467 host_processor_set_priv(
468 host_priv_t host_priv
,
469 processor_set_t pset_name
,
470 processor_set_t
*pset
)
472 if ((host_priv
== HOST_PRIV_NULL
) || (pset_name
== PROCESSOR_SET_NULL
)) {
473 *pset
= PROCESSOR_SET_NULL
;
474 return(KERN_INVALID_ARGUMENT
);
478 pset_reference(*pset
);
479 return(KERN_SUCCESS
);
483 * host_processor_info
485 * Return info about the processors on this host. It will return
486 * the number of processors, and the specific type of info requested
492 processor_flavor_t flavor
,
493 natural_t
*proc_count
,
494 processor_info_array_t
*proc_info
,
495 mach_msg_type_number_t
*proc_info_count
)
504 processor_info_t proc_data
;
506 if (host
== HOST_NULL
)
507 return KERN_INVALID_ARGUMENT
;
509 kr
= processor_info_count(flavor
, &count
);
510 if (kr
!= KERN_SUCCESS
) {
514 for (num
= i
= 0; i
< NCPUS
; i
++)
515 if (machine_slot
[i
].is_cpu
)
518 size
= (vm_size_t
)round_page_32(num
* count
* sizeof(natural_t
));
520 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, TRUE
);
521 if (kr
!= KERN_SUCCESS
)
522 return KERN_RESOURCE_SHORTAGE
;
524 kr
= vm_map_wire(ipc_kernel_map
, addr
, addr
+ size
,
525 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
526 if (kr
!= KERN_SUCCESS
) {
527 kmem_free(ipc_kernel_map
, addr
, size
);
528 return KERN_RESOURCE_SHORTAGE
;
531 proc_data
= (processor_info_t
) addr
;
532 for (i
= 0; i
< NCPUS
; i
++) {
536 if (machine_slot
[i
].is_cpu
) {
537 kr
= processor_info(cpu_to_processor(i
),
542 if (kr
!= KERN_SUCCESS
) {
543 kmem_free(ipc_kernel_map
, addr
, size
);
546 assert(count
== count2
);
551 kr
= vm_map_unwire(ipc_kernel_map
, addr
, addr
+ size
, FALSE
);
552 assert(kr
== KERN_SUCCESS
);
553 size
= (vm_size_t
)(num
* count
* sizeof(natural_t
));
554 kr
= vm_map_copyin(ipc_kernel_map
, addr
, size
, TRUE
, ©
);
555 assert(kr
== KERN_SUCCESS
);
558 *proc_info
= (processor_info_array_t
) copy
;
559 *proc_info_count
= num
* count
;
560 return(KERN_SUCCESS
);
564 * Kernel interface for setting a special port.
567 kernel_set_special_port(
568 host_priv_t host_priv
,
574 host_lock(host_priv
);
575 old_port
= host_priv
->special
[id
];
576 host_priv
->special
[id
] = port
;
577 host_unlock(host_priv
);
578 if (IP_VALID(old_port
))
579 ipc_port_release_send(old_port
);
584 * User interface for setting a special port.
586 * Only permits the user to set a user-owned special port
587 * ID, rejecting a kernel-owned special port ID.
589 * A special kernel port cannot be set up using this
590 * routine; use kernel_set_special_port() instead.
593 host_set_special_port(
594 host_priv_t host_priv
,
598 if (host_priv
== HOST_PRIV_NULL
||
599 id
<= HOST_MAX_SPECIAL_KERNEL_PORT
|| id
> HOST_MAX_SPECIAL_PORT
) {
601 ipc_port_release_send(port
);
602 return KERN_INVALID_ARGUMENT
;
605 return kernel_set_special_port(host_priv
, id
, port
);
610 * User interface for retrieving a special port.
612 * Note that there is nothing to prevent a user special
613 * port from disappearing after it has been discovered by
614 * the caller; thus, using a special port can always result
615 * in a "port not valid" error.
619 host_get_special_port(
620 host_priv_t host_priv
,
627 if (host_priv
== HOST_PRIV_NULL
||
628 id
== HOST_SECURITY_PORT
)
629 return KERN_INVALID_ARGUMENT
;
632 if (node
!= HOST_LOCAL_NODE
)
633 return norma_get_special_port(host_priv
, node
, id
, portp
);
636 host_lock(host_priv
);
637 port
= realhost
.special
[id
];
638 *portp
= ipc_port_copy_send(port
);
639 host_unlock(host_priv
);
648 * Return the IO master access port for this host.
653 io_master_t
*io_masterp
)
655 if (host
== HOST_NULL
)
656 return KERN_INVALID_ARGUMENT
;
658 return (host_get_io_master_port(host_priv_self(), io_masterp
));
674 host_security_self(void)