]> git.saurik.com Git - apple/libpthread.git/blob - src/pthread.c
8eca496e496194d7436954bfbc69798d8a9631da
[apple/libpthread.git] / src / pthread.c
1 /*
2 * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23 /*
24 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
25 * All Rights Reserved
26 *
27 * Permission to use, copy, modify, and distribute this software and
28 * its documentation for any purpose and without fee is hereby granted,
29 * provided that the above copyright notice appears in all copies and
30 * that both the copyright notice and this permission notice appear in
31 * supporting documentation.
32 *
33 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
34 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE.
36 *
37 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
38 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
39 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
40 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
41 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
42 *
43 */
44 /*
45 * MkLinux
46 */
47
48 /*
49 * POSIX Pthread Library
50 */
51
52 #include "resolver.h"
53 #include "internal.h"
54 #include "private.h"
55 #include "workqueue_private.h"
56 #include "introspection_private.h"
57 #include "qos_private.h"
58 #include "tsd_private.h"
59 #include "pthread/stack_np.h"
60 #include "offsets.h" // included to validate the offsets at build time
61
62 #include <stdlib.h>
63 #include <errno.h>
64 #include <signal.h>
65 #include <unistd.h>
66 #include <mach/mach_init.h>
67 #include <mach/mach_vm.h>
68 #include <mach/mach_sync_ipc.h>
69 #include <sys/time.h>
70 #include <sys/resource.h>
71 #include <sys/sysctl.h>
72 #include <sys/queue.h>
73 #include <sys/ulock.h>
74 #include <sys/mman.h>
75 #include <machine/vmparam.h>
76 #define __APPLE_API_PRIVATE
77 #include <machine/cpu_capabilities.h>
78 #if __has_include(<ptrauth.h>)
79 #include <ptrauth.h>
80 #endif // __has_include(<ptrauth.h>)
81
82 #include <_simple.h>
83 #include <platform/string.h>
84 #include <platform/compat.h>
85
86 #include <stack_logging.h>
87
88 // Defined in libsyscall; initialized in libmalloc
89 extern malloc_logger_t *__syscall_logger;
90
91 extern int __sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
92 void *newp, size_t newlen);
93 extern void __exit(int) __attribute__((noreturn));
94 extern int __pthread_kill(mach_port_t, int);
95
96 extern void _pthread_joiner_wake(pthread_t thread);
97
98 #if !VARIANT_DYLD
99 PTHREAD_NOEXPORT extern struct _pthread *_main_thread_ptr;
100 #define main_thread() (_main_thread_ptr)
101 #endif // VARIANT_DYLD
102
103 // Default stack size is 512KB; independent of the main thread's stack size.
104 #define DEFAULT_STACK_SIZE (size_t)(512 * 1024)
105
106
107 //
108 // Global constants
109 //
110
111 /*
112 * The pthread may be offset into a page. In that event, by contract
113 * with the kernel, the allocation will extend PTHREAD_SIZE from the
114 * start of the next page. There's also one page worth of allocation
115 * below stacksize for the guard page. <rdar://problem/19941744>
116 */
117 #define PTHREAD_SIZE ((size_t)mach_vm_round_page(sizeof(struct _pthread)))
118 #define PTHREAD_ALLOCADDR(stackaddr, stacksize) ((stackaddr - stacksize) - vm_page_size)
119 #define PTHREAD_ALLOCSIZE(stackaddr, stacksize) ((round_page((uintptr_t)stackaddr) + PTHREAD_SIZE) - (uintptr_t)PTHREAD_ALLOCADDR(stackaddr, stacksize))
120
121 static const pthread_attr_t _pthread_attr_default = {
122 .sig = _PTHREAD_ATTR_SIG,
123 .stacksize = 0,
124 .detached = PTHREAD_CREATE_JOINABLE,
125 .inherit = _PTHREAD_DEFAULT_INHERITSCHED,
126 .policy = _PTHREAD_DEFAULT_POLICY,
127 .defaultguardpage = true,
128 // compile time constant for _pthread_default_priority(0)
129 .qosclass = (1U << (THREAD_QOS_LEGACY - 1 + _PTHREAD_PRIORITY_QOS_CLASS_SHIFT)) |
130 ((uint8_t)-1 & _PTHREAD_PRIORITY_PRIORITY_MASK),
131 };
132
133 #if PTHREAD_LAYOUT_SPI
134
135 const struct pthread_layout_offsets_s pthread_layout_offsets = {
136 .plo_version = 1,
137 .plo_pthread_tsd_base_offset = offsetof(struct _pthread, tsd),
138 .plo_pthread_tsd_base_address_offset = 0,
139 .plo_pthread_tsd_entry_size = sizeof(((struct _pthread *)NULL)->tsd[0]),
140 };
141
142 #endif // PTHREAD_LAYOUT_SPI
143
144 //
145 // Global exported variables
146 //
147
148 // This global should be used (carefully) by anyone needing to know if a
149 // pthread (other than the main thread) has been created.
150 int __is_threaded = 0;
151 int __unix_conforming = 0;
152
153 //
154 // Global internal variables
155 //
156
157 // _pthread_list_lock protects _pthread_count, access to the __pthread_head
158 // list. Externally imported by pthread_cancelable.c.
159 struct __pthread_list __pthread_head = TAILQ_HEAD_INITIALIZER(__pthread_head);
160 _pthread_lock _pthread_list_lock = _PTHREAD_LOCK_INITIALIZER;
161
162 uint32_t _main_qos;
163
164 #if VARIANT_DYLD
165 // The main thread's pthread_t
166 struct _pthread _main_thread __attribute__((aligned(64))) = { };
167 #define main_thread() (&_main_thread)
168 #else // VARIANT_DYLD
169 struct _pthread *_main_thread_ptr;
170 #endif // VARIANT_DYLD
171
172 #if PTHREAD_DEBUG_LOG
173 #include <fcntl.h>
174 int _pthread_debuglog;
175 uint64_t _pthread_debugstart;
176 #endif
177
178 //
179 // Global static variables
180 //
181 static bool __workq_newapi;
182 static uint8_t default_priority;
183 #if !VARIANT_DYLD
184 static uint8_t max_priority;
185 static uint8_t min_priority;
186 #endif // !VARIANT_DYLD
187 static int _pthread_count = 1;
188 static int pthread_concurrency;
189 static uintptr_t _pthread_ptr_munge_token;
190
191 static void (*exitf)(int) = __exit;
192 #if !VARIANT_DYLD
193 static void *(*_pthread_malloc)(size_t) = NULL;
194 static void (*_pthread_free)(void *) = NULL;
195 #endif // !VARIANT_DYLD
196
197 // work queue support data
198 PTHREAD_NORETURN
199 static void
200 __pthread_invalid_keventfunction(void **events, int *nevents)
201 {
202 PTHREAD_CLIENT_CRASH(0, "Invalid kqworkq setup");
203 }
204
205 PTHREAD_NORETURN
206 static void
207 __pthread_invalid_workloopfunction(uint64_t *workloop_id, void **events, int *nevents)
208 {
209 PTHREAD_CLIENT_CRASH(0, "Invalid kqwl setup");
210 }
211 static pthread_workqueue_function2_t __libdispatch_workerfunction;
212 static pthread_workqueue_function_kevent_t __libdispatch_keventfunction = &__pthread_invalid_keventfunction;
213 static pthread_workqueue_function_workloop_t __libdispatch_workloopfunction = &__pthread_invalid_workloopfunction;
214 static int __pthread_supported_features; // supported feature set
215
216 #if defined(__i386__) || defined(__x86_64__)
217 static mach_vm_address_t __pthread_stack_hint = 0xB0000000;
218 #elif defined(__arm__) || defined(__arm64__)
219 static mach_vm_address_t __pthread_stack_hint = 0x30000000;
220 #else
221 #error no __pthread_stack_hint for this architecture
222 #endif
223
224 //
225 // Function prototypes
226 //
227
228 // pthread primitives
229 static inline void _pthread_struct_init(pthread_t t, const pthread_attr_t *attrs,
230 void *stack, size_t stacksize, void *freeaddr, size_t freesize);
231
232 #if VARIANT_DYLD
233 static void _pthread_set_self_dyld(void);
234 #endif // VARIANT_DYLD
235 static inline void _pthread_set_self_internal(pthread_t);
236
237 static void _pthread_dealloc_reply_port(pthread_t t);
238 static void _pthread_dealloc_special_reply_port(pthread_t t);
239
240 static inline void __pthread_started_thread(pthread_t t);
241
242 static void _pthread_exit(pthread_t self, void *value_ptr) __dead2;
243
244 static inline void _pthread_introspection_thread_create(pthread_t t);
245 static inline void _pthread_introspection_thread_start(pthread_t t);
246 static inline void _pthread_introspection_thread_terminate(pthread_t t);
247 static inline void _pthread_introspection_thread_destroy(pthread_t t);
248
249 extern void _pthread_set_self(pthread_t);
250 extern void start_wqthread(pthread_t self, mach_port_t kport, void *stackaddr, void *unused, int reuse); // trampoline into _pthread_wqthread
251 extern void thread_start(pthread_t self, mach_port_t kport, void *(*fun)(void *), void * funarg, size_t stacksize, unsigned int flags); // trampoline into _pthread_start
252
253 /*
254 * Flags filed passed to bsdthread_create and back in pthread_start
255 * 31 <---------------------------------> 0
256 * _________________________________________
257 * | flags(8) | policy(8) | importance(16) |
258 * -----------------------------------------
259 */
260 #define PTHREAD_START_CUSTOM 0x01000000 // <rdar://problem/34501401>
261 #define PTHREAD_START_SETSCHED 0x02000000
262 // was PTHREAD_START_DETACHED 0x04000000
263 #define PTHREAD_START_QOSCLASS 0x08000000
264 #define PTHREAD_START_TSD_BASE_SET 0x10000000
265 #define PTHREAD_START_SUSPENDED 0x20000000
266 #define PTHREAD_START_QOSCLASS_MASK 0x00ffffff
267 #define PTHREAD_START_POLICY_BITSHIFT 16
268 #define PTHREAD_START_POLICY_MASK 0xff
269 #define PTHREAD_START_IMPORTANCE_MASK 0xffff
270
271 extern pthread_t __bsdthread_create(void *(*func)(void *), void * func_arg, void * stack, pthread_t thread, unsigned int flags);
272 extern int __bsdthread_register(void (*)(pthread_t, mach_port_t, void *(*)(void *), void *, size_t, unsigned int), void (*)(pthread_t, mach_port_t, void *, void *, int), int,void (*)(pthread_t, mach_port_t, void *(*)(void *), void *, size_t, unsigned int), int32_t *,__uint64_t);
273 extern int __bsdthread_terminate(void * freeaddr, size_t freesize, mach_port_t kport, mach_port_t joinsem);
274 extern __uint64_t __thread_selfid( void );
275
276 #if __LP64__
277 _Static_assert(offsetof(struct _pthread, tsd) == 224, "TSD LP64 offset");
278 #else
279 _Static_assert(offsetof(struct _pthread, tsd) == 176, "TSD ILP32 offset");
280 #endif
281 _Static_assert(offsetof(struct _pthread, tsd) + _PTHREAD_STRUCT_DIRECT_THREADID_OFFSET
282 == offsetof(struct _pthread, thread_id),
283 "_PTHREAD_STRUCT_DIRECT_THREADID_OFFSET is correct");
284
285 #pragma mark pthread attrs
286
287 _Static_assert(sizeof(struct _pthread_attr_t) == sizeof(__darwin_pthread_attr_t),
288 "internal pthread_attr_t == external pthread_attr_t");
289
290 int
291 pthread_attr_destroy(pthread_attr_t *attr)
292 {
293 int ret = EINVAL;
294 if (attr->sig == _PTHREAD_ATTR_SIG) {
295 attr->sig = 0;
296 ret = 0;
297 }
298 return ret;
299 }
300
301 int
302 pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate)
303 {
304 int ret = EINVAL;
305 if (attr->sig == _PTHREAD_ATTR_SIG) {
306 *detachstate = attr->detached;
307 ret = 0;
308 }
309 return ret;
310 }
311
312 int
313 pthread_attr_getinheritsched(const pthread_attr_t *attr, int *inheritsched)
314 {
315 int ret = EINVAL;
316 if (attr->sig == _PTHREAD_ATTR_SIG) {
317 *inheritsched = attr->inherit;
318 ret = 0;
319 }
320 return ret;
321 }
322
323 static PTHREAD_ALWAYS_INLINE void
324 _pthread_attr_get_schedparam(const pthread_attr_t *attr,
325 struct sched_param *param)
326 {
327 if (attr->schedset) {
328 *param = attr->param;
329 } else {
330 param->sched_priority = default_priority;
331 param->quantum = 10; /* quantum isn't public yet */
332 }
333 }
334
335 int
336 pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param)
337 {
338 int ret = EINVAL;
339 if (attr->sig == _PTHREAD_ATTR_SIG) {
340 _pthread_attr_get_schedparam(attr, param);
341 ret = 0;
342 }
343 return ret;
344 }
345
346 int
347 pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy)
348 {
349 int ret = EINVAL;
350 if (attr->sig == _PTHREAD_ATTR_SIG) {
351 *policy = attr->policy;
352 ret = 0;
353 }
354 return ret;
355 }
356
357 int
358 pthread_attr_init(pthread_attr_t *attr)
359 {
360 *attr = _pthread_attr_default;
361 return 0;
362 }
363
364 int
365 pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate)
366 {
367 int ret = EINVAL;
368 if (attr->sig == _PTHREAD_ATTR_SIG &&
369 (detachstate == PTHREAD_CREATE_JOINABLE ||
370 detachstate == PTHREAD_CREATE_DETACHED)) {
371 attr->detached = detachstate;
372 ret = 0;
373 }
374 return ret;
375 }
376
377 int
378 pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched)
379 {
380 int ret = EINVAL;
381 if (attr->sig == _PTHREAD_ATTR_SIG &&
382 (inheritsched == PTHREAD_INHERIT_SCHED ||
383 inheritsched == PTHREAD_EXPLICIT_SCHED)) {
384 attr->inherit = inheritsched;
385 ret = 0;
386 }
387 return ret;
388 }
389
390 int
391 pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param)
392 {
393 int ret = EINVAL;
394 if (attr->sig == _PTHREAD_ATTR_SIG) {
395 /* TODO: Validate sched_param fields */
396 attr->param = *param;
397 attr->schedset = 1;
398 ret = 0;
399 }
400 return ret;
401 }
402
403 int
404 pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy)
405 {
406 int ret = EINVAL;
407 if (attr->sig == _PTHREAD_ATTR_SIG && (policy == SCHED_OTHER ||
408 policy == SCHED_RR || policy == SCHED_FIFO)) {
409 if (!_PTHREAD_POLICY_IS_FIXEDPRI(policy)) {
410 /* non-fixedpri policy should remove cpupercent */
411 attr->cpupercentset = 0;
412 }
413 attr->policy = policy;
414 attr->policyset = 1;
415 ret = 0;
416 }
417 return ret;
418 }
419
420 int
421 pthread_attr_setscope(pthread_attr_t *attr, int scope)
422 {
423 int ret = EINVAL;
424 if (attr->sig == _PTHREAD_ATTR_SIG) {
425 if (scope == PTHREAD_SCOPE_SYSTEM) {
426 // No attribute yet for the scope.
427 ret = 0;
428 } else if (scope == PTHREAD_SCOPE_PROCESS) {
429 ret = ENOTSUP;
430 }
431 }
432 return ret;
433 }
434
435 int
436 pthread_attr_getscope(const pthread_attr_t *attr, int *scope)
437 {
438 int ret = EINVAL;
439 if (attr->sig == _PTHREAD_ATTR_SIG) {
440 *scope = PTHREAD_SCOPE_SYSTEM;
441 ret = 0;
442 }
443 return ret;
444 }
445
446 int
447 pthread_attr_getstackaddr(const pthread_attr_t *attr, void **stackaddr)
448 {
449 int ret = EINVAL;
450 if (attr->sig == _PTHREAD_ATTR_SIG) {
451 *stackaddr = attr->stackaddr;
452 ret = 0;
453 }
454 return ret;
455 }
456
457 int
458 pthread_attr_setstackaddr(pthread_attr_t *attr, void *stackaddr)
459 {
460 int ret = EINVAL;
461 if (attr->sig == _PTHREAD_ATTR_SIG &&
462 ((uintptr_t)stackaddr % vm_page_size) == 0) {
463 attr->stackaddr = stackaddr;
464 attr->defaultguardpage = false;
465 attr->guardsize = 0;
466 ret = 0;
467 }
468 return ret;
469 }
470
471 static inline size_t
472 _pthread_attr_stacksize(const pthread_attr_t *attr)
473 {
474 return attr->stacksize ? attr->stacksize : DEFAULT_STACK_SIZE;
475 }
476
477 int
478 pthread_attr_getstacksize(const pthread_attr_t *attr, size_t *stacksize)
479 {
480 int ret = EINVAL;
481 if (attr->sig == _PTHREAD_ATTR_SIG) {
482 *stacksize = _pthread_attr_stacksize(attr);
483 ret = 0;
484 }
485 return ret;
486 }
487
488 int
489 pthread_attr_setstacksize(pthread_attr_t *attr, size_t stacksize)
490 {
491 int ret = EINVAL;
492 if (attr->sig == _PTHREAD_ATTR_SIG &&
493 (stacksize % vm_page_size) == 0 &&
494 stacksize >= PTHREAD_STACK_MIN) {
495 attr->stacksize = stacksize;
496 ret = 0;
497 }
498 return ret;
499 }
500
501 int
502 pthread_attr_getstack(const pthread_attr_t *attr, void **stackaddr, size_t * stacksize)
503 {
504 int ret = EINVAL;
505 if (attr->sig == _PTHREAD_ATTR_SIG) {
506 *stackaddr = (void *)((uintptr_t)attr->stackaddr - attr->stacksize);
507 *stacksize = _pthread_attr_stacksize(attr);
508 ret = 0;
509 }
510 return ret;
511 }
512
513 // Per SUSv3, the stackaddr is the base address, the lowest addressable byte
514 // address. This is not the same as in pthread_attr_setstackaddr.
515 int
516 pthread_attr_setstack(pthread_attr_t *attr, void *stackaddr, size_t stacksize)
517 {
518 int ret = EINVAL;
519 if (attr->sig == _PTHREAD_ATTR_SIG &&
520 ((uintptr_t)stackaddr % vm_page_size) == 0 &&
521 (stacksize % vm_page_size) == 0 &&
522 stacksize >= PTHREAD_STACK_MIN) {
523 attr->stackaddr = (void *)((uintptr_t)stackaddr + stacksize);
524 attr->stacksize = stacksize;
525 ret = 0;
526 }
527 return ret;
528 }
529
530 int
531 pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize)
532 {
533 int ret = EINVAL;
534 if (attr->sig == _PTHREAD_ATTR_SIG && (guardsize % vm_page_size) == 0) {
535 /* Guardsize of 0 is valid, means no guard */
536 attr->defaultguardpage = false;
537 attr->guardsize = guardsize;
538 ret = 0;
539 }
540 return ret;
541 }
542
543 static inline size_t
544 _pthread_attr_guardsize(const pthread_attr_t *attr)
545 {
546 return attr->defaultguardpage ? vm_page_size : attr->guardsize;
547 }
548
549 int
550 pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize)
551 {
552 int ret = EINVAL;
553 if (attr->sig == _PTHREAD_ATTR_SIG) {
554 *guardsize = _pthread_attr_guardsize(attr);
555 ret = 0;
556 }
557 return ret;
558 }
559
560 int
561 pthread_attr_setcpupercent_np(pthread_attr_t *attr, int percent,
562 unsigned long refillms)
563 {
564 int ret = EINVAL;
565 if (attr->sig == _PTHREAD_ATTR_SIG && percent < UINT8_MAX &&
566 refillms < _PTHREAD_ATTR_REFILLMS_MAX && attr->policyset &&
567 _PTHREAD_POLICY_IS_FIXEDPRI(attr->policy)) {
568 attr->cpupercent = percent;
569 attr->refillms = (uint32_t)(refillms & 0x00ffffff);
570 attr->cpupercentset = 1;
571 ret = 0;
572 }
573 return ret;
574 }
575
576 #pragma mark pthread lifetime
577
578 // Allocate a thread structure, stack and guard page.
579 //
580 // The thread structure may optionally be placed in the same allocation as the
581 // stack, residing above the top of the stack. This cannot be done if a
582 // custom stack address is provided.
583 //
584 // Similarly the guard page cannot be allocated if a custom stack address is
585 // provided.
586 //
587 // The allocated thread structure is initialized with values that indicate how
588 // it should be freed.
589
590 static pthread_t
591 _pthread_allocate(const pthread_attr_t *attrs, void **stack,
592 bool from_mach_thread)
593 {
594 mach_vm_address_t allocaddr = __pthread_stack_hint;
595 size_t allocsize, guardsize, stacksize, pthreadoff;
596 kern_return_t kr;
597 pthread_t t;
598
599 if (os_unlikely(attrs->stacksize != 0 &&
600 attrs->stacksize < PTHREAD_STACK_MIN)) {
601 PTHREAD_CLIENT_CRASH(attrs->stacksize, "Stack size in attrs is too small");
602 }
603
604 if (os_unlikely(((uintptr_t)attrs->stackaddr % vm_page_size) != 0)) {
605 PTHREAD_CLIENT_CRASH(attrs->stacksize, "Unaligned stack addr in attrs");
606 }
607
608 // Allocate a pthread structure if necessary
609
610 if (attrs->stackaddr != NULL) {
611 allocsize = PTHREAD_SIZE;
612 guardsize = 0;
613 pthreadoff = 0;
614 // <rdar://problem/42588315> if the attrs struct specifies a custom
615 // stack address but not a custom size, using ->stacksize here instead
616 // of _pthread_attr_stacksize stores stacksize as zero, indicating
617 // that the stack size is unknown.
618 stacksize = attrs->stacksize;
619 } else {
620 guardsize = _pthread_attr_guardsize(attrs);
621 stacksize = _pthread_attr_stacksize(attrs) + PTHREAD_T_OFFSET;
622 pthreadoff = stacksize + guardsize;
623 allocsize = pthreadoff + PTHREAD_SIZE;
624 allocsize = mach_vm_round_page(allocsize);
625 }
626
627 kr = mach_vm_map(mach_task_self(), &allocaddr, allocsize, vm_page_size - 1,
628 VM_MAKE_TAG(VM_MEMORY_STACK)| VM_FLAGS_ANYWHERE, MEMORY_OBJECT_NULL,
629 0, FALSE, VM_PROT_DEFAULT, VM_PROT_ALL, VM_INHERIT_DEFAULT);
630
631 if (kr != KERN_SUCCESS) {
632 kr = mach_vm_allocate(mach_task_self(), &allocaddr, allocsize,
633 VM_MAKE_TAG(VM_MEMORY_STACK)| VM_FLAGS_ANYWHERE);
634 } else if (__syscall_logger && !from_mach_thread) {
635 // libsyscall will not output malloc stack logging events when
636 // VM_MEMORY_STACK is passed in to facilitate mach thread promotion.
637 // To avoid losing the stack traces for normal p-thread create
638 // operations, libpthread must pretend to be the vm syscall and log
639 // the allocations. <rdar://36418708>
640 int eventTypeFlags = stack_logging_type_vm_allocate |
641 stack_logging_type_mapped_file_or_shared_mem;
642 __syscall_logger(eventTypeFlags | VM_MAKE_TAG(VM_MEMORY_STACK),
643 (uintptr_t)mach_task_self(), (uintptr_t)allocsize, 0,
644 (uintptr_t)allocaddr, 0);
645 }
646
647 if (kr != KERN_SUCCESS) {
648 *stack = NULL;
649 return NULL;
650 } else if (__syscall_logger && !from_mach_thread) {
651 // libsyscall will not output malloc stack logging events when
652 // VM_MEMORY_STACK is passed in to facilitate mach thread promotion.
653 // To avoid losing the stack traces for normal p-thread create
654 // operations, libpthread must pretend to be the vm syscall and log
655 // the allocations. <rdar://36418708>
656 int eventTypeFlags = stack_logging_type_vm_allocate;
657 __syscall_logger(eventTypeFlags | VM_MAKE_TAG(VM_MEMORY_STACK),
658 (uintptr_t)mach_task_self(), (uintptr_t)allocsize, 0,
659 (uintptr_t)allocaddr, 0);
660 }
661
662 // The stack grows down.
663 // Set the guard page at the lowest address of the
664 // newly allocated stack. Return the highest address
665 // of the stack.
666 if (guardsize) {
667 (void)mach_vm_protect(mach_task_self(), allocaddr, guardsize,
668 FALSE, VM_PROT_NONE);
669 }
670
671 // Thread structure resides at the top of the stack (when using a
672 // custom stack, allocsize == PTHREAD_SIZE, so places the pthread_t
673 // at allocaddr).
674 t = (pthread_t)(allocaddr + pthreadoff);
675 if (attrs->stackaddr) {
676 *stack = attrs->stackaddr;
677 } else {
678 *stack = t;
679 }
680
681 _pthread_struct_init(t, attrs, *stack, stacksize, allocaddr, allocsize);
682 return t;
683 }
684
685 PTHREAD_NOINLINE
686 void
687 _pthread_deallocate(pthread_t t, bool from_mach_thread)
688 {
689 kern_return_t ret;
690
691 // Don't free the main thread.
692 if (t != main_thread()) {
693 if (!from_mach_thread) { // see __pthread_add_thread
694 _pthread_introspection_thread_destroy(t);
695 }
696 ret = mach_vm_deallocate(mach_task_self(), t->freeaddr, t->freesize);
697 if (ret != KERN_SUCCESS) {
698 PTHREAD_INTERNAL_CRASH(ret, "Unable to deallocate stack");
699 }
700 }
701 }
702
703 #pragma clang diagnostic push
704 #pragma clang diagnostic ignored "-Wreturn-stack-address"
705
706 PTHREAD_NOINLINE
707 static void*
708 _pthread_current_stack_address(void)
709 {
710 int a;
711 return &a;
712 }
713
714 #pragma clang diagnostic pop
715
716 void
717 _pthread_joiner_wake(pthread_t thread)
718 {
719 uint32_t *exit_gate = &thread->tl_exit_gate;
720
721 for (;;) {
722 int ret = __ulock_wake(UL_UNFAIR_LOCK | ULF_NO_ERRNO, exit_gate, 0);
723 if (ret == 0 || ret == -ENOENT) {
724 return;
725 }
726 if (ret != -EINTR) {
727 PTHREAD_INTERNAL_CRASH(-ret, "pthread_join() wake failure");
728 }
729 }
730 }
731
732 // Terminates the thread if called from the currently running thread.
733 PTHREAD_NORETURN PTHREAD_NOINLINE PTHREAD_NOT_TAIL_CALLED
734 static void
735 _pthread_terminate(pthread_t t, void *exit_value)
736 {
737 _pthread_introspection_thread_terminate(t);
738
739 uintptr_t freeaddr = (uintptr_t)t->freeaddr;
740 size_t freesize = t->freesize;
741 bool should_exit;
742
743 // the size of just the stack
744 size_t freesize_stack = t->freesize;
745
746 // We usually pass our structure+stack to bsdthread_terminate to free, but
747 // if we get told to keep the pthread_t structure around then we need to
748 // adjust the free size and addr in the pthread_t to just refer to the
749 // structure and not the stack. If we do end up deallocating the
750 // structure, this is useless work since no one can read the result, but we
751 // can't do it after the call to pthread_remove_thread because it isn't
752 // safe to dereference t after that.
753 if ((void*)t > t->freeaddr && (void*)t < t->freeaddr + t->freesize){
754 // Check to ensure the pthread structure itself is part of the
755 // allocation described by freeaddr/freesize, in which case we split and
756 // only deallocate the area below the pthread structure. In the event of a
757 // custom stack, the freeaddr/size will be the pthread structure itself, in
758 // which case we shouldn't free anything (the final else case).
759 freesize_stack = trunc_page((uintptr_t)t - (uintptr_t)freeaddr);
760
761 // describe just the remainder for deallocation when the pthread_t goes away
762 t->freeaddr += freesize_stack;
763 t->freesize -= freesize_stack;
764 } else if (t == main_thread()) {
765 freeaddr = t->stackaddr - pthread_get_stacksize_np(t);
766 uintptr_t stackborder = trunc_page((uintptr_t)_pthread_current_stack_address());
767 freesize_stack = stackborder - freeaddr;
768 } else {
769 freesize_stack = 0;
770 }
771
772 mach_port_t kport = _pthread_kernel_thread(t);
773 bool keep_thread_struct = false, needs_wake = false;
774 semaphore_t custom_stack_sema = MACH_PORT_NULL;
775
776 _pthread_dealloc_special_reply_port(t);
777 _pthread_dealloc_reply_port(t);
778
779 _PTHREAD_LOCK(_pthread_list_lock);
780
781 // This piece of code interacts with pthread_join. It will always:
782 // - set tl_exit_gate to MACH_PORT_DEAD (thread exited)
783 // - set tl_exit_value to the value passed to pthread_exit()
784 // - decrement _pthread_count, so that we can exit the process when all
785 // threads exited even if not all of them were joined.
786 t->tl_exit_gate = MACH_PORT_DEAD;
787 t->tl_exit_value = exit_value;
788 should_exit = (--_pthread_count <= 0);
789
790 // If we see a joiner, we prepost that the join has to succeed,
791 // and the joiner is committed to finish (even if it was canceled)
792 if (t->tl_join_ctx) {
793 custom_stack_sema = _pthread_joiner_prepost_wake(t); // unsets tl_joinable
794 needs_wake = true;
795 }
796
797 // Joinable threads that have no joiner yet are kept on the thread list
798 // so that pthread_join() can later discover the thread when it is joined,
799 // and will have to do the pthread_t cleanup.
800 if (t->tl_joinable) {
801 t->tl_joiner_cleans_up = keep_thread_struct = true;
802 } else {
803 TAILQ_REMOVE(&__pthread_head, t, tl_plist);
804 }
805
806 _PTHREAD_UNLOCK(_pthread_list_lock);
807
808 if (needs_wake) {
809 // When we found a waiter, we want to drop the very contended list lock
810 // before we do the syscall in _pthread_joiner_wake(). Then, we decide
811 // who gets to cleanup the pthread_t between the joiner and the exiting
812 // thread:
813 // - the joiner tries to set tl_join_ctx to NULL
814 // - the exiting thread tries to set tl_joiner_cleans_up to true
815 // Whoever does it first commits the other guy to cleanup the pthread_t
816 _pthread_joiner_wake(t);
817 _PTHREAD_LOCK(_pthread_list_lock);
818 if (t->tl_join_ctx) {
819 t->tl_joiner_cleans_up = true;
820 keep_thread_struct = true;
821 }
822 _PTHREAD_UNLOCK(_pthread_list_lock);
823 }
824
825 //
826 // /!\ dereferencing `t` past this point is not safe /!\
827 //
828
829 if (keep_thread_struct || t == main_thread()) {
830 // Use the adjusted freesize of just the stack that we computed above.
831 freesize = freesize_stack;
832 } else {
833 _pthread_introspection_thread_destroy(t);
834 }
835
836 // Check if there is nothing to free because the thread has a custom
837 // stack allocation and is joinable.
838 if (freesize == 0) {
839 freeaddr = 0;
840 }
841 if (should_exit) {
842 exitf(0);
843 }
844 __bsdthread_terminate((void *)freeaddr, freesize, kport, custom_stack_sema);
845 PTHREAD_INTERNAL_CRASH(t, "thread didn't terminate");
846 }
847
848 PTHREAD_NORETURN
849 static void
850 _pthread_terminate_invoke(pthread_t t, void *exit_value)
851 {
852 #if PTHREAD_T_OFFSET
853 void *p = NULL;
854 // <rdar://problem/25688492> During pthread termination there is a race
855 // between pthread_join and pthread_terminate; if the joiner is responsible
856 // for cleaning up the pthread_t struct, then it may destroy some part of the
857 // stack with it on 16k OSes. So that this doesn't cause _pthread_terminate()
858 // to crash because its stack has been removed from under its feet, just make
859 // sure termination happens in a part of the stack that is not on the same
860 // page as the pthread_t.
861 if (trunc_page((uintptr_t)__builtin_frame_address(0)) ==
862 trunc_page((uintptr_t)t)) {
863 p = alloca(PTHREAD_T_OFFSET);
864 }
865 // And this __asm__ volatile is needed to stop the compiler from optimising
866 // away the alloca() completely.
867 __asm__ volatile ("" : : "r"(p) );
868 #endif
869 _pthread_terminate(t, exit_value);
870 }
871
872 #pragma mark pthread start / body
873
874 PTHREAD_NORETURN
875 void
876 _pthread_start(pthread_t self, mach_port_t kport,
877 __unused void *(*fun)(void *), __unused void *arg,
878 __unused size_t stacksize, unsigned int pflags)
879 {
880 if (os_unlikely(pflags & PTHREAD_START_SUSPENDED)) {
881 PTHREAD_INTERNAL_CRASH(pflags,
882 "kernel without PTHREAD_START_SUSPENDED support");
883 }
884 if (os_unlikely((pflags & PTHREAD_START_TSD_BASE_SET) == 0)) {
885 PTHREAD_INTERNAL_CRASH(pflags,
886 "thread_set_tsd_base() wasn't called by the kernel");
887 }
888 PTHREAD_DEBUG_ASSERT(MACH_PORT_VALID(kport));
889 PTHREAD_DEBUG_ASSERT(_pthread_kernel_thread(self) == kport);
890 _pthread_markcancel_if_canceled(self, kport);
891
892 _pthread_set_self_internal(self);
893 __pthread_started_thread(self);
894 _pthread_exit(self, (self->fun)(self->arg));
895 }
896
897 PTHREAD_ALWAYS_INLINE
898 static inline void
899 _pthread_struct_init(pthread_t t, const pthread_attr_t *attrs,
900 void *stackaddr, size_t stacksize, void *freeaddr, size_t freesize)
901 {
902 PTHREAD_DEBUG_ASSERT(t->sig != _PTHREAD_SIG);
903
904 t->sig = _PTHREAD_SIG;
905 t->tsd[_PTHREAD_TSD_SLOT_PTHREAD_SELF] = t;
906 t->tsd[_PTHREAD_TSD_SLOT_ERRNO] = &t->err_no;
907 if (attrs->schedset == 0) {
908 t->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] = attrs->qosclass;
909 } else {
910 t->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] =
911 _pthread_unspecified_priority();
912 }
913 t->tsd[_PTHREAD_TSD_SLOT_PTR_MUNGE] = _pthread_ptr_munge_token;
914 t->tl_has_custom_stack = (attrs->stackaddr != NULL);
915
916 _PTHREAD_LOCK_INIT(t->lock);
917
918 t->stackaddr = stackaddr;
919 t->stackbottom = stackaddr - stacksize;
920 t->freeaddr = freeaddr;
921 t->freesize = freesize;
922
923 t->guardsize = _pthread_attr_guardsize(attrs);
924 t->tl_joinable = (attrs->detached == PTHREAD_CREATE_JOINABLE);
925 t->inherit = attrs->inherit;
926 t->tl_policy = attrs->policy;
927 t->schedset = attrs->schedset;
928 _pthread_attr_get_schedparam(attrs, &t->tl_param);
929 t->cancel_state = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
930 }
931
932 #pragma mark pthread public interface
933
934 /* Need to deprecate this in future */
935 int
936 _pthread_is_threaded(void)
937 {
938 return __is_threaded;
939 }
940
941 /* Non portable public api to know whether this process has(had) atleast one thread
942 * apart from main thread. There could be race if there is a thread in the process of
943 * creation at the time of call . It does not tell whether there are more than one thread
944 * at this point of time.
945 */
946 int
947 pthread_is_threaded_np(void)
948 {
949 return __is_threaded;
950 }
951
952
953 PTHREAD_NOEXPORT_VARIANT
954 mach_port_t
955 pthread_mach_thread_np(pthread_t t)
956 {
957 mach_port_t kport = MACH_PORT_NULL;
958 (void)_pthread_is_valid(t, &kport);
959 return kport;
960 }
961
962 PTHREAD_NOEXPORT_VARIANT
963 pthread_t
964 pthread_from_mach_thread_np(mach_port_t kernel_thread)
965 {
966 struct _pthread *p = NULL;
967
968 /* No need to wait as mach port is already known */
969 _PTHREAD_LOCK(_pthread_list_lock);
970
971 TAILQ_FOREACH(p, &__pthread_head, tl_plist) {
972 if (_pthread_kernel_thread(p) == kernel_thread) {
973 break;
974 }
975 }
976
977 _PTHREAD_UNLOCK(_pthread_list_lock);
978
979 return p;
980 }
981
982 PTHREAD_NOEXPORT_VARIANT
983 size_t
984 pthread_get_stacksize_np(pthread_t t)
985 {
986 size_t size = 0;
987
988 if (t == NULL) {
989 return ESRCH; // XXX bug?
990 }
991
992 #if TARGET_OS_OSX
993 // The default rlimit based allocations will be provided with a stacksize
994 // of the current limit and a freesize of the max. However, custom
995 // allocations will just have the guard page to free. If we aren't in the
996 // latter case, call into rlimit to determine the current stack size. In
997 // the event that the current limit == max limit then we'll fall down the
998 // fast path, but since it's unlikely that the limit is going to be lowered
999 // after it's been change to the max, we should be fine.
1000 //
1001 // Of course, on arm rlim_cur == rlim_max and there's only the one guard
1002 // page. So, we can skip all this there.
1003 if (t == main_thread()) {
1004 size_t stacksize = t->stackaddr - t->stackbottom;
1005
1006 if (stacksize + vm_page_size != t->freesize) {
1007 // We want to call getrlimit() just once, as it's relatively
1008 // expensive
1009 static size_t rlimit_stack;
1010
1011 if (rlimit_stack == 0) {
1012 struct rlimit limit;
1013 int ret = getrlimit(RLIMIT_STACK, &limit);
1014
1015 if (ret == 0) {
1016 rlimit_stack = (size_t) limit.rlim_cur;
1017 }
1018 }
1019
1020 if (rlimit_stack == 0 || rlimit_stack > t->freesize) {
1021 return stacksize;
1022 } else {
1023 return round_page(rlimit_stack);
1024 }
1025 }
1026 }
1027 #endif /* TARGET_OS_OSX */
1028
1029 if (t == pthread_self() || t == main_thread()) {
1030 size = t->stackaddr - t->stackbottom;;
1031 goto out;
1032 }
1033
1034 if (_pthread_validate_thread_and_list_lock(t)) {
1035 size = t->stackaddr - t->stackbottom;;
1036 _PTHREAD_UNLOCK(_pthread_list_lock);
1037 }
1038
1039 out:
1040 // <rdar://problem/42588315> binary compatibility issues force us to return
1041 // DEFAULT_STACK_SIZE here when we do not know the size of the stack
1042 return size ? size : DEFAULT_STACK_SIZE;
1043 }
1044
1045 PTHREAD_NOEXPORT_VARIANT
1046 void *
1047 pthread_get_stackaddr_np(pthread_t t)
1048 {
1049 // since the main thread will not get de-allocated from underneath us
1050 if (t == pthread_self() || t == main_thread()) {
1051 return t->stackaddr;
1052 }
1053
1054 if (!_pthread_validate_thread_and_list_lock(t)) {
1055 return (void *)(uintptr_t)ESRCH; // XXX bug?
1056 }
1057
1058 void *addr = t->stackaddr;
1059 _PTHREAD_UNLOCK(_pthread_list_lock);
1060 return addr;
1061 }
1062
1063
1064 static mach_port_t
1065 _pthread_reply_port(pthread_t t)
1066 {
1067 void *p;
1068 if (t == NULL) {
1069 p = _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MIG_REPLY);
1070 } else {
1071 p = t->tsd[_PTHREAD_TSD_SLOT_MIG_REPLY];
1072 }
1073 return (mach_port_t)(uintptr_t)p;
1074 }
1075
1076 static void
1077 _pthread_set_reply_port(pthread_t t, mach_port_t reply_port)
1078 {
1079 void *p = (void *)(uintptr_t)reply_port;
1080 if (t == NULL) {
1081 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_MIG_REPLY, p);
1082 } else {
1083 t->tsd[_PTHREAD_TSD_SLOT_MIG_REPLY] = p;
1084 }
1085 }
1086
1087 static void
1088 _pthread_dealloc_reply_port(pthread_t t)
1089 {
1090 mach_port_t reply_port = _pthread_reply_port(t);
1091 if (reply_port != MACH_PORT_NULL) {
1092 mig_dealloc_reply_port(reply_port);
1093 }
1094 }
1095
1096 static mach_port_t
1097 _pthread_special_reply_port(pthread_t t)
1098 {
1099 void *p;
1100 if (t == NULL) {
1101 p = _pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_SPECIAL_REPLY);
1102 } else {
1103 p = t->tsd[_PTHREAD_TSD_SLOT_MACH_SPECIAL_REPLY];
1104 }
1105 return (mach_port_t)(uintptr_t)p;
1106 }
1107
1108 static void
1109 _pthread_dealloc_special_reply_port(pthread_t t)
1110 {
1111 mach_port_t special_reply_port = _pthread_special_reply_port(t);
1112 if (special_reply_port != MACH_PORT_NULL) {
1113 thread_destruct_special_reply_port(special_reply_port,
1114 THREAD_SPECIAL_REPLY_PORT_ALL);
1115 }
1116 }
1117
1118 pthread_t
1119 pthread_main_thread_np(void)
1120 {
1121 return main_thread();
1122 }
1123
1124 /* returns non-zero if the current thread is the main thread */
1125 int
1126 pthread_main_np(void)
1127 {
1128 return pthread_self() == main_thread();
1129 }
1130
1131
1132 static int
1133 _pthread_threadid_slow(pthread_t thread, uint64_t *thread_id)
1134 {
1135 unsigned int info_count = THREAD_IDENTIFIER_INFO_COUNT;
1136 mach_port_t thport = _pthread_kernel_thread(thread);
1137 struct thread_identifier_info info;
1138 kern_return_t kr;
1139
1140 kr = thread_info(thport, THREAD_IDENTIFIER_INFO,
1141 (thread_info_t)&info, &info_count);
1142 if (kr == KERN_SUCCESS && info.thread_id) {
1143 *thread_id = info.thread_id;
1144 os_atomic_store(&thread->thread_id, info.thread_id, relaxed);
1145 return 0;
1146 }
1147 return EINVAL;
1148 }
1149
1150 /*
1151 * if we are passed in a pthread_t that is NULL, then we return the current
1152 * thread's thread_id. So folks don't have to call pthread_self, in addition to
1153 * us doing it, if they just want their thread_id.
1154 */
1155 PTHREAD_NOEXPORT_VARIANT
1156 int
1157 pthread_threadid_np(pthread_t thread, uint64_t *thread_id)
1158 {
1159 int res = 0;
1160 pthread_t self = pthread_self();
1161
1162 if (thread_id == NULL) {
1163 return EINVAL;
1164 }
1165
1166 if (thread == NULL || thread == self) {
1167 *thread_id = self->thread_id;
1168 } else if (!_pthread_validate_thread_and_list_lock(thread)) {
1169 res = ESRCH;
1170 } else {
1171 *thread_id = os_atomic_load(&thread->thread_id, relaxed);
1172 if (os_unlikely(*thread_id == 0)) {
1173 // there is a race at init because the thread sets its own TID.
1174 // correct this by asking mach
1175 res = _pthread_threadid_slow(thread, thread_id);
1176 }
1177 _PTHREAD_UNLOCK(_pthread_list_lock);
1178 }
1179 return res;
1180 }
1181
1182 PTHREAD_NOEXPORT_VARIANT
1183 int
1184 pthread_getname_np(pthread_t thread, char *threadname, size_t len)
1185 {
1186 if (thread == pthread_self()) {
1187 strlcpy(threadname, thread->pthread_name, len);
1188 return 0;
1189 }
1190
1191 if (!_pthread_validate_thread_and_list_lock(thread)) {
1192 return ESRCH;
1193 }
1194
1195 strlcpy(threadname, thread->pthread_name, len);
1196 _PTHREAD_UNLOCK(_pthread_list_lock);
1197 return 0;
1198 }
1199
1200
1201 int
1202 pthread_setname_np(const char *name)
1203 {
1204 int res;
1205 pthread_t self = pthread_self();
1206
1207 size_t len = 0;
1208 if (name != NULL) {
1209 len = strlen(name);
1210 }
1211
1212 /* protytype is in pthread_internals.h */
1213 res = __proc_info(5, getpid(), 2, (uint64_t)0, (void*)name, (int)len);
1214 if (res == 0) {
1215 if (len > 0) {
1216 strlcpy(self->pthread_name, name, MAXTHREADNAMESIZE);
1217 } else {
1218 bzero(self->pthread_name, MAXTHREADNAMESIZE);
1219 }
1220 }
1221 return res;
1222
1223 }
1224
1225 PTHREAD_ALWAYS_INLINE
1226 static inline void
1227 __pthread_add_thread(pthread_t t, bool from_mach_thread)
1228 {
1229 if (from_mach_thread) {
1230 _PTHREAD_LOCK_FROM_MACH_THREAD(_pthread_list_lock);
1231 } else {
1232 _PTHREAD_LOCK(_pthread_list_lock);
1233 }
1234
1235 TAILQ_INSERT_TAIL(&__pthread_head, t, tl_plist);
1236 _pthread_count++;
1237
1238 if (from_mach_thread) {
1239 _PTHREAD_UNLOCK_FROM_MACH_THREAD(_pthread_list_lock);
1240 } else {
1241 _PTHREAD_UNLOCK(_pthread_list_lock);
1242 }
1243
1244 if (!from_mach_thread) {
1245 // PR-26275485: Mach threads will likely crash trying to run
1246 // introspection code. Since the fall out from the introspection
1247 // code not seeing the injected thread is likely less than crashing
1248 // in the introspection code, just don't make the call.
1249 _pthread_introspection_thread_create(t);
1250 }
1251 }
1252
1253 PTHREAD_ALWAYS_INLINE
1254 static inline void
1255 __pthread_undo_add_thread(pthread_t t, bool from_mach_thread)
1256 {
1257 if (from_mach_thread) {
1258 _PTHREAD_LOCK_FROM_MACH_THREAD(_pthread_list_lock);
1259 } else {
1260 _PTHREAD_LOCK(_pthread_list_lock);
1261 }
1262
1263 TAILQ_REMOVE(&__pthread_head, t, tl_plist);
1264 _pthread_count--;
1265
1266 if (from_mach_thread) {
1267 _PTHREAD_UNLOCK_FROM_MACH_THREAD(_pthread_list_lock);
1268 } else {
1269 _PTHREAD_UNLOCK(_pthread_list_lock);
1270 }
1271 }
1272
1273 PTHREAD_ALWAYS_INLINE
1274 static inline void
1275 __pthread_started_thread(pthread_t t)
1276 {
1277 mach_port_t kport = _pthread_kernel_thread(t);
1278 if (os_unlikely(!MACH_PORT_VALID(kport))) {
1279 PTHREAD_CLIENT_CRASH(kport,
1280 "Unable to allocate thread port, possible port leak");
1281 }
1282 _pthread_introspection_thread_start(t);
1283 }
1284
1285 #define _PTHREAD_CREATE_NONE 0x0
1286 #define _PTHREAD_CREATE_FROM_MACH_THREAD 0x1
1287 #define _PTHREAD_CREATE_SUSPENDED 0x2
1288
1289 static int
1290 _pthread_create(pthread_t *thread, const pthread_attr_t *attrs,
1291 void *(*start_routine)(void *), void *arg, unsigned int create_flags)
1292 {
1293 pthread_t t = NULL;
1294 void *stack = NULL;
1295 bool from_mach_thread = (create_flags & _PTHREAD_CREATE_FROM_MACH_THREAD);
1296
1297 if (attrs == NULL) {
1298 attrs = &_pthread_attr_default;
1299 } else if (attrs->sig != _PTHREAD_ATTR_SIG) {
1300 return EINVAL;
1301 }
1302
1303 unsigned int flags = PTHREAD_START_CUSTOM;
1304 if (attrs->schedset != 0) {
1305 struct sched_param p;
1306 _pthread_attr_get_schedparam(attrs, &p);
1307 flags |= PTHREAD_START_SETSCHED;
1308 flags |= ((attrs->policy & PTHREAD_START_POLICY_MASK) << PTHREAD_START_POLICY_BITSHIFT);
1309 flags |= (p.sched_priority & PTHREAD_START_IMPORTANCE_MASK);
1310 } else if (attrs->qosclass != 0) {
1311 flags |= PTHREAD_START_QOSCLASS;
1312 flags |= (attrs->qosclass & PTHREAD_START_QOSCLASS_MASK);
1313 }
1314 if (create_flags & _PTHREAD_CREATE_SUSPENDED) {
1315 flags |= PTHREAD_START_SUSPENDED;
1316 }
1317
1318 __is_threaded = 1;
1319
1320 t =_pthread_allocate(attrs, &stack, from_mach_thread);
1321 if (t == NULL) {
1322 return EAGAIN;
1323 }
1324
1325 t->arg = arg;
1326 t->fun = start_routine;
1327 __pthread_add_thread(t, from_mach_thread);
1328
1329 if (__bsdthread_create(start_routine, arg, stack, t, flags) ==
1330 (pthread_t)-1) {
1331 if (errno == EMFILE) {
1332 PTHREAD_CLIENT_CRASH(0,
1333 "Unable to allocate thread port, possible port leak");
1334 }
1335 __pthread_undo_add_thread(t, from_mach_thread);
1336 _pthread_deallocate(t, from_mach_thread);
1337 return EAGAIN;
1338 }
1339
1340 // n.b. if a thread is created detached and exits, t will be invalid
1341 *thread = t;
1342 return 0;
1343 }
1344
1345 int
1346 pthread_create(pthread_t *thread, const pthread_attr_t *attr,
1347 void *(*start_routine)(void *), void *arg)
1348 {
1349 unsigned int flags = _PTHREAD_CREATE_NONE;
1350 return _pthread_create(thread, attr, start_routine, arg, flags);
1351 }
1352
1353 int
1354 pthread_create_from_mach_thread(pthread_t *thread, const pthread_attr_t *attr,
1355 void *(*start_routine)(void *), void *arg)
1356 {
1357 unsigned int flags = _PTHREAD_CREATE_FROM_MACH_THREAD;
1358 return _pthread_create(thread, attr, start_routine, arg, flags);
1359 }
1360
1361 int
1362 pthread_create_suspended_np(pthread_t *thread, const pthread_attr_t *attr,
1363 void *(*start_routine)(void *), void *arg)
1364 {
1365 unsigned int flags = _PTHREAD_CREATE_SUSPENDED;
1366 return _pthread_create(thread, attr, start_routine, arg, flags);
1367 }
1368
1369
1370 PTHREAD_NOEXPORT_VARIANT
1371 int
1372 pthread_detach(pthread_t thread)
1373 {
1374 int res = 0;
1375 bool join = false, wake = false;
1376
1377 if (!_pthread_validate_thread_and_list_lock(thread)) {
1378 return ESRCH;
1379 }
1380
1381 if (!thread->tl_joinable) {
1382 res = EINVAL;
1383 } else if (thread->tl_exit_gate == MACH_PORT_DEAD) {
1384 // Join the thread if it's already exited.
1385 join = true;
1386 } else {
1387 thread->tl_joinable = false; // _pthread_joiner_prepost_wake uses this
1388 if (thread->tl_join_ctx) {
1389 (void)_pthread_joiner_prepost_wake(thread);
1390 wake = true;
1391 }
1392 }
1393 _PTHREAD_UNLOCK(_pthread_list_lock);
1394
1395 if (join) {
1396 pthread_join(thread, NULL);
1397 } else if (wake) {
1398 _pthread_joiner_wake(thread);
1399 }
1400 return res;
1401 }
1402
1403 PTHREAD_NOEXPORT_VARIANT
1404 int
1405 pthread_kill(pthread_t th, int sig)
1406 {
1407 if (sig < 0 || sig > NSIG) {
1408 return EINVAL;
1409 }
1410
1411 mach_port_t kport = MACH_PORT_NULL;
1412 {
1413 if (!_pthread_is_valid(th, &kport)) {
1414 return ESRCH;
1415 }
1416 }
1417
1418 int ret = __pthread_kill(kport, sig);
1419
1420 if (ret == -1) {
1421 ret = errno;
1422 }
1423 return ret;
1424 }
1425
1426 PTHREAD_NOEXPORT_VARIANT
1427 int
1428 __pthread_workqueue_setkill(int enable)
1429 {
1430 {
1431 return __bsdthread_ctl(BSDTHREAD_CTL_WORKQ_ALLOW_KILL, enable, 0, 0);
1432 }
1433 }
1434
1435
1436 /* For compatibility... */
1437
1438 pthread_t
1439 _pthread_self(void)
1440 {
1441 return pthread_self();
1442 }
1443
1444 /*
1445 * Terminate a thread.
1446 */
1447 extern int __disable_threadsignal(int);
1448
1449 PTHREAD_NORETURN
1450 static void
1451 _pthread_exit(pthread_t self, void *exit_value)
1452 {
1453 struct __darwin_pthread_handler_rec *handler;
1454
1455 // Disable signal delivery while we clean up
1456 __disable_threadsignal(1);
1457
1458 // Set cancel state to disable and type to deferred
1459 _pthread_setcancelstate_exit(self, exit_value);
1460
1461 while ((handler = self->__cleanup_stack) != 0) {
1462 (handler->__routine)(handler->__arg);
1463 self->__cleanup_stack = handler->__next;
1464 }
1465 _pthread_tsd_cleanup(self);
1466
1467 // Clear per-thread semaphore cache
1468 os_put_cached_semaphore(SEMAPHORE_NULL);
1469
1470 _pthread_terminate_invoke(self, exit_value);
1471 }
1472
1473 void
1474 pthread_exit(void *exit_value)
1475 {
1476 pthread_t self = pthread_self();
1477 if (os_unlikely(self->wqthread)) {
1478 PTHREAD_CLIENT_CRASH(0, "pthread_exit() called from a thread "
1479 "not created by pthread_create()");
1480 }
1481 _pthread_exit(self, exit_value);
1482 }
1483
1484
1485 PTHREAD_NOEXPORT_VARIANT
1486 int
1487 pthread_getschedparam(pthread_t thread, int *policy, struct sched_param *param)
1488 {
1489 if (!_pthread_validate_thread_and_list_lock(thread)) {
1490 return ESRCH;
1491 }
1492
1493 if (policy) *policy = thread->tl_policy;
1494 if (param) *param = thread->tl_param;
1495 _PTHREAD_UNLOCK(_pthread_list_lock);
1496 return 0;
1497 }
1498
1499
1500
1501 PTHREAD_ALWAYS_INLINE
1502 static inline int
1503 pthread_setschedparam_internal(pthread_t thread, mach_port_t kport, int policy,
1504 const struct sched_param *param)
1505 {
1506 policy_base_data_t bases;
1507 policy_base_t base;
1508 mach_msg_type_number_t count;
1509 kern_return_t ret;
1510
1511 switch (policy) {
1512 case SCHED_OTHER:
1513 bases.ts.base_priority = param->sched_priority;
1514 base = (policy_base_t)&bases.ts;
1515 count = POLICY_TIMESHARE_BASE_COUNT;
1516 break;
1517 case SCHED_FIFO:
1518 bases.fifo.base_priority = param->sched_priority;
1519 base = (policy_base_t)&bases.fifo;
1520 count = POLICY_FIFO_BASE_COUNT;
1521 break;
1522 case SCHED_RR:
1523 bases.rr.base_priority = param->sched_priority;
1524 /* quantum isn't public yet */
1525 bases.rr.quantum = param->quantum;
1526 base = (policy_base_t)&bases.rr;
1527 count = POLICY_RR_BASE_COUNT;
1528 break;
1529 default:
1530 return EINVAL;
1531 }
1532 ret = thread_policy(kport, policy, base, count, TRUE);
1533 return (ret != KERN_SUCCESS) ? EINVAL : 0;
1534 }
1535
1536 PTHREAD_NOEXPORT_VARIANT
1537 int
1538 pthread_setschedparam(pthread_t t, int policy, const struct sched_param *param)
1539 {
1540 mach_port_t kport = MACH_PORT_NULL;
1541 int bypass = 1;
1542
1543 // since the main thread will not get de-allocated from underneath us
1544 if (t == pthread_self() || t == main_thread()) {
1545 kport = _pthread_kernel_thread(t);
1546 } else {
1547 bypass = 0;
1548 if (!_pthread_is_valid(t, &kport)) {
1549 return ESRCH;
1550 }
1551 }
1552
1553 int res = pthread_setschedparam_internal(t, kport, policy, param);
1554 if (res) return res;
1555
1556 if (bypass) {
1557 _PTHREAD_LOCK(_pthread_list_lock);
1558 } else if (!_pthread_validate_thread_and_list_lock(t)) {
1559 // Ensure the thread is still valid.
1560 return ESRCH;
1561 }
1562
1563 t->tl_policy = policy;
1564 t->tl_param = *param;
1565 _PTHREAD_UNLOCK(_pthread_list_lock);
1566 return 0;
1567 }
1568
1569
1570 int
1571 sched_get_priority_min(int policy)
1572 {
1573 return default_priority - 16;
1574 }
1575
1576 int
1577 sched_get_priority_max(int policy)
1578 {
1579 return default_priority + 16;
1580 }
1581
1582 int
1583 pthread_equal(pthread_t t1, pthread_t t2)
1584 {
1585 return (t1 == t2);
1586 }
1587
1588 /*
1589 * Force LLVM not to optimise this to a call to __pthread_set_self, if it does
1590 * then _pthread_set_self won't be bound when secondary threads try and start up.
1591 */
1592 PTHREAD_NOINLINE
1593 void
1594 _pthread_set_self(pthread_t p)
1595 {
1596 #if VARIANT_DYLD
1597 if (os_likely(!p)) {
1598 return _pthread_set_self_dyld();
1599 }
1600 #endif // VARIANT_DYLD
1601 _pthread_set_self_internal(p);
1602 _thread_set_tsd_base(&p->tsd[0]);
1603 }
1604
1605 #if VARIANT_DYLD
1606 // _pthread_set_self_dyld is noinline+noexport to allow the option for
1607 // static libsyscall to adopt this as the entry point from mach_init if
1608 // desired
1609 PTHREAD_NOINLINE PTHREAD_NOEXPORT
1610 void
1611 _pthread_set_self_dyld(void)
1612 {
1613 pthread_t p = main_thread();
1614 p->thread_id = __thread_selfid();
1615
1616 if (os_unlikely(p->thread_id == -1ull)) {
1617 PTHREAD_INTERNAL_CRASH(0, "failed to set thread_id");
1618 }
1619
1620 // <rdar://problem/40930651> pthread self and the errno address are the
1621 // bare minimium TSD setup that dyld needs to actually function. Without
1622 // this, TSD access will fail and crash if it uses bits of Libc prior to
1623 // library initialization. __pthread_init will finish the initialization
1624 // during library init.
1625 p->tsd[_PTHREAD_TSD_SLOT_PTHREAD_SELF] = p;
1626 p->tsd[_PTHREAD_TSD_SLOT_ERRNO] = &p->err_no;
1627 _thread_set_tsd_base(&p->tsd[0]);
1628 }
1629 #endif // VARIANT_DYLD
1630
1631 PTHREAD_ALWAYS_INLINE
1632 static inline void
1633 _pthread_set_self_internal(pthread_t p)
1634 {
1635 os_atomic_store(&p->thread_id, __thread_selfid(), relaxed);
1636
1637 if (os_unlikely(p->thread_id == -1ull)) {
1638 PTHREAD_INTERNAL_CRASH(0, "failed to set thread_id");
1639 }
1640 }
1641
1642
1643 // <rdar://problem/28984807> pthread_once should have an acquire barrier
1644 PTHREAD_ALWAYS_INLINE
1645 static inline void
1646 _os_once_acquire(os_once_t *predicate, void *context, os_function_t function)
1647 {
1648 if (OS_EXPECT(os_atomic_load(predicate, acquire), ~0l) != ~0l) {
1649 _os_once(predicate, context, function);
1650 OS_COMPILER_CAN_ASSUME(*predicate == ~0l);
1651 }
1652 }
1653
1654 struct _pthread_once_context {
1655 pthread_once_t *pthread_once;
1656 void (*routine)(void);
1657 };
1658
1659 static void
1660 __pthread_once_handler(void *context)
1661 {
1662 struct _pthread_once_context *ctx = context;
1663 pthread_cleanup_push((void*)__os_once_reset, &ctx->pthread_once->once);
1664 ctx->routine();
1665 pthread_cleanup_pop(0);
1666 ctx->pthread_once->sig = _PTHREAD_ONCE_SIG;
1667 }
1668
1669 PTHREAD_NOEXPORT_VARIANT
1670 int
1671 pthread_once(pthread_once_t *once_control, void (*init_routine)(void))
1672 {
1673 struct _pthread_once_context ctx = { once_control, init_routine };
1674 do {
1675 _os_once_acquire(&once_control->once, &ctx, __pthread_once_handler);
1676 } while (once_control->sig == _PTHREAD_ONCE_SIG_init);
1677 return 0;
1678 }
1679
1680
1681 int
1682 pthread_getconcurrency(void)
1683 {
1684 return pthread_concurrency;
1685 }
1686
1687 int
1688 pthread_setconcurrency(int new_level)
1689 {
1690 if (new_level < 0) {
1691 return EINVAL;
1692 }
1693 pthread_concurrency = new_level;
1694 return 0;
1695 }
1696
1697 #if !defined(VARIANT_STATIC)
1698 void *
1699 malloc(size_t sz)
1700 {
1701 if (_pthread_malloc) {
1702 return _pthread_malloc(sz);
1703 } else {
1704 return NULL;
1705 }
1706 }
1707
1708 void
1709 free(void *p)
1710 {
1711 if (_pthread_free) {
1712 _pthread_free(p);
1713 }
1714 }
1715 #endif // VARIANT_STATIC
1716
1717 /*
1718 * Perform package initialization - called automatically when application starts
1719 */
1720 struct ProgramVars; /* forward reference */
1721
1722 #if !VARIANT_DYLD
1723 static unsigned long
1724 _pthread_strtoul(const char *p, const char **endptr, int base)
1725 {
1726 uintptr_t val = 0;
1727
1728 // Expect hex string starting with "0x"
1729 if ((base == 16 || base == 0) && p && p[0] == '0' && p[1] == 'x') {
1730 p += 2;
1731 while (1) {
1732 char c = *p;
1733 if ('0' <= c && c <= '9') {
1734 val = (val << 4) + (c - '0');
1735 } else if ('a' <= c && c <= 'f') {
1736 val = (val << 4) + (c - 'a' + 10);
1737 } else if ('A' <= c && c <= 'F') {
1738 val = (val << 4) + (c - 'A' + 10);
1739 } else {
1740 break;
1741 }
1742 ++p;
1743 }
1744 }
1745
1746 *endptr = (char *)p;
1747 return val;
1748 }
1749
1750 static int
1751 parse_main_stack_params(const char *apple[],
1752 void **stackaddr,
1753 size_t *stacksize,
1754 void **allocaddr,
1755 size_t *allocsize)
1756 {
1757 const char *p = _simple_getenv(apple, "main_stack");
1758 if (!p) return 0;
1759
1760 int ret = 0;
1761 const char *s = p;
1762
1763 *stackaddr = _pthread_strtoul(s, &s, 16);
1764 if (*s != ',') goto out;
1765
1766 *stacksize = _pthread_strtoul(s + 1, &s, 16);
1767 if (*s != ',') goto out;
1768
1769 *allocaddr = _pthread_strtoul(s + 1, &s, 16);
1770 if (*s != ',') goto out;
1771
1772 *allocsize = _pthread_strtoul(s + 1, &s, 16);
1773 if (*s != ',' && *s != 0) goto out;
1774
1775 ret = 1;
1776 out:
1777 bzero((char *)p, strlen(p));
1778 return ret;
1779 }
1780
1781 static void
1782 parse_ptr_munge_params(const char *envp[], const char *apple[])
1783 {
1784 const char *p, *s;
1785 p = _simple_getenv(apple, "ptr_munge");
1786 if (p) {
1787 _pthread_ptr_munge_token = _pthread_strtoul(p, &s, 16);
1788 bzero((char *)p, strlen(p));
1789 }
1790 #if !DEBUG
1791 if (_pthread_ptr_munge_token) return;
1792 #endif
1793 p = _simple_getenv(envp, "PTHREAD_PTR_MUNGE_TOKEN");
1794 if (p) {
1795 uintptr_t t = _pthread_strtoul(p, &s, 16);
1796 if (t) _pthread_ptr_munge_token = t;
1797 }
1798 }
1799
1800 int
1801 __pthread_init(const struct _libpthread_functions *pthread_funcs,
1802 const char *envp[], const char *apple[],
1803 const struct ProgramVars *vars __unused)
1804 {
1805 // Save our provided pushed-down functions
1806 if (pthread_funcs) {
1807 exitf = pthread_funcs->exit;
1808
1809 if (pthread_funcs->version >= 2) {
1810 _pthread_malloc = pthread_funcs->malloc;
1811 _pthread_free = pthread_funcs->free;
1812 }
1813 }
1814
1815 //
1816 // Get host information
1817 //
1818
1819 kern_return_t kr;
1820 host_flavor_t flavor = HOST_PRIORITY_INFO;
1821 mach_msg_type_number_t count = HOST_PRIORITY_INFO_COUNT;
1822 host_priority_info_data_t priority_info;
1823 host_t host = mach_host_self();
1824 kr = host_info(host, flavor, (host_info_t)&priority_info, &count);
1825 if (kr != KERN_SUCCESS) {
1826 PTHREAD_INTERNAL_CRASH(kr, "host_info() failed");
1827 } else {
1828 default_priority = (uint8_t)priority_info.user_priority;
1829 min_priority = (uint8_t)priority_info.minimum_priority;
1830 max_priority = (uint8_t)priority_info.maximum_priority;
1831 }
1832 mach_port_deallocate(mach_task_self(), host);
1833
1834 //
1835 // Set up the main thread structure
1836 //
1837
1838 // Get the address and size of the main thread's stack from the kernel.
1839 void *stackaddr = 0;
1840 size_t stacksize = 0;
1841 void *allocaddr = 0;
1842 size_t allocsize = 0;
1843 if (!parse_main_stack_params(apple, &stackaddr, &stacksize, &allocaddr, &allocsize) ||
1844 stackaddr == NULL || stacksize == 0) {
1845 // Fall back to previous bevhaior.
1846 size_t len = sizeof(stackaddr);
1847 int mib[] = { CTL_KERN, KERN_USRSTACK };
1848 if (__sysctl(mib, 2, &stackaddr, &len, NULL, 0) != 0) {
1849 #if defined(__LP64__)
1850 stackaddr = (void *)USRSTACK64;
1851 #else
1852 stackaddr = (void *)USRSTACK;
1853 #endif
1854 }
1855 stacksize = DFLSSIZ;
1856 allocaddr = 0;
1857 allocsize = 0;
1858 }
1859
1860 // Initialize random ptr_munge token from the kernel.
1861 parse_ptr_munge_params(envp, apple);
1862
1863 // libpthread.a in dyld "owns" the main thread structure itself and sets
1864 // up the tsd to point to it. So take the pthread_self() from there
1865 // and make it our main thread point.
1866 pthread_t thread = (pthread_t)_pthread_getspecific_direct(
1867 _PTHREAD_TSD_SLOT_PTHREAD_SELF);
1868 if (os_unlikely(thread == NULL)) {
1869 PTHREAD_INTERNAL_CRASH(0, "PTHREAD_SELF TSD not initialized");
1870 }
1871 _main_thread_ptr = thread;
1872
1873 PTHREAD_DEBUG_ASSERT(_pthread_attr_default.qosclass ==
1874 _pthread_default_priority(0));
1875 _pthread_struct_init(thread, &_pthread_attr_default,
1876 stackaddr, stacksize, allocaddr, allocsize);
1877 thread->tl_joinable = true;
1878
1879 // Finish initialization with common code that is reinvoked on the
1880 // child side of a fork.
1881
1882 // Finishes initialization of main thread attributes.
1883 // Initializes the thread list and add the main thread.
1884 // Calls _pthread_set_self() to prepare the main thread for execution.
1885 _pthread_main_thread_init(thread);
1886
1887 struct _pthread_registration_data registration_data;
1888 // Set up kernel entry points with __bsdthread_register.
1889 _pthread_bsdthread_init(&registration_data);
1890
1891 // Have pthread_key and pthread_mutex do their init envvar checks.
1892 _pthread_key_global_init(envp);
1893 _pthread_mutex_global_init(envp, &registration_data);
1894
1895 #if PTHREAD_DEBUG_LOG
1896 _SIMPLE_STRING path = _simple_salloc();
1897 _simple_sprintf(path, "/var/tmp/libpthread.%d.log", getpid());
1898 _pthread_debuglog = open(_simple_string(path),
1899 O_WRONLY | O_APPEND | O_CREAT | O_NOFOLLOW | O_CLOEXEC, 0666);
1900 _simple_sfree(path);
1901 _pthread_debugstart = mach_absolute_time();
1902 #endif
1903
1904 return 0;
1905 }
1906 #endif // !VARIANT_DYLD
1907
1908 PTHREAD_NOEXPORT void
1909 _pthread_main_thread_init(pthread_t p)
1910 {
1911 TAILQ_INIT(&__pthread_head);
1912 _PTHREAD_LOCK_INIT(_pthread_list_lock);
1913 _PTHREAD_LOCK_INIT(p->lock);
1914 _pthread_set_kernel_thread(p, mach_thread_self());
1915 _pthread_set_reply_port(p, mach_reply_port());
1916 p->__cleanup_stack = NULL;
1917 p->tl_join_ctx = NULL;
1918 p->tl_exit_gate = MACH_PORT_NULL;
1919 p->tsd[__TSD_SEMAPHORE_CACHE] = (void*)(uintptr_t)SEMAPHORE_NULL;
1920 p->tsd[__TSD_MACH_SPECIAL_REPLY] = 0;
1921
1922 // Initialize the list of threads with the new main thread.
1923 TAILQ_INSERT_HEAD(&__pthread_head, p, tl_plist);
1924 _pthread_count = 1;
1925
1926 _pthread_introspection_thread_start(p);
1927 }
1928
1929 PTHREAD_NOEXPORT
1930 void
1931 _pthread_main_thread_postfork_init(pthread_t p)
1932 {
1933 _pthread_main_thread_init(p);
1934 _pthread_set_self_internal(p);
1935 }
1936
1937 int
1938 sched_yield(void)
1939 {
1940 swtch_pri(0);
1941 return 0;
1942 }
1943
1944 // XXX remove
1945 void
1946 cthread_yield(void)
1947 {
1948 sched_yield();
1949 }
1950
1951 void
1952 pthread_yield_np(void)
1953 {
1954 sched_yield();
1955 }
1956
1957 // Libsystem knows about this symbol and exports it to libsyscall
1958 int
1959 pthread_current_stack_contains_np(const void *addr, size_t length)
1960 {
1961 uintptr_t begin = (uintptr_t) addr, end;
1962 uintptr_t stack_base = (uintptr_t) _pthread_self_direct()->stackbottom;
1963 uintptr_t stack_top = (uintptr_t) _pthread_self_direct()->stackaddr;
1964
1965 if (stack_base == stack_top) {
1966 return -ENOTSUP;
1967 }
1968
1969 if (__builtin_add_overflow(begin, length, &end)) {
1970 return -EINVAL;
1971 }
1972
1973 return stack_base <= begin && end <= stack_top;
1974 }
1975
1976
1977
1978 // Libsystem knows about this symbol and exports it to libsyscall
1979 PTHREAD_NOEXPORT_VARIANT
1980 void
1981 _pthread_clear_qos_tsd(mach_port_t thread_port)
1982 {
1983 if (thread_port == MACH_PORT_NULL || (uintptr_t)_pthread_getspecific_direct(_PTHREAD_TSD_SLOT_MACH_THREAD_SELF) == thread_port) {
1984 /* Clear the current thread's TSD, that can be done inline. */
1985 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS,
1986 _pthread_unspecified_priority());
1987 } else {
1988 pthread_t p;
1989
1990 _PTHREAD_LOCK(_pthread_list_lock);
1991
1992 TAILQ_FOREACH(p, &__pthread_head, tl_plist) {
1993 mach_port_t kp = _pthread_kernel_thread(p);
1994 if (thread_port == kp) {
1995 p->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] =
1996 _pthread_unspecified_priority();
1997 break;
1998 }
1999 }
2000
2001 _PTHREAD_UNLOCK(_pthread_list_lock);
2002 }
2003 }
2004
2005
2006 #pragma mark pthread/stack_np.h public interface
2007
2008
2009 #if defined(__i386__) || defined(__x86_64__) || defined(__arm__) || defined(__arm64__)
2010 #if __ARM64_ARCH_8_32__
2011 /*
2012 * arm64_32 uses 64-bit sizes for the frame pointer and
2013 * return address of a stack frame.
2014 */
2015 typedef uint64_t frame_data_addr_t;
2016 #else
2017 typedef uintptr_t frame_data_addr_t;
2018 #endif
2019
2020 struct frame_data {
2021 frame_data_addr_t frame_addr_next;
2022 frame_data_addr_t ret_addr;
2023 };
2024 #else
2025 #error ********** Unimplemented architecture
2026 #endif
2027
2028 uintptr_t
2029 pthread_stack_frame_decode_np(uintptr_t frame_addr, uintptr_t *return_addr)
2030 {
2031 struct frame_data *frame = (struct frame_data *)frame_addr;
2032
2033 if (return_addr) {
2034 #if __has_feature(ptrauth_calls)
2035 *return_addr = (uintptr_t)ptrauth_strip((void *)frame->ret_addr,
2036 ptrauth_key_return_address);
2037 #else
2038 *return_addr = (uintptr_t)frame->ret_addr;
2039 #endif /* __has_feature(ptrauth_calls) */
2040 }
2041
2042 #if __has_feature(ptrauth_calls)
2043 return (uintptr_t)ptrauth_strip((void *)frame->frame_addr_next,
2044 ptrauth_key_frame_pointer);
2045 #endif /* __has_feature(ptrauth_calls) */
2046 return (uintptr_t)frame->frame_addr_next;
2047 }
2048
2049
2050 #pragma mark pthread workqueue support routines
2051
2052
2053 PTHREAD_NOEXPORT void
2054 _pthread_bsdthread_init(struct _pthread_registration_data *data)
2055 {
2056 bzero(data, sizeof(*data));
2057 data->version = sizeof(struct _pthread_registration_data);
2058 data->dispatch_queue_offset = __PTK_LIBDISPATCH_KEY0 * sizeof(void *);
2059 data->return_to_kernel_offset = __TSD_RETURN_TO_KERNEL * sizeof(void *);
2060 data->tsd_offset = offsetof(struct _pthread, tsd);
2061 data->mach_thread_self_offset = __TSD_MACH_THREAD_SELF * sizeof(void *);
2062
2063 int rv = __bsdthread_register(thread_start, start_wqthread, (int)PTHREAD_SIZE,
2064 (void*)data, (uintptr_t)sizeof(*data), data->dispatch_queue_offset);
2065
2066 if (rv > 0) {
2067 int required_features =
2068 PTHREAD_FEATURE_FINEPRIO |
2069 PTHREAD_FEATURE_BSDTHREADCTL |
2070 PTHREAD_FEATURE_SETSELF |
2071 PTHREAD_FEATURE_QOS_MAINTENANCE |
2072 PTHREAD_FEATURE_QOS_DEFAULT;
2073 if ((rv & required_features) != required_features) {
2074 PTHREAD_INTERNAL_CRASH(rv, "Missing required kernel support");
2075 }
2076 __pthread_supported_features = rv;
2077 }
2078
2079 /*
2080 * TODO: differentiate between (-1, EINVAL) after fork (which has the side
2081 * effect of resetting the child's stack_addr_hint before bailing out) and
2082 * (-1, EINVAL) because of invalid arguments. We'd probably like to treat
2083 * the latter as fatal.
2084 *
2085 * <rdar://problem/36451838>
2086 */
2087
2088 pthread_priority_t main_qos = (pthread_priority_t)data->main_qos;
2089
2090 if (_pthread_priority_thread_qos(main_qos) != THREAD_QOS_UNSPECIFIED) {
2091 _pthread_set_main_qos(main_qos);
2092 main_thread()->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] = main_qos;
2093 }
2094
2095 if (data->stack_addr_hint) {
2096 __pthread_stack_hint = data->stack_addr_hint;
2097 }
2098
2099 if (__libdispatch_workerfunction != NULL) {
2100 // prepare the kernel for workq action
2101 (void)__workq_open();
2102 }
2103 }
2104
2105 PTHREAD_NOINLINE
2106 static void
2107 _pthread_wqthread_legacy_worker_wrap(pthread_priority_t pp)
2108 {
2109 /* Old thread priorities are inverted from where we have them in
2110 * the new flexible priority scheme. The highest priority is zero,
2111 * up to 2, with background at 3.
2112 */
2113 pthread_workqueue_function_t func = (pthread_workqueue_function_t)__libdispatch_workerfunction;
2114 bool overcommit = (pp & _PTHREAD_PRIORITY_OVERCOMMIT_FLAG);
2115 int opts = overcommit ? WORKQ_ADDTHREADS_OPTION_OVERCOMMIT : 0;
2116
2117 switch (_pthread_priority_thread_qos(pp)) {
2118 case THREAD_QOS_USER_INITIATED:
2119 return (*func)(WORKQ_HIGH_PRIOQUEUE, opts, NULL);
2120 case THREAD_QOS_LEGACY:
2121 /* B&I builders can't pass a QOS_CLASS_DEFAULT thread to dispatch, for fear of the QoS being
2122 * picked up by NSThread (et al) and transported around the system. So change the TSD to
2123 * make this thread look like QOS_CLASS_USER_INITIATED even though it will still run as legacy.
2124 */
2125 _pthread_setspecific_direct(_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS,
2126 _pthread_priority_make_from_thread_qos(THREAD_QOS_USER_INITIATED, 0, 0));
2127 return (*func)(WORKQ_DEFAULT_PRIOQUEUE, opts, NULL);
2128 case THREAD_QOS_UTILITY:
2129 return (*func)(WORKQ_LOW_PRIOQUEUE, opts, NULL);
2130 case THREAD_QOS_BACKGROUND:
2131 return (*func)(WORKQ_BG_PRIOQUEUE, opts, NULL);
2132 }
2133 PTHREAD_INTERNAL_CRASH(pp, "Invalid pthread priority for the legacy interface");
2134 }
2135
2136 PTHREAD_ALWAYS_INLINE
2137 static inline pthread_priority_t
2138 _pthread_wqthread_priority(int flags)
2139 {
2140 pthread_priority_t pp = 0;
2141 thread_qos_t qos;
2142
2143 if (flags & WQ_FLAG_THREAD_KEVENT) {
2144 pp |= _PTHREAD_PRIORITY_NEEDS_UNBIND_FLAG;
2145 }
2146 if (flags & WQ_FLAG_THREAD_EVENT_MANAGER) {
2147 return pp | _PTHREAD_PRIORITY_EVENT_MANAGER_FLAG;
2148 }
2149
2150 if (flags & WQ_FLAG_THREAD_OVERCOMMIT) {
2151 pp |= _PTHREAD_PRIORITY_OVERCOMMIT_FLAG;
2152 }
2153 if (flags & WQ_FLAG_THREAD_PRIO_QOS) {
2154 qos = (thread_qos_t)(flags & WQ_FLAG_THREAD_PRIO_MASK);
2155 pp = _pthread_priority_make_from_thread_qos(qos, 0, pp);
2156 } else if (flags & WQ_FLAG_THREAD_PRIO_SCHED) {
2157 pp |= _PTHREAD_PRIORITY_SCHED_PRI_MASK;
2158 pp |= (flags & WQ_FLAG_THREAD_PRIO_MASK);
2159 } else {
2160 PTHREAD_INTERNAL_CRASH(flags, "Missing priority");
2161 }
2162 return pp;
2163 }
2164
2165 PTHREAD_NOINLINE
2166 static void
2167 _pthread_wqthread_setup(pthread_t self, mach_port_t kport, void *stacklowaddr,
2168 int flags)
2169 {
2170 void *stackaddr = self;
2171 size_t stacksize = (uintptr_t)self - (uintptr_t)stacklowaddr;
2172
2173 _pthread_struct_init(self, &_pthread_attr_default, stackaddr, stacksize,
2174 PTHREAD_ALLOCADDR(stackaddr, stacksize),
2175 PTHREAD_ALLOCSIZE(stackaddr, stacksize));
2176
2177 _pthread_set_kernel_thread(self, kport);
2178 self->wqthread = 1;
2179 self->wqkillset = 0;
2180 self->tl_joinable = false;
2181
2182 // Update the running thread count and set childrun bit.
2183 if (os_unlikely((flags & WQ_FLAG_THREAD_TSD_BASE_SET) == 0)) {
2184 PTHREAD_INTERNAL_CRASH(flags,
2185 "thread_set_tsd_base() wasn't called by the kernel");
2186 }
2187 _pthread_set_self_internal(self);
2188 __pthread_add_thread(self, false);
2189 __pthread_started_thread(self);
2190 }
2191
2192 PTHREAD_NORETURN PTHREAD_NOINLINE
2193 static void
2194 _pthread_wqthread_exit(pthread_t self)
2195 {
2196 pthread_priority_t pp;
2197 thread_qos_t qos;
2198
2199 pp = (pthread_priority_t)self->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS];
2200 qos = _pthread_priority_thread_qos(pp);
2201 if (qos == THREAD_QOS_UNSPECIFIED || qos > WORKQ_THREAD_QOS_CLEANUP) {
2202 // Reset QoS to something low for the cleanup process
2203 pp = _pthread_priority_make_from_thread_qos(WORKQ_THREAD_QOS_CLEANUP, 0, 0);
2204 self->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] = (void *)pp;
2205 }
2206
2207 _pthread_exit(self, NULL);
2208 }
2209
2210 // workqueue entry point from kernel
2211 void
2212 _pthread_wqthread(pthread_t self, mach_port_t kport, void *stacklowaddr,
2213 void *keventlist, int flags, int nkevents)
2214 {
2215 if ((flags & WQ_FLAG_THREAD_REUSE) == 0) {
2216 _pthread_wqthread_setup(self, kport, stacklowaddr, flags);
2217 }
2218
2219 pthread_priority_t pp;
2220
2221 if (flags & WQ_FLAG_THREAD_OUTSIDEQOS) {
2222 self->wq_outsideqos = 1;
2223 pp = _pthread_priority_make_from_thread_qos(THREAD_QOS_LEGACY, 0,
2224 _PTHREAD_PRIORITY_FALLBACK_FLAG);
2225 } else {
2226 self->wq_outsideqos = 0;
2227 pp = _pthread_wqthread_priority(flags);
2228 }
2229
2230 self->tsd[_PTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS] = (void *)pp;
2231
2232 // avoid spills on the stack hard to keep used stack space minimal
2233 if (os_unlikely(nkevents == WORKQ_EXIT_THREAD_NKEVENT)) {
2234 _pthread_wqthread_exit(self);
2235 } else if (flags & WQ_FLAG_THREAD_WORKLOOP) {
2236 kqueue_id_t *kqidptr = (kqueue_id_t *)keventlist - 1;
2237 self->fun = (void *(*)(void*))__libdispatch_workloopfunction;
2238 self->arg = keventlist;
2239 self->wq_nevents = nkevents;
2240 (*__libdispatch_workloopfunction)(kqidptr, &self->arg, &self->wq_nevents);
2241 __workq_kernreturn(WQOPS_THREAD_WORKLOOP_RETURN, self->arg, self->wq_nevents, 0);
2242 } else if (flags & WQ_FLAG_THREAD_KEVENT) {
2243 self->fun = (void *(*)(void*))__libdispatch_keventfunction;
2244 self->arg = keventlist;
2245 self->wq_nevents = nkevents;
2246 (*__libdispatch_keventfunction)(&self->arg, &self->wq_nevents);
2247 __workq_kernreturn(WQOPS_THREAD_KEVENT_RETURN, self->arg, self->wq_nevents, 0);
2248 } else {
2249 self->fun = (void *(*)(void*))__libdispatch_workerfunction;
2250 self->arg = (void *)(uintptr_t)pp;
2251 self->wq_nevents = 0;
2252 if (os_likely(__workq_newapi)) {
2253 (*__libdispatch_workerfunction)(pp);
2254 } else {
2255 _pthread_wqthread_legacy_worker_wrap(pp);
2256 }
2257 __workq_kernreturn(WQOPS_THREAD_RETURN, NULL, 0, 0);
2258 }
2259
2260 _os_set_crash_log_cause_and_message(self->err_no,
2261 "BUG IN LIBPTHREAD: __workq_kernreturn returned");
2262 /*
2263 * 52858993: we should never return but the compiler insists on outlining,
2264 * so the __builtin_trap() is in _start_wqthread in pthread_asm.s
2265 */
2266 }
2267
2268
2269 #pragma mark pthread workqueue API for libdispatch
2270
2271
2272 _Static_assert(WORKQ_KEVENT_EVENT_BUFFER_LEN == WQ_KEVENT_LIST_LEN,
2273 "Kernel and userland should agree on the event list size");
2274
2275 void
2276 pthread_workqueue_setdispatchoffset_np(int offset)
2277 {
2278 __workq_kernreturn(WQOPS_QUEUE_NEWSPISUPP, NULL, offset, 0x00);
2279 }
2280
2281 int
2282 pthread_workqueue_setup(struct pthread_workqueue_config *cfg, size_t cfg_size)
2283 {
2284 int rv = EBUSY;
2285 struct workq_dispatch_config wdc_cfg;
2286 size_t min_size = 0;
2287
2288 if (cfg_size < sizeof(uint32_t)) {
2289 return EINVAL;
2290 }
2291
2292 switch (cfg->version) {
2293 case 1:
2294 min_size = offsetof(struct pthread_workqueue_config, queue_label_offs);
2295 break;
2296 case 2:
2297 min_size = sizeof(struct pthread_workqueue_config);
2298 break;
2299 default:
2300 return EINVAL;
2301 }
2302
2303 if (!cfg || cfg_size < min_size) {
2304 return EINVAL;
2305 }
2306
2307 if (cfg->flags & ~PTHREAD_WORKQUEUE_CONFIG_SUPPORTED_FLAGS ||
2308 cfg->version < PTHREAD_WORKQUEUE_CONFIG_MIN_SUPPORTED_VERSION) {
2309 return ENOTSUP;
2310 }
2311
2312 if (__libdispatch_workerfunction == NULL) {
2313 __workq_newapi = true;
2314
2315 wdc_cfg.wdc_version = WORKQ_DISPATCH_CONFIG_VERSION;
2316 wdc_cfg.wdc_flags = 0;
2317 wdc_cfg.wdc_queue_serialno_offs = cfg->queue_serialno_offs;
2318 #if WORKQ_DISPATCH_CONFIG_VERSION >= 2
2319 wdc_cfg.wdc_queue_label_offs = cfg->queue_label_offs;
2320 #endif
2321
2322 // Tell the kernel about dispatch internals
2323 rv = (int) __workq_kernreturn(WQOPS_SETUP_DISPATCH, &wdc_cfg, sizeof(wdc_cfg), 0);
2324 if (rv == -1) {
2325 return errno;
2326 } else {
2327 __libdispatch_keventfunction = cfg->kevent_cb;
2328 __libdispatch_workloopfunction = cfg->workloop_cb;
2329 __libdispatch_workerfunction = cfg->workq_cb;
2330
2331 // Prepare the kernel for workq action
2332 (void)__workq_open();
2333 if (__is_threaded == 0) {
2334 __is_threaded = 1;
2335 }
2336
2337 return 0;
2338 }
2339 }
2340
2341 return rv;
2342 }
2343
2344 int
2345 _pthread_workqueue_init_with_workloop(pthread_workqueue_function2_t queue_func,
2346 pthread_workqueue_function_kevent_t kevent_func,
2347 pthread_workqueue_function_workloop_t workloop_func,
2348 int offset, int flags)
2349 {
2350 struct pthread_workqueue_config cfg = {
2351 .version = PTHREAD_WORKQUEUE_CONFIG_VERSION,
2352 .flags = 0,
2353 .workq_cb = queue_func,
2354 .kevent_cb = kevent_func,
2355 .workloop_cb = workloop_func,
2356 .queue_serialno_offs = offset,
2357 .queue_label_offs = 0,
2358 };
2359
2360 return pthread_workqueue_setup(&cfg, sizeof(cfg));
2361 }
2362
2363 int
2364 _pthread_workqueue_init_with_kevent(pthread_workqueue_function2_t queue_func,
2365 pthread_workqueue_function_kevent_t kevent_func,
2366 int offset, int flags)
2367 {
2368 return _pthread_workqueue_init_with_workloop(queue_func, kevent_func, NULL, offset, flags);
2369 }
2370
2371 int
2372 _pthread_workqueue_init(pthread_workqueue_function2_t func, int offset, int flags)
2373 {
2374 return _pthread_workqueue_init_with_kevent(func, NULL, offset, flags);
2375 }
2376
2377 int
2378 pthread_workqueue_setdispatch_np(pthread_workqueue_function_t worker_func)
2379 {
2380 struct pthread_workqueue_config cfg = {
2381 .version = PTHREAD_WORKQUEUE_CONFIG_VERSION,
2382 .flags = 0,
2383 .workq_cb = (uint64_t)(pthread_workqueue_function2_t)worker_func,
2384 .kevent_cb = 0,
2385 .workloop_cb = 0,
2386 .queue_serialno_offs = 0,
2387 .queue_label_offs = 0,
2388 };
2389
2390 return pthread_workqueue_setup(&cfg, sizeof(cfg));
2391 }
2392
2393 int
2394 _pthread_workqueue_supported(void)
2395 {
2396 if (os_unlikely(!__pthread_supported_features)) {
2397 PTHREAD_INTERNAL_CRASH(0, "libpthread has not been initialized");
2398 }
2399
2400 return __pthread_supported_features;
2401 }
2402
2403 int
2404 pthread_workqueue_addthreads_np(int queue_priority, int options, int numthreads)
2405 {
2406 int res = 0;
2407
2408 // Cannot add threads without a worker function registered.
2409 if (__libdispatch_workerfunction == NULL) {
2410 return EPERM;
2411 }
2412
2413 pthread_priority_t kp = 0;
2414 int compat_priority = queue_priority & WQ_FLAG_THREAD_PRIO_MASK;
2415 int flags = 0;
2416
2417 if (options & WORKQ_ADDTHREADS_OPTION_OVERCOMMIT) {
2418 flags = _PTHREAD_PRIORITY_OVERCOMMIT_FLAG;
2419 }
2420
2421 #pragma clang diagnostic push
2422 #pragma clang diagnostic ignored "-Wdeprecated-declarations"
2423 kp = _pthread_qos_class_encode_workqueue(compat_priority, flags);
2424 #pragma clang diagnostic pop
2425
2426 res = __workq_kernreturn(WQOPS_QUEUE_REQTHREADS, NULL, numthreads, (int)kp);
2427 if (res == -1) {
2428 res = errno;
2429 }
2430 return res;
2431 }
2432
2433 bool
2434 _pthread_workqueue_should_narrow(pthread_priority_t pri)
2435 {
2436 int res = __workq_kernreturn(WQOPS_SHOULD_NARROW, NULL, (int)pri, 0);
2437 if (res == -1) {
2438 return false;
2439 }
2440 return res;
2441 }
2442
2443 int
2444 _pthread_workqueue_addthreads(int numthreads, pthread_priority_t priority)
2445 {
2446 int res = 0;
2447
2448 if (__libdispatch_workerfunction == NULL) {
2449 return EPERM;
2450 }
2451
2452 #if TARGET_OS_OSX
2453 // <rdar://problem/37687655> Legacy simulators fail to boot
2454 //
2455 // Older sims set the deprecated _PTHREAD_PRIORITY_ROOTQUEUE_FLAG wrongly,
2456 // which is aliased to _PTHREAD_PRIORITY_SCHED_PRI_FLAG and that XNU
2457 // validates and rejects.
2458 //
2459 // As a workaround, forcefully unset this bit that cannot be set here
2460 // anyway.
2461 priority &= ~_PTHREAD_PRIORITY_SCHED_PRI_FLAG;
2462 #endif
2463
2464 res = __workq_kernreturn(WQOPS_QUEUE_REQTHREADS, NULL, numthreads, (int)priority);
2465 if (res == -1) {
2466 res = errno;
2467 }
2468 return res;
2469 }
2470
2471 int
2472 _pthread_workqueue_set_event_manager_priority(pthread_priority_t priority)
2473 {
2474 int res = __workq_kernreturn(WQOPS_SET_EVENT_MANAGER_PRIORITY, NULL, (int)priority, 0);
2475 if (res == -1) {
2476 res = errno;
2477 }
2478 return res;
2479 }
2480
2481 int
2482 _pthread_workloop_create(uint64_t workloop_id, uint64_t options, pthread_attr_t *attr)
2483 {
2484 struct kqueue_workloop_params params = {
2485 .kqwlp_version = sizeof(struct kqueue_workloop_params),
2486 .kqwlp_id = workloop_id,
2487 .kqwlp_flags = 0,
2488 };
2489
2490 if (!attr) {
2491 return EINVAL;
2492 }
2493
2494 if (attr->schedset) {
2495 params.kqwlp_flags |= KQ_WORKLOOP_CREATE_SCHED_PRI;
2496 params.kqwlp_sched_pri = attr->param.sched_priority;
2497 }
2498
2499 if (attr->policyset) {
2500 params.kqwlp_flags |= KQ_WORKLOOP_CREATE_SCHED_POL;
2501 params.kqwlp_sched_pol = attr->policy;
2502 }
2503
2504 if (attr->cpupercentset) {
2505 params.kqwlp_flags |= KQ_WORKLOOP_CREATE_CPU_PERCENT;
2506 params.kqwlp_cpu_percent = attr->cpupercent;
2507 params.kqwlp_cpu_refillms = attr->refillms;
2508 }
2509
2510 int res = __kqueue_workloop_ctl(KQ_WORKLOOP_CREATE, 0, &params,
2511 sizeof(params));
2512 if (res == -1) {
2513 res = errno;
2514 }
2515 return res;
2516 }
2517
2518 int
2519 _pthread_workloop_destroy(uint64_t workloop_id)
2520 {
2521 struct kqueue_workloop_params params = {
2522 .kqwlp_version = sizeof(struct kqueue_workloop_params),
2523 .kqwlp_id = workloop_id,
2524 };
2525
2526 int res = __kqueue_workloop_ctl(KQ_WORKLOOP_DESTROY, 0, &params,
2527 sizeof(params));
2528 if (res == -1) {
2529 res = errno;
2530 }
2531 return res;
2532 }
2533
2534
2535 #pragma mark Introspection SPI for libpthread.
2536
2537
2538 static pthread_introspection_hook_t _pthread_introspection_hook;
2539
2540 pthread_introspection_hook_t
2541 pthread_introspection_hook_install(pthread_introspection_hook_t hook)
2542 {
2543 pthread_introspection_hook_t prev;
2544 prev = _pthread_atomic_xchg_ptr((void**)&_pthread_introspection_hook, hook);
2545 return prev;
2546 }
2547
2548 PTHREAD_NOINLINE
2549 static void
2550 _pthread_introspection_hook_callout_thread_create(pthread_t t)
2551 {
2552 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_CREATE, t, t,
2553 PTHREAD_SIZE);
2554 }
2555
2556 static inline void
2557 _pthread_introspection_thread_create(pthread_t t)
2558 {
2559 if (os_fastpath(!_pthread_introspection_hook)) return;
2560 _pthread_introspection_hook_callout_thread_create(t);
2561 }
2562
2563 PTHREAD_NOINLINE
2564 static void
2565 _pthread_introspection_hook_callout_thread_start(pthread_t t)
2566 {
2567 size_t freesize;
2568 void *freeaddr;
2569 if (t == main_thread()) {
2570 size_t stacksize = t->stackaddr - t->stackbottom;
2571 freesize = stacksize + t->guardsize;
2572 freeaddr = t->stackaddr - freesize;
2573 } else {
2574 freesize = t->freesize - PTHREAD_SIZE;
2575 freeaddr = t->freeaddr;
2576 }
2577 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_START, t,
2578 freeaddr, freesize);
2579 }
2580
2581 static inline void
2582 _pthread_introspection_thread_start(pthread_t t)
2583 {
2584 if (os_fastpath(!_pthread_introspection_hook)) return;
2585 _pthread_introspection_hook_callout_thread_start(t);
2586 }
2587
2588 PTHREAD_NOINLINE
2589 static void
2590 _pthread_introspection_hook_callout_thread_terminate(pthread_t t)
2591 {
2592 size_t freesize;
2593 void *freeaddr;
2594 if (t == main_thread()) {
2595 size_t stacksize = t->stackaddr - t->stackbottom;
2596 freesize = stacksize + t->guardsize;
2597 freeaddr = t->stackaddr - freesize;
2598 } else {
2599 freesize = t->freesize - PTHREAD_SIZE;
2600 freeaddr = t->freeaddr;
2601 }
2602 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_TERMINATE, t,
2603 freeaddr, freesize);
2604 }
2605
2606 static inline void
2607 _pthread_introspection_thread_terminate(pthread_t t)
2608 {
2609 if (os_fastpath(!_pthread_introspection_hook)) return;
2610 _pthread_introspection_hook_callout_thread_terminate(t);
2611 }
2612
2613 PTHREAD_NOINLINE
2614 static void
2615 _pthread_introspection_hook_callout_thread_destroy(pthread_t t)
2616 {
2617 _pthread_introspection_hook(PTHREAD_INTROSPECTION_THREAD_DESTROY, t, t,
2618 PTHREAD_SIZE);
2619 }
2620
2621 static inline void
2622 _pthread_introspection_thread_destroy(pthread_t t)
2623 {
2624 if (os_fastpath(!_pthread_introspection_hook)) return;
2625 _pthread_introspection_hook_callout_thread_destroy(t);
2626 }
2627
2628
2629 #if !VARIANT_DYLD
2630 #pragma mark libplatform shims
2631
2632 #include <platform/string.h>
2633
2634 // pthread_setup initializes large structures to 0,
2635 // which the compiler turns into a library call to memset.
2636 //
2637 // To avoid linking against Libc, provide a simple wrapper
2638 // that calls through to the libplatform primitives
2639
2640 #undef memset
2641 PTHREAD_NOEXPORT
2642 void *
2643 memset(void *b, int c, size_t len)
2644 {
2645 return _platform_memset(b, c, len);
2646 }
2647
2648 #undef bzero
2649 PTHREAD_NOEXPORT
2650 void
2651 bzero(void *s, size_t n)
2652 {
2653 _platform_bzero(s, n);
2654 }
2655
2656 #undef memcpy
2657 PTHREAD_NOEXPORT
2658 void *
2659 memcpy(void* a, const void* b, unsigned long s)
2660 {
2661 return _platform_memmove(a, b, s);
2662 }
2663
2664 #endif // !VARIANT_DYLD