ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/sysdeps.h
Revision: 1.23
Committed: 2004-01-26T13:52:31Z (20 years, 5 months ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.22: +9 -1 lines
Log Message:
Use bswap instruction on IA-32 too. Optimize bswap_64 on little-endian
(x86 for now) systems.

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * sysdeps.h - System dependent definitions for Linux
3     *
4 cebix 1.21 * SheepShaver (C) 1997-2004 Christian Bauer and Marc Hellwig
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     #ifndef SYSDEPS_H
22     #define SYSDEPS_H
23    
24     #ifndef __STDC__
25     #error "Your compiler is not ANSI. Get a real one."
26     #endif
27    
28     #include "config.h"
29     #include "user_strings_unix.h"
30    
31     #ifndef STDC_HEADERS
32     #error "You don't have ANSI C header files."
33     #endif
34    
35     #ifdef HAVE_UNISTD_H
36     # include <sys/types.h>
37     # include <unistd.h>
38     #endif
39    
40     #include <netinet/in.h>
41     #include <assert.h>
42     #include <stdio.h>
43     #include <stdlib.h>
44     #include <string.h>
45     #include <signal.h>
46    
47     #ifdef HAVE_FCNTL_H
48     # include <fcntl.h>
49     #endif
50    
51     #ifdef TIME_WITH_SYS_TIME
52     # include <sys/time.h>
53     # include <time.h>
54     #else
55     # ifdef HAVE_SYS_TIME_H
56     # include <sys/time.h>
57     # else
58     # include <time.h>
59     # endif
60     #endif
61    
62 gbeauche 1.5 // Define for external components
63     #define SHEEPSHAVER 1
64    
65 gbeauche 1.4 // Mac and host address space are the same
66     #define REAL_ADDRESSING 1
67    
68 gbeauche 1.5 #define POWERPC_ROM 1
69    
70     #if EMULATED_PPC
71 gbeauche 1.7 // Handle interrupts asynchronously?
72     #define ASYNC_IRQ 0
73 gbeauche 1.5 // Mac ROM is write protected when banked memory is used
74     #if REAL_ADDRESSING || DIRECT_ADDRESSING
75     # define ROM_IS_WRITE_PROTECTED 0
76     # define USE_SCRATCHMEM_SUBTERFUGE 1
77 cebix 1.1 #else
78 gbeauche 1.5 # define ROM_IS_WRITE_PROTECTED 1
79     #endif
80 gbeauche 1.9 // Configure PowerPC emulator
81 gbeauche 1.13 #define PPC_CHECK_INTERRUPTS (ASYNC_IRQ ? 0 : 1)
82 gbeauche 1.14 #define PPC_DECODE_CACHE 1
83 gbeauche 1.10 #define PPC_FLIGHT_RECORDER 1
84 gbeauche 1.17 #define PPC_PROFILE_COMPILE_TIME 0
85     #define PPC_PROFILE_GENERIC_CALLS 0
86 gbeauche 1.16 #define KPX_MAX_CPUS 1
87 gbeauche 1.5 #else
88     // Mac ROM is write protected
89     #define ROM_IS_WRITE_PROTECTED 1
90     #define USE_SCRATCHMEM_SUBTERFUGE 0
91 cebix 1.1 #endif
92    
93     // Data types
94     typedef unsigned char uint8;
95     typedef signed char int8;
96     #if SIZEOF_SHORT == 2
97     typedef unsigned short uint16;
98     typedef short int16;
99     #elif SIZEOF_INT == 2
100     typedef unsigned int uint16;
101     typedef int int16;
102     #else
103     #error "No 2 byte type, you lose."
104     #endif
105     #if SIZEOF_INT == 4
106     typedef unsigned int uint32;
107     typedef int int32;
108     #elif SIZEOF_LONG == 4
109     typedef unsigned long uint32;
110     typedef long int32;
111     #else
112     #error "No 4 byte type, you lose."
113     #endif
114     #if SIZEOF_LONG == 8
115     typedef unsigned long uint64;
116     typedef long int64;
117 gbeauche 1.3 #define VAL64(a) (a ## l)
118     #define UVAL64(a) (a ## ul)
119 cebix 1.1 #elif SIZEOF_LONG_LONG == 8
120     typedef unsigned long long uint64;
121     typedef long long int64;
122 gbeauche 1.3 #define VAL64(a) (a ## LL)
123     #define UVAL64(a) (a ## uLL)
124 cebix 1.1 #else
125     #error "No 8 byte type, you lose."
126     #endif
127 gbeauche 1.3 #if SIZEOF_VOID_P == 4
128     typedef uint32 uintptr;
129     typedef int32 intptr;
130     #elif SIZEOF_VOID_P == 8
131     typedef uint64 uintptr;
132     typedef int64 intptr;
133     #else
134     #error "Unsupported size of pointer"
135 gbeauche 1.5 #endif
136    
137 gbeauche 1.15 /**
138     * Helper functions to byteswap data
139     **/
140    
141     #if defined(__GNUC__)
142 gbeauche 1.23 #if defined(__x86_64__) || defined(__i386__)
143 gbeauche 1.15 // Linux/AMD64 currently has no asm optimized bswap_32() in <byteswap.h>
144     #define opt_bswap_32 do_opt_bswap_32
145     static inline uint32 do_opt_bswap_32(uint32 x)
146     {
147     uint32 v;
148     __asm__ __volatile__ ("bswap %0" : "=r" (v) : "0" (x));
149     return v;
150     }
151     #endif
152     #endif
153    
154 gbeauche 1.5 #ifdef HAVE_BYTESWAP_H
155     #include <byteswap.h>
156     #endif
157    
158 gbeauche 1.15 #ifdef opt_bswap_16
159     #undef bswap_16
160     #define bswap_16 opt_bswap_16
161     #endif
162 gbeauche 1.5 #ifndef bswap_16
163     #define bswap_16 generic_bswap_16
164     #endif
165    
166     static inline uint16 generic_bswap_16(uint16 x)
167     {
168     return ((x & 0xff) << 8) | ((x >> 8) & 0xff);
169     }
170    
171 gbeauche 1.15 #ifdef opt_bswap_32
172     #undef bswap_32
173     #define bswap_32 opt_bswap_32
174     #endif
175 gbeauche 1.5 #ifndef bswap_32
176     #define bswap_32 generic_bswap_32
177     #endif
178    
179     static inline uint32 generic_bswap_32(uint32 x)
180     {
181     return (((x & 0xff000000) >> 24) |
182     ((x & 0x00ff0000) >> 8) |
183     ((x & 0x0000ff00) << 8) |
184     ((x & 0x000000ff) << 24) );
185     }
186 gbeauche 1.23
187     #if defined(__i386__)
188     #define opt_bswap_64 do_opt_bswap_64
189     static inline uint64 do_opt_bswap_64(uint64 x)
190     {
191     return (bswap_32(x >> 32) | (((uint64)bswap_32((uint32)x)) << 32));
192     }
193     #endif
194 gbeauche 1.5
195 gbeauche 1.15 #ifdef opt_bswap_64
196     #undef bswap_64
197     #define bswap_64 opt_bswap_64
198     #endif
199 gbeauche 1.5 #ifndef bswap_64
200     #define bswap_64 generic_bswap_64
201     #endif
202    
203     static inline uint64 generic_bswap_64(uint64 x)
204     {
205     return (((x & UVAL64(0xff00000000000000)) >> 56) |
206     ((x & UVAL64(0x00ff000000000000)) >> 40) |
207     ((x & UVAL64(0x0000ff0000000000)) >> 24) |
208     ((x & UVAL64(0x000000ff00000000)) >> 8) |
209     ((x & UVAL64(0x00000000ff000000)) << 8) |
210     ((x & UVAL64(0x0000000000ff0000)) << 24) |
211     ((x & UVAL64(0x000000000000ff00)) << 40) |
212     ((x & UVAL64(0x00000000000000ff)) << 56) );
213     }
214    
215     #ifdef WORDS_BIGENDIAN
216     static inline uint16 tswap16(uint16 x) { return x; }
217     static inline uint32 tswap32(uint32 x) { return x; }
218     static inline uint64 tswap64(uint64 x) { return x; }
219     #else
220     static inline uint16 tswap16(uint16 x) { return bswap_16(x); }
221     static inline uint32 tswap32(uint32 x) { return bswap_32(x); }
222     static inline uint64 tswap64(uint64 x) { return bswap_64(x); }
223 gbeauche 1.3 #endif
224 cebix 1.1
225 gbeauche 1.6 // spin locks
226     #ifdef __GNUC__
227    
228 gbeauche 1.22 #if defined(__powerpc__) || defined(__ppc__)
229 gbeauche 1.6 #define HAVE_TEST_AND_SET 1
230 gbeauche 1.20 static inline int testandset(volatile int *p)
231 gbeauche 1.6 {
232     int ret;
233 gbeauche 1.22 __asm__ __volatile__("0: lwarx %0,0,%1\n"
234     " xor. %0,%3,%0\n"
235     " bne 1f\n"
236     " stwcx. %2,0,%1\n"
237     " bne- 0b\n"
238 gbeauche 1.6 "1: "
239     : "=&r" (ret)
240     : "r" (p), "r" (1), "r" (0)
241     : "cr0", "memory");
242     return ret;
243     }
244     #endif
245    
246     #ifdef __i386__
247     #define HAVE_TEST_AND_SET 1
248 gbeauche 1.20 static inline int testandset(volatile int *p)
249 gbeauche 1.6 {
250 gbeauche 1.20 int ret;
251 gbeauche 1.6 long int readval;
252 gbeauche 1.20 /* Note: the "xchg" instruction does not need a "lock" prefix */
253     __asm__ __volatile__("xchgl %0, %1"
254     : "=r" (ret), "=m" (*p), "=a" (readval)
255     : "0" (1), "m" (*p)
256 gbeauche 1.6 : "memory");
257     return ret;
258     }
259     #endif
260    
261     #ifdef __s390__
262     #define HAVE_TEST_AND_SET 1
263 gbeauche 1.20 static inline int testandset(volatile int *p)
264 gbeauche 1.6 {
265     int ret;
266    
267     __asm__ __volatile__("0: cs %0,%1,0(%2)\n"
268     " jl 0b"
269     : "=&d" (ret)
270     : "r" (1), "a" (p), "0" (*p)
271     : "cc", "memory" );
272     return ret;
273     }
274     #endif
275    
276     #ifdef __alpha__
277     #define HAVE_TEST_AND_SET 1
278 gbeauche 1.20 static inline int testandset(volatile int *p)
279 gbeauche 1.6 {
280     int ret;
281     unsigned long one;
282    
283     __asm__ __volatile__("0: mov 1,%2\n"
284     " ldl_l %0,%1\n"
285     " stl_c %2,%1\n"
286     " beq %2,1f\n"
287     ".subsection 2\n"
288     "1: br 0b\n"
289     ".previous"
290     : "=r" (ret), "=m" (*p), "=r" (one)
291     : "m" (*p));
292     return ret;
293     }
294     #endif
295    
296     #ifdef __sparc__
297     #define HAVE_TEST_AND_SET 1
298 gbeauche 1.20 static inline int testandset(volatile int *p)
299 gbeauche 1.6 {
300     int ret;
301    
302     __asm__ __volatile__("ldstub [%1], %0"
303     : "=r" (ret)
304     : "r" (p)
305     : "memory");
306    
307     return (ret ? 1 : 0);
308     }
309     #endif
310    
311     #ifdef __arm__
312     #define HAVE_TEST_AND_SET 1
313 gbeauche 1.20 static inline int testandset(volatile int *p)
314 gbeauche 1.6 {
315     register unsigned int ret;
316     __asm__ __volatile__("swp %0, %1, [%2]"
317     : "=r"(ret)
318     : "0"(1), "r"(p));
319    
320     return ret;
321     }
322     #endif
323    
324     #endif /* __GNUC__ */
325    
326     #if HAVE_TEST_AND_SET
327     #define HAVE_SPINLOCKS 1
328 gbeauche 1.20 typedef volatile int spinlock_t;
329 gbeauche 1.6
330 gbeauche 1.8 static const spinlock_t SPIN_LOCK_UNLOCKED = 0;
331 gbeauche 1.6
332     static inline void spin_lock(spinlock_t *lock)
333     {
334     while (testandset(lock));
335     }
336    
337     static inline void spin_unlock(spinlock_t *lock)
338     {
339     *lock = 0;
340     }
341    
342     static inline int spin_trylock(spinlock_t *lock)
343     {
344     return !testandset(lock);
345     }
346     #endif
347    
348 cebix 1.1 // Time data type for Time Manager emulation
349     #ifdef HAVE_CLOCK_GETTIME
350     typedef struct timespec tm_time_t;
351     #else
352     typedef struct timeval tm_time_t;
353     #endif
354    
355 gbeauche 1.18 // Timing functions
356     extern uint64 GetTicks_usec(void);
357     extern void Delay_usec(uint32 usec);
358    
359 cebix 1.2 // Setup pthread attributes
360     extern void Set_pthread_attr(pthread_attr_t *attr, int priority);
361    
362 cebix 1.1 // Various definitions
363     typedef struct rgb_color {
364     uint8 red;
365     uint8 green;
366     uint8 blue;
367     uint8 alpha;
368     } rgb_color;
369    
370 gbeauche 1.19 // X11 display fast locks
371     #ifdef HAVE_SPINLOCKS
372     #define X11_LOCK_TYPE spinlock_t
373     #define X11_LOCK_INIT SPIN_LOCK_UNLOCKED
374     #define XDisplayLock() spin_lock(&x_display_lock)
375     #define XDisplayUnlock() spin_unlock(&x_display_lock)
376     #elif defined(HAVE_PTHREADS)
377     #define X11_LOCK_TYPE pthread_mutex_t
378     #define X11_LOCK_INIT PTHREAD_MUTEX_INITIALIZER
379     #define XDisplayLock() pthread_mutex_lock(&x_display_lock);
380     #define XDisplayUnlock() pthread_mutex_unlock(&x_display_lock);
381     #else
382     #define XDisplayLock()
383     #define XDisplayUnlock()
384     #endif
385     #ifdef X11_LOCK_TYPE
386     extern X11_LOCK_TYPE x_display_lock;
387     #endif
388    
389 cebix 1.1 // Macro for calling MacOS routines
390     #define CallMacOS(type, tvect) call_macos((uint32)tvect)
391     #define CallMacOS1(type, tvect, arg1) call_macos1((uint32)tvect, (uint32)arg1)
392     #define CallMacOS2(type, tvect, arg1, arg2) call_macos2((uint32)tvect, (uint32)arg1, (uint32)arg2)
393     #define CallMacOS3(type, tvect, arg1, arg2, arg3) call_macos3((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3)
394     #define CallMacOS4(type, tvect, arg1, arg2, arg3, arg4) call_macos4((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4)
395     #define CallMacOS5(type, tvect, arg1, arg2, arg3, arg4, arg5) call_macos5((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4, (uint32)arg5)
396     #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)
397     #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)
398    
399 gbeauche 1.3 #ifdef __cplusplus
400     extern "C" {
401     #endif
402     extern uint32 call_macos(uint32 tvect);
403     extern uint32 call_macos1(uint32 tvect, uint32 arg1);
404     extern uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2);
405     extern uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3);
406     extern uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4);
407     extern uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5);
408     extern uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6);
409     extern uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7);
410     #ifdef __cplusplus
411     }
412     #endif
413 cebix 1.1
414     #endif