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
) {
100 if (thread
->max_priority
<= MAXPRI_THROTTLE
)
101 sched_background_incr();
105 if (policy
!= POLICY_TIMESHARE
&& oldmode
) {
106 thread
->sched_mode
= TH_MODE_FIXED
;
108 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
) {
109 if (thread
->max_priority
<= MAXPRI_THROTTLE
)
110 sched_background_decr();
117 if (policy
== POLICY_TIMESHARE
)
118 thread
->saved_mode
= TH_MODE_TIMESHARE
;
120 thread
->saved_mode
= TH_MODE_FIXED
;
123 if (priority
>= thread
->max_priority
)
124 priority
= thread
->max_priority
- thread
->task_priority
;
126 if (priority
>= MINPRI_KERNEL
)
127 priority
-= MINPRI_KERNEL
;
129 if (priority
>= MINPRI_RESERVED
)
130 priority
-= MINPRI_RESERVED
;
132 priority
-= BASEPRI_DEFAULT
;
134 priority
+= thread
->task_priority
;
136 if (priority
> thread
->max_priority
)
137 priority
= thread
->max_priority
;
139 if (priority
< MINPRI
)
142 thread
->importance
= priority
- thread
->task_priority
;
145 set_priority(thread
, priority
);
148 thread_unlock(thread
);
151 return (KERN_SUCCESS
);
157 * Set scheduling policy and parameters, both base and limit, for
158 * the given thread. Policy can be any policy implemented by the
159 * processor set, whether enabled or not.
164 processor_set_t pset
,
167 mach_msg_type_number_t base_count
,
168 policy_limit_t limit
,
169 mach_msg_type_number_t limit_count
)
172 kern_return_t result
= KERN_SUCCESS
;
174 if ( thread
== THREAD_NULL
||
175 pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
176 return (KERN_INVALID_ARGUMENT
);
178 thread_mtx_lock(thread
);
184 policy_rr_base_t rr_base
= (policy_rr_base_t
) base
;
185 policy_rr_limit_t rr_limit
= (policy_rr_limit_t
) limit
;
187 if ( base_count
!= POLICY_RR_BASE_COUNT
||
188 limit_count
!= POLICY_RR_LIMIT_COUNT
) {
189 result
= KERN_INVALID_ARGUMENT
;
193 bas
= rr_base
->base_priority
;
194 max
= rr_limit
->max_priority
;
195 if (invalid_pri(bas
) || invalid_pri(max
)) {
196 result
= KERN_INVALID_ARGUMENT
;
205 policy_fifo_base_t fifo_base
= (policy_fifo_base_t
) base
;
206 policy_fifo_limit_t fifo_limit
= (policy_fifo_limit_t
) limit
;
208 if ( base_count
!= POLICY_FIFO_BASE_COUNT
||
209 limit_count
!= POLICY_FIFO_LIMIT_COUNT
) {
210 result
= KERN_INVALID_ARGUMENT
;
214 bas
= fifo_base
->base_priority
;
215 max
= fifo_limit
->max_priority
;
216 if (invalid_pri(bas
) || invalid_pri(max
)) {
217 result
= KERN_INVALID_ARGUMENT
;
224 case POLICY_TIMESHARE
:
226 policy_timeshare_base_t ts_base
= (policy_timeshare_base_t
) base
;
227 policy_timeshare_limit_t ts_limit
=
228 (policy_timeshare_limit_t
) limit
;
230 if ( base_count
!= POLICY_TIMESHARE_BASE_COUNT
||
231 limit_count
!= POLICY_TIMESHARE_LIMIT_COUNT
) {
232 result
= KERN_INVALID_ARGUMENT
;
236 bas
= ts_base
->base_priority
;
237 max
= ts_limit
->max_priority
;
238 if (invalid_pri(bas
) || invalid_pri(max
)) {
239 result
= KERN_INVALID_ARGUMENT
;
247 result
= KERN_INVALID_POLICY
;
250 if (result
!= KERN_SUCCESS
) {
251 thread_mtx_unlock(thread
);
256 result
= thread_policy_common(thread
, policy
, bas
);
258 thread_mtx_unlock(thread
);
267 * Set scheduling policy and parameters, both base and limit, for
268 * the given thread. Policy must be a policy which is enabled for the
269 * processor set. Change contained threads if requested.
276 mach_msg_type_number_t count
,
279 kern_return_t result
= KERN_SUCCESS
;
280 processor_set_t pset
= &pset0
;
281 policy_limit_t limit
= NULL
;
283 policy_rr_limit_data_t rr_limit
;
284 policy_fifo_limit_data_t fifo_limit
;
285 policy_timeshare_limit_data_t ts_limit
;
287 if (thread
== THREAD_NULL
)
288 return (KERN_INVALID_ARGUMENT
);
290 thread_mtx_lock(thread
);
292 if ( invalid_policy(policy
) ||
293 ((POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
) & policy
) == 0 ) {
294 thread_mtx_unlock(thread
);
296 return (KERN_INVALID_POLICY
);
301 * Set scheduling limits to base priority.
307 policy_rr_base_t rr_base
;
309 if (count
!= POLICY_RR_BASE_COUNT
) {
310 result
= KERN_INVALID_ARGUMENT
;
314 limcount
= POLICY_RR_LIMIT_COUNT
;
315 rr_base
= (policy_rr_base_t
) base
;
316 rr_limit
.max_priority
= rr_base
->base_priority
;
317 limit
= (policy_limit_t
) &rr_limit
;
324 policy_fifo_base_t fifo_base
;
326 if (count
!= POLICY_FIFO_BASE_COUNT
) {
327 result
= KERN_INVALID_ARGUMENT
;
331 limcount
= POLICY_FIFO_LIMIT_COUNT
;
332 fifo_base
= (policy_fifo_base_t
) base
;
333 fifo_limit
.max_priority
= fifo_base
->base_priority
;
334 limit
= (policy_limit_t
) &fifo_limit
;
339 case POLICY_TIMESHARE
:
341 policy_timeshare_base_t ts_base
;
343 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
344 result
= KERN_INVALID_ARGUMENT
;
348 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
349 ts_base
= (policy_timeshare_base_t
) base
;
350 ts_limit
.max_priority
= ts_base
->base_priority
;
351 limit
= (policy_limit_t
) &ts_limit
;
357 result
= KERN_INVALID_POLICY
;
364 * Use current scheduling limits. Ensure that the
365 * new base priority will not exceed current limits.
371 policy_rr_base_t rr_base
;
373 if (count
!= POLICY_RR_BASE_COUNT
) {
374 result
= KERN_INVALID_ARGUMENT
;
378 limcount
= POLICY_RR_LIMIT_COUNT
;
379 rr_base
= (policy_rr_base_t
) base
;
380 if (rr_base
->base_priority
> thread
->max_priority
) {
381 result
= KERN_POLICY_LIMIT
;
385 rr_limit
.max_priority
= thread
->max_priority
;
386 limit
= (policy_limit_t
) &rr_limit
;
393 policy_fifo_base_t fifo_base
;
395 if (count
!= POLICY_FIFO_BASE_COUNT
) {
396 result
= KERN_INVALID_ARGUMENT
;
400 limcount
= POLICY_FIFO_LIMIT_COUNT
;
401 fifo_base
= (policy_fifo_base_t
) base
;
402 if (fifo_base
->base_priority
> thread
->max_priority
) {
403 result
= KERN_POLICY_LIMIT
;
407 fifo_limit
.max_priority
= thread
->max_priority
;
408 limit
= (policy_limit_t
) &fifo_limit
;
413 case POLICY_TIMESHARE
:
415 policy_timeshare_base_t ts_base
;
417 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
418 result
= KERN_INVALID_ARGUMENT
;
422 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
423 ts_base
= (policy_timeshare_base_t
) base
;
424 if (ts_base
->base_priority
> thread
->max_priority
) {
425 result
= KERN_POLICY_LIMIT
;
429 ts_limit
.max_priority
= thread
->max_priority
;
430 limit
= (policy_limit_t
) &ts_limit
;
436 result
= KERN_INVALID_POLICY
;
442 thread_mtx_unlock(thread
);
444 if (result
== KERN_SUCCESS
)
445 result
= thread_set_policy(thread
, pset
,
446 policy
, base
, count
, limit
, limcount
);