1 /* Machine-dependent pthreads configuration and inline functions.
2 powerpc version.
3 Copyright (C) 2002, 2003 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 The GNU C Library 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 GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc.,
19 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 /* These routines are from Appendix G of the 'PowerPC 601 RISC Microprocessor
22 User's Manual', by IBM and Motorola. */
23
24 #ifndef _PT_MACHINE_H
25 #define _PT_MACHINE_H 1
26
27 #ifndef PT_EI
28 # define PT_EI __extern_always_inline
29 #endif
30
31 extern long int testandset (int *spinlock);
32 extern int __compare_and_swap (long int *p, long int oldval, long int newval);
33 extern int __compare_and_swap32 (int *p, int oldval, int newval);
34
35 /* For multiprocessor systems, we want to ensure all memory accesses
36 are completed before we reset a lock. On other systems, we still
37 need to make sure that the compiler has flushed everything to memory. */
38 #define MEMORY_BARRIER() __asm__ __volatile__ ("lwsync" : : : "memory")
39 #define READ_MEMORY_BARRIER() __asm__ __volatile__ ("lwsync" : : : "memory")
40 #define WRITE_MEMORY_BARRIER() __asm__ __volatile__ ("eieio" : : : "memory")
41
42 /* We want the OS to assign stack addresses. */
43 #define FLOATING_STACKS 1
44
45 /* Maximum size of the stack if the rlimit is unlimited. */
46 #define ARCH_STACK_MAX_SIZE 16*1024*1024
47
48 /* Get some notion of the current stack. Need not be exactly the top
49 of the stack, just something somewhere in the current frame. */
50 #define CURRENT_STACK_FRAME stack_pointer
51 register char * stack_pointer __asm__ ("r1");
52
53 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
54 struct pthread;
55 register struct pthread *__thread_self __asm__("r13");
56
57 #ifdef NOT_FOR_L4
58 /* Return the thread descriptor for the current thread. */
59 #define THREAD_SELF __thread_self
60
61 /* Initialize the thread-unique value. */
62 #define INIT_THREAD_SELF(descr, nr) (__thread_self = (descr))
63
64 /* Access to data in the thread descriptor is easy. */
65 #define THREAD_GETMEM(descr, member) \
66 ((void) (descr), THREAD_SELF->member)
67 #define THREAD_GETMEM_NC(descr, member) \
68 ((void) (descr), THREAD_SELF->member)
69 #define THREAD_SETMEM(descr, member, value) \
70 ((void) (descr), THREAD_SELF->member = (value))
71 #define THREAD_SETMEM_NC(descr, member, value) \
72 ((void) (descr), THREAD_SELF->member = (value))
73 #endif /* L4 */
74
75 /* Compare-and-swap for semaphores. */
76 /* note that test-and-set(x) is the same as !compare-and-swap(x, 0, 1) */
77
78 #define HAS_COMPARE_AND_SWAP
79 #define HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
80
81 PT_EI int
__compare_and_swap(long int * p,long int oldval,long int newval)82 __compare_and_swap (long int *p, long int oldval, long int newval)
83 {
84 long int ret;
85
86 __asm__ __volatile__ (
87 "0: ldarx %0,0,%1 ;"
88 " xor. %0,%3,%0;"
89 " bne 1f;"
90 " stdcx. %2,0,%1;"
91 " bne- 0b;"
92 "1: "
93 : "=&r"(ret)
94 : "r"(p), "r"(newval), "r"(oldval)
95 : "cr0", "memory");
96 /* This version of __compare_and_swap is to be used when acquiring
97 a lock, so we don't need to worry about whether other memory
98 operations have completed, but we do need to be sure that any loads
99 after this point really occur after we have acquired the lock. */
100 __asm__ __volatile__ ("isync" : : : "memory");
101 return (int)(ret == 0);
102 }
103
104 PT_EI int
__compare_and_swap_with_release_semantics(long int * p,long int oldval,long int newval)105 __compare_and_swap_with_release_semantics (long int *p,
106 long int oldval, long int newval)
107 {
108 long int ret;
109
110 MEMORY_BARRIER ();
111 __asm__ __volatile__ (
112 "0: ldarx %0,0,%1 ;"
113 " xor. %0,%3,%0;"
114 " bne 1f;"
115 " stdcx. %2,0,%1;"
116 " bne- 0b;"
117 "1: "
118 : "=&r"(ret)
119 : "r"(p), "r"(newval), "r"(oldval)
120 : "cr0", "memory");
121 return (int)(ret == 0);
122 }
123
124 PT_EI int
__compare_and_swap32(int * p,int oldval,int newval)125 __compare_and_swap32 (int *p, int oldval, int newval)
126 {
127 int ret;
128
129 __asm__ __volatile__ (
130 "0: lwarx %0,0,%1 ;"
131 " xor. %0,%3,%0;"
132 " bne 1f;"
133 " stwcx. %2,0,%1;"
134 " bne- 0b;"
135 "1: "
136 : "=&r"(ret)
137 : "r"(p), "r"(newval), "r"(oldval)
138 : "cr0", "memory");
139 /* This version of __compare_and_swap is to be used when acquiring
140 a lock, so we don't need to worry about whether other memory
141 operations have completed, but we do need to be sure that any loads
142 after this point really occur after we have acquired the lock. */
143 __asm__ __volatile__ ("isync" : : : "memory");
144 return (int)(ret == 0);
145 }
146
147 PT_EI int
__compare_and_swap32_with_release_semantics(long int * p,long int oldval,long int newval)148 __compare_and_swap32_with_release_semantics (long int *p,
149 long int oldval, long int newval)
150 {
151 long int ret;
152
153 MEMORY_BARRIER ();
154 __asm__ __volatile__ (
155 "0: lwarx %0,0,%1 ;"
156 " xor. %0,%3,%0;"
157 " bne 1f;"
158 " stwcx. %2,0,%1;"
159 " bne- 0b;"
160 "1: "
161 : "=&r"(ret)
162 : "r"(p), "r"(newval), "r"(oldval)
163 : "cr0", "memory");
164 return (int)(ret == 0);
165 }
166
167 PT_EI long int
testandset(int * p)168 testandset (int *p)
169 {
170 long int ret, val = 1;
171
172 MEMORY_BARRIER ();
173 __asm__ __volatile__ (
174 "0: lwarx %0,0,%1 ;"
175 " cmpwi 0,%0,0;"
176 " bne 1f;"
177 " stwcx. %2,0,%1;"
178 " bne- 0b;"
179 "1: "
180 : "=&r"(ret)
181 : "r"(p), "r" (val)
182 : "cr0", "memory");
183 MEMORY_BARRIER ();
184 return ret != 0;
185 }
186
187 #endif /* pt-machine.h */
188