2 * Copyright (c) 2000-2004 Apple Computer, 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
);
82 if ( !(thread
->sched_mode
& TH_MODE_REALTIME
) &&
83 !(thread
->safe_mode
& TH_MODE_REALTIME
) ) {
84 if (!(thread
->sched_mode
& TH_MODE_FAILSAFE
)) {
85 integer_t oldmode
= (thread
->sched_mode
& TH_MODE_TIMESHARE
);
87 if (policy
== POLICY_TIMESHARE
&& !oldmode
) {
88 thread
->sched_mode
|= TH_MODE_TIMESHARE
;
90 if (thread
->state
& TH_RUN
)
91 pset_share_incr(thread
->processor_set
);
94 if (policy
!= POLICY_TIMESHARE
&& oldmode
) {
95 thread
->sched_mode
&= ~TH_MODE_TIMESHARE
;
97 if (thread
->state
& TH_RUN
)
98 pset_share_decr(thread
->processor_set
);
102 if (policy
== POLICY_TIMESHARE
)
103 thread
->safe_mode
|= TH_MODE_TIMESHARE
;
105 thread
->safe_mode
&= ~TH_MODE_TIMESHARE
;
108 if (priority
>= thread
->max_priority
)
109 priority
= thread
->max_priority
- thread
->task_priority
;
111 if (priority
>= MINPRI_KERNEL
)
112 priority
-= MINPRI_KERNEL
;
114 if (priority
>= MINPRI_RESERVED
)
115 priority
-= MINPRI_RESERVED
;
117 priority
-= BASEPRI_DEFAULT
;
119 priority
+= thread
->task_priority
;
121 if (priority
> thread
->max_priority
)
122 priority
= thread
->max_priority
;
124 if (priority
< MINPRI
)
127 thread
->importance
= priority
- thread
->task_priority
;
129 set_priority(thread
, priority
);
132 thread_unlock(thread
);
135 return (KERN_SUCCESS
);
141 * Set scheduling policy and parameters, both base and limit, for
142 * the given thread. Policy can be any policy implemented by the
143 * processor set, whether enabled or not.
148 processor_set_t pset
,
151 mach_msg_type_number_t base_count
,
152 policy_limit_t limit
,
153 mach_msg_type_number_t limit_count
)
156 kern_return_t result
= KERN_SUCCESS
;
158 if ( thread
== THREAD_NULL
||
159 pset
== PROCESSOR_SET_NULL
)
160 return (KERN_INVALID_ARGUMENT
);
162 thread_mtx_lock(thread
);
164 if (pset
!= thread
->processor_set
) {
165 thread_mtx_unlock(thread
);
167 return (KERN_FAILURE
);
174 policy_rr_base_t rr_base
= (policy_rr_base_t
) base
;
175 policy_rr_limit_t rr_limit
= (policy_rr_limit_t
) limit
;
177 if ( base_count
!= POLICY_RR_BASE_COUNT
||
178 limit_count
!= POLICY_RR_LIMIT_COUNT
) {
179 result
= KERN_INVALID_ARGUMENT
;
183 bas
= rr_base
->base_priority
;
184 max
= rr_limit
->max_priority
;
185 if (invalid_pri(bas
) || invalid_pri(max
)) {
186 result
= KERN_INVALID_ARGUMENT
;
195 policy_fifo_base_t fifo_base
= (policy_fifo_base_t
) base
;
196 policy_fifo_limit_t fifo_limit
= (policy_fifo_limit_t
) limit
;
198 if ( base_count
!= POLICY_FIFO_BASE_COUNT
||
199 limit_count
!= POLICY_FIFO_LIMIT_COUNT
) {
200 result
= KERN_INVALID_ARGUMENT
;
204 bas
= fifo_base
->base_priority
;
205 max
= fifo_limit
->max_priority
;
206 if (invalid_pri(bas
) || invalid_pri(max
)) {
207 result
= KERN_INVALID_ARGUMENT
;
214 case POLICY_TIMESHARE
:
216 policy_timeshare_base_t ts_base
= (policy_timeshare_base_t
) base
;
217 policy_timeshare_limit_t ts_limit
=
218 (policy_timeshare_limit_t
) limit
;
220 if ( base_count
!= POLICY_TIMESHARE_BASE_COUNT
||
221 limit_count
!= POLICY_TIMESHARE_LIMIT_COUNT
) {
222 result
= KERN_INVALID_ARGUMENT
;
226 bas
= ts_base
->base_priority
;
227 max
= ts_limit
->max_priority
;
228 if (invalid_pri(bas
) || invalid_pri(max
)) {
229 result
= KERN_INVALID_ARGUMENT
;
237 result
= KERN_INVALID_POLICY
;
240 if (result
!= KERN_SUCCESS
) {
241 thread_mtx_unlock(thread
);
246 result
= thread_policy_common(thread
, policy
, bas
);
248 thread_mtx_unlock(thread
);
257 * Set scheduling policy and parameters, both base and limit, for
258 * the given thread. Policy must be a policy which is enabled for the
259 * processor set. Change contained threads if requested.
266 mach_msg_type_number_t count
,
269 kern_return_t result
= KERN_SUCCESS
;
270 processor_set_t pset
;
271 policy_limit_t limit
;
273 policy_rr_limit_data_t rr_limit
;
274 policy_fifo_limit_data_t fifo_limit
;
275 policy_timeshare_limit_data_t ts_limit
;
277 if (thread
== THREAD_NULL
)
278 return (KERN_INVALID_ARGUMENT
);
280 thread_mtx_lock(thread
);
282 pset
= thread
->processor_set
;
283 if (pset
== PROCESSOR_SET_NULL
) {
284 thread_mtx_unlock(thread
);
286 return (KERN_INVALID_ARGUMENT
);
289 if ( invalid_policy(policy
) ||
290 ((POLICY_TIMESHARE
| POLICY_RR
| POLICY_FIFO
) & policy
) == 0 ) {
291 thread_mtx_unlock(thread
);
293 return (KERN_INVALID_POLICY
);
298 * Set scheduling limits to base priority.
304 policy_rr_base_t rr_base
;
306 if (count
!= POLICY_RR_BASE_COUNT
) {
307 result
= KERN_INVALID_ARGUMENT
;
311 limcount
= POLICY_RR_LIMIT_COUNT
;
312 rr_base
= (policy_rr_base_t
) base
;
313 rr_limit
.max_priority
= rr_base
->base_priority
;
314 limit
= (policy_limit_t
) &rr_limit
;
321 policy_fifo_base_t fifo_base
;
323 if (count
!= POLICY_FIFO_BASE_COUNT
) {
324 result
= KERN_INVALID_ARGUMENT
;
328 limcount
= POLICY_FIFO_LIMIT_COUNT
;
329 fifo_base
= (policy_fifo_base_t
) base
;
330 fifo_limit
.max_priority
= fifo_base
->base_priority
;
331 limit
= (policy_limit_t
) &fifo_limit
;
336 case POLICY_TIMESHARE
:
338 policy_timeshare_base_t ts_base
;
340 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
341 result
= KERN_INVALID_ARGUMENT
;
345 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
346 ts_base
= (policy_timeshare_base_t
) base
;
347 ts_limit
.max_priority
= ts_base
->base_priority
;
348 limit
= (policy_limit_t
) &ts_limit
;
354 result
= KERN_INVALID_POLICY
;
361 * Use current scheduling limits. Ensure that the
362 * new base priority will not exceed current limits.
368 policy_rr_base_t rr_base
;
370 if (count
!= POLICY_RR_BASE_COUNT
) {
371 result
= KERN_INVALID_ARGUMENT
;
375 limcount
= POLICY_RR_LIMIT_COUNT
;
376 rr_base
= (policy_rr_base_t
) base
;
377 if (rr_base
->base_priority
> thread
->max_priority
) {
378 result
= KERN_POLICY_LIMIT
;
382 rr_limit
.max_priority
= thread
->max_priority
;
383 limit
= (policy_limit_t
) &rr_limit
;
390 policy_fifo_base_t fifo_base
;
392 if (count
!= POLICY_FIFO_BASE_COUNT
) {
393 result
= KERN_INVALID_ARGUMENT
;
397 limcount
= POLICY_FIFO_LIMIT_COUNT
;
398 fifo_base
= (policy_fifo_base_t
) base
;
399 if (fifo_base
->base_priority
> thread
->max_priority
) {
400 result
= KERN_POLICY_LIMIT
;
404 fifo_limit
.max_priority
= thread
->max_priority
;
405 limit
= (policy_limit_t
) &fifo_limit
;
410 case POLICY_TIMESHARE
:
412 policy_timeshare_base_t ts_base
;
414 if (count
!= POLICY_TIMESHARE_BASE_COUNT
) {
415 result
= KERN_INVALID_ARGUMENT
;
419 limcount
= POLICY_TIMESHARE_LIMIT_COUNT
;
420 ts_base
= (policy_timeshare_base_t
) base
;
421 if (ts_base
->base_priority
> thread
->max_priority
) {
422 result
= KERN_POLICY_LIMIT
;
426 ts_limit
.max_priority
= thread
->max_priority
;
427 limit
= (policy_limit_t
) &ts_limit
;
433 result
= KERN_INVALID_POLICY
;
439 thread_mtx_unlock(thread
);
441 if (result
== KERN_SUCCESS
)
442 result
= thread_set_policy(thread
, pset
,
443 policy
, base
, count
, limit
, limcount
);