2 * Copyright (c) 2008 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 #include <sys/appleapiopts.h>
30 #include <machine/cpu_capabilities.h>
31 #include <machine/commpage.h>
32 #include <mach/i386/syscall_sw.h>
35 /* PREEMPTION FREE ZONE (PFZ)
37 * A portion of the commpage is speacial-cased by the kernel to be "preemption free",
38 * ie as if we had disabled interrupts in user mode. This facilitates writing
39 * "nearly-lockless" code, for example code that must be serialized by a spinlock but
40 * which we do not want to preempt while the spinlock is held.
42 * The PFZ is implemented by collecting all the "preemption-free" code into a single
43 * contiguous region of the commpage. Register %ebx is used as a flag register;
44 * before entering the PFZ, %ebx is cleared. If some event occurs that would normally
45 * result in a premption while in the PFZ, the kernel sets %ebx nonzero instead of
46 * preempting. Then, when the routine leaves the PFZ we check %ebx and
47 * if nonzero execute a special "pfz_exit" syscall to take the delayed preemption.
49 * PFZ code must bound the amount of time spent in the PFZ, in order to control
50 * latency. Backward branches are dangerous and must not be used in a way that
51 * could inadvertently create a long-running loop.
53 * Because they cannot be implemented reasonably without a lock, we put the "atomic"
54 * FIFO enqueue and dequeue in the PFZ. As long as we don't take a page fault trying to
55 * access queue elements, these implementations behave nearly-locklessly.
56 * But we still must take a spinlock to serialize, and in case of page faults.
60 * typedef volatile struct {
61 * void *opaque1; <-- ptr to first queue element or null
62 * void *opaque2; <-- ptr to last queue element or null
63 * int opaque3; <-- spinlock
66 * void OSAtomicFifoEnqueue( OSFifoQueueHead *list, void *new, size_t offset);
70 /* Subroutine to make a preempt syscall. Called when we notice %ebx is
71 * nonzero after returning from a PFZ subroutine.
72 * When we enter kernel:
73 * %edx = return address
75 * Destroys %eax, %ecx, and %edx.
77 COMMPAGE_FUNCTION_START(preempt, 32, 4)
78 popl %edx // get return address
79 movl %esp,%ecx // save stack ptr here
80 movl $(-58),%eax /* 58 = pfz_exit */
81 xorl %ebx,%ebx // clear "preemption pending" flag
83 COMMPAGE_DESCRIPTOR(preempt,_COMM_PAGE_PREEMPT,0,0)
86 /* Subroutine to back off if we cannot get the spinlock. Called
87 * after a few attempts inline in the PFZ subroutines. This code is
89 * %edi = ptr to queue head structure
90 * %ebx = preemption flag (nonzero if preemption pending)
93 COMMPAGE_FUNCTION_START(backoff, 32, 4)
94 testl %ebx,%ebx // does kernel want to preempt us?
96 xorl %ebx,%ebx // yes, clear flag
97 pushl %edx // preserve regs used by preempt syscall
99 COMMPAGE_CALL(_COMM_PAGE_PREEMPT,_COMM_PAGE_BACKOFF,backoff)
103 pause // SMT-friendly backoff
104 cmpl $0,8(%edi) // sniff the lockword
105 jnz 1b // loop if still taken
106 ret // lockword is free, so reenter PFZ
107 COMMPAGE_DESCRIPTOR(backoff,_COMM_PAGE_BACKOFF,0,0)
110 /* Preemption-free-zone routine to FIFO Enqueue:
111 * %edi = ptr to queue head structure
112 * %esi = ptr to element to enqueue
113 * %edx = offset of link field in elements
114 * %ebx = preemption flag (kernel sets nonzero if we should preempt)
117 COMMPAGE_FUNCTION_START(pfz_enqueue, 32, 4)
118 movl $0,(%edx,%esi) // zero forward link in new element
123 cmpxchgl %ecx, 8(%edi) // try to take the spinlock
129 cmpxchgl %ecx, 8(%edi) // try 2nd time to take the spinlock
135 cmpxchgl %ecx, 8(%edi) // try 3rd time to take the spinlock
138 COMMPAGE_CALL(_COMM_PAGE_BACKOFF,_COMM_PAGE_PFZ_ENQUEUE,pfz_enqueue)
139 jmp 1b // loop to try again
141 movl 4(%edi),%ecx // get ptr to last element in q
142 testl %ecx,%ecx // q null?
144 movl %esi,(%edi) // q empty so this is first element
147 movl %esi,(%edx,%ecx) // point to new element from last
149 movl %esi,4(%edi) // new element becomes last in q
150 movl $0,8(%edi) // unlock spinlock
152 COMMPAGE_DESCRIPTOR(pfz_enqueue,_COMM_PAGE_PFZ_ENQUEUE,0,0)
155 /* Preemption-free-zone routine to FIFO Dequeue:
156 * %edi = ptr to queue head structure
157 * %edx = offset of link field in elements
158 * %ebx = preemption flag (kernel sets nonzero if we should preempt)
160 * Returns with next element (or 0) in %eax.
163 COMMPAGE_FUNCTION_START(pfz_dequeue, 32, 4)
168 cmpxchgl %ecx, 8(%edi) // try to take the spinlock
174 cmpxchgl %ecx, 8(%edi) // try 2nd time to take the spinlock
180 cmpxchgl %ecx, 8(%edi) // try 3rd time to take the spinlock
183 COMMPAGE_CALL(_COMM_PAGE_BACKOFF,_COMM_PAGE_PFZ_DEQUEUE,pfz_dequeue)
184 jmp 1b // loop to try again
186 movl (%edi),%eax // get ptr to first element in q
187 testl %eax,%eax // q null?
189 movl (%edx,%eax),%esi// get ptr to 2nd element in q
190 testl %esi,%esi // is there a 2nd element?
192 movl %esi,4(%edi) // clear "last" field of q head
194 movl %esi,(%edi) // update "first" field of q head
196 movl $0,8(%edi) // unlock spinlock
198 COMMPAGE_DESCRIPTOR(pfz_dequeue,_COMM_PAGE_PFZ_DEQUEUE,0,0)
203 /************************* x86_64 versions follow **************************/
207 * typedef volatile struct {
208 * void *opaque1; <-- ptr to first queue element or null
209 * void *opaque2; <-- ptr to last queue element or null
210 * int opaque3; <-- spinlock
213 * void OSAtomicFifoEnqueue( OSFifoQueueHead *list, void *new, size_t offset);
217 /* Subroutine to make a preempt syscall. Called when we notice %ebx is
218 * nonzero after returning from a PFZ subroutine. Not in PFZ.
220 * All registers preserved (but does clear the %ebx preemption flag).
222 COMMPAGE_FUNCTION_START(preempt_64, 64, 4)
226 movl $(SYSCALL_CONSTRUCT_MACH(58)),%eax /* 58 = pfz_exit */
233 COMMPAGE_DESCRIPTOR(preempt_64,_COMM_PAGE_PREEMPT,0,0)
236 /* Subroutine to back off if we cannot get the spinlock. Called
237 * after a few attempts inline in the PFZ subroutines. This code is
239 * %rdi = ptr to queue head structure
240 * %ebx = preemption flag (nonzero if preemption pending)
243 COMMPAGE_FUNCTION_START(backoff_64, 64, 4)
244 testl %ebx,%ebx // does kernel want to preempt us?
246 COMMPAGE_CALL(_COMM_PAGE_PREEMPT,_COMM_PAGE_BACKOFF,backoff_64)
248 pause // SMT-friendly backoff
249 cmpl $0,16(%rdi) // sniff the lockword
250 jnz 1b // loop if still taken
251 ret // lockword is free, so reenter PFZ
252 COMMPAGE_DESCRIPTOR(backoff_64,_COMM_PAGE_BACKOFF,0,0)
255 /* Preemption-free-zone routine to FIFO Enqueue:
256 * %rdi = ptr to queue head structure
257 * %rsi = ptr to new element to enqueue
258 * %rdx = offset of link field in elements
259 * %ebx = preemption flag (kernel sets nonzero if we should preempt)
262 COMMPAGE_FUNCTION_START(pfz_enqueue_64, 64, 4)
263 movq $0,(%rdx,%rsi) // zero forward link in new element
268 cmpxchgl %ecx,16(%rdi) // try to take the spinlock
274 cmpxchgl %ecx,16(%rdi) // try 2nd time to take the spinlock
280 cmpxchgl %ecx,16(%rdi) // try 3rd time to take the spinlock
283 COMMPAGE_CALL(_COMM_PAGE_BACKOFF,_COMM_PAGE_PFZ_ENQUEUE,pfz_enqueue_64)
284 jmp 1b // loop to try again
286 movq 8(%rdi),%rcx // get ptr to last element in q
287 testq %rcx,%rcx // q null?
289 movq %rsi,(%rdi) // q empty so this is first element
292 movq %rsi,(%rdx,%rcx) // point to new element from last
294 movq %rsi,8(%rdi) // new element becomes last in q
295 movl $0,16(%rdi) // unlock spinlock
297 COMMPAGE_DESCRIPTOR(pfz_enqueue_64,_COMM_PAGE_PFZ_ENQUEUE,0,0)
301 /* Preemption-free-zone routine to FIFO Dequeue:
302 * %rdi = ptr to queue head structure
303 * %rdx = offset of link field in elements
304 * %ebx = preemption flag (kernel sets nonzero if we should preempt)
306 * Returns with next element (or 0) in %rax.
309 COMMPAGE_FUNCTION_START(pfz_dequeue_64, 64, 4)
314 cmpxchgl %ecx,16(%rdi) // try to take the spinlock
320 cmpxchgl %ecx,16(%rdi) // try 2nd time to take the spinlock
326 cmpxchgl %ecx,16(%rdi) // try 3rd time to take the spinlock
329 COMMPAGE_CALL(_COMM_PAGE_BACKOFF,_COMM_PAGE_PFZ_DEQUEUE,pfz_dequeue_64)
330 jmp 1b // loop to try again
332 movq (%rdi),%rax // get ptr to first element in q
333 testq %rax,%rax // q null?
335 movq (%rdx,%rax),%rsi// get ptr to 2nd element in q
336 testq %rsi,%rsi // is there a 2nd element?
338 movq %rsi,8(%rdi) // no - clear "last" field of q head
340 movq %rsi,(%rdi) // update "first" field of q head
342 movl $0,16(%rdi) // unlock spinlock
344 COMMPAGE_DESCRIPTOR(pfz_dequeue_64,_COMM_PAGE_PFZ_DEQUEUE,0,0)