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>
83 #if defined(CONFIG_XNUPOST)
85 #include <tests/xnupost.h>
87 #endif /* CONFIG_XNUPOST */
92 #include <mach/mach_host_server.h>
93 #include <mach/processor_set_server.h>
95 struct processor_set pset0
;
96 struct pset_node pset_node0
;
97 decl_simple_lock_data(static, pset_node_lock
)
99 lck_grp_t pset_lck_grp
;
102 queue_head_t terminated_tasks
; /* To be used ONLY for stackshot. */
103 queue_head_t corpse_tasks
;
105 int terminated_tasks_count
;
106 queue_head_t threads
;
108 decl_lck_mtx_data(, tasks_threads_lock
)
109 decl_lck_mtx_data(, tasks_corpse_lock
)
111 processor_t processor_list
;
112 unsigned int processor_count
;
113 static processor_t processor_list_tail
;
114 decl_simple_lock_data(, processor_list_lock
)
116 uint32_t processor_avail_count
;
117 uint32_t processor_avail_count_user
;
119 processor_t master_processor
;
121 boolean_t sched_stats_active
= FALSE
;
123 processor_t processor_array
[MAX_SCHED_CPUS
] = { 0 };
125 #if defined(CONFIG_XNUPOST)
126 kern_return_t
ipi_test(void);
127 extern void arm64_ipi_test(void);
135 for (p
= processor_list
; p
!= NULL
; p
= p
->processor_list
) {
137 thread_block(THREAD_CONTINUE_NULL
);
138 kprintf("Running IPI test on cpu %d\n", p
->cpu_id
);
142 /* unbind thread from specific cpu */
143 thread_bind(PROCESSOR_NULL
);
144 thread_block(THREAD_CONTINUE_NULL
);
146 T_PASS("Done running IPI tests");
148 T_PASS("Unsupported platform. Not running IPI tests");
150 #endif /* __arm64__ */
154 #endif /* defined(CONFIG_XNUPOST) */
156 int sched_enable_smt
= 1;
159 processor_bootstrap(void)
161 lck_grp_init(&pset_lck_grp
, "pset", LCK_GRP_ATTR_NULL
);
163 simple_lock_init(&pset_node_lock
, 0);
165 pset_node0
.psets
= &pset0
;
166 pset_init(&pset0
, &pset_node0
);
169 queue_init(&terminated_tasks
);
170 queue_init(&threads
);
171 queue_init(&corpse_tasks
);
173 simple_lock_init(&processor_list_lock
, 0);
175 master_processor
= cpu_to_processor(master_cpu
);
177 processor_init(master_processor
, master_cpu
, &pset0
);
181 * Initialize the given processor for the cpu
182 * indicated by cpu_id, and assign to the
183 * specified processor set.
187 processor_t processor
,
189 processor_set_t pset
)
193 if (processor
!= master_processor
) {
194 /* Scheduler state for master_processor initialized in sched_init() */
195 SCHED(processor_init
)(processor
);
198 assert(cpu_id
< MAX_SCHED_CPUS
);
200 processor
->state
= PROCESSOR_OFF_LINE
;
201 processor
->active_thread
= processor
->next_thread
= processor
->idle_thread
= THREAD_NULL
;
202 processor
->processor_set
= pset
;
203 processor_state_update_idle(processor
);
204 processor
->starting_pri
= MINPRI
;
205 processor
->cpu_id
= cpu_id
;
206 timer_call_setup(&processor
->quantum_timer
, thread_quantum_expire
, processor
);
207 processor
->quantum_end
= UINT64_MAX
;
208 processor
->deadline
= UINT64_MAX
;
209 processor
->first_timeslice
= FALSE
;
210 processor
->processor_primary
= processor
; /* no SMT relationship known at this point */
211 processor
->processor_secondary
= NULL
;
212 processor
->is_SMT
= FALSE
;
213 processor
->is_recommended
= (pset
->recommended_bitmask
& (1ULL << cpu_id
)) ? TRUE
: FALSE
;
214 processor
->processor_self
= IP_NULL
;
215 processor_data_init(processor
);
216 processor
->processor_list
= NULL
;
217 processor
->cpu_quiesce_state
= CPU_QUIESCE_COUNTER_NONE
;
218 processor
->cpu_quiesce_last_checkin
= 0;
219 processor
->must_idle
= false;
223 bit_set(pset
->cpu_bitmask
, cpu_id
);
224 if (pset
->cpu_set_count
++ == 0) {
225 pset
->cpu_set_low
= pset
->cpu_set_hi
= cpu_id
;
227 pset
->cpu_set_low
= (cpu_id
< pset
->cpu_set_low
)? cpu_id
: pset
->cpu_set_low
;
228 pset
->cpu_set_hi
= (cpu_id
> pset
->cpu_set_hi
)? cpu_id
: pset
->cpu_set_hi
;
233 simple_lock(&processor_list_lock
, LCK_GRP_NULL
);
234 if (processor_list
== NULL
) {
235 processor_list
= processor
;
237 processor_list_tail
->processor_list
= processor
;
239 processor_list_tail
= processor
;
241 processor_array
[cpu_id
] = processor
;
242 simple_unlock(&processor_list_lock
);
246 processor_set_primary(
247 processor_t processor
,
250 assert(processor
->processor_primary
== primary
|| processor
->processor_primary
== processor
);
251 /* Re-adjust primary point for this (possibly) secondary processor */
252 processor
->processor_primary
= primary
;
254 assert(primary
->processor_secondary
== NULL
|| primary
->processor_secondary
== processor
);
255 if (primary
!= processor
) {
256 /* Link primary to secondary, assumes a 2-way SMT model
257 * We'll need to move to a queue if any future architecture
258 * requires otherwise.
260 assert(processor
->processor_secondary
== NULL
);
261 primary
->processor_secondary
= processor
;
262 /* Mark both processors as SMT siblings */
263 primary
->is_SMT
= TRUE
;
264 processor
->is_SMT
= TRUE
;
266 processor_set_t pset
= processor
->processor_set
;
267 spl_t s
= splsched();
269 bit_clear(pset
->primary_map
, processor
->cpu_id
);
277 processor_t processor
)
279 return processor
->processor_set
;
283 processor_state_update_idle(processor_t processor
)
285 processor
->current_pri
= IDLEPRI
;
286 processor
->current_sfi_class
= SFI_CLASS_KERNEL
;
287 processor
->current_recommended_pset_type
= PSET_SMP
;
288 processor
->current_perfctl_class
= PERFCONTROL_CLASS_IDLE
;
289 processor
->current_urgency
= THREAD_URGENCY_NONE
;
290 processor
->current_is_NO_SMT
= false;
291 processor
->current_is_bound
= false;
295 processor_state_update_from_thread(processor_t processor
, thread_t thread
)
297 processor
->current_pri
= thread
->sched_pri
;
298 processor
->current_sfi_class
= thread
->sfi_class
;
299 processor
->current_recommended_pset_type
= recommended_pset_type(thread
);
300 processor
->current_perfctl_class
= thread_get_perfcontrol_class(thread
);
301 processor
->current_urgency
= thread_get_urgency(thread
, NULL
, NULL
);
302 #if DEBUG || DEVELOPMENT
303 processor
->current_is_NO_SMT
= (thread
->sched_flags
& TH_SFLAG_NO_SMT
) || (thread
->task
->t_flags
& TF_NO_SMT
);
305 processor
->current_is_NO_SMT
= (thread
->sched_flags
& TH_SFLAG_NO_SMT
);
307 processor
->current_is_bound
= thread
->bound_processor
!= PROCESSOR_NULL
;
311 processor_state_update_explicit(processor_t processor
, int pri
, sfi_class_id_t sfi_class
,
312 pset_cluster_type_t pset_type
, perfcontrol_class_t perfctl_class
, thread_urgency_t urgency
)
314 processor
->current_pri
= pri
;
315 processor
->current_sfi_class
= sfi_class
;
316 processor
->current_recommended_pset_type
= pset_type
;
317 processor
->current_perfctl_class
= perfctl_class
;
318 processor
->current_urgency
= urgency
;
331 /* some schedulers do not support multiple psets */
332 if (SCHED(multiple_psets_enabled
) == FALSE
) {
333 return processor_pset(master_processor
);
336 processor_set_t
*prev
, pset
= kalloc(sizeof(*pset
));
338 if (pset
!= PROCESSOR_SET_NULL
) {
339 pset_init(pset
, node
);
341 simple_lock(&pset_node_lock
, LCK_GRP_NULL
);
344 while (*prev
!= PROCESSOR_SET_NULL
) {
345 prev
= &(*prev
)->pset_list
;
350 simple_unlock(&pset_node_lock
);
357 * Find processor set in specified node with specified cluster_id.
358 * Returns default_pset if not found.
363 processor_set_t default_pset
)
365 simple_lock(&pset_node_lock
, LCK_GRP_NULL
);
366 pset_node_t node
= &pset_node0
;
367 processor_set_t pset
= NULL
;
371 while (pset
!= NULL
) {
372 if (pset
->pset_cluster_id
== cluster_id
) {
375 pset
= pset
->pset_list
;
377 } while ((node
= node
->node_list
) != NULL
);
378 simple_unlock(&pset_node_lock
);
386 * Initialize the given processor_set structure.
390 processor_set_t pset
,
393 if (pset
!= &pset0
) {
394 /* Scheduler state for pset0 initialized in sched_init() */
395 SCHED(pset_init
)(pset
);
396 SCHED(rt_init
)(pset
);
399 pset
->online_processor_count
= 0;
400 pset
->load_average
= 0;
401 pset
->cpu_set_low
= pset
->cpu_set_hi
= 0;
402 pset
->cpu_set_count
= 0;
403 pset
->last_chosen
= -1;
404 pset
->cpu_bitmask
= 0;
405 pset
->recommended_bitmask
= ~0ULL;
406 pset
->primary_map
= ~0ULL;
407 pset
->cpu_state_map
[PROCESSOR_OFF_LINE
] = ~0ULL;
408 for (uint i
= PROCESSOR_SHUTDOWN
; i
< PROCESSOR_STATE_LEN
; i
++) {
409 pset
->cpu_state_map
[i
] = 0;
411 pset
->pending_AST_URGENT_cpu_mask
= 0;
412 pset
->pending_AST_PREEMPT_cpu_mask
= 0;
413 #if defined(CONFIG_SCHED_DEFERRED_AST)
414 pset
->pending_deferred_AST_cpu_mask
= 0;
416 pset
->pending_spill_cpu_mask
= 0;
417 pset_lock_init(pset
);
418 pset
->pset_self
= IP_NULL
;
419 pset
->pset_name_self
= IP_NULL
;
420 pset
->pset_list
= PROCESSOR_SET_NULL
;
422 pset
->pset_cluster_type
= PSET_SMP
;
423 pset
->pset_cluster_id
= 0;
425 simple_lock(&pset_node_lock
, LCK_GRP_NULL
);
427 simple_unlock(&pset_node_lock
);
431 processor_info_count(
432 processor_flavor_t flavor
,
433 mach_msg_type_number_t
*count
)
436 case PROCESSOR_BASIC_INFO
:
437 *count
= PROCESSOR_BASIC_INFO_COUNT
;
440 case PROCESSOR_CPU_LOAD_INFO
:
441 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
445 return cpu_info_count(flavor
, count
);
454 processor_t processor
,
455 processor_flavor_t flavor
,
457 processor_info_t info
,
458 mach_msg_type_number_t
*count
)
461 kern_return_t result
;
463 if (processor
== PROCESSOR_NULL
) {
464 return KERN_INVALID_ARGUMENT
;
467 cpu_id
= processor
->cpu_id
;
470 case PROCESSOR_BASIC_INFO
:
472 processor_basic_info_t basic_info
;
474 if (*count
< PROCESSOR_BASIC_INFO_COUNT
) {
478 basic_info
= (processor_basic_info_t
) info
;
479 basic_info
->cpu_type
= slot_type(cpu_id
);
480 basic_info
->cpu_subtype
= slot_subtype(cpu_id
);
481 state
= processor
->state
;
482 if (state
== PROCESSOR_OFF_LINE
483 #if defined(__x86_64__)
484 || !processor
->is_recommended
487 basic_info
->running
= FALSE
;
489 basic_info
->running
= TRUE
;
491 basic_info
->slot_num
= cpu_id
;
492 if (processor
== master_processor
) {
493 basic_info
->is_master
= TRUE
;
495 basic_info
->is_master
= FALSE
;
498 *count
= PROCESSOR_BASIC_INFO_COUNT
;
504 case PROCESSOR_CPU_LOAD_INFO
:
506 processor_cpu_load_info_t cpu_load_info
;
508 uint64_t idle_time_snapshot1
, idle_time_snapshot2
;
509 uint64_t idle_time_tstamp1
, idle_time_tstamp2
;
512 * We capture the accumulated idle time twice over
513 * the course of this function, as well as the timestamps
514 * when each were last updated. Since these are
515 * all done using non-atomic racy mechanisms, the
516 * most we can infer is whether values are stable.
517 * timer_grab() is the only function that can be
518 * used reliably on another processor's per-processor
522 if (*count
< PROCESSOR_CPU_LOAD_INFO_COUNT
) {
526 cpu_load_info
= (processor_cpu_load_info_t
) info
;
527 if (precise_user_kernel_time
) {
528 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] =
529 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, user_state
)) / hz_tick_interval
);
530 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] =
531 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, system_state
)) / hz_tick_interval
);
533 uint64_t tval
= timer_grab(&PROCESSOR_DATA(processor
, user_state
)) +
534 timer_grab(&PROCESSOR_DATA(processor
, system_state
));
536 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = (uint32_t)(tval
/ hz_tick_interval
);
537 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
540 idle_state
= &PROCESSOR_DATA(processor
, idle_state
);
541 idle_time_snapshot1
= timer_grab(idle_state
);
542 idle_time_tstamp1
= idle_state
->tstamp
;
545 * Idle processors are not continually updating their
546 * per-processor idle timer, so it may be extremely
547 * out of date, resulting in an over-representation
548 * of non-idle time between two measurement
549 * intervals by e.g. top(1). If we are non-idle, or
550 * have evidence that the timer is being updated
551 * concurrently, we consider its value up-to-date.
553 if (PROCESSOR_DATA(processor
, current_state
) != idle_state
) {
554 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
555 (uint32_t)(idle_time_snapshot1
/ hz_tick_interval
);
556 } else if ((idle_time_snapshot1
!= (idle_time_snapshot2
= timer_grab(idle_state
))) ||
557 (idle_time_tstamp1
!= (idle_time_tstamp2
= idle_state
->tstamp
))) {
558 /* Idle timer is being updated concurrently, second stamp is good enough */
559 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
560 (uint32_t)(idle_time_snapshot2
/ hz_tick_interval
);
563 * Idle timer may be very stale. Fortunately we have established
564 * that idle_time_snapshot1 and idle_time_tstamp1 are unchanging
566 idle_time_snapshot1
+= mach_absolute_time() - idle_time_tstamp1
;
568 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
569 (uint32_t)(idle_time_snapshot1
/ hz_tick_interval
);
572 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
574 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
581 result
= cpu_info(flavor
, cpu_id
, info
, count
);
582 if (result
== KERN_SUCCESS
) {
592 processor_t processor
)
594 processor_set_t pset
;
596 kern_return_t result
;
599 if (processor
== PROCESSOR_NULL
|| processor
->processor_set
== PROCESSOR_SET_NULL
) {
600 return KERN_INVALID_ARGUMENT
;
603 if (processor
== master_processor
) {
606 prev
= thread_bind(processor
);
607 thread_block(THREAD_CONTINUE_NULL
);
609 result
= cpu_start(processor
->cpu_id
);
616 bool scheduler_disable
= false;
618 if ((processor
->processor_primary
!= processor
) && (sched_enable_smt
== 0)) {
619 if (cpu_can_exit(processor
->cpu_id
)) {
623 * This secondary SMT processor must start in order to service interrupts,
624 * so instead it will be disabled at the scheduler level.
626 scheduler_disable
= true;
630 pset
= processor
->processor_set
;
632 if (processor
->state
!= PROCESSOR_OFF_LINE
) {
639 pset_update_processor_state(pset
, processor
, PROCESSOR_START
);
644 * Create the idle processor thread.
646 if (processor
->idle_thread
== THREAD_NULL
) {
647 result
= idle_thread_create(processor
);
648 if (result
!= KERN_SUCCESS
) {
651 pset_update_processor_state(pset
, processor
, PROCESSOR_OFF_LINE
);
660 * If there is no active thread, the processor
661 * has never been started. Create a dedicated
664 if (processor
->active_thread
== THREAD_NULL
&&
665 processor
->next_thread
== THREAD_NULL
) {
666 result
= kernel_thread_create((thread_continue_t
)processor_start_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
667 if (result
!= KERN_SUCCESS
) {
670 pset_update_processor_state(pset
, processor
, PROCESSOR_OFF_LINE
);
679 thread
->bound_processor
= processor
;
680 processor
->next_thread
= thread
;
681 thread
->state
= TH_RUN
;
682 thread
->last_made_runnable_time
= mach_absolute_time();
683 thread_unlock(thread
);
686 thread_deallocate(thread
);
689 if (processor
->processor_self
== IP_NULL
) {
690 ipc_processor_init(processor
);
693 result
= cpu_start(processor
->cpu_id
);
694 if (result
!= KERN_SUCCESS
) {
697 pset_update_processor_state(pset
, processor
, PROCESSOR_OFF_LINE
);
703 if (scheduler_disable
) {
704 assert(processor
->processor_primary
!= processor
);
705 sched_processor_enable(processor
, FALSE
);
708 ipc_processor_enable(processor
);
716 processor_t processor
)
718 if (processor
== PROCESSOR_NULL
) {
719 return KERN_INVALID_ARGUMENT
;
722 return processor_shutdown(processor
);
727 processor_start_from_user(
728 processor_t processor
)
732 if (processor
== PROCESSOR_NULL
) {
733 return KERN_INVALID_ARGUMENT
;
736 if (!cpu_can_exit(processor
->cpu_id
)) {
737 ret
= sched_processor_enable(processor
, TRUE
);
739 ret
= processor_start(processor
);
746 processor_exit_from_user(
747 processor_t processor
)
751 if (processor
== PROCESSOR_NULL
) {
752 return KERN_INVALID_ARGUMENT
;
755 if (!cpu_can_exit(processor
->cpu_id
)) {
756 ret
= sched_processor_enable(processor
, FALSE
);
758 ret
= processor_shutdown(processor
);
765 enable_smt_processors(bool enable
)
767 if (machine_info
.logical_cpu_max
== machine_info
.physical_cpu_max
) {
768 /* Not an SMT system */
769 return KERN_INVALID_ARGUMENT
;
772 int ncpus
= machine_info
.logical_cpu_max
;
774 for (int i
= 1; i
< ncpus
; i
++) {
775 processor_t processor
= processor_array
[i
];
777 if (processor
->processor_primary
!= processor
) {
779 processor_start_from_user(processor
);
780 } else { /* Disable */
781 processor_exit_from_user(processor
);
787 host_basic_info_data_t hinfo
;
788 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
789 kern_return_t kret
= host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
790 if (kret
!= KERN_SUCCESS
) {
794 if (enable
&& (hinfo
.logical_cpu
!= hinfo
.logical_cpu_max
)) {
798 if (!enable
&& (hinfo
.logical_cpu
!= hinfo
.physical_cpu
)) {
807 processor_t processor
,
808 processor_info_t info
,
809 mach_msg_type_number_t count
)
811 if (processor
== PROCESSOR_NULL
) {
812 return KERN_INVALID_ARGUMENT
;
815 return cpu_control(processor
->cpu_id
, info
, count
);
819 processor_set_create(
820 __unused host_t host
,
821 __unused processor_set_t
*new_set
,
822 __unused processor_set_t
*new_name
)
828 processor_set_destroy(
829 __unused processor_set_t pset
)
835 processor_get_assignment(
836 processor_t processor
,
837 processor_set_t
*pset
)
841 if (processor
== PROCESSOR_NULL
) {
842 return KERN_INVALID_ARGUMENT
;
845 state
= processor
->state
;
846 if (state
== PROCESSOR_SHUTDOWN
|| state
== PROCESSOR_OFF_LINE
) {
857 processor_set_t pset
,
860 processor_set_info_t info
,
861 mach_msg_type_number_t
*count
)
863 if (pset
== PROCESSOR_SET_NULL
) {
864 return KERN_INVALID_ARGUMENT
;
867 if (flavor
== PROCESSOR_SET_BASIC_INFO
) {
868 processor_set_basic_info_t basic_info
;
870 if (*count
< PROCESSOR_SET_BASIC_INFO_COUNT
) {
874 basic_info
= (processor_set_basic_info_t
) info
;
875 #if defined(__x86_64__)
876 basic_info
->processor_count
= processor_avail_count_user
;
878 basic_info
->processor_count
= processor_avail_count
;
880 basic_info
->default_policy
= POLICY_TIMESHARE
;
882 *count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
885 } else if (flavor
== PROCESSOR_SET_TIMESHARE_DEFAULT
) {
886 policy_timeshare_base_t ts_base
;
888 if (*count
< POLICY_TIMESHARE_BASE_COUNT
) {
892 ts_base
= (policy_timeshare_base_t
) info
;
893 ts_base
->base_priority
= BASEPRI_DEFAULT
;
895 *count
= POLICY_TIMESHARE_BASE_COUNT
;
898 } else if (flavor
== PROCESSOR_SET_FIFO_DEFAULT
) {
899 policy_fifo_base_t fifo_base
;
901 if (*count
< POLICY_FIFO_BASE_COUNT
) {
905 fifo_base
= (policy_fifo_base_t
) info
;
906 fifo_base
->base_priority
= BASEPRI_DEFAULT
;
908 *count
= POLICY_FIFO_BASE_COUNT
;
911 } else if (flavor
== PROCESSOR_SET_RR_DEFAULT
) {
912 policy_rr_base_t rr_base
;
914 if (*count
< POLICY_RR_BASE_COUNT
) {
918 rr_base
= (policy_rr_base_t
) info
;
919 rr_base
->base_priority
= BASEPRI_DEFAULT
;
920 rr_base
->quantum
= 1;
922 *count
= POLICY_RR_BASE_COUNT
;
925 } else if (flavor
== PROCESSOR_SET_TIMESHARE_LIMITS
) {
926 policy_timeshare_limit_t ts_limit
;
928 if (*count
< POLICY_TIMESHARE_LIMIT_COUNT
) {
932 ts_limit
= (policy_timeshare_limit_t
) info
;
933 ts_limit
->max_priority
= MAXPRI_KERNEL
;
935 *count
= POLICY_TIMESHARE_LIMIT_COUNT
;
938 } else if (flavor
== PROCESSOR_SET_FIFO_LIMITS
) {
939 policy_fifo_limit_t fifo_limit
;
941 if (*count
< POLICY_FIFO_LIMIT_COUNT
) {
945 fifo_limit
= (policy_fifo_limit_t
) info
;
946 fifo_limit
->max_priority
= MAXPRI_KERNEL
;
948 *count
= POLICY_FIFO_LIMIT_COUNT
;
951 } else if (flavor
== PROCESSOR_SET_RR_LIMITS
) {
952 policy_rr_limit_t rr_limit
;
954 if (*count
< POLICY_RR_LIMIT_COUNT
) {
958 rr_limit
= (policy_rr_limit_t
) info
;
959 rr_limit
->max_priority
= MAXPRI_KERNEL
;
961 *count
= POLICY_RR_LIMIT_COUNT
;
964 } else if (flavor
== PROCESSOR_SET_ENABLED_POLICIES
) {
967 if (*count
< (sizeof(*enabled
) / sizeof(int))) {
971 enabled
= (int *) info
;
972 *enabled
= POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
;
974 *count
= sizeof(*enabled
) / sizeof(int);
981 return KERN_INVALID_ARGUMENT
;
985 * processor_set_statistics
987 * Returns scheduling statistics for a processor set.
990 processor_set_statistics(
991 processor_set_t pset
,
993 processor_set_info_t info
,
994 mach_msg_type_number_t
*count
)
996 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
) {
997 return KERN_INVALID_PROCESSOR_SET
;
1000 if (flavor
== PROCESSOR_SET_LOAD_INFO
) {
1001 processor_set_load_info_t load_info
;
1003 if (*count
< PROCESSOR_SET_LOAD_INFO_COUNT
) {
1004 return KERN_FAILURE
;
1007 load_info
= (processor_set_load_info_t
) info
;
1009 load_info
->mach_factor
= sched_mach_factor
;
1010 load_info
->load_average
= sched_load_average
;
1012 load_info
->task_count
= tasks_count
;
1013 load_info
->thread_count
= threads_count
;
1015 *count
= PROCESSOR_SET_LOAD_INFO_COUNT
;
1016 return KERN_SUCCESS
;
1019 return KERN_INVALID_ARGUMENT
;
1023 * processor_set_max_priority:
1025 * Specify max priority permitted on processor set. This affects
1026 * newly created and assigned threads. Optionally change existing
1030 processor_set_max_priority(
1031 __unused processor_set_t pset
,
1032 __unused
int max_priority
,
1033 __unused boolean_t change_threads
)
1035 return KERN_INVALID_ARGUMENT
;
1039 * processor_set_policy_enable:
1041 * Allow indicated policy on processor set.
1045 processor_set_policy_enable(
1046 __unused processor_set_t pset
,
1047 __unused
int policy
)
1049 return KERN_INVALID_ARGUMENT
;
1053 * processor_set_policy_disable:
1055 * Forbid indicated policy on processor set. Time sharing cannot
1059 processor_set_policy_disable(
1060 __unused processor_set_t pset
,
1061 __unused
int policy
,
1062 __unused boolean_t change_threads
)
1064 return KERN_INVALID_ARGUMENT
;
1068 * processor_set_things:
1070 * Common internals for processor_set_{threads,tasks}
1073 processor_set_things(
1074 processor_set_t pset
,
1076 mach_msg_type_number_t
*count
,
1084 unsigned int actual_tasks
;
1085 vm_size_t task_size
, task_size_needed
;
1087 thread_t
*thread_list
;
1088 unsigned int actual_threads
;
1089 vm_size_t thread_size
, thread_size_needed
;
1091 void *addr
, *newaddr
;
1092 vm_size_t size
, size_needed
;
1094 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
) {
1095 return KERN_INVALID_ARGUMENT
;
1099 task_size_needed
= 0;
1104 thread_size_needed
= 0;
1109 lck_mtx_lock(&tasks_threads_lock
);
1111 /* do we have the memory we need? */
1112 if (type
== PSET_THING_THREAD
) {
1113 thread_size_needed
= threads_count
* sizeof(void *);
1118 task_size_needed
= tasks_count
* sizeof(void *);
1120 if (task_size_needed
<= task_size
&&
1121 thread_size_needed
<= thread_size
) {
1125 /* unlock and allocate more memory */
1126 lck_mtx_unlock(&tasks_threads_lock
);
1128 /* grow task array */
1129 if (task_size_needed
> task_size
) {
1130 if (task_size
!= 0) {
1131 kfree(task_list
, task_size
);
1134 assert(task_size_needed
> 0);
1135 task_size
= task_size_needed
;
1137 task_list
= (task_t
*)kalloc(task_size
);
1138 if (task_list
== NULL
) {
1139 if (thread_size
!= 0) {
1140 kfree(thread_list
, thread_size
);
1142 return KERN_RESOURCE_SHORTAGE
;
1146 /* grow thread array */
1147 if (thread_size_needed
> thread_size
) {
1148 if (thread_size
!= 0) {
1149 kfree(thread_list
, thread_size
);
1152 assert(thread_size_needed
> 0);
1153 thread_size
= thread_size_needed
;
1155 thread_list
= (thread_t
*)kalloc(thread_size
);
1156 if (thread_list
== 0) {
1157 if (task_size
!= 0) {
1158 kfree(task_list
, task_size
);
1160 return KERN_RESOURCE_SHORTAGE
;
1165 /* OK, have memory and the list locked */
1167 /* If we need it, get the thread list */
1168 if (type
== PSET_THING_THREAD
) {
1169 for (thread
= (thread_t
)queue_first(&threads
);
1170 !queue_end(&threads
, (queue_entry_t
)thread
);
1171 thread
= (thread_t
)queue_next(&thread
->threads
)) {
1172 #if defined(SECURE_KERNEL)
1173 if (thread
->task
!= kernel_task
) {
1175 thread_reference_internal(thread
);
1176 thread_list
[actual_threads
++] = thread
;
1177 #if defined(SECURE_KERNEL)
1185 /* get a list of the tasks */
1186 for (task
= (task_t
)queue_first(&tasks
);
1187 !queue_end(&tasks
, (queue_entry_t
)task
);
1188 task
= (task_t
)queue_next(&task
->tasks
)) {
1189 #if defined(SECURE_KERNEL)
1190 if (task
!= kernel_task
) {
1192 task_reference_internal(task
);
1193 task_list
[actual_tasks
++] = task
;
1194 #if defined(SECURE_KERNEL)
1202 lck_mtx_unlock(&tasks_threads_lock
);
1205 unsigned int j
, used
;
1207 /* for each task, make sure we are allowed to examine it */
1208 for (i
= used
= 0; i
< actual_tasks
; i
++) {
1209 if (mac_task_check_expose_task(task_list
[i
])) {
1210 task_deallocate(task_list
[i
]);
1213 task_list
[used
++] = task_list
[i
];
1215 actual_tasks
= used
;
1216 task_size_needed
= actual_tasks
* sizeof(void *);
1218 if (type
== PSET_THING_THREAD
) {
1219 /* for each thread (if any), make sure it's task is in the allowed list */
1220 for (i
= used
= 0; i
< actual_threads
; i
++) {
1221 boolean_t found_task
= FALSE
;
1223 task
= thread_list
[i
]->task
;
1224 for (j
= 0; j
< actual_tasks
; j
++) {
1225 if (task_list
[j
] == task
) {
1231 thread_list
[used
++] = thread_list
[i
];
1233 thread_deallocate(thread_list
[i
]);
1236 actual_threads
= used
;
1237 thread_size_needed
= actual_threads
* sizeof(void *);
1239 /* done with the task list */
1240 for (i
= 0; i
< actual_tasks
; i
++) {
1241 task_deallocate(task_list
[i
]);
1243 kfree(task_list
, task_size
);
1250 if (type
== PSET_THING_THREAD
) {
1251 if (actual_threads
== 0) {
1252 /* no threads available to return */
1253 assert(task_size
== 0);
1254 if (thread_size
!= 0) {
1255 kfree(thread_list
, thread_size
);
1259 return KERN_SUCCESS
;
1261 size_needed
= actual_threads
* sizeof(void *);
1265 if (actual_tasks
== 0) {
1266 /* no tasks available to return */
1267 assert(thread_size
== 0);
1268 if (task_size
!= 0) {
1269 kfree(task_list
, task_size
);
1273 return KERN_SUCCESS
;
1275 size_needed
= actual_tasks
* sizeof(void *);
1280 /* if we allocated too much, must copy */
1281 if (size_needed
< size
) {
1282 newaddr
= kalloc(size_needed
);
1284 for (i
= 0; i
< actual_tasks
; i
++) {
1285 if (type
== PSET_THING_THREAD
) {
1286 thread_deallocate(thread_list
[i
]);
1288 task_deallocate(task_list
[i
]);
1294 return KERN_RESOURCE_SHORTAGE
;
1297 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
1304 *thing_list
= (void **)addr
;
1305 *count
= (unsigned int)size
/ sizeof(void *);
1307 return KERN_SUCCESS
;
1312 * processor_set_tasks:
1314 * List all tasks in the processor set.
1317 processor_set_tasks(
1318 processor_set_t pset
,
1319 task_array_t
*task_list
,
1320 mach_msg_type_number_t
*count
)
1323 mach_msg_type_number_t i
;
1325 ret
= processor_set_things(pset
, (void **)task_list
, count
, PSET_THING_TASK
);
1326 if (ret
!= KERN_SUCCESS
) {
1330 /* do the conversion that Mig should handle */
1331 for (i
= 0; i
< *count
; i
++) {
1332 (*task_list
)[i
] = (task_t
)convert_task_to_port((*task_list
)[i
]);
1334 return KERN_SUCCESS
;
1338 * processor_set_threads:
1340 * List all threads in the processor set.
1342 #if defined(SECURE_KERNEL)
1344 processor_set_threads(
1345 __unused processor_set_t pset
,
1346 __unused thread_array_t
*thread_list
,
1347 __unused mach_msg_type_number_t
*count
)
1349 return KERN_FAILURE
;
1351 #elif defined(CONFIG_EMBEDDED)
1353 processor_set_threads(
1354 __unused processor_set_t pset
,
1355 __unused thread_array_t
*thread_list
,
1356 __unused mach_msg_type_number_t
*count
)
1358 return KERN_NOT_SUPPORTED
;
1362 processor_set_threads(
1363 processor_set_t pset
,
1364 thread_array_t
*thread_list
,
1365 mach_msg_type_number_t
*count
)
1368 mach_msg_type_number_t i
;
1370 ret
= processor_set_things(pset
, (void **)thread_list
, count
, PSET_THING_THREAD
);
1371 if (ret
!= KERN_SUCCESS
) {
1375 /* do the conversion that Mig should handle */
1376 for (i
= 0; i
< *count
; i
++) {
1377 (*thread_list
)[i
] = (thread_t
)convert_thread_to_port((*thread_list
)[i
]);
1379 return KERN_SUCCESS
;
1384 * processor_set_policy_control
1386 * Controls the scheduling attributes governing the processor set.
1387 * Allows control of enabled policies, and per-policy base and limit
1391 processor_set_policy_control(
1392 __unused processor_set_t pset
,
1393 __unused
int flavor
,
1394 __unused processor_set_info_t policy_info
,
1395 __unused mach_msg_type_number_t count
,
1396 __unused boolean_t change
)
1398 return KERN_INVALID_ARGUMENT
;
1401 #undef pset_deallocate
1402 void pset_deallocate(processor_set_t pset
);
1405 __unused processor_set_t pset
)
1410 #undef pset_reference
1411 void pset_reference(processor_set_t pset
);
1414 __unused processor_set_t pset
)
1420 recommended_pset_type(thread_t thread
)