]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/syscall_subr.c
xnu-2422.1.72.tar.gz
[apple/xnu.git] / osfmk / kern / syscall_subr.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2009 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 * 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 */
56/*
57 */
58
1c79356b
A
59#include <mach/boolean.h>
60#include <mach/thread_switch.h>
61#include <ipc/ipc_port.h>
62#include <ipc/ipc_space.h>
63#include <kern/counters.h>
1c79356b
A
64#include <kern/ipc_kobject.h>
65#include <kern/processor.h>
66#include <kern/sched.h>
67#include <kern/sched_prim.h>
68#include <kern/spl.h>
69#include <kern/task.h>
70#include <kern/thread.h>
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>
76
b0d623f7
A
77
78#ifdef MACH_BSD
79extern void workqueue_thread_yielded(void);
39236c6e 80extern sched_call_t workqueue_get_sched_callback(void);
b0d623f7
A
81#endif /* MACH_BSD */
82
83
84/* Called from commpage to take a delayed preemption when exiting
85 * the "Preemption Free Zone" (PFZ).
86 */
87kern_return_t
88pfz_exit(
89__unused struct pfz_exit_args *args)
90{
91 /* For now, nothing special to do. We'll pick up the ASTs on kernel exit. */
92
93 return (KERN_SUCCESS);
94}
95
96
1c79356b
A
97/*
98 * swtch and swtch_pri both attempt to context switch (logic in
99 * thread_block no-ops the context switch if nothing would happen).
100 * A boolean is returned that indicates whether there is anything
101 * else runnable.
102 *
103 * This boolean can be used by a thread waiting on a
104 * lock or condition: If FALSE is returned, the thread is justified
105 * in becoming a resource hog by continuing to spin because there's
106 * nothing else useful that the processor could do. If TRUE is
107 * returned, the thread should make one more check on the
108 * lock and then be a good citizen and really suspend.
109 */
110
91447636 111static void
0b4e3aa0 112swtch_continue(void)
1c79356b 113{
0b4e3aa0
A
114 register processor_t myprocessor;
115 boolean_t result;
1c79356b 116
91447636 117 disable_preemption();
1c79356b 118 myprocessor = current_processor();
6d2010ae 119 result = !SCHED(processor_queue_empty)(myprocessor) || rt_runq.count > 0;
91447636 120 enable_preemption();
1c79356b 121
0b4e3aa0
A
122 thread_syscall_return(result);
123 /*NOTREACHED*/
124}
1c79356b
A
125
126boolean_t
91447636
A
127swtch(
128 __unused struct swtch_args *args)
1c79356b
A
129{
130 register processor_t myprocessor;
131 boolean_t result;
132
91447636 133 disable_preemption();
1c79356b 134 myprocessor = current_processor();
6d2010ae 135 if (SCHED(processor_queue_empty)(myprocessor) && rt_runq.count == 0) {
1c79356b
A
136 mp_enable_preemption();
137
138 return (FALSE);
139 }
91447636 140 enable_preemption();
1c79356b
A
141
142 counter(c_swtch_block++);
143
91447636 144 thread_block_reason((thread_continue_t)swtch_continue, NULL, AST_YIELD);
1c79356b 145
91447636 146 disable_preemption();
1c79356b 147 myprocessor = current_processor();
6d2010ae 148 result = !SCHED(processor_queue_empty)(myprocessor) || rt_runq.count > 0;
91447636 149 enable_preemption();
1c79356b
A
150
151 return (result);
152}
153
91447636 154static void
0b4e3aa0
A
155swtch_pri_continue(void)
156{
157 register processor_t myprocessor;
158 boolean_t result;
159
91447636 160 thread_depress_abort_internal(current_thread());
0b4e3aa0 161
91447636 162 disable_preemption();
0b4e3aa0 163 myprocessor = current_processor();
6d2010ae 164 result = !SCHED(processor_queue_empty)(myprocessor) || rt_runq.count > 0;
0b4e3aa0
A
165 mp_enable_preemption();
166
167 thread_syscall_return(result);
168 /*NOTREACHED*/
169}
170
1c79356b
A
171boolean_t
172swtch_pri(
91447636 173__unused struct swtch_pri_args *args)
1c79356b 174{
1c79356b
A
175 register processor_t myprocessor;
176 boolean_t result;
1c79356b 177
91447636 178 disable_preemption();
1c79356b 179 myprocessor = current_processor();
6d2010ae 180 if (SCHED(processor_queue_empty)(myprocessor) && rt_runq.count == 0) {
0b4e3aa0 181 mp_enable_preemption();
1c79356b
A
182
183 return (FALSE);
184 }
91447636 185 enable_preemption();
0b4e3aa0
A
186
187 counter(c_swtch_pri_block++);
188
6d2010ae 189 thread_depress_abstime(thread_depress_time);
1c79356b 190
91447636 191 thread_block_reason((thread_continue_t)swtch_pri_continue, NULL, AST_YIELD);
1c79356b 192
91447636 193 thread_depress_abort_internal(current_thread());
1c79356b 194
91447636 195 disable_preemption();
1c79356b 196 myprocessor = current_processor();
6d2010ae 197 result = !SCHED(processor_queue_empty)(myprocessor) || rt_runq.count > 0;
91447636 198 enable_preemption();
1c79356b
A
199
200 return (result);
201}
202
39236c6e
A
203static int
204thread_switch_disable_workqueue_sched_callback(void)
205{
206 sched_call_t callback = workqueue_get_sched_callback();
207 thread_t self = current_thread();
208 if (!callback || self->sched_call != callback) {
209 return FALSE;
210 }
211 spl_t s = splsched();
212 thread_lock(self);
213 thread_sched_call(self, NULL);
214 thread_unlock(self);
215 splx(s);
216 return TRUE;
217}
218
219static void
220thread_switch_enable_workqueue_sched_callback(void)
221{
222 sched_call_t callback = workqueue_get_sched_callback();
223 thread_t self = current_thread();
224 spl_t s = splsched();
225 thread_lock(self);
226 thread_sched_call(self, callback);
227 thread_unlock(self);
228 splx(s);
229}
230
91447636
A
231static void
232thread_switch_continue(void)
233{
234 register thread_t self = current_thread();
235 int option = self->saved.swtch.option;
39236c6e
A
236 boolean_t reenable_workq_callback = self->saved.swtch.reenable_workq_callback;
237
91447636 238
39236c6e 239 if (option == SWITCH_OPTION_DEPRESS || option == SWITCH_OPTION_OSLOCK_DEPRESS)
91447636
A
240 thread_depress_abort_internal(self);
241
39236c6e
A
242 if (reenable_workq_callback)
243 thread_switch_enable_workqueue_sched_callback();
244
91447636
A
245 thread_syscall_return(KERN_SUCCESS);
246 /*NOTREACHED*/
247}
248
1c79356b
A
249/*
250 * thread_switch:
251 *
252 * Context switch. User may supply thread hint.
253 */
254kern_return_t
255thread_switch(
91447636 256 struct thread_switch_args *args)
1c79356b 257{
91447636
A
258 register thread_t thread, self = current_thread();
259 mach_port_name_t thread_name = args->thread_name;
260 int option = args->option;
261 mach_msg_timeout_t option_time = args->option_time;
39236c6e
A
262 uint32_t scale_factor = NSEC_PER_MSEC;
263 boolean_t reenable_workq_callback = FALSE;
264 boolean_t depress_option = FALSE;
265 boolean_t wait_option = FALSE;
1c79356b
A
266
267 /*
39236c6e 268 * Validate and process option.
1c79356b
A
269 */
270 switch (option) {
271
272 case SWITCH_OPTION_NONE:
39236c6e
A
273 workqueue_thread_yielded();
274 break;
1c79356b 275 case SWITCH_OPTION_WAIT:
39236c6e
A
276 wait_option = TRUE;
277 workqueue_thread_yielded();
278 break;
279 case SWITCH_OPTION_DEPRESS:
280 depress_option = TRUE;
281 workqueue_thread_yielded();
282 break;
283 case SWITCH_OPTION_DISPATCH_CONTENTION:
284 scale_factor = NSEC_PER_USEC;
285 wait_option = TRUE;
286 if (thread_switch_disable_workqueue_sched_callback())
287 reenable_workq_callback = TRUE;
288 break;
289 case SWITCH_OPTION_OSLOCK_DEPRESS:
290 depress_option = TRUE;
291 if (thread_switch_disable_workqueue_sched_callback())
292 reenable_workq_callback = TRUE;
293 break;
294 case SWITCH_OPTION_OSLOCK_WAIT:
295 wait_option = TRUE;
296 if (thread_switch_disable_workqueue_sched_callback())
297 reenable_workq_callback = TRUE;
298 break;
1c79356b
A
299 default:
300 return (KERN_INVALID_ARGUMENT);
301 }
302
91447636
A
303 /*
304 * Translate the port name if supplied.
305 */
1c79356b
A
306 if (thread_name != MACH_PORT_NULL) {
307 ipc_port_t port;
308
91447636 309 if (ipc_port_translate_send(self->task->itk_space,
1c79356b
A
310 thread_name, &port) == KERN_SUCCESS) {
311 ip_reference(port);
312 ip_unlock(port);
313
91447636 314 thread = convert_port_to_thread(port);
316670eb 315 ip_release(port);
91447636
A
316
317 if (thread == self) {
2d21ac55 318 (void)thread_deallocate_internal(thread);
91447636
A
319 thread = THREAD_NULL;
320 }
1c79356b 321 }
91447636
A
322 else
323 thread = THREAD_NULL;
1c79356b 324 }
91447636
A
325 else
326 thread = THREAD_NULL;
327
39236c6e
A
328
329 if (option == SWITCH_OPTION_OSLOCK_DEPRESS || option == SWITCH_OPTION_OSLOCK_WAIT) {
330 if (thread != THREAD_NULL) {
331
332 if (thread->task != self->task) {
333 /*
334 * OSLock boosting only applies to other threads
335 * in your same task (even if you have a port for
336 * a thread in another task)
337 */
338
339 (void)thread_deallocate_internal(thread);
340 thread = THREAD_NULL;
341 } else {
342 /*
343 * Attempt to kick the lock owner up to our same IO throttling tier.
344 * If the thread is currently blocked in throttle_lowpri_io(),
345 * it will immediately break out.
346 */
347 int new_policy = proc_get_effective_thread_policy(self, TASK_POLICY_IO);
348
349 set_thread_iotier_override(thread, new_policy);
350 }
351 }
352 }
353
91447636
A
354 /*
355 * Try to handoff if supplied.
356 */
357 if (thread != THREAD_NULL) {
358 processor_t processor;
359 spl_t s;
360
361 s = splsched();
362 thread_lock(thread);
363
364 /*
2d21ac55
A
365 * Check that the thread is not bound
366 * to a different processor, and that realtime
367 * is not involved.
91447636
A
368 *
369 * Next, pull it off its run queue. If it
370 * doesn't come, it's not eligible.
371 */
372 processor = current_processor();
373 if (processor->current_pri < BASEPRI_RTQUEUES &&
374 thread->sched_pri < BASEPRI_RTQUEUES &&
91447636
A
375 (thread->bound_processor == PROCESSOR_NULL ||
376 thread->bound_processor == processor) &&
6d2010ae 377 thread_run_queue_remove(thread) ) {
91447636
A
378 /*
379 * Hah, got it!!
380 */
381 thread_unlock(thread);
382
2d21ac55 383 (void)thread_deallocate_internal(thread);
91447636 384
39236c6e 385 if (wait_option)
91447636 386 assert_wait_timeout((event_t)assert_wait_timeout, THREAD_ABORTSAFE,
39236c6e 387 option_time, scale_factor);
91447636 388 else
39236c6e 389 if (depress_option)
91447636
A
390 thread_depress_ms(option_time);
391
392 self->saved.swtch.option = option;
39236c6e 393 self->saved.swtch.reenable_workq_callback = reenable_workq_callback;
91447636
A
394
395 thread_run(self, (thread_continue_t)thread_switch_continue, NULL, thread);
396 /* NOTREACHED */
397 }
398
399 thread_unlock(thread);
400 splx(s);
401
402 thread_deallocate(thread);
403 }
404
39236c6e
A
405 if (wait_option)
406 assert_wait_timeout((event_t)assert_wait_timeout, THREAD_ABORTSAFE, option_time, scale_factor);
91447636 407 else
39236c6e 408 if (depress_option)
91447636
A
409 thread_depress_ms(option_time);
410
411 self->saved.swtch.option = option;
39236c6e 412 self->saved.swtch.reenable_workq_callback = reenable_workq_callback;
91447636
A
413
414 thread_block_reason((thread_continue_t)thread_switch_continue, NULL, AST_YIELD);
415
39236c6e 416 if (depress_option)
91447636
A
417 thread_depress_abort_internal(self);
418
39236c6e
A
419 if (reenable_workq_callback)
420 thread_switch_enable_workqueue_sched_callback();
421
91447636
A
422 return (KERN_SUCCESS);
423}
1c79356b 424
91447636
A
425/*
426 * Depress thread's priority to lowest possible for the specified interval,
427 * with a value of zero resulting in no timeout being scheduled.
428 */
429void
430thread_depress_abstime(
431 uint64_t interval)
432{
433 register thread_t self = current_thread();
434 uint64_t deadline;
435 spl_t s;
436
437 s = splsched();
438 thread_lock(self);
6d2010ae 439 if (!(self->sched_flags & TH_SFLAG_DEPRESSED_MASK)) {
91447636
A
440 processor_t myprocessor = self->last_processor;
441
442 self->sched_pri = DEPRESSPRI;
443 myprocessor->current_pri = self->sched_pri;
6d2010ae 444 self->sched_flags |= TH_SFLAG_DEPRESS;
91447636
A
445
446 if (interval != 0) {
447 clock_absolutetime_interval_to_deadline(interval, &deadline);
39236c6e 448 if (!timer_call_enter(&self->depress_timer, deadline, TIMER_CALL_USER_CRITICAL))
91447636
A
449 self->depress_timer_active++;
450 }
451 }
452 thread_unlock(self);
453 splx(s);
454}
455
456void
457thread_depress_ms(
458 mach_msg_timeout_t interval)
459{
460 uint64_t abstime;
461
462 clock_interval_to_absolutetime_interval(
39236c6e 463 interval, NSEC_PER_MSEC, &abstime);
91447636
A
464 thread_depress_abstime(abstime);
465}
466
467/*
468 * Priority depression expiration.
469 */
470void
471thread_depress_expire(
472 void *p0,
473 __unused void *p1)
474{
475 thread_t thread = p0;
476 spl_t s;
477
478 s = splsched();
479 thread_lock(thread);
480 if (--thread->depress_timer_active == 0) {
6d2010ae
A
481 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
482 SCHED(compute_priority)(thread, FALSE);
91447636
A
483 }
484 thread_unlock(thread);
485 splx(s);
486}
487
488/*
489 * Prematurely abort priority depression if there is one.
490 */
491kern_return_t
492thread_depress_abort_internal(
493 thread_t thread)
494{
495 kern_return_t result = KERN_NOT_DEPRESSED;
496 spl_t s;
497
498 s = splsched();
499 thread_lock(thread);
6d2010ae
A
500 if (!(thread->sched_flags & TH_SFLAG_POLLDEPRESS)) {
501 if (thread->sched_flags & TH_SFLAG_DEPRESSED_MASK) {
502 thread->sched_flags &= ~TH_SFLAG_DEPRESSED_MASK;
503 SCHED(compute_priority)(thread, FALSE);
91447636
A
504 result = KERN_SUCCESS;
505 }
506
507 if (timer_call_cancel(&thread->depress_timer))
508 thread->depress_timer_active--;
509 }
510 thread_unlock(thread);
511 splx(s);
512
513 return (result);
514}
515
516void
517thread_poll_yield(
518 thread_t self)
519{
520 spl_t s;
521
522 assert(self == current_thread());
523
524 s = splsched();
6d2010ae 525 if (self->sched_mode == TH_MODE_FIXED) {
91447636
A
526 uint64_t total_computation, abstime;
527
528 abstime = mach_absolute_time();
529 total_computation = abstime - self->computation_epoch;
530 total_computation += self->computation_metered;
531 if (total_computation >= max_poll_computation) {
532 processor_t myprocessor = current_processor();
533 ast_t preempt;
534
535 thread_lock(self);
6d2010ae 536 if (!(self->sched_flags & TH_SFLAG_DEPRESSED_MASK)) {
91447636
A
537 self->sched_pri = DEPRESSPRI;
538 myprocessor->current_pri = self->sched_pri;
91447636
A
539 }
540 self->computation_epoch = abstime;
541 self->computation_metered = 0;
6d2010ae 542 self->sched_flags |= TH_SFLAG_POLLDEPRESS;
91447636
A
543
544 abstime += (total_computation >> sched_poll_yield_shift);
39236c6e 545 if (!timer_call_enter(&self->depress_timer, abstime, TIMER_CALL_USER_CRITICAL))
91447636
A
546 self->depress_timer_active++;
547 thread_unlock(self);
548
c910b4d9 549 if ((preempt = csw_check(myprocessor)) != AST_NONE)
91447636
A
550 ast_on(preempt);
551 }
552 }
553 splx(s);
1c79356b 554}
2d21ac55
A
555
556
557void
558thread_yield_internal(
559 mach_msg_timeout_t ms)
560{
561 processor_t myprocessor;
562
563 disable_preemption();
564 myprocessor = current_processor();
6d2010ae 565 if (SCHED(processor_queue_empty)(myprocessor) && rt_runq.count == 0) {
2d21ac55
A
566 mp_enable_preemption();
567
568 return;
569 }
570 enable_preemption();
571
572 thread_depress_ms(ms);
573
574 thread_block_reason(THREAD_CONTINUE_NULL, NULL, AST_YIELD);
575
576 thread_depress_abort_internal(current_thread());
577}
578