OSDN Git Service

Replace FSF snail mail address with URLs
[uclinux-h8/uClibc.git] / libpthread / linuxthreads / sysdeps / powerpc / powerpc64 / pt-machine.h
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, see <http://www.gnu.org/licenses/>.  */
19
20 /* These routines are from Appendix G of the 'PowerPC 601 RISC Microprocessor
21    User's Manual', by IBM and Motorola.  */
22
23 #ifndef _PT_MACHINE_H
24 #define _PT_MACHINE_H   1
25
26 #ifndef PT_EI
27 # define PT_EI __extern_always_inline
28 #endif
29
30 extern long int testandset (int *spinlock);
31 extern int __compare_and_swap (long int *p, long int oldval, long int newval);
32 extern int __compare_and_swap32 (int *p, int oldval, int newval);
33
34 /* For multiprocessor systems, we want to ensure all memory accesses
35    are completed before we reset a lock.  On other systems, we still
36    need to make sure that the compiler has flushed everything to memory.  */
37 #define MEMORY_BARRIER() __asm__ __volatile__ ("lwsync" : : : "memory")
38 #define READ_MEMORY_BARRIER() __asm__ __volatile__ ("lwsync" : : : "memory")
39 #define WRITE_MEMORY_BARRIER() __asm__ __volatile__ ("eieio" : : : "memory")
40
41 /* We want the OS to assign stack addresses.  */
42 #define FLOATING_STACKS 1
43
44 /* Maximum size of the stack if the rlimit is unlimited.  */
45 #define ARCH_STACK_MAX_SIZE     16*1024*1024
46
47 /* Get some notion of the current stack.  Need not be exactly the top
48    of the stack, just something somewhere in the current frame.  */
49 #define CURRENT_STACK_FRAME  stack_pointer
50 register char * stack_pointer __asm__ ("r1");
51
52 /* Register r13 (tp) is reserved by the ABI as "thread pointer". */
53 struct _pthread_descr_struct;
54 register struct _pthread_descr_struct *__thread_self __asm__("r13");
55
56 /* Return the thread descriptor for the current thread.  */
57 #define THREAD_SELF  __thread_self
58
59 /* Initialize the thread-unique value.  */
60 #define INIT_THREAD_SELF(descr, nr)  (__thread_self = (descr))
61
62 /* Access to data in the thread descriptor is easy.  */
63 #define THREAD_GETMEM(descr, member) \
64   ((void) (descr), THREAD_SELF->member)
65 #define THREAD_GETMEM_NC(descr, member) \
66   ((void) (descr), THREAD_SELF->member)
67 #define THREAD_SETMEM(descr, member, value) \
68   ((void) (descr), THREAD_SELF->member = (value))
69 #define THREAD_SETMEM_NC(descr, member, value) \
70   ((void) (descr), THREAD_SELF->member = (value))
71
72 /* Compare-and-swap for semaphores. */
73 /* note that test-and-set(x) is the same as !compare-and-swap(x, 0, 1) */
74
75 #define HAS_COMPARE_AND_SWAP
76 #define HAS_COMPARE_AND_SWAP_WITH_RELEASE_SEMANTICS
77
78 PT_EI int
79 __compare_and_swap (long int *p, long int oldval, long int newval)
80 {
81   long int ret;
82
83   __asm__ __volatile__ (
84            "0:    ldarx %0,0,%1 ;"
85            "      xor. %0,%3,%0;"
86            "      bne 1f;"
87            "      stdcx. %2,0,%1;"
88            "      bne- 0b;"
89            "1:    "
90         : "=&r"(ret)
91         : "r"(p), "r"(newval), "r"(oldval)
92         : "cr0", "memory");
93   /* This version of __compare_and_swap is to be used when acquiring
94      a lock, so we don't need to worry about whether other memory
95      operations have completed, but we do need to be sure that any loads
96      after this point really occur after we have acquired the lock.  */
97   __asm__ __volatile__ ("isync" : : : "memory");
98   return (int)(ret == 0);
99 }
100
101 PT_EI int
102 __compare_and_swap_with_release_semantics (long int *p,
103                                            long int oldval, long int newval)
104 {
105   long int ret;
106
107   MEMORY_BARRIER ();
108   __asm__ __volatile__ (
109            "0:    ldarx %0,0,%1 ;"
110            "      xor. %0,%3,%0;"
111            "      bne 1f;"
112            "      stdcx. %2,0,%1;"
113            "      bne- 0b;"
114            "1:    "
115         : "=&r"(ret)
116         : "r"(p), "r"(newval), "r"(oldval)
117         : "cr0", "memory");
118   return (int)(ret == 0);
119 }
120
121 PT_EI int
122 __compare_and_swap32 (int *p, int oldval, int newval)
123 {
124   int ret;
125
126   __asm__ __volatile__ (
127            "0:    lwarx %0,0,%1 ;"
128            "      xor. %0,%3,%0;"
129            "      bne 1f;"
130            "      stwcx. %2,0,%1;"
131            "      bne- 0b;"
132            "1:    "
133         : "=&r"(ret)
134         : "r"(p), "r"(newval), "r"(oldval)
135         : "cr0", "memory");
136   /* This version of __compare_and_swap is to be used when acquiring
137      a lock, so we don't need to worry about whether other memory
138      operations have completed, but we do need to be sure that any loads
139      after this point really occur after we have acquired the lock.  */
140   __asm__ __volatile__ ("isync" : : : "memory");
141   return (int)(ret == 0);
142 }
143
144 PT_EI int
145 __compare_and_swap32_with_release_semantics (long int *p,
146                                            long int oldval, long int newval)
147 {
148   long int ret;
149
150   MEMORY_BARRIER ();
151   __asm__ __volatile__ (
152            "0:    lwarx %0,0,%1 ;"
153            "      xor. %0,%3,%0;"
154            "      bne 1f;"
155            "      stwcx. %2,0,%1;"
156            "      bne- 0b;"
157            "1:    "
158         : "=&r"(ret)
159         : "r"(p), "r"(newval), "r"(oldval)
160         : "cr0", "memory");
161   return (int)(ret == 0);
162 }
163
164 PT_EI long int
165 testandset (int *p)
166 {
167   long int ret, val = 1;
168
169   MEMORY_BARRIER ();
170   __asm__ __volatile__ (
171            "0:    lwarx %0,0,%1 ;"
172            "      cmpwi  0,%0,0;"
173            "      bne 1f;"
174            "      stwcx. %2,0,%1;"
175            "      bne- 0b;"
176            "1:    "
177         : "=&r"(ret)
178         : "r"(p), "r" (val)
179         : "cr0", "memory");
180   MEMORY_BARRIER ();
181   return ret != 0;
182 }
183
184 #endif /* pt-machine.h */