1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/include/md/_darwin.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,303 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef nspr_darwin_defs_h___ 1.10 +#define nspr_darwin_defs_h___ 1.11 + 1.12 +#include "prthread.h" 1.13 + 1.14 +#include <libkern/OSAtomic.h> 1.15 +#include <sys/syscall.h> 1.16 + 1.17 +#ifdef __APPLE__ 1.18 +#include <AvailabilityMacros.h> 1.19 +#include <TargetConditionals.h> 1.20 +#endif 1.21 + 1.22 +#define PR_LINKER_ARCH "darwin" 1.23 +#define _PR_SI_SYSNAME "DARWIN" 1.24 +#ifdef __i386__ 1.25 +#define _PR_SI_ARCHITECTURE "x86" 1.26 +#elif defined(__x86_64__) 1.27 +#define _PR_SI_ARCHITECTURE "x86-64" 1.28 +#elif defined(__ppc__) 1.29 +#define _PR_SI_ARCHITECTURE "ppc" 1.30 +#elif defined(__arm__) 1.31 +#define _PR_SI_ARCHITECTURE "arm" 1.32 +#elif defined(__aarch64__) 1.33 +#define _PR_SI_ARCHITECTURE "aarch64" 1.34 +#else 1.35 +#error "Unknown CPU architecture" 1.36 +#endif 1.37 +#define PR_DLL_SUFFIX ".dylib" 1.38 + 1.39 +#define _PR_VMBASE 0x30000000 1.40 +#define _PR_STACK_VMBASE 0x50000000 1.41 +#define _MD_DEFAULT_STACK_SIZE 65536L 1.42 +#define _MD_MMAP_FLAGS MAP_PRIVATE 1.43 + 1.44 +#undef HAVE_STACK_GROWING_UP 1.45 +#define HAVE_DLL 1.46 +#if defined(__x86_64__) || TARGET_OS_IPHONE 1.47 +#define USE_DLFCN 1.48 +#else 1.49 +#define USE_MACH_DYLD 1.50 +#endif 1.51 +#define _PR_HAVE_SOCKADDR_LEN 1.52 +#define _PR_STAT_HAS_ST_ATIMESPEC 1.53 +#define _PR_HAVE_LARGE_OFF_T 1.54 +#define _PR_HAVE_SYSV_SEMAPHORES 1.55 +#define PR_HAVE_SYSV_NAMED_SHARED_MEMORY 1.56 + 1.57 +#define _PR_INET6 1.58 +/* 1.59 + * I'd prefer to use getipnodebyname and getipnodebyaddr but the 1.60 + * getipnodebyname(3) man page on Mac OS X 10.2 says they are not 1.61 + * thread-safe. AI_V4MAPPED|AI_ADDRCONFIG doesn't work either. 1.62 + */ 1.63 +#define _PR_HAVE_GETHOSTBYNAME2 1.64 +#define _PR_HAVE_GETADDRINFO 1.65 +/* 1.66 + * On Mac OS X 10.2, gethostbyaddr fails with h_errno=NO_RECOVERY 1.67 + * if you pass an IPv4-mapped IPv6 address to it. 1.68 + */ 1.69 +#define _PR_GHBA_DISALLOW_V4MAPPED 1.70 +#ifdef __APPLE__ 1.71 +#if !defined(MAC_OS_X_VERSION_10_3) || \ 1.72 + MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3 1.73 +/* 1.74 + * socket(AF_INET6) fails with EPROTONOSUPPORT on Mac OS X 10.1. 1.75 + * IPv6 under OS X 10.2 and below is not complete (see bug 222031). 1.76 + */ 1.77 +#define _PR_INET6_PROBE 1.78 +#endif /* DT < 10.3 */ 1.79 +#if defined(MAC_OS_X_VERSION_10_2) && \ 1.80 + MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_2 1.81 +/* Mac OS X 10.2 has inet_ntop and inet_pton. */ 1.82 +#define _PR_HAVE_INET_NTOP 1.83 +#endif /* DT >= 10.2 */ 1.84 +#endif /* __APPLE__ */ 1.85 +#define _PR_IPV6_V6ONLY_PROBE 1.86 +/* The IPV6_V6ONLY socket option is not defined on Mac OS X 10.1. */ 1.87 +#ifndef IPV6_V6ONLY 1.88 +#define IPV6_V6ONLY 27 1.89 +#endif 1.90 + 1.91 +#ifdef __ppc__ 1.92 +#define _PR_HAVE_ATOMIC_OPS 1.93 +#define _MD_INIT_ATOMIC() 1.94 +extern PRInt32 _PR_DarwinPPC_AtomicIncrement(PRInt32 *val); 1.95 +#define _MD_ATOMIC_INCREMENT(val) _PR_DarwinPPC_AtomicIncrement(val) 1.96 +extern PRInt32 _PR_DarwinPPC_AtomicDecrement(PRInt32 *val); 1.97 +#define _MD_ATOMIC_DECREMENT(val) _PR_DarwinPPC_AtomicDecrement(val) 1.98 +extern PRInt32 _PR_DarwinPPC_AtomicSet(PRInt32 *val, PRInt32 newval); 1.99 +#define _MD_ATOMIC_SET(val, newval) _PR_DarwinPPC_AtomicSet(val, newval) 1.100 +extern PRInt32 _PR_DarwinPPC_AtomicAdd(PRInt32 *ptr, PRInt32 val); 1.101 +#define _MD_ATOMIC_ADD(ptr, val) _PR_DarwinPPC_AtomicAdd(ptr, val) 1.102 +#endif /* __ppc__ */ 1.103 + 1.104 +#ifdef __i386__ 1.105 +#define _PR_HAVE_ATOMIC_OPS 1.106 +#define _MD_INIT_ATOMIC() 1.107 +extern PRInt32 _PR_Darwin_x86_AtomicIncrement(PRInt32 *val); 1.108 +#define _MD_ATOMIC_INCREMENT(val) _PR_Darwin_x86_AtomicIncrement(val) 1.109 +extern PRInt32 _PR_Darwin_x86_AtomicDecrement(PRInt32 *val); 1.110 +#define _MD_ATOMIC_DECREMENT(val) _PR_Darwin_x86_AtomicDecrement(val) 1.111 +extern PRInt32 _PR_Darwin_x86_AtomicSet(PRInt32 *val, PRInt32 newval); 1.112 +#define _MD_ATOMIC_SET(val, newval) _PR_Darwin_x86_AtomicSet(val, newval) 1.113 +extern PRInt32 _PR_Darwin_x86_AtomicAdd(PRInt32 *ptr, PRInt32 val); 1.114 +#define _MD_ATOMIC_ADD(ptr, val) _PR_Darwin_x86_AtomicAdd(ptr, val) 1.115 +#endif /* __i386__ */ 1.116 + 1.117 +#ifdef __x86_64__ 1.118 +#define _PR_HAVE_ATOMIC_OPS 1.119 +#define _MD_INIT_ATOMIC() 1.120 +extern PRInt32 _PR_Darwin_x86_64_AtomicIncrement(PRInt32 *val); 1.121 +#define _MD_ATOMIC_INCREMENT(val) _PR_Darwin_x86_64_AtomicIncrement(val) 1.122 +extern PRInt32 _PR_Darwin_x86_64_AtomicDecrement(PRInt32 *val); 1.123 +#define _MD_ATOMIC_DECREMENT(val) _PR_Darwin_x86_64_AtomicDecrement(val) 1.124 +extern PRInt32 _PR_Darwin_x86_64_AtomicSet(PRInt32 *val, PRInt32 newval); 1.125 +#define _MD_ATOMIC_SET(val, newval) _PR_Darwin_x86_64_AtomicSet(val, newval) 1.126 +extern PRInt32 _PR_Darwin_x86_64_AtomicAdd(PRInt32 *ptr, PRInt32 val); 1.127 +#define _MD_ATOMIC_ADD(ptr, val) _PR_Darwin_x86_64_AtomicAdd(ptr, val) 1.128 +#endif /* __x86_64__ */ 1.129 + 1.130 +#if defined(__arm__) || defined(__aarch64__) 1.131 +#define _PR_HAVE_ATOMIC_OPS 1.132 +#define _MD_INIT_ATOMIC() 1.133 +#define _MD_ATOMIC_INCREMENT(val) OSAtomicIncrement32(val) 1.134 +#define _MD_ATOMIC_DECREMENT(val) OSAtomicDecrement32(val) 1.135 +static inline PRInt32 _MD_ATOMIC_SET(PRInt32 *val, PRInt32 newval) 1.136 +{ 1.137 + PRInt32 oldval; 1.138 + do { 1.139 + oldval = *val; 1.140 + } while (!OSAtomicCompareAndSwap32(oldval, newval, val)); 1.141 + return oldval; 1.142 +} 1.143 +#define _MD_ATOMIC_ADD(ptr, val) OSAtomicAdd32(val, ptr) 1.144 +#endif /* __arm__ || __aarch64__ */ 1.145 + 1.146 +#define USE_SETJMP 1.147 + 1.148 +#if !defined(_PR_PTHREADS) 1.149 + 1.150 +#include <setjmp.h> 1.151 + 1.152 +#define PR_CONTEXT_TYPE jmp_buf 1.153 + 1.154 +#define CONTEXT(_th) ((_th)->md.context) 1.155 +#define _MD_GET_SP(_th) (((struct sigcontext *) (_th)->md.context)->sc_onstack) 1.156 +#define PR_NUM_GCREGS _JBLEN 1.157 + 1.158 +/* 1.159 +** Initialize a thread context to run "_main()" when started 1.160 +*/ 1.161 +#define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \ 1.162 +{ \ 1.163 + *status = PR_TRUE; \ 1.164 + if (setjmp(CONTEXT(_thread))) { \ 1.165 + _main(); \ 1.166 + } \ 1.167 + _MD_GET_SP(_thread) = (unsigned char*) ((_sp) - 64); \ 1.168 +} 1.169 + 1.170 +#define _MD_SWITCH_CONTEXT(_thread) \ 1.171 + if (!setjmp(CONTEXT(_thread))) { \ 1.172 + (_thread)->md.errcode = errno; \ 1.173 + _PR_Schedule(); \ 1.174 + } 1.175 + 1.176 +/* 1.177 +** Restore a thread context, saved by _MD_SWITCH_CONTEXT 1.178 +*/ 1.179 +#define _MD_RESTORE_CONTEXT(_thread) \ 1.180 +{ \ 1.181 + errno = (_thread)->md.errcode; \ 1.182 + _MD_SET_CURRENT_THREAD(_thread); \ 1.183 + longjmp(CONTEXT(_thread), 1); \ 1.184 +} 1.185 + 1.186 +/* Machine-dependent (MD) data structures */ 1.187 + 1.188 +struct _MDThread { 1.189 + PR_CONTEXT_TYPE context; 1.190 + int id; 1.191 + int errcode; 1.192 +}; 1.193 + 1.194 +struct _MDThreadStack { 1.195 + PRInt8 notused; 1.196 +}; 1.197 + 1.198 +struct _MDLock { 1.199 + PRInt8 notused; 1.200 +}; 1.201 + 1.202 +struct _MDSemaphore { 1.203 + PRInt8 notused; 1.204 +}; 1.205 + 1.206 +struct _MDCVar { 1.207 + PRInt8 notused; 1.208 +}; 1.209 + 1.210 +struct _MDSegment { 1.211 + PRInt8 notused; 1.212 +}; 1.213 + 1.214 +/* 1.215 + * md-specific cpu structure field 1.216 + */ 1.217 +#define _PR_MD_MAX_OSFD FD_SETSIZE 1.218 + 1.219 +struct _MDCPU_Unix { 1.220 + PRCList ioQ; 1.221 + PRUint32 ioq_timeout; 1.222 + PRInt32 ioq_max_osfd; 1.223 + PRInt32 ioq_osfd_cnt; 1.224 +#ifndef _PR_USE_POLL 1.225 + fd_set fd_read_set, fd_write_set, fd_exception_set; 1.226 + PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD], 1.227 + fd_exception_cnt[_PR_MD_MAX_OSFD]; 1.228 +#else 1.229 + struct pollfd *ioq_pollfds; 1.230 + int ioq_pollfds_size; 1.231 +#endif /* _PR_USE_POLL */ 1.232 +}; 1.233 + 1.234 +#define _PR_IOQ(_cpu) ((_cpu)->md.md_unix.ioQ) 1.235 +#define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu)) 1.236 +#define _PR_FD_READ_SET(_cpu) ((_cpu)->md.md_unix.fd_read_set) 1.237 +#define _PR_FD_READ_CNT(_cpu) ((_cpu)->md.md_unix.fd_read_cnt) 1.238 +#define _PR_FD_WRITE_SET(_cpu) ((_cpu)->md.md_unix.fd_write_set) 1.239 +#define _PR_FD_WRITE_CNT(_cpu) ((_cpu)->md.md_unix.fd_write_cnt) 1.240 +#define _PR_FD_EXCEPTION_SET(_cpu) ((_cpu)->md.md_unix.fd_exception_set) 1.241 +#define _PR_FD_EXCEPTION_CNT(_cpu) ((_cpu)->md.md_unix.fd_exception_cnt) 1.242 +#define _PR_IOQ_TIMEOUT(_cpu) ((_cpu)->md.md_unix.ioq_timeout) 1.243 +#define _PR_IOQ_MAX_OSFD(_cpu) ((_cpu)->md.md_unix.ioq_max_osfd) 1.244 +#define _PR_IOQ_OSFD_CNT(_cpu) ((_cpu)->md.md_unix.ioq_osfd_cnt) 1.245 +#define _PR_IOQ_POLLFDS(_cpu) ((_cpu)->md.md_unix.ioq_pollfds) 1.246 +#define _PR_IOQ_POLLFDS_SIZE(_cpu) ((_cpu)->md.md_unix.ioq_pollfds_size) 1.247 + 1.248 +#define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu) 32 1.249 + 1.250 +struct _MDCPU { 1.251 + struct _MDCPU_Unix md_unix; 1.252 +}; 1.253 + 1.254 +#define _MD_INIT_LOCKS() 1.255 +#define _MD_NEW_LOCK(lock) PR_SUCCESS 1.256 +#define _MD_FREE_LOCK(lock) 1.257 +#define _MD_LOCK(lock) 1.258 +#define _MD_UNLOCK(lock) 1.259 +#define _MD_INIT_IO() 1.260 +#define _MD_IOQ_LOCK() 1.261 +#define _MD_IOQ_UNLOCK() 1.262 + 1.263 +extern PRStatus _MD_InitializeThread(PRThread *thread); 1.264 + 1.265 +#define _MD_INIT_RUNNING_CPU(cpu) _MD_unix_init_running_cpu(cpu) 1.266 +#define _MD_INIT_THREAD _MD_InitializeThread 1.267 +#define _MD_EXIT_THREAD(thread) 1.268 +#define _MD_SUSPEND_THREAD(thread) _MD_suspend_thread 1.269 +#define _MD_RESUME_THREAD(thread) _MD_resume_thread 1.270 +#define _MD_CLEAN_THREAD(_thread) 1.271 + 1.272 +extern PRStatus _MD_CREATE_THREAD( 1.273 + PRThread *thread, 1.274 + void (*start) (void *), 1.275 + PRThreadPriority priority, 1.276 + PRThreadScope scope, 1.277 + PRThreadState state, 1.278 + PRUint32 stackSize); 1.279 +extern void _MD_SET_PRIORITY(struct _MDThread *thread, PRUintn newPri); 1.280 +extern PRStatus _MD_WAIT(PRThread *, PRIntervalTime timeout); 1.281 +extern PRStatus _MD_WAKEUP_WAITER(PRThread *); 1.282 +extern void _MD_YIELD(void); 1.283 + 1.284 +#endif /* ! _PR_PTHREADS */ 1.285 + 1.286 +#define _MD_EARLY_INIT _MD_EarlyInit 1.287 +#define _MD_FINAL_INIT _PR_UnixInit 1.288 +#define _MD_INTERVAL_INIT _PR_Mach_IntervalInit 1.289 +#define _MD_GET_INTERVAL _PR_Mach_GetInterval 1.290 +#define _MD_INTERVAL_PER_SEC _PR_Mach_TicksPerSecond 1.291 + 1.292 +extern void _MD_EarlyInit(void); 1.293 +extern void _PR_Mach_IntervalInit(void); 1.294 +extern PRIntervalTime _PR_Mach_GetInterval(void); 1.295 +extern PRIntervalTime _PR_Mach_TicksPerSecond(void); 1.296 + 1.297 +/* 1.298 + * We wrapped the select() call. _MD_SELECT refers to the built-in, 1.299 + * unwrapped version. 1.300 + */ 1.301 +#define _MD_SELECT(nfds,r,w,e,tv) syscall(SYS_select,nfds,r,w,e,tv) 1.302 + 1.303 +/* For writev() */ 1.304 +#include <sys/uio.h> 1.305 + 1.306 +#endif /* nspr_darwin_defs_h___ */