ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/Unix/sysdeps.h
Revision: 1.6
Committed: 2003-09-29T08:27:56Z (20 years, 9 months ago) by gbeauche
Content type: text/plain
Branch: MAIN
Changes since 1.5: +123 -0 lines
Log Message:
spinlocks from QEMU

File Contents

# Content
1 /*
2 * sysdeps.h - System dependent definitions for Linux
3 *
4 * SheepShaver (C) 1997-2002 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_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 // Define for external components
63 #define SHEEPSHAVER 1
64
65 // Mac and host address space are the same
66 #define REAL_ADDRESSING 1
67
68 #define POWERPC_ROM 1
69
70 #if EMULATED_PPC
71 // Mac ROM is write protected when banked memory is used
72 #if REAL_ADDRESSING || DIRECT_ADDRESSING
73 # define ROM_IS_WRITE_PROTECTED 0
74 # define USE_SCRATCHMEM_SUBTERFUGE 1
75 #else
76 # define ROM_IS_WRITE_PROTECTED 1
77 #endif
78 #else
79 // Mac ROM is write protected
80 #define ROM_IS_WRITE_PROTECTED 1
81 #define USE_SCRATCHMEM_SUBTERFUGE 0
82 #endif
83
84 // Data types
85 typedef unsigned char uint8;
86 typedef signed char int8;
87 #if SIZEOF_SHORT == 2
88 typedef unsigned short uint16;
89 typedef short int16;
90 #elif SIZEOF_INT == 2
91 typedef unsigned int uint16;
92 typedef int int16;
93 #else
94 #error "No 2 byte type, you lose."
95 #endif
96 #if SIZEOF_INT == 4
97 typedef unsigned int uint32;
98 typedef int int32;
99 #elif SIZEOF_LONG == 4
100 typedef unsigned long uint32;
101 typedef long int32;
102 #else
103 #error "No 4 byte type, you lose."
104 #endif
105 #if SIZEOF_LONG == 8
106 typedef unsigned long uint64;
107 typedef long int64;
108 #define VAL64(a) (a ## l)
109 #define UVAL64(a) (a ## ul)
110 #elif SIZEOF_LONG_LONG == 8
111 typedef unsigned long long uint64;
112 typedef long long int64;
113 #define VAL64(a) (a ## LL)
114 #define UVAL64(a) (a ## uLL)
115 #else
116 #error "No 8 byte type, you lose."
117 #endif
118 #if SIZEOF_VOID_P == 4
119 typedef uint32 uintptr;
120 typedef int32 intptr;
121 #elif SIZEOF_VOID_P == 8
122 typedef uint64 uintptr;
123 typedef int64 intptr;
124 #else
125 #error "Unsupported size of pointer"
126 #endif
127
128 // Helper functions to byteswap data
129 #ifdef HAVE_BYTESWAP_H
130 #include <byteswap.h>
131 #endif
132
133 #ifndef bswap_16
134 #define bswap_16 generic_bswap_16
135 #endif
136
137 static inline uint16 generic_bswap_16(uint16 x)
138 {
139 return ((x & 0xff) << 8) | ((x >> 8) & 0xff);
140 }
141
142 #ifndef bswap_32
143 #define bswap_32 generic_bswap_32
144 #endif
145
146 static inline uint32 generic_bswap_32(uint32 x)
147 {
148 return (((x & 0xff000000) >> 24) |
149 ((x & 0x00ff0000) >> 8) |
150 ((x & 0x0000ff00) << 8) |
151 ((x & 0x000000ff) << 24) );
152 }
153
154 #ifndef bswap_64
155 #define bswap_64 generic_bswap_64
156 #endif
157
158 static inline uint64 generic_bswap_64(uint64 x)
159 {
160 return (((x & UVAL64(0xff00000000000000)) >> 56) |
161 ((x & UVAL64(0x00ff000000000000)) >> 40) |
162 ((x & UVAL64(0x0000ff0000000000)) >> 24) |
163 ((x & UVAL64(0x000000ff00000000)) >> 8) |
164 ((x & UVAL64(0x00000000ff000000)) << 8) |
165 ((x & UVAL64(0x0000000000ff0000)) << 24) |
166 ((x & UVAL64(0x000000000000ff00)) << 40) |
167 ((x & UVAL64(0x00000000000000ff)) << 56) );
168 }
169
170 #ifdef WORDS_BIGENDIAN
171 static inline uint16 tswap16(uint16 x) { return x; }
172 static inline uint32 tswap32(uint32 x) { return x; }
173 static inline uint64 tswap64(uint64 x) { return x; }
174 #else
175 static inline uint16 tswap16(uint16 x) { return bswap_16(x); }
176 static inline uint32 tswap32(uint32 x) { return bswap_32(x); }
177 static inline uint64 tswap64(uint64 x) { return bswap_64(x); }
178 #endif
179
180 // spin locks
181 #ifdef __GNUC__
182
183 #ifdef __powerpc__
184 #define HAVE_TEST_AND_SET 1
185 static inline int testandset(int *p)
186 {
187 int ret;
188 __asm__ __volatile__("0: lwarx %0,0,%1 ;"
189 " xor. %0,%3,%0;"
190 " bne 1f;"
191 " stwcx. %2,0,%1;"
192 " bne- 0b;"
193 "1: "
194 : "=&r" (ret)
195 : "r" (p), "r" (1), "r" (0)
196 : "cr0", "memory");
197 return ret;
198 }
199 #endif
200
201 #ifdef __i386__
202 #define HAVE_TEST_AND_SET 1
203 static inline int testandset(int *p)
204 {
205 char ret;
206 long int readval;
207
208 __asm__ __volatile__("lock; cmpxchgl %3, %1; sete %0"
209 : "=q" (ret), "=m" (*p), "=a" (readval)
210 : "r" (1), "m" (*p), "a" (0)
211 : "memory");
212 return ret;
213 }
214 #endif
215
216 #ifdef __s390__
217 #define HAVE_TEST_AND_SET 1
218 static inline int testandset(int *p)
219 {
220 int ret;
221
222 __asm__ __volatile__("0: cs %0,%1,0(%2)\n"
223 " jl 0b"
224 : "=&d" (ret)
225 : "r" (1), "a" (p), "0" (*p)
226 : "cc", "memory" );
227 return ret;
228 }
229 #endif
230
231 #ifdef __alpha__
232 #define HAVE_TEST_AND_SET 1
233 static inline int testandset(int *p)
234 {
235 int ret;
236 unsigned long one;
237
238 __asm__ __volatile__("0: mov 1,%2\n"
239 " ldl_l %0,%1\n"
240 " stl_c %2,%1\n"
241 " beq %2,1f\n"
242 ".subsection 2\n"
243 "1: br 0b\n"
244 ".previous"
245 : "=r" (ret), "=m" (*p), "=r" (one)
246 : "m" (*p));
247 return ret;
248 }
249 #endif
250
251 #ifdef __sparc__
252 #define HAVE_TEST_AND_SET 1
253 static inline int testandset(int *p)
254 {
255 int ret;
256
257 __asm__ __volatile__("ldstub [%1], %0"
258 : "=r" (ret)
259 : "r" (p)
260 : "memory");
261
262 return (ret ? 1 : 0);
263 }
264 #endif
265
266 #ifdef __arm__
267 #define HAVE_TEST_AND_SET 1
268 static inline int testandset(int *p)
269 {
270 register unsigned int ret;
271 __asm__ __volatile__("swp %0, %1, [%2]"
272 : "=r"(ret)
273 : "0"(1), "r"(p));
274
275 return ret;
276 }
277 #endif
278
279 #endif /* __GNUC__ */
280
281 #if HAVE_TEST_AND_SET
282 #define HAVE_SPINLOCKS 1
283 typedef int spinlock_t;
284
285 const spinlock_t SPIN_LOCK_UNLOCKED = 0;
286
287 static inline void spin_lock(spinlock_t *lock)
288 {
289 while (testandset(lock));
290 }
291
292 static inline void spin_unlock(spinlock_t *lock)
293 {
294 *lock = 0;
295 }
296
297 static inline int spin_trylock(spinlock_t *lock)
298 {
299 return !testandset(lock);
300 }
301 #endif
302
303 // Time data type for Time Manager emulation
304 #ifdef HAVE_CLOCK_GETTIME
305 typedef struct timespec tm_time_t;
306 #else
307 typedef struct timeval tm_time_t;
308 #endif
309
310 // Setup pthread attributes
311 extern void Set_pthread_attr(pthread_attr_t *attr, int priority);
312
313 // Various definitions
314 typedef struct rgb_color {
315 uint8 red;
316 uint8 green;
317 uint8 blue;
318 uint8 alpha;
319 } rgb_color;
320
321 // Macro for calling MacOS routines
322 #define CallMacOS(type, tvect) call_macos((uint32)tvect)
323 #define CallMacOS1(type, tvect, arg1) call_macos1((uint32)tvect, (uint32)arg1)
324 #define CallMacOS2(type, tvect, arg1, arg2) call_macos2((uint32)tvect, (uint32)arg1, (uint32)arg2)
325 #define CallMacOS3(type, tvect, arg1, arg2, arg3) call_macos3((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3)
326 #define CallMacOS4(type, tvect, arg1, arg2, arg3, arg4) call_macos4((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4)
327 #define CallMacOS5(type, tvect, arg1, arg2, arg3, arg4, arg5) call_macos5((uint32)tvect, (uint32)arg1, (uint32)arg2, (uint32)arg3, (uint32)arg4, (uint32)arg5)
328 #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)
329 #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)
330
331 #ifdef __cplusplus
332 extern "C" {
333 #endif
334 extern uint32 call_macos(uint32 tvect);
335 extern uint32 call_macos1(uint32 tvect, uint32 arg1);
336 extern uint32 call_macos2(uint32 tvect, uint32 arg1, uint32 arg2);
337 extern uint32 call_macos3(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3);
338 extern uint32 call_macos4(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4);
339 extern uint32 call_macos5(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5);
340 extern uint32 call_macos6(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6);
341 extern uint32 call_macos7(uint32 tvect, uint32 arg1, uint32 arg2, uint32 arg3, uint32 arg4, uint32 arg5, uint32 arg6, uint32 arg7);
342 #ifdef __cplusplus
343 }
344 #endif
345
346 #endif