[d630139] | 1 | /*
|
---|
[6b781c0] | 2 | * Copyright (c) 2007 Michal Kebrt
|
---|
[d630139] | 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 |
|
---|
[228666c] | 29 | /** @addtogroup libcarm32
|
---|
[d630139] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
[6b781c0] | 33 | * @brief Atomic operations.
|
---|
[d630139] | 34 | */
|
---|
| 35 |
|
---|
| 36 | #ifndef LIBC_arm32_ATOMIC_H_
|
---|
| 37 | #define LIBC_arm32_ATOMIC_H_
|
---|
| 38 |
|
---|
[cd769305] | 39 | #define LIBC_ARCH_ATOMIC_H_
|
---|
[228666c] | 40 | #define CAS
|
---|
[cd769305] | 41 |
|
---|
| 42 | #include <atomicdflt.h>
|
---|
[8d04f709] | 43 | #include <bool.h>
|
---|
[cd769305] | 44 | #include <sys/types.h>
|
---|
[8d04f709] | 45 |
|
---|
[cd769305] | 46 | extern uintptr_t *ras_page;
|
---|
[8d04f709] | 47 |
|
---|
[228666c] | 48 | static inline bool cas(atomic_t *val, atomic_count_t ov, atomic_count_t nv)
|
---|
[8d04f709] | 49 | {
|
---|
[228666c] | 50 | atomic_count_t ret = 0;
|
---|
| 51 |
|
---|
[cd769305] | 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 | );
|
---|
[228666c] | 77 |
|
---|
[cd769305] | 78 | ras_page[0] = 0;
|
---|
[228666c] | 79 | asm volatile (
|
---|
| 80 | "" ::: "memory"
|
---|
| 81 | );
|
---|
[cd769305] | 82 | ras_page[1] = 0xffffffff;
|
---|
[228666c] | 83 |
|
---|
[cd769305] | 84 | return (bool) ret;
|
---|
[8d04f709] | 85 | }
|
---|
| 86 |
|
---|
[d630139] | 87 | /** Atomic addition.
|
---|
| 88 | *
|
---|
[6b781c0] | 89 | * @param val Where to add.
|
---|
| 90 | * @param i Value to be added.
|
---|
[d630139] | 91 | *
|
---|
| 92 | * @return Value after addition.
|
---|
[228666c] | 93 | *
|
---|
[d630139] | 94 | */
|
---|
[228666c] | 95 | static inline atomic_count_t atomic_add(atomic_t *val, atomic_count_t i)
|
---|
[d630139] | 96 | {
|
---|
[228666c] | 97 | atomic_count_t ret = 0;
|
---|
| 98 |
|
---|
[cd769305] | 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 | */
|
---|
[6b781c0] | 104 | asm volatile (
|
---|
[cd769305] | 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)
|
---|
[6b781c0] | 119 | );
|
---|
[228666c] | 120 |
|
---|
[cd769305] | 121 | ras_page[0] = 0;
|
---|
[228666c] | 122 | asm volatile (
|
---|
| 123 | "" ::: "memory"
|
---|
| 124 | );
|
---|
[cd769305] | 125 | ras_page[1] = 0xffffffff;
|
---|
[228666c] | 126 |
|
---|
[6b781c0] | 127 | return ret;
|
---|
[d630139] | 128 | }
|
---|
| 129 |
|
---|
[6b781c0] | 130 |
|
---|
| 131 | /** Atomic increment.
|
---|
| 132 | *
|
---|
| 133 | * @param val Variable to be incremented.
|
---|
[228666c] | 134 | *
|
---|
[6b781c0] | 135 | */
|
---|
[de7663f] | 136 | static inline void atomic_inc(atomic_t *val)
|
---|
| 137 | {
|
---|
| 138 | atomic_add(val, 1);
|
---|
| 139 | }
|
---|
[6b781c0] | 140 |
|
---|
| 141 |
|
---|
| 142 | /** Atomic decrement.
|
---|
| 143 | *
|
---|
| 144 | * @param val Variable to be decremented.
|
---|
[228666c] | 145 | *
|
---|
[6b781c0] | 146 | */
|
---|
[de7663f] | 147 | static inline void atomic_dec(atomic_t *val)
|
---|
| 148 | {
|
---|
| 149 | atomic_add(val, -1);
|
---|
| 150 | }
|
---|
[d630139] | 151 |
|
---|
| 152 |
|
---|
[6b781c0] | 153 | /** Atomic pre-increment.
|
---|
| 154 | *
|
---|
| 155 | * @param val Variable to be incremented.
|
---|
| 156 | * @return Value after incrementation.
|
---|
[228666c] | 157 | *
|
---|
[6b781c0] | 158 | */
|
---|
[228666c] | 159 | static inline atomic_count_t atomic_preinc(atomic_t *val)
|
---|
[de7663f] | 160 | {
|
---|
| 161 | return atomic_add(val, 1);
|
---|
| 162 | }
|
---|
[6b781c0] | 163 |
|
---|
| 164 |
|
---|
| 165 | /** Atomic pre-decrement.
|
---|
| 166 | *
|
---|
| 167 | * @param val Variable to be decremented.
|
---|
| 168 | * @return Value after decrementation.
|
---|
[228666c] | 169 | *
|
---|
[6b781c0] | 170 | */
|
---|
[228666c] | 171 | static inline atomic_count_t atomic_predec(atomic_t *val)
|
---|
[de7663f] | 172 | {
|
---|
| 173 | return atomic_add(val, -1);
|
---|
| 174 | }
|
---|
[6b781c0] | 175 |
|
---|
| 176 |
|
---|
| 177 | /** Atomic post-increment.
|
---|
| 178 | *
|
---|
| 179 | * @param val Variable to be incremented.
|
---|
| 180 | * @return Value before incrementation.
|
---|
[228666c] | 181 | *
|
---|
[6b781c0] | 182 | */
|
---|
[228666c] | 183 | static inline atomic_count_t atomic_postinc(atomic_t *val)
|
---|
[de7663f] | 184 | {
|
---|
| 185 | return atomic_add(val, 1) - 1;
|
---|
| 186 | }
|
---|
[6b781c0] | 187 |
|
---|
| 188 |
|
---|
| 189 | /** Atomic post-decrement.
|
---|
| 190 | *
|
---|
| 191 | * @param val Variable to be decremented.
|
---|
| 192 | * @return Value before decrementation.
|
---|
[228666c] | 193 | *
|
---|
[6b781c0] | 194 | */
|
---|
[228666c] | 195 | static inline atomic_count_t atomic_postdec(atomic_t *val)
|
---|
[de7663f] | 196 | {
|
---|
| 197 | return atomic_add(val, -1) + 1;
|
---|
| 198 | }
|
---|
[6b781c0] | 199 |
|
---|
[d630139] | 200 |
|
---|
| 201 | #endif
|
---|
| 202 |
|
---|
| 203 | /** @}
|
---|
| 204 | */
|
---|