]>
Commit | Line | Data |
---|---|---|
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 | 67 | static kern_return_t |
1c79356b A |
68 | thread_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 | ||
6d2010ae A |
82 | if ((policy == POLICY_TIMESHARE) |
83 | && !SCHED(supports_timeshare_mode)()) | |
84 | policy = TH_MODE_FIXED; | |
85 | ||
1c79356b A |
86 | s = splsched(); |
87 | thread_lock(thread); | |
88 | ||
6d2010ae A |
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; | |
55e303ae A |
93 | |
94 | if (policy == POLICY_TIMESHARE && !oldmode) { | |
6d2010ae | 95 | thread->sched_mode = TH_MODE_TIMESHARE; |
55e303ae | 96 | |
2d21ac55 A |
97 | if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN) |
98 | sched_share_incr(); | |
55e303ae | 99 | } |
0b4e3aa0 | 100 | else |
55e303ae | 101 | if (policy != POLICY_TIMESHARE && oldmode) { |
6d2010ae | 102 | thread->sched_mode = TH_MODE_FIXED; |
55e303ae | 103 | |
2d21ac55 A |
104 | if ((thread->state & (TH_RUN|TH_IDLE)) == TH_RUN) |
105 | sched_share_decr(); | |
55e303ae | 106 | } |
0b4e3aa0 A |
107 | } |
108 | else { | |
109 | if (policy == POLICY_TIMESHARE) | |
6d2010ae | 110 | thread->saved_mode = TH_MODE_TIMESHARE; |
0b4e3aa0 | 111 | else |
6d2010ae | 112 | thread->saved_mode = TH_MODE_FIXED; |
0b4e3aa0 A |
113 | } |
114 | ||
115 | if (priority >= thread->max_priority) | |
116 | priority = thread->max_priority - thread->task_priority; | |
117 | else | |
118 | if (priority >= MINPRI_KERNEL) | |
119 | priority -= MINPRI_KERNEL; | |
120 | else | |
91447636 A |
121 | if (priority >= MINPRI_RESERVED) |
122 | priority -= MINPRI_RESERVED; | |
0b4e3aa0 A |
123 | else |
124 | priority -= BASEPRI_DEFAULT; | |
125 | ||
126 | priority += thread->task_priority; | |
127 | ||
128 | if (priority > thread->max_priority) | |
129 | priority = thread->max_priority; | |
9bccf70c A |
130 | else |
131 | if (priority < MINPRI) | |
132 | priority = MINPRI; | |
0b4e3aa0 A |
133 | |
134 | thread->importance = priority - thread->task_priority; | |
135 | ||
6d2010ae A |
136 | #if CONFIG_EMBEDDED |
137 | /* No one can have a base priority less than MAXPRI_THROTTLE */ | |
138 | if (priority < MAXPRI_THROTTLE) | |
139 | priority = MAXPRI_THROTTLE; | |
140 | #endif /* CONFIG_EMBEDDED */ | |
141 | ||
9bccf70c | 142 | set_priority(thread, priority); |
1c79356b A |
143 | } |
144 | ||
145 | thread_unlock(thread); | |
146 | splx(s); | |
147 | ||
0b4e3aa0 | 148 | return (KERN_SUCCESS); |
1c79356b A |
149 | } |
150 | ||
151 | /* | |
152 | * thread_set_policy | |
153 | * | |
154 | * Set scheduling policy and parameters, both base and limit, for | |
155 | * the given thread. Policy can be any policy implemented by the | |
156 | * processor set, whether enabled or not. | |
157 | */ | |
158 | kern_return_t | |
159 | thread_set_policy( | |
91447636 | 160 | thread_t thread, |
1c79356b A |
161 | processor_set_t pset, |
162 | policy_t policy, | |
163 | policy_base_t base, | |
164 | mach_msg_type_number_t base_count, | |
165 | policy_limit_t limit, | |
166 | mach_msg_type_number_t limit_count) | |
167 | { | |
0b4e3aa0 | 168 | int max, bas; |
1c79356b A |
169 | kern_return_t result = KERN_SUCCESS; |
170 | ||
91447636 | 171 | if ( thread == THREAD_NULL || |
2d21ac55 | 172 | pset == PROCESSOR_SET_NULL || pset != &pset0) |
1c79356b A |
173 | return (KERN_INVALID_ARGUMENT); |
174 | ||
91447636 | 175 | thread_mtx_lock(thread); |
1c79356b | 176 | |
1c79356b A |
177 | switch (policy) { |
178 | ||
179 | case POLICY_RR: | |
180 | { | |
181 | policy_rr_base_t rr_base = (policy_rr_base_t) base; | |
182 | policy_rr_limit_t rr_limit = (policy_rr_limit_t) limit; | |
183 | ||
184 | if ( base_count != POLICY_RR_BASE_COUNT || | |
185 | limit_count != POLICY_RR_LIMIT_COUNT ) { | |
186 | result = KERN_INVALID_ARGUMENT; | |
187 | break; | |
188 | } | |
189 | ||
1c79356b A |
190 | bas = rr_base->base_priority; |
191 | max = rr_limit->max_priority; | |
192 | if (invalid_pri(bas) || invalid_pri(max)) { | |
193 | result = KERN_INVALID_ARGUMENT; | |
194 | break; | |
195 | } | |
196 | ||
197 | break; | |
198 | } | |
199 | ||
200 | case POLICY_FIFO: | |
201 | { | |
202 | policy_fifo_base_t fifo_base = (policy_fifo_base_t) base; | |
203 | policy_fifo_limit_t fifo_limit = (policy_fifo_limit_t) limit; | |
204 | ||
205 | if ( base_count != POLICY_FIFO_BASE_COUNT || | |
206 | limit_count != POLICY_FIFO_LIMIT_COUNT) { | |
207 | result = KERN_INVALID_ARGUMENT; | |
208 | break; | |
209 | } | |
210 | ||
1c79356b A |
211 | bas = fifo_base->base_priority; |
212 | max = fifo_limit->max_priority; | |
213 | if (invalid_pri(bas) || invalid_pri(max)) { | |
214 | result = KERN_INVALID_ARGUMENT; | |
215 | break; | |
216 | } | |
217 | ||
218 | break; | |
219 | } | |
220 | ||
221 | case POLICY_TIMESHARE: | |
222 | { | |
223 | policy_timeshare_base_t ts_base = (policy_timeshare_base_t) base; | |
224 | policy_timeshare_limit_t ts_limit = | |
225 | (policy_timeshare_limit_t) limit; | |
226 | ||
227 | if ( base_count != POLICY_TIMESHARE_BASE_COUNT || | |
228 | limit_count != POLICY_TIMESHARE_LIMIT_COUNT ) { | |
229 | result = KERN_INVALID_ARGUMENT; | |
230 | break; | |
231 | } | |
232 | ||
1c79356b A |
233 | bas = ts_base->base_priority; |
234 | max = ts_limit->max_priority; | |
235 | if (invalid_pri(bas) || invalid_pri(max)) { | |
236 | result = KERN_INVALID_ARGUMENT; | |
237 | break; | |
238 | } | |
239 | ||
240 | break; | |
241 | } | |
242 | ||
243 | default: | |
244 | result = KERN_INVALID_POLICY; | |
245 | } | |
246 | ||
247 | if (result != KERN_SUCCESS) { | |
91447636 | 248 | thread_mtx_unlock(thread); |
1c79356b | 249 | |
91447636 | 250 | return (result); |
1c79356b A |
251 | } |
252 | ||
0b4e3aa0 | 253 | result = thread_policy_common(thread, policy, bas); |
1c79356b | 254 | |
91447636 A |
255 | thread_mtx_unlock(thread); |
256 | ||
257 | return (result); | |
1c79356b A |
258 | } |
259 | ||
260 | ||
261 | /* | |
262 | * thread_policy | |
263 | * | |
264 | * Set scheduling policy and parameters, both base and limit, for | |
265 | * the given thread. Policy must be a policy which is enabled for the | |
266 | * processor set. Change contained threads if requested. | |
267 | */ | |
268 | kern_return_t | |
269 | thread_policy( | |
91447636 | 270 | thread_t thread, |
1c79356b A |
271 | policy_t policy, |
272 | policy_base_t base, | |
273 | mach_msg_type_number_t count, | |
274 | boolean_t set_limit) | |
275 | { | |
1c79356b | 276 | kern_return_t result = KERN_SUCCESS; |
2d21ac55 A |
277 | processor_set_t pset = &pset0; |
278 | policy_limit_t limit = NULL; | |
279 | int limcount = 0; | |
1c79356b A |
280 | policy_rr_limit_data_t rr_limit; |
281 | policy_fifo_limit_data_t fifo_limit; | |
282 | policy_timeshare_limit_data_t ts_limit; | |
283 | ||
91447636 | 284 | if (thread == THREAD_NULL) |
1c79356b A |
285 | return (KERN_INVALID_ARGUMENT); |
286 | ||
91447636 A |
287 | thread_mtx_lock(thread); |
288 | ||
0b4e3aa0 A |
289 | if ( invalid_policy(policy) || |
290 | ((POLICY_TIMESHARE | POLICY_RR | POLICY_FIFO) & policy) == 0 ) { | |
91447636 | 291 | thread_mtx_unlock(thread); |
1c79356b | 292 | |
91447636 | 293 | return (KERN_INVALID_POLICY); |
1c79356b A |
294 | } |
295 | ||
296 | if (set_limit) { | |
297 | /* | |
298 | * Set scheduling limits to base priority. | |
299 | */ | |
300 | switch (policy) { | |
301 | ||
302 | case POLICY_RR: | |
303 | { | |
304 | policy_rr_base_t rr_base; | |
305 | ||
306 | if (count != POLICY_RR_BASE_COUNT) { | |
307 | result = KERN_INVALID_ARGUMENT; | |
308 | break; | |
309 | } | |
310 | ||
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; | |
315 | ||
316 | break; | |
317 | } | |
318 | ||
319 | case POLICY_FIFO: | |
320 | { | |
321 | policy_fifo_base_t fifo_base; | |
322 | ||
323 | if (count != POLICY_FIFO_BASE_COUNT) { | |
324 | result = KERN_INVALID_ARGUMENT; | |
325 | break; | |
326 | } | |
327 | ||
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; | |
332 | ||
333 | break; | |
334 | } | |
335 | ||
336 | case POLICY_TIMESHARE: | |
337 | { | |
338 | policy_timeshare_base_t ts_base; | |
339 | ||
340 | if (count != POLICY_TIMESHARE_BASE_COUNT) { | |
341 | result = KERN_INVALID_ARGUMENT; | |
342 | break; | |
343 | } | |
344 | ||
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; | |
349 | ||
350 | break; | |
351 | } | |
352 | ||
353 | default: | |
354 | result = KERN_INVALID_POLICY; | |
355 | break; | |
356 | } | |
357 | ||
358 | } | |
359 | else { | |
360 | /* | |
361 | * Use current scheduling limits. Ensure that the | |
362 | * new base priority will not exceed current limits. | |
363 | */ | |
364 | switch (policy) { | |
365 | ||
366 | case POLICY_RR: | |
367 | { | |
368 | policy_rr_base_t rr_base; | |
369 | ||
370 | if (count != POLICY_RR_BASE_COUNT) { | |
371 | result = KERN_INVALID_ARGUMENT; | |
372 | break; | |
373 | } | |
374 | ||
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; | |
379 | break; | |
380 | } | |
381 | ||
382 | rr_limit.max_priority = thread->max_priority; | |
383 | limit = (policy_limit_t) &rr_limit; | |
384 | ||
385 | break; | |
386 | } | |
387 | ||
388 | case POLICY_FIFO: | |
389 | { | |
390 | policy_fifo_base_t fifo_base; | |
391 | ||
392 | if (count != POLICY_FIFO_BASE_COUNT) { | |
393 | result = KERN_INVALID_ARGUMENT; | |
394 | break; | |
395 | } | |
396 | ||
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; | |
401 | break; | |
402 | } | |
403 | ||
404 | fifo_limit.max_priority = thread->max_priority; | |
405 | limit = (policy_limit_t) &fifo_limit; | |
406 | ||
407 | break; | |
408 | } | |
409 | ||
410 | case POLICY_TIMESHARE: | |
411 | { | |
412 | policy_timeshare_base_t ts_base; | |
413 | ||
414 | if (count != POLICY_TIMESHARE_BASE_COUNT) { | |
415 | result = KERN_INVALID_ARGUMENT; | |
416 | break; | |
417 | } | |
418 | ||
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; | |
423 | break; | |
424 | } | |
425 | ||
426 | ts_limit.max_priority = thread->max_priority; | |
427 | limit = (policy_limit_t) &ts_limit; | |
428 | ||
429 | break; | |
430 | } | |
431 | ||
432 | default: | |
433 | result = KERN_INVALID_POLICY; | |
434 | break; | |
435 | } | |
436 | ||
437 | } | |
438 | ||
91447636 | 439 | thread_mtx_unlock(thread); |
1c79356b A |
440 | |
441 | if (result == KERN_SUCCESS) | |
91447636 | 442 | result = thread_set_policy(thread, pset, |
1c79356b A |
443 | policy, base, count, limit, limcount); |
444 | ||
445 | return(result); | |
446 | } |