]> git.saurik.com Git - apple/xnu.git/blob - iokit/IOKit/ppc/IOSharedLockImp.h
xnu-517.9.5.tar.gz
[apple/xnu.git] / iokit / IOKit / ppc / IOSharedLockImp.h
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1998 Apple Computer, Inc. All rights reserved.
24 *
25 * HISTORY
26 *
27 */
28
29 /* Copyright (c) 1992 NeXT Computer, Inc. All rights reserved.
30 *
31 * EventShmemLock.h - Shared memory area locks for use between the
32 * WindowServer and the Event Driver.
33 *
34 * HISTORY
35 * 30 Nov 1992 Ben Fathi (benf@next.com)
36 * Ported to m98k.
37 *
38 * 29 April 1992 Mike Paquette at NeXT
39 * Created.
40 *
41 * Multiprocessor locks used within the shared memory area between the
42 * kernel and event system. These must work in both user and kernel mode.
43 * The locks are defined in an include file so they get exported to the local
44 * include file area.
45 */
46
47
48 #ifndef _IOKIT_IOSHAREDLOCKIMP_H
49 #define _IOKIT_IOSHAREDLOCKIMP_H
50
51 #include <architecture/ppc/asm_help.h>
52 #ifdef KERNEL
53 #undef END
54 #include <mach/ppc/asm.h>
55 #endif
56
57 .macro DISABLE_PREEMPTION
58 #ifdef KERNEL
59 stwu r1,-(FM_SIZE)(r1)
60 mflr r0
61 stw r3,FM_ARG0(r1)
62 stw r0,(FM_SIZE+FM_LR_SAVE)(r1)
63 bl EXT(_disable_preemption)
64 lwz r3,FM_ARG0(r1)
65 lwz r1,0(r1)
66 lwz r0,FM_LR_SAVE(r1)
67 mtlr r0
68 #endif
69 .endmacro
70 .macro ENABLE_PREEMPTION
71 #ifdef KERNEL
72 stwu r1,-(FM_SIZE)(r1)
73 mflr r0
74 stw r3,FM_ARG0(r1)
75 stw r0,(FM_SIZE+FM_LR_SAVE)(r1)
76 bl EXT(_enable_preemption)
77 lwz r3,FM_ARG0(r1)
78 lwz r1,0(r1)
79 lwz r0,FM_LR_SAVE(r1)
80 mtlr r0
81 #endif
82 .endmacro
83
84 /*
85 * void
86 * ev_lock(p)
87 * register int *p;
88 *
89 * Lock the lock pointed to by p. Spin (possibly forever) until
90 * the lock is available. Test and test and set logic used.
91 */
92 TEXT
93
94 #ifndef KERNEL
95 LEAF(_ev_lock)
96
97 li a6,1 // lock value
98
99 8: lwz a7,0(a0) // Get lock word
100 mr. a7,a7 // Is it held?
101 bne-- 8b // Yup...
102
103 9: lwarx a7,0,a0 // read the lock
104 mr. a7,a7 // Is it held?
105 bne-- 7f // yes, kill reservation
106 stwcx. a6,0,a0 // try to get the lock
107 bne-- 9b // failed, try again
108 isync
109 blr // got it, return
110
111 7: li a7,-4 // Point to a spot in the red zone
112 stwcx. a7,a7,r1 // Kill reservation
113 b 8b // Go wait some more...
114
115
116 END(_ev_lock)
117
118 LEAF(_IOSpinLock)
119
120 li a6,1 // lock value
121
122 8: lwz a7,0(a0) // Get lock word
123 mr. a7,a7 // Is it held?
124 bne-- 8b // Yup...
125
126 9: lwarx a7,0,a0 // read the lock
127 mr. a7,a7 // Is it held?
128 bne-- 7f // yes, kill reservation
129 stwcx. a6,0,a0 // try to get the lock
130 bne-- 9b // failed, try again
131 isync
132 blr // got it, return
133
134 7: li a7,-4 // Point to a spot in the red zone
135 stwcx. a7,a7,r1 // Kill reservation
136 b 8b // Go wait some more...
137 END(_IOSpinLock)
138 #endif
139
140 /*
141 * void
142 * spin_unlock(p)
143 * int *p;
144 *
145 * Unlock the lock pointed to by p.
146 */
147
148 LEAF(_ev_unlock)
149 sync
150 li a7,0
151 stw a7,0(a0)
152 ENABLE_PREEMPTION()
153 blr
154 END(_ev_unlock)
155
156 LEAF(_IOSpinUnlock)
157 sync
158 li a7,0
159 stw a7,0(a0)
160 ENABLE_PREEMPTION()
161 blr
162 END(_IOSpinUnlock)
163
164
165 /*
166 * ev_try_lock(p)
167 * int *p;
168 *
169 * Try to lock p. Return TRUE if successful in obtaining lock.
170 */
171
172 LEAF(_ev_try_lock)
173
174 DISABLE_PREEMPTION()
175
176 li a6,1 // lock value
177
178 lwz a7,0(a0) // Get lock word
179 mr. a7,a7 // Is it held?
180 bne-- 6f // Yup...
181
182 9: lwarx a7,0,a0 // read the lock
183 mr. a7,a7 // Is it held?
184 bne-- 7f // yes, kill reservation
185 stwcx. a6,0,a0 // try to get the lock
186 bne-- 9b // failed, try again
187 li a0,1 // return TRUE
188 isync
189 blr // got it, return
190
191 7: li a7,-4 // Point to a spot in the red zone
192 stwcx. a7,a7,r1 // Kill reservation
193
194 6:
195 ENABLE_PREEMPTION()
196 li a0,0 // return FALSE
197 blr
198
199 END(_ev_try_lock)
200
201 LEAF(_IOTrySpinLock)
202
203 DISABLE_PREEMPTION()
204
205 li a6,1 // lock value
206
207 lwz a7,0(a0) // Get lock word
208 mr. a7,a7 // Is it held?
209 bne-- 6f // Yup...
210
211 9: lwarx a7,0,a0 // read the lock
212 mr. a7,a7 // Is it held?
213 bne-- 7f // yes, kill reservation
214 stwcx. a6,0,a0 // try to get the lock
215 bne-- 9b // failed, try again
216 li a0,1 // return TRUE
217 isync
218 blr // got it, return
219
220 7: li a7,-4 // Point to a spot in the red zone
221 stwcx. a7,a7,r1 // Kill reservation
222
223 6:
224 ENABLE_PREEMPTION()
225 li a0,0 // return FALSE
226 blr
227
228 END(_IOTrySpinLock)
229
230 #endif /* ! _IOKIT_IOSHAREDLOCKIMP_H */