]> git.saurik.com Git - apple/xnu.git/blob - osfmk/kern/processor.c
23a5496119f7ea63cce73147b87f3c0cd5b8bdc6
[apple/xnu.git] / osfmk / kern / processor.c
1 /*
2 * Copyright (c) 2000-2009 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 * processor.c: processor and processor_set manipulation routines.
61 */
62
63 #include <mach/boolean.h>
64 #include <mach/policy.h>
65 #include <mach/processor.h>
66 #include <mach/processor_info.h>
67 #include <mach/vm_param.h>
68 #include <kern/cpu_number.h>
69 #include <kern/host.h>
70 #include <kern/machine.h>
71 #include <kern/misc_protos.h>
72 #include <kern/processor.h>
73 #include <kern/sched.h>
74 #include <kern/task.h>
75 #include <kern/thread.h>
76 #include <kern/ipc_host.h>
77 #include <kern/ipc_tt.h>
78 #include <ipc/ipc_port.h>
79 #include <kern/kalloc.h>
80
81 /*
82 * Exported interface
83 */
84 #include <mach/mach_host_server.h>
85 #include <mach/processor_set_server.h>
86
87 struct processor_set pset0;
88 struct pset_node pset_node0;
89 decl_simple_lock_data(static,pset_node_lock)
90
91 queue_head_t tasks;
92 queue_head_t terminated_tasks; /* To be used ONLY for stackshot. */
93 int tasks_count;
94 queue_head_t threads;
95 int threads_count;
96 decl_lck_mtx_data(,tasks_threads_lock)
97
98 processor_t processor_list;
99 unsigned int processor_count;
100 static processor_t processor_list_tail;
101 decl_simple_lock_data(,processor_list_lock)
102
103 uint32_t processor_avail_count;
104
105 processor_t master_processor;
106 int master_cpu = 0;
107 boolean_t sched_stats_active = FALSE;
108
109 /* Forwards */
110 kern_return_t processor_set_things(
111 processor_set_t pset,
112 mach_port_t **thing_list,
113 mach_msg_type_number_t *count,
114 int type);
115
116 void
117 processor_bootstrap(void)
118 {
119 pset_init(&pset0, &pset_node0);
120 pset_node0.psets = &pset0;
121
122 simple_lock_init(&pset_node_lock, 0);
123
124 queue_init(&tasks);
125 queue_init(&terminated_tasks);
126 queue_init(&threads);
127
128 simple_lock_init(&processor_list_lock, 0);
129
130 master_processor = cpu_to_processor(master_cpu);
131
132 processor_init(master_processor, master_cpu, &pset0);
133 }
134
135 /*
136 * Initialize the given processor for the cpu
137 * indicated by cpu_id, and assign to the
138 * specified processor set.
139 */
140 void
141 processor_init(
142 processor_t processor,
143 int cpu_id,
144 processor_set_t pset)
145 {
146 if (processor != master_processor) {
147 /* Scheduler state deferred until sched_init() */
148 SCHED(processor_init)(processor);
149 }
150
151 processor->state = PROCESSOR_OFF_LINE;
152 processor->active_thread = processor->next_thread = processor->idle_thread = THREAD_NULL;
153 processor->processor_set = pset;
154 processor->current_pri = MINPRI;
155 processor->current_thmode = TH_MODE_NONE;
156 processor->cpu_id = cpu_id;
157 timer_call_setup(&processor->quantum_timer, thread_quantum_expire, processor);
158 processor->deadline = UINT64_MAX;
159 processor->timeslice = 0;
160 processor->processor_meta = PROCESSOR_META_NULL;
161 processor->processor_self = IP_NULL;
162 processor_data_init(processor);
163 processor->processor_list = NULL;
164
165 pset_lock(pset);
166 if (pset->cpu_set_count++ == 0)
167 pset->cpu_set_low = pset->cpu_set_hi = cpu_id;
168 else {
169 pset->cpu_set_low = (cpu_id < pset->cpu_set_low)? cpu_id: pset->cpu_set_low;
170 pset->cpu_set_hi = (cpu_id > pset->cpu_set_hi)? cpu_id: pset->cpu_set_hi;
171 }
172 pset_unlock(pset);
173
174 simple_lock(&processor_list_lock);
175 if (processor_list == NULL)
176 processor_list = processor;
177 else
178 processor_list_tail->processor_list = processor;
179 processor_list_tail = processor;
180 processor_count++;
181 simple_unlock(&processor_list_lock);
182 }
183
184 void
185 processor_meta_init(
186 processor_t processor,
187 processor_t primary)
188 {
189 processor_meta_t pmeta = primary->processor_meta;
190
191 if (pmeta == PROCESSOR_META_NULL) {
192 pmeta = kalloc(sizeof (*pmeta));
193
194 queue_init(&pmeta->idle_queue);
195
196 pmeta->primary = primary;
197 }
198
199 processor->processor_meta = pmeta;
200 }
201
202 processor_set_t
203 processor_pset(
204 processor_t processor)
205 {
206 return (processor->processor_set);
207 }
208
209 pset_node_t
210 pset_node_root(void)
211 {
212 return &pset_node0;
213 }
214
215 processor_set_t
216 pset_create(
217 pset_node_t node)
218 {
219 processor_set_t *prev, pset = kalloc(sizeof (*pset));
220
221 if (pset != PROCESSOR_SET_NULL) {
222 pset_init(pset, node);
223
224 simple_lock(&pset_node_lock);
225
226 prev = &node->psets;
227 while (*prev != PROCESSOR_SET_NULL)
228 prev = &(*prev)->pset_list;
229
230 *prev = pset;
231
232 simple_unlock(&pset_node_lock);
233 }
234
235 return (pset);
236 }
237
238 /*
239 * Initialize the given processor_set structure.
240 */
241 void
242 pset_init(
243 processor_set_t pset,
244 pset_node_t node)
245 {
246 if (pset != &pset0) {
247 /* Scheduler state deferred until sched_init() */
248 SCHED(pset_init)(pset);
249 }
250
251 queue_init(&pset->active_queue);
252 queue_init(&pset->idle_queue);
253 pset->online_processor_count = 0;
254 pset_pri_init_hint(pset, PROCESSOR_NULL);
255 pset_count_init_hint(pset, PROCESSOR_NULL);
256 pset->cpu_set_low = pset->cpu_set_hi = 0;
257 pset->cpu_set_count = 0;
258 pset_lock_init(pset);
259 pset->pset_self = IP_NULL;
260 pset->pset_name_self = IP_NULL;
261 pset->pset_list = PROCESSOR_SET_NULL;
262 pset->node = node;
263 }
264
265 kern_return_t
266 processor_info_count(
267 processor_flavor_t flavor,
268 mach_msg_type_number_t *count)
269 {
270 switch (flavor) {
271
272 case PROCESSOR_BASIC_INFO:
273 *count = PROCESSOR_BASIC_INFO_COUNT;
274 break;
275
276 case PROCESSOR_CPU_LOAD_INFO:
277 *count = PROCESSOR_CPU_LOAD_INFO_COUNT;
278 break;
279
280 default:
281 return (cpu_info_count(flavor, count));
282 }
283
284 return (KERN_SUCCESS);
285 }
286
287
288 kern_return_t
289 processor_info(
290 register processor_t processor,
291 processor_flavor_t flavor,
292 host_t *host,
293 processor_info_t info,
294 mach_msg_type_number_t *count)
295 {
296 register int cpu_id, state;
297 kern_return_t result;
298
299 if (processor == PROCESSOR_NULL)
300 return (KERN_INVALID_ARGUMENT);
301
302 cpu_id = processor->cpu_id;
303
304 switch (flavor) {
305
306 case PROCESSOR_BASIC_INFO:
307 {
308 register processor_basic_info_t basic_info;
309
310 if (*count < PROCESSOR_BASIC_INFO_COUNT)
311 return (KERN_FAILURE);
312
313 basic_info = (processor_basic_info_t) info;
314 basic_info->cpu_type = slot_type(cpu_id);
315 basic_info->cpu_subtype = slot_subtype(cpu_id);
316 state = processor->state;
317 if (state == PROCESSOR_OFF_LINE)
318 basic_info->running = FALSE;
319 else
320 basic_info->running = TRUE;
321 basic_info->slot_num = cpu_id;
322 if (processor == master_processor)
323 basic_info->is_master = TRUE;
324 else
325 basic_info->is_master = FALSE;
326
327 *count = PROCESSOR_BASIC_INFO_COUNT;
328 *host = &realhost;
329
330 return (KERN_SUCCESS);
331 }
332
333 case PROCESSOR_CPU_LOAD_INFO:
334 {
335 processor_cpu_load_info_t cpu_load_info;
336 timer_data_t idle_temp;
337 timer_t idle_state;
338
339 if (*count < PROCESSOR_CPU_LOAD_INFO_COUNT)
340 return (KERN_FAILURE);
341
342 cpu_load_info = (processor_cpu_load_info_t) info;
343 if (precise_user_kernel_time) {
344 cpu_load_info->cpu_ticks[CPU_STATE_USER] =
345 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor, user_state)) / hz_tick_interval);
346 cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] =
347 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor, system_state)) / hz_tick_interval);
348 } else {
349 uint64_t tval = timer_grab(&PROCESSOR_DATA(processor, user_state)) +
350 timer_grab(&PROCESSOR_DATA(processor, system_state));
351
352 cpu_load_info->cpu_ticks[CPU_STATE_USER] = (uint32_t)(tval / hz_tick_interval);
353 cpu_load_info->cpu_ticks[CPU_STATE_SYSTEM] = 0;
354 }
355
356 idle_state = &PROCESSOR_DATA(processor, idle_state);
357 idle_temp = *idle_state;
358
359 if (PROCESSOR_DATA(processor, current_state) != idle_state ||
360 timer_grab(&idle_temp) != timer_grab(idle_state)) {
361 cpu_load_info->cpu_ticks[CPU_STATE_IDLE] =
362 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor, idle_state)) / hz_tick_interval);
363 } else {
364 timer_advance(&idle_temp, mach_absolute_time() - idle_temp.tstamp);
365
366 cpu_load_info->cpu_ticks[CPU_STATE_IDLE] =
367 (uint32_t)(timer_grab(&idle_temp) / hz_tick_interval);
368 }
369
370 cpu_load_info->cpu_ticks[CPU_STATE_NICE] = 0;
371
372 *count = PROCESSOR_CPU_LOAD_INFO_COUNT;
373 *host = &realhost;
374
375 return (KERN_SUCCESS);
376 }
377
378 default:
379 result = cpu_info(flavor, cpu_id, info, count);
380 if (result == KERN_SUCCESS)
381 *host = &realhost;
382
383 return (result);
384 }
385 }
386
387 kern_return_t
388 processor_start(
389 processor_t processor)
390 {
391 processor_set_t pset;
392 thread_t thread;
393 kern_return_t result;
394 spl_t s;
395
396 if (processor == PROCESSOR_NULL || processor->processor_set == PROCESSOR_SET_NULL)
397 return (KERN_INVALID_ARGUMENT);
398
399 if (processor == master_processor) {
400 processor_t prev;
401
402 prev = thread_bind(processor);
403 thread_block(THREAD_CONTINUE_NULL);
404
405 result = cpu_start(processor->cpu_id);
406
407 thread_bind(prev);
408
409 return (result);
410 }
411
412 s = splsched();
413 pset = processor->processor_set;
414 pset_lock(pset);
415 if (processor->state != PROCESSOR_OFF_LINE) {
416 pset_unlock(pset);
417 splx(s);
418
419 return (KERN_FAILURE);
420 }
421
422 processor->state = PROCESSOR_START;
423 pset_unlock(pset);
424 splx(s);
425
426 /*
427 * Create the idle processor thread.
428 */
429 if (processor->idle_thread == THREAD_NULL) {
430 result = idle_thread_create(processor);
431 if (result != KERN_SUCCESS) {
432 s = splsched();
433 pset_lock(pset);
434 processor->state = PROCESSOR_OFF_LINE;
435 pset_unlock(pset);
436 splx(s);
437
438 return (result);
439 }
440 }
441
442 /*
443 * If there is no active thread, the processor
444 * has never been started. Create a dedicated
445 * start up thread.
446 */
447 if ( processor->active_thread == THREAD_NULL &&
448 processor->next_thread == THREAD_NULL ) {
449 result = kernel_thread_create((thread_continue_t)processor_start_thread, NULL, MAXPRI_KERNEL, &thread);
450 if (result != KERN_SUCCESS) {
451 s = splsched();
452 pset_lock(pset);
453 processor->state = PROCESSOR_OFF_LINE;
454 pset_unlock(pset);
455 splx(s);
456
457 return (result);
458 }
459
460 s = splsched();
461 thread_lock(thread);
462 thread->bound_processor = processor;
463 processor->next_thread = thread;
464 thread->state = TH_RUN;
465 thread_unlock(thread);
466 splx(s);
467
468 thread_deallocate(thread);
469 }
470
471 if (processor->processor_self == IP_NULL)
472 ipc_processor_init(processor);
473
474 result = cpu_start(processor->cpu_id);
475 if (result != KERN_SUCCESS) {
476 s = splsched();
477 pset_lock(pset);
478 processor->state = PROCESSOR_OFF_LINE;
479 pset_unlock(pset);
480 splx(s);
481
482 return (result);
483 }
484
485 ipc_processor_enable(processor);
486
487 return (KERN_SUCCESS);
488 }
489
490 kern_return_t
491 processor_exit(
492 processor_t processor)
493 {
494 if (processor == PROCESSOR_NULL)
495 return(KERN_INVALID_ARGUMENT);
496
497 return(processor_shutdown(processor));
498 }
499
500 kern_return_t
501 processor_control(
502 processor_t processor,
503 processor_info_t info,
504 mach_msg_type_number_t count)
505 {
506 if (processor == PROCESSOR_NULL)
507 return(KERN_INVALID_ARGUMENT);
508
509 return(cpu_control(processor->cpu_id, info, count));
510 }
511
512 kern_return_t
513 processor_set_create(
514 __unused host_t host,
515 __unused processor_set_t *new_set,
516 __unused processor_set_t *new_name)
517 {
518 return(KERN_FAILURE);
519 }
520
521 kern_return_t
522 processor_set_destroy(
523 __unused processor_set_t pset)
524 {
525 return(KERN_FAILURE);
526 }
527
528 kern_return_t
529 processor_get_assignment(
530 processor_t processor,
531 processor_set_t *pset)
532 {
533 int state;
534
535 if (processor == PROCESSOR_NULL)
536 return(KERN_INVALID_ARGUMENT);
537
538 state = processor->state;
539 if (state == PROCESSOR_SHUTDOWN || state == PROCESSOR_OFF_LINE)
540 return(KERN_FAILURE);
541
542 *pset = &pset0;
543
544 return(KERN_SUCCESS);
545 }
546
547 kern_return_t
548 processor_set_info(
549 processor_set_t pset,
550 int flavor,
551 host_t *host,
552 processor_set_info_t info,
553 mach_msg_type_number_t *count)
554 {
555 if (pset == PROCESSOR_SET_NULL)
556 return(KERN_INVALID_ARGUMENT);
557
558 if (flavor == PROCESSOR_SET_BASIC_INFO) {
559 register processor_set_basic_info_t basic_info;
560
561 if (*count < PROCESSOR_SET_BASIC_INFO_COUNT)
562 return(KERN_FAILURE);
563
564 basic_info = (processor_set_basic_info_t) info;
565 basic_info->processor_count = processor_avail_count;
566 basic_info->default_policy = POLICY_TIMESHARE;
567
568 *count = PROCESSOR_SET_BASIC_INFO_COUNT;
569 *host = &realhost;
570 return(KERN_SUCCESS);
571 }
572 else if (flavor == PROCESSOR_SET_TIMESHARE_DEFAULT) {
573 register policy_timeshare_base_t ts_base;
574
575 if (*count < POLICY_TIMESHARE_BASE_COUNT)
576 return(KERN_FAILURE);
577
578 ts_base = (policy_timeshare_base_t) info;
579 ts_base->base_priority = BASEPRI_DEFAULT;
580
581 *count = POLICY_TIMESHARE_BASE_COUNT;
582 *host = &realhost;
583 return(KERN_SUCCESS);
584 }
585 else if (flavor == PROCESSOR_SET_FIFO_DEFAULT) {
586 register policy_fifo_base_t fifo_base;
587
588 if (*count < POLICY_FIFO_BASE_COUNT)
589 return(KERN_FAILURE);
590
591 fifo_base = (policy_fifo_base_t) info;
592 fifo_base->base_priority = BASEPRI_DEFAULT;
593
594 *count = POLICY_FIFO_BASE_COUNT;
595 *host = &realhost;
596 return(KERN_SUCCESS);
597 }
598 else if (flavor == PROCESSOR_SET_RR_DEFAULT) {
599 register policy_rr_base_t rr_base;
600
601 if (*count < POLICY_RR_BASE_COUNT)
602 return(KERN_FAILURE);
603
604 rr_base = (policy_rr_base_t) info;
605 rr_base->base_priority = BASEPRI_DEFAULT;
606 rr_base->quantum = 1;
607
608 *count = POLICY_RR_BASE_COUNT;
609 *host = &realhost;
610 return(KERN_SUCCESS);
611 }
612 else if (flavor == PROCESSOR_SET_TIMESHARE_LIMITS) {
613 register policy_timeshare_limit_t ts_limit;
614
615 if (*count < POLICY_TIMESHARE_LIMIT_COUNT)
616 return(KERN_FAILURE);
617
618 ts_limit = (policy_timeshare_limit_t) info;
619 ts_limit->max_priority = MAXPRI_KERNEL;
620
621 *count = POLICY_TIMESHARE_LIMIT_COUNT;
622 *host = &realhost;
623 return(KERN_SUCCESS);
624 }
625 else if (flavor == PROCESSOR_SET_FIFO_LIMITS) {
626 register policy_fifo_limit_t fifo_limit;
627
628 if (*count < POLICY_FIFO_LIMIT_COUNT)
629 return(KERN_FAILURE);
630
631 fifo_limit = (policy_fifo_limit_t) info;
632 fifo_limit->max_priority = MAXPRI_KERNEL;
633
634 *count = POLICY_FIFO_LIMIT_COUNT;
635 *host = &realhost;
636 return(KERN_SUCCESS);
637 }
638 else if (flavor == PROCESSOR_SET_RR_LIMITS) {
639 register policy_rr_limit_t rr_limit;
640
641 if (*count < POLICY_RR_LIMIT_COUNT)
642 return(KERN_FAILURE);
643
644 rr_limit = (policy_rr_limit_t) info;
645 rr_limit->max_priority = MAXPRI_KERNEL;
646
647 *count = POLICY_RR_LIMIT_COUNT;
648 *host = &realhost;
649 return(KERN_SUCCESS);
650 }
651 else if (flavor == PROCESSOR_SET_ENABLED_POLICIES) {
652 register int *enabled;
653
654 if (*count < (sizeof(*enabled)/sizeof(int)))
655 return(KERN_FAILURE);
656
657 enabled = (int *) info;
658 *enabled = POLICY_TIMESHARE | POLICY_RR | POLICY_FIFO;
659
660 *count = sizeof(*enabled)/sizeof(int);
661 *host = &realhost;
662 return(KERN_SUCCESS);
663 }
664
665
666 *host = HOST_NULL;
667 return(KERN_INVALID_ARGUMENT);
668 }
669
670 /*
671 * processor_set_statistics
672 *
673 * Returns scheduling statistics for a processor set.
674 */
675 kern_return_t
676 processor_set_statistics(
677 processor_set_t pset,
678 int flavor,
679 processor_set_info_t info,
680 mach_msg_type_number_t *count)
681 {
682 if (pset == PROCESSOR_SET_NULL || pset != &pset0)
683 return (KERN_INVALID_PROCESSOR_SET);
684
685 if (flavor == PROCESSOR_SET_LOAD_INFO) {
686 register processor_set_load_info_t load_info;
687
688 if (*count < PROCESSOR_SET_LOAD_INFO_COUNT)
689 return(KERN_FAILURE);
690
691 load_info = (processor_set_load_info_t) info;
692
693 load_info->mach_factor = sched_mach_factor;
694 load_info->load_average = sched_load_average;
695
696 load_info->task_count = tasks_count;
697 load_info->thread_count = threads_count;
698
699 *count = PROCESSOR_SET_LOAD_INFO_COUNT;
700 return(KERN_SUCCESS);
701 }
702
703 return(KERN_INVALID_ARGUMENT);
704 }
705
706 /*
707 * processor_set_max_priority:
708 *
709 * Specify max priority permitted on processor set. This affects
710 * newly created and assigned threads. Optionally change existing
711 * ones.
712 */
713 kern_return_t
714 processor_set_max_priority(
715 __unused processor_set_t pset,
716 __unused int max_priority,
717 __unused boolean_t change_threads)
718 {
719 return (KERN_INVALID_ARGUMENT);
720 }
721
722 /*
723 * processor_set_policy_enable:
724 *
725 * Allow indicated policy on processor set.
726 */
727
728 kern_return_t
729 processor_set_policy_enable(
730 __unused processor_set_t pset,
731 __unused int policy)
732 {
733 return (KERN_INVALID_ARGUMENT);
734 }
735
736 /*
737 * processor_set_policy_disable:
738 *
739 * Forbid indicated policy on processor set. Time sharing cannot
740 * be forbidden.
741 */
742 kern_return_t
743 processor_set_policy_disable(
744 __unused processor_set_t pset,
745 __unused int policy,
746 __unused boolean_t change_threads)
747 {
748 return (KERN_INVALID_ARGUMENT);
749 }
750
751 #define THING_TASK 0
752 #define THING_THREAD 1
753
754 /*
755 * processor_set_things:
756 *
757 * Common internals for processor_set_{threads,tasks}
758 */
759 kern_return_t
760 processor_set_things(
761 processor_set_t pset,
762 mach_port_t **thing_list,
763 mach_msg_type_number_t *count,
764 int type)
765 {
766 unsigned int actual; /* this many things */
767 unsigned int maxthings;
768 unsigned int i;
769
770 vm_size_t size, size_needed;
771 void *addr;
772
773 if (pset == PROCESSOR_SET_NULL || pset != &pset0)
774 return (KERN_INVALID_ARGUMENT);
775
776 size = 0;
777 addr = NULL;
778
779 for (;;) {
780 lck_mtx_lock(&tasks_threads_lock);
781
782 if (type == THING_TASK)
783 maxthings = tasks_count;
784 else
785 maxthings = threads_count;
786
787 /* do we have the memory we need? */
788
789 size_needed = maxthings * sizeof (mach_port_t);
790 if (size_needed <= size)
791 break;
792
793 /* unlock and allocate more memory */
794 lck_mtx_unlock(&tasks_threads_lock);
795
796 if (size != 0)
797 kfree(addr, size);
798
799 assert(size_needed > 0);
800 size = size_needed;
801
802 addr = kalloc(size);
803 if (addr == 0)
804 return (KERN_RESOURCE_SHORTAGE);
805 }
806
807 /* OK, have memory and the list locked */
808
809 actual = 0;
810 switch (type) {
811
812 case THING_TASK: {
813 task_t task, *task_list = (task_t *)addr;
814
815 for (task = (task_t)queue_first(&tasks);
816 !queue_end(&tasks, (queue_entry_t)task);
817 task = (task_t)queue_next(&task->tasks)) {
818 #if defined(SECURE_KERNEL)
819 if (task != kernel_task) {
820 #endif
821 task_reference_internal(task);
822 task_list[actual++] = task;
823 #if defined(SECURE_KERNEL)
824 }
825 #endif
826 }
827
828 break;
829 }
830
831 case THING_THREAD: {
832 thread_t thread, *thread_list = (thread_t *)addr;
833
834 for (thread = (thread_t)queue_first(&threads);
835 !queue_end(&threads, (queue_entry_t)thread);
836 thread = (thread_t)queue_next(&thread->threads)) {
837 thread_reference_internal(thread);
838 thread_list[actual++] = thread;
839 }
840
841 break;
842 }
843
844 }
845
846 lck_mtx_unlock(&tasks_threads_lock);
847
848 if (actual < maxthings)
849 size_needed = actual * sizeof (mach_port_t);
850
851 if (actual == 0) {
852 /* no things, so return null pointer and deallocate memory */
853 *thing_list = NULL;
854 *count = 0;
855
856 if (size != 0)
857 kfree(addr, size);
858 }
859 else {
860 /* if we allocated too much, must copy */
861
862 if (size_needed < size) {
863 void *newaddr;
864
865 newaddr = kalloc(size_needed);
866 if (newaddr == 0) {
867 switch (type) {
868
869 case THING_TASK: {
870 task_t *task_list = (task_t *)addr;
871
872 for (i = 0; i < actual; i++)
873 task_deallocate(task_list[i]);
874 break;
875 }
876
877 case THING_THREAD: {
878 thread_t *thread_list = (thread_t *)addr;
879
880 for (i = 0; i < actual; i++)
881 thread_deallocate(thread_list[i]);
882 break;
883 }
884
885 }
886
887 kfree(addr, size);
888 return (KERN_RESOURCE_SHORTAGE);
889 }
890
891 bcopy((void *) addr, (void *) newaddr, size_needed);
892 kfree(addr, size);
893 addr = newaddr;
894 }
895
896 *thing_list = (mach_port_t *)addr;
897 *count = actual;
898
899 /* do the conversion that Mig should handle */
900
901 switch (type) {
902
903 case THING_TASK: {
904 task_t *task_list = (task_t *)addr;
905
906 for (i = 0; i < actual; i++)
907 (*thing_list)[i] = convert_task_to_port(task_list[i]);
908 break;
909 }
910
911 case THING_THREAD: {
912 thread_t *thread_list = (thread_t *)addr;
913
914 for (i = 0; i < actual; i++)
915 (*thing_list)[i] = convert_thread_to_port(thread_list[i]);
916 break;
917 }
918
919 }
920 }
921
922 return (KERN_SUCCESS);
923 }
924
925
926 /*
927 * processor_set_tasks:
928 *
929 * List all tasks in the processor set.
930 */
931 kern_return_t
932 processor_set_tasks(
933 processor_set_t pset,
934 task_array_t *task_list,
935 mach_msg_type_number_t *count)
936 {
937 return(processor_set_things(pset, (mach_port_t **)task_list, count, THING_TASK));
938 }
939
940 /*
941 * processor_set_threads:
942 *
943 * List all threads in the processor set.
944 */
945 #if defined(SECURE_KERNEL)
946 kern_return_t
947 processor_set_threads(
948 __unused processor_set_t pset,
949 __unused thread_array_t *thread_list,
950 __unused mach_msg_type_number_t *count)
951 {
952 return KERN_FAILURE;
953 }
954 #elif defined(CONFIG_EMBEDDED)
955 kern_return_t
956 processor_set_threads(
957 __unused processor_set_t pset,
958 __unused thread_array_t *thread_list,
959 __unused mach_msg_type_number_t *count)
960 {
961 return KERN_NOT_SUPPORTED;
962 }
963 #else
964 kern_return_t
965 processor_set_threads(
966 processor_set_t pset,
967 thread_array_t *thread_list,
968 mach_msg_type_number_t *count)
969 {
970 return(processor_set_things(pset, (mach_port_t **)thread_list, count, THING_THREAD));
971 }
972 #endif
973
974 /*
975 * processor_set_policy_control
976 *
977 * Controls the scheduling attributes governing the processor set.
978 * Allows control of enabled policies, and per-policy base and limit
979 * priorities.
980 */
981 kern_return_t
982 processor_set_policy_control(
983 __unused processor_set_t pset,
984 __unused int flavor,
985 __unused processor_set_info_t policy_info,
986 __unused mach_msg_type_number_t count,
987 __unused boolean_t change)
988 {
989 return (KERN_INVALID_ARGUMENT);
990 }
991
992 #undef pset_deallocate
993 void pset_deallocate(processor_set_t pset);
994 void
995 pset_deallocate(
996 __unused processor_set_t pset)
997 {
998 return;
999 }
1000
1001 #undef pset_reference
1002 void pset_reference(processor_set_t pset);
1003 void
1004 pset_reference(
1005 __unused processor_set_t pset)
1006 {
1007 return;
1008 }