[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 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 |
|
---|
[add04f7] | 29 | /** @addtogroup ia32
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_ia32_ATOMIC_H_
|
---|
| 36 | #define KERN_ia32_ATOMIC_H_
|
---|
[f761f1eb] | 37 |
|
---|
[d99c1d2] | 38 | #include <typedefs.h>
|
---|
[53f9821] | 39 | #include <arch/barrier.h>
|
---|
| 40 | #include <preemption.h>
|
---|
[7a0359b] | 41 | #include <trace.h>
|
---|
[59e07c91] | 42 |
|
---|
[7a0359b] | 43 | NO_TRACE static inline void atomic_inc(atomic_t *val)
|
---|
[228666c] | 44 | {
|
---|
[5f85c91] | 45 | #ifdef CONFIG_SMP
|
---|
[add04f7] | 46 | asm volatile (
|
---|
| 47 | "lock incl %[count]\n"
|
---|
| 48 | : [count] "+m" (val->count)
|
---|
| 49 | );
|
---|
[18e0a6c] | 50 | #else
|
---|
[add04f7] | 51 | asm volatile (
|
---|
| 52 | "incl %[count]\n"
|
---|
| 53 | : [count] "+m" (val->count)
|
---|
| 54 | );
|
---|
[5f85c91] | 55 | #endif /* CONFIG_SMP */
|
---|
[18e0a6c] | 56 | }
|
---|
| 57 |
|
---|
[7a0359b] | 58 | NO_TRACE static inline void atomic_dec(atomic_t *val)
|
---|
[228666c] | 59 | {
|
---|
[5f85c91] | 60 | #ifdef CONFIG_SMP
|
---|
[add04f7] | 61 | asm volatile (
|
---|
| 62 | "lock decl %[count]\n"
|
---|
| 63 | : [count] "+m" (val->count)
|
---|
| 64 | );
|
---|
[18e0a6c] | 65 | #else
|
---|
[add04f7] | 66 | asm volatile (
|
---|
| 67 | "decl %[count]\n"
|
---|
[f36c061] | 68 | : [count] "+m" (val->count)
|
---|
[add04f7] | 69 | );
|
---|
[5f85c91] | 70 | #endif /* CONFIG_SMP */
|
---|
[18e0a6c] | 71 | }
|
---|
| 72 |
|
---|
[7a0359b] | 73 | NO_TRACE static inline atomic_count_t atomic_postinc(atomic_t *val)
|
---|
[73a4bab] | 74 | {
|
---|
[228666c] | 75 | atomic_count_t r = 1;
|
---|
[add04f7] | 76 |
|
---|
[e7b7be3f] | 77 | asm volatile (
|
---|
[add04f7] | 78 | "lock xaddl %[r], %[count]\n"
|
---|
[228666c] | 79 | : [count] "+m" (val->count),
|
---|
| 80 | [r] "+r" (r)
|
---|
[73a4bab] | 81 | );
|
---|
[add04f7] | 82 |
|
---|
[73a4bab] | 83 | return r;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[7a0359b] | 86 | NO_TRACE static inline atomic_count_t atomic_postdec(atomic_t *val)
|
---|
[73a4bab] | 87 | {
|
---|
[228666c] | 88 | atomic_count_t r = -1;
|
---|
[10c071e] | 89 |
|
---|
[e7b7be3f] | 90 | asm volatile (
|
---|
[add04f7] | 91 | "lock xaddl %[r], %[count]\n"
|
---|
[228666c] | 92 | : [count] "+m" (val->count),
|
---|
| 93 | [r] "+r" (r)
|
---|
[73a4bab] | 94 | );
|
---|
[10c071e] | 95 |
|
---|
[73a4bab] | 96 | return r;
|
---|
| 97 | }
|
---|
| 98 |
|
---|
[add04f7] | 99 | #define atomic_preinc(val) (atomic_postinc(val) + 1)
|
---|
| 100 | #define atomic_predec(val) (atomic_postdec(val) - 1)
|
---|
[73a4bab] | 101 |
|
---|
[7a0359b] | 102 | NO_TRACE static inline atomic_count_t test_and_set(atomic_t *val)
|
---|
[228666c] | 103 | {
|
---|
[ba371e1] | 104 | atomic_count_t v = 1;
|
---|
[18e0a6c] | 105 |
|
---|
[e7b7be3f] | 106 | asm volatile (
|
---|
[add04f7] | 107 | "xchgl %[v], %[count]\n"
|
---|
[ba371e1] | 108 | : [v] "+r" (v),
|
---|
[228666c] | 109 | [count] "+m" (val->count)
|
---|
[18e0a6c] | 110 | );
|
---|
| 111 |
|
---|
| 112 | return v;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[23684b7] | 115 | /** ia32 specific fast spinlock */
|
---|
[7a0359b] | 116 | NO_TRACE static inline void atomic_lock_arch(atomic_t *val)
|
---|
[53f9821] | 117 | {
|
---|
[228666c] | 118 | atomic_count_t tmp;
|
---|
[add04f7] | 119 |
|
---|
[53f9821] | 120 | preemption_disable();
|
---|
[e7b7be3f] | 121 | asm volatile (
|
---|
[9f491d7] | 122 | "0:\n"
|
---|
[8c15255] | 123 | #ifndef PROCESSOR_i486
|
---|
[add04f7] | 124 | "pause\n" /* Pentium 4's HT love this instruction */
|
---|
[8c15255] | 125 | #endif
|
---|
[add04f7] | 126 | "mov %[count], %[tmp]\n"
|
---|
| 127 | "testl %[tmp], %[tmp]\n"
|
---|
[9f491d7] | 128 | "jnz 0b\n" /* lightweight looping on locked spinlock */
|
---|
[53f9821] | 129 |
|
---|
[add04f7] | 130 | "incl %[tmp]\n" /* now use the atomic operation */
|
---|
| 131 | "xchgl %[count], %[tmp]\n"
|
---|
| 132 | "testl %[tmp], %[tmp]\n"
|
---|
[9f491d7] | 133 | "jnz 0b\n"
|
---|
[228666c] | 134 | : [count] "+m" (val->count),
|
---|
| 135 | [tmp] "=&r" (tmp)
|
---|
[9f491d7] | 136 | );
|
---|
[228666c] | 137 |
|
---|
[53f9821] | 138 | /*
|
---|
| 139 | * Prevent critical section code from bleeding out this way up.
|
---|
| 140 | */
|
---|
| 141 | CS_ENTER_BARRIER();
|
---|
| 142 | }
|
---|
[f761f1eb] | 143 |
|
---|
| 144 | #endif
|
---|
[b45c443] | 145 |
|
---|
[06e1e95] | 146 | /** @}
|
---|
[b45c443] | 147 | */
|
---|