enable kexec
[linux-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 extern inline int verify_area(int type, const void __user *addr,
69                                                 unsigned long size)
70 {
71         return access_ok(type, addr, size) ? 0 : -EFAULT;
72 }
73
74 /*
75  * The exception table consists of pairs of addresses: the first is the
76  * address of an instruction that is allowed to fault, and the second is
77  * the address at which the program should continue.  No registers are
78  * modified, so it is entirely up to the continuation code to figure out
79  * what to do.
80  *
81  * All the routines below use bits of fixup code that are out of line
82  * with the main instruction path.  This means when everything is well,
83  * we don't even have to jump over them.  Further, they do not intrude
84  * on our cache or tlb entries.
85  */
86
87 struct exception_table_entry
88 {
89         unsigned long insn, fixup;
90 };
91
92 #ifndef __s390x__
93 #define __uaccess_fixup \
94         ".section .fixup,\"ax\"\n"      \
95         "2: lhi    %0,%4\n"             \
96         "   bras   1,3f\n"              \
97         "   .long  1b\n"                \
98         "3: l      1,0(1)\n"            \
99         "   br     1\n"                 \
100         ".previous\n"                   \
101         ".section __ex_table,\"a\"\n"   \
102         "   .align 4\n"                 \
103         "   .long  0b,2b\n"             \
104         ".previous"
105 #define __uaccess_clobber "cc", "1"
106 #else /* __s390x__ */
107 #define __uaccess_fixup \
108         ".section .fixup,\"ax\"\n"      \
109         "2: lghi   %0,%4\n"             \
110         "   jg     1b\n"                \
111         ".previous\n"                   \
112         ".section __ex_table,\"a\"\n"   \
113         "   .align 8\n"                 \
114         "   .quad  0b,2b\n"             \
115         ".previous"
116 #define __uaccess_clobber "cc"
117 #endif /* __s390x__ */
118
119 /*
120  * These are the main single-value transfer routines.  They automatically
121  * use the right size if we just have the right pointer type.
122  */
123 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
124 #define __put_user_asm(x, ptr, err) \
125 ({                                                              \
126         err = 0;                                                \
127         asm volatile(                                           \
128                 "0: mvcs  0(%1,%2),%3,%0\n"                     \
129                 "1:\n"                                          \
130                 __uaccess_fixup                                 \
131                 : "+&d" (err)                                   \
132                 : "d" (sizeof(*(ptr))), "a" (ptr), "Q" (x),     \
133                   "K" (-EFAULT)                                 \
134                 : __uaccess_clobber );                          \
135 })
136 #else
137 #define __put_user_asm(x, ptr, err) \
138 ({                                                              \
139         err = 0;                                                \
140         asm volatile(                                           \
141                 "0: mvcs  0(%1,%2),0(%3),%0\n"                  \
142                 "1:\n"                                          \
143                 __uaccess_fixup                                 \
144                 : "+&d" (err)                                   \
145                 : "d" (sizeof(*(ptr))), "a" (ptr), "a" (&(x)),  \
146                   "K" (-EFAULT), "m" (x)                        \
147                 : __uaccess_clobber );                          \
148 })
149 #endif
150
151 #ifndef __CHECKER__
152 #define __put_user(x, ptr) \
153 ({                                                              \
154         __typeof__(*(ptr)) __x = (x);                           \
155         int __pu_err;                                           \
156         switch (sizeof (*(ptr))) {                              \
157         case 1:                                                 \
158         case 2:                                                 \
159         case 4:                                                 \
160         case 8:                                                 \
161                 __put_user_asm(__x, ptr, __pu_err);             \
162                 break;                                          \
163         default:                                                \
164                 __pu_err = __put_user_bad();                    \
165                 break;                                          \
166          }                                                      \
167         __pu_err;                                               \
168 })
169 #else
170 #define __put_user(x, ptr)                      \
171 ({                                              \
172         void __user *p;                         \
173         p = (ptr);                              \
174         0;                                      \
175 })
176 #endif
177
178 #define put_user(x, ptr)                                        \
179 ({                                                              \
180         might_sleep();                                          \
181         __put_user(x, ptr);                                     \
182 })
183
184
185 extern int __put_user_bad(void);
186
187 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
188 #define __get_user_asm(x, ptr, err) \
189 ({                                                              \
190         err = 0;                                                \
191         asm volatile (                                          \
192                 "0: mvcp  %O1(%2,%R1),0(%3),%0\n"               \
193                 "1:\n"                                          \
194                 __uaccess_fixup                                 \
195                 : "+&d" (err), "=Q" (x)                         \
196                 : "d" (sizeof(*(ptr))), "a" (ptr),              \
197                   "K" (-EFAULT)                                 \
198                 : __uaccess_clobber );                          \
199 })
200 #else
201 #define __get_user_asm(x, ptr, err) \
202 ({                                                              \
203         err = 0;                                                \
204         asm volatile (                                          \
205                 "0: mvcp  0(%2,%5),0(%3),%0\n"                  \
206                 "1:\n"                                          \
207                 __uaccess_fixup                                 \
208                 : "+&d" (err), "=m" (x)                         \
209                 : "d" (sizeof(*(ptr))), "a" (ptr),              \
210                   "K" (-EFAULT), "a" (&(x))                     \
211                 : __uaccess_clobber );                          \
212 })
213 #endif
214
215 #ifndef __CHECKER__
216 #define __get_user(x, ptr)                                      \
217 ({                                                              \
218         __typeof__(*(ptr)) __x;                                 \
219         int __gu_err;                                           \
220         switch (sizeof(*(ptr))) {                               \
221         case 1:                                                 \
222         case 2:                                                 \
223         case 4:                                                 \
224         case 8:                                                 \
225                 __get_user_asm(__x, ptr, __gu_err);             \
226                 break;                                          \
227         default:                                                \
228                 __x = 0;                                        \
229                 __gu_err = __get_user_bad();                    \
230                 break;                                          \
231         }                                                       \
232         (x) = __x;                                              \
233         __gu_err;                                               \
234 })
235 #else
236 #define __get_user(x, ptr)                      \
237 ({                                              \
238         void __user *p;                         \
239         p = (ptr);                              \
240         0;                                      \
241 })
242 #endif
243
244
245 #define get_user(x, ptr)                                        \
246 ({                                                              \
247         might_sleep();                                          \
248         __get_user(x, ptr);                                     \
249 })
250
251 extern int __get_user_bad(void);
252
253 extern long __copy_to_user_asm(const void *from, long n, void __user *to);
254
255 /**
256  * __copy_to_user: - Copy a block of data into user space, with less checking.
257  * @to:   Destination address, in user space.
258  * @from: Source address, in kernel space.
259  * @n:    Number of bytes to copy.
260  *
261  * Context: User context only.  This function may sleep.
262  *
263  * Copy data from kernel space to user space.  Caller must check
264  * the specified block with access_ok() before calling this function.
265  *
266  * Returns number of bytes that could not be copied.
267  * On success, this will be zero.
268  */
269 static inline unsigned long
270 __copy_to_user(void __user *to, const void *from, unsigned long n)
271 {
272         return __copy_to_user_asm(from, n, to);
273 }
274
275 #define __copy_to_user_inatomic __copy_to_user
276 #define __copy_from_user_inatomic __copy_from_user
277
278 /**
279  * copy_to_user: - Copy a block of data into user space.
280  * @to:   Destination address, in user space.
281  * @from: Source address, in kernel space.
282  * @n:    Number of bytes to copy.
283  *
284  * Context: User context only.  This function may sleep.
285  *
286  * Copy data from kernel space to user space.
287  *
288  * Returns number of bytes that could not be copied.
289  * On success, this will be zero.
290  */
291 static inline unsigned long
292 copy_to_user(void __user *to, const void *from, unsigned long n)
293 {
294         might_sleep();
295         if (access_ok(VERIFY_WRITE, to, n))
296                 n = __copy_to_user(to, from, n);
297         return n;
298 }
299
300 extern long __copy_from_user_asm(void *to, long n, const void __user *from);
301
302 /**
303  * __copy_from_user: - Copy a block of data from user space, with less checking.
304  * @to:   Destination address, in kernel space.
305  * @from: Source address, in user space.
306  * @n:    Number of bytes to copy.
307  *
308  * Context: User context only.  This function may sleep.
309  *
310  * Copy data from user space to kernel space.  Caller must check
311  * the specified block with access_ok() before calling this function.
312  *
313  * Returns number of bytes that could not be copied.
314  * On success, this will be zero.
315  *
316  * If some data could not be copied, this function will pad the copied
317  * data to the requested size using zero bytes.
318  */
319 static inline unsigned long
320 __copy_from_user(void *to, const void __user *from, unsigned long n)
321 {
322         return __copy_from_user_asm(to, n, from);
323 }
324
325 /**
326  * copy_from_user: - Copy a block of data from user space.
327  * @to:   Destination address, in kernel space.
328  * @from: Source address, in user space.
329  * @n:    Number of bytes to copy.
330  *
331  * Context: User context only.  This function may sleep.
332  *
333  * Copy data from user space to kernel space.
334  *
335  * Returns number of bytes that could not be copied.
336  * On success, this will be zero.
337  *
338  * If some data could not be copied, this function will pad the copied
339  * data to the requested size using zero bytes.
340  */
341 static inline unsigned long
342 copy_from_user(void *to, const void __user *from, unsigned long n)
343 {
344         might_sleep();
345         if (access_ok(VERIFY_READ, from, n))
346                 n = __copy_from_user(to, from, n);
347         else
348                 memset(to, 0, n);
349         return n;
350 }
351
352 extern unsigned long __copy_in_user_asm(const void __user *from, long n,
353                                                         void __user *to);
354
355 static inline unsigned long
356 __copy_in_user(void __user *to, const void __user *from, unsigned long n)
357 {
358         return __copy_in_user_asm(from, n, to);
359 }
360
361 static inline unsigned long
362 copy_in_user(void __user *to, const void __user *from, unsigned long n)
363 {
364         might_sleep();
365         if (__access_ok(from,n) && __access_ok(to,n))
366                 n = __copy_in_user_asm(from, n, to);
367         return n;
368 }
369
370 /*
371  * Copy a null terminated string from userspace.
372  */
373 extern long __strncpy_from_user_asm(long count, char *dst,
374                                         const char __user *src);
375
376 static inline long
377 strncpy_from_user(char *dst, const char __user *src, long count)
378 {
379         long res = -EFAULT;
380         might_sleep();
381         if (access_ok(VERIFY_READ, src, 1))
382                 res = __strncpy_from_user_asm(count, dst, src);
383         return res;
384 }
385
386
387 extern long __strnlen_user_asm(long count, const char __user *src);
388
389 static inline unsigned long
390 strnlen_user(const char __user * src, unsigned long n)
391 {
392         might_sleep();
393         return __strnlen_user_asm(n, src);
394 }
395
396 /**
397  * strlen_user: - Get the size of a string in user space.
398  * @str: The string to measure.
399  *
400  * Context: User context only.  This function may sleep.
401  *
402  * Get the size of a NUL-terminated string in user space.
403  *
404  * Returns the size of the string INCLUDING the terminating NUL.
405  * On exception, returns 0.
406  *
407  * If there is a limit on the length of a valid string, you may wish to
408  * consider using strnlen_user() instead.
409  */
410 #define strlen_user(str) strnlen_user(str, ~0UL)
411
412 /*
413  * Zero Userspace
414  */
415
416 extern long __clear_user_asm(void __user *to, long n);
417
418 static inline unsigned long
419 __clear_user(void __user *to, unsigned long n)
420 {
421         return __clear_user_asm(to, n);
422 }
423
424 static inline unsigned long
425 clear_user(void __user *to, unsigned long n)
426 {
427         might_sleep();
428         if (access_ok(VERIFY_WRITE, to, n))
429                 n = __clear_user_asm(to, n);
430         return n;
431 }
432
433 #endif /* __S390_UACCESS_H */