1 /*	$OpenBSD: _atomic_lock.c,v 1.2 2020/06/25 02:22:31 drahn Exp $	*/
2 /*
3  * Copyright (c) 1998 Dale Rahn <drahn@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 /*
19  * Atomic lock for powerpc
20  */
21 
22 #include <machine/spinlock.h>
23 
24 int
25 _atomic_lock(volatile _atomic_lock_t *lock)
26 {
27 	_atomic_lock_t old;
28 
29 	__asm__("1: lwarx 0,0,%1   \n"
30 		"   stwcx. %2,0,%1 \n"
31 		"   bne- 1b        \n"
32 		"   mr %0, 0	   \n"
33 		: "=r" (old), "=r" (lock)
34 		: "r" (_ATOMIC_LOCK_LOCKED), "1" (lock) : "0"
35 	);
36 
37 	return (old != _ATOMIC_LOCK_UNLOCKED);
38 
39 	/*
40 	 * Dale <drahn@openbsd.org> says:
41 	 *   Side note. to prevent two processes from accessing
42 	 *   the same address with the lwarx in one instruction
43 	 *   and the stwcx in another process, the current powerpc
44 	 *   kernel uses a stwcx instruction without the corresponding
45 	 *   lwarx which causes any reservation of a process
46 	 *   to be removed.  if a context switch occurs
47 	 *   between the two accesses the store will not occur
48 	 *   and the condition code will cause it to loop. If on
49 	 *   a dual processor machine, the reserve will cause
50 	 *   appropriate bus cycle accesses to notify other
51 	 *   processors.
52 	 */
53 }
54