Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * S390 version |
Heiko Carstens | a53c8fa | 2012-07-20 11:15:04 +0200 | [diff] [blame] | 3 | * Copyright IBM Corp. 1999, 2000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * Author(s): Hartmut Penner (hp@de.ibm.com), |
| 5 | * Martin Schwidefsky (schwidefsky@de.ibm.com) |
| 6 | * |
| 7 | * Derived from "include/asm-i386/uaccess.h" |
| 8 | */ |
| 9 | #ifndef __S390_UACCESS_H |
| 10 | #define __S390_UACCESS_H |
| 11 | |
| 12 | /* |
| 13 | * User space memory access functions |
| 14 | */ |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/errno.h> |
Heiko Carstens | b5a882f | 2017-02-17 08:13:28 +0100 | [diff] [blame] | 17 | #include <asm/processor.h> |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 18 | #include <asm/ctl_reg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #define segment_eq(a,b) ((a).ar4 == (b).ar4) |
| 41 | |
Heiko Carstens | b5a882f | 2017-02-17 08:13:28 +0100 | [diff] [blame] | 42 | static inline void set_fs(mm_segment_t fs) |
| 43 | { |
| 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); |
| 48 | } else { |
| 49 | clear_cpu_flag(CIF_ASCE_SECONDARY); |
| 50 | __ctl_load(S390_lowcore.user_asce, 7, 7); |
| 51 | } |
| 52 | } |
| 53 | |
Heiko Carstens | 491af99 | 2012-05-29 07:33:59 +0200 | [diff] [blame] | 54 | static inline int __range_ok(unsigned long addr, unsigned long size) |
| 55 | { |
| 56 | return 1; |
| 57 | } |
| 58 | |
| 59 | #define __access_ok(addr, size) \ |
| 60 | ({ \ |
| 61 | __chk_user_ptr(addr); \ |
| 62 | __range_ok((unsigned long)(addr), (size)); \ |
Heiko Carstens | 7683f74 | 2011-05-26 09:48:25 +0200 | [diff] [blame] | 63 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
Heiko Carstens | 7683f74 | 2011-05-26 09:48:25 +0200 | [diff] [blame] | 65 | #define access_ok(type, addr, size) __access_ok(addr, size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | /* |
| 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 |
| 72 | * what to do. |
| 73 | * |
| 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. |
| 78 | */ |
| 79 | |
| 80 | struct exception_table_entry |
| 81 | { |
Heiko Carstens | eb608fb | 2012-09-05 13:26:11 +0200 | [diff] [blame] | 82 | int insn, fixup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | }; |
| 84 | |
Heiko Carstens | eb608fb | 2012-09-05 13:26:11 +0200 | [diff] [blame] | 85 | static inline unsigned long extable_fixup(const struct exception_table_entry *x) |
| 86 | { |
| 87 | return (unsigned long)&x->fixup + x->fixup; |
| 88 | } |
| 89 | |
Ard Biesheuvel | c352e8b | 2016-03-22 14:28:14 -0700 | [diff] [blame] | 90 | #define ARCH_HAS_RELATIVE_EXTABLE |
Heiko Carstens | eb608fb | 2012-09-05 13:26:11 +0200 | [diff] [blame] | 91 | |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 92 | /** |
| 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. |
| 97 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 98 | * Context: User context only. This function may sleep if pagefaults are |
| 99 | * enabled. |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 100 | * |
| 101 | * Copy data from user space to kernel space. Caller must check |
| 102 | * the specified block with access_ok() before calling this function. |
| 103 | * |
| 104 | * Returns number of bytes that could not be copied. |
| 105 | * On success, this will be zero. |
| 106 | * |
| 107 | * If some data could not be copied, this function will pad the copied |
| 108 | * data to the requested size using zero bytes. |
| 109 | */ |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 110 | unsigned long __must_check __copy_from_user(void *to, const void __user *from, |
| 111 | unsigned long n); |
Gerald Schaefer | d02765d | 2006-09-20 15:59:42 +0200 | [diff] [blame] | 112 | |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 113 | /** |
| 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. |
| 118 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 119 | * Context: User context only. This function may sleep if pagefaults are |
| 120 | * enabled. |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 121 | * |
| 122 | * Copy data from kernel space to user space. Caller must check |
| 123 | * the specified block with access_ok() before calling this function. |
| 124 | * |
| 125 | * Returns number of bytes that could not be copied. |
| 126 | * On success, this will be zero. |
| 127 | */ |
| 128 | unsigned long __must_check __copy_to_user(void __user *to, const void *from, |
| 129 | unsigned long n); |
| 130 | |
| 131 | #define __copy_to_user_inatomic __copy_to_user |
| 132 | #define __copy_from_user_inatomic __copy_from_user |
Gerald Schaefer | 6c1e3e7 | 2009-12-07 12:51:47 +0100 | [diff] [blame] | 133 | |
Heiko Carstens | c9ca784 | 2014-04-17 14:16:03 +0200 | [diff] [blame] | 134 | #ifdef CONFIG_HAVE_MARCH_Z10_FEATURES |
| 135 | |
| 136 | #define __put_get_user_asm(to, from, size, spec) \ |
| 137 | ({ \ |
| 138 | register unsigned long __reg0 asm("0") = spec; \ |
| 139 | int __rc; \ |
| 140 | \ |
| 141 | asm volatile( \ |
| 142 | "0: mvcos %1,%3,%2\n" \ |
| 143 | "1: xr %0,%0\n" \ |
| 144 | "2:\n" \ |
| 145 | ".pushsection .fixup, \"ax\"\n" \ |
| 146 | "3: lhi %0,%5\n" \ |
| 147 | " jg 2b\n" \ |
| 148 | ".popsection\n" \ |
| 149 | EX_TABLE(0b,3b) EX_TABLE(1b,3b) \ |
Heiko Carstens | d09c537 | 2017-03-27 09:48:04 +0200 | [diff] [blame] | 150 | : "=d" (__rc), "+Q" (*(to)) \ |
Heiko Carstens | c9ca784 | 2014-04-17 14:16:03 +0200 | [diff] [blame] | 151 | : "d" (size), "Q" (*(from)), \ |
| 152 | "d" (__reg0), "K" (-EFAULT) \ |
| 153 | : "cc"); \ |
| 154 | __rc; \ |
| 155 | }) |
| 156 | |
Heiko Carstens | dc4aace | 2016-06-20 10:35:20 +0200 | [diff] [blame] | 157 | static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size) |
| 158 | { |
| 159 | unsigned long spec = 0x810000UL; |
| 160 | int rc; |
| 161 | |
| 162 | switch (size) { |
| 163 | case 1: |
| 164 | rc = __put_get_user_asm((unsigned char __user *)ptr, |
| 165 | (unsigned char *)x, |
| 166 | size, spec); |
| 167 | break; |
| 168 | case 2: |
| 169 | rc = __put_get_user_asm((unsigned short __user *)ptr, |
| 170 | (unsigned short *)x, |
| 171 | size, spec); |
| 172 | break; |
| 173 | case 4: |
| 174 | rc = __put_get_user_asm((unsigned int __user *)ptr, |
| 175 | (unsigned int *)x, |
| 176 | size, spec); |
| 177 | break; |
| 178 | case 8: |
| 179 | rc = __put_get_user_asm((unsigned long __user *)ptr, |
| 180 | (unsigned long *)x, |
| 181 | size, spec); |
| 182 | break; |
Heiko Carstens | 0b92515 | 2017-01-02 08:51:02 +0100 | [diff] [blame] | 183 | } |
Heiko Carstens | dc4aace | 2016-06-20 10:35:20 +0200 | [diff] [blame] | 184 | return rc; |
| 185 | } |
| 186 | |
| 187 | static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size) |
| 188 | { |
| 189 | unsigned long spec = 0x81UL; |
| 190 | int rc; |
| 191 | |
| 192 | switch (size) { |
| 193 | case 1: |
| 194 | rc = __put_get_user_asm((unsigned char *)x, |
| 195 | (unsigned char __user *)ptr, |
| 196 | size, spec); |
| 197 | break; |
| 198 | case 2: |
| 199 | rc = __put_get_user_asm((unsigned short *)x, |
| 200 | (unsigned short __user *)ptr, |
| 201 | size, spec); |
| 202 | break; |
| 203 | case 4: |
| 204 | rc = __put_get_user_asm((unsigned int *)x, |
| 205 | (unsigned int __user *)ptr, |
| 206 | size, spec); |
| 207 | break; |
| 208 | case 8: |
| 209 | rc = __put_get_user_asm((unsigned long *)x, |
| 210 | (unsigned long __user *)ptr, |
| 211 | size, spec); |
| 212 | break; |
Heiko Carstens | 0b92515 | 2017-01-02 08:51:02 +0100 | [diff] [blame] | 213 | } |
Heiko Carstens | dc4aace | 2016-06-20 10:35:20 +0200 | [diff] [blame] | 214 | return rc; |
| 215 | } |
Heiko Carstens | c9ca784 | 2014-04-17 14:16:03 +0200 | [diff] [blame] | 216 | |
| 217 | #else /* CONFIG_HAVE_MARCH_Z10_FEATURES */ |
| 218 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 219 | static inline int __put_user_fn(void *x, void __user *ptr, unsigned long size) |
Gerald Schaefer | d02765d | 2006-09-20 15:59:42 +0200 | [diff] [blame] | 220 | { |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 221 | size = __copy_to_user(ptr, x, size); |
| 222 | return size ? -EFAULT : 0; |
Gerald Schaefer | d02765d | 2006-09-20 15:59:42 +0200 | [diff] [blame] | 223 | } |
| 224 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 225 | static inline int __get_user_fn(void *x, const void __user *ptr, unsigned long size) |
Gerald Schaefer | d02765d | 2006-09-20 15:59:42 +0200 | [diff] [blame] | 226 | { |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 227 | size = __copy_from_user(x, ptr, size); |
| 228 | return size ? -EFAULT : 0; |
Gerald Schaefer | d02765d | 2006-09-20 15:59:42 +0200 | [diff] [blame] | 229 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | |
Heiko Carstens | c9ca784 | 2014-04-17 14:16:03 +0200 | [diff] [blame] | 231 | #endif /* CONFIG_HAVE_MARCH_Z10_FEATURES */ |
| 232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | /* |
| 234 | * These are the main single-value transfer routines. They automatically |
| 235 | * use the right size if we just have the right pointer type. |
| 236 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | #define __put_user(x, ptr) \ |
| 238 | ({ \ |
| 239 | __typeof__(*(ptr)) __x = (x); \ |
Gerald Schaefer | d02765d | 2006-09-20 15:59:42 +0200 | [diff] [blame] | 240 | int __pu_err = -EFAULT; \ |
Al Viro | 17566c3 | 2005-08-23 22:48:22 +0100 | [diff] [blame] | 241 | __chk_user_ptr(ptr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | switch (sizeof (*(ptr))) { \ |
| 243 | case 1: \ |
| 244 | case 2: \ |
| 245 | case 4: \ |
| 246 | case 8: \ |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 247 | __pu_err = __put_user_fn(&__x, ptr, \ |
| 248 | sizeof(*(ptr))); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | break; \ |
| 250 | default: \ |
| 251 | __put_user_bad(); \ |
| 252 | break; \ |
| 253 | } \ |
Heiko Carstens | ee64baf | 2016-06-13 10:17:20 +0200 | [diff] [blame] | 254 | __builtin_expect(__pu_err, 0); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | |
| 257 | #define put_user(x, ptr) \ |
| 258 | ({ \ |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 259 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 260 | __put_user(x, ptr); \ |
| 261 | }) |
| 262 | |
| 263 | |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 264 | int __put_user_bad(void) __attribute__((noreturn)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | #define __get_user(x, ptr) \ |
| 267 | ({ \ |
Gerald Schaefer | d02765d | 2006-09-20 15:59:42 +0200 | [diff] [blame] | 268 | int __gu_err = -EFAULT; \ |
| 269 | __chk_user_ptr(ptr); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | switch (sizeof(*(ptr))) { \ |
Martin Schwidefsky | 1047aa7 | 2005-11-07 00:59:11 -0800 | [diff] [blame] | 271 | case 1: { \ |
Al Viro | fd2d2b1 | 2016-08-21 22:00:54 -0400 | [diff] [blame] | 272 | unsigned char __x = 0; \ |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 273 | __gu_err = __get_user_fn(&__x, ptr, \ |
| 274 | sizeof(*(ptr))); \ |
Al Viro | 97fa5a6 | 2006-02-03 20:11:52 -0500 | [diff] [blame] | 275 | (x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | break; \ |
Martin Schwidefsky | 1047aa7 | 2005-11-07 00:59:11 -0800 | [diff] [blame] | 277 | }; \ |
| 278 | case 2: { \ |
Al Viro | fd2d2b1 | 2016-08-21 22:00:54 -0400 | [diff] [blame] | 279 | unsigned short __x = 0; \ |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 280 | __gu_err = __get_user_fn(&__x, ptr, \ |
| 281 | sizeof(*(ptr))); \ |
Al Viro | 97fa5a6 | 2006-02-03 20:11:52 -0500 | [diff] [blame] | 282 | (x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
Martin Schwidefsky | 1047aa7 | 2005-11-07 00:59:11 -0800 | [diff] [blame] | 283 | break; \ |
| 284 | }; \ |
| 285 | case 4: { \ |
Al Viro | fd2d2b1 | 2016-08-21 22:00:54 -0400 | [diff] [blame] | 286 | unsigned int __x = 0; \ |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 287 | __gu_err = __get_user_fn(&__x, ptr, \ |
| 288 | sizeof(*(ptr))); \ |
Al Viro | 97fa5a6 | 2006-02-03 20:11:52 -0500 | [diff] [blame] | 289 | (x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
Martin Schwidefsky | 1047aa7 | 2005-11-07 00:59:11 -0800 | [diff] [blame] | 290 | break; \ |
| 291 | }; \ |
| 292 | case 8: { \ |
Al Viro | fd2d2b1 | 2016-08-21 22:00:54 -0400 | [diff] [blame] | 293 | unsigned long long __x = 0; \ |
Heiko Carstens | cfa785e | 2014-01-22 14:49:30 +0100 | [diff] [blame] | 294 | __gu_err = __get_user_fn(&__x, ptr, \ |
| 295 | sizeof(*(ptr))); \ |
Al Viro | 97fa5a6 | 2006-02-03 20:11:52 -0500 | [diff] [blame] | 296 | (x) = *(__force __typeof__(*(ptr)) *) &__x; \ |
Martin Schwidefsky | 1047aa7 | 2005-11-07 00:59:11 -0800 | [diff] [blame] | 297 | break; \ |
| 298 | }; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | default: \ |
| 300 | __get_user_bad(); \ |
| 301 | break; \ |
| 302 | } \ |
Heiko Carstens | ee64baf | 2016-06-13 10:17:20 +0200 | [diff] [blame] | 303 | __builtin_expect(__gu_err, 0); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | }) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
| 306 | #define get_user(x, ptr) \ |
| 307 | ({ \ |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 308 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | __get_user(x, ptr); \ |
| 310 | }) |
| 311 | |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 312 | int __get_user_bad(void) __attribute__((noreturn)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | |
| 314 | #define __put_user_unaligned __put_user |
| 315 | #define __get_user_unaligned __get_user |
| 316 | |
Josh Poimboeuf | 0d025d2 | 2016-08-30 08:04:16 -0500 | [diff] [blame] | 317 | extern void __compiletime_error("usercopy buffer size is too small") |
| 318 | __bad_copy_user(void); |
| 319 | |
| 320 | static inline void copy_user_overflow(int size, unsigned long count) |
| 321 | { |
| 322 | WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count); |
| 323 | } |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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. |
| 330 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 331 | * Context: User context only. This function may sleep if pagefaults are |
| 332 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | * |
| 334 | * Copy data from kernel space to user space. |
| 335 | * |
| 336 | * Returns number of bytes that could not be copied. |
| 337 | * On success, this will be zero. |
| 338 | */ |
Heiko Carstens | f7675ad | 2006-12-04 15:39:55 +0100 | [diff] [blame] | 339 | static inline unsigned long __must_check |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | copy_to_user(void __user *to, const void *from, unsigned long n) |
| 341 | { |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 342 | might_fault(); |
Heiko Carstens | d12a297 | 2013-02-21 16:57:42 +0100 | [diff] [blame] | 343 | return __copy_to_user(to, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | } |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | /** |
| 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. |
| 351 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 352 | * Context: User context only. This function may sleep if pagefaults are |
| 353 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | * |
| 355 | * Copy data from user space to kernel space. |
| 356 | * |
| 357 | * Returns number of bytes that could not be copied. |
| 358 | * On success, this will be zero. |
| 359 | * |
| 360 | * If some data could not be copied, this function will pad the copied |
| 361 | * data to the requested size using zero bytes. |
| 362 | */ |
Heiko Carstens | f7675ad | 2006-12-04 15:39:55 +0100 | [diff] [blame] | 363 | static inline unsigned long __must_check |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | copy_from_user(void *to, const void __user *from, unsigned long n) |
| 365 | { |
Heiko Carstens | 1dcec25 | 2010-02-26 22:37:22 +0100 | [diff] [blame] | 366 | unsigned int sz = __compiletime_object_size(to); |
| 367 | |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 368 | might_fault(); |
Heiko Carstens | 1dcec25 | 2010-02-26 22:37:22 +0100 | [diff] [blame] | 369 | if (unlikely(sz != -1 && sz < n)) { |
Josh Poimboeuf | 0d025d2 | 2016-08-30 08:04:16 -0500 | [diff] [blame] | 370 | if (!__builtin_constant_p(n)) |
| 371 | copy_user_overflow(sz, n); |
| 372 | else |
| 373 | __bad_copy_user(); |
Heiko Carstens | 1dcec25 | 2010-02-26 22:37:22 +0100 | [diff] [blame] | 374 | return n; |
| 375 | } |
Heiko Carstens | d12a297 | 2013-02-21 16:57:42 +0100 | [diff] [blame] | 376 | return __copy_from_user(to, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 379 | unsigned long __must_check |
| 380 | __copy_in_user(void __user *to, const void __user *from, unsigned long n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | |
Heiko Carstens | f7675ad | 2006-12-04 15:39:55 +0100 | [diff] [blame] | 382 | static inline unsigned long __must_check |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | copy_in_user(void __user *to, const void __user *from, unsigned long n) |
| 384 | { |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 385 | might_fault(); |
Heiko Carstens | d12a297 | 2013-02-21 16:57:42 +0100 | [diff] [blame] | 386 | return __copy_in_user(to, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /* |
| 390 | * Copy a null terminated string from userspace. |
| 391 | */ |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 392 | |
| 393 | long __strncpy_from_user(char *dst, const char __user *src, long count); |
| 394 | |
Heiko Carstens | f7675ad | 2006-12-04 15:39:55 +0100 | [diff] [blame] | 395 | static inline long __must_check |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | strncpy_from_user(char *dst, const char __user *src, long count) |
| 397 | { |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 398 | might_fault(); |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 399 | return __strncpy_from_user(dst, src, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 402 | unsigned long __must_check __strnlen_user(const char __user *src, unsigned long count); |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 403 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 404 | static inline unsigned long strnlen_user(const char __user *src, unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 405 | { |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 406 | might_fault(); |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 407 | return __strnlen_user(src, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | } |
| 409 | |
| 410 | /** |
| 411 | * strlen_user: - Get the size of a string in user space. |
| 412 | * @str: The string to measure. |
| 413 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 414 | * Context: User context only. This function may sleep if pagefaults are |
| 415 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | * |
| 417 | * Get the size of a NUL-terminated string in user space. |
| 418 | * |
| 419 | * Returns the size of the string INCLUDING the terminating NUL. |
| 420 | * On exception, returns 0. |
| 421 | * |
| 422 | * If there is a limit on the length of a valid string, you may wish to |
| 423 | * consider using strnlen_user() instead. |
| 424 | */ |
| 425 | #define strlen_user(str) strnlen_user(str, ~0UL) |
| 426 | |
| 427 | /* |
| 428 | * Zero Userspace |
| 429 | */ |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 430 | unsigned long __must_check __clear_user(void __user *to, unsigned long size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 432 | static inline unsigned long __must_check clear_user(void __user *to, unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
Heiko Carstens | dab4079d | 2009-06-12 10:26:32 +0200 | [diff] [blame] | 434 | might_fault(); |
Heiko Carstens | 4f41c2b | 2014-01-23 11:18:36 +0100 | [diff] [blame] | 435 | return __clear_user(to, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Heiko Carstens | 211deca | 2014-01-24 12:51:27 +0100 | [diff] [blame] | 438 | int copy_to_user_real(void __user *dest, void *src, unsigned long count); |
Heiko Carstens | 8a5d847 | 2015-03-13 12:55:56 +0100 | [diff] [blame] | 439 | void s390_kernel_write(void *dst, const void *src, size_t size); |
David Howells | a0616cd | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | #endif /* __S390_UACCESS_H */ |