| 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 Software.Distribution@CS.CMU.EDU |
| 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 | static inline char xchgb(volatile char * cp, char new) |
| 110 | { |
| 111 | char old = new; |
| 112 | |
| 113 | __asm__ volatile (" xchgb %0,%2" : |
| 114 | "=q" (old) : |
| 115 | "0" (new), "m" (*(volatile char *)cp) : "memory" ); |
| 116 | return (old); |
| 117 | } |
| 118 | |
| 119 | static inline void atomic_incl(volatile long * p, long delta) |
| 120 | { |
| 121 | __asm__ volatile (" lock \n \ |
| 122 | add %0,%1" : \ |
| 123 | : \ |
| 124 | "r" (delta), "m" (*(volatile long *)p)); |
| 125 | } |
| 126 | |
| 127 | static inline void atomic_incs(volatile short * p, short delta) |
| 128 | { |
| 129 | __asm__ volatile (" lock \n \ |
| 130 | addw %0,%1" : \ |
| 131 | : \ |
| 132 | "q" (delta), "m" (*(volatile short *)p)); |
| 133 | } |
| 134 | |
| 135 | static inline void atomic_incb(volatile char * p, char delta) |
| 136 | { |
| 137 | __asm__ volatile (" lock \n \ |
| 138 | addb %0,%1" : \ |
| 139 | : \ |
| 140 | "q" (delta), "m" (*(volatile char *)p)); |
| 141 | } |
| 142 | |
| 143 | static inline void atomic_decl(volatile long * p, long delta) |
| 144 | { |
| 145 | __asm__ volatile (" lock \n \ |
| 146 | sub %0,%1" : \ |
| 147 | : \ |
| 148 | "r" (delta), "m" (*(volatile long *)p)); |
| 149 | } |
| 150 | |
| 151 | static inline int atomic_decl_and_test(volatile long * p, long delta) |
| 152 | { |
| 153 | uint8_t ret; |
| 154 | __asm__ volatile ( |
| 155 | " lock \n\t" |
| 156 | " sub %1,%2 \n\t" |
| 157 | " sete %0" |
| 158 | : "=qm" (ret) |
| 159 | : "r" (delta), "m" (*(volatile long *)p)); |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | static inline void atomic_decs(volatile short * p, short delta) |
| 164 | { |
| 165 | __asm__ volatile (" lock \n \ |
| 166 | subw %0,%1" : \ |
| 167 | : \ |
| 168 | "q" (delta), "m" (*(volatile short *)p)); |
| 169 | } |
| 170 | |
| 171 | static inline void atomic_decb(volatile char * p, char delta) |
| 172 | { |
| 173 | __asm__ volatile (" lock \n \ |
| 174 | subb %0,%1" : \ |
| 175 | : \ |
| 176 | "q" (delta), "m" (*(volatile char *)p)); |
| 177 | } |
| 178 | |
| 179 | static inline long atomic_getl(const volatile long * p) |
| 180 | { |
| 181 | return (*p); |
| 182 | } |
| 183 | |
| 184 | static inline short atomic_gets(const volatile short * p) |
| 185 | { |
| 186 | return (*p); |
| 187 | } |
| 188 | |
| 189 | static inline char atomic_getb(const volatile char * p) |
| 190 | { |
| 191 | return (*p); |
| 192 | } |
| 193 | |
| 194 | static inline void atomic_setl(volatile long * p, long value) |
| 195 | { |
| 196 | *p = value; |
| 197 | } |
| 198 | |
| 199 | static inline void atomic_sets(volatile short * p, short value) |
| 200 | { |
| 201 | *p = value; |
| 202 | } |
| 203 | |
| 204 | static inline void atomic_setb(volatile char * p, char value) |
| 205 | { |
| 206 | *p = value; |
| 207 | } |
| 208 | |
| 209 | #endif /* MACH_KERNEL_PRIVATE */ |
| 210 | |
| 211 | #endif /* _I386_BIT_ROUTINES_H_ */ |
| 212 | |