blob: f0d8b1c51343c813761416d147255aeac9e74140 [file] [log] [blame]
Jeff Dikef8aaeace2006-01-08 01:01:32 -08001#ifndef _ASM_GENERIC_FUTEX_H
2#define _ASM_GENERIC_FUTEX_H
3
Jeff Dikef8aaeace2006-01-08 01:01:32 -08004#include <linux/futex.h>
Jeff Dike730f4122008-04-30 00:54:49 -07005#include <linux/uaccess.h>
Jeff Dikef8aaeace2006-01-08 01:01:32 -08006#include <asm/errno.h>
Jeff Dikef8aaeace2006-01-08 01:01:32 -08007
Ley Foon Tan00f634b2014-11-06 15:19:34 +08008#ifndef CONFIG_SMP
9/*
10 * The following implementation only for uniprocessor machines.
David Hildenbrandf3dae072015-05-11 17:52:13 +020011 * It relies on preempt_disable() ensuring mutual exclusion.
Ley Foon Tan00f634b2014-11-06 15:19:34 +080012 *
13 */
14
15/**
Jiri Slaby81da9f82017-08-24 09:31:05 +020016 * arch_futex_atomic_op_inuser() - Atomic arithmetic operation with constant
Ley Foon Tan00f634b2014-11-06 15:19:34 +080017 * argument and comparison of the previous
18 * futex value with another constant.
19 *
20 * @encoded_op: encoded operation to execute
21 * @uaddr: pointer to user space address
22 *
23 * Return:
24 * 0 - On success
25 * <0 - On error
26 */
27static inline int
Jiri Slaby81da9f82017-08-24 09:31:05 +020028arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
Ley Foon Tan00f634b2014-11-06 15:19:34 +080029{
Ley Foon Tan00f634b2014-11-06 15:19:34 +080030 int oldval, ret;
31 u32 tmp;
32
David Hildenbrandf3dae072015-05-11 17:52:13 +020033 preempt_disable();
Ley Foon Tan00f634b2014-11-06 15:19:34 +080034 pagefault_disable();
35
36 ret = -EFAULT;
37 if (unlikely(get_user(oldval, uaddr) != 0))
38 goto out_pagefault_enable;
39
40 ret = 0;
41 tmp = oldval;
42
43 switch (op) {
44 case FUTEX_OP_SET:
45 tmp = oparg;
46 break;
47 case FUTEX_OP_ADD:
48 tmp += oparg;
49 break;
50 case FUTEX_OP_OR:
51 tmp |= oparg;
52 break;
53 case FUTEX_OP_ANDN:
54 tmp &= ~oparg;
55 break;
56 case FUTEX_OP_XOR:
57 tmp ^= oparg;
58 break;
59 default:
60 ret = -ENOSYS;
61 }
62
63 if (ret == 0 && unlikely(put_user(tmp, uaddr) != 0))
64 ret = -EFAULT;
65
66out_pagefault_enable:
67 pagefault_enable();
David Hildenbrandf3dae072015-05-11 17:52:13 +020068 preempt_enable();
Ley Foon Tan00f634b2014-11-06 15:19:34 +080069
Jiri Slaby81da9f82017-08-24 09:31:05 +020070 if (ret == 0)
71 *oval = oldval;
72
Ley Foon Tan00f634b2014-11-06 15:19:34 +080073 return ret;
74}
75
76/**
77 * futex_atomic_cmpxchg_inatomic() - Compare and exchange the content of the
78 * uaddr with newval if the current value is
79 * oldval.
80 * @uval: pointer to store content of @uaddr
81 * @uaddr: pointer to user space address
82 * @oldval: old value
83 * @newval: new value to store to @uaddr
84 *
85 * Return:
86 * 0 - On success
87 * <0 - On error
88 */
89static inline int
90futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
91 u32 oldval, u32 newval)
92{
93 u32 val;
94
David Hildenbrandd9b9ff82015-05-11 17:52:14 +020095 preempt_disable();
Romain Perierfba7cd62016-04-14 15:36:03 +020096 if (unlikely(get_user(val, uaddr) != 0)) {
97 preempt_enable();
Ley Foon Tan00f634b2014-11-06 15:19:34 +080098 return -EFAULT;
Romain Perierfba7cd62016-04-14 15:36:03 +020099 }
Ley Foon Tan00f634b2014-11-06 15:19:34 +0800100
Romain Perierfba7cd62016-04-14 15:36:03 +0200101 if (val == oldval && unlikely(put_user(newval, uaddr) != 0)) {
102 preempt_enable();
Ley Foon Tan00f634b2014-11-06 15:19:34 +0800103 return -EFAULT;
Romain Perierfba7cd62016-04-14 15:36:03 +0200104 }
Ley Foon Tan00f634b2014-11-06 15:19:34 +0800105
106 *uval = val;
David Hildenbrandd9b9ff82015-05-11 17:52:14 +0200107 preempt_enable();
Ley Foon Tan00f634b2014-11-06 15:19:34 +0800108
109 return 0;
110}
111
112#else
Jeff Dikef8aaeace2006-01-08 01:01:32 -0800113static inline int
Jiri Slaby81da9f82017-08-24 09:31:05 +0200114arch_futex_atomic_op_inuser(int op, u32 oparg, int *oval, u32 __user *uaddr)
Jeff Dikef8aaeace2006-01-08 01:01:32 -0800115{
Jeff Dikef8aaeace2006-01-08 01:01:32 -0800116 int oldval = 0, ret;
Jeff Dikef8aaeace2006-01-08 01:01:32 -0800117
Peter Zijlstraa8663742006-12-06 20:32:20 -0800118 pagefault_disable();
Jeff Dikef8aaeace2006-01-08 01:01:32 -0800119
120 switch (op) {
121 case FUTEX_OP_SET:
122 case FUTEX_OP_ADD:
123 case FUTEX_OP_OR:
124 case FUTEX_OP_ANDN:
125 case FUTEX_OP_XOR:
126 default:
127 ret = -ENOSYS;
128 }
129
Peter Zijlstraa8663742006-12-06 20:32:20 -0800130 pagefault_enable();
Jeff Dikef8aaeace2006-01-08 01:01:32 -0800131
Jiri Slaby81da9f82017-08-24 09:31:05 +0200132 if (!ret)
133 *oval = oldval;
134
Jeff Dikef8aaeace2006-01-08 01:01:32 -0800135 return ret;
136}
137
Ingo Molnare9056f12006-03-27 01:16:21 -0800138static inline int
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800139futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
140 u32 oldval, u32 newval)
Ingo Molnare9056f12006-03-27 01:16:21 -0800141{
142 return -ENOSYS;
143}
144
Ley Foon Tan00f634b2014-11-06 15:19:34 +0800145#endif /* CONFIG_SMP */
Jeff Dikef8aaeace2006-01-08 01:01:32 -0800146#endif