tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

_darwin.h (9756B)


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