Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 2 | #ifndef _ASM_X86_FUTEX_H |
| 3 | #define _ASM_X86_FUTEX_H |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 4 | |
| 5 | #ifdef __KERNEL__ |
| 6 | |
| 7 | #include <linux/futex.h> |
Jeff Dike | 730f412 | 2008-04-30 00:54:49 -0700 | [diff] [blame] | 8 | #include <linux/uaccess.h> |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 9 | |
| 10 | #include <asm/asm.h> |
| 11 | #include <asm/errno.h> |
| 12 | #include <asm/processor.h> |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 13 | #include <asm/smap.h> |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 14 | |
| 15 | #define __futex_atomic_op1(insn, ret, oldval, uaddr, oparg) \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 16 | asm volatile("\t" ASM_STAC "\n" \ |
| 17 | "1:\t" insn "\n" \ |
| 18 | "2:\t" ASM_CLAC "\n" \ |
| 19 | "\t.section .fixup,\"ax\"\n" \ |
Joe Perches | 9407913 | 2008-03-23 01:02:12 -0700 | [diff] [blame] | 20 | "3:\tmov\t%3, %1\n" \ |
| 21 | "\tjmp\t2b\n" \ |
| 22 | "\t.previous\n" \ |
| 23 | _ASM_EXTABLE(1b, 3b) \ |
| 24 | : "=r" (oldval), "=r" (ret), "+m" (*uaddr) \ |
| 25 | : "i" (-EFAULT), "0" (oparg), "1" (0)) |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 26 | |
| 27 | #define __futex_atomic_op2(insn, ret, oldval, uaddr, oparg) \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 28 | asm volatile("\t" ASM_STAC "\n" \ |
| 29 | "1:\tmovl %2, %0\n" \ |
Joe Perches | 9407913 | 2008-03-23 01:02:12 -0700 | [diff] [blame] | 30 | "\tmovl\t%0, %3\n" \ |
| 31 | "\t" insn "\n" \ |
Mathieu Desnoyers | 1f49a2c | 2008-08-15 12:45:09 -0400 | [diff] [blame] | 32 | "2:\t" LOCK_PREFIX "cmpxchgl %3, %2\n" \ |
Joe Perches | 9407913 | 2008-03-23 01:02:12 -0700 | [diff] [blame] | 33 | "\tjnz\t1b\n" \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 34 | "3:\t" ASM_CLAC "\n" \ |
| 35 | "\t.section .fixup,\"ax\"\n" \ |
Joe Perches | 9407913 | 2008-03-23 01:02:12 -0700 | [diff] [blame] | 36 | "4:\tmov\t%5, %1\n" \ |
| 37 | "\tjmp\t3b\n" \ |
| 38 | "\t.previous\n" \ |
| 39 | _ASM_EXTABLE(1b, 4b) \ |
| 40 | _ASM_EXTABLE(2b, 4b) \ |
| 41 | : "=&a" (oldval), "=&r" (ret), \ |
| 42 | "+m" (*uaddr), "=&r" (tem) \ |
| 43 | : "r" (oparg), "i" (-EFAULT), "1" (0)) |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 44 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 45 | static inline int arch_futex_atomic_op_inuser(int op, int oparg, int *oval, |
| 46 | u32 __user *uaddr) |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 47 | { |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 48 | int oldval = 0, ret, tem; |
| 49 | |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 50 | pagefault_disable(); |
| 51 | |
| 52 | switch (op) { |
| 53 | case FUTEX_OP_SET: |
| 54 | __futex_atomic_op1("xchgl %0, %2", ret, oldval, uaddr, oparg); |
| 55 | break; |
| 56 | case FUTEX_OP_ADD: |
Mathieu Desnoyers | 1f49a2c | 2008-08-15 12:45:09 -0400 | [diff] [blame] | 57 | __futex_atomic_op1(LOCK_PREFIX "xaddl %0, %2", ret, oldval, |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 58 | uaddr, oparg); |
| 59 | break; |
| 60 | case FUTEX_OP_OR: |
| 61 | __futex_atomic_op2("orl %4, %3", ret, oldval, uaddr, oparg); |
| 62 | break; |
| 63 | case FUTEX_OP_ANDN: |
| 64 | __futex_atomic_op2("andl %4, %3", ret, oldval, uaddr, ~oparg); |
| 65 | break; |
| 66 | case FUTEX_OP_XOR: |
| 67 | __futex_atomic_op2("xorl %4, %3", ret, oldval, uaddr, oparg); |
| 68 | break; |
| 69 | default: |
| 70 | ret = -ENOSYS; |
| 71 | } |
| 72 | |
| 73 | pagefault_enable(); |
| 74 | |
Jiri Slaby | 30d6e0a | 2017-08-24 09:31:05 +0200 | [diff] [blame] | 75 | if (!ret) |
| 76 | *oval = oldval; |
| 77 | |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 78 | return ret; |
| 79 | } |
| 80 | |
Michel Lespinasse | 8d7718a | 2011-03-10 18:50:58 -0800 | [diff] [blame] | 81 | static inline int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr, |
| 82 | u32 oldval, u32 newval) |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 83 | { |
Qiaowei Ren | 0ee3b6f | 2013-12-14 14:25:03 +0800 | [diff] [blame] | 84 | return user_atomic_cmpxchg_inatomic(uval, uaddr, oldval, newval); |
Thomas Gleixner | 2f18e47 | 2008-01-30 13:30:20 +0100 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | #endif |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 88 | #endif /* _ASM_X86_FUTEX_H */ |