home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / nsprpub / pr / src / md / unix / hpux.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  9.0 KB  |  335 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /*
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  * 
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  * 
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. #include "primpl.h"
  20. #if defined(HPUX10_30) || defined(HPUX11)
  21. /* for fesettrapenable */
  22. #include <fenv.h>
  23. #else
  24. /* for fpsetmask */
  25. #include <math.h>
  26. #endif
  27. #include <setjmp.h>
  28. #include <signal.h>
  29. #include <values.h>
  30.  
  31. /*
  32. ** On HP-UX we need to define a SIGFPE handler because coercion of a
  33. ** NaN to an int causes SIGFPE to be raised. Thanks to Marianne
  34. ** Mueller and Doug Priest at SunSoft for this fix.
  35. **
  36. ** Under DCE threads, sigaction() installs a per-thread signal handler,
  37. ** so we use the sigvector() interface to install a process-wide
  38. ** handler.
  39. */
  40.  
  41. #ifdef _PR_DCETHREADS
  42. static void
  43. CatchFPE(int sig, int code, struct sigcontext *scp)
  44. {
  45.     unsigned i, e;
  46.     int r, t;
  47.     int *source, *destination;
  48.  
  49.     /* check excepting instructions */
  50.     for ( i = 0; i < 7; i++ ) {
  51.     e = *(i+&(scp->sc_sl.sl_ss.ss_frexcp1));
  52.     if ( e & 0xfc000000 != 0 ) {
  53.         if ((e & 0xf4017720) == 0x24010200) {
  54.                 r = ((e >> 20) & 0x3e);
  55.                 t = (e & 0x1f) << 1;
  56.                 if (e & 0x08000000) {
  57.                     r |= (e >> 7) & 1;
  58.                     t |= (e >> 6) & 1;
  59.                 }
  60.                 source = (int *)(&scp->sc_sl.sl_ss.ss_frstat + r);
  61.                 destination = (int *)(&scp->sc_sl.sl_ss.ss_frstat + t);
  62.                 *destination = *source < 0 ? -MAXINT-1 : MAXINT;
  63.             }
  64.     }
  65.     *(i+&(scp->sc_sl.sl_ss.ss_frexcp1)) = 0;
  66.     }
  67.  
  68.     /* clear T-bit */
  69.     scp->sc_sl.sl_ss.ss_frstat &= ~0x40;
  70. }
  71. #else /* _PR_DCETHREADS */
  72. static void
  73. CatchFPE(int sig, siginfo_t *info, void *context)
  74. {
  75.     ucontext_t *ucp = (ucontext_t *) context;
  76.     unsigned i, e;
  77.     int r, t;
  78.     int *source, *destination;
  79.  
  80.     /* check excepting instructions */
  81.     for ( i = 0; i < 7; i++ ) {
  82.     e = *(i+&(ucp->uc_mcontext.ss_frexcp1));
  83.     if ( e & 0xfc000000 != 0 ) {
  84.         if ((e & 0xf4017720) == 0x24010200) {
  85.                 r = ((e >> 20) & 0x3e);
  86.                 t = (e & 0x1f) << 1;
  87.                 if (e & 0x08000000) {
  88.                     r |= (e >> 7) & 1;
  89.                     t |= (e >> 6) & 1;
  90.                 }
  91.                 source = (int *)(&ucp->uc_mcontext.ss_frstat + r);
  92.                 destination = (int *)(&ucp->uc_mcontext.ss_frstat + t);
  93.                 *destination = *source < 0 ? -MAXINT-1 : MAXINT;
  94.             }
  95.     }
  96.     *(i+&(ucp->uc_mcontext.ss_frexcp1)) = 0;
  97.     }
  98.  
  99.     /* clear T-bit */
  100.     ucp->uc_mcontext.ss_frstat &= ~0x40;
  101. }
  102. #endif /* _PR_DCETHREADS */
  103.  
  104. void _MD_hpux_install_sigfpe_handler(void)
  105. {
  106. #ifdef _PR_DCETHREADS
  107.     struct sigvec v;
  108.  
  109.     v.sv_handler = CatchFPE;
  110.     v.sv_mask = 0;
  111.     v.sv_flags = 0;
  112.     sigvector(SIGFPE, &v, NULL);
  113. #else
  114.     struct sigaction act;
  115.  
  116.     sigaction(SIGFPE, NULL, &act);
  117.     act.sa_flags |= SA_SIGINFO;
  118.     act.sa_sigaction = CatchFPE;
  119.     sigaction(SIGFPE, &act, NULL);
  120. #endif /* _PR_DCETHREADS */
  121.  
  122. #if defined(HPUX10_30) || defined(HPUX11)
  123.     fesettrapenable(FE_INVALID);
  124. #else
  125.     fpsetmask(FP_X_INV);
  126. #endif
  127. }
  128.  
  129. #if !defined(PTHREADS_USER)
  130.  
  131. void _MD_EarlyInit(void)
  132. {
  133.     _MD_hpux_install_sigfpe_handler();
  134.  
  135. #ifndef _PR_PTHREADS
  136.     /*
  137.      * The following piece of code is taken from ns/nspr/src/md_HP-UX.c.
  138.      * In the comment for revision 1.6, dated 1995/09/11 23:33:34,
  139.      * robm says:
  140.      *     This version has some problems which need to be addressed.
  141.      *     First, intercept all system calls and prevent them from
  142.      *     executing the library code which performs stack switches
  143.      *     before normal system call invocation.  In order for library
  144.      *     calls which make system calls to work (like stdio), however,
  145.      *     we must also allocate our own stack and switch the primordial
  146.      *     stack to use it. This isn't so bad, except that I fudged the
  147.      *     backtrace length when copying the old stack to the new one.
  148.      *
  149.      * This is the original comment of robm in the code:
  150.      *    XXXrobm Horrific. To avoid a problem with HP's system call
  151.      *    code, we allocate a new stack for the primordial thread and
  152.      *    use it. However, we don't know how far back the original stack
  153.      *    goes. We should create a routine that performs a backtrace and
  154.      *    finds out just how much we need to copy. As a temporary measure,
  155.      *    I just copy an arbitrary guess.
  156.      *
  157.      * In an email to servereng dated 2 Jan 1997, Mike Patnode (mikep)
  158.      * suggests that this only needs to be done for HP-UX 9.
  159.      */
  160. #ifdef HPUX9
  161. #define PIDOOMA_STACK_SIZE 524288
  162. #define BACKTRACE_SIZE 8192
  163.     {
  164.         jmp_buf jb;
  165.         char *newstack;
  166.         char *oldstack;
  167.  
  168.         if(!setjmp(jb)) {
  169.             newstack = (char *) PR_MALLOC(PIDOOMA_STACK_SIZE);
  170.         oldstack = (char *) (*(((int *) jb) + 1) - BACKTRACE_SIZE);
  171.             memcpy(newstack, oldstack, BACKTRACE_SIZE);
  172.             *(((int *) jb) + 1) = (int) (newstack + BACKTRACE_SIZE);
  173.             longjmp(jb, 1);
  174.         }
  175.     }
  176. #endif  /* HPUX9 */
  177. #endif  /* !_PR_PTHREADS */
  178. }
  179.  
  180. PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np)
  181. {
  182. #ifndef _PR_PTHREADS
  183.     if (isCurrent) {
  184.     (void) setjmp(CONTEXT(t));
  185.     }
  186.     *np = sizeof(CONTEXT(t)) / sizeof(PRWord);
  187.     return (PRWord *) CONTEXT(t);
  188. #else
  189.     *np = 0;
  190.     return NULL;
  191. #endif
  192. }
  193.  
  194. #ifndef _PR_PTHREADS
  195. void
  196. _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri)
  197. {
  198.     return;
  199. }
  200.  
  201. PRStatus
  202. _MD_InitializeThread(PRThread *thread)
  203. {
  204.     return PR_SUCCESS;
  205. }
  206.  
  207. PRStatus
  208. _MD_WAIT(PRThread *thread, PRIntervalTime ticks)
  209. {
  210.     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  211.     _PR_MD_SWITCH_CONTEXT(thread);
  212.     return PR_SUCCESS;
  213. }
  214.  
  215. PRStatus
  216. _MD_WAKEUP_WAITER(PRThread *thread)
  217. {
  218.     if (thread) {
  219.     PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE));
  220.     }
  221.     return PR_SUCCESS;
  222. }
  223.  
  224. /* These functions should not be called for HP-UX */
  225. void
  226. _MD_YIELD(void)
  227. {
  228.     PR_NOT_REACHED("_MD_YIELD should not be called for HP-UX.");
  229. }
  230.  
  231. PRStatus
  232. _MD_CREATE_THREAD(
  233.     PRThread *thread,
  234.     void (*start) (void *),
  235.     PRThreadPriority priority,
  236.     PRThreadScope scope,
  237.     PRThreadState state,
  238.     PRUint32 stackSize)
  239. {
  240.     PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for HP-UX.");
  241. }
  242. #endif /* _PR_PTHREADS */
  243.  
  244. void
  245. _MD_suspend_thread(PRThread *thread)
  246. {
  247. #ifdef _PR_PTHREADS
  248. #endif
  249. }
  250.  
  251. void
  252. _MD_resume_thread(PRThread *thread)
  253. {
  254. #ifdef _PR_PTHREADS
  255. #endif
  256. }
  257. #endif /* PTHREADS_USER */
  258.  
  259. /*
  260.  * See if we have the privilege to set the scheduling policy and
  261.  * priority of threads.  Returns 0 if privilege is available.
  262.  * Returns EPERM otherwise.
  263.  */
  264.  
  265. #if defined(_PR_PTHREADS) && !defined(_PR_DCETHREADS)
  266. PRIntn pt_hpux_privcheck()
  267. {
  268.     PRIntn policy;
  269.     struct sched_param schedule;
  270.     PRIntn rv;
  271.     pthread_t me = pthread_self();
  272.  
  273.     rv = pthread_getschedparam(me, &policy, &schedule);
  274.     PR_ASSERT(0 == rv);
  275.     rv = pthread_setschedparam(me, policy, &schedule);
  276.     PR_ASSERT(0 == rv || EPERM == rv);
  277.     return rv;
  278. }
  279. #endif
  280.  
  281. /*
  282.  * The HP version of strchr is buggy. It looks past the end of the
  283.  * string and causes a segmentation fault when our (NSPR) version
  284.  * of malloc is used.
  285.  *
  286.  * A better solution might be to put a cushion in our malloc just in
  287.  * case HP's version of strchr somehow gets used instead of this one.
  288.  */
  289. char *
  290. strchr(const char *s, int c)
  291. {
  292.     char ch;
  293.  
  294.     if (!s) {
  295.         return NULL;
  296.     }
  297.  
  298.     ch = (char) c;
  299.  
  300.     while ((*s) && ((*s) != ch)) {
  301.         s++;
  302.     }
  303.  
  304.     if ((*s) == ch) {
  305.         return (char *) s;
  306.     }
  307.  
  308.     return NULL;
  309. }
  310.  
  311. /*
  312.  * Implemementation of memcmp in HP-UX (verified on releases A.09.03,
  313.  * A.09.07, and B.10.10) dumps core if called with:
  314.  * 1. First operand with address = 1(mod 4).
  315.  * 2. Size = 1(mod 4)
  316.  * 3. Last byte of the second operand is the last byte of the page and 
  317.  *    next page is not accessible(not mapped or protected)
  318.  * Thus, using the following naive version (tons of optimizations are
  319.  * possible;^)
  320.  */
  321.  
  322. int memcmp(const void *s1, const void *s2, size_t n)
  323. {
  324.     register unsigned char *p1 = (unsigned char *) s1,
  325.             *p2 = (unsigned char *) s2;
  326.  
  327.     while (n-- > 0) {
  328.         register int r = ((int) ((unsigned int) *p1)) 
  329.                 - ((int) ((unsigned int) *p2));
  330.         if (r) return r;
  331.         p1++; p2++;
  332.     }
  333.     return 0; 
  334. }
  335.