1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef nspr_darwin_defs_h___
7 #define nspr_darwin_defs_h___
8 
9 #include "prthread.h"
10 
11 #include <libkern/OSAtomic.h>
12 #include <sys/syscall.h>
13 
14 #ifdef __APPLE__
15 #include <AvailabilityMacros.h>
16 #include <TargetConditionals.h>
17 #endif
18 
19 #define PR_LINKER_ARCH	"darwin"
20 #define _PR_SI_SYSNAME  "DARWIN"
21 #ifdef __i386__
22 #define _PR_SI_ARCHITECTURE "x86"
23 #elif defined(__x86_64__)
24 #define _PR_SI_ARCHITECTURE "x86-64"
25 #elif defined(__ppc__)
26 #define _PR_SI_ARCHITECTURE "ppc"
27 #elif defined(__arm__)
28 #define _PR_SI_ARCHITECTURE "arm"
29 #elif defined(__aarch64__)
30 #define _PR_SI_ARCHITECTURE "aarch64"
31 #else
32 #error "Unknown CPU architecture"
33 #endif
34 #define PR_DLL_SUFFIX		".dylib"
35 
36 #define _PR_VMBASE              0x30000000
37 #define _PR_STACK_VMBASE	0x50000000
38 #define _MD_DEFAULT_STACK_SIZE	65536L
39 #define _MD_MMAP_FLAGS          MAP_PRIVATE
40 
41 #undef  HAVE_STACK_GROWING_UP
42 #define HAVE_DLL
43 #if defined(__x86_64__) || TARGET_OS_IPHONE
44 #define USE_DLFCN
45 #else
46 #define USE_MACH_DYLD
47 #endif
48 #define _PR_HAVE_SOCKADDR_LEN
49 #define _PR_STAT_HAS_ST_ATIMESPEC
50 #define _PR_HAVE_LARGE_OFF_T
51 #define _PR_HAVE_SYSV_SEMAPHORES
52 #define PR_HAVE_SYSV_NAMED_SHARED_MEMORY
53 
54 #define _PR_INET6
55 /*
56  * I'd prefer to use getipnodebyname and getipnodebyaddr but the
57  * getipnodebyname(3) man page on Mac OS X 10.2 says they are not
58  * thread-safe.  AI_V4MAPPED|AI_ADDRCONFIG doesn't work either.
59  */
60 #define _PR_HAVE_GETHOSTBYNAME2
61 #define _PR_HAVE_GETADDRINFO
62 /*
63  * On Mac OS X 10.2, gethostbyaddr fails with h_errno=NO_RECOVERY
64  * if you pass an IPv4-mapped IPv6 address to it.
65  */
66 #define _PR_GHBA_DISALLOW_V4MAPPED
67 #ifdef __APPLE__
68 #if !defined(MAC_OS_X_VERSION_10_3) || \
69     MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3
70 /*
71  * socket(AF_INET6) fails with EPROTONOSUPPORT on Mac OS X 10.1.
72  * IPv6 under OS X 10.2 and below is not complete (see bug 222031).
73  */
74 #define _PR_INET6_PROBE
75 #endif /* DT < 10.3 */
76 #if defined(MAC_OS_X_VERSION_10_2) && \
77     MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_2
78 /* Mac OS X 10.2 has inet_ntop and inet_pton. */
79 #define _PR_HAVE_INET_NTOP
80 #endif /* DT >= 10.2 */
81 #endif /* __APPLE__ */
82 #define _PR_IPV6_V6ONLY_PROBE
83 /* The IPV6_V6ONLY socket option is not defined on Mac OS X 10.1. */
84 #ifndef IPV6_V6ONLY
85 #define IPV6_V6ONLY 27
86 #endif
87 
88 #ifdef __ppc__
89 #define _PR_HAVE_ATOMIC_OPS
90 #define _MD_INIT_ATOMIC()
91 extern PRInt32 _PR_DarwinPPC_AtomicIncrement(PRInt32 *val);
92 #define _MD_ATOMIC_INCREMENT(val)   _PR_DarwinPPC_AtomicIncrement(val)
93 extern PRInt32 _PR_DarwinPPC_AtomicDecrement(PRInt32 *val);
94 #define _MD_ATOMIC_DECREMENT(val)   _PR_DarwinPPC_AtomicDecrement(val)
95 extern PRInt32 _PR_DarwinPPC_AtomicSet(PRInt32 *val, PRInt32 newval);
96 #define _MD_ATOMIC_SET(val, newval) _PR_DarwinPPC_AtomicSet(val, newval)
97 extern PRInt32 _PR_DarwinPPC_AtomicAdd(PRInt32 *ptr, PRInt32 val);
98 #define _MD_ATOMIC_ADD(ptr, val)    _PR_DarwinPPC_AtomicAdd(ptr, val)
99 #endif /* __ppc__ */
100 
101 #ifdef __i386__
102 #define _PR_HAVE_ATOMIC_OPS
103 #define _MD_INIT_ATOMIC()
104 extern PRInt32 _PR_Darwin_x86_AtomicIncrement(PRInt32 *val);
105 #define _MD_ATOMIC_INCREMENT(val)   _PR_Darwin_x86_AtomicIncrement(val)
106 extern PRInt32 _PR_Darwin_x86_AtomicDecrement(PRInt32 *val);
107 #define _MD_ATOMIC_DECREMENT(val)   _PR_Darwin_x86_AtomicDecrement(val)
108 extern PRInt32 _PR_Darwin_x86_AtomicSet(PRInt32 *val, PRInt32 newval);
109 #define _MD_ATOMIC_SET(val, newval) _PR_Darwin_x86_AtomicSet(val, newval)
110 extern PRInt32 _PR_Darwin_x86_AtomicAdd(PRInt32 *ptr, PRInt32 val);
111 #define _MD_ATOMIC_ADD(ptr, val)    _PR_Darwin_x86_AtomicAdd(ptr, val)
112 #endif /* __i386__ */
113 
114 #ifdef __x86_64__
115 #define _PR_HAVE_ATOMIC_OPS
116 #define _MD_INIT_ATOMIC()
117 extern PRInt32 _PR_Darwin_x86_64_AtomicIncrement(PRInt32 *val);
118 #define _MD_ATOMIC_INCREMENT(val)   _PR_Darwin_x86_64_AtomicIncrement(val)
119 extern PRInt32 _PR_Darwin_x86_64_AtomicDecrement(PRInt32 *val);
120 #define _MD_ATOMIC_DECREMENT(val)   _PR_Darwin_x86_64_AtomicDecrement(val)
121 extern PRInt32 _PR_Darwin_x86_64_AtomicSet(PRInt32 *val, PRInt32 newval);
122 #define _MD_ATOMIC_SET(val, newval) _PR_Darwin_x86_64_AtomicSet(val, newval)
123 extern PRInt32 _PR_Darwin_x86_64_AtomicAdd(PRInt32 *ptr, PRInt32 val);
124 #define _MD_ATOMIC_ADD(ptr, val)    _PR_Darwin_x86_64_AtomicAdd(ptr, val)
125 #endif /* __x86_64__ */
126 
127 #if defined(__arm__) || defined(__aarch64__)
128 #define _PR_HAVE_ATOMIC_OPS
129 #define _MD_INIT_ATOMIC()
130 #define _MD_ATOMIC_INCREMENT(val)   OSAtomicIncrement32(val)
131 #define _MD_ATOMIC_DECREMENT(val)   OSAtomicDecrement32(val)
_MD_ATOMIC_SET(PRInt32 * val,PRInt32 newval)132 static inline PRInt32 _MD_ATOMIC_SET(PRInt32 *val, PRInt32 newval)
133 {
134     PRInt32 oldval;
135     do {
136         oldval = *val;
137     } while (!OSAtomicCompareAndSwap32(oldval, newval, val));
138     return oldval;
139 }
140 #define _MD_ATOMIC_ADD(ptr, val)    OSAtomicAdd32(val, ptr)
141 #endif /* __arm__ || __aarch64__ */
142 
143 #define USE_SETJMP
144 
145 #if !defined(_PR_PTHREADS)
146 
147 #include <setjmp.h>
148 
149 #define PR_CONTEXT_TYPE	jmp_buf
150 
151 #define CONTEXT(_th)       ((_th)->md.context)
152 #define _MD_GET_SP(_th)    (((struct sigcontext *) (_th)->md.context)->sc_onstack)
153 #define PR_NUM_GCREGS	    _JBLEN
154 
155 /*
156 ** Initialize a thread context to run "_main()" when started
157 */
158 #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)  \
159 {  \
160     *status = PR_TRUE;  \
161     if (setjmp(CONTEXT(_thread))) {  \
162         _main();  \
163     }  \
164     _MD_GET_SP(_thread) = (unsigned char*) ((_sp) - 64); \
165 }
166 
167 #define _MD_SWITCH_CONTEXT(_thread)  \
168     if (!setjmp(CONTEXT(_thread))) {  \
169 	(_thread)->md.errcode = errno;  \
170 	_PR_Schedule();  \
171     }
172 
173 /*
174 ** Restore a thread context, saved by _MD_SWITCH_CONTEXT
175 */
176 #define _MD_RESTORE_CONTEXT(_thread) \
177 {   \
178     errno = (_thread)->md.errcode;  \
179     _MD_SET_CURRENT_THREAD(_thread);  \
180     longjmp(CONTEXT(_thread), 1);  \
181 }
182 
183 /* Machine-dependent (MD) data structures */
184 
185 struct _MDThread {
186     PR_CONTEXT_TYPE context;
187     int id;
188     int errcode;
189 };
190 
191 struct _MDThreadStack {
192     PRInt8 notused;
193 };
194 
195 struct _MDLock {
196     PRInt8 notused;
197 };
198 
199 struct _MDSemaphore {
200     PRInt8 notused;
201 };
202 
203 struct _MDCVar {
204     PRInt8 notused;
205 };
206 
207 struct _MDSegment {
208     PRInt8 notused;
209 };
210 
211 /*
212  * md-specific cpu structure field
213  */
214 #define _PR_MD_MAX_OSFD FD_SETSIZE
215 
216 struct _MDCPU_Unix {
217     PRCList ioQ;
218     PRUint32 ioq_timeout;
219     PRInt32 ioq_max_osfd;
220     PRInt32 ioq_osfd_cnt;
221 #ifndef _PR_USE_POLL
222     fd_set fd_read_set, fd_write_set, fd_exception_set;
223     PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
224 				fd_exception_cnt[_PR_MD_MAX_OSFD];
225 #else
226 	struct pollfd *ioq_pollfds;
227 	int ioq_pollfds_size;
228 #endif	/* _PR_USE_POLL */
229 };
230 
231 #define _PR_IOQ(_cpu)			((_cpu)->md.md_unix.ioQ)
232 #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
233 #define _PR_FD_READ_SET(_cpu)		((_cpu)->md.md_unix.fd_read_set)
234 #define _PR_FD_READ_CNT(_cpu)		((_cpu)->md.md_unix.fd_read_cnt)
235 #define _PR_FD_WRITE_SET(_cpu)		((_cpu)->md.md_unix.fd_write_set)
236 #define _PR_FD_WRITE_CNT(_cpu)		((_cpu)->md.md_unix.fd_write_cnt)
237 #define _PR_FD_EXCEPTION_SET(_cpu)	((_cpu)->md.md_unix.fd_exception_set)
238 #define _PR_FD_EXCEPTION_CNT(_cpu)	((_cpu)->md.md_unix.fd_exception_cnt)
239 #define _PR_IOQ_TIMEOUT(_cpu)		((_cpu)->md.md_unix.ioq_timeout)
240 #define _PR_IOQ_MAX_OSFD(_cpu)		((_cpu)->md.md_unix.ioq_max_osfd)
241 #define _PR_IOQ_OSFD_CNT(_cpu)		((_cpu)->md.md_unix.ioq_osfd_cnt)
242 #define _PR_IOQ_POLLFDS(_cpu)		((_cpu)->md.md_unix.ioq_pollfds)
243 #define _PR_IOQ_POLLFDS_SIZE(_cpu)	((_cpu)->md.md_unix.ioq_pollfds_size)
244 
245 #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu)	32
246 
247 struct _MDCPU {
248 	struct _MDCPU_Unix md_unix;
249 };
250 
251 #define _MD_INIT_LOCKS()
252 #define _MD_NEW_LOCK(lock) PR_SUCCESS
253 #define _MD_FREE_LOCK(lock)
254 #define _MD_LOCK(lock)
255 #define _MD_UNLOCK(lock)
256 #define _MD_INIT_IO()
257 #define _MD_IOQ_LOCK()
258 #define _MD_IOQ_UNLOCK()
259 
260 extern PRStatus _MD_InitializeThread(PRThread *thread);
261 
262 #define _MD_INIT_RUNNING_CPU(cpu)       _MD_unix_init_running_cpu(cpu)
263 #define _MD_INIT_THREAD                 _MD_InitializeThread
264 #define _MD_EXIT_THREAD(thread)
265 #define _MD_SUSPEND_THREAD(thread)      _MD_suspend_thread
266 #define _MD_RESUME_THREAD(thread)       _MD_resume_thread
267 #define _MD_CLEAN_THREAD(_thread)
268 
269 extern PRStatus _MD_CREATE_THREAD(
270     PRThread *thread,
271     void (*start) (void *),
272     PRThreadPriority priority,
273     PRThreadScope scope,
274     PRThreadState state,
275     PRUint32 stackSize);
276 extern void _MD_SET_PRIORITY(struct _MDThread *thread, PRUintn newPri);
277 extern PRStatus _MD_WAIT(PRThread *, PRIntervalTime timeout);
278 extern PRStatus _MD_WAKEUP_WAITER(PRThread *);
279 extern void _MD_YIELD(void);
280 
281 #endif /* ! _PR_PTHREADS */
282 
283 #define _MD_EARLY_INIT          _MD_EarlyInit
284 #define _MD_FINAL_INIT			_PR_UnixInit
285 #define _MD_INTERVAL_INIT       _PR_Mach_IntervalInit
286 #define _MD_GET_INTERVAL        _PR_Mach_GetInterval
287 #define _MD_INTERVAL_PER_SEC    _PR_Mach_TicksPerSecond
288 
289 extern void             _MD_EarlyInit(void);
290 extern void             _PR_Mach_IntervalInit(void);
291 extern PRIntervalTime   _PR_Mach_GetInterval(void);
292 extern PRIntervalTime   _PR_Mach_TicksPerSecond(void);
293 
294 /*
295  * We wrapped the select() call.  _MD_SELECT refers to the built-in,
296  * unwrapped version.
297  */
298 #define _MD_SELECT(nfds,r,w,e,tv) syscall(SYS_select,nfds,r,w,e,tv)
299 
300 /* For writev() */
301 #include <sys/uio.h>
302 
303 #endif /* nspr_darwin_defs_h___ */
304