blob: a70ee84c024126ca562f8d738b52a590e4fd6a2b [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
Gerald Schaefer4db84d42012-09-10 16:43:26 +020017
18/*
19 * Returns kernel address for user virtual address. If the returned address is
20 * >= -4095 (IS_ERR_VALUE(x) returns true), a fault has occured and the address
21 * contains the (negative) exception code.
22 */
23static __always_inline unsigned long follow_table(struct mm_struct *mm,
24 unsigned long addr, int write)
Martin Schwidefskye4aa4022007-10-22 12:52:46 +020025{
26 pgd_t *pgd;
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020027 pud_t *pud;
Martin Schwidefskye4aa4022007-10-22 12:52:46 +020028 pmd_t *pmd;
Gerald Schaefer4db84d42012-09-10 16:43:26 +020029 pte_t *ptep;
Martin Schwidefskye4aa4022007-10-22 12:52:46 +020030
31 pgd = pgd_offset(mm, addr);
32 if (pgd_none(*pgd) || unlikely(pgd_bad(*pgd)))
Gerald Schaefer4db84d42012-09-10 16:43:26 +020033 return -0x3aUL;
Martin Schwidefskye4aa4022007-10-22 12:52:46 +020034
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020035 pud = pud_offset(pgd, addr);
36 if (pud_none(*pud) || unlikely(pud_bad(*pud)))
Gerald Schaefer4db84d42012-09-10 16:43:26 +020037 return -0x3bUL;
Martin Schwidefsky190a1d72007-10-22 12:52:48 +020038
39 pmd = pmd_offset(pud, addr);
Gerald Schaefer4db84d42012-09-10 16:43:26 +020040 if (pmd_none(*pmd))
41 return -0x10UL;
Gerald Schaefer156152f2012-10-25 17:24:12 +020042 if (pmd_large(*pmd)) {
Gerald Schaefer4db84d42012-09-10 16:43:26 +020043 if (write && (pmd_val(*pmd) & _SEGMENT_ENTRY_RO))
44 return -0x04UL;
45 return (pmd_val(*pmd) & HPAGE_MASK) + (addr & ~HPAGE_MASK);
46 }
47 if (unlikely(pmd_bad(*pmd)))
48 return -0x10UL;
Martin Schwidefskye4aa4022007-10-22 12:52:46 +020049
Gerald Schaefer4db84d42012-09-10 16:43:26 +020050 ptep = pte_offset_map(pmd, addr);
51 if (!pte_present(*ptep))
52 return -0x11UL;
Martin Schwidefskyabf09be2012-11-07 13:17:37 +010053 if (write && (!pte_write(*ptep) || !pte_dirty(*ptep)))
Gerald Schaefer4db84d42012-09-10 16:43:26 +020054 return -0x04UL;
55
56 return (pte_val(*ptep) & PAGE_MASK) + (addr & ~PAGE_MASK);
Martin Schwidefskye4aa4022007-10-22 12:52:46 +020057}
58
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +010059static __always_inline size_t __user_copy_pt(unsigned long uaddr, void *kptr,
60 size_t n, int write_user)
Gerald Schaefer59f35d52006-12-04 15:40:45 +010061{
62 struct mm_struct *mm = current->mm;
Gerald Schaefer4db84d42012-09-10 16:43:26 +020063 unsigned long offset, done, size, kaddr;
Gerald Schaefer59f35d52006-12-04 15:40:45 +010064 void *from, *to;
65
66 done = 0;
67retry:
68 spin_lock(&mm->page_table_lock);
69 do {
Gerald Schaefer4db84d42012-09-10 16:43:26 +020070 kaddr = follow_table(mm, uaddr, write_user);
71 if (IS_ERR_VALUE(kaddr))
Gerald Schaefer59f35d52006-12-04 15:40:45 +010072 goto fault;
73
Gerald Schaefer4db84d42012-09-10 16:43:26 +020074 offset = uaddr & ~PAGE_MASK;
Gerald Schaefer59f35d52006-12-04 15:40:45 +010075 size = min(n - done, PAGE_SIZE - offset);
76 if (write_user) {
Gerald Schaefer4db84d42012-09-10 16:43:26 +020077 to = (void *) kaddr;
Gerald Schaefer59f35d52006-12-04 15:40:45 +010078 from = kptr + done;
79 } else {
Gerald Schaefer4db84d42012-09-10 16:43:26 +020080 from = (void *) kaddr;
Gerald Schaefer59f35d52006-12-04 15:40:45 +010081 to = kptr + done;
82 }
83 memcpy(to, from, size);
84 done += size;
85 uaddr += size;
86 } while (done < n);
Gerald Schaefer59f35d52006-12-04 15:40:45 +010087 spin_unlock(&mm->page_table_lock);
88 return n - done;
89fault:
90 spin_unlock(&mm->page_table_lock);
Gerald Schaefer4db84d42012-09-10 16:43:26 +020091 if (__handle_fault(uaddr, -kaddr, write_user))
Gerald Schaefer59f35d52006-12-04 15:40:45 +010092 return n - done;
93 goto retry;
94}
95
Gerald Schaeferc1821c22007-02-05 21:18:17 +010096/*
97 * Do DAT for user address by page table walk, return kernel address.
98 * This function needs to be called with current->mm->page_table_lock held.
99 */
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200100static __always_inline unsigned long __dat_user_addr(unsigned long uaddr,
101 int write)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100102{
103 struct mm_struct *mm = current->mm;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200104 unsigned long kaddr;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100105 int rc;
106
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100107retry:
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200108 kaddr = follow_table(mm, uaddr, write);
109 if (IS_ERR_VALUE(kaddr))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100110 goto fault;
111
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200112 return kaddr;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100113fault:
114 spin_unlock(&mm->page_table_lock);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200115 rc = __handle_fault(uaddr, -kaddr, write);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100116 spin_lock(&mm->page_table_lock);
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100117 if (!rc)
118 goto retry;
119 return 0;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100120}
121
Gerald Schaefer59f35d52006-12-04 15:40:45 +0100122size_t copy_from_user_pt(size_t n, const void __user *from, void *to)
123{
124 size_t rc;
125
126 if (segment_eq(get_fs(), KERNEL_DS)) {
127 memcpy(to, (void __kernel __force *) from, n);
128 return 0;
129 }
130 rc = __user_copy_pt((unsigned long) from, to, n, 0);
131 if (unlikely(rc))
132 memset(to + n - rc, 0, rc);
133 return rc;
134}
135
136size_t copy_to_user_pt(size_t n, void __user *to, const void *from)
137{
138 if (segment_eq(get_fs(), KERNEL_DS)) {
139 memcpy((void __kernel __force *) to, from, n);
140 return 0;
141 }
142 return __user_copy_pt((unsigned long) to, (void *) from, n, 1);
143}
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100144
145static size_t clear_user_pt(size_t n, void __user *to)
146{
147 long done, size, ret;
148
149 if (segment_eq(get_fs(), KERNEL_DS)) {
150 memset((void __kernel __force *) to, 0, n);
151 return 0;
152 }
153 done = 0;
154 do {
155 if (n - done > PAGE_SIZE)
156 size = PAGE_SIZE;
157 else
158 size = n - done;
159 ret = __user_copy_pt((unsigned long) to + done,
160 &empty_zero_page, size, 1);
161 done += size;
162 if (ret)
163 return ret + n - done;
164 } while (done < n);
165 return 0;
166}
167
168static size_t strnlen_user_pt(size_t count, const char __user *src)
169{
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100170 unsigned long uaddr = (unsigned long) src;
171 struct mm_struct *mm = current->mm;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200172 unsigned long offset, done, len, kaddr;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100173 size_t len_str;
174
175 if (segment_eq(get_fs(), KERNEL_DS))
176 return strnlen((const char __kernel __force *) src, count) + 1;
177 done = 0;
178retry:
179 spin_lock(&mm->page_table_lock);
180 do {
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200181 kaddr = follow_table(mm, uaddr, 0);
182 if (IS_ERR_VALUE(kaddr))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100183 goto fault;
184
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200185 offset = uaddr & ~PAGE_MASK;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100186 len = min(count - done, PAGE_SIZE - offset);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200187 len_str = strnlen((char *) kaddr, len);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100188 done += len_str;
189 uaddr += len_str;
190 } while ((len_str == len) && (done < count));
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100191 spin_unlock(&mm->page_table_lock);
192 return done + 1;
193fault:
194 spin_unlock(&mm->page_table_lock);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200195 if (__handle_fault(uaddr, -kaddr, 0))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100196 return 0;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100197 goto retry;
198}
199
200static size_t strncpy_from_user_pt(size_t count, const char __user *src,
201 char *dst)
202{
203 size_t n = strnlen_user_pt(count, src);
204
205 if (!n)
206 return -EFAULT;
207 if (n > count)
208 n = count;
209 if (segment_eq(get_fs(), KERNEL_DS)) {
210 memcpy(dst, (const char __kernel __force *) src, n);
211 if (dst[n-1] == '\0')
212 return n-1;
213 else
214 return n;
215 }
216 if (__user_copy_pt((unsigned long) src, dst, n, 0))
217 return -EFAULT;
218 if (dst[n-1] == '\0')
219 return n-1;
220 else
221 return n;
222}
223
224static size_t copy_in_user_pt(size_t n, void __user *to,
225 const void __user *from)
226{
227 struct mm_struct *mm = current->mm;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200228 unsigned long offset_max, uaddr, done, size, error_code;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100229 unsigned long uaddr_from = (unsigned long) from;
230 unsigned long uaddr_to = (unsigned long) to;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200231 unsigned long kaddr_to, kaddr_from;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100232 int write_user;
233
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200234 if (segment_eq(get_fs(), KERNEL_DS)) {
235 memcpy((void __force *) to, (void __force *) from, n);
236 return 0;
237 }
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100238 done = 0;
239retry:
240 spin_lock(&mm->page_table_lock);
241 do {
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100242 write_user = 0;
243 uaddr = uaddr_from;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200244 kaddr_from = follow_table(mm, uaddr_from, 0);
245 error_code = kaddr_from;
246 if (IS_ERR_VALUE(error_code))
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100247 goto fault;
Martin Schwidefskye4aa4022007-10-22 12:52:46 +0200248
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100249 write_user = 1;
250 uaddr = uaddr_to;
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200251 kaddr_to = follow_table(mm, uaddr_to, 1);
252 error_code = (unsigned long) kaddr_to;
253 if (IS_ERR_VALUE(error_code))
Gerald Schaefer6c1e3e72009-12-07 12:51:47 +0100254 goto fault;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100255
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200256 offset_max = max(uaddr_from & ~PAGE_MASK,
257 uaddr_to & ~PAGE_MASK);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100258 size = min(n - done, PAGE_SIZE - offset_max);
259
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200260 memcpy((void *) kaddr_to, (void *) kaddr_from, size);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100261 done += size;
262 uaddr_from += size;
263 uaddr_to += size;
264 } while (done < n);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100265 spin_unlock(&mm->page_table_lock);
266 return n - done;
267fault:
268 spin_unlock(&mm->page_table_lock);
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200269 if (__handle_fault(uaddr, -error_code, write_user))
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100270 return n - done;
271 goto retry;
272}
273
274#define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg) \
275 asm volatile("0: l %1,0(%6)\n" \
276 "1: " insn \
277 "2: cs %1,%2,0(%6)\n" \
278 "3: jl 1b\n" \
279 " lhi %0,0\n" \
280 "4:\n" \
281 EX_TABLE(0b,4b) EX_TABLE(2b,4b) EX_TABLE(3b,4b) \
282 : "=d" (ret), "=&d" (oldval), "=&d" (newval), \
283 "=m" (*uaddr) \
284 : "0" (-EFAULT), "d" (oparg), "a" (uaddr), \
285 "m" (*uaddr) : "cc" );
286
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800287static int __futex_atomic_op_pt(int op, u32 __user *uaddr, int oparg, int *old)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100288{
289 int oldval = 0, newval, ret;
290
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100291 switch (op) {
292 case FUTEX_OP_SET:
293 __futex_atomic_op("lr %2,%5\n",
294 ret, oldval, newval, uaddr, oparg);
295 break;
296 case FUTEX_OP_ADD:
297 __futex_atomic_op("lr %2,%1\nar %2,%5\n",
298 ret, oldval, newval, uaddr, oparg);
299 break;
300 case FUTEX_OP_OR:
301 __futex_atomic_op("lr %2,%1\nor %2,%5\n",
302 ret, oldval, newval, uaddr, oparg);
303 break;
304 case FUTEX_OP_ANDN:
305 __futex_atomic_op("lr %2,%1\nnr %2,%5\n",
306 ret, oldval, newval, uaddr, oparg);
307 break;
308 case FUTEX_OP_XOR:
309 __futex_atomic_op("lr %2,%1\nxr %2,%5\n",
310 ret, oldval, newval, uaddr, oparg);
311 break;
312 default:
313 ret = -ENOSYS;
314 }
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200315 if (ret == 0)
316 *old = oldval;
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100317 return ret;
318}
319
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800320int futex_atomic_op_pt(int op, u32 __user *uaddr, int oparg, int *old)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100321{
322 int ret;
323
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200324 if (segment_eq(get_fs(), KERNEL_DS))
325 return __futex_atomic_op_pt(op, uaddr, oparg, old);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100326 spin_lock(&current->mm->page_table_lock);
Martin Schwidefsky3c52e492011-10-30 15:17:15 +0100327 uaddr = (u32 __force __user *)
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200328 __dat_user_addr((__force unsigned long) uaddr, 1);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100329 if (!uaddr) {
330 spin_unlock(&current->mm->page_table_lock);
331 return -EFAULT;
332 }
333 get_page(virt_to_page(uaddr));
334 spin_unlock(&current->mm->page_table_lock);
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200335 ret = __futex_atomic_op_pt(op, uaddr, oparg, old);
336 put_page(virt_to_page(uaddr));
337 return ret;
338}
339
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800340static int __futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr,
341 u32 oldval, u32 newval)
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200342{
343 int ret;
344
345 asm volatile("0: cs %1,%4,0(%5)\n"
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800346 "1: la %0,0\n"
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200347 "2:\n"
348 EX_TABLE(0b,2b) EX_TABLE(1b,2b)
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100349 : "=d" (ret), "+d" (oldval), "=m" (*uaddr)
350 : "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
351 : "cc", "memory" );
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800352 *uval = oldval;
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200353 return ret;
354}
355
Michel Lespinasse8d7718a2011-03-10 18:50:58 -0800356int futex_atomic_cmpxchg_pt(u32 *uval, u32 __user *uaddr,
357 u32 oldval, u32 newval)
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200358{
359 int ret;
360
361 if (segment_eq(get_fs(), KERNEL_DS))
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800362 return __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval);
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200363 spin_lock(&current->mm->page_table_lock);
Martin Schwidefsky3c52e492011-10-30 15:17:15 +0100364 uaddr = (u32 __force __user *)
Gerald Schaefer4db84d42012-09-10 16:43:26 +0200365 __dat_user_addr((__force unsigned long) uaddr, 1);
Heiko Carstens3f12ebc2008-04-17 07:46:27 +0200366 if (!uaddr) {
367 spin_unlock(&current->mm->page_table_lock);
368 return -EFAULT;
369 }
370 get_page(virt_to_page(uaddr));
371 spin_unlock(&current->mm->page_table_lock);
Michel Lespinasse37a9d912011-03-10 18:48:51 -0800372 ret = __futex_atomic_cmpxchg_pt(uval, uaddr, oldval, newval);
Gerald Schaeferc1821c22007-02-05 21:18:17 +0100373 put_page(virt_to_page(uaddr));
374 return ret;
375}
376
377struct uaccess_ops uaccess_pt = {
378 .copy_from_user = copy_from_user_pt,
379 .copy_from_user_small = copy_from_user_pt,
380 .copy_to_user = copy_to_user_pt,
381 .copy_to_user_small = copy_to_user_pt,
382 .copy_in_user = copy_in_user_pt,
383 .clear_user = clear_user_pt,
384 .strnlen_user = strnlen_user_pt,
385 .strncpy_from_user = strncpy_from_user_pt,
386 .futex_atomic_op = futex_atomic_op_pt,
387 .futex_atomic_cmpxchg = futex_atomic_cmpxchg_pt,
388};