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