2 * Copyright (c) 2000-2007 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
->high_hint
= PROCESSOR_NULL
;
218 pset
->low_hint
= PROCESSOR_NULL
;
219 pset_lock_init(pset
);
220 pset
->pset_self
= IP_NULL
;
221 pset
->pset_name_self
= IP_NULL
;
222 pset
->pset_list
= PROCESSOR_SET_NULL
;
227 processor_info_count(
228 processor_flavor_t flavor
,
229 mach_msg_type_number_t
*count
)
233 case PROCESSOR_BASIC_INFO
:
234 *count
= PROCESSOR_BASIC_INFO_COUNT
;
237 case PROCESSOR_CPU_LOAD_INFO
:
238 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
242 return (cpu_info_count(flavor
, count
));
245 return (KERN_SUCCESS
);
251 register processor_t processor
,
252 processor_flavor_t flavor
,
254 processor_info_t info
,
255 mach_msg_type_number_t
*count
)
257 register int slot_num
, state
;
258 kern_return_t result
;
260 if (processor
== PROCESSOR_NULL
)
261 return (KERN_INVALID_ARGUMENT
);
263 slot_num
= PROCESSOR_DATA(processor
, slot_num
);
267 case PROCESSOR_BASIC_INFO
:
269 register processor_basic_info_t basic_info
;
271 if (*count
< PROCESSOR_BASIC_INFO_COUNT
)
272 return (KERN_FAILURE
);
274 basic_info
= (processor_basic_info_t
) info
;
275 basic_info
->cpu_type
= slot_type(slot_num
);
276 basic_info
->cpu_subtype
= slot_subtype(slot_num
);
277 state
= processor
->state
;
278 if (state
== PROCESSOR_OFF_LINE
)
279 basic_info
->running
= FALSE
;
281 basic_info
->running
= TRUE
;
282 basic_info
->slot_num
= slot_num
;
283 if (processor
== master_processor
)
284 basic_info
->is_master
= TRUE
;
286 basic_info
->is_master
= FALSE
;
288 *count
= PROCESSOR_BASIC_INFO_COUNT
;
291 return (KERN_SUCCESS
);
294 case PROCESSOR_CPU_LOAD_INFO
:
296 register processor_cpu_load_info_t cpu_load_info
;
298 if (*count
< PROCESSOR_CPU_LOAD_INFO_COUNT
)
299 return (KERN_FAILURE
);
301 cpu_load_info
= (processor_cpu_load_info_t
) info
;
302 cpu_load_info
->cpu_ticks
[CPU_STATE_USER
] =
303 timer_grab(&PROCESSOR_DATA(processor
, user_state
)) / hz_tick_interval
;
304 cpu_load_info
->cpu_ticks
[CPU_STATE_SYSTEM
] =
305 timer_grab(&PROCESSOR_DATA(processor
, system_state
)) / hz_tick_interval
;
306 cpu_load_info
->cpu_ticks
[CPU_STATE_IDLE
] =
307 timer_grab(&PROCESSOR_DATA(processor
, idle_state
)) / hz_tick_interval
;
308 cpu_load_info
->cpu_ticks
[CPU_STATE_NICE
] = 0;
310 *count
= PROCESSOR_CPU_LOAD_INFO_COUNT
;
313 return (KERN_SUCCESS
);
317 result
= cpu_info(flavor
, slot_num
, info
, count
);
318 if (result
== KERN_SUCCESS
)
327 processor_t processor
)
329 processor_set_t pset
;
331 kern_return_t result
;
334 if (processor
== PROCESSOR_NULL
|| processor
->processor_set
== PROCESSOR_SET_NULL
)
335 return (KERN_INVALID_ARGUMENT
);
337 if (processor
== master_processor
) {
340 prev
= thread_bind(processor
);
341 thread_block(THREAD_CONTINUE_NULL
);
343 result
= cpu_start(PROCESSOR_DATA(processor
, slot_num
));
351 pset
= processor
->processor_set
;
353 if (processor
->state
!= PROCESSOR_OFF_LINE
) {
357 return (KERN_FAILURE
);
360 processor
->state
= PROCESSOR_START
;
365 * Create the idle processor thread.
367 if (processor
->idle_thread
== THREAD_NULL
) {
368 result
= idle_thread_create(processor
);
369 if (result
!= KERN_SUCCESS
) {
372 processor
->state
= PROCESSOR_OFF_LINE
;
381 * If there is no active thread, the processor
382 * has never been started. Create a dedicated
385 if ( processor
->active_thread
== THREAD_NULL
&&
386 processor
->next_thread
== THREAD_NULL
) {
387 result
= kernel_thread_create((thread_continue_t
)processor_start_thread
, NULL
, MAXPRI_KERNEL
, &thread
);
388 if (result
!= KERN_SUCCESS
) {
391 processor
->state
= PROCESSOR_OFF_LINE
;
400 thread
->bound_processor
= processor
;
401 processor
->next_thread
= thread
;
402 thread
->state
= TH_RUN
;
403 thread_unlock(thread
);
406 thread_deallocate(thread
);
409 if (processor
->processor_self
== IP_NULL
)
410 ipc_processor_init(processor
);
412 result
= cpu_start(PROCESSOR_DATA(processor
, slot_num
));
413 if (result
!= KERN_SUCCESS
) {
416 processor
->state
= PROCESSOR_OFF_LINE
;
417 timer_call_shutdown(processor
);
424 ipc_processor_enable(processor
);
426 return (KERN_SUCCESS
);
431 processor_t processor
)
433 if (processor
== PROCESSOR_NULL
)
434 return(KERN_INVALID_ARGUMENT
);
436 return(processor_shutdown(processor
));
441 processor_t processor
,
442 processor_info_t info
,
443 mach_msg_type_number_t count
)
445 if (processor
== PROCESSOR_NULL
)
446 return(KERN_INVALID_ARGUMENT
);
448 return(cpu_control(PROCESSOR_DATA(processor
, slot_num
), info
, count
));
452 processor_set_create(
453 __unused host_t host
,
454 __unused processor_set_t
*new_set
,
455 __unused processor_set_t
*new_name
)
457 return(KERN_FAILURE
);
461 processor_set_destroy(
462 __unused processor_set_t pset
)
464 return(KERN_FAILURE
);
468 processor_get_assignment(
469 processor_t processor
,
470 processor_set_t
*pset
)
474 state
= processor
->state
;
475 if (state
== PROCESSOR_SHUTDOWN
|| state
== PROCESSOR_OFF_LINE
)
476 return(KERN_FAILURE
);
480 return(KERN_SUCCESS
);
485 processor_set_t pset
,
488 processor_set_info_t info
,
489 mach_msg_type_number_t
*count
)
491 if (pset
== PROCESSOR_SET_NULL
)
492 return(KERN_INVALID_ARGUMENT
);
494 if (flavor
== PROCESSOR_SET_BASIC_INFO
) {
495 register processor_set_basic_info_t basic_info
;
497 if (*count
< PROCESSOR_SET_BASIC_INFO_COUNT
)
498 return(KERN_FAILURE
);
500 basic_info
= (processor_set_basic_info_t
) info
;
501 basic_info
->processor_count
= processor_avail_count
;
502 basic_info
->default_policy
= POLICY_TIMESHARE
;
504 *count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
506 return(KERN_SUCCESS
);
508 else if (flavor
== PROCESSOR_SET_TIMESHARE_DEFAULT
) {
509 register policy_timeshare_base_t ts_base
;
511 if (*count
< POLICY_TIMESHARE_BASE_COUNT
)
512 return(KERN_FAILURE
);
514 ts_base
= (policy_timeshare_base_t
) info
;
515 ts_base
->base_priority
= BASEPRI_DEFAULT
;
517 *count
= POLICY_TIMESHARE_BASE_COUNT
;
519 return(KERN_SUCCESS
);
521 else if (flavor
== PROCESSOR_SET_FIFO_DEFAULT
) {
522 register policy_fifo_base_t fifo_base
;
524 if (*count
< POLICY_FIFO_BASE_COUNT
)
525 return(KERN_FAILURE
);
527 fifo_base
= (policy_fifo_base_t
) info
;
528 fifo_base
->base_priority
= BASEPRI_DEFAULT
;
530 *count
= POLICY_FIFO_BASE_COUNT
;
532 return(KERN_SUCCESS
);
534 else if (flavor
== PROCESSOR_SET_RR_DEFAULT
) {
535 register policy_rr_base_t rr_base
;
537 if (*count
< POLICY_RR_BASE_COUNT
)
538 return(KERN_FAILURE
);
540 rr_base
= (policy_rr_base_t
) info
;
541 rr_base
->base_priority
= BASEPRI_DEFAULT
;
542 rr_base
->quantum
= 1;
544 *count
= POLICY_RR_BASE_COUNT
;
546 return(KERN_SUCCESS
);
548 else if (flavor
== PROCESSOR_SET_TIMESHARE_LIMITS
) {
549 register policy_timeshare_limit_t ts_limit
;
551 if (*count
< POLICY_TIMESHARE_LIMIT_COUNT
)
552 return(KERN_FAILURE
);
554 ts_limit
= (policy_timeshare_limit_t
) info
;
555 ts_limit
->max_priority
= MAXPRI_KERNEL
;
557 *count
= POLICY_TIMESHARE_LIMIT_COUNT
;
559 return(KERN_SUCCESS
);
561 else if (flavor
== PROCESSOR_SET_FIFO_LIMITS
) {
562 register policy_fifo_limit_t fifo_limit
;
564 if (*count
< POLICY_FIFO_LIMIT_COUNT
)
565 return(KERN_FAILURE
);
567 fifo_limit
= (policy_fifo_limit_t
) info
;
568 fifo_limit
->max_priority
= MAXPRI_KERNEL
;
570 *count
= POLICY_FIFO_LIMIT_COUNT
;
572 return(KERN_SUCCESS
);
574 else if (flavor
== PROCESSOR_SET_RR_LIMITS
) {
575 register policy_rr_limit_t rr_limit
;
577 if (*count
< POLICY_RR_LIMIT_COUNT
)
578 return(KERN_FAILURE
);
580 rr_limit
= (policy_rr_limit_t
) info
;
581 rr_limit
->max_priority
= MAXPRI_KERNEL
;
583 *count
= POLICY_RR_LIMIT_COUNT
;
585 return(KERN_SUCCESS
);
587 else if (flavor
== PROCESSOR_SET_ENABLED_POLICIES
) {
588 register int *enabled
;
590 if (*count
< (sizeof(*enabled
)/sizeof(int)))
591 return(KERN_FAILURE
);
593 enabled
= (int *) info
;
594 *enabled
= POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
;
596 *count
= sizeof(*enabled
)/sizeof(int);
598 return(KERN_SUCCESS
);
603 return(KERN_INVALID_ARGUMENT
);
607 * processor_set_statistics
609 * Returns scheduling statistics for a processor set.
612 processor_set_statistics(
613 processor_set_t pset
,
615 processor_set_info_t info
,
616 mach_msg_type_number_t
*count
)
618 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
619 return (KERN_INVALID_PROCESSOR_SET
);
621 if (flavor
== PROCESSOR_SET_LOAD_INFO
) {
622 register processor_set_load_info_t load_info
;
624 if (*count
< PROCESSOR_SET_LOAD_INFO_COUNT
)
625 return(KERN_FAILURE
);
627 load_info
= (processor_set_load_info_t
) info
;
629 load_info
->mach_factor
= sched_mach_factor
;
630 load_info
->load_average
= sched_load_average
;
632 load_info
->task_count
= tasks_count
;
633 load_info
->thread_count
= threads_count
;
635 *count
= PROCESSOR_SET_LOAD_INFO_COUNT
;
636 return(KERN_SUCCESS
);
639 return(KERN_INVALID_ARGUMENT
);
643 * processor_set_max_priority:
645 * Specify max priority permitted on processor set. This affects
646 * newly created and assigned threads. Optionally change existing
650 processor_set_max_priority(
651 __unused processor_set_t pset
,
652 __unused
int max_priority
,
653 __unused boolean_t change_threads
)
655 return (KERN_INVALID_ARGUMENT
);
659 * processor_set_policy_enable:
661 * Allow indicated policy on processor set.
665 processor_set_policy_enable(
666 __unused processor_set_t pset
,
669 return (KERN_INVALID_ARGUMENT
);
673 * processor_set_policy_disable:
675 * Forbid indicated policy on processor set. Time sharing cannot
679 processor_set_policy_disable(
680 __unused processor_set_t pset
,
682 __unused boolean_t change_threads
)
684 return (KERN_INVALID_ARGUMENT
);
688 #define THING_THREAD 1
691 * processor_set_things:
693 * Common internals for processor_set_{threads,tasks}
696 processor_set_things(
697 processor_set_t pset
,
698 mach_port_t
**thing_list
,
699 mach_msg_type_number_t
*count
,
702 unsigned int actual
; /* this many things */
703 unsigned int maxthings
;
706 vm_size_t size
, size_needed
;
709 if (pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
710 return (KERN_INVALID_ARGUMENT
);
716 mutex_lock(&tasks_threads_lock
);
718 if (type
== THING_TASK
)
719 maxthings
= tasks_count
;
721 maxthings
= threads_count
;
723 /* do we have the memory we need? */
725 size_needed
= maxthings
* sizeof (mach_port_t
);
726 if (size_needed
<= size
)
729 /* unlock and allocate more memory */
730 mutex_unlock(&tasks_threads_lock
);
735 assert(size_needed
> 0);
740 return (KERN_RESOURCE_SHORTAGE
);
743 /* OK, have memory and the list locked */
749 task_t task
, *task_list
= (task_t
*)addr
;
751 for (task
= (task_t
)queue_first(&tasks
);
752 !queue_end(&tasks
, (queue_entry_t
)task
);
753 task
= (task_t
)queue_next(&task
->tasks
)) {
754 #if defined(SECURE_KERNEL)
755 if (task
!= kernel_task
) {
757 task_reference_internal(task
);
758 task_list
[actual
++] = task
;
759 #if defined(SECURE_KERNEL)
768 thread_t thread
, *thread_list
= (thread_t
*)addr
;
770 for (thread
= (thread_t
)queue_first(&threads
);
771 !queue_end(&threads
, (queue_entry_t
)thread
);
772 thread
= (thread_t
)queue_next(&thread
->threads
)) {
773 thread_reference_internal(thread
);
774 thread_list
[actual
++] = thread
;
782 mutex_unlock(&tasks_threads_lock
);
784 if (actual
< maxthings
)
785 size_needed
= actual
* sizeof (mach_port_t
);
788 /* no things, so return null pointer and deallocate memory */
796 /* if we allocated too much, must copy */
798 if (size_needed
< size
) {
801 newaddr
= kalloc(size_needed
);
806 task_t
*task_list
= (task_t
*)addr
;
808 for (i
= 0; i
< actual
; i
++)
809 task_deallocate(task_list
[i
]);
814 thread_t
*thread_list
= (thread_t
*)addr
;
816 for (i
= 0; i
< actual
; i
++)
817 thread_deallocate(thread_list
[i
]);
824 return (KERN_RESOURCE_SHORTAGE
);
827 bcopy((void *) addr
, (void *) newaddr
, size_needed
);
832 *thing_list
= (mach_port_t
*)addr
;
835 /* do the conversion that Mig should handle */
840 task_t
*task_list
= (task_t
*)addr
;
842 for (i
= 0; i
< actual
; i
++)
843 (*thing_list
)[i
] = convert_task_to_port(task_list
[i
]);
848 thread_t
*thread_list
= (thread_t
*)addr
;
850 for (i
= 0; i
< actual
; i
++)
851 (*thing_list
)[i
] = convert_thread_to_port(thread_list
[i
]);
858 return (KERN_SUCCESS
);
863 * processor_set_tasks:
865 * List all tasks in the processor set.
869 processor_set_t pset
,
870 task_array_t
*task_list
,
871 mach_msg_type_number_t
*count
)
873 return(processor_set_things(pset
, (mach_port_t
**)task_list
, count
, THING_TASK
));
877 * processor_set_threads:
879 * List all threads in the processor set.
881 #if defined(SECURE_KERNEL)
883 processor_set_threads(
884 __unused processor_set_t pset
,
885 __unused thread_array_t
*thread_list
,
886 __unused mach_msg_type_number_t
*count
)
892 processor_set_threads(
893 processor_set_t pset
,
894 thread_array_t
*thread_list
,
895 mach_msg_type_number_t
*count
)
897 return(processor_set_things(pset
, (mach_port_t
**)thread_list
, count
, THING_THREAD
));
902 * processor_set_policy_control
904 * Controls the scheduling attributes governing the processor set.
905 * Allows control of enabled policies, and per-policy base and limit
909 processor_set_policy_control(
910 __unused processor_set_t pset
,
912 __unused processor_set_info_t policy_info
,
913 __unused mach_msg_type_number_t count
,
914 __unused boolean_t change
)
916 return (KERN_INVALID_ARGUMENT
);
919 #undef pset_deallocate
920 void pset_deallocate(processor_set_t pset
);
923 __unused processor_set_t pset
)
928 #undef pset_reference
929 void pset_reference(processor_set_t pset
);
932 __unused processor_set_t pset
)