pt-machine.h 5.6 KB

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