Rev 1313 | Rev 1702 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1313 | Rev 1323 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
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 |
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. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
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 |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
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 |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
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 |
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. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** |
29 | /** |
30 | * @file spinlock.c |
30 | * @file spinlock.c |
31 | * @brief Spinlocks. |
31 | * @brief Spinlocks. |
32 | */ |
32 | */ |
33 | 33 | ||
34 | #include <synch/spinlock.h> |
34 | #include <synch/spinlock.h> |
35 | #include <atomic.h> |
35 | #include <atomic.h> |
36 | #include <arch/barrier.h> |
36 | #include <arch/barrier.h> |
37 | #include <arch.h> |
37 | #include <arch.h> |
38 | #include <preemption.h> |
38 | #include <preemption.h> |
39 | #include <print.h> |
39 | #include <print.h> |
40 | #include <debug.h> |
40 | #include <debug.h> |
41 | #include <symtab.h> |
41 | #include <symtab.h> |
42 | 42 | ||
43 | #ifdef CONFIG_FB |
43 | #ifdef CONFIG_FB |
44 | #include <genarch/fb/fb.h> |
44 | #include <genarch/fb/fb.h> |
45 | #endif |
45 | #endif |
46 | 46 | ||
47 | #ifdef CONFIG_SMP |
47 | #ifdef CONFIG_SMP |
48 | 48 | ||
49 | /** Initialize spinlock |
49 | /** Initialize spinlock |
50 | * |
50 | * |
51 | * Initialize spinlock. |
51 | * Initialize spinlock. |
52 | * |
52 | * |
53 | * @param sl Pointer to spinlock_t structure. |
53 | * @param sl Pointer to spinlock_t structure. |
54 | */ |
54 | */ |
55 | void spinlock_initialize(spinlock_t *sl, char *name) |
55 | void spinlock_initialize(spinlock_t *sl, char *name) |
56 | { |
56 | { |
57 | atomic_set(&sl->val, 0); |
57 | atomic_set(&sl->val, 0); |
58 | #ifdef CONFIG_DEBUG_SPINLOCK |
58 | #ifdef CONFIG_DEBUG_SPINLOCK |
59 | sl->name = name; |
59 | sl->name = name; |
60 | #endif |
60 | #endif |
61 | } |
61 | } |
62 | 62 | ||
63 | /** Lock spinlock |
63 | /** Lock spinlock |
64 | * |
64 | * |
65 | * Lock spinlock. |
65 | * Lock spinlock. |
66 | * This version has limitted ability to report |
66 | * This version has limitted ability to report |
67 | * possible occurence of deadlock. |
67 | * possible occurence of deadlock. |
68 | * |
68 | * |
69 | * @param sl Pointer to spinlock_t structure. |
69 | * @param sl Pointer to spinlock_t structure. |
70 | */ |
70 | */ |
71 | #ifdef CONFIG_DEBUG_SPINLOCK |
71 | #ifdef CONFIG_DEBUG_SPINLOCK |
72 | void spinlock_lock_debug(spinlock_t *sl) |
72 | void spinlock_lock_debug(spinlock_t *sl) |
73 | { |
73 | { |
74 | count_t i = 0; |
74 | count_t i = 0; |
75 | char *symbol; |
75 | char *symbol; |
76 | bool deadlock_reported = false; |
76 | bool deadlock_reported = false; |
77 | 77 | ||
78 | preemption_disable(); |
78 | preemption_disable(); |
79 | while (test_and_set(&sl->val)) { |
79 | while (test_and_set(&sl->val)) { |
80 | 80 | ||
81 | /* |
81 | /* |
82 | * We need to be careful about printflock and fb_lock. |
82 | * We need to be careful about printflock and fb_lock. |
83 | * Both of them are used to report deadlocks via |
83 | * Both of them are used to report deadlocks via |
84 | * printf() and fb_putchar(). |
84 | * printf() and fb_putchar(). |
85 | * |
85 | * |
86 | * We trust our code that there is no possible deadlock |
86 | * We trust our code that there is no possible deadlock |
87 | * caused by these two locks (except when an exception |
87 | * caused by these two locks (except when an exception |
88 | * is triggered for instance by printf() or fb_putchar()). |
88 | * is triggered for instance by printf() or fb_putchar()). |
89 | * However, we encountered false positives caused by very |
89 | * However, we encountered false positives caused by very |
90 | * slow VESA framebuffer interaction (especially when |
90 | * slow VESA framebuffer interaction (especially when |
91 | * run in a simulator) that caused problems with both |
91 | * run in a simulator) that caused problems with both |
92 | * printflock and fb_lock. |
92 | * printflock and fb_lock. |
93 | * |
93 | * |
94 | * Possible deadlocks on both printflock and fb_lock |
94 | * Possible deadlocks on both printflock and fb_lock |
95 | * are therefore not reported as they would cause an |
95 | * are therefore not reported as they would cause an |
96 | * infinite recursion. |
96 | * infinite recursion. |
97 | */ |
97 | */ |
98 | if (sl == &printflock) |
98 | if (sl == &printflock) |
99 | continue; |
99 | continue; |
100 | #ifdef CONFIG_FB |
100 | #ifdef CONFIG_FB |
101 | if (sl == &fb_lock) |
101 | if (sl == &fb_lock) |
102 | continue; |
102 | continue; |
103 | #endif |
103 | #endif |
104 | if (i++ > 300000) { |
104 | if (i++ > 10000000) { |
105 | printf("cpu%d: looping on spinlock %.*p:%s, caller=%.*p", |
105 | printf("cpu%d: looping on spinlock %.*p:%s, caller=%.*p", |
106 | CPU->id, sizeof(__address) * 2, sl, sl->name, sizeof(__address) * 2, CALLER); |
106 | CPU->id, sizeof(__address) * 2, sl, sl->name, sizeof(__address) * 2, CALLER); |
107 | symbol = get_symtab_entry(CALLER); |
107 | symbol = get_symtab_entry(CALLER); |
108 | if (symbol) |
108 | if (symbol) |
109 | printf("(%s)", symbol); |
109 | printf("(%s)", symbol); |
110 | printf("\n"); |
110 | printf("\n"); |
111 | i = 0; |
111 | i = 0; |
112 | deadlock_reported = true; |
112 | deadlock_reported = true; |
113 | } |
113 | } |
114 | } |
114 | } |
115 | 115 | ||
116 | if (deadlock_reported) |
116 | if (deadlock_reported) |
117 | printf("cpu%d: not deadlocked\n", CPU->id); |
117 | printf("cpu%d: not deadlocked\n", CPU->id); |
118 | 118 | ||
119 | /* |
119 | /* |
120 | * Prevent critical section code from bleeding out this way up. |
120 | * Prevent critical section code from bleeding out this way up. |
121 | */ |
121 | */ |
122 | CS_ENTER_BARRIER(); |
122 | CS_ENTER_BARRIER(); |
123 | } |
123 | } |
124 | #endif |
124 | #endif |
125 | 125 | ||
126 | /** Lock spinlock conditionally |
126 | /** Lock spinlock conditionally |
127 | * |
127 | * |
128 | * Lock spinlock conditionally. |
128 | * Lock spinlock conditionally. |
129 | * If the spinlock is not available at the moment, |
129 | * If the spinlock is not available at the moment, |
130 | * signal failure. |
130 | * signal failure. |
131 | * |
131 | * |
132 | * @param sl Pointer to spinlock_t structure. |
132 | * @param sl Pointer to spinlock_t structure. |
133 | * |
133 | * |
134 | * @return Zero on failure, non-zero otherwise. |
134 | * @return Zero on failure, non-zero otherwise. |
135 | */ |
135 | */ |
136 | int spinlock_trylock(spinlock_t *sl) |
136 | int spinlock_trylock(spinlock_t *sl) |
137 | { |
137 | { |
138 | int rc; |
138 | int rc; |
139 | 139 | ||
140 | preemption_disable(); |
140 | preemption_disable(); |
141 | rc = !test_and_set(&sl->val); |
141 | rc = !test_and_set(&sl->val); |
142 | 142 | ||
143 | /* |
143 | /* |
144 | * Prevent critical section code from bleeding out this way up. |
144 | * Prevent critical section code from bleeding out this way up. |
145 | */ |
145 | */ |
146 | CS_ENTER_BARRIER(); |
146 | CS_ENTER_BARRIER(); |
147 | 147 | ||
148 | if (!rc) |
148 | if (!rc) |
149 | preemption_enable(); |
149 | preemption_enable(); |
150 | 150 | ||
151 | return rc; |
151 | return rc; |
152 | } |
152 | } |
153 | 153 | ||
154 | #endif |
154 | #endif |
155 | 155 |