mfd: wm8350-i2c: Make sure the i2c regmap functions are compiled
[linux/fpc-iii.git] / arch / s390 / lib / uaccess_pt.c
blob26588fdcb7768a6e697c1dfb82614d5bd55e217e
1 /*
2 * User access functions based on page table walks for enhanced
3 * system layout without hardware support.
5 * Copyright IBM Corp. 2006, 2012
6 * Author(s): Gerald Schaefer (gerald.schaefer@de.ibm.com)
7 */
9 #include <linux/errno.h>
10 #include <linux/hardirq.h>
11 #include <linux/mm.h>
12 #include <linux/hugetlb.h>
13 #include <asm/uaccess.h>
14 #include <asm/futex.h>
15 #include "uaccess.h"
17 #ifndef CONFIG_64BIT
18 #define AHI "ahi"
19 #define SLR "slr"
20 #else
21 #define AHI "aghi"
22 #define SLR "slgr"
23 #endif
25 static size_t strnlen_kernel(size_t count, const char __user *src)
27 register unsigned long reg0 asm("0") = 0UL;
28 unsigned long tmp1, tmp2;
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;
45 static size_t copy_in_kernel(size_t count, void __user *to,
46 const void __user *from)
48 unsigned long tmp1;
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;
76 * Returns kernel address for user virtual address. If the returned address is
77 * >= -4095 (IS_ERR_VALUE(x) returns true), a fault has occured and the address
78 * contains the (negative) exception code.
80 #ifdef CONFIG_64BIT
82 static unsigned long follow_table(struct mm_struct *mm,
83 unsigned long address, int write)
85 unsigned long *table = (unsigned long *)__pa(mm->pgd);
87 if (unlikely(address > mm->context.asce_limit - 1))
88 return -0x38UL;
89 switch (mm->context.asce_bits & _ASCE_TYPE_MASK) {
90 case _ASCE_TYPE_REGION1:
91 table = table + ((address >> 53) & 0x7ff);
92 if (unlikely(*table & _REGION_ENTRY_INVALID))
93 return -0x39UL;
94 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
95 /* fallthrough */
96 case _ASCE_TYPE_REGION2:
97 table = table + ((address >> 42) & 0x7ff);
98 if (unlikely(*table & _REGION_ENTRY_INVALID))
99 return -0x3aUL;
100 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
101 /* fallthrough */
102 case _ASCE_TYPE_REGION3:
103 table = table + ((address >> 31) & 0x7ff);
104 if (unlikely(*table & _REGION_ENTRY_INVALID))
105 return -0x3bUL;
106 table = (unsigned long *)(*table & _REGION_ENTRY_ORIGIN);
107 /* fallthrough */
108 case _ASCE_TYPE_SEGMENT:
109 table = table + ((address >> 20) & 0x7ff);
110 if (unlikely(*table & _SEGMENT_ENTRY_INVALID))
111 return -0x10UL;
112 if (unlikely(*table & _SEGMENT_ENTRY_LARGE)) {
113 if (write && (*table & _SEGMENT_ENTRY_PROTECT))
114 return -0x04UL;
115 return (*table & _SEGMENT_ENTRY_ORIGIN_LARGE) +
116 (address & ~_SEGMENT_ENTRY_ORIGIN_LARGE);
118 table = (unsigned long *)(*table & _SEGMENT_ENTRY_ORIGIN);
120 table = table + ((address >> 12) & 0xff);
121 if (unlikely(*table & _PAGE_INVALID))
122 return -0x11UL;
123 if (write && (*table & _PAGE_PROTECT))
124 return -0x04UL;
125 return (*table & PAGE_MASK) + (address & ~PAGE_MASK);
128 #else /* CONFIG_64BIT */
130 static unsigned long follow_table(struct mm_struct *mm,
131 unsigned long address, int write)
133 unsigned long *table = (unsigned long *)__pa(mm->pgd);
135 table = table + ((address >> 20) & 0x7ff);
136 if (unlikely(*table & _SEGMENT_ENTRY_INVALID))
137 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;
142 if (write && (*table & _PAGE_PROTECT))
143 return -0x04UL;
144 return (*table & PAGE_MASK) + (address & ~PAGE_MASK);
147 #endif /* CONFIG_64BIT */
149 static __always_inline size_t __user_copy_pt(unsigned long uaddr, void *kptr,
150 size_t n, int write_user)
152 struct mm_struct *mm = current->mm;
153 unsigned long offset, done, size, kaddr;
154 void *from, *to;
156 done = 0;
157 retry:
158 spin_lock(&mm->page_table_lock);
159 do {
160 kaddr = follow_table(mm, uaddr, write_user);
161 if (IS_ERR_VALUE(kaddr))
162 goto fault;
164 offset = uaddr & ~PAGE_MASK;
165 size = min(n - done, PAGE_SIZE - offset);
166 if (write_user) {
167 to = (void *) kaddr;
168 from = kptr + done;
169 } else {
170 from = (void *) kaddr;
171 to = kptr + done;
173 memcpy(to, from, size);
174 done += size;
175 uaddr += size;
176 } while (done < n);
177 spin_unlock(&mm->page_table_lock);
178 return n - done;
179 fault:
180 spin_unlock(&mm->page_table_lock);
181 if (__handle_fault(uaddr, -kaddr, write_user))
182 return n - done;
183 goto retry;
187 * Do DAT for user address by page table walk, return kernel address.
188 * This function needs to be called with current->mm->page_table_lock held.
190 static __always_inline unsigned long __dat_user_addr(unsigned long uaddr,
191 int write)
193 struct mm_struct *mm = current->mm;
194 unsigned long kaddr;
195 int rc;
197 retry:
198 kaddr = follow_table(mm, uaddr, write);
199 if (IS_ERR_VALUE(kaddr))
200 goto fault;
202 return kaddr;
203 fault:
204 spin_unlock(&mm->page_table_lock);
205 rc = __handle_fault(uaddr, -kaddr, write);
206 spin_lock(&mm->page_table_lock);
207 if (!rc)
208 goto retry;
209 return 0;
212 size_t copy_from_user_pt(size_t n, const void __user *from, void *to)
214 size_t rc;
216 if (segment_eq(get_fs(), KERNEL_DS))
217 return copy_in_kernel(n, (void __user *) to, from);
218 rc = __user_copy_pt((unsigned long) from, to, n, 0);
219 if (unlikely(rc))
220 memset(to + n - rc, 0, rc);
221 return rc;
224 size_t copy_to_user_pt(size_t n, void __user *to, const void *from)
226 if (segment_eq(get_fs(), KERNEL_DS))
227 return copy_in_kernel(n, to, (void __user *) from);
228 return __user_copy_pt((unsigned long) to, (void *) from, n, 1);
231 static size_t clear_user_pt(size_t n, void __user *to)
233 void *zpage = (void *) empty_zero_page;
234 long done, size, ret;
236 done = 0;
237 do {
238 if (n - done > PAGE_SIZE)
239 size = PAGE_SIZE;
240 else
241 size = n - done;
242 if (segment_eq(get_fs(), KERNEL_DS))
243 ret = copy_in_kernel(n, to, (void __user *) zpage);
244 else
245 ret = __user_copy_pt((unsigned long) to, zpage, size, 1);
246 done += size;
247 to += size;
248 if (ret)
249 return ret + n - done;
250 } while (done < n);
251 return 0;
254 static size_t strnlen_user_pt(size_t count, const char __user *src)
256 unsigned long uaddr = (unsigned long) src;
257 struct mm_struct *mm = current->mm;
258 unsigned long offset, done, len, kaddr;
259 size_t len_str;
261 if (unlikely(!count))
262 return 0;
263 if (segment_eq(get_fs(), KERNEL_DS))
264 return strnlen_kernel(count, src);
265 done = 0;
266 retry:
267 spin_lock(&mm->page_table_lock);
268 do {
269 kaddr = follow_table(mm, uaddr, 0);
270 if (IS_ERR_VALUE(kaddr))
271 goto fault;
273 offset = uaddr & ~PAGE_MASK;
274 len = min(count - done, PAGE_SIZE - offset);
275 len_str = strnlen((char *) kaddr, len);
276 done += len_str;
277 uaddr += len_str;
278 } while ((len_str == len) && (done < count));
279 spin_unlock(&mm->page_table_lock);
280 return done + 1;
281 fault:
282 spin_unlock(&mm->page_table_lock);
283 if (__handle_fault(uaddr, -kaddr, 0))
284 return 0;
285 goto retry;
288 static size_t strncpy_from_user_pt(size_t count, const char __user *src,
289 char *dst)
291 size_t done, len, offset, len_str;
293 if (unlikely(!count))
294 return 0;
295 done = 0;
296 do {
297 offset = (size_t)src & ~PAGE_MASK;
298 len = min(count - done, PAGE_SIZE - offset);
299 if (segment_eq(get_fs(), KERNEL_DS)) {
300 if (copy_in_kernel(len, (void __user *) dst, src))
301 return -EFAULT;
302 } else {
303 if (__user_copy_pt((unsigned long) src, dst, len, 0))
304 return -EFAULT;
306 len_str = strnlen(dst, len);
307 done += len_str;
308 src += len_str;
309 dst += len_str;
310 } while ((len_str == len) && (done < count));
311 return done;
314 static size_t copy_in_user_pt(size_t n, void __user *to,
315 const void __user *from)
317 struct mm_struct *mm = current->mm;
318 unsigned long offset_max, uaddr, done, size, error_code;
319 unsigned long uaddr_from = (unsigned long) from;
320 unsigned long uaddr_to = (unsigned long) to;
321 unsigned long kaddr_to, kaddr_from;
322 int write_user;
324 if (segment_eq(get_fs(), KERNEL_DS))
325 return copy_in_kernel(n, to, from);
326 done = 0;
327 retry:
328 spin_lock(&mm->page_table_lock);
329 do {
330 write_user = 0;
331 uaddr = uaddr_from;
332 kaddr_from = follow_table(mm, uaddr_from, 0);
333 error_code = kaddr_from;
334 if (IS_ERR_VALUE(error_code))
335 goto fault;
337 write_user = 1;
338 uaddr = uaddr_to;
339 kaddr_to = follow_table(mm, uaddr_to, 1);
340 error_code = (unsigned long) kaddr_to;
341 if (IS_ERR_VALUE(error_code))
342 goto fault;
344 offset_max = max(uaddr_from & ~PAGE_MASK,
345 uaddr_to & ~PAGE_MASK);
346 size = min(n - done, PAGE_SIZE - offset_max);
348 memcpy((void *) kaddr_to, (void *) kaddr_from, size);
349 done += size;
350 uaddr_from += size;
351 uaddr_to += size;
352 } while (done < n);
353 spin_unlock(&mm->page_table_lock);
354 return n - done;
355 fault:
356 spin_unlock(&mm->page_table_lock);
357 if (__handle_fault(uaddr, -error_code, write_user))
358 return n - done;
359 goto retry;
362 #define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg) \
363 asm volatile("0: l %1,0(%6)\n" \
364 "1: " insn \
365 "2: cs %1,%2,0(%6)\n" \
366 "3: jl 1b\n" \
367 " lhi %0,0\n" \
368 "4:\n" \
369 EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b) \
370 : "=d" (ret), "=&d" (oldval), "=&d" (newval), \
371 "=m" (*uaddr) \
372 : "0" (-EFAULT), "d" (oparg), "a" (uaddr), \
373 "m" (*uaddr) : "cc" );
375 static int __futex_atomic_op_pt(int op, u32 __user *uaddr, int oparg, int *old)
377 int oldval = 0, newval, ret;
379 switch (op) {
380 case FUTEX_OP_SET:
381 __futex_atomic_op("lr %2,%5\n",
382 ret, oldval, newval, uaddr, oparg);
383 break;
384 case FUTEX_OP_ADD:
385 __futex_atomic_op("lr %2,%1\nar %2,%5\n",
386 ret, oldval, newval, uaddr, oparg);
387 break;
388 case FUTEX_OP_OR:
389 __futex_atomic_op("lr %2,%1\nor %2,%5\n",
390 ret, oldval, newval, uaddr, oparg);
391 break;
392 case FUTEX_OP_ANDN:
393 __futex_atomic_op("lr %2,%1\nnr %2,%5\n",
394 ret, oldval, newval, uaddr, oparg);
395 break;
396 case FUTEX_OP_XOR:
397 __futex_atomic_op("lr %2,%1\nxr %2,%5\n",
398 ret, oldval, newval, uaddr, oparg);
399 break;
400 default:
401 ret = -ENOSYS;
403 if (ret == 0)
404 *old = oldval;
405 return ret;
408 int futex_atomic_op_pt(int op, u32 __user *uaddr, int oparg, int *old)
410 int ret;
412 if (segment_eq(get_fs(), KERNEL_DS))
413 return __futex_atomic_op_pt(op, uaddr, oparg, old);
414 spin_lock(&current->mm->page_table_lock);
415 uaddr = (u32 __force __user *)
416 __dat_user_addr((__force unsigned long) uaddr, 1);
417 if (!uaddr) {
418 spin_unlock(&current->mm->page_table_lock);
419 return -EFAULT;
421 get_page(virt_to_page(uaddr));
422 spin_unlock(&current->mm->page_table_lock);
423 ret = __futex_atomic_op_pt(op, uaddr, oparg, old);
424 put_page(virt_to_page(uaddr));
425 return ret;
428 static int __futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr,
429 u32 oldval, u32 newval)
431 int ret;
433 asm volatile("0: cs %1,%4,0(%5)\n"
434 "1: la %0,0\n"
435 "2:\n"
436 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
437 : "=d" (ret), "+d" (oldval), "=m" (*uaddr)
438 : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
439 : "cc", "memory" );
440 *uval = oldval;
441 return ret;
444 int futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr,
445 u32 oldval, u32 newval)
447 int ret;
449 if (segment_eq(get_fs(), KERNEL_DS))
450 return __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval);
451 spin_lock(&current->mm->page_table_lock);
452 uaddr = (u32 __force __user *)
453 __dat_user_addr((__force unsigned long) uaddr, 1);
454 if (!uaddr) {
455 spin_unlock(&current->mm->page_table_lock);
456 return -EFAULT;
458 get_page(virt_to_page(uaddr));
459 spin_unlock(&current->mm->page_table_lock);
460 ret = __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval);
461 put_page(virt_to_page(uaddr));
462 return ret;
465 struct uaccess_ops uaccess_pt = {
466 .copy_from_user = copy_from_user_pt,
467 .copy_from_user_small = copy_from_user_pt,
468 .copy_to_user = copy_to_user_pt,
469 .copy_to_user_small = copy_to_user_pt,
470 .copy_in_user = copy_in_user_pt,
471 .clear_user = clear_user_pt,
472 .strnlen_user = strnlen_user_pt,
473 .strncpy_from_user = strncpy_from_user_pt,
474 .futex_atomic_op = futex_atomic_op_pt,
475 .futex_atomic_cmpxchg = futex_atomic_cmpxchg_pt,