2 * Copyright (c) 2000-2003 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/host_special_ports.h>
71 #include <mach/kern_return.h>
72 #include <mach/machine.h>
73 #include <mach/port.h>
74 #include <kern/processor.h>
75 #include <mach/processor_info.h>
76 #include <mach/vm_param.h>
77 #include <mach/mach_host_server.h>
79 #include <dipc/dipc_funcs.h>
80 #include <dipc/special_ports.h>
83 vm_statistics_data_t vm_stat
[NCPUS
];
89 host_priv_t host_priv
,
90 processor_array_t
*processor_list
,
91 mach_msg_type_number_t
*countp
)
94 register processor_t
*tp
;
98 if (host_priv
== HOST_PRIV_NULL
)
99 return(KERN_INVALID_ARGUMENT
);
101 assert(host_priv
== &realhost
);
104 * Determine how many processors we have.
105 * (This number shouldn't change.)
109 for (i
= 0; i
< NCPUS
; i
++)
110 if (machine_slot
[i
].is_cpu
)
114 panic("host_processors");
116 addr
= kalloc((vm_size_t
) (count
* sizeof(mach_port_t
)));
118 return KERN_RESOURCE_SHORTAGE
;
120 tp
= (processor_t
*) addr
;
121 for (i
= 0; i
< NCPUS
; i
++)
122 if (machine_slot
[i
].is_cpu
)
123 *tp
++ = cpu_to_processor(i
);
126 *processor_list
= (processor_array_t
)addr
;
128 /* do the conversion that Mig should handle */
130 tp
= (processor_t
*) addr
;
131 for (i
= 0; i
< count
; i
++)
132 ((mach_port_t
*) tp
)[i
] =
133 (mach_port_t
)convert_processor_to_port(tp
[i
]);
141 host_flavor_t flavor
,
143 mach_msg_type_number_t
*count
)
146 if (host
== HOST_NULL
)
147 return(KERN_INVALID_ARGUMENT
);
151 case HOST_BASIC_INFO
:
153 register host_basic_info_t basic_info
;
156 * Basic information about this host.
158 if (*count
< HOST_BASIC_INFO_COUNT
)
159 return(KERN_FAILURE
);
161 basic_info
= (host_basic_info_t
) info
;
163 basic_info
->max_cpus
= machine_info
.max_cpus
;
164 basic_info
->avail_cpus
= machine_info
.avail_cpus
;
165 basic_info
->memory_size
= machine_info
.memory_size
;
166 basic_info
->cpu_type
=
167 machine_slot
[master_processor
->slot_num
].cpu_type
;
168 basic_info
->cpu_subtype
=
169 machine_slot
[master_processor
->slot_num
].cpu_subtype
;
171 *count
= HOST_BASIC_INFO_COUNT
;
173 return(KERN_SUCCESS
);
176 case HOST_SCHED_INFO
:
178 register host_sched_info_t sched_info
;
179 extern int tick
; /* XXX */
182 * Return scheduler information.
184 if (*count
< HOST_SCHED_INFO_COUNT
)
185 return(KERN_FAILURE
);
187 sched_info
= (host_sched_info_t
) info
;
189 sched_info
->min_timeout
= tick
/ 1000; /* XXX */
190 sched_info
->min_quantum
= tick
/ 1000; /* XXX */
192 *count
= HOST_SCHED_INFO_COUNT
;
194 return(KERN_SUCCESS
);
197 case HOST_RESOURCE_SIZES
:
200 * Return sizes of kernel data structures
202 if (*count
< HOST_RESOURCE_SIZES_COUNT
)
203 return(KERN_FAILURE
);
205 /* XXX Fail until ledgers are implemented */
206 return(KERN_INVALID_ARGUMENT
);
209 case HOST_PRIORITY_INFO
:
211 register host_priority_info_t priority_info
;
213 if (*count
< HOST_PRIORITY_INFO_COUNT
)
214 return(KERN_FAILURE
);
216 priority_info
= (host_priority_info_t
) info
;
218 priority_info
->kernel_priority
= MINPRI_KERNEL
;
219 priority_info
->system_priority
= MINPRI_KERNEL
;
220 priority_info
->server_priority
= MINPRI_SYSTEM
;
221 priority_info
->user_priority
= BASEPRI_DEFAULT
;
222 priority_info
->depress_priority
= DEPRESSPRI
;
223 priority_info
->idle_priority
= IDLEPRI
;
224 priority_info
->minimum_priority
= MINPRI_STANDARD
;
225 priority_info
->maximum_priority
= MAXPRI_SYSTEM
;
227 *count
= HOST_PRIORITY_INFO_COUNT
;
229 return(KERN_SUCCESS
);
233 * Gestalt for various trap facilities.
235 case HOST_MACH_MSG_TRAP
:
236 case HOST_SEMAPHORE_TRAPS
:
243 return(KERN_INVALID_ARGUMENT
);
250 host_flavor_t flavor
,
252 mach_msg_type_number_t
*count
)
255 if (host
== HOST_NULL
)
256 return(KERN_INVALID_HOST
);
260 case HOST_LOAD_INFO
: {
261 register host_load_info_t load_info
;
262 extern uint32_t avenrun
[3], mach_factor
[3];
264 if (*count
< HOST_LOAD_INFO_COUNT
)
265 return(KERN_FAILURE
);
267 load_info
= (host_load_info_t
) info
;
269 bcopy((char *) avenrun
,
270 (char *) load_info
->avenrun
,
272 bcopy((char *) mach_factor
,
273 (char *) load_info
->mach_factor
,
276 *count
= HOST_LOAD_INFO_COUNT
;
277 return(KERN_SUCCESS
);
281 register vm_statistics_t stat
;
282 vm_statistics_data_t host_vm_stat
;
283 extern int vm_page_free_count
, vm_page_active_count
,
284 vm_page_inactive_count
, vm_page_wire_count
;
286 if (*count
< HOST_VM_INFO_COUNT
)
287 return(KERN_FAILURE
);
290 host_vm_stat
= *stat
;
295 for (i
= 1; i
< NCPUS
; i
++) {
297 host_vm_stat
.zero_fill_count
+=
298 stat
->zero_fill_count
;
299 host_vm_stat
.reactivations
+=
301 host_vm_stat
.pageins
+= stat
->pageins
;
302 host_vm_stat
.pageouts
+= stat
->pageouts
;
303 host_vm_stat
.faults
+= stat
->faults
;
304 host_vm_stat
.cow_faults
+= stat
->cow_faults
;
305 host_vm_stat
.lookups
+= stat
->lookups
;
306 host_vm_stat
.hits
+= stat
->hits
;
311 stat
= (vm_statistics_t
) info
;
313 stat
->free_count
= vm_page_free_count
;
314 stat
->active_count
= vm_page_active_count
;
315 stat
->inactive_count
= vm_page_inactive_count
;
316 stat
->wire_count
= vm_page_wire_count
;
317 stat
->zero_fill_count
= host_vm_stat
.zero_fill_count
;
318 stat
->reactivations
= host_vm_stat
.reactivations
;
319 stat
->pageins
= host_vm_stat
.pageins
;
320 stat
->pageouts
= host_vm_stat
.pageouts
;
321 stat
->faults
= host_vm_stat
.faults
;
322 stat
->cow_faults
= host_vm_stat
.cow_faults
;
323 stat
->lookups
= host_vm_stat
.lookups
;
324 stat
->hits
= host_vm_stat
.hits
;
326 *count
= HOST_VM_INFO_COUNT
;
327 return(KERN_SUCCESS
);
330 case HOST_CPU_LOAD_INFO
: {
331 host_cpu_load_info_t cpu_load_info
;
332 unsigned long ticks_value1
, ticks_value2
;
335 #define GET_TICKS_VALUE(__cpu,__state) \
338 ticks_value1 = *(volatile integer_t *) \
339 (&machine_slot[(__cpu)].cpu_ticks[(__state)]); \
340 ticks_value2 = *(volatile integer_t *) \
341 (&machine_slot[(__cpu)].cpu_ticks[(__state)]); \
342 } while (ticks_value1 != ticks_value2); \
343 cpu_load_info->cpu_ticks[(__state)] += ticks_value1; \
346 if (*count
< HOST_CPU_LOAD_INFO_COUNT
)
349 cpu_load_info
= (host_cpu_load_info_t
) info
;
351 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = 0;
352 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
353 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
354 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] = 0;
355 for (i
= 0; i
< NCPUS
; i
++) {
356 if (!machine_slot
[i
].is_cpu
||
357 !machine_slot
[i
].running
)
359 GET_TICKS_VALUE(i
, CPU_STATE_USER
);
360 GET_TICKS_VALUE(i
, CPU_STATE_NICE
);
361 GET_TICKS_VALUE(i
, CPU_STATE_SYSTEM
);
362 GET_TICKS_VALUE(i
, CPU_STATE_IDLE
);
365 *count
= HOST_CPU_LOAD_INFO_COUNT
;
370 return(KERN_INVALID_ARGUMENT
);
375 * Get host statistics that require privilege.
376 * None for now, just call the un-privileged version.
379 host_priv_statistics(
380 host_priv_t host_priv
,
381 host_flavor_t flavor
,
383 mach_msg_type_number_t
*count
)
385 return(host_statistics((host_t
)host_priv
, flavor
, info
, count
));
392 vm_size_t
*out_page_size
)
394 if (host
== HOST_NULL
)
395 return(KERN_INVALID_ARGUMENT
);
397 *out_page_size
= PAGE_SIZE
;
399 return(KERN_SUCCESS
);
403 * Return kernel version string (more than you ever
404 * wanted to know about what version of the kernel this is).
410 kernel_version_t out_version
)
412 extern char version
[];
414 if (host
== HOST_NULL
)
415 return(KERN_INVALID_ARGUMENT
);
417 (void) strncpy(out_version
, version
, sizeof(kernel_version_t
));
419 return(KERN_SUCCESS
);
423 * host_processor_sets:
425 * List all processor sets on the host.
429 host_priv_t host_priv
,
430 processor_set_name_array_t
*pset_list
,
431 mach_msg_type_number_t
*count
)
435 if (host_priv
== HOST_PRIV_NULL
)
436 return KERN_INVALID_ARGUMENT
;
439 * Allocate memory. Can be pageable because it won't be
440 * touched while holding a lock.
443 addr
= kalloc((vm_size_t
) sizeof(mach_port_t
));
445 return KERN_RESOURCE_SHORTAGE
;
447 /* take ref for convert_pset_name_to_port */
448 pset_reference(&default_pset
);
449 /* do the conversion that Mig should handle */
450 *((ipc_port_t
*) addr
) = convert_pset_name_to_port(&default_pset
);
452 *pset_list
= (processor_set_array_t
)addr
;
459 * host_processor_set_priv:
461 * Return control port for given processor set.
464 host_processor_set_priv(
465 host_priv_t host_priv
,
466 processor_set_t pset_name
,
467 processor_set_t
*pset
)
469 if ((host_priv
== HOST_PRIV_NULL
) || (pset_name
== PROCESSOR_SET_NULL
)) {
470 *pset
= PROCESSOR_SET_NULL
;
471 return(KERN_INVALID_ARGUMENT
);
475 pset_reference(*pset
);
476 return(KERN_SUCCESS
);
480 * host_processor_info
482 * Return info about the processors on this host. It will return
483 * the number of processors, and the specific type of info requested
489 processor_flavor_t flavor
,
490 natural_t
*proc_count
,
491 processor_info_array_t
*proc_info
,
492 mach_msg_type_number_t
*proc_info_count
)
501 processor_info_t proc_data
;
503 if (host
== HOST_NULL
)
504 return KERN_INVALID_ARGUMENT
;
506 kr
= processor_info_count(flavor
, &count
);
507 if (kr
!= KERN_SUCCESS
) {
511 for (num
= i
= 0; i
< NCPUS
; i
++)
512 if (machine_slot
[i
].is_cpu
)
515 size
= (vm_size_t
)round_page_32(num
* count
* sizeof(natural_t
));
517 kr
= vm_allocate(ipc_kernel_map
, &addr
, size
, TRUE
);
518 if (kr
!= KERN_SUCCESS
)
519 return KERN_RESOURCE_SHORTAGE
;
521 kr
= vm_map_wire(ipc_kernel_map
, addr
, addr
+ size
,
522 VM_PROT_READ
|VM_PROT_WRITE
, FALSE
);
523 if (kr
!= KERN_SUCCESS
) {
524 kmem_free(ipc_kernel_map
, addr
, size
);
525 return KERN_RESOURCE_SHORTAGE
;
528 proc_data
= (processor_info_t
) addr
;
529 for (i
= 0; i
< NCPUS
; i
++) {
533 if (machine_slot
[i
].is_cpu
) {
534 kr
= processor_info(cpu_to_processor(i
),
539 if (kr
!= KERN_SUCCESS
) {
540 kmem_free(ipc_kernel_map
, addr
, size
);
543 assert(count
== count2
);
548 kr
= vm_map_unwire(ipc_kernel_map
, addr
, addr
+ size
, FALSE
);
549 assert(kr
== KERN_SUCCESS
);
550 size
= (vm_size_t
)(num
* count
* sizeof(natural_t
));
551 kr
= vm_map_copyin(ipc_kernel_map
, addr
, size
, TRUE
, ©
);
552 assert(kr
== KERN_SUCCESS
);
555 *proc_info
= (processor_info_array_t
) copy
;
556 *proc_info_count
= num
* count
;
557 return(KERN_SUCCESS
);
561 * Kernel interface for setting a special port.
564 kernel_set_special_port(
565 host_priv_t host_priv
,
571 host_lock(host_priv
);
572 old_port
= host_priv
->special
[id
];
573 host_priv
->special
[id
] = port
;
574 host_unlock(host_priv
);
575 if (IP_VALID(old_port
))
576 ipc_port_release_send(old_port
);
581 * User interface for setting a special port.
583 * Only permits the user to set a user-owned special port
584 * ID, rejecting a kernel-owned special port ID.
586 * A special kernel port cannot be set up using this
587 * routine; use kernel_set_special_port() instead.
590 host_set_special_port(
591 host_priv_t host_priv
,
595 if (host_priv
== HOST_PRIV_NULL
||
596 id
<= HOST_MAX_SPECIAL_KERNEL_PORT
|| id
> HOST_MAX_SPECIAL_PORT
) {
598 ipc_port_release_send(port
);
599 return KERN_INVALID_ARGUMENT
;
602 return kernel_set_special_port(host_priv
, id
, port
);
607 * User interface for retrieving a special port.
609 * Note that there is nothing to prevent a user special
610 * port from disappearing after it has been discovered by
611 * the caller; thus, using a special port can always result
612 * in a "port not valid" error.
616 host_get_special_port(
617 host_priv_t host_priv
,
624 if (host_priv
== HOST_PRIV_NULL
||
625 id
== HOST_SECURITY_PORT
)
626 return KERN_INVALID_ARGUMENT
;
629 if (node
!= HOST_LOCAL_NODE
)
630 return norma_get_special_port(host_priv
, node
, id
, portp
);
633 host_lock(host_priv
);
634 port
= realhost
.special
[id
];
635 *portp
= ipc_port_copy_send(port
);
636 host_unlock(host_priv
);
645 * Return the IO master access port for this host.
650 io_master_t
*io_masterp
)
652 if (host
== HOST_NULL
)
653 return KERN_INVALID_ARGUMENT
;
655 return (host_get_io_master_port(host_priv_self(), io_masterp
));
671 host_security_self(void)