ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/sysdeps.h
Revision: 1.34
Committed: 2004-11-13T14:09:16Z (20 years ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.33: +14 -9 lines
Log Message:
Implement Direct Addressing mode similarly to Basilisk II. This is to get
SheepShaver working on OSes that don't support maipping of Low Memory globals
at 0x00000000, e.g. Windows.

File Contents

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