[f761f1eb] | 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 |
|
---|
[cf26ba9] | 29 | /**
|
---|
| 30 | * @file spinlock.c
|
---|
| 31 | * @brief Spinlocks.
|
---|
| 32 | */
|
---|
| 33 |
|
---|
[5e04b48d] | 34 | #include <synch/spinlock.h>
|
---|
[23684b7] | 35 | #include <atomic.h>
|
---|
[7dd56f1] | 36 | #include <arch/barrier.h>
|
---|
[5e04b48d] | 37 | #include <arch.h>
|
---|
[c842f04] | 38 | #include <preemption.h>
|
---|
[9c0a9b3] | 39 | #include <print.h>
|
---|
[5e04b48d] | 40 | #include <debug.h>
|
---|
[2d93f1f9] | 41 | #include <symtab.h>
|
---|
[f761f1eb] | 42 |
|
---|
[d9cf9d5f] | 43 | #ifdef CONFIG_FB
|
---|
| 44 | #include <genarch/fb/fb.h>
|
---|
| 45 | #endif
|
---|
| 46 |
|
---|
[5f85c91] | 47 | #ifdef CONFIG_SMP
|
---|
[f761f1eb] | 48 |
|
---|
[5e04b48d] | 49 | /** Initialize spinlock
|
---|
| 50 | *
|
---|
| 51 | * Initialize spinlock.
|
---|
| 52 | *
|
---|
| 53 | * @param sl Pointer to spinlock_t structure.
|
---|
| 54 | */
|
---|
[2d93f1f9] | 55 | void spinlock_initialize(spinlock_t *sl, char *name)
|
---|
[f761f1eb] | 56 | {
|
---|
[80d2bdb] | 57 | atomic_set(&sl->val, 0);
|
---|
[2d93f1f9] | 58 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
| 59 | sl->name = name;
|
---|
| 60 | #endif
|
---|
[f761f1eb] | 61 | }
|
---|
| 62 |
|
---|
[5e04b48d] | 63 | /** Lock spinlock
|
---|
| 64 | *
|
---|
| 65 | * Lock spinlock.
|
---|
| 66 | * This version has limitted ability to report
|
---|
| 67 | * possible occurence of deadlock.
|
---|
| 68 | *
|
---|
| 69 | * @param sl Pointer to spinlock_t structure.
|
---|
| 70 | */
|
---|
[53f9821] | 71 | #ifdef CONFIG_DEBUG_SPINLOCK
|
---|
[14df080] | 72 | #define DEADLOCK_THRESHOLD 100000000
|
---|
[53f9821] | 73 | void spinlock_lock_debug(spinlock_t *sl)
|
---|
[f761f1eb] | 74 | {
|
---|
[05e2a7ad] | 75 | count_t i = 0;
|
---|
[2d93f1f9] | 76 | char *symbol;
|
---|
[05e2a7ad] | 77 | bool deadlock_reported = false;
|
---|
[f761f1eb] | 78 |
|
---|
[c842f04] | 79 | preemption_disable();
|
---|
[f761f1eb] | 80 | while (test_and_set(&sl->val)) {
|
---|
[d9cf9d5f] | 81 |
|
---|
| 82 | /*
|
---|
| 83 | * We need to be careful about printflock and fb_lock.
|
---|
| 84 | * Both of them are used to report deadlocks via
|
---|
| 85 | * printf() and fb_putchar().
|
---|
| 86 | *
|
---|
| 87 | * We trust our code that there is no possible deadlock
|
---|
| 88 | * caused by these two locks (except when an exception
|
---|
| 89 | * is triggered for instance by printf() or fb_putchar()).
|
---|
| 90 | * However, we encountered false positives caused by very
|
---|
| 91 | * slow VESA framebuffer interaction (especially when
|
---|
| 92 | * run in a simulator) that caused problems with both
|
---|
| 93 | * printflock and fb_lock.
|
---|
| 94 | *
|
---|
| 95 | * Possible deadlocks on both printflock and fb_lock
|
---|
| 96 | * are therefore not reported as they would cause an
|
---|
| 97 | * infinite recursion.
|
---|
| 98 | */
|
---|
| 99 | if (sl == &printflock)
|
---|
| 100 | continue;
|
---|
| 101 | #ifdef CONFIG_FB
|
---|
| 102 | if (sl == &fb_lock)
|
---|
| 103 | continue;
|
---|
| 104 | #endif
|
---|
[14df080] | 105 | if (i++ > DEADLOCK_THRESHOLD) {
|
---|
[ad45bde9] | 106 | printf("cpu%d: looping on spinlock %.*p:%s, caller=%.*p",
|
---|
| 107 | CPU->id, sizeof(__address) * 2, sl, sl->name, sizeof(__address) * 2, CALLER);
|
---|
[3fc03fd] | 108 | symbol = get_symtab_entry(CALLER);
|
---|
[2d93f1f9] | 109 | if (symbol)
|
---|
| 110 | printf("(%s)", symbol);
|
---|
| 111 | printf("\n");
|
---|
[f761f1eb] | 112 | i = 0;
|
---|
[05e2a7ad] | 113 | deadlock_reported = true;
|
---|
[f761f1eb] | 114 | }
|
---|
| 115 | }
|
---|
[5e04b48d] | 116 |
|
---|
[05e2a7ad] | 117 | if (deadlock_reported)
|
---|
| 118 | printf("cpu%d: not deadlocked\n", CPU->id);
|
---|
| 119 |
|
---|
[5e04b48d] | 120 | /*
|
---|
| 121 | * Prevent critical section code from bleeding out this way up.
|
---|
| 122 | */
|
---|
[7dd56f1] | 123 | CS_ENTER_BARRIER();
|
---|
[f761f1eb] | 124 | }
|
---|
| 125 | #endif
|
---|
| 126 |
|
---|
[5e04b48d] | 127 | /** Lock spinlock conditionally
|
---|
| 128 | *
|
---|
| 129 | * Lock spinlock conditionally.
|
---|
| 130 | * If the spinlock is not available at the moment,
|
---|
| 131 | * signal failure.
|
---|
| 132 | *
|
---|
| 133 | * @param sl Pointer to spinlock_t structure.
|
---|
| 134 | *
|
---|
| 135 | * @return Zero on failure, non-zero otherwise.
|
---|
| 136 | */
|
---|
[f761f1eb] | 137 | int spinlock_trylock(spinlock_t *sl)
|
---|
| 138 | {
|
---|
[7dd56f1] | 139 | int rc;
|
---|
| 140 |
|
---|
[c842f04] | 141 | preemption_disable();
|
---|
[7dd56f1] | 142 | rc = !test_and_set(&sl->val);
|
---|
[5e04b48d] | 143 |
|
---|
| 144 | /*
|
---|
| 145 | * Prevent critical section code from bleeding out this way up.
|
---|
| 146 | */
|
---|
[7dd56f1] | 147 | CS_ENTER_BARRIER();
|
---|
[c842f04] | 148 |
|
---|
| 149 | if (!rc)
|
---|
| 150 | preemption_enable();
|
---|
[7dd56f1] | 151 |
|
---|
| 152 | return rc;
|
---|
[f761f1eb] | 153 | }
|
---|
| 154 |
|
---|
| 155 | #endif
|
---|