3 * Copyright IBM Corp. 1999, 2000
4 * Author(s): Hartmut Penner (hp@de.ibm.com),
5 * Martin Schwidefsky (schwidefsky@de.ibm.com)
7 * Derived from "include/asm-i386/uaccess.h"
9 #ifndef __S390_UACCESS_H
10 #define __S390_UACCESS_H
13 * User space memory access functions
15 #include <linux/sched.h>
16 #include <linux/errno.h>
17 #include <asm/processor.h>
18 #include <asm/ctl_reg.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)
40 #define segment_eq(a,b) ((a).ar4 == (b).ar4)
42 static inline void set_fs(mm_segment_t fs)
44 current->thread.mm_segment = fs;
45 if (segment_eq(fs, KERNEL_DS)) {
46 set_cpu_flag(CIF_ASCE_SECONDARY);
47 __ctl_load(S390_lowcore.kernel_asce, 7, 7);
49 clear_cpu_flag(CIF_ASCE_SECONDARY);
50 __ctl_load(S390_lowcore.user_asce, 7, 7);
54 static inline int __range_ok(unsigned long addr, unsigned long size)
59 #define __access_ok(addr, size) \
61 __chk_user_ptr(addr); \
62 __range_ok((unsigned long)(addr), (size)); \
65 #define access_ok(type, addr, size) __access_ok(addr, size)
68 * The exception table consists of pairs of addresses: the first is the
69 * address of an instruction that is allowed to fault, and the second is
70 * the address at which the program should continue. No registers are
71 * modified, so it is entirely up to the continuation code to figure out
74 * All the routines below use bits of fixup code that are out of line
75 * with the main instruction path. This means when everything is well,
76 * we don't even have to jump over them. Further, they do not intrude
77 * on our cache or tlb entries.
80 struct exception_table_entry
85 static inline unsigned long extable_fixup(const struct exception_table_entry *x)
87 return (unsigned long)&x->fixup + x->fixup;
90 #define ARCH_HAS_RELATIVE_EXTABLE
93 * __copy_from_user: - Copy a block of data from user space, with less checking.
94 * @to: Destination address, in kernel space.
95 * @from: Source address, in user space.
96 * @n: Number of bytes to copy.
98 * Context: User context only. This function may sleep if pagefaults are
101 * Copy data from user space to kernel space. Caller must check
102 * the specified block with access_ok() before calling this function.
104 * Returns number of bytes that could not be copied.
105 * On success, this will be zero.
107 * If some data could not be copied, this function will pad the copied
108 * data to the requested size using zero bytes.
110 unsigned long __must_check __copy_from_user(void *to, const void __user *from,
114 * __copy_to_user: - Copy a block of data into user space, with less checking.
115 * @to: Destination address, in user space.
116 * @from: Source address, in kernel space.
117 * @n: Number of bytes to copy.
119 * Context: User context only. This function may sleep if pagefaults are
122 * Copy data from kernel space to user space. Caller must check
123 * the specified block with access_ok() before calling this function.
125 * Returns number of bytes that could not be copied.
126 * On success, this will be zero.
128 unsigned long __must_check __copy_to_user(void __user *to, const void *from,
131 #define __copy_to_user_inatomic __copy_to_user
132 #define __copy_from_user_inatomic __copy_from_user
134 #ifdef CONFIG_HAVE_MARCH_Z10_FEATURES
136 #define __put_get_user_asm(to, from, size, spec) \
138 register unsigned long __reg0 asm("0") = spec; \
142 "0: mvcos %1,%3,%2\n" \
145 ".pushsection .fixup, \"ax\"\n" \
149 EX_TABLE(0b,3b) EX_TABLE(1b,3b) \
150 : "=d" (__rc), "=Q" (*(to)) \
151 : "d" (size), "Q" (*(from)), \
152 "d" (__reg0), "K" (-EFAULT) \
157 static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
159 unsigned long spec = 0x810000UL;
164 rc = __put_get_user_asm((unsigned char __user *)ptr,
169 rc = __put_get_user_asm((unsigned short __user *)ptr,
174 rc = __put_get_user_asm((unsigned int __user *)ptr,
179 rc = __put_get_user_asm((unsigned long __user *)ptr,
187 static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
189 unsigned long spec = 0x81UL;
194 rc = __put_get_user_asm((unsigned char *)x,
195 (unsigned char __user *)ptr,
199 rc = __put_get_user_asm((unsigned short *)x,
200 (unsigned short __user *)ptr,
204 rc = __put_get_user_asm((unsigned int *)x,
205 (unsigned int __user *)ptr,
209 rc = __put_get_user_asm((unsigned long *)x,
210 (unsigned long __user *)ptr,
217 #else /* CONFIG_HAVE_MARCH_Z10_FEATURES */
219 static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size)
221 size = __copy_to_user(ptr, x, size);
222 return size ? -EFAULT : 0;
225 static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size)
227 size = __copy_from_user(x, ptr, size);
228 return size ? -EFAULT : 0;
231 #endif /* CONFIG_HAVE_MARCH_Z10_FEATURES */
234 * These are the main single-value transfer routines. They automatically
235 * use the right size if we just have the right pointer type.
237 #define __put_user(x, ptr) \
239 __typeof__(*(ptr)) __x = (x); \
240 int __pu_err = -EFAULT; \
241 __chk_user_ptr(ptr); \
242 switch (sizeof (*(ptr))) { \
247 __pu_err = __put_user_fn(&__x, ptr, \
254 __builtin_expect(__pu_err, 0); \
257 #define put_user(x, ptr) \
260 __put_user(x, ptr); \
264 int __put_user_bad(void) __attribute__((noreturn));
266 #define __get_user(x, ptr) \
268 int __gu_err = -EFAULT; \
269 __chk_user_ptr(ptr); \
270 switch (sizeof(*(ptr))) { \
272 unsigned char __x = 0; \
273 __gu_err = __get_user_fn(&__x, ptr, \
275 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
279 unsigned short __x = 0; \
280 __gu_err = __get_user_fn(&__x, ptr, \
282 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
286 unsigned int __x = 0; \
287 __gu_err = __get_user_fn(&__x, ptr, \
289 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
293 unsigned long long __x = 0; \
294 __gu_err = __get_user_fn(&__x, ptr, \
296 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
303 __builtin_expect(__gu_err, 0); \
306 #define get_user(x, ptr) \
309 __get_user(x, ptr); \
312 int __get_user_bad(void) __attribute__((noreturn));
314 #define __put_user_unaligned __put_user
315 #define __get_user_unaligned __get_user
317 extern void __compiletime_error("usercopy buffer size is too small")
318 __bad_copy_user(void);
320 static inline void copy_user_overflow(int size, unsigned long count)
322 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
326 * copy_to_user: - Copy a block of data into user space.
327 * @to: Destination address, in user space.
328 * @from: Source address, in kernel space.
329 * @n: Number of bytes to copy.
331 * Context: User context only. This function may sleep if pagefaults are
334 * Copy data from kernel space to user space.
336 * Returns number of bytes that could not be copied.
337 * On success, this will be zero.
339 static inline unsigned long __must_check
340 copy_to_user(void __user *to, const void *from, unsigned long n)
343 return __copy_to_user(to, from, n);
347 * copy_from_user: - Copy a block of data from user space.
348 * @to: Destination address, in kernel space.
349 * @from: Source address, in user space.
350 * @n: Number of bytes to copy.
352 * Context: User context only. This function may sleep if pagefaults are
355 * Copy data from user space to kernel space.
357 * Returns number of bytes that could not be copied.
358 * On success, this will be zero.
360 * If some data could not be copied, this function will pad the copied
361 * data to the requested size using zero bytes.
363 static inline unsigned long __must_check
364 copy_from_user(void *to, const void __user *from, unsigned long n)
366 unsigned int sz = __compiletime_object_size(to);
369 if (unlikely(sz != -1 && sz < n)) {
370 if (!__builtin_constant_p(n))
371 copy_user_overflow(sz, n);
376 return __copy_from_user(to, from, n);
379 unsigned long __must_check
380 __copy_in_user(void __user *to, const void __user *from, unsigned long n);
382 static inline unsigned long __must_check
383 copy_in_user(void __user *to, const void __user *from, unsigned long n)
386 return __copy_in_user(to, from, n);
390 * Copy a null terminated string from userspace.
393 long __strncpy_from_user(char *dst, const char __user *src, long count);
395 static inline long __must_check
396 strncpy_from_user(char *dst, const char __user *src, long count)
399 return __strncpy_from_user(dst, src, count);
402 unsigned long __must_check __strnlen_user(const char __user *src, unsigned long count);
404 static inline unsigned long strnlen_user(const char __user *src, unsigned long n)
407 return __strnlen_user(src, n);
411 * strlen_user: - Get the size of a string in user space.
412 * @str: The string to measure.
414 * Context: User context only. This function may sleep if pagefaults are
417 * Get the size of a NUL-terminated string in user space.
419 * Returns the size of the string INCLUDING the terminating NUL.
420 * On exception, returns 0.
422 * If there is a limit on the length of a valid string, you may wish to
423 * consider using strnlen_user() instead.
425 #define strlen_user(str) strnlen_user(str, ~0UL)
430 unsigned long __must_check __clear_user(void __user *to, unsigned long size);
432 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
435 return __clear_user(to, n);
438 int copy_to_user_real(void __user *dest, void *src, unsigned long count);
439 void s390_kernel_write(void *dst, const void *src, size_t size);
441 #endif /* __S390_UACCESS_H */