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; \
20 might_lock_read(¤t->mm->mmap_sem); \
21 __asm__ __volatile__( \
26 " testb %%al,%%al\n" \
32 ".section .fixup,\"ax\"\n" \
37 : "=r"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
39 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
44 __strncpy_from_user(char *dst
, const char __user
*src
, long count
)
47 __do_strncpy_from_user(dst
, src
, count
, res
);
50 EXPORT_SYMBOL(__strncpy_from_user
);
53 strncpy_from_user(char *dst
, const char __user
*src
, long count
)
56 if (access_ok(VERIFY_READ
, src
, 1))
57 return __strncpy_from_user(dst
, src
, count
);
60 EXPORT_SYMBOL(strncpy_from_user
);
66 unsigned long __clear_user(void __user
*addr
, unsigned long size
)
71 might_lock_read(¤t
->mm
->mmap_sem
);
72 /* no memory constraint because it doesn't change any memory gcc knows
75 " testq %[size8],%[size8]\n"
77 "0: movq %[zero],(%[dst])\n"
78 " addq %[eight],%[dst]\n"
79 " decl %%ecx ; jnz 0b\n"
80 "4: movq %[size1],%%rcx\n"
81 " testl %%ecx,%%ecx\n"
83 "1: movb %b[zero],(%[dst])\n"
85 " decl %%ecx ; jnz 1b\n"
87 ".section .fixup,\"ax\"\n"
88 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
93 : [size8
] "=c"(size
), [dst
] "=&D" (__d0
)
94 : [size1
] "r"(size
& 7), "[size8]" (size
/ 8), "[dst]"(addr
),
95 [zero
] "r" (0UL), [eight
] "r" (8UL));
98 EXPORT_SYMBOL(__clear_user
);
100 unsigned long clear_user(void __user
*to
, unsigned long n
)
102 if (access_ok(VERIFY_WRITE
, to
, n
))
103 return __clear_user(to
, n
);
106 EXPORT_SYMBOL(clear_user
);
109 * Return the size of a string (including the ending 0)
111 * Return 0 on exception, a value greater than N if too long
114 long __strnlen_user(const char __user
*s
, long n
)
122 if (__get_user(c
, s
))
130 EXPORT_SYMBOL(__strnlen_user
);
132 long strnlen_user(const char __user
*s
, long n
)
134 if (!access_ok(VERIFY_READ
, s
, n
))
136 return __strnlen_user(s
, n
);
138 EXPORT_SYMBOL(strnlen_user
);
140 long strlen_user(const char __user
*s
)
154 EXPORT_SYMBOL(strlen_user
);
156 unsigned long copy_in_user(void __user
*to
, const void __user
*from
, unsigned len
)
158 if (access_ok(VERIFY_WRITE
, to
, len
) && access_ok(VERIFY_READ
, from
, len
)) {
159 return copy_user_generic((__force
void *)to
, (__force
void *)from
, len
);
163 EXPORT_SYMBOL(copy_in_user
);
166 * Try to copy last bytes and clear the rest if needed.
167 * Since protection fault in copy_from/to_user is not a normal situation,
168 * it is not necessary to optimize tail handling.
171 copy_user_handle_tail(char *to
, char *from
, unsigned len
, unsigned zerorest
)
177 if (__get_user_nocheck(c
, from
++, sizeof(char)))
179 if (__put_user_nocheck(c
, to
++, sizeof(char)))
183 for (c
= 0, zero_len
= len
; zerorest
&& zero_len
; --zero_len
)
184 if (__put_user_nocheck(c
, to
++, sizeof(char)))