Remove UIO_USERISPACE, we do not support any split instruction/data
[dragonfly.git] / sys / i386 / i386 / mplock.s
1 /*
2  * $FreeBSD: src/sys/i386/i386/mplock.s,v 1.29.2.2 2000/05/16 06:58:06 dillon Exp $
3  * $DragonFly: src/sys/i386/i386/Attic/mplock.s,v 1.14 2004/07/16 05:48:29 dillon Exp $
4  *
5  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
6  * 
7  * This code is derived from software contributed to The DragonFly Project
8  * by Matthew Dillon <dillon@backplane.com>
9  * 
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  * 3. Neither the name of The DragonFly Project nor the names of its
21  *    contributors may be used to endorse or promote products derived
22  *    from this software without specific, prior written permission.
23  * 
24  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
28  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
30  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
32  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  * 
37  *                              DragonFly MPLOCK operation
38  *
39  * Each thread has an MP lock count, td_mpcount, and there is a shared
40  * global called mp_lock.  mp_lock is the physical MP lock and contains either
41  * -1 or the cpuid of the cpu owning the lock.  The count is *NOT* integrated
42  * into mp_lock but instead resides in each thread td_mpcount.
43  *
44  * When obtaining or releasing the MP lock the td_mpcount is PREDISPOSED
45  * to the desired count *PRIOR* to operating on the mp_lock itself.  MP
46  * lock operations can occur outside a critical section with interrupts
47  * enabled with the provisio (which the routines below handle) that an
48  * interrupt may come along and preempt us, racing our cmpxchgl instruction
49  * to perform the operation we have requested by pre-dispoing td_mpcount.
50  *
51  * Additionally, the LWKT threading system manages the MP lock and
52  * lwkt_switch(), in particular, may be called after pre-dispoing td_mpcount
53  * to handle 'blocking' on the MP lock.
54  *
55  *
56  * Recoded from the FreeBSD original:
57  * ----------------------------------------------------------------------------
58  * "THE BEER-WARE LICENSE" (Revision 42):
59  * <phk@FreeBSD.org> wrote this file.  As long as you retain this notice you
60  * can do whatever you want with this stuff. If we meet some day, and you think
61  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
62  * ----------------------------------------------------------------------------
63  */
64
65 #include <machine/asmacros.h>
66 #include <machine/smptests.h>           /** GRAB_LOPRIO */
67 #include <machine/apicreg.h>
68
69 #include "assym.s"
70
71 /*
72  * YYY Debugging only.  Define this to be paranoid about invalidating the
73  * TLB when we get giant.
74  */
75 #undef PARANOID_INVLTLB
76
77         .data
78         ALIGN_DATA
79 #ifdef SMP
80         .globl  mp_lock
81 mp_lock:
82         .long   -1                      /* initialized to not held */
83 #endif
84
85         .text
86         SUPERALIGN_TEXT
87
88         /*
89          * Note on cmpxchgl... exchanges ecx with mem if mem matches eax.
90          * Z=1 (jz) on success.   A lock prefix is required for MP.
91          */
92 NON_GPROF_ENTRY(cpu_get_initial_mplock)
93         movl    PCPU(curthread),%ecx
94         movl    $1,TD_MPCOUNT(%ecx)     /* curthread has mpcount of 1 */
95         movl    $0,mp_lock              /* owned by cpu 0 */
96         NON_GPROF_RET
97
98         /*
99          * cpu_try_mplock() returns non-zero on success, 0 on failure.  It
100          * only adjusts mp_lock, it does not touch td_mpcount.  Callers
101          * should always increment td_mpcount *before* trying to acquire
102          * the actual lock, predisposing td_mpcount to the desired state of
103          * the lock.
104          *
105          * NOTE! Only call cpu_try_mplock() inside a critical section.  If
106          * you don't an interrupt can come along and get and release
107          * the lock before our cmpxchgl instruction, causing us to fail 
108          * but resulting in the lock being held by our cpu.
109          */
110 NON_GPROF_ENTRY(cpu_try_mplock)
111         movl    PCPU(cpuid),%ecx
112         movl    $-1,%eax
113         lock cmpxchgl %ecx,mp_lock      /* ecx<->mem if eax matches */
114         jnz     1f
115 #ifdef PARANOID_INVLTLB
116         movl    %cr3,%eax; movl %eax,%cr3       /* YYY check and remove */
117 #endif
118         movl    $1,%eax
119         NON_GPROF_RET
120 1:
121         subl    %eax,%eax
122         NON_GPROF_RET
123
124         /*
125          * get_mplock() Obtains the MP lock and may switch away if it cannot
126          * get it.  This routine may be called WITHOUT a critical section
127          * and with cpu interrupts enabled.
128          *
129          * To handle races in a sane fashion we predispose TD_MPCOUNT,
130          * which prevents us from losing the lock in a race if we already
131          * have it or happen to get it.  It also means that we might get
132          * the lock in an interrupt race before we have a chance to execute
133          * our cmpxchgl instruction, so we have to handle that case.
134          * Fortunately simply calling lwkt_switch() handles the situation
135          * for us and also 'blocks' us until the MP lock can be obtained.
136          */
137 NON_GPROF_ENTRY(get_mplock)
138         movl    PCPU(cpuid),%ecx
139         movl    PCPU(curthread),%edx
140         incl    TD_MPCOUNT(%edx)        /* predispose */
141         cmpl    %ecx,mp_lock
142         jne     1f
143         NON_GPROF_RET                   /* success! */
144
145         /*
146          * We don't already own the mp_lock, use cmpxchgl to try to get
147          * it.
148          */
149 1:
150         movl    $-1,%eax
151         lock cmpxchgl %ecx,mp_lock
152         jnz     2f
153 #ifdef PARANOID_INVLTLB
154         movl    %cr3,%eax; movl %eax,%cr3 /* YYY check and remove */
155 #endif
156         NON_GPROF_RET                   /* success */
157
158         /*
159          * Failure, but we could end up owning mp_lock anyway due to
160          * an interrupt race.  lwkt_switch() will clean up the mess
161          * and 'block' until the mp_lock is obtained.
162          */
163 2:
164         call    lwkt_switch
165 #ifdef INVARIANTS
166         movl    PCPU(cpuid),%eax        /* failure */
167         cmpl    %eax,mp_lock
168         jne     4f
169 #endif
170         NON_GPROF_RET
171 #ifdef INVARIANTS
172 4:
173         cmpl    $0,panicstr             /* don't double panic */
174         je      badmp_get2
175         NON_GPROF_RET
176 #endif
177
178         /*
179          * try_mplock() attempts to obtain the MP lock.  1 is returned on
180          * success, 0 on failure.  We do not have to be in a critical section
181          * and interrupts are almost certainly enabled.
182          *
183          * We must pre-dispose TD_MPCOUNT in order to deal with races in
184          * a reasonable way.
185          *
186          */
187 NON_GPROF_ENTRY(try_mplock)
188         movl    PCPU(cpuid),%ecx
189         movl    PCPU(curthread),%edx
190         incl    TD_MPCOUNT(%edx)                /* pre-dispose for race */
191         cmpl    %ecx,mp_lock
192         je      1f                              /* trivial success */
193         movl    $-1,%eax
194         lock cmpxchgl %ecx,mp_lock
195         jnz     2f
196         /*
197          * Success
198          */
199 #ifdef PARANOID_INVLTLB
200         movl    %cr3,%eax; movl %eax,%cr3       /* YYY check and remove */
201 #endif
202 1:
203         movl    $1,%eax                         /* success (cmpxchgl good!) */
204         NON_GPROF_RET
205
206         /*
207          * The cmpxchgl failed but we might have raced.  Undo the mess by
208          * predispoing TD_MPCOUNT and then checking.  If TD_MPCOUNT is
209          * still non-zero we don't care what state the lock is in (since
210          * we obviously didn't own it above), just return failure even if
211          * we won the lock in an interrupt race.  If TD_MPCOUNT is zero
212          * make sure we don't own the lock in case we did win it in a race.
213          */
214 2:
215         decl    TD_MPCOUNT(%edx)
216         cmpl    $0,TD_MPCOUNT(%edx)
217         jne     3f
218         movl    PCPU(cpuid),%eax
219         movl    $-1,%ecx
220         lock cmpxchgl %ecx,mp_lock
221 3:
222         subl    %eax,%eax
223         NON_GPROF_RET
224         
225         /*
226          * rel_mplock() releases a previously obtained MP lock.
227          *
228          * In order to release the MP lock we pre-dispose TD_MPCOUNT for
229          * the release and basically repeat the release portion of try_mplock
230          * above.
231          */
232 NON_GPROF_ENTRY(rel_mplock)
233         movl    PCPU(curthread),%edx
234         movl    TD_MPCOUNT(%edx),%eax
235 #ifdef INVARIANTS
236         cmpl    $0,%eax
237         je      badmp_rel
238 #endif
239         subl    $1,%eax
240         movl    %eax,TD_MPCOUNT(%edx)
241         cmpl    $0,%eax
242         jne     3f
243         movl    PCPU(cpuid),%eax
244         movl    $-1,%ecx
245         lock cmpxchgl %ecx,mp_lock
246 3:
247         NON_GPROF_RET
248
249 #ifdef INVARIANTS
250
251 badmp_get:
252         pushl   $bmpsw1
253         call    panic
254 badmp_get2:
255         pushl   $bmpsw1a
256         call    panic
257 badmp_rel:
258         pushl   $bmpsw2
259         call    panic
260
261         .data
262
263 bmpsw1:
264         .asciz  "try/get_mplock(): already have lock! %d %p"
265
266 bmpsw1a:
267         .asciz  "try/get_mplock(): failed on count or switch %d %p"
268
269 bmpsw2:
270         .asciz  "rel_mplock(): mpcount already 0 @ %p %p %p %p %p %p %p %p!"
271
272 #endif
273