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>
84 #include <mach/mach_host_server.h>
85 #include <mach/processor_set_server.h>
87 struct processor_set pset0
;
88 struct pset_node pset_node0
;
89 decl_simple_lock_data(static,pset_node_lock
)
95 decl_lck_mtx_data(,tasks_threads_lock
)
97 processor_t processor_list
;
98 unsigned int processor_count
;
99 static processor_t processor_list_tail
;
100 decl_simple_lock_data(,processor_list_lock
)
102 uint32_t processor_avail_count
;
104 processor_t master_processor
;
108 kern_return_t
processor_set_things(
109 processor_set_t pset
,
110 mach_port_t
**thing_list
,
111 mach_msg_type_number_t
*count
,
115 processor_bootstrap(void)
117 pset_init(&pset0
, &pset_node0
);
118 pset_node0
.psets
= &pset0
;
120 simple_lock_init(&pset_node_lock
, 0);
123 queue_init(&threads
);
125 simple_lock_init(&processor_list_lock
, 0);
127 master_processor
= cpu_to_processor(master_cpu
);
129 processor_init(master_processor
, master_cpu
, &pset0
);
133 * Initialize the given processor for the cpu
134 * indicated by cpu_id, and assign to the
135 * specified processor set.
139 processor_t processor
,
141 processor_set_t pset
)
143 run_queue_init(&processor
->runq
);
145 processor
->state
= PROCESSOR_OFF_LINE
;
146 processor
->active_thread
= processor
->next_thread
= processor
->idle_thread
= THREAD_NULL
;
147 processor
->processor_set
= pset
;
148 processor
->current_pri
= MINPRI
;
149 processor
->cpu_id
= cpu_id
;
150 timer_call_setup(&processor
->quantum_timer
, thread_quantum_expire
, processor
);
151 processor
->deadline
= UINT64_MAX
;
152 processor
->timeslice
= 0;
153 processor
->processor_meta
= PROCESSOR_META_NULL
;
154 processor
->processor_self
= IP_NULL
;
155 processor_data_init(processor
);
156 processor
->processor_list
= NULL
;
159 if (pset
->cpu_set_count
++ == 0)
160 pset
->cpu_set_low
= pset
->cpu_set_hi
= cpu_id
;
162 pset
->cpu_set_low
= (cpu_id
< pset
->cpu_set_low
)? cpu_id
: pset
->cpu_set_low
;
163 pset
->cpu_set_hi
= (cpu_id
> pset
->cpu_set_hi
)? cpu_id
: pset
->cpu_set_hi
;
167 simple_lock(&processor_list_lock
);
168 if (processor_list
== NULL
)
169 processor_list
= processor
;
171 processor_list_tail
->processor_list
= processor
;
172 processor_list_tail
= processor
;
174 simple_unlock(&processor_list_lock
);
179 processor_t processor
,
182 processor_meta_t pmeta
= primary
->processor_meta
;
184 if (pmeta
== PROCESSOR_META_NULL
) {
185 pmeta
= kalloc(sizeof (*pmeta
));
187 queue_init(&pmeta
->idle_queue
);
189 pmeta
->primary
= primary
;
192 processor
->processor_meta
= pmeta
;
197 processor_t processor
)
199 return (processor
->processor_set
);
212 processor_set_t
*prev
, pset
= kalloc(sizeof (*pset
));
214 if (pset
!= PROCESSOR_SET_NULL
) {
215 pset_init(pset
, node
);
217 simple_lock(&pset_node_lock
);
220 while (*prev
!= PROCESSOR_SET_NULL
)
221 prev
= &(*prev
)->pset_list
;
225 simple_unlock(&pset_node_lock
);
232 * Initialize the given processor_set structure.
236 processor_set_t pset
,
239 queue_init(&pset
->active_queue
);
240 queue_init(&pset
->idle_queue
);
241 pset
->processor_count
= 0;
242 pset
->low_pri
= pset
->low_count
= PROCESSOR_NULL
;
243 pset
->cpu_set_low
= pset
->cpu_set_hi
= 0;
244 pset
->cpu_set_count
= 0;
245 pset_lock_init(pset
);
246 pset
->pset_self
= IP_NULL
;
247 pset
->pset_name_self
= IP_NULL
;
248 pset
->pset_list
= PROCESSOR_SET_NULL
;
253 processor_info_count(
254 processor_flavor_t flavor
,
255 mach_msg_type_number_t
*count
)
259 case PROCESSOR_BASIC_INFO
:
260 *count
= PROCESSOR_BASIC_INFO_COUNT
;
263 case PROCESSOR_CPU_LOAD_INFO
:
264 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
268 return (cpu_info_count(flavor
, count
));
271 return (KERN_SUCCESS
);
277 register processor_t processor
,
278 processor_flavor_t flavor
,
280 processor_info_t info
,
281 mach_msg_type_number_t
*count
)
283 register int cpu_id
, state
;
284 kern_return_t result
;
286 if (processor
== PROCESSOR_NULL
)
287 return (KERN_INVALID_ARGUMENT
);
289 cpu_id
= processor
->cpu_id
;
293 case PROCESSOR_BASIC_INFO
:
295 register processor_basic_info_t basic_info
;
297 if (*count
< PROCESSOR_BASIC_INFO_COUNT
)
298 return (KERN_FAILURE
);
300 basic_info
= (processor_basic_info_t
) info
;
301 basic_info
->cpu_type
= slot_type(cpu_id
);
302 basic_info
->cpu_subtype
= slot_subtype(cpu_id
);
303 state
= processor
->state
;
304 if (state
== PROCESSOR_OFF_LINE
)
305 basic_info
->running
= FALSE
;
307 basic_info
->running
= TRUE
;
308 basic_info
->slot_num
= cpu_id
;
309 if (processor
== master_processor
)
310 basic_info
->is_master
= TRUE
;
312 basic_info
->is_master
= FALSE
;
314 *count
= PROCESSOR_BASIC_INFO_COUNT
;
317 return (KERN_SUCCESS
);
320 case PROCESSOR_CPU_LOAD_INFO
:
322 register processor_cpu_load_info_t cpu_load_info
;
324 if (*count
< PROCESSOR_CPU_LOAD_INFO_COUNT
)
325 return (KERN_FAILURE
);
327 cpu_load_info
= (processor_cpu_load_info_t
) info
;
328 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] =
329 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, user_state
)) / hz_tick_interval
);
330 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] =
331 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, system_state
)) / hz_tick_interval
);
332 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
333 (uint32_t)(timer_grab(&PROCESSOR_DATA(processor
, idle_state
)) / hz_tick_interval
);
334 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
336 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
339 return (KERN_SUCCESS
);
343 result
= cpu_info(flavor
, cpu_id
, info
, count
);
344 if (result
== KERN_SUCCESS
)
353 processor_t processor
)
355 processor_set_t pset
;
357 kern_return_t result
;
360 if (processor
== PROCESSOR_NULL
|| processor
->processor_set
== PROCESSOR_SET_NULL
)
361 return (KERN_INVALID_ARGUMENT
);
363 if (processor
== master_processor
) {
366 prev
= thread_bind(processor
);
367 thread_block(THREAD_CONTINUE_NULL
);
369 result
= cpu_start(processor
->cpu_id
);
377 pset
= processor
->processor_set
;
379 if (processor
->state
!= PROCESSOR_OFF_LINE
) {
383 return (KERN_FAILURE
);
386 processor
->state
= PROCESSOR_START
;
391 * Create the idle processor thread.
393 if (processor
->idle_thread
== THREAD_NULL
) {
394 result
= idle_thread_create(processor
);
395 if (result
!= KERN_SUCCESS
) {
398 processor
->state
= PROCESSOR_OFF_LINE
;
407 * If there is no active thread, the processor
408 * has never been started. Create a dedicated
411 if ( processor
->active_thread
== THREAD_NULL
&&
412 processor
->next_thread
== THREAD_NULL
) {
413 result
= kernel_thread_create((thread_continue_t
)processor_start_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
414 if (result
!= KERN_SUCCESS
) {
417 processor
->state
= PROCESSOR_OFF_LINE
;
426 thread
->bound_processor
= processor
;
427 processor
->next_thread
= thread
;
428 thread
->state
= TH_RUN
;
429 thread_unlock(thread
);
432 thread_deallocate(thread
);
435 if (processor
->processor_self
== IP_NULL
)
436 ipc_processor_init(processor
);
438 result
= cpu_start(processor
->cpu_id
);
439 if (result
!= KERN_SUCCESS
) {
442 processor
->state
= PROCESSOR_OFF_LINE
;
449 ipc_processor_enable(processor
);
451 return (KERN_SUCCESS
);
456 processor_t processor
)
458 if (processor
== PROCESSOR_NULL
)
459 return(KERN_INVALID_ARGUMENT
);
461 return(processor_shutdown(processor
));
466 processor_t processor
,
467 processor_info_t info
,
468 mach_msg_type_number_t count
)
470 if (processor
== PROCESSOR_NULL
)
471 return(KERN_INVALID_ARGUMENT
);
473 return(cpu_control(processor
->cpu_id
, info
, count
));
477 processor_set_create(
478 __unused host_t host
,
479 __unused processor_set_t
*new_set
,
480 __unused processor_set_t
*new_name
)
482 return(KERN_FAILURE
);
486 processor_set_destroy(
487 __unused processor_set_t pset
)
489 return(KERN_FAILURE
);
493 processor_get_assignment(
494 processor_t processor
,
495 processor_set_t
*pset
)
499 state
= processor
->state
;
500 if (state
== PROCESSOR_SHUTDOWN
|| state
== PROCESSOR_OFF_LINE
)
501 return(KERN_FAILURE
);
505 return(KERN_SUCCESS
);
510 processor_set_t pset
,
513 processor_set_info_t info
,
514 mach_msg_type_number_t
*count
)
516 if (pset
== PROCESSOR_SET_NULL
)
517 return(KERN_INVALID_ARGUMENT
);
519 if (flavor
== PROCESSOR_SET_BASIC_INFO
) {
520 register processor_set_basic_info_t basic_info
;
522 if (*count
< PROCESSOR_SET_BASIC_INFO_COUNT
)
523 return(KERN_FAILURE
);
525 basic_info
= (processor_set_basic_info_t
) info
;
526 basic_info
->processor_count
= processor_avail_count
;
527 basic_info
->default_policy
= POLICY_TIMESHARE
;
529 *count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
531 return(KERN_SUCCESS
);
533 else if (flavor
== PROCESSOR_SET_TIMESHARE_DEFAULT
) {
534 register policy_timeshare_base_t ts_base
;
536 if (*count
< POLICY_TIMESHARE_BASE_COUNT
)
537 return(KERN_FAILURE
);
539 ts_base
= (policy_timeshare_base_t
) info
;
540 ts_base
->base_priority
= BASEPRI_DEFAULT
;
542 *count
= POLICY_TIMESHARE_BASE_COUNT
;
544 return(KERN_SUCCESS
);
546 else if (flavor
== PROCESSOR_SET_FIFO_DEFAULT
) {
547 register policy_fifo_base_t fifo_base
;
549 if (*count
< POLICY_FIFO_BASE_COUNT
)
550 return(KERN_FAILURE
);
552 fifo_base
= (policy_fifo_base_t
) info
;
553 fifo_base
->base_priority
= BASEPRI_DEFAULT
;
555 *count
= POLICY_FIFO_BASE_COUNT
;
557 return(KERN_SUCCESS
);
559 else if (flavor
== PROCESSOR_SET_RR_DEFAULT
) {
560 register policy_rr_base_t rr_base
;
562 if (*count
< POLICY_RR_BASE_COUNT
)
563 return(KERN_FAILURE
);
565 rr_base
= (policy_rr_base_t
) info
;
566 rr_base
->base_priority
= BASEPRI_DEFAULT
;
567 rr_base
->quantum
= 1;
569 *count
= POLICY_RR_BASE_COUNT
;
571 return(KERN_SUCCESS
);
573 else if (flavor
== PROCESSOR_SET_TIMESHARE_LIMITS
) {
574 register policy_timeshare_limit_t ts_limit
;
576 if (*count
< POLICY_TIMESHARE_LIMIT_COUNT
)
577 return(KERN_FAILURE
);
579 ts_limit
= (policy_timeshare_limit_t
) info
;
580 ts_limit
->max_priority
= MAXPRI_KERNEL
;
582 *count
= POLICY_TIMESHARE_LIMIT_COUNT
;
584 return(KERN_SUCCESS
);
586 else if (flavor
== PROCESSOR_SET_FIFO_LIMITS
) {
587 register policy_fifo_limit_t fifo_limit
;
589 if (*count
< POLICY_FIFO_LIMIT_COUNT
)
590 return(KERN_FAILURE
);
592 fifo_limit
= (policy_fifo_limit_t
) info
;
593 fifo_limit
->max_priority
= MAXPRI_KERNEL
;
595 *count
= POLICY_FIFO_LIMIT_COUNT
;
597 return(KERN_SUCCESS
);
599 else if (flavor
== PROCESSOR_SET_RR_LIMITS
) {
600 register policy_rr_limit_t rr_limit
;
602 if (*count
< POLICY_RR_LIMIT_COUNT
)
603 return(KERN_FAILURE
);
605 rr_limit
= (policy_rr_limit_t
) info
;
606 rr_limit
->max_priority
= MAXPRI_KERNEL
;
608 *count
= POLICY_RR_LIMIT_COUNT
;
610 return(KERN_SUCCESS
);
612 else if (flavor
== PROCESSOR_SET_ENABLED_POLICIES
) {
613 register int *enabled
;
615 if (*count
< (sizeof(*enabled
)/sizeof(int)))
616 return(KERN_FAILURE
);
618 enabled
= (int *) info
;
619 *enabled
= POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
;
621 *count
= sizeof(*enabled
)/sizeof(int);
623 return(KERN_SUCCESS
);
628 return(KERN_INVALID_ARGUMENT
);
632 * processor_set_statistics
634 * Returns scheduling statistics for a processor set.
637 processor_set_statistics(
638 processor_set_t pset
,
640 processor_set_info_t info
,
641 mach_msg_type_number_t
*count
)
643 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
644 return (KERN_INVALID_PROCESSOR_SET
);
646 if (flavor
== PROCESSOR_SET_LOAD_INFO
) {
647 register processor_set_load_info_t load_info
;
649 if (*count
< PROCESSOR_SET_LOAD_INFO_COUNT
)
650 return(KERN_FAILURE
);
652 load_info
= (processor_set_load_info_t
) info
;
654 load_info
->mach_factor
= sched_mach_factor
;
655 load_info
->load_average
= sched_load_average
;
657 load_info
->task_count
= tasks_count
;
658 load_info
->thread_count
= threads_count
;
660 *count
= PROCESSOR_SET_LOAD_INFO_COUNT
;
661 return(KERN_SUCCESS
);
664 return(KERN_INVALID_ARGUMENT
);
668 * processor_set_max_priority:
670 * Specify max priority permitted on processor set. This affects
671 * newly created and assigned threads. Optionally change existing
675 processor_set_max_priority(
676 __unused processor_set_t pset
,
677 __unused
int max_priority
,
678 __unused boolean_t change_threads
)
680 return (KERN_INVALID_ARGUMENT
);
684 * processor_set_policy_enable:
686 * Allow indicated policy on processor set.
690 processor_set_policy_enable(
691 __unused processor_set_t pset
,
694 return (KERN_INVALID_ARGUMENT
);
698 * processor_set_policy_disable:
700 * Forbid indicated policy on processor set. Time sharing cannot
704 processor_set_policy_disable(
705 __unused processor_set_t pset
,
707 __unused boolean_t change_threads
)
709 return (KERN_INVALID_ARGUMENT
);
713 #define THING_THREAD 1
716 * processor_set_things:
718 * Common internals for processor_set_{threads,tasks}
721 processor_set_things(
722 processor_set_t pset
,
723 mach_port_t
**thing_list
,
724 mach_msg_type_number_t
*count
,
727 unsigned int actual
; /* this many things */
728 unsigned int maxthings
;
731 vm_size_t size
, size_needed
;
734 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
735 return (KERN_INVALID_ARGUMENT
);
741 lck_mtx_lock(&tasks_threads_lock
);
743 if (type
== THING_TASK
)
744 maxthings
= tasks_count
;
746 maxthings
= threads_count
;
748 /* do we have the memory we need? */
750 size_needed
= maxthings
* sizeof (mach_port_t
);
751 if (size_needed
<= size
)
754 /* unlock and allocate more memory */
755 lck_mtx_unlock(&tasks_threads_lock
);
760 assert(size_needed
> 0);
765 return (KERN_RESOURCE_SHORTAGE
);
768 /* OK, have memory and the list locked */
774 task_t task
, *task_list
= (task_t
*)addr
;
776 for (task
= (task_t
)queue_first(&tasks
);
777 !queue_end(&tasks
, (queue_entry_t
)task
);
778 task
= (task_t
)queue_next(&task
->tasks
)) {
779 #if defined(SECURE_KERNEL)
780 if (task
!= kernel_task
) {
782 task_reference_internal(task
);
783 task_list
[actual
++] = task
;
784 #if defined(SECURE_KERNEL)
793 thread_t thread
, *thread_list
= (thread_t
*)addr
;
795 for (thread
= (thread_t
)queue_first(&threads
);
796 !queue_end(&threads
, (queue_entry_t
)thread
);
797 thread
= (thread_t
)queue_next(&thread
->threads
)) {
798 thread_reference_internal(thread
);
799 thread_list
[actual
++] = thread
;
807 lck_mtx_unlock(&tasks_threads_lock
);
809 if (actual
< maxthings
)
810 size_needed
= actual
* sizeof (mach_port_t
);
813 /* no things, so return null pointer and deallocate memory */
821 /* if we allocated too much, must copy */
823 if (size_needed
< size
) {
826 newaddr
= kalloc(size_needed
);
831 task_t
*task_list
= (task_t
*)addr
;
833 for (i
= 0; i
< actual
; i
++)
834 task_deallocate(task_list
[i
]);
839 thread_t
*thread_list
= (thread_t
*)addr
;
841 for (i
= 0; i
< actual
; i
++)
842 thread_deallocate(thread_list
[i
]);
849 return (KERN_RESOURCE_SHORTAGE
);
852 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
857 *thing_list
= (mach_port_t
*)addr
;
860 /* do the conversion that Mig should handle */
865 task_t
*task_list
= (task_t
*)addr
;
867 for (i
= 0; i
< actual
; i
++)
868 (*thing_list
)[i
] = convert_task_to_port(task_list
[i
]);
873 thread_t
*thread_list
= (thread_t
*)addr
;
875 for (i
= 0; i
< actual
; i
++)
876 (*thing_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
883 return (KERN_SUCCESS
);
888 * processor_set_tasks:
890 * List all tasks in the processor set.
894 processor_set_t pset
,
895 task_array_t
*task_list
,
896 mach_msg_type_number_t
*count
)
898 return(processor_set_things(pset
, (mach_port_t
**)task_list
, count
, THING_TASK
));
902 * processor_set_threads:
904 * List all threads in the processor set.
906 #if defined(SECURE_KERNEL)
908 processor_set_threads(
909 __unused processor_set_t pset
,
910 __unused thread_array_t
*thread_list
,
911 __unused mach_msg_type_number_t
*count
)
915 #elif defined(CONFIG_EMBEDDED)
917 processor_set_threads(
918 __unused processor_set_t pset
,
919 __unused thread_array_t
*thread_list
,
920 __unused mach_msg_type_number_t
*count
)
922 return KERN_NOT_SUPPORTED
;
926 processor_set_threads(
927 processor_set_t pset
,
928 thread_array_t
*thread_list
,
929 mach_msg_type_number_t
*count
)
931 return(processor_set_things(pset
, (mach_port_t
**)thread_list
, count
, THING_THREAD
));
936 * processor_set_policy_control
938 * Controls the scheduling attributes governing the processor set.
939 * Allows control of enabled policies, and per-policy base and limit
943 processor_set_policy_control(
944 __unused processor_set_t pset
,
946 __unused processor_set_info_t policy_info
,
947 __unused mach_msg_type_number_t count
,
948 __unused boolean_t change
)
950 return (KERN_INVALID_ARGUMENT
);
953 #undef pset_deallocate
954 void pset_deallocate(processor_set_t pset
);
957 __unused processor_set_t pset
)
962 #undef pset_reference
963 void pset_reference(processor_set_t pset
);
966 __unused processor_set_t pset
)