1 /*
2  * Distributed under the Boost Software License, Version 1.0.
3  * (See accompanying file LICENSE_1_0.txt or copy at
4  * http://www.boost.org/LICENSE_1_0.txt)
5  *
6  * Copyright (c) 2009, 2011 Helge Bahmann
7  * Copyright (c) 2009 Phil Endecott
8  * Copyright (c) 2013 Tim Blechmann
9  * Linux-specific code by Phil Endecott
10  * Copyright (c) 2014 Andrey Semashev
11  */
12 /*!
13  * \file   atomic/detail/ops_linux_arm.hpp
14  *
15  * This header contains implementation of the \c operations template.
16  */
17 
18 #ifndef BOOST_ATOMIC_DETAIL_OPS_LINUX_ARM_HPP_INCLUDED_
19 #define BOOST_ATOMIC_DETAIL_OPS_LINUX_ARM_HPP_INCLUDED_
20 
21 #include <boost/memory_order.hpp>
22 #include <boost/atomic/detail/config.hpp>
23 #include <boost/atomic/detail/storage_type.hpp>
24 #include <boost/atomic/detail/operations_fwd.hpp>
25 #include <boost/atomic/capabilities.hpp>
26 #include <boost/atomic/detail/ops_cas_based.hpp>
27 #include <boost/atomic/detail/ops_extending_cas_based.hpp>
28 
29 #ifdef BOOST_HAS_PRAGMA_ONCE
30 #pragma once
31 #endif
32 
33 namespace boost {
34 namespace atomics {
35 namespace detail {
36 
37 // Different ARM processors have different atomic instructions.  In particular,
38 // architecture versions before v6 (which are still in widespread use, e.g. the
39 // Intel/Marvell XScale chips like the one in the NSLU2) have only atomic swap.
40 // On Linux the kernel provides some support that lets us abstract away from
41 // these differences: it provides emulated CAS and barrier functions at special
42 // addresses that are guaranteed not to be interrupted by the kernel.  Using
43 // this facility is slightly slower than inline assembler would be, but much
44 // faster than a system call.
45 //
46 // While this emulated CAS is "strong" in the sense that it does not fail
47 // "spuriously" (i.e.: it never fails to perform the exchange when the value
48 // found equals the value expected), it does not return the found value on
49 // failure. To satisfy the atomic API, compare_exchange_{weak|strong} must
50 // return the found value on failure, and we have to manually load this value
51 // after the emulated CAS reports failure. This in turn introduces a race
52 // between the CAS failing (due to the "wrong" value being found) and subsequently
53 // loading (which might turn up the "right" value). From an application's
54 // point of view this looks like "spurious failure", and therefore the
55 // emulated CAS is only good enough to provide compare_exchange_weak
56 // semantics.
57 
58 struct linux_arm_cas_base
59 {
fence_before_storeboost::atomics::detail::linux_arm_cas_base60     static BOOST_FORCEINLINE void fence_before_store(memory_order order) BOOST_NOEXCEPT
61     {
62         if ((order & memory_order_release) != 0)
63             hardware_full_fence();
64     }
65 
fence_after_storeboost::atomics::detail::linux_arm_cas_base66     static BOOST_FORCEINLINE void fence_after_store(memory_order order) BOOST_NOEXCEPT
67     {
68         if (order == memory_order_seq_cst)
69             hardware_full_fence();
70     }
71 
fence_after_loadboost::atomics::detail::linux_arm_cas_base72     static BOOST_FORCEINLINE void fence_after_load(memory_order order) BOOST_NOEXCEPT
73     {
74         if ((order & (memory_order_consume | memory_order_acquire)) != 0)
75             hardware_full_fence();
76     }
77 
hardware_full_fenceboost::atomics::detail::linux_arm_cas_base78     static BOOST_FORCEINLINE void hardware_full_fence() BOOST_NOEXCEPT
79     {
80         typedef void (*kernel_dmb_t)(void);
81         ((kernel_dmb_t)0xffff0fa0)();
82     }
83 };
84 
85 template< bool Signed >
86 struct linux_arm_cas :
87     public linux_arm_cas_base
88 {
89     typedef typename make_storage_type< 4u, Signed >::type storage_type;
90 
storeboost::atomics::detail::linux_arm_cas91     static BOOST_FORCEINLINE void store(storage_type volatile& storage, storage_type v, memory_order order) BOOST_NOEXCEPT
92     {
93         fence_before_store(order);
94         storage = v;
95         fence_after_store(order);
96     }
97 
loadboost::atomics::detail::linux_arm_cas98     static BOOST_FORCEINLINE storage_type load(storage_type const volatile& storage, memory_order order) BOOST_NOEXCEPT
99     {
100         storage_type v = storage;
101         fence_after_load(order);
102         return v;
103     }
104 
compare_exchange_strongboost::atomics::detail::linux_arm_cas105     static BOOST_FORCEINLINE bool compare_exchange_strong(
106         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order success_order, memory_order failure_order) BOOST_NOEXCEPT
107     {
108         while (true)
109         {
110             storage_type tmp = expected;
111             if (compare_exchange_weak(storage, tmp, desired, success_order, failure_order))
112                 return true;
113             if (tmp != expected)
114             {
115                 expected = tmp;
116                 return false;
117             }
118         }
119     }
120 
compare_exchange_weakboost::atomics::detail::linux_arm_cas121     static BOOST_FORCEINLINE bool compare_exchange_weak(
122         storage_type volatile& storage, storage_type& expected, storage_type desired, memory_order, memory_order) BOOST_NOEXCEPT
123     {
124         typedef storage_type (*kernel_cmpxchg32_t)(storage_type oldval, storage_type newval, volatile storage_type* ptr);
125 
126         if (((kernel_cmpxchg32_t)0xffff0fc0)(expected, desired, &storage) == 0)
127         {
128             return true;
129         }
130         else
131         {
132             expected = storage;
133             return false;
134         }
135     }
136 
is_lock_freeboost::atomics::detail::linux_arm_cas137     static BOOST_FORCEINLINE bool is_lock_free(storage_type const volatile&) BOOST_NOEXCEPT
138     {
139         return true;
140     }
141 };
142 
143 template< bool Signed >
144 struct operations< 1u, Signed > :
145     public extending_cas_based_operations< cas_based_operations< linux_arm_cas< Signed > >, 1u, Signed >
146 {
147 };
148 
149 template< bool Signed >
150 struct operations< 2u, Signed > :
151     public extending_cas_based_operations< cas_based_operations< linux_arm_cas< Signed > >, 2u, Signed >
152 {
153 };
154 
155 template< bool Signed >
156 struct operations< 4u, Signed > :
157     public cas_based_operations< linux_arm_cas< Signed > >
158 {
159 };
160 
thread_fence(memory_order order)161 BOOST_FORCEINLINE void thread_fence(memory_order order) BOOST_NOEXCEPT
162 {
163     if (order != memory_order_relaxed)
164         linux_arm_cas_base::hardware_full_fence();
165 }
166 
signal_fence(memory_order order)167 BOOST_FORCEINLINE void signal_fence(memory_order order) BOOST_NOEXCEPT
168 {
169     if (order != memory_order_relaxed)
170         __asm__ __volatile__ ("" ::: "memory");
171 }
172 
173 } // namespace detail
174 } // namespace atomics
175 } // namespace boost
176 
177 #endif // BOOST_ATOMIC_DETAIL_OPS_LINUX_ARM_HPP_INCLUDED_
178