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