Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #include "primpl.h" |
michael@0 | 7 | #include <setjmp.h> |
michael@0 | 8 | |
michael@0 | 9 | #if defined(HPUX_LW_TIMER) |
michael@0 | 10 | |
michael@0 | 11 | #include <machine/inline.h> |
michael@0 | 12 | #include <machine/clock.h> |
michael@0 | 13 | #include <unistd.h> |
michael@0 | 14 | #include <sys/time.h> |
michael@0 | 15 | #include <sys/pstat.h> |
michael@0 | 16 | |
michael@0 | 17 | int __lw_get_thread_times(int which, int64_t *sample, int64_t *time); |
michael@0 | 18 | |
michael@0 | 19 | static double msecond_per_itick; |
michael@0 | 20 | |
michael@0 | 21 | void _PR_HPUX_LW_IntervalInit(void) |
michael@0 | 22 | { |
michael@0 | 23 | struct pst_processor psp; |
michael@0 | 24 | int iticksperclktick, clk_tck; |
michael@0 | 25 | int rv; |
michael@0 | 26 | |
michael@0 | 27 | rv = pstat_getprocessor(&psp, sizeof(psp), 1, 0); |
michael@0 | 28 | PR_ASSERT(rv != -1); |
michael@0 | 29 | |
michael@0 | 30 | iticksperclktick = psp.psp_iticksperclktick; |
michael@0 | 31 | clk_tck = sysconf(_SC_CLK_TCK); |
michael@0 | 32 | msecond_per_itick = (1000.0)/(double)(iticksperclktick * clk_tck); |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | PRIntervalTime _PR_HPUX_LW_GetInterval(void) |
michael@0 | 36 | { |
michael@0 | 37 | int64_t time, sample; |
michael@0 | 38 | |
michael@0 | 39 | __lw_get_thread_times(1, &sample, &time); |
michael@0 | 40 | /* |
michael@0 | 41 | * Division is slower than float multiplication. |
michael@0 | 42 | * return (time / iticks_per_msecond); |
michael@0 | 43 | */ |
michael@0 | 44 | return (time * msecond_per_itick); |
michael@0 | 45 | } |
michael@0 | 46 | #endif /* HPUX_LW_TIMER */ |
michael@0 | 47 | |
michael@0 | 48 | #if !defined(PTHREADS_USER) |
michael@0 | 49 | |
michael@0 | 50 | void _MD_EarlyInit(void) |
michael@0 | 51 | { |
michael@0 | 52 | #ifndef _PR_PTHREADS |
michael@0 | 53 | /* |
michael@0 | 54 | * The following piece of code is taken from ns/nspr/src/md_HP-UX.c. |
michael@0 | 55 | * In the comment for revision 1.6, dated 1995/09/11 23:33:34, |
michael@0 | 56 | * robm says: |
michael@0 | 57 | * This version has some problems which need to be addressed. |
michael@0 | 58 | * First, intercept all system calls and prevent them from |
michael@0 | 59 | * executing the library code which performs stack switches |
michael@0 | 60 | * before normal system call invocation. In order for library |
michael@0 | 61 | * calls which make system calls to work (like stdio), however, |
michael@0 | 62 | * we must also allocate our own stack and switch the primordial |
michael@0 | 63 | * stack to use it. This isn't so bad, except that I fudged the |
michael@0 | 64 | * backtrace length when copying the old stack to the new one. |
michael@0 | 65 | * |
michael@0 | 66 | * This is the original comment of robm in the code: |
michael@0 | 67 | * XXXrobm Horrific. To avoid a problem with HP's system call |
michael@0 | 68 | * code, we allocate a new stack for the primordial thread and |
michael@0 | 69 | * use it. However, we don't know how far back the original stack |
michael@0 | 70 | * goes. We should create a routine that performs a backtrace and |
michael@0 | 71 | * finds out just how much we need to copy. As a temporary measure, |
michael@0 | 72 | * I just copy an arbitrary guess. |
michael@0 | 73 | * |
michael@0 | 74 | * In an email to servereng dated 2 Jan 1997, Mike Patnode (mikep) |
michael@0 | 75 | * suggests that this only needs to be done for HP-UX 9. |
michael@0 | 76 | */ |
michael@0 | 77 | #ifdef HPUX9 |
michael@0 | 78 | #define PIDOOMA_STACK_SIZE 524288 |
michael@0 | 79 | #define BACKTRACE_SIZE 8192 |
michael@0 | 80 | { |
michael@0 | 81 | jmp_buf jb; |
michael@0 | 82 | char *newstack; |
michael@0 | 83 | char *oldstack; |
michael@0 | 84 | |
michael@0 | 85 | if(!setjmp(jb)) { |
michael@0 | 86 | newstack = (char *) PR_MALLOC(PIDOOMA_STACK_SIZE); |
michael@0 | 87 | oldstack = (char *) (*(((int *) jb) + 1) - BACKTRACE_SIZE); |
michael@0 | 88 | memcpy(newstack, oldstack, BACKTRACE_SIZE); |
michael@0 | 89 | *(((int *) jb) + 1) = (int) (newstack + BACKTRACE_SIZE); |
michael@0 | 90 | longjmp(jb, 1); |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | #endif /* HPUX9 */ |
michael@0 | 94 | #endif /* !_PR_PTHREADS */ |
michael@0 | 95 | } |
michael@0 | 96 | |
michael@0 | 97 | PRWord *_MD_HomeGCRegisters(PRThread *t, int isCurrent, int *np) |
michael@0 | 98 | { |
michael@0 | 99 | #ifndef _PR_PTHREADS |
michael@0 | 100 | if (isCurrent) { |
michael@0 | 101 | (void) setjmp(CONTEXT(t)); |
michael@0 | 102 | } |
michael@0 | 103 | *np = sizeof(CONTEXT(t)) / sizeof(PRWord); |
michael@0 | 104 | return (PRWord *) CONTEXT(t); |
michael@0 | 105 | #else |
michael@0 | 106 | *np = 0; |
michael@0 | 107 | return NULL; |
michael@0 | 108 | #endif |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | #ifndef _PR_PTHREADS |
michael@0 | 112 | void |
michael@0 | 113 | _MD_SET_PRIORITY(_MDThread *thread, PRUintn newPri) |
michael@0 | 114 | { |
michael@0 | 115 | return; |
michael@0 | 116 | } |
michael@0 | 117 | |
michael@0 | 118 | PRStatus |
michael@0 | 119 | _MD_InitializeThread(PRThread *thread) |
michael@0 | 120 | { |
michael@0 | 121 | return PR_SUCCESS; |
michael@0 | 122 | } |
michael@0 | 123 | |
michael@0 | 124 | PRStatus |
michael@0 | 125 | _MD_WAIT(PRThread *thread, PRIntervalTime ticks) |
michael@0 | 126 | { |
michael@0 | 127 | PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE)); |
michael@0 | 128 | _PR_MD_SWITCH_CONTEXT(thread); |
michael@0 | 129 | return PR_SUCCESS; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | PRStatus |
michael@0 | 133 | _MD_WAKEUP_WAITER(PRThread *thread) |
michael@0 | 134 | { |
michael@0 | 135 | if (thread) { |
michael@0 | 136 | PR_ASSERT(!(thread->flags & _PR_GLOBAL_SCOPE)); |
michael@0 | 137 | } |
michael@0 | 138 | return PR_SUCCESS; |
michael@0 | 139 | } |
michael@0 | 140 | |
michael@0 | 141 | /* These functions should not be called for HP-UX */ |
michael@0 | 142 | void |
michael@0 | 143 | _MD_YIELD(void) |
michael@0 | 144 | { |
michael@0 | 145 | PR_NOT_REACHED("_MD_YIELD should not be called for HP-UX."); |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | PRStatus |
michael@0 | 149 | _MD_CREATE_THREAD( |
michael@0 | 150 | PRThread *thread, |
michael@0 | 151 | void (*start) (void *), |
michael@0 | 152 | PRThreadPriority priority, |
michael@0 | 153 | PRThreadScope scope, |
michael@0 | 154 | PRThreadState state, |
michael@0 | 155 | PRUint32 stackSize) |
michael@0 | 156 | { |
michael@0 | 157 | PR_NOT_REACHED("_MD_CREATE_THREAD should not be called for HP-UX."); |
michael@0 | 158 | } |
michael@0 | 159 | #endif /* _PR_PTHREADS */ |
michael@0 | 160 | |
michael@0 | 161 | void |
michael@0 | 162 | _MD_suspend_thread(PRThread *thread) |
michael@0 | 163 | { |
michael@0 | 164 | #ifdef _PR_PTHREADS |
michael@0 | 165 | #endif |
michael@0 | 166 | } |
michael@0 | 167 | |
michael@0 | 168 | void |
michael@0 | 169 | _MD_resume_thread(PRThread *thread) |
michael@0 | 170 | { |
michael@0 | 171 | #ifdef _PR_PTHREADS |
michael@0 | 172 | #endif |
michael@0 | 173 | } |
michael@0 | 174 | #endif /* PTHREADS_USER */ |
michael@0 | 175 | |
michael@0 | 176 | /* |
michael@0 | 177 | * The HP version of strchr is buggy. It looks past the end of the |
michael@0 | 178 | * string and causes a segmentation fault when our (NSPR) version |
michael@0 | 179 | * of malloc is used. |
michael@0 | 180 | * |
michael@0 | 181 | * A better solution might be to put a cushion in our malloc just in |
michael@0 | 182 | * case HP's version of strchr somehow gets used instead of this one. |
michael@0 | 183 | */ |
michael@0 | 184 | char * |
michael@0 | 185 | strchr(const char *s, int c) |
michael@0 | 186 | { |
michael@0 | 187 | char ch; |
michael@0 | 188 | |
michael@0 | 189 | if (!s) { |
michael@0 | 190 | return NULL; |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | ch = (char) c; |
michael@0 | 194 | |
michael@0 | 195 | while ((*s) && ((*s) != ch)) { |
michael@0 | 196 | s++; |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | if ((*s) == ch) { |
michael@0 | 200 | return (char *) s; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | return NULL; |
michael@0 | 204 | } |
michael@0 | 205 | |
michael@0 | 206 | /* |
michael@0 | 207 | * Implemementation of memcmp in HP-UX (verified on releases A.09.03, |
michael@0 | 208 | * A.09.07, and B.10.10) dumps core if called with: |
michael@0 | 209 | * 1. First operand with address = 1(mod 4). |
michael@0 | 210 | * 2. Size = 1(mod 4) |
michael@0 | 211 | * 3. Last byte of the second operand is the last byte of the page and |
michael@0 | 212 | * next page is not accessible(not mapped or protected) |
michael@0 | 213 | * Thus, using the following naive version (tons of optimizations are |
michael@0 | 214 | * possible;^) |
michael@0 | 215 | */ |
michael@0 | 216 | |
michael@0 | 217 | int memcmp(const void *s1, const void *s2, size_t n) |
michael@0 | 218 | { |
michael@0 | 219 | register unsigned char *p1 = (unsigned char *) s1, |
michael@0 | 220 | *p2 = (unsigned char *) s2; |
michael@0 | 221 | |
michael@0 | 222 | while (n-- > 0) { |
michael@0 | 223 | register int r = ((int) ((unsigned int) *p1)) |
michael@0 | 224 | - ((int) ((unsigned int) *p2)); |
michael@0 | 225 | if (r) return r; |
michael@0 | 226 | p1++; p2++; |
michael@0 | 227 | } |
michael@0 | 228 | return 0; |
michael@0 | 229 | } |