[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 |
|
---|
[cc73a8a1] | 29 | /** @addtogroup sync
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
[cf26ba9] | 33 | /**
|
---|
[b45c443] | 34 | * @file
|
---|
[b60c582] | 35 | * @brief Spinlocks.
|
---|
[cf26ba9] | 36 | */
|
---|
[b60c582] | 37 |
|
---|
[5e04b48d] | 38 | #include <synch/spinlock.h>
|
---|
[23684b7] | 39 | #include <atomic.h>
|
---|
[7dd56f1] | 40 | #include <arch/barrier.h>
|
---|
[5e04b48d] | 41 | #include <arch.h>
|
---|
[c842f04] | 42 | #include <preemption.h>
|
---|
[9c0a9b3] | 43 | #include <print.h>
|
---|
[5e04b48d] | 44 | #include <debug.h>
|
---|
[2d93f1f9] | 45 | #include <symtab.h>
|
---|
[311929ec] | 46 | #include <stacktrace.h>
|
---|
[1066041] | 47 | #include <cpu.h>
|
---|
[f761f1eb] | 48 |
|
---|
[5f85c91] | 49 | #ifdef CONFIG_SMP
|
---|
[f761f1eb] | 50 |
|
---|
[5e04b48d] | 51 | /** Initialize spinlock
|
---|
| 52 | *
|
---|
| 53 | * @param sl Pointer to spinlock_t structure.
|
---|
[90c8b8d] | 54 | *
|
---|
[5e04b48d] | 55 | */
|
---|
[a000878c] | 56 | void spinlock_initialize(spinlock_t *lock, const char *name)
|
---|
[f761f1eb] | 57 | {
|
---|
[90c8b8d] | 58 | atomic_set(&lock->val, 0);
|
---|
[2d93f1f9] | 59 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
[90c8b8d] | 60 | lock->name = name;
|
---|
| 61 | #endif
|
---|
[f761f1eb] | 62 | }
|
---|
| 63 |
|
---|
[90c8b8d] | 64 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
| 65 |
|
---|
[5e04b48d] | 66 | /** Lock spinlock
|
---|
| 67 | *
|
---|
| 68 | * Lock spinlock.
|
---|
| 69 | * This version has limitted ability to report
|
---|
| 70 | * possible occurence of deadlock.
|
---|
| 71 | *
|
---|
[90c8b8d] | 72 | * @param lock Pointer to spinlock_t structure.
|
---|
| 73 | *
|
---|
[5e04b48d] | 74 | */
|
---|
[90c8b8d] | 75 | void spinlock_lock_debug(spinlock_t *lock)
|
---|
[f761f1eb] | 76 | {
|
---|
[98000fb] | 77 | size_t i = 0;
|
---|
[05e2a7ad] | 78 | bool deadlock_reported = false;
|
---|
[90c8b8d] | 79 |
|
---|
[c842f04] | 80 | preemption_disable();
|
---|
[90c8b8d] | 81 | while (test_and_set(&lock->val)) {
|
---|
[d9cf9d5f] | 82 | /*
|
---|
[90c8b8d] | 83 | * We need to be careful about particular locks
|
---|
| 84 | * which are directly used to report deadlocks
|
---|
| 85 | * via printf() (and recursively other functions).
|
---|
| 86 | * This conserns especially printf_lock and the
|
---|
| 87 | * framebuffer lock.
|
---|
[c263c77] | 88 | *
|
---|
| 89 | * Any lock whose name is prefixed by "*" will be
|
---|
| 90 | * ignored by this deadlock detection routine
|
---|
| 91 | * as this might cause an infinite recursion.
|
---|
| 92 | * We trust our code that there is no possible deadlock
|
---|
| 93 | * caused by these locks (except when an exception
|
---|
| 94 | * is triggered for instance by printf()).
|
---|
| 95 | *
|
---|
| 96 | * We encountered false positives caused by very
|
---|
| 97 | * slow framebuffer interaction (especially when
|
---|
| 98 | * run in a simulator) that caused problems with both
|
---|
| 99 | * printf_lock and the framebuffer lock.
|
---|
[d9cf9d5f] | 100 | */
|
---|
[c263c77] | 101 | if (lock->name[0] == '*')
|
---|
| 102 | continue;
|
---|
| 103 |
|
---|
[14df080] | 104 | if (i++ > DEADLOCK_THRESHOLD) {
|
---|
[7e752b2] | 105 | printf("cpu%u: looping on spinlock %p:%s, "
|
---|
| 106 | "caller=%p (%s)\n", CPU->id, lock, lock->name,
|
---|
| 107 | (void *) CALLER, symtab_fmt_name_lookup(CALLER));
|
---|
[311929ec] | 108 | stack_trace();
|
---|
[44814b8] | 109 |
|
---|
[f761f1eb] | 110 | i = 0;
|
---|
[05e2a7ad] | 111 | deadlock_reported = true;
|
---|
[f761f1eb] | 112 | }
|
---|
| 113 | }
|
---|
[90c8b8d] | 114 |
|
---|
[05e2a7ad] | 115 | if (deadlock_reported)
|
---|
[8ed4014] | 116 | printf("cpu%u: not deadlocked\n", CPU->id);
|
---|
[90c8b8d] | 117 |
|
---|
[5e04b48d] | 118 | /*
|
---|
| 119 | * Prevent critical section code from bleeding out this way up.
|
---|
| 120 | */
|
---|
[7dd56f1] | 121 | CS_ENTER_BARRIER();
|
---|
[f761f1eb] | 122 | }
|
---|
[90c8b8d] | 123 |
|
---|
[13108f24] | 124 | /** Unlock spinlock
|
---|
| 125 | *
|
---|
| 126 | * Unlock spinlock.
|
---|
| 127 | *
|
---|
| 128 | * @param sl Pointer to spinlock_t structure.
|
---|
| 129 | */
|
---|
| 130 | void spinlock_unlock_debug(spinlock_t *lock)
|
---|
| 131 | {
|
---|
[ffe4a87] | 132 | ASSERT_SPINLOCK(spinlock_locked(lock), lock);
|
---|
[13108f24] | 133 |
|
---|
| 134 | /*
|
---|
| 135 | * Prevent critical section code from bleeding out this way down.
|
---|
| 136 | */
|
---|
| 137 | CS_LEAVE_BARRIER();
|
---|
| 138 |
|
---|
| 139 | atomic_set(&lock->val, 0);
|
---|
| 140 | preemption_enable();
|
---|
| 141 | }
|
---|
| 142 |
|
---|
[f761f1eb] | 143 | #endif
|
---|
| 144 |
|
---|
[5e04b48d] | 145 | /** Lock spinlock conditionally
|
---|
| 146 | *
|
---|
[2b4a9f26] | 147 | * Lock spinlock conditionally. If the spinlock is not available
|
---|
| 148 | * at the moment, signal failure.
|
---|
[5e04b48d] | 149 | *
|
---|
[90c8b8d] | 150 | * @param lock Pointer to spinlock_t structure.
|
---|
[5e04b48d] | 151 | *
|
---|
| 152 | * @return Zero on failure, non-zero otherwise.
|
---|
[90c8b8d] | 153 | *
|
---|
[5e04b48d] | 154 | */
|
---|
[90c8b8d] | 155 | int spinlock_trylock(spinlock_t *lock)
|
---|
[f761f1eb] | 156 | {
|
---|
[c842f04] | 157 | preemption_disable();
|
---|
[90c8b8d] | 158 | int rc = !test_and_set(&lock->val);
|
---|
| 159 |
|
---|
[5e04b48d] | 160 | /*
|
---|
| 161 | * Prevent critical section code from bleeding out this way up.
|
---|
| 162 | */
|
---|
[7dd56f1] | 163 | CS_ENTER_BARRIER();
|
---|
[90c8b8d] | 164 |
|
---|
[c842f04] | 165 | if (!rc)
|
---|
| 166 | preemption_enable();
|
---|
[7dd56f1] | 167 |
|
---|
| 168 | return rc;
|
---|
[f761f1eb] | 169 | }
|
---|
| 170 |
|
---|
[ffe4a87] | 171 | /** Find out whether the spinlock is currently locked.
|
---|
| 172 | *
|
---|
| 173 | * @param lock Spinlock.
|
---|
| 174 | * @return True if the spinlock is locked, false otherwise.
|
---|
| 175 | */
|
---|
| 176 | bool spinlock_locked(spinlock_t *lock)
|
---|
| 177 | {
|
---|
| 178 | return atomic_get(&lock->val) != 0;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[f761f1eb] | 181 | #endif
|
---|
[b45c443] | 182 |
|
---|
[a9f1372] | 183 | /** Initialize interrupts-disabled spinlock
|
---|
| 184 | *
|
---|
| 185 | * @param lock IRQ spinlock to be initialized.
|
---|
| 186 | * @param name IRQ spinlock name.
|
---|
| 187 | *
|
---|
| 188 | */
|
---|
| 189 | void irq_spinlock_initialize(irq_spinlock_t *lock, const char *name)
|
---|
| 190 | {
|
---|
| 191 | spinlock_initialize(&(lock->lock), name);
|
---|
| 192 | lock->guard = false;
|
---|
| 193 | lock->ipl = 0;
|
---|
| 194 | }
|
---|
| 195 |
|
---|
| 196 | /** Lock interrupts-disabled spinlock
|
---|
| 197 | *
|
---|
| 198 | * Lock a spinlock which requires disabled interrupts.
|
---|
| 199 | *
|
---|
| 200 | * @param lock IRQ spinlock to be locked.
|
---|
[9fe9d296] | 201 | * @param irq_dis If true, disables interrupts before locking the spinlock.
|
---|
| 202 | * If false, interrupts are expected to be already disabled.
|
---|
[a9f1372] | 203 | *
|
---|
| 204 | */
|
---|
| 205 | void irq_spinlock_lock(irq_spinlock_t *lock, bool irq_dis)
|
---|
| 206 | {
|
---|
| 207 | if (irq_dis) {
|
---|
| 208 | ipl_t ipl = interrupts_disable();
|
---|
| 209 | spinlock_lock(&(lock->lock));
|
---|
| 210 |
|
---|
| 211 | lock->guard = true;
|
---|
| 212 | lock->ipl = ipl;
|
---|
| 213 | } else {
|
---|
| 214 | ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
|
---|
| 215 |
|
---|
| 216 | spinlock_lock(&(lock->lock));
|
---|
| 217 | ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
|
---|
| 218 | }
|
---|
| 219 | }
|
---|
| 220 |
|
---|
| 221 | /** Unlock interrupts-disabled spinlock
|
---|
| 222 | *
|
---|
| 223 | * Unlock a spinlock which requires disabled interrupts.
|
---|
| 224 | *
|
---|
| 225 | * @param lock IRQ spinlock to be unlocked.
|
---|
| 226 | * @param irq_res If true, interrupts are restored to previously
|
---|
| 227 | * saved interrupt level.
|
---|
| 228 | *
|
---|
| 229 | */
|
---|
| 230 | void irq_spinlock_unlock(irq_spinlock_t *lock, bool irq_res)
|
---|
| 231 | {
|
---|
| 232 | ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
|
---|
| 233 |
|
---|
| 234 | if (irq_res) {
|
---|
| 235 | ASSERT_IRQ_SPINLOCK(lock->guard, lock);
|
---|
| 236 |
|
---|
| 237 | lock->guard = false;
|
---|
| 238 | ipl_t ipl = lock->ipl;
|
---|
| 239 |
|
---|
| 240 | spinlock_unlock(&(lock->lock));
|
---|
| 241 | interrupts_restore(ipl);
|
---|
| 242 | } else {
|
---|
| 243 | ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
|
---|
| 244 | spinlock_unlock(&(lock->lock));
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | /** Lock interrupts-disabled spinlock
|
---|
| 249 | *
|
---|
| 250 | * Lock an interrupts-disabled spinlock conditionally. If the
|
---|
| 251 | * spinlock is not available at the moment, signal failure.
|
---|
| 252 | * Interrupts are expected to be already disabled.
|
---|
| 253 | *
|
---|
| 254 | * @param lock IRQ spinlock to be locked conditionally.
|
---|
| 255 | *
|
---|
| 256 | * @return Zero on failure, non-zero otherwise.
|
---|
| 257 | *
|
---|
| 258 | */
|
---|
| 259 | int irq_spinlock_trylock(irq_spinlock_t *lock)
|
---|
| 260 | {
|
---|
| 261 | ASSERT_IRQ_SPINLOCK(interrupts_disabled(), lock);
|
---|
| 262 | int rc = spinlock_trylock(&(lock->lock));
|
---|
| 263 |
|
---|
[4e5dabf] | 264 | ASSERT_IRQ_SPINLOCK((!rc) || (!lock->guard), lock);
|
---|
[a9f1372] | 265 | return rc;
|
---|
| 266 | }
|
---|
| 267 |
|
---|
| 268 | /** Pass lock from one interrupts-disabled spinlock to another
|
---|
| 269 | *
|
---|
| 270 | * Pass lock from one IRQ spinlock to another IRQ spinlock
|
---|
| 271 | * without enabling interrupts during the process.
|
---|
| 272 | *
|
---|
| 273 | * The first IRQ spinlock is supposed to be locked.
|
---|
| 274 | *
|
---|
| 275 | * @param unlock IRQ spinlock to be unlocked.
|
---|
| 276 | * @param lock IRQ spinlock to be locked.
|
---|
| 277 | *
|
---|
| 278 | */
|
---|
| 279 | void irq_spinlock_pass(irq_spinlock_t *unlock, irq_spinlock_t *lock)
|
---|
| 280 | {
|
---|
| 281 | ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
|
---|
| 282 |
|
---|
| 283 | /* Pass guard from unlock to lock */
|
---|
| 284 | bool guard = unlock->guard;
|
---|
| 285 | ipl_t ipl = unlock->ipl;
|
---|
| 286 | unlock->guard = false;
|
---|
| 287 |
|
---|
| 288 | spinlock_unlock(&(unlock->lock));
|
---|
| 289 | spinlock_lock(&(lock->lock));
|
---|
| 290 |
|
---|
| 291 | ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
|
---|
| 292 |
|
---|
| 293 | if (guard) {
|
---|
| 294 | lock->guard = true;
|
---|
| 295 | lock->ipl = ipl;
|
---|
| 296 | }
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | /** Hand-over-hand locking of interrupts-disabled spinlocks
|
---|
| 300 | *
|
---|
| 301 | * Implement hand-over-hand locking between two interrupts-disabled
|
---|
| 302 | * spinlocks without enabling interrupts during the process.
|
---|
| 303 | *
|
---|
| 304 | * The first IRQ spinlock is supposed to be locked.
|
---|
| 305 | *
|
---|
| 306 | * @param unlock IRQ spinlock to be unlocked.
|
---|
| 307 | * @param lock IRQ spinlock to be locked.
|
---|
| 308 | *
|
---|
| 309 | */
|
---|
| 310 | void irq_spinlock_exchange(irq_spinlock_t *unlock, irq_spinlock_t *lock)
|
---|
| 311 | {
|
---|
| 312 | ASSERT_IRQ_SPINLOCK(interrupts_disabled(), unlock);
|
---|
| 313 |
|
---|
| 314 | spinlock_lock(&(lock->lock));
|
---|
| 315 | ASSERT_IRQ_SPINLOCK(!lock->guard, lock);
|
---|
| 316 |
|
---|
| 317 | /* Pass guard from unlock to lock */
|
---|
| 318 | if (unlock->guard) {
|
---|
| 319 | lock->guard = true;
|
---|
| 320 | lock->ipl = unlock->ipl;
|
---|
| 321 | unlock->guard = false;
|
---|
| 322 | }
|
---|
| 323 |
|
---|
| 324 | spinlock_unlock(&(unlock->lock));
|
---|
| 325 | }
|
---|
| 326 |
|
---|
[ffe4a87] | 327 | /** Find out whether the IRQ spinlock is currently locked.
|
---|
| 328 | *
|
---|
| 329 | * @param lock IRQ spinlock.
|
---|
| 330 | * @return True if the IRQ spinlock is locked, false otherwise.
|
---|
| 331 | */
|
---|
| 332 | bool irq_spinlock_locked(irq_spinlock_t *ilock)
|
---|
| 333 | {
|
---|
| 334 | return spinlock_locked(&ilock->lock);
|
---|
| 335 | }
|
---|
| 336 |
|
---|
[cc73a8a1] | 337 | /** @}
|
---|
[b45c443] | 338 | */
|
---|