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 * Gestalt for various trap facilities.
234 case HOST_MACH_MSG_TRAP
:
235 case HOST_SEMAPHORE_TRAPS
:
242 return(KERN_INVALID_ARGUMENT
);
249 host_flavor_t flavor
,
251 mach_msg_type_number_t
*count
)
254 if (host
== HOST_NULL
)
255 return(KERN_INVALID_HOST
);
259 case HOST_LOAD_INFO
: {
260 register host_load_info_t load_info
;
261 extern uint32_t avenrun
[3], mach_factor
[3];
263 if (*count
< HOST_LOAD_INFO_COUNT
)
264 return(KERN_FAILURE
);
266 load_info
= (host_load_info_t
) info
;
268 bcopy((char *) avenrun
,
269 (char *) load_info
->avenrun
,
271 bcopy((char *) mach_factor
,
272 (char *) load_info
->mach_factor
,
275 *count
= HOST_LOAD_INFO_COUNT
;
276 return(KERN_SUCCESS
);
280 register vm_statistics_t stat
;
281 vm_statistics_data_t host_vm_stat
;
282 extern int vm_page_free_count
, vm_page_active_count
,
283 vm_page_inactive_count
, vm_page_wire_count
;
285 if (*count
< HOST_VM_INFO_COUNT
)
286 return(KERN_FAILURE
);
289 host_vm_stat
= *stat
;
294 for (i
= 1; i
< NCPUS
; i
++) {
296 host_vm_stat
.zero_fill_count
+=
297 stat
->zero_fill_count
;
298 host_vm_stat
.reactivations
+=
300 host_vm_stat
.pageins
+= stat
->pageins
;
301 host_vm_stat
.pageouts
+= stat
->pageouts
;
302 host_vm_stat
.faults
+= stat
->faults
;
303 host_vm_stat
.cow_faults
+= stat
->cow_faults
;
304 host_vm_stat
.lookups
+= stat
->lookups
;
305 host_vm_stat
.hits
+= stat
->hits
;
310 stat
= (vm_statistics_t
) info
;
312 stat
->free_count
= vm_page_free_count
;
313 stat
->active_count
= vm_page_active_count
;
314 stat
->inactive_count
= vm_page_inactive_count
;
315 stat
->wire_count
= vm_page_wire_count
;
316 stat
->zero_fill_count
= host_vm_stat
.zero_fill_count
;
317 stat
->reactivations
= host_vm_stat
.reactivations
;
318 stat
->pageins
= host_vm_stat
.pageins
;
319 stat
->pageouts
= host_vm_stat
.pageouts
;
320 stat
->faults
= host_vm_stat
.faults
;
321 stat
->cow_faults
= host_vm_stat
.cow_faults
;
322 stat
->lookups
= host_vm_stat
.lookups
;
323 stat
->hits
= host_vm_stat
.hits
;
325 *count
= HOST_VM_INFO_COUNT
;
326 return(KERN_SUCCESS
);
329 case HOST_CPU_LOAD_INFO
: {
330 host_cpu_load_info_t cpu_load_info
;
331 unsigned long ticks_value1
, ticks_value2
;
334 #define GET_TICKS_VALUE(__cpu,__state) \
337 ticks_value1 = *(volatile integer_t *) \
338 (&machine_slot[(__cpu)].cpu_ticks[(__state)]); \
339 ticks_value2 = *(volatile integer_t *) \
340 (&machine_slot[(__cpu)].cpu_ticks[(__state)]); \
341 } while (ticks_value1 != ticks_value2); \
342 cpu_load_info->cpu_ticks[(__state)] += ticks_value1; \
345 if (*count
< HOST_CPU_LOAD_INFO_COUNT
)
348 cpu_load_info
= (host_cpu_load_info_t
) info
;
350 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = 0;
351 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
352 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
353 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] = 0;
354 for (i
= 0; i
< NCPUS
; i
++) {
355 if (!machine_slot
[i
].is_cpu
||
356 !machine_slot
[i
].running
)
358 GET_TICKS_VALUE(i
, CPU_STATE_USER
);
359 GET_TICKS_VALUE(i
, CPU_STATE_NICE
);
360 GET_TICKS_VALUE(i
, CPU_STATE_SYSTEM
);
361 GET_TICKS_VALUE(i
, CPU_STATE_IDLE
);
364 *count
= HOST_CPU_LOAD_INFO_COUNT
;
369 return(KERN_INVALID_ARGUMENT
);
374 * Get host statistics that require privilege.
375 * None for now, just call the un-privileged version.
378 host_priv_statistics(
379 host_priv_t host_priv
,
380 host_flavor_t flavor
,
382 mach_msg_type_number_t
*count
)
384 return(host_statistics((host_t
)host_priv
, flavor
, info
, count
));
391 vm_size_t
*out_page_size
)
393 if (host
== HOST_NULL
)
394 return(KERN_INVALID_ARGUMENT
);
396 *out_page_size
= PAGE_SIZE
;
398 return(KERN_SUCCESS
);
402 * Return kernel version string (more than you ever
403 * wanted to know about what version of the kernel this is).
409 kernel_version_t out_version
)
411 extern char version
[];
413 if (host
== HOST_NULL
)
414 return(KERN_INVALID_ARGUMENT
);
416 (void) strncpy(out_version
, version
, sizeof(kernel_version_t
));
418 return(KERN_SUCCESS
);
422 * host_processor_sets:
424 * List all processor sets on the host.
428 host_priv_t host_priv
,
429 processor_set_name_array_t
*pset_list
,
430 mach_msg_type_number_t
*count
)
434 if (host_priv
== HOST_PRIV_NULL
)
435 return KERN_INVALID_ARGUMENT
;
438 * Allocate memory. Can be pageable because it won't be
439 * touched while holding a lock.
442 addr
= kalloc((vm_size_t
) sizeof(mach_port_t
));
444 return KERN_RESOURCE_SHORTAGE
;
446 /* take ref for convert_pset_name_to_port */
447 pset_reference(&default_pset
);
448 /* do the conversion that Mig should handle */
449 *((ipc_port_t
*) addr
) = convert_pset_name_to_port(&default_pset
);
451 *pset_list
= (processor_set_array_t
)addr
;
458 * host_processor_set_priv:
460 * Return control port for given processor set.
463 host_processor_set_priv(
464 host_priv_t host_priv
,
465 processor_set_t pset_name
,
466 processor_set_t
*pset
)
468 if ((host_priv
== HOST_PRIV_NULL
) || (pset_name
== PROCESSOR_SET_NULL
)) {
469 *pset
= PROCESSOR_SET_NULL
;
470 return(KERN_INVALID_ARGUMENT
);
474 pset_reference(*pset
);
475 return(KERN_SUCCESS
);
479 * host_processor_info
481 * Return info about the processors on this host. It will return
482 * the number of processors, and the specific type of info requested
488 processor_flavor_t flavor
,
489 natural_t
*proc_count
,
490 processor_info_array_t
*proc_info
,
491 mach_msg_type_number_t
*proc_info_count
)
500 processor_info_t proc_data
;
502 if (host
== HOST_NULL
)
503 return KERN_INVALID_ARGUMENT
;
505 kr
= processor_info_count(flavor
, &count
);
506 if (kr
!= KERN_SUCCESS
) {
510 for (num
= i
= 0; i
< NCPUS
; i
++)
511 if (machine_slot
[i
].is_cpu
)
514 size
= (vm_size_t
)round_page(num
* count
* sizeof(natural_t
));
516 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, TRUE
);
517 if (kr
!= KERN_SUCCESS
)
518 return KERN_RESOURCE_SHORTAGE
;
520 kr
= vm_map_wire(ipc_kernel_map
, addr
, addr
+ size
,
521 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
522 if (kr
!= KERN_SUCCESS
) {
523 kmem_free(ipc_kernel_map
, addr
, size
);
524 return KERN_RESOURCE_SHORTAGE
;
527 proc_data
= (processor_info_t
) addr
;
528 for (i
= 0; i
< NCPUS
; i
++) {
532 if (machine_slot
[i
].is_cpu
) {
533 kr
= processor_info(cpu_to_processor(i
),
538 if (kr
!= KERN_SUCCESS
) {
539 kmem_free(ipc_kernel_map
, addr
, size
);
542 assert(count
== count2
);
547 kr
= vm_map_unwire(ipc_kernel_map
, addr
, addr
+ size
, FALSE
);
548 assert(kr
== KERN_SUCCESS
);
549 size
= (vm_size_t
)(num
* count
* sizeof(natural_t
));
550 kr
= vm_map_copyin(ipc_kernel_map
, addr
, size
, TRUE
, ©
);
551 assert(kr
== KERN_SUCCESS
);
554 *proc_info
= (processor_info_array_t
) copy
;
555 *proc_info_count
= num
* count
;
556 return(KERN_SUCCESS
);
563 * Return the IO master access port for this host.
568 io_master_t
*io_master
)
570 if (host
== HOST_NULL
)
571 return KERN_INVALID_ARGUMENT
;
572 *io_master
= ipc_port_copy_send(realhost
.io_master
);
576 #define io_master_deallocate(x)
581 * Return the IO master access port for this host.
585 host_priv_t host_priv
,
586 io_master_t io_master
)
588 io_master_t old_master
;
590 if (host_priv
== HOST_PRIV_NULL
)
591 return KERN_INVALID_ARGUMENT
;
593 old_master
= realhost
.io_master
;
594 realhost
.io_master
= io_master
;
595 io_master_deallocate(old_master
);
600 * User interface for setting a special port.
602 * Only permits the user to set a user-owned special port
603 * ID, rejecting a kernel-owned special port ID.
605 * A special kernel port cannot be set up using this
606 * routine; use kernel_set_special_port() instead.
609 host_set_special_port(
610 host_priv_t host_priv
,
615 return norma_set_special_port(host_priv
, id
, port
);
623 * User interface for retrieving a special port.
625 * When all processing is local, this call does not block.
626 * If processing goes remote to discover a remote UID,
627 * this call blocks but not indefinitely. If the remote
628 * node does not exist, has panic'ed, or is booting but
629 * hasn't yet turned on DIPC, then we expect the transport
630 * to return an error.
632 * This routine always returns SUCCESS, even if there's
635 * Note that there is nothing to prevent a user special
636 * port from disappearing after it has been discovered by
637 * the caller; thus, using a special port can always result
638 * in a "port not valid" error.
642 host_get_special_port(
643 host_priv_t host_priv
,
649 return norma_get_special_port(host_priv
, node
, id
, portp
);
668 host_security_self(void)