1/*
2 * Copyright (c) 2015 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29#ifndef _SYS_ULOCK_H
30#define _SYS_ULOCK_H
31
32#include <mach/mach_port.h>
33#include <sys/cdefs.h>
34#include <stdint.h>
35
36__BEGIN_DECLS
37
38#if PRIVATE
39
40#ifdef XNU_KERNEL_PRIVATE
41extern mach_port_name_t ipc_entry_name_mask(mach_port_name_t name);
42
43static __inline mach_port_name_t
44ulock_owner_value_to_port_name(uint32_t uval)
45{
46 /*
47 * userland uses the least significant bits for flags as these are
48 * never used in the mach port name, and are generally always set by
49 * the ipc_entry code in the kernel. Here we reconstruct a mach port
50 * name that we can use in the kernel.
51 */
52 return ipc_entry_name_mask(name: (mach_port_name_t)uval);
53}
54
55extern int ulock_wake(struct task *task, uint32_t operation, user_addr_t addr, uint64_t wake_value);
56
57#else
58static __inline mach_port_name_t
59ulock_owner_value_to_port_name(uint32_t uval)
60{
61 return uval | 0x3;
62}
63#endif
64
65#ifndef KERNEL
66
67/* timeout is specified in microseconds */
68extern int __ulock_wait(uint32_t operation, void *addr, uint64_t value,
69 uint32_t timeout);
70/* timeout is either a delta specified in nanoseconds or a deadline in
71 * mach_absolute_time units specified together with the ULF_DEADLINE flag */
72extern int __ulock_wait2(uint32_t operation, void *addr, uint64_t value,
73 uint64_t timeout, uint64_t value2);
74extern int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
75
76#endif /* !KERNEL */
77
78/*
79 * operation bits [7, 0] contain the operation code.
80 *
81 * NOTE: make sure to add logic for handling any new
82 * types to kdp_ulock_find_owner()
83 */
84#define UL_COMPARE_AND_WAIT 1
85#define UL_UNFAIR_LOCK 2
86#define UL_COMPARE_AND_WAIT_SHARED 3
87#define UL_UNFAIR_LOCK64_SHARED 4
88#define UL_COMPARE_AND_WAIT64 5
89#define UL_COMPARE_AND_WAIT64_SHARED 6
90/* obsolete names */
91#define UL_OSSPINLOCK UL_COMPARE_AND_WAIT
92#define UL_HANDOFFLOCK UL_UNFAIR_LOCK
93/* These operation code are only implemented in (DEVELOPMENT || DEBUG) kernels */
94#define UL_DEBUG_SIMULATE_COPYIN_FAULT 253
95#define UL_DEBUG_HASH_DUMP_ALL 254
96#define UL_DEBUG_HASH_DUMP_PID 255
97
98/*
99 * operation bits [15, 8] contain the flags for __ulock_wake
100 */
101#define ULF_WAKE_ALL 0x00000100
102#define ULF_WAKE_THREAD 0x00000200
103#define ULF_WAKE_ALLOW_NON_OWNER 0x00000400
104
105/*
106 * operation bits [23, 16] contain the flags for __ulock_wait
107 *
108 * @const ULF_WAIT_WORKQ_DATA_CONTENTION
109 * The waiter is contending on this lock for synchronization around global data.
110 * This causes the workqueue subsystem to not create new threads to offset for
111 * waiters on this lock.
112 *
113 * @const ULF_WAIT_CANCEL_POINT
114 * This wait is a cancelation point
115 *
116 * @const ULF_WAIT_ADAPTIVE_SPIN
117 * Use adaptive spinning when the thread that currently holds the unfair lock
118 * is on core.
119 */
120#define ULF_WAIT_WORKQ_DATA_CONTENTION 0x00010000
121#define ULF_WAIT_CANCEL_POINT 0x00020000
122#define ULF_WAIT_ADAPTIVE_SPIN 0x00040000
123
124/*
125 * operation bits [31, 24] contain the generic flags
126 *
127 * @const ULF_DEADLINE
128 * put timeout - if specified - is a deadline specified in mach absolute
129 * time units
130 */
131#define ULF_NO_ERRNO 0x01000000
132#define ULF_DEADLINE 0x02000000
133
134/*
135 * masks
136 */
137#define UL_OPCODE_MASK 0x000000FF
138#define UL_FLAGS_MASK 0xFFFFFF00
139#define ULF_GENERIC_MASK 0xFFFF0000
140
141#define ULF_WAIT_MASK (ULF_NO_ERRNO | ULF_DEADLINE | \
142 ULF_WAIT_WORKQ_DATA_CONTENTION | \
143 ULF_WAIT_CANCEL_POINT | ULF_WAIT_ADAPTIVE_SPIN)
144
145#define ULF_WAKE_MASK (ULF_NO_ERRNO | \
146 ULF_WAKE_ALL | \
147 ULF_WAKE_THREAD | \
148 ULF_WAKE_ALLOW_NON_OWNER)
149
150#endif /* PRIVATE */
151
152__END_DECLS
153
154#endif
155