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
);
85 if ( !(thread
->sched_mode
& TH_MODE_REALTIME
) &&
86 !(thread
->safe_mode
& TH_MODE_REALTIME
) ) {
87 if (!(thread
->sched_mode
& TH_MODE_FAILSAFE
)) {
88 integer_t oldmode
= (thread
->sched_mode
& TH_MODE_TIMESHARE
);
90 if (policy
== POLICY_TIMESHARE
&& !oldmode
) {
91 thread
->sched_mode
|= TH_MODE_TIMESHARE
;
93 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
)
97 if (policy
!= POLICY_TIMESHARE
&& oldmode
) {
98 thread
->sched_mode
&= ~TH_MODE_TIMESHARE
;
100 if ((thread
->state
& (TH_RUN
|TH_IDLE
)) == TH_RUN
)
105 if (policy
== POLICY_TIMESHARE
)
106 thread
->safe_mode
|= TH_MODE_TIMESHARE
;
108 thread
->safe_mode
&= ~TH_MODE_TIMESHARE
;
111 if (priority
>= thread
->max_priority
)
112 priority
= thread
->max_priority
- thread
->task_priority
;
114 if (priority
>= MINPRI_KERNEL
)
115 priority
-= MINPRI_KERNEL
;
117 if (priority
>= MINPRI_RESERVED
)
118 priority
-= MINPRI_RESERVED
;
120 priority
-= BASEPRI_DEFAULT
;
122 priority
+= thread
->task_priority
;
124 if (priority
> thread
->max_priority
)
125 priority
= thread
->max_priority
;
127 if (priority
< MINPRI
)
130 thread
->importance
= priority
- thread
->task_priority
;
132 set_priority(thread
, priority
);
135 thread_unlock(thread
);
138 return (KERN_SUCCESS
);
144 * Set scheduling policy and parameters, both base and limit, for
145 * the given thread. Policy can be any policy implemented by the
146 * processor set, whether enabled or not.
151 processor_set_t pset
,
154 mach_msg_type_number_t base_count
,
155 policy_limit_t limit
,
156 mach_msg_type_number_t limit_count
)
159 kern_return_t result
= KERN_SUCCESS
;
161 if ( thread
== THREAD_NULL
||
162 pset
== PROCESSOR_SET_NULL
|| pset
!= &pset0
)
163 return (KERN_INVALID_ARGUMENT
);
165 thread_mtx_lock(thread
);
171 policy_rr_base_t rr_base
= (policy_rr_base_t
) base
;
172 policy_rr_limit_t rr_limit
= (policy_rr_limit_t
) limit
;
174 if ( base_count
!= POLICY_RR_BASE_COUNT
||
175 limit_count
!= POLICY_RR_LIMIT_COUNT
) {
176 result
= KERN_INVALID_ARGUMENT
;
180 bas
= rr_base
->base_priority
;
181 max
= rr_limit
->max_priority
;
182 if (invalid_pri(bas
) || invalid_pri(max
)) {
183 result
= KERN_INVALID_ARGUMENT
;
192 policy_fifo_base_t fifo_base
= (policy_fifo_base_t
) base
;
193 policy_fifo_limit_t fifo_limit
= (policy_fifo_limit_t
) limit
;
195 if ( base_count
!= POLICY_FIFO_BASE_COUNT
||
196 limit_count
!= POLICY_FIFO_LIMIT_COUNT
) {
197 result
= KERN_INVALID_ARGUMENT
;
201 bas
= fifo_base
->base_priority
;
202 max
= fifo_limit
->max_priority
;
203 if (invalid_pri(bas
) || invalid_pri(max
)) {
204 result
= KERN_INVALID_ARGUMENT
;
211 case POLICY_TIMESHARE
:
213 policy_timeshare_base_t ts_base
= (policy_timeshare_base_t
) base
;
214 policy_timeshare_limit_t ts_limit
=
215 (policy_timeshare_limit_t
) limit
;
217 if ( base_count
!= POLICY_TIMESHARE_BASE_COUNT
||
218 limit_count
!= POLICY_TIMESHARE_LIMIT_COUNT
) {
219 result
= KERN_INVALID_ARGUMENT
;
223 bas
= ts_base
->base_priority
;
224 max
= ts_limit
->max_priority
;
225 if (invalid_pri(bas
) || invalid_pri(max
)) {
226 result
= KERN_INVALID_ARGUMENT
;
234 result
= KERN_INVALID_POLICY
;
237 if (result
!= KERN_SUCCESS
) {
238 thread_mtx_unlock(thread
);
243 result
= thread_policy_common(thread
, policy
, bas
);
245 thread_mtx_unlock(thread
);
254 * Set scheduling policy and parameters, both base and limit, for
255 * the given thread. Policy must be a policy which is enabled for the
256 * processor set. Change contained threads if requested.
263 mach_msg_type_number_t count
,
266 kern_return_t result
= KERN_SUCCESS
;
267 processor_set_t pset
= &pset0
;
268 policy_limit_t limit
= NULL
;
270 policy_rr_limit_data_t rr_limit
;
271 policy_fifo_limit_data_t fifo_limit
;
272 policy_timeshare_limit_data_t ts_limit
;
274 if (thread
== THREAD_NULL
)
275 return (KERN_INVALID_ARGUMENT
);
277 thread_mtx_lock(thread
);
279 if ( invalid_policy(policy
) ||
280 ((POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
) & policy
) == 0 ) {
281 thread_mtx_unlock(thread
);
283 return (KERN_INVALID_POLICY
);
288 * Set scheduling limits to base priority.
294 policy_rr_base_t rr_base
;
296 if (count
!= POLICY_RR_BASE_COUNT
) {
297 result
= KERN_INVALID_ARGUMENT
;
301 limcount
= POLICY_RR_LIMIT_COUNT
;
302 rr_base
= (policy_rr_base_t
) base
;
303 rr_limit
.max_priority
= rr_base
->base_priority
;
304 limit
= (policy_limit_t
) &rr_limit
;
311 policy_fifo_base_t fifo_base
;
313 if (count
!= POLICY_FIFO_BASE_COUNT
) {
314 result
= KERN_INVALID_ARGUMENT
;
318 limcount
= POLICY_FIFO_LIMIT_COUNT
;
319 fifo_base
= (policy_fifo_base_t
) base
;
320 fifo_limit
.max_priority
= fifo_base
->base_priority
;
321 limit
= (policy_limit_t
) &fifo_limit
;
326 case POLICY_TIMESHARE
:
328 policy_timeshare_base_t ts_base
;
330 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
331 result
= KERN_INVALID_ARGUMENT
;
335 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
336 ts_base
= (policy_timeshare_base_t
) base
;
337 ts_limit
.max_priority
= ts_base
->base_priority
;
338 limit
= (policy_limit_t
) &ts_limit
;
344 result
= KERN_INVALID_POLICY
;
351 * Use current scheduling limits. Ensure that the
352 * new base priority will not exceed current limits.
358 policy_rr_base_t rr_base
;
360 if (count
!= POLICY_RR_BASE_COUNT
) {
361 result
= KERN_INVALID_ARGUMENT
;
365 limcount
= POLICY_RR_LIMIT_COUNT
;
366 rr_base
= (policy_rr_base_t
) base
;
367 if (rr_base
->base_priority
> thread
->max_priority
) {
368 result
= KERN_POLICY_LIMIT
;
372 rr_limit
.max_priority
= thread
->max_priority
;
373 limit
= (policy_limit_t
) &rr_limit
;
380 policy_fifo_base_t fifo_base
;
382 if (count
!= POLICY_FIFO_BASE_COUNT
) {
383 result
= KERN_INVALID_ARGUMENT
;
387 limcount
= POLICY_FIFO_LIMIT_COUNT
;
388 fifo_base
= (policy_fifo_base_t
) base
;
389 if (fifo_base
->base_priority
> thread
->max_priority
) {
390 result
= KERN_POLICY_LIMIT
;
394 fifo_limit
.max_priority
= thread
->max_priority
;
395 limit
= (policy_limit_t
) &fifo_limit
;
400 case POLICY_TIMESHARE
:
402 policy_timeshare_base_t ts_base
;
404 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
405 result
= KERN_INVALID_ARGUMENT
;
409 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
410 ts_base
= (policy_timeshare_base_t
) base
;
411 if (ts_base
->base_priority
> thread
->max_priority
) {
412 result
= KERN_POLICY_LIMIT
;
416 ts_limit
.max_priority
= thread
->max_priority
;
417 limit
= (policy_limit_t
) &ts_limit
;
423 result
= KERN_INVALID_POLICY
;
429 thread_mtx_unlock(thread
);
431 if (result
== KERN_SUCCESS
)
432 result
= thread_set_policy(thread
, pset
,
433 policy
, base
, count
, limit
, limcount
);