2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
35 /* The routines in this module are all obsolete */
37 #include <mach/boolean.h>
38 #include <mach/thread_switch.h>
39 #include <ipc/ipc_port.h>
40 #include <ipc/ipc_space.h>
41 #include <kern/ipc_kobject.h>
42 #include <kern/processor.h>
43 #include <kern/sched.h>
44 #include <kern/sched_prim.h>
46 #include <kern/task.h>
47 #include <kern/thread.h>
48 #include <mach/policy.h>
50 #include <kern/syscall_subr.h>
51 #include <mach/mach_host_server.h>
52 #include <mach/mach_syscalls.h>
54 #include <kern/misc_protos.h>
56 #include <kern/sched.h>
57 #include <kern/sched_prim.h>
58 #include <kern/assert.h>
59 #include <kern/thread.h>
60 #include <mach/mach_host_server.h>
61 #include <mach/thread_act_server.h>
62 #include <mach/host_priv_server.h>
65 * thread_policy_common:
67 * Set scheduling policy & priority for thread.
77 if ( thread
== THREAD_NULL
||
78 invalid_policy(policy
) )
79 return(KERN_INVALID_ARGUMENT
);
84 if ( !(thread
->sched_mode
& TH_MODE_REALTIME
) &&
85 !(thread
->safe_mode
& TH_MODE_REALTIME
) ) {
86 if (!(thread
->sched_mode
& TH_MODE_FAILSAFE
)) {
87 integer_t oldmode
= (thread
->sched_mode
& TH_MODE_TIMESHARE
);
89 if (policy
== POLICY_TIMESHARE
&& !oldmode
) {
90 thread
->sched_mode
|= TH_MODE_TIMESHARE
;
92 if (thread
->state
& TH_RUN
)
93 pset_share_incr(thread
->processor_set
);
96 if (policy
!= POLICY_TIMESHARE
&& oldmode
) {
97 thread
->sched_mode
&= ~TH_MODE_TIMESHARE
;
99 if (thread
->state
& TH_RUN
)
100 pset_share_decr(thread
->processor_set
);
104 if (policy
== POLICY_TIMESHARE
)
105 thread
->safe_mode
|= TH_MODE_TIMESHARE
;
107 thread
->safe_mode
&= ~TH_MODE_TIMESHARE
;
110 if (priority
>= thread
->max_priority
)
111 priority
= thread
->max_priority
- thread
->task_priority
;
113 if (priority
>= MINPRI_KERNEL
)
114 priority
-= MINPRI_KERNEL
;
116 if (priority
>= MINPRI_RESERVED
)
117 priority
-= MINPRI_RESERVED
;
119 priority
-= BASEPRI_DEFAULT
;
121 priority
+= thread
->task_priority
;
123 if (priority
> thread
->max_priority
)
124 priority
= thread
->max_priority
;
126 if (priority
< MINPRI
)
129 thread
->importance
= priority
- thread
->task_priority
;
131 set_priority(thread
, priority
);
134 thread_unlock(thread
);
137 return (KERN_SUCCESS
);
143 * Set scheduling policy and parameters, both base and limit, for
144 * the given thread. Policy can be any policy implemented by the
145 * processor set, whether enabled or not.
150 processor_set_t pset
,
153 mach_msg_type_number_t base_count
,
154 policy_limit_t limit
,
155 mach_msg_type_number_t limit_count
)
158 kern_return_t result
= KERN_SUCCESS
;
160 if ( thread
== THREAD_NULL
||
161 pset
== PROCESSOR_SET_NULL
)
162 return (KERN_INVALID_ARGUMENT
);
164 thread_mtx_lock(thread
);
166 if (pset
!= thread
->processor_set
) {
167 thread_mtx_unlock(thread
);
169 return (KERN_FAILURE
);
176 policy_rr_base_t rr_base
= (policy_rr_base_t
) base
;
177 policy_rr_limit_t rr_limit
= (policy_rr_limit_t
) limit
;
179 if ( base_count
!= POLICY_RR_BASE_COUNT
||
180 limit_count
!= POLICY_RR_LIMIT_COUNT
) {
181 result
= KERN_INVALID_ARGUMENT
;
185 bas
= rr_base
->base_priority
;
186 max
= rr_limit
->max_priority
;
187 if (invalid_pri(bas
) || invalid_pri(max
)) {
188 result
= KERN_INVALID_ARGUMENT
;
197 policy_fifo_base_t fifo_base
= (policy_fifo_base_t
) base
;
198 policy_fifo_limit_t fifo_limit
= (policy_fifo_limit_t
) limit
;
200 if ( base_count
!= POLICY_FIFO_BASE_COUNT
||
201 limit_count
!= POLICY_FIFO_LIMIT_COUNT
) {
202 result
= KERN_INVALID_ARGUMENT
;
206 bas
= fifo_base
->base_priority
;
207 max
= fifo_limit
->max_priority
;
208 if (invalid_pri(bas
) || invalid_pri(max
)) {
209 result
= KERN_INVALID_ARGUMENT
;
216 case POLICY_TIMESHARE
:
218 policy_timeshare_base_t ts_base
= (policy_timeshare_base_t
) base
;
219 policy_timeshare_limit_t ts_limit
=
220 (policy_timeshare_limit_t
) limit
;
222 if ( base_count
!= POLICY_TIMESHARE_BASE_COUNT
||
223 limit_count
!= POLICY_TIMESHARE_LIMIT_COUNT
) {
224 result
= KERN_INVALID_ARGUMENT
;
228 bas
= ts_base
->base_priority
;
229 max
= ts_limit
->max_priority
;
230 if (invalid_pri(bas
) || invalid_pri(max
)) {
231 result
= KERN_INVALID_ARGUMENT
;
239 result
= KERN_INVALID_POLICY
;
242 if (result
!= KERN_SUCCESS
) {
243 thread_mtx_unlock(thread
);
248 result
= thread_policy_common(thread
, policy
, bas
);
250 thread_mtx_unlock(thread
);
259 * Set scheduling policy and parameters, both base and limit, for
260 * the given thread. Policy must be a policy which is enabled for the
261 * processor set. Change contained threads if requested.
268 mach_msg_type_number_t count
,
271 kern_return_t result
= KERN_SUCCESS
;
272 processor_set_t pset
;
273 policy_limit_t limit
;
275 policy_rr_limit_data_t rr_limit
;
276 policy_fifo_limit_data_t fifo_limit
;
277 policy_timeshare_limit_data_t ts_limit
;
279 if (thread
== THREAD_NULL
)
280 return (KERN_INVALID_ARGUMENT
);
282 thread_mtx_lock(thread
);
284 pset
= thread
->processor_set
;
285 if (pset
== PROCESSOR_SET_NULL
) {
286 thread_mtx_unlock(thread
);
288 return (KERN_INVALID_ARGUMENT
);
291 if ( invalid_policy(policy
) ||
292 ((POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
) & policy
) == 0 ) {
293 thread_mtx_unlock(thread
);
295 return (KERN_INVALID_POLICY
);
300 * Set scheduling limits to base priority.
306 policy_rr_base_t rr_base
;
308 if (count
!= POLICY_RR_BASE_COUNT
) {
309 result
= KERN_INVALID_ARGUMENT
;
313 limcount
= POLICY_RR_LIMIT_COUNT
;
314 rr_base
= (policy_rr_base_t
) base
;
315 rr_limit
.max_priority
= rr_base
->base_priority
;
316 limit
= (policy_limit_t
) &rr_limit
;
323 policy_fifo_base_t fifo_base
;
325 if (count
!= POLICY_FIFO_BASE_COUNT
) {
326 result
= KERN_INVALID_ARGUMENT
;
330 limcount
= POLICY_FIFO_LIMIT_COUNT
;
331 fifo_base
= (policy_fifo_base_t
) base
;
332 fifo_limit
.max_priority
= fifo_base
->base_priority
;
333 limit
= (policy_limit_t
) &fifo_limit
;
338 case POLICY_TIMESHARE
:
340 policy_timeshare_base_t ts_base
;
342 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
343 result
= KERN_INVALID_ARGUMENT
;
347 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
348 ts_base
= (policy_timeshare_base_t
) base
;
349 ts_limit
.max_priority
= ts_base
->base_priority
;
350 limit
= (policy_limit_t
) &ts_limit
;
356 result
= KERN_INVALID_POLICY
;
363 * Use current scheduling limits. Ensure that the
364 * new base priority will not exceed current limits.
370 policy_rr_base_t rr_base
;
372 if (count
!= POLICY_RR_BASE_COUNT
) {
373 result
= KERN_INVALID_ARGUMENT
;
377 limcount
= POLICY_RR_LIMIT_COUNT
;
378 rr_base
= (policy_rr_base_t
) base
;
379 if (rr_base
->base_priority
> thread
->max_priority
) {
380 result
= KERN_POLICY_LIMIT
;
384 rr_limit
.max_priority
= thread
->max_priority
;
385 limit
= (policy_limit_t
) &rr_limit
;
392 policy_fifo_base_t fifo_base
;
394 if (count
!= POLICY_FIFO_BASE_COUNT
) {
395 result
= KERN_INVALID_ARGUMENT
;
399 limcount
= POLICY_FIFO_LIMIT_COUNT
;
400 fifo_base
= (policy_fifo_base_t
) base
;
401 if (fifo_base
->base_priority
> thread
->max_priority
) {
402 result
= KERN_POLICY_LIMIT
;
406 fifo_limit
.max_priority
= thread
->max_priority
;
407 limit
= (policy_limit_t
) &fifo_limit
;
412 case POLICY_TIMESHARE
:
414 policy_timeshare_base_t ts_base
;
416 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
417 result
= KERN_INVALID_ARGUMENT
;
421 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
422 ts_base
= (policy_timeshare_base_t
) base
;
423 if (ts_base
->base_priority
> thread
->max_priority
) {
424 result
= KERN_POLICY_LIMIT
;
428 ts_limit
.max_priority
= thread
->max_priority
;
429 limit
= (policy_limit_t
) &ts_limit
;
435 result
= KERN_INVALID_POLICY
;
441 thread_mtx_unlock(thread
);
443 if (result
== KERN_SUCCESS
)
444 result
= thread_set_policy(thread
, pset
,
445 policy
, base
, count
, limit
, limcount
);