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