| 1 | /*
|
|---|
| 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
|---|
| 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 ia32
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /** @file
|
|---|
| 33 | */
|
|---|
| 34 |
|
|---|
| 35 | #ifndef KERN_ia32_ATOMIC_H_
|
|---|
| 36 | #define KERN_ia32_ATOMIC_H_
|
|---|
| 37 |
|
|---|
| 38 | #include <typedefs.h>
|
|---|
| 39 | #include <arch/barrier.h>
|
|---|
| 40 | #include <preemption.h>
|
|---|
| 41 | #include <trace.h>
|
|---|
| 42 |
|
|---|
| 43 | NO_TRACE static inline void atomic_inc(atomic_t *val)
|
|---|
| 44 | {
|
|---|
| 45 | #ifdef CONFIG_SMP
|
|---|
| 46 | asm volatile (
|
|---|
| 47 | "lock incl %[count]\n"
|
|---|
| 48 | : [count] "+m" (val->count)
|
|---|
| 49 | );
|
|---|
| 50 | #else
|
|---|
| 51 | asm volatile (
|
|---|
| 52 | "incl %[count]\n"
|
|---|
| 53 | : [count] "+m" (val->count)
|
|---|
| 54 | );
|
|---|
| 55 | #endif /* CONFIG_SMP */
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | NO_TRACE static inline void atomic_dec(atomic_t *val)
|
|---|
| 59 | {
|
|---|
| 60 | #ifdef CONFIG_SMP
|
|---|
| 61 | asm volatile (
|
|---|
| 62 | "lock decl %[count]\n"
|
|---|
| 63 | : [count] "+m" (val->count)
|
|---|
| 64 | );
|
|---|
| 65 | #else
|
|---|
| 66 | asm volatile (
|
|---|
| 67 | "decl %[count]\n"
|
|---|
| 68 | : [count] "+m" (val->count)
|
|---|
| 69 | );
|
|---|
| 70 | #endif /* CONFIG_SMP */
|
|---|
| 71 | }
|
|---|
| 72 |
|
|---|
| 73 | NO_TRACE static inline atomic_count_t atomic_postinc(atomic_t *val)
|
|---|
| 74 | {
|
|---|
| 75 | atomic_count_t r = 1;
|
|---|
| 76 |
|
|---|
| 77 | asm volatile (
|
|---|
| 78 | "lock xaddl %[r], %[count]\n"
|
|---|
| 79 | : [count] "+m" (val->count),
|
|---|
| 80 | [r] "+r" (r)
|
|---|
| 81 | );
|
|---|
| 82 |
|
|---|
| 83 | return r;
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | NO_TRACE static inline atomic_count_t atomic_postdec(atomic_t *val)
|
|---|
| 87 | {
|
|---|
| 88 | atomic_count_t r = -1;
|
|---|
| 89 |
|
|---|
| 90 | asm volatile (
|
|---|
| 91 | "lock xaddl %[r], %[count]\n"
|
|---|
| 92 | : [count] "+m" (val->count),
|
|---|
| 93 | [r] "+r" (r)
|
|---|
| 94 | );
|
|---|
| 95 |
|
|---|
| 96 | return r;
|
|---|
| 97 | }
|
|---|
| 98 |
|
|---|
| 99 | #define atomic_preinc(val) (atomic_postinc(val) + 1)
|
|---|
| 100 | #define atomic_predec(val) (atomic_postdec(val) - 1)
|
|---|
| 101 |
|
|---|
| 102 | NO_TRACE static inline atomic_count_t test_and_set(atomic_t *val)
|
|---|
| 103 | {
|
|---|
| 104 | atomic_count_t v = 1;
|
|---|
| 105 |
|
|---|
| 106 | asm volatile (
|
|---|
| 107 | "xchgl %[v], %[count]\n"
|
|---|
| 108 | : [v] "+r" (v),
|
|---|
| 109 | [count] "+m" (val->count)
|
|---|
| 110 | );
|
|---|
| 111 |
|
|---|
| 112 | return v;
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 |
|
|---|
| 116 | /** ia32 specific fast spinlock */
|
|---|
| 117 | NO_TRACE static inline void atomic_lock_arch(atomic_t *val)
|
|---|
| 118 | {
|
|---|
| 119 | atomic_count_t tmp;
|
|---|
| 120 |
|
|---|
| 121 | preemption_disable();
|
|---|
| 122 | asm volatile (
|
|---|
| 123 | "0:\n"
|
|---|
| 124 | #ifndef PROCESSOR_i486
|
|---|
| 125 | "pause\n" /* Pentium 4's HT love this instruction */
|
|---|
| 126 | #endif
|
|---|
| 127 | "mov %[count], %[tmp]\n"
|
|---|
| 128 | "testl %[tmp], %[tmp]\n"
|
|---|
| 129 | "jnz 0b\n" /* lightweight looping on locked spinlock */
|
|---|
| 130 |
|
|---|
| 131 | "incl %[tmp]\n" /* now use the atomic operation */
|
|---|
| 132 | "xchgl %[count], %[tmp]\n"
|
|---|
| 133 | "testl %[tmp], %[tmp]\n"
|
|---|
| 134 | "jnz 0b\n"
|
|---|
| 135 | : [count] "+m" (val->count),
|
|---|
| 136 | [tmp] "=&r" (tmp)
|
|---|
| 137 | );
|
|---|
| 138 |
|
|---|
| 139 | /*
|
|---|
| 140 | * Prevent critical section code from bleeding out this way up.
|
|---|
| 141 | */
|
|---|
| 142 | CS_ENTER_BARRIER();
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 |
|
|---|
| 146 | #define _atomic_cas_impl(pptr, exp_val, new_val, old_val, prefix) \
|
|---|
| 147 | ({ \
|
|---|
| 148 | switch (sizeof(typeof(*(pptr)))) { \
|
|---|
| 149 | case 1: \
|
|---|
| 150 | asm volatile ( \
|
|---|
| 151 | prefix " cmpxchgb %[newval], %[ptr]\n" \
|
|---|
| 152 | : /* Output operands. */ \
|
|---|
| 153 | /* Old/current value is returned in eax. */ \
|
|---|
| 154 | [oldval] "=a" (old_val), \
|
|---|
| 155 | /* (*ptr) will be read and written to, hence "+" */ \
|
|---|
| 156 | [ptr] "+m" (*pptr) \
|
|---|
| 157 | : /* Input operands. */ \
|
|---|
| 158 | /* Expected value must be in eax. */ \
|
|---|
| 159 | [expval] "a" (exp_val), \
|
|---|
| 160 | /* The new value may be in any register. */ \
|
|---|
| 161 | [newval] "r" (new_val) \
|
|---|
| 162 | : "memory" \
|
|---|
| 163 | ); \
|
|---|
| 164 | break; \
|
|---|
| 165 | case 2: \
|
|---|
| 166 | asm volatile ( \
|
|---|
| 167 | prefix " cmpxchgw %[newval], %[ptr]\n" \
|
|---|
| 168 | : /* Output operands. */ \
|
|---|
| 169 | /* Old/current value is returned in eax. */ \
|
|---|
| 170 | [oldval] "=a" (old_val), \
|
|---|
| 171 | /* (*ptr) will be read and written to, hence "+" */ \
|
|---|
| 172 | [ptr] "+m" (*pptr) \
|
|---|
| 173 | : /* Input operands. */ \
|
|---|
| 174 | /* Expected value must be in eax. */ \
|
|---|
| 175 | [expval] "a" (exp_val), \
|
|---|
| 176 | /* The new value may be in any register. */ \
|
|---|
| 177 | [newval] "r" (new_val) \
|
|---|
| 178 | : "memory" \
|
|---|
| 179 | ); \
|
|---|
| 180 | break; \
|
|---|
| 181 | case 4: \
|
|---|
| 182 | asm volatile ( \
|
|---|
| 183 | prefix " cmpxchgl %[newval], %[ptr]\n" \
|
|---|
| 184 | : /* Output operands. */ \
|
|---|
| 185 | /* Old/current value is returned in eax. */ \
|
|---|
| 186 | [oldval] "=a" (old_val), \
|
|---|
| 187 | /* (*ptr) will be read and written to, hence "+" */ \
|
|---|
| 188 | [ptr] "+m" (*pptr) \
|
|---|
| 189 | : /* Input operands. */ \
|
|---|
| 190 | /* Expected value must be in eax. */ \
|
|---|
| 191 | [expval] "a" (exp_val), \
|
|---|
| 192 | /* The new value may be in any register. */ \
|
|---|
| 193 | [newval] "r" (new_val) \
|
|---|
| 194 | : "memory" \
|
|---|
| 195 | ); \
|
|---|
| 196 | break; \
|
|---|
| 197 | } \
|
|---|
| 198 | })
|
|---|
| 199 |
|
|---|
| 200 |
|
|---|
| 201 | #ifndef local_atomic_cas
|
|---|
| 202 |
|
|---|
| 203 | #define local_atomic_cas(pptr, exp_val, new_val) \
|
|---|
| 204 | ({ \
|
|---|
| 205 | typeof(*(pptr)) old_val; \
|
|---|
| 206 | _atomic_cas_impl(pptr, exp_val, new_val, old_val, ""); \
|
|---|
| 207 | \
|
|---|
| 208 | old_val; \
|
|---|
| 209 | })
|
|---|
| 210 |
|
|---|
| 211 | #else
|
|---|
| 212 | /* Check if arch/atomic.h does not accidentally include /atomic.h .*/
|
|---|
| 213 | #error Architecture specific cpu local atomics already defined! Check your includes.
|
|---|
| 214 | #endif
|
|---|
| 215 |
|
|---|
| 216 |
|
|---|
| 217 | #ifndef local_atomic_exchange
|
|---|
| 218 | /*
|
|---|
| 219 | * Issuing a xchg instruction always implies lock prefix semantics.
|
|---|
| 220 | * Therefore, it is cheaper to use a cmpxchg without a lock prefix
|
|---|
| 221 | * in a loop.
|
|---|
| 222 | */
|
|---|
| 223 | #define local_atomic_exchange(pptr, new_val) \
|
|---|
| 224 | ({ \
|
|---|
| 225 | typeof(*(pptr)) exp_val; \
|
|---|
| 226 | typeof(*(pptr)) old_val; \
|
|---|
| 227 | \
|
|---|
| 228 | do { \
|
|---|
| 229 | exp_val = *pptr; \
|
|---|
| 230 | old_val = local_atomic_cas(pptr, exp_val, new_val); \
|
|---|
| 231 | } while (old_val != exp_val); \
|
|---|
| 232 | \
|
|---|
| 233 | old_val; \
|
|---|
| 234 | })
|
|---|
| 235 |
|
|---|
| 236 | #else
|
|---|
| 237 | /* Check if arch/atomic.h does not accidentally include /atomic.h .*/
|
|---|
| 238 | #error Architecture specific cpu local atomics already defined! Check your includes.
|
|---|
| 239 | #endif
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 | #endif
|
|---|
| 243 |
|
|---|
| 244 | /** @}
|
|---|
| 245 | */
|
|---|