]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/host.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / osfmk / kern / host.c
1 /*
2 * Copyright (c) 2000-2008 Apple 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/mach_types.h>
66 #include <mach/boolean.h>
67 #include <mach/host_info.h>
68 #include <mach/host_special_ports.h>
69 #include <mach/kern_return.h>
70 #include <mach/machine.h>
71 #include <mach/port.h>
72 #include <mach/processor_info.h>
73 #include <mach/vm_param.h>
74 #include <mach/processor.h>
75 #include <mach/mach_host_server.h>
76 #include <mach/host_priv_server.h>
77 #include <mach/vm_map.h>
78
79 #include <kern/kern_types.h>
80 #include <kern/assert.h>
81 #include <kern/kalloc.h>
82 #include <kern/host.h>
83 #include <kern/host_statistics.h>
84 #include <kern/ipc_host.h>
85 #include <kern/misc_protos.h>
86 #include <kern/sched.h>
87 #include <kern/processor.h>
88
89 #include <vm/vm_map.h>
90
91 host_data_t realhost;
92
93 kern_return_t
94 host_processors(
95 host_priv_t host_priv,
96 processor_array_t *out_array,
97 mach_msg_type_number_t *countp)
98 {
99 register processor_t processor, *tp;
100 void *addr;
101 unsigned int count, i;
102
103 if (host_priv == HOST_PRIV_NULL)
104 return (KERN_INVALID_ARGUMENT);
105
106 assert(host_priv == &realhost);
107
108 count = processor_count;
109 assert(count != 0);
110
111 addr = kalloc((vm_size_t) (count * sizeof(mach_port_t)));
112 if (addr == 0)
113 return (KERN_RESOURCE_SHORTAGE);
114
115 tp = (processor_t *) addr;
116 *tp++ = processor = processor_list;
117
118 if (count > 1) {
119 simple_lock(&processor_list_lock);
120
121 for (i = 1; i < count; i++)
122 *tp++ = processor = processor->processor_list;
123
124 simple_unlock(&processor_list_lock);
125 }
126
127 *countp = count;
128 *out_array = (processor_array_t)addr;
129
130 /* do the conversion that Mig should handle */
131
132 tp = (processor_t *) addr;
133 for (i = 0; i < count; i++)
134 ((mach_port_t *) tp)[i] =
135 (mach_port_t)convert_processor_to_port(tp[i]);
136
137 return (KERN_SUCCESS);
138 }
139
140 kern_return_t
141 host_info(
142 host_t host,
143 host_flavor_t flavor,
144 host_info_t info,
145 mach_msg_type_number_t *count)
146 {
147
148 if (host == HOST_NULL)
149 return (KERN_INVALID_ARGUMENT);
150
151 switch (flavor) {
152
153 case HOST_BASIC_INFO:
154 {
155 register host_basic_info_t basic_info;
156 register int master_num;
157
158 /*
159 * Basic information about this host.
160 */
161 if (*count < HOST_BASIC_INFO_OLD_COUNT)
162 return (KERN_FAILURE);
163
164 basic_info = (host_basic_info_t) info;
165
166 basic_info->memory_size = machine_info.memory_size;
167 basic_info->max_cpus = machine_info.max_cpus;
168 basic_info->avail_cpus = processor_avail_count;
169 master_num = master_processor->cpu_num;
170 basic_info->cpu_type = slot_type(master_num);
171 basic_info->cpu_subtype = slot_subtype(master_num);
172
173 if (*count >= HOST_BASIC_INFO_COUNT) {
174 basic_info->cpu_threadtype = slot_threadtype(master_num);
175 basic_info->physical_cpu = machine_info.physical_cpu;
176 basic_info->physical_cpu_max = machine_info.physical_cpu_max;
177 basic_info->logical_cpu = machine_info.logical_cpu;
178 basic_info->logical_cpu_max = machine_info.logical_cpu_max;
179 basic_info->max_mem = machine_info.max_mem;
180
181 *count = HOST_BASIC_INFO_COUNT;
182 } else {
183 *count = HOST_BASIC_INFO_OLD_COUNT;
184 }
185
186 return (KERN_SUCCESS);
187 }
188
189 case HOST_SCHED_INFO:
190 {
191 register host_sched_info_t sched_info;
192
193 /*
194 * Return scheduler information.
195 */
196 if (*count < HOST_SCHED_INFO_COUNT)
197 return (KERN_FAILURE);
198
199 sched_info = (host_sched_info_t) info;
200
201 sched_info->min_timeout =
202 sched_info->min_quantum = std_quantum_us / 1000;
203
204 *count = HOST_SCHED_INFO_COUNT;
205
206 return (KERN_SUCCESS);
207 }
208
209 case HOST_RESOURCE_SIZES:
210 {
211 /*
212 * Return sizes of kernel data structures
213 */
214 if (*count < HOST_RESOURCE_SIZES_COUNT)
215 return (KERN_FAILURE);
216
217 /* XXX Fail until ledgers are implemented */
218 return (KERN_INVALID_ARGUMENT);
219 }
220
221 case HOST_PRIORITY_INFO:
222 {
223 register host_priority_info_t priority_info;
224
225 if (*count < HOST_PRIORITY_INFO_COUNT)
226 return (KERN_FAILURE);
227
228 priority_info = (host_priority_info_t) info;
229
230 priority_info->kernel_priority = MINPRI_KERNEL;
231 priority_info->system_priority = MINPRI_KERNEL;
232 priority_info->server_priority = MINPRI_RESERVED;
233 priority_info->user_priority = BASEPRI_DEFAULT;
234 priority_info->depress_priority = DEPRESSPRI;
235 priority_info->idle_priority = IDLEPRI;
236 priority_info->minimum_priority = MINPRI_USER;
237 priority_info->maximum_priority = MAXPRI_RESERVED;
238
239 *count = HOST_PRIORITY_INFO_COUNT;
240
241 return (KERN_SUCCESS);
242 }
243
244 /*
245 * Gestalt for various trap facilities.
246 */
247 case HOST_MACH_MSG_TRAP:
248 case HOST_SEMAPHORE_TRAPS:
249 {
250 *count = 0;
251 return (KERN_SUCCESS);
252 }
253
254 default:
255 return (KERN_INVALID_ARGUMENT);
256 }
257 }
258
259 kern_return_t
260 host_statistics(
261 host_t host,
262 host_flavor_t flavor,
263 host_info_t info,
264 mach_msg_type_number_t *count)
265 {
266
267 if (host == HOST_NULL)
268 return (KERN_INVALID_HOST);
269
270 switch(flavor) {
271
272 case HOST_LOAD_INFO:
273 {
274 host_load_info_t load_info;
275
276 if (*count < HOST_LOAD_INFO_COUNT)
277 return (KERN_FAILURE);
278
279 load_info = (host_load_info_t) info;
280
281 bcopy((char *) avenrun,
282 (char *) load_info->avenrun, sizeof avenrun);
283 bcopy((char *) mach_factor,
284 (char *) load_info->mach_factor, sizeof mach_factor);
285
286 *count = HOST_LOAD_INFO_COUNT;
287 return (KERN_SUCCESS);
288 }
289
290 case HOST_VM_INFO:
291 {
292 register processor_t processor;
293 register vm_statistics_t stat;
294 vm_statistics_data_t host_vm_stat;
295 mach_msg_type_number_t original_count;
296
297 if (*count < HOST_VM_INFO_REV0_COUNT)
298 return (KERN_FAILURE);
299
300 processor = processor_list;
301 stat = &PROCESSOR_DATA(processor, vm_stat);
302 host_vm_stat = *stat;
303
304 if (processor_count > 1) {
305 simple_lock(&processor_list_lock);
306
307 while ((processor = processor->processor_list) != NULL) {
308 stat = &PROCESSOR_DATA(processor, vm_stat);
309
310 host_vm_stat.zero_fill_count += stat->zero_fill_count;
311 host_vm_stat.reactivations += stat->reactivations;
312 host_vm_stat.pageins += stat->pageins;
313 host_vm_stat.pageouts += stat->pageouts;
314 host_vm_stat.faults += stat->faults;
315 host_vm_stat.cow_faults += stat->cow_faults;
316 host_vm_stat.lookups += stat->lookups;
317 host_vm_stat.hits += stat->hits;
318 }
319
320 simple_unlock(&processor_list_lock);
321 }
322
323 stat = (vm_statistics_t) info;
324
325 stat->free_count = vm_page_free_count + vm_page_speculative_count;
326 stat->active_count = vm_page_active_count;
327 stat->inactive_count = vm_page_inactive_count;
328 stat->wire_count = vm_page_wire_count;
329 stat->zero_fill_count = host_vm_stat.zero_fill_count;
330 stat->reactivations = host_vm_stat.reactivations;
331 stat->pageins = host_vm_stat.pageins;
332 stat->pageouts = host_vm_stat.pageouts;
333 stat->faults = host_vm_stat.faults;
334 stat->cow_faults = host_vm_stat.cow_faults;
335 stat->lookups = host_vm_stat.lookups;
336 stat->hits = host_vm_stat.hits;
337
338 /*
339 * Fill in extra info added in later revisions of the
340 * vm_statistics data structure. Fill in only what can fit
341 * in the data structure the caller gave us !
342 */
343 original_count = *count;
344 *count = HOST_VM_INFO_REV0_COUNT; /* rev0 already filled in */
345 if (original_count >= HOST_VM_INFO_REV1_COUNT) {
346 /* rev1 added "purgeable" info */
347 stat->purgeable_count = vm_page_purgeable_count;
348 stat->purges = vm_page_purged_count;
349 *count = HOST_VM_INFO_REV1_COUNT;
350 }
351 if (original_count >= HOST_VM_INFO_REV2_COUNT) {
352 /* rev2 added "speculative" info */
353 stat->speculative_count = vm_page_speculative_count;
354 *count = HOST_VM_INFO_REV2_COUNT;
355 }
356
357 return (KERN_SUCCESS);
358 }
359
360 case HOST_CPU_LOAD_INFO:
361 {
362 register processor_t processor;
363 host_cpu_load_info_t cpu_load_info;
364
365 if (*count < HOST_CPU_LOAD_INFO_COUNT)
366 return (KERN_FAILURE);
367
368 #define GET_TICKS_VALUE(processor, state, timer) \
369 MACRO_BEGIN \
370 cpu_load_info->cpu_ticks[(state)] += \
371 timer_grab(&PROCESSOR_DATA(processor, timer)) / hz_tick_interval; \
372 MACRO_END
373
374 cpu_load_info = (host_cpu_load_info_t)info;
375 cpu_load_info->cpu_ticks[CPU_STATE_USER] = 0;
376 cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] = 0;
377 cpu_load_info->cpu_ticks[CPU_STATE_IDLE] = 0;
378 cpu_load_info->cpu_ticks[CPU_STATE_NICE] = 0;
379
380 processor = processor_list;
381 GET_TICKS_VALUE(processor, CPU_STATE_USER, user_state);
382 GET_TICKS_VALUE(processor, CPU_STATE_SYSTEM, system_state);
383 GET_TICKS_VALUE(processor, CPU_STATE_IDLE, idle_state);
384
385 if (processor_count > 1) {
386 simple_lock(&processor_list_lock);
387
388 while ((processor = processor->processor_list) != NULL) {
389 GET_TICKS_VALUE(processor, CPU_STATE_USER, user_state);
390 GET_TICKS_VALUE(processor, CPU_STATE_SYSTEM, system_state);
391 GET_TICKS_VALUE(processor, CPU_STATE_IDLE, idle_state);
392 }
393
394 simple_unlock(&processor_list_lock);
395 }
396
397 *count = HOST_CPU_LOAD_INFO_COUNT;
398
399 return (KERN_SUCCESS);
400 }
401
402 default:
403 return (KERN_INVALID_ARGUMENT);
404 }
405 }
406
407 /*
408 * Get host statistics that require privilege.
409 * None for now, just call the un-privileged version.
410 */
411 kern_return_t
412 host_priv_statistics(
413 host_priv_t host_priv,
414 host_flavor_t flavor,
415 host_info_t info,
416 mach_msg_type_number_t *count)
417 {
418 return(host_statistics((host_t)host_priv, flavor, info, count));
419 }
420
421 kern_return_t
422 host_page_size(
423 host_t host,
424 vm_size_t *out_page_size)
425 {
426 if (host == HOST_NULL)
427 return(KERN_INVALID_ARGUMENT);
428
429 *out_page_size = PAGE_SIZE;
430
431 return(KERN_SUCCESS);
432 }
433
434 /*
435 * Return kernel version string (more than you ever
436 * wanted to know about what version of the kernel this is).
437 */
438 extern char version[];
439
440 kern_return_t
441 host_kernel_version(
442 host_t host,
443 kernel_version_t out_version)
444 {
445
446 if (host == HOST_NULL)
447 return(KERN_INVALID_ARGUMENT);
448
449 (void) strncpy(out_version, version, sizeof(kernel_version_t));
450
451 return(KERN_SUCCESS);
452 }
453
454 /*
455 * host_processor_sets:
456 *
457 * List all processor sets on the host.
458 */
459 kern_return_t
460 host_processor_sets(
461 host_priv_t host_priv,
462 processor_set_name_array_t *pset_list,
463 mach_msg_type_number_t *count)
464 {
465 void *addr;
466
467 if (host_priv == HOST_PRIV_NULL)
468 return (KERN_INVALID_ARGUMENT);
469
470 /*
471 * Allocate memory. Can be pageable because it won't be
472 * touched while holding a lock.
473 */
474
475 addr = kalloc((vm_size_t) sizeof(mach_port_t));
476 if (addr == 0)
477 return (KERN_RESOURCE_SHORTAGE);
478
479 /* do the conversion that Mig should handle */
480 *((ipc_port_t *) addr) = convert_pset_name_to_port(&pset0);
481
482 *pset_list = (processor_set_array_t)addr;
483 *count = 1;
484
485 return (KERN_SUCCESS);
486 }
487
488 /*
489 * host_processor_set_priv:
490 *
491 * Return control port for given processor set.
492 */
493 kern_return_t
494 host_processor_set_priv(
495 host_priv_t host_priv,
496 processor_set_t pset_name,
497 processor_set_t *pset)
498 {
499 if (host_priv == HOST_PRIV_NULL || pset_name == PROCESSOR_SET_NULL) {
500 *pset = PROCESSOR_SET_NULL;
501
502 return (KERN_INVALID_ARGUMENT);
503 }
504
505 *pset = pset_name;
506
507 return (KERN_SUCCESS);
508 }
509
510 /*
511 * host_processor_info
512 *
513 * Return info about the processors on this host. It will return
514 * the number of processors, and the specific type of info requested
515 * in an OOL array.
516 */
517 kern_return_t
518 host_processor_info(
519 host_t host,
520 processor_flavor_t flavor,
521 natural_t *out_pcount,
522 processor_info_array_t *out_array,
523 mach_msg_type_number_t *out_array_count)
524 {
525 kern_return_t result;
526 processor_t processor;
527 host_t thost;
528 processor_info_t info;
529 unsigned int icount, tcount;
530 unsigned int pcount, i;
531 vm_offset_t addr;
532 vm_size_t size, needed;
533 vm_map_copy_t copy;
534
535 if (host == HOST_NULL)
536 return (KERN_INVALID_ARGUMENT);
537
538 result = processor_info_count(flavor, &icount);
539 if (result != KERN_SUCCESS)
540 return (result);
541
542 pcount = processor_count;
543 assert(pcount != 0);
544
545 needed = pcount * icount * sizeof(natural_t);
546 size = round_page(needed);
547 result = kmem_alloc(ipc_kernel_map, &addr, size);
548 if (result != KERN_SUCCESS)
549 return (KERN_RESOURCE_SHORTAGE);
550
551 info = (processor_info_t) addr;
552 processor = processor_list;
553 tcount = icount;
554
555 result = processor_info(processor, flavor, &thost, info, &tcount);
556 if (result != KERN_SUCCESS) {
557 kmem_free(ipc_kernel_map, addr, size);
558 return (result);
559 }
560
561 if (pcount > 1) {
562 for (i = 1; i < pcount; i++) {
563 simple_lock(&processor_list_lock);
564 processor = processor->processor_list;
565 simple_unlock(&processor_list_lock);
566
567 info += icount;
568 tcount = icount;
569 result = processor_info(processor, flavor, &thost, info, &tcount);
570 if (result != KERN_SUCCESS) {
571 kmem_free(ipc_kernel_map, addr, size);
572 return (result);
573 }
574 }
575 }
576
577 if (size != needed)
578 bzero((char *) addr + needed, size - needed);
579
580 result = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr),
581 vm_map_round_page(addr + size), FALSE);
582 assert(result == KERN_SUCCESS);
583 result = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr,
584 (vm_map_size_t)size, TRUE, &copy);
585 assert(result == KERN_SUCCESS);
586
587 *out_pcount = pcount;
588 *out_array = (processor_info_array_t) copy;
589 *out_array_count = pcount * icount;
590
591 return (KERN_SUCCESS);
592 }
593
594 /*
595 * Kernel interface for setting a special port.
596 */
597 kern_return_t
598 kernel_set_special_port(
599 host_priv_t host_priv,
600 int id,
601 ipc_port_t port)
602 {
603 ipc_port_t old_port;
604
605 host_lock(host_priv);
606 old_port = host_priv->special[id];
607 host_priv->special[id] = port;
608 host_unlock(host_priv);
609 if (IP_VALID(old_port))
610 ipc_port_release_send(old_port);
611 return KERN_SUCCESS;
612 }
613
614 /*
615 * User interface for setting a special port.
616 *
617 * Only permits the user to set a user-owned special port
618 * ID, rejecting a kernel-owned special port ID.
619 *
620 * A special kernel port cannot be set up using this
621 * routine; use kernel_set_special_port() instead.
622 */
623 kern_return_t
624 host_set_special_port(
625 host_priv_t host_priv,
626 int id,
627 ipc_port_t port)
628 {
629 if (host_priv == HOST_PRIV_NULL ||
630 id <= HOST_MAX_SPECIAL_KERNEL_PORT || id > HOST_MAX_SPECIAL_PORT ) {
631 if (IP_VALID(port))
632 ipc_port_release_send(port);
633 return KERN_INVALID_ARGUMENT;
634 }
635
636 return kernel_set_special_port(host_priv, id, port);
637 }
638
639
640 /*
641 * User interface for retrieving a special port.
642 *
643 * Note that there is nothing to prevent a user special
644 * port from disappearing after it has been discovered by
645 * the caller; thus, using a special port can always result
646 * in a "port not valid" error.
647 */
648
649 kern_return_t
650 host_get_special_port(
651 host_priv_t host_priv,
652 __unused int node,
653 int id,
654 ipc_port_t *portp)
655 {
656 ipc_port_t port;
657
658 if (host_priv == HOST_PRIV_NULL ||
659 id == HOST_SECURITY_PORT || id > HOST_MAX_SPECIAL_PORT )
660 return KERN_INVALID_ARGUMENT;
661
662 host_lock(host_priv);
663 port = realhost.special[id];
664 *portp = ipc_port_copy_send(port);
665 host_unlock(host_priv);
666
667 return KERN_SUCCESS;
668 }
669
670
671 /*
672 * host_get_io_master
673 *
674 * Return the IO master access port for this host.
675 */
676 kern_return_t
677 host_get_io_master(
678 host_t host,
679 io_master_t *io_masterp)
680 {
681 if (host == HOST_NULL)
682 return KERN_INVALID_ARGUMENT;
683
684 return (host_get_io_master_port(host_priv_self(), io_masterp));
685 }
686
687 host_t
688 host_self(void)
689 {
690 return &realhost;
691 }
692
693 host_priv_t
694 host_priv_self(void)
695 {
696 return &realhost;
697 }
698
699 host_security_t
700 host_security_self(void)
701 {
702 return &realhost;
703 }
704