]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/syscall_subr.c
xnu-4903.241.1.tar.gz
[apple/xnu.git] / osfmk / kern / syscall_subr.c
CommitLineData
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 */
31/*
32 * Mach Operating System
33 * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University
34 * All Rights Reserved.
35 *
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.
41 *
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.
45 *
46 * Carnegie Mellon requests users of this software to return to
47 *
48 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
49 * School of Computer Science
50 * Carnegie Mellon University
51 * Pittsburgh PA 15213-3890
52 *
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>
61#include <kern/counters.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
79static void thread_depress_abstime(uint64_t interval);
80static 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 */
85kern_return_t
86pfz_exit(
87__unused struct pfz_exit_args *args)
88{
89 /* For now, nothing special to do. We'll pick up the ASTs on kernel exit. */
90
91 return (KERN_SUCCESS);
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 102static void
0b4e3aa0 103swtch_continue(void)
1c79356b 104{
39037602 105 processor_t myprocessor;
5ba3f43e 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
119boolean_t
91447636
A
120swtch(
121 __unused struct swtch_args *args)
1c79356b 122{
39037602 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
130 return (FALSE);
131 }
91447636 132 enable_preemption();
1c79356b
A
133
134 counter(c_swtch_block++);
135
d9a64523 136 thread_yield_with_continuation((thread_continue_t)swtch_continue, NULL);
1c79356b
A
137}
138
91447636 139static void
0b4e3aa0
A
140swtch_pri_continue(void)
141{
39037602 142 processor_t myprocessor;
5ba3f43e 143 boolean_t result;
0b4e3aa0 144
d9a64523 145 thread_depress_abort(current_thread());
0b4e3aa0 146
5ba3f43e 147 disable_preemption();
0b4e3aa0 148 myprocessor = current_processor();
5ba3f43e 149 result = SCHED(thread_should_yield)(myprocessor, current_thread());
0b4e3aa0
A
150 mp_enable_preemption();
151
e8c3f781
A
152 ml_delay_on_yield();
153
0b4e3aa0
A
154 thread_syscall_return(result);
155 /*NOTREACHED*/
156}
157
1c79356b
A
158boolean_t
159swtch_pri(
91447636 160__unused struct swtch_pri_args *args)
1c79356b 161{
39037602 162 processor_t myprocessor;
1c79356b 163
91447636 164 disable_preemption();
1c79356b 165 myprocessor = current_processor();
5ba3f43e 166 if (!SCHED(thread_should_yield)(myprocessor, current_thread())) {
0b4e3aa0 167 mp_enable_preemption();
1c79356b
A
168
169 return (FALSE);
170 }
91447636 171 enable_preemption();
0b4e3aa0
A
172
173 counter(c_swtch_pri_block++);
174
6d2010ae 175 thread_depress_abstime(thread_depress_time);
1c79356b 176
d9a64523 177 thread_yield_with_continuation((thread_continue_t)swtch_pri_continue, NULL);
39236c6e
A
178}
179
180static void
d9a64523 181thread_switch_continue(void *parameter, __unused int ret)
39236c6e 182{
d9a64523
A
183 thread_t self = current_thread();
184 int option = (int)(intptr_t)parameter;
91447636 185
39236c6e 186 if (option == SWITCH_OPTION_DEPRESS || option == SWITCH_OPTION_OSLOCK_DEPRESS)
d9a64523 187 thread_depress_abort(self);
39236c6e 188
e8c3f781
A
189 ml_delay_on_yield();
190
91447636
A
191 thread_syscall_return(KERN_SUCCESS);
192 /*NOTREACHED*/
193}
194
1c79356b
A
195/*
196 * thread_switch:
197 *
198 * Context switch. User may supply thread hint.
199 */
200kern_return_t
201thread_switch(
91447636 202 struct thread_switch_args *args)
1c79356b 203{
3e170ce0
A
204 thread_t thread = THREAD_NULL;
205 thread_t self = current_thread();
91447636
A
206 mach_port_name_t thread_name = args->thread_name;
207 int option = args->option;
208 mach_msg_timeout_t option_time = args->option_time;
39236c6e 209 uint32_t scale_factor = NSEC_PER_MSEC;
39236c6e
A
210 boolean_t depress_option = FALSE;
211 boolean_t wait_option = FALSE;
d9a64523 212 wait_interrupt_t interruptible = THREAD_ABORTSAFE;
1c79356b
A
213
214 /*
d9a64523
A
215 * Validate and process option.
216 */
217 switch (option) {
1c79356b 218 case SWITCH_OPTION_NONE:
39236c6e 219 break;
1c79356b 220 case SWITCH_OPTION_WAIT:
39236c6e 221 wait_option = TRUE;
39236c6e
A
222 break;
223 case SWITCH_OPTION_DEPRESS:
224 depress_option = TRUE;
39236c6e
A
225 break;
226 case SWITCH_OPTION_DISPATCH_CONTENTION:
227 scale_factor = NSEC_PER_USEC;
228 wait_option = TRUE;
d9a64523 229 interruptible |= THREAD_WAIT_NOREPORT;
39236c6e
A
230 break;
231 case SWITCH_OPTION_OSLOCK_DEPRESS:
232 depress_option = TRUE;
d9a64523 233 interruptible |= THREAD_WAIT_NOREPORT;
39236c6e
A
234 break;
235 case SWITCH_OPTION_OSLOCK_WAIT:
236 wait_option = TRUE;
d9a64523 237 interruptible |= THREAD_WAIT_NOREPORT;
39236c6e 238 break;
1c79356b
A
239 default:
240 return (KERN_INVALID_ARGUMENT);
241 }
242
91447636
A
243 /*
244 * Translate the port name if supplied.
245 */
3e170ce0
A
246 if (thread_name != MACH_PORT_NULL) {
247 ipc_port_t port;
1c79356b 248
91447636 249 if (ipc_port_translate_send(self->task->itk_space,
3e170ce0 250 thread_name, &port) == KERN_SUCCESS) {
1c79356b
A
251 ip_reference(port);
252 ip_unlock(port);
253
91447636 254 thread = convert_port_to_thread(port);
316670eb 255 ip_release(port);
91447636
A
256
257 if (thread == self) {
3e170ce0 258 thread_deallocate(thread);
91447636
A
259 thread = THREAD_NULL;
260 }
1c79356b
A
261 }
262 }
39236c6e
A
263
264 if (option == SWITCH_OPTION_OSLOCK_DEPRESS || option == SWITCH_OPTION_OSLOCK_WAIT) {
265 if (thread != THREAD_NULL) {
266
267 if (thread->task != self->task) {
268 /*
269 * OSLock boosting only applies to other threads
270 * in your same task (even if you have a port for
271 * a thread in another task)
272 */
273
3e170ce0 274 thread_deallocate(thread);
39236c6e
A
275 thread = THREAD_NULL;
276 } else {
277 /*
278 * Attempt to kick the lock owner up to our same IO throttling tier.
279 * If the thread is currently blocked in throttle_lowpri_io(),
280 * it will immediately break out.
3e170ce0
A
281 *
282 * TODO: SFI break out?
39236c6e
A
283 */
284 int new_policy = proc_get_effective_thread_policy(self, TASK_POLICY_IO);
3e170ce0 285
39236c6e
A
286 set_thread_iotier_override(thread, new_policy);
287 }
288 }
289 }
290
91447636
A
291 /*
292 * Try to handoff if supplied.
293 */
294 if (thread != THREAD_NULL) {
3e170ce0 295 spl_t s = splsched();
91447636 296
3e170ce0
A
297 /* This may return a different thread if the target is pushing on something */
298 thread_t pulled_thread = thread_run_queue_remove_for_handoff(thread);
91447636 299
fe8ab488 300 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_SCHED_THREAD_SWITCH)|DBG_FUNC_NONE,
3e170ce0
A
301 thread_tid(thread), thread->state,
302 pulled_thread ? TRUE : FALSE, 0, 0);
303
304 if (pulled_thread != THREAD_NULL) {
305 /* We can't be dropping the last ref here */
306 thread_deallocate_safe(thread);
91447636 307
39236c6e 308 if (wait_option)
d9a64523 309 assert_wait_timeout((event_t)assert_wait_timeout, interruptible,
3e170ce0
A
310 option_time, scale_factor);
311 else if (depress_option)
91447636
A
312 thread_depress_ms(option_time);
313
d9a64523
A
314 thread_run(self, thread_switch_continue, (void *)(intptr_t)option, pulled_thread);
315 __builtin_unreachable();
91447636
A
316 }
317
91447636
A
318 splx(s);
319
320 thread_deallocate(thread);
321 }
3e170ce0 322
e8c3f781 323 if (wait_option) {
d9a64523 324 assert_wait_timeout((event_t)assert_wait_timeout, interruptible, option_time, scale_factor);
e8c3f781
A
325 } else {
326 disable_preemption();
327 bool should_yield = SCHED(thread_should_yield)(current_processor(), current_thread());
328 enable_preemption();
329
330 if (should_yield == false) {
331 /* Early-return if yielding to the scheduler will not be beneficial */
332 return KERN_SUCCESS;
333 }
334
335 if (depress_option) {
336 thread_depress_ms(option_time);
337 }
338 }
3e170ce0 339
d9a64523
A
340 thread_yield_with_continuation(thread_switch_continue, (void *)(intptr_t)option);
341 __builtin_unreachable();
342}
39236c6e 343
d9a64523
A
344void
345thread_yield_with_continuation(
346 thread_continue_t continuation,
347 void *parameter)
348{
349 assert(continuation);
350 thread_block_reason(continuation, parameter, AST_YIELD);
351 __builtin_unreachable();
91447636 352}
1c79356b 353
d9a64523 354
39037602
A
355/* Returns a +1 thread reference */
356thread_t
357port_name_to_thread_for_ulock(mach_port_name_t thread_name)
358{
359 thread_t thread = THREAD_NULL;
360 thread_t self = current_thread();
361
362 /*
363 * Translate the port name if supplied.
364 */
365 if (thread_name != MACH_PORT_NULL) {
366 ipc_port_t port;
367
368 if (ipc_port_translate_send(self->task->itk_space,
369 thread_name, &port) == KERN_SUCCESS) {
370 ip_reference(port);
371 ip_unlock(port);
372
373 thread = convert_port_to_thread(port);
374 ip_release(port);
375
376 if (thread == THREAD_NULL) {
377 return thread;
378 }
379
380 if ((thread == self) || (thread->task != self->task)) {
381 thread_deallocate(thread);
382 thread = THREAD_NULL;
383 }
384 }
385 }
386
387 return thread;
388}
389
390/* This function is called after an assert_wait(), therefore it must not
391 * cause another wait until after the thread_run() or thread_block()
392 *
d9a64523
A
393 *
394 * When called with a NULL continuation, the thread ref is consumed
395 * (thread_handoff_deallocate calling convention) else it is up to the
396 * continuation to do the cleanup (thread_handoff_parameter calling convention)
397 * and it instead doesn't return.
39037602 398 */
d9a64523
A
399static wait_result_t
400thread_handoff_internal(thread_t thread, thread_continue_t continuation,
401 void *parameter)
39037602
A
402{
403 thread_t deallocate_thread = THREAD_NULL;
404 thread_t self = current_thread();
405
406 /*
407 * Try to handoff if supplied.
408 */
409 if (thread != THREAD_NULL) {
410 spl_t s = splsched();
411
412 thread_t pulled_thread = thread_run_queue_remove_for_handoff(thread);
413
414 KERNEL_DEBUG_CONSTANT(MACHDBG_CODE(DBG_MACH_SCHED,MACH_SCHED_THREAD_SWITCH)|DBG_FUNC_NONE,
415 thread_tid(thread), thread->state,
416 pulled_thread ? TRUE : FALSE, 0, 0);
417
418 if (pulled_thread != THREAD_NULL) {
d9a64523
A
419 if (continuation == NULL) {
420 /* We can't be dropping the last ref here */
421 thread_deallocate_safe(thread);
422 }
39037602 423
d9a64523 424 int result = thread_run(self, continuation, parameter, pulled_thread);
39037602
A
425
426 splx(s);
427 return result;
428 }
429
430 splx(s);
431
432 deallocate_thread = thread;
433 thread = THREAD_NULL;
434 }
435
d9a64523 436 int result = thread_block_parameter(continuation, parameter);
39037602
A
437 if (deallocate_thread != THREAD_NULL) {
438 thread_deallocate(deallocate_thread);
439 }
440
441 return result;
442}
443
d9a64523
A
444void
445thread_handoff_parameter(thread_t thread, thread_continue_t continuation,
446 void *parameter)
447{
448 thread_handoff_internal(thread, continuation, parameter);
449 panic("NULL continuation passed to %s", __func__);
450 __builtin_unreachable();
451}
452
453wait_result_t
454thread_handoff_deallocate(thread_t thread)
455{
456 return thread_handoff_internal(thread, NULL, NULL);
457}
458
459/*
460 * Thread depression
461 *
462 * This mechanism drops a thread to priority 0 in order for it to yield to
463 * all other runnnable threads on the system. It can be canceled or timed out,
464 * whereupon the thread goes back to where it was.
465 *
466 * Note that TH_SFLAG_DEPRESS and TH_SFLAG_POLLDEPRESS are never set at the
467 * same time. DEPRESS always defers to POLLDEPRESS.
468 *
469 * DEPRESS only lasts across a single thread_block call, and never returns
470 * to userspace.
471 * POLLDEPRESS can be active anywhere up until thread termination.
472 */
473
91447636
A
474/*
475 * Depress thread's priority to lowest possible for the specified interval,
d9a64523
A
476 * with an interval of zero resulting in no timeout being scheduled.
477 *
478 * Must block with AST_YIELD afterwards to take effect
91447636
A
479 */
480void
d9a64523 481thread_depress_abstime(uint64_t interval)
91447636 482{
d9a64523
A
483 thread_t self = current_thread();
484
485 spl_t s = splsched();
486 thread_lock(self);
487
488 assert((self->sched_flags & TH_SFLAG_DEPRESS) == 0);
489
490 if ((self->sched_flags & TH_SFLAG_POLLDEPRESS) == 0) {
6d2010ae 491 self->sched_flags |= TH_SFLAG_DEPRESS;
d9a64523 492 thread_recompute_sched_pri(self, SETPRI_LAZY);
91447636
A
493
494 if (interval != 0) {
d9a64523
A
495 uint64_t deadline;
496
91447636 497 clock_absolutetime_interval_to_deadline(interval, &deadline);
39236c6e 498 if (!timer_call_enter(&self->depress_timer, deadline, TIMER_CALL_USER_CRITICAL))
91447636
A
499 self->depress_timer_active++;
500 }
501 }
d9a64523 502
91447636 503 thread_unlock(self);
d9a64523 504 splx(s);
91447636
A
505}
506
507void
d9a64523 508thread_depress_ms(mach_msg_timeout_t interval)
91447636 509{
d9a64523 510 uint64_t abstime;
91447636 511
d9a64523 512 clock_interval_to_absolutetime_interval(interval, NSEC_PER_MSEC, &abstime);
91447636
A
513 thread_depress_abstime(abstime);
514}
515
516/*
517 * Priority depression expiration.
518 */
519void
d9a64523
A
520thread_depress_expire(void *p0,
521 __unused void *p1)
91447636 522{
d9a64523
A
523 thread_t thread = (thread_t)p0;
524
525 spl_t s = splsched();
526 thread_lock(thread);
527
528 assert((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != TH_SFLAG_DEPRESSED_MASK);
91447636 529
91447636 530 if (--thread->depress_timer_active == 0) {
6d2010ae 531 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
d9a64523 532 thread_recompute_sched_pri(thread, SETPRI_DEFAULT);
91447636 533 }
d9a64523
A
534
535 thread_unlock(thread);
536 splx(s);
91447636
A
537}
538
539/*
d9a64523 540 * Prematurely abort priority depression if there is one.
91447636
A
541 */
542kern_return_t
d9a64523 543thread_depress_abort(thread_t thread)
91447636 544{
d9a64523
A
545 kern_return_t result = KERN_NOT_DEPRESSED;
546
547 spl_t s = splsched();
548 thread_lock(thread);
91447636 549
d9a64523
A
550 assert((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != TH_SFLAG_DEPRESSED_MASK);
551
552 /*
553 * User-triggered depress-aborts should not get out
554 * of the poll-depress, but they should cancel a regular depress.
555 */
556 if ((thread->sched_flags & TH_SFLAG_POLLDEPRESS) == 0) {
557 result = thread_depress_abort_locked(thread);
91447636 558 }
d9a64523 559
91447636 560 thread_unlock(thread);
d9a64523 561 splx(s);
91447636 562
d9a64523 563 return result;
91447636
A
564}
565
d9a64523
A
566/*
567 * Prematurely abort priority depression or poll depression if one is active.
568 * Called with the thread locked.
569 */
570kern_return_t
571thread_depress_abort_locked(thread_t thread)
91447636 572{
d9a64523
A
573 if ((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) == 0)
574 return KERN_NOT_DEPRESSED;
575
576 assert((thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) != TH_SFLAG_DEPRESSED_MASK);
91447636 577
d9a64523
A
578 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
579
580 thread_recompute_sched_pri(thread, SETPRI_LAZY);
581
582 if (timer_call_cancel(&thread->depress_timer))
583 thread->depress_timer_active--;
584
585 return KERN_SUCCESS;
586}
587
588/*
589 * Invoked as part of a polling operation like a no-timeout port receive
590 *
591 * Forces a fixpri thread to yield if it is detected polling without blocking for too long.
592 */
593void
594thread_poll_yield(thread_t self)
595{
91447636 596 assert(self == current_thread());
d9a64523 597 assert((self->sched_flags & TH_SFLAG_DEPRESS) == 0);
91447636 598
d9a64523
A
599 if (self->sched_mode != TH_MODE_FIXED)
600 return;
91447636 601
d9a64523 602 spl_t s = splsched();
91447636 603
d9a64523
A
604 uint64_t abstime = mach_absolute_time();
605 uint64_t total_computation = abstime -
606 self->computation_epoch + self->computation_metered;
fe8ab488 607
d9a64523
A
608 if (total_computation >= max_poll_computation) {
609 thread_lock(self);
610
611 self->computation_epoch = abstime;
612 self->computation_metered = 0;
613
614 uint64_t yield_expiration = abstime +
615 (total_computation >> sched_poll_yield_shift);
616
617 if (!timer_call_enter(&self->depress_timer, yield_expiration,
618 TIMER_CALL_USER_CRITICAL))
619 self->depress_timer_active++;
620
621 self->sched_flags |= TH_SFLAG_POLLDEPRESS;
622 thread_recompute_sched_pri(self, SETPRI_DEFAULT);
623
624 thread_unlock(self);
91447636
A
625 }
626 splx(s);
1c79356b 627}
2d21ac55 628
d9a64523
A
629/*
630 * Kernel-internal interface to yield for a specified period
631 *
632 * WARNING: Will still yield to priority 0 even if the thread is holding a contended lock!
633 */
2d21ac55 634void
d9a64523 635thread_yield_internal(mach_msg_timeout_t ms)
2d21ac55 636{
d9a64523
A
637 thread_t self = current_thread();
638
639 assert((self->sched_flags & TH_SFLAG_DEPRESSED_MASK) != TH_SFLAG_DEPRESSED_MASK);
640
2d21ac55
A
641 processor_t myprocessor;
642
643 disable_preemption();
644 myprocessor = current_processor();
d9a64523 645 if (!SCHED(thread_should_yield)(myprocessor, self)) {
2d21ac55
A
646 mp_enable_preemption();
647
648 return;
649 }
650 enable_preemption();
651
652 thread_depress_ms(ms);
653
654 thread_block_reason(THREAD_CONTINUE_NULL, NULL, AST_YIELD);
655
d9a64523 656 thread_depress_abort(self);
2d21ac55
A
657}
658
39037602
A
659/*
660 * This yields to a possible non-urgent preemption pending on the current processor.
661 *
662 * This is useful when doing a long computation in the kernel without returning to userspace.
663 *
664 * As opposed to other yielding mechanisms, this does not drop the priority of the current thread.
665 */
666void
667thread_yield_to_preemption()
668{
669 /*
670 * ast_pending() should ideally be called with interrupts disabled, but
671 * the check here is fine because csw_check() will do the right thing.
672 */
673 ast_t *pending_ast = ast_pending();
674 ast_t ast = AST_NONE;
675 processor_t p;
676
677 if (*pending_ast & AST_PREEMPT) {
678 thread_t self = current_thread();
679
680 spl_t s = splsched();
681
682 p = current_processor();
683 thread_lock(self);
684 ast = csw_check(p, AST_YIELD);
685 ast_on(ast);
686 thread_unlock(self);
687
688 if (ast != AST_NONE) {
689 (void)thread_block_reason(THREAD_CONTINUE_NULL, NULL, ast);
690 }
691
692 splx(s);
693 }
694}
695