sparc.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
  3. * Copyright (c) 1996-1999 by Silicon Graphics. All rights reserved.
  4. * Copyright (c) 1999-2003 by Hewlett-Packard Company. All rights reserved.
  5. *
  6. *
  7. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  8. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  9. *
  10. * Permission is hereby granted to use or copy this program
  11. * for any purpose, provided the above notices are retained on all copies.
  12. * Permission to modify the code and to distribute modified code is granted,
  13. * provided the above notices are retained, and a notice that the code was
  14. * modified is included with the above copyright notice.
  15. *
  16. */
  17. /* TODO: Very incomplete; Add support for sparc64. */
  18. /* Non-ancient SPARCs provide compare-and-swap (casa). */
  19. #include "../all_atomic_load_store.h"
  20. /* Real SPARC code uses TSO: */
  21. #include "../ordered_except_wr.h"
  22. /* Test_and_set location is just a byte. */
  23. #include "../test_and_set_t_is_char.h"
  24. AO_INLINE AO_TS_VAL_t
  25. AO_test_and_set_full(volatile AO_TS_t *addr) {
  26. AO_TS_VAL_t oldval;
  27. __asm__ __volatile__("ldstub %1,%0"
  28. : "=r"(oldval), "=m"(*addr)
  29. : "m"(*addr) : "memory");
  30. return oldval;
  31. }
  32. #define AO_HAVE_test_and_set_full
  33. #ifndef AO_NO_SPARC_V9
  34. /* Returns nonzero if the comparison succeeded. */
  35. AO_INLINE int
  36. AO_compare_and_swap_full(volatile AO_t *addr, AO_t old, AO_t new_val) {
  37. char ret;
  38. __asm__ __volatile__ ("membar #StoreLoad | #LoadLoad\n\t"
  39. # if defined(__arch64__)
  40. "casx [%2],%0,%1\n\t"
  41. # else
  42. "cas [%2],%0,%1\n\t" /* 32-bit version */
  43. # endif
  44. "membar #StoreLoad | #StoreStore\n\t"
  45. "cmp %0,%1\n\t"
  46. "be,a 0f\n\t"
  47. "mov 1,%0\n\t"/* one insn after branch always executed */
  48. "clr %0\n\t"
  49. "0:\n\t"
  50. : "=r" (ret), "+r" (new_val)
  51. : "r" (addr), "0" (old)
  52. : "memory", "cc");
  53. return (int)ret;
  54. }
  55. #define AO_HAVE_compare_and_swap_full
  56. /* TODO: implement AO_fetch_compare_and_swap. */
  57. #endif /* !AO_NO_SPARC_V9 */
  58. /* TODO: Extend this for SPARC v8 and v9 (V8 also has swap, V9 has CAS, */
  59. /* there are barriers like membar #LoadStore, CASA (32-bit) and */
  60. /* CASXA (64-bit) instructions added in V9). */