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>
19 #include <asm/ctl_reg.h>
22 #define VERIFY_WRITE 1
26 * The fs value determines whether argument validity checking should be
27 * performed or not. If get_fs() == USER_DS, checking is performed, with
28 * get_fs() == KERNEL_DS, checking is bypassed.
30 * For historical reasons, these macros are grossly misnamed.
33 #define MAKE_MM_SEG(a) ((mm_segment_t) { (a) })
36 #define KERNEL_DS MAKE_MM_SEG(0)
37 #define USER_DS MAKE_MM_SEG(1)
39 #define get_ds() (KERNEL_DS)
40 #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 __ctl_load(__pto, 7, 7); \
51 #define segment_eq(a,b) ((a).ar4 == (b).ar4)
53 #define __access_ok(addr, size) \
55 __chk_user_ptr(addr); \
59 #define access_ok(type, addr, size) __access_ok(addr, size)
62 * The exception table consists of pairs of addresses: the first is the
63 * address of an instruction that is allowed to fault, and the second is
64 * the address at which the program should continue. No registers are
65 * modified, so it is entirely up to the continuation code to figure out
68 * All the routines below use bits of fixup code that are out of line
69 * with the main instruction path. This means when everything is well,
70 * we don't even have to jump over them. Further, they do not intrude
71 * on our cache or tlb entries.
74 struct exception_table_entry
76 unsigned long insn, fixup;
80 size_t (*copy_from_user)(size_t, const void __user *, void *);
81 size_t (*copy_from_user_small)(size_t, const void __user *, void *);
82 size_t (*copy_to_user)(size_t, void __user *, const void *);
83 size_t (*copy_to_user_small)(size_t, void __user *, const void *);
84 size_t (*copy_in_user)(size_t, void __user *, const void __user *);
85 size_t (*clear_user)(size_t, void __user *);
86 size_t (*strnlen_user)(size_t, const char __user *);
87 size_t (*strncpy_from_user)(size_t, const char __user *, char *);
88 int (*futex_atomic_op)(int op, u32 __user *, int oparg, int *old);
89 int (*futex_atomic_cmpxchg)(u32 *, u32 __user *, u32 old, u32 new);
92 extern struct uaccess_ops uaccess;
93 extern struct uaccess_ops uaccess_std;
94 extern struct uaccess_ops uaccess_mvcos;
95 extern struct uaccess_ops uaccess_mvcos_switch;
96 extern struct uaccess_ops uaccess_pt;
98 extern int __handle_fault(unsigned long, unsigned long, int);
100 static inline int __put_user_fn(size_t size, void __user *ptr, void *x)
102 size = uaccess.copy_to_user_small(size, ptr, x);
103 return size ? -EFAULT : size;
106 static inline int __get_user_fn(size_t size, const void __user *ptr, void *x)
108 size = uaccess.copy_from_user_small(size, ptr, x);
109 return size ? -EFAULT : size;
113 * These are the main single-value transfer routines. They automatically
114 * use the right size if we just have the right pointer type.
116 #define __put_user(x, ptr) \
118 __typeof__(*(ptr)) __x = (x); \
119 int __pu_err = -EFAULT; \
120 __chk_user_ptr(ptr); \
121 switch (sizeof (*(ptr))) { \
126 __pu_err = __put_user_fn(sizeof (*(ptr)), \
136 #define put_user(x, ptr) \
139 __put_user(x, ptr); \
143 extern int __put_user_bad(void) __attribute__((noreturn));
145 #define __get_user(x, ptr) \
147 int __gu_err = -EFAULT; \
148 __chk_user_ptr(ptr); \
149 switch (sizeof(*(ptr))) { \
152 __gu_err = __get_user_fn(sizeof (*(ptr)), \
154 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
158 unsigned short __x; \
159 __gu_err = __get_user_fn(sizeof (*(ptr)), \
161 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
166 __gu_err = __get_user_fn(sizeof (*(ptr)), \
168 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
172 unsigned long long __x; \
173 __gu_err = __get_user_fn(sizeof (*(ptr)), \
175 (x) = *(__force __typeof__(*(ptr)) *) &__x; \
185 #define get_user(x, ptr) \
188 __get_user(x, ptr); \
191 extern int __get_user_bad(void) __attribute__((noreturn));
193 #define __put_user_unaligned __put_user
194 #define __get_user_unaligned __get_user
197 * __copy_to_user: - Copy a block of data into user space, with less checking.
198 * @to: Destination address, in user space.
199 * @from: Source address, in kernel space.
200 * @n: Number of bytes to copy.
202 * Context: User context only. This function may sleep.
204 * Copy data from kernel space to user space. Caller must check
205 * the specified block with access_ok() before calling this function.
207 * Returns number of bytes that could not be copied.
208 * On success, this will be zero.
210 static inline unsigned long __must_check
211 __copy_to_user(void __user *to, const void *from, unsigned long n)
213 if (__builtin_constant_p(n) && (n <= 256))
214 return uaccess.copy_to_user_small(n, to, from);
216 return uaccess.copy_to_user(n, to, from);
219 #define __copy_to_user_inatomic __copy_to_user
220 #define __copy_from_user_inatomic __copy_from_user
223 * copy_to_user: - Copy a block of data into user space.
224 * @to: Destination address, in user space.
225 * @from: Source address, in kernel space.
226 * @n: Number of bytes to copy.
228 * Context: User context only. This function may sleep.
230 * Copy data from kernel space to user space.
232 * Returns number of bytes that could not be copied.
233 * On success, this will be zero.
235 static inline unsigned long __must_check
236 copy_to_user(void __user *to, const void *from, unsigned long n)
239 if (access_ok(VERIFY_WRITE, to, n))
240 n = __copy_to_user(to, from, n);
245 * __copy_from_user: - Copy a block of data from user space, with less checking.
246 * @to: Destination address, in kernel space.
247 * @from: Source address, in user space.
248 * @n: Number of bytes to copy.
250 * Context: User context only. This function may sleep.
252 * Copy data from user space to kernel space. Caller must check
253 * the specified block with access_ok() before calling this function.
255 * Returns number of bytes that could not be copied.
256 * On success, this will be zero.
258 * If some data could not be copied, this function will pad the copied
259 * data to the requested size using zero bytes.
261 static inline unsigned long __must_check
262 __copy_from_user(void *to, const void __user *from, unsigned long n)
264 if (__builtin_constant_p(n) && (n <= 256))
265 return uaccess.copy_from_user_small(n, from, to);
267 return uaccess.copy_from_user(n, from, to);
270 extern void copy_from_user_overflow(void)
271 #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
272 __compiletime_warning("copy_from_user() buffer size is not provably correct")
277 * copy_from_user: - Copy a block of data from user space.
278 * @to: Destination address, in kernel space.
279 * @from: Source address, in user space.
280 * @n: Number of bytes to copy.
282 * Context: User context only. This function may sleep.
284 * Copy data from user space to kernel space.
286 * Returns number of bytes that could not be copied.
287 * On success, this will be zero.
289 * If some data could not be copied, this function will pad the copied
290 * data to the requested size using zero bytes.
292 static inline unsigned long __must_check
293 copy_from_user(void *to, const void __user *from, unsigned long n)
295 unsigned int sz = __compiletime_object_size(to);
298 if (unlikely(sz != -1 && sz < n)) {
299 copy_from_user_overflow();
302 if (access_ok(VERIFY_READ, from, n))
303 n = __copy_from_user(to, from, n);
309 static inline unsigned long __must_check
310 __copy_in_user(void __user *to, const void __user *from, unsigned long n)
312 return uaccess.copy_in_user(n, to, from);
315 static inline unsigned long __must_check
316 copy_in_user(void __user *to, const void __user *from, unsigned long n)
319 if (__access_ok(from,n) && __access_ok(to,n))
320 n = __copy_in_user(to, from, n);
325 * Copy a null terminated string from userspace.
327 static inline long __must_check
328 strncpy_from_user(char *dst, const char __user *src, long count)
332 if (access_ok(VERIFY_READ, src, 1))
333 res = uaccess.strncpy_from_user(count, src, dst);
337 static inline unsigned long
338 strnlen_user(const char __user * src, unsigned long n)
341 return uaccess.strnlen_user(n, src);
345 * strlen_user: - Get the size of a string in user space.
346 * @str: The string to measure.
348 * Context: User context only. This function may sleep.
350 * Get the size of a NUL-terminated string in user space.
352 * Returns the size of the string INCLUDING the terminating NUL.
353 * On exception, returns 0.
355 * If there is a limit on the length of a valid string, you may wish to
356 * consider using strnlen_user() instead.
358 #define strlen_user(str) strnlen_user(str, ~0UL)
364 static inline unsigned long __must_check
365 __clear_user(void __user *to, unsigned long n)
367 return uaccess.clear_user(n, to);
370 static inline unsigned long __must_check
371 clear_user(void __user *to, unsigned long n)
374 if (access_ok(VERIFY_WRITE, to, n))
375 n = uaccess.clear_user(n, to);
379 extern int memcpy_real(void *, void *, size_t);
380 extern void memcpy_absolute(void *, void *, size_t);
381 extern int copy_to_user_real(void __user *dest, void *src, size_t count);
382 extern int copy_from_user_real(void *dest, void __user *src, size_t count);
384 #endif /* __S390_UACCESS_H */