2 * Copyright (c) 2000-2009 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988 Carnegie Mellon University
34 * All Rights Reserved.
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.
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.
46 * Carnegie Mellon requests users of this software to return to
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
53 * any improvements or extensions that they make and grant Carnegie Mellon
54 * the rights to redistribute these changes.
60 * processor.c: processor and processor_set manipulation routines.
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>
81 #include <security/mac_mach_internal.h>
87 #include <mach/mach_host_server.h>
88 #include <mach/processor_set_server.h>
90 struct processor_set pset0
;
91 struct pset_node pset_node0
;
92 decl_simple_lock_data(static,pset_node_lock
)
95 queue_head_t terminated_tasks
; /* To be used ONLY for stackshot. */
96 queue_head_t corpse_tasks
;
98 int terminated_tasks_count
;
101 decl_lck_mtx_data(,tasks_threads_lock
)
102 decl_lck_mtx_data(,tasks_corpse_lock
)
104 processor_t processor_list
;
105 unsigned int processor_count
;
106 static processor_t processor_list_tail
;
107 decl_simple_lock_data(,processor_list_lock
)
109 uint32_t processor_avail_count
;
111 processor_t master_processor
;
113 boolean_t sched_stats_active
= FALSE
;
115 processor_t processor_array
[MAX_SCHED_CPUS
] = { 0 };
120 processor_bootstrap(void)
122 pset_init(&pset0
, &pset_node0
);
123 pset_node0
.psets
= &pset0
;
125 simple_lock_init(&pset_node_lock
, 0);
128 queue_init(&terminated_tasks
);
129 queue_init(&threads
);
130 queue_init(&corpse_tasks
);
132 simple_lock_init(&processor_list_lock
, 0);
134 master_processor
= cpu_to_processor(master_cpu
);
136 processor_init(master_processor
, master_cpu
, &pset0
);
140 * Initialize the given processor for the cpu
141 * indicated by cpu_id, and assign to the
142 * specified processor set.
146 processor_t processor
,
148 processor_set_t pset
)
152 if (processor
!= master_processor
) {
153 /* Scheduler state for master_processor initialized in sched_init() */
154 SCHED(processor_init
)(processor
);
157 processor
->state
= PROCESSOR_OFF_LINE
;
158 processor
->active_thread
= processor
->next_thread
= processor
->idle_thread
= THREAD_NULL
;
159 processor
->processor_set
= pset
;
160 processor_state_update_idle(processor
);
161 processor
->starting_pri
= MINPRI
;
162 processor
->cpu_id
= cpu_id
;
163 timer_call_setup(&processor
->quantum_timer
, thread_quantum_expire
, processor
);
164 processor
->quantum_end
= UINT64_MAX
;
165 processor
->deadline
= UINT64_MAX
;
166 processor
->first_timeslice
= FALSE
;
167 processor
->processor_primary
= processor
; /* no SMT relationship known at this point */
168 processor
->processor_secondary
= NULL
;
169 processor
->is_SMT
= FALSE
;
170 processor
->is_recommended
= (pset
->recommended_bitmask
& (1ULL << cpu_id
)) ? TRUE
: FALSE
;
171 processor
->processor_self
= IP_NULL
;
172 processor_data_init(processor
);
173 processor
->processor_list
= NULL
;
177 bit_set(pset
->cpu_bitmask
, cpu_id
);
178 if (pset
->cpu_set_count
++ == 0)
179 pset
->cpu_set_low
= pset
->cpu_set_hi
= cpu_id
;
181 pset
->cpu_set_low
= (cpu_id
< pset
->cpu_set_low
)? cpu_id
: pset
->cpu_set_low
;
182 pset
->cpu_set_hi
= (cpu_id
> pset
->cpu_set_hi
)? cpu_id
: pset
->cpu_set_hi
;
187 simple_lock(&processor_list_lock
);
188 if (processor_list
== NULL
)
189 processor_list
= processor
;
191 processor_list_tail
->processor_list
= processor
;
192 processor_list_tail
= processor
;
194 assert(cpu_id
< MAX_SCHED_CPUS
);
195 processor_array
[cpu_id
] = processor
;
196 simple_unlock(&processor_list_lock
);
200 processor_set_primary(
201 processor_t processor
,
204 assert(processor
->processor_primary
== primary
|| processor
->processor_primary
== processor
);
205 /* Re-adjust primary point for this (possibly) secondary processor */
206 processor
->processor_primary
= primary
;
208 assert(primary
->processor_secondary
== NULL
|| primary
->processor_secondary
== processor
);
209 if (primary
!= processor
) {
210 /* Link primary to secondary, assumes a 2-way SMT model
211 * We'll need to move to a queue if any future architecture
212 * requires otherwise.
214 assert(processor
->processor_secondary
== NULL
);
215 primary
->processor_secondary
= processor
;
216 /* Mark both processors as SMT siblings */
217 primary
->is_SMT
= TRUE
;
218 processor
->is_SMT
= TRUE
;
224 processor_t processor
)
226 return (processor
->processor_set
);
230 processor_state_update_idle(processor_t processor
)
232 processor
->current_pri
= IDLEPRI
;
233 processor
->current_sfi_class
= SFI_CLASS_KERNEL
;
234 processor
->current_recommended_pset_type
= PSET_SMP
;
235 processor
->current_perfctl_class
= PERFCONTROL_CLASS_IDLE
;
239 processor_state_update_from_thread(processor_t processor
, thread_t thread
)
241 processor
->current_pri
= thread
->sched_pri
;
242 processor
->current_sfi_class
= thread
->sfi_class
;
243 processor
->current_recommended_pset_type
= recommended_pset_type(thread
);
244 processor
->current_perfctl_class
= thread_get_perfcontrol_class(thread
);
248 processor_state_update_explicit(processor_t processor
, int pri
, sfi_class_id_t sfi_class
,
249 pset_cluster_type_t pset_type
, perfcontrol_class_t perfctl_class
)
251 processor
->current_pri
= pri
;
252 processor
->current_sfi_class
= sfi_class
;
253 processor
->current_recommended_pset_type
= pset_type
;
254 processor
->current_perfctl_class
= perfctl_class
;
267 /* some schedulers do not support multiple psets */
268 if (SCHED(multiple_psets_enabled
) == FALSE
)
269 return processor_pset(master_processor
);
271 processor_set_t
*prev
, pset
= kalloc(sizeof (*pset
));
273 if (pset
!= PROCESSOR_SET_NULL
) {
274 pset_init(pset
, node
);
276 simple_lock(&pset_node_lock
);
279 while (*prev
!= PROCESSOR_SET_NULL
)
280 prev
= &(*prev
)->pset_list
;
284 simple_unlock(&pset_node_lock
);
291 * Find processor set in specified node with specified cluster_id.
292 * Returns default_pset if not found.
297 processor_set_t default_pset
)
299 simple_lock(&pset_node_lock
);
300 pset_node_t node
= &pset_node0
;
301 processor_set_t pset
= NULL
;
305 while (pset
!= NULL
) {
306 if (pset
->pset_cluster_id
== cluster_id
)
308 pset
= pset
->pset_list
;
310 } while ((node
= node
->node_list
) != NULL
);
311 simple_unlock(&pset_node_lock
);
318 * Initialize the given processor_set structure.
322 processor_set_t pset
,
325 if (pset
!= &pset0
) {
326 /* Scheduler state for pset0 initialized in sched_init() */
327 SCHED(pset_init
)(pset
);
328 SCHED(rt_init
)(pset
);
331 queue_init(&pset
->active_queue
);
332 queue_init(&pset
->idle_queue
);
333 queue_init(&pset
->idle_secondary_queue
);
334 queue_init(&pset
->unused_queue
);
335 pset
->online_processor_count
= 0;
336 pset
->active_processor_count
= 0;
337 pset
->load_average
= 0;
338 pset
->cpu_set_low
= pset
->cpu_set_hi
= 0;
339 pset
->cpu_set_count
= 0;
340 pset
->cpu_bitmask
= 0;
341 pset
->recommended_bitmask
= ~0ULL;
342 pset
->pending_AST_cpu_mask
= 0;
343 #if defined(CONFIG_SCHED_DEFERRED_AST)
344 pset
->pending_deferred_AST_cpu_mask
= 0;
346 pset
->pending_spill_cpu_mask
= 0;
347 pset_lock_init(pset
);
348 pset
->pset_self
= IP_NULL
;
349 pset
->pset_name_self
= IP_NULL
;
350 pset
->pset_list
= PROCESSOR_SET_NULL
;
352 pset
->pset_cluster_type
= PSET_SMP
;
353 pset
->pset_cluster_id
= 0;
357 processor_info_count(
358 processor_flavor_t flavor
,
359 mach_msg_type_number_t
*count
)
363 case PROCESSOR_BASIC_INFO
:
364 *count
= PROCESSOR_BASIC_INFO_COUNT
;
367 case PROCESSOR_CPU_LOAD_INFO
:
368 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
372 return (cpu_info_count(flavor
, count
));
375 return (KERN_SUCCESS
);
381 processor_t processor
,
382 processor_flavor_t flavor
,
384 processor_info_t info
,
385 mach_msg_type_number_t
*count
)
388 kern_return_t result
;
390 if (processor
== PROCESSOR_NULL
)
391 return (KERN_INVALID_ARGUMENT
);
393 cpu_id
= processor
->cpu_id
;
397 case PROCESSOR_BASIC_INFO
:
399 processor_basic_info_t basic_info
;
401 if (*count
< PROCESSOR_BASIC_INFO_COUNT
)
402 return (KERN_FAILURE
);
404 basic_info
= (processor_basic_info_t
) info
;
405 basic_info
->cpu_type
= slot_type(cpu_id
);
406 basic_info
->cpu_subtype
= slot_subtype(cpu_id
);
407 state
= processor
->state
;
408 if (state
== PROCESSOR_OFF_LINE
)
409 basic_info
->running
= FALSE
;
411 basic_info
->running
= TRUE
;
412 basic_info
->slot_num
= cpu_id
;
413 if (processor
== master_processor
)
414 basic_info
->is_master
= TRUE
;
416 basic_info
->is_master
= FALSE
;
418 *count
= PROCESSOR_BASIC_INFO_COUNT
;
421 return (KERN_SUCCESS
);
424 case PROCESSOR_CPU_LOAD_INFO
:
426 processor_cpu_load_info_t cpu_load_info
;
428 uint64_t idle_time_snapshot1
, idle_time_snapshot2
;
429 uint64_t idle_time_tstamp1
, idle_time_tstamp2
;
432 * We capture the accumulated idle time twice over
433 * the course of this function, as well as the timestamps
434 * when each were last updated. Since these are
435 * all done using non-atomic racy mechanisms, the
436 * most we can infer is whether values are stable.
437 * timer_grab() is the only function that can be
438 * used reliably on another processor's per-processor
442 if (*count
< PROCESSOR_CPU_LOAD_INFO_COUNT
)
443 return (KERN_FAILURE
);
445 cpu_load_info
= (processor_cpu_load_info_t
) info
;
446 if (precise_user_kernel_time
) {
447 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] =
448 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, user_state
)) / hz_tick_interval
);
449 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] =
450 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, system_state
)) / hz_tick_interval
);
452 uint64_t tval
= timer_grab(&PROCESSOR_DATA(processor
, user_state
)) +
453 timer_grab(&PROCESSOR_DATA(processor
, system_state
));
455 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = (uint32_t)(tval
/ hz_tick_interval
);
456 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
459 idle_state
= &PROCESSOR_DATA(processor
, idle_state
);
460 idle_time_snapshot1
= timer_grab(idle_state
);
461 idle_time_tstamp1
= idle_state
->tstamp
;
464 * Idle processors are not continually updating their
465 * per-processor idle timer, so it may be extremely
466 * out of date, resulting in an over-representation
467 * of non-idle time between two measurement
468 * intervals by e.g. top(1). If we are non-idle, or
469 * have evidence that the timer is being updated
470 * concurrently, we consider its value up-to-date.
472 if (PROCESSOR_DATA(processor
, current_state
) != idle_state
) {
473 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
474 (uint32_t)(idle_time_snapshot1
/ hz_tick_interval
);
475 } else if ((idle_time_snapshot1
!= (idle_time_snapshot2
= timer_grab(idle_state
))) ||
476 (idle_time_tstamp1
!= (idle_time_tstamp2
= idle_state
->tstamp
))){
477 /* Idle timer is being updated concurrently, second stamp is good enough */
478 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
479 (uint32_t)(idle_time_snapshot2
/ hz_tick_interval
);
482 * Idle timer may be very stale. Fortunately we have established
483 * that idle_time_snapshot1 and idle_time_tstamp1 are unchanging
485 idle_time_snapshot1
+= mach_absolute_time() - idle_time_tstamp1
;
487 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
488 (uint32_t)(idle_time_snapshot1
/ hz_tick_interval
);
491 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
493 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
496 return (KERN_SUCCESS
);
500 result
= cpu_info(flavor
, cpu_id
, info
, count
);
501 if (result
== KERN_SUCCESS
)
510 processor_t processor
)
512 processor_set_t pset
;
514 kern_return_t result
;
517 if (processor
== PROCESSOR_NULL
|| processor
->processor_set
== PROCESSOR_SET_NULL
)
518 return (KERN_INVALID_ARGUMENT
);
520 if (processor
== master_processor
) {
523 prev
= thread_bind(processor
);
524 thread_block(THREAD_CONTINUE_NULL
);
526 result
= cpu_start(processor
->cpu_id
);
534 pset
= processor
->processor_set
;
536 if (processor
->state
!= PROCESSOR_OFF_LINE
) {
540 return (KERN_FAILURE
);
543 processor
->state
= PROCESSOR_START
;
548 * Create the idle processor thread.
550 if (processor
->idle_thread
== THREAD_NULL
) {
551 result
= idle_thread_create(processor
);
552 if (result
!= KERN_SUCCESS
) {
555 processor
->state
= PROCESSOR_OFF_LINE
;
564 * If there is no active thread, the processor
565 * has never been started. Create a dedicated
568 if ( processor
->active_thread
== THREAD_NULL
&&
569 processor
->next_thread
== THREAD_NULL
) {
570 result
= kernel_thread_create((thread_continue_t
)processor_start_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
571 if (result
!= KERN_SUCCESS
) {
574 processor
->state
= PROCESSOR_OFF_LINE
;
583 thread
->bound_processor
= processor
;
584 processor
->next_thread
= thread
;
585 thread
->state
= TH_RUN
;
586 thread
->last_made_runnable_time
= mach_absolute_time();
587 thread_unlock(thread
);
590 thread_deallocate(thread
);
593 if (processor
->processor_self
== IP_NULL
)
594 ipc_processor_init(processor
);
596 result
= cpu_start(processor
->cpu_id
);
597 if (result
!= KERN_SUCCESS
) {
600 processor
->state
= PROCESSOR_OFF_LINE
;
607 ipc_processor_enable(processor
);
609 return (KERN_SUCCESS
);
614 processor_t processor
)
616 if (processor
== PROCESSOR_NULL
)
617 return(KERN_INVALID_ARGUMENT
);
619 return(processor_shutdown(processor
));
624 processor_t processor
,
625 processor_info_t info
,
626 mach_msg_type_number_t count
)
628 if (processor
== PROCESSOR_NULL
)
629 return(KERN_INVALID_ARGUMENT
);
631 return(cpu_control(processor
->cpu_id
, info
, count
));
635 processor_set_create(
636 __unused host_t host
,
637 __unused processor_set_t
*new_set
,
638 __unused processor_set_t
*new_name
)
640 return(KERN_FAILURE
);
644 processor_set_destroy(
645 __unused processor_set_t pset
)
647 return(KERN_FAILURE
);
651 processor_get_assignment(
652 processor_t processor
,
653 processor_set_t
*pset
)
657 if (processor
== PROCESSOR_NULL
)
658 return(KERN_INVALID_ARGUMENT
);
660 state
= processor
->state
;
661 if (state
== PROCESSOR_SHUTDOWN
|| state
== PROCESSOR_OFF_LINE
)
662 return(KERN_FAILURE
);
666 return(KERN_SUCCESS
);
671 processor_set_t pset
,
674 processor_set_info_t info
,
675 mach_msg_type_number_t
*count
)
677 if (pset
== PROCESSOR_SET_NULL
)
678 return(KERN_INVALID_ARGUMENT
);
680 if (flavor
== PROCESSOR_SET_BASIC_INFO
) {
681 processor_set_basic_info_t basic_info
;
683 if (*count
< PROCESSOR_SET_BASIC_INFO_COUNT
)
684 return(KERN_FAILURE
);
686 basic_info
= (processor_set_basic_info_t
) info
;
687 basic_info
->processor_count
= processor_avail_count
;
688 basic_info
->default_policy
= POLICY_TIMESHARE
;
690 *count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
692 return(KERN_SUCCESS
);
694 else if (flavor
== PROCESSOR_SET_TIMESHARE_DEFAULT
) {
695 policy_timeshare_base_t ts_base
;
697 if (*count
< POLICY_TIMESHARE_BASE_COUNT
)
698 return(KERN_FAILURE
);
700 ts_base
= (policy_timeshare_base_t
) info
;
701 ts_base
->base_priority
= BASEPRI_DEFAULT
;
703 *count
= POLICY_TIMESHARE_BASE_COUNT
;
705 return(KERN_SUCCESS
);
707 else if (flavor
== PROCESSOR_SET_FIFO_DEFAULT
) {
708 policy_fifo_base_t fifo_base
;
710 if (*count
< POLICY_FIFO_BASE_COUNT
)
711 return(KERN_FAILURE
);
713 fifo_base
= (policy_fifo_base_t
) info
;
714 fifo_base
->base_priority
= BASEPRI_DEFAULT
;
716 *count
= POLICY_FIFO_BASE_COUNT
;
718 return(KERN_SUCCESS
);
720 else if (flavor
== PROCESSOR_SET_RR_DEFAULT
) {
721 policy_rr_base_t rr_base
;
723 if (*count
< POLICY_RR_BASE_COUNT
)
724 return(KERN_FAILURE
);
726 rr_base
= (policy_rr_base_t
) info
;
727 rr_base
->base_priority
= BASEPRI_DEFAULT
;
728 rr_base
->quantum
= 1;
730 *count
= POLICY_RR_BASE_COUNT
;
732 return(KERN_SUCCESS
);
734 else if (flavor
== PROCESSOR_SET_TIMESHARE_LIMITS
) {
735 policy_timeshare_limit_t ts_limit
;
737 if (*count
< POLICY_TIMESHARE_LIMIT_COUNT
)
738 return(KERN_FAILURE
);
740 ts_limit
= (policy_timeshare_limit_t
) info
;
741 ts_limit
->max_priority
= MAXPRI_KERNEL
;
743 *count
= POLICY_TIMESHARE_LIMIT_COUNT
;
745 return(KERN_SUCCESS
);
747 else if (flavor
== PROCESSOR_SET_FIFO_LIMITS
) {
748 policy_fifo_limit_t fifo_limit
;
750 if (*count
< POLICY_FIFO_LIMIT_COUNT
)
751 return(KERN_FAILURE
);
753 fifo_limit
= (policy_fifo_limit_t
) info
;
754 fifo_limit
->max_priority
= MAXPRI_KERNEL
;
756 *count
= POLICY_FIFO_LIMIT_COUNT
;
758 return(KERN_SUCCESS
);
760 else if (flavor
== PROCESSOR_SET_RR_LIMITS
) {
761 policy_rr_limit_t rr_limit
;
763 if (*count
< POLICY_RR_LIMIT_COUNT
)
764 return(KERN_FAILURE
);
766 rr_limit
= (policy_rr_limit_t
) info
;
767 rr_limit
->max_priority
= MAXPRI_KERNEL
;
769 *count
= POLICY_RR_LIMIT_COUNT
;
771 return(KERN_SUCCESS
);
773 else if (flavor
== PROCESSOR_SET_ENABLED_POLICIES
) {
776 if (*count
< (sizeof(*enabled
)/sizeof(int)))
777 return(KERN_FAILURE
);
779 enabled
= (int *) info
;
780 *enabled
= POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
;
782 *count
= sizeof(*enabled
)/sizeof(int);
784 return(KERN_SUCCESS
);
789 return(KERN_INVALID_ARGUMENT
);
793 * processor_set_statistics
795 * Returns scheduling statistics for a processor set.
798 processor_set_statistics(
799 processor_set_t pset
,
801 processor_set_info_t info
,
802 mach_msg_type_number_t
*count
)
804 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
805 return (KERN_INVALID_PROCESSOR_SET
);
807 if (flavor
== PROCESSOR_SET_LOAD_INFO
) {
808 processor_set_load_info_t load_info
;
810 if (*count
< PROCESSOR_SET_LOAD_INFO_COUNT
)
811 return(KERN_FAILURE
);
813 load_info
= (processor_set_load_info_t
) info
;
815 load_info
->mach_factor
= sched_mach_factor
;
816 load_info
->load_average
= sched_load_average
;
818 load_info
->task_count
= tasks_count
;
819 load_info
->thread_count
= threads_count
;
821 *count
= PROCESSOR_SET_LOAD_INFO_COUNT
;
822 return(KERN_SUCCESS
);
825 return(KERN_INVALID_ARGUMENT
);
829 * processor_set_max_priority:
831 * Specify max priority permitted on processor set. This affects
832 * newly created and assigned threads. Optionally change existing
836 processor_set_max_priority(
837 __unused processor_set_t pset
,
838 __unused
int max_priority
,
839 __unused boolean_t change_threads
)
841 return (KERN_INVALID_ARGUMENT
);
845 * processor_set_policy_enable:
847 * Allow indicated policy on processor set.
851 processor_set_policy_enable(
852 __unused processor_set_t pset
,
855 return (KERN_INVALID_ARGUMENT
);
859 * processor_set_policy_disable:
861 * Forbid indicated policy on processor set. Time sharing cannot
865 processor_set_policy_disable(
866 __unused processor_set_t pset
,
868 __unused boolean_t change_threads
)
870 return (KERN_INVALID_ARGUMENT
);
874 * processor_set_things:
876 * Common internals for processor_set_{threads,tasks}
879 processor_set_things(
880 processor_set_t pset
,
882 mach_msg_type_number_t
*count
,
890 unsigned int actual_tasks
;
891 vm_size_t task_size
, task_size_needed
;
893 thread_t
*thread_list
;
894 unsigned int actual_threads
;
895 vm_size_t thread_size
, thread_size_needed
;
897 void *addr
, *newaddr
;
898 vm_size_t size
, size_needed
;
900 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
901 return (KERN_INVALID_ARGUMENT
);
904 task_size_needed
= 0;
909 thread_size_needed
= 0;
914 lck_mtx_lock(&tasks_threads_lock
);
916 /* do we have the memory we need? */
917 if (type
== PSET_THING_THREAD
)
918 thread_size_needed
= threads_count
* sizeof(void *);
922 task_size_needed
= tasks_count
* sizeof(void *);
924 if (task_size_needed
<= task_size
&&
925 thread_size_needed
<= thread_size
)
928 /* unlock and allocate more memory */
929 lck_mtx_unlock(&tasks_threads_lock
);
931 /* grow task array */
932 if (task_size_needed
> task_size
) {
934 kfree(task_list
, task_size
);
936 assert(task_size_needed
> 0);
937 task_size
= task_size_needed
;
939 task_list
= (task_t
*)kalloc(task_size
);
940 if (task_list
== NULL
) {
941 if (thread_size
!= 0)
942 kfree(thread_list
, thread_size
);
943 return (KERN_RESOURCE_SHORTAGE
);
947 /* grow thread array */
948 if (thread_size_needed
> thread_size
) {
949 if (thread_size
!= 0)
950 kfree(thread_list
, thread_size
);
952 assert(thread_size_needed
> 0);
953 thread_size
= thread_size_needed
;
955 thread_list
= (thread_t
*)kalloc(thread_size
);
956 if (thread_list
== 0) {
958 kfree(task_list
, task_size
);
959 return (KERN_RESOURCE_SHORTAGE
);
964 /* OK, have memory and the list locked */
966 /* If we need it, get the thread list */
967 if (type
== PSET_THING_THREAD
) {
968 for (thread
= (thread_t
)queue_first(&threads
);
969 !queue_end(&threads
, (queue_entry_t
)thread
);
970 thread
= (thread_t
)queue_next(&thread
->threads
)) {
971 #if defined(SECURE_KERNEL)
972 if (thread
->task
!= kernel_task
) {
974 thread_reference_internal(thread
);
975 thread_list
[actual_threads
++] = thread
;
976 #if defined(SECURE_KERNEL)
984 /* get a list of the tasks */
985 for (task
= (task_t
)queue_first(&tasks
);
986 !queue_end(&tasks
, (queue_entry_t
)task
);
987 task
= (task_t
)queue_next(&task
->tasks
)) {
988 #if defined(SECURE_KERNEL)
989 if (task
!= kernel_task
) {
991 task_reference_internal(task
);
992 task_list
[actual_tasks
++] = task
;
993 #if defined(SECURE_KERNEL)
1001 lck_mtx_unlock(&tasks_threads_lock
);
1004 unsigned int j
, used
;
1006 /* for each task, make sure we are allowed to examine it */
1007 for (i
= used
= 0; i
< actual_tasks
; i
++) {
1008 if (mac_task_check_expose_task(task_list
[i
])) {
1009 task_deallocate(task_list
[i
]);
1012 task_list
[used
++] = task_list
[i
];
1014 actual_tasks
= used
;
1015 task_size_needed
= actual_tasks
* sizeof(void *);
1017 if (type
== PSET_THING_THREAD
) {
1019 /* for each thread (if any), make sure it's task is in the allowed list */
1020 for (i
= used
= 0; i
< actual_threads
; i
++) {
1021 boolean_t found_task
= FALSE
;
1023 task
= thread_list
[i
]->task
;
1024 for (j
= 0; j
< actual_tasks
; j
++) {
1025 if (task_list
[j
] == task
) {
1031 thread_list
[used
++] = thread_list
[i
];
1033 thread_deallocate(thread_list
[i
]);
1035 actual_threads
= used
;
1036 thread_size_needed
= actual_threads
* sizeof(void *);
1038 /* done with the task list */
1039 for (i
= 0; i
< actual_tasks
; i
++)
1040 task_deallocate(task_list
[i
]);
1041 kfree(task_list
, task_size
);
1048 if (type
== PSET_THING_THREAD
) {
1049 if (actual_threads
== 0) {
1050 /* no threads available to return */
1051 assert(task_size
== 0);
1052 if (thread_size
!= 0)
1053 kfree(thread_list
, thread_size
);
1056 return KERN_SUCCESS
;
1058 size_needed
= actual_threads
* sizeof(void *);
1062 if (actual_tasks
== 0) {
1063 /* no tasks available to return */
1064 assert(thread_size
== 0);
1066 kfree(task_list
, task_size
);
1069 return KERN_SUCCESS
;
1071 size_needed
= actual_tasks
* sizeof(void *);
1076 /* if we allocated too much, must copy */
1077 if (size_needed
< size
) {
1078 newaddr
= kalloc(size_needed
);
1080 for (i
= 0; i
< actual_tasks
; i
++) {
1081 if (type
== PSET_THING_THREAD
)
1082 thread_deallocate(thread_list
[i
]);
1084 task_deallocate(task_list
[i
]);
1088 return (KERN_RESOURCE_SHORTAGE
);
1091 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
1098 *thing_list
= (void **)addr
;
1099 *count
= (unsigned int)size
/ sizeof(void *);
1101 return (KERN_SUCCESS
);
1106 * processor_set_tasks:
1108 * List all tasks in the processor set.
1111 processor_set_tasks(
1112 processor_set_t pset
,
1113 task_array_t
*task_list
,
1114 mach_msg_type_number_t
*count
)
1117 mach_msg_type_number_t i
;
1119 ret
= processor_set_things(pset
, (void **)task_list
, count
, PSET_THING_TASK
);
1120 if (ret
!= KERN_SUCCESS
)
1123 /* do the conversion that Mig should handle */
1124 for (i
= 0; i
< *count
; i
++)
1125 (*task_list
)[i
] = (task_t
)convert_task_to_port((*task_list
)[i
]);
1126 return KERN_SUCCESS
;
1130 * processor_set_threads:
1132 * List all threads in the processor set.
1134 #if defined(SECURE_KERNEL)
1136 processor_set_threads(
1137 __unused processor_set_t pset
,
1138 __unused thread_array_t
*thread_list
,
1139 __unused mach_msg_type_number_t
*count
)
1141 return KERN_FAILURE
;
1143 #elif defined(CONFIG_EMBEDDED)
1145 processor_set_threads(
1146 __unused processor_set_t pset
,
1147 __unused thread_array_t
*thread_list
,
1148 __unused mach_msg_type_number_t
*count
)
1150 return KERN_NOT_SUPPORTED
;
1154 processor_set_threads(
1155 processor_set_t pset
,
1156 thread_array_t
*thread_list
,
1157 mach_msg_type_number_t
*count
)
1160 mach_msg_type_number_t i
;
1162 ret
= processor_set_things(pset
, (void **)thread_list
, count
, PSET_THING_THREAD
);
1163 if (ret
!= KERN_SUCCESS
)
1166 /* do the conversion that Mig should handle */
1167 for (i
= 0; i
< *count
; i
++)
1168 (*thread_list
)[i
] = (thread_t
)convert_thread_to_port((*thread_list
)[i
]);
1169 return KERN_SUCCESS
;
1174 * processor_set_policy_control
1176 * Controls the scheduling attributes governing the processor set.
1177 * Allows control of enabled policies, and per-policy base and limit
1181 processor_set_policy_control(
1182 __unused processor_set_t pset
,
1183 __unused
int flavor
,
1184 __unused processor_set_info_t policy_info
,
1185 __unused mach_msg_type_number_t count
,
1186 __unused boolean_t change
)
1188 return (KERN_INVALID_ARGUMENT
);
1191 #undef pset_deallocate
1192 void pset_deallocate(processor_set_t pset
);
1195 __unused processor_set_t pset
)
1200 #undef pset_reference
1201 void pset_reference(processor_set_t pset
);
1204 __unused processor_set_t pset
)
1210 recommended_pset_type(thread_t thread
)