1 | /*
|
---|
2 | * Copyright (c) 2007 Michal Kebrt
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup libcarm32
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief Atomic operations.
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef LIBC_arm32_ATOMIC_H_
|
---|
37 | #define LIBC_arm32_ATOMIC_H_
|
---|
38 |
|
---|
39 | #define LIBC_ARCH_ATOMIC_H_
|
---|
40 | #define CAS
|
---|
41 |
|
---|
42 | #include <atomicdflt.h>
|
---|
43 | #include <bool.h>
|
---|
44 | #include <sys/types.h>
|
---|
45 |
|
---|
46 | extern uintptr_t *ras_page;
|
---|
47 |
|
---|
48 | static inline bool cas(atomic_t *val, atomic_count_t ov, atomic_count_t nv)
|
---|
49 | {
|
---|
50 | atomic_count_t ret = 0;
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * The following instructions between labels 1 and 2 constitute a
|
---|
54 | * Restartable Atomic Seqeunce. Should the sequence be non-atomic,
|
---|
55 | * the kernel will restart it.
|
---|
56 | */
|
---|
57 | asm volatile (
|
---|
58 | "1:\n"
|
---|
59 | " adr %[ret], 1b\n"
|
---|
60 | " str %[ret], %[rp0]\n"
|
---|
61 | " adr %[ret], 2f\n"
|
---|
62 | " str %[ret], %[rp1]\n"
|
---|
63 | " ldr %[ret], %[addr]\n"
|
---|
64 | " cmp %[ret], %[ov]\n"
|
---|
65 | " streq %[nv], %[addr]\n"
|
---|
66 | "2:\n"
|
---|
67 | " moveq %[ret], #1\n"
|
---|
68 | " movne %[ret], #0\n"
|
---|
69 | : [ret] "+&r" (ret),
|
---|
70 | [rp0] "=m" (ras_page[0]),
|
---|
71 | [rp1] "=m" (ras_page[1]),
|
---|
72 | [addr] "+m" (val->count)
|
---|
73 | : [ov] "r" (ov),
|
---|
74 | [nv] "r" (nv)
|
---|
75 | : "memory"
|
---|
76 | );
|
---|
77 |
|
---|
78 | ras_page[0] = 0;
|
---|
79 | asm volatile (
|
---|
80 | "" ::: "memory"
|
---|
81 | );
|
---|
82 | ras_page[1] = 0xffffffff;
|
---|
83 |
|
---|
84 | return (bool) ret;
|
---|
85 | }
|
---|
86 |
|
---|
87 | /** Atomic addition.
|
---|
88 | *
|
---|
89 | * @param val Where to add.
|
---|
90 | * @param i Value to be added.
|
---|
91 | *
|
---|
92 | * @return Value after addition.
|
---|
93 | *
|
---|
94 | */
|
---|
95 | static inline atomic_count_t atomic_add(atomic_t *val, atomic_count_t i)
|
---|
96 | {
|
---|
97 | atomic_count_t ret = 0;
|
---|
98 |
|
---|
99 | /*
|
---|
100 | * The following instructions between labels 1 and 2 constitute a
|
---|
101 | * Restartable Atomic Seqeunce. Should the sequence be non-atomic,
|
---|
102 | * the kernel will restart it.
|
---|
103 | */
|
---|
104 | asm volatile (
|
---|
105 | "1:\n"
|
---|
106 | " adr %[ret], 1b\n"
|
---|
107 | " str %[ret], %[rp0]\n"
|
---|
108 | " adr %[ret], 2f\n"
|
---|
109 | " str %[ret], %[rp1]\n"
|
---|
110 | " ldr %[ret], %[addr]\n"
|
---|
111 | " add %[ret], %[ret], %[imm]\n"
|
---|
112 | " str %[ret], %[addr]\n"
|
---|
113 | "2:\n"
|
---|
114 | : [ret] "+&r" (ret),
|
---|
115 | [rp0] "=m" (ras_page[0]),
|
---|
116 | [rp1] "=m" (ras_page[1]),
|
---|
117 | [addr] "+m" (val->count)
|
---|
118 | : [imm] "r" (i)
|
---|
119 | );
|
---|
120 |
|
---|
121 | ras_page[0] = 0;
|
---|
122 | asm volatile (
|
---|
123 | "" ::: "memory"
|
---|
124 | );
|
---|
125 | ras_page[1] = 0xffffffff;
|
---|
126 |
|
---|
127 | return ret;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | /** Atomic increment.
|
---|
132 | *
|
---|
133 | * @param val Variable to be incremented.
|
---|
134 | *
|
---|
135 | */
|
---|
136 | static inline void atomic_inc(atomic_t *val)
|
---|
137 | {
|
---|
138 | atomic_add(val, 1);
|
---|
139 | }
|
---|
140 |
|
---|
141 |
|
---|
142 | /** Atomic decrement.
|
---|
143 | *
|
---|
144 | * @param val Variable to be decremented.
|
---|
145 | *
|
---|
146 | */
|
---|
147 | static inline void atomic_dec(atomic_t *val)
|
---|
148 | {
|
---|
149 | atomic_add(val, -1);
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /** Atomic pre-increment.
|
---|
154 | *
|
---|
155 | * @param val Variable to be incremented.
|
---|
156 | * @return Value after incrementation.
|
---|
157 | *
|
---|
158 | */
|
---|
159 | static inline atomic_count_t atomic_preinc(atomic_t *val)
|
---|
160 | {
|
---|
161 | return atomic_add(val, 1);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /** Atomic pre-decrement.
|
---|
166 | *
|
---|
167 | * @param val Variable to be decremented.
|
---|
168 | * @return Value after decrementation.
|
---|
169 | *
|
---|
170 | */
|
---|
171 | static inline atomic_count_t atomic_predec(atomic_t *val)
|
---|
172 | {
|
---|
173 | return atomic_add(val, -1);
|
---|
174 | }
|
---|
175 |
|
---|
176 |
|
---|
177 | /** Atomic post-increment.
|
---|
178 | *
|
---|
179 | * @param val Variable to be incremented.
|
---|
180 | * @return Value before incrementation.
|
---|
181 | *
|
---|
182 | */
|
---|
183 | static inline atomic_count_t atomic_postinc(atomic_t *val)
|
---|
184 | {
|
---|
185 | return atomic_add(val, 1) - 1;
|
---|
186 | }
|
---|
187 |
|
---|
188 |
|
---|
189 | /** Atomic post-decrement.
|
---|
190 | *
|
---|
191 | * @param val Variable to be decremented.
|
---|
192 | * @return Value before decrementation.
|
---|
193 | *
|
---|
194 | */
|
---|
195 | static inline atomic_count_t atomic_postdec(atomic_t *val)
|
---|
196 | {
|
---|
197 | return atomic_add(val, -1) + 1;
|
---|
198 | }
|
---|
199 |
|
---|
200 |
|
---|
201 | #endif
|
---|
202 |
|
---|
203 | /** @}
|
---|
204 | */
|
---|