[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f43d8ce] | 3 | * Copyright (c) 2023 Jiří Zárevúcky
|
---|
[f761f1eb] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 28 | */
|
---|
| 29 |
|
---|
[e88eb48] | 30 | /** @addtogroup kernel_sync
|
---|
[b45c443] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[cf26ba9] | 34 | /**
|
---|
[b45c443] | 35 | * @file
|
---|
[b60c582] | 36 | * @brief Spinlocks.
|
---|
[cf26ba9] | 37 | */
|
---|
[b60c582] | 38 |
|
---|
[4777e02] | 39 | #ifdef CONFIG_SMP
|
---|
| 40 |
|
---|
[2b264c4] | 41 | #include <arch/asm.h>
|
---|
[5e04b48d] | 42 | #include <synch/spinlock.h>
|
---|
[23684b7] | 43 | #include <atomic.h>
|
---|
[05882233] | 44 | #include <barrier.h>
|
---|
[5e04b48d] | 45 | #include <arch.h>
|
---|
[c842f04] | 46 | #include <preemption.h>
|
---|
[bab75df6] | 47 | #include <stdio.h>
|
---|
[5e04b48d] | 48 | #include <debug.h>
|
---|
[2d93f1f9] | 49 | #include <symtab.h>
|
---|
[311929ec] | 50 | #include <stacktrace.h>
|
---|
[1066041] | 51 | #include <cpu.h>
|
---|
[f761f1eb] | 52 |
|
---|
[5e04b48d] | 53 | /** Initialize spinlock
|
---|
| 54 | *
|
---|
| 55 | * @param sl Pointer to spinlock_t structure.
|
---|
[90c8b8d] | 56 | *
|
---|
[5e04b48d] | 57 | */
|
---|
[a000878c] | 58 | void spinlock_initialize(spinlock_t *lock, const char *name)
|
---|
[f761f1eb] | 59 | {
|
---|
[78de83de] | 60 | atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
|
---|
[2d93f1f9] | 61 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
[90c8b8d] | 62 | lock->name = name;
|
---|
| 63 | #endif
|
---|
[f761f1eb] | 64 | }
|
---|
| 65 |
|
---|
[5e04b48d] | 66 | /** Lock spinlock
|
---|
| 67 | *
|
---|
[90c8b8d] | 68 | * @param lock Pointer to spinlock_t structure.
|
---|
| 69 | *
|
---|
[5e04b48d] | 70 | */
|
---|
[f43d8ce] | 71 | void spinlock_lock(spinlock_t *lock)
|
---|
[f761f1eb] | 72 | {
|
---|
[f43d8ce] | 73 | preemption_disable();
|
---|
| 74 |
|
---|
[05e2a7ad] | 75 | bool deadlock_reported = false;
|
---|
[f43d8ce] | 76 | size_t i = 0;
|
---|
[a35b458] | 77 |
|
---|
[78de83de] | 78 | while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) {
|
---|
[8addb24a] | 79 | cpu_spin_hint();
|
---|
[2b264c4] | 80 |
|
---|
[f43d8ce] | 81 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
[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).
|
---|
[c55ab66] | 86 | * This concerns especially printf_lock and the
|
---|
[90c8b8d] | 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;
|
---|
[a35b458] | 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();
|
---|
[a35b458] | 109 |
|
---|
[f761f1eb] | 110 | i = 0;
|
---|
[05e2a7ad] | 111 | deadlock_reported = true;
|
---|
[f761f1eb] | 112 | }
|
---|
[f43d8ce] | 113 | #endif
|
---|
[f761f1eb] | 114 | }
|
---|
[a35b458] | 115 |
|
---|
[f43d8ce] | 116 | /* Avoid compiler warning with debug disabled. */
|
---|
| 117 | (void) i;
|
---|
| 118 |
|
---|
[05e2a7ad] | 119 | if (deadlock_reported)
|
---|
[8ed4014] | 120 | printf("cpu%u: not deadlocked\n", CPU->id);
|
---|
[f761f1eb] | 121 | }
|
---|
[90c8b8d] | 122 |
|
---|
[13108f24] | 123 | /** Unlock spinlock
|
---|
| 124 | *
|
---|
| 125 | * @param sl Pointer to spinlock_t structure.
|
---|
| 126 | */
|
---|
[f43d8ce] | 127 | void spinlock_unlock(spinlock_t *lock)
|
---|
[13108f24] | 128 | {
|
---|
[f43d8ce] | 129 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
[ffe4a87] | 130 | ASSERT_SPINLOCK(spinlock_locked(lock), lock);
|
---|
[f43d8ce] | 131 | #endif
|
---|
[a35b458] | 132 |
|
---|
[78de83de] | 133 | atomic_flag_clear_explicit(&lock->flag, memory_order_release);
|
---|
[13108f24] | 134 | preemption_enable();
|
---|
| 135 | }
|
---|
| 136 |
|
---|
[f43d8ce] | 137 | /**
|
---|
[2b4a9f26] | 138 | * Lock spinlock conditionally. If the spinlock is not available
|
---|
| 139 | * at the moment, signal failure.
|
---|
[5e04b48d] | 140 | *
|
---|
[90c8b8d] | 141 | * @param lock Pointer to spinlock_t structure.
|
---|
[5e04b48d] | 142 | *
|
---|
[f43d8ce] | 143 | * @return true on success.
|
---|
[90c8b8d] | 144 | *
|
---|
[5e04b48d] | 145 | */
|
---|
[89ea2dc] | 146 | bool spinlock_trylock(spinlock_t *lock)
|
---|
[f761f1eb] | 147 | {
|
---|
[c842f04] | 148 | preemption_disable();
|
---|
[f43d8ce] | 149 |
|
---|
[78de83de] | 150 | bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire);
|
---|
[a35b458] | 151 |
|
---|
[89ea2dc] | 152 | if (!ret)
|
---|
[c842f04] | 153 | preemption_enable();
|
---|
[a35b458] | 154 |
|
---|
[89ea2dc] | 155 | return ret;
|
---|
[f761f1eb] | 156 | }
|
---|
| 157 |
|
---|
[ffe4a87] | 158 | /** Find out whether the spinlock is currently locked.
|
---|
| 159 | *
|
---|
| 160 | * @param lock Spinlock.
|
---|
| 161 | * @return True if the spinlock is locked, false otherwise.
|
---|
| 162 | */
|
---|
| 163 | bool spinlock_locked(spinlock_t *lock)
|
---|
| 164 | {
|
---|
[95d45482] | 165 | // NOTE: Atomic flag doesn't support simple atomic read (by design),
|
---|
| 166 | // so instead we test_and_set and then clear if necessary.
|
---|
| 167 | // This function is only used inside assert, so we don't need
|
---|
| 168 | // any preemption_disable/enable here.
|
---|
[78de83de] | 169 |
|
---|
| 170 | bool ret = atomic_flag_test_and_set_explicit(&lock->flag, memory_order_relaxed);
|
---|
| 171 | if (!ret)
|
---|
| 172 | atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
|
---|
| 173 | return ret;
|
---|
[f43d8ce] | 174 | }
|
---|
[b45c443] | 175 |
|
---|
[4777e02] | 176 | #endif /* CONFIG_SMP */
|
---|
| 177 |
|
---|
[cc73a8a1] | 178 | /** @}
|
---|
[b45c443] | 179 | */
|
---|