]> git.saurik.com Git - apple/libc.git/blob - x86_64/sys/spinlocks_asm.s
7bc0fbd27b1e2f66f132890ac87397ee5d69b145
[apple/libc.git] / x86_64 / sys / spinlocks_asm.s
1 /*
2 * Copyright (c) 2003-2009 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 #include <sys/appleapiopts.h>
30 #include <machine/cpu_capabilities.h>
31 #include <platfunc.h>
32 #include <mach/i386/syscall_sw.h>
33
34 PLATFUNC_FUNCTION_START(OSSpinLockTry, up, 64, 4)
35 PLATFUNC_FUNCTION_START(_spin_lock_try, up, 64, 4)
36 xorl %eax, %eax
37 orl $-1, %edx
38 cmpxchgl %edx, (%rdi)
39 setz %dl
40 movzbl %dl, %eax
41 ret
42 PLATFUNC_DESCRIPTOR(OSSpinLockTry,up,kUP,0)
43 PLATFUNC_DESCRIPTOR(_spin_lock_try,up,kUP,0)
44
45
46 PLATFUNC_FUNCTION_START_GENERIC(OSSpinLockTry, mp, 64, 4)
47 PLATFUNC_FUNCTION_START_GENERIC(_spin_lock_try, mp, 64, 4)
48 xorl %eax, %eax
49 orl $-1, %edx
50 lock
51 cmpxchgl %edx, (%rdi)
52 setz %dl
53 movzbl %dl, %eax
54 ret
55 PLATFUNC_DESCRIPTOR(OSSpinLockTry,mp,0,kUP)
56 PLATFUNC_DESCRIPTOR(_spin_lock_try,mp,0,kUP)
57
58
59 PLATFUNC_FUNCTION_START(OSSpinLockLock, up, 64, 4)
60 PLATFUNC_FUNCTION_START(_spin_lock, up, 64, 4)
61 PLATFUNC_FUNCTION_START(spin_lock, up, 64, 4)
62 movq %rdi,%r8
63 0:
64 xorl %eax, %eax
65 orl $-1, %edx
66 cmpxchgl %edx, (%r8)
67 jnz 1f
68 ret
69 1:
70 /* failed to get lock so relinquish the processor immediately on UP */
71 xorl %edi,%edi /* THREAD_NULL */
72 movl $1,%esi /* SWITCH_OPTION_DEPRESS */
73 movl $1,%edx /* 1 ms */
74 movl $(SYSCALL_CONSTRUCT_MACH(61)),%eax /* 61 = thread_switch */
75 syscall
76 jmp 0b
77 PLATFUNC_DESCRIPTOR(OSSpinLockLock,up,kUP,0)
78 PLATFUNC_DESCRIPTOR(_spin_lock,up,kUP,0)
79 PLATFUNC_DESCRIPTOR(spin_lock,up,kUP,0)
80
81
82 PLATFUNC_FUNCTION_START_GENERIC(OSSpinLockLock, mp, 64, 4)
83 PLATFUNC_FUNCTION_START_GENERIC(_spin_lock, mp, 64, 4)
84 PLATFUNC_FUNCTION_START_GENERIC(spin_lock, mp, 64, 4)
85 movq %rdi,%r8
86 0:
87 xorl %eax, %eax
88 orl $-1, %edx
89 lock
90 cmpxchgl %edx, (%r8)
91 jnz 1f
92 ret
93 1:
94 xorl %eax, %eax
95 movl $(MP_SPIN_TRIES), %edx
96 2: /* spin for awhile before relinquish */
97 pause
98 cmpl %eax, (%r8)
99 jz 0b
100 decl %edx
101 jnz 2b
102 /* failed to get lock after spinning so relinquish */
103 xorl %edi,%edi /* THREAD_NULL */
104 movl $1,%esi /* SWITCH_OPTION_DEPRESS */
105 movl $1,%edx /* 1 ms */
106 movl $(SYSCALL_CONSTRUCT_MACH(61)),%eax /* 61 = thread_switch */
107 syscall
108 jmp 0b
109 PLATFUNC_DESCRIPTOR(OSSpinLockLock,mp,0,kUP)
110 PLATFUNC_DESCRIPTOR(_spin_lock,mp,0,kUP)
111 PLATFUNC_DESCRIPTOR(spin_lock,mp,0,kUP)
112
113 // void OSSpinLockUnlock( OSSpinLock *lock );
114 .align 2, 0x90
115 .globl _OSSpinLockUnlock
116 .globl _spin_unlock
117 .globl __spin_unlock
118 _OSSpinLockUnlock:
119 _spin_unlock:
120 __spin_unlock:
121 movl $0, (%rdi)
122 ret
123