2 * Copyright (c) 2000 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.
60 #include <mach_host.h>
62 #include <mach/boolean.h>
63 #include <kern/assert.h>
64 #include <kern/kalloc.h>
65 #include <kern/host.h>
66 #include <kern/host_statistics.h>
67 #include <kern/ipc_host.h>
68 #include <kern/misc_protos.h>
69 #include <mach/host_info.h>
70 #include <mach/kern_return.h>
71 #include <mach/machine.h>
72 #include <mach/port.h>
73 #include <kern/processor.h>
74 #include <mach/processor_info.h>
75 #include <mach/vm_param.h>
76 #include <mach/mach_host_server.h>
78 #include <dipc/dipc_funcs.h>
79 #include <dipc/special_ports.h>
82 vm_statistics_data_t vm_stat
[NCPUS
];
88 host_priv_t host_priv
,
89 processor_array_t
*processor_list
,
90 mach_msg_type_number_t
*countp
)
93 register processor_t
*tp
;
97 if (host_priv
== HOST_PRIV_NULL
)
98 return(KERN_INVALID_ARGUMENT
);
100 assert(host_priv
== &realhost
);
103 * Determine how many processors we have.
104 * (This number shouldn't change.)
108 for (i
= 0; i
< NCPUS
; i
++)
109 if (machine_slot
[i
].is_cpu
)
113 panic("host_processors");
115 addr
= kalloc((vm_size_t
) (count
* sizeof(mach_port_t
)));
117 return KERN_RESOURCE_SHORTAGE
;
119 tp
= (processor_t
*) addr
;
120 for (i
= 0; i
< NCPUS
; i
++)
121 if (machine_slot
[i
].is_cpu
)
122 *tp
++ = cpu_to_processor(i
);
125 *processor_list
= (processor_array_t
)addr
;
127 /* do the conversion that Mig should handle */
129 tp
= (processor_t
*) addr
;
130 for (i
= 0; i
< count
; i
++)
131 ((mach_port_t
*) tp
)[i
] =
132 (mach_port_t
)convert_processor_to_port(tp
[i
]);
140 host_flavor_t flavor
,
142 mach_msg_type_number_t
*count
)
145 if (host
== HOST_NULL
)
146 return(KERN_INVALID_ARGUMENT
);
150 case HOST_BASIC_INFO
:
152 register host_basic_info_t basic_info
;
155 * Basic information about this host.
157 if (*count
< HOST_BASIC_INFO_COUNT
)
158 return(KERN_FAILURE
);
160 basic_info
= (host_basic_info_t
) info
;
162 basic_info
->max_cpus
= machine_info
.max_cpus
;
163 basic_info
->avail_cpus
= machine_info
.avail_cpus
;
164 basic_info
->memory_size
= machine_info
.memory_size
;
165 basic_info
->cpu_type
=
166 machine_slot
[master_processor
->slot_num
].cpu_type
;
167 basic_info
->cpu_subtype
=
168 machine_slot
[master_processor
->slot_num
].cpu_subtype
;
170 *count
= HOST_BASIC_INFO_COUNT
;
172 return(KERN_SUCCESS
);
175 case HOST_SCHED_INFO
:
177 register host_sched_info_t sched_info
;
178 extern int tick
; /* XXX */
181 * Return scheduler information.
183 if (*count
< HOST_SCHED_INFO_COUNT
)
184 return(KERN_FAILURE
);
186 sched_info
= (host_sched_info_t
) info
;
188 sched_info
->min_timeout
= tick
/ 1000; /* XXX */
189 sched_info
->min_quantum
= tick
/ 1000; /* XXX */
191 *count
= HOST_SCHED_INFO_COUNT
;
193 return(KERN_SUCCESS
);
196 case HOST_RESOURCE_SIZES
:
199 * Return sizes of kernel data structures
201 if (*count
< HOST_RESOURCE_SIZES_COUNT
)
202 return(KERN_FAILURE
);
204 /* XXX Fail until ledgers are implemented */
205 return(KERN_INVALID_ARGUMENT
);
208 case HOST_PRIORITY_INFO
:
210 register host_priority_info_t priority_info
;
212 if (*count
< HOST_PRIORITY_INFO_COUNT
)
213 return(KERN_FAILURE
);
215 priority_info
= (host_priority_info_t
) info
;
217 priority_info
->kernel_priority
= MINPRI_KERNEL
;
218 priority_info
->system_priority
= MINPRI_KERNEL
;
219 priority_info
->server_priority
= MINPRI_SYSTEM
;
220 priority_info
->user_priority
= BASEPRI_DEFAULT
;
221 priority_info
->depress_priority
= DEPRESSPRI
;
222 priority_info
->idle_priority
= IDLEPRI
;
223 priority_info
->minimum_priority
= MINPRI_STANDARD
;
224 priority_info
->maximum_priority
= MAXPRI_SYSTEM
;
226 *count
= HOST_PRIORITY_INFO_COUNT
;
228 return(KERN_SUCCESS
);
232 * JMM - Temporary check to see if semaphore traps are
233 * supported on this machine. Sadly, just trying to call
234 * the traps gets your process terminated instead of
235 * returning an error, so we have to query during mach_init
236 * to see if the machine supports them.
238 * KERN_INVALID_ARGUMENT - kernel has no semaphore traps
239 * KERN_SUCCESS - kernel has sema traps (up to semaphore_signal_wait)
240 * KERN_SEMAPHORE_DESTROYED - kernel has the latest semaphore traps
242 case HOST_SEMAPHORE_TRAPS
:
249 return(KERN_INVALID_ARGUMENT
);
256 host_flavor_t flavor
,
258 mach_msg_type_number_t
*count
)
261 if (host
== HOST_NULL
)
262 return(KERN_INVALID_HOST
);
266 case HOST_LOAD_INFO
: {
267 register host_load_info_t load_info
;
268 extern integer_t avenrun
[3], mach_factor
[3];
270 if (*count
< HOST_LOAD_INFO_COUNT
)
271 return(KERN_FAILURE
);
273 load_info
= (host_load_info_t
) info
;
275 bcopy((char *) avenrun
,
276 (char *) load_info
->avenrun
,
278 bcopy((char *) mach_factor
,
279 (char *) load_info
->mach_factor
,
282 *count
= HOST_LOAD_INFO_COUNT
;
283 return(KERN_SUCCESS
);
287 register vm_statistics_t stat
;
288 vm_statistics_data_t host_vm_stat
;
289 extern int vm_page_free_count
, vm_page_active_count
,
290 vm_page_inactive_count
, vm_page_wire_count
;
292 if (*count
< HOST_VM_INFO_COUNT
)
293 return(KERN_FAILURE
);
296 host_vm_stat
= *stat
;
301 for (i
= 1; i
< NCPUS
; i
++) {
303 host_vm_stat
.zero_fill_count
+=
304 stat
->zero_fill_count
;
305 host_vm_stat
.reactivations
+=
307 host_vm_stat
.pageins
+= stat
->pageins
;
308 host_vm_stat
.pageouts
+= stat
->pageouts
;
309 host_vm_stat
.faults
+= stat
->faults
;
310 host_vm_stat
.cow_faults
+= stat
->cow_faults
;
311 host_vm_stat
.lookups
+= stat
->lookups
;
312 host_vm_stat
.hits
+= stat
->hits
;
317 stat
= (vm_statistics_t
) info
;
319 stat
->free_count
= vm_page_free_count
;
320 stat
->active_count
= vm_page_active_count
;
321 stat
->inactive_count
= vm_page_inactive_count
;
322 stat
->wire_count
= vm_page_wire_count
;
323 stat
->zero_fill_count
= host_vm_stat
.zero_fill_count
;
324 stat
->reactivations
= host_vm_stat
.reactivations
;
325 stat
->pageins
= host_vm_stat
.pageins
;
326 stat
->pageouts
= host_vm_stat
.pageouts
;
327 stat
->faults
= host_vm_stat
.faults
;
328 stat
->cow_faults
= host_vm_stat
.cow_faults
;
329 stat
->lookups
= host_vm_stat
.lookups
;
330 stat
->hits
= host_vm_stat
.hits
;
332 *count
= HOST_VM_INFO_COUNT
;
333 return(KERN_SUCCESS
);
336 case HOST_CPU_LOAD_INFO
: {
337 host_cpu_load_info_t cpu_load_info
;
338 unsigned long ticks_value1
, ticks_value2
;
341 #define GET_TICKS_VALUE(__cpu,__state) \
344 ticks_value1 = *(volatile integer_t *) \
345 (&machine_slot[(__cpu)].cpu_ticks[(__state)]); \
346 ticks_value2 = *(volatile integer_t *) \
347 (&machine_slot[(__cpu)].cpu_ticks[(__state)]); \
348 } while (ticks_value1 != ticks_value2); \
349 cpu_load_info->cpu_ticks[(__state)] += ticks_value1; \
352 if (*count
< HOST_CPU_LOAD_INFO_COUNT
)
355 cpu_load_info
= (host_cpu_load_info_t
) info
;
357 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = 0;
358 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
359 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
360 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] = 0;
361 for (i
= 0; i
< NCPUS
; i
++) {
362 if (!machine_slot
[i
].is_cpu
||
363 !machine_slot
[i
].running
)
365 GET_TICKS_VALUE(i
, CPU_STATE_USER
);
366 GET_TICKS_VALUE(i
, CPU_STATE_NICE
);
367 GET_TICKS_VALUE(i
, CPU_STATE_SYSTEM
);
368 GET_TICKS_VALUE(i
, CPU_STATE_IDLE
);
371 *count
= HOST_CPU_LOAD_INFO_COUNT
;
376 return(KERN_INVALID_ARGUMENT
);
381 * Get host statistics that require privilege.
382 * None for now, just call the un-privileged version.
385 host_priv_statistics(
386 host_priv_t host_priv
,
387 host_flavor_t flavor
,
389 mach_msg_type_number_t
*count
)
391 return(host_statistics((host_t
)host_priv
, flavor
, info
, count
));
398 vm_size_t
*out_page_size
)
400 if (host
== HOST_NULL
)
401 return(KERN_INVALID_ARGUMENT
);
403 *out_page_size
= PAGE_SIZE
;
405 return(KERN_SUCCESS
);
409 * Return kernel version string (more than you ever
410 * wanted to know about what version of the kernel this is).
416 kernel_version_t out_version
)
418 extern char version
[];
420 if (host
== HOST_NULL
)
421 return(KERN_INVALID_ARGUMENT
);
423 (void) strncpy(out_version
, version
, sizeof(kernel_version_t
));
425 return(KERN_SUCCESS
);
429 * host_processor_sets:
431 * List all processor sets on the host.
435 host_priv_t host_priv
,
436 processor_set_name_array_t
*pset_list
,
437 mach_msg_type_number_t
*count
)
441 if (host_priv
== HOST_PRIV_NULL
)
442 return KERN_INVALID_ARGUMENT
;
445 * Allocate memory. Can be pageable because it won't be
446 * touched while holding a lock.
449 addr
= kalloc((vm_size_t
) sizeof(mach_port_t
));
451 return KERN_RESOURCE_SHORTAGE
;
453 /* take ref for convert_pset_name_to_port */
454 pset_reference(&default_pset
);
455 /* do the conversion that Mig should handle */
456 *((ipc_port_t
*) addr
) = convert_pset_name_to_port(&default_pset
);
458 *pset_list
= (processor_set_array_t
)addr
;
465 * host_processor_set_priv:
467 * Return control port for given processor set.
470 host_processor_set_priv(
471 host_priv_t host_priv
,
472 processor_set_t pset_name
,
473 processor_set_t
*pset
)
475 if ((host_priv
== HOST_PRIV_NULL
) || (pset_name
== PROCESSOR_SET_NULL
)) {
476 *pset
= PROCESSOR_SET_NULL
;
477 return(KERN_INVALID_ARGUMENT
);
481 pset_reference(*pset
);
482 return(KERN_SUCCESS
);
486 * host_processor_info
488 * Return info about the processors on this host. It will return
489 * the number of processors, and the specific type of info requested
495 processor_flavor_t flavor
,
496 natural_t
*proc_count
,
497 processor_info_array_t
*proc_info
,
498 mach_msg_type_number_t
*proc_info_count
)
507 processor_info_t proc_data
;
509 if (host
== HOST_NULL
)
510 return KERN_INVALID_ARGUMENT
;
512 kr
= processor_info_count(flavor
, &count
);
513 if (kr
!= KERN_SUCCESS
) {
517 for (num
= i
= 0; i
< NCPUS
; i
++)
518 if (machine_slot
[i
].is_cpu
)
521 size
= (vm_size_t
)round_page(num
* count
* sizeof(natural_t
));
523 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, TRUE
);
524 if (kr
!= KERN_SUCCESS
)
525 return KERN_RESOURCE_SHORTAGE
;
527 kr
= vm_map_wire(ipc_kernel_map
, addr
, addr
+ size
,
528 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
529 if (kr
!= KERN_SUCCESS
) {
530 kmem_free(ipc_kernel_map
, addr
, size
);
531 return KERN_RESOURCE_SHORTAGE
;
534 proc_data
= (processor_info_t
) addr
;
535 for (i
= 0; i
< NCPUS
; i
++) {
539 if (machine_slot
[i
].is_cpu
) {
540 kr
= processor_info(cpu_to_processor(i
),
545 if (kr
!= KERN_SUCCESS
) {
546 kmem_free(ipc_kernel_map
, addr
, size
);
549 assert(count
== count2
);
554 kr
= vm_map_unwire(ipc_kernel_map
, addr
, addr
+ size
, FALSE
);
555 assert(kr
== KERN_SUCCESS
);
556 size
= (vm_size_t
)(num
* count
* sizeof(natural_t
));
557 kr
= vm_map_copyin(ipc_kernel_map
, addr
, size
, TRUE
, ©
);
558 assert(kr
== KERN_SUCCESS
);
561 *proc_info
= (processor_info_array_t
) copy
;
562 *proc_info_count
= num
* count
;
563 return(KERN_SUCCESS
);
570 * Return the IO master access port for this host.
575 io_master_t
*io_master
)
577 if (host
== HOST_NULL
)
578 return KERN_INVALID_ARGUMENT
;
579 *io_master
= ipc_port_copy_send(realhost
.io_master
);
583 #define io_master_deallocate(x)
588 * Return the IO master access port for this host.
592 host_priv_t host_priv
,
593 io_master_t io_master
)
595 io_master_t old_master
;
597 if (host_priv
== HOST_PRIV_NULL
)
598 return KERN_INVALID_ARGUMENT
;
600 old_master
= realhost
.io_master
;
601 realhost
.io_master
= io_master
;
602 io_master_deallocate(old_master
);
607 * User interface for setting a special port.
609 * Only permits the user to set a user-owned special port
610 * ID, rejecting a kernel-owned special port ID.
612 * A special kernel port cannot be set up using this
613 * routine; use kernel_set_special_port() instead.
616 host_set_special_port(
617 host_priv_t host_priv
,
622 return norma_set_special_port(host_priv
, id
, port
);
630 * User interface for retrieving a special port.
632 * When all processing is local, this call does not block.
633 * If processing goes remote to discover a remote UID,
634 * this call blocks but not indefinitely. If the remote
635 * node does not exist, has panic'ed, or is booting but
636 * hasn't yet turned on DIPC, then we expect the transport
637 * to return an error.
639 * This routine always returns SUCCESS, even if there's
642 * Note that there is nothing to prevent a user special
643 * port from disappearing after it has been discovered by
644 * the caller; thus, using a special port can always result
645 * in a "port not valid" error.
649 host_get_special_port(
650 host_priv_t host_priv
,
656 return norma_get_special_port(host_priv
, node
, id
, portp
);
675 host_security_self(void)