blob: 8d39760bae68f9f4e65bbbf4d42d5db25ef4958c [file] [log] [blame]
Gerald Schaefer59f35d52006-12-04 15:40:45 +01001/*
Gerald Schaeferc1821c22007-02-05 21:18:17 +01002 * User access functions based on page table walks for enhanced
3 * system layout without hardware support.
Gerald Schaefer59f35d52006-12-04 15:40:45 +01004 *
Gerald Schaefer4db84d42012-09-10 16:43:26 +02005 * Copyright IBM Corp. 2006, 2012
Gerald Schaefer59f35d52006-12-04 15:40:45 +01006 * Author(s): Gerald Schaefer (gerald.schaefer@de.ibm.com)
7 */
8
9#include <linux/errno.h>
Heiko Carstensd8ad0752007-01-09 10:18:50 +010010#include <linux/hardirq.h>
Gerald Schaefer59f35d52006-12-04 15:40:45 +010011#include <linux/mm.h>
Gerald Schaefer4db84d42012-09-10 16:43:26 +020012#include <linux/hugetlb.h>
Heiko Carstens22155912006-12-08 15:53:49 +010013#include <asm/uaccess.h>
Gerald Schaefer59f35d52006-12-04 15:40:45 +010014#include <asm/futex.h>
Heiko Carstens2b67fc42007-02-05 21:16:47 +010015#include "uaccess.h"
Gerald Schaefer59f35d52006-12-04 15:40:45 +010016
Heiko Carstens066c4372013-02-25 09:10:23 +010017#ifndef CONFIG_64BIT
18#define AHI "ahi"
19#define SLR "slr"
20#else
21#define AHI "aghi"
22#define SLR "slgr"
23#endif
24
Heiko Carstens211deca2014-01-24 12:51:27 +010025static unsigned long strnlen_kernel(const char __user *src, unsigned long count)
Heiko Carstens066c4372013-02-25 09:10:23 +010026{
27 register unsigned long reg0 asm("0") = 0UL;
28 unsigned long tmp1, tmp2;
29
30 asm volatile(
31 " la %2,0(%1)\n"
32 " la %3,0(%0,%1)\n"
33 " "SLR" %0,%0\n"
34 "0: srst %3,%2\n"
35 " jo 0b\n"
36 " la %0,1(%3)\n" /* strnlen_kernel results includes \0 */
37 " "SLR" %0,%1\n"
38 "1:\n"
39 EX_TABLE(0b,1b)
40 : "+a" (count), "+a" (src), "=a" (tmp1), "=a" (tmp2)
41 : "d" (reg0) : "cc", "memory");
42 return count;
43}
44
Heiko Carstens211deca2014-01-24 12:51:27 +010045static unsigned long copy_in_kernel(void __user *to, const void __user *from,
46 unsigned long count)
Heiko Carstens066c4372013-02-25 09:10:23 +010047{
48 unsigned long tmp1;
49
50 asm volatile(
51 " "AHI" %0,-1\n"
52 " jo 5f\n"
53 " bras %3,3f\n"
54 "0:"AHI" %0,257\n"
55 "1: mvc 0(1,%1),0(%2)\n"
56 " la %1,1(%1)\n"
57 " la %2,1(%2)\n"
58 " "AHI" %0,-1\n"
59 " jnz 1b\n"
60 " j 5f\n"
61 "2: mvc 0(256,%1),0(%2)\n"
62 " la %1,256(%1)\n"
63 " la %2,256(%2)\n"
64 "3:"AHI" %0,-256\n"
65 " jnm 2b\n"
66 "4: ex %0,1b-0b(%3)\n"
67 "5:"SLR" %0,%0\n"
68 "6:\n"
69 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
70 : "+a" (count), "+a" (to), "+a" (from), "=a" (tmp1)
71 : : "cc", "memory");
72 return count;
73}
Gerald Schaefer4db84d42012-09-10 16:43:26 +020074
75/*
76 * Returns kernel address for user virtual address. If the returned address is
Hendrik Bruecknerb4a96012013-12-13 12:53:42 +010077 * >= -4095 (IS_ERR_VALUE(x) returns true), a fault has occurred and the
78 * address contains the (negative) exception code.
Gerald Schaefer4db84d42012-09-10 16:43:26 +020079 */
Heiko Carstensea815312013-03-21 12:50:39 +010080#ifdef CONFIG_64BIT
Heiko Carstens71a86ef2013-11-21 16:22:17 +010081
Heiko Carstensea815312013-03-21 12:50:39 +010082static unsigned long follow_table(struct mm_struct *mm,
83 unsigned long address, int write)
Martin Schwidefskye4aa4022007-10-22 12:52:46 +020084{
Heiko Carstensea815312013-03-21 12:50:39 +010085 unsigned long *table = (unsigned long *)__pa(mm->pgd);
Martin Schwidefskye4aa4022007-10-22 12:52:46 +020086
Heiko Carstens71a86ef2013-11-21 16:22:17 +010087 if (unlikely(address > mm->context.asce_limit - 1))
88 return -0x38UL;
Heiko Carstensea815312013-03-21 12:50:39 +010089 switch (mm->context.asce_bits & _ASCE_TYPE_MASK) {
90 case _ASCE_TYPE_REGION1:
91 table = table + ((address >> 53) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +020092 if (unlikely(*table & _REGION_ENTRY_INVALID))
Heiko Carstensea815312013-03-21 12:50:39 +010093 return -0x39UL;
94 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
Heiko Carstens12d84712013-04-29 10:58:56 +020095 /* fallthrough */
Heiko Carstensea815312013-03-21 12:50:39 +010096 case _ASCE_TYPE_REGION2:
97 table = table + ((address >> 42) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +020098 if (unlikely(*table & _REGION_ENTRY_INVALID))
Heiko Carstensea815312013-03-21 12:50:39 +010099 return -0x3aUL;
100 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
Heiko Carstens12d84712013-04-29 10:58:56 +0200101 /* fallthrough */
Heiko Carstensea815312013-03-21 12:50:39 +0100102 case _ASCE_TYPE_REGION3:
103 table = table + ((address >> 31) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200104 if (unlikely(*table & _REGION_ENTRY_INVALID))
Heiko Carstensea815312013-03-21 12:50:39 +0100105 return -0x3bUL;
106 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
Heiko Carstens12d84712013-04-29 10:58:56 +0200107 /* fallthrough */
Heiko Carstensea815312013-03-21 12:50:39 +0100108 case _ASCE_TYPE_SEGMENT:
109 table = table + ((address >> 20) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200110 if (unlikely(*table & _SEGMENT_ENTRY_INVALID))
Heiko Carstensea815312013-03-21 12:50:39 +0100111 return -0x10UL;
112 if (unlikely(*table & _SEGMENT_ENTRY_LARGE)) {
Martin Schwidefskye5098612013-07-23 20:57:57 +0200113 if (write && (*table & _SEGMENT_ENTRY_PROTECT))
Heiko Carstensea815312013-03-21 12:50:39 +0100114 return -0x04UL;
115 return (*table & _SEGMENT_ENTRY_ORIGIN_LARGE) +
116 (address & ~_SEGMENT_ENTRY_ORIGIN_LARGE);
117 }
118 table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200119 }
Heiko Carstensea815312013-03-21 12:50:39 +0100120 table = table + ((address >> 12) & 0xff);
121 if (unlikely(*table & _PAGE_INVALID))
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200122 return -0x11UL;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200123 if (write && (*table & _PAGE_PROTECT))
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200124 return -0x04UL;
Heiko Carstensea815312013-03-21 12:50:39 +0100125 return (*table & PAGE_MASK) + (address & ~PAGE_MASK);
Martin Schwidefskye4aa4022007-10-22 12:52:46 +0200126}
127
Heiko Carstensea815312013-03-21 12:50:39 +0100128#else /* CONFIG_64BIT */
129
130static unsigned long follow_table(struct mm_struct *mm,
131 unsigned long address, int write)
132{
133 unsigned long *table = (unsigned long *)__pa(mm->pgd);
134
135 table = table + ((address >> 20) & 0x7ff);
Martin Schwidefskye5098612013-07-23 20:57:57 +0200136 if (unlikely(*table & _SEGMENT_ENTRY_INVALID))
Heiko Carstensea815312013-03-21 12:50:39 +0100137 return -0x10UL;
138 table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
139 table = table + ((address >> 12) & 0xff);
140 if (unlikely(*table & _PAGE_INVALID))
141 return -0x11UL;
Martin Schwidefskye5098612013-07-23 20:57:57 +0200142 if (write && (*table & _PAGE_PROTECT))
Heiko Carstensea815312013-03-21 12:50:39 +0100143 return -0x04UL;
144 return (*table & PAGE_MASK) + (address & ~PAGE_MASK);
145}
146
147#endif /* CONFIG_64BIT */
148
Heiko Carstens211deca2014-01-24 12:51:27 +0100149static inline unsigned long __user_copy_pt(unsigned long uaddr, void *kptr,
150 unsigned long n, int write_user)
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100151{
152 struct mm_struct *mm = current->mm;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200153 unsigned long offset, done, size, kaddr;
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100154 void *from, *to;
155
Heiko Carstensb03b4672014-01-17 13:12:34 +0100156 if (!mm)
157 return n;
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100158 done = 0;
159retry:
160 spin_lock(&mm->page_table_lock);
161 do {
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200162 kaddr = follow_table(mm, uaddr, write_user);
163 if (IS_ERR_VALUE(kaddr))
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100164 goto fault;
165
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200166 offset = uaddr & ~PAGE_MASK;
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100167 size = min(n - done, PAGE_SIZE - offset);
168 if (write_user) {
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200169 to = (void *) kaddr;
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100170 from = kptr + done;
171 } else {
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200172 from = (void *) kaddr;
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100173 to = kptr + done;
174 }
175 memcpy(to, from, size);
176 done += size;
177 uaddr += size;
178 } while (done < n);
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100179 spin_unlock(&mm->page_table_lock);
180 return n - done;
181fault:
182 spin_unlock(&mm->page_table_lock);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200183 if (__handle_fault(uaddr, -kaddr, write_user))
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100184 return n - done;
185 goto retry;
186}
187
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100188/*
189 * Do DAT for user address by page table walk, return kernel address.
190 * This function needs to be called with current->mm->page_table_lock held.
191 */
Heiko Carstens211deca2014-01-24 12:51:27 +0100192static inline unsigned long __dat_user_addr(unsigned long uaddr, int write)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100193{
194 struct mm_struct *mm = current->mm;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200195 unsigned long kaddr;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100196 int rc;
197
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100198retry:
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200199 kaddr = follow_table(mm, uaddr, write);
200 if (IS_ERR_VALUE(kaddr))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100201 goto fault;
202
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200203 return kaddr;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100204fault:
205 spin_unlock(&mm->page_table_lock);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200206 rc = __handle_fault(uaddr, -kaddr, write);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100207 spin_lock(&mm->page_table_lock);
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100208 if (!rc)
209 goto retry;
210 return 0;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100211}
212
Heiko Carstens211deca2014-01-24 12:51:27 +0100213unsigned long copy_from_user_pt(void *to, const void __user *from, unsigned long n)
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100214{
Heiko Carstens211deca2014-01-24 12:51:27 +0100215 unsigned long rc;
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100216
Heiko Carstens066c4372013-02-25 09:10:23 +0100217 if (segment_eq(get_fs(), KERNEL_DS))
Heiko Carstenscfa785e2014-01-22 14:49:30 +0100218 return copy_in_kernel((void __user *) to, from, n);
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100219 rc = __user_copy_pt((unsigned long) from, to, n, 0);
220 if (unlikely(rc))
221 memset(to + n - rc, 0, rc);
222 return rc;
223}
224
Heiko Carstens211deca2014-01-24 12:51:27 +0100225unsigned long copy_to_user_pt(void __user *to, const void *from, unsigned long n)
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100226{
Heiko Carstens066c4372013-02-25 09:10:23 +0100227 if (segment_eq(get_fs(), KERNEL_DS))
Heiko Carstenscfa785e2014-01-22 14:49:30 +0100228 return copy_in_kernel(to, (void __user *) from, n);
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100229 return __user_copy_pt((unsigned long) to, (void *) from, n, 1);
230}
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100231
Heiko Carstens211deca2014-01-24 12:51:27 +0100232unsigned long clear_user_pt(void __user *to, unsigned long n)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100233{
Heiko Carstensb7fef2d2013-03-21 08:24:11 +0100234 void *zpage = (void *) empty_zero_page;
Heiko Carstens211deca2014-01-24 12:51:27 +0100235 unsigned long done, size, ret;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100236
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100237 done = 0;
238 do {
239 if (n - done > PAGE_SIZE)
240 size = PAGE_SIZE;
241 else
242 size = n - done;
Heiko Carstens066c4372013-02-25 09:10:23 +0100243 if (segment_eq(get_fs(), KERNEL_DS))
Heiko Carstenscfa785e2014-01-22 14:49:30 +0100244 ret = copy_in_kernel(to, (void __user *) zpage, n);
Heiko Carstens066c4372013-02-25 09:10:23 +0100245 else
246 ret = __user_copy_pt((unsigned long) to, zpage, size, 1);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100247 done += size;
Heiko Carstens066c4372013-02-25 09:10:23 +0100248 to += size;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100249 if (ret)
250 return ret + n - done;
251 } while (done < n);
252 return 0;
253}
254
Heiko Carstens211deca2014-01-24 12:51:27 +0100255unsigned long strnlen_user_pt(const char __user *src, unsigned long count)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100256{
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100257 unsigned long uaddr = (unsigned long) src;
258 struct mm_struct *mm = current->mm;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200259 unsigned long offset, done, len, kaddr;
Heiko Carstens211deca2014-01-24 12:51:27 +0100260 unsigned long len_str;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100261
Heiko Carstensf45655f2013-02-21 13:30:42 +0100262 if (unlikely(!count))
263 return 0;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100264 if (segment_eq(get_fs(), KERNEL_DS))
Heiko Carstenscfa785e2014-01-22 14:49:30 +0100265 return strnlen_kernel(src, count);
Heiko Carstensb03b4672014-01-17 13:12:34 +0100266 if (!mm)
267 return 0;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100268 done = 0;
269retry:
270 spin_lock(&mm->page_table_lock);
271 do {
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200272 kaddr = follow_table(mm, uaddr, 0);
273 if (IS_ERR_VALUE(kaddr))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100274 goto fault;
275
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200276 offset = uaddr & ~PAGE_MASK;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100277 len = min(count - done, PAGE_SIZE - offset);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200278 len_str = strnlen((char *) kaddr, len);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100279 done += len_str;
280 uaddr += len_str;
281 } while ((len_str == len) && (done < count));
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100282 spin_unlock(&mm->page_table_lock);
283 return done + 1;
284fault:
285 spin_unlock(&mm->page_table_lock);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200286 if (__handle_fault(uaddr, -kaddr, 0))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100287 return 0;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100288 goto retry;
289}
290
Heiko Carstens211deca2014-01-24 12:51:27 +0100291long strncpy_from_user_pt(char *dst, const char __user *src, long count)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100292{
Heiko Carstens211deca2014-01-24 12:51:27 +0100293 unsigned long done, len, offset, len_str;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100294
Heiko Carstens211deca2014-01-24 12:51:27 +0100295 if (unlikely(count <= 0))
Heiko Carstensf45655f2013-02-21 13:30:42 +0100296 return 0;
Heiko Carstens225cf8d2013-02-25 07:24:20 +0100297 done = 0;
298 do {
Heiko Carstens211deca2014-01-24 12:51:27 +0100299 offset = (unsigned long)src & ~PAGE_MASK;
Heiko Carstens225cf8d2013-02-25 07:24:20 +0100300 len = min(count - done, PAGE_SIZE - offset);
Heiko Carstens066c4372013-02-25 09:10:23 +0100301 if (segment_eq(get_fs(), KERNEL_DS)) {
Heiko Carstenscfa785e2014-01-22 14:49:30 +0100302 if (copy_in_kernel((void __user *) dst, src, len))
Heiko Carstens066c4372013-02-25 09:10:23 +0100303 return -EFAULT;
304 } else {
305 if (__user_copy_pt((unsigned long) src, dst, len, 0))
306 return -EFAULT;
307 }
Heiko Carstens225cf8d2013-02-25 07:24:20 +0100308 len_str = strnlen(dst, len);
309 done += len_str;
310 src += len_str;
311 dst += len_str;
312 } while ((len_str == len) && (done < count));
313 return done;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100314}
315
Heiko Carstens211deca2014-01-24 12:51:27 +0100316unsigned long copy_in_user_pt(void __user *to, const void __user *from,
317 unsigned long n)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100318{
319 struct mm_struct *mm = current->mm;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200320 unsigned long offset_max, uaddr, done, size, error_code;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100321 unsigned long uaddr_from = (unsigned long) from;
322 unsigned long uaddr_to = (unsigned long) to;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200323 unsigned long kaddr_to, kaddr_from;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100324 int write_user;
325
Heiko Carstens066c4372013-02-25 09:10:23 +0100326 if (segment_eq(get_fs(), KERNEL_DS))
Heiko Carstenscfa785e2014-01-22 14:49:30 +0100327 return copy_in_kernel(to, from, n);
Heiko Carstensb03b4672014-01-17 13:12:34 +0100328 if (!mm)
329 return n;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100330 done = 0;
331retry:
332 spin_lock(&mm->page_table_lock);
333 do {
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100334 write_user = 0;
335 uaddr = uaddr_from;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200336 kaddr_from = follow_table(mm, uaddr_from, 0);
337 error_code = kaddr_from;
338 if (IS_ERR_VALUE(error_code))
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100339 goto fault;
Martin Schwidefskye4aa4022007-10-22 12:52:46 +0200340
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100341 write_user = 1;
342 uaddr = uaddr_to;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200343 kaddr_to = follow_table(mm, uaddr_to, 1);
344 error_code = (unsigned long) kaddr_to;
345 if (IS_ERR_VALUE(error_code))
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100346 goto fault;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100347
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200348 offset_max = max(uaddr_from & ~PAGE_MASK,
349 uaddr_to & ~PAGE_MASK);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100350 size = min(n - done, PAGE_SIZE - offset_max);
351
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200352 memcpy((void *) kaddr_to, (void *) kaddr_from, size);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100353 done += size;
354 uaddr_from += size;
355 uaddr_to += size;
356 } while (done < n);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100357 spin_unlock(&mm->page_table_lock);
358 return n - done;
359fault:
360 spin_unlock(&mm->page_table_lock);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200361 if (__handle_fault(uaddr, -error_code, write_user))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100362 return n - done;
363 goto retry;
364}
365
366#define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg) \
367 asm volatile("0: l %1,0(%6)\n" \
368 "1: " insn \
369 "2: cs %1,%2,0(%6)\n" \
370 "3: jl 1b\n" \
371 " lhi %0,0\n" \
372 "4:\n" \
373 EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b) \
374 : "=d" (ret), "=&d" (oldval), "=&d" (newval), \
375 "=m" (*uaddr) \
376 : "0" (-EFAULT), "d" (oparg), "a" (uaddr), \
377 "m" (*uaddr) : "cc" );
378
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800379static int __futex_atomic_op_pt(int op, u32 __user *uaddr, int oparg, int *old)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100380{
381 int oldval = 0, newval, ret;
382
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100383 switch (op) {
384 case FUTEX_OP_SET:
385 __futex_atomic_op("lr %2,%5\n",
386 ret, oldval, newval, uaddr, oparg);
387 break;
388 case FUTEX_OP_ADD:
389 __futex_atomic_op("lr %2,%1\nar %2,%5\n",
390 ret, oldval, newval, uaddr, oparg);
391 break;
392 case FUTEX_OP_OR:
393 __futex_atomic_op("lr %2,%1\nor %2,%5\n",
394 ret, oldval, newval, uaddr, oparg);
395 break;
396 case FUTEX_OP_ANDN:
397 __futex_atomic_op("lr %2,%1\nnr %2,%5\n",
398 ret, oldval, newval, uaddr, oparg);
399 break;
400 case FUTEX_OP_XOR:
401 __futex_atomic_op("lr %2,%1\nxr %2,%5\n",
402 ret, oldval, newval, uaddr, oparg);
403 break;
404 default:
405 ret = -ENOSYS;
406 }
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200407 if (ret == 0)
408 *old = oldval;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100409 return ret;
410}
411
Heiko Carstens4f41c2b2014-01-23 11:18:36 +0100412int __futex_atomic_op_inuser(int op, u32 __user *uaddr, int oparg, int *old)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100413{
414 int ret;
415
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200416 if (segment_eq(get_fs(), KERNEL_DS))
417 return __futex_atomic_op_pt(op, uaddr, oparg, old);
Heiko Carstensb03b4672014-01-17 13:12:34 +0100418 if (unlikely(!current->mm))
419 return -EFAULT;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100420 spin_lock(&current->mm->page_table_lock);
Martin Schwidefsky3c52e492011-10-30 15:17:15 +0100421 uaddr = (u32 __force __user *)
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200422 __dat_user_addr((__force unsigned long) uaddr, 1);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100423 if (!uaddr) {
424 spin_unlock(&current->mm->page_table_lock);
425 return -EFAULT;
426 }
427 get_page(virt_to_page(uaddr));
428 spin_unlock(&current->mm->page_table_lock);
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200429 ret = __futex_atomic_op_pt(op, uaddr, oparg, old);
430 put_page(virt_to_page(uaddr));
431 return ret;
432}
433
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800434static int __futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr,
435 u32 oldval, u32 newval)
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200436{
437 int ret;
438
439 asm volatile("0: cs %1,%4,0(%5)\n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800440 "1: la %0,0\n"
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200441 "2:\n"
442 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100443 : "=d" (ret), "+d" (oldval), "=m" (*uaddr)
444 : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
445 : "cc", "memory" );
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800446 *uval = oldval;
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200447 return ret;
448}
449
Heiko Carstens4f41c2b2014-01-23 11:18:36 +0100450int futex_atomic_cmpxchg_inatomic(u32 *uval, u32 __user *uaddr,
451 u32 oldval, u32 newval)
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200452{
453 int ret;
454
455 if (segment_eq(get_fs(), KERNEL_DS))
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800456 return __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval);
Heiko Carstensb03b4672014-01-17 13:12:34 +0100457 if (unlikely(!current->mm))
458 return -EFAULT;
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200459 spin_lock(&current->mm->page_table_lock);
Martin Schwidefsky3c52e492011-10-30 15:17:15 +0100460 uaddr = (u32 __force __user *)
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200461 __dat_user_addr((__force unsigned long) uaddr, 1);
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200462 if (!uaddr) {
463 spin_unlock(&current->mm->page_table_lock);
464 return -EFAULT;
465 }
466 get_page(virt_to_page(uaddr));
467 spin_unlock(&current->mm->page_table_lock);
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800468 ret = __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100469 put_page(virt_to_page(uaddr));
470 return ret;
471}