ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/sysdeps.h
Revision: 1.28
Committed: 2004-05-14T08:25:30Z (20 years, 1 month ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.27: +3 -0 lines
Log Message:
Use assembly optimizations on x86 for adde/addo/etc. emulation

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