]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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) \ | |
15 | do { \ | |
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 | ||
43 | long | |
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 | ||
51 | long | |
52 | strncpy_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 | ||
64 | unsigned 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 | ||
99 | unsigned 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 | ||
112 | long strnlen_user(const char __user *s, long n) | |
113 | { | |
114 | long res = 0; | |
115 | char c; | |
116 | ||
117 | if (!access_ok(VERIFY_READ, s, n)) | |
118 | return 0; | |
119 | ||
120 | while (1) { | |
121 | if (res>n) | |
122 | return n+1; | |
123 | if (__get_user(c, s)) | |
124 | return 0; | |
125 | if (!c) | |
126 | return res+1; | |
127 | res++; | |
128 | s++; | |
129 | } | |
130 | } | |
131 | ||
132 | long strlen_user(const char __user *s) | |
133 | { | |
134 | long res = 0; | |
135 | char c; | |
136 | ||
137 | for (;;) { | |
138 | if (get_user(c, s)) | |
139 | return 0; | |
140 | if (!c) | |
141 | return res+1; | |
142 | res++; | |
143 | s++; | |
144 | } | |
145 | } | |
146 | ||
147 | unsigned long copy_in_user(void __user *to, const void __user *from, unsigned len) | |
148 | { | |
149 | if (access_ok(VERIFY_WRITE, to, len) && access_ok(VERIFY_READ, from, len)) { | |
150 | return copy_user_generic((__force void *)to, (__force void *)from, len); | |
151 | } | |
152 | return len; | |
153 | } |