andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #ifndef nspr_darwin_defs_h___ andre@0: #define nspr_darwin_defs_h___ andre@0: andre@0: #include "prthread.h" andre@0: andre@0: #include andre@0: #include andre@0: andre@0: #ifdef __APPLE__ andre@0: #include andre@0: #include andre@0: #endif andre@0: andre@0: #define PR_LINKER_ARCH "darwin" andre@0: #define _PR_SI_SYSNAME "DARWIN" andre@0: #ifdef __i386__ andre@0: #define _PR_SI_ARCHITECTURE "x86" andre@0: #elif defined(__x86_64__) andre@0: #define _PR_SI_ARCHITECTURE "x86-64" andre@0: #elif defined(__ppc__) andre@0: #define _PR_SI_ARCHITECTURE "ppc" andre@0: #elif defined(__arm__) andre@0: #define _PR_SI_ARCHITECTURE "arm" andre@0: #elif defined(__aarch64__) andre@0: #define _PR_SI_ARCHITECTURE "aarch64" andre@0: #else andre@0: #error "Unknown CPU architecture" andre@0: #endif andre@0: #define PR_DLL_SUFFIX ".dylib" andre@0: andre@0: #define _PR_VMBASE 0x30000000 andre@0: #define _PR_STACK_VMBASE 0x50000000 andre@0: #define _MD_DEFAULT_STACK_SIZE 65536L andre@0: #define _MD_MMAP_FLAGS MAP_PRIVATE andre@0: andre@0: #undef HAVE_STACK_GROWING_UP andre@0: #define HAVE_DLL andre@0: #if defined(__x86_64__) || TARGET_OS_IPHONE andre@0: #define USE_DLFCN andre@0: #else andre@0: #define USE_MACH_DYLD andre@0: #endif andre@0: #define _PR_HAVE_SOCKADDR_LEN andre@0: #define _PR_STAT_HAS_ST_ATIMESPEC andre@0: #define _PR_HAVE_LARGE_OFF_T andre@0: #define _PR_HAVE_SYSV_SEMAPHORES andre@0: #define PR_HAVE_SYSV_NAMED_SHARED_MEMORY andre@0: andre@0: #define _PR_INET6 andre@0: /* andre@0: * I'd prefer to use getipnodebyname and getipnodebyaddr but the andre@0: * getipnodebyname(3) man page on Mac OS X 10.2 says they are not andre@0: * thread-safe. AI_V4MAPPED|AI_ADDRCONFIG doesn't work either. andre@0: */ andre@0: #define _PR_HAVE_GETHOSTBYNAME2 andre@0: #define _PR_HAVE_GETADDRINFO andre@0: /* andre@0: * On Mac OS X 10.2, gethostbyaddr fails with h_errno=NO_RECOVERY andre@0: * if you pass an IPv4-mapped IPv6 address to it. andre@0: */ andre@0: #define _PR_GHBA_DISALLOW_V4MAPPED andre@0: #ifdef __APPLE__ andre@0: #if !defined(MAC_OS_X_VERSION_10_3) || \ andre@0: MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 andre@0: /* andre@0: * socket(AF_INET6) fails with EPROTONOSUPPORT on Mac OS X 10.1. andre@0: * IPv6 under OS X 10.2 and below is not complete (see bug 222031). andre@0: */ andre@0: #define _PR_INET6_PROBE andre@0: #endif /* DT < 10.3 */ andre@0: #if defined(MAC_OS_X_VERSION_10_2) && \ andre@0: MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_2 andre@0: /* Mac OS X 10.2 has inet_ntop and inet_pton. */ andre@0: #define _PR_HAVE_INET_NTOP andre@0: #endif /* DT >= 10.2 */ andre@0: #endif /* __APPLE__ */ andre@0: #define _PR_IPV6_V6ONLY_PROBE andre@0: /* The IPV6_V6ONLY socket option is not defined on Mac OS X 10.1. */ andre@0: #ifndef IPV6_V6ONLY andre@0: #define IPV6_V6ONLY 27 andre@0: #endif andre@0: andre@0: #ifdef __ppc__ andre@0: #define _PR_HAVE_ATOMIC_OPS andre@0: #define _MD_INIT_ATOMIC() andre@0: extern PRInt32 _PR_DarwinPPC_AtomicIncrement(PRInt32 *val); andre@0: #define _MD_ATOMIC_INCREMENT(val) _PR_DarwinPPC_AtomicIncrement(val) andre@0: extern PRInt32 _PR_DarwinPPC_AtomicDecrement(PRInt32 *val); andre@0: #define _MD_ATOMIC_DECREMENT(val) _PR_DarwinPPC_AtomicDecrement(val) andre@0: extern PRInt32 _PR_DarwinPPC_AtomicSet(PRInt32 *val, PRInt32 newval); andre@0: #define _MD_ATOMIC_SET(val, newval) _PR_DarwinPPC_AtomicSet(val, newval) andre@0: extern PRInt32 _PR_DarwinPPC_AtomicAdd(PRInt32 *ptr, PRInt32 val); andre@0: #define _MD_ATOMIC_ADD(ptr, val) _PR_DarwinPPC_AtomicAdd(ptr, val) andre@0: #endif /* __ppc__ */ andre@0: andre@0: #ifdef __i386__ andre@0: #define _PR_HAVE_ATOMIC_OPS andre@0: #define _MD_INIT_ATOMIC() andre@0: extern PRInt32 _PR_Darwin_x86_AtomicIncrement(PRInt32 *val); andre@0: #define _MD_ATOMIC_INCREMENT(val) _PR_Darwin_x86_AtomicIncrement(val) andre@0: extern PRInt32 _PR_Darwin_x86_AtomicDecrement(PRInt32 *val); andre@0: #define _MD_ATOMIC_DECREMENT(val) _PR_Darwin_x86_AtomicDecrement(val) andre@0: extern PRInt32 _PR_Darwin_x86_AtomicSet(PRInt32 *val, PRInt32 newval); andre@0: #define _MD_ATOMIC_SET(val, newval) _PR_Darwin_x86_AtomicSet(val, newval) andre@0: extern PRInt32 _PR_Darwin_x86_AtomicAdd(PRInt32 *ptr, PRInt32 val); andre@0: #define _MD_ATOMIC_ADD(ptr, val) _PR_Darwin_x86_AtomicAdd(ptr, val) andre@0: #endif /* __i386__ */ andre@0: andre@0: #ifdef __x86_64__ andre@0: #define _PR_HAVE_ATOMIC_OPS andre@0: #define _MD_INIT_ATOMIC() andre@0: extern PRInt32 _PR_Darwin_x86_64_AtomicIncrement(PRInt32 *val); andre@0: #define _MD_ATOMIC_INCREMENT(val) _PR_Darwin_x86_64_AtomicIncrement(val) andre@0: extern PRInt32 _PR_Darwin_x86_64_AtomicDecrement(PRInt32 *val); andre@0: #define _MD_ATOMIC_DECREMENT(val) _PR_Darwin_x86_64_AtomicDecrement(val) andre@0: extern PRInt32 _PR_Darwin_x86_64_AtomicSet(PRInt32 *val, PRInt32 newval); andre@0: #define _MD_ATOMIC_SET(val, newval) _PR_Darwin_x86_64_AtomicSet(val, newval) andre@0: extern PRInt32 _PR_Darwin_x86_64_AtomicAdd(PRInt32 *ptr, PRInt32 val); andre@0: #define _MD_ATOMIC_ADD(ptr, val) _PR_Darwin_x86_64_AtomicAdd(ptr, val) andre@0: #endif /* __x86_64__ */ andre@0: andre@0: #if defined(__arm__) || defined(__aarch64__) andre@0: #define _PR_HAVE_ATOMIC_OPS andre@0: #define _MD_INIT_ATOMIC() andre@0: #define _MD_ATOMIC_INCREMENT(val) OSAtomicIncrement32(val) andre@0: #define _MD_ATOMIC_DECREMENT(val) OSAtomicDecrement32(val) andre@0: static inline PRInt32 _MD_ATOMIC_SET(PRInt32 *val, PRInt32 newval) andre@0: { andre@0: PRInt32 oldval; andre@0: do { andre@0: oldval = *val; andre@0: } while (!OSAtomicCompareAndSwap32(oldval, newval, val)); andre@0: return oldval; andre@0: } andre@0: #define _MD_ATOMIC_ADD(ptr, val) OSAtomicAdd32(val, ptr) andre@0: #endif /* __arm__ || __aarch64__ */ andre@0: andre@0: #define USE_SETJMP andre@0: andre@0: #if !defined(_PR_PTHREADS) andre@0: andre@0: #include andre@0: andre@0: #define PR_CONTEXT_TYPE jmp_buf andre@0: andre@0: #define CONTEXT(_th) ((_th)->md.context) andre@0: #define _MD_GET_SP(_th) (((struct sigcontext *) (_th)->md.context)->sc_onstack) andre@0: #define PR_NUM_GCREGS _JBLEN andre@0: andre@0: /* andre@0: ** Initialize a thread context to run "_main()" when started andre@0: */ andre@0: #define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \ andre@0: { \ andre@0: *status = PR_TRUE; \ andre@0: if (setjmp(CONTEXT(_thread))) { \ andre@0: _main(); \ andre@0: } \ andre@0: _MD_GET_SP(_thread) = (unsigned char*) ((_sp) - 64); \ andre@0: } andre@0: andre@0: #define _MD_SWITCH_CONTEXT(_thread) \ andre@0: if (!setjmp(CONTEXT(_thread))) { \ andre@0: (_thread)->md.errcode = errno; \ andre@0: _PR_Schedule(); \ andre@0: } andre@0: andre@0: /* andre@0: ** Restore a thread context, saved by _MD_SWITCH_CONTEXT andre@0: */ andre@0: #define _MD_RESTORE_CONTEXT(_thread) \ andre@0: { \ andre@0: errno = (_thread)->md.errcode; \ andre@0: _MD_SET_CURRENT_THREAD(_thread); \ andre@0: longjmp(CONTEXT(_thread), 1); \ andre@0: } andre@0: andre@0: /* Machine-dependent (MD) data structures */ andre@0: andre@0: struct _MDThread { andre@0: PR_CONTEXT_TYPE context; andre@0: int id; andre@0: int errcode; andre@0: }; andre@0: andre@0: struct _MDThreadStack { andre@0: PRInt8 notused; andre@0: }; andre@0: andre@0: struct _MDLock { andre@0: PRInt8 notused; andre@0: }; andre@0: andre@0: struct _MDSemaphore { andre@0: PRInt8 notused; andre@0: }; andre@0: andre@0: struct _MDCVar { andre@0: PRInt8 notused; andre@0: }; andre@0: andre@0: struct _MDSegment { andre@0: PRInt8 notused; andre@0: }; andre@0: andre@0: /* andre@0: * md-specific cpu structure field andre@0: */ andre@0: #define _PR_MD_MAX_OSFD FD_SETSIZE andre@0: andre@0: struct _MDCPU_Unix { andre@0: PRCList ioQ; andre@0: PRUint32 ioq_timeout; andre@0: PRInt32 ioq_max_osfd; andre@0: PRInt32 ioq_osfd_cnt; andre@0: #ifndef _PR_USE_POLL andre@0: fd_set fd_read_set, fd_write_set, fd_exception_set; andre@0: PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD], andre@0: fd_exception_cnt[_PR_MD_MAX_OSFD]; andre@0: #else andre@0: struct pollfd *ioq_pollfds; andre@0: int ioq_pollfds_size; andre@0: #endif /* _PR_USE_POLL */ andre@0: }; andre@0: andre@0: #define _PR_IOQ(_cpu) ((_cpu)->md.md_unix.ioQ) andre@0: #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu)) andre@0: #define _PR_FD_READ_SET(_cpu) ((_cpu)->md.md_unix.fd_read_set) andre@0: #define _PR_FD_READ_CNT(_cpu) ((_cpu)->md.md_unix.fd_read_cnt) andre@0: #define _PR_FD_WRITE_SET(_cpu) ((_cpu)->md.md_unix.fd_write_set) andre@0: #define _PR_FD_WRITE_CNT(_cpu) ((_cpu)->md.md_unix.fd_write_cnt) andre@0: #define _PR_FD_EXCEPTION_SET(_cpu) ((_cpu)->md.md_unix.fd_exception_set) andre@0: #define _PR_FD_EXCEPTION_CNT(_cpu) ((_cpu)->md.md_unix.fd_exception_cnt) andre@0: #define _PR_IOQ_TIMEOUT(_cpu) ((_cpu)->md.md_unix.ioq_timeout) andre@0: #define _PR_IOQ_MAX_OSFD(_cpu) ((_cpu)->md.md_unix.ioq_max_osfd) andre@0: #define _PR_IOQ_OSFD_CNT(_cpu) ((_cpu)->md.md_unix.ioq_osfd_cnt) andre@0: #define _PR_IOQ_POLLFDS(_cpu) ((_cpu)->md.md_unix.ioq_pollfds) andre@0: #define _PR_IOQ_POLLFDS_SIZE(_cpu) ((_cpu)->md.md_unix.ioq_pollfds_size) andre@0: andre@0: #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu) 32 andre@0: andre@0: struct _MDCPU { andre@0: struct _MDCPU_Unix md_unix; andre@0: }; andre@0: andre@0: #define _MD_INIT_LOCKS() andre@0: #define _MD_NEW_LOCK(lock) PR_SUCCESS andre@0: #define _MD_FREE_LOCK(lock) andre@0: #define _MD_LOCK(lock) andre@0: #define _MD_UNLOCK(lock) andre@0: #define _MD_INIT_IO() andre@0: #define _MD_IOQ_LOCK() andre@0: #define _MD_IOQ_UNLOCK() andre@0: andre@0: extern PRStatus _MD_InitializeThread(PRThread *thread); andre@0: andre@0: #define _MD_INIT_RUNNING_CPU(cpu) _MD_unix_init_running_cpu(cpu) andre@0: #define _MD_INIT_THREAD _MD_InitializeThread andre@0: #define _MD_EXIT_THREAD(thread) andre@0: #define _MD_SUSPEND_THREAD(thread) _MD_suspend_thread andre@0: #define _MD_RESUME_THREAD(thread) _MD_resume_thread andre@0: #define _MD_CLEAN_THREAD(_thread) andre@0: andre@0: extern PRStatus _MD_CREATE_THREAD( andre@0: PRThread *thread, andre@0: void (*start) (void *), andre@0: PRThreadPriority priority, andre@0: PRThreadScope scope, andre@0: PRThreadState state, andre@0: PRUint32 stackSize); andre@0: extern void _MD_SET_PRIORITY(struct _MDThread *thread, PRUintn newPri); andre@0: extern PRStatus _MD_WAIT(PRThread *, PRIntervalTime timeout); andre@0: extern PRStatus _MD_WAKEUP_WAITER(PRThread *); andre@0: extern void _MD_YIELD(void); andre@0: andre@0: #endif /* ! _PR_PTHREADS */ andre@0: andre@0: #define _MD_EARLY_INIT _MD_EarlyInit andre@0: #define _MD_FINAL_INIT _PR_UnixInit andre@0: #define _MD_INTERVAL_INIT _PR_Mach_IntervalInit andre@0: #define _MD_GET_INTERVAL _PR_Mach_GetInterval andre@0: #define _MD_INTERVAL_PER_SEC _PR_Mach_TicksPerSecond andre@0: andre@0: extern void _MD_EarlyInit(void); andre@0: extern void _PR_Mach_IntervalInit(void); andre@0: extern PRIntervalTime _PR_Mach_GetInterval(void); andre@0: extern PRIntervalTime _PR_Mach_TicksPerSecond(void); andre@0: andre@0: /* andre@0: * We wrapped the select() call. _MD_SELECT refers to the built-in, andre@0: * unwrapped version. andre@0: */ andre@0: #define _MD_SELECT(nfds,r,w,e,tv) syscall(SYS_select,nfds,r,w,e,tv) andre@0: andre@0: /* For writev() */ andre@0: #include andre@0: andre@0: #endif /* nspr_darwin_defs_h___ */