Pull ngam-maule-steiner into release branch
[sfrench/cifs-2.6.git] / include / asm-s390 / uaccess.h
1 /*
2  *  include/asm-s390/uaccess.h
3  *
4  *  S390 version
5  *    Copyright (C) 1999,2000 IBM Deutschland Entwicklung GmbH, IBM Corporation
6  *    Author(s): Hartmut Penner (hp@de.ibm.com),
7  *               Martin Schwidefsky (schwidefsky@de.ibm.com)
8  *
9  *  Derived from "include/asm-i386/uaccess.h"
10  */
11 #ifndef __S390_UACCESS_H
12 #define __S390_UACCESS_H
13
14 /*
15  * User space memory access functions
16  */
17 #include <linux/sched.h>
18 #include <linux/errno.h>
19
20 #define VERIFY_READ     0
21 #define VERIFY_WRITE    1
22
23
24 /*
25  * The fs value determines whether argument validity checking should be
26  * performed or not.  If get_fs() == USER_DS, checking is performed, with
27  * get_fs() == KERNEL_DS, checking is bypassed.
28  *
29  * For historical reasons, these macros are grossly misnamed.
30  */
31
32 #define MAKE_MM_SEG(a)  ((mm_segment_t) { (a) })
33
34
35 #define KERNEL_DS       MAKE_MM_SEG(0)
36 #define USER_DS         MAKE_MM_SEG(1)
37
38 #define get_ds()        (KERNEL_DS)
39 #define get_fs()        (current->thread.mm_segment)
40
41 #ifdef __s390x__
42 #define set_fs(x) \
43 ({                                                                      \
44         unsigned long __pto;                                            \
45         current->thread.mm_segment = (x);                               \
46         __pto = current->thread.mm_segment.ar4 ?                        \
47                 S390_lowcore.user_asce : S390_lowcore.kernel_asce;      \
48         asm volatile ("lctlg 7,7,%0" : : "m" (__pto) );                 \
49 })
50 #else
51 #define set_fs(x) \
52 ({                                                                      \
53         unsigned long __pto;                                            \
54         current->thread.mm_segment = (x);                               \
55         __pto = current->thread.mm_segment.ar4 ?                        \
56                 S390_lowcore.user_asce : S390_lowcore.kernel_asce;      \
57         asm volatile ("lctl  7,7,%0" : : "m" (__pto) );                 \
58 })
59 #endif
60
61 #define segment_eq(a,b) ((a).ar4 == (b).ar4)
62
63
64 #define __access_ok(addr,size) (1)
65
66 #define access_ok(type,addr,size) __access_ok(addr,size)
67
68 /* this function will go away soon - use access_ok() instead */
69 extern inline int __deprecated verify_area(int type, const void __user *addr,
70                                                 unsigned long size)
71 {
72         return access_ok(type, addr, size) ? 0 : -EFAULT;
73 }
74
75 /*
76  * The exception table consists of pairs of addresses: the first is the
77  * address of an instruction that is allowed to fault, and the second is
78  * the address at which the program should continue.  No registers are
79  * modified, so it is entirely up to the continuation code to figure out
80  * what to do.
81  *
82  * All the routines below use bits of fixup code that are out of line
83  * with the main instruction path.  This means when everything is well,
84  * we don't even have to jump over them.  Further, they do not intrude
85  * on our cache or tlb entries.
86  */
87
88 struct exception_table_entry
89 {
90         unsigned long insn, fixup;
91 };
92
93 #ifndef __s390x__
94 #define __uaccess_fixup \
95         ".section .fixup,\"ax\"\n"      \
96         "2: lhi    %0,%4\n"             \
97         "   bras   1,3f\n"              \
98         "   .long  1b\n"                \
99         "3: l      1,0(1)\n"            \
100         "   br     1\n"                 \
101         ".previous\n"                   \
102         ".section __ex_table,\"a\"\n"   \
103         "   .align 4\n"                 \
104         "   .long  0b,2b\n"             \
105         ".previous"
106 #define __uaccess_clobber "cc", "1"
107 #else /* __s390x__ */
108 #define __uaccess_fixup \
109         ".section .fixup,\"ax\"\n"      \
110         "2: lghi   %0,%4\n"             \
111         "   jg     1b\n"                \
112         ".previous\n"                   \
113         ".section __ex_table,\"a\"\n"   \
114         "   .align 8\n"                 \
115         "   .quad  0b,2b\n"             \
116         ".previous"
117 #define __uaccess_clobber "cc"
118 #endif /* __s390x__ */
119
120 /*
121  * These are the main single-value transfer routines.  They automatically
122  * use the right size if we just have the right pointer type.
123  */
124 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
125 #define __put_user_asm(x, ptr, err) \
126 ({                                                              \
127         err = 0;                                                \
128         asm volatile(                                           \
129                 "0: mvcs  0(%1,%2),%3,%0\n"                     \
130                 "1:\n"                                          \
131                 __uaccess_fixup                                 \
132                 : "+&d" (err)                                   \
133                 : "d" (sizeof(*(ptr))), "a" (ptr), "Q" (x),     \
134                   "K" (-EFAULT)                                 \
135                 : __uaccess_clobber );                          \
136 })
137 #else
138 #define __put_user_asm(x, ptr, err) \
139 ({                                                              \
140         err = 0;                                                \
141         asm volatile(                                           \
142                 "0: mvcs  0(%1,%2),0(%3),%0\n"                  \
143                 "1:\n"                                          \
144                 __uaccess_fixup                                 \
145                 : "+&d" (err)                                   \
146                 : "d" (sizeof(*(ptr))), "a" (ptr), "a" (&(x)),  \
147                   "K" (-EFAULT), "m" (x)                        \
148                 : __uaccess_clobber );                          \
149 })
150 #endif
151
152 #define __put_user(x, ptr) \
153 ({                                                              \
154         __typeof__(*(ptr)) __x = (x);                           \
155         int __pu_err;                                           \
156         __chk_user_ptr(ptr);                                    \
157         switch (sizeof (*(ptr))) {                              \
158         case 1:                                                 \
159         case 2:                                                 \
160         case 4:                                                 \
161         case 8:                                                 \
162                 __put_user_asm(__x, ptr, __pu_err);             \
163                 break;                                          \
164         default:                                                \
165                 __put_user_bad();                               \
166                 break;                                          \
167          }                                                      \
168         __pu_err;                                               \
169 })
170
171 #define put_user(x, ptr)                                        \
172 ({                                                              \
173         might_sleep();                                          \
174         __put_user(x, ptr);                                     \
175 })
176
177
178 extern int __put_user_bad(void) __attribute__((noreturn));
179
180 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
181 #define __get_user_asm(x, ptr, err) \
182 ({                                                              \
183         err = 0;                                                \
184         asm volatile (                                          \
185                 "0: mvcp  %O1(%2,%R1),0(%3),%0\n"               \
186                 "1:\n"                                          \
187                 __uaccess_fixup                                 \
188                 : "+&d" (err), "=Q" (x)                         \
189                 : "d" (sizeof(*(ptr))), "a" (ptr),              \
190                   "K" (-EFAULT)                                 \
191                 : __uaccess_clobber );                          \
192 })
193 #else
194 #define __get_user_asm(x, ptr, err) \
195 ({                                                              \
196         err = 0;                                                \
197         asm volatile (                                          \
198                 "0: mvcp  0(%2,%5),0(%3),%0\n"                  \
199                 "1:\n"                                          \
200                 __uaccess_fixup                                 \
201                 : "+&d" (err), "=m" (x)                         \
202                 : "d" (sizeof(*(ptr))), "a" (ptr),              \
203                   "K" (-EFAULT), "a" (&(x))                     \
204                 : __uaccess_clobber );                          \
205 })
206 #endif
207
208 #define __get_user(x, ptr)                                      \
209 ({                                                              \
210         __typeof__(*(ptr)) __x;                                 \
211         int __gu_err;                                           \
212         __chk_user_ptr(ptr);                                    \
213         switch (sizeof(*(ptr))) {                               \
214         case 1:                                                 \
215         case 2:                                                 \
216         case 4:                                                 \
217         case 8:                                                 \
218                 __get_user_asm(__x, ptr, __gu_err);             \
219                 break;                                          \
220         default:                                                \
221                 __get_user_bad();                               \
222                 break;                                          \
223         }                                                       \
224         (x) = __x;                                              \
225         __gu_err;                                               \
226 })
227
228 #define get_user(x, ptr)                                        \
229 ({                                                              \
230         might_sleep();                                          \
231         __get_user(x, ptr);                                     \
232 })
233
234 extern int __get_user_bad(void) __attribute__((noreturn));
235
236 #define __put_user_unaligned __put_user
237 #define __get_user_unaligned __get_user
238
239 extern long __copy_to_user_asm(const void *from, long n, void __user *to);
240
241 /**
242  * __copy_to_user: - Copy a block of data into user space, with less checking.
243  * @to:   Destination address, in user space.
244  * @from: Source address, in kernel space.
245  * @n:    Number of bytes to copy.
246  *
247  * Context: User context only.  This function may sleep.
248  *
249  * Copy data from kernel space to user space.  Caller must check
250  * the specified block with access_ok() before calling this function.
251  *
252  * Returns number of bytes that could not be copied.
253  * On success, this will be zero.
254  */
255 static inline unsigned long
256 __copy_to_user(void __user *to, const void *from, unsigned long n)
257 {
258         return __copy_to_user_asm(from, n, to);
259 }
260
261 #define __copy_to_user_inatomic __copy_to_user
262 #define __copy_from_user_inatomic __copy_from_user
263
264 /**
265  * copy_to_user: - Copy a block of data into user space.
266  * @to:   Destination address, in user space.
267  * @from: Source address, in kernel space.
268  * @n:    Number of bytes to copy.
269  *
270  * Context: User context only.  This function may sleep.
271  *
272  * Copy data from kernel space to user space.
273  *
274  * Returns number of bytes that could not be copied.
275  * On success, this will be zero.
276  */
277 static inline unsigned long
278 copy_to_user(void __user *to, const void *from, unsigned long n)
279 {
280         might_sleep();
281         if (access_ok(VERIFY_WRITE, to, n))
282                 n = __copy_to_user(to, from, n);
283         return n;
284 }
285
286 extern long __copy_from_user_asm(void *to, long n, const void __user *from);
287
288 /**
289  * __copy_from_user: - Copy a block of data from user space, with less checking.
290  * @to:   Destination address, in kernel space.
291  * @from: Source address, in user space.
292  * @n:    Number of bytes to copy.
293  *
294  * Context: User context only.  This function may sleep.
295  *
296  * Copy data from user space to kernel space.  Caller must check
297  * the specified block with access_ok() before calling this function.
298  *
299  * Returns number of bytes that could not be copied.
300  * On success, this will be zero.
301  *
302  * If some data could not be copied, this function will pad the copied
303  * data to the requested size using zero bytes.
304  */
305 static inline unsigned long
306 __copy_from_user(void *to, const void __user *from, unsigned long n)
307 {
308         return __copy_from_user_asm(to, n, from);
309 }
310
311 /**
312  * copy_from_user: - Copy a block of data from user space.
313  * @to:   Destination address, in kernel space.
314  * @from: Source address, in user space.
315  * @n:    Number of bytes to copy.
316  *
317  * Context: User context only.  This function may sleep.
318  *
319  * Copy data from user space to kernel space.
320  *
321  * Returns number of bytes that could not be copied.
322  * On success, this will be zero.
323  *
324  * If some data could not be copied, this function will pad the copied
325  * data to the requested size using zero bytes.
326  */
327 static inline unsigned long
328 copy_from_user(void *to, const void __user *from, unsigned long n)
329 {
330         might_sleep();
331         if (access_ok(VERIFY_READ, from, n))
332                 n = __copy_from_user(to, from, n);
333         else
334                 memset(to, 0, n);
335         return n;
336 }
337
338 extern unsigned long __copy_in_user_asm(const void __user *from, long n,
339                                                         void __user *to);
340
341 static inline unsigned long
342 __copy_in_user(void __user *to, const void __user *from, unsigned long n)
343 {
344         return __copy_in_user_asm(from, n, to);
345 }
346
347 static inline unsigned long
348 copy_in_user(void __user *to, const void __user *from, unsigned long n)
349 {
350         might_sleep();
351         if (__access_ok(from,n) && __access_ok(to,n))
352                 n = __copy_in_user_asm(from, n, to);
353         return n;
354 }
355
356 /*
357  * Copy a null terminated string from userspace.
358  */
359 extern long __strncpy_from_user_asm(long count, char *dst,
360                                         const char __user *src);
361
362 static inline long
363 strncpy_from_user(char *dst, const char __user *src, long count)
364 {
365         long res = -EFAULT;
366         might_sleep();
367         if (access_ok(VERIFY_READ, src, 1))
368                 res = __strncpy_from_user_asm(count, dst, src);
369         return res;
370 }
371
372
373 extern long __strnlen_user_asm(long count, const char __user *src);
374
375 static inline unsigned long
376 strnlen_user(const char __user * src, unsigned long n)
377 {
378         might_sleep();
379         return __strnlen_user_asm(n, src);
380 }
381
382 /**
383  * strlen_user: - Get the size of a string in user space.
384  * @str: The string to measure.
385  *
386  * Context: User context only.  This function may sleep.
387  *
388  * Get the size of a NUL-terminated string in user space.
389  *
390  * Returns the size of the string INCLUDING the terminating NUL.
391  * On exception, returns 0.
392  *
393  * If there is a limit on the length of a valid string, you may wish to
394  * consider using strnlen_user() instead.
395  */
396 #define strlen_user(str) strnlen_user(str, ~0UL)
397
398 /*
399  * Zero Userspace
400  */
401
402 extern long __clear_user_asm(void __user *to, long n);
403
404 static inline unsigned long
405 __clear_user(void __user *to, unsigned long n)
406 {
407         return __clear_user_asm(to, n);
408 }
409
410 static inline unsigned long
411 clear_user(void __user *to, unsigned long n)
412 {
413         might_sleep();
414         if (access_ok(VERIFY_WRITE, to, n))
415                 n = __clear_user_asm(to, n);
416         return n;
417 }
418
419 #endif /* __S390_UACCESS_H */