xref: /xnu-10063.141.1/osfmk/i386/bit_routines.h (revision d8b80295118ef25ac3a784134bcf95cd8e88109f)
1 /*
2  * Copyright (c) 2000-2013 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 /*
29  * Copyright (C) 1998 Apple Computer
30  * All Rights Reserved
31  */
32 /*
33  * @OSF_COPYRIGHT@
34  */
35 /*
36  * Mach Operating System
37  * Copyright (c) 1991,1990 Carnegie Mellon University
38  * All Rights Reserved.
39  *
40  * Permission to use, copy, modify and distribute this software and its
41  * documentation is hereby granted, provided that both the copyright
42  * notice and this permission notice appear in all copies of the
43  * software, derivative works or modified versions, and any portions
44  * thereof, and that both notices appear in supporting documentation.
45  *
46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
48  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49  *
50  * Carnegie Mellon requests users of this software to return to
51  *
52  *  Software Distribution Coordinator  or  [email protected]
53  *  School of Computer Science
54  *  Carnegie Mellon University
55  *  Pittsburgh PA 15213-3890
56  *
57  * any improvements or extensions that they make and grant Carnegie Mellon
58  * the rights to redistribute these changes.
59  */
60 
61 /*
62  * Bit-mask manipulation routines
63  */
64 
65 #ifndef	_I386_BIT_ROUTINES_H_
66 #define	_I386_BIT_ROUTINES_H_
67 
68 #ifdef MACH_KERNEL_PRIVATE
69 
70 /*
71  *	General bit-lock routines.
72  */
73 
74 #define	bit_lock(bit,l)							\
75 	__asm__ volatile("	jmp	1f	\n			\
76 		 	0:	btl	%0, %1	\n			\
77 				jb	0b	\n			\
78 			1:	lock		\n			\
79 				btsl	%0,%1	\n			\
80 				jb	0b"			:	\
81 								:	\
82 			"r" (bit), "m" (*(volatile int *)(l))	:	\
83 			"memory");
84 
85 #define	bit_unlock(bit,l)						\
86 	__asm__ volatile("	lock		\n			\
87 				btrl	%0,%1"			:	\
88 								:	\
89 			"r" (bit), "m" (*(volatile int *)(l)));
90 
91 /*
92  *      Set or clear individual bits in a long word.
93  *      The locked access is needed only to lock access
94  *      to the word, not to individual bits.
95  */
96 
97 #define	i_bit_set(bit,l)						\
98 	__asm__ volatile("	lock		\n			\
99 				btsl	%0,%1"			:	\
100 								:	\
101 			"r" (bit), "m" (*(volatile int *)(l)));
102 
103 #define	i_bit_clear(bit,l)						\
104 	__asm__ volatile("	lock		\n			\
105 				btrl	%0,%1"			:	\
106 								:	\
107 			"r" (bit), "m" (*(volatile int *)(l)));
108 
109 
atomic_incl(volatile long * p,long delta)110 static inline void	atomic_incl(volatile long * p, long delta)
111 {
112 	__asm__ volatile ("	lock		\n		\
113 				add    %0,%1"		:	\
114 							:	\
115 				"r" (delta), "m" (*(volatile long *)p));
116 }
117 
atomic_decl(volatile long * p,long delta)118 static inline void	atomic_decl(volatile long * p, long delta)
119 {
120 	__asm__ volatile ("	lock		\n		\
121 				sub		%0,%1"		:	\
122 							:	\
123 				"r" (delta), "m" (*(volatile long *)p));
124 }
125 
atomic_decl_and_test(volatile long * p,long delta)126 static inline int	atomic_decl_and_test(volatile long * p, long delta)
127 {
128 	uint8_t	ret;
129 	__asm__ volatile (
130 		"	lock		\n\t"
131 		"	sub		%1,%2	\n\t"
132 		"	sete	%0"
133 		: "=qm" (ret)
134 		: "r" (delta), "m" (*(volatile long *)p));
135 	return ret;
136 }
137 #endif /* MACH_KERNEL_PRIVATE */
138 
139 #endif	/* _I386_BIT_ROUTINES_H_ */
140