page allocator: inline some __alloc_pages() wrappers
[linux-2.6/mini2440.git] / include / asm-sh / uaccess_32.h
blob1e41fda74bd38a0ae459ba884b598b9ed78425ce
1 /* $Id: uaccess.h,v 1.11 2003/10/13 07:21:20 lethal Exp $
3 * User space memory access functions
5 * Copyright (C) 1999, 2002 Niibe Yutaka
6 * Copyright (C) 2003 Paul Mundt
8 * Based on:
9 * MIPS implementation version 1.15 by
10 * Copyright (C) 1996, 1997, 1998 by Ralf Baechle
11 * and i386 version.
13 #ifndef __ASM_SH_UACCESS_32_H
14 #define __ASM_SH_UACCESS_32_H
16 #include <linux/errno.h>
17 #include <linux/sched.h>
19 #define VERIFY_READ 0
20 #define VERIFY_WRITE 1
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.
27 * For historical reasons (Data Segment Register?), these macros are misnamed.
30 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
32 #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL)
33 #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
35 #define segment_eq(a,b) ((a).seg == (b).seg)
37 #define get_ds() (KERNEL_DS)
39 #if !defined(CONFIG_MMU)
40 /* NOMMU is always true */
41 #define __addr_ok(addr) (1)
43 static inline mm_segment_t get_fs(void)
45 return USER_DS;
48 static inline void set_fs(mm_segment_t s)
53 * __access_ok: Check if address with size is OK or not.
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.
59 static inline int __access_ok(unsigned long addr, unsigned long size)
61 return 1;
63 #else /* CONFIG_MMU */
64 #define __addr_ok(addr) \
65 ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
67 #define get_fs() (current_thread_info()->addr_limit)
68 #define set_fs(x) (current_thread_info()->addr_limit = (x))
71 * __access_ok: Check if address with size is OK or not.
73 * Uhhuh, this needs 33-bit arithmetic. We have a carry..
75 * sum := addr + size; carry? --> flag = true;
76 * if (sum >= addr_limit) flag = true;
78 static inline int __access_ok(unsigned long addr, unsigned long size)
80 unsigned long flag, sum;
82 __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");
91 return flag == 0;
93 #endif /* CONFIG_MMU */
95 #define access_ok(type, addr, size) \
96 (__chk_user_ptr(addr), \
97 __access_ok((unsigned long __force)(addr), (size)))
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 ...
104 * As SuperH uses the same address space for kernel and user data, we
105 * can just do these as direct assignments.
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
111 #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)))
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()")
119 #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)))
122 struct __large_struct { unsigned long buf[100]; };
123 #define __m(x) (*(struct __large_struct __user *)(x))
125 #define __get_user_size(x,ptr,size,retval) \
126 do { \
127 retval = 0; \
128 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; \
142 } while (0)
144 #define __get_user_nocheck(x,ptr,size) \
145 ({ \
146 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); \
151 (x) = (__typeof__(*(ptr)))__gu_val; \
152 __gu_err; \
155 #define __get_user_check(x,ptr,size) \
156 ({ \
157 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); \
162 (x) = (__typeof__(*(ptr)))__gu_val; \
163 __gu_err; \
166 #define __get_user_asm(x, addr, err, insn) \
167 ({ \
168 __asm__ __volatile__( \
169 "1:\n\t" \
170 "mov." insn " %2, %1\n\t" \
171 "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" \
177 " mov %3, %0\n\t" \
178 ".balign 4\n" \
179 "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) \
185 :"m" (__m(addr)), "i" (-EFAULT), "0" (err)); })
187 extern void __get_user_unknown(void);
189 #define __put_user_size(x,ptr,size,retval) \
190 do { \
191 retval = 0; \
192 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(); \
208 } while (0)
210 #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; \
219 #define __put_user_check(x,ptr,size) \
220 ({ \
221 long __pu_err = -EFAULT; \
222 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
223 if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
224 __put_user_size((x), __pu_addr, (size), \
225 __pu_err); \
226 __pu_err; \
229 #define __put_user_asm(x, addr, err, insn) \
230 ({ \
231 __asm__ __volatile__( \
232 "1:\n\t" \
233 "mov." insn " %1, %2\n\t" \
234 "2:\n" \
235 ".section .fixup,\"ax\"\n" \
236 "3:\n\t" \
237 "mov.l 4f, %0\n\t" \
238 "jmp @%0\n\t" \
239 " mov %3, %0\n\t" \
240 ".balign 4\n" \
241 "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) \
247 :"r" (x), "m" (__m(addr)), "i" (-EFAULT), "0" (err) \
248 :"memory"); })
250 #if defined(CONFIG_CPU_LITTLE_ENDIAN)
251 #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" \
257 "2:\n" \
258 ".section .fixup,\"ax\"\n" \
259 "3:\n\t" \
260 "mov.l 4f,%0\n\t" \
261 "jmp @%0\n\t" \
262 " mov %3,%0\n\t" \
263 ".balign 4\n" \
264 "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) \
270 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
271 : "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" \
279 "2:\n" \
280 ".section .fixup,\"ax\"\n" \
281 "3:\n\t" \
282 "mov.l 4f,%0\n\t" \
283 "jmp @%0\n\t" \
284 " mov %3,%0\n\t" \
285 ".balign 4\n" \
286 "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) \
292 : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
293 : "memory"); })
294 #endif
296 extern void __put_user_unknown(void);
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);
303 static __always_inline unsigned long
304 __copy_from_user(void *to, const void __user *from, unsigned long n)
306 return __copy_user(to, (__force void *)from, n);
309 static __always_inline unsigned long __must_check
310 __copy_to_user(void __user *to, const void *from, unsigned long n)
312 return __copy_user((__force void *)to, from, n);
315 #define __copy_to_user_inatomic __copy_to_user
316 #define __copy_from_user_inatomic __copy_from_user
319 * Clear the area and return remaining number of bytes
320 * (on failure. Usually it's 0.)
322 extern __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
324 #define clear_user(addr,n) ({ \
325 void * __cl_addr = (addr); \
326 unsigned long __cl_size = (n); \
327 if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
328 __cl_size = __clear_user(__cl_addr, __cl_size); \
329 __cl_size; })
331 static __inline__ int
332 __strncpy_from_user(unsigned long __dest, unsigned long __user __src, int __count)
334 __kernel_size_t res;
335 unsigned long __dummy, _d, _s, _c;
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"
344 "dt %4\n\t"
345 "bf/s 9b\n\t"
346 " add #1, %3\n\t"
347 "2:\n\t"
348 "sub %4, %0\n"
349 "3:\n"
350 ".section .fixup,\"ax\"\n"
351 "4:\n\t"
352 "mov.l 5f, %1\n\t"
353 "jmp @%1\n\t"
354 " mov %9, %0\n\t"
355 ".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"
362 : "=r" (res), "=&z" (__dummy), "=r" (_s), "=r" (_d), "=r"(_c)
363 : "0" (__count), "2" (__src), "3" (__dest), "4" (__count),
364 "i" (-EFAULT)
365 : "memory", "t");
367 return res;
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.
377 * Copies a NUL-terminated string from userspace to kernel space.
379 * On success, returns the length of the string (not including the trailing
380 * NUL).
382 * If access to userspace fails, returns -EFAULT (some data may have been
383 * copied).
385 * If @count is smaller than the length of the string, copies @count bytes
386 * and returns @count.
388 #define strncpy_from_user(dest,src,count) ({ \
389 unsigned long __sfu_src = (unsigned long) (src); \
390 int __sfu_count = (int) (count); \
391 long __sfu_res = -EFAULT; \
392 if(__access_ok(__sfu_src, __sfu_count)) { \
393 __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
394 } __sfu_res; })
397 * Return the size of a string (including the ending 0 even when we have
398 * exceeded the maximum string length).
400 static __inline__ long __strnlen_user(const char __user *__s, long __n)
402 unsigned long res;
403 unsigned long __dummy;
405 __asm__ __volatile__(
406 "1:\t"
407 "mov.b @(%0,%3), %1\n\t"
408 "cmp/eq %4, %0\n\t"
409 "bt/s 2f\n\t"
410 " add #1, %0\n\t"
411 "tst %1, %1\n\t"
412 "bf 1b\n\t"
413 "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;
433 * strnlen_user: - Get the size of a string in user space.
434 * @s: The string to measure.
435 * @n: The maximum valid length
437 * Context: User context only. This function may sleep.
439 * Get the size of a NUL-terminated string in user space.
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.
445 static __inline__ long strnlen_user(const char __user *s, long n)
447 if (!__addr_ok(s))
448 return 0;
449 else
450 return __strnlen_user(s, n);
454 * strlen_user: - Get the size of a string in user space.
455 * @str: The string to measure.
457 * Context: User context only. This function may sleep.
459 * Get the size of a NUL-terminated string in user space.
461 * Returns the size of the string INCLUDING the terminating NUL.
462 * On exception, returns 0.
464 * If there is a limit on the length of a valid string, you may wish to
465 * consider using strnlen_user() instead.
467 #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
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.
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.
482 struct exception_table_entry
484 unsigned long insn, fixup;
487 extern int fixup_exception(struct pt_regs *regs);
489 #endif /* __ASM_SH_UACCESS_32_H */