Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Russell King | 4baa992 | 2008-08-02 10:55:55 +0100 | [diff] [blame] | 2 | * arch/arm/include/asm/uaccess.h |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | #ifndef _ASMARM_UACCESS_H |
| 9 | #define _ASMARM_UACCESS_H |
| 10 | |
| 11 | /* |
| 12 | * User space memory access functions |
| 13 | */ |
Russell King | 87c5257 | 2008-11-29 17:35:51 +0000 | [diff] [blame] | 14 | #include <linux/string.h> |
| 15 | #include <linux/thread_info.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <asm/errno.h> |
| 17 | #include <asm/memory.h> |
| 18 | #include <asm/domain.h> |
Catalin Marinas | 8b59278 | 2009-07-24 12:32:57 +0100 | [diff] [blame] | 19 | #include <asm/unified.h> |
David Howells | 9f97da7 | 2012-03-28 18:30:01 +0100 | [diff] [blame] | 20 | #include <asm/compiler.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
Nicolas Pitre | afdd3bb | 2014-02-04 13:23:48 +0100 | [diff] [blame] | 22 | #ifndef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS |
Nicolas Pitre | c89efa7 | 2013-09-17 19:16:38 +0100 | [diff] [blame] | 23 | #include <asm-generic/uaccess-unaligned.h> |
| 24 | #else |
| 25 | #define __get_user_unaligned __get_user |
| 26 | #define __put_user_unaligned __put_user |
| 27 | #endif |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #define VERIFY_READ 0 |
| 30 | #define VERIFY_WRITE 1 |
| 31 | |
| 32 | /* |
| 33 | * The exception table consists of pairs of addresses: the first is the |
| 34 | * address of an instruction that is allowed to fault, and the second is |
| 35 | * the address at which the program should continue. No registers are |
| 36 | * modified, so it is entirely up to the continuation code to figure out |
| 37 | * what to do. |
| 38 | * |
| 39 | * All the routines below use bits of fixup code that are out of line |
| 40 | * with the main instruction path. This means when everything is well, |
| 41 | * we don't even have to jump over them. Further, they do not intrude |
| 42 | * on our cache or tlb entries. |
| 43 | */ |
| 44 | |
| 45 | struct exception_table_entry |
| 46 | { |
| 47 | unsigned long insn, fixup; |
| 48 | }; |
| 49 | |
| 50 | extern int fixup_exception(struct pt_regs *regs); |
| 51 | |
| 52 | /* |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 53 | * These two are intentionally not defined anywhere - if the kernel |
| 54 | * code generates any references to them, that's a bug. |
| 55 | */ |
| 56 | extern int __get_user_bad(void); |
| 57 | extern int __put_user_bad(void); |
| 58 | |
| 59 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | * Note that this is actually 0x1,0000,0000 |
| 61 | */ |
| 62 | #define KERNEL_DS 0x00000000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | #define get_ds() (KERNEL_DS) |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 64 | |
| 65 | #ifdef CONFIG_MMU |
| 66 | |
| 67 | #define USER_DS TASK_SIZE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | #define get_fs() (current_thread_info()->addr_limit) |
| 69 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 70 | static inline void set_fs(mm_segment_t fs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
| 72 | current_thread_info()->addr_limit = fs; |
| 73 | modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER); |
| 74 | } |
| 75 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 76 | #define segment_eq(a, b) ((a) == (b)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
| 78 | #define __addr_ok(addr) ({ \ |
| 79 | unsigned long flag; \ |
| 80 | __asm__("cmp %2, %0; movlo %0, #0" \ |
| 81 | : "=&r" (flag) \ |
| 82 | : "0" (current_thread_info()->addr_limit), "r" (addr) \ |
| 83 | : "cc"); \ |
| 84 | (flag == 0); }) |
| 85 | |
| 86 | /* We use 33-bit arithmetic here... */ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 87 | #define __range_ok(addr, size) ({ \ |
Tilman Schmidt | 16cf5b3 | 2007-02-10 01:45:41 -0800 | [diff] [blame] | 88 | unsigned long flag, roksum; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | __chk_user_ptr(addr); \ |
| 90 | __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \ |
Tilman Schmidt | 16cf5b3 | 2007-02-10 01:45:41 -0800 | [diff] [blame] | 91 | : "=&r" (flag), "=&r" (roksum) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \ |
| 93 | : "cc"); \ |
| 94 | flag; }) |
| 95 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | /* |
| 97 | * Single-value transfer routines. They automatically use the right |
| 98 | * size if we just have the right pointer type. Note that the functions |
| 99 | * which read from user space (*get_*) need to take care not to leak |
| 100 | * kernel data even if the calling code is buggy and fails to check |
| 101 | * the return value. This means zeroing out the destination variable |
| 102 | * or buffer on error. Normally this is done out of line by the |
| 103 | * fixup code, but there are a few places where it intrudes on the |
| 104 | * main code path. When we only write to user space, there is no |
| 105 | * problem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | extern int __get_user_1(void *); |
| 108 | extern int __get_user_2(void *); |
| 109 | extern int __get_user_4(void *); |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 110 | extern int __get_user_32t_8(void *); |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 111 | extern int __get_user_8(void *); |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 112 | extern int __get_user_64t_1(void *); |
| 113 | extern int __get_user_64t_2(void *); |
| 114 | extern int __get_user_64t_4(void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 116 | #define __GUP_CLOBBER_1 "lr", "cc" |
| 117 | #ifdef CONFIG_CPU_USE_DOMAINS |
| 118 | #define __GUP_CLOBBER_2 "ip", "lr", "cc" |
| 119 | #else |
| 120 | #define __GUP_CLOBBER_2 "lr", "cc" |
| 121 | #endif |
| 122 | #define __GUP_CLOBBER_4 "lr", "cc" |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 123 | #define __GUP_CLOBBER_32t_8 "lr", "cc" |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 124 | #define __GUP_CLOBBER_8 "lr", "cc" |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 125 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 126 | #define __get_user_x(__r2, __p, __e, __l, __s) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | __asm__ __volatile__ ( \ |
| 128 | __asmeq("%0", "r0") __asmeq("%1", "r2") \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 129 | __asmeq("%3", "r1") \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | "bl __get_user_" #__s \ |
| 131 | : "=&r" (__e), "=r" (__r2) \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 132 | : "0" (__p), "r" (__l) \ |
| 133 | : __GUP_CLOBBER_##__s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 135 | /* narrowing a double-word get into a single 32bit word register: */ |
| 136 | #ifdef __ARMEB__ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 137 | #define __get_user_x_32t(__r2, __p, __e, __l, __s) \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 138 | __get_user_x(__r2, __p, __e, __l, 32t_8) |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 139 | #else |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 140 | #define __get_user_x_32t __get_user_x |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 141 | #endif |
| 142 | |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 143 | /* |
| 144 | * storing result into proper least significant word of 64bit target var, |
| 145 | * different only for big endian case where 64 bit __r2 lsw is r3: |
| 146 | */ |
| 147 | #ifdef __ARMEB__ |
| 148 | #define __get_user_x_64t(__r2, __p, __e, __l, __s) \ |
| 149 | __asm__ __volatile__ ( \ |
| 150 | __asmeq("%0", "r0") __asmeq("%1", "r2") \ |
| 151 | __asmeq("%3", "r1") \ |
| 152 | "bl __get_user_64t_" #__s \ |
| 153 | : "=&r" (__e), "=r" (__r2) \ |
| 154 | : "0" (__p), "r" (__l) \ |
| 155 | : __GUP_CLOBBER_##__s) |
| 156 | #else |
| 157 | #define __get_user_x_64t __get_user_x |
| 158 | #endif |
| 159 | |
| 160 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 161 | #define __get_user_check(x, p) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | ({ \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 163 | unsigned long __limit = current_thread_info()->addr_limit - 1; \ |
Tobias Klauser | c5a69d5 | 2007-02-17 20:11:19 +0100 | [diff] [blame] | 164 | register const typeof(*(p)) __user *__p asm("r0") = (p);\ |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 165 | register typeof(x) __r2 asm("r2"); \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 166 | register unsigned long __l asm("r1") = __limit; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | register int __e asm("r0"); \ |
| 168 | switch (sizeof(*(__p))) { \ |
| 169 | case 1: \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 170 | if (sizeof((x)) >= 8) \ |
| 171 | __get_user_x_64t(__r2, __p, __e, __l, 1); \ |
| 172 | else \ |
| 173 | __get_user_x(__r2, __p, __e, __l, 1); \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 174 | break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | case 2: \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 176 | if (sizeof((x)) >= 8) \ |
| 177 | __get_user_x_64t(__r2, __p, __e, __l, 2); \ |
| 178 | else \ |
| 179 | __get_user_x(__r2, __p, __e, __l, 2); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | break; \ |
| 181 | case 4: \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 182 | if (sizeof((x)) >= 8) \ |
| 183 | __get_user_x_64t(__r2, __p, __e, __l, 4); \ |
| 184 | else \ |
| 185 | __get_user_x(__r2, __p, __e, __l, 4); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | break; \ |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 187 | case 8: \ |
| 188 | if (sizeof((x)) < 8) \ |
Victor Kamensky | d998138 | 2014-09-04 06:07:33 +0100 | [diff] [blame] | 189 | __get_user_x_32t(__r2, __p, __e, __l, 4); \ |
Daniel Thompson | e38361d | 2014-07-10 20:58:08 +0100 | [diff] [blame] | 190 | else \ |
| 191 | __get_user_x(__r2, __p, __e, __l, 8); \ |
| 192 | break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | default: __e = __get_user_bad(); break; \ |
| 194 | } \ |
Russell King | d2c5b69 | 2005-11-18 14:22:03 +0000 | [diff] [blame] | 195 | x = (typeof(*(p))) __r2; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | __e; \ |
| 197 | }) |
| 198 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 199 | #define get_user(x, p) \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 200 | ({ \ |
| 201 | might_fault(); \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 202 | __get_user_check(x, p); \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 203 | }) |
| 204 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 205 | extern int __put_user_1(void *, unsigned int); |
| 206 | extern int __put_user_2(void *, unsigned int); |
| 207 | extern int __put_user_4(void *, unsigned int); |
| 208 | extern int __put_user_8(void *, unsigned long long); |
| 209 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 210 | #define __put_user_x(__r2, __p, __e, __l, __s) \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 211 | __asm__ __volatile__ ( \ |
| 212 | __asmeq("%0", "r0") __asmeq("%2", "r2") \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 213 | __asmeq("%3", "r1") \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 214 | "bl __put_user_" #__s \ |
| 215 | : "=&r" (__e) \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 216 | : "0" (__p), "r" (__r2), "r" (__l) \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 217 | : "ip", "lr", "cc") |
| 218 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 219 | #define __put_user_check(x, p) \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 220 | ({ \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 221 | unsigned long __limit = current_thread_info()->addr_limit - 1; \ |
Andrey Ryabinin | 537094b6 | 2014-05-07 08:07:25 +0100 | [diff] [blame] | 222 | const typeof(*(p)) __user *__tmp_p = (p); \ |
Tobias Klauser | c5a69d5 | 2007-02-17 20:11:19 +0100 | [diff] [blame] | 223 | register const typeof(*(p)) __r2 asm("r2") = (x); \ |
Andrey Ryabinin | 537094b6 | 2014-05-07 08:07:25 +0100 | [diff] [blame] | 224 | register const typeof(*(p)) __user *__p asm("r0") = __tmp_p; \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 225 | register unsigned long __l asm("r1") = __limit; \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 226 | register int __e asm("r0"); \ |
| 227 | switch (sizeof(*(__p))) { \ |
| 228 | case 1: \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 229 | __put_user_x(__r2, __p, __e, __l, 1); \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 230 | break; \ |
| 231 | case 2: \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 232 | __put_user_x(__r2, __p, __e, __l, 2); \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 233 | break; \ |
| 234 | case 4: \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 235 | __put_user_x(__r2, __p, __e, __l, 4); \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 236 | break; \ |
| 237 | case 8: \ |
Russell King | 8404663 | 2012-09-07 18:22:28 +0100 | [diff] [blame] | 238 | __put_user_x(__r2, __p, __e, __l, 8); \ |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 239 | break; \ |
| 240 | default: __e = __put_user_bad(); break; \ |
| 241 | } \ |
| 242 | __e; \ |
| 243 | }) |
| 244 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 245 | #define put_user(x, p) \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 246 | ({ \ |
| 247 | might_fault(); \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 248 | __put_user_check(x, p); \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 249 | }) |
| 250 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 251 | #else /* CONFIG_MMU */ |
| 252 | |
| 253 | /* |
| 254 | * uClinux has only one addr space, so has simplified address limits. |
| 255 | */ |
| 256 | #define USER_DS KERNEL_DS |
| 257 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 258 | #define segment_eq(a, b) (1) |
| 259 | #define __addr_ok(addr) ((void)(addr), 1) |
| 260 | #define __range_ok(addr, size) ((void)(addr), 0) |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 261 | #define get_fs() (KERNEL_DS) |
| 262 | |
| 263 | static inline void set_fs(mm_segment_t fs) |
| 264 | { |
| 265 | } |
| 266 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 267 | #define get_user(x, p) __get_user(x, p) |
| 268 | #define put_user(x, p) __put_user(x, p) |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 269 | |
| 270 | #endif /* CONFIG_MMU */ |
| 271 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 272 | #define access_ok(type, addr, size) (__range_ok(addr, size) == 0) |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 273 | |
Will Deacon | 8c56cc8 | 2012-07-06 15:45:39 +0100 | [diff] [blame] | 274 | #define user_addr_max() \ |
Uwe Kleine-König | 83de911 | 2014-06-05 10:23:10 +0200 | [diff] [blame] | 275 | (segment_eq(get_fs(), KERNEL_DS) ? ~0UL : get_fs()) |
Will Deacon | 8c56cc8 | 2012-07-06 15:45:39 +0100 | [diff] [blame] | 276 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 277 | /* |
| 278 | * The "__xxx" versions of the user access functions do not verify the |
| 279 | * address space - it must have been done previously with a separate |
| 280 | * "access_ok()" call. |
| 281 | * |
| 282 | * The "xxx_error" versions set the third argument to EFAULT if an |
| 283 | * error occurs, and leave it unchanged on success. Note that these |
| 284 | * versions are void (ie, don't return a value as such). |
| 285 | */ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 286 | #define __get_user(x, ptr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | ({ \ |
| 288 | long __gu_err = 0; \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 289 | __get_user_err((x), (ptr), __gu_err); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | __gu_err; \ |
| 291 | }) |
| 292 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 293 | #define __get_user_error(x, ptr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | ({ \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 295 | __get_user_err((x), (ptr), err); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | (void) 0; \ |
| 297 | }) |
| 298 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 299 | #define __get_user_err(x, ptr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | do { \ |
| 301 | unsigned long __gu_addr = (unsigned long)(ptr); \ |
| 302 | unsigned long __gu_val; \ |
| 303 | __chk_user_ptr(ptr); \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 304 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | switch (sizeof(*(ptr))) { \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 306 | case 1: __get_user_asm_byte(__gu_val, __gu_addr, err); break; \ |
| 307 | case 2: __get_user_asm_half(__gu_val, __gu_addr, err); break; \ |
| 308 | case 4: __get_user_asm_word(__gu_val, __gu_addr, err); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | default: (__gu_val) = __get_user_bad(); \ |
| 310 | } \ |
| 311 | (x) = (__typeof__(*(ptr)))__gu_val; \ |
| 312 | } while (0) |
| 313 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 314 | #define __get_user_asm_byte(x, addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | __asm__ __volatile__( \ |
Catalin Marinas | 4e7682d | 2012-01-25 11:38:13 +0100 | [diff] [blame] | 316 | "1: " TUSER(ldrb) " %1,[%2],#0\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | "2:\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 318 | " .pushsection .fixup,\"ax\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 319 | " .align 2\n" \ |
| 320 | "3: mov %0, %3\n" \ |
| 321 | " mov %1, #0\n" \ |
| 322 | " b 2b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 323 | " .popsection\n" \ |
| 324 | " .pushsection __ex_table,\"a\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | " .align 3\n" \ |
| 326 | " .long 1b, 3b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 327 | " .popsection" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | : "+r" (err), "=&r" (x) \ |
| 329 | : "r" (addr), "i" (-EFAULT) \ |
| 330 | : "cc") |
| 331 | |
| 332 | #ifndef __ARMEB__ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 333 | #define __get_user_asm_half(x, __gu_addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | ({ \ |
| 335 | unsigned long __b1, __b2; \ |
| 336 | __get_user_asm_byte(__b1, __gu_addr, err); \ |
| 337 | __get_user_asm_byte(__b2, __gu_addr + 1, err); \ |
| 338 | (x) = __b1 | (__b2 << 8); \ |
| 339 | }) |
| 340 | #else |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 341 | #define __get_user_asm_half(x, __gu_addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | ({ \ |
| 343 | unsigned long __b1, __b2; \ |
| 344 | __get_user_asm_byte(__b1, __gu_addr, err); \ |
| 345 | __get_user_asm_byte(__b2, __gu_addr + 1, err); \ |
| 346 | (x) = (__b1 << 8) | __b2; \ |
| 347 | }) |
| 348 | #endif |
| 349 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 350 | #define __get_user_asm_word(x, addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | __asm__ __volatile__( \ |
Catalin Marinas | 4e7682d | 2012-01-25 11:38:13 +0100 | [diff] [blame] | 352 | "1: " TUSER(ldr) " %1,[%2],#0\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | "2:\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 354 | " .pushsection .fixup,\"ax\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | " .align 2\n" \ |
| 356 | "3: mov %0, %3\n" \ |
| 357 | " mov %1, #0\n" \ |
| 358 | " b 2b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 359 | " .popsection\n" \ |
| 360 | " .pushsection __ex_table,\"a\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | " .align 3\n" \ |
| 362 | " .long 1b, 3b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 363 | " .popsection" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | : "+r" (err), "=&r" (x) \ |
| 365 | : "r" (addr), "i" (-EFAULT) \ |
| 366 | : "cc") |
| 367 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 368 | #define __put_user(x, ptr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | ({ \ |
| 370 | long __pu_err = 0; \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 371 | __put_user_err((x), (ptr), __pu_err); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | __pu_err; \ |
| 373 | }) |
| 374 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 375 | #define __put_user_error(x, ptr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 376 | ({ \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 377 | __put_user_err((x), (ptr), err); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | (void) 0; \ |
| 379 | }) |
| 380 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 381 | #define __put_user_err(x, ptr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | do { \ |
| 383 | unsigned long __pu_addr = (unsigned long)(ptr); \ |
| 384 | __typeof__(*(ptr)) __pu_val = (x); \ |
| 385 | __chk_user_ptr(ptr); \ |
Will Deacon | ad72907 | 2012-09-07 18:24:10 +0100 | [diff] [blame] | 386 | might_fault(); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | switch (sizeof(*(ptr))) { \ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 388 | case 1: __put_user_asm_byte(__pu_val, __pu_addr, err); break; \ |
| 389 | case 2: __put_user_asm_half(__pu_val, __pu_addr, err); break; \ |
| 390 | case 4: __put_user_asm_word(__pu_val, __pu_addr, err); break; \ |
| 391 | case 8: __put_user_asm_dword(__pu_val, __pu_addr, err); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | default: __put_user_bad(); \ |
| 393 | } \ |
| 394 | } while (0) |
| 395 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 396 | #define __put_user_asm_byte(x, __pu_addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | __asm__ __volatile__( \ |
Catalin Marinas | 4e7682d | 2012-01-25 11:38:13 +0100 | [diff] [blame] | 398 | "1: " TUSER(strb) " %1,[%2],#0\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | "2:\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 400 | " .pushsection .fixup,\"ax\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | " .align 2\n" \ |
| 402 | "3: mov %0, %3\n" \ |
| 403 | " b 2b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 404 | " .popsection\n" \ |
| 405 | " .pushsection __ex_table,\"a\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 406 | " .align 3\n" \ |
| 407 | " .long 1b, 3b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 408 | " .popsection" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | : "+r" (err) \ |
| 410 | : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \ |
| 411 | : "cc") |
| 412 | |
| 413 | #ifndef __ARMEB__ |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 414 | #define __put_user_asm_half(x, __pu_addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | ({ \ |
Michael S. Tsirkin | e8b94de | 2015-01-06 14:37:22 +0200 | [diff] [blame] | 416 | unsigned long __temp = (__force unsigned long)(x); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | __put_user_asm_byte(__temp, __pu_addr, err); \ |
| 418 | __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \ |
| 419 | }) |
| 420 | #else |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 421 | #define __put_user_asm_half(x, __pu_addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | ({ \ |
Michael S. Tsirkin | e8b94de | 2015-01-06 14:37:22 +0200 | [diff] [blame] | 423 | unsigned long __temp = (__force unsigned long)(x); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | __put_user_asm_byte(__temp >> 8, __pu_addr, err); \ |
| 425 | __put_user_asm_byte(__temp, __pu_addr + 1, err); \ |
| 426 | }) |
| 427 | #endif |
| 428 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 429 | #define __put_user_asm_word(x, __pu_addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | __asm__ __volatile__( \ |
Catalin Marinas | 4e7682d | 2012-01-25 11:38:13 +0100 | [diff] [blame] | 431 | "1: " TUSER(str) " %1,[%2],#0\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | "2:\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 433 | " .pushsection .fixup,\"ax\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | " .align 2\n" \ |
| 435 | "3: mov %0, %3\n" \ |
| 436 | " b 2b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 437 | " .popsection\n" \ |
| 438 | " .pushsection __ex_table,\"a\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | " .align 3\n" \ |
| 440 | " .long 1b, 3b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 441 | " .popsection" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | : "+r" (err) \ |
| 443 | : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \ |
| 444 | : "cc") |
| 445 | |
| 446 | #ifndef __ARMEB__ |
| 447 | #define __reg_oper0 "%R2" |
| 448 | #define __reg_oper1 "%Q2" |
| 449 | #else |
| 450 | #define __reg_oper0 "%Q2" |
| 451 | #define __reg_oper1 "%R2" |
| 452 | #endif |
| 453 | |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 454 | #define __put_user_asm_dword(x, __pu_addr, err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | __asm__ __volatile__( \ |
Catalin Marinas | 4e7682d | 2012-01-25 11:38:13 +0100 | [diff] [blame] | 456 | ARM( "1: " TUSER(str) " " __reg_oper1 ", [%1], #4\n" ) \ |
| 457 | ARM( "2: " TUSER(str) " " __reg_oper0 ", [%1]\n" ) \ |
| 458 | THUMB( "1: " TUSER(str) " " __reg_oper1 ", [%1]\n" ) \ |
| 459 | THUMB( "2: " TUSER(str) " " __reg_oper0 ", [%1, #4]\n" ) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | "3:\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 461 | " .pushsection .fixup,\"ax\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | " .align 2\n" \ |
| 463 | "4: mov %0, %3\n" \ |
| 464 | " b 3b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 465 | " .popsection\n" \ |
| 466 | " .pushsection __ex_table,\"a\"\n" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | " .align 3\n" \ |
| 468 | " .long 1b, 4b\n" \ |
| 469 | " .long 2b, 4b\n" \ |
Russell King | 4260415 | 2010-04-19 10:15:03 +0100 | [diff] [blame] | 470 | " .popsection" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | : "+r" (err), "+r" (__pu_addr) \ |
| 472 | : "r" (x), "i" (-EFAULT) \ |
| 473 | : "cc") |
| 474 | |
Russell King | 02fcb97 | 2006-06-21 14:44:52 +0100 | [diff] [blame] | 475 | |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 476 | #ifdef CONFIG_MMU |
Russell King | 9957329 | 2006-10-26 10:27:42 +0100 | [diff] [blame] | 477 | extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n); |
| 478 | extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n); |
Nicolas Pitre | a1f9884 | 2009-03-08 22:34:45 -0400 | [diff] [blame] | 479 | extern unsigned long __must_check __copy_to_user_std(void __user *to, const void *from, unsigned long n); |
Russell King | 9957329 | 2006-10-26 10:27:42 +0100 | [diff] [blame] | 480 | extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n); |
Nicolas Pitre | a1f9884 | 2009-03-08 22:34:45 -0400 | [diff] [blame] | 481 | extern unsigned long __must_check __clear_user_std(void __user *addr, unsigned long n); |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 482 | #else |
Michael S. Tsirkin | 295bb01 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 483 | #define __copy_from_user(to, from, n) (memcpy(to, (void __force *)from, n), 0) |
| 484 | #define __copy_to_user(to, from, n) (memcpy((void __force *)to, from, n), 0) |
| 485 | #define __clear_user(addr, n) (memset((void __force *)addr, 0, n), 0) |
Russell King | 9641c7c | 2006-06-21 20:38:17 +0100 | [diff] [blame] | 486 | #endif |
| 487 | |
Russell King | 9957329 | 2006-10-26 10:27:42 +0100 | [diff] [blame] | 488 | static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | { |
| 490 | if (access_ok(VERIFY_READ, from, n)) |
Russell King | 02fcb97 | 2006-06-21 14:44:52 +0100 | [diff] [blame] | 491 | n = __copy_from_user(to, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | else /* security hole - plug it */ |
Russell King | 59f0cb0 | 2008-10-27 11:24:09 +0000 | [diff] [blame] | 493 | memset(to, 0, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | return n; |
| 495 | } |
| 496 | |
Russell King | 9957329 | 2006-10-26 10:27:42 +0100 | [diff] [blame] | 497 | static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | { |
| 499 | if (access_ok(VERIFY_WRITE, to, n)) |
Russell King | 02fcb97 | 2006-06-21 14:44:52 +0100 | [diff] [blame] | 500 | n = __copy_to_user(to, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | return n; |
| 502 | } |
| 503 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | #define __copy_to_user_inatomic __copy_to_user |
| 505 | #define __copy_from_user_inatomic __copy_from_user |
| 506 | |
Russell King | 9957329 | 2006-10-26 10:27:42 +0100 | [diff] [blame] | 507 | 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] | 508 | { |
| 509 | if (access_ok(VERIFY_WRITE, to, n)) |
Russell King | 02fcb97 | 2006-06-21 14:44:52 +0100 | [diff] [blame] | 510 | n = __clear_user(to, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | return n; |
| 512 | } |
| 513 | |
Will Deacon | 8c56cc8 | 2012-07-06 15:45:39 +0100 | [diff] [blame] | 514 | extern long strncpy_from_user(char *dest, const char __user *src, long count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | |
Will Deacon | 8c56cc8 | 2012-07-06 15:45:39 +0100 | [diff] [blame] | 516 | extern __must_check long strlen_user(const char __user *str); |
| 517 | extern __must_check long strnlen_user(const char __user *str, long n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | |
| 519 | #endif /* _ASMARM_UACCESS_H */ |