this repo has no description
1#ifndef _ELFLOADER_DTHREADS_H_
2#define _ELFLOADER_DTHREADS_H_
3
4// Darwin + pthreads = dthreads ;)
5// also: "dwq" stands for "Darwin workqueue"
6
7#include <stdint.h>
8
9#define DARWIN_TAILQ_ENTRY(type) \
10 struct { \
11 struct type* tqe_next; \
12 struct type** tqe_prev; \
13 }
14
15typedef struct darwin_os_unfair_lock_s {
16 uint32_t _os_unfair_lock_opaque;
17} darwin_os_unfair_lock;
18
19typedef darwin_os_unfair_lock dthread_lock;
20
21typedef int darwin_errno_t;
22
23struct __darwin_pthread_handler_rec;
24struct dthread_join_context_s;
25
26struct darwin_sched_param {
27 int sched_priority;
28 int quantum;
29};
30
31#define DARWIN_MAXTHREADNAMESIZE 64
32
33// remember to change this as necessary depending on our target Darwin-based OS (e.g. if we ever add support for iOS)
34#if 0 /* TARGET_OS_EMBEDDED */
35#define DARWIN_EXTERNAL_POSIX_THREAD_KEYS_MAX 256
36#define DARWIN_INTERNAL_POSIX_THREAD_KEYS_MAX 256
37#define DARWIN_INTERNAL_POSIX_THREAD_KEYS_END 512
38#else
39#define DARWIN_EXTERNAL_POSIX_THREAD_KEYS_MAX 512
40#define DARWIN_INTERNAL_POSIX_THREAD_KEYS_MAX 256
41#define DARWIN_INTERNAL_POSIX_THREAD_KEYS_END 768
42#endif
43
44#define DTHREAD_TSD_SLOT_PTHREAD_SELF 0
45#define DTHREAD_TSD_SLOT_ERRNO 1
46#define DTHREAD_TSD_SLOT_MIG_REPLY 2
47#define DTHREAD_TSD_SLOT_MACH_THREAD_SELF 3
48#define DTHREAD_TSD_SLOT_PTHREAD_QOS_CLASS 4
49#define DTHREAD_TSD_SLOT_RETURN_TO_KERNEL 5
50#define DTHREAD_TSD_SLOT_PTR_MUNGE 7
51#define DTHREAD_TSD_SLOT_MACH_SPECIAL_REPLY 8
52
53#define DTHREAD_START_TSD_BASE_SET 0x10000000
54
55#define DTHREAD_CANCEL_ENABLE 0x01
56#define DTHREAD_CANCEL_DEFERRED 0x02
57
58#define DTHREAD_INHERIT_SCHED 1
59
60#define DARWIN_POLICY_TIMESHARE 1
61
62#define DARWIN_THREAD_QOS_LEGACY 4
63
64#define DTHREAD_PRIORITY_QOS_CLASS_SHIFT 8ULL
65
66#define DTHREAD_PRIORITY_PRIORITY_MASK 0x000000ff
67
68#define DTHREAD_DEFAULT_PRIORITY (1U << (DARWIN_THREAD_QOS_LEGACY - 1 + DTHREAD_PRIORITY_QOS_CLASS_SHIFT)) | ((uint8_t)-1 & DTHREAD_PRIORITY_PRIORITY_MASK)
69
70#define DWQ_FLAG_THREAD_TSD_BASE_SET 0x00200000
71
72// ***
73// KEEP IN SYNC WITH THE DEFINITION OF `struct _pthread` IN libpthread's `internal.h`
74// ***
75//
76// that also means that we have to remember to account for changes in some of the things it depends on,
77// like `os_unfair_lock`, `TAILQ_ENTRY`, `sched_param`, and more
78//
79// (although, i doubt that the total size of the structure will ever change; there's a comment in the real
80// structure definition that says that they have to keep the TSD offset stable to not break older software)
81typedef struct _dthread {
82 long sig;
83 struct __darwin_pthread_handler_rec* __cleanup_stack;
84
85 DARWIN_TAILQ_ENTRY(_pthread) tl_plist;
86 struct dthread_join_context_s* tl_join_ctx;
87 void* tl_exit_value;
88 uint32_t tl_policy:8,
89 tl_joinable:1,
90 tl_joiner_cleans_up:1,
91 tl_has_custom_stack:1,
92 __tl_pad:21;
93 uint32_t tl_exit_gate;
94 struct darwin_sched_param tl_param;
95 void* __unused_padding;
96
97 dthread_lock lock;
98 uint16_t max_tsd_key;
99 uint16_t inherit:8,
100 kernalloc:1,
101 schedset:1,
102 wqthread:1,
103 wqkillset:1,
104 __flags_pad:4;
105
106 char pthread_name[DARWIN_MAXTHREADNAMESIZE];
107
108 void* (*fun)(void*);
109 void* arg;
110 int wq_nevents;
111 bool wq_outsideqos;
112 uint8_t canceled;
113 uint16_t cancel_state;
114 darwin_errno_t cancel_error;
115 darwin_errno_t err_no;
116
117 void* stackaddr;
118 void* stackbottom;
119 void* freeaddr;
120 size_t freesize;
121 size_t guardsize;
122
123 __attribute__((aligned(8)))
124 uint64_t thread_id;
125
126 __attribute__((aligned(16)))
127 void* tsd[DARWIN_EXTERNAL_POSIX_THREAD_KEYS_MAX + DARWIN_INTERNAL_POSIX_THREAD_KEYS_MAX];
128}* dthread_t;
129
130#endif // _ELFLOADER_DTHREADS_H_