]> git.saurik.com Git - apple/xnu.git/blob - osfmk/i386/commpage/atomic.s
25f9ca6ca22d77363d8320ab771d4e5cc54ef3e2
[apple/xnu.git] / osfmk / i386 / commpage / atomic.s
1 /*
2 * Copyright (c) 2004 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 <sys/appleapiopts.h>
25 #include <machine/cpu_capabilities.h>
26 #include <machine/commpage.h>
27
28 /* OSAtomic.h library native implementations. */
29
30 .text
31 .align 2, 0x90
32
33 // This is a regparm(3) subroutine used by:
34
35 // bool OSAtomicCompareAndSwap32( int32_t old, int32_t new, int32_t *value);
36 // int32_t OSAtomicAnd32( int32_t mask, int32_t *value);
37 // int32_t OSAtomicOr32( int32_t mask, int32_t *value);
38 // int32_t OSAtomicXor32( int32_t mask, int32_t *value);
39
40 // It assumes old -> %eax, new -> %edx, value -> %ecx
41 // on success: returns with ZF set
42 // on failure: returns with *value in %eax, ZF clear
43
44 // The first word of the routine contains the address of the first instruction,
45 // so callers can pass parameters in registers by using the absolute:
46
47 // call *_COMPARE_AND_SWAP32
48
49 // TODO: move the .long onto a separate page to reduce icache pollution (?)
50
51 Lcompare_and_swap32_mp:
52 .long _COMM_PAGE_COMPARE_AND_SWAP32+4
53 lock
54 cmpxchgl %edx, (%ecx)
55 ret
56
57 COMMPAGE_DESCRIPTOR(compare_and_swap32_mp,_COMM_PAGE_COMPARE_AND_SWAP32,0,kUP)
58
59 Lcompare_and_swap32_up:
60 .long _COMM_PAGE_COMPARE_AND_SWAP32+4
61 cmpxchgl %edx, (%ecx)
62 ret
63
64 COMMPAGE_DESCRIPTOR(compare_and_swap32_up,_COMM_PAGE_COMPARE_AND_SWAP32,kUP,0)
65
66 // This is a subroutine used by:
67 // bool OSAtomicCompareAndSwap64( int64_t old, int64_t new, int64_t *value);
68
69 // It assumes old -> %eax/%edx, new -> %ebx/%ecx, value -> %esi
70 // on success: returns with ZF set
71 // on failure: returns with *value in %eax/%edx, ZF clear
72
73 Lcompare_and_swap64_mp:
74 .long _COMM_PAGE_COMPARE_AND_SWAP64+4
75 lock
76 cmpxchg8b (%esi)
77 ret
78
79 COMMPAGE_DESCRIPTOR(compare_and_swap64_mp,_COMM_PAGE_COMPARE_AND_SWAP64,0,kUP)
80
81 Lcompare_and_swap64_up:
82 .long _COMM_PAGE_COMPARE_AND_SWAP64+4
83 cmpxchg8b (%esi)
84 ret
85
86 COMMPAGE_DESCRIPTOR(compare_and_swap64_up,_COMM_PAGE_COMPARE_AND_SWAP64,kUP,0)
87
88 // This is a subroutine used by:
89 // bool OSAtomicTestAndSet( uint32_t n, void *value );
90 // It assumes n -> %eax, value -> %edx
91
92 // Returns: old value of bit in CF
93
94 Lbit_test_and_set_mp:
95 .long _COMM_PAGE_BTS+4
96 lock
97 bts %eax, (%edx)
98 ret
99
100 COMMPAGE_DESCRIPTOR(bit_test_and_set_mp,_COMM_PAGE_BTS,0,kUP)
101
102 Lbit_test_and_set_up:
103 .long _COMM_PAGE_BTS+4
104 bts %eax, (%edx)
105 ret
106
107 COMMPAGE_DESCRIPTOR(bit_test_and_set_up,_COMM_PAGE_BTS,kUP,0)
108
109 // This is a subroutine used by:
110 // bool OSAtomicTestAndClear( uint32_t n, void *value );
111 // It assumes n -> %eax, value -> %edx
112
113 // Returns: old value of bit in CF
114
115 Lbit_test_and_clear_mp:
116 .long _COMM_PAGE_BTC+4
117 lock
118 btc %eax, (%edx)
119 ret
120
121 COMMPAGE_DESCRIPTOR(bit_test_and_clear_mp,_COMM_PAGE_BTC,0,kUP)
122
123 Lbit_test_and_clear_up:
124 .long _COMM_PAGE_BTC+4
125 btc %eax, (%edx)
126 ret
127
128 COMMPAGE_DESCRIPTOR(bit_test_and_clear_up,_COMM_PAGE_BTC,kUP,0)
129
130 // This is a subroutine used by:
131 // int32_t OSAtomicAdd32( int32_t amt, int32_t *value );
132 // It assumes amt -> %eax, value -> %edx
133
134 // Returns: old value in %eax
135 // NB: OSAtomicAdd32 returns the new value, so clients will add amt to %eax
136
137 Latomic_add32_mp:
138 .long _COMM_PAGE_ATOMIC_ADD32+4
139 lock
140 xaddl %eax, (%edx)
141 ret
142
143 COMMPAGE_DESCRIPTOR(atomic_add32_mp,_COMM_PAGE_ATOMIC_ADD32,0,kUP)
144
145 Latomic_add32_up:
146 .long _COMM_PAGE_ATOMIC_ADD32+4
147 xaddl %eax, (%edx)
148 ret
149
150 COMMPAGE_DESCRIPTOR(atomic_add32_up,_COMM_PAGE_ATOMIC_ADD32,kUP,0)