2 * Copyright (c) 2000-2019 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
->startup_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_offlined
= false;
211 processor
->processor_primary
= processor
; /* no SMT relationship known at this point */
212 processor
->processor_secondary
= NULL
;
213 processor
->is_SMT
= false;
214 processor
->is_recommended
= true;
215 processor
->processor_self
= IP_NULL
;
216 processor_data_init(processor
);
217 processor
->processor_list
= NULL
;
218 processor
->cpu_quiesce_state
= CPU_QUIESCE_COUNTER_NONE
;
219 processor
->cpu_quiesce_last_checkin
= 0;
220 processor
->must_idle
= false;
224 bit_set(pset
->cpu_bitmask
, cpu_id
);
225 bit_set(pset
->recommended_bitmask
, cpu_id
);
226 bit_set(pset
->primary_map
, cpu_id
);
227 bit_set(pset
->cpu_state_map
[PROCESSOR_OFF_LINE
], cpu_id
);
228 if (pset
->cpu_set_count
++ == 0) {
229 pset
->cpu_set_low
= pset
->cpu_set_hi
= cpu_id
;
231 pset
->cpu_set_low
= (cpu_id
< pset
->cpu_set_low
)? cpu_id
: pset
->cpu_set_low
;
232 pset
->cpu_set_hi
= (cpu_id
> pset
->cpu_set_hi
)? cpu_id
: pset
->cpu_set_hi
;
237 simple_lock(&processor_list_lock
, LCK_GRP_NULL
);
238 if (processor_list
== NULL
) {
239 processor_list
= processor
;
241 processor_list_tail
->processor_list
= processor
;
243 processor_list_tail
= processor
;
245 processor_array
[cpu_id
] = processor
;
246 simple_unlock(&processor_list_lock
);
250 processor_set_primary(
251 processor_t processor
,
254 assert(processor
->processor_primary
== primary
|| processor
->processor_primary
== processor
);
255 /* Re-adjust primary point for this (possibly) secondary processor */
256 processor
->processor_primary
= primary
;
258 assert(primary
->processor_secondary
== NULL
|| primary
->processor_secondary
== processor
);
259 if (primary
!= processor
) {
260 /* Link primary to secondary, assumes a 2-way SMT model
261 * We'll need to move to a queue if any future architecture
262 * requires otherwise.
264 assert(processor
->processor_secondary
== NULL
);
265 primary
->processor_secondary
= processor
;
266 /* Mark both processors as SMT siblings */
267 primary
->is_SMT
= TRUE
;
268 processor
->is_SMT
= TRUE
;
270 processor_set_t pset
= processor
->processor_set
;
271 spl_t s
= splsched();
273 bit_clear(pset
->primary_map
, processor
->cpu_id
);
281 processor_t processor
)
283 return processor
->processor_set
;
287 processor_state_update_idle(processor_t processor
)
289 processor
->current_pri
= IDLEPRI
;
290 processor
->current_sfi_class
= SFI_CLASS_KERNEL
;
291 processor
->current_recommended_pset_type
= PSET_SMP
;
292 processor
->current_perfctl_class
= PERFCONTROL_CLASS_IDLE
;
293 processor
->current_urgency
= THREAD_URGENCY_NONE
;
294 processor
->current_is_NO_SMT
= false;
295 processor
->current_is_bound
= false;
299 processor_state_update_from_thread(processor_t processor
, thread_t thread
)
301 processor
->current_pri
= thread
->sched_pri
;
302 processor
->current_sfi_class
= thread
->sfi_class
;
303 processor
->current_recommended_pset_type
= recommended_pset_type(thread
);
304 processor
->current_perfctl_class
= thread_get_perfcontrol_class(thread
);
305 processor
->current_urgency
= thread_get_urgency(thread
, NULL
, NULL
);
306 #if DEBUG || DEVELOPMENT
307 processor
->current_is_NO_SMT
= (thread
->sched_flags
& TH_SFLAG_NO_SMT
) || (thread
->task
->t_flags
& TF_NO_SMT
);
309 processor
->current_is_NO_SMT
= (thread
->sched_flags
& TH_SFLAG_NO_SMT
);
311 processor
->current_is_bound
= thread
->bound_processor
!= PROCESSOR_NULL
;
315 processor_state_update_explicit(processor_t processor
, int pri
, sfi_class_id_t sfi_class
,
316 pset_cluster_type_t pset_type
, perfcontrol_class_t perfctl_class
, thread_urgency_t urgency
)
318 processor
->current_pri
= pri
;
319 processor
->current_sfi_class
= sfi_class
;
320 processor
->current_recommended_pset_type
= pset_type
;
321 processor
->current_perfctl_class
= perfctl_class
;
322 processor
->current_urgency
= urgency
;
335 /* some schedulers do not support multiple psets */
336 if (SCHED(multiple_psets_enabled
) == FALSE
) {
337 return processor_pset(master_processor
);
340 processor_set_t
*prev
, pset
= kalloc(sizeof(*pset
));
342 if (pset
!= PROCESSOR_SET_NULL
) {
343 pset_init(pset
, node
);
345 simple_lock(&pset_node_lock
, LCK_GRP_NULL
);
348 while (*prev
!= PROCESSOR_SET_NULL
) {
349 prev
= &(*prev
)->pset_list
;
354 simple_unlock(&pset_node_lock
);
361 * Find processor set in specified node with specified cluster_id.
362 * Returns default_pset if not found.
367 processor_set_t default_pset
)
369 simple_lock(&pset_node_lock
, LCK_GRP_NULL
);
370 pset_node_t node
= &pset_node0
;
371 processor_set_t pset
= NULL
;
375 while (pset
!= NULL
) {
376 if (pset
->pset_cluster_id
== cluster_id
) {
379 pset
= pset
->pset_list
;
381 } while ((node
= node
->node_list
) != NULL
);
382 simple_unlock(&pset_node_lock
);
390 * Initialize the given processor_set structure.
394 processor_set_t pset
,
397 if (pset
!= &pset0
) {
398 /* Scheduler state for pset0 initialized in sched_init() */
399 SCHED(pset_init
)(pset
);
400 SCHED(rt_init
)(pset
);
403 pset
->online_processor_count
= 0;
404 pset
->load_average
= 0;
405 pset
->cpu_set_low
= pset
->cpu_set_hi
= 0;
406 pset
->cpu_set_count
= 0;
407 pset
->last_chosen
= -1;
408 pset
->cpu_bitmask
= 0;
409 pset
->recommended_bitmask
= 0;
410 pset
->primary_map
= 0;
411 for (uint i
= 0; i
< PROCESSOR_STATE_LEN
; i
++) {
412 pset
->cpu_state_map
[i
] = 0;
414 pset
->pending_AST_URGENT_cpu_mask
= 0;
415 pset
->pending_AST_PREEMPT_cpu_mask
= 0;
416 #if defined(CONFIG_SCHED_DEFERRED_AST)
417 pset
->pending_deferred_AST_cpu_mask
= 0;
419 pset
->pending_spill_cpu_mask
= 0;
420 pset_lock_init(pset
);
421 pset
->pset_self
= IP_NULL
;
422 pset
->pset_name_self
= IP_NULL
;
423 pset
->pset_list
= PROCESSOR_SET_NULL
;
425 pset
->pset_cluster_type
= PSET_SMP
;
426 pset
->pset_cluster_id
= 0;
428 simple_lock(&pset_node_lock
, LCK_GRP_NULL
);
430 simple_unlock(&pset_node_lock
);
434 processor_info_count(
435 processor_flavor_t flavor
,
436 mach_msg_type_number_t
*count
)
439 case PROCESSOR_BASIC_INFO
:
440 *count
= PROCESSOR_BASIC_INFO_COUNT
;
443 case PROCESSOR_CPU_LOAD_INFO
:
444 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
448 return cpu_info_count(flavor
, count
);
457 processor_t processor
,
458 processor_flavor_t flavor
,
460 processor_info_t info
,
461 mach_msg_type_number_t
*count
)
464 kern_return_t result
;
466 if (processor
== PROCESSOR_NULL
) {
467 return KERN_INVALID_ARGUMENT
;
470 cpu_id
= processor
->cpu_id
;
473 case PROCESSOR_BASIC_INFO
:
475 processor_basic_info_t basic_info
;
477 if (*count
< PROCESSOR_BASIC_INFO_COUNT
) {
481 basic_info
= (processor_basic_info_t
) info
;
482 basic_info
->cpu_type
= slot_type(cpu_id
);
483 basic_info
->cpu_subtype
= slot_subtype(cpu_id
);
484 state
= processor
->state
;
485 if (state
== PROCESSOR_OFF_LINE
486 #if defined(__x86_64__)
487 || !processor
->is_recommended
490 basic_info
->running
= FALSE
;
492 basic_info
->running
= TRUE
;
494 basic_info
->slot_num
= cpu_id
;
495 if (processor
== master_processor
) {
496 basic_info
->is_master
= TRUE
;
498 basic_info
->is_master
= FALSE
;
501 *count
= PROCESSOR_BASIC_INFO_COUNT
;
507 case PROCESSOR_CPU_LOAD_INFO
:
509 processor_cpu_load_info_t cpu_load_info
;
511 uint64_t idle_time_snapshot1
, idle_time_snapshot2
;
512 uint64_t idle_time_tstamp1
, idle_time_tstamp2
;
515 * We capture the accumulated idle time twice over
516 * the course of this function, as well as the timestamps
517 * when each were last updated. Since these are
518 * all done using non-atomic racy mechanisms, the
519 * most we can infer is whether values are stable.
520 * timer_grab() is the only function that can be
521 * used reliably on another processor's per-processor
525 if (*count
< PROCESSOR_CPU_LOAD_INFO_COUNT
) {
529 cpu_load_info
= (processor_cpu_load_info_t
) info
;
530 if (precise_user_kernel_time
) {
531 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] =
532 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, user_state
)) / hz_tick_interval
);
533 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] =
534 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, system_state
)) / hz_tick_interval
);
536 uint64_t tval
= timer_grab(&PROCESSOR_DATA(processor
, user_state
)) +
537 timer_grab(&PROCESSOR_DATA(processor
, system_state
));
539 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] = (uint32_t)(tval
/ hz_tick_interval
);
540 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] = 0;
543 idle_state
= &PROCESSOR_DATA(processor
, idle_state
);
544 idle_time_snapshot1
= timer_grab(idle_state
);
545 idle_time_tstamp1
= idle_state
->tstamp
;
548 * Idle processors are not continually updating their
549 * per-processor idle timer, so it may be extremely
550 * out of date, resulting in an over-representation
551 * of non-idle time between two measurement
552 * intervals by e.g. top(1). If we are non-idle, or
553 * have evidence that the timer is being updated
554 * concurrently, we consider its value up-to-date.
556 if (PROCESSOR_DATA(processor
, current_state
) != idle_state
) {
557 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
558 (uint32_t)(idle_time_snapshot1
/ hz_tick_interval
);
559 } else if ((idle_time_snapshot1
!= (idle_time_snapshot2
= timer_grab(idle_state
))) ||
560 (idle_time_tstamp1
!= (idle_time_tstamp2
= idle_state
->tstamp
))) {
561 /* Idle timer is being updated concurrently, second stamp is good enough */
562 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
563 (uint32_t)(idle_time_snapshot2
/ hz_tick_interval
);
566 * Idle timer may be very stale. Fortunately we have established
567 * that idle_time_snapshot1 and idle_time_tstamp1 are unchanging
569 idle_time_snapshot1
+= mach_absolute_time() - idle_time_tstamp1
;
571 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
572 (uint32_t)(idle_time_snapshot1
/ hz_tick_interval
);
575 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
577 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
584 result
= cpu_info(flavor
, cpu_id
, info
, count
);
585 if (result
== KERN_SUCCESS
) {
595 processor_t processor
)
597 processor_set_t pset
;
599 kern_return_t result
;
602 if (processor
== PROCESSOR_NULL
|| processor
->processor_set
== PROCESSOR_SET_NULL
) {
603 return KERN_INVALID_ARGUMENT
;
606 if (processor
== master_processor
) {
609 prev
= thread_bind(processor
);
610 thread_block(THREAD_CONTINUE_NULL
);
612 result
= cpu_start(processor
->cpu_id
);
619 bool scheduler_disable
= false;
621 if ((processor
->processor_primary
!= processor
) && (sched_enable_smt
== 0)) {
622 if (cpu_can_exit(processor
->cpu_id
)) {
626 * This secondary SMT processor must start in order to service interrupts,
627 * so instead it will be disabled at the scheduler level.
629 scheduler_disable
= true;
633 pset
= processor
->processor_set
;
635 if (processor
->state
!= PROCESSOR_OFF_LINE
) {
642 pset_update_processor_state(pset
, processor
, PROCESSOR_START
);
647 * Create the idle processor thread.
649 if (processor
->idle_thread
== THREAD_NULL
) {
650 result
= idle_thread_create(processor
);
651 if (result
!= KERN_SUCCESS
) {
654 pset_update_processor_state(pset
, processor
, PROCESSOR_OFF_LINE
);
663 * If there is no active thread, the processor
664 * has never been started. Create a dedicated
667 if (processor
->active_thread
== THREAD_NULL
&&
668 processor
->startup_thread
== THREAD_NULL
) {
669 result
= kernel_thread_create(processor_start_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
670 if (result
!= KERN_SUCCESS
) {
673 pset_update_processor_state(pset
, processor
, PROCESSOR_OFF_LINE
);
682 thread
->bound_processor
= processor
;
683 processor
->startup_thread
= thread
;
684 thread
->state
= TH_RUN
;
685 thread
->last_made_runnable_time
= mach_absolute_time();
686 thread_unlock(thread
);
689 thread_deallocate(thread
);
692 if (processor
->processor_self
== IP_NULL
) {
693 ipc_processor_init(processor
);
696 result
= cpu_start(processor
->cpu_id
);
697 if (result
!= KERN_SUCCESS
) {
700 pset_update_processor_state(pset
, processor
, PROCESSOR_OFF_LINE
);
706 if (scheduler_disable
) {
707 assert(processor
->processor_primary
!= processor
);
708 sched_processor_enable(processor
, FALSE
);
711 ipc_processor_enable(processor
);
719 processor_t processor
)
721 if (processor
== PROCESSOR_NULL
) {
722 return KERN_INVALID_ARGUMENT
;
725 return processor_shutdown(processor
);
730 processor_start_from_user(
731 processor_t processor
)
735 if (processor
== PROCESSOR_NULL
) {
736 return KERN_INVALID_ARGUMENT
;
739 if (!cpu_can_exit(processor
->cpu_id
)) {
740 ret
= sched_processor_enable(processor
, TRUE
);
742 ret
= processor_start(processor
);
749 processor_exit_from_user(
750 processor_t processor
)
754 if (processor
== PROCESSOR_NULL
) {
755 return KERN_INVALID_ARGUMENT
;
758 if (!cpu_can_exit(processor
->cpu_id
)) {
759 ret
= sched_processor_enable(processor
, FALSE
);
761 ret
= processor_shutdown(processor
);
768 enable_smt_processors(bool enable
)
770 if (machine_info
.logical_cpu_max
== machine_info
.physical_cpu_max
) {
771 /* Not an SMT system */
772 return KERN_INVALID_ARGUMENT
;
775 int ncpus
= machine_info
.logical_cpu_max
;
777 for (int i
= 1; i
< ncpus
; i
++) {
778 processor_t processor
= processor_array
[i
];
780 if (processor
->processor_primary
!= processor
) {
782 processor_start_from_user(processor
);
783 } else { /* Disable */
784 processor_exit_from_user(processor
);
790 host_basic_info_data_t hinfo
;
791 mach_msg_type_number_t count
= HOST_BASIC_INFO_COUNT
;
792 kern_return_t kret
= host_info((host_t
)BSD_HOST
, HOST_BASIC_INFO
, (host_info_t
)&hinfo
, &count
);
793 if (kret
!= KERN_SUCCESS
) {
797 if (enable
&& (hinfo
.logical_cpu
!= hinfo
.logical_cpu_max
)) {
801 if (!enable
&& (hinfo
.logical_cpu
!= hinfo
.physical_cpu
)) {
810 processor_t processor
,
811 processor_info_t info
,
812 mach_msg_type_number_t count
)
814 if (processor
== PROCESSOR_NULL
) {
815 return KERN_INVALID_ARGUMENT
;
818 return cpu_control(processor
->cpu_id
, info
, count
);
822 processor_set_create(
823 __unused host_t host
,
824 __unused processor_set_t
*new_set
,
825 __unused processor_set_t
*new_name
)
831 processor_set_destroy(
832 __unused processor_set_t pset
)
838 processor_get_assignment(
839 processor_t processor
,
840 processor_set_t
*pset
)
844 if (processor
== PROCESSOR_NULL
) {
845 return KERN_INVALID_ARGUMENT
;
848 state
= processor
->state
;
849 if (state
== PROCESSOR_SHUTDOWN
|| state
== PROCESSOR_OFF_LINE
) {
860 processor_set_t pset
,
863 processor_set_info_t info
,
864 mach_msg_type_number_t
*count
)
866 if (pset
== PROCESSOR_SET_NULL
) {
867 return KERN_INVALID_ARGUMENT
;
870 if (flavor
== PROCESSOR_SET_BASIC_INFO
) {
871 processor_set_basic_info_t basic_info
;
873 if (*count
< PROCESSOR_SET_BASIC_INFO_COUNT
) {
877 basic_info
= (processor_set_basic_info_t
) info
;
878 #if defined(__x86_64__)
879 basic_info
->processor_count
= processor_avail_count_user
;
881 basic_info
->processor_count
= processor_avail_count
;
883 basic_info
->default_policy
= POLICY_TIMESHARE
;
885 *count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
888 } else if (flavor
== PROCESSOR_SET_TIMESHARE_DEFAULT
) {
889 policy_timeshare_base_t ts_base
;
891 if (*count
< POLICY_TIMESHARE_BASE_COUNT
) {
895 ts_base
= (policy_timeshare_base_t
) info
;
896 ts_base
->base_priority
= BASEPRI_DEFAULT
;
898 *count
= POLICY_TIMESHARE_BASE_COUNT
;
901 } else if (flavor
== PROCESSOR_SET_FIFO_DEFAULT
) {
902 policy_fifo_base_t fifo_base
;
904 if (*count
< POLICY_FIFO_BASE_COUNT
) {
908 fifo_base
= (policy_fifo_base_t
) info
;
909 fifo_base
->base_priority
= BASEPRI_DEFAULT
;
911 *count
= POLICY_FIFO_BASE_COUNT
;
914 } else if (flavor
== PROCESSOR_SET_RR_DEFAULT
) {
915 policy_rr_base_t rr_base
;
917 if (*count
< POLICY_RR_BASE_COUNT
) {
921 rr_base
= (policy_rr_base_t
) info
;
922 rr_base
->base_priority
= BASEPRI_DEFAULT
;
923 rr_base
->quantum
= 1;
925 *count
= POLICY_RR_BASE_COUNT
;
928 } else if (flavor
== PROCESSOR_SET_TIMESHARE_LIMITS
) {
929 policy_timeshare_limit_t ts_limit
;
931 if (*count
< POLICY_TIMESHARE_LIMIT_COUNT
) {
935 ts_limit
= (policy_timeshare_limit_t
) info
;
936 ts_limit
->max_priority
= MAXPRI_KERNEL
;
938 *count
= POLICY_TIMESHARE_LIMIT_COUNT
;
941 } else if (flavor
== PROCESSOR_SET_FIFO_LIMITS
) {
942 policy_fifo_limit_t fifo_limit
;
944 if (*count
< POLICY_FIFO_LIMIT_COUNT
) {
948 fifo_limit
= (policy_fifo_limit_t
) info
;
949 fifo_limit
->max_priority
= MAXPRI_KERNEL
;
951 *count
= POLICY_FIFO_LIMIT_COUNT
;
954 } else if (flavor
== PROCESSOR_SET_RR_LIMITS
) {
955 policy_rr_limit_t rr_limit
;
957 if (*count
< POLICY_RR_LIMIT_COUNT
) {
961 rr_limit
= (policy_rr_limit_t
) info
;
962 rr_limit
->max_priority
= MAXPRI_KERNEL
;
964 *count
= POLICY_RR_LIMIT_COUNT
;
967 } else if (flavor
== PROCESSOR_SET_ENABLED_POLICIES
) {
970 if (*count
< (sizeof(*enabled
) / sizeof(int))) {
974 enabled
= (int *) info
;
975 *enabled
= POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
;
977 *count
= sizeof(*enabled
) / sizeof(int);
984 return KERN_INVALID_ARGUMENT
;
988 * processor_set_statistics
990 * Returns scheduling statistics for a processor set.
993 processor_set_statistics(
994 processor_set_t pset
,
996 processor_set_info_t info
,
997 mach_msg_type_number_t
*count
)
999 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
) {
1000 return KERN_INVALID_PROCESSOR_SET
;
1003 if (flavor
== PROCESSOR_SET_LOAD_INFO
) {
1004 processor_set_load_info_t load_info
;
1006 if (*count
< PROCESSOR_SET_LOAD_INFO_COUNT
) {
1007 return KERN_FAILURE
;
1010 load_info
= (processor_set_load_info_t
) info
;
1012 load_info
->mach_factor
= sched_mach_factor
;
1013 load_info
->load_average
= sched_load_average
;
1015 load_info
->task_count
= tasks_count
;
1016 load_info
->thread_count
= threads_count
;
1018 *count
= PROCESSOR_SET_LOAD_INFO_COUNT
;
1019 return KERN_SUCCESS
;
1022 return KERN_INVALID_ARGUMENT
;
1026 * processor_set_max_priority:
1028 * Specify max priority permitted on processor set. This affects
1029 * newly created and assigned threads. Optionally change existing
1033 processor_set_max_priority(
1034 __unused processor_set_t pset
,
1035 __unused
int max_priority
,
1036 __unused boolean_t change_threads
)
1038 return KERN_INVALID_ARGUMENT
;
1042 * processor_set_policy_enable:
1044 * Allow indicated policy on processor set.
1048 processor_set_policy_enable(
1049 __unused processor_set_t pset
,
1050 __unused
int policy
)
1052 return KERN_INVALID_ARGUMENT
;
1056 * processor_set_policy_disable:
1058 * Forbid indicated policy on processor set. Time sharing cannot
1062 processor_set_policy_disable(
1063 __unused processor_set_t pset
,
1064 __unused
int policy
,
1065 __unused boolean_t change_threads
)
1067 return KERN_INVALID_ARGUMENT
;
1071 * processor_set_things:
1073 * Common internals for processor_set_{threads,tasks}
1076 processor_set_things(
1077 processor_set_t pset
,
1079 mach_msg_type_number_t
*count
,
1087 unsigned int actual_tasks
;
1088 vm_size_t task_size
, task_size_needed
;
1090 thread_t
*thread_list
;
1091 unsigned int actual_threads
;
1092 vm_size_t thread_size
, thread_size_needed
;
1094 void *addr
, *newaddr
;
1095 vm_size_t size
, size_needed
;
1097 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
) {
1098 return KERN_INVALID_ARGUMENT
;
1102 task_size_needed
= 0;
1107 thread_size_needed
= 0;
1112 lck_mtx_lock(&tasks_threads_lock
);
1114 /* do we have the memory we need? */
1115 if (type
== PSET_THING_THREAD
) {
1116 thread_size_needed
= threads_count
* sizeof(void *);
1121 task_size_needed
= tasks_count
* sizeof(void *);
1123 if (task_size_needed
<= task_size
&&
1124 thread_size_needed
<= thread_size
) {
1128 /* unlock and allocate more memory */
1129 lck_mtx_unlock(&tasks_threads_lock
);
1131 /* grow task array */
1132 if (task_size_needed
> task_size
) {
1133 if (task_size
!= 0) {
1134 kfree(task_list
, task_size
);
1137 assert(task_size_needed
> 0);
1138 task_size
= task_size_needed
;
1140 task_list
= (task_t
*)kalloc(task_size
);
1141 if (task_list
== NULL
) {
1142 if (thread_size
!= 0) {
1143 kfree(thread_list
, thread_size
);
1145 return KERN_RESOURCE_SHORTAGE
;
1149 /* grow thread array */
1150 if (thread_size_needed
> thread_size
) {
1151 if (thread_size
!= 0) {
1152 kfree(thread_list
, thread_size
);
1155 assert(thread_size_needed
> 0);
1156 thread_size
= thread_size_needed
;
1158 thread_list
= (thread_t
*)kalloc(thread_size
);
1159 if (thread_list
== 0) {
1160 if (task_size
!= 0) {
1161 kfree(task_list
, task_size
);
1163 return KERN_RESOURCE_SHORTAGE
;
1168 /* OK, have memory and the list locked */
1170 /* If we need it, get the thread list */
1171 if (type
== PSET_THING_THREAD
) {
1172 for (thread
= (thread_t
)queue_first(&threads
);
1173 !queue_end(&threads
, (queue_entry_t
)thread
);
1174 thread
= (thread_t
)queue_next(&thread
->threads
)) {
1175 #if defined(SECURE_KERNEL)
1176 if (thread
->task
!= kernel_task
) {
1178 thread_reference_internal(thread
);
1179 thread_list
[actual_threads
++] = thread
;
1180 #if defined(SECURE_KERNEL)
1188 /* get a list of the tasks */
1189 for (task
= (task_t
)queue_first(&tasks
);
1190 !queue_end(&tasks
, (queue_entry_t
)task
);
1191 task
= (task_t
)queue_next(&task
->tasks
)) {
1192 #if defined(SECURE_KERNEL)
1193 if (task
!= kernel_task
) {
1195 task_reference_internal(task
);
1196 task_list
[actual_tasks
++] = task
;
1197 #if defined(SECURE_KERNEL)
1205 lck_mtx_unlock(&tasks_threads_lock
);
1208 unsigned int j
, used
;
1210 /* for each task, make sure we are allowed to examine it */
1211 for (i
= used
= 0; i
< actual_tasks
; i
++) {
1212 if (mac_task_check_expose_task(task_list
[i
])) {
1213 task_deallocate(task_list
[i
]);
1216 task_list
[used
++] = task_list
[i
];
1218 actual_tasks
= used
;
1219 task_size_needed
= actual_tasks
* sizeof(void *);
1221 if (type
== PSET_THING_THREAD
) {
1222 /* for each thread (if any), make sure it's task is in the allowed list */
1223 for (i
= used
= 0; i
< actual_threads
; i
++) {
1224 boolean_t found_task
= FALSE
;
1226 task
= thread_list
[i
]->task
;
1227 for (j
= 0; j
< actual_tasks
; j
++) {
1228 if (task_list
[j
] == task
) {
1234 thread_list
[used
++] = thread_list
[i
];
1236 thread_deallocate(thread_list
[i
]);
1239 actual_threads
= used
;
1240 thread_size_needed
= actual_threads
* sizeof(void *);
1242 /* done with the task list */
1243 for (i
= 0; i
< actual_tasks
; i
++) {
1244 task_deallocate(task_list
[i
]);
1246 kfree(task_list
, task_size
);
1253 if (type
== PSET_THING_THREAD
) {
1254 if (actual_threads
== 0) {
1255 /* no threads available to return */
1256 assert(task_size
== 0);
1257 if (thread_size
!= 0) {
1258 kfree(thread_list
, thread_size
);
1262 return KERN_SUCCESS
;
1264 size_needed
= actual_threads
* sizeof(void *);
1268 if (actual_tasks
== 0) {
1269 /* no tasks available to return */
1270 assert(thread_size
== 0);
1271 if (task_size
!= 0) {
1272 kfree(task_list
, task_size
);
1276 return KERN_SUCCESS
;
1278 size_needed
= actual_tasks
* sizeof(void *);
1283 /* if we allocated too much, must copy */
1284 if (size_needed
< size
) {
1285 newaddr
= kalloc(size_needed
);
1287 for (i
= 0; i
< actual_tasks
; i
++) {
1288 if (type
== PSET_THING_THREAD
) {
1289 thread_deallocate(thread_list
[i
]);
1291 task_deallocate(task_list
[i
]);
1297 return KERN_RESOURCE_SHORTAGE
;
1300 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
1307 *thing_list
= (void **)addr
;
1308 *count
= (unsigned int)size
/ sizeof(void *);
1310 return KERN_SUCCESS
;
1315 * processor_set_tasks:
1317 * List all tasks in the processor set.
1320 processor_set_tasks(
1321 processor_set_t pset
,
1322 task_array_t
*task_list
,
1323 mach_msg_type_number_t
*count
)
1326 mach_msg_type_number_t i
;
1328 ret
= processor_set_things(pset
, (void **)task_list
, count
, PSET_THING_TASK
);
1329 if (ret
!= KERN_SUCCESS
) {
1333 /* do the conversion that Mig should handle */
1334 for (i
= 0; i
< *count
; i
++) {
1335 (*task_list
)[i
] = (task_t
)convert_task_to_port((*task_list
)[i
]);
1337 return KERN_SUCCESS
;
1341 * processor_set_threads:
1343 * List all threads in the processor set.
1345 #if defined(SECURE_KERNEL)
1347 processor_set_threads(
1348 __unused processor_set_t pset
,
1349 __unused thread_array_t
*thread_list
,
1350 __unused mach_msg_type_number_t
*count
)
1352 return KERN_FAILURE
;
1354 #elif defined(CONFIG_EMBEDDED)
1356 processor_set_threads(
1357 __unused processor_set_t pset
,
1358 __unused thread_array_t
*thread_list
,
1359 __unused mach_msg_type_number_t
*count
)
1361 return KERN_NOT_SUPPORTED
;
1365 processor_set_threads(
1366 processor_set_t pset
,
1367 thread_array_t
*thread_list
,
1368 mach_msg_type_number_t
*count
)
1371 mach_msg_type_number_t i
;
1373 ret
= processor_set_things(pset
, (void **)thread_list
, count
, PSET_THING_THREAD
);
1374 if (ret
!= KERN_SUCCESS
) {
1378 /* do the conversion that Mig should handle */
1379 for (i
= 0; i
< *count
; i
++) {
1380 (*thread_list
)[i
] = (thread_t
)convert_thread_to_port((*thread_list
)[i
]);
1382 return KERN_SUCCESS
;
1387 * processor_set_policy_control
1389 * Controls the scheduling attributes governing the processor set.
1390 * Allows control of enabled policies, and per-policy base and limit
1394 processor_set_policy_control(
1395 __unused processor_set_t pset
,
1396 __unused
int flavor
,
1397 __unused processor_set_info_t policy_info
,
1398 __unused mach_msg_type_number_t count
,
1399 __unused boolean_t change
)
1401 return KERN_INVALID_ARGUMENT
;
1404 #undef pset_deallocate
1405 void pset_deallocate(processor_set_t pset
);
1408 __unused processor_set_t pset
)
1413 #undef pset_reference
1414 void pset_reference(processor_set_t pset
);
1417 __unused processor_set_t pset
)
1423 #if CONFIG_SCHED_CLUTCH
1426 * The clutch scheduler decides the recommendation of a thread based
1427 * on its thread group's properties and recommendations. The only thread
1428 * level property it looks at is the bucket for the thread to implement
1429 * the policy of not running Utility & BG buckets on the P-cores. Any
1430 * other policy being added to this routine might need to be reflected
1431 * in places such as sched_clutch_hierarchy_thread_pset() &
1432 * sched_clutch_migrate_thread_group() which rely on getting the recommendations
1435 * Note: The current implementation does not support TH_SFLAG_ECORE_ONLY &
1436 * TH_SFLAG_PCORE_ONLY flags which are used for debugging utilities. A similar
1437 * version of that functionality can be implemented by putting these flags
1438 * on a thread group instead of individual thread basis.
1442 recommended_pset_type(thread_t thread
)
1448 #else /* CONFIG_SCHED_CLUTCH */
1451 recommended_pset_type(thread_t thread
)
1457 #endif /* CONFIG_SCHED_CLUTCH */