xref: /openbsd-src/lib/libc/arch/aarch64/gen/_atomic_lock.c (revision a7720a9992916b369aecbf7f5386e0c41a6850a2)
1*a7720a99Skettenis /*	$OpenBSD: _atomic_lock.c,v 1.2 2018/05/17 20:28:32 kettenis Exp $	*/
259231d1eSguenther 
359231d1eSguenther /*
459231d1eSguenther  * Copyright (c) 2004 Dale Rahn. All rights reserved.
559231d1eSguenther  *
659231d1eSguenther  * Redistribution and use in source and binary forms, with or without
759231d1eSguenther  * modification, are permitted provided that the following conditions
859231d1eSguenther  * are met:
959231d1eSguenther  * 1. Redistributions of source code must retain the above copyright
1059231d1eSguenther  *    notice, this list of conditions and the following disclaimer.
1159231d1eSguenther  * 2. Redistributions in binary form must reproduce the above copyright
1259231d1eSguenther  *    notice, this list of conditions and the following disclaimer in the
1359231d1eSguenther  *    documentation and/or other materials provided with the distribution.
1459231d1eSguenther  *
1559231d1eSguenther  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1659231d1eSguenther  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1759231d1eSguenther  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1859231d1eSguenther  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1959231d1eSguenther  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2059231d1eSguenther  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2159231d1eSguenther  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2259231d1eSguenther  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2359231d1eSguenther  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2459231d1eSguenther  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2559231d1eSguenther  */
2659231d1eSguenther 
2759231d1eSguenther /*
2859231d1eSguenther  * Atomic lock for arm
2959231d1eSguenther  */
3059231d1eSguenther 
3159231d1eSguenther #include <sys/types.h>
3259231d1eSguenther #include <machine/spinlock.h>
3359231d1eSguenther 
3459231d1eSguenther int
_atomic_lock(volatile _atomic_lock_t * lock)3559231d1eSguenther _atomic_lock(volatile _atomic_lock_t *lock)
3659231d1eSguenther {
3759231d1eSguenther 	_atomic_lock_t old = 0;
3859231d1eSguenther 	uint32_t scratch = 0;
3959231d1eSguenther 
4059231d1eSguenther 	__asm__("1: ldaxr %w0, [%x1]       \n"
4159231d1eSguenther 		"   stlxr %w2, %w3, [%x1]  \n"
4259231d1eSguenther 		"   cmp %w2, #0            \n"
4359231d1eSguenther 		"   bne 1b                 \n"
4459231d1eSguenther 		: "+r" (old), "+r" (lock), "+r" (scratch)
4559231d1eSguenther 		: "r" (_ATOMIC_LOCK_LOCKED));
4659231d1eSguenther 
4759231d1eSguenther 	return (old != _ATOMIC_LOCK_UNLOCKED);
4859231d1eSguenther }
49