blob: 9bc2c295818e3c2d2faa97ecee0e17a336843b48 [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 */
8#include <asm/uaccess.h>
9
10/*
11 * Copy a null terminated string from userspace.
12 */
13
14#define __do_strncpy_from_user(dst,src,count,res) \
15do { \
16 long __d0, __d1, __d2; \
17 might_sleep(); \
18 __asm__ __volatile__( \
19 " testq %1,%1\n" \
20 " jz 2f\n" \
21 "0: lodsb\n" \
22 " stosb\n" \
23 " testb %%al,%%al\n" \
24 " jz 1f\n" \
25 " decq %1\n" \
26 " jnz 0b\n" \
27 "1: subq %1,%0\n" \
28 "2:\n" \
29 ".section .fixup,\"ax\"\n" \
30 "3: movq %5,%0\n" \
31 " jmp 2b\n" \
32 ".previous\n" \
33 ".section __ex_table,\"a\"\n" \
34 " .align 8\n" \
35 " .quad 0b,3b\n" \
36 ".previous" \
37 : "=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}
50
51long
52strncpy_from_user(char *dst, const char __user *src, long count)
53{
54 long res = -EFAULT;
55 if (access_ok(VERIFY_READ, src, 1))
56 __do_strncpy_from_user(dst, src, count, res);
57 return res;
58}
59
60/*
61 * Zero Userspace
62 */
63
64unsigned long __clear_user(void __user *addr, unsigned long size)
65{
66 long __d0;
67 might_sleep();
68 /* no memory constraint because it doesn't change any memory gcc knows
69 about */
70 asm volatile(
71 " testq %[size8],%[size8]\n"
72 " jz 4f\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"
78 " jz 2f\n"
79 "1: movb %b[zero],(%[dst])\n"
80 " incq %[dst]\n"
81 " decl %%ecx ; jnz 1b\n"
82 "2:\n"
83 ".section .fixup,\"ax\"\n"
84 "3: lea 0(%[size1],%[size8],8),%[size8]\n"
85 " jmp 2b\n"
86 ".previous\n"
87 ".section __ex_table,\"a\"\n"
88 " .align 8\n"
89 " .quad 0b,3b\n"
90 " .quad 1b,2b\n"
91 ".previous"
92 : [size8] "=c"(size), [dst] "=&D" (__d0)
93 : [size1] "r"(size & 7), "[size8]" (size / 8), "[dst]"(addr),
94 [zero] "r" (0UL), [eight] "r" (8UL));
95 return size;
96}
97
98
99unsigned long clear_user(void __user *to, unsigned long n)
100{
101 if (access_ok(VERIFY_WRITE, to, n))
102 return __clear_user(to, n);
103 return n;
104}
105
106/*
107 * Return the size of a string (including the ending 0)
108 *
109 * Return 0 on exception, a value greater than N if too long
110 */
111
Jan Beulich5f1d1892006-01-11 22:46:48 +0100112long __strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 long res = 0;
115 char c;
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 while (1) {
118 if (res>n)
119 return n+1;
120 if (__get_user(c, s))
121 return 0;
122 if (!c)
123 return res+1;
124 res++;
125 s++;
126 }
127}
128
Jan Beulich5f1d1892006-01-11 22:46:48 +0100129long strnlen_user(const char __user *s, long n)
130{
131 if (!access_ok(VERIFY_READ, s, n))
132 return 0;
133 return __strnlen_user(s, n);
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136long strlen_user(const char __user *s)
137{
138 long res = 0;
139 char c;
140
141 for (;;) {
142 if (get_user(c, s))
143 return 0;
144 if (!c)
145 return res+1;
146 res++;
147 s++;
148 }
149}
150
151unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len)
152{
153 if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) {
154 return copy_user_generic((__force void *)to, (__force void *)from, len);
155 }
156 return len;
157}