blob: 847d12945998a99c3bca081aa89a56940b911621 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * User address space access functions.
3 *
4 * Copyright 1997 Andi Kleen <ak@muc.de>
5 * Copyright 1997 Linus Torvalds
6 * Copyright 2002 Andi Kleen <ak@suse.de>
7 */
Andi Kleen2ee60e172006-06-26 13:59:44 +02008#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/uaccess.h>
10
11/*
12 * Copy a null terminated string from userspace.
13 */
14
15#define __do_strncpy_from_user(dst,src,count,res) \
16do { \
17 long __d0, __d1, __d2; \
18 might_sleep(); \
Nick Pigginc10d38d2008-09-10 13:37:17 +020019 if (current->mm) \
20 might_lock_read(&current->mm->mmap_sem); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070021 __asm__ __volatile__( \
22 " testq %1,%1\n" \
23 " jz 2f\n" \
24 "0: lodsb\n" \
25 " stosb\n" \
26 " testb %%al,%%al\n" \
27 " jz 1f\n" \
28 " decq %1\n" \
29 " jnz 0b\n" \
30 "1: subq %1,%0\n" \
31 "2:\n" \
32 ".section .fixup,\"ax\"\n" \
33 "3: movq %5,%0\n" \
34 " jmp 2b\n" \
35 ".previous\n" \
H. Peter Anvin8da804f2008-02-04 16:47:57 +010036 _ASM_EXTABLE(0b,3b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 : "=r"(res), "=c"(count), "=&a" (__d0), "=&S" (__d1), \
38 "=&D" (__d2) \
39 : "i"(-EFAULT), "0"(count), "1"(count), "3"(src), "4"(dst) \
40 : "memory"); \
41} while (0)
42
43long
44__strncpy_from_user(char *dst, const char __user *src, long count)
45{
46 long res;
47 __do_strncpy_from_user(dst, src, count, res);
48 return res;
49}
Andi Kleen2ee60e172006-06-26 13:59:44 +020050EXPORT_SYMBOL(__strncpy_from_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
52long
53strncpy_from_user(char *dst, const char __user *src, long count)
54{
55 long res = -EFAULT;
56 if (access_ok(VERIFY_READ, src, 1))
Andi Kleen6bfa9bb2006-06-26 13:59:47 +020057 return __strncpy_from_user(dst, src, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 return res;
59}
Andi Kleen2ee60e172006-06-26 13:59:44 +020060EXPORT_SYMBOL(strncpy_from_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62/*
63 * Zero Userspace
64 */
65
66unsigned long __clear_user(void __user *addr, unsigned long size)
67{
68 long __d0;
69 might_sleep();
Nick Pigginc10d38d2008-09-10 13:37:17 +020070 if (current->mm)
71 might_lock_read(&current->mm->mmap_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* no memory constraint because it doesn't change any memory gcc knows
73 about */
74 asm volatile(
75 " testq %[size8],%[size8]\n"
76 " jz 4f\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"
82 " jz 2f\n"
83 "1: movb %b[zero],(%[dst])\n"
84 " incq %[dst]\n"
85 " decl %%ecx ; jnz 1b\n"
86 "2:\n"
87 ".section .fixup,\"ax\"\n"
88 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
89 " jmp 2b\n"
90 ".previous\n"
H. Peter Anvin8da804f2008-02-04 16:47:57 +010091 _ASM_EXTABLE(0b,3b)
92 _ASM_EXTABLE(1b,2b)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 : [size8] "=c"(size), [dst] "=&D" (__d0)
94 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
95 [zero] "r" (0UL), [eight] "r" (8UL));
96 return size;
97}
Andi Kleen2ee60e172006-06-26 13:59:44 +020098EXPORT_SYMBOL(__clear_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100unsigned long clear_user(void __user *to, unsigned long n)
101{
102 if (access_ok(VERIFY_WRITE, to, n))
103 return __clear_user(to, n);
104 return n;
105}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200106EXPORT_SYMBOL(clear_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * Return the size of a string (including the ending 0)
110 *
111 * Return 0 on exception, a value greater than N if too long
112 */
113
Jan Beulich5f1d1892006-01-11 22:46:48 +0100114long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 long res = 0;
117 char c;
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 while (1) {
120 if (res>n)
121 return n+1;
122 if (__get_user(c, s))
123 return 0;
124 if (!c)
125 return res+1;
126 res++;
127 s++;
128 }
129}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200130EXPORT_SYMBOL(__strnlen_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Jan Beulich5f1d1892006-01-11 22:46:48 +0100132long strnlen_user(const char __user *s, long n)
133{
134 if (!access_ok(VERIFY_READ, s, n))
135 return 0;
136 return __strnlen_user(s, n);
137}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200138EXPORT_SYMBOL(strnlen_user);
Jan Beulich5f1d1892006-01-11 22:46:48 +0100139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140long strlen_user(const char __user *s)
141{
142 long res = 0;
143 char c;
144
145 for (;;) {
146 if (get_user(c, s))
147 return 0;
148 if (!c)
149 return res+1;
150 res++;
151 s++;
152 }
153}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200154EXPORT_SYMBOL(strlen_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
157{
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);
160 }
161 return len;
162}
Andi Kleen2ee60e172006-06-26 13:59:44 +0200163EXPORT_SYMBOL(copy_in_user);
164
Vitaly Mayatskikh11295852008-07-02 15:48:21 +0200165/*
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.
169 */
170unsigned long
171copy_user_handle_tail(char *to, char *from, unsigned len, unsigned zerorest)
172{
173 char c;
174 unsigned zero_len;
175
176 for (; len; --len) {
177 if (__get_user_nocheck(c, from++, sizeof(char)))
178 break;
179 if (__put_user_nocheck(c, to++, sizeof(char)))
180 break;
181 }
182
183 for (c = 0, zero_len = len; zerorest && zero_len; --zero_len)
184 if (__put_user_nocheck(c, to++, sizeof(char)))
185 break;
186 return len;
187}