xref: /xnu-10063.101.15/bsd/sys/ulock.h (revision 94d3b452840153a99b38a3a9659680b2a006908e)
1*94d3b452SApple OSS Distributions /*
2*94d3b452SApple OSS Distributions  * Copyright (c) 2015 Apple Inc. All rights reserved.
3*94d3b452SApple OSS Distributions  *
4*94d3b452SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*94d3b452SApple OSS Distributions  *
6*94d3b452SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*94d3b452SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*94d3b452SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*94d3b452SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*94d3b452SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*94d3b452SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*94d3b452SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*94d3b452SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*94d3b452SApple OSS Distributions  *
15*94d3b452SApple OSS Distributions  * Please obtain a copy of the License at
16*94d3b452SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*94d3b452SApple OSS Distributions  *
18*94d3b452SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*94d3b452SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*94d3b452SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*94d3b452SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*94d3b452SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*94d3b452SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*94d3b452SApple OSS Distributions  * limitations under the License.
25*94d3b452SApple OSS Distributions  *
26*94d3b452SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*94d3b452SApple OSS Distributions  */
28*94d3b452SApple OSS Distributions 
29*94d3b452SApple OSS Distributions #ifndef _SYS_ULOCK_H
30*94d3b452SApple OSS Distributions #define _SYS_ULOCK_H
31*94d3b452SApple OSS Distributions 
32*94d3b452SApple OSS Distributions #include <mach/mach_port.h>
33*94d3b452SApple OSS Distributions #include <sys/cdefs.h>
34*94d3b452SApple OSS Distributions #include <stdint.h>
35*94d3b452SApple OSS Distributions 
36*94d3b452SApple OSS Distributions __BEGIN_DECLS
37*94d3b452SApple OSS Distributions 
38*94d3b452SApple OSS Distributions #if PRIVATE
39*94d3b452SApple OSS Distributions 
40*94d3b452SApple OSS Distributions #ifdef XNU_KERNEL_PRIVATE
41*94d3b452SApple OSS Distributions extern mach_port_name_t ipc_entry_name_mask(mach_port_name_t name);
42*94d3b452SApple OSS Distributions 
43*94d3b452SApple OSS Distributions static __inline mach_port_name_t
ulock_owner_value_to_port_name(uint32_t uval)44*94d3b452SApple OSS Distributions ulock_owner_value_to_port_name(uint32_t uval)
45*94d3b452SApple OSS Distributions {
46*94d3b452SApple OSS Distributions 	/*
47*94d3b452SApple OSS Distributions 	 * userland uses the least significant bits for flags as these are
48*94d3b452SApple OSS Distributions 	 * never used in the mach port name, and are generally always set by
49*94d3b452SApple OSS Distributions 	 * the ipc_entry code in the kernel. Here we reconstruct a mach port
50*94d3b452SApple OSS Distributions 	 * name that we can use in the kernel.
51*94d3b452SApple OSS Distributions 	 */
52*94d3b452SApple OSS Distributions 	return ipc_entry_name_mask((mach_port_name_t)uval);
53*94d3b452SApple OSS Distributions }
54*94d3b452SApple OSS Distributions 
55*94d3b452SApple OSS Distributions extern int ulock_wake(struct task *task, uint32_t operation, user_addr_t addr, uint64_t wake_value);
56*94d3b452SApple OSS Distributions 
57*94d3b452SApple OSS Distributions #else
58*94d3b452SApple OSS Distributions static __inline mach_port_name_t
59*94d3b452SApple OSS Distributions ulock_owner_value_to_port_name(uint32_t uval)
60*94d3b452SApple OSS Distributions {
61*94d3b452SApple OSS Distributions 	return uval | 0x3;
62*94d3b452SApple OSS Distributions }
63*94d3b452SApple OSS Distributions #endif
64*94d3b452SApple OSS Distributions 
65*94d3b452SApple OSS Distributions #ifndef KERNEL
66*94d3b452SApple OSS Distributions 
67*94d3b452SApple OSS Distributions /* timeout is specified in microseconds */
68*94d3b452SApple OSS Distributions extern int __ulock_wait(uint32_t operation, void *addr, uint64_t value,
69*94d3b452SApple OSS Distributions     uint32_t timeout);
70*94d3b452SApple OSS Distributions /* timeout is either a delta specified in nanoseconds or a deadline in
71*94d3b452SApple OSS Distributions  * mach_absolute_time units specified together with the ULF_DEADLINE flag */
72*94d3b452SApple OSS Distributions extern int __ulock_wait2(uint32_t operation, void *addr, uint64_t value,
73*94d3b452SApple OSS Distributions     uint64_t timeout, uint64_t value2);
74*94d3b452SApple OSS Distributions extern int __ulock_wake(uint32_t operation, void *addr, uint64_t wake_value);
75*94d3b452SApple OSS Distributions 
76*94d3b452SApple OSS Distributions #endif /* !KERNEL */
77*94d3b452SApple OSS Distributions 
78*94d3b452SApple OSS Distributions /*
79*94d3b452SApple OSS Distributions  * operation bits [7, 0] contain the operation code.
80*94d3b452SApple OSS Distributions  *
81*94d3b452SApple OSS Distributions  * NOTE: make sure to add logic for handling any new
82*94d3b452SApple OSS Distributions  *       types to kdp_ulock_find_owner()
83*94d3b452SApple OSS Distributions  */
84*94d3b452SApple OSS Distributions #define UL_COMPARE_AND_WAIT             1
85*94d3b452SApple OSS Distributions #define UL_UNFAIR_LOCK                  2
86*94d3b452SApple OSS Distributions #define UL_COMPARE_AND_WAIT_SHARED      3
87*94d3b452SApple OSS Distributions #define UL_UNFAIR_LOCK64_SHARED         4
88*94d3b452SApple OSS Distributions #define UL_COMPARE_AND_WAIT64           5
89*94d3b452SApple OSS Distributions #define UL_COMPARE_AND_WAIT64_SHARED    6
90*94d3b452SApple OSS Distributions /* obsolete names */
91*94d3b452SApple OSS Distributions #define UL_OSSPINLOCK                   UL_COMPARE_AND_WAIT
92*94d3b452SApple OSS Distributions #define UL_HANDOFFLOCK                  UL_UNFAIR_LOCK
93*94d3b452SApple OSS Distributions /* These operation code are only implemented in (DEVELOPMENT || DEBUG) kernels */
94*94d3b452SApple OSS Distributions #define UL_DEBUG_SIMULATE_COPYIN_FAULT  253
95*94d3b452SApple OSS Distributions #define UL_DEBUG_HASH_DUMP_ALL          254
96*94d3b452SApple OSS Distributions #define UL_DEBUG_HASH_DUMP_PID          255
97*94d3b452SApple OSS Distributions 
98*94d3b452SApple OSS Distributions /*
99*94d3b452SApple OSS Distributions  * operation bits [15, 8] contain the flags for __ulock_wake
100*94d3b452SApple OSS Distributions  */
101*94d3b452SApple OSS Distributions #define ULF_WAKE_ALL                    0x00000100
102*94d3b452SApple OSS Distributions #define ULF_WAKE_THREAD                 0x00000200
103*94d3b452SApple OSS Distributions #define ULF_WAKE_ALLOW_NON_OWNER        0x00000400
104*94d3b452SApple OSS Distributions 
105*94d3b452SApple OSS Distributions /*
106*94d3b452SApple OSS Distributions  * operation bits [23, 16] contain the flags for __ulock_wait
107*94d3b452SApple OSS Distributions  *
108*94d3b452SApple OSS Distributions  * @const ULF_WAIT_WORKQ_DATA_CONTENTION
109*94d3b452SApple OSS Distributions  * The waiter is contending on this lock for synchronization around global data.
110*94d3b452SApple OSS Distributions  * This causes the workqueue subsystem to not create new threads to offset for
111*94d3b452SApple OSS Distributions  * waiters on this lock.
112*94d3b452SApple OSS Distributions  *
113*94d3b452SApple OSS Distributions  * @const ULF_WAIT_CANCEL_POINT
114*94d3b452SApple OSS Distributions  * This wait is a cancelation point
115*94d3b452SApple OSS Distributions  *
116*94d3b452SApple OSS Distributions  * @const ULF_WAIT_ADAPTIVE_SPIN
117*94d3b452SApple OSS Distributions  * Use adaptive spinning when the thread that currently holds the unfair lock
118*94d3b452SApple OSS Distributions  * is on core.
119*94d3b452SApple OSS Distributions  */
120*94d3b452SApple OSS Distributions #define ULF_WAIT_WORKQ_DATA_CONTENTION  0x00010000
121*94d3b452SApple OSS Distributions #define ULF_WAIT_CANCEL_POINT           0x00020000
122*94d3b452SApple OSS Distributions #define ULF_WAIT_ADAPTIVE_SPIN          0x00040000
123*94d3b452SApple OSS Distributions 
124*94d3b452SApple OSS Distributions /*
125*94d3b452SApple OSS Distributions  * operation bits [31, 24] contain the generic flags
126*94d3b452SApple OSS Distributions  *
127*94d3b452SApple OSS Distributions  * @const ULF_DEADLINE
128*94d3b452SApple OSS Distributions  * put timeout - if specified - is a deadline specified in mach absolute
129*94d3b452SApple OSS Distributions  * time units
130*94d3b452SApple OSS Distributions  */
131*94d3b452SApple OSS Distributions #define ULF_NO_ERRNO                    0x01000000
132*94d3b452SApple OSS Distributions #define ULF_DEADLINE                    0x02000000
133*94d3b452SApple OSS Distributions 
134*94d3b452SApple OSS Distributions /*
135*94d3b452SApple OSS Distributions  * masks
136*94d3b452SApple OSS Distributions  */
137*94d3b452SApple OSS Distributions #define UL_OPCODE_MASK          0x000000FF
138*94d3b452SApple OSS Distributions #define UL_FLAGS_MASK           0xFFFFFF00
139*94d3b452SApple OSS Distributions #define ULF_GENERIC_MASK        0xFFFF0000
140*94d3b452SApple OSS Distributions 
141*94d3b452SApple OSS Distributions #define ULF_WAIT_MASK           (ULF_NO_ERRNO | ULF_DEADLINE | \
142*94d3b452SApple OSS Distributions 	                         ULF_WAIT_WORKQ_DATA_CONTENTION | \
143*94d3b452SApple OSS Distributions 	                         ULF_WAIT_CANCEL_POINT | ULF_WAIT_ADAPTIVE_SPIN)
144*94d3b452SApple OSS Distributions 
145*94d3b452SApple OSS Distributions #define ULF_WAKE_MASK           (ULF_NO_ERRNO | \
146*94d3b452SApple OSS Distributions 	                         ULF_WAKE_ALL | \
147*94d3b452SApple OSS Distributions 	                         ULF_WAKE_THREAD | \
148*94d3b452SApple OSS Distributions 	                         ULF_WAKE_ALLOW_NON_OWNER)
149*94d3b452SApple OSS Distributions 
150*94d3b452SApple OSS Distributions #endif /* PRIVATE */
151*94d3b452SApple OSS Distributions 
152*94d3b452SApple OSS Distributions __END_DECLS
153*94d3b452SApple OSS Distributions 
154*94d3b452SApple OSS Distributions #endif
155