pt-machine.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* Machine-dependent pthreads configuration and inline functions.
  2. Xtensa version.
  3. Copyright (C) 2007 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 Lesser General Public
  7. License as published by the Free Software Foundation; either
  8. version 2.1 of the 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. Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public
  14. License along with the GNU C Library; if not, see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef _PT_MACHINE_H
  17. #define _PT_MACHINE_H 1
  18. #include <bits/xtensa-config.h>
  19. #include <sys/syscall.h>
  20. #include <asm/unistd.h>
  21. #ifndef PT_EI
  22. # define PT_EI extern inline __attribute__ ((gnu_inline))
  23. #endif
  24. #define MEMORY_BARRIER() __asm__ ("memw" : : : "memory")
  25. #define HAS_COMPARE_AND_SWAP
  26. extern long int testandset (int *spinlock);
  27. extern int __compare_and_swap (long int *p, long int oldval, long int newval);
  28. #if XCHAL_HAVE_EXCLUSIVE
  29. /* Spinlock implementation; required. */
  30. PT_EI long int
  31. testandset (int *spinlock)
  32. {
  33. unsigned long tmp;
  34. __asm__ volatile (
  35. " memw \n"
  36. "1: l32ex %0, %1 \n"
  37. " bnez %0, 2f \n"
  38. " movi %0, 1 \n"
  39. " s32ex %0, %1 \n"
  40. " getex %0 \n"
  41. " beqz %0, 1b \n"
  42. " movi %0, 0 \n"
  43. " memw \n"
  44. "2: \n"
  45. : "=&a" (tmp)
  46. : "a" (spinlock)
  47. : "memory"
  48. );
  49. return tmp;
  50. }
  51. PT_EI int
  52. __compare_and_swap (long int *p, long int oldval, long int newval)
  53. {
  54. unsigned long tmp;
  55. unsigned long value;
  56. __asm__ volatile (
  57. " memw \n"
  58. "1: l32ex %0, %2 \n"
  59. " bne %0, %4, 2f \n"
  60. " mov %1, %3 \n"
  61. " s32ex %1, %2 \n"
  62. " getex %1 \n"
  63. " beqz %1, 1b \n"
  64. " memw \n"
  65. "2: \n"
  66. : "=&a" (tmp), "=&a" (value)
  67. : "a" (p), "a" (newval), "a" (oldval)
  68. : "memory" );
  69. return tmp == oldval;
  70. }
  71. #elif XCHAL_HAVE_S32C1I
  72. /* Spinlock implementation; required. */
  73. PT_EI long int
  74. testandset (int *spinlock)
  75. {
  76. unsigned long tmp;
  77. __asm__ volatile (
  78. " movi %0, 0 \n"
  79. " wsr %0, SCOMPARE1 \n"
  80. " movi %0, 1 \n"
  81. " s32c1i %0, %1 \n"
  82. : "=&a" (tmp), "+m" (*spinlock)
  83. :: "memory"
  84. );
  85. return tmp;
  86. }
  87. PT_EI int
  88. __compare_and_swap (long int *p, long int oldval, long int newval)
  89. {
  90. unsigned long tmp;
  91. unsigned long value;
  92. __asm__ volatile (
  93. "1: l32i %0, %2 \n"
  94. " bne %0, %4, 2f \n"
  95. " wsr %0, SCOMPARE1 \n"
  96. " mov %1, %0 \n"
  97. " mov %0, %3 \n"
  98. " s32c1i %0, %2 \n"
  99. " bne %1, %0, 1b \n"
  100. "2: \n"
  101. : "=&a" (tmp), "=&a" (value), "+m" (*p)
  102. : "a" (newval), "a" (oldval)
  103. : "memory" );
  104. return tmp == oldval;
  105. }
  106. #else
  107. #error No hardware atomic operations
  108. #endif
  109. /* Get some notion of the current stack. Need not be exactly the top
  110. of the stack, just something somewhere in the current frame. */
  111. #define CURRENT_STACK_FRAME __builtin_frame_address (0)
  112. #endif /* _PT_MACHINE_H */