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