--- SheepShaver/src/Unix/sysdeps.h 2003/09/29 15:46:07 1.7 +++ SheepShaver/src/Unix/sysdeps.h 2007/01/17 07:05:19 1.55 @@ -1,7 +1,7 @@ /* * sysdeps.h - System dependent definitions for Linux * - * SheepShaver (C) 1997-2002 Christian Bauer and Marc Hellwig + * SheepShaver (C) 1997-2005 Christian Bauer and Marc Hellwig * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -41,9 +41,14 @@ #include #include #include +#include #include #include +#ifdef HAVE_PTHREADS +# include +#endif + #ifdef HAVE_FCNTL_H # include #endif @@ -59,17 +64,37 @@ # endif #endif +// Fix offsetof() on FreeBSD and GCC >= 3.4 +#if defined(__FreeBSD__) && defined(__cplusplus) +#undef offsetof +/* The cast to "char &" below avoids problems with user-defined + "operator &", which can appear in a POD type. */ +#define offsetof(TYPE, MEMBER) \ + (__offsetof__ (reinterpret_cast \ + (&reinterpret_cast \ + (static_cast (0)->MEMBER)))) +#endif + // Define for external components #define SHEEPSHAVER 1 -// Mac and host address space are the same +// Always use Real Addressing mode on native architectures +// Otherwise, use Direct Addressing mode if NATMEM_OFFSET is set +#if !defined(EMULATED_PPC) +#define REAL_ADDRESSING 1 +#include "ppc_asm.tmpl" +#elif defined(NATMEM_OFFSET) +#define DIRECT_ADDRESSING 1 +#else #define REAL_ADDRESSING 1 +#endif + +// Always use the complete non-stubs Ethernet driver +#define USE_ETHER_FULL_DRIVER 1 #define POWERPC_ROM 1 #if EMULATED_PPC -// Handle interrupts asynchronously? -#define ASYNC_IRQ 0 // Mac ROM is write protected when banked memory is used #if REAL_ADDRESSING || DIRECT_ADDRESSING # define ROM_IS_WRITE_PROTECTED 0 @@ -77,6 +102,20 @@ #else # define ROM_IS_WRITE_PROTECTED 1 #endif +// Configure PowerPC emulator +#define PPC_REENTRANT_JIT 1 +#define PPC_CHECK_INTERRUPTS 1 +#define PPC_DECODE_CACHE 1 +#define PPC_FLIGHT_RECORDER 1 +#define PPC_PROFILE_COMPILE_TIME 0 +#define PPC_PROFILE_GENERIC_CALLS 1 +#define KPX_MAX_CPUS 1 +#if ENABLE_DYNGEN +#define PPC_ENABLE_JIT 1 +#endif +#if defined(__i386__) || defined(__x86_64__) +#define DYNGEN_ASM_OPTS 1 +#endif #else // Mac ROM is write protected #define ROM_IS_WRITE_PROTECTED 1 @@ -127,11 +166,37 @@ typedef int64 intptr; #error "Unsupported size of pointer" #endif -// Helper functions to byteswap data +// Define if the host processor supports fast unaligned load/stores +#if defined __i386__ || defined __x86_64__ +#define UNALIGNED_PROFITABLE 1 +#endif + + +/** + * Helper functions to byteswap data + **/ + +#if defined(__GNUC__) +#if defined(__x86_64__) || defined(__i386__) +// Linux/AMD64 currently has no asm optimized bswap_32() in +#define opt_bswap_32 do_opt_bswap_32 +static inline uint32 do_opt_bswap_32(uint32 x) +{ + uint32 v; + __asm__ __volatile__ ("bswap %0" : "=r" (v) : "0" (x)); + return v; +} +#endif +#endif + #ifdef HAVE_BYTESWAP_H #include #endif +#ifdef opt_bswap_16 +#undef bswap_16 +#define bswap_16 opt_bswap_16 +#endif #ifndef bswap_16 #define bswap_16 generic_bswap_16 #endif @@ -141,6 +206,10 @@ static inline uint16 generic_bswap_16(ui return ((x & 0xff) << 8) | ((x >> 8) & 0xff); } +#ifdef opt_bswap_32 +#undef bswap_32 +#define bswap_32 opt_bswap_32 +#endif #ifndef bswap_32 #define bswap_32 generic_bswap_32 #endif @@ -153,6 +222,18 @@ static inline uint32 generic_bswap_32(ui ((x & 0x000000ff) << 24) ); } +#if defined(__i386__) +#define opt_bswap_64 do_opt_bswap_64 +static inline uint64 do_opt_bswap_64(uint64 x) +{ + return (bswap_32(x >> 32) | (((uint64)bswap_32((uint32)x)) << 32)); +} +#endif + +#ifdef opt_bswap_64 +#undef bswap_64 +#define bswap_64 opt_bswap_64 +#endif #ifndef bswap_64 #define bswap_64 generic_bswap_64 #endif @@ -182,16 +263,16 @@ static inline uint64 tswap64(uint64 x) { // spin locks #ifdef __GNUC__ -#ifdef __powerpc__ +#if defined(__powerpc__) || defined(__ppc__) #define HAVE_TEST_AND_SET 1 -static inline int testandset(int *p) +static inline int testandset(volatile int *p) { int ret; - __asm__ __volatile__("0: lwarx %0,0,%1 ;" - " xor. %0,%3,%0;" - " bne 1f;" - " stwcx. %2,0,%1;" - " bne- 0b;" + __asm__ __volatile__("0: lwarx %0,0,%1\n" + " xor. %0,%3,%0\n" + " bne 1f\n" + " stwcx. %2,0,%1\n" + " bne- 0b\n" "1: " : "=&r" (ret) : "r" (p), "r" (1), "r" (0) @@ -200,16 +281,15 @@ static inline int testandset(int *p) } #endif -#ifdef __i386__ +#if defined(__i386__) || defined(__x86_64__) #define HAVE_TEST_AND_SET 1 -static inline int testandset(int *p) +static inline int testandset(volatile int *p) { - char ret; - long int readval; - - __asm__ __volatile__("lock; cmpxchgl %3, %1; sete %0" - : "=q" (ret), "=m" (*p), "=a" (readval) - : "r" (1), "m" (*p), "a" (0) + long int ret; + /* Note: the "xchg" instruction does not need a "lock" prefix */ + __asm__ __volatile__("xchgl %k0, %1" + : "=r" (ret), "=m" (*p) + : "0" (1), "m" (*p) : "memory"); return ret; } @@ -217,7 +297,7 @@ static inline int testandset(int *p) #ifdef __s390__ #define HAVE_TEST_AND_SET 1 -static inline int testandset(int *p) +static inline int testandset(volatile int *p) { int ret; @@ -232,7 +312,7 @@ static inline int testandset(int *p) #ifdef __alpha__ #define HAVE_TEST_AND_SET 1 -static inline int testandset(int *p) +static inline int testandset(volatile int *p) { int ret; unsigned long one; @@ -252,7 +332,7 @@ static inline int testandset(int *p) #ifdef __sparc__ #define HAVE_TEST_AND_SET 1 -static inline int testandset(int *p) +static inline int testandset(volatile int *p) { int ret; @@ -267,7 +347,7 @@ static inline int testandset(int *p) #ifdef __arm__ #define HAVE_TEST_AND_SET 1 -static inline int testandset(int *p) +static inline int testandset(volatile int *p) { register unsigned int ret; __asm__ __volatile__("swp %0, %1, [%2]" @@ -280,12 +360,13 @@ static inline int testandset(int *p) #endif /* __GNUC__ */ -#if HAVE_TEST_AND_SET -#define HAVE_SPINLOCKS 1 -typedef int spinlock_t; +typedef volatile int spinlock_t; -const spinlock_t SPIN_LOCK_UNLOCKED = 0; +static const spinlock_t SPIN_LOCK_UNLOCKED = 0; +#if defined(HAVE_TEST_AND_SET) && defined(HAVE_PTHREADS) +// There is nothing to lock if we are not in an multithreaded environment +#define HAVE_SPINLOCKS 1 static inline void spin_lock(spinlock_t *lock) { while (testandset(lock)); @@ -300,6 +381,19 @@ static inline int spin_trylock(spinlock_ { return !testandset(lock); } +#else +static inline void spin_lock(spinlock_t *lock) +{ +} + +static inline void spin_unlock(spinlock_t *lock) +{ +} + +static inline int spin_trylock(spinlock_t *lock) +{ + return 1; +} #endif // Time data type for Time Manager emulation @@ -309,8 +403,28 @@ typedef struct timespec tm_time_t; typedef struct timeval tm_time_t; #endif +/* Define codes for all the float formats that we know of. + * Though we only handle IEEE format. */ +#define UNKNOWN_FLOAT_FORMAT 0 +#define IEEE_FLOAT_FORMAT 1 +#define VAX_FLOAT_FORMAT 2 +#define IBM_FLOAT_FORMAT 3 +#define C4X_FLOAT_FORMAT 4 + +// High-precision timing +#if defined(HAVE_PTHREADS) && defined(HAVE_CLOCK_NANOSLEEP) +#define PRECISE_TIMING 1 +#define PRECISE_TIMING_POSIX 1 +#endif + +// Timing functions +extern uint64 GetTicks_usec(void); +extern void Delay_usec(uint32 usec); + +#ifdef HAVE_PTHREADS // Setup pthread attributes extern void Set_pthread_attr(pthread_attr_t *attr, int priority); +#endif // Various definitions typedef struct rgb_color { @@ -320,15 +434,34 @@ typedef struct rgb_color { uint8 alpha; } rgb_color; +// X11 display fast locks +#if defined(HAVE_PTHREADS) +#define X11_LOCK_TYPE pthread_mutex_t +#define X11_LOCK_INIT PTHREAD_MUTEX_INITIALIZER +#define XDisplayLock() pthread_mutex_lock(&x_display_lock); +#define XDisplayUnlock() pthread_mutex_unlock(&x_display_lock); +#elif defined(HAVE_SPINLOCKS) +#define X11_LOCK_TYPE spinlock_t +#define X11_LOCK_INIT SPIN_LOCK_UNLOCKED +#define XDisplayLock() spin_lock(&x_display_lock) +#define XDisplayUnlock() spin_unlock(&x_display_lock) +#else +#define XDisplayLock() +#define XDisplayUnlock() +#endif +#ifdef X11_LOCK_TYPE +extern X11_LOCK_TYPE x_display_lock; +#endif + // Macro for calling MacOS routines -#define CallMacOS(type, tvect) call_macos((uint32)tvect) -#define CallMacOS1(type, tvect, arg1) call_macos1((uint32)tvect, (uint32)arg1) -#define CallMacOS2(type, tvect, arg1, arg2) call_macos2((uint32)tvect, (uint32)arg1, (uint32)arg2) -#define CallMacOS3(type, tvect, arg1, arg2, arg3) call_macos3((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3) -#define CallMacOS4(type, tvect, arg1, arg2, arg3, arg4) call_macos4((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4) -#define CallMacOS5(type, tvect, arg1, arg2, arg3, arg4, arg5) call_macos5((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4, (uint32)arg5) -#define CallMacOS6(type, tvect, arg1, arg2, arg3, arg4, arg5, arg6) call_macos6((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4, (uint32)arg5, (uint32)arg6) -#define CallMacOS7(type, tvect, arg1, arg2, arg3, arg4, arg5, arg6, arg7) call_macos7((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4, (uint32)arg5, (uint32)arg6, (uint32)arg7) +#define CallMacOS(type, tvect) call_macos((uintptr)tvect) +#define CallMacOS1(type, tvect, arg1) call_macos1((uintptr)tvect, (uintptr)arg1) +#define CallMacOS2(type, tvect, arg1, arg2) call_macos2((uintptr)tvect, (uintptr)arg1, (uintptr)arg2) +#define CallMacOS3(type, tvect, arg1, arg2, arg3) call_macos3((uintptr)tvect, (uintptr)arg1, (uintptr)arg2, (uintptr)arg3) +#define CallMacOS4(type, tvect, arg1, arg2, arg3, arg4) call_macos4((uintptr)tvect, (uintptr)arg1, (uintptr)arg2, (uintptr)arg3, (uintptr)arg4) +#define CallMacOS5(type, tvect, arg1, arg2, arg3, arg4, arg5) call_macos5((uintptr)tvect, (uintptr)arg1, (uintptr)arg2, (uintptr)arg3, (uintptr)arg4, (uintptr)arg5) +#define CallMacOS6(type, tvect, arg1, arg2, arg3, arg4, arg5, arg6) call_macos6((uintptr)tvect, (uintptr)arg1, (uintptr)arg2, (uintptr)arg3, (uintptr)arg4, (uintptr)arg5, (uintptr)arg6) +#define CallMacOS7(type, tvect, arg1, arg2, arg3, arg4, arg5, arg6, arg7) call_macos7((uintptr)tvect, (uintptr)arg1, (uintptr)arg2, (uintptr)arg3, (uintptr)arg4, (uintptr)arg5, (uintptr)arg6, (uintptr)arg7) #ifdef __cplusplus extern "C" {