futex.h 3.4 KB
Newer Older
C
Catalin Marinas 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * Copyright (C) 2012 ARM Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __ASM_FUTEX_H
#define __ASM_FUTEX_H

#ifdef __KERNEL__

#include <linux/futex.h>
#include <linux/uaccess.h>
23

C
Catalin Marinas 已提交
24 25
#include <asm/errno.h>

26 27
#define FUTEX_MAX_LOOPS	128 /* What's the largest number you can think of? */

C
Catalin Marinas 已提交
28
#define __futex_atomic_op(insn, ret, oldval, uaddr, tmp, oparg)		\
29
do {									\
30 31
	unsigned int loops = FUTEX_MAX_LOOPS;				\
									\
32
	uaccess_enable();						\
C
Catalin Marinas 已提交
33
	asm volatile(							\
34
"	prfm	pstl1strm, %2\n"					\
35
"1:	ldxr	%w1, %2\n"						\
C
Catalin Marinas 已提交
36
	insn "\n"							\
37
"2:	stlxr	%w0, %w3, %2\n"						\
38 39 40 41
"	cbz	%w0, 3f\n"						\
"	sub	%w4, %w4, %w0\n"					\
"	cbnz	%w4, 1b\n"						\
"	mov	%w0, %w7\n"						\
C
Catalin Marinas 已提交
42
"3:\n"									\
43
"	dmb	ish\n"							\
C
Catalin Marinas 已提交
44
"	.pushsection .fixup,\"ax\"\n"					\
45
"	.align	2\n"							\
46
"4:	mov	%w0, %w6\n"						\
C
Catalin Marinas 已提交
47 48
"	b	3b\n"							\
"	.popsection\n"							\
49 50
	_ASM_EXTABLE(1b, 4b)						\
	_ASM_EXTABLE(2b, 4b)						\
51 52 53
	: "=&r" (ret), "=&r" (oldval), "+Q" (*uaddr), "=&r" (tmp),	\
	  "+r" (loops)							\
	: "r" (oparg), "Ir" (-EFAULT), "Ir" (-EAGAIN)			\
54 55 56
	: "memory");							\
	uaccess_disable();						\
} while (0)
C
Catalin Marinas 已提交
57 58

static inline int
59
arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *_uaddr)
C
Catalin Marinas 已提交
60
{
61
	int oldval = 0, ret, tmp;
62
	u32 __user *uaddr = __uaccess_mask_ptr(_uaddr);
C
Catalin Marinas 已提交
63

64
	pagefault_disable();
C
Catalin Marinas 已提交
65 66 67

	switch (op) {
	case FUTEX_OP_SET:
68
		__futex_atomic_op("mov	%w3, %w5",
C
Catalin Marinas 已提交
69 70 71
				  ret, oldval, uaddr, tmp, oparg);
		break;
	case FUTEX_OP_ADD:
72
		__futex_atomic_op("add	%w3, %w1, %w5",
C
Catalin Marinas 已提交
73 74 75
				  ret, oldval, uaddr, tmp, oparg);
		break;
	case FUTEX_OP_OR:
76
		__futex_atomic_op("orr	%w3, %w1, %w5",
C
Catalin Marinas 已提交
77 78 79
				  ret, oldval, uaddr, tmp, oparg);
		break;
	case FUTEX_OP_ANDN:
80
		__futex_atomic_op("and	%w3, %w1, %w5",
C
Catalin Marinas 已提交
81 82 83
				  ret, oldval, uaddr, tmp, ~oparg);
		break;
	case FUTEX_OP_XOR:
84
		__futex_atomic_op("eor	%w3, %w1, %w5",
C
Catalin Marinas 已提交
85 86 87 88 89 90
				  ret, oldval, uaddr, tmp, oparg);
		break;
	default:
		ret = -ENOSYS;
	}

91
	pagefault_enable();
C
Catalin Marinas 已提交
92

93 94 95
	if (!ret)
		*oval = oldval;

C
Catalin Marinas 已提交
96 97 98 99
	return ret;
}

static inline int
100
futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *_uaddr,
C
Catalin Marinas 已提交
101 102 103
			      u32 oldval, u32 newval)
{
	int ret = 0;
104
	unsigned int loops = FUTEX_MAX_LOOPS;
C
Catalin Marinas 已提交
105
	u32 val, tmp;
106
	u32 __user *uaddr;
C
Catalin Marinas 已提交
107

108
	if (!access_ok(VERIFY_WRITE, _uaddr, sizeof(u32)))
C
Catalin Marinas 已提交
109 110
		return -EFAULT;

111
	uaddr = __uaccess_mask_ptr(_uaddr);
112
	uaccess_enable();
C
Catalin Marinas 已提交
113
	asm volatile("// futex_atomic_cmpxchg_inatomic\n"
114
"	prfm	pstl1strm, %2\n"
115
"1:	ldxr	%w1, %2\n"
116 117 118 119 120 121 122
"	sub	%w3, %w1, %w5\n"
"	cbnz	%w3, 4f\n"
"2:	stlxr	%w3, %w6, %2\n"
"	cbz	%w3, 3f\n"
"	sub	%w4, %w4, %w3\n"
"	cbnz	%w4, 1b\n"
"	mov	%w0, %w8\n"
C
Catalin Marinas 已提交
123
"3:\n"
124 125
"	dmb	ish\n"
"4:\n"
C
Catalin Marinas 已提交
126
"	.pushsection .fixup,\"ax\"\n"
127 128
"5:	mov	%w0, %w7\n"
"	b	4b\n"
C
Catalin Marinas 已提交
129
"	.popsection\n"
130 131 132 133
	_ASM_EXTABLE(1b, 5b)
	_ASM_EXTABLE(2b, 5b)
	: "+r" (ret), "=&r" (val), "+Q" (*uaddr), "=&r" (tmp), "+r" (loops)
	: "r" (oldval), "r" (newval), "Ir" (-EFAULT), "Ir" (-EAGAIN)
134
	: "memory");
135
	uaccess_disable();
C
Catalin Marinas 已提交
136 137 138 139 140 141 142

	*uval = val;
	return ret;
}

#endif /* __KERNEL__ */
#endif /* __ASM_FUTEX_H */