ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/sysdeps.h
Revision: 1.27
Committed: 2004-05-12T11:38:16Z (20 years ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.26: +0 -4 lines
Log Message:
Direct block chaining works on all supported platforms, enabled by default

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