tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

_linux.h (23503B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 /*
      7 * This file is used by not only Linux but also other glibc systems
      8 * such as GNU/Hurd and GNU/k*BSD.
      9 */
     10 
     11 #ifndef nspr_linux_defs_h___
     12 #define nspr_linux_defs_h___
     13 
     14 #include "prthread.h"
     15 
     16 /*
     17 * Internal configuration macros
     18 */
     19 
     20 #define PR_LINKER_ARCH  "linux"
     21 #define _PR_SI_SYSNAME  "LINUX"
     22 #ifdef __powerpc64__
     23 #define _PR_SI_ARCHITECTURE "ppc64"
     24 #elif defined(__powerpc__)
     25 #define _PR_SI_ARCHITECTURE "ppc"
     26 #elif defined(__alpha)
     27 #define _PR_SI_ARCHITECTURE "alpha"
     28 #elif defined(__ia64__)
     29 #define _PR_SI_ARCHITECTURE "ia64"
     30 #elif defined(__x86_64__)
     31 #define _PR_SI_ARCHITECTURE "x86-64"
     32 #elif defined(__mc68000__)
     33 #define _PR_SI_ARCHITECTURE "m68k"
     34 #elif defined(__sparc__) && defined(__arch64__)
     35 #define _PR_SI_ARCHITECTURE "sparc64"
     36 #elif defined(__sparc__)
     37 #define _PR_SI_ARCHITECTURE "sparc"
     38 #elif defined(__i386__)
     39 #define _PR_SI_ARCHITECTURE "x86"
     40 #elif defined(__mips__)
     41 #define _PR_SI_ARCHITECTURE "mips"
     42 #elif defined(__arm__)
     43 #define _PR_SI_ARCHITECTURE "arm"
     44 #elif defined(__aarch64__)
     45 #define _PR_SI_ARCHITECTURE "aarch64"
     46 #elif defined(__hppa__)
     47 #define _PR_SI_ARCHITECTURE "hppa"
     48 #elif defined(__s390x__)
     49 #define _PR_SI_ARCHITECTURE "s390x"
     50 #elif defined(__s390__)
     51 #define _PR_SI_ARCHITECTURE "s390"
     52 #elif defined(__sh__)
     53 #define _PR_SI_ARCHITECTURE "sh"
     54 #elif defined(__avr32__)
     55 #define _PR_SI_ARCHITECTURE "avr32"
     56 #elif defined(__m32r__)
     57 #define _PR_SI_ARCHITECTURE "m32r"
     58 #elif defined(__or1k__)
     59 #define _PR_SI_ARCHITECTURE "or1k"
     60 #elif defined(__riscv) && (__riscv_xlen == 32)
     61 #define _PR_SI_ARCHITECTURE "riscv32"
     62 #elif defined(__riscv) && (__riscv_xlen == 64)
     63 #define _PR_SI_ARCHITECTURE "riscv64"
     64 #elif defined(__e2k__)
     65 #define _PR_SI_ARCHITECTURE "e2k"
     66 #elif defined(__arc__)
     67 #define _PR_SI_ARCHITECTURE "arc"
     68 #elif defined(__nios2__)
     69 #define _PR_SI_ARCHITECTURE "nios2"
     70 #elif defined(__microblaze__)
     71 #define _PR_SI_ARCHITECTURE "microblaze"
     72 #elif defined(__nds32__)
     73 #define _PR_SI_ARCHITECTURE "nds32"
     74 #elif defined(__xtensa__)
     75 #define _PR_SI_ARCHITECTURE "xtensa"
     76 #elif defined(__loongarch64)
     77 #define _PR_SI_ARCHITECTURE "loongarch64"
     78 #else
     79 #error "Unknown CPU architecture"
     80 #endif
     81 #define PR_DLL_SUFFIX       ".so"
     82 
     83 #define _PR_VMBASE              0x30000000
     84 #define _PR_STACK_VMBASE    0x50000000
     85 #define _MD_DEFAULT_STACK_SIZE  65536L
     86 #define _MD_MMAP_FLAGS          MAP_PRIVATE
     87 
     88 #if defined(__aarch64__) || defined(__mips__)
     89 #define _MD_MINIMUM_STACK_SIZE  0x20000
     90 #endif
     91 
     92 #undef  HAVE_STACK_GROWING_UP
     93 
     94 /*
     95 * Elf linux supports dl* functions
     96 */
     97 #define HAVE_DLL
     98 #define USE_DLFCN
     99 #if defined(ANDROID)
    100 #define NO_DLOPEN_NULL
    101 #endif
    102 
    103 #if defined(__FreeBSD_kernel__) || defined(__GNU__)
    104 #define _PR_HAVE_SOCKADDR_LEN
    105 #endif
    106 
    107 #if defined(__i386__)
    108 #define _PR_HAVE_ATOMIC_OPS
    109 #define _MD_INIT_ATOMIC()
    110 extern PRInt32 _PR_x86_AtomicIncrement(PRInt32 *val);
    111 #define _MD_ATOMIC_INCREMENT          _PR_x86_AtomicIncrement
    112 extern PRInt32 _PR_x86_AtomicDecrement(PRInt32 *val);
    113 #define _MD_ATOMIC_DECREMENT          _PR_x86_AtomicDecrement
    114 extern PRInt32 _PR_x86_AtomicAdd(PRInt32 *ptr, PRInt32 val);
    115 #define _MD_ATOMIC_ADD                _PR_x86_AtomicAdd
    116 extern PRInt32 _PR_x86_AtomicSet(PRInt32 *val, PRInt32 newval);
    117 #define _MD_ATOMIC_SET                _PR_x86_AtomicSet
    118 #endif
    119 
    120 #if defined(__ia64__)
    121 #define _PR_HAVE_ATOMIC_OPS
    122 #define _MD_INIT_ATOMIC()
    123 extern PRInt32 _PR_ia64_AtomicIncrement(PRInt32 *val);
    124 #define _MD_ATOMIC_INCREMENT          _PR_ia64_AtomicIncrement
    125 extern PRInt32 _PR_ia64_AtomicDecrement(PRInt32 *val);
    126 #define _MD_ATOMIC_DECREMENT          _PR_ia64_AtomicDecrement
    127 extern PRInt32 _PR_ia64_AtomicAdd(PRInt32 *ptr, PRInt32 val);
    128 #define _MD_ATOMIC_ADD                _PR_ia64_AtomicAdd
    129 extern PRInt32 _PR_ia64_AtomicSet(PRInt32 *val, PRInt32 newval);
    130 #define _MD_ATOMIC_SET                _PR_ia64_AtomicSet
    131 #endif
    132 
    133 #if defined(__x86_64__)
    134 #define _PR_HAVE_ATOMIC_OPS
    135 #define _MD_INIT_ATOMIC()
    136 extern PRInt32 _PR_x86_64_AtomicIncrement(PRInt32 *val);
    137 #define _MD_ATOMIC_INCREMENT          _PR_x86_64_AtomicIncrement
    138 extern PRInt32 _PR_x86_64_AtomicDecrement(PRInt32 *val);
    139 #define _MD_ATOMIC_DECREMENT          _PR_x86_64_AtomicDecrement
    140 extern PRInt32 _PR_x86_64_AtomicAdd(PRInt32 *ptr, PRInt32 val);
    141 #define _MD_ATOMIC_ADD                _PR_x86_64_AtomicAdd
    142 extern PRInt32 _PR_x86_64_AtomicSet(PRInt32 *val, PRInt32 newval);
    143 #define _MD_ATOMIC_SET                _PR_x86_64_AtomicSet
    144 #endif
    145 
    146 #if defined(__loongarch__)
    147 #if defined(__GNUC__)
    148 /* Use GCC built-in functions */
    149 #define _PR_HAVE_ATOMIC_OPS
    150 #define _MD_INIT_ATOMIC()
    151 #define _MD_ATOMIC_INCREMENT(ptr) __sync_add_and_fetch(ptr, 1)
    152 #define _MD_ATOMIC_DECREMENT(ptr) __sync_sub_and_fetch(ptr, 1)
    153 #define _MD_ATOMIC_ADD(ptr, i) __sync_add_and_fetch(ptr, i)
    154 #define _MD_ATOMIC_SET(ptr, nv) __sync_lock_test_and_set(ptr, nv)
    155 #endif
    156 #endif
    157 
    158 #if defined(__or1k__)
    159 #if defined(__GNUC__)
    160 /* Use GCC built-in functions */
    161 #define _PR_HAVE_ATOMIC_OPS
    162 #define _MD_INIT_ATOMIC()
    163 #define _MD_ATOMIC_INCREMENT(ptr) __sync_add_and_fetch(ptr, 1)
    164 #define _MD_ATOMIC_DECREMENT(ptr) __sync_sub_and_fetch(ptr, 1)
    165 #define _MD_ATOMIC_ADD(ptr, i) __sync_add_and_fetch(ptr, i)
    166 #define _MD_ATOMIC_SET(ptr, nv) __sync_lock_test_and_set(ptr, nv)
    167 #endif
    168 #endif
    169 
    170 #if defined(__powerpc__) && !defined(__powerpc64__)
    171 #define _PR_HAVE_ATOMIC_OPS
    172 #define _MD_INIT_ATOMIC()
    173 extern PRInt32 _PR_ppc_AtomicIncrement(PRInt32 *val);
    174 #define _MD_ATOMIC_INCREMENT          _PR_ppc_AtomicIncrement
    175 extern PRInt32 _PR_ppc_AtomicDecrement(PRInt32 *val);
    176 #define _MD_ATOMIC_DECREMENT          _PR_ppc_AtomicDecrement
    177 extern PRInt32 _PR_ppc_AtomicAdd(PRInt32 *ptr, PRInt32 val);
    178 #define _MD_ATOMIC_ADD                _PR_ppc_AtomicAdd
    179 extern PRInt32 _PR_ppc_AtomicSet(PRInt32 *val, PRInt32 newval);
    180 #define _MD_ATOMIC_SET                _PR_ppc_AtomicSet
    181 #endif
    182 
    183 #if defined(__powerpc64__)
    184 #if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
    185 /* Use GCC built-in functions */
    186 #define _PR_HAVE_ATOMIC_OPS
    187 #define _MD_INIT_ATOMIC()
    188 #define _MD_ATOMIC_INCREMENT(ptr) __sync_add_and_fetch(ptr, 1)
    189 #define _MD_ATOMIC_DECREMENT(ptr) __sync_sub_and_fetch(ptr, 1)
    190 #define _MD_ATOMIC_ADD(ptr, i) __sync_add_and_fetch(ptr, i)
    191 #define _MD_ATOMIC_SET(ptr, nv) __sync_lock_test_and_set(ptr, nv)
    192 #endif
    193 #endif
    194 
    195 #if defined(__mips__) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
    196 /* Use GCC built-in functions */
    197 #define _PR_HAVE_ATOMIC_OPS
    198 #define _MD_INIT_ATOMIC()
    199 #define _MD_ATOMIC_INCREMENT(ptr) __sync_add_and_fetch(ptr, 1)
    200 #define _MD_ATOMIC_DECREMENT(ptr) __sync_sub_and_fetch(ptr, 1)
    201 #define _MD_ATOMIC_ADD(ptr, i) __sync_add_and_fetch(ptr, i)
    202 #define _MD_ATOMIC_SET(ptr, nv) __sync_lock_test_and_set(ptr, nv)
    203 #endif
    204 
    205 #if defined(__riscv) && defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
    206 /* Use GCC built-in functions */
    207 #define _PR_HAVE_ATOMIC_OPS
    208 #define _MD_INIT_ATOMIC()
    209 #define _MD_ATOMIC_INCREMENT(ptr) __sync_add_and_fetch(ptr, 1)
    210 #define _MD_ATOMIC_DECREMENT(ptr) __sync_sub_and_fetch(ptr, 1)
    211 #define _MD_ATOMIC_ADD(ptr, i) __sync_add_and_fetch(ptr, i)
    212 #define _MD_ATOMIC_SET(ptr, nv) __sync_lock_test_and_set(ptr, nv)
    213 #endif
    214 
    215 #if defined(__alpha)
    216 #define _PR_HAVE_ATOMIC_OPS
    217 #define _MD_INIT_ATOMIC()
    218 #define _MD_ATOMIC_ADD(ptr, i) ({               \
    219    PRInt32 __atomic_tmp, __atomic_ret;   \
    220    __asm__ __volatile__(                       \
    221    "1: ldl_l   %[ret], %[val]          \n"     \
    222    "   addl    %[ret], %[inc], %[tmp]  \n"     \
    223    "   addl    %[ret], %[inc], %[ret]  \n"     \
    224    "   stl_c   %[tmp], %[val]          \n"     \
    225    "   beq     %[tmp], 2f              \n"     \
    226    ".subsection 2                      \n"     \
    227    "2: br      1b                      \n"     \
    228    ".previous"                                 \
    229    : [ret] "=&r" (__atomic_ret),               \
    230      [tmp] "=&r" (__atomic_tmp),               \
    231      [val] "=m" (*ptr)                         \
    232    : [inc] "Ir" (i), "m" (*ptr));              \
    233    __atomic_ret;                               \
    234 })
    235 #define _MD_ATOMIC_INCREMENT(ptr) _MD_ATOMIC_ADD(ptr, 1)
    236 #define _MD_ATOMIC_DECREMENT(ptr) ({            \
    237    PRInt32 __atomic_tmp, __atomic_ret;   \
    238    __asm__ __volatile__(                       \
    239    "1: ldl_l   %[ret], %[val]          \n"     \
    240    "   subl    %[ret], 1, %[tmp]       \n"     \
    241    "   subl    %[ret], 1, %[ret]       \n"     \
    242    "   stl_c   %[tmp], %[val]          \n"     \
    243    "   beq     %[tmp], 2f              \n"     \
    244    ".subsection 2                      \n"     \
    245    "2: br      1b                      \n"     \
    246    ".previous"                                 \
    247    : [ret] "=&r" (__atomic_ret),               \
    248      [tmp] "=&r" (__atomic_tmp),               \
    249      [val] "=m" (*ptr)                         \
    250    : "m" (*ptr));                              \
    251    __atomic_ret;                               \
    252 })
    253 #define _MD_ATOMIC_SET(ptr, n) ({               \
    254    PRInt32 __atomic_tmp, __atomic_ret;   \
    255    __asm__ __volatile__(                       \
    256    "1: ldl_l   %[ret], %[val]          \n"     \
    257    "   mov     %[newval], %[tmp]       \n"     \
    258    "   stl_c   %[tmp], %[val]          \n"     \
    259    "   beq     %[tmp], 2f              \n"     \
    260    ".subsection 2                      \n"     \
    261    "2: br      1b                      \n"     \
    262    ".previous"                                 \
    263    : [ret] "=&r" (__atomic_ret),               \
    264      [tmp] "=&r"(__atomic_tmp),                \
    265      [val] "=m" (*ptr)                         \
    266    : [newval] "Ir" (n), "m" (*ptr));           \
    267    __atomic_ret;                               \
    268 })
    269 #endif
    270 
    271 #if defined(__arm__) || defined(__aarch64__)
    272 #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
    273 /* Use GCC built-in functions */
    274 #define _PR_HAVE_ATOMIC_OPS
    275 #define _MD_INIT_ATOMIC()
    276 
    277 #define _MD_ATOMIC_INCREMENT(ptr) __sync_add_and_fetch(ptr, 1)
    278 #define _MD_ATOMIC_DECREMENT(ptr) __sync_sub_and_fetch(ptr, 1)
    279 #define _MD_ATOMIC_SET(ptr, nv) __sync_lock_test_and_set(ptr, nv)
    280 #define _MD_ATOMIC_ADD(ptr, i) __sync_add_and_fetch(ptr, i)
    281 
    282 #elif defined(_PR_ARM_KUSER)
    283 #define _PR_HAVE_ATOMIC_OPS
    284 #define _MD_INIT_ATOMIC()
    285 
    286 /*
    287 * The kernel provides this helper function at a fixed address with a fixed
    288 * ABI signature, directly callable from user space.
    289 *
    290 * Definition:
    291 * Atomically store newval in *ptr if *ptr is equal to oldval.
    292 * Return zero if *ptr was changed or non-zero if no exchange happened.
    293 */
    294 typedef int (__kernel_cmpxchg_t)(int oldval, int newval, volatile int *ptr);
    295 #define __kernel_cmpxchg (*(__kernel_cmpxchg_t *)0xffff0fc0)
    296 
    297 #define _MD_ATOMIC_INCREMENT(ptr) _MD_ATOMIC_ADD(ptr, 1)
    298 #define _MD_ATOMIC_DECREMENT(ptr) _MD_ATOMIC_ADD(ptr, -1)
    299 
    300 static inline PRInt32 _MD_ATOMIC_ADD(PRInt32 *ptr, PRInt32 n)
    301 {
    302    PRInt32 ov, nv;
    303    volatile PRInt32 *vp = ptr;
    304 
    305    do {
    306        ov = *vp;
    307        nv = ov + n;
    308    } while (__kernel_cmpxchg(ov, nv, vp));
    309 
    310    return nv;
    311 }
    312 
    313 static inline PRInt32 _MD_ATOMIC_SET(PRInt32 *ptr, PRInt32 nv)
    314 {
    315    PRInt32 ov;
    316    volatile PRInt32 *vp = ptr;
    317 
    318    do {
    319        ov = *vp;
    320    } while (__kernel_cmpxchg(ov, nv, vp));
    321 
    322    return ov;
    323 }
    324 #endif
    325 #endif /* __arm__ */
    326 
    327 #define USE_SETJMP
    328 #if (defined(__GLIBC__) && __GLIBC__ >= 2) || defined(ANDROID)
    329 #define _PR_POLL_AVAILABLE
    330 #endif
    331 #undef _PR_USE_POLL
    332 #define _PR_STAT_HAS_ONLY_ST_ATIME
    333 #if defined(__alpha) || defined(__ia64__)
    334 #define _PR_HAVE_LARGE_OFF_T
    335 #elif (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1) \
    336    || defined(ANDROID)
    337 #define _PR_HAVE_OFF64_T
    338 #else
    339 #define _PR_NO_LARGE_FILES
    340 #endif
    341 #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1) \
    342    || defined(ANDROID)
    343 #define _PR_INET6
    344 #define _PR_HAVE_INET_NTOP
    345 #define _PR_HAVE_GETHOSTBYNAME2
    346 #define _PR_HAVE_GETADDRINFO
    347 #define _PR_INET6_PROBE
    348 #endif
    349 #ifndef ANDROID
    350 #define _PR_HAVE_SYSV_SEMAPHORES
    351 #define PR_HAVE_SYSV_NAMED_SHARED_MEMORY
    352 #endif
    353 /* Android has gethostbyname_r but not gethostbyaddr_r or gethostbyname2_r. */
    354 #if (__GLIBC__ >= 2) && defined(_PR_PTHREADS)
    355 #define _PR_HAVE_GETHOST_R
    356 #define _PR_HAVE_GETHOST_R_INT
    357 #endif
    358 
    359 #ifdef _PR_PTHREADS
    360 
    361 extern void _MD_CleanupBeforeExit(void);
    362 #define _MD_CLEANUP_BEFORE_EXIT _MD_CleanupBeforeExit
    363 
    364 #else  /* ! _PR_PTHREADS */
    365 
    366 #include <setjmp.h>
    367 
    368 #define PR_CONTEXT_TYPE sigjmp_buf
    369 
    370 #define CONTEXT(_th) ((_th)->md.context)
    371 
    372 #ifdef __powerpc__
    373 /*
    374 * PowerPC based MkLinux
    375 *
    376 * On the PowerPC, the new style jmp_buf isn't used until glibc
    377 * 2.1.
    378 */
    379 #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
    380 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[JB_GPR1]
    381 #else
    382 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__misc[0]
    383 #endif /* glibc 2.1 or later */
    384 #define _MD_SET_FP(_t, val)
    385 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    386 #define _MD_GET_FP_PTR(_t) ((void *) 0)
    387 /* aix = 64, macos = 70 */
    388 #define PR_NUM_GCREGS  64
    389 
    390 #elif defined(__alpha)
    391 /* Alpha based Linux */
    392 
    393 #if defined(__GLIBC__) && __GLIBC__ >= 2
    394 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[JB_SP]
    395 #define _MD_SET_FP(_t, val)
    396 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    397 #define _MD_GET_FP_PTR(_t) ((void *) 0)
    398 #define _MD_SP_TYPE long int
    399 #else
    400 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
    401 #define _MD_SET_FP(_t, val)
    402 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    403 #define _MD_GET_FP_PTR(_t) ((void *) 0)
    404 #define _MD_SP_TYPE __ptr_t
    405 #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
    406 
    407 /* XXX not sure if this is correct, or maybe it should be 17? */
    408 #define PR_NUM_GCREGS 9
    409 
    410 #elif defined(__ia64__)
    411 
    412 #define _MD_GET_SP(_t)      ((long *)((_t)->md.context[0].__jmpbuf)[0])
    413 #define _MD_SET_FP(_t, val)
    414 #define _MD_GET_SP_PTR(_t)  &(_MD_GET_SP(_t))
    415 #define _MD_GET_FP_PTR(_t)  ((void *) 0)
    416 #define _MD_SP_TYPE         long int
    417 
    418 #define PR_NUM_GCREGS       _JBLEN
    419 
    420 #elif defined(__mc68000__)
    421 /* m68k based Linux */
    422 
    423 /*
    424 * On the m68k, glibc still uses the old style sigjmp_buf, even
    425 * in glibc 2.0.7.
    426 */
    427 #if defined(__GLIBC__) && __GLIBC__ >= 2
    428 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
    429 #define _MD_SET_FP(_t, val)
    430 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    431 #define _MD_GET_FP_PTR(_t) ((void *) 0)
    432 #define _MD_SP_TYPE int
    433 #else
    434 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
    435 #define _MD_SET_FP(_t, val)
    436 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    437 #define _MD_GET_FP_PTR(_t) ((void *) 0)
    438 #define _MD_SP_TYPE __ptr_t
    439 #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
    440 
    441 /* XXX not sure if this is correct, or maybe it should be 17? */
    442 #define PR_NUM_GCREGS 9
    443 
    444 #elif defined(__sparc__)
    445 /* Sparc */
    446 #if defined(__GLIBC__) && __GLIBC__ >= 2
    447 /*
    448 * You need glibc2-2.0.7-25 or later. The libraries that came with
    449 * Red Hat 5.1 are not new enough, but they are in 5.2.
    450 */
    451 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[JB_SP]
    452 #define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[JB_FP] = val)
    453 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    454 #define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[JB_FP])
    455 #define _MD_SP_TYPE int
    456 #else
    457 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__fp
    458 #define _MD_SET_FP(_t, val)
    459 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    460 #define _MD_GET_FP_PTR(_t) ((void *) 0)
    461 #define _MD_SP_TYPE __ptr_t
    462 #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
    463 
    464 #elif defined(__i386__)
    465 /* Intel based Linux */
    466 #if defined(__GLIBC__) && __GLIBC__ >= 2
    467 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[JB_SP]
    468 #define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[JB_BP] = val)
    469 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    470 #define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[JB_BP])
    471 #define _MD_SP_TYPE int
    472 #else
    473 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
    474 #define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[0].__bp = val)
    475 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    476 #define _MD_GET_FP_PTR(_t) &((_t)->md.context[0].__jmpbuf[0].__bp)
    477 #define _MD_SP_TYPE __ptr_t
    478 #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
    479 #define PR_NUM_GCREGS   6
    480 
    481 #elif defined(__mips__)
    482 /* Linux/MIPS */
    483 #if defined(__GLIBC__) && __GLIBC__ >= 2
    484 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__sp
    485 #define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[0].__fp = (val))
    486 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    487 #define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[0].__fp)
    488 #define _MD_SP_TYPE __ptr_t
    489 #else
    490 #error "Linux/MIPS pre-glibc2 not supported yet"
    491 #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
    492 
    493 #elif defined(__arm__)
    494 /* ARM/Linux */
    495 #if defined(__GLIBC__) && __GLIBC__ >= 2
    496 #ifdef __ARM_EABI__
    497 /* EABI */
    498 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[8]
    499 #define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[7] = (val))
    500 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    501 #define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[7])
    502 #define _MD_SP_TYPE __ptr_t
    503 #else /* __ARM_EABI__ */
    504 /* old ABI */
    505 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[20]
    506 #define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[19] = (val))
    507 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    508 #define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[19])
    509 #define _MD_SP_TYPE __ptr_t
    510 #endif /* __ARM_EABI__ */
    511 #else
    512 #error "ARM/Linux pre-glibc2 not supported yet"
    513 #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
    514 
    515 #elif defined(__sh__)
    516 /* SH/Linux */
    517 #if defined(__GLIBC__) && __GLIBC__ >= 2
    518 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[7]
    519 #define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[6] = (val))
    520 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    521 #define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[6])
    522 #define _MD_SP_TYPE __ptr_t
    523 #else
    524 #error "SH/Linux pre-glibc2 not supported yet"
    525 #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
    526 
    527 #elif defined(__m32r__)
    528 /* Linux/M32R */
    529 #if defined(__GLIBC__) && __GLIBC__ >= 2
    530 #define _MD_GET_SP(_t) (_t)->md.context[0].__jmpbuf[0].__regs[JB_SP]
    531 #define _MD_SET_FP(_t, val) ((_t)->md.context[0].__jmpbuf[0].__regs[JB_FP] = (val))
    532 #define _MD_GET_SP_PTR(_t) &(_MD_GET_SP(_t))
    533 #define _MD_GET_FP_PTR(_t) (&(_t)->md.context[0].__jmpbuf[0].__regs[JB_FP])
    534 #define _MD_SP_TYPE __ptr_t
    535 #else
    536 #error "Linux/M32R pre-glibc2 not supported yet"
    537 #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
    538 
    539 #else
    540 
    541 #error "Unknown CPU architecture"
    542 
    543 #endif /*__powerpc__*/
    544 
    545 /*
    546 ** Initialize a thread context to run "_main()" when started
    547 */
    548 #ifdef __powerpc__
    549 
    550 #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)  \
    551 {  \
    552    *status = PR_TRUE;  \
    553    if (sigsetjmp(CONTEXT(_thread), 1)) {  \
    554        _main();  \
    555    }  \
    556    _MD_GET_SP(_thread) = (unsigned char*) ((_sp) - 128); \
    557    _thread->md.sp = _MD_GET_SP_PTR(_thread); \
    558    _thread->md.fp = _MD_GET_FP_PTR(_thread); \
    559    _MD_SET_FP(_thread, 0); \
    560 }
    561 
    562 #elif defined(__mips__)
    563 
    564 #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)  \
    565 {  \
    566    *status = PR_TRUE;  \
    567    (void) sigsetjmp(CONTEXT(_thread), 1);  \
    568    _thread->md.context[0].__jmpbuf[0].__pc = (__ptr_t) _main;  \
    569    _MD_GET_SP(_thread) = (_MD_SP_TYPE) ((_sp) - 64); \
    570    _thread->md.sp = _MD_GET_SP_PTR(_thread); \
    571    _thread->md.fp = _MD_GET_FP_PTR(_thread); \
    572    _MD_SET_FP(_thread, 0); \
    573 }
    574 
    575 #else
    576 
    577 #define _MD_INIT_CONTEXT(_thread, _sp, _main, status)  \
    578 {  \
    579    *status = PR_TRUE;  \
    580    if (sigsetjmp(CONTEXT(_thread), 1)) {  \
    581        _main();  \
    582    }  \
    583    _MD_GET_SP(_thread) = (_MD_SP_TYPE) ((_sp) - 64); \
    584    _thread->md.sp = _MD_GET_SP_PTR(_thread); \
    585    _thread->md.fp = _MD_GET_FP_PTR(_thread); \
    586    _MD_SET_FP(_thread, 0); \
    587 }
    588 
    589 #endif /*__powerpc__*/
    590 
    591 #define _MD_SWITCH_CONTEXT(_thread)  \
    592    if (!sigsetjmp(CONTEXT(_thread), 1)) {  \
    593    (_thread)->md.errcode = errno;  \
    594    _PR_Schedule();  \
    595    }
    596 
    597 /*
    598 ** Restore a thread context, saved by _MD_SWITCH_CONTEXT
    599 */
    600 #define _MD_RESTORE_CONTEXT(_thread) \
    601 {   \
    602    errno = (_thread)->md.errcode;  \
    603    _MD_SET_CURRENT_THREAD(_thread);  \
    604    siglongjmp(CONTEXT(_thread), 1);  \
    605 }
    606 
    607 /* Machine-dependent (MD) data structures */
    608 
    609 struct _MDThread {
    610    PR_CONTEXT_TYPE context;
    611    void *sp;
    612    void *fp;
    613    int id;
    614    int errcode;
    615 };
    616 
    617 struct _MDThreadStack {
    618    PRInt8 notused;
    619 };
    620 
    621 struct _MDLock {
    622    PRInt8 notused;
    623 };
    624 
    625 struct _MDSemaphore {
    626    PRInt8 notused;
    627 };
    628 
    629 struct _MDCVar {
    630    PRInt8 notused;
    631 };
    632 
    633 struct _MDSegment {
    634    PRInt8 notused;
    635 };
    636 
    637 /*
    638 * md-specific cpu structure field
    639 */
    640 #include <sys/time.h>  /* for FD_SETSIZE */
    641 #define _PR_MD_MAX_OSFD FD_SETSIZE
    642 
    643 struct _MDCPU_Unix {
    644    PRCList ioQ;
    645    PRUint32 ioq_timeout;
    646    PRInt32 ioq_max_osfd;
    647    PRInt32 ioq_osfd_cnt;
    648 #ifndef _PR_USE_POLL
    649    fd_set fd_read_set, fd_write_set, fd_exception_set;
    650    PRInt16 fd_read_cnt[_PR_MD_MAX_OSFD],fd_write_cnt[_PR_MD_MAX_OSFD],
    651            fd_exception_cnt[_PR_MD_MAX_OSFD];
    652 #else
    653    struct pollfd *ioq_pollfds;
    654    int ioq_pollfds_size;
    655 #endif  /* _PR_USE_POLL */
    656 };
    657 
    658 #define _PR_IOQ(_cpu)           ((_cpu)->md.md_unix.ioQ)
    659 #define _PR_ADD_TO_IOQ(_pq, _cpu) PR_APPEND_LINK(&_pq.links, &_PR_IOQ(_cpu))
    660 #define _PR_FD_READ_SET(_cpu)       ((_cpu)->md.md_unix.fd_read_set)
    661 #define _PR_FD_READ_CNT(_cpu)       ((_cpu)->md.md_unix.fd_read_cnt)
    662 #define _PR_FD_WRITE_SET(_cpu)      ((_cpu)->md.md_unix.fd_write_set)
    663 #define _PR_FD_WRITE_CNT(_cpu)      ((_cpu)->md.md_unix.fd_write_cnt)
    664 #define _PR_FD_EXCEPTION_SET(_cpu)  ((_cpu)->md.md_unix.fd_exception_set)
    665 #define _PR_FD_EXCEPTION_CNT(_cpu)  ((_cpu)->md.md_unix.fd_exception_cnt)
    666 #define _PR_IOQ_TIMEOUT(_cpu)       ((_cpu)->md.md_unix.ioq_timeout)
    667 #define _PR_IOQ_MAX_OSFD(_cpu)      ((_cpu)->md.md_unix.ioq_max_osfd)
    668 #define _PR_IOQ_OSFD_CNT(_cpu)      ((_cpu)->md.md_unix.ioq_osfd_cnt)
    669 #define _PR_IOQ_POLLFDS(_cpu)       ((_cpu)->md.md_unix.ioq_pollfds)
    670 #define _PR_IOQ_POLLFDS_SIZE(_cpu)  ((_cpu)->md.md_unix.ioq_pollfds_size)
    671 
    672 #define _PR_IOQ_MIN_POLLFDS_SIZE(_cpu)  32
    673 
    674 struct _MDCPU {
    675    struct _MDCPU_Unix md_unix;
    676 };
    677 
    678 #define _MD_INIT_LOCKS()
    679 #define _MD_NEW_LOCK(lock) PR_SUCCESS
    680 #define _MD_FREE_LOCK(lock)
    681 #define _MD_LOCK(lock)
    682 #define _MD_UNLOCK(lock)
    683 #define _MD_INIT_IO()
    684 #define _MD_IOQ_LOCK()
    685 #define _MD_IOQ_UNLOCK()
    686 
    687 extern PRStatus _MD_InitializeThread(PRThread *thread);
    688 
    689 #define _MD_INIT_RUNNING_CPU(cpu)       _MD_unix_init_running_cpu(cpu)
    690 #define _MD_INIT_THREAD                 _MD_InitializeThread
    691 #define _MD_EXIT_THREAD(thread)
    692 #define _MD_SUSPEND_THREAD(thread)      _MD_suspend_thread
    693 #define _MD_RESUME_THREAD(thread)       _MD_resume_thread
    694 #define _MD_CLEAN_THREAD(_thread)
    695 
    696 extern PRStatus _MD_CREATE_THREAD(
    697    PRThread *thread,
    698    void (*start) (void *),
    699    PRThreadPriority priority,
    700    PRThreadScope scope,
    701    PRThreadState state,
    702    PRUint32 stackSize);
    703 extern void _MD_SET_PRIORITY(struct _MDThread *thread, PRUintn newPri);
    704 extern PRStatus _MD_WAIT(PRThread *, PRIntervalTime timeout);
    705 extern PRStatus _MD_WAKEUP_WAITER(PRThread *);
    706 extern void _MD_YIELD(void);
    707 
    708 #endif /* ! _PR_PTHREADS */
    709 
    710 extern void _MD_EarlyInit(void);
    711 
    712 #define _MD_EARLY_INIT                  _MD_EarlyInit
    713 #define _MD_FINAL_INIT                  _PR_UnixInit
    714 #define _PR_HAVE_CLOCK_MONOTONIC
    715 
    716 /*
    717 * We wrapped the select() call.  _MD_SELECT refers to the built-in,
    718 * unwrapped version.
    719 */
    720 #define _MD_SELECT __select
    721 
    722 #ifdef _PR_POLL_AVAILABLE
    723 #include <sys/poll.h>
    724 extern int __syscall_poll(struct pollfd *ufds, unsigned long int nfds,
    725                          int timeout);
    726 #define _MD_POLL __syscall_poll
    727 #endif
    728 
    729 /* For writev() */
    730 #include <sys/uio.h>
    731 
    732 extern void _MD_linux_map_sendfile_error(int err);
    733 
    734 #endif /* nspr_linux_defs_h___ */