]> git.saurik.com Git - apple/libpthread.git/blob - kern/kern_internal.h
libpthread-218.30.1.tar.gz
[apple/libpthread.git] / kern / kern_internal.h
1 /*
2 * Copyright (c) 2000-2003 Apple Computer, 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_PTHREAD_INTERNAL_H_
30 #define _SYS_PTHREAD_INTERNAL_H_
31
32 #ifdef KERNEL
33 #include <kern/thread_call.h>
34 #include <sys/pthread_shims.h>
35 #include <sys/queue.h>
36 #endif
37
38 #include "kern/synch_internal.h"
39 #include "kern/workqueue_internal.h"
40 #include "kern/kern_trace.h"
41 #include "pthread/qos.h"
42 #include "private/qos_private.h"
43
44 /* pthread userspace SPI feature checking, these constants are returned from bsdthread_register,
45 * as a bitmask, to inform userspace of the supported feature set. Old releases of OS X return
46 * from this call either zero or -1, allowing us to return a positive number for feature bits.
47 */
48 #define PTHREAD_FEATURE_DISPATCHFUNC 0x01 /* same as WQOPS_QUEUE_NEWSPISUPP, checks for dispatch function support */
49 #define PTHREAD_FEATURE_FINEPRIO 0x02 /* are fine grained prioirities available */
50 #define PTHREAD_FEATURE_BSDTHREADCTL 0x04 /* is the bsdthread_ctl syscall available */
51 #define PTHREAD_FEATURE_SETSELF 0x08 /* is the BSDTHREAD_CTL_SET_SELF command of bsdthread_ctl available */
52 #define PTHREAD_FEATURE_QOS_MAINTENANCE 0x10 /* is QOS_CLASS_MAINTENANCE available */
53 #define PTHREAD_FEATURE_RESERVED 0x20 /* burnt, shipped in OSX 10.11 & iOS 9 with partial kevent delivery support */
54 #define PTHREAD_FEATURE_KEVENT 0x40 /* supports direct kevent delivery */
55 #define PTHREAD_FEATURE_QOS_DEFAULT 0x40000000 /* the kernel supports QOS_CLASS_DEFAULT */
56
57 /* pthread bsdthread_ctl sysctl commands */
58 #define BSDTHREAD_CTL_SET_QOS 0x10 /* bsdthread_ctl(BSDTHREAD_CTL_SET_QOS, thread_port, tsd_entry_addr, 0) */
59 #define BSDTHREAD_CTL_GET_QOS 0x20 /* bsdthread_ctl(BSDTHREAD_CTL_GET_QOS, thread_port, 0, 0) */
60 #define BSDTHREAD_CTL_QOS_OVERRIDE_START 0x40 /* bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_START, thread_port, priority, 0) */
61 #define BSDTHREAD_CTL_QOS_OVERRIDE_END 0x80 /* bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_END, thread_port, 0, 0) */
62 #define BSDTHREAD_CTL_SET_SELF 0x100 /* bsdthread_ctl(BSDTHREAD_CTL_SET_SELF, priority, voucher, flags) */
63 #define BSDTHREAD_CTL_QOS_OVERRIDE_RESET 0x200 /* bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_RESET, 0, 0, 0) */
64 #define BSDTHREAD_CTL_QOS_OVERRIDE_DISPATCH 0x400 /* bsdthread_ctl(BSDTHREAD_CTL_QOS_OVERRIDE_DISPATCH, thread_port, priority, 0) */
65 #define BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_ADD 0x401 /* bsdthread_ctl(BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_ADD, thread_port, priority, resource) */
66 #define BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_RESET 0x402 /* bsdthread_ctl(BSDTHREAD_CTL_QOS_DISPATCH_ASYNCHRONOUS_OVERRIDE_RESET, 0|1 (?reset_all), resource, 0) */
67
68 /* qos_class_t is mapped into one of these bits in the bitfield, this mapping now exists here because
69 * libdispatch requires the QoS class mask of the pthread_priority_t to be a bitfield.
70 */
71 #define __PTHREAD_PRIORITY_CBIT_USER_INTERACTIVE 0x20
72 #define __PTHREAD_PRIORITY_CBIT_USER_INITIATED 0x10
73 #define __PTHREAD_PRIORITY_CBIT_DEFAULT 0x8
74 #define __PTHREAD_PRIORITY_CBIT_UTILITY 0x4
75 #define __PTHREAD_PRIORITY_CBIT_BACKGROUND 0x2
76 #define __PTHREAD_PRIORITY_CBIT_MAINTENANCE 0x1
77 #define __PTHREAD_PRIORITY_CBIT_UNSPECIFIED 0x0
78
79 /* Added support for QOS_CLASS_MAINTENANCE */
80 static inline pthread_priority_t
81 _pthread_priority_make_newest(qos_class_t qc, int rel, unsigned long flags)
82 {
83 pthread_priority_t cls;
84 switch (qc) {
85 case QOS_CLASS_USER_INTERACTIVE: cls = __PTHREAD_PRIORITY_CBIT_USER_INTERACTIVE; break;
86 case QOS_CLASS_USER_INITIATED: cls = __PTHREAD_PRIORITY_CBIT_USER_INITIATED; break;
87 case QOS_CLASS_DEFAULT: cls = __PTHREAD_PRIORITY_CBIT_DEFAULT; break;
88 case QOS_CLASS_UTILITY: cls = __PTHREAD_PRIORITY_CBIT_UTILITY; break;
89 case QOS_CLASS_BACKGROUND: cls = __PTHREAD_PRIORITY_CBIT_BACKGROUND; break;
90 case QOS_CLASS_MAINTENANCE: cls = __PTHREAD_PRIORITY_CBIT_MAINTENANCE; break;
91 case QOS_CLASS_UNSPECIFIED:
92 default:
93 cls = __PTHREAD_PRIORITY_CBIT_UNSPECIFIED;
94 rel = 1; // results in priority bits == 0 <rdar://problem/16184900>
95 break;
96 }
97
98 pthread_priority_t p =
99 (flags & _PTHREAD_PRIORITY_FLAGS_MASK) |
100 ((cls << _PTHREAD_PRIORITY_QOS_CLASS_SHIFT) & _PTHREAD_PRIORITY_QOS_CLASS_MASK) |
101 (((uint8_t)rel - 1) & _PTHREAD_PRIORITY_PRIORITY_MASK);
102
103 return p;
104 }
105
106 /* Added support for QOS_CLASS_LEGACY and QOS_CLASS_INHERIT */
107 static inline pthread_priority_t
108 _pthread_priority_make_version2(qos_class_t qc, int rel, unsigned long flags)
109 {
110 pthread_priority_t cls;
111 switch (qc) {
112 case QOS_CLASS_USER_INTERACTIVE: cls = __PTHREAD_PRIORITY_CBIT_USER_INTERACTIVE; break;
113 case QOS_CLASS_USER_INITIATED: cls = __PTHREAD_PRIORITY_CBIT_USER_INITIATED; break;
114 case QOS_CLASS_DEFAULT: cls = __PTHREAD_PRIORITY_CBIT_DEFAULT; break;
115 case QOS_CLASS_UTILITY: cls = __PTHREAD_PRIORITY_CBIT_UTILITY; break;
116 case QOS_CLASS_BACKGROUND: cls = __PTHREAD_PRIORITY_CBIT_BACKGROUND; break;
117 case QOS_CLASS_UNSPECIFIED:
118 default:
119 cls = __PTHREAD_PRIORITY_CBIT_UNSPECIFIED;
120 rel = 1; // results in priority bits == 0 <rdar://problem/16184900>
121 break;
122 }
123
124 /*
125 * __PTHREAD_PRIORITY_CBIT_MAINTENANCE was defined as the 0th bit by shifting all the
126 * existing bits to the left by one. So for backward compatiblity for kernels that does
127 * not support QOS_CLASS_MAINTENANCE, we have to make it up by shifting the cls bit to
128 * right by one.
129 */
130 cls >>= 1;
131
132 pthread_priority_t p =
133 (flags & _PTHREAD_PRIORITY_FLAGS_MASK) |
134 ((cls << _PTHREAD_PRIORITY_QOS_CLASS_SHIFT) & _PTHREAD_PRIORITY_QOS_CLASS_MASK) |
135 (((uint8_t)rel - 1) & _PTHREAD_PRIORITY_PRIORITY_MASK);
136
137 return p;
138 }
139
140 /* QOS_CLASS_MAINTENANCE is supported */
141 static inline qos_class_t
142 _pthread_priority_get_qos_newest(pthread_priority_t priority)
143 {
144 qos_class_t qc;
145 switch ((priority & _PTHREAD_PRIORITY_QOS_CLASS_MASK) >> _PTHREAD_PRIORITY_QOS_CLASS_SHIFT) {
146 case __PTHREAD_PRIORITY_CBIT_USER_INTERACTIVE: qc = QOS_CLASS_USER_INTERACTIVE; break;
147 case __PTHREAD_PRIORITY_CBIT_USER_INITIATED: qc = QOS_CLASS_USER_INITIATED; break;
148 case __PTHREAD_PRIORITY_CBIT_DEFAULT: qc = QOS_CLASS_DEFAULT; break;
149 case __PTHREAD_PRIORITY_CBIT_UTILITY: qc = QOS_CLASS_UTILITY; break;
150 case __PTHREAD_PRIORITY_CBIT_BACKGROUND: qc = QOS_CLASS_BACKGROUND; break;
151 case __PTHREAD_PRIORITY_CBIT_MAINTENANCE: qc = QOS_CLASS_MAINTENANCE; break;
152 case __PTHREAD_PRIORITY_CBIT_UNSPECIFIED:
153 default: qc = QOS_CLASS_UNSPECIFIED; break;
154 }
155 return qc;
156 }
157
158 /* QOS_CLASS_MAINTENANCE is not supported */
159 static inline qos_class_t
160 _pthread_priority_get_qos_version2(pthread_priority_t priority)
161 {
162 qos_class_t qc;
163 pthread_priority_t cls;
164
165 cls = (priority & _PTHREAD_PRIORITY_QOS_CLASS_MASK) >> _PTHREAD_PRIORITY_QOS_CLASS_SHIFT;
166
167 /*
168 * __PTHREAD_PRIORITY_CBIT_MAINTENANCE was defined as the 0th bit by shifting all the
169 * existing bits to the left by one. So for backward compatiblity for kernels that does
170 * not support QOS_CLASS_MAINTENANCE, pthread_priority_make() shifted the cls bit to the
171 * right by one. Therefore we have to shift it back during decoding the priority bit.
172 */
173 cls <<= 1;
174
175 switch (cls) {
176 case __PTHREAD_PRIORITY_CBIT_USER_INTERACTIVE: qc = QOS_CLASS_USER_INTERACTIVE; break;
177 case __PTHREAD_PRIORITY_CBIT_USER_INITIATED: qc = QOS_CLASS_USER_INITIATED; break;
178 case __PTHREAD_PRIORITY_CBIT_DEFAULT: qc = QOS_CLASS_DEFAULT; break;
179 case __PTHREAD_PRIORITY_CBIT_UTILITY: qc = QOS_CLASS_UTILITY; break;
180 case __PTHREAD_PRIORITY_CBIT_BACKGROUND: qc = QOS_CLASS_BACKGROUND; break;
181 case __PTHREAD_PRIORITY_CBIT_UNSPECIFIED:
182 default: qc = QOS_CLASS_UNSPECIFIED; break;
183 }
184 return qc;
185 }
186
187 #define _pthread_priority_get_relpri(priority) \
188 ((int8_t)((priority & _PTHREAD_PRIORITY_PRIORITY_MASK) >> _PTHREAD_PRIORITY_PRIORITY_SHIFT) + 1)
189
190 #define _pthread_priority_get_flags(priority) \
191 (priority & _PTHREAD_PRIORITY_FLAGS_MASK)
192
193 #define _pthread_priority_split_newest(priority, qos, relpri) \
194 ({ qos = _pthread_priority_get_qos_newest(priority); \
195 relpri = (qos == QOS_CLASS_UNSPECIFIED) ? 0 : \
196 _pthread_priority_get_relpri(priority); \
197 })
198
199 #define _pthread_priority_split_version2(priority, qos, relpri) \
200 ({ qos = _pthread_priority_get_qos_version2(priority); \
201 relpri = (qos == QOS_CLASS_UNSPECIFIED) ? 0 : \
202 _pthread_priority_get_relpri(priority); \
203 })
204
205 /* <rdar://problem/15969976> Required for backward compatibility on older kernels. */
206 #define _pthread_priority_make_version1(qos, relpri, flags) \
207 (((flags >> 15) & 0xffff0000) | \
208 ((qos << 8) & 0x0000ff00) | \
209 (((uint8_t)relpri - 1) & 0x000000ff))
210
211 /* userspace <-> kernel registration struct, for passing data to/from the kext during main thread init. */
212 struct _pthread_registration_data {
213 /*
214 * version == sizeof(struct _pthread_registration_data)
215 *
216 * The structure can only grow, so we use its size as the version.
217 * Userspace initializes this to the size of its structure and the kext
218 * will copy out the version that was actually consumed.
219 *
220 * n.b. you must make sure the size of this structure isn't LP64-dependent
221 */
222 uint64_t version;
223
224 uint64_t dispatch_queue_offset; /* copy-in */
225 uint64_t /* pthread_priority_t */ main_qos; /* copy-out */
226 uint32_t tsd_offset; /* copy-in */
227 } __attribute__ ((packed));
228
229 #ifdef KERNEL
230
231 /* The set of features, from the feature bits above, that we support. */
232 #define PTHREAD_FEATURE_SUPPORTED ( \
233 PTHREAD_FEATURE_DISPATCHFUNC | \
234 PTHREAD_FEATURE_FINEPRIO | \
235 PTHREAD_FEATURE_BSDTHREADCTL | \
236 PTHREAD_FEATURE_SETSELF | \
237 PTHREAD_FEATURE_QOS_MAINTENANCE | \
238 PTHREAD_FEATURE_QOS_DEFAULT | \
239 PTHREAD_FEATURE_KEVENT )
240
241 extern pthread_callbacks_t pthread_kern;
242
243 struct ksyn_waitq_element {
244 TAILQ_ENTRY(ksyn_waitq_element) kwe_list; /* link to other list members */
245 void * kwe_kwqqueue; /* queue blocked on */
246 uint32_t kwe_state; /* state */
247 uint32_t kwe_lockseq; /* the sequence of the entry */
248 uint32_t kwe_count; /* upper bound on number of matches still pending */
249 uint32_t kwe_psynchretval; /* thread retval */
250 void *kwe_uth; /* uthread */
251 uint64_t kwe_tid; /* tid of waiter */
252 };
253 typedef struct ksyn_waitq_element * ksyn_waitq_element_t;
254
255 pthread_priority_t thread_qos_get_pthread_priority(int qos) __attribute__((const));
256 int thread_qos_get_class_index(int qos) __attribute__((const));
257 int pthread_priority_get_thread_qos(pthread_priority_t priority) __attribute__((const));
258 int pthread_priority_get_class_index(pthread_priority_t priority) __attribute__((const));
259 pthread_priority_t class_index_get_pthread_priority(int index) __attribute__((const));
260 int class_index_get_thread_qos(int index) __attribute__((const));
261 int qos_class_get_class_index(int qos) __attribute__((const));
262
263 #define PTH_DEFAULT_STACKSIZE 512*1024
264 #define MAX_PTHREAD_SIZE 64*1024
265
266 /* exported from the kernel but not present in any headers. */
267 extern thread_t port_name_to_thread(mach_port_name_t port_name);
268
269 /* function declarations for pthread_kext.c */
270 void pthread_init(void);
271 void psynch_zoneinit(void);
272 void _pth_proc_hashinit(proc_t p);
273 void _pth_proc_hashdelete(proc_t p);
274 void pth_global_hashinit(void);
275 void psynch_wq_cleanup(void*, void*);
276
277 void _pthread_init(void);
278 int _fill_procworkqueue(proc_t p, struct proc_workqueueinfo * pwqinfo);
279 uint32_t _get_pwq_state_kdp(proc_t p);
280 void _workqueue_exit(struct proc *p);
281 void _workqueue_mark_exiting(struct proc *p);
282 void _workqueue_thread_yielded(void);
283 sched_call_t _workqueue_get_sched_callback(void);
284
285 int _bsdthread_create(struct proc *p, user_addr_t user_func, user_addr_t user_funcarg, user_addr_t user_stack, user_addr_t user_pthread, uint32_t flags, user_addr_t *retval);
286 int _bsdthread_register(struct proc *p, user_addr_t threadstart, user_addr_t wqthread, int pthsize, user_addr_t dummy_value, user_addr_t targetconc_ptr, uint64_t dispatchqueue_offset, int32_t *retval);
287 int _bsdthread_terminate(struct proc *p, user_addr_t stackaddr, size_t size, uint32_t kthport, uint32_t sem, int32_t *retval);
288 int _bsdthread_ctl_set_qos(struct proc *p, user_addr_t cmd, mach_port_name_t kport, user_addr_t tsd_priority_addr, user_addr_t arg3, int *retval);
289 int _bsdthread_ctl_set_self(struct proc *p, user_addr_t cmd, pthread_priority_t priority, mach_port_name_t voucher, _pthread_set_flags_t flags, int *retval);
290 int _bsdthread_ctl_qos_override_start(struct proc *p, user_addr_t cmd, mach_port_name_t kport, pthread_priority_t priority, user_addr_t resource, int *retval);
291 int _bsdthread_ctl_qos_override_end(struct proc *p, user_addr_t cmd, mach_port_name_t kport, user_addr_t resource, user_addr_t arg3, int *retval);
292 int _bsdthread_ctl_qos_override_dispatch(struct proc __unused *p, user_addr_t __unused cmd, mach_port_name_t kport, pthread_priority_t priority, user_addr_t arg3, int __unused *retval);
293 int _bsdthread_ctl_qos_override_reset(struct proc __unused *p, user_addr_t __unused cmd, user_addr_t arg1, user_addr_t arg2, user_addr_t arg3, int __unused *retval);
294 int _bsdthread_ctl_qos_dispatch_asynchronous_override_add(struct proc __unused *p, user_addr_t __unused cmd, mach_port_name_t kport, pthread_priority_t priority, user_addr_t resource, int __unused *retval);
295 int _bsdthread_ctl_qos_dispatch_asynchronous_override_reset(struct proc __unused *p, user_addr_t __unused cmd, int reset_all, user_addr_t resource, user_addr_t arg3, int __unused *retval);
296 int _bsdthread_ctl(struct proc *p, user_addr_t cmd, user_addr_t arg1, user_addr_t arg2, user_addr_t arg3, int *retval);
297 int _thread_selfid(__unused struct proc *p, uint64_t *retval);
298 int _workq_kernreturn(struct proc *p, int options, user_addr_t item, int arg2, int arg3, int32_t *retval);
299 int _workq_open(struct proc *p, int32_t *retval);
300
301 int _psynch_mutexwait(proc_t p, user_addr_t mutex, uint32_t mgen, uint32_t ugen, uint64_t tid, uint32_t flags, uint32_t * retval);
302 int _psynch_mutexdrop(proc_t p, user_addr_t mutex, uint32_t mgen, uint32_t ugen, uint64_t tid, uint32_t flags, uint32_t * retval);
303 int _psynch_cvbroad(proc_t p, user_addr_t cv, uint64_t cvlsgen, uint64_t cvudgen, uint32_t flags, user_addr_t mutex, uint64_t mugen, uint64_t tid, uint32_t *retval);
304 int _psynch_cvsignal(proc_t p, user_addr_t cv, uint64_t cvlsgen, uint32_t cvugen, int thread_port, user_addr_t mutex, uint64_t mugen, uint64_t tid, uint32_t flags, uint32_t * retval);
305 int _psynch_cvwait(proc_t p, user_addr_t cv, uint64_t cvlsgen, uint32_t cvugen, user_addr_t mutex, uint64_t mugen, uint32_t flags, int64_t sec, uint32_t nsec, uint32_t * retval);
306 int _psynch_cvclrprepost(proc_t p, user_addr_t cv, uint32_t cvgen, uint32_t cvugen, uint32_t cvsgen, uint32_t prepocnt, uint32_t preposeq, uint32_t flags, int *retval);
307 int _psynch_rw_longrdlock(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t * retval);
308 int _psynch_rw_rdlock(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
309 int _psynch_rw_unlock(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
310 int _psynch_rw_wrlock(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
311 int _psynch_rw_yieldwrlock(proc_t p, user_addr_t rwlock, uint32_t lgenval, uint32_t ugenval, uint32_t rw_wc, int flags, uint32_t *retval);
312
313 extern lck_grp_attr_t *pthread_lck_grp_attr;
314 extern lck_grp_t *pthread_lck_grp;
315 extern lck_attr_t *pthread_lck_attr;
316 extern lck_mtx_t *pthread_list_mlock;
317 extern thread_call_t psynch_thcall;
318
319 struct uthread* current_uthread(void);
320
321 // Call for the kernel's kevent system to request threads. A list of QoS/event
322 // counts should be provided, sorted by flags and then QoS class. If the
323 // identity of the thread to handle the request is known, it will be returned.
324 // If a new thread must be created, NULL will be returned.
325 thread_t _workq_reqthreads(struct proc *p, int requests_count,
326 workq_reqthreads_req_t requests);
327
328 // Resolve a pthread_priority_t to a QoS/relative pri
329 integer_t _thread_qos_from_pthread_priority(unsigned long pri, unsigned long *flags);
330 // Clear out extraneous flags/pri info for putting in voucher
331 pthread_priority_t _pthread_priority_canonicalize(pthread_priority_t pri, boolean_t for_propagation);
332
333 #endif // KERNEL
334
335 #endif /* _SYS_PTHREAD_INTERNAL_H_ */
336