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" \
35 : "=r"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
37 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
42 __strncpy_from_user(char *dst, const char __user *src, long count)
45 __do_strncpy_from_user(dst, src, count, res);
48 EXPORT_SYMBOL(__strncpy_from_user);
51 strncpy_from_user(char *dst, const char __user *src, long count)
54 if (access_ok(VERIFY_READ, src, 1))
55 return __strncpy_from_user(dst, src, count);
58 EXPORT_SYMBOL(strncpy_from_user);
64 unsigned long __clear_user(void __user *addr, unsigned long size)
68 /* no memory constraint because it doesn't change any memory gcc knows
71 " testq %[size8],%[size8]\n"
73 "0: movq %[zero],(%[dst])\n"
74 " addq %[eight],%[dst]\n"
75 " decl %%ecx ; jnz 0b\n"
76 "4: movq %[size1],%%rcx\n"
77 " testl %%ecx,%%ecx\n"
79 "1: movb %b[zero],(%[dst])\n"
81 " decl %%ecx ; jnz 1b\n"
83 ".section .fixup,\"ax\"\n"
84 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
89 : [size8] "=c"(size), [dst] "=&D" (__d0)
90 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
91 [zero] "r" (0UL), [eight] "r" (8UL));
94 EXPORT_SYMBOL(__clear_user);
96 unsigned long clear_user(void __user *to, unsigned long n)
98 if (access_ok(VERIFY_WRITE, to, n))
99 return __clear_user(to, n);
102 EXPORT_SYMBOL(clear_user);
105 * Return the size of a string (including the ending 0)
107 * Return 0 on exception, a value greater than N if too long
110 long __strnlen_user(const char __user *s, long n)
118 if (__get_user(c, s))
126 EXPORT_SYMBOL(__strnlen_user);
128 long strnlen_user(const char __user *s, long n)
130 if (!access_ok(VERIFY_READ, s, n))
132 return __strnlen_user(s, n);
134 EXPORT_SYMBOL(strnlen_user);
136 long strlen_user(const char __user *s)
150 EXPORT_SYMBOL(strlen_user);
152 unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
154 if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
155 return copy_user_generic((__force void *)to, (__force void *)from, len);
159 EXPORT_SYMBOL(copy_in_user);
162 * Try to copy last bytes and clear the rest if needed.
163 * Since protection fault in copy_from/to_user is not a normal situation,
164 * it is not necessary to optimize tail handling.
167 copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest)
173 if (__get_user_nocheck(c, from++, sizeof(char)))
175 if (__put_user_nocheck(c, to++, sizeof(char)))
179 for (c = 0, zero_len = len; zerorest && zero_len; --zero_len)
180 if (__put_user_nocheck(c, to++, sizeof(char)))