blob: b6082f3c1dc4f7766cae31cfa7ecae1b3cc3680c [file] [log] [blame]
Paul Mundt9b01bd92007-11-10 19:55:50 +09001/* $Id: uaccess.h,v 1.11 2003/10/13 07:21:20 lethal Exp $
2 *
3 * User space memory access functions
4 *
5 * Copyright (C) 1999, 2002 Niibe Yutaka
6 * Copyright (C) 2003 Paul Mundt
7 *
8 * Based on:
9 * MIPS implementation version 1.15 by
10 * Copyright (C) 1996, 1997, 1998 by Ralf Baechle
11 * and i386 version.
12 */
13#ifndef __ASM_SH_UACCESS_H
14#define __ASM_SH_UACCESS_H
15
16#include <linux/errno.h>
17#include <linux/sched.h>
18
19#define VERIFY_READ 0
20#define VERIFY_WRITE 1
21
22/*
23 * The fs value determines whether argument validity checking should be
24 * performed or not. If get_fs() == USER_DS, checking is performed, with
25 * get_fs() == KERNEL_DS, checking is bypassed.
26 *
27 * For historical reasons (Data Segment Register?), these macros are misnamed.
28 */
29
30#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
31
32#define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL)
33#define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
34
35#define segment_eq(a,b) ((a).seg == (b).seg)
36
37#define get_ds() (KERNEL_DS)
38
39#if !defined(CONFIG_MMU)
40/* NOMMU is always true */
41#define __addr_ok(addr) (1)
42
43static inline mm_segment_t get_fs(void)
44{
45 return USER_DS;
46}
47
48static inline void set_fs(mm_segment_t s)
49{
50}
51
52/*
53 * __access_ok: Check if address with size is OK or not.
54 *
55 * If we don't have an MMU (or if its disabled) the only thing we really have
56 * to look out for is if the address resides somewhere outside of what
57 * available RAM we have.
58 *
59 * TODO: This check could probably also stand to be restricted somewhat more..
60 * though it still does the Right Thing(tm) for the time being.
61 */
62static inline int __access_ok(unsigned long addr, unsigned long size)
63{
64 return ((addr >= memory_start) && ((addr + size) < memory_end));
65}
66#else /* CONFIG_MMU */
67#define __addr_ok(addr) \
68 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
69
70#define get_fs() (current_thread_info()->addr_limit)
71#define set_fs(x) (current_thread_info()->addr_limit = (x))
72
73/*
74 * __access_ok: Check if address with size is OK or not.
75 *
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090076 * Uhhuh, this needs 33-bit arithmetic. We have a carry..
Paul Mundt9b01bd92007-11-10 19:55:50 +090077 *
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090078 * sum := addr + size; carry? --> flag = true;
79 * if (sum >= addr_limit) flag = true;
Paul Mundt9b01bd92007-11-10 19:55:50 +090080 */
81static inline int __access_ok(unsigned long addr, unsigned long size)
82{
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090083 unsigned long flag, sum;
Paul Mundt9b01bd92007-11-10 19:55:50 +090084
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090085 __asm__("clrt\n\t"
86 "addc %3, %1\n\t"
87 "movt %0\n\t"
88 "cmp/hi %4, %1\n\t"
89 "rotcl %0"
90 :"=&r" (flag), "=r" (sum)
91 :"1" (addr), "r" (size),
92 "r" (current_thread_info()->addr_limit.seg)
93 :"t");
Paul Mundt9b01bd92007-11-10 19:55:50 +090094 return flag == 0;
95}
96#endif /* CONFIG_MMU */
97
Paul Mundt0465b9f2007-12-26 18:37:16 +090098#define access_ok(type, addr, size) \
99 (__chk_user_ptr(addr), \
100 __access_ok((unsigned long __force)(addr), (size)))
Paul Mundt9b01bd92007-11-10 19:55:50 +0900101
102/*
103 * Uh, these should become the main single-value transfer routines ...
104 * They automatically use the right size if we just have the right
105 * pointer type ...
106 *
107 * As SuperH uses the same address space for kernel and user data, we
108 * can just do these as direct assignments.
109 *
110 * Careful to not
111 * (a) re-use the arguments for side effects (sizeof is ok)
112 * (b) require any knowledge of processes at this stage
113 */
Paul Mundt0465b9f2007-12-26 18:37:16 +0900114#define put_user(x,ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
115#define get_user(x,ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
Paul Mundt9b01bd92007-11-10 19:55:50 +0900116
117/*
118 * The "__xxx" versions do not do address space checking, useful when
119 * doing multiple accesses to the same area (the user has to do the
120 * checks by hand with "access_ok()")
121 */
Paul Mundt0465b9f2007-12-26 18:37:16 +0900122#define __put_user(x,ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
123#define __get_user(x,ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Paul Mundt9b01bd92007-11-10 19:55:50 +0900124
125struct __large_struct { unsigned long buf[100]; };
126#define __m(x) (*(struct __large_struct __user *)(x))
127
128#define __get_user_size(x,ptr,size,retval) \
129do { \
130 retval = 0; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900131 switch (size) { \
132 case 1: \
133 __get_user_asm(x, ptr, retval, "b"); \
134 break; \
135 case 2: \
136 __get_user_asm(x, ptr, retval, "w"); \
137 break; \
138 case 4: \
139 __get_user_asm(x, ptr, retval, "l"); \
140 break; \
141 default: \
142 __get_user_unknown(); \
143 break; \
144 } \
145} while (0)
146
147#define __get_user_nocheck(x,ptr,size) \
148({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900149 long __gu_err; \
150 unsigned long __gu_val; \
151 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
152 __chk_user_ptr(ptr); \
153 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900154 (x) = (__typeof__(*(ptr)))__gu_val; \
155 __gu_err; \
156})
157
Paul Mundt9b01bd92007-11-10 19:55:50 +0900158#define __get_user_check(x,ptr,size) \
159({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900160 long __gu_err = -EFAULT; \
161 unsigned long __gu_val = 0; \
162 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
163 if (likely(access_ok(VERIFY_READ, __gu_addr, (size)))) \
164 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900165 (x) = (__typeof__(*(ptr)))__gu_val; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900166 __gu_err; \
167})
Paul Mundt9b01bd92007-11-10 19:55:50 +0900168
169#define __get_user_asm(x, addr, err, insn) \
170({ \
171__asm__ __volatile__( \
172 "1:\n\t" \
173 "mov." insn " %2, %1\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900174 "2:\n" \
175 ".section .fixup,\"ax\"\n" \
176 "3:\n\t" \
177 "mov #0, %1\n\t" \
178 "mov.l 4f, %0\n\t" \
179 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900180 " mov %3, %0\n\t" \
181 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900182 "4: .long 2b\n\t" \
183 ".previous\n" \
184 ".section __ex_table,\"a\"\n\t" \
185 ".long 1b, 3b\n\t" \
186 ".previous" \
187 :"=&r" (err), "=&r" (x) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900188 :"m" (__m(addr)), "i" (-EFAULT), "0" (err)); })
Paul Mundt9b01bd92007-11-10 19:55:50 +0900189
190extern void __get_user_unknown(void);
191
192#define __put_user_size(x,ptr,size,retval) \
193do { \
194 retval = 0; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900195 switch (size) { \
196 case 1: \
197 __put_user_asm(x, ptr, retval, "b"); \
198 break; \
199 case 2: \
200 __put_user_asm(x, ptr, retval, "w"); \
201 break; \
202 case 4: \
203 __put_user_asm(x, ptr, retval, "l"); \
204 break; \
205 case 8: \
206 __put_user_u64(x, ptr, retval); \
207 break; \
208 default: \
209 __put_user_unknown(); \
210 } \
211} while (0)
212
Paul Mundt0465b9f2007-12-26 18:37:16 +0900213#define __put_user_nocheck(x,ptr,size) \
214({ \
215 long __pu_err; \
216 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
217 __chk_user_ptr(ptr); \
218 __put_user_size((x), __pu_addr, (size), __pu_err); \
219 __pu_err; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900220})
221
222#define __put_user_check(x,ptr,size) \
223({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900224 long __pu_err = -EFAULT; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900225 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900226 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
227 __put_user_size((x), __pu_addr, (size), \
228 __pu_err); \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900229 __pu_err; \
230})
231
232#define __put_user_asm(x, addr, err, insn) \
233({ \
234__asm__ __volatile__( \
235 "1:\n\t" \
236 "mov." insn " %1, %2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900237 "2:\n" \
238 ".section .fixup,\"ax\"\n" \
239 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900240 "mov.l 4f, %0\n\t" \
241 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900242 " mov %3, %0\n\t" \
243 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900244 "4: .long 2b\n\t" \
245 ".previous\n" \
246 ".section __ex_table,\"a\"\n\t" \
247 ".long 1b, 3b\n\t" \
248 ".previous" \
249 :"=&r" (err) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900250 :"r" (x), "m" (__m(addr)), "i" (-EFAULT), "0" (err) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900251 :"memory"); })
252
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900253#if defined(CONFIG_CPU_LITTLE_ENDIAN)
Paul Mundt9b01bd92007-11-10 19:55:50 +0900254#define __put_user_u64(val,addr,retval) \
255({ \
256__asm__ __volatile__( \
257 "1:\n\t" \
258 "mov.l %R1,%2\n\t" \
259 "mov.l %S1,%T2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900260 "2:\n" \
261 ".section .fixup,\"ax\"\n" \
262 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900263 "mov.l 4f,%0\n\t" \
264 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900265 " mov %3,%0\n\t" \
266 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900267 "4: .long 2b\n\t" \
268 ".previous\n" \
269 ".section __ex_table,\"a\"\n\t" \
270 ".long 1b, 3b\n\t" \
271 ".previous" \
272 : "=r" (retval) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900273 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900274 : "memory"); })
275#else
276#define __put_user_u64(val,addr,retval) \
277({ \
278__asm__ __volatile__( \
279 "1:\n\t" \
280 "mov.l %S1,%2\n\t" \
281 "mov.l %R1,%T2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900282 "2:\n" \
283 ".section .fixup,\"ax\"\n" \
284 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900285 "mov.l 4f,%0\n\t" \
286 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900287 " mov %3,%0\n\t" \
288 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900289 "4: .long 2b\n\t" \
290 ".previous\n" \
291 ".section __ex_table,\"a\"\n\t" \
292 ".long 1b, 3b\n\t" \
293 ".previous" \
294 : "=r" (retval) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900295 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900296 : "memory"); })
297#endif
298
299extern void __put_user_unknown(void);
300
301/* Generic arbitrary sized copy. */
302/* Return the number of bytes NOT copied */
303__kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n);
304
305#define copy_to_user(to,from,n) ({ \
306void *__copy_to = (void *) (to); \
307__kernel_size_t __copy_size = (__kernel_size_t) (n); \
308__kernel_size_t __copy_res; \
309if(__copy_size && __access_ok((unsigned long)__copy_to, __copy_size)) { \
310__copy_res = __copy_user(__copy_to, (void *) (from), __copy_size); \
311} else __copy_res = __copy_size; \
312__copy_res; })
313
314#define copy_from_user(to,from,n) ({ \
315void *__copy_to = (void *) (to); \
316void *__copy_from = (void *) (from); \
317__kernel_size_t __copy_size = (__kernel_size_t) (n); \
318__kernel_size_t __copy_res; \
319if(__copy_size && __access_ok((unsigned long)__copy_from, __copy_size)) { \
320__copy_res = __copy_user(__copy_to, __copy_from, __copy_size); \
321} else __copy_res = __copy_size; \
322__copy_res; })
323
324static __always_inline unsigned long
325__copy_from_user(void *to, const void __user *from, unsigned long n)
326{
327 return __copy_user(to, (__force void *)from, n);
328}
329
330static __always_inline unsigned long __must_check
331__copy_to_user(void __user *to, const void *from, unsigned long n)
332{
333 return __copy_user((__force void *)to, from, n);
334}
335
336#define __copy_to_user_inatomic __copy_to_user
337#define __copy_from_user_inatomic __copy_from_user
338
339/*
340 * Clear the area and return remaining number of bytes
341 * (on failure. Usually it's 0.)
342 */
343extern __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
344
345#define clear_user(addr,n) ({ \
346void * __cl_addr = (addr); \
347unsigned long __cl_size = (n); \
348if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
349__cl_size = __clear_user(__cl_addr, __cl_size); \
350__cl_size; })
351
352static __inline__ int
353__strncpy_from_user(unsigned long __dest, unsigned long __user __src, int __count)
354{
355 __kernel_size_t res;
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900356 unsigned long __dummy, _d, _s, _c;
Paul Mundt9b01bd92007-11-10 19:55:50 +0900357
358 __asm__ __volatile__(
359 "9:\n"
360 "mov.b @%2+, %1\n\t"
361 "cmp/eq #0, %1\n\t"
362 "bt/s 2f\n"
363 "1:\n"
364 "mov.b %1, @%3\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900365 "dt %4\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900366 "bf/s 9b\n\t"
367 " add #1, %3\n\t"
368 "2:\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900369 "sub %4, %0\n"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900370 "3:\n"
371 ".section .fixup,\"ax\"\n"
372 "4:\n\t"
373 "mov.l 5f, %1\n\t"
374 "jmp @%1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900375 " mov %9, %0\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900376 ".balign 4\n"
377 "5: .long 3b\n"
378 ".previous\n"
379 ".section __ex_table,\"a\"\n"
380 " .balign 4\n"
381 " .long 9b,4b\n"
382 ".previous"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900383 : "=r" (res), "=&z" (__dummy), "=r" (_s), "=r" (_d), "=r"(_c)
384 : "0" (__count), "2" (__src), "3" (__dest), "4" (__count),
Paul Mundt9b01bd92007-11-10 19:55:50 +0900385 "i" (-EFAULT)
386 : "memory", "t");
387
388 return res;
389}
390
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900391/**
392 * strncpy_from_user: - Copy a NUL terminated string from userspace.
393 * @dst: Destination address, in kernel space. This buffer must be at
394 * least @count bytes long.
395 * @src: Source address, in user space.
396 * @count: Maximum number of bytes to copy, including the trailing NUL.
397 *
398 * Copies a NUL-terminated string from userspace to kernel space.
399 *
400 * On success, returns the length of the string (not including the trailing
401 * NUL).
402 *
403 * If access to userspace fails, returns -EFAULT (some data may have been
404 * copied).
405 *
406 * If @count is smaller than the length of the string, copies @count bytes
407 * and returns @count.
408 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900409#define strncpy_from_user(dest,src,count) ({ \
410unsigned long __sfu_src = (unsigned long) (src); \
411int __sfu_count = (int) (count); \
412long __sfu_res = -EFAULT; \
413if(__access_ok(__sfu_src, __sfu_count)) { \
414__sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
415} __sfu_res; })
416
417/*
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900418 * Return the size of a string (including the ending 0 even when we have
419 * exceeded the maximum string length).
Paul Mundt9b01bd92007-11-10 19:55:50 +0900420 */
421static __inline__ long __strnlen_user(const char __user *__s, long __n)
422{
423 unsigned long res;
424 unsigned long __dummy;
425
426 __asm__ __volatile__(
Paul Mundt9b01bd92007-11-10 19:55:50 +0900427 "1:\t"
428 "mov.b @(%0,%3), %1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900429 "cmp/eq %4, %0\n\t"
430 "bt/s 2f\n\t"
431 " add #1, %0\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900432 "tst %1, %1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900433 "bf 1b\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900434 "2:\n"
435 ".section .fixup,\"ax\"\n"
436 "3:\n\t"
437 "mov.l 4f, %1\n\t"
438 "jmp @%1\n\t"
439 " mov #0, %0\n"
440 ".balign 4\n"
441 "4: .long 2b\n"
442 ".previous\n"
443 ".section __ex_table,\"a\"\n"
444 " .balign 4\n"
445 " .long 1b,3b\n"
446 ".previous"
447 : "=z" (res), "=&r" (__dummy)
448 : "0" (0), "r" (__s), "r" (__n)
449 : "t");
450 return res;
451}
452
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900453/**
454 * strnlen_user: - Get the size of a string in user space.
455 * @s: The string to measure.
456 * @n: The maximum valid length
457 *
458 * Context: User context only. This function may sleep.
459 *
460 * Get the size of a NUL-terminated string in user space.
461 *
462 * Returns the size of the string INCLUDING the terminating NUL.
463 * On exception, returns 0.
464 * If the string is too long, returns a value greater than @n.
465 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900466static __inline__ long strnlen_user(const char __user *s, long n)
467{
468 if (!__addr_ok(s))
469 return 0;
470 else
471 return __strnlen_user(s, n);
472}
473
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900474/**
475 * strlen_user: - Get the size of a string in user space.
476 * @str: The string to measure.
477 *
478 * Context: User context only. This function may sleep.
479 *
480 * Get the size of a NUL-terminated string in user space.
481 *
482 * Returns the size of the string INCLUDING the terminating NUL.
483 * On exception, returns 0.
484 *
485 * If there is a limit on the length of a valid string, you may wish to
486 * consider using strnlen_user() instead.
487 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900488#define strlen_user(str) strnlen_user(str, ~0UL >> 1)
489
490/*
491 * The exception table consists of pairs of addresses: the first is the
492 * address of an instruction that is allowed to fault, and the second is
493 * the address at which the program should continue. No registers are
494 * modified, so it is entirely up to the continuation code to figure out
495 * what to do.
496 *
497 * All the routines below use bits of fixup code that are out of line
498 * with the main instruction path. This means when everything is well,
499 * we don't even have to jump over them. Further, they do not intrude
500 * on our cache or tlb entries.
501 */
502
503struct exception_table_entry
504{
505 unsigned long insn, fixup;
506};
507
508extern int fixup_exception(struct pt_regs *regs);
509
510#endif /* __ASM_SH_UACCESS_H */