_darwin.h 9.3 KB

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