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