]> git.saurik.com Git - apple/xnu.git/blame_incremental - osfmk/kern/host.c
xnu-792.21.3.tar.gz
[apple/xnu.git] / osfmk / kern / host.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28/*
29 * @OSF_COPYRIGHT@
30 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
35 *
36 * Permission to use, copy, modify and distribute this software and its
37 * documentation is hereby granted, provided that both the copyright
38 * notice and this permission notice appear in all copies of the
39 * software, derivative works or modified versions, and any portions
40 * thereof, and that both notices appear in supporting documentation.
41 *
42 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
43 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
44 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
55 */
56/*
57 */
58
59/*
60 * host.c
61 *
62 * Non-ipc host functions.
63 */
64
65#include <mach_host.h>
66
67#include <mach/mach_types.h>
68#include <mach/boolean.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 <mach/processor_info.h>
75#include <mach/vm_param.h>
76#include <mach/processor.h>
77#include <mach/mach_host_server.h>
78#include <mach/host_priv_server.h>
79#include <mach/vm_map.h>
80
81#include <kern/kern_types.h>
82#include <kern/assert.h>
83#include <kern/kalloc.h>
84#include <kern/host.h>
85#include <kern/host_statistics.h>
86#include <kern/ipc_host.h>
87#include <kern/misc_protos.h>
88#include <kern/sched.h>
89#include <kern/processor.h>
90
91#include <vm/vm_map.h>
92
93#if DIPC
94#include <dipc/dipc_funcs.h>
95#include <dipc/special_ports.h>
96#endif
97
98host_data_t realhost;
99
100kern_return_t
101host_processors(
102 host_priv_t host_priv,
103 processor_array_t *out_array,
104 mach_msg_type_number_t *countp)
105{
106 register processor_t processor, *tp;
107 void *addr;
108 unsigned int count, i;
109
110 if (host_priv == HOST_PRIV_NULL)
111 return (KERN_INVALID_ARGUMENT);
112
113 assert(host_priv == &realhost);
114
115 count = processor_count;
116 assert(count != 0);
117
118 addr = kalloc((vm_size_t) (count * sizeof(mach_port_t)));
119 if (addr == 0)
120 return (KERN_RESOURCE_SHORTAGE);
121
122 tp = (processor_t *) addr;
123 *tp++ = processor = processor_list;
124
125 if (count > 1) {
126 simple_lock(&processor_list_lock);
127
128 for (i = 1; i < count; i++)
129 *tp++ = processor = processor->processor_list;
130
131 simple_unlock(&processor_list_lock);
132 }
133
134 *countp = count;
135 *out_array = (processor_array_t)addr;
136
137 /* do the conversion that Mig should handle */
138
139 tp = (processor_t *) addr;
140 for (i = 0; i < count; i++)
141 ((mach_port_t *) tp)[i] =
142 (mach_port_t)convert_processor_to_port(tp[i]);
143
144 return (KERN_SUCCESS);
145}
146
147kern_return_t
148host_info(
149 host_t host,
150 host_flavor_t flavor,
151 host_info_t info,
152 mach_msg_type_number_t *count)
153{
154
155 if (host == HOST_NULL)
156 return (KERN_INVALID_ARGUMENT);
157
158 switch (flavor) {
159
160 case HOST_BASIC_INFO:
161 {
162 register host_basic_info_t basic_info;
163 register int master_slot;
164
165 /*
166 * Basic information about this host.
167 */
168 if (*count < HOST_BASIC_INFO_OLD_COUNT)
169 return (KERN_FAILURE);
170
171 basic_info = (host_basic_info_t) info;
172
173 basic_info->max_cpus = machine_info.max_cpus;
174 basic_info->avail_cpus = machine_info.avail_cpus;
175 basic_info->memory_size = machine_info.memory_size;
176 master_slot = PROCESSOR_DATA(master_processor, slot_num);
177 basic_info->cpu_type = slot_type(master_slot);
178 basic_info->cpu_subtype = slot_subtype(master_slot);
179
180 if (*count >= HOST_BASIC_INFO_COUNT) {
181 basic_info->cpu_threadtype = slot_threadtype(master_slot);
182 basic_info->physical_cpu = machine_info.physical_cpu;
183 basic_info->physical_cpu_max = machine_info.physical_cpu_max;
184 basic_info->logical_cpu = machine_info.logical_cpu;
185 basic_info->logical_cpu_max = machine_info.logical_cpu_max;
186 basic_info->max_mem = machine_info.max_mem;
187
188 *count = HOST_BASIC_INFO_COUNT;
189 } else {
190 *count = HOST_BASIC_INFO_OLD_COUNT;
191 }
192
193 return (KERN_SUCCESS);
194 }
195
196 case HOST_SCHED_INFO:
197 {
198 register host_sched_info_t sched_info;
199
200 /*
201 * Return scheduler information.
202 */
203 if (*count < HOST_SCHED_INFO_COUNT)
204 return (KERN_FAILURE);
205
206 sched_info = (host_sched_info_t) info;
207
208 sched_info->min_timeout =
209 sched_info->min_quantum = std_quantum_us / 1000;
210
211 *count = HOST_SCHED_INFO_COUNT;
212
213 return (KERN_SUCCESS);
214 }
215
216 case HOST_RESOURCE_SIZES:
217 {
218 /*
219 * Return sizes of kernel data structures
220 */
221 if (*count < HOST_RESOURCE_SIZES_COUNT)
222 return (KERN_FAILURE);
223
224 /* XXX Fail until ledgers are implemented */
225 return (KERN_INVALID_ARGUMENT);
226 }
227
228 case HOST_PRIORITY_INFO:
229 {
230 register host_priority_info_t priority_info;
231
232 if (*count < HOST_PRIORITY_INFO_COUNT)
233 return (KERN_FAILURE);
234
235 priority_info = (host_priority_info_t) info;
236
237 priority_info->kernel_priority = MINPRI_KERNEL;
238 priority_info->system_priority = MINPRI_KERNEL;
239 priority_info->server_priority = MINPRI_RESERVED;
240 priority_info->user_priority = BASEPRI_DEFAULT;
241 priority_info->depress_priority = DEPRESSPRI;
242 priority_info->idle_priority = IDLEPRI;
243 priority_info->minimum_priority = MINPRI_USER;
244 priority_info->maximum_priority = MAXPRI_RESERVED;
245
246 *count = HOST_PRIORITY_INFO_COUNT;
247
248 return (KERN_SUCCESS);
249 }
250
251 /*
252 * Gestalt for various trap facilities.
253 */
254 case HOST_MACH_MSG_TRAP:
255 case HOST_SEMAPHORE_TRAPS:
256 {
257 *count = 0;
258 return (KERN_SUCCESS);
259 }
260
261 default:
262 return (KERN_INVALID_ARGUMENT);
263 }
264}
265
266kern_return_t
267host_statistics(
268 host_t host,
269 host_flavor_t flavor,
270 host_info_t info,
271 mach_msg_type_number_t *count)
272{
273
274 if (host == HOST_NULL)
275 return (KERN_INVALID_HOST);
276
277 switch(flavor) {
278
279 case HOST_LOAD_INFO:
280 {
281 host_load_info_t load_info;
282
283 if (*count < HOST_LOAD_INFO_COUNT)
284 return (KERN_FAILURE);
285
286 load_info = (host_load_info_t) info;
287
288 bcopy((char *) avenrun,
289 (char *) load_info->avenrun, sizeof avenrun);
290 bcopy((char *) mach_factor,
291 (char *) load_info->mach_factor, sizeof mach_factor);
292
293 *count = HOST_LOAD_INFO_COUNT;
294 return (KERN_SUCCESS);
295 }
296
297 case HOST_VM_INFO:
298 {
299 register processor_t processor;
300 register vm_statistics_t stat;
301 vm_statistics_data_t host_vm_stat;
302
303 if (*count < HOST_VM_INFO_REV0_COUNT)
304 return (KERN_FAILURE);
305
306 processor = processor_list;
307 stat = &PROCESSOR_DATA(processor, vm_stat);
308 host_vm_stat = *stat;
309
310 if (processor_count > 1) {
311 simple_lock(&processor_list_lock);
312
313 while ((processor = processor->processor_list) != NULL) {
314 stat = &PROCESSOR_DATA(processor, vm_stat);
315
316 host_vm_stat.zero_fill_count += stat->zero_fill_count;
317 host_vm_stat.reactivations += stat->reactivations;
318 host_vm_stat.pageins += stat->pageins;
319 host_vm_stat.pageouts += stat->pageouts;
320 host_vm_stat.faults += stat->faults;
321 host_vm_stat.cow_faults += stat->cow_faults;
322 host_vm_stat.lookups += stat->lookups;
323 host_vm_stat.hits += stat->hits;
324 }
325
326 simple_unlock(&processor_list_lock);
327 }
328
329 stat = (vm_statistics_t) info;
330
331 stat->free_count = vm_page_free_count;
332 stat->active_count = vm_page_active_count;
333 stat->inactive_count = vm_page_inactive_count;
334 stat->wire_count = vm_page_wire_count;
335 stat->zero_fill_count = host_vm_stat.zero_fill_count;
336 stat->reactivations = host_vm_stat.reactivations;
337 stat->pageins = host_vm_stat.pageins;
338 stat->pageouts = host_vm_stat.pageouts;
339 stat->faults = host_vm_stat.faults;
340 stat->cow_faults = host_vm_stat.cow_faults;
341 stat->lookups = host_vm_stat.lookups;
342 stat->hits = host_vm_stat.hits;
343
344 if (*count >= HOST_VM_INFO_COUNT) {
345 /* info that was not in revision 0 of that interface */
346 stat->purgeable_count = vm_page_purgeable_count;
347 stat->purges = vm_page_purged_count;
348 *count = HOST_VM_INFO_COUNT;
349 } else {
350 *count = HOST_VM_INFO_REV0_COUNT;
351 }
352
353 return (KERN_SUCCESS);
354 }
355
356 case HOST_CPU_LOAD_INFO:
357 {
358 register processor_t processor;
359 host_cpu_load_info_t cpu_load_info;
360 unsigned long ticks_value1, ticks_value2;
361
362 if (*count < HOST_CPU_LOAD_INFO_COUNT)
363 return (KERN_FAILURE);
364
365#define GET_TICKS_VALUE(processor, state) \
366MACRO_BEGIN \
367 do { \
368 ticks_value1 = *(volatile integer_t *) \
369 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
370 ticks_value2 = *(volatile integer_t *) \
371 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
372 } while (ticks_value1 != ticks_value2); \
373 \
374 cpu_load_info->cpu_ticks[(state)] += ticks_value1; \
375MACRO_END
376
377 cpu_load_info = (host_cpu_load_info_t)info;
378 cpu_load_info->cpu_ticks[CPU_STATE_USER] = 0;
379 cpu_load_info->cpu_ticks[CPU_STATE_NICE] = 0;
380 cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] = 0;
381 cpu_load_info->cpu_ticks[CPU_STATE_IDLE] = 0;
382
383 processor = processor_list;
384 GET_TICKS_VALUE(processor, CPU_STATE_USER);
385 GET_TICKS_VALUE(processor, CPU_STATE_NICE);
386 GET_TICKS_VALUE(processor, CPU_STATE_SYSTEM);
387 GET_TICKS_VALUE(processor, CPU_STATE_IDLE);
388
389 if (processor_count > 1) {
390 simple_lock(&processor_list_lock);
391
392 while ((processor = processor->processor_list) != NULL) {
393 GET_TICKS_VALUE(processor, CPU_STATE_USER);
394 GET_TICKS_VALUE(processor, CPU_STATE_NICE);
395 GET_TICKS_VALUE(processor, CPU_STATE_SYSTEM);
396 GET_TICKS_VALUE(processor, CPU_STATE_IDLE);
397 }
398
399 simple_unlock(&processor_list_lock);
400 }
401
402 *count = HOST_CPU_LOAD_INFO_COUNT;
403
404 return (KERN_SUCCESS);
405 }
406
407 default:
408 return (KERN_INVALID_ARGUMENT);
409 }
410}
411
412/*
413 * Get host statistics that require privilege.
414 * None for now, just call the un-privileged version.
415 */
416kern_return_t
417host_priv_statistics(
418 host_priv_t host_priv,
419 host_flavor_t flavor,
420 host_info_t info,
421 mach_msg_type_number_t *count)
422{
423 return(host_statistics((host_t)host_priv, flavor, info, count));
424}
425
426
427kern_return_t
428host_page_size(
429 host_t host,
430 vm_size_t *out_page_size)
431{
432 if (host == HOST_NULL)
433 return(KERN_INVALID_ARGUMENT);
434
435 *out_page_size = PAGE_SIZE;
436
437 return(KERN_SUCCESS);
438}
439
440/*
441 * Return kernel version string (more than you ever
442 * wanted to know about what version of the kernel this is).
443 */
444extern char version[];
445
446kern_return_t
447host_kernel_version(
448 host_t host,
449 kernel_version_t out_version)
450{
451
452 if (host == HOST_NULL)
453 return(KERN_INVALID_ARGUMENT);
454
455 (void) strncpy(out_version, version, sizeof(kernel_version_t));
456
457 return(KERN_SUCCESS);
458}
459
460/*
461 * host_processor_sets:
462 *
463 * List all processor sets on the host.
464 */
465kern_return_t
466host_processor_sets(
467 host_priv_t host_priv,
468 processor_set_name_array_t *pset_list,
469 mach_msg_type_number_t *count)
470{
471 void *addr;
472
473 if (host_priv == HOST_PRIV_NULL)
474 return KERN_INVALID_ARGUMENT;
475
476 /*
477 * Allocate memory. Can be pageable because it won't be
478 * touched while holding a lock.
479 */
480
481 addr = kalloc((vm_size_t) sizeof(mach_port_t));
482 if (addr == 0)
483 return KERN_RESOURCE_SHORTAGE;
484
485 /* take ref for convert_pset_name_to_port */
486 pset_reference(&default_pset);
487 /* do the conversion that Mig should handle */
488 *((ipc_port_t *) addr) = convert_pset_name_to_port(&default_pset);
489
490 *pset_list = (processor_set_array_t)addr;
491 *count = 1;
492
493 return KERN_SUCCESS;
494}
495
496/*
497 * host_processor_set_priv:
498 *
499 * Return control port for given processor set.
500 */
501kern_return_t
502host_processor_set_priv(
503 host_priv_t host_priv,
504 processor_set_t pset_name,
505 processor_set_t *pset)
506{
507 if ((host_priv == HOST_PRIV_NULL) || (pset_name == PROCESSOR_SET_NULL)) {
508 *pset = PROCESSOR_SET_NULL;
509 return(KERN_INVALID_ARGUMENT);
510 }
511
512 *pset = pset_name;
513 pset_reference(*pset);
514 return(KERN_SUCCESS);
515}
516
517/*
518 * host_processor_info
519 *
520 * Return info about the processors on this host. It will return
521 * the number of processors, and the specific type of info requested
522 * in an OOL array.
523 */
524kern_return_t
525host_processor_info(
526 host_t host,
527 processor_flavor_t flavor,
528 natural_t *out_pcount,
529 processor_info_array_t *out_array,
530 mach_msg_type_number_t *out_array_count)
531{
532 kern_return_t result;
533 processor_t processor;
534 host_t thost;
535 processor_info_t info;
536 unsigned int icount, tcount;
537 unsigned int pcount, i;
538 vm_offset_t addr;
539 vm_size_t size;
540 vm_map_copy_t copy;
541
542 if (host == HOST_NULL)
543 return (KERN_INVALID_ARGUMENT);
544
545 result = processor_info_count(flavor, &icount);
546 if (result != KERN_SUCCESS)
547 return (result);
548
549 pcount = processor_count;
550 assert(pcount != 0);
551
552 size = round_page(pcount * icount * sizeof(natural_t));
553 result = kmem_alloc(ipc_kernel_map, &addr, size);
554 if (result != KERN_SUCCESS)
555 return (KERN_RESOURCE_SHORTAGE);
556
557 info = (processor_info_t) addr;
558 processor = processor_list;
559 tcount = icount;
560
561 result = processor_info(processor, flavor, &thost, info, &tcount);
562 if (result != KERN_SUCCESS) {
563 kmem_free(ipc_kernel_map, addr, size);
564 return (result);
565 }
566
567 if (pcount > 1) {
568 for (i = 1; i < pcount; i++) {
569 simple_lock(&processor_list_lock);
570 processor = processor->processor_list;
571 simple_unlock(&processor_list_lock);
572
573 info += icount;
574 tcount = icount;
575 result = processor_info(processor, flavor, &thost, info, &tcount);
576 if (result != KERN_SUCCESS) {
577 kmem_free(ipc_kernel_map, addr, size);
578 return (result);
579 }
580 }
581 }
582
583 result = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr),
584 vm_map_round_page(addr + size), FALSE);
585 assert(result == KERN_SUCCESS);
586 result = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr,
587 (vm_map_size_t)size, TRUE, &copy);
588 assert(result == KERN_SUCCESS);
589
590 *out_pcount = pcount;
591 *out_array = (processor_info_array_t) copy;
592 *out_array_count = pcount * icount;
593
594 return (KERN_SUCCESS);
595}
596
597/*
598 * Kernel interface for setting a special port.
599 */
600kern_return_t
601kernel_set_special_port(
602 host_priv_t host_priv,
603 int id,
604 ipc_port_t port)
605{
606 ipc_port_t old_port;
607
608 host_lock(host_priv);
609 old_port = host_priv->special[id];
610 host_priv->special[id] = port;
611 host_unlock(host_priv);
612 if (IP_VALID(old_port))
613 ipc_port_release_send(old_port);
614 return KERN_SUCCESS;
615}
616
617/*
618 * User interface for setting a special port.
619 *
620 * Only permits the user to set a user-owned special port
621 * ID, rejecting a kernel-owned special port ID.
622 *
623 * A special kernel port cannot be set up using this
624 * routine; use kernel_set_special_port() instead.
625 */
626kern_return_t
627host_set_special_port(
628 host_priv_t host_priv,
629 int id,
630 ipc_port_t port)
631{
632 if (host_priv == HOST_PRIV_NULL ||
633 id <= HOST_MAX_SPECIAL_KERNEL_PORT || id > HOST_MAX_SPECIAL_PORT ) {
634 if (IP_VALID(port))
635 ipc_port_release_send(port);
636 return KERN_INVALID_ARGUMENT;
637 }
638
639 return kernel_set_special_port(host_priv, id, port);
640}
641
642
643/*
644 * User interface for retrieving a special port.
645 *
646 * Note that there is nothing to prevent a user special
647 * port from disappearing after it has been discovered by
648 * the caller; thus, using a special port can always result
649 * in a "port not valid" error.
650 */
651
652kern_return_t
653host_get_special_port(
654 host_priv_t host_priv,
655 __unused int node,
656 int id,
657 ipc_port_t *portp)
658{
659 ipc_port_t port;
660
661 if (host_priv == HOST_PRIV_NULL ||
662 id == HOST_SECURITY_PORT || id > HOST_MAX_SPECIAL_PORT )
663 return KERN_INVALID_ARGUMENT;
664
665#if DIPC
666 if (node != HOST_LOCAL_NODE)
667 return norma_get_special_port(host_priv, node, id, portp);
668#endif
669
670 host_lock(host_priv);
671 port = realhost.special[id];
672 *portp = ipc_port_copy_send(port);
673 host_unlock(host_priv);
674
675 return KERN_SUCCESS;
676}
677
678
679/*
680 * host_get_io_master
681 *
682 * Return the IO master access port for this host.
683 */
684kern_return_t
685host_get_io_master(
686 host_t host,
687 io_master_t *io_masterp)
688{
689 if (host == HOST_NULL)
690 return KERN_INVALID_ARGUMENT;
691
692 return (host_get_io_master_port(host_priv_self(), io_masterp));
693}
694
695host_t
696host_self(void)
697{
698 return &realhost;
699}
700
701host_priv_t
702host_priv_self(void)
703{
704 return &realhost;
705}
706
707host_security_t
708host_security_self(void)
709{
710 return &realhost;
711}
712