1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
/* 
 * The contents of this file are subject to the Mozilla Public
 * License Version 1.1 (the "License"); you may not use this file
 * except in compliance with the License. You may obtain a copy of
 * the License at http://www.mozilla.org/MPL/
 * 
 * Software distributed under the License is distributed on an "AS
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 * implied. See the License for the specific language governing
 * rights and limitations under the License.
 * 
 * The Original Code is the Netscape Portable Runtime library.
 * 
 * The Initial Developer of the Original Code is Netscape
 * Communications Corporation.  Portions created by Netscape are 
 * Copyright (C) 1994-2000 Netscape Communications Corporation.  All
 * Rights Reserved.
 * 
 * Contributor(s):  Silicon Graphics, Inc.
 * 
 * Portions created by SGI are Copyright (C) 2000-2001 Silicon
 * Graphics, Inc.  All Rights Reserved.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL"), in which case the provisions of the GPL are applicable 
 * instead of those above.  If you wish to allow use of your 
 * version of this file only under the terms of the GPL and not to
 * allow others to use your version of this file under the MPL,
 * indicate your decision by deleting the provisions above and
 * replace them with the notice and other provisions required by
 * the GPL.  If you do not delete the provisions above, a recipient
 * may use your version of this file under either the MPL or the
 * GPL.
 */

/*
 * This file is derived directly from Netscape Communications Corporation,
 * and consists of extensive modifications made during the year(s) 1999-2000.
 */

#ifndef __ST_MD_H__
#define __ST_MD_H__

#if defined(ETIMEDOUT) && !defined(ETIME)
    #define ETIME ETIMEDOUT
#endif

#if defined(MAP_ANONYMOUS) && !defined(MAP_ANON)
    #define MAP_ANON MAP_ANONYMOUS
#endif

#ifndef MAP_FAILED
    #define MAP_FAILED -1
#endif

/*****************************************
 * Platform specifics
 */
#if defined (AIX)
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_SYSV_ANON_MMAP
    #define MD_ACCEPT_NB_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    
    #ifndef MD_HAVE_SOCKLEN_T
        #define MD_HAVE_SOCKLEN_T
        #define socklen_t unsigned long
    #endif
    
    #define MD_SETJMP(env) _setjmp(env)
    #define MD_LONGJMP(env, val) _longjmp(env, val)
    
    #define MD_INIT_CONTEXT(_thread, _sp, _main) \
        ST_BEGIN_MACRO                             \
        if (MD_SETJMP((_thread)->context))         \
        _main();                                 \
        (_thread)->context[3] = (long) (_sp);      \
        ST_END_MACRO
    
    #define MD_GET_UTIME()                        \
        timebasestruct_t rt;                        \
        (void) read_real_time(&rt, TIMEBASE_SZ);    \
        (void) time_base_to_time(&rt, TIMEBASE_SZ); \
        return (rt.tb_high * 1000000LL + rt.tb_low / 1000)

#elif defined (CYGWIN)
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_BSD_ANON_MMAP
    #define MD_ACCEPT_NB_NOT_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    
    #define MD_SETJMP(env) setjmp(env)
    #define MD_LONGJMP(env, val) longjmp(env, val)
    
    #define MD_JB_SP  7
    
    #define MD_GET_SP(_t) (_t)->context[MD_JB_SP]

    #define MD_INIT_CONTEXT(_thread, _sp, _main) \
        ST_BEGIN_MACRO                             \
        if (MD_SETJMP((_thread)->context))         \
        _main();                                 \
        MD_GET_SP(_thread) = (long) (_sp);         \
        ST_END_MACRO

    #define MD_GET_UTIME()            \
        struct timeval tv;              \
        (void) gettimeofday(&tv, NULL); \
        return (tv.tv_sec * 1000000LL + tv.tv_usec)

#elif defined (DARWIN)
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_BSD_ANON_MMAP
    #define MD_ACCEPT_NB_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    #define MD_HAVE_SOCKLEN_T
    
    #define MD_SETJMP(env) _setjmp(env)
    #define MD_LONGJMP(env, val) _longjmp(env, val)
    
    #if defined(__ppc__)
        #define MD_JB_SP  0
    #elif defined(__i386__)
        #define MD_JB_SP  9
    #elif defined(__x86_64__)
        #define MD_JB_SP  4
    #else
        #error Unknown CPU architecture
    #endif

    #define MD_INIT_CONTEXT(_thread, _sp, _main)   \
        ST_BEGIN_MACRO                               \
        if (MD_SETJMP((_thread)->context))           \
        _main();                                   \
        *((long *)&((_thread)->context[MD_JB_SP])) = (long) (_sp); \
        ST_END_MACRO

    #define MD_GET_UTIME()            \
        struct timeval tv;              \
        (void) gettimeofday(&tv, NULL); \
        return (tv.tv_sec * 1000000LL + tv.tv_usec)

#elif defined (FREEBSD)
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_BSD_ANON_MMAP
    #define MD_ACCEPT_NB_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    
    #define MD_SETJMP(env) _setjmp(env)
    #define MD_LONGJMP(env, val) _longjmp(env, val)

    #if defined(__i386__)
        #define MD_JB_SP   2
    #elif defined(__alpha__)
        #define MD_JB_SP  34
    #elif defined(__amd64__)
        #define MD_JB_SP   2
    #else
        #error Unknown CPU architecture
    #endif

    #define MD_INIT_CONTEXT(_thread, _sp, _main)          \
        ST_BEGIN_MACRO                                      \
        if (MD_SETJMP((_thread)->context))                  \
        _main();                                          \
        (_thread)->context[0]._jb[MD_JB_SP] = (long) (_sp); \
        ST_END_MACRO

    #define MD_GET_UTIME()            \
        struct timeval tv;              \
        (void) gettimeofday(&tv, NULL); \
        return (tv.tv_sec * 1000000LL + tv.tv_usec)

#elif defined (HPUX)
    #define MD_STACK_GROWS_UP
    #define MD_USE_BSD_ANON_MMAP
    #define MD_ACCEPT_NB_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    
    #define MD_SETJMP(env) _setjmp(env)
    #define MD_LONGJMP(env, val) _longjmp(env, val)
    
    #ifndef __LP64__
        /* 32-bit mode (ILP32 data model) */
        #define MD_INIT_CONTEXT(_thread, _sp, _main)        \
            ST_BEGIN_MACRO                                    \
            if (MD_SETJMP((_thread)->context))                \
            _main();                                        \
            ((long *)((_thread)->context))[1] = (long) (_sp); \
            ST_END_MACRO
    #else
        /* 64-bit mode (LP64 data model) */
        #define MD_STACK_PAD_SIZE 256
        /* Last stack frame must be preserved */
        #define MD_INIT_CONTEXT(_thread, _sp, _main)                     \
            ST_BEGIN_MACRO                                                 \
            if (MD_SETJMP((_thread)->context))                             \
            _main();                                                     \
            memcpy((char *)(_sp) - MD_STACK_PAD_SIZE,                      \
                 ((char **)((_thread)->context))[1] - MD_STACK_PAD_SIZE, \
                 MD_STACK_PAD_SIZE);                                     \
            ((long *)((_thread)->context))[1] = (long) (_sp);              \
            ST_END_MACRO
    #endif /* !__LP64__ */

    #define MD_GET_UTIME()            \
        struct timeval tv;              \
        (void) gettimeofday(&tv, NULL); \
        return (tv.tv_sec * 1000000LL + tv.tv_usec)

#elif defined (IRIX)
    #include <sys/syssgi.h>
    
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_SYSV_ANON_MMAP
    #define MD_ACCEPT_NB_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    
    #define MD_SETJMP(env) setjmp(env)
    #define MD_LONGJMP(env, val) longjmp(env, val)

    #define MD_INIT_CONTEXT(_thread, _sp, _main) \
        ST_BEGIN_MACRO                             \
        (void) MD_SETJMP((_thread)->context);      \
        (_thread)->context[JB_SP] = (long) (_sp);  \
        (_thread)->context[JB_PC] = (long) _main;  \
        ST_END_MACRO

    #define MD_GET_UTIME()                         \
        static int inited = 0;                       \
        static clockid_t clock_id = CLOCK_SGI_CYCLE; \
        struct timespec ts;                          \
        if (!inited) {                               \
        if (syssgi(SGI_CYCLECNTR_SIZE) < 64)       \
          clock_id = CLOCK_REALTIME;               \
        inited = 1;                                \
        }                                            \
        (void) clock_gettime(clock_id, &ts);         \
        return (ts.tv_sec * 1000000LL + ts.tv_nsec / 1000)
    
    /*
     * Cap the stack by zeroing out the saved return address register
     * value. This allows libexc, used by SpeedShop, to know when to stop
     * backtracing since it won't find main, start, or any other known
     * stack root function in a state thread's stack. Without this libexc
     * traces right off the stack and crashes.
     * The function preamble stores ra at 8(sp), this stores zero there.
     * N.B. This macro is compiler/ABI dependent. It must change if ANY more
     * automatic variables are added to the _st_thread_main() routine, because
     * the address where ra is stored will change.
     */
    #if !defined(__GNUC__) && defined(_MIPS_SIM) && _MIPS_SIM != _ABIO32
        #define MD_CAP_STACK(var_addr) \
            (((volatile __uint64_t *)(var_addr))[1] = 0)
    #endif

#elif defined (LINUX)
    /*
     * These are properties of the linux kernel and are the same on every
     * flavor and architecture.
     */
    #define MD_USE_BSD_ANON_MMAP
    #define MD_ACCEPT_NB_NOT_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    /*
     * Modern GNU/Linux is Posix.1g compliant.
     */
    #define MD_HAVE_SOCKLEN_T
    
    /*
     * All architectures and flavors of linux have the gettimeofday
     * function but if you know of a faster way, use it.
     */
    #define MD_GET_UTIME()            \
        struct timeval tv;              \
        (void) gettimeofday(&tv, NULL); \
        return (tv.tv_sec * 1000000LL + tv.tv_usec)

    #if defined(__ia64__)
        #define MD_STACK_GROWS_DOWN
        
        /*
         * IA-64 architecture.  Besides traditional memory call stack, IA-64
         * uses general register stack.  Thus each thread needs a backing store
         * for register stack in addition to memory stack.  Standard
         * setjmp()/longjmp() cannot be used for thread context switching
         * because their implementation implicitly assumes that only one
         * register stack exists.
         */
        #ifdef USE_LIBC_SETJMP
            #undef USE_LIBC_SETJMP
        #endif
        #define MD_USE_BUILTIN_SETJMP
    
        #define MD_STACK_PAD_SIZE 128
        /* Last register stack frame must be preserved */
        #define MD_INIT_CONTEXT(_thread, _sp, _bsp, _main)                       \
            ST_BEGIN_MACRO                                                         \
            if (MD_SETJMP((_thread)->context))                                     \
            _main();                                                             \
            memcpy((char *)(_bsp) - MD_STACK_PAD_SIZE,                             \
                 (char *)(_thread)->context[0].__jmpbuf[17] - MD_STACK_PAD_SIZE, \
                 MD_STACK_PAD_SIZE);                                             \
            (_thread)->context[0].__jmpbuf[0]  = (long) (_sp);                     \
            (_thread)->context[0].__jmpbuf[17] = (long) (_bsp);                    \
            ST_END_MACRO
    #elif defined(__mips__)
        #define MD_STACK_GROWS_DOWN
        
        #define MD_INIT_CONTEXT(_thread, _sp, _main)               \
            ST_BEGIN_MACRO                                           \
            MD_SETJMP((_thread)->context);                           \
            _thread->context[0].__jmpbuf[0].__pc = (__ptr_t) _main;  \
            _thread->context[0].__jmpbuf[0].__sp = _sp;              \
            ST_END_MACRO
    #else /* Not IA-64 or mips */
        /*
         * On linux, there are a few styles of jmpbuf format.  These vary based
         * on architecture/glibc combination.
         *
         * Most of the glibc based toggles were lifted from:
         * mozilla/nsprpub/pr/include/md/_linux.h
         */
        /*
         * Starting with glibc 2.4, JB_SP definitions are not public anymore.
         * They, however, can still be found in glibc source tree in
         * architecture-specific "jmpbuf-offsets.h" files.
         * Most importantly, the content of jmp_buf is mangled by setjmp to make
         * it completely opaque (the mangling can be disabled by setting the
         * LD_POINTER_GUARD environment variable before application execution).
         * Therefore we will use built-in _st_md_cxt_save/_st_md_cxt_restore
         * functions as a setjmp/longjmp replacement wherever they are available
         * unless USE_LIBC_SETJMP is defined.
         */
        #if defined(__powerpc__)
            #define MD_STACK_GROWS_DOWN
        
            #if (__GLIBC__ > 2) || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 1)
                #ifndef JB_GPR1
                    #define JB_GPR1 0
                #endif
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_GPR1]   
            #else
                /* not an error but certainly cause for caution */
                #error "Untested use of old glibc on powerpc"
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__misc[0]
            #endif /* glibc 2.1 or later */
        #elif defined(__alpha)
            #define MD_STACK_GROWS_DOWN
        
            #if defined(__GLIBC__) && __GLIBC__ >= 2
                #ifndef JB_SP
                    #define JB_SP 8
                #endif
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_SP]
            #else
                /* not an error but certainly cause for caution */
                #error "Untested use of old glibc on alpha"
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__sp
            #endif
        #elif defined(__mc68000__)
            #define MD_STACK_GROWS_DOWN
            
            /* m68k still uses old style sigjmp_buf */
            #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__sp
        #elif defined(__sparc__)
            #define MD_STACK_GROWS_DOWN
        
            #if defined(__GLIBC__) && __GLIBC__ >= 2
                #ifndef JB_SP
                    #define JB_SP 0
                #endif
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_SP]
            #else
                /* not an error but certainly cause for caution */
                #error "Untested use of old glic on sparc -- also using odd mozilla derived __fp"
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__fp
            #endif
        #elif defined(__i386__)
            #define MD_STACK_GROWS_DOWN
            #define MD_USE_BUILTIN_SETJMP
            
            #if defined(__GLIBC__) && __GLIBC__ >= 2
                #ifndef JB_SP
                    #define JB_SP 4
                #endif
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_SP]
            #else
                /* not an error but certainly cause for caution */
                #error "Untested use of old glibc on i386"
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__sp
            #endif
        #elif defined(__amd64__) || defined(__x86_64__)
            #define MD_STACK_GROWS_DOWN
            #define MD_USE_BUILTIN_SETJMP
            
            #ifndef JB_RSP
                #define JB_RSP 6
            #endif
            #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[JB_RSP]
        #elif defined(__arm__)
            #define MD_STACK_GROWS_DOWN
            
            #if defined(__GLIBC__) && __GLIBC__ >= 2
                #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[8]
            #else
                #error "ARM/Linux pre-glibc2 not supported yet"
            #endif /* defined(__GLIBC__) && __GLIBC__ >= 2 */
        #elif defined(__s390__)
            #define MD_STACK_GROWS_DOWN
            
            /* There is no JB_SP in glibc at this time. (glibc 2.2.5)
             */
            #define MD_GET_SP(_t) (_t)->context[0].__jmpbuf[0].__gregs[9]
        #elif defined(__hppa__)
            #define MD_STACK_GROWS_UP
            
            /* yes, this is gross, unfortunately at the moment (2002/08/01) there is
             * a bug in hppa's glibc header definition for JB_SP, so we can't
             * use that...
             */
            #define MD_GET_SP(_t) (*(long *)(((char *)&(_t)->context[0].__jmpbuf[0]) + 76))
        #else
            #error "Unknown CPU architecture"
        #endif /* Cases with common MD_INIT_CONTEXT and different SP locations */
        
        #define MD_INIT_CONTEXT(_thread, _sp, _main) \
            ST_BEGIN_MACRO                             \
            if (MD_SETJMP((_thread)->context))         \
            _main();                                 \
            MD_GET_SP(_thread) = (long) (_sp);         \
            ST_END_MACRO
    #endif /* Cases with different MD_INIT_CONTEXT */
    
    #if defined(MD_USE_BUILTIN_SETJMP) && !defined(USE_LIBC_SETJMP)
        #define MD_SETJMP(env) _st_md_cxt_save(env)
        #define MD_LONGJMP(env, val) _st_md_cxt_restore(env, val)
        
        extern int _st_md_cxt_save(jmp_buf env);
        extern void _st_md_cxt_restore(jmp_buf env, int val);
    #else
        #define MD_SETJMP(env) setjmp(env)
        #define MD_LONGJMP(env, val) longjmp(env, val)
    #endif

#elif defined (NETBSD)
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_BSD_ANON_MMAP
    #define MD_ACCEPT_NB_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    #define MD_HAVE_SOCKLEN_T
    
    #define MD_SETJMP(env) _setjmp(env)
    #define MD_LONGJMP(env, val) _longjmp(env, val)
    
    #if defined(__i386__)
        #define MD_JB_SP   2
    #elif defined(__alpha__)
        #define MD_JB_SP  34
    #elif defined(__sparc__)
        #define MD_JB_SP   0
    #elif defined(__vax__)
        #define MD_JB_SP   2
    #else
        #error Unknown CPU architecture
    #endif
    
    #define MD_INIT_CONTEXT(_thread, _sp, _main)   \
        ST_BEGIN_MACRO                               \
        if (MD_SETJMP((_thread)->context))           \
        _main();                                   \
        (_thread)->context[MD_JB_SP] = (long) (_sp); \
        ST_END_MACRO
    
    #define MD_GET_UTIME()            \
        struct timeval tv;              \
        (void) gettimeofday(&tv, NULL); \
        return (tv.tv_sec * 1000000LL + tv.tv_usec)

#elif defined (OPENBSD)
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_BSD_ANON_MMAP
    #define MD_ACCEPT_NB_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    
    #define MD_SETJMP(env) _setjmp(env)
    #define MD_LONGJMP(env, val) _longjmp(env, val)
    
    #if defined(__i386__)
        #define MD_JB_SP   2
    #elif defined(__alpha__)
        #define MD_JB_SP  34
    #elif defined(__sparc__)
        #define MD_JB_SP   0
    #elif defined(__amd64__)
        #define MD_JB_SP   6
    #else
        #error Unknown CPU architecture
    #endif
    
    #define MD_INIT_CONTEXT(_thread, _sp, _main)   \
        ST_BEGIN_MACRO                               \
        if (MD_SETJMP((_thread)->context))           \
        _main();                                   \
        (_thread)->context[MD_JB_SP] = (long) (_sp); \
        ST_END_MACRO
    
    #define MD_GET_UTIME()            \
        struct timeval tv;              \
        (void) gettimeofday(&tv, NULL); \
        return (tv.tv_sec * 1000000LL + tv.tv_usec)

#elif defined (OSF1)
    #include <signal.h>
    
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_SYSV_ANON_MMAP
    #define MD_ACCEPT_NB_NOT_INHERITED
    #define MD_ALWAYS_UNSERIALIZED_ACCEPT
    
    #define MD_SETJMP(env) _setjmp(env)
    #define MD_LONGJMP(env, val) _longjmp(env, val)
    
    #define MD_INIT_CONTEXT(_thread, _sp, _main)                         \
        ST_BEGIN_MACRO                                                     \
        if (MD_SETJMP((_thread)->context))                                 \
        _main();                                                         \
        ((struct sigcontext *)((_thread)->context))->sc_sp = (long) (_sp); \
        ST_END_MACRO
    
    #define MD_GET_UTIME()            \
        struct timeval tv;              \
        (void) gettimeofday(&tv, NULL); \
        return (tv.tv_sec * 1000000LL + tv.tv_usec)

#elif defined (SOLARIS)
    #include <sys/filio.h>
    extern int getpagesize(void);
    
    #define MD_STACK_GROWS_DOWN
    #define MD_USE_SYSV_ANON_MMAP
    #define MD_ACCEPT_NB_NOT_INHERITED
    
    #define MD_SETJMP(env) setjmp(env)
    #define MD_LONGJMP(env, val) longjmp(env, val)
    
    #if defined(sparc) || defined(__sparc)
        #ifdef _LP64
            #define MD_STACK_PAD_SIZE 4095
        #endif
        #define MD_INIT_CONTEXT(_thread, _sp, _main) \
            ST_BEGIN_MACRO                             \
            (void) MD_SETJMP((_thread)->context);      \
            (_thread)->context[1] = (long) (_sp);      \
            (_thread)->context[2] = (long) _main;      \
            ST_END_MACRO
    #elif defined(i386) || defined(__i386)
        #define MD_INIT_CONTEXT(_thread, _sp, _main) \
            ST_BEGIN_MACRO                             \
            (void) MD_SETJMP((_thread)->context);      \
            (_thread)->context[4] = (long) (_sp);      \
            (_thread)->context[5] = (long) _main;      \
            ST_END_MACRO
    #elif defined(__amd64__)
        #define MD_INIT_CONTEXT(_thread, _sp, _main)   \
            ST_BEGIN_MACRO                               \
            if (MD_SETJMP((_thread)->context))           \
            _main();                                   \
            (_thread)->context[6] = (long) (_sp); \
            ST_END_MACRO
    #else
        #error Unknown CPU architecture
    #endif
    
    #define MD_GET_UTIME() \
        return (gethrtime() / 1000)

#else
    #error Unknown OS
#endif /* OS */

/*****************************************
 * Other defines
 */
#if !defined(MD_HAVE_POLL) && !defined(MD_DONT_HAVE_POLL)
    #define MD_HAVE_POLL
#endif

#ifndef MD_STACK_PAD_SIZE
    #define MD_STACK_PAD_SIZE 128
#endif

#if !defined(MD_HAVE_SOCKLEN_T) && !defined(socklen_t)
    #define socklen_t int
#endif

#ifndef MD_CAP_STACK
    #define MD_CAP_STACK(var_addr)
#endif

#endif /* !__ST_MD_H__ */