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