]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/mk_sp.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / osfmk / kern / mk_sp.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
30 *
31 */
32
91447636 33/* The routines in this module are all obsolete */
1c79356b
A
34
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>
43#include <kern/spl.h>
44#include <kern/task.h>
45#include <kern/thread.h>
1c79356b
A
46#include <mach/policy.h>
47
48#include <kern/syscall_subr.h>
49#include <mach/mach_host_server.h>
50#include <mach/mach_syscalls.h>
51
1c79356b
A
52#include <kern/misc_protos.h>
53#include <kern/spl.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>
1c79356b
A
59#include <mach/thread_act_server.h>
60#include <mach/host_priv_server.h>
61
1c79356b
A
62/*
63 * thread_policy_common:
64 *
0b4e3aa0 65 * Set scheduling policy & priority for thread.
1c79356b 66 */
0b4e3aa0 67static kern_return_t
1c79356b
A
68thread_policy_common(
69 thread_t thread,
70 integer_t policy,
0b4e3aa0 71 integer_t priority)
1c79356b 72{
1c79356b
A
73 spl_t s;
74
75 if ( thread == THREAD_NULL ||
76 invalid_policy(policy) )
77 return(KERN_INVALID_ARGUMENT);
78
2d21ac55
A
79 if (thread->static_param)
80 return (KERN_SUCCESS);
81
1c79356b
A
82 s = splsched();
83 thread_lock(thread);
84
0b4e3aa0
A
85 if ( !(thread->sched_mode & TH_MODE_REALTIME) &&
86 !(thread->safe_mode & TH_MODE_REALTIME) ) {
87 if (!(thread->sched_mode & TH_MODE_FAILSAFE)) {
55e303ae
A
88 integer_t oldmode = (thread->sched_mode & TH_MODE_TIMESHARE);
89
90 if (policy == POLICY_TIMESHARE && !oldmode) {
0b4e3aa0 91 thread->sched_mode |= TH_MODE_TIMESHARE;
55e303ae 92
2d21ac55
A
93 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN)
94 sched_share_incr();
55e303ae 95 }
0b4e3aa0 96 else
55e303ae 97 if (policy != POLICY_TIMESHARE && oldmode) {
0b4e3aa0 98 thread->sched_mode &= ~TH_MODE_TIMESHARE;
55e303ae 99
2d21ac55
A
100 if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN)
101 sched_share_decr();
55e303ae 102 }
0b4e3aa0
A
103 }
104 else {
105 if (policy == POLICY_TIMESHARE)
106 thread->safe_mode |= TH_MODE_TIMESHARE;
107 else
108 thread->safe_mode &= ~TH_MODE_TIMESHARE;
109 }
110
111 if (priority >= thread->max_priority)
112 priority = thread->max_priority - thread->task_priority;
113 else
114 if (priority >= MINPRI_KERNEL)
115 priority -= MINPRI_KERNEL;
116 else
91447636
A
117 if (priority >= MINPRI_RESERVED)
118 priority -= MINPRI_RESERVED;
0b4e3aa0
A
119 else
120 priority -= BASEPRI_DEFAULT;
121
122 priority += thread->task_priority;
123
124 if (priority > thread->max_priority)
125 priority = thread->max_priority;
9bccf70c
A
126 else
127 if (priority < MINPRI)
128 priority = MINPRI;
0b4e3aa0
A
129
130 thread->importance = priority - thread->task_priority;
131
9bccf70c 132 set_priority(thread, priority);
1c79356b
A
133 }
134
135 thread_unlock(thread);
136 splx(s);
137
0b4e3aa0 138 return (KERN_SUCCESS);
1c79356b
A
139}
140
141/*
142 * thread_set_policy
143 *
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.
147 */
148kern_return_t
149thread_set_policy(
91447636 150 thread_t thread,
1c79356b
A
151 processor_set_t pset,
152 policy_t policy,
153 policy_base_t base,
154 mach_msg_type_number_t base_count,
155 policy_limit_t limit,
156 mach_msg_type_number_t limit_count)
157{
0b4e3aa0 158 int max, bas;
1c79356b
A
159 kern_return_t result = KERN_SUCCESS;
160
91447636 161 if ( thread == THREAD_NULL ||
2d21ac55 162 pset == PROCESSOR_SET_NULL || pset != &pset0)
1c79356b
A
163 return (KERN_INVALID_ARGUMENT);
164
91447636 165 thread_mtx_lock(thread);
1c79356b 166
1c79356b
A
167 switch (policy) {
168
169 case POLICY_RR:
170 {
171 policy_rr_base_t rr_base = (policy_rr_base_t) base;
172 policy_rr_limit_t rr_limit = (policy_rr_limit_t) limit;
173
174 if ( base_count != POLICY_RR_BASE_COUNT ||
175 limit_count != POLICY_RR_LIMIT_COUNT ) {
176 result = KERN_INVALID_ARGUMENT;
177 break;
178 }
179
1c79356b
A
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;
184 break;
185 }
186
187 break;
188 }
189
190 case POLICY_FIFO:
191 {
192 policy_fifo_base_t fifo_base = (policy_fifo_base_t) base;
193 policy_fifo_limit_t fifo_limit = (policy_fifo_limit_t) limit;
194
195 if ( base_count != POLICY_FIFO_BASE_COUNT ||
196 limit_count != POLICY_FIFO_LIMIT_COUNT) {
197 result = KERN_INVALID_ARGUMENT;
198 break;
199 }
200
1c79356b
A
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;
205 break;
206 }
207
208 break;
209 }
210
211 case POLICY_TIMESHARE:
212 {
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;
216
217 if ( base_count != POLICY_TIMESHARE_BASE_COUNT ||
218 limit_count != POLICY_TIMESHARE_LIMIT_COUNT ) {
219 result = KERN_INVALID_ARGUMENT;
220 break;
221 }
222
1c79356b
A
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;
227 break;
228 }
229
230 break;
231 }
232
233 default:
234 result = KERN_INVALID_POLICY;
235 }
236
237 if (result != KERN_SUCCESS) {
91447636 238 thread_mtx_unlock(thread);
1c79356b 239
91447636 240 return (result);
1c79356b
A
241 }
242
0b4e3aa0 243 result = thread_policy_common(thread, policy, bas);
1c79356b 244
91447636
A
245 thread_mtx_unlock(thread);
246
247 return (result);
1c79356b
A
248}
249
250
251/*
252 * thread_policy
253 *
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.
257 */
258kern_return_t
259thread_policy(
91447636 260 thread_t thread,
1c79356b
A
261 policy_t policy,
262 policy_base_t base,
263 mach_msg_type_number_t count,
264 boolean_t set_limit)
265{
1c79356b 266 kern_return_t result = KERN_SUCCESS;
2d21ac55
A
267 processor_set_t pset = &pset0;
268 policy_limit_t limit = NULL;
269 int limcount = 0;
1c79356b
A
270 policy_rr_limit_data_t rr_limit;
271 policy_fifo_limit_data_t fifo_limit;
272 policy_timeshare_limit_data_t ts_limit;
273
91447636 274 if (thread == THREAD_NULL)
1c79356b
A
275 return (KERN_INVALID_ARGUMENT);
276
91447636
A
277 thread_mtx_lock(thread);
278
0b4e3aa0
A
279 if ( invalid_policy(policy) ||
280 ((POLICY_TIMESHARE | POLICY_RR | POLICY_FIFO) & policy) == 0 ) {
91447636 281 thread_mtx_unlock(thread);
1c79356b 282
91447636 283 return (KERN_INVALID_POLICY);
1c79356b
A
284 }
285
286 if (set_limit) {
287 /*
288 * Set scheduling limits to base priority.
289 */
290 switch (policy) {
291
292 case POLICY_RR:
293 {
294 policy_rr_base_t rr_base;
295
296 if (count != POLICY_RR_BASE_COUNT) {
297 result = KERN_INVALID_ARGUMENT;
298 break;
299 }
300
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;
305
306 break;
307 }
308
309 case POLICY_FIFO:
310 {
311 policy_fifo_base_t fifo_base;
312
313 if (count != POLICY_FIFO_BASE_COUNT) {
314 result = KERN_INVALID_ARGUMENT;
315 break;
316 }
317
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;
322
323 break;
324 }
325
326 case POLICY_TIMESHARE:
327 {
328 policy_timeshare_base_t ts_base;
329
330 if (count != POLICY_TIMESHARE_BASE_COUNT) {
331 result = KERN_INVALID_ARGUMENT;
332 break;
333 }
334
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;
339
340 break;
341 }
342
343 default:
344 result = KERN_INVALID_POLICY;
345 break;
346 }
347
348 }
349 else {
350 /*
351 * Use current scheduling limits. Ensure that the
352 * new base priority will not exceed current limits.
353 */
354 switch (policy) {
355
356 case POLICY_RR:
357 {
358 policy_rr_base_t rr_base;
359
360 if (count != POLICY_RR_BASE_COUNT) {
361 result = KERN_INVALID_ARGUMENT;
362 break;
363 }
364
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;
369 break;
370 }
371
372 rr_limit.max_priority = thread->max_priority;
373 limit = (policy_limit_t) &rr_limit;
374
375 break;
376 }
377
378 case POLICY_FIFO:
379 {
380 policy_fifo_base_t fifo_base;
381
382 if (count != POLICY_FIFO_BASE_COUNT) {
383 result = KERN_INVALID_ARGUMENT;
384 break;
385 }
386
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;
391 break;
392 }
393
394 fifo_limit.max_priority = thread->max_priority;
395 limit = (policy_limit_t) &fifo_limit;
396
397 break;
398 }
399
400 case POLICY_TIMESHARE:
401 {
402 policy_timeshare_base_t ts_base;
403
404 if (count != POLICY_TIMESHARE_BASE_COUNT) {
405 result = KERN_INVALID_ARGUMENT;
406 break;
407 }
408
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;
413 break;
414 }
415
416 ts_limit.max_priority = thread->max_priority;
417 limit = (policy_limit_t) &ts_limit;
418
419 break;
420 }
421
422 default:
423 result = KERN_INVALID_POLICY;
424 break;
425 }
426
427 }
428
91447636 429 thread_mtx_unlock(thread);
1c79356b
A
430
431 if (result == KERN_SUCCESS)
91447636 432 result = thread_set_policy(thread, pset,
1c79356b
A
433 policy, base, count, limit, limcount);
434
435 return(result);
436}