| 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 | |
| 18 | #if (AO_GNUC_PREREQ(5, 4) || AO_CLANG_PREREQ(8, 0)) && defined(__s390x__) \ |
| 19 | && !defined(AO_DISABLE_GCC_ATOMICS) |
| 20 | /* Probably, it could be enabled for earlier clang/gcc versions. */ |
| 21 | /* But, e.g., clang-3.8.0 produces a backend error for AtomicFence. */ |
| 22 | |
| 23 | # include "generic.h" |
| 24 | |
| 25 | #else /* AO_DISABLE_GCC_ATOMICS */ |
| 26 | |
| 27 | /* The relevant documentation appears to be at */ |
| 28 | /* http://publibz.boulder.ibm.com/epubs/pdf/dz9zr003.pdf */ |
| 29 | /* around page 5-96. Apparently: */ |
| 30 | /* - Memory references in general are atomic only for a single */ |
| 31 | /* byte. But it appears that the most common load/store */ |
| 32 | /* instructions also guarantee atomicity for aligned */ |
| 33 | /* operands of standard types. WE FOOLISHLY ASSUME that */ |
| 34 | /* compilers only generate those. If that turns out to be */ |
| 35 | /* wrong, we need inline assembly code for AO_load and */ |
| 36 | /* AO_store. */ |
| 37 | /* - A store followed by a load is unordered since the store */ |
| 38 | /* may be delayed. Otherwise everything is ordered. */ |
| 39 | /* - There is a hardware compare-and-swap (CS) instruction. */ |
| 40 | |
| 41 | #include "../all_aligned_atomic_load_store.h" |
| 42 | |
| 43 | #include "../ordered_except_wr.h" |
| 44 | |
| 45 | #include "../test_and_set_t_is_ao_t.h" |
| 46 | /* TODO: Is there a way to do byte-sized test-and-set? */ |
| 47 | |
| 48 | /* TODO: AO_nop_full should probably be implemented directly. */ |
| 49 | /* It appears that certain BCR instructions have that effect. */ |
| 50 | /* Presumably they're cheaper than CS? */ |
| 51 | |
| 52 | #ifndef AO_GENERALIZE_ASM_BOOL_CAS |
| 53 | AO_INLINE int AO_compare_and_swap_full(volatile AO_t *addr, |
| 54 | AO_t old, AO_t new_val) |
| 55 | { |
| 56 | int retval; |
| 57 | __asm__ __volatile__ ( |
| 58 | # ifndef __s390x__ |
| 59 | " cs %1,%2,0(%3)\n" |
| 60 | # else |
| 61 | " csg %1,%2,0(%3)\n" |
| 62 | # endif |
| 63 | " ipm %0\n" |
| 64 | " srl %0,28\n" |
| 65 | : "=&d" (retval), "+d" (old) |
| 66 | : "d" (new_val), "a" (addr) |
| 67 | : "cc", "memory"); |
| 68 | return retval == 0; |
| 69 | } |
| 70 | #define AO_HAVE_compare_and_swap_full |
| 71 | #endif /* !AO_GENERALIZE_ASM_BOOL_CAS */ |
| 72 | |
| 73 | AO_INLINE AO_t |
| 74 | AO_fetch_compare_and_swap_full(volatile AO_t *addr, |
| 75 | AO_t old, AO_t new_val) |
| 76 | { |
| 77 | __asm__ __volatile__ ( |
| 78 | # ifndef __s390x__ |
| 79 | " cs %0,%2,%1\n" |
| 80 | # else |
| 81 | " csg %0,%2,%1\n" |
| 82 | # endif |
| 83 | : "+d" (old), "=Q" (*addr) |
| 84 | : "d" (new_val), "m" (*addr) |
| 85 | : "cc", "memory"); |
| 86 | return old; |
| 87 | } |
| 88 | #define AO_HAVE_fetch_compare_and_swap_full |
| 89 | |
| 90 | #endif /* AO_DISABLE_GCC_ATOMICS */ |
| 91 | |
| 92 | /* TODO: Add double-wide operations for 32-bit executables. */ |
| 93 | |