]> git.saurik.com Git - apple/libc.git/blame - x86_64/sys/OSAtomic.s
Libc-391.5.18.tar.gz
[apple/libc.git] / x86_64 / sys / OSAtomic.s
CommitLineData
8e029c65
A
1/*
2 * Copyright (c) 2004-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24#include <machine/cpu_capabilities.h>
25
26#define DECLARE(x) \
27.align 2, 0x90 ; \
28.globl x ; \
29.globl x ## Barrier ; \
30x: ; \
31x ## Barrier:
32
33.text
34
35
36// uint32_t OSAtomicAnd32( uint32_t mask, uint32_t *value);
37DECLARE(_OSAtomicAnd32)
38 movq $(_COMM_PAGE_COMPARE_AND_SWAP32), %rcx
39 movl %edi, %r11d // save mask
40 movl (%rsi), %eax // get value
41 movq %rsi, %rdx // put ptr where compare-and-swap expects it
421:
43 movl %r11d, %esi // original mask
44 movl %eax, %edi // old value
45 andl %eax, %esi // new value
46 call *%rcx // %edi=old value, %esi=new value. %rdx=ptr
47 jnz 1b
48 movl %esi, %eax
49 ret
50
51
52// uint32_t OSAtomicOr32( uint32_t mask, uint32_t *value);
53DECLARE(_OSAtomicOr32)
54 movq $(_COMM_PAGE_COMPARE_AND_SWAP32), %rcx
55 movl %edi, %r11d // save mask
56 movl (%rsi), %eax // get value
57 movq %rsi, %rdx // put ptr where compare-and-swap expects it
581:
59 movl %r11d, %esi // original mask
60 movl %eax, %edi // old value
61 orl %eax, %esi // new value
62 call *%rcx // %edi=old value, %esi=new value. %rdx=ptr
63 jnz 1b
64 movl %esi, %eax
65 ret
66
67
68// uint32_t OSAtomicXor32( uint32_t mask, uint32_t *value);
69DECLARE(_OSAtomicXor32)
70 movq $(_COMM_PAGE_COMPARE_AND_SWAP32), %rcx
71 movl %edi, %r11d // save mask
72 movl (%rsi), %eax // get value
73 movq %rsi, %rdx // put ptr where compare-and-swap expects it
741:
75 movl %r11d, %esi // original mask
76 movl %eax, %edi // old value
77 xorl %eax, %esi // new value
78 call *%rcx // %edi=old value, %esi=new value. %rdx=ptr
79 jnz 1b
80 movl %esi, %eax
81 ret
82
83
84// bool OSAtomicCompareAndSwap32( int32_t old, int32_t new, int32_t *value);
85DECLARE(_OSAtomicCompareAndSwap32)
86 movq $(_COMM_PAGE_COMPARE_AND_SWAP32), %rcx
87 call *%rcx // %edi=old value, %esi=new value. %rdx=ptr
88 sete %al
89 ret
90
91
92// bool OSAtomicCompareAndSwap64( int64_t old, int64_t new, int64_t *value);
93DECLARE(_OSAtomicCompareAndSwap64)
94 movq $(_COMM_PAGE_COMPARE_AND_SWAP64), %rcx
95 call *%rcx // %rdi=old value, %rsi=new value. %rdx=ptr
96 sete %al
97 ret
98
99
100// int32_t OSAtomicAdd32( int32_t amt, int32_t *value );
101DECLARE(_OSAtomicAdd32)
102 movq $(_COMM_PAGE_ATOMIC_ADD32), %rcx
103 movl %edi, %eax // save amt to add
104 call *%rcx
105 addl %edi,%eax // new value
106 ret
107
108
109// int64_t OSAtomicAdd64( int64_t amt, int64_t *value );
110DECLARE(_OSAtomicAdd64)
111 movq $(_COMM_PAGE_ATOMIC_ADD64), %rcx
112 movq %rdi, %rax // save amt to add
113 call *%rcx
114 addq %rdi, %rax // new value
115 ret
116
117
118// bool OSAtomicTestAndSet( uint32_t n, void *value );
119DECLARE(_OSAtomicTestAndSet)
120 movq $(_COMM_PAGE_BTS), %rax
121 xorl $7, %edi // bit position is numbered big endian
122 call *%rax
123 setc %al
124 ret
125
126
127// bool OSAtomicTestAndClear( uint32_t n, void *value );
128DECLARE(_OSAtomicTestAndClear)
129 movq $(_COMM_PAGE_BTC), %rax
130 xorl $7, %edi // bit position is numbered big endian
131 call *%rax
132 setc %al
133 ret
134
135// bool OSSpinLockTry( OSSpinLock *lock );
136 .align 2, 0x90
137 .globl _OSSpinLockTry
138_OSSpinLockTry:
139 movq $(_COMM_PAGE_SPINLOCK_TRY), %rax
140 jmp *%rax
141
142
143// void OSSpinLockLock( OSSpinLock *lock );
144 .align 2, 0x90
145 .globl _OSSpinLockLock
146_OSSpinLockLock:
147 movq $(_COMM_PAGE_SPINLOCK_LOCK), %rax
148 jmp *%rax
149
150
151// void OSSpinLockUnlock( OSSpinLock *lock );
152 .align 2, 0x90
153 .globl _OSSpinLockUnlock
154_OSSpinLockUnlock:
155 movl $0, (%rdi)
156 ret
157
158
159// void OSMemoryBarrier( void );
160 .align 2, 0x90
161 .globl _OSMemoryBarrier
162_OSMemoryBarrier:
163 ret