2 * User address space access functions.
4 * Copyright 1997 Andi Kleen <ak@muc.de>
5 * Copyright 1997 Linus Torvalds
6 * Copyright 2002 Andi Kleen <ak@suse.de>
8 #include <linux/module.h>
9 #include <asm/uaccess.h>
12 * Copy a null terminated string from userspace.
15 #define __do_strncpy_from_user(dst,src,count,res) \
17 long __d0, __d1, __d2; \
19 __asm__ __volatile__( \
24 " testb %%al,%%al\n" \
30 ".section .fixup,\"ax\"\n" \
34 ".section __ex_table,\"a\"\n" \
38 : "=r"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
40 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
45 __strncpy_from_user(char *dst, const char __user *src, long count)
48 __do_strncpy_from_user(dst, src, count, res);
51 EXPORT_SYMBOL(__strncpy_from_user);
54 strncpy_from_user(char *dst, const char __user *src, long count)
57 if (access_ok(VERIFY_READ, src, 1))
58 return __strncpy_from_user(dst, src, count);
61 EXPORT_SYMBOL(strncpy_from_user);
67 unsigned long __clear_user(void __user *addr, unsigned long size)
71 /* no memory constraint because it doesn't change any memory gcc knows
74 " testq %[size8],%[size8]\n"
76 "0: movq %[zero],(%[dst])\n"
77 " addq %[eight],%[dst]\n"
78 " decl %%ecx ; jnz 0b\n"
79 "4: movq %[size1],%%rcx\n"
80 " testl %%ecx,%%ecx\n"
82 "1: movb %b[zero],(%[dst])\n"
84 " decl %%ecx ; jnz 1b\n"
86 ".section .fixup,\"ax\"\n"
87 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
90 ".section __ex_table,\"a\"\n"
95 : [size8] "=c"(size), [dst] "=&D" (__d0)
96 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
97 [zero] "r" (0UL), [eight] "r" (8UL));
100 EXPORT_SYMBOL(__clear_user);
102 unsigned long clear_user(void __user *to, unsigned long n)
104 if (access_ok(VERIFY_WRITE, to, n))
105 return __clear_user(to, n);
108 EXPORT_SYMBOL(clear_user);
111 * Return the size of a string (including the ending 0)
113 * Return 0 on exception, a value greater than N if too long
116 long __strnlen_user(const char __user *s, long n)
124 if (__get_user(c, s))
132 EXPORT_SYMBOL(__strnlen_user);
134 long strnlen_user(const char __user *s, long n)
136 if (!access_ok(VERIFY_READ, s, n))
138 return __strnlen_user(s, n);
140 EXPORT_SYMBOL(strnlen_user);
142 long strlen_user(const char __user *s)
156 EXPORT_SYMBOL(strlen_user);
158 unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
160 if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
161 return copy_user_generic((__force void *)to, (__force void *)from, len);
165 EXPORT_SYMBOL(copy_in_user);