]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - arch/tile/include/asm/futex.h
futex: Remove duplicated code and fix undefined behaviour
[mirror_ubuntu-bionic-kernel.git] / arch / tile / include / asm / futex.h
CommitLineData
867e359b
CM
1/*
2 * Copyright 2010 Tilera Corporation. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation, version 2.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
11 * NON INFRINGEMENT. See the GNU General Public License for
12 * more details.
13 *
14 * These routines make two important assumptions:
15 *
16 * 1. atomic_t is really an int and can be freely cast back and forth
17 * (validated in __init_atomic_per_cpu).
18 *
19 * 2. userspace uses sys_cmpxchg() for all atomic operations, thus using
20 * the same locking convention that all the kernel atomic routines use.
21 */
22
23#ifndef _ASM_TILE_FUTEX_H
24#define _ASM_TILE_FUTEX_H
25
26#ifndef __ASSEMBLY__
27
28#include <linux/futex.h>
29#include <linux/uaccess.h>
30#include <linux/errno.h>
47d632f9 31#include <asm/atomic.h>
867e359b 32
47d632f9
CM
33/*
34 * Support macros for futex operations. Do not use these macros directly.
35 * They assume "ret", "val", "oparg", and "uaddr" in the lexical context.
36 * __futex_cmpxchg() additionally assumes "oldval".
37 */
38
39#ifdef __tilegx__
40
41#define __futex_asm(OP) \
42 asm("1: {" #OP " %1, %3, %4; movei %0, 0 }\n" \
43 ".pushsection .fixup,\"ax\"\n" \
44 "0: { movei %0, %5; j 9f }\n" \
45 ".section __ex_table,\"a\"\n" \
d4d9eab4 46 ".align 8\n" \
47d632f9
CM
47 ".quad 1b, 0b\n" \
48 ".popsection\n" \
49 "9:" \
50 : "=r" (ret), "=r" (val), "+m" (*(uaddr)) \
51 : "r" (uaddr), "r" (oparg), "i" (-EFAULT))
52
53#define __futex_set() __futex_asm(exch4)
54#define __futex_add() __futex_asm(fetchadd4)
55#define __futex_or() __futex_asm(fetchor4)
56#define __futex_andn() ({ oparg = ~oparg; __futex_asm(fetchand4); })
57#define __futex_cmpxchg() \
58 ({ __insn_mtspr(SPR_CMPEXCH_VALUE, oldval); __futex_asm(cmpexch4); })
59
60#define __futex_xor() \
61 ({ \
62 u32 oldval, n = oparg; \
63 if ((ret = __get_user(oldval, uaddr)) == 0) { \
64 do { \
65 oparg = oldval ^ n; \
66 __futex_cmpxchg(); \
67 } while (ret == 0 && oldval != val); \
68 } \
69 })
70
71/* No need to prefetch, since the atomic ops go to the home cache anyway. */
72#define __futex_prolog()
867e359b 73
867e359b 74#else
47d632f9
CM
75
76#define __futex_call(FN) \
77 { \
78 struct __get_user gu = FN((u32 __force *)uaddr, lock, oparg); \
79 val = gu.val; \
80 ret = gu.err; \
867e359b 81 }
47d632f9 82
b7271b9f
PZ
83#define __futex_set() __futex_call(__atomic32_xchg)
84#define __futex_add() __futex_call(__atomic32_xchg_add)
85#define __futex_or() __futex_call(__atomic32_fetch_or)
86#define __futex_andn() __futex_call(__atomic32_fetch_andn)
87#define __futex_xor() __futex_call(__atomic32_fetch_xor)
47d632f9
CM
88
89#define __futex_cmpxchg() \
90 { \
b7271b9f
PZ
91 struct __get_user gu = __atomic32_cmpxchg((u32 __force *)uaddr, \
92 lock, oldval, oparg); \
47d632f9
CM
93 val = gu.val; \
94 ret = gu.err; \
95 }
96
97/*
98 * Find the lock pointer for the atomic calls to use, and issue a
99 * prefetch to the user address to bring it into cache. Similar to
100 * __atomic_setup(), but we can't do a read into the L1 since it might
101 * fault; instead we do a prefetch into the L2.
102 */
103#define __futex_prolog() \
104 int *lock; \
105 __insn_prefetch(uaddr); \
106 lock = __atomic_hashed_lock((int __force *)uaddr)
867e359b
CM
107#endif
108
30d6e0a4
JS
109static inline int arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval,
110 u32 __user *uaddr)
867e359b 111{
47d632f9
CM
112 int uninitialized_var(val), ret;
113
114 __futex_prolog();
115
116 /* The 32-bit futex code makes this assumption, so validate it here. */
117 BUILD_BUG_ON(sizeof(atomic_t) != sizeof(int));
867e359b 118
867e359b
CM
119 pagefault_disable();
120 switch (op) {
121 case FUTEX_OP_SET:
47d632f9 122 __futex_set();
867e359b
CM
123 break;
124 case FUTEX_OP_ADD:
47d632f9 125 __futex_add();
867e359b
CM
126 break;
127 case FUTEX_OP_OR:
47d632f9 128 __futex_or();
867e359b
CM
129 break;
130 case FUTEX_OP_ANDN:
47d632f9 131 __futex_andn();
867e359b
CM
132 break;
133 case FUTEX_OP_XOR:
47d632f9 134 __futex_xor();
867e359b
CM
135 break;
136 default:
47d632f9
CM
137 ret = -ENOSYS;
138 break;
867e359b
CM
139 }
140 pagefault_enable();
141
30d6e0a4
JS
142 if (!ret)
143 *oval = val;
144
867e359b
CM
145 return ret;
146}
147
8d7718aa 148static inline int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
47d632f9 149 u32 oldval, u32 oparg)
867e359b 150{
47d632f9
CM
151 int ret, val;
152
153 __futex_prolog();
867e359b 154
8d7718aa 155 if (!access_ok(VERIFY_WRITE, uaddr, sizeof(u32)))
867e359b
CM
156 return -EFAULT;
157
47d632f9 158 __futex_cmpxchg();
867e359b 159
47d632f9
CM
160 *uval = val;
161 return ret;
162}
0707ad30 163
867e359b
CM
164#endif /* !__ASSEMBLY__ */
165
166#endif /* _ASM_TILE_FUTEX_H */