2 * Copyright (c) 2000-2008 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_mutex_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);
122 mutex_init(&tasks_threads_lock
, 0);
124 queue_init(&threads
);
126 simple_lock_init(&processor_list_lock
, 0);
128 master_processor
= cpu_to_processor(master_cpu
);
130 processor_init(master_processor
, master_cpu
, &pset0
);
134 * Initialize the given processor for the cpu
135 * indicated by cpu_num, and assign to the
136 * specified processor set.
140 processor_t processor
,
142 processor_set_t pset
)
144 run_queue_init(&processor
->runq
);
146 processor
->state
= PROCESSOR_OFF_LINE
;
147 processor
->active_thread
= processor
->next_thread
= processor
->idle_thread
= THREAD_NULL
;
148 processor
->processor_set
= pset
;
149 processor
->current_pri
= MINPRI
;
150 processor
->cpu_num
= cpu_num
;
151 timer_call_setup(&processor
->quantum_timer
, thread_quantum_expire
, processor
);
152 processor
->deadline
= UINT64_MAX
;
153 processor
->timeslice
= 0;
154 processor
->processor_self
= IP_NULL
;
155 simple_lock_init(&processor
->lock
, 0);
156 processor_data_init(processor
);
157 processor
->processor_list
= NULL
;
159 simple_lock(&processor_list_lock
);
160 if (processor_list
== NULL
)
161 processor_list
= processor
;
163 processor_list_tail
->processor_list
= processor
;
164 processor_list_tail
= processor
;
166 simple_unlock(&processor_list_lock
);
171 processor_t processor
)
173 return (processor
->processor_set
);
186 processor_set_t
*prev
, pset
= kalloc(sizeof (*pset
));
188 if (pset
!= PROCESSOR_SET_NULL
) {
189 pset_init(pset
, node
);
191 simple_lock(&pset_node_lock
);
194 while (*prev
!= PROCESSOR_SET_NULL
)
195 prev
= &(*prev
)->pset_list
;
199 simple_unlock(&pset_node_lock
);
206 * Initialize the given processor_set structure.
210 processor_set_t pset
,
213 queue_init(&pset
->active_queue
);
214 queue_init(&pset
->idle_queue
);
215 pset
->processor_count
= 0;
216 pset
->low_pri
= pset
->low_count
= PROCESSOR_NULL
;
217 pset_lock_init(pset
);
218 pset
->pset_self
= IP_NULL
;
219 pset
->pset_name_self
= IP_NULL
;
220 pset
->pset_list
= PROCESSOR_SET_NULL
;
225 processor_info_count(
226 processor_flavor_t flavor
,
227 mach_msg_type_number_t
*count
)
231 case PROCESSOR_BASIC_INFO
:
232 *count
= PROCESSOR_BASIC_INFO_COUNT
;
235 case PROCESSOR_CPU_LOAD_INFO
:
236 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
240 return (cpu_info_count(flavor
, count
));
243 return (KERN_SUCCESS
);
249 register processor_t processor
,
250 processor_flavor_t flavor
,
252 processor_info_t info
,
253 mach_msg_type_number_t
*count
)
255 register int cpu_num
, state
;
256 kern_return_t result
;
258 if (processor
== PROCESSOR_NULL
)
259 return (KERN_INVALID_ARGUMENT
);
261 cpu_num
= processor
->cpu_num
;
265 case PROCESSOR_BASIC_INFO
:
267 register processor_basic_info_t basic_info
;
269 if (*count
< PROCESSOR_BASIC_INFO_COUNT
)
270 return (KERN_FAILURE
);
272 basic_info
= (processor_basic_info_t
) info
;
273 basic_info
->cpu_type
= slot_type(cpu_num
);
274 basic_info
->cpu_subtype
= slot_subtype(cpu_num
);
275 state
= processor
->state
;
276 if (state
== PROCESSOR_OFF_LINE
)
277 basic_info
->running
= FALSE
;
279 basic_info
->running
= TRUE
;
280 basic_info
->slot_num
= cpu_num
;
281 if (processor
== master_processor
)
282 basic_info
->is_master
= TRUE
;
284 basic_info
->is_master
= FALSE
;
286 *count
= PROCESSOR_BASIC_INFO_COUNT
;
289 return (KERN_SUCCESS
);
292 case PROCESSOR_CPU_LOAD_INFO
:
294 register processor_cpu_load_info_t cpu_load_info
;
296 if (*count
< PROCESSOR_CPU_LOAD_INFO_COUNT
)
297 return (KERN_FAILURE
);
299 cpu_load_info
= (processor_cpu_load_info_t
) info
;
300 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] =
301 timer_grab(&PROCESSOR_DATA(processor
, user_state
)) / hz_tick_interval
;
302 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] =
303 timer_grab(&PROCESSOR_DATA(processor
, system_state
)) / hz_tick_interval
;
304 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
305 timer_grab(&PROCESSOR_DATA(processor
, idle_state
)) / hz_tick_interval
;
306 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
308 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
311 return (KERN_SUCCESS
);
315 result
= cpu_info(flavor
, cpu_num
, info
, count
);
316 if (result
== KERN_SUCCESS
)
325 processor_t processor
)
327 processor_set_t pset
;
329 kern_return_t result
;
332 if (processor
== PROCESSOR_NULL
|| processor
->processor_set
== PROCESSOR_SET_NULL
)
333 return (KERN_INVALID_ARGUMENT
);
335 if (processor
== master_processor
) {
338 prev
= thread_bind(processor
);
339 thread_block(THREAD_CONTINUE_NULL
);
341 result
= cpu_start(processor
->cpu_num
);
349 pset
= processor
->processor_set
;
351 if (processor
->state
!= PROCESSOR_OFF_LINE
) {
355 return (KERN_FAILURE
);
358 processor
->state
= PROCESSOR_START
;
363 * Create the idle processor thread.
365 if (processor
->idle_thread
== THREAD_NULL
) {
366 result
= idle_thread_create(processor
);
367 if (result
!= KERN_SUCCESS
) {
370 processor
->state
= PROCESSOR_OFF_LINE
;
379 * If there is no active thread, the processor
380 * has never been started. Create a dedicated
383 if ( processor
->active_thread
== THREAD_NULL
&&
384 processor
->next_thread
== THREAD_NULL
) {
385 result
= kernel_thread_create((thread_continue_t
)processor_start_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
386 if (result
!= KERN_SUCCESS
) {
389 processor
->state
= PROCESSOR_OFF_LINE
;
398 thread
->bound_processor
= processor
;
399 processor
->next_thread
= thread
;
400 thread
->state
= TH_RUN
;
401 thread_unlock(thread
);
404 thread_deallocate(thread
);
407 if (processor
->processor_self
== IP_NULL
)
408 ipc_processor_init(processor
);
410 result
= cpu_start(processor
->cpu_num
);
411 if (result
!= KERN_SUCCESS
) {
414 processor
->state
= PROCESSOR_OFF_LINE
;
421 ipc_processor_enable(processor
);
423 return (KERN_SUCCESS
);
428 processor_t processor
)
430 if (processor
== PROCESSOR_NULL
)
431 return(KERN_INVALID_ARGUMENT
);
433 return(processor_shutdown(processor
));
438 processor_t processor
,
439 processor_info_t info
,
440 mach_msg_type_number_t count
)
442 if (processor
== PROCESSOR_NULL
)
443 return(KERN_INVALID_ARGUMENT
);
445 return(cpu_control(processor
->cpu_num
, info
, count
));
449 processor_set_create(
450 __unused host_t host
,
451 __unused processor_set_t
*new_set
,
452 __unused processor_set_t
*new_name
)
454 return(KERN_FAILURE
);
458 processor_set_destroy(
459 __unused processor_set_t pset
)
461 return(KERN_FAILURE
);
465 processor_get_assignment(
466 processor_t processor
,
467 processor_set_t
*pset
)
471 state
= processor
->state
;
472 if (state
== PROCESSOR_SHUTDOWN
|| state
== PROCESSOR_OFF_LINE
)
473 return(KERN_FAILURE
);
477 return(KERN_SUCCESS
);
482 processor_set_t pset
,
485 processor_set_info_t info
,
486 mach_msg_type_number_t
*count
)
488 if (pset
== PROCESSOR_SET_NULL
)
489 return(KERN_INVALID_ARGUMENT
);
491 if (flavor
== PROCESSOR_SET_BASIC_INFO
) {
492 register processor_set_basic_info_t basic_info
;
494 if (*count
< PROCESSOR_SET_BASIC_INFO_COUNT
)
495 return(KERN_FAILURE
);
497 basic_info
= (processor_set_basic_info_t
) info
;
498 basic_info
->processor_count
= processor_avail_count
;
499 basic_info
->default_policy
= POLICY_TIMESHARE
;
501 *count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
503 return(KERN_SUCCESS
);
505 else if (flavor
== PROCESSOR_SET_TIMESHARE_DEFAULT
) {
506 register policy_timeshare_base_t ts_base
;
508 if (*count
< POLICY_TIMESHARE_BASE_COUNT
)
509 return(KERN_FAILURE
);
511 ts_base
= (policy_timeshare_base_t
) info
;
512 ts_base
->base_priority
= BASEPRI_DEFAULT
;
514 *count
= POLICY_TIMESHARE_BASE_COUNT
;
516 return(KERN_SUCCESS
);
518 else if (flavor
== PROCESSOR_SET_FIFO_DEFAULT
) {
519 register policy_fifo_base_t fifo_base
;
521 if (*count
< POLICY_FIFO_BASE_COUNT
)
522 return(KERN_FAILURE
);
524 fifo_base
= (policy_fifo_base_t
) info
;
525 fifo_base
->base_priority
= BASEPRI_DEFAULT
;
527 *count
= POLICY_FIFO_BASE_COUNT
;
529 return(KERN_SUCCESS
);
531 else if (flavor
== PROCESSOR_SET_RR_DEFAULT
) {
532 register policy_rr_base_t rr_base
;
534 if (*count
< POLICY_RR_BASE_COUNT
)
535 return(KERN_FAILURE
);
537 rr_base
= (policy_rr_base_t
) info
;
538 rr_base
->base_priority
= BASEPRI_DEFAULT
;
539 rr_base
->quantum
= 1;
541 *count
= POLICY_RR_BASE_COUNT
;
543 return(KERN_SUCCESS
);
545 else if (flavor
== PROCESSOR_SET_TIMESHARE_LIMITS
) {
546 register policy_timeshare_limit_t ts_limit
;
548 if (*count
< POLICY_TIMESHARE_LIMIT_COUNT
)
549 return(KERN_FAILURE
);
551 ts_limit
= (policy_timeshare_limit_t
) info
;
552 ts_limit
->max_priority
= MAXPRI_KERNEL
;
554 *count
= POLICY_TIMESHARE_LIMIT_COUNT
;
556 return(KERN_SUCCESS
);
558 else if (flavor
== PROCESSOR_SET_FIFO_LIMITS
) {
559 register policy_fifo_limit_t fifo_limit
;
561 if (*count
< POLICY_FIFO_LIMIT_COUNT
)
562 return(KERN_FAILURE
);
564 fifo_limit
= (policy_fifo_limit_t
) info
;
565 fifo_limit
->max_priority
= MAXPRI_KERNEL
;
567 *count
= POLICY_FIFO_LIMIT_COUNT
;
569 return(KERN_SUCCESS
);
571 else if (flavor
== PROCESSOR_SET_RR_LIMITS
) {
572 register policy_rr_limit_t rr_limit
;
574 if (*count
< POLICY_RR_LIMIT_COUNT
)
575 return(KERN_FAILURE
);
577 rr_limit
= (policy_rr_limit_t
) info
;
578 rr_limit
->max_priority
= MAXPRI_KERNEL
;
580 *count
= POLICY_RR_LIMIT_COUNT
;
582 return(KERN_SUCCESS
);
584 else if (flavor
== PROCESSOR_SET_ENABLED_POLICIES
) {
585 register int *enabled
;
587 if (*count
< (sizeof(*enabled
)/sizeof(int)))
588 return(KERN_FAILURE
);
590 enabled
= (int *) info
;
591 *enabled
= POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
;
593 *count
= sizeof(*enabled
)/sizeof(int);
595 return(KERN_SUCCESS
);
600 return(KERN_INVALID_ARGUMENT
);
604 * processor_set_statistics
606 * Returns scheduling statistics for a processor set.
609 processor_set_statistics(
610 processor_set_t pset
,
612 processor_set_info_t info
,
613 mach_msg_type_number_t
*count
)
615 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
616 return (KERN_INVALID_PROCESSOR_SET
);
618 if (flavor
== PROCESSOR_SET_LOAD_INFO
) {
619 register processor_set_load_info_t load_info
;
621 if (*count
< PROCESSOR_SET_LOAD_INFO_COUNT
)
622 return(KERN_FAILURE
);
624 load_info
= (processor_set_load_info_t
) info
;
626 load_info
->mach_factor
= sched_mach_factor
;
627 load_info
->load_average
= sched_load_average
;
629 load_info
->task_count
= tasks_count
;
630 load_info
->thread_count
= threads_count
;
632 *count
= PROCESSOR_SET_LOAD_INFO_COUNT
;
633 return(KERN_SUCCESS
);
636 return(KERN_INVALID_ARGUMENT
);
640 * processor_set_max_priority:
642 * Specify max priority permitted on processor set. This affects
643 * newly created and assigned threads. Optionally change existing
647 processor_set_max_priority(
648 __unused processor_set_t pset
,
649 __unused
int max_priority
,
650 __unused boolean_t change_threads
)
652 return (KERN_INVALID_ARGUMENT
);
656 * processor_set_policy_enable:
658 * Allow indicated policy on processor set.
662 processor_set_policy_enable(
663 __unused processor_set_t pset
,
666 return (KERN_INVALID_ARGUMENT
);
670 * processor_set_policy_disable:
672 * Forbid indicated policy on processor set. Time sharing cannot
676 processor_set_policy_disable(
677 __unused processor_set_t pset
,
679 __unused boolean_t change_threads
)
681 return (KERN_INVALID_ARGUMENT
);
685 #define THING_THREAD 1
688 * processor_set_things:
690 * Common internals for processor_set_{threads,tasks}
693 processor_set_things(
694 processor_set_t pset
,
695 mach_port_t
**thing_list
,
696 mach_msg_type_number_t
*count
,
699 unsigned int actual
; /* this many things */
700 unsigned int maxthings
;
703 vm_size_t size
, size_needed
;
706 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
707 return (KERN_INVALID_ARGUMENT
);
713 mutex_lock(&tasks_threads_lock
);
715 if (type
== THING_TASK
)
716 maxthings
= tasks_count
;
718 maxthings
= threads_count
;
720 /* do we have the memory we need? */
722 size_needed
= maxthings
* sizeof (mach_port_t
);
723 if (size_needed
<= size
)
726 /* unlock and allocate more memory */
727 mutex_unlock(&tasks_threads_lock
);
732 assert(size_needed
> 0);
737 return (KERN_RESOURCE_SHORTAGE
);
740 /* OK, have memory and the list locked */
746 task_t task
, *task_list
= (task_t
*)addr
;
748 for (task
= (task_t
)queue_first(&tasks
);
749 !queue_end(&tasks
, (queue_entry_t
)task
);
750 task
= (task_t
)queue_next(&task
->tasks
)) {
751 #if defined(SECURE_KERNEL)
752 if (task
!= kernel_task
) {
754 task_reference_internal(task
);
755 task_list
[actual
++] = task
;
756 #if defined(SECURE_KERNEL)
765 thread_t thread
, *thread_list
= (thread_t
*)addr
;
767 for (thread
= (thread_t
)queue_first(&threads
);
768 !queue_end(&threads
, (queue_entry_t
)thread
);
769 thread
= (thread_t
)queue_next(&thread
->threads
)) {
770 thread_reference_internal(thread
);
771 thread_list
[actual
++] = thread
;
779 mutex_unlock(&tasks_threads_lock
);
781 if (actual
< maxthings
)
782 size_needed
= actual
* sizeof (mach_port_t
);
785 /* no things, so return null pointer and deallocate memory */
793 /* if we allocated too much, must copy */
795 if (size_needed
< size
) {
798 newaddr
= kalloc(size_needed
);
803 task_t
*task_list
= (task_t
*)addr
;
805 for (i
= 0; i
< actual
; i
++)
806 task_deallocate(task_list
[i
]);
811 thread_t
*thread_list
= (thread_t
*)addr
;
813 for (i
= 0; i
< actual
; i
++)
814 thread_deallocate(thread_list
[i
]);
821 return (KERN_RESOURCE_SHORTAGE
);
824 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
829 *thing_list
= (mach_port_t
*)addr
;
832 /* do the conversion that Mig should handle */
837 task_t
*task_list
= (task_t
*)addr
;
839 for (i
= 0; i
< actual
; i
++)
840 (*thing_list
)[i
] = convert_task_to_port(task_list
[i
]);
845 thread_t
*thread_list
= (thread_t
*)addr
;
847 for (i
= 0; i
< actual
; i
++)
848 (*thing_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
855 return (KERN_SUCCESS
);
860 * processor_set_tasks:
862 * List all tasks in the processor set.
866 processor_set_t pset
,
867 task_array_t
*task_list
,
868 mach_msg_type_number_t
*count
)
870 return(processor_set_things(pset
, (mach_port_t
**)task_list
, count
, THING_TASK
));
874 * processor_set_threads:
876 * List all threads in the processor set.
878 #if defined(SECURE_KERNEL)
880 processor_set_threads(
881 __unused processor_set_t pset
,
882 __unused thread_array_t
*thread_list
,
883 __unused mach_msg_type_number_t
*count
)
887 #elif defined(CONFIG_EMBEDDED)
889 processor_set_threads(
890 __unused processor_set_t pset
,
891 __unused thread_array_t
*thread_list
,
892 __unused mach_msg_type_number_t
*count
)
894 return KERN_NOT_SUPPORTED
;
898 processor_set_threads(
899 processor_set_t pset
,
900 thread_array_t
*thread_list
,
901 mach_msg_type_number_t
*count
)
903 return(processor_set_things(pset
, (mach_port_t
**)thread_list
, count
, THING_THREAD
));
908 * processor_set_policy_control
910 * Controls the scheduling attributes governing the processor set.
911 * Allows control of enabled policies, and per-policy base and limit
915 processor_set_policy_control(
916 __unused processor_set_t pset
,
918 __unused processor_set_info_t policy_info
,
919 __unused mach_msg_type_number_t count
,
920 __unused boolean_t change
)
922 return (KERN_INVALID_ARGUMENT
);
925 #undef pset_deallocate
926 void pset_deallocate(processor_set_t pset
);
929 __unused processor_set_t pset
)
934 #undef pset_reference
935 void pset_reference(processor_set_t pset
);
938 __unused processor_set_t pset
)