2 * include/asm-s390/uaccess.h
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)
9 * Derived from "include/asm-i386/uaccess.h"
11 #ifndef __S390_UACCESS_H
12 #define __S390_UACCESS_H
15 * User space memory access functions
17 #include <linux/sched.h>
18 #include <linux/errno.h>
21 #define VERIFY_WRITE 1
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.
29 * For historical reasons, these macros are grossly misnamed.
32 #define MAKE_MM_SEG(a) ((mm_segment_t) { (a) })
35 #define KERNEL_DS MAKE_MM_SEG(0)
36 #define USER_DS MAKE_MM_SEG(1)
38 #define get_ds() (KERNEL_DS)
39 #define get_fs() (current->thread.mm_segment)
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) ); \
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) ); \
61 #define segment_eq(a,b) ((a).ar4 == (b).ar4)
64 #define __access_ok(addr,size) (1)
66 #define access_ok(type,addr,size) __access_ok(addr,size)
68 extern inline int verify_area(int type, const void * addr, unsigned long size)
70 return access_ok(type,addr,size)?0:-EFAULT;
74 * The exception table consists of pairs of addresses: the first is the
75 * address of an instruction that is allowed to fault, and the second is
76 * the address at which the program should continue. No registers are
77 * modified, so it is entirely up to the continuation code to figure out
80 * All the routines below use bits of fixup code that are out of line
81 * with the main instruction path. This means when everything is well,
82 * we don't even have to jump over them. Further, they do not intrude
83 * on our cache or tlb entries.
86 struct exception_table_entry
88 unsigned long insn, fixup;
92 #define __uaccess_fixup \
93 ".section .fixup,\"ax\"\n" \
100 ".section __ex_table,\"a\"\n" \
104 #define __uaccess_clobber "cc", "1"
105 #else /* __s390x__ */
106 #define __uaccess_fixup \
107 ".section .fixup,\"ax\"\n" \
111 ".section __ex_table,\"a\"\n" \
115 #define __uaccess_clobber "cc"
116 #endif /* __s390x__ */
119 * These are the main single-value transfer routines. They automatically
120 * use the right size if we just have the right pointer type.
122 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
123 #define __put_user_asm(x, ptr, err) \
127 "0: mvcs 0(%1,%2),%3,%0\n" \
131 : "d" (sizeof(*(ptr))), "a" (ptr), "Q" (x), \
133 : __uaccess_clobber ); \
136 #define __put_user_asm(x, ptr, err) \
140 "0: mvcs 0(%1,%2),0(%3),%0\n" \
144 : "d" (sizeof(*(ptr))), "a" (ptr), "a" (&(x)), \
145 "K" (-EFAULT), "m" (x) \
146 : __uaccess_clobber ); \
150 #define __put_user(x, ptr) \
152 __typeof__(*(ptr)) __x = (x); \
154 switch (sizeof (*(ptr))) { \
159 __put_user_asm(__x, ptr, __pu_err); \
162 __pu_err = __put_user_bad(); \
168 #define put_user(x, ptr) \
171 __put_user(x, ptr); \
175 extern int __put_user_bad(void);
177 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
178 #define __get_user_asm(x, ptr, err) \
182 "0: mvcp %O1(%2,%R1),0(%3),%0\n" \
185 : "+&d" (err), "=Q" (x) \
186 : "d" (sizeof(*(ptr))), "a" (ptr), \
188 : __uaccess_clobber ); \
191 #define __get_user_asm(x, ptr, err) \
195 "0: mvcp 0(%2,%5),0(%3),%0\n" \
198 : "+&d" (err), "=m" (x) \
199 : "d" (sizeof(*(ptr))), "a" (ptr), \
200 "K" (-EFAULT), "a" (&(x)) \
201 : __uaccess_clobber ); \
205 #define __get_user(x, ptr) \
207 __typeof__(*(ptr)) __x; \
209 switch (sizeof(*(ptr))) { \
214 __get_user_asm(__x, ptr, __gu_err); \
218 __gu_err = __get_user_bad(); \
225 #define get_user(x, ptr) \
228 __get_user(x, ptr); \
231 extern int __get_user_bad(void);
233 extern long __copy_to_user_asm(const void *from, long n, void *to);
236 * __copy_to_user: - Copy a block of data into user space, with less checking.
237 * @to: Destination address, in user space.
238 * @from: Source address, in kernel space.
239 * @n: Number of bytes to copy.
241 * Context: User context only. This function may sleep.
243 * Copy data from kernel space to user space. Caller must check
244 * the specified block with access_ok() before calling this function.
246 * Returns number of bytes that could not be copied.
247 * On success, this will be zero.
249 static inline unsigned long
250 __copy_to_user(void __user *to, const void *from, unsigned long n)
252 return __copy_to_user_asm(from, n, to);
256 * copy_to_user: - Copy a block of data into user space.
257 * @to: Destination address, in user space.
258 * @from: Source address, in kernel space.
259 * @n: Number of bytes to copy.
261 * Context: User context only. This function may sleep.
263 * Copy data from kernel space to user space.
265 * Returns number of bytes that could not be copied.
266 * On success, this will be zero.
268 static inline unsigned long
269 copy_to_user(void __user *to, const void *from, unsigned long n)
272 if (access_ok(VERIFY_WRITE, to, n))
273 n = __copy_to_user(to, from, n);
277 extern long __copy_from_user_asm(void *to, long n, const void *from);
280 * __copy_from_user: - Copy a block of data from user space, with less checking.
281 * @to: Destination address, in kernel space.
282 * @from: Source address, in user space.
283 * @n: Number of bytes to copy.
285 * Context: User context only. This function may sleep.
287 * Copy data from user space to kernel space. Caller must check
288 * the specified block with access_ok() before calling this function.
290 * Returns number of bytes that could not be copied.
291 * On success, this will be zero.
293 * If some data could not be copied, this function will pad the copied
294 * data to the requested size using zero bytes.
296 static inline unsigned long
297 __copy_from_user(void *to, const void __user *from, unsigned long n)
299 return __copy_from_user_asm(to, n, from);
303 * copy_from_user: - Copy a block of data from user space.
304 * @to: Destination address, in kernel space.
305 * @from: Source address, in user space.
306 * @n: Number of bytes to copy.
308 * Context: User context only. This function may sleep.
310 * Copy data from user space to kernel space.
312 * Returns number of bytes that could not be copied.
313 * On success, this will be zero.
315 * If some data could not be copied, this function will pad the copied
316 * data to the requested size using zero bytes.
318 static inline unsigned long
319 copy_from_user(void *to, const void __user *from, unsigned long n)
322 if (access_ok(VERIFY_READ, from, n))
323 n = __copy_from_user(to, from, n);
329 extern long __copy_in_user_asm(const void *from, long n, void *to);
331 static inline unsigned long
332 __copy_in_user(void __user *to, const void __user *from, unsigned long n)
334 __copy_in_user_asm(from, n, to);
337 static inline unsigned long
338 copy_in_user(void __user *to, const void __user *from, unsigned long n)
341 if (__access_ok(from,n) && __access_ok(to,n))
342 n = __copy_in_user_asm(from, n, to);
347 * Copy a null terminated string from userspace.
349 extern long __strncpy_from_user_asm(char *dst, const char *src, long count);
352 strncpy_from_user(char *dst, const char *src, long count)
356 if (access_ok(VERIFY_READ, src, 1))
357 res = __strncpy_from_user_asm(dst, src, count);
362 extern long __strnlen_user_asm(const char *src, long count);
364 static inline unsigned long
365 strnlen_user(const char * src, unsigned long n)
368 return __strnlen_user_asm(src, n);
372 * strlen_user: - Get the size of a string in user space.
373 * @str: The string to measure.
375 * Context: User context only. This function may sleep.
377 * Get the size of a NUL-terminated string in user space.
379 * Returns the size of the string INCLUDING the terminating NUL.
380 * On exception, returns 0.
382 * If there is a limit on the length of a valid string, you may wish to
383 * consider using strnlen_user() instead.
385 #define strlen_user(str) strnlen_user(str, ~0UL)
391 extern long __clear_user_asm(void *to, long n);
393 static inline unsigned long
394 __clear_user(void *to, unsigned long n)
396 return __clear_user_asm(to, n);
399 static inline unsigned long
400 clear_user(void *to, unsigned long n)
403 if (access_ok(VERIFY_WRITE, to, n))
404 n = __clear_user_asm(to, n);
408 #endif /* __S390_UACCESS_H */