]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
d9a64523 | 2 | * Copyright (c) 2000-2017 Apple Inc. All rights reserved. |
1c79356b | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
d9a64523 | 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. | |
d9a64523 | 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. | |
d9a64523 | 17 | * |
2d21ac55 A |
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. | |
d9a64523 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * @OSF_COPYRIGHT@ | |
30 | */ | |
0a7de745 | 31 | /* |
1c79356b A |
32 | * Mach Operating System |
33 | * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University | |
34 | * All Rights Reserved. | |
0a7de745 | 35 | * |
1c79356b A |
36 | * Permission to use, copy, modify and distribute this software and its |
37 | * documentation is hereby granted, provided that both the copyright | |
38 | * notice and this permission notice appear in all copies of the | |
39 | * software, derivative works or modified versions, and any portions | |
40 | * thereof, and that both notices appear in supporting documentation. | |
0a7de745 | 41 | * |
1c79356b A |
42 | * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" |
43 | * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR | |
44 | * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. | |
0a7de745 | 45 | * |
1c79356b | 46 | * Carnegie Mellon requests users of this software to return to |
0a7de745 | 47 | * |
1c79356b A |
48 | * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU |
49 | * School of Computer Science | |
50 | * Carnegie Mellon University | |
51 | * Pittsburgh PA 15213-3890 | |
0a7de745 | 52 | * |
1c79356b A |
53 | * any improvements or extensions that they make and grant Carnegie Mellon |
54 | * the rights to redistribute these changes. | |
55 | */ | |
1c79356b | 56 | |
1c79356b A |
57 | #include <mach/boolean.h> |
58 | #include <mach/thread_switch.h> | |
59 | #include <ipc/ipc_port.h> | |
60 | #include <ipc/ipc_space.h> | |
c3c9b80d | 61 | #include <kern/counter.h> |
1c79356b A |
62 | #include <kern/ipc_kobject.h> |
63 | #include <kern/processor.h> | |
64 | #include <kern/sched.h> | |
65 | #include <kern/sched_prim.h> | |
66 | #include <kern/spl.h> | |
67 | #include <kern/task.h> | |
68 | #include <kern/thread.h> | |
39037602 A |
69 | #include <kern/policy_internal.h> |
70 | ||
1c79356b A |
71 | #include <mach/policy.h> |
72 | ||
73 | #include <kern/syscall_subr.h> | |
74 | #include <mach/mach_host_server.h> | |
75 | #include <mach/mach_syscalls.h> | |
fe8ab488 | 76 | #include <sys/kdebug.h> |
813fb2f6 | 77 | #include <kern/ast.h> |
b0d623f7 | 78 | |
d9a64523 A |
79 | static void thread_depress_abstime(uint64_t interval); |
80 | static void thread_depress_ms(mach_msg_timeout_t interval); | |
b0d623f7 A |
81 | |
82 | /* Called from commpage to take a delayed preemption when exiting | |
83 | * the "Preemption Free Zone" (PFZ). | |
84 | */ | |
85 | kern_return_t | |
86 | pfz_exit( | |
0a7de745 | 87 | __unused struct pfz_exit_args *args) |
b0d623f7 A |
88 | { |
89 | /* For now, nothing special to do. We'll pick up the ASTs on kernel exit. */ | |
90 | ||
0a7de745 | 91 | return KERN_SUCCESS; |
b0d623f7 A |
92 | } |
93 | ||
94 | ||
1c79356b A |
95 | /* |
96 | * swtch and swtch_pri both attempt to context switch (logic in | |
97 | * thread_block no-ops the context switch if nothing would happen). | |
98 | * A boolean is returned that indicates whether there is anything | |
3e170ce0 | 99 | * else runnable. That's no excuse to spin, though. |
1c79356b A |
100 | */ |
101 | ||
91447636 | 102 | static void |
0b4e3aa0 | 103 | swtch_continue(void) |
1c79356b | 104 | { |
0a7de745 A |
105 | processor_t myprocessor; |
106 | boolean_t result; | |
1c79356b | 107 | |
5ba3f43e | 108 | disable_preemption(); |
1c79356b | 109 | myprocessor = current_processor(); |
5ba3f43e | 110 | result = SCHED(thread_should_yield)(myprocessor, current_thread()); |
91447636 | 111 | enable_preemption(); |
1c79356b | 112 | |
e8c3f781 A |
113 | ml_delay_on_yield(); |
114 | ||
0b4e3aa0 A |
115 | thread_syscall_return(result); |
116 | /*NOTREACHED*/ | |
117 | } | |
1c79356b A |
118 | |
119 | boolean_t | |
91447636 A |
120 | swtch( |
121 | __unused struct swtch_args *args) | |
1c79356b | 122 | { |
0a7de745 | 123 | processor_t myprocessor; |
1c79356b | 124 | |
91447636 | 125 | disable_preemption(); |
1c79356b | 126 | myprocessor = current_processor(); |
5ba3f43e | 127 | if (!SCHED(thread_should_yield)(myprocessor, current_thread())) { |
1c79356b A |
128 | mp_enable_preemption(); |
129 | ||
0a7de745 | 130 | return FALSE; |
1c79356b | 131 | } |
91447636 | 132 | enable_preemption(); |
1c79356b | 133 | |
d9a64523 | 134 | thread_yield_with_continuation((thread_continue_t)swtch_continue, NULL); |
1c79356b A |
135 | } |
136 | ||
91447636 | 137 | static void |
0b4e3aa0 A |
138 | swtch_pri_continue(void) |
139 | { | |
0a7de745 A |
140 | processor_t myprocessor; |
141 | boolean_t result; | |
0b4e3aa0 | 142 | |
d9a64523 | 143 | thread_depress_abort(current_thread()); |
0b4e3aa0 | 144 | |
5ba3f43e | 145 | disable_preemption(); |
0b4e3aa0 | 146 | myprocessor = current_processor(); |
5ba3f43e | 147 | result = SCHED(thread_should_yield)(myprocessor, current_thread()); |
0b4e3aa0 A |
148 | mp_enable_preemption(); |
149 | ||
e8c3f781 A |
150 | ml_delay_on_yield(); |
151 | ||
0b4e3aa0 A |
152 | thread_syscall_return(result); |
153 | /*NOTREACHED*/ | |
154 | } | |
155 | ||
1c79356b A |
156 | boolean_t |
157 | swtch_pri( | |
0a7de745 | 158 | __unused struct swtch_pri_args *args) |
1c79356b | 159 | { |
0a7de745 | 160 | processor_t myprocessor; |
1c79356b | 161 | |
91447636 | 162 | disable_preemption(); |
1c79356b | 163 | myprocessor = current_processor(); |
5ba3f43e | 164 | if (!SCHED(thread_should_yield)(myprocessor, current_thread())) { |
0b4e3aa0 | 165 | mp_enable_preemption(); |
1c79356b | 166 | |
0a7de745 | 167 | return FALSE; |
1c79356b | 168 | } |
91447636 | 169 | enable_preemption(); |
0b4e3aa0 | 170 | |
6d2010ae | 171 | thread_depress_abstime(thread_depress_time); |
1c79356b | 172 | |
d9a64523 | 173 | thread_yield_with_continuation((thread_continue_t)swtch_pri_continue, NULL); |
39236c6e A |
174 | } |
175 | ||
176 | static void | |
d9a64523 | 177 | thread_switch_continue(void *parameter, __unused int ret) |
39236c6e | 178 | { |
d9a64523 A |
179 | thread_t self = current_thread(); |
180 | int option = (int)(intptr_t)parameter; | |
91447636 | 181 | |
0a7de745 | 182 | if (option == SWITCH_OPTION_DEPRESS || option == SWITCH_OPTION_OSLOCK_DEPRESS) { |
d9a64523 | 183 | thread_depress_abort(self); |
0a7de745 | 184 | } |
39236c6e | 185 | |
e8c3f781 A |
186 | ml_delay_on_yield(); |
187 | ||
91447636 A |
188 | thread_syscall_return(KERN_SUCCESS); |
189 | /*NOTREACHED*/ | |
190 | } | |
191 | ||
1c79356b A |
192 | /* |
193 | * thread_switch: | |
194 | * | |
195 | * Context switch. User may supply thread hint. | |
196 | */ | |
197 | kern_return_t | |
198 | thread_switch( | |
91447636 | 199 | struct thread_switch_args *args) |
1c79356b | 200 | { |
0a7de745 A |
201 | thread_t thread = THREAD_NULL; |
202 | thread_t self = current_thread(); | |
203 | mach_port_name_t thread_name = args->thread_name; | |
cb323159 | 204 | int option = args->option; |
0a7de745 | 205 | mach_msg_timeout_t option_time = args->option_time; |
cb323159 A |
206 | uint32_t scale_factor = NSEC_PER_MSEC; |
207 | boolean_t depress_option = FALSE; | |
208 | boolean_t wait_option = FALSE; | |
0a7de745 | 209 | wait_interrupt_t interruptible = THREAD_ABORTSAFE; |
cb323159 | 210 | port_to_thread_options_t ptt_options = PORT_TO_THREAD_NOT_CURRENT_THREAD; |
0a7de745 A |
211 | |
212 | /* | |
d9a64523 | 213 | * Validate and process option. |
cb323159 A |
214 | * |
215 | * OSLock boosting only applies to other threads | |
216 | * in your same task (even if you have a port for | |
217 | * a thread in another task) | |
d9a64523 A |
218 | */ |
219 | switch (option) { | |
1c79356b | 220 | case SWITCH_OPTION_NONE: |
39236c6e | 221 | break; |
1c79356b | 222 | case SWITCH_OPTION_WAIT: |
39236c6e | 223 | wait_option = TRUE; |
39236c6e A |
224 | break; |
225 | case SWITCH_OPTION_DEPRESS: | |
226 | depress_option = TRUE; | |
39236c6e A |
227 | break; |
228 | case SWITCH_OPTION_DISPATCH_CONTENTION: | |
229 | scale_factor = NSEC_PER_USEC; | |
230 | wait_option = TRUE; | |
d9a64523 | 231 | interruptible |= THREAD_WAIT_NOREPORT; |
39236c6e A |
232 | break; |
233 | case SWITCH_OPTION_OSLOCK_DEPRESS: | |
234 | depress_option = TRUE; | |
d9a64523 | 235 | interruptible |= THREAD_WAIT_NOREPORT; |
cb323159 | 236 | ptt_options |= PORT_TO_THREAD_IN_CURRENT_TASK; |
39236c6e A |
237 | break; |
238 | case SWITCH_OPTION_OSLOCK_WAIT: | |
239 | wait_option = TRUE; | |
d9a64523 | 240 | interruptible |= THREAD_WAIT_NOREPORT; |
cb323159 | 241 | ptt_options |= PORT_TO_THREAD_IN_CURRENT_TASK; |
39236c6e | 242 | break; |
1c79356b | 243 | default: |
0a7de745 A |
244 | return KERN_INVALID_ARGUMENT; |
245 | } | |
1c79356b | 246 | |
91447636 A |
247 | /* |
248 | * Translate the port name if supplied. | |
249 | */ | |
3e170ce0 | 250 | if (thread_name != MACH_PORT_NULL) { |
cb323159 | 251 | thread = port_name_to_thread(thread_name, ptt_options); |
1c79356b | 252 | } |
39236c6e A |
253 | |
254 | if (option == SWITCH_OPTION_OSLOCK_DEPRESS || option == SWITCH_OPTION_OSLOCK_WAIT) { | |
255 | if (thread != THREAD_NULL) { | |
cb323159 A |
256 | /* |
257 | * Attempt to kick the lock owner up to our same IO throttling tier. | |
258 | * If the thread is currently blocked in throttle_lowpri_io(), | |
259 | * it will immediately break out. | |
260 | * | |
261 | * TODO: SFI break out? | |
262 | */ | |
263 | int new_policy = proc_get_effective_thread_policy(self, TASK_POLICY_IO); | |
264 | ||
265 | set_thread_iotier_override(thread, new_policy); | |
39236c6e A |
266 | } |
267 | } | |
268 | ||
91447636 A |
269 | /* |
270 | * Try to handoff if supplied. | |
271 | */ | |
272 | if (thread != THREAD_NULL) { | |
3e170ce0 | 273 | spl_t s = splsched(); |
91447636 | 274 | |
3e170ce0 A |
275 | /* This may return a different thread if the target is pushing on something */ |
276 | thread_t pulled_thread = thread_run_queue_remove_for_handoff(thread); | |
91447636 | 277 | |
0a7de745 A |
278 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SWITCH) | DBG_FUNC_NONE, |
279 | thread_tid(thread), thread->state, | |
280 | pulled_thread ? TRUE : FALSE, 0, 0); | |
3e170ce0 A |
281 | |
282 | if (pulled_thread != THREAD_NULL) { | |
283 | /* We can't be dropping the last ref here */ | |
284 | thread_deallocate_safe(thread); | |
91447636 | 285 | |
0a7de745 | 286 | if (wait_option) { |
d9a64523 | 287 | assert_wait_timeout((event_t)assert_wait_timeout, interruptible, |
0a7de745 A |
288 | option_time, scale_factor); |
289 | } else if (depress_option) { | |
91447636 | 290 | thread_depress_ms(option_time); |
0a7de745 | 291 | } |
91447636 | 292 | |
d9a64523 A |
293 | thread_run(self, thread_switch_continue, (void *)(intptr_t)option, pulled_thread); |
294 | __builtin_unreachable(); | |
91447636 A |
295 | } |
296 | ||
91447636 A |
297 | splx(s); |
298 | ||
299 | thread_deallocate(thread); | |
300 | } | |
3e170ce0 | 301 | |
e8c3f781 | 302 | if (wait_option) { |
d9a64523 | 303 | assert_wait_timeout((event_t)assert_wait_timeout, interruptible, option_time, scale_factor); |
e8c3f781 A |
304 | } else { |
305 | disable_preemption(); | |
306 | bool should_yield = SCHED(thread_should_yield)(current_processor(), current_thread()); | |
307 | enable_preemption(); | |
308 | ||
309 | if (should_yield == false) { | |
310 | /* Early-return if yielding to the scheduler will not be beneficial */ | |
311 | return KERN_SUCCESS; | |
312 | } | |
313 | ||
314 | if (depress_option) { | |
315 | thread_depress_ms(option_time); | |
316 | } | |
317 | } | |
3e170ce0 | 318 | |
d9a64523 A |
319 | thread_yield_with_continuation(thread_switch_continue, (void *)(intptr_t)option); |
320 | __builtin_unreachable(); | |
321 | } | |
39236c6e | 322 | |
d9a64523 A |
323 | void |
324 | thread_yield_with_continuation( | |
0a7de745 A |
325 | thread_continue_t continuation, |
326 | void *parameter) | |
d9a64523 A |
327 | { |
328 | assert(continuation); | |
329 | thread_block_reason(continuation, parameter, AST_YIELD); | |
330 | __builtin_unreachable(); | |
91447636 | 331 | } |
1c79356b | 332 | |
39037602 A |
333 | /* This function is called after an assert_wait(), therefore it must not |
334 | * cause another wait until after the thread_run() or thread_block() | |
335 | * | |
f427ee49 A |
336 | * Following are the calling convention for thread ref deallocation. |
337 | * | |
338 | * 1) If no continuation is provided, then thread ref is consumed. | |
339 | * (thread_handoff_deallocate convention). | |
340 | * | |
341 | * 2) If continuation is provided with option THREAD_HANDOFF_SETRUN_NEEDED | |
342 | * then thread ref is always consumed. | |
d9a64523 | 343 | * |
f427ee49 A |
344 | * 3) If continuation is provided with option THREAD_HANDOFF_NONE then thread |
345 | * ref is not consumed and it is upto the continuation to deallocate | |
346 | * the thread reference. | |
39037602 | 347 | */ |
d9a64523 A |
348 | static wait_result_t |
349 | thread_handoff_internal(thread_t thread, thread_continue_t continuation, | |
f427ee49 | 350 | void *parameter, thread_handoff_option_t option) |
39037602 | 351 | { |
39037602 A |
352 | thread_t self = current_thread(); |
353 | ||
354 | /* | |
355 | * Try to handoff if supplied. | |
356 | */ | |
357 | if (thread != THREAD_NULL) { | |
358 | spl_t s = splsched(); | |
359 | ||
f427ee49 | 360 | thread_t pulled_thread = thread_prepare_for_handoff(thread, option); |
39037602 | 361 | |
0a7de745 A |
362 | KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED, MACH_SCHED_THREAD_SWITCH) | DBG_FUNC_NONE, |
363 | thread_tid(thread), thread->state, | |
364 | pulled_thread ? TRUE : FALSE, 0, 0); | |
39037602 | 365 | |
f427ee49 A |
366 | /* Deallocate thread ref if needed */ |
367 | if (continuation == NULL || (option & THREAD_HANDOFF_SETRUN_NEEDED)) { | |
368 | /* Use the safe version of thread deallocate */ | |
369 | thread_deallocate_safe(thread); | |
370 | } | |
39037602 | 371 | |
f427ee49 | 372 | if (pulled_thread != THREAD_NULL) { |
d9a64523 | 373 | int result = thread_run(self, continuation, parameter, pulled_thread); |
39037602 A |
374 | |
375 | splx(s); | |
376 | return result; | |
377 | } | |
378 | ||
379 | splx(s); | |
39037602 A |
380 | } |
381 | ||
d9a64523 | 382 | int result = thread_block_parameter(continuation, parameter); |
39037602 A |
383 | return result; |
384 | } | |
385 | ||
d9a64523 A |
386 | void |
387 | thread_handoff_parameter(thread_t thread, thread_continue_t continuation, | |
f427ee49 | 388 | void *parameter, thread_handoff_option_t option) |
d9a64523 | 389 | { |
f427ee49 | 390 | thread_handoff_internal(thread, continuation, parameter, option); |
d9a64523 A |
391 | panic("NULL continuation passed to %s", __func__); |
392 | __builtin_unreachable(); | |
393 | } | |
394 | ||
395 | wait_result_t | |
f427ee49 | 396 | thread_handoff_deallocate(thread_t thread, thread_handoff_option_t option) |
d9a64523 | 397 | { |
f427ee49 | 398 | return thread_handoff_internal(thread, NULL, NULL, option); |
d9a64523 A |
399 | } |
400 | ||
401 | /* | |
402 | * Thread depression | |
403 | * | |
404 | * This mechanism drops a thread to priority 0 in order for it to yield to | |
405 | * all other runnnable threads on the system. It can be canceled or timed out, | |
406 | * whereupon the thread goes back to where it was. | |
407 | * | |
408 | * Note that TH_SFLAG_DEPRESS and TH_SFLAG_POLLDEPRESS are never set at the | |
409 | * same time. DEPRESS always defers to POLLDEPRESS. | |
410 | * | |
411 | * DEPRESS only lasts across a single thread_block call, and never returns | |
412 | * to userspace. | |
413 | * POLLDEPRESS can be active anywhere up until thread termination. | |
414 | */ | |
415 | ||
91447636 A |
416 | /* |
417 | * Depress thread's priority to lowest possible for the specified interval, | |
d9a64523 A |
418 | * with an interval of zero resulting in no timeout being scheduled. |
419 | * | |
420 | * Must block with AST_YIELD afterwards to take effect | |
91447636 A |
421 | */ |
422 | void | |
d9a64523 | 423 | thread_depress_abstime(uint64_t interval) |
91447636 | 424 | { |
d9a64523 A |
425 | thread_t self = current_thread(); |
426 | ||
427 | spl_t s = splsched(); | |
428 | thread_lock(self); | |
429 | ||
430 | assert((self->sched_flags & TH_SFLAG_DEPRESS) == 0); | |
431 | ||
432 | if ((self->sched_flags & TH_SFLAG_POLLDEPRESS) == 0) { | |
6d2010ae | 433 | self->sched_flags |= TH_SFLAG_DEPRESS; |
d9a64523 | 434 | thread_recompute_sched_pri(self, SETPRI_LAZY); |
91447636 A |
435 | |
436 | if (interval != 0) { | |
d9a64523 A |
437 | uint64_t deadline; |
438 | ||
91447636 | 439 | clock_absolutetime_interval_to_deadline(interval, &deadline); |
0a7de745 | 440 | if (!timer_call_enter(&self->depress_timer, deadline, TIMER_CALL_USER_CRITICAL)) { |
91447636 | 441 | self->depress_timer_active++; |
0a7de745 | 442 | } |
91447636 A |
443 | } |
444 | } | |
d9a64523 | 445 | |
91447636 | 446 | thread_unlock(self); |
d9a64523 | 447 | splx(s); |
91447636 A |
448 | } |
449 | ||
450 | void | |
d9a64523 | 451 | thread_depress_ms(mach_msg_timeout_t interval) |
91447636 | 452 | { |
d9a64523 | 453 | uint64_t abstime; |
91447636 | 454 | |
d9a64523 | 455 | clock_interval_to_absolutetime_interval(interval, NSEC_PER_MSEC, &abstime); |
91447636 A |
456 | thread_depress_abstime(abstime); |
457 | } | |
458 | ||
459 | /* | |
460 | * Priority depression expiration. | |
461 | */ | |
462 | void | |
d9a64523 | 463 | thread_depress_expire(void *p0, |
0a7de745 | 464 | __unused void *p1) |
91447636 | 465 | { |
d9a64523 A |
466 | thread_t thread = (thread_t)p0; |
467 | ||
468 | spl_t s = splsched(); | |
469 | thread_lock(thread); | |
470 | ||
471 | assert((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != TH_SFLAG_DEPRESSED_MASK); | |
91447636 | 472 | |
91447636 | 473 | if (--thread->depress_timer_active == 0) { |
6d2010ae | 474 | thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK; |
cb323159 A |
475 | if ((thread->state & TH_RUN) == TH_RUN) { |
476 | thread->last_basepri_change_time = mach_absolute_time(); | |
477 | } | |
d9a64523 | 478 | thread_recompute_sched_pri(thread, SETPRI_DEFAULT); |
91447636 | 479 | } |
d9a64523 A |
480 | |
481 | thread_unlock(thread); | |
482 | splx(s); | |
91447636 A |
483 | } |
484 | ||
485 | /* | |
d9a64523 | 486 | * Prematurely abort priority depression if there is one. |
91447636 A |
487 | */ |
488 | kern_return_t | |
d9a64523 | 489 | thread_depress_abort(thread_t thread) |
91447636 | 490 | { |
d9a64523 A |
491 | kern_return_t result = KERN_NOT_DEPRESSED; |
492 | ||
493 | spl_t s = splsched(); | |
494 | thread_lock(thread); | |
91447636 | 495 | |
d9a64523 A |
496 | assert((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != TH_SFLAG_DEPRESSED_MASK); |
497 | ||
498 | /* | |
499 | * User-triggered depress-aborts should not get out | |
500 | * of the poll-depress, but they should cancel a regular depress. | |
501 | */ | |
502 | if ((thread->sched_flags & TH_SFLAG_POLLDEPRESS) == 0) { | |
503 | result = thread_depress_abort_locked(thread); | |
91447636 | 504 | } |
d9a64523 | 505 | |
91447636 | 506 | thread_unlock(thread); |
d9a64523 | 507 | splx(s); |
91447636 | 508 | |
d9a64523 | 509 | return result; |
91447636 A |
510 | } |
511 | ||
d9a64523 A |
512 | /* |
513 | * Prematurely abort priority depression or poll depression if one is active. | |
514 | * Called with the thread locked. | |
515 | */ | |
516 | kern_return_t | |
517 | thread_depress_abort_locked(thread_t thread) | |
91447636 | 518 | { |
0a7de745 | 519 | if ((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) == 0) { |
d9a64523 | 520 | return KERN_NOT_DEPRESSED; |
0a7de745 | 521 | } |
d9a64523 A |
522 | |
523 | assert((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != TH_SFLAG_DEPRESSED_MASK); | |
91447636 | 524 | |
d9a64523 | 525 | thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK; |
cb323159 A |
526 | if ((thread->state & TH_RUN) == TH_RUN) { |
527 | thread->last_basepri_change_time = mach_absolute_time(); | |
528 | } | |
d9a64523 A |
529 | |
530 | thread_recompute_sched_pri(thread, SETPRI_LAZY); | |
531 | ||
0a7de745 | 532 | if (timer_call_cancel(&thread->depress_timer)) { |
d9a64523 | 533 | thread->depress_timer_active--; |
0a7de745 | 534 | } |
d9a64523 A |
535 | |
536 | return KERN_SUCCESS; | |
537 | } | |
538 | ||
539 | /* | |
540 | * Invoked as part of a polling operation like a no-timeout port receive | |
541 | * | |
542 | * Forces a fixpri thread to yield if it is detected polling without blocking for too long. | |
543 | */ | |
544 | void | |
545 | thread_poll_yield(thread_t self) | |
546 | { | |
91447636 | 547 | assert(self == current_thread()); |
d9a64523 | 548 | assert((self->sched_flags & TH_SFLAG_DEPRESS) == 0); |
91447636 | 549 | |
0a7de745 | 550 | if (self->sched_mode != TH_MODE_FIXED) { |
d9a64523 | 551 | return; |
0a7de745 | 552 | } |
91447636 | 553 | |
d9a64523 | 554 | spl_t s = splsched(); |
91447636 | 555 | |
d9a64523 A |
556 | uint64_t abstime = mach_absolute_time(); |
557 | uint64_t total_computation = abstime - | |
0a7de745 | 558 | self->computation_epoch + self->computation_metered; |
fe8ab488 | 559 | |
d9a64523 A |
560 | if (total_computation >= max_poll_computation) { |
561 | thread_lock(self); | |
562 | ||
563 | self->computation_epoch = abstime; | |
564 | self->computation_metered = 0; | |
565 | ||
566 | uint64_t yield_expiration = abstime + | |
0a7de745 | 567 | (total_computation >> sched_poll_yield_shift); |
d9a64523 A |
568 | |
569 | if (!timer_call_enter(&self->depress_timer, yield_expiration, | |
0a7de745 | 570 | TIMER_CALL_USER_CRITICAL)) { |
d9a64523 | 571 | self->depress_timer_active++; |
0a7de745 | 572 | } |
d9a64523 A |
573 | |
574 | self->sched_flags |= TH_SFLAG_POLLDEPRESS; | |
575 | thread_recompute_sched_pri(self, SETPRI_DEFAULT); | |
576 | ||
577 | thread_unlock(self); | |
91447636 A |
578 | } |
579 | splx(s); | |
1c79356b | 580 | } |
2d21ac55 | 581 | |
d9a64523 A |
582 | /* |
583 | * Kernel-internal interface to yield for a specified period | |
584 | * | |
585 | * WARNING: Will still yield to priority 0 even if the thread is holding a contended lock! | |
586 | */ | |
2d21ac55 | 587 | void |
d9a64523 | 588 | thread_yield_internal(mach_msg_timeout_t ms) |
2d21ac55 | 589 | { |
d9a64523 A |
590 | thread_t self = current_thread(); |
591 | ||
592 | assert((self->sched_flags & TH_SFLAG_DEPRESSED_MASK) != TH_SFLAG_DEPRESSED_MASK); | |
593 | ||
0a7de745 | 594 | processor_t myprocessor; |
2d21ac55 A |
595 | |
596 | disable_preemption(); | |
597 | myprocessor = current_processor(); | |
d9a64523 | 598 | if (!SCHED(thread_should_yield)(myprocessor, self)) { |
2d21ac55 A |
599 | mp_enable_preemption(); |
600 | ||
601 | return; | |
602 | } | |
603 | enable_preemption(); | |
604 | ||
605 | thread_depress_ms(ms); | |
606 | ||
607 | thread_block_reason(THREAD_CONTINUE_NULL, NULL, AST_YIELD); | |
608 | ||
d9a64523 | 609 | thread_depress_abort(self); |
2d21ac55 A |
610 | } |
611 | ||
39037602 A |
612 | /* |
613 | * This yields to a possible non-urgent preemption pending on the current processor. | |
614 | * | |
615 | * This is useful when doing a long computation in the kernel without returning to userspace. | |
616 | * | |
617 | * As opposed to other yielding mechanisms, this does not drop the priority of the current thread. | |
618 | */ | |
619 | void | |
620 | thread_yield_to_preemption() | |
621 | { | |
0a7de745 A |
622 | /* |
623 | * ast_pending() should ideally be called with interrupts disabled, but | |
39037602 A |
624 | * the check here is fine because csw_check() will do the right thing. |
625 | */ | |
626 | ast_t *pending_ast = ast_pending(); | |
627 | ast_t ast = AST_NONE; | |
628 | processor_t p; | |
629 | ||
630 | if (*pending_ast & AST_PREEMPT) { | |
631 | thread_t self = current_thread(); | |
632 | ||
633 | spl_t s = splsched(); | |
634 | ||
635 | p = current_processor(); | |
636 | thread_lock(self); | |
0a7de745 | 637 | ast = csw_check(self, p, AST_YIELD); |
39037602 A |
638 | ast_on(ast); |
639 | thread_unlock(self); | |
640 | ||
641 | if (ast != AST_NONE) { | |
0a7de745 | 642 | (void)thread_block_reason(THREAD_CONTINUE_NULL, NULL, ast); |
39037602 A |
643 | } |
644 | ||
645 | splx(s); | |
646 | } | |
647 | } |