]> git.saurik.com Git - apple/xnu.git/blame - osfmk/kern/sync_sema.c
xnu-7195.101.1.tar.gz
[apple/xnu.git] / osfmk / kern / sync_sema.c
CommitLineData
1c79356b 1/*
f427ee49 2 * Copyright (c) 2000-2020 Apple Inc. All rights reserved.
1c79356b 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
0a7de745 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.
0a7de745 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.
0a7de745 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.
0a7de745 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * @OSF_COPYRIGHT@
0a7de745 30 *
1c79356b
A
31 */
32/*
33 * File: kern/sync_sema.c
34 * Author: Joseph CaraDonna
35 *
36 * Contains RT distributed semaphore synchronization services.
37 */
38
39#include <mach/mach_types.h>
91447636 40#include <mach/mach_traps.h>
1c79356b
A
41#include <mach/kern_return.h>
42#include <mach/semaphore.h>
43#include <mach/sync_policy.h>
91447636 44#include <mach/task.h>
1c79356b
A
45
46#include <kern/misc_protos.h>
47#include <kern/sync_sema.h>
48#include <kern/spl.h>
49#include <kern/ipc_kobject.h>
50#include <kern/ipc_sync.h>
51#include <kern/ipc_tt.h>
52#include <kern/thread.h>
53#include <kern/clock.h>
54#include <ipc/ipc_port.h>
55#include <ipc/ipc_space.h>
56#include <kern/host.h>
3e170ce0 57#include <kern/waitq.h>
1c79356b
A
58#include <kern/zalloc.h>
59#include <kern/mach_param.h>
60
316670eb
A
61#include <libkern/OSAtomic.h>
62
9bccf70c 63static unsigned int semaphore_event;
cf7d32b8 64#define SEMAPHORE_EVENT CAST_EVENT64_T(&semaphore_event)
1c79356b 65
f427ee49 66ZONE_DECLARE(semaphore_zone, "semaphores", sizeof(struct semaphore), ZC_NONE);
1c79356b 67
d9a64523
A
68os_refgrp_decl(static, sema_refgrp, "semaphore", NULL);
69
91447636
A
70/* Forward declarations */
71
72
0a7de745 73kern_return_t
91447636 74semaphore_wait_trap_internal(
0a7de745
A
75 mach_port_name_t name,
76 void (*caller_cont)(kern_return_t));
91447636 77
0a7de745 78kern_return_t
91447636 79semaphore_wait_signal_trap_internal(
0a7de745
A
80 mach_port_name_t wait_name,
81 mach_port_name_t signal_name,
82 void (*caller_cont)(kern_return_t));
91447636 83
0a7de745 84kern_return_t
91447636 85semaphore_timedwait_trap_internal(
0a7de745
A
86 mach_port_name_t name,
87 unsigned int sec,
88 clock_res_t nsec,
89 void (*caller_cont)(kern_return_t));
91447636 90
0a7de745 91kern_return_t
91447636 92semaphore_timedwait_signal_trap_internal(
0a7de745
A
93 mach_port_name_t wait_name,
94 mach_port_name_t signal_name,
95 unsigned int sec,
96 clock_res_t nsec,
97 void (*caller_cont)(kern_return_t));
91447636 98
2d21ac55
A
99kern_return_t
100semaphore_signal_internal_trap(mach_port_name_t sema_name);
91447636
A
101
102kern_return_t
103semaphore_signal_internal(
0a7de745
A
104 semaphore_t semaphore,
105 thread_t thread,
106 int options);
91447636
A
107
108kern_return_t
109semaphore_convert_wait_result(
0a7de745 110 int wait_result);
91447636
A
111
112void
f427ee49 113semaphore_wait_continue(void *arg __unused, wait_result_t wr);
91447636 114
b0d623f7 115static kern_return_t
91447636 116semaphore_wait_internal(
0a7de745
A
117 semaphore_t wait_semaphore,
118 semaphore_t signal_semaphore,
119 uint64_t deadline,
120 int option,
121 void (*caller_cont)(kern_return_t));
91447636 122
b0d623f7
A
123static __inline__ uint64_t
124semaphore_deadline(
0a7de745
A
125 unsigned int sec,
126 clock_res_t nsec)
b0d623f7 127{
0a7de745 128 uint64_t abstime;
b0d623f7 129
0a7de745 130 nanoseconds_to_absolutetime((uint64_t)sec * NSEC_PER_SEC + nsec, &abstime);
b0d623f7
A
131 clock_absolutetime_interval_to_deadline(abstime, &abstime);
132
0a7de745 133 return abstime;
b0d623f7
A
134}
135
1c79356b
A
136/*
137 * Routine: semaphore_create
138 *
139 * Creates a semaphore.
140 * The port representing the semaphore is returned as a parameter.
141 */
142kern_return_t
143semaphore_create(
0a7de745
A
144 task_t task,
145 semaphore_t *new_semaphore,
f427ee49
A
146 int policy,
147 int value)
1c79356b 148{
f427ee49 149 semaphore_t s = SEMAPHORE_NULL;
0a7de745 150 kern_return_t kret;
1c79356b 151
b0d623f7 152 *new_semaphore = SEMAPHORE_NULL;
f427ee49 153 if (task == TASK_NULL || value < 0 || policy > SYNC_POLICY_MAX || policy < 0) {
1c79356b 154 return KERN_INVALID_ARGUMENT;
0a7de745 155 }
1c79356b 156
0a7de745 157 s = (semaphore_t) zalloc(semaphore_zone);
1c79356b 158
0a7de745
A
159 if (s == SEMAPHORE_NULL) {
160 return KERN_RESOURCE_SHORTAGE;
161 }
b0d623f7 162
3e170ce0 163 kret = waitq_init(&s->waitq, policy | SYNC_POLICY_DISABLE_IRQ); /* also inits lock */
b0d623f7
A
164 if (kret != KERN_SUCCESS) {
165 zfree(semaphore_zone, s);
166 return kret;
1c79356b
A
167 }
168
1c79356b 169 /*
3e170ce0 170 * Initialize the semaphore values.
1c79356b 171 */
0a7de745 172 s->port = IP_NULL;
d9a64523 173 os_ref_init(&s->ref_count, &sema_refgrp);
3e170ce0
A
174 s->count = value;
175 s->active = TRUE;
176 s->owner = task;
1c79356b
A
177
178 /*
179 * Associate the new semaphore with the task by adding
180 * the new semaphore to the task's semaphore list.
1c79356b
A
181 */
182 task_lock(task);
c3c9b80d
A
183 /* Check for race with task_terminate */
184 if (!task->active) {
185 task_unlock(task);
186 zfree(semaphore_zone, s);
187 return KERN_INVALID_TASK;
188 }
1c79356b
A
189 enqueue_head(&task->semaphore_list, (queue_entry_t) s);
190 task->semaphores_owned++;
1c79356b
A
191 task_unlock(task);
192
193 *new_semaphore = s;
194
195 return KERN_SUCCESS;
0a7de745 196}
1c79356b
A
197
198/*
3e170ce0 199 * Routine: semaphore_destroy_internal
1c79356b 200 *
4bd07ac2
A
201 * Disassociate a semaphore from its owning task, mark it inactive,
202 * and set any waiting threads running with THREAD_RESTART.
1c79356b 203 *
4bd07ac2
A
204 * Conditions:
205 * task is locked
206 * semaphore is locked
207 * semaphore is owned by the specified task
208 * Returns:
209 * with semaphore unlocked
1c79356b 210 */
4bd07ac2 211static void
3e170ce0 212semaphore_destroy_internal(
0a7de745
A
213 task_t task,
214 semaphore_t semaphore)
1c79356b 215{
0a7de745 216 int old_count;
3e170ce0 217
4bd07ac2
A
218 /* unlink semaphore from owning task */
219 assert(semaphore->owner == task);
6d2010ae 220 remqueue((queue_entry_t) semaphore);
1c79356b
A
221 semaphore->owner = TASK_NULL;
222 task->semaphores_owned--;
1c79356b 223
1c79356b
A
224 /*
225 * Deactivate semaphore
226 */
227 assert(semaphore->active);
228 semaphore->active = FALSE;
229
230 /*
0a7de745 231 * Wakeup blocked threads
1c79356b
A
232 */
233 old_count = semaphore->count;
234 semaphore->count = 0;
235
236 if (old_count < 0) {
3e170ce0 237 waitq_wakeup64_all_locked(&semaphore->waitq,
0a7de745
A
238 SEMAPHORE_EVENT,
239 THREAD_RESTART, NULL,
240 WAITQ_ALL_PRIORITIES,
241 WAITQ_UNLOCK);
3e170ce0 242 /* waitq/semaphore is unlocked */
1c79356b
A
243 } else {
244 semaphore_unlock(semaphore);
245 }
1c79356b
A
246}
247
3e170ce0
A
248/*
249 * Routine: semaphore_destroy
250 *
251 * Destroys a semaphore and consume the caller's reference on the
252 * semaphore.
253 */
254kern_return_t
255semaphore_destroy(
0a7de745
A
256 task_t task,
257 semaphore_t semaphore)
3e170ce0 258{
4bd07ac2 259 spl_t spl_level;
3e170ce0 260
0a7de745 261 if (semaphore == SEMAPHORE_NULL) {
3e170ce0 262 return KERN_INVALID_ARGUMENT;
0a7de745 263 }
3e170ce0
A
264
265 if (task == TASK_NULL) {
4bd07ac2
A
266 semaphore_dereference(semaphore);
267 return KERN_INVALID_ARGUMENT;
268 }
269
270 task_lock(task);
271 spl_level = splsched();
272 semaphore_lock(semaphore);
273
274 if (semaphore->owner != task) {
275 semaphore_unlock(semaphore);
d9a64523 276 semaphore_dereference(semaphore);
4bd07ac2
A
277 splx(spl_level);
278 task_unlock(task);
279 return KERN_INVALID_ARGUMENT;
3e170ce0 280 }
d9a64523 281
4bd07ac2
A
282 semaphore_destroy_internal(task, semaphore);
283 /* semaphore unlocked */
284
285 splx(spl_level);
286 task_unlock(task);
287
3e170ce0 288 semaphore_dereference(semaphore);
4bd07ac2
A
289 return KERN_SUCCESS;
290}
291
292/*
293 * Routine: semaphore_destroy_all
294 *
295 * Destroy all the semaphores associated with a given task.
296 */
297#define SEMASPERSPL 20 /* max number of semaphores to destroy per spl hold */
298
299void
300semaphore_destroy_all(
0a7de745 301 task_t task)
4bd07ac2
A
302{
303 uint32_t count;
304 spl_t spl_level;
305
306 count = 0;
307 task_lock(task);
308 while (!queue_empty(&task->semaphore_list)) {
309 semaphore_t semaphore;
310
311 semaphore = (semaphore_t) queue_first(&task->semaphore_list);
312
0a7de745 313 if (count == 0) {
4bd07ac2 314 spl_level = splsched();
0a7de745 315 }
4bd07ac2
A
316 semaphore_lock(semaphore);
317
318 semaphore_destroy_internal(task, semaphore);
319 /* semaphore unlocked */
320
321 /* throttle number of semaphores per interrupt disablement */
322 if (++count == SEMASPERSPL) {
323 count = 0;
324 splx(spl_level);
325 }
326 }
0a7de745 327 if (count != 0) {
4bd07ac2 328 splx(spl_level);
0a7de745 329 }
4bd07ac2
A
330
331 task_unlock(task);
3e170ce0
A
332}
333
1c79356b
A
334/*
335 * Routine: semaphore_signal_internal
336 *
0a7de745 337 * Signals the semaphore as direct.
1c79356b
A
338 * Assumptions:
339 * Semaphore is locked.
340 */
341kern_return_t
342semaphore_signal_internal(
0a7de745
A
343 semaphore_t semaphore,
344 thread_t thread,
345 int options)
1c79356b
A
346{
347 kern_return_t kr;
348 spl_t spl_level;
349
350 spl_level = splsched();
351 semaphore_lock(semaphore);
352
353 if (!semaphore->active) {
354 semaphore_unlock(semaphore);
355 splx(spl_level);
356 return KERN_TERMINATED;
357 }
358
91447636 359 if (thread != THREAD_NULL) {
1c79356b 360 if (semaphore->count < 0) {
3e170ce0 361 kr = waitq_wakeup64_thread_locked(
0a7de745
A
362 &semaphore->waitq,
363 SEMAPHORE_EVENT,
364 thread,
365 THREAD_AWAKENED,
366 WAITQ_UNLOCK);
3e170ce0 367 /* waitq/semaphore is unlocked */
1c79356b 368 } else {
1c79356b 369 kr = KERN_NOT_WAITING;
3e170ce0 370 semaphore_unlock(semaphore);
1c79356b
A
371 }
372 splx(spl_level);
373 return kr;
0a7de745 374 }
1c79356b
A
375
376 if (options & SEMAPHORE_SIGNAL_ALL) {
377 int old_count = semaphore->count;
378
3e170ce0 379 kr = KERN_NOT_WAITING;
1c79356b
A
380 if (old_count < 0) {
381 semaphore->count = 0; /* always reset */
3e170ce0 382 kr = waitq_wakeup64_all_locked(
0a7de745
A
383 &semaphore->waitq,
384 SEMAPHORE_EVENT,
385 THREAD_AWAKENED, NULL,
386 WAITQ_ALL_PRIORITIES,
387 WAITQ_UNLOCK);
3e170ce0 388 /* waitq / semaphore is unlocked */
1c79356b 389 } else {
0a7de745 390 if (options & SEMAPHORE_SIGNAL_PREPOST) {
1c79356b 391 semaphore->count++;
0a7de745 392 }
1c79356b 393 kr = KERN_SUCCESS;
3e170ce0 394 semaphore_unlock(semaphore);
1c79356b
A
395 }
396 splx(spl_level);
397 return kr;
398 }
0a7de745 399
1c79356b 400 if (semaphore->count < 0) {
f427ee49
A
401 waitq_options_t wq_option = (options & SEMAPHORE_THREAD_HANDOFF) ?
402 WQ_OPTION_HANDOFF : WQ_OPTION_NONE;
3e170ce0 403 kr = waitq_wakeup64_one_locked(
0a7de745
A
404 &semaphore->waitq,
405 SEMAPHORE_EVENT,
406 THREAD_AWAKENED, NULL,
407 WAITQ_ALL_PRIORITIES,
f427ee49
A
408 WAITQ_KEEP_LOCKED,
409 wq_option);
3e170ce0 410 if (kr == KERN_SUCCESS) {
1c79356b
A
411 semaphore_unlock(semaphore);
412 splx(spl_level);
413 return KERN_SUCCESS;
3e170ce0 414 } else {
1c79356b 415 semaphore->count = 0; /* all waiters gone */
3e170ce0 416 }
1c79356b
A
417 }
418
419 if (options & SEMAPHORE_SIGNAL_PREPOST) {
420 semaphore->count++;
421 }
422
423 semaphore_unlock(semaphore);
424 splx(spl_level);
425 return KERN_NOT_WAITING;
426}
427
428/*
429 * Routine: semaphore_signal_thread
430 *
91447636
A
431 * If the specified thread is blocked on the semaphore, it is
432 * woken up. If a NULL thread was supplied, then any one
1c79356b
A
433 * thread is woken up. Otherwise the caller gets KERN_NOT_WAITING
434 * and the semaphore is unchanged.
435 */
436kern_return_t
437semaphore_signal_thread(
0a7de745
A
438 semaphore_t semaphore,
439 thread_t thread)
1c79356b 440{
0a7de745 441 kern_return_t ret;
1c79356b 442
0a7de745 443 if (semaphore == SEMAPHORE_NULL) {
1c79356b 444 return KERN_INVALID_ARGUMENT;
0a7de745 445 }
1c79356b
A
446
447 ret = semaphore_signal_internal(semaphore,
0a7de745
A
448 thread,
449 SEMAPHORE_OPTION_NONE);
1c79356b 450 return ret;
0a7de745 451}
1c79356b
A
452
453/*
454 * Routine: semaphore_signal_thread_trap
455 *
456 * Trap interface to the semaphore_signal_thread function.
457 */
458kern_return_t
459semaphore_signal_thread_trap(
91447636 460 struct semaphore_signal_thread_trap_args *args)
1c79356b 461{
91447636
A
462 mach_port_name_t sema_name = args->signal_name;
463 mach_port_name_t thread_name = args->thread_name;
0a7de745
A
464 semaphore_t semaphore;
465 thread_t thread;
466 kern_return_t kr;
1c79356b 467
0a7de745 468 /*
1c79356b
A
469 * MACH_PORT_NULL is not an error. It means that we want to
470 * select any one thread that is already waiting, but not to
471 * pre-post the semaphore.
472 */
473 if (thread_name != MACH_PORT_NULL) {
cb323159 474 thread = port_name_to_thread(thread_name, PORT_TO_THREAD_NONE);
0a7de745 475 if (thread == THREAD_NULL) {
1c79356b 476 return KERN_INVALID_ARGUMENT;
0a7de745
A
477 }
478 } else {
91447636 479 thread = THREAD_NULL;
0a7de745 480 }
1c79356b
A
481
482 kr = port_name_to_semaphore(sema_name, &semaphore);
91447636
A
483 if (kr == KERN_SUCCESS) {
484 kr = semaphore_signal_internal(semaphore,
0a7de745
A
485 thread,
486 SEMAPHORE_OPTION_NONE);
91447636
A
487 semaphore_dereference(semaphore);
488 }
489 if (thread != THREAD_NULL) {
490 thread_deallocate(thread);
1c79356b 491 }
1c79356b
A
492 return kr;
493}
494
495
496
497/*
498 * Routine: semaphore_signal
499 *
500 * Traditional (in-kernel client and MIG interface) semaphore
501 * signal routine. Most users will access the trap version.
502 *
503 * This interface in not defined to return info about whether
504 * this call found a thread waiting or not. The internal
505 * routines (and future external routines) do. We have to
506 * convert those into plain KERN_SUCCESS returns.
507 */
508kern_return_t
509semaphore_signal(
0a7de745 510 semaphore_t semaphore)
1c79356b 511{
0a7de745 512 kern_return_t kr;
1c79356b 513
0a7de745 514 if (semaphore == SEMAPHORE_NULL) {
1c79356b 515 return KERN_INVALID_ARGUMENT;
0a7de745 516 }
1c79356b
A
517
518 kr = semaphore_signal_internal(semaphore,
0a7de745
A
519 THREAD_NULL,
520 SEMAPHORE_SIGNAL_PREPOST);
521 if (kr == KERN_NOT_WAITING) {
1c79356b 522 return KERN_SUCCESS;
0a7de745 523 }
1c79356b
A
524 return kr;
525}
526
527/*
528 * Routine: semaphore_signal_trap
529 *
530 * Trap interface to the semaphore_signal function.
531 */
532kern_return_t
533semaphore_signal_trap(
91447636 534 struct semaphore_signal_trap_args *args)
1c79356b 535{
91447636 536 mach_port_name_t sema_name = args->signal_name;
2d21ac55 537
0a7de745 538 return semaphore_signal_internal_trap(sema_name);
2d21ac55
A
539}
540
541kern_return_t
542semaphore_signal_internal_trap(mach_port_name_t sema_name)
543{
0a7de745 544 semaphore_t semaphore;
1c79356b
A
545 kern_return_t kr;
546
547 kr = port_name_to_semaphore(sema_name, &semaphore);
91447636 548 if (kr == KERN_SUCCESS) {
0a7de745
A
549 kr = semaphore_signal_internal(semaphore,
550 THREAD_NULL,
551 SEMAPHORE_SIGNAL_PREPOST);
91447636 552 semaphore_dereference(semaphore);
0a7de745 553 if (kr == KERN_NOT_WAITING) {
91447636 554 kr = KERN_SUCCESS;
0a7de745 555 }
1c79356b 556 }
1c79356b
A
557 return kr;
558}
559
560/*
561 * Routine: semaphore_signal_all
562 *
563 * Awakens ALL threads currently blocked on the semaphore.
564 * The semaphore count returns to zero.
565 */
566kern_return_t
567semaphore_signal_all(
0a7de745 568 semaphore_t semaphore)
1c79356b
A
569{
570 kern_return_t kr;
571
0a7de745 572 if (semaphore == SEMAPHORE_NULL) {
1c79356b 573 return KERN_INVALID_ARGUMENT;
0a7de745 574 }
1c79356b
A
575
576 kr = semaphore_signal_internal(semaphore,
0a7de745
A
577 THREAD_NULL,
578 SEMAPHORE_SIGNAL_ALL);
579 if (kr == KERN_NOT_WAITING) {
1c79356b 580 return KERN_SUCCESS;
0a7de745 581 }
1c79356b
A
582 return kr;
583}
584
585/*
586 * Routine: semaphore_signal_all_trap
587 *
588 * Trap interface to the semaphore_signal_all function.
589 */
590kern_return_t
591semaphore_signal_all_trap(
91447636 592 struct semaphore_signal_all_trap_args *args)
1c79356b 593{
91447636 594 mach_port_name_t sema_name = args->signal_name;
0a7de745 595 semaphore_t semaphore;
1c79356b
A
596 kern_return_t kr;
597
598 kr = port_name_to_semaphore(sema_name, &semaphore);
91447636
A
599 if (kr == KERN_SUCCESS) {
600 kr = semaphore_signal_internal(semaphore,
0a7de745
A
601 THREAD_NULL,
602 SEMAPHORE_SIGNAL_ALL);
91447636 603 semaphore_dereference(semaphore);
0a7de745 604 if (kr == KERN_NOT_WAITING) {
91447636 605 kr = KERN_SUCCESS;
0a7de745 606 }
1c79356b 607 }
1c79356b
A
608 return kr;
609}
610
611/*
612 * Routine: semaphore_convert_wait_result
613 *
614 * Generate the return code after a semaphore wait/block. It
615 * takes the wait result as an input and coverts that to an
616 * appropriate result.
617 */
618kern_return_t
619semaphore_convert_wait_result(int wait_result)
620{
621 switch (wait_result) {
622 case THREAD_AWAKENED:
623 return KERN_SUCCESS;
624
625 case THREAD_TIMED_OUT:
626 return KERN_OPERATION_TIMED_OUT;
0a7de745 627
1c79356b
A
628 case THREAD_INTERRUPTED:
629 return KERN_ABORTED;
630
631 case THREAD_RESTART:
632 return KERN_TERMINATED;
633
634 default:
635 panic("semaphore_block\n");
636 return KERN_FAILURE;
637 }
638}
639
640/*
641 * Routine: semaphore_wait_continue
642 *
643 * Common continuation routine after waiting on a semphore.
644 * It returns directly to user space.
645 */
646void
f427ee49 647semaphore_wait_continue(void *arg __unused, wait_result_t wr)
1c79356b
A
648{
649 thread_t self = current_thread();
1c79356b
A
650 void (*caller_cont)(kern_return_t) = self->sth_continuation;
651
652 assert(self->sth_waitsemaphore != SEMAPHORE_NULL);
653 semaphore_dereference(self->sth_waitsemaphore);
0a7de745 654 if (self->sth_signalsemaphore != SEMAPHORE_NULL) {
1c79356b 655 semaphore_dereference(self->sth_signalsemaphore);
0a7de745 656 }
1c79356b 657
f427ee49 658 assert(self->handoff_thread == THREAD_NULL);
1c79356b 659 assert(caller_cont != (void (*)(kern_return_t))0);
f427ee49 660 (*caller_cont)(semaphore_convert_wait_result(wr));
1c79356b
A
661}
662
1c79356b
A
663/*
664 * Routine: semaphore_wait_internal
665 *
666 * Decrements the semaphore count by one. If the count is
667 * negative after the decrement, the calling thread blocks
668 * (possibly at a continuation and/or with a timeout).
669 *
670 * Assumptions:
671 * The reference
672 * A reference is held on the signal semaphore.
673 */
b0d623f7 674static kern_return_t
1c79356b 675semaphore_wait_internal(
0a7de745
A
676 semaphore_t wait_semaphore,
677 semaphore_t signal_semaphore,
678 uint64_t deadline,
679 int option,
680 void (*caller_cont)(kern_return_t))
1c79356b 681{
0a7de745
A
682 int wait_result;
683 spl_t spl_level;
684 kern_return_t kr = KERN_ALREADY_WAITING;
1c79356b
A
685
686 spl_level = splsched();
687 semaphore_lock(wait_semaphore);
f427ee49
A
688 thread_t self = current_thread();
689 thread_t handoff_thread = THREAD_NULL;
690 thread_handoff_option_t handoff_option = THREAD_HANDOFF_NONE;
691 int semaphore_signal_options = SEMAPHORE_SIGNAL_PREPOST;
1c79356b 692
1c79356b
A
693 if (!wait_semaphore->active) {
694 kr = KERN_TERMINATED;
695 } else if (wait_semaphore->count > 0) {
696 wait_semaphore->count--;
697 kr = KERN_SUCCESS;
b0d623f7 698 } else if (option & SEMAPHORE_TIMEOUT_NOBLOCK) {
1c79356b 699 kr = KERN_OPERATION_TIMED_OUT;
55e303ae 700 } else {
1c79356b 701 wait_semaphore->count = -1; /* we don't keep an actual count */
813fb2f6
A
702
703 thread_set_pending_block_hint(self, kThreadWaitSemaphore);
3e170ce0 704 (void)waitq_assert_wait64_locked(
0a7de745
A
705 &wait_semaphore->waitq,
706 SEMAPHORE_EVENT,
707 THREAD_ABORTSAFE,
708 TIMEOUT_URGENCY_USER_NORMAL,
709 deadline, TIMEOUT_NO_LEEWAY,
710 self);
f427ee49
A
711
712 semaphore_signal_options |= SEMAPHORE_THREAD_HANDOFF;
1c79356b
A
713 }
714 semaphore_unlock(wait_semaphore);
715 splx(spl_level);
716
717 /*
718 * wait_semaphore is unlocked so we are free to go ahead and
719 * signal the signal_semaphore (if one was provided).
720 */
721 if (signal_semaphore != SEMAPHORE_NULL) {
722 kern_return_t signal_kr;
723
724 /*
725 * lock the signal semaphore reference we got and signal it.
726 * This will NOT block (we cannot block after having asserted
727 * our intention to wait above).
728 */
729 signal_kr = semaphore_signal_internal(signal_semaphore,
f427ee49 730 THREAD_NULL, semaphore_signal_options);
1c79356b 731
0a7de745 732 if (signal_kr == KERN_NOT_WAITING) {
f427ee49 733 assert(self->handoff_thread == THREAD_NULL);
1c79356b 734 signal_kr = KERN_SUCCESS;
0a7de745
A
735 } else if (signal_kr == KERN_TERMINATED) {
736 /*
1c79356b
A
737 * Uh!Oh! The semaphore we were to signal died.
738 * We have to get ourselves out of the wait in
739 * case we get stuck here forever (it is assumed
740 * that the semaphore we were posting is gating
741 * the decision by someone else to post the
742 * semaphore we are waiting on). People will
743 * discover the other dead semaphore soon enough.
744 * If we got out of the wait cleanly (someone
745 * already posted a wakeup to us) then return that
746 * (most important) result. Otherwise,
747 * return the KERN_TERMINATED status.
748 */
f427ee49 749 assert(self->handoff_thread == THREAD_NULL);
1c79356b
A
750 clear_wait(self, THREAD_INTERRUPTED);
751 kr = semaphore_convert_wait_result(self->wait_result);
0a7de745 752 if (kr == KERN_ABORTED) {
1c79356b 753 kr = KERN_TERMINATED;
0a7de745 754 }
1c79356b
A
755 }
756 }
0a7de745 757
1c79356b
A
758 /*
759 * If we had an error, or we didn't really need to wait we can
760 * return now that we have signalled the signal semaphore.
761 */
0a7de745 762 if (kr != KERN_ALREADY_WAITING) {
f427ee49 763 assert(self->handoff_thread == THREAD_NULL);
1c79356b 764 return kr;
0a7de745 765 }
1c79356b 766
f427ee49
A
767 if (self->handoff_thread) {
768 handoff_thread = self->handoff_thread;
769 self->handoff_thread = THREAD_NULL;
770 handoff_option = THREAD_HANDOFF_SETRUN_NEEDED;
771 }
1c79356b
A
772 /*
773 * Now, we can block. If the caller supplied a continuation
774 * pointer of his own for after the block, block with the
f427ee49 775 * appropriate semaphore continuation. This will gather the
1c79356b
A
776 * semaphore results, release references on the semaphore(s),
777 * and then call the caller's continuation.
778 */
779 if (caller_cont) {
1c79356b
A
780 self->sth_continuation = caller_cont;
781 self->sth_waitsemaphore = wait_semaphore;
782 self->sth_signalsemaphore = signal_semaphore;
f427ee49
A
783
784 thread_handoff_parameter(handoff_thread, semaphore_wait_continue,
785 NULL, handoff_option);
0a7de745 786 } else {
f427ee49 787 wait_result = thread_handoff_deallocate(handoff_thread, handoff_option);
1c79356b
A
788 }
789
f427ee49 790 assert(self->handoff_thread == THREAD_NULL);
0a7de745 791 return semaphore_convert_wait_result(wait_result);
1c79356b
A
792}
793
794
795/*
796 * Routine: semaphore_wait
797 *
798 * Traditional (non-continuation) interface presented to
0a7de745 799 * in-kernel clients to wait on a semaphore.
1c79356b
A
800 */
801kern_return_t
802semaphore_wait(
0a7de745
A
803 semaphore_t semaphore)
804{
805 if (semaphore == SEMAPHORE_NULL) {
1c79356b 806 return KERN_INVALID_ARGUMENT;
0a7de745 807 }
1c79356b 808
0a7de745
A
809 return semaphore_wait_internal(semaphore,
810 SEMAPHORE_NULL,
811 0ULL, SEMAPHORE_OPTION_NONE,
812 (void (*)(kern_return_t))0);
b0d623f7
A
813}
814
815kern_return_t
816semaphore_wait_noblock(
0a7de745
A
817 semaphore_t semaphore)
818{
819 if (semaphore == SEMAPHORE_NULL) {
b0d623f7 820 return KERN_INVALID_ARGUMENT;
0a7de745 821 }
b0d623f7 822
0a7de745
A
823 return semaphore_wait_internal(semaphore,
824 SEMAPHORE_NULL,
825 0ULL, SEMAPHORE_TIMEOUT_NOBLOCK,
826 (void (*)(kern_return_t))0);
b0d623f7
A
827}
828
829kern_return_t
830semaphore_wait_deadline(
0a7de745
A
831 semaphore_t semaphore,
832 uint64_t deadline)
833{
834 if (semaphore == SEMAPHORE_NULL) {
b0d623f7 835 return KERN_INVALID_ARGUMENT;
0a7de745 836 }
b0d623f7 837
0a7de745
A
838 return semaphore_wait_internal(semaphore,
839 SEMAPHORE_NULL,
840 deadline, SEMAPHORE_OPTION_NONE,
841 (void (*)(kern_return_t))0);
1c79356b
A
842}
843
844/*
845 * Trap: semaphore_wait_trap
846 *
847 * Trap version of semaphore wait. Called on behalf of user-level
848 * clients.
849 */
91447636 850
1c79356b
A
851kern_return_t
852semaphore_wait_trap(
91447636
A
853 struct semaphore_wait_trap_args *args)
854{
0a7de745 855 return semaphore_wait_trap_internal(args->wait_name, thread_syscall_return);
91447636
A
856}
857
858
859
860kern_return_t
861semaphore_wait_trap_internal(
0a7de745 862 mach_port_name_t name,
91447636 863 void (*caller_cont)(kern_return_t))
0a7de745
A
864{
865 semaphore_t semaphore;
1c79356b
A
866 kern_return_t kr;
867
868 kr = port_name_to_semaphore(name, &semaphore);
91447636
A
869 if (kr == KERN_SUCCESS) {
870 kr = semaphore_wait_internal(semaphore,
0a7de745
A
871 SEMAPHORE_NULL,
872 0ULL, SEMAPHORE_OPTION_NONE,
873 caller_cont);
91447636
A
874 semaphore_dereference(semaphore);
875 }
1c79356b
A
876 return kr;
877}
878
879/*
880 * Routine: semaphore_timedwait
881 *
882 * Traditional (non-continuation) interface presented to
0a7de745 883 * in-kernel clients to wait on a semaphore with a timeout.
1c79356b
A
884 *
885 * A timeout of {0,0} is considered non-blocking.
886 */
887kern_return_t
888semaphore_timedwait(
0a7de745
A
889 semaphore_t semaphore,
890 mach_timespec_t wait_time)
b0d623f7 891{
0a7de745
A
892 int option = SEMAPHORE_OPTION_NONE;
893 uint64_t deadline = 0;
b0d623f7 894
0a7de745 895 if (semaphore == SEMAPHORE_NULL) {
1c79356b 896 return KERN_INVALID_ARGUMENT;
0a7de745
A
897 }
898
899 if (BAD_MACH_TIMESPEC(&wait_time)) {
1c79356b 900 return KERN_INVALID_VALUE;
0a7de745 901 }
b0d623f7 902
0a7de745 903 if (wait_time.tv_sec == 0 && wait_time.tv_nsec == 0) {
b0d623f7 904 option = SEMAPHORE_TIMEOUT_NOBLOCK;
0a7de745 905 } else {
b0d623f7 906 deadline = semaphore_deadline(wait_time.tv_sec, wait_time.tv_nsec);
0a7de745
A
907 }
908
909 return semaphore_wait_internal(semaphore,
910 SEMAPHORE_NULL,
911 deadline, option,
912 (void (*)(kern_return_t))0);
1c79356b
A
913}
914
915/*
916 * Trap: semaphore_timedwait_trap
917 *
918 * Trap version of a semaphore_timedwait. The timeout parameter
919 * is passed in two distinct parts and re-assembled on this side
920 * of the trap interface (to accomodate calling conventions that
921 * pass structures as pointers instead of inline in registers without
922 * having to add a copyin).
923 *
924 * A timeout of {0,0} is considered non-blocking.
925 */
926kern_return_t
927semaphore_timedwait_trap(
91447636 928 struct semaphore_timedwait_trap_args *args)
0a7de745
A
929{
930 return semaphore_timedwait_trap_internal(args->wait_name, args->sec, args->nsec, thread_syscall_return);
91447636
A
931}
932
933
934kern_return_t
935semaphore_timedwait_trap_internal(
936 mach_port_name_t name,
937 unsigned int sec,
938 clock_res_t nsec,
939 void (*caller_cont)(kern_return_t))
940{
1c79356b
A
941 semaphore_t semaphore;
942 mach_timespec_t wait_time;
943 kern_return_t kr;
944
945 wait_time.tv_sec = sec;
946 wait_time.tv_nsec = nsec;
0a7de745 947 if (BAD_MACH_TIMESPEC(&wait_time)) {
1c79356b 948 return KERN_INVALID_VALUE;
0a7de745
A
949 }
950
1c79356b 951 kr = port_name_to_semaphore(name, &semaphore);
91447636 952 if (kr == KERN_SUCCESS) {
0a7de745
A
953 int option = SEMAPHORE_OPTION_NONE;
954 uint64_t deadline = 0;
b0d623f7 955
0a7de745 956 if (sec == 0 && nsec == 0) {
b0d623f7 957 option = SEMAPHORE_TIMEOUT_NOBLOCK;
0a7de745 958 } else {
b0d623f7 959 deadline = semaphore_deadline(sec, nsec);
0a7de745 960 }
b0d623f7 961
91447636 962 kr = semaphore_wait_internal(semaphore,
0a7de745
A
963 SEMAPHORE_NULL,
964 deadline, option,
965 caller_cont);
91447636
A
966 semaphore_dereference(semaphore);
967 }
1c79356b
A
968 return kr;
969}
970
971/*
972 * Routine: semaphore_wait_signal
973 *
974 * Atomically register a wait on a semaphore and THEN signal
975 * another. This is the in-kernel entry point that does not
976 * block at a continuation and does not free a signal_semaphore
977 * reference.
978 */
979kern_return_t
980semaphore_wait_signal(
0a7de745
A
981 semaphore_t wait_semaphore,
982 semaphore_t signal_semaphore)
1c79356b 983{
0a7de745 984 if (wait_semaphore == SEMAPHORE_NULL) {
1c79356b 985 return KERN_INVALID_ARGUMENT;
0a7de745
A
986 }
987
988 return semaphore_wait_internal(wait_semaphore,
989 signal_semaphore,
990 0ULL, SEMAPHORE_OPTION_NONE,
991 (void (*)(kern_return_t))0);
1c79356b
A
992}
993
994/*
995 * Trap: semaphore_wait_signal_trap
996 *
997 * Atomically register a wait on a semaphore and THEN signal
0a7de745 998 * another. This is the trap version from user space.
1c79356b
A
999 */
1000kern_return_t
1001semaphore_wait_signal_trap(
91447636
A
1002 struct semaphore_wait_signal_trap_args *args)
1003{
0a7de745 1004 return semaphore_wait_signal_trap_internal(args->wait_name, args->signal_name, thread_syscall_return);
91447636
A
1005}
1006
1007kern_return_t
1008semaphore_wait_signal_trap_internal(
1009 mach_port_name_t wait_name,
1010 mach_port_name_t signal_name,
1011 void (*caller_cont)(kern_return_t))
1c79356b
A
1012{
1013 semaphore_t wait_semaphore;
1014 semaphore_t signal_semaphore;
1015 kern_return_t kr;
1016
1017 kr = port_name_to_semaphore(signal_name, &signal_semaphore);
91447636
A
1018 if (kr == KERN_SUCCESS) {
1019 kr = port_name_to_semaphore(wait_name, &wait_semaphore);
1020 if (kr == KERN_SUCCESS) {
1021 kr = semaphore_wait_internal(wait_semaphore,
0a7de745
A
1022 signal_semaphore,
1023 0ULL, SEMAPHORE_OPTION_NONE,
1024 caller_cont);
91447636
A
1025 semaphore_dereference(wait_semaphore);
1026 }
1c79356b 1027 semaphore_dereference(signal_semaphore);
1c79356b 1028 }
1c79356b
A
1029 return kr;
1030}
1031
1032
1033/*
1034 * Routine: semaphore_timedwait_signal
1035 *
1036 * Atomically register a wait on a semaphore and THEN signal
1037 * another. This is the in-kernel entry point that does not
1038 * block at a continuation.
1039 *
1040 * A timeout of {0,0} is considered non-blocking.
1041 */
1042kern_return_t
1043semaphore_timedwait_signal(
0a7de745
A
1044 semaphore_t wait_semaphore,
1045 semaphore_t signal_semaphore,
1046 mach_timespec_t wait_time)
1c79356b 1047{
0a7de745
A
1048 int option = SEMAPHORE_OPTION_NONE;
1049 uint64_t deadline = 0;
b0d623f7 1050
0a7de745 1051 if (wait_semaphore == SEMAPHORE_NULL) {
1c79356b 1052 return KERN_INVALID_ARGUMENT;
0a7de745
A
1053 }
1054
1055 if (BAD_MACH_TIMESPEC(&wait_time)) {
1c79356b 1056 return KERN_INVALID_VALUE;
0a7de745 1057 }
b0d623f7 1058
0a7de745 1059 if (wait_time.tv_sec == 0 && wait_time.tv_nsec == 0) {
b0d623f7 1060 option = SEMAPHORE_TIMEOUT_NOBLOCK;
0a7de745 1061 } else {
b0d623f7 1062 deadline = semaphore_deadline(wait_time.tv_sec, wait_time.tv_nsec);
0a7de745
A
1063 }
1064
1065 return semaphore_wait_internal(wait_semaphore,
1066 signal_semaphore,
1067 deadline, option,
1068 (void (*)(kern_return_t))0);
1c79356b
A
1069}
1070
1071/*
1072 * Trap: semaphore_timedwait_signal_trap
1073 *
1074 * Atomically register a timed wait on a semaphore and THEN signal
0a7de745 1075 * another. This is the trap version from user space.
1c79356b
A
1076 */
1077kern_return_t
1078semaphore_timedwait_signal_trap(
91447636
A
1079 struct semaphore_timedwait_signal_trap_args *args)
1080{
0a7de745 1081 return semaphore_timedwait_signal_trap_internal(args->wait_name, args->signal_name, args->sec, args->nsec, thread_syscall_return);
91447636
A
1082}
1083
1084kern_return_t
1085semaphore_timedwait_signal_trap_internal(
1086 mach_port_name_t wait_name,
1087 mach_port_name_t signal_name,
1088 unsigned int sec,
1089 clock_res_t nsec,
1090 void (*caller_cont)(kern_return_t))
1c79356b
A
1091{
1092 semaphore_t wait_semaphore;
1093 semaphore_t signal_semaphore;
1094 mach_timespec_t wait_time;
1095 kern_return_t kr;
1096
1097 wait_time.tv_sec = sec;
1098 wait_time.tv_nsec = nsec;
0a7de745 1099 if (BAD_MACH_TIMESPEC(&wait_time)) {
1c79356b 1100 return KERN_INVALID_VALUE;
0a7de745
A
1101 }
1102
1c79356b 1103 kr = port_name_to_semaphore(signal_name, &signal_semaphore);
91447636
A
1104 if (kr == KERN_SUCCESS) {
1105 kr = port_name_to_semaphore(wait_name, &wait_semaphore);
1106 if (kr == KERN_SUCCESS) {
0a7de745
A
1107 int option = SEMAPHORE_OPTION_NONE;
1108 uint64_t deadline = 0;
b0d623f7 1109
0a7de745 1110 if (sec == 0 && nsec == 0) {
b0d623f7 1111 option = SEMAPHORE_TIMEOUT_NOBLOCK;
0a7de745 1112 } else {
b0d623f7 1113 deadline = semaphore_deadline(sec, nsec);
0a7de745 1114 }
b0d623f7 1115
91447636 1116 kr = semaphore_wait_internal(wait_semaphore,
0a7de745
A
1117 signal_semaphore,
1118 deadline, option,
1119 caller_cont);
91447636
A
1120 semaphore_dereference(wait_semaphore);
1121 }
1c79356b 1122 semaphore_dereference(signal_semaphore);
1c79356b 1123 }
1c79356b
A
1124 return kr;
1125}
1126
1127
1128/*
1129 * Routine: semaphore_reference
1130 *
1131 * Take out a reference on a semaphore. This keeps the data structure
1132 * in existence (but the semaphore may be deactivated).
1133 */
1134void
1135semaphore_reference(
0a7de745 1136 semaphore_t semaphore)
1c79356b 1137{
d9a64523 1138 os_ref_retain(&semaphore->ref_count);
1c79356b
A
1139}
1140
1141/*
1142 * Routine: semaphore_dereference
1143 *
1144 * Release a reference on a semaphore. If this is the last reference,
1145 * the semaphore data structure is deallocated.
1146 */
1147void
1148semaphore_dereference(
0a7de745 1149 semaphore_t semaphore)
1c79356b 1150{
4bd07ac2
A
1151 uint32_t collisions;
1152 spl_t spl_level;
1153
0a7de745 1154 if (semaphore == NULL) {
3e170ce0 1155 return;
0a7de745 1156 }
3e170ce0 1157
d9a64523 1158 if (os_ref_release(&semaphore->ref_count) > 0) {
3e170ce0 1159 return;
d9a64523 1160 }
3e170ce0
A
1161
1162 /*
1163 * Last ref, clean up the port [if any]
1164 * associated with the semaphore, destroy
1165 * it (if still active) and then free
1166 * the semaphore.
1167 */
1168 ipc_port_t port = semaphore->port;
1169
1170 if (IP_VALID(port)) {
1171 assert(!port->ip_srights);
1172 ipc_port_dealloc_kernel(port);
1173 }
4bd07ac2
A
1174
1175 /*
1176 * Lock the semaphore to lock in the owner task reference.
1177 * Then continue to try to lock the task (inverse order).
1178 */
1179 spl_level = splsched();
1180 semaphore_lock(semaphore);
1181 for (collisions = 0; semaphore->active; collisions++) {
1182 task_t task = semaphore->owner;
1183
1184 assert(task != TASK_NULL);
0a7de745 1185
4bd07ac2
A
1186 if (task_lock_try(task)) {
1187 semaphore_destroy_internal(task, semaphore);
1188 /* semaphore unlocked */
1189 splx(spl_level);
1190 task_unlock(task);
1191 goto out;
1192 }
0a7de745 1193
4bd07ac2
A
1194 /* failed to get out-of-order locks */
1195 semaphore_unlock(semaphore);
1196 splx(spl_level);
1197 mutex_pause(collisions);
1198 spl_level = splsched();
1199 semaphore_lock(semaphore);
1c79356b 1200 }
4bd07ac2
A
1201 semaphore_unlock(semaphore);
1202 splx(spl_level);
1203
0a7de745 1204out:
3e170ce0 1205 zfree(semaphore_zone, semaphore);
1c79356b 1206}
3e170ce0 1207
813fb2f6
A
1208#define WAITQ_TO_SEMA(wq) ((semaphore_t) ((uintptr_t)(wq) - offsetof(struct semaphore, waitq)))
1209void
1210kdp_sema_find_owner(struct waitq * waitq, __assert_only event64_t event, thread_waitinfo_t * waitinfo)
1211{
1212 semaphore_t sem = WAITQ_TO_SEMA(waitq);
1213 assert(event == SEMAPHORE_EVENT);
f427ee49
A
1214
1215 zone_require(semaphore_zone, sem);
3e170ce0 1216
813fb2f6 1217 waitinfo->context = VM_KERNEL_UNSLIDE_OR_PERM(sem->port);
0a7de745 1218 if (sem->owner) {
813fb2f6 1219 waitinfo->owner = pid_from_task(sem->owner);
0a7de745 1220 }
813fb2f6 1221}