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@
33 /* The routines in this module are all obsolete */
35 #include <mach/boolean.h>
36 #include <mach/thread_switch.h>
37 #include <ipc/ipc_port.h>
38 #include <ipc/ipc_space.h>
39 #include <kern/ipc_kobject.h>
40 #include <kern/processor.h>
41 #include <kern/sched.h>
42 #include <kern/sched_prim.h>
44 #include <kern/task.h>
45 #include <kern/thread.h>
46 #include <mach/policy.h>
48 #include <kern/syscall_subr.h>
49 #include <mach/mach_host_server.h>
50 #include <mach/mach_syscalls.h>
52 #include <kern/misc_protos.h>
54 #include <kern/sched.h>
55 #include <kern/sched_prim.h>
56 #include <kern/assert.h>
57 #include <kern/thread.h>
58 #include <mach/mach_host_server.h>
59 #include <mach/thread_act_server.h>
60 #include <mach/host_priv_server.h>
63 * thread_policy_common:
65 * Set scheduling policy & priority for thread.
75 if ( thread
== THREAD_NULL
||
76 invalid_policy(policy
) )
77 return(KERN_INVALID_ARGUMENT
);
79 if (thread
->static_param
)
80 return (KERN_SUCCESS
);
82 if ((policy
== POLICY_TIMESHARE
)
83 && !SCHED(supports_timeshare_mode
)())
84 policy
= TH_MODE_FIXED
;
89 if ( (thread
->sched_mode
!= TH_MODE_REALTIME
) &&
90 (thread
->saved_mode
!= TH_MODE_REALTIME
) ) {
91 if (!(thread
->sched_flags
& TH_SFLAG_DEMOTED_MASK
)) {
92 boolean_t oldmode
= thread
->sched_mode
== TH_MODE_TIMESHARE
;
94 if (policy
== POLICY_TIMESHARE
&& !oldmode
) {
95 thread
->sched_mode
= TH_MODE_TIMESHARE
;
97 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
)
101 if (policy
!= POLICY_TIMESHARE
&& oldmode
) {
102 thread
->sched_mode
= TH_MODE_FIXED
;
104 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
)
109 if (policy
== POLICY_TIMESHARE
)
110 thread
->saved_mode
= TH_MODE_TIMESHARE
;
112 thread
->saved_mode
= TH_MODE_FIXED
;
115 if (priority
>= thread
->max_priority
)
116 priority
= thread
->max_priority
- thread
->task_priority
;
118 if (priority
>= MINPRI_KERNEL
)
119 priority
-= MINPRI_KERNEL
;
121 if (priority
>= MINPRI_RESERVED
)
122 priority
-= MINPRI_RESERVED
;
124 priority
-= BASEPRI_DEFAULT
;
126 priority
+= thread
->task_priority
;
128 if (priority
> thread
->max_priority
)
129 priority
= thread
->max_priority
;
131 if (priority
< MINPRI
)
135 if ((thread
->task
->ext_appliedstate
.apptype
== PROC_POLICY_IOS_APPLE_DAEMON
) &&
136 (thread
->appliedstate
.hw_bg
== TASK_POLICY_BACKGROUND_ATTRIBUTE_ALL
)) {
137 thread
->saved_importance
= priority
- thread
->task_priority
;
138 priority
= MAXPRI_THROTTLE
;
140 thread
->importance
= priority
- thread
->task_priority
;
142 /* No one can have a base priority less than MAXPRI_THROTTLE */
143 if (priority
< MAXPRI_THROTTLE
)
144 priority
= MAXPRI_THROTTLE
;
145 #else /* CONFIG_EMBEDDED */
146 thread
->importance
= priority
- thread
->task_priority
;
148 #endif /* CONFIG_EMBEDDED */
150 set_priority(thread
, priority
);
153 thread_unlock(thread
);
156 return (KERN_SUCCESS
);
162 * Set scheduling policy and parameters, both base and limit, for
163 * the given thread. Policy can be any policy implemented by the
164 * processor set, whether enabled or not.
169 processor_set_t pset
,
172 mach_msg_type_number_t base_count
,
173 policy_limit_t limit
,
174 mach_msg_type_number_t limit_count
)
177 kern_return_t result
= KERN_SUCCESS
;
179 if ( thread
== THREAD_NULL
||
180 pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
181 return (KERN_INVALID_ARGUMENT
);
183 thread_mtx_lock(thread
);
189 policy_rr_base_t rr_base
= (policy_rr_base_t
) base
;
190 policy_rr_limit_t rr_limit
= (policy_rr_limit_t
) limit
;
192 if ( base_count
!= POLICY_RR_BASE_COUNT
||
193 limit_count
!= POLICY_RR_LIMIT_COUNT
) {
194 result
= KERN_INVALID_ARGUMENT
;
198 bas
= rr_base
->base_priority
;
199 max
= rr_limit
->max_priority
;
200 if (invalid_pri(bas
) || invalid_pri(max
)) {
201 result
= KERN_INVALID_ARGUMENT
;
210 policy_fifo_base_t fifo_base
= (policy_fifo_base_t
) base
;
211 policy_fifo_limit_t fifo_limit
= (policy_fifo_limit_t
) limit
;
213 if ( base_count
!= POLICY_FIFO_BASE_COUNT
||
214 limit_count
!= POLICY_FIFO_LIMIT_COUNT
) {
215 result
= KERN_INVALID_ARGUMENT
;
219 bas
= fifo_base
->base_priority
;
220 max
= fifo_limit
->max_priority
;
221 if (invalid_pri(bas
) || invalid_pri(max
)) {
222 result
= KERN_INVALID_ARGUMENT
;
229 case POLICY_TIMESHARE
:
231 policy_timeshare_base_t ts_base
= (policy_timeshare_base_t
) base
;
232 policy_timeshare_limit_t ts_limit
=
233 (policy_timeshare_limit_t
) limit
;
235 if ( base_count
!= POLICY_TIMESHARE_BASE_COUNT
||
236 limit_count
!= POLICY_TIMESHARE_LIMIT_COUNT
) {
237 result
= KERN_INVALID_ARGUMENT
;
241 bas
= ts_base
->base_priority
;
242 max
= ts_limit
->max_priority
;
243 if (invalid_pri(bas
) || invalid_pri(max
)) {
244 result
= KERN_INVALID_ARGUMENT
;
252 result
= KERN_INVALID_POLICY
;
255 if (result
!= KERN_SUCCESS
) {
256 thread_mtx_unlock(thread
);
261 result
= thread_policy_common(thread
, policy
, bas
);
263 thread_mtx_unlock(thread
);
272 * Set scheduling policy and parameters, both base and limit, for
273 * the given thread. Policy must be a policy which is enabled for the
274 * processor set. Change contained threads if requested.
281 mach_msg_type_number_t count
,
284 kern_return_t result
= KERN_SUCCESS
;
285 processor_set_t pset
= &pset0
;
286 policy_limit_t limit
= NULL
;
288 policy_rr_limit_data_t rr_limit
;
289 policy_fifo_limit_data_t fifo_limit
;
290 policy_timeshare_limit_data_t ts_limit
;
292 if (thread
== THREAD_NULL
)
293 return (KERN_INVALID_ARGUMENT
);
295 thread_mtx_lock(thread
);
297 if ( invalid_policy(policy
) ||
298 ((POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
) & policy
) == 0 ) {
299 thread_mtx_unlock(thread
);
301 return (KERN_INVALID_POLICY
);
306 * Set scheduling limits to base priority.
312 policy_rr_base_t rr_base
;
314 if (count
!= POLICY_RR_BASE_COUNT
) {
315 result
= KERN_INVALID_ARGUMENT
;
319 limcount
= POLICY_RR_LIMIT_COUNT
;
320 rr_base
= (policy_rr_base_t
) base
;
321 rr_limit
.max_priority
= rr_base
->base_priority
;
322 limit
= (policy_limit_t
) &rr_limit
;
329 policy_fifo_base_t fifo_base
;
331 if (count
!= POLICY_FIFO_BASE_COUNT
) {
332 result
= KERN_INVALID_ARGUMENT
;
336 limcount
= POLICY_FIFO_LIMIT_COUNT
;
337 fifo_base
= (policy_fifo_base_t
) base
;
338 fifo_limit
.max_priority
= fifo_base
->base_priority
;
339 limit
= (policy_limit_t
) &fifo_limit
;
344 case POLICY_TIMESHARE
:
346 policy_timeshare_base_t ts_base
;
348 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
349 result
= KERN_INVALID_ARGUMENT
;
353 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
354 ts_base
= (policy_timeshare_base_t
) base
;
355 ts_limit
.max_priority
= ts_base
->base_priority
;
356 limit
= (policy_limit_t
) &ts_limit
;
362 result
= KERN_INVALID_POLICY
;
369 * Use current scheduling limits. Ensure that the
370 * new base priority will not exceed current limits.
376 policy_rr_base_t rr_base
;
378 if (count
!= POLICY_RR_BASE_COUNT
) {
379 result
= KERN_INVALID_ARGUMENT
;
383 limcount
= POLICY_RR_LIMIT_COUNT
;
384 rr_base
= (policy_rr_base_t
) base
;
385 if (rr_base
->base_priority
> thread
->max_priority
) {
386 result
= KERN_POLICY_LIMIT
;
390 rr_limit
.max_priority
= thread
->max_priority
;
391 limit
= (policy_limit_t
) &rr_limit
;
398 policy_fifo_base_t fifo_base
;
400 if (count
!= POLICY_FIFO_BASE_COUNT
) {
401 result
= KERN_INVALID_ARGUMENT
;
405 limcount
= POLICY_FIFO_LIMIT_COUNT
;
406 fifo_base
= (policy_fifo_base_t
) base
;
407 if (fifo_base
->base_priority
> thread
->max_priority
) {
408 result
= KERN_POLICY_LIMIT
;
412 fifo_limit
.max_priority
= thread
->max_priority
;
413 limit
= (policy_limit_t
) &fifo_limit
;
418 case POLICY_TIMESHARE
:
420 policy_timeshare_base_t ts_base
;
422 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
423 result
= KERN_INVALID_ARGUMENT
;
427 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
428 ts_base
= (policy_timeshare_base_t
) base
;
429 if (ts_base
->base_priority
> thread
->max_priority
) {
430 result
= KERN_POLICY_LIMIT
;
434 ts_limit
.max_priority
= thread
->max_priority
;
435 limit
= (policy_limit_t
) &ts_limit
;
441 result
= KERN_INVALID_POLICY
;
447 thread_mtx_unlock(thread
);
449 if (result
== KERN_SUCCESS
)
450 result
= thread_set_policy(thread
, pset
,
451 policy
, base
, count
, limit
, limcount
);