_solaris.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is the Netscape Portable Runtime (NSPR).
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998-2000
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. *
  24. * Alternatively, the contents of this file may be used under the terms of
  25. * either the GNU General Public License Version 2 or later (the "GPL"), or
  26. * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. * in which case the provisions of the GPL or the LGPL are applicable instead
  28. * of those above. If you wish to allow use of your version of this file only
  29. * under the terms of either the GPL or the LGPL, and not to allow others to
  30. * use your version of this file under the terms of the MPL, indicate your
  31. * decision by deleting the provisions above and replace them with the notice
  32. * and other provisions required by the GPL or the LGPL. If you do not delete
  33. * the provisions above, a recipient may use your version of this file under
  34. * the terms of any one of the MPL, the GPL or the LGPL.
  35. *
  36. * ***** END LICENSE BLOCK ***** */
  37. #ifndef nspr_solaris_defs_h___
  38. #define nspr_solaris_defs_h___
  39. /*
  40. * Internal configuration macros
  41. */
  42. #define PR_LINKER_ARCH "solaris"
  43. #define _PR_SI_SYSNAME "SOLARIS"
  44. #ifdef sparc
  45. #define _PR_SI_ARCHITECTURE "sparc"
  46. #elif defined(__x86_64)
  47. #define _PR_SI_ARCHITECTURE "x86-64"
  48. #elif defined(i386)
  49. #define _PR_SI_ARCHITECTURE "x86"
  50. #else
  51. #error unknown processor
  52. #endif
  53. #define PR_DLL_SUFFIX ".so"
  54. #define _PR_VMBASE 0x30000000
  55. #define _PR_STACK_VMBASE 0x50000000
  56. #define _MD_DEFAULT_STACK_SIZE (2*65536L)
  57. #define _MD_MMAP_FLAGS MAP_SHARED
  58. #undef HAVE_STACK_GROWING_UP
  59. #ifndef HAVE_WEAK_IO_SYMBOLS
  60. #define HAVE_WEAK_IO_SYMBOLS
  61. #endif
  62. #undef HAVE_WEAK_MALLOC_SYMBOLS
  63. #define HAVE_DLL
  64. #define USE_DLFCN
  65. #define NEED_STRFTIME_LOCK
  66. /*
  67. * Intel x86 has atomic instructions.
  68. *
  69. * Sparc v8 does not have instructions to efficiently implement
  70. * atomic increment/decrement operations. In the local threads
  71. * only and pthreads versions, we use the default atomic routine
  72. * implementation in pratom.c. The obsolete global threads only
  73. * version uses a global mutex_t to implement the atomic routines
  74. * in solaris.c, which is actually equivalent to the default
  75. * implementation.
  76. *
  77. * 64-bit Solaris requires sparc v9, which has atomic instructions.
  78. */
  79. #if defined(i386) || defined(_PR_GLOBAL_THREADS_ONLY) || defined(IS_64)
  80. #define _PR_HAVE_ATOMIC_OPS
  81. #endif
  82. #define _PR_POLL_AVAILABLE
  83. #define _PR_USE_POLL
  84. #define _PR_STAT_HAS_ST_ATIM
  85. #ifdef SOLARIS2_5
  86. #define _PR_HAVE_SYSV_SEMAPHORES
  87. #define PR_HAVE_SYSV_NAMED_SHARED_MEMORY
  88. #else
  89. #define _PR_HAVE_POSIX_SEMAPHORES
  90. #define PR_HAVE_POSIX_NAMED_SHARED_MEMORY
  91. #endif
  92. #define _PR_HAVE_GETIPNODEBYNAME
  93. #define _PR_HAVE_GETIPNODEBYADDR
  94. #define _PR_HAVE_GETADDRINFO
  95. #define _PR_INET6_PROBE
  96. #define _PR_ACCEPT_INHERIT_NONBLOCK
  97. #ifdef _PR_INET6
  98. #define _PR_HAVE_INET_NTOP
  99. #else
  100. #define AF_INET6 26
  101. struct addrinfo {
  102. int ai_flags;
  103. int ai_family;
  104. int ai_socktype;
  105. int ai_protocol;
  106. size_t ai_addrlen;
  107. char *ai_canonname;
  108. struct sockaddr *ai_addr;
  109. struct addrinfo *ai_next;
  110. };
  111. #define AI_CANONNAME 0x0010
  112. #define AI_V4MAPPED 0x0001
  113. #define AI_ALL 0x0002
  114. #define AI_ADDRCONFIG 0x0004
  115. #define _PR_HAVE_MD_SOCKADDR_IN6
  116. /* isomorphic to struct in6_addr on Solaris 8 */
  117. struct _md_in6_addr {
  118. union {
  119. PRUint8 _S6_u8[16];
  120. PRUint32 _S6_u32[4];
  121. PRUint32 __S6_align;
  122. } _S6_un;
  123. };
  124. /* isomorphic to struct sockaddr_in6 on Solaris 8 */
  125. struct _md_sockaddr_in6 {
  126. PRUint16 sin6_family;
  127. PRUint16 sin6_port;
  128. PRUint32 sin6_flowinfo;
  129. struct _md_in6_addr sin6_addr;
  130. PRUint32 sin6_scope_id;
  131. PRUint32 __sin6_src_id;
  132. };
  133. #endif
  134. #if defined(_PR_GLOBAL_THREADS_ONLY) || defined(_PR_PTHREADS)
  135. #define _PR_HAVE_GETHOST_R
  136. #define _PR_HAVE_GETHOST_R_POINTER
  137. #endif
  138. #include "prinrval.h"
  139. NSPR_API(PRIntervalTime) _MD_Solaris_GetInterval(void);
  140. #define _MD_GET_INTERVAL _MD_Solaris_GetInterval
  141. NSPR_API(PRIntervalTime) _MD_Solaris_TicksPerSecond(void);
  142. #define _MD_INTERVAL_PER_SEC _MD_Solaris_TicksPerSecond
  143. #if defined(_PR_HAVE_ATOMIC_OPS)
  144. /*
  145. ** Atomic Operations
  146. */
  147. #define _MD_INIT_ATOMIC()
  148. NSPR_API(PRInt32) _MD_AtomicIncrement(PRInt32 *val);
  149. #define _MD_ATOMIC_INCREMENT _MD_AtomicIncrement
  150. NSPR_API(PRInt32) _MD_AtomicAdd(PRInt32 *ptr, PRInt32 val);
  151. #define _MD_ATOMIC_ADD _MD_AtomicAdd
  152. NSPR_API(PRInt32) _MD_AtomicDecrement(PRInt32 *val);
  153. #define _MD_ATOMIC_DECREMENT _MD_AtomicDecrement
  154. NSPR_API(PRInt32) _MD_AtomicSet(PRInt32 *val, PRInt32 newval);
  155. #define _MD_ATOMIC_SET _MD_AtomicSet
  156. #endif /* _PR_HAVE_ATOMIC_OPS */
  157. #if defined(_PR_PTHREADS)
  158. NSPR_API(void) _MD_EarlyInit(void);
  159. #define _MD_EARLY_INIT _MD_EarlyInit
  160. #define _MD_FINAL_INIT _PR_UnixInit
  161. #elif defined(_PR_GLOBAL_THREADS_ONLY)
  162. #include "prthread.h"
  163. #include <ucontext.h>
  164. /*
  165. ** Iinitialization Related definitions
  166. */
  167. NSPR_API(void) _MD_EarlyInit(void);
  168. #define _MD_EARLY_INIT _MD_EarlyInit
  169. #define _MD_FINAL_INIT _PR_UnixInit
  170. #define _MD_GET_SP(threadp) threadp->md.sp
  171. /*
  172. ** Clean-up the thread machine dependent data structure
  173. */
  174. #define _MD_INIT_THREAD _MD_InitializeThread
  175. #define _MD_INIT_ATTACHED_THREAD _MD_InitializeThread
  176. NSPR_API(PRStatus) _MD_CreateThread(PRThread *thread,
  177. void (*start)(void *),
  178. PRThreadPriority priority,
  179. PRThreadScope scope,
  180. PRThreadState state,
  181. PRUint32 stackSize);
  182. #define _MD_CREATE_THREAD _MD_CreateThread
  183. #define _PR_CONTEXT_TYPE ucontext_t
  184. #define CONTEXT(_thread) (&(_thread)->md.context)
  185. #include <thread.h>
  186. #include <sys/lwp.h>
  187. #include <synch.h>
  188. extern struct PRLock *_pr_schedLock;
  189. /*
  190. ** Thread Local Storage
  191. */
  192. #define THREAD_KEY_T thread_key_t
  193. extern struct PRThread *_pr_attached_thread_tls();
  194. extern struct PRThread *_pr_current_thread_tls();
  195. extern struct _PRCPU *_pr_current_cpu_tls();
  196. extern struct PRThread *_pr_last_thread_tls();
  197. extern THREAD_KEY_T threadid_key;
  198. extern THREAD_KEY_T cpuid_key;
  199. extern THREAD_KEY_T last_thread_key;
  200. #define _MD_GET_ATTACHED_THREAD() _pr_attached_thread_tls()
  201. #define _MD_CURRENT_THREAD() _pr_current_thread_tls()
  202. #define _MD_CURRENT_CPU() _pr_current_cpu_tls()
  203. #define _MD_LAST_THREAD() _pr_last_thread_tls()
  204. #define _MD_SET_CURRENT_THREAD(newval) \
  205. PR_BEGIN_MACRO \
  206. thr_setspecific(threadid_key, (void *)newval); \
  207. PR_END_MACRO
  208. #define _MD_SET_CURRENT_CPU(newval) \
  209. PR_BEGIN_MACRO \
  210. thr_setspecific(cpuid_key, (void *)newval); \
  211. PR_END_MACRO
  212. #define _MD_SET_LAST_THREAD(newval) \
  213. PR_BEGIN_MACRO \
  214. thr_setspecific(last_thread_key, (void *)newval); \
  215. PR_END_MACRO
  216. #define _MD_CLEAN_THREAD(_thread) _MD_cleanup_thread(_thread)
  217. extern void _MD_exit_thread(PRThread *thread);
  218. #define _MD_EXIT_THREAD(thread) _MD_exit_thread(thread)
  219. #define _MD_SUSPEND_THREAD(thread) _MD_Suspend(thread)
  220. #define _MD_RESUME_THREAD(thread) thr_continue((thread)->md.handle)
  221. /* XXXX Needs to be defined - Prashant */
  222. #define _MD_SUSPEND_CPU(cpu)
  223. #define _MD_RESUME_CPU(cpu)
  224. extern void _MD_Begin_SuspendAll(void);
  225. extern void _MD_End_SuspendAll(void);
  226. extern void _MD_End_ResumeAll(void);
  227. #define _MD_BEGIN_SUSPEND_ALL() _MD_Begin_SuspendAll()
  228. #define _MD_BEGIN_RESUME_ALL()
  229. #define _MD_END_SUSPEND_ALL() _MD_End_SuspendAll()
  230. #define _MD_END_RESUME_ALL() _MD_End_ResumeAll()
  231. #define _MD_INIT_LOCKS()
  232. #define _MD_NEW_LOCK(md_lockp) (mutex_init(&((md_lockp)->lock),USYNC_THREAD,NULL) ? PR_FAILURE : PR_SUCCESS)
  233. #define _MD_FREE_LOCK(md_lockp) mutex_destroy(&((md_lockp)->lock))
  234. #define _MD_UNLOCK(md_lockp) mutex_unlock(&((md_lockp)->lock))
  235. #define _MD_TEST_AND_LOCK(md_lockp) mutex_trylock(&((md_lockp)->lock))
  236. struct _MDLock;
  237. NSPR_API(void) _MD_lock(struct _MDLock *md_lock);
  238. #undef PROFILE_LOCKS
  239. #ifndef PROFILE_LOCKS
  240. #define _MD_LOCK(md_lockp) _MD_lock(md_lockp)
  241. #else
  242. #define _MD_LOCK(md_lockp) \
  243. PR_BEGIN_MACRO \
  244. int rv = _MD_TEST_AND_LOCK(md_lockp); \
  245. if (rv == 0) { \
  246. (md_lockp)->hitcount++; \
  247. } else { \
  248. (md_lockp)->misscount++; \
  249. _MD_lock(md_lockp); \
  250. } \
  251. PR_END_MACRO
  252. #endif
  253. #define _PR_LOCK_HEAP() if (_pr_heapLock) _MD_LOCK(&_pr_heapLock->md)
  254. #define _PR_UNLOCK_HEAP() if (_pr_heapLock) _MD_UNLOCK(&_pr_heapLock->md)
  255. #define _MD_ATTACH_THREAD(threadp)
  256. #define THR_KEYCREATE thr_keycreate
  257. #define THR_SELF thr_self
  258. #define _MD_NEW_CV(condp) cond_init(&((condp)->cv), USYNC_THREAD, 0)
  259. #define COND_WAIT(condp, mutexp) cond_wait(condp, mutexp)
  260. #define COND_TIMEDWAIT(condp, mutexp, tspec) \
  261. cond_timedwait(condp, mutexp, tspec)
  262. #define _MD_NOTIFY_CV(condp, lockp) cond_signal(&((condp)->cv))
  263. #define _MD_NOTIFYALL_CV(condp,unused) cond_broadcast(&((condp)->cv))
  264. #define _MD_FREE_CV(condp) cond_destroy(&((condp)->cv))
  265. #define _MD_YIELD() thr_yield()
  266. #include <time.h>
  267. /*
  268. * Because clock_gettime() on Solaris/x86 2.4 always generates a
  269. * segmentation fault, we use an emulated version _pr_solx86_clock_gettime(),
  270. * which is implemented using gettimeofday().
  271. */
  272. #if defined(i386) && defined(SOLARIS2_4)
  273. extern int _pr_solx86_clock_gettime(clockid_t clock_id, struct timespec *tp);
  274. #define GETTIME(tt) _pr_solx86_clock_gettime(CLOCK_REALTIME, (tt))
  275. #else
  276. #define GETTIME(tt) clock_gettime(CLOCK_REALTIME, (tt))
  277. #endif /* i386 && SOLARIS2_4 */
  278. #define MUTEX_T mutex_t
  279. #define COND_T cond_t
  280. #define _MD_NEW_SEM(md_semp,_val) sema_init(&((md_semp)->sem),_val,USYNC_THREAD,NULL)
  281. #define _MD_DESTROY_SEM(md_semp) sema_destroy(&((md_semp)->sem))
  282. #define _MD_WAIT_SEM(md_semp) sema_wait(&((md_semp)->sem))
  283. #define _MD_POST_SEM(md_semp) sema_post(&((md_semp)->sem))
  284. #define _MD_SAVE_ERRNO(_thread)
  285. #define _MD_RESTORE_ERRNO(_thread)
  286. #define _MD_INIT_RUNNING_CPU(cpu) _MD_unix_init_running_cpu(cpu)
  287. extern struct _MDLock _pr_ioq_lock;
  288. #define _MD_IOQ_LOCK() _MD_LOCK(&_pr_ioq_lock)
  289. #define _MD_IOQ_UNLOCK() _MD_UNLOCK(&_pr_ioq_lock)
  290. extern PRStatus _MD_wait(struct PRThread *, PRIntervalTime timeout);
  291. #define _MD_WAIT _MD_wait
  292. extern PRStatus _MD_WakeupWaiter(struct PRThread *);
  293. #define _MD_WAKEUP_WAITER _MD_WakeupWaiter
  294. NSPR_API(void) _MD_InitIO(void);
  295. #define _MD_INIT_IO _MD_InitIO
  296. #define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \
  297. PR_BEGIN_MACRO \
  298. *status = PR_TRUE; \
  299. PR_END_MACRO
  300. #define _MD_SWITCH_CONTEXT(_thread)
  301. #define _MD_RESTORE_CONTEXT(_newThread)
  302. struct _MDLock {
  303. MUTEX_T lock;
  304. #ifdef PROFILE_LOCKS
  305. PRInt32 hitcount;
  306. PRInt32 misscount;
  307. #endif
  308. };
  309. struct _MDCVar {
  310. COND_T cv;
  311. };
  312. struct _MDSemaphore {
  313. sema_t sem;
  314. };
  315. struct _MDThread {
  316. _PR_CONTEXT_TYPE context;
  317. thread_t handle;
  318. lwpid_t lwpid;
  319. uint_t sp; /* stack pointer */
  320. uint_t threadID; /* ptr to solaris-internal thread id structures */
  321. struct _MDSemaphore waiter_sem;
  322. };
  323. struct _MDThreadStack {
  324. PRInt8 notused;
  325. };
  326. struct _MDSegment {
  327. PRInt8 notused;
  328. };
  329. /*
  330. * md-specific cpu structure field, common to all Unix platforms
  331. */
  332. #define _PR_MD_MAX_OSFD FD_SETSIZE
  333. struct _MDCPU_Unix {
  334. PRCList ioQ;
  335. PRUint32 ioq_timeout;
  336. PRInt32 ioq_max_osfd;
  337. PRInt32 ioq_osfd_cnt;
  338. #ifndef _PR_USE_POLL
  339. fd_set fd_read_set, fd_write_set, fd_exception_set;
  340. PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
  341. fd_exception_cnt[_PR_MD_MAX_OSFD];
  342. #else
  343. struct pollfd *ioq_pollfds;
  344. int ioq_pollfds_size;
  345. #endif /* _PR_USE_POLL */
  346. };
  347. #define _PR_IOQ(_cpu) ((_cpu)->md.md_unix.ioQ)
  348. #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
  349. #define _PR_FD_READ_SET(_cpu) ((_cpu)->md.md_unix.fd_read_set)
  350. #define _PR_FD_READ_CNT(_cpu) ((_cpu)->md.md_unix.fd_read_cnt)
  351. #define _PR_FD_WRITE_SET(_cpu) ((_cpu)->md.md_unix.fd_write_set)
  352. #define _PR_FD_WRITE_CNT(_cpu) ((_cpu)->md.md_unix.fd_write_cnt)
  353. #define _PR_FD_EXCEPTION_SET(_cpu) ((_cpu)->md.md_unix.fd_exception_set)
  354. #define _PR_FD_EXCEPTION_CNT(_cpu) ((_cpu)->md.md_unix.fd_exception_cnt)
  355. #define _PR_IOQ_TIMEOUT(_cpu) ((_cpu)->md.md_unix.ioq_timeout)
  356. #define _PR_IOQ_MAX_OSFD(_cpu) ((_cpu)->md.md_unix.ioq_max_osfd)
  357. #define _PR_IOQ_OSFD_CNT(_cpu) ((_cpu)->md.md_unix.ioq_osfd_cnt)
  358. #define _PR_IOQ_POLLFDS(_cpu) ((_cpu)->md.md_unix.ioq_pollfds)
  359. #define _PR_IOQ_POLLFDS_SIZE(_cpu) ((_cpu)->md.md_unix.ioq_pollfds_size)
  360. #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu) 32
  361. struct _MDCPU {
  362. struct _MDCPU_Unix md_unix;
  363. };
  364. /* The following defines the unwrapped versions of select() and poll(). */
  365. extern int _select(int nfds, fd_set *readfds, fd_set *writefds,
  366. fd_set *exceptfds, struct timeval *timeout);
  367. #define _MD_SELECT _select
  368. #include <poll.h>
  369. #define _MD_POLL _poll
  370. extern int _poll(struct pollfd *fds, unsigned long nfds, int timeout);
  371. PR_BEGIN_EXTERN_C
  372. /*
  373. ** Missing function prototypes
  374. */
  375. extern int gethostname (char *name, int namelen);
  376. PR_END_EXTERN_C
  377. #else /* _PR_GLOBAL_THREADS_ONLY */
  378. /*
  379. * LOCAL_THREADS_ONLY implementation on Solaris
  380. */
  381. #include "prthread.h"
  382. #include <errno.h>
  383. #include <ucontext.h>
  384. #include <sys/stack.h>
  385. #include <synch.h>
  386. /*
  387. ** Iinitialization Related definitions
  388. */
  389. NSPR_API(void) _MD_EarlyInit(void);
  390. NSPR_API(void) _MD_SolarisInit();
  391. #define _MD_EARLY_INIT _MD_EarlyInit
  392. #define _MD_FINAL_INIT _MD_SolarisInit
  393. #define _MD_INIT_THREAD _MD_InitializeThread
  394. #ifdef USE_SETJMP
  395. #include <setjmp.h>
  396. #define _PR_CONTEXT_TYPE jmp_buf
  397. #ifdef sparc
  398. #define _MD_GET_SP(_t) (_t)->md.context[2]
  399. #else
  400. #define _MD_GET_SP(_t) (_t)->md.context[4]
  401. #endif
  402. #define PR_NUM_GCREGS _JBLEN
  403. #define CONTEXT(_thread) (_thread)->md.context
  404. #else /* ! USE_SETJMP */
  405. #ifdef sparc
  406. #define _PR_CONTEXT_TYPE ucontext_t
  407. #define _MD_GET_SP(_t) (_t)->md.context.uc_mcontext.gregs[REG_SP]
  408. /*
  409. ** Sparc's use register windows. the _MD_GetRegisters for the sparc's
  410. ** doesn't actually store anything into the argument buffer; instead the
  411. ** register windows are homed to the stack. I assume that the stack
  412. ** always has room for the registers to spill to...
  413. */
  414. #define PR_NUM_GCREGS 0
  415. #else
  416. #define _PR_CONTEXT_TYPE unsigned int edi; sigset_t oldMask, blockMask; ucontext_t
  417. #define _MD_GET_SP(_t) (_t)->md.context.uc_mcontext.gregs[USP]
  418. #define PR_NUM_GCREGS _JBLEN
  419. #endif
  420. #define CONTEXT(_thread) (&(_thread)->md.context)
  421. #endif /* ! USE_SETJMP */
  422. #include <time.h>
  423. /*
  424. * Because clock_gettime() on Solaris/x86 always generates a
  425. * segmentation fault, we use an emulated version _pr_solx86_clock_gettime(),
  426. * which is implemented using gettimeofday().
  427. */
  428. #ifdef i386
  429. #define GETTIME(tt) _pr_solx86_clock_gettime(CLOCK_REALTIME, (tt))
  430. #else
  431. #define GETTIME(tt) clock_gettime(CLOCK_REALTIME, (tt))
  432. #endif /* i386 */
  433. #define _MD_SAVE_ERRNO(_thread) (_thread)->md.errcode = errno;
  434. #define _MD_RESTORE_ERRNO(_thread) errno = (_thread)->md.errcode;
  435. #ifdef sparc
  436. #ifdef USE_SETJMP
  437. #define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \
  438. PR_BEGIN_MACRO \
  439. int *context = (_thread)->md.context; \
  440. *status = PR_TRUE; \
  441. (void) setjmp(context); \
  442. (_thread)->md.context[1] = (int) ((_sp) - 64); \
  443. (_thread)->md.context[2] = (int) _main; \
  444. (_thread)->md.context[3] = (int) _main + 4; \
  445. _thread->no_sched = 0; \
  446. PR_END_MACRO
  447. #define _MD_SWITCH_CONTEXT(_thread) \
  448. if (!setjmp(CONTEXT(_thread))) { \
  449. _MD_SAVE_ERRNO(_thread) \
  450. _MD_SET_LAST_THREAD(_thread); \
  451. _MD_SET_CURRENT_THREAD(_thread); \
  452. _PR_Schedule(); \
  453. }
  454. #define _MD_RESTORE_CONTEXT(_newThread) \
  455. { \
  456. _MD_RESTORE_ERRNO(_newThread) \
  457. _MD_SET_CURRENT_THREAD(_newThread); \
  458. longjmp(CONTEXT(_newThread), 1); \
  459. }
  460. #else
  461. /*
  462. ** Initialize the thread context preparing it to execute _main.
  463. */
  464. #define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \
  465. PR_BEGIN_MACRO \
  466. ucontext_t *uc = CONTEXT(_thread); \
  467. *status = PR_TRUE; \
  468. getcontext(uc); \
  469. uc->uc_stack.ss_sp = (char *) ((unsigned long)(_sp - WINDOWSIZE - SA(MINFRAME)) & 0xfffffff8); \
  470. uc->uc_stack.ss_size = _thread->stack->stackSize; \
  471. uc->uc_stack.ss_flags = 0; /* ? */ \
  472. uc->uc_mcontext.gregs[REG_SP] = (unsigned int) uc->uc_stack.ss_sp; \
  473. uc->uc_mcontext.gregs[REG_PC] = (unsigned int) _main; \
  474. uc->uc_mcontext.gregs[REG_nPC] = (unsigned int) ((char*)_main)+4; \
  475. uc->uc_flags = UC_ALL; \
  476. _thread->no_sched = 0; \
  477. PR_END_MACRO
  478. /*
  479. ** Switch away from the current thread context by saving its state and
  480. ** calling the thread scheduler. Reload cpu when we come back from the
  481. ** context switch because it might have changed.
  482. */
  483. #define _MD_SWITCH_CONTEXT(_thread) \
  484. PR_BEGIN_MACRO \
  485. if (!getcontext(CONTEXT(_thread))) { \
  486. _MD_SAVE_ERRNO(_thread); \
  487. _MD_SET_LAST_THREAD(_thread); \
  488. _PR_Schedule(); \
  489. } \
  490. PR_END_MACRO
  491. /*
  492. ** Restore a thread context that was saved by _MD_SWITCH_CONTEXT or
  493. ** initialized by _MD_INIT_CONTEXT.
  494. */
  495. #define _MD_RESTORE_CONTEXT(_newThread) \
  496. PR_BEGIN_MACRO \
  497. ucontext_t *uc = CONTEXT(_newThread); \
  498. uc->uc_mcontext.gregs[11] = 1; \
  499. _MD_RESTORE_ERRNO(_newThread); \
  500. _MD_SET_CURRENT_THREAD(_newThread); \
  501. setcontext(uc); \
  502. PR_END_MACRO
  503. #endif
  504. #else /* x86 solaris */
  505. #ifdef USE_SETJMP
  506. #define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \
  507. PR_BEGIN_MACRO \
  508. *status = PR_TRUE; \
  509. if (setjmp(CONTEXT(_thread))) _main(); \
  510. _MD_GET_SP(_thread) = (int) ((_sp) - 64); \
  511. PR_END_MACRO
  512. #define _MD_SWITCH_CONTEXT(_thread) \
  513. if (!setjmp(CONTEXT(_thread))) { \
  514. _MD_SAVE_ERRNO(_thread) \
  515. _PR_Schedule(); \
  516. }
  517. #define _MD_RESTORE_CONTEXT(_newThread) \
  518. { \
  519. _MD_RESTORE_ERRNO(_newThread) \
  520. _MD_SET_CURRENT_THREAD(_newThread); \
  521. longjmp(CONTEXT(_newThread), 1); \
  522. }
  523. #else /* USE_SETJMP */
  524. #define WINDOWSIZE 0
  525. int getedi(void);
  526. void setedi(int);
  527. #define _MD_INIT_CONTEXT(_thread, _sp, _main, status) \
  528. PR_BEGIN_MACRO \
  529. ucontext_t *uc = CONTEXT(_thread); \
  530. *status = PR_TRUE; \
  531. getcontext(uc); \
  532. /* Force sp to be double aligned! */ \
  533. uc->uc_mcontext.gregs[USP] = (int) ((unsigned long)(_sp - WINDOWSIZE - SA(MINFRAME)) & 0xfffffff8); \
  534. uc->uc_mcontext.gregs[PC] = (int) _main; \
  535. (_thread)->no_sched = 0; \
  536. PR_END_MACRO
  537. /* getcontext() may return 1, contrary to what the man page says */
  538. #define _MD_SWITCH_CONTEXT(_thread) \
  539. PR_BEGIN_MACRO \
  540. ucontext_t *uc = CONTEXT(_thread); \
  541. PR_ASSERT(_thread->no_sched); \
  542. sigfillset(&((_thread)->md.blockMask)); \
  543. sigprocmask(SIG_BLOCK, &((_thread)->md.blockMask), \
  544. &((_thread)->md.oldMask)); \
  545. (_thread)->md.edi = getedi(); \
  546. if (! getcontext(uc)) { \
  547. sigprocmask(SIG_SETMASK, &((_thread)->md.oldMask), NULL); \
  548. uc->uc_mcontext.gregs[EDI] = (_thread)->md.edi; \
  549. _MD_SAVE_ERRNO(_thread) \
  550. _MD_SET_LAST_THREAD(_thread); \
  551. _PR_Schedule(); \
  552. } else { \
  553. sigprocmask(SIG_SETMASK, &((_thread)->md.oldMask), NULL); \
  554. setedi((_thread)->md.edi); \
  555. PR_ASSERT(_MD_LAST_THREAD() !=_MD_CURRENT_THREAD()); \
  556. _MD_LAST_THREAD()->no_sched = 0; \
  557. } \
  558. PR_END_MACRO
  559. /*
  560. ** Restore a thread context, saved by _PR_SWITCH_CONTEXT
  561. */
  562. #define _MD_RESTORE_CONTEXT(_newthread) \
  563. PR_BEGIN_MACRO \
  564. ucontext_t *uc = CONTEXT(_newthread); \
  565. uc->uc_mcontext.gregs[EAX] = 1; \
  566. _MD_RESTORE_ERRNO(_newthread) \
  567. _MD_SET_CURRENT_THREAD(_newthread); \
  568. (_newthread)->no_sched = 1; \
  569. setcontext(uc); \
  570. PR_END_MACRO
  571. #endif /* USE_SETJMP */
  572. #endif /* sparc */
  573. struct _MDLock {
  574. PRInt8 notused;
  575. };
  576. struct _MDCVar {
  577. PRInt8 notused;
  578. };
  579. struct _MDSemaphore {
  580. PRInt8 notused;
  581. };
  582. struct _MDThread {
  583. _PR_CONTEXT_TYPE context;
  584. int errcode;
  585. int id;
  586. };
  587. struct _MDThreadStack {
  588. PRInt8 notused;
  589. };
  590. struct _MDSegment {
  591. PRInt8 notused;
  592. };
  593. /*
  594. * md-specific cpu structure field
  595. */
  596. #define _PR_MD_MAX_OSFD FD_SETSIZE
  597. struct _MDCPU_Unix {
  598. PRCList ioQ;
  599. PRUint32 ioq_timeout;
  600. PRInt32 ioq_max_osfd;
  601. PRInt32 ioq_osfd_cnt;
  602. #ifndef _PR_USE_POLL
  603. fd_set fd_read_set, fd_write_set, fd_exception_set;
  604. PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
  605. fd_exception_cnt[_PR_MD_MAX_OSFD];
  606. #else
  607. struct pollfd *ioq_pollfds;
  608. int ioq_pollfds_size;
  609. #endif /* _PR_USE_POLL */
  610. };
  611. #define _PR_IOQ(_cpu) ((_cpu)->md.md_unix.ioQ)
  612. #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
  613. #define _PR_FD_READ_SET(_cpu) ((_cpu)->md.md_unix.fd_read_set)
  614. #define _PR_FD_READ_CNT(_cpu) ((_cpu)->md.md_unix.fd_read_cnt)
  615. #define _PR_FD_WRITE_SET(_cpu) ((_cpu)->md.md_unix.fd_write_set)
  616. #define _PR_FD_WRITE_CNT(_cpu) ((_cpu)->md.md_unix.fd_write_cnt)
  617. #define _PR_FD_EXCEPTION_SET(_cpu) ((_cpu)->md.md_unix.fd_exception_set)
  618. #define _PR_FD_EXCEPTION_CNT(_cpu) ((_cpu)->md.md_unix.fd_exception_cnt)
  619. #define _PR_IOQ_TIMEOUT(_cpu) ((_cpu)->md.md_unix.ioq_timeout)
  620. #define _PR_IOQ_MAX_OSFD(_cpu) ((_cpu)->md.md_unix.ioq_max_osfd)
  621. #define _PR_IOQ_OSFD_CNT(_cpu) ((_cpu)->md.md_unix.ioq_osfd_cnt)
  622. #define _PR_IOQ_POLLFDS(_cpu) ((_cpu)->md.md_unix.ioq_pollfds)
  623. #define _PR_IOQ_POLLFDS_SIZE(_cpu) ((_cpu)->md.md_unix.ioq_pollfds_size)
  624. #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu) 32
  625. struct _MDCPU {
  626. struct _MDCPU_Unix md_unix;
  627. };
  628. #ifndef _PR_PTHREADS
  629. #define _MD_INIT_LOCKS()
  630. #endif
  631. #define _MD_NEW_LOCK(lock) PR_SUCCESS
  632. #define _MD_FREE_LOCK(lock)
  633. #define _MD_LOCK(lock)
  634. #define _MD_UNLOCK(lock)
  635. #define _MD_INIT_IO()
  636. #define _MD_IOQ_LOCK()
  637. #define _MD_IOQ_UNLOCK()
  638. #define _MD_INIT_RUNNING_CPU(cpu) _MD_unix_init_running_cpu(cpu)
  639. #define _MD_INIT_THREAD _MD_InitializeThread
  640. #define _MD_EXIT_THREAD(thread)
  641. #define _MD_SUSPEND_THREAD(thread)
  642. #define _MD_RESUME_THREAD(thread)
  643. #define _MD_CLEAN_THREAD(_thread)
  644. extern PRStatus _MD_WAIT(struct PRThread *, PRIntervalTime timeout);
  645. extern PRStatus _MD_WAKEUP_WAITER(struct PRThread *);
  646. extern void _MD_YIELD(void);
  647. extern PRStatus _MD_InitializeThread(PRThread *thread);
  648. extern void _MD_SET_PRIORITY(struct _MDThread *thread,
  649. PRThreadPriority newPri);
  650. extern PRStatus _MD_CREATE_THREAD(PRThread *thread, void (*start) (void *),
  651. PRThreadPriority priority, PRThreadScope scope, PRThreadState state,
  652. PRUint32 stackSize);
  653. NSPR_API(PRIntervalTime) _MD_Solaris_GetInterval(void);
  654. #define _MD_GET_INTERVAL _MD_Solaris_GetInterval
  655. NSPR_API(PRIntervalTime) _MD_Solaris_TicksPerSecond(void);
  656. #define _MD_INTERVAL_PER_SEC _MD_Solaris_TicksPerSecond
  657. /* The following defines the unwrapped versions of select() and poll(). */
  658. extern int _select(int nfds, fd_set *readfds, fd_set *writefds,
  659. fd_set *exceptfds, struct timeval *timeout);
  660. #define _MD_SELECT _select
  661. #include <stropts.h>
  662. #include <poll.h>
  663. #define _MD_POLL _poll
  664. extern int _poll(struct pollfd *fds, unsigned long nfds, int timeout);
  665. PR_BEGIN_EXTERN_C
  666. /*
  667. ** Missing function prototypes
  668. */
  669. extern int gethostname (char *name, int namelen);
  670. PR_END_EXTERN_C
  671. #endif /* _PR_GLOBAL_THREADS_ONLY */
  672. extern void _MD_solaris_map_sendfile_error(int err);
  673. #endif /* nspr_solaris_defs_h___ */