blob: db2dd85918c2923ce2877e76eed8d7d693885b2a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Jakub Jelinek4732efb2005-09-06 15:16:25 -07002#ifndef _ASM_FUTEX_H
3#define _ASM_FUTEX_H
4
Jakub Jelineka192dc12006-09-26 14:00:56 -07005#include <linux/futex.h>
Jeff Dike730f4122008-04-30 00:54:49 -07006#include <linux/uaccess.h>
Jakub Jelineka192dc12006-09-26 14:00:56 -07007#include <asm/errno.h>
Jakub Jelinek4732efb2005-09-06 15:16:25 -07008
Jakub Jelineka192dc12006-09-26 14:00:56 -07009#define __futex_atomic_op1(insn, ret, oldval, uaddr, oparg) \
10do { \
11 register unsigned long r8 __asm ("r8") = 0; \
12 __asm__ __volatile__( \
13 " mf;; \n" \
14 "[1:] " insn ";; \n" \
15 " .xdata4 \"__ex_table\", 1b-., 2f-. \n" \
16 "[2:]" \
17 : "+r" (r8), "=r" (oldval) \
18 : "r" (uaddr), "r" (oparg) \
19 : "memory"); \
20 ret = r8; \
21} while (0)
22
23#define __futex_atomic_op2(insn, ret, oldval, uaddr, oparg) \
24do { \
25 register unsigned long r8 __asm ("r8") = 0; \
26 int val, newval; \
27 do { \
28 __asm__ __volatile__( \
29 " mf;; \n" \
30 "[1:] ld4 %3=[%4];; \n" \
31 " mov %2=%3 \n" \
32 insn ";; \n" \
33 " mov ar.ccv=%2;; \n" \
34 "[2:] cmpxchg4.acq %1=[%4],%3,ar.ccv;; \n" \
35 " .xdata4 \"__ex_table\", 1b-., 3f-.\n" \
36 " .xdata4 \"__ex_table\", 2b-., 3f-.\n" \
37 "[3:]" \
38 : "+r" (r8), "=r" (val), "=&r" (oldval), \
39 "=&r" (newval) \
40 : "r" (uaddr), "r" (oparg) \
41 : "memory"); \
42 if (unlikely (r8)) \
43 break; \
44 } while (unlikely (val != oldval)); \
45 ret = r8; \
46} while (0)
47
48static inline int
Jiri Slaby30d6e0a2017-08-24 09:31:05 +020049arch_futex_atomic_op_inuser(int op, int oparg, int *oval, u32 __user *uaddr)
Jakub Jelineka192dc12006-09-26 14:00:56 -070050{
Jakub Jelineka192dc12006-09-26 14:00:56 -070051 int oldval = 0, ret;
Jakub Jelineka192dc12006-09-26 14:00:56 -070052
Peter Zijlstraa8663742006-12-06 20:32:20 -080053 pagefault_disable();
Jakub Jelineka192dc12006-09-26 14:00:56 -070054
55 switch (op) {
56 case FUTEX_OP_SET:
57 __futex_atomic_op1("xchg4 %1=[%2],%3", ret, oldval, uaddr,
58 oparg);
59 break;
60 case FUTEX_OP_ADD:
61 __futex_atomic_op2("add %3=%3,%5", ret, oldval, uaddr, oparg);
62 break;
63 case FUTEX_OP_OR:
64 __futex_atomic_op2("or %3=%3,%5", ret, oldval, uaddr, oparg);
65 break;
66 case FUTEX_OP_ANDN:
67 __futex_atomic_op2("and %3=%3,%5", ret, oldval, uaddr,
68 ~oparg);
69 break;
70 case FUTEX_OP_XOR:
71 __futex_atomic_op2("xor %3=%3,%5", ret, oldval, uaddr, oparg);
72 break;
73 default:
74 ret = -ENOSYS;
75 }
76
Peter Zijlstraa8663742006-12-06 20:32:20 -080077 pagefault_enable();
Jakub Jelineka192dc12006-09-26 14:00:56 -070078
Jiri Slaby30d6e0a2017-08-24 09:31:05 +020079 if (!ret)
80 *oval = oldval;
81
Jakub Jelineka192dc12006-09-26 14:00:56 -070082 return ret;
83}
84
85static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080086futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
87 u32 oldval, u32 newval)
Jakub Jelineka192dc12006-09-26 14:00:56 -070088{
Michel Lespinasse8d7718a2011-03-10 18:50:58 -080089 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
Jakub Jelineka192dc12006-09-26 14:00:56 -070090 return -EFAULT;
91
92 {
Stephan Schreiber136f39d2013-03-19 15:22:27 -070093 register unsigned long r8 __asm ("r8") = 0;
Michel Lespinasse37a9d912011-03-10 18:48:51 -080094 unsigned long prev;
Jakub Jelineka192dc12006-09-26 14:00:56 -070095 __asm__ __volatile__(
96 " mf;; \n"
Luck, Tonyc76f39b2012-04-16 16:28:01 -070097 " mov ar.ccv=%4;; \n"
98 "[1:] cmpxchg4.acq %1=[%2],%3,ar.ccv \n"
Jakub Jelineka192dc12006-09-26 14:00:56 -070099 " .xdata4 \"__ex_table\", 1b-., 2f-. \n"
100 "[2:]"
Stephan Schreiber136f39d2013-03-19 15:22:27 -0700101 : "+r" (r8), "=&r" (prev)
Jakub Jelineka192dc12006-09-26 14:00:56 -0700102 : "r" (uaddr), "r" (newval),
103 "rO" ((long) (unsigned) oldval)
104 : "memory");
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800105 *uval = prev;
Jakub Jelineka192dc12006-09-26 14:00:56 -0700106 return r8;
107 }
108}
109
110#endif /* _ASM_FUTEX_H */