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 slot_num, and assign to the
136 * specified processor set.
142 processor_set_t pset
)
144 run_queue_init(&p
->runq
);
146 p
->state
= PROCESSOR_OFF_LINE
;
147 p
->active_thread
= p
->next_thread
= p
->idle_thread
= THREAD_NULL
;
148 p
->processor_set
= pset
;
149 p
->current_pri
= MINPRI
;
150 timer_call_setup(&p
->quantum_timer
, thread_quantum_expire
, p
);
151 p
->deadline
= UINT64_MAX
;
153 p
->processor_self
= IP_NULL
;
154 simple_lock_init(&p
->lock
, 0);
155 processor_data_init(p
);
156 PROCESSOR_DATA(p
, slot_num
) = slot_num
;
157 p
->processor_list
= NULL
;
159 simple_lock(&processor_list_lock
);
160 if (processor_list
== NULL
)
163 processor_list_tail
->processor_list
= p
;
164 processor_list_tail
= p
;
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
->idle_count
= 0;
216 pset
->processor_count
= 0;
217 pset
->low_pri
= PROCESSOR_NULL
;
218 pset_lock_init(pset
);
219 pset
->pset_self
= IP_NULL
;
220 pset
->pset_name_self
= IP_NULL
;
221 pset
->pset_list
= PROCESSOR_SET_NULL
;
226 processor_info_count(
227 processor_flavor_t flavor
,
228 mach_msg_type_number_t
*count
)
232 case PROCESSOR_BASIC_INFO
:
233 *count
= PROCESSOR_BASIC_INFO_COUNT
;
236 case PROCESSOR_CPU_LOAD_INFO
:
237 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
241 return (cpu_info_count(flavor
, count
));
244 return (KERN_SUCCESS
);
250 register processor_t processor
,
251 processor_flavor_t flavor
,
253 processor_info_t info
,
254 mach_msg_type_number_t
*count
)
256 register int slot_num
, state
;
257 kern_return_t result
;
259 if (processor
== PROCESSOR_NULL
)
260 return (KERN_INVALID_ARGUMENT
);
262 slot_num
= PROCESSOR_DATA(processor
, slot_num
);
266 case PROCESSOR_BASIC_INFO
:
268 register processor_basic_info_t basic_info
;
270 if (*count
< PROCESSOR_BASIC_INFO_COUNT
)
271 return (KERN_FAILURE
);
273 basic_info
= (processor_basic_info_t
) info
;
274 basic_info
->cpu_type
= slot_type(slot_num
);
275 basic_info
->cpu_subtype
= slot_subtype(slot_num
);
276 state
= processor
->state
;
277 if (state
== PROCESSOR_OFF_LINE
)
278 basic_info
->running
= FALSE
;
280 basic_info
->running
= TRUE
;
281 basic_info
->slot_num
= slot_num
;
282 if (processor
== master_processor
)
283 basic_info
->is_master
= TRUE
;
285 basic_info
->is_master
= FALSE
;
287 *count
= PROCESSOR_BASIC_INFO_COUNT
;
290 return (KERN_SUCCESS
);
293 case PROCESSOR_CPU_LOAD_INFO
:
295 register processor_cpu_load_info_t cpu_load_info
;
297 if (*count
< PROCESSOR_CPU_LOAD_INFO_COUNT
)
298 return (KERN_FAILURE
);
300 cpu_load_info
= (processor_cpu_load_info_t
) info
;
301 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] =
302 timer_grab(&PROCESSOR_DATA(processor
, user_state
)) / hz_tick_interval
;
303 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] =
304 timer_grab(&PROCESSOR_DATA(processor
, system_state
)) / hz_tick_interval
;
305 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
306 timer_grab(&PROCESSOR_DATA(processor
, idle_state
)) / hz_tick_interval
;
307 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
309 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
312 return (KERN_SUCCESS
);
316 result
= cpu_info(flavor
, slot_num
, info
, count
);
317 if (result
== KERN_SUCCESS
)
326 processor_t processor
)
328 processor_set_t pset
;
330 kern_return_t result
;
333 if (processor
== PROCESSOR_NULL
|| processor
->processor_set
== PROCESSOR_SET_NULL
)
334 return (KERN_INVALID_ARGUMENT
);
336 if (processor
== master_processor
) {
339 prev
= thread_bind(processor
);
340 thread_block(THREAD_CONTINUE_NULL
);
342 result
= cpu_start(PROCESSOR_DATA(processor
, slot_num
));
350 pset
= processor
->processor_set
;
352 if (processor
->state
!= PROCESSOR_OFF_LINE
) {
356 return (KERN_FAILURE
);
359 processor
->state
= PROCESSOR_START
;
364 * Create the idle processor thread.
366 if (processor
->idle_thread
== THREAD_NULL
) {
367 result
= idle_thread_create(processor
);
368 if (result
!= KERN_SUCCESS
) {
371 processor
->state
= PROCESSOR_OFF_LINE
;
380 * If there is no active thread, the processor
381 * has never been started. Create a dedicated
384 if ( processor
->active_thread
== THREAD_NULL
&&
385 processor
->next_thread
== THREAD_NULL
) {
386 result
= kernel_thread_create((thread_continue_t
)processor_start_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
387 if (result
!= KERN_SUCCESS
) {
390 processor
->state
= PROCESSOR_OFF_LINE
;
399 thread
->bound_processor
= processor
;
400 processor
->next_thread
= thread
;
401 thread
->state
= TH_RUN
;
402 thread_unlock(thread
);
405 thread_deallocate(thread
);
408 if (processor
->processor_self
== IP_NULL
)
409 ipc_processor_init(processor
);
411 result
= cpu_start(PROCESSOR_DATA(processor
, slot_num
));
412 if (result
!= KERN_SUCCESS
) {
415 processor
->state
= PROCESSOR_OFF_LINE
;
416 timer_call_shutdown(processor
);
423 ipc_processor_enable(processor
);
425 return (KERN_SUCCESS
);
430 processor_t processor
)
432 if (processor
== PROCESSOR_NULL
)
433 return(KERN_INVALID_ARGUMENT
);
435 return(processor_shutdown(processor
));
440 processor_t processor
,
441 processor_info_t info
,
442 mach_msg_type_number_t count
)
444 if (processor
== PROCESSOR_NULL
)
445 return(KERN_INVALID_ARGUMENT
);
447 return(cpu_control(PROCESSOR_DATA(processor
, slot_num
), info
, count
));
451 processor_set_create(
452 __unused host_t host
,
453 __unused processor_set_t
*new_set
,
454 __unused processor_set_t
*new_name
)
456 return(KERN_FAILURE
);
460 processor_set_destroy(
461 __unused processor_set_t pset
)
463 return(KERN_FAILURE
);
467 processor_get_assignment(
468 processor_t processor
,
469 processor_set_t
*pset
)
473 state
= processor
->state
;
474 if (state
== PROCESSOR_SHUTDOWN
|| state
== PROCESSOR_OFF_LINE
)
475 return(KERN_FAILURE
);
479 return(KERN_SUCCESS
);
484 processor_set_t pset
,
487 processor_set_info_t info
,
488 mach_msg_type_number_t
*count
)
490 if (pset
== PROCESSOR_SET_NULL
)
491 return(KERN_INVALID_ARGUMENT
);
493 if (flavor
== PROCESSOR_SET_BASIC_INFO
) {
494 register processor_set_basic_info_t basic_info
;
496 if (*count
< PROCESSOR_SET_BASIC_INFO_COUNT
)
497 return(KERN_FAILURE
);
499 basic_info
= (processor_set_basic_info_t
) info
;
500 basic_info
->processor_count
= processor_avail_count
;
501 basic_info
->default_policy
= POLICY_TIMESHARE
;
503 *count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
505 return(KERN_SUCCESS
);
507 else if (flavor
== PROCESSOR_SET_TIMESHARE_DEFAULT
) {
508 register policy_timeshare_base_t ts_base
;
510 if (*count
< POLICY_TIMESHARE_BASE_COUNT
)
511 return(KERN_FAILURE
);
513 ts_base
= (policy_timeshare_base_t
) info
;
514 ts_base
->base_priority
= BASEPRI_DEFAULT
;
516 *count
= POLICY_TIMESHARE_BASE_COUNT
;
518 return(KERN_SUCCESS
);
520 else if (flavor
== PROCESSOR_SET_FIFO_DEFAULT
) {
521 register policy_fifo_base_t fifo_base
;
523 if (*count
< POLICY_FIFO_BASE_COUNT
)
524 return(KERN_FAILURE
);
526 fifo_base
= (policy_fifo_base_t
) info
;
527 fifo_base
->base_priority
= BASEPRI_DEFAULT
;
529 *count
= POLICY_FIFO_BASE_COUNT
;
531 return(KERN_SUCCESS
);
533 else if (flavor
== PROCESSOR_SET_RR_DEFAULT
) {
534 register policy_rr_base_t rr_base
;
536 if (*count
< POLICY_RR_BASE_COUNT
)
537 return(KERN_FAILURE
);
539 rr_base
= (policy_rr_base_t
) info
;
540 rr_base
->base_priority
= BASEPRI_DEFAULT
;
541 rr_base
->quantum
= 1;
543 *count
= POLICY_RR_BASE_COUNT
;
545 return(KERN_SUCCESS
);
547 else if (flavor
== PROCESSOR_SET_TIMESHARE_LIMITS
) {
548 register policy_timeshare_limit_t ts_limit
;
550 if (*count
< POLICY_TIMESHARE_LIMIT_COUNT
)
551 return(KERN_FAILURE
);
553 ts_limit
= (policy_timeshare_limit_t
) info
;
554 ts_limit
->max_priority
= MAXPRI_KERNEL
;
556 *count
= POLICY_TIMESHARE_LIMIT_COUNT
;
558 return(KERN_SUCCESS
);
560 else if (flavor
== PROCESSOR_SET_FIFO_LIMITS
) {
561 register policy_fifo_limit_t fifo_limit
;
563 if (*count
< POLICY_FIFO_LIMIT_COUNT
)
564 return(KERN_FAILURE
);
566 fifo_limit
= (policy_fifo_limit_t
) info
;
567 fifo_limit
->max_priority
= MAXPRI_KERNEL
;
569 *count
= POLICY_FIFO_LIMIT_COUNT
;
571 return(KERN_SUCCESS
);
573 else if (flavor
== PROCESSOR_SET_RR_LIMITS
) {
574 register policy_rr_limit_t rr_limit
;
576 if (*count
< POLICY_RR_LIMIT_COUNT
)
577 return(KERN_FAILURE
);
579 rr_limit
= (policy_rr_limit_t
) info
;
580 rr_limit
->max_priority
= MAXPRI_KERNEL
;
582 *count
= POLICY_RR_LIMIT_COUNT
;
584 return(KERN_SUCCESS
);
586 else if (flavor
== PROCESSOR_SET_ENABLED_POLICIES
) {
587 register int *enabled
;
589 if (*count
< (sizeof(*enabled
)/sizeof(int)))
590 return(KERN_FAILURE
);
592 enabled
= (int *) info
;
593 *enabled
= POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
;
595 *count
= sizeof(*enabled
)/sizeof(int);
597 return(KERN_SUCCESS
);
602 return(KERN_INVALID_ARGUMENT
);
606 * processor_set_statistics
608 * Returns scheduling statistics for a processor set.
611 processor_set_statistics(
612 processor_set_t pset
,
614 processor_set_info_t info
,
615 mach_msg_type_number_t
*count
)
617 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
618 return (KERN_INVALID_PROCESSOR_SET
);
620 if (flavor
== PROCESSOR_SET_LOAD_INFO
) {
621 register processor_set_load_info_t load_info
;
623 if (*count
< PROCESSOR_SET_LOAD_INFO_COUNT
)
624 return(KERN_FAILURE
);
626 load_info
= (processor_set_load_info_t
) info
;
628 load_info
->mach_factor
= sched_mach_factor
;
629 load_info
->load_average
= sched_load_average
;
631 load_info
->task_count
= tasks_count
;
632 load_info
->thread_count
= threads_count
;
634 *count
= PROCESSOR_SET_LOAD_INFO_COUNT
;
635 return(KERN_SUCCESS
);
638 return(KERN_INVALID_ARGUMENT
);
642 * processor_set_max_priority:
644 * Specify max priority permitted on processor set. This affects
645 * newly created and assigned threads. Optionally change existing
649 processor_set_max_priority(
650 __unused processor_set_t pset
,
651 __unused
int max_priority
,
652 __unused boolean_t change_threads
)
654 return (KERN_INVALID_ARGUMENT
);
658 * processor_set_policy_enable:
660 * Allow indicated policy on processor set.
664 processor_set_policy_enable(
665 __unused processor_set_t pset
,
668 return (KERN_INVALID_ARGUMENT
);
672 * processor_set_policy_disable:
674 * Forbid indicated policy on processor set. Time sharing cannot
678 processor_set_policy_disable(
679 __unused processor_set_t pset
,
681 __unused boolean_t change_threads
)
683 return (KERN_INVALID_ARGUMENT
);
687 #define THING_THREAD 1
690 * processor_set_things:
692 * Common internals for processor_set_{threads,tasks}
695 processor_set_things(
696 processor_set_t pset
,
697 mach_port_t
**thing_list
,
698 mach_msg_type_number_t
*count
,
701 unsigned int actual
; /* this many things */
702 unsigned int maxthings
;
705 vm_size_t size
, size_needed
;
708 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
709 return (KERN_INVALID_ARGUMENT
);
715 mutex_lock(&tasks_threads_lock
);
717 if (type
== THING_TASK
)
718 maxthings
= tasks_count
;
720 maxthings
= threads_count
;
722 /* do we have the memory we need? */
724 size_needed
= maxthings
* sizeof (mach_port_t
);
725 if (size_needed
<= size
)
728 /* unlock and allocate more memory */
729 mutex_unlock(&tasks_threads_lock
);
734 assert(size_needed
> 0);
739 return (KERN_RESOURCE_SHORTAGE
);
742 /* OK, have memory and the list locked */
748 task_t task
, *task_list
= (task_t
*)addr
;
750 for (task
= (task_t
)queue_first(&tasks
);
751 !queue_end(&tasks
, (queue_entry_t
)task
);
752 task
= (task_t
)queue_next(&task
->tasks
)) {
753 #if defined(SECURE_KERNEL)
754 if (task
!= kernel_task
) {
756 task_reference_internal(task
);
757 task_list
[actual
++] = task
;
758 #if defined(SECURE_KERNEL)
767 thread_t thread
, *thread_list
= (thread_t
*)addr
;
769 for (thread
= (thread_t
)queue_first(&threads
);
770 !queue_end(&threads
, (queue_entry_t
)thread
);
771 thread
= (thread_t
)queue_next(&thread
->threads
)) {
772 thread_reference_internal(thread
);
773 thread_list
[actual
++] = thread
;
781 mutex_unlock(&tasks_threads_lock
);
783 if (actual
< maxthings
)
784 size_needed
= actual
* sizeof (mach_port_t
);
787 /* no things, so return null pointer and deallocate memory */
795 /* if we allocated too much, must copy */
797 if (size_needed
< size
) {
800 newaddr
= kalloc(size_needed
);
805 task_t
*task_list
= (task_t
*)addr
;
807 for (i
= 0; i
< actual
; i
++)
808 task_deallocate(task_list
[i
]);
813 thread_t
*thread_list
= (thread_t
*)addr
;
815 for (i
= 0; i
< actual
; i
++)
816 thread_deallocate(thread_list
[i
]);
823 return (KERN_RESOURCE_SHORTAGE
);
826 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
831 *thing_list
= (mach_port_t
*)addr
;
834 /* do the conversion that Mig should handle */
839 task_t
*task_list
= (task_t
*)addr
;
841 for (i
= 0; i
< actual
; i
++)
842 (*thing_list
)[i
] = convert_task_to_port(task_list
[i
]);
847 thread_t
*thread_list
= (thread_t
*)addr
;
849 for (i
= 0; i
< actual
; i
++)
850 (*thing_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
857 return (KERN_SUCCESS
);
862 * processor_set_tasks:
864 * List all tasks in the processor set.
868 processor_set_t pset
,
869 task_array_t
*task_list
,
870 mach_msg_type_number_t
*count
)
872 return(processor_set_things(pset
, (mach_port_t
**)task_list
, count
, THING_TASK
));
876 * processor_set_threads:
878 * List all threads in the processor set.
880 #if defined(SECURE_KERNEL)
882 processor_set_threads(
883 __unused processor_set_t pset
,
884 __unused thread_array_t
*thread_list
,
885 __unused mach_msg_type_number_t
*count
)
891 processor_set_threads(
892 processor_set_t pset
,
893 thread_array_t
*thread_list
,
894 mach_msg_type_number_t
*count
)
896 return(processor_set_things(pset
, (mach_port_t
**)thread_list
, count
, THING_THREAD
));
901 * processor_set_policy_control
903 * Controls the scheduling attributes governing the processor set.
904 * Allows control of enabled policies, and per-policy base and limit
908 processor_set_policy_control(
909 __unused processor_set_t pset
,
911 __unused processor_set_info_t policy_info
,
912 __unused mach_msg_type_number_t count
,
913 __unused boolean_t change
)
915 return (KERN_INVALID_ARGUMENT
);
918 #undef pset_deallocate
919 void pset_deallocate(processor_set_t pset
);
922 __unused processor_set_t pset
)
927 #undef pset_reference
928 void pset_reference(processor_set_t pset
);
931 __unused processor_set_t pset
)