1 /* $OpenBSD: lock_machdep.c,v 1.6 2007/05/29 18:18:20 tom Exp $ */
2 /* $NetBSD: lock_machdep.c,v 1.1.2.3 2000/05/03 14:40:30 sommerfeld Exp $ */
3
4 /*-
5 * Copyright (c) 1998, 1999 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
10 * NASA Ames Research Center.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. All advertising materials mentioning features or use of this software
21 * must display the following acknowledgement:
22 * This product includes software developed by the NetBSD
23 * Foundation, Inc. and its contributors.
24 * 4. Neither the name of The NetBSD Foundation nor the names of its
25 * contributors may be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
30 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
32 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
36 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
37 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38 * POSSIBILITY OF SUCH DAMAGE.
39 */
40
41 #include <sys/cdefs.h> /* RCS ID & Copyright macro defns */
42
43 /*
44 * Machine-dependent spin lock operations.
45 */
46
47 #include <sys/param.h>
48 #include <sys/lock.h>
49 #include <sys/systm.h>
50
51 #include <machine/atomic.h>
52 #include <machine/lock.h>
53 #include <machine/cpufunc.h>
54
55 #include <ddb/db_output.h>
56
57 #ifdef LOCKDEBUG
58
59 void
60 __cpu_simple_lock_init(__cpu_simple_lock_t *lockp)
61 {
62 *lockp = __SIMPLELOCK_UNLOCKED;
63 }
64
65 #if defined (DEBUG) && defined(DDB)
66 int spin_limit = 10000000;
67 #endif
68
69 void
70 __cpu_simple_lock(__cpu_simple_lock_t *lockp)
71 {
72 #if defined (DEBUG) && defined(DDB)
73 int spincount = 0;
74 #endif
75
76 while (i386_atomic_testset_i(lockp, __SIMPLELOCK_LOCKED)
77 == __SIMPLELOCK_LOCKED) {
78 #if defined(DEBUG) && defined(DDB)
79 spincount++;
80 if (spincount == spin_limit) {
81 extern int db_active;
82 db_printf("spundry\n");
83 if (db_active) {
84 db_printf("but already in debugger\n");
85 } else {
86 Debugger();
87 }
88 }
89 #endif
90 }
91 }
92
93 int
94 __cpu_simple_lock_try(__cpu_simple_lock_t *lockp)
95 {
96
97 if (i386_atomic_testset_i(lockp, __SIMPLELOCK_LOCKED)
98 == __SIMPLELOCK_UNLOCKED)
99 return (1);
100 return (0);
101 }
102
103 void
104 __cpu_simple_unlock(__cpu_simple_lock_t *lockp)
105 {
106 *lockp = __SIMPLELOCK_UNLOCKED;
107 }
108
109 #endif
110
111 int
112 rw_cas_486(volatile unsigned long *p, unsigned long o, unsigned long n)
113 {
114 return (i486_atomic_cas_int((u_int *)p, o, n) != o);
115 }