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