blob: 1e41fda74bd38a0ae459ba884b598b9ed78425ce [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 */
Magnus Damm1e6760c2008-02-07 19:50:52 +090013#ifndef __ASM_SH_UACCESS_32_H
14#define __ASM_SH_UACCESS_32_H
Paul Mundt9b01bd92007-11-10 19:55:50 +090015
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.
Paul Mundt9b01bd92007-11-10 19:55:50 +090058 */
59static inline int __access_ok(unsigned long addr, unsigned long size)
60{
Paul Mundt9460c0c2008-03-21 18:08:58 +090061 return 1;
Paul Mundt9b01bd92007-11-10 19:55:50 +090062}
63#else /* CONFIG_MMU */
64#define __addr_ok(addr) \
65 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
66
67#define get_fs() (current_thread_info()->addr_limit)
68#define set_fs(x) (current_thread_info()->addr_limit = (x))
69
70/*
71 * __access_ok: Check if address with size is OK or not.
72 *
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090073 * Uhhuh, this needs 33-bit arithmetic. We have a carry..
Paul Mundt9b01bd92007-11-10 19:55:50 +090074 *
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090075 * sum := addr + size; carry? --> flag = true;
76 * if (sum >= addr_limit) flag = true;
Paul Mundt9b01bd92007-11-10 19:55:50 +090077 */
78static inline int __access_ok(unsigned long addr, unsigned long size)
79{
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090080 unsigned long flag, sum;
Paul Mundt9b01bd92007-11-10 19:55:50 +090081
Stuart Menefy0fb19dc2007-11-30 18:16:23 +090082 __asm__("clrt\n\t"
83 "addc %3, %1\n\t"
84 "movt %0\n\t"
85 "cmp/hi %4, %1\n\t"
86 "rotcl %0"
87 :"=&r" (flag), "=r" (sum)
88 :"1" (addr), "r" (size),
89 "r" (current_thread_info()->addr_limit.seg)
90 :"t");
Paul Mundt9b01bd92007-11-10 19:55:50 +090091 return flag == 0;
92}
93#endif /* CONFIG_MMU */
94
Paul Mundt0465b9f2007-12-26 18:37:16 +090095#define access_ok(type, addr, size) \
96 (__chk_user_ptr(addr), \
97 __access_ok((unsigned long __force)(addr), (size)))
Paul Mundt9b01bd92007-11-10 19:55:50 +090098
99/*
100 * Uh, these should become the main single-value transfer routines ...
101 * They automatically use the right size if we just have the right
102 * pointer type ...
103 *
104 * As SuperH uses the same address space for kernel and user data, we
105 * can just do these as direct assignments.
106 *
107 * Careful to not
108 * (a) re-use the arguments for side effects (sizeof is ok)
109 * (b) require any knowledge of processes at this stage
110 */
Paul Mundt0465b9f2007-12-26 18:37:16 +0900111#define put_user(x,ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
112#define get_user(x,ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
Paul Mundt9b01bd92007-11-10 19:55:50 +0900113
114/*
115 * The "__xxx" versions do not do address space checking, useful when
116 * doing multiple accesses to the same area (the user has to do the
117 * checks by hand with "access_ok()")
118 */
Paul Mundt0465b9f2007-12-26 18:37:16 +0900119#define __put_user(x,ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
120#define __get_user(x,ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
Paul Mundt9b01bd92007-11-10 19:55:50 +0900121
122struct __large_struct { unsigned long buf[100]; };
123#define __m(x) (*(struct __large_struct __user *)(x))
124
125#define __get_user_size(x,ptr,size,retval) \
126do { \
127 retval = 0; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900128 switch (size) { \
129 case 1: \
130 __get_user_asm(x, ptr, retval, "b"); \
131 break; \
132 case 2: \
133 __get_user_asm(x, ptr, retval, "w"); \
134 break; \
135 case 4: \
136 __get_user_asm(x, ptr, retval, "l"); \
137 break; \
138 default: \
139 __get_user_unknown(); \
140 break; \
141 } \
142} while (0)
143
144#define __get_user_nocheck(x,ptr,size) \
145({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900146 long __gu_err; \
147 unsigned long __gu_val; \
148 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
149 __chk_user_ptr(ptr); \
150 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900151 (x) = (__typeof__(*(ptr)))__gu_val; \
152 __gu_err; \
153})
154
Paul Mundt9b01bd92007-11-10 19:55:50 +0900155#define __get_user_check(x,ptr,size) \
156({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900157 long __gu_err = -EFAULT; \
158 unsigned long __gu_val = 0; \
159 const __typeof__(*(ptr)) *__gu_addr = (ptr); \
160 if (likely(access_ok(VERIFY_READ, __gu_addr, (size)))) \
161 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900162 (x) = (__typeof__(*(ptr)))__gu_val; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900163 __gu_err; \
164})
Paul Mundt9b01bd92007-11-10 19:55:50 +0900165
166#define __get_user_asm(x, addr, err, insn) \
167({ \
168__asm__ __volatile__( \
169 "1:\n\t" \
170 "mov." insn " %2, %1\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900171 "2:\n" \
172 ".section .fixup,\"ax\"\n" \
173 "3:\n\t" \
174 "mov #0, %1\n\t" \
175 "mov.l 4f, %0\n\t" \
176 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900177 " mov %3, %0\n\t" \
178 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900179 "4: .long 2b\n\t" \
180 ".previous\n" \
181 ".section __ex_table,\"a\"\n\t" \
182 ".long 1b, 3b\n\t" \
183 ".previous" \
184 :"=&r" (err), "=&r" (x) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900185 :"m" (__m(addr)), "i" (-EFAULT), "0" (err)); })
Paul Mundt9b01bd92007-11-10 19:55:50 +0900186
187extern void __get_user_unknown(void);
188
189#define __put_user_size(x,ptr,size,retval) \
190do { \
191 retval = 0; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900192 switch (size) { \
193 case 1: \
194 __put_user_asm(x, ptr, retval, "b"); \
195 break; \
196 case 2: \
197 __put_user_asm(x, ptr, retval, "w"); \
198 break; \
199 case 4: \
200 __put_user_asm(x, ptr, retval, "l"); \
201 break; \
202 case 8: \
203 __put_user_u64(x, ptr, retval); \
204 break; \
205 default: \
206 __put_user_unknown(); \
207 } \
208} while (0)
209
Paul Mundt0465b9f2007-12-26 18:37:16 +0900210#define __put_user_nocheck(x,ptr,size) \
211({ \
212 long __pu_err; \
213 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
214 __chk_user_ptr(ptr); \
215 __put_user_size((x), __pu_addr, (size), __pu_err); \
216 __pu_err; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900217})
218
219#define __put_user_check(x,ptr,size) \
220({ \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900221 long __pu_err = -EFAULT; \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900222 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
Paul Mundt0465b9f2007-12-26 18:37:16 +0900223 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
224 __put_user_size((x), __pu_addr, (size), \
225 __pu_err); \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900226 __pu_err; \
227})
228
229#define __put_user_asm(x, addr, err, insn) \
230({ \
231__asm__ __volatile__( \
232 "1:\n\t" \
233 "mov." insn " %1, %2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900234 "2:\n" \
235 ".section .fixup,\"ax\"\n" \
236 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900237 "mov.l 4f, %0\n\t" \
238 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900239 " mov %3, %0\n\t" \
240 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900241 "4: .long 2b\n\t" \
242 ".previous\n" \
243 ".section __ex_table,\"a\"\n\t" \
244 ".long 1b, 3b\n\t" \
245 ".previous" \
246 :"=&r" (err) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900247 :"r" (x), "m" (__m(addr)), "i" (-EFAULT), "0" (err) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900248 :"memory"); })
249
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900250#if defined(CONFIG_CPU_LITTLE_ENDIAN)
Paul Mundt9b01bd92007-11-10 19:55:50 +0900251#define __put_user_u64(val,addr,retval) \
252({ \
253__asm__ __volatile__( \
254 "1:\n\t" \
255 "mov.l %R1,%2\n\t" \
256 "mov.l %S1,%T2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900257 "2:\n" \
258 ".section .fixup,\"ax\"\n" \
259 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900260 "mov.l 4f,%0\n\t" \
261 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900262 " mov %3,%0\n\t" \
263 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900264 "4: .long 2b\n\t" \
265 ".previous\n" \
266 ".section __ex_table,\"a\"\n\t" \
267 ".long 1b, 3b\n\t" \
268 ".previous" \
269 : "=r" (retval) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900270 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900271 : "memory"); })
272#else
273#define __put_user_u64(val,addr,retval) \
274({ \
275__asm__ __volatile__( \
276 "1:\n\t" \
277 "mov.l %S1,%2\n\t" \
278 "mov.l %R1,%T2\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900279 "2:\n" \
280 ".section .fixup,\"ax\"\n" \
281 "3:\n\t" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900282 "mov.l 4f,%0\n\t" \
283 "jmp @%0\n\t" \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900284 " mov %3,%0\n\t" \
285 ".balign 4\n" \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900286 "4: .long 2b\n\t" \
287 ".previous\n" \
288 ".section __ex_table,\"a\"\n\t" \
289 ".long 1b, 3b\n\t" \
290 ".previous" \
291 : "=r" (retval) \
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900292 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
Paul Mundt9b01bd92007-11-10 19:55:50 +0900293 : "memory"); })
294#endif
295
296extern void __put_user_unknown(void);
297
298/* Generic arbitrary sized copy. */
299/* Return the number of bytes NOT copied */
300__kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n);
301
Paul Mundt9b01bd92007-11-10 19:55:50 +0900302
303static __always_inline unsigned long
304__copy_from_user(void *to, const void __user *from, unsigned long n)
305{
306 return __copy_user(to, (__force void *)from, n);
307}
308
309static __always_inline unsigned long __must_check
310__copy_to_user(void __user *to, const void *from, unsigned long n)
311{
312 return __copy_user((__force void *)to, from, n);
313}
314
315#define __copy_to_user_inatomic __copy_to_user
316#define __copy_from_user_inatomic __copy_from_user
317
318/*
319 * Clear the area and return remaining number of bytes
320 * (on failure. Usually it's 0.)
321 */
322extern __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
323
324#define clear_user(addr,n) ({ \
325void * __cl_addr = (addr); \
326unsigned long __cl_size = (n); \
327if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
328__cl_size = __clear_user(__cl_addr, __cl_size); \
329__cl_size; })
330
331static __inline__ int
332__strncpy_from_user(unsigned long __dest, unsigned long __user __src, int __count)
333{
334 __kernel_size_t res;
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900335 unsigned long __dummy, _d, _s, _c;
Paul Mundt9b01bd92007-11-10 19:55:50 +0900336
337 __asm__ __volatile__(
338 "9:\n"
339 "mov.b @%2+, %1\n\t"
340 "cmp/eq #0, %1\n\t"
341 "bt/s 2f\n"
342 "1:\n"
343 "mov.b %1, @%3\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900344 "dt %4\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900345 "bf/s 9b\n\t"
346 " add #1, %3\n\t"
347 "2:\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900348 "sub %4, %0\n"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900349 "3:\n"
350 ".section .fixup,\"ax\"\n"
351 "4:\n\t"
352 "mov.l 5f, %1\n\t"
353 "jmp @%1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900354 " mov %9, %0\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900355 ".balign 4\n"
356 "5: .long 3b\n"
357 ".previous\n"
358 ".section __ex_table,\"a\"\n"
359 " .balign 4\n"
360 " .long 9b,4b\n"
361 ".previous"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900362 : "=r" (res), "=&z" (__dummy), "=r" (_s), "=r" (_d), "=r"(_c)
363 : "0" (__count), "2" (__src), "3" (__dest), "4" (__count),
Paul Mundt9b01bd92007-11-10 19:55:50 +0900364 "i" (-EFAULT)
365 : "memory", "t");
366
367 return res;
368}
369
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900370/**
371 * strncpy_from_user: - Copy a NUL terminated string from userspace.
372 * @dst: Destination address, in kernel space. This buffer must be at
373 * least @count bytes long.
374 * @src: Source address, in user space.
375 * @count: Maximum number of bytes to copy, including the trailing NUL.
376 *
377 * Copies a NUL-terminated string from userspace to kernel space.
378 *
379 * On success, returns the length of the string (not including the trailing
380 * NUL).
381 *
382 * If access to userspace fails, returns -EFAULT (some data may have been
383 * copied).
384 *
385 * If @count is smaller than the length of the string, copies @count bytes
386 * and returns @count.
387 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900388#define strncpy_from_user(dest,src,count) ({ \
389unsigned long __sfu_src = (unsigned long) (src); \
390int __sfu_count = (int) (count); \
391long __sfu_res = -EFAULT; \
392if(__access_ok(__sfu_src, __sfu_count)) { \
393__sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
394} __sfu_res; })
395
396/*
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900397 * Return the size of a string (including the ending 0 even when we have
398 * exceeded the maximum string length).
Paul Mundt9b01bd92007-11-10 19:55:50 +0900399 */
400static __inline__ long __strnlen_user(const char __user *__s, long __n)
401{
402 unsigned long res;
403 unsigned long __dummy;
404
405 __asm__ __volatile__(
Paul Mundt9b01bd92007-11-10 19:55:50 +0900406 "1:\t"
407 "mov.b @(%0,%3), %1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900408 "cmp/eq %4, %0\n\t"
409 "bt/s 2f\n\t"
410 " add #1, %0\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900411 "tst %1, %1\n\t"
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900412 "bf 1b\n\t"
Paul Mundt9b01bd92007-11-10 19:55:50 +0900413 "2:\n"
414 ".section .fixup,\"ax\"\n"
415 "3:\n\t"
416 "mov.l 4f, %1\n\t"
417 "jmp @%1\n\t"
418 " mov #0, %0\n"
419 ".balign 4\n"
420 "4: .long 2b\n"
421 ".previous\n"
422 ".section __ex_table,\"a\"\n"
423 " .balign 4\n"
424 " .long 1b,3b\n"
425 ".previous"
426 : "=z" (res), "=&r" (__dummy)
427 : "0" (0), "r" (__s), "r" (__n)
428 : "t");
429 return res;
430}
431
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900432/**
433 * strnlen_user: - Get the size of a string in user space.
434 * @s: The string to measure.
435 * @n: The maximum valid length
436 *
437 * Context: User context only. This function may sleep.
438 *
439 * Get the size of a NUL-terminated string in user space.
440 *
441 * Returns the size of the string INCLUDING the terminating NUL.
442 * On exception, returns 0.
443 * If the string is too long, returns a value greater than @n.
444 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900445static __inline__ long strnlen_user(const char __user *s, long n)
446{
447 if (!__addr_ok(s))
448 return 0;
449 else
450 return __strnlen_user(s, n);
451}
452
Stuart Menefy0fb19dc2007-11-30 18:16:23 +0900453/**
454 * strlen_user: - Get the size of a string in user space.
455 * @str: The string to measure.
456 *
457 * Context: User context only. This function may sleep.
458 *
459 * Get the size of a NUL-terminated string in user space.
460 *
461 * Returns the size of the string INCLUDING the terminating NUL.
462 * On exception, returns 0.
463 *
464 * If there is a limit on the length of a valid string, you may wish to
465 * consider using strnlen_user() instead.
466 */
Paul Mundt9b01bd92007-11-10 19:55:50 +0900467#define strlen_user(str) strnlen_user(str, ~0UL >> 1)
468
469/*
470 * The exception table consists of pairs of addresses: the first is the
471 * address of an instruction that is allowed to fault, and the second is
472 * the address at which the program should continue. No registers are
473 * modified, so it is entirely up to the continuation code to figure out
474 * what to do.
475 *
476 * All the routines below use bits of fixup code that are out of line
477 * with the main instruction path. This means when everything is well,
478 * we don't even have to jump over them. Further, they do not intrude
479 * on our cache or tlb entries.
480 */
481
482struct exception_table_entry
483{
484 unsigned long insn, fixup;
485};
486
487extern int fixup_exception(struct pt_regs *regs);
488
Magnus Damm1e6760c2008-02-07 19:50:52 +0900489#endif /* __ASM_SH_UACCESS_32_H */