]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/host.c
xnu-792.24.17.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_HEADER_START@
5 *
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.
11 *
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
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * @OSF_COPYRIGHT@
24 */
25 /*
26 * Mach Operating System
27 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
28 * All Rights Reserved.
29 *
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.
35 *
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.
39 *
40 * Carnegie Mellon requests users of this software to return to
41 *
42 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
43 * School of Computer Science
44 * Carnegie Mellon University
45 * Pittsburgh PA 15213-3890
46 *
47 * any improvements or extensions that they make and grant Carnegie Mellon
48 * the rights to redistribute these changes.
49 */
50 /*
51 */
52
53 /*
54 * host.c
55 *
56 * Non-ipc host functions.
57 */
58
59 #include <mach_host.h>
60
61 #include <mach/mach_types.h>
62 #include <mach/boolean.h>
63 #include <mach/host_info.h>
64 #include <mach/host_special_ports.h>
65 #include <mach/kern_return.h>
66 #include <mach/machine.h>
67 #include <mach/port.h>
68 #include <mach/processor_info.h>
69 #include <mach/vm_param.h>
70 #include <mach/processor.h>
71 #include <mach/mach_host_server.h>
72 #include <mach/host_priv_server.h>
73 #include <mach/vm_map.h>
74
75 #include <kern/kern_types.h>
76 #include <kern/assert.h>
77 #include <kern/kalloc.h>
78 #include <kern/host.h>
79 #include <kern/host_statistics.h>
80 #include <kern/ipc_host.h>
81 #include <kern/misc_protos.h>
82 #include <kern/sched.h>
83 #include <kern/processor.h>
84
85 #include <vm/vm_map.h>
86
87 #if DIPC
88 #include <dipc/dipc_funcs.h>
89 #include <dipc/special_ports.h>
90 #endif
91
92 host_data_t realhost;
93
94 kern_return_t
95 host_processors(
96 host_priv_t host_priv,
97 processor_array_t *out_array,
98 mach_msg_type_number_t *countp)
99 {
100 register processor_t processor, *tp;
101 void *addr;
102 unsigned int count, i;
103
104 if (host_priv == HOST_PRIV_NULL)
105 return (KERN_INVALID_ARGUMENT);
106
107 assert(host_priv == &realhost);
108
109 count = processor_count;
110 assert(count != 0);
111
112 addr = kalloc((vm_size_t) (count * sizeof(mach_port_t)));
113 if (addr == 0)
114 return (KERN_RESOURCE_SHORTAGE);
115
116 tp = (processor_t *) addr;
117 *tp++ = processor = processor_list;
118
119 if (count > 1) {
120 simple_lock(&processor_list_lock);
121
122 for (i = 1; i < count; i++)
123 *tp++ = processor = processor->processor_list;
124
125 simple_unlock(&processor_list_lock);
126 }
127
128 *countp = count;
129 *out_array = (processor_array_t)addr;
130
131 /* do the conversion that Mig should handle */
132
133 tp = (processor_t *) addr;
134 for (i = 0; i < count; i++)
135 ((mach_port_t *) tp)[i] =
136 (mach_port_t)convert_processor_to_port(tp[i]);
137
138 return (KERN_SUCCESS);
139 }
140
141 kern_return_t
142 host_info(
143 host_t host,
144 host_flavor_t flavor,
145 host_info_t info,
146 mach_msg_type_number_t *count)
147 {
148
149 if (host == HOST_NULL)
150 return (KERN_INVALID_ARGUMENT);
151
152 switch (flavor) {
153
154 case HOST_BASIC_INFO:
155 {
156 register host_basic_info_t basic_info;
157 register int master_slot;
158
159 /*
160 * Basic information about this host.
161 */
162 if (*count < HOST_BASIC_INFO_OLD_COUNT)
163 return (KERN_FAILURE);
164
165 basic_info = (host_basic_info_t) info;
166
167 basic_info->max_cpus = machine_info.max_cpus;
168 basic_info->avail_cpus = machine_info.avail_cpus;
169 basic_info->memory_size = machine_info.memory_size;
170 master_slot = PROCESSOR_DATA(master_processor, slot_num);
171 basic_info->cpu_type = slot_type(master_slot);
172 basic_info->cpu_subtype = slot_subtype(master_slot);
173
174 if (*count >= HOST_BASIC_INFO_COUNT) {
175 basic_info->cpu_threadtype = slot_threadtype(master_slot);
176 basic_info->physical_cpu = machine_info.physical_cpu;
177 basic_info->physical_cpu_max = machine_info.physical_cpu_max;
178 basic_info->logical_cpu = machine_info.logical_cpu;
179 basic_info->logical_cpu_max = machine_info.logical_cpu_max;
180 basic_info->max_mem = machine_info.max_mem;
181
182 *count = HOST_BASIC_INFO_COUNT;
183 } else {
184 *count = HOST_BASIC_INFO_OLD_COUNT;
185 }
186
187 return (KERN_SUCCESS);
188 }
189
190 case HOST_SCHED_INFO:
191 {
192 register host_sched_info_t sched_info;
193
194 /*
195 * Return scheduler information.
196 */
197 if (*count < HOST_SCHED_INFO_COUNT)
198 return (KERN_FAILURE);
199
200 sched_info = (host_sched_info_t) info;
201
202 sched_info->min_timeout =
203 sched_info->min_quantum = std_quantum_us / 1000;
204
205 *count = HOST_SCHED_INFO_COUNT;
206
207 return (KERN_SUCCESS);
208 }
209
210 case HOST_RESOURCE_SIZES:
211 {
212 /*
213 * Return sizes of kernel data structures
214 */
215 if (*count < HOST_RESOURCE_SIZES_COUNT)
216 return (KERN_FAILURE);
217
218 /* XXX Fail until ledgers are implemented */
219 return (KERN_INVALID_ARGUMENT);
220 }
221
222 case HOST_PRIORITY_INFO:
223 {
224 register host_priority_info_t priority_info;
225
226 if (*count < HOST_PRIORITY_INFO_COUNT)
227 return (KERN_FAILURE);
228
229 priority_info = (host_priority_info_t) info;
230
231 priority_info->kernel_priority = MINPRI_KERNEL;
232 priority_info->system_priority = MINPRI_KERNEL;
233 priority_info->server_priority = MINPRI_RESERVED;
234 priority_info->user_priority = BASEPRI_DEFAULT;
235 priority_info->depress_priority = DEPRESSPRI;
236 priority_info->idle_priority = IDLEPRI;
237 priority_info->minimum_priority = MINPRI_USER;
238 priority_info->maximum_priority = MAXPRI_RESERVED;
239
240 *count = HOST_PRIORITY_INFO_COUNT;
241
242 return (KERN_SUCCESS);
243 }
244
245 /*
246 * Gestalt for various trap facilities.
247 */
248 case HOST_MACH_MSG_TRAP:
249 case HOST_SEMAPHORE_TRAPS:
250 {
251 *count = 0;
252 return (KERN_SUCCESS);
253 }
254
255 default:
256 return (KERN_INVALID_ARGUMENT);
257 }
258 }
259
260 kern_return_t
261 host_statistics(
262 host_t host,
263 host_flavor_t flavor,
264 host_info_t info,
265 mach_msg_type_number_t *count)
266 {
267
268 if (host == HOST_NULL)
269 return (KERN_INVALID_HOST);
270
271 switch(flavor) {
272
273 case HOST_LOAD_INFO:
274 {
275 host_load_info_t load_info;
276
277 if (*count < HOST_LOAD_INFO_COUNT)
278 return (KERN_FAILURE);
279
280 load_info = (host_load_info_t) info;
281
282 bcopy((char *) avenrun,
283 (char *) load_info->avenrun, sizeof avenrun);
284 bcopy((char *) mach_factor,
285 (char *) load_info->mach_factor, sizeof mach_factor);
286
287 *count = HOST_LOAD_INFO_COUNT;
288 return (KERN_SUCCESS);
289 }
290
291 case HOST_VM_INFO:
292 {
293 register processor_t processor;
294 register vm_statistics_t stat;
295 vm_statistics_data_t host_vm_stat;
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;
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 if (*count >= HOST_VM_INFO_COUNT) {
339 /* info that was not in revision 0 of that interface */
340 stat->purgeable_count = vm_page_purgeable_count;
341 stat->purges = vm_page_purged_count;
342 *count = HOST_VM_INFO_COUNT;
343 } else {
344 *count = HOST_VM_INFO_REV0_COUNT;
345 }
346
347 return (KERN_SUCCESS);
348 }
349
350 case HOST_CPU_LOAD_INFO:
351 {
352 register processor_t processor;
353 host_cpu_load_info_t cpu_load_info;
354 unsigned long ticks_value1, ticks_value2;
355
356 if (*count < HOST_CPU_LOAD_INFO_COUNT)
357 return (KERN_FAILURE);
358
359 #define GET_TICKS_VALUE(processor, state) \
360 MACRO_BEGIN \
361 do { \
362 ticks_value1 = *(volatile integer_t *) \
363 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
364 ticks_value2 = *(volatile integer_t *) \
365 &PROCESSOR_DATA((processor), cpu_ticks[(state)]); \
366 } while (ticks_value1 != ticks_value2); \
367 \
368 cpu_load_info->cpu_ticks[(state)] += ticks_value1; \
369 MACRO_END
370
371 cpu_load_info = (host_cpu_load_info_t)info;
372 cpu_load_info->cpu_ticks[CPU_STATE_USER] = 0;
373 cpu_load_info->cpu_ticks[CPU_STATE_NICE] = 0;
374 cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] = 0;
375 cpu_load_info->cpu_ticks[CPU_STATE_IDLE] = 0;
376
377 processor = processor_list;
378 GET_TICKS_VALUE(processor, CPU_STATE_USER);
379 GET_TICKS_VALUE(processor, CPU_STATE_NICE);
380 GET_TICKS_VALUE(processor, CPU_STATE_SYSTEM);
381 GET_TICKS_VALUE(processor, CPU_STATE_IDLE);
382
383 if (processor_count > 1) {
384 simple_lock(&processor_list_lock);
385
386 while ((processor = processor->processor_list) != NULL) {
387 GET_TICKS_VALUE(processor, CPU_STATE_USER);
388 GET_TICKS_VALUE(processor, CPU_STATE_NICE);
389 GET_TICKS_VALUE(processor, CPU_STATE_SYSTEM);
390 GET_TICKS_VALUE(processor, CPU_STATE_IDLE);
391 }
392
393 simple_unlock(&processor_list_lock);
394 }
395
396 *count = HOST_CPU_LOAD_INFO_COUNT;
397
398 return (KERN_SUCCESS);
399 }
400
401 default:
402 return (KERN_INVALID_ARGUMENT);
403 }
404 }
405
406 /*
407 * Get host statistics that require privilege.
408 * None for now, just call the un-privileged version.
409 */
410 kern_return_t
411 host_priv_statistics(
412 host_priv_t host_priv,
413 host_flavor_t flavor,
414 host_info_t info,
415 mach_msg_type_number_t *count)
416 {
417 return(host_statistics((host_t)host_priv, flavor, info, count));
418 }
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 /* take ref for convert_pset_name_to_port */
480 pset_reference(&default_pset);
481 /* do the conversion that Mig should handle */
482 *((ipc_port_t *) addr) = convert_pset_name_to_port(&default_pset);
483
484 *pset_list = (processor_set_array_t)addr;
485 *count = 1;
486
487 return KERN_SUCCESS;
488 }
489
490 /*
491 * host_processor_set_priv:
492 *
493 * Return control port for given processor set.
494 */
495 kern_return_t
496 host_processor_set_priv(
497 host_priv_t host_priv,
498 processor_set_t pset_name,
499 processor_set_t *pset)
500 {
501 if ((host_priv == HOST_PRIV_NULL) || (pset_name == PROCESSOR_SET_NULL)) {
502 *pset = PROCESSOR_SET_NULL;
503 return(KERN_INVALID_ARGUMENT);
504 }
505
506 *pset = pset_name;
507 pset_reference(*pset);
508 return(KERN_SUCCESS);
509 }
510
511 /*
512 * host_processor_info
513 *
514 * Return info about the processors on this host. It will return
515 * the number of processors, and the specific type of info requested
516 * in an OOL array.
517 */
518 kern_return_t
519 host_processor_info(
520 host_t host,
521 processor_flavor_t flavor,
522 natural_t *out_pcount,
523 processor_info_array_t *out_array,
524 mach_msg_type_number_t *out_array_count)
525 {
526 kern_return_t result;
527 processor_t processor;
528 host_t thost;
529 processor_info_t info;
530 unsigned int icount, tcount;
531 unsigned int pcount, i;
532 vm_offset_t addr;
533 vm_size_t size, needed;
534 vm_map_copy_t copy;
535
536 if (host == HOST_NULL)
537 return (KERN_INVALID_ARGUMENT);
538
539 result = processor_info_count(flavor, &icount);
540 if (result != KERN_SUCCESS)
541 return (result);
542
543 pcount = processor_count;
544 assert(pcount != 0);
545
546 needed = pcount * icount * sizeof(natural_t);
547 size = round_page(needed);
548 result = kmem_alloc(ipc_kernel_map, &addr, size);
549 if (result != KERN_SUCCESS)
550 return (KERN_RESOURCE_SHORTAGE);
551
552 info = (processor_info_t) addr;
553 processor = processor_list;
554 tcount = icount;
555
556 result = processor_info(processor, flavor, &thost, info, &tcount);
557 if (result != KERN_SUCCESS) {
558 kmem_free(ipc_kernel_map, addr, size);
559 return (result);
560 }
561
562 if (pcount > 1) {
563 for (i = 1; i < pcount; i++) {
564 simple_lock(&processor_list_lock);
565 processor = processor->processor_list;
566 simple_unlock(&processor_list_lock);
567
568 info += icount;
569 tcount = icount;
570 result = processor_info(processor, flavor, &thost, info, &tcount);
571 if (result != KERN_SUCCESS) {
572 kmem_free(ipc_kernel_map, addr, size);
573 return (result);
574 }
575 }
576 }
577
578 if (size != needed)
579 bzero((char *) addr + needed, size - needed);
580
581 result = vm_map_unwire(ipc_kernel_map, vm_map_trunc_page(addr),
582 vm_map_round_page(addr + size), FALSE);
583 assert(result == KERN_SUCCESS);
584 result = vm_map_copyin(ipc_kernel_map, (vm_map_address_t)addr,
585 (vm_map_size_t)size, TRUE, &copy);
586 assert(result == KERN_SUCCESS);
587
588 *out_pcount = pcount;
589 *out_array = (processor_info_array_t) copy;
590 *out_array_count = pcount * icount;
591
592 return (KERN_SUCCESS);
593 }
594
595 /*
596 * Kernel interface for setting a special port.
597 */
598 kern_return_t
599 kernel_set_special_port(
600 host_priv_t host_priv,
601 int id,
602 ipc_port_t port)
603 {
604 ipc_port_t old_port;
605
606 host_lock(host_priv);
607 old_port = host_priv->special[id];
608 host_priv->special[id] = port;
609 host_unlock(host_priv);
610 if (IP_VALID(old_port))
611 ipc_port_release_send(old_port);
612 return KERN_SUCCESS;
613 }
614
615 /*
616 * User interface for setting a special port.
617 *
618 * Only permits the user to set a user-owned special port
619 * ID, rejecting a kernel-owned special port ID.
620 *
621 * A special kernel port cannot be set up using this
622 * routine; use kernel_set_special_port() instead.
623 */
624 kern_return_t
625 host_set_special_port(
626 host_priv_t host_priv,
627 int id,
628 ipc_port_t port)
629 {
630 if (host_priv == HOST_PRIV_NULL ||
631 id <= HOST_MAX_SPECIAL_KERNEL_PORT || id > HOST_MAX_SPECIAL_PORT ) {
632 if (IP_VALID(port))
633 ipc_port_release_send(port);
634 return KERN_INVALID_ARGUMENT;
635 }
636
637 return kernel_set_special_port(host_priv, id, port);
638 }
639
640
641 /*
642 * User interface for retrieving a special port.
643 *
644 * Note that there is nothing to prevent a user special
645 * port from disappearing after it has been discovered by
646 * the caller; thus, using a special port can always result
647 * in a "port not valid" error.
648 */
649
650 kern_return_t
651 host_get_special_port(
652 host_priv_t host_priv,
653 __unused int node,
654 int id,
655 ipc_port_t *portp)
656 {
657 ipc_port_t port;
658
659 if (host_priv == HOST_PRIV_NULL ||
660 id == HOST_SECURITY_PORT || id > HOST_MAX_SPECIAL_PORT )
661 return KERN_INVALID_ARGUMENT;
662
663 #if DIPC
664 if (node != HOST_LOCAL_NODE)
665 return norma_get_special_port(host_priv, node, id, portp);
666 #endif
667
668 host_lock(host_priv);
669 port = realhost.special[id];
670 *portp = ipc_port_copy_send(port);
671 host_unlock(host_priv);
672
673 return KERN_SUCCESS;
674 }
675
676
677 /*
678 * host_get_io_master
679 *
680 * Return the IO master access port for this host.
681 */
682 kern_return_t
683 host_get_io_master(
684 host_t host,
685 io_master_t *io_masterp)
686 {
687 if (host == HOST_NULL)
688 return KERN_INVALID_ARGUMENT;
689
690 return (host_get_io_master_port(host_priv_self(), io_masterp));
691 }
692
693 host_t
694 host_self(void)
695 {
696 return &realhost;
697 }
698
699 host_priv_t
700 host_priv_self(void)
701 {
702 return &realhost;
703 }
704
705 host_security_t
706 host_security_self(void)
707 {
708 return &realhost;
709 }
710