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)
69 * The exception table consists of pairs of addresses: the first is the
70 * address of an instruction that is allowed to fault, and the second is
71 * the address at which the program should continue. No registers are
72 * modified, so it is entirely up to the continuation code to figure out
75 * All the routines below use bits of fixup code that are out of line
76 * with the main instruction path. This means when everything is well,
77 * we don't even have to jump over them. Further, they do not intrude
78 * on our cache or tlb entries.
81 struct exception_table_entry
83 unsigned long insn, fixup;
87 #define __uaccess_fixup \
88 ".section .fixup,\"ax\"\n" \
95 ".section __ex_table,\"a\"\n" \
99 #define __uaccess_clobber "cc", "1"
100 #else /* __s390x__ */
101 #define __uaccess_fixup \
102 ".section .fixup,\"ax\"\n" \
106 ".section __ex_table,\"a\"\n" \
110 #define __uaccess_clobber "cc"
111 #endif /* __s390x__ */
114 * These are the main single-value transfer routines. They automatically
115 * use the right size if we just have the right pointer type.
117 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
118 #define __put_user_asm(x, ptr, err) \
122 "0: mvcs 0(%1,%2),%3,%0\n" \
126 : "d" (sizeof(*(ptr))), "a" (ptr), "Q" (x), \
128 : __uaccess_clobber ); \
131 #define __put_user_asm(x, ptr, err) \
135 "0: mvcs 0(%1,%2),0(%3),%0\n" \
139 : "d" (sizeof(*(ptr))), "a" (ptr), "a" (&(x)), \
140 "K" (-EFAULT), "m" (x) \
141 : __uaccess_clobber ); \
145 #define __put_user(x, ptr) \
147 __typeof__(*(ptr)) __x = (x); \
149 __chk_user_ptr(ptr); \
150 switch (sizeof (*(ptr))) { \
155 __put_user_asm(__x, ptr, __pu_err); \
164 #define put_user(x, ptr) \
167 __put_user(x, ptr); \
171 extern int __put_user_bad(void) __attribute__((noreturn));
173 #if __GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 2)
174 #define __get_user_asm(x, ptr, err) \
178 "0: mvcp %O1(%2,%R1),0(%3),%0\n" \
181 : "+&d" (err), "=Q" (x) \
182 : "d" (sizeof(*(ptr))), "a" (ptr), \
184 : __uaccess_clobber ); \
187 #define __get_user_asm(x, ptr, err) \
191 "0: mvcp 0(%2,%5),0(%3),%0\n" \
194 : "+&d" (err), "=m" (x) \
195 : "d" (sizeof(*(ptr))), "a" (ptr), \
196 "K" (-EFAULT), "a" (&(x)) \
197 : __uaccess_clobber ); \
201 #define __get_user(x, ptr) \
203 __typeof__(*(ptr)) __x; \
205 __chk_user_ptr(ptr); \
206 switch (sizeof(*(ptr))) { \
211 __get_user_asm(__x, ptr, __gu_err); \
221 #define get_user(x, ptr) \
224 __get_user(x, ptr); \
227 extern int __get_user_bad(void) __attribute__((noreturn));
229 #define __put_user_unaligned __put_user
230 #define __get_user_unaligned __get_user
232 extern long __copy_to_user_asm(const void *from, long n, void __user *to);
235 * __copy_to_user: - Copy a block of data into user space, with less checking.
236 * @to: Destination address, in user space.
237 * @from: Source address, in kernel space.
238 * @n: Number of bytes to copy.
240 * Context: User context only. This function may sleep.
242 * Copy data from kernel space to user space. Caller must check
243 * the specified block with access_ok() before calling this function.
245 * Returns number of bytes that could not be copied.
246 * On success, this will be zero.
248 static inline unsigned long
249 __copy_to_user(void __user *to, const void *from, unsigned long n)
251 return __copy_to_user_asm(from, n, to);
254 #define __copy_to_user_inatomic __copy_to_user
255 #define __copy_from_user_inatomic __copy_from_user
258 * copy_to_user: - Copy a block of data into user space.
259 * @to: Destination address, in user space.
260 * @from: Source address, in kernel space.
261 * @n: Number of bytes to copy.
263 * Context: User context only. This function may sleep.
265 * Copy data from kernel space to user space.
267 * Returns number of bytes that could not be copied.
268 * On success, this will be zero.
270 static inline unsigned long
271 copy_to_user(void __user *to, const void *from, unsigned long n)
274 if (access_ok(VERIFY_WRITE, to, n))
275 n = __copy_to_user(to, from, n);
279 extern long __copy_from_user_asm(void *to, long n, const void __user *from);
282 * __copy_from_user: - Copy a block of data from user space, with less checking.
283 * @to: Destination address, in kernel space.
284 * @from: Source address, in user space.
285 * @n: Number of bytes to copy.
287 * Context: User context only. This function may sleep.
289 * Copy data from user space to kernel space. Caller must check
290 * the specified block with access_ok() before calling this function.
292 * Returns number of bytes that could not be copied.
293 * On success, this will be zero.
295 * If some data could not be copied, this function will pad the copied
296 * data to the requested size using zero bytes.
298 static inline unsigned long
299 __copy_from_user(void *to, const void __user *from, unsigned long n)
301 return __copy_from_user_asm(to, n, from);
305 * copy_from_user: - Copy a block of data from user space.
306 * @to: Destination address, in kernel space.
307 * @from: Source address, in user space.
308 * @n: Number of bytes to copy.
310 * Context: User context only. This function may sleep.
312 * Copy data from user space to kernel space.
314 * Returns number of bytes that could not be copied.
315 * On success, this will be zero.
317 * If some data could not be copied, this function will pad the copied
318 * data to the requested size using zero bytes.
320 static inline unsigned long
321 copy_from_user(void *to, const void __user *from, unsigned long n)
324 if (access_ok(VERIFY_READ, from, n))
325 n = __copy_from_user(to, from, n);
331 extern unsigned long __copy_in_user_asm(const void __user *from, long n,
334 static inline unsigned long
335 __copy_in_user(void __user *to, const void __user *from, unsigned long n)
337 return __copy_in_user_asm(from, n, to);
340 static inline unsigned long
341 copy_in_user(void __user *to, const void __user *from, unsigned long n)
344 if (__access_ok(from,n) && __access_ok(to,n))
345 n = __copy_in_user_asm(from, n, to);
350 * Copy a null terminated string from userspace.
352 extern long __strncpy_from_user_asm(long count, char *dst,
353 const char __user *src);
356 strncpy_from_user(char *dst, const char __user *src, long count)
360 if (access_ok(VERIFY_READ, src, 1))
361 res = __strncpy_from_user_asm(count, dst, src);
366 extern long __strnlen_user_asm(long count, const char __user *src);
368 static inline unsigned long
369 strnlen_user(const char __user * src, unsigned long n)
372 return __strnlen_user_asm(n, src);
376 * strlen_user: - Get the size of a string in user space.
377 * @str: The string to measure.
379 * Context: User context only. This function may sleep.
381 * Get the size of a NUL-terminated string in user space.
383 * Returns the size of the string INCLUDING the terminating NUL.
384 * On exception, returns 0.
386 * If there is a limit on the length of a valid string, you may wish to
387 * consider using strnlen_user() instead.
389 #define strlen_user(str) strnlen_user(str, ~0UL)
395 extern long __clear_user_asm(void __user *to, long n);
397 static inline unsigned long
398 __clear_user(void __user *to, unsigned long n)
400 return __clear_user_asm(to, n);
403 static inline unsigned long
404 clear_user(void __user *to, unsigned long n)
407 if (access_ok(VERIFY_WRITE, to, n))
408 n = __clear_user_asm(to, n);
412 #endif /* __S390_UACCESS_H */