blob: 44abd16823294209279499f8f8f3384c093ddb2f [file] [log] [blame]
Paul Mundt02f7e622008-06-03 18:48:54 +09001/*
Paul Mundt9b01bd92007-11-10 19:55:50 +09002 * User space memory access functions
3 *
4 * Copyright (C) 1999, 2002 Niibe Yutaka
Paul Mundt02f7e622008-06-03 18:48:54 +09005 * Copyright (C) 2003 - 2008 Paul Mundt
Paul Mundt9b01bd92007-11-10 19:55:50 +09006 *
7 * Based on:
8 * MIPS implementation version 1.15 by
9 * Copyright (C) 1996, 1997, 1998 by Ralf Baechle
10 * and i386 version.
11 */
Magnus Damm1e6760c2008-02-07 19:50:52 +090012#ifndef __ASM_SH_UACCESS_32_H
13#define __ASM_SH_UACCESS_32_H
Paul Mundt9b01bd92007-11-10 19:55:50 +090014
15#include <linux/errno.h>
16#include <linux/sched.h>
Paul Mundt02f7e622008-06-03 18:48:54 +090017#include <asm/segment.h>
Paul Mundt9b01bd92007-11-10 19:55:50 +090018
19#define VERIFY_READ 0
20#define VERIFY_WRITE 1
21
Paul Mundt9b01bd92007-11-10 19:55:50 +090022#define __addr_ok(addr) \
Paul Mundt31f6a112008-06-03 19:38:22 +090023 ((unsigned long __force)(addr) < current_thread_info()->addr_limit.seg)
Paul Mundt9b01bd92007-11-10 19:55:50 +090024
25/*
26 * __access_ok: Check if address with size is OK or not.
27 *
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090028 * Uhhuh, this needs 33-bit arithmetic. We have a carry..
Paul Mundt9b01bd92007-11-10 19:55:50 +090029 *
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090030 * sum := addr + size; carry? --> flag = true;
31 * if (sum >= addr_limit) flag = true;
Paul Mundt9b01bd92007-11-10 19:55:50 +090032 */
Paul Mundt31f6a112008-06-03 19:38:22 +090033#define __access_ok(addr, size) \
34 (__addr_ok((addr) + (size)))
Paul Mundt0465b9f2007-12-26 18:37:16 +090035#define access_ok(type, addr, size) \
36 (__chk_user_ptr(addr), \
37 __access_ok((unsigned long __force)(addr), (size)))
Paul Mundt9b01bd92007-11-10 19:55:50 +090038
39/*
40 * Uh, these should become the main single-value transfer routines ...
41 * They automatically use the right size if we just have the right
42 * pointer type ...
43 *
44 * As SuperH uses the same address space for kernel and user data, we
45 * can just do these as direct assignments.
46 *
47 * Careful to not
48 * (a) re-use the arguments for side effects (sizeof is ok)
49 * (b) require any knowledge of processes at this stage
50 */
Paul Mundt0465b9f2007-12-26 18:37:16 +090051#define put_user(x,ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
52#define get_user(x,ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
Paul Mundt9b01bd92007-11-10 19:55:50 +090053
54/*
55 * The "__xxx" versions do not do address space checking, useful when
56 * doing multiple accesses to the same area (the user has to do the
57 * checks by hand with "access_ok()")
58 */
Paul Mundt0465b9f2007-12-26 18:37:16 +090059#define __put_user(x,ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
60#define __get_user(x,ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Paul Mundt9b01bd92007-11-10 19:55:50 +090061
62struct __large_struct { unsigned long buf[100]; };
63#define __m(x) (*(struct __large_struct __user *)(x))
64
65#define __get_user_size(x,ptr,size,retval) \
66do { \
67 retval = 0; \
Paul Mundt9b01bd92007-11-10 19:55:50 +090068 switch (size) { \
69 case 1: \
70 __get_user_asm(x, ptr, retval, "b"); \
71 break; \
72 case 2: \
73 __get_user_asm(x, ptr, retval, "w"); \
74 break; \
75 case 4: \
76 __get_user_asm(x, ptr, retval, "l"); \
77 break; \
78 default: \
79 __get_user_unknown(); \
80 break; \
81 } \
82} while (0)
83
84#define __get_user_nocheck(x,ptr,size) \
85({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +090086 long __gu_err; \
87 unsigned long __gu_val; \
88 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
89 __chk_user_ptr(ptr); \
90 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
Paul Mundt9b01bd92007-11-10 19:55:50 +090091 (x) = (__typeof__(*(ptr)))__gu_val; \
92 __gu_err; \
93})
94
Paul Mundt9b01bd92007-11-10 19:55:50 +090095#define __get_user_check(x,ptr,size) \
96({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +090097 long __gu_err = -EFAULT; \
98 unsigned long __gu_val = 0; \
99 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
100 if (likely(access_ok(VERIFY_READ, __gu_addr, (size)))) \
101 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900102 (x) = (__typeof__(*(ptr)))__gu_val; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900103 __gu_err; \
104})
Paul Mundt9b01bd92007-11-10 19:55:50 +0900105
106#define __get_user_asm(x, addr, err, insn) \
107({ \
108__asm__ __volatile__( \
109 "1:\n\t" \
110 "mov." insn " %2, %1\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900111 "2:\n" \
112 ".section .fixup,\"ax\"\n" \
113 "3:\n\t" \
114 "mov #0, %1\n\t" \
115 "mov.l 4f, %0\n\t" \
116 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900117 " mov %3, %0\n\t" \
118 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900119 "4: .long 2b\n\t" \
120 ".previous\n" \
121 ".section __ex_table,\"a\"\n\t" \
122 ".long 1b, 3b\n\t" \
123 ".previous" \
124 :"=&r" (err), "=&r" (x) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900125 :"m" (__m(addr)), "i" (-EFAULT), "0" (err)); })
Paul Mundt9b01bd92007-11-10 19:55:50 +0900126
127extern void __get_user_unknown(void);
128
129#define __put_user_size(x,ptr,size,retval) \
130do { \
131 retval = 0; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900132 switch (size) { \
133 case 1: \
134 __put_user_asm(x, ptr, retval, "b"); \
135 break; \
136 case 2: \
137 __put_user_asm(x, ptr, retval, "w"); \
138 break; \
139 case 4: \
140 __put_user_asm(x, ptr, retval, "l"); \
141 break; \
142 case 8: \
143 __put_user_u64(x, ptr, retval); \
144 break; \
145 default: \
146 __put_user_unknown(); \
147 } \
148} while (0)
149
Paul Mundt0465b9f2007-12-26 18:37:16 +0900150#define __put_user_nocheck(x,ptr,size) \
151({ \
152 long __pu_err; \
153 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
154 __chk_user_ptr(ptr); \
155 __put_user_size((x), __pu_addr, (size), __pu_err); \
156 __pu_err; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900157})
158
159#define __put_user_check(x,ptr,size) \
160({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900161 long __pu_err = -EFAULT; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900162 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900163 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
164 __put_user_size((x), __pu_addr, (size), \
165 __pu_err); \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900166 __pu_err; \
167})
168
169#define __put_user_asm(x, addr, err, insn) \
170({ \
171__asm__ __volatile__( \
172 "1:\n\t" \
173 "mov." insn " %1, %2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900174 "2:\n" \
175 ".section .fixup,\"ax\"\n" \
176 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900177 "mov.l 4f, %0\n\t" \
178 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900179 " mov %3, %0\n\t" \
180 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900181 "4: .long 2b\n\t" \
182 ".previous\n" \
183 ".section __ex_table,\"a\"\n\t" \
184 ".long 1b, 3b\n\t" \
185 ".previous" \
186 :"=&r" (err) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900187 :"r" (x), "m" (__m(addr)), "i" (-EFAULT), "0" (err) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900188 :"memory"); })
189
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900190#if defined(CONFIG_CPU_LITTLE_ENDIAN)
Paul Mundt9b01bd92007-11-10 19:55:50 +0900191#define __put_user_u64(val,addr,retval) \
192({ \
193__asm__ __volatile__( \
194 "1:\n\t" \
195 "mov.l %R1,%2\n\t" \
196 "mov.l %S1,%T2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900197 "2:\n" \
198 ".section .fixup,\"ax\"\n" \
199 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900200 "mov.l 4f,%0\n\t" \
201 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900202 " mov %3,%0\n\t" \
203 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900204 "4: .long 2b\n\t" \
205 ".previous\n" \
206 ".section __ex_table,\"a\"\n\t" \
207 ".long 1b, 3b\n\t" \
208 ".previous" \
209 : "=r" (retval) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900210 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900211 : "memory"); })
212#else
213#define __put_user_u64(val,addr,retval) \
214({ \
215__asm__ __volatile__( \
216 "1:\n\t" \
217 "mov.l %S1,%2\n\t" \
218 "mov.l %R1,%T2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900219 "2:\n" \
220 ".section .fixup,\"ax\"\n" \
221 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900222 "mov.l 4f,%0\n\t" \
223 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900224 " mov %3,%0\n\t" \
225 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900226 "4: .long 2b\n\t" \
227 ".previous\n" \
228 ".section __ex_table,\"a\"\n\t" \
229 ".long 1b, 3b\n\t" \
230 ".previous" \
231 : "=r" (retval) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900232 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900233 : "memory"); })
234#endif
235
236extern void __put_user_unknown(void);
237
238/* Generic arbitrary sized copy. */
239/* Return the number of bytes NOT copied */
240__kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n);
241
Paul Mundt9b01bd92007-11-10 19:55:50 +0900242
243static __always_inline unsigned long
244__copy_from_user(void *to, const void __user *from, unsigned long n)
245{
246 return __copy_user(to, (__force void *)from, n);
247}
248
249static __always_inline unsigned long __must_check
250__copy_to_user(void __user *to, const void *from, unsigned long n)
251{
252 return __copy_user((__force void *)to, from, n);
253}
254
255#define __copy_to_user_inatomic __copy_to_user
256#define __copy_from_user_inatomic __copy_from_user
257
258/*
259 * Clear the area and return remaining number of bytes
260 * (on failure. Usually it's 0.)
261 */
262extern __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
263
264#define clear_user(addr,n) ({ \
265void * __cl_addr = (addr); \
266unsigned long __cl_size = (n); \
267if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
268__cl_size = __clear_user(__cl_addr, __cl_size); \
269__cl_size; })
270
271static __inline__ int
272__strncpy_from_user(unsigned long __dest, unsigned long __user __src, int __count)
273{
274 __kernel_size_t res;
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900275 unsigned long __dummy, _d, _s, _c;
Paul Mundt9b01bd92007-11-10 19:55:50 +0900276
277 __asm__ __volatile__(
278 "9:\n"
279 "mov.b @%2+, %1\n\t"
280 "cmp/eq #0, %1\n\t"
281 "bt/s 2f\n"
282 "1:\n"
283 "mov.b %1, @%3\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900284 "dt %4\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900285 "bf/s 9b\n\t"
286 " add #1, %3\n\t"
287 "2:\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900288 "sub %4, %0\n"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900289 "3:\n"
290 ".section .fixup,\"ax\"\n"
291 "4:\n\t"
292 "mov.l 5f, %1\n\t"
293 "jmp @%1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900294 " mov %9, %0\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900295 ".balign 4\n"
296 "5: .long 3b\n"
297 ".previous\n"
298 ".section __ex_table,\"a\"\n"
299 " .balign 4\n"
300 " .long 9b,4b\n"
301 ".previous"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900302 : "=r" (res), "=&z" (__dummy), "=r" (_s), "=r" (_d), "=r"(_c)
303 : "0" (__count), "2" (__src), "3" (__dest), "4" (__count),
Paul Mundt9b01bd92007-11-10 19:55:50 +0900304 "i" (-EFAULT)
305 : "memory", "t");
306
307 return res;
308}
309
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900310/**
311 * strncpy_from_user: - Copy a NUL terminated string from userspace.
312 * @dst: Destination address, in kernel space. This buffer must be at
313 * least @count bytes long.
314 * @src: Source address, in user space.
315 * @count: Maximum number of bytes to copy, including the trailing NUL.
316 *
317 * Copies a NUL-terminated string from userspace to kernel space.
318 *
319 * On success, returns the length of the string (not including the trailing
320 * NUL).
321 *
322 * If access to userspace fails, returns -EFAULT (some data may have been
323 * copied).
324 *
325 * If @count is smaller than the length of the string, copies @count bytes
326 * and returns @count.
327 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900328#define strncpy_from_user(dest,src,count) ({ \
329unsigned long __sfu_src = (unsigned long) (src); \
330int __sfu_count = (int) (count); \
331long __sfu_res = -EFAULT; \
332if(__access_ok(__sfu_src, __sfu_count)) { \
333__sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
334} __sfu_res; })
335
336/*
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900337 * Return the size of a string (including the ending 0 even when we have
338 * exceeded the maximum string length).
Paul Mundt9b01bd92007-11-10 19:55:50 +0900339 */
340static __inline__ long __strnlen_user(const char __user *__s, long __n)
341{
342 unsigned long res;
343 unsigned long __dummy;
344
345 __asm__ __volatile__(
Paul Mundt9b01bd92007-11-10 19:55:50 +0900346 "1:\t"
347 "mov.b @(%0,%3), %1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900348 "cmp/eq %4, %0\n\t"
349 "bt/s 2f\n\t"
350 " add #1, %0\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900351 "tst %1, %1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900352 "bf 1b\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900353 "2:\n"
354 ".section .fixup,\"ax\"\n"
355 "3:\n\t"
356 "mov.l 4f, %1\n\t"
357 "jmp @%1\n\t"
358 " mov #0, %0\n"
359 ".balign 4\n"
360 "4: .long 2b\n"
361 ".previous\n"
362 ".section __ex_table,\"a\"\n"
363 " .balign 4\n"
364 " .long 1b,3b\n"
365 ".previous"
366 : "=z" (res), "=&r" (__dummy)
367 : "0" (0), "r" (__s), "r" (__n)
368 : "t");
369 return res;
370}
371
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900372/**
373 * strnlen_user: - Get the size of a string in user space.
374 * @s: The string to measure.
375 * @n: The maximum valid length
376 *
377 * Context: User context only. This function may sleep.
378 *
379 * Get the size of a NUL-terminated string in user space.
380 *
381 * Returns the size of the string INCLUDING the terminating NUL.
382 * On exception, returns 0.
383 * If the string is too long, returns a value greater than @n.
384 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900385static __inline__ long strnlen_user(const char __user *s, long n)
386{
387 if (!__addr_ok(s))
388 return 0;
389 else
390 return __strnlen_user(s, n);
391}
392
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900393/**
394 * strlen_user: - Get the size of a string in user space.
395 * @str: The string to measure.
396 *
397 * Context: User context only. This function may sleep.
398 *
399 * Get the size of a NUL-terminated string in user space.
400 *
401 * Returns the size of the string INCLUDING the terminating NUL.
402 * On exception, returns 0.
403 *
404 * If there is a limit on the length of a valid string, you may wish to
405 * consider using strnlen_user() instead.
406 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900407#define strlen_user(str) strnlen_user(str, ~0UL >> 1)
408
409/*
410 * The exception table consists of pairs of addresses: the first is the
411 * address of an instruction that is allowed to fault, and the second is
412 * the address at which the program should continue. No registers are
413 * modified, so it is entirely up to the continuation code to figure out
414 * what to do.
415 *
416 * All the routines below use bits of fixup code that are out of line
417 * with the main instruction path. This means when everything is well,
418 * we don't even have to jump over them. Further, they do not intrude
419 * on our cache or tlb entries.
420 */
421
422struct exception_table_entry
423{
424 unsigned long insn, fixup;
425};
426
427extern int fixup_exception(struct pt_regs *regs);
428
Magnus Damm1e6760c2008-02-07 19:50:52 +0900429#endif /* __ASM_SH_UACCESS_32_H */