H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 1 | #ifndef _ASM_X86_UACCESS_H |
| 2 | #define _ASM_X86_UACCESS_H |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 3 | /* |
| 4 | * User space memory access functions |
| 5 | */ |
| 6 | #include <linux/errno.h> |
| 7 | #include <linux/compiler.h> |
| 8 | #include <linux/thread_info.h> |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 9 | #include <linux/string.h> |
| 10 | #include <asm/asm.h> |
| 11 | #include <asm/page.h> |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 12 | #include <asm/smap.h> |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 13 | |
| 14 | #define VERIFY_READ 0 |
| 15 | #define VERIFY_WRITE 1 |
| 16 | |
| 17 | /* |
| 18 | * The fs value determines whether argument validity checking should be |
| 19 | * performed or not. If get_fs() == USER_DS, checking is performed, with |
| 20 | * get_fs() == KERNEL_DS, checking is bypassed. |
| 21 | * |
| 22 | * For historical reasons, these macros are grossly misnamed. |
| 23 | */ |
| 24 | |
| 25 | #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) |
| 26 | |
| 27 | #define KERNEL_DS MAKE_MM_SEG(-1UL) |
Linus Torvalds | 9063c61 | 2009-06-20 15:40:00 -0700 | [diff] [blame] | 28 | #define USER_DS MAKE_MM_SEG(TASK_SIZE_MAX) |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 29 | |
| 30 | #define get_ds() (KERNEL_DS) |
| 31 | #define get_fs() (current_thread_info()->addr_limit) |
| 32 | #define set_fs(x) (current_thread_info()->addr_limit = (x)) |
| 33 | |
| 34 | #define segment_eq(a, b) ((a).seg == (b).seg) |
| 35 | |
Linus Torvalds | 4ae73f2 | 2012-05-26 10:14:39 -0700 | [diff] [blame] | 36 | #define user_addr_max() (current_thread_info()->addr_limit.seg) |
Arun Sharma | bc6ca7b | 2012-04-20 15:41:35 -0700 | [diff] [blame] | 37 | #define __addr_ok(addr) \ |
| 38 | ((unsigned long __force)(addr) < user_addr_max()) |
Glauber Costa | 002ca16 | 2008-06-25 11:08:51 -0300 | [diff] [blame] | 39 | |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 40 | /* |
| 41 | * Test whether a block of memory is a valid user space address. |
| 42 | * Returns 0 if the range is valid, nonzero otherwise. |
| 43 | * |
| 44 | * This is equivalent to the following test: |
Jiri Olsa | 26afb7c | 2011-05-12 16:30:30 +0200 | [diff] [blame] | 45 | * (u33)addr + (u33)size > (u33)current->addr_limit.seg (u65 for x86_64) |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 46 | * |
| 47 | * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry... |
| 48 | */ |
| 49 | |
Arun Sharma | bc6ca7b | 2012-04-20 15:41:35 -0700 | [diff] [blame] | 50 | #define __range_not_ok(addr, size, limit) \ |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 51 | ({ \ |
| 52 | unsigned long flag, roksum; \ |
| 53 | __chk_user_ptr(addr); \ |
| 54 | asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0" \ |
| 55 | : "=&r" (flag), "=r" (roksum) \ |
| 56 | : "1" (addr), "g" ((long)(size)), \ |
Arun Sharma | bc6ca7b | 2012-04-20 15:41:35 -0700 | [diff] [blame] | 57 | "rm" (limit)); \ |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 58 | flag; \ |
| 59 | }) |
| 60 | |
| 61 | /** |
| 62 | * access_ok: - Checks if a user space pointer is valid |
| 63 | * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that |
| 64 | * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe |
| 65 | * to write to a block, it is always safe to read from it. |
| 66 | * @addr: User space pointer to start of block to check |
| 67 | * @size: Size of block to check |
| 68 | * |
| 69 | * Context: User context only. This function may sleep. |
| 70 | * |
| 71 | * Checks if a pointer to a block of memory in user space is valid. |
| 72 | * |
| 73 | * Returns true (nonzero) if the memory block may be valid, false (zero) |
| 74 | * if it is definitely invalid. |
| 75 | * |
| 76 | * Note that, depending on architecture, this function probably just |
| 77 | * checks that the pointer is in the user space range - after calling |
| 78 | * this function, memory access functions may still return -EFAULT. |
| 79 | */ |
Arun Sharma | bc6ca7b | 2012-04-20 15:41:35 -0700 | [diff] [blame] | 80 | #define access_ok(type, addr, size) \ |
| 81 | (likely(__range_not_ok(addr, size, user_addr_max()) == 0)) |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 82 | |
| 83 | /* |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 84 | * The exception table consists of pairs of addresses relative to the |
| 85 | * exception table enty itself: the first is the address of an |
| 86 | * instruction that is allowed to fault, and the second is the address |
| 87 | * at which the program should continue. No registers are modified, |
| 88 | * so it is entirely up to the continuation code to figure out what to |
| 89 | * do. |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 90 | * |
| 91 | * All the routines below use bits of fixup code that are out of line |
| 92 | * with the main instruction path. This means when everything is well, |
| 93 | * we don't even have to jump over them. Further, they do not intrude |
| 94 | * on our cache or tlb entries. |
| 95 | */ |
| 96 | |
| 97 | struct exception_table_entry { |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 98 | int insn, fixup; |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 99 | }; |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 100 | /* This is not the generic standard exception_table_entry format */ |
| 101 | #define ARCH_HAS_SORT_EXTABLE |
| 102 | #define ARCH_HAS_SEARCH_EXTABLE |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 103 | |
| 104 | extern int fixup_exception(struct pt_regs *regs); |
H. Peter Anvin | 7062765 | 2012-04-20 17:12:48 -0700 | [diff] [blame] | 105 | extern int early_fixup_exception(unsigned long *ip); |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 106 | |
| 107 | /* |
| 108 | * These are the main single-value transfer routines. They automatically |
| 109 | * use the right size if we just have the right pointer type. |
| 110 | * |
| 111 | * This gets kind of ugly. We want to return _two_ values in "get_user()" |
| 112 | * and yet we don't want to do any pointers, because that is too much |
| 113 | * of a performance impact. Thus we have a few rather ugly macros here, |
| 114 | * and hide all the ugliness from the user. |
| 115 | * |
| 116 | * The "__xxx" versions of the user access functions are versions that |
| 117 | * do not verify the address space, that must have been done previously |
| 118 | * with a separate "access_ok()" call (this is used when we do multiple |
| 119 | * accesses to the same area of user memory). |
| 120 | */ |
| 121 | |
| 122 | extern int __get_user_1(void); |
| 123 | extern int __get_user_2(void); |
| 124 | extern int __get_user_4(void); |
| 125 | extern int __get_user_8(void); |
| 126 | extern int __get_user_bad(void); |
| 127 | |
H. Peter Anvin | 3578baa | 2013-02-12 11:47:31 -0800 | [diff] [blame] | 128 | /* |
| 129 | * This is a type: either unsigned long, if the argument fits into |
| 130 | * that type, or otherwise unsigned long long. |
| 131 | */ |
| 132 | #define __inttype(x) \ |
| 133 | __typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL)) |
Glauber Costa | 865e5b7 | 2008-06-25 11:05:11 -0300 | [diff] [blame] | 134 | |
| 135 | /** |
| 136 | * get_user: - Get a simple variable from user space. |
| 137 | * @x: Variable to store result. |
| 138 | * @ptr: Source address, in user space. |
| 139 | * |
| 140 | * Context: User context only. This function may sleep. |
| 141 | * |
| 142 | * This macro copies a single simple variable from user space to kernel |
| 143 | * space. It supports simple types like char and int, but not larger |
| 144 | * data types like structures or arrays. |
| 145 | * |
| 146 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 147 | * dereferencing @ptr must be assignable to @x without a cast. |
| 148 | * |
| 149 | * Returns zero on success, or -EFAULT on error. |
| 150 | * On error, the variable @x is set to zero. |
H. Peter Anvin | ff52c3b | 2013-02-12 15:37:02 -0800 | [diff] [blame] | 151 | */ |
| 152 | /* |
H. Peter Anvin | 3578baa | 2013-02-12 11:47:31 -0800 | [diff] [blame] | 153 | * Careful: we have to cast the result to the type of the pointer |
| 154 | * for sign reasons. |
H. Peter Anvin | ff52c3b | 2013-02-12 15:37:02 -0800 | [diff] [blame] | 155 | * |
H. Peter Anvin | f69fa9a | 2013-08-29 13:34:50 -0700 | [diff] [blame^] | 156 | * The use of _ASM_DX as the register specifier is a bit of a |
H. Peter Anvin | ff52c3b | 2013-02-12 15:37:02 -0800 | [diff] [blame] | 157 | * simplification, as gcc only cares about it as the starting point |
| 158 | * and not size: for a 64-bit value it will use %ecx:%edx on 32 bits |
| 159 | * (%ecx being the next register in gcc's x86 register sequence), and |
| 160 | * %rdx on 64 bits. |
H. Peter Anvin | f69fa9a | 2013-08-29 13:34:50 -0700 | [diff] [blame^] | 161 | * |
| 162 | * Clang/LLVM cares about the size of the register, but still wants |
| 163 | * the base register for something that ends up being a pair. |
Glauber Costa | 865e5b7 | 2008-06-25 11:05:11 -0300 | [diff] [blame] | 164 | */ |
Glauber Costa | 865e5b7 | 2008-06-25 11:05:11 -0300 | [diff] [blame] | 165 | #define get_user(x, ptr) \ |
| 166 | ({ \ |
| 167 | int __ret_gu; \ |
Jan-Simon Möller | bdfc017 | 2013-08-29 21:13:05 +0200 | [diff] [blame] | 168 | register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); \ |
Glauber Costa | 865e5b7 | 2008-06-25 11:05:11 -0300 | [diff] [blame] | 169 | __chk_user_ptr(ptr); \ |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 170 | might_fault(); \ |
H. Peter Anvin | 3578baa | 2013-02-12 11:47:31 -0800 | [diff] [blame] | 171 | asm volatile("call __get_user_%P3" \ |
| 172 | : "=a" (__ret_gu), "=r" (__val_gu) \ |
| 173 | : "0" (ptr), "i" (sizeof(*(ptr)))); \ |
| 174 | (x) = (__typeof__(*(ptr))) __val_gu; \ |
Glauber Costa | 865e5b7 | 2008-06-25 11:05:11 -0300 | [diff] [blame] | 175 | __ret_gu; \ |
| 176 | }) |
| 177 | |
Glauber Costa | e30a44f | 2008-06-25 13:17:43 -0300 | [diff] [blame] | 178 | #define __put_user_x(size, x, ptr, __ret_pu) \ |
| 179 | asm volatile("call __put_user_" #size : "=a" (__ret_pu) \ |
Hiroshi Shimamoto | 4d5d783 | 2009-01-19 16:34:26 -0800 | [diff] [blame] | 180 | : "0" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx") |
Glauber Costa | e30a44f | 2008-06-25 13:17:43 -0300 | [diff] [blame] | 181 | |
| 182 | |
| 183 | |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 184 | #ifdef CONFIG_X86_32 |
Hiroshi Shimamoto | 18114f6 | 2009-01-30 18:16:46 -0800 | [diff] [blame] | 185 | #define __put_user_asm_u64(x, addr, err, errret) \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 186 | asm volatile(ASM_STAC "\n" \ |
| 187 | "1: movl %%eax,0(%2)\n" \ |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 188 | "2: movl %%edx,4(%2)\n" \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 189 | "3: " ASM_CLAC "\n" \ |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 190 | ".section .fixup,\"ax\"\n" \ |
| 191 | "4: movl %3,%0\n" \ |
| 192 | " jmp 3b\n" \ |
| 193 | ".previous\n" \ |
| 194 | _ASM_EXTABLE(1b, 4b) \ |
| 195 | _ASM_EXTABLE(2b, 4b) \ |
| 196 | : "=r" (err) \ |
Hiroshi Shimamoto | 18114f6 | 2009-01-30 18:16:46 -0800 | [diff] [blame] | 197 | : "A" (x), "r" (addr), "i" (errret), "0" (err)) |
Glauber Costa | e30a44f | 2008-06-25 13:17:43 -0300 | [diff] [blame] | 198 | |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 199 | #define __put_user_asm_ex_u64(x, addr) \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 200 | asm volatile(ASM_STAC "\n" \ |
| 201 | "1: movl %%eax,0(%1)\n" \ |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 202 | "2: movl %%edx,4(%1)\n" \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 203 | "3: " ASM_CLAC "\n" \ |
H. Peter Anvin | 535c0c3 | 2012-04-20 16:57:35 -0700 | [diff] [blame] | 204 | _ASM_EXTABLE_EX(1b, 2b) \ |
| 205 | _ASM_EXTABLE_EX(2b, 3b) \ |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 206 | : : "A" (x), "r" (addr)) |
| 207 | |
Glauber Costa | e30a44f | 2008-06-25 13:17:43 -0300 | [diff] [blame] | 208 | #define __put_user_x8(x, ptr, __ret_pu) \ |
| 209 | asm volatile("call __put_user_8" : "=a" (__ret_pu) \ |
| 210 | : "A" ((typeof(*(ptr)))(x)), "c" (ptr) : "ebx") |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 211 | #else |
Hiroshi Shimamoto | 18114f6 | 2009-01-30 18:16:46 -0800 | [diff] [blame] | 212 | #define __put_user_asm_u64(x, ptr, retval, errret) \ |
H. Peter Anvin | ebe119c | 2009-07-20 23:27:39 -0700 | [diff] [blame] | 213 | __put_user_asm(x, ptr, retval, "q", "", "er", errret) |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 214 | #define __put_user_asm_ex_u64(x, addr) \ |
H. Peter Anvin | ebe119c | 2009-07-20 23:27:39 -0700 | [diff] [blame] | 215 | __put_user_asm_ex(x, addr, "q", "", "er") |
Glauber Costa | e30a44f | 2008-06-25 13:17:43 -0300 | [diff] [blame] | 216 | #define __put_user_x8(x, ptr, __ret_pu) __put_user_x(8, x, ptr, __ret_pu) |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 217 | #endif |
| 218 | |
Glauber Costa | e30a44f | 2008-06-25 13:17:43 -0300 | [diff] [blame] | 219 | extern void __put_user_bad(void); |
| 220 | |
| 221 | /* |
| 222 | * Strange magic calling convention: pointer in %ecx, |
| 223 | * value in %eax(:%edx), return value in %eax. clobbers %rbx |
| 224 | */ |
| 225 | extern void __put_user_1(void); |
| 226 | extern void __put_user_2(void); |
| 227 | extern void __put_user_4(void); |
| 228 | extern void __put_user_8(void); |
| 229 | |
Glauber Costa | e30a44f | 2008-06-25 13:17:43 -0300 | [diff] [blame] | 230 | /** |
| 231 | * put_user: - Write a simple value into user space. |
| 232 | * @x: Value to copy to user space. |
| 233 | * @ptr: Destination address, in user space. |
| 234 | * |
| 235 | * Context: User context only. This function may sleep. |
| 236 | * |
| 237 | * This macro copies a single simple value from kernel space to user |
| 238 | * space. It supports simple types like char and int, but not larger |
| 239 | * data types like structures or arrays. |
| 240 | * |
| 241 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 242 | * to the result of dereferencing @ptr. |
| 243 | * |
| 244 | * Returns zero on success, or -EFAULT on error. |
| 245 | */ |
| 246 | #define put_user(x, ptr) \ |
| 247 | ({ \ |
| 248 | int __ret_pu; \ |
| 249 | __typeof__(*(ptr)) __pu_val; \ |
| 250 | __chk_user_ptr(ptr); \ |
Ingo Molnar | d1a7618 | 2008-10-28 16:54:49 +0100 | [diff] [blame] | 251 | might_fault(); \ |
Glauber Costa | e30a44f | 2008-06-25 13:17:43 -0300 | [diff] [blame] | 252 | __pu_val = x; \ |
| 253 | switch (sizeof(*(ptr))) { \ |
| 254 | case 1: \ |
| 255 | __put_user_x(1, __pu_val, ptr, __ret_pu); \ |
| 256 | break; \ |
| 257 | case 2: \ |
| 258 | __put_user_x(2, __pu_val, ptr, __ret_pu); \ |
| 259 | break; \ |
| 260 | case 4: \ |
| 261 | __put_user_x(4, __pu_val, ptr, __ret_pu); \ |
| 262 | break; \ |
| 263 | case 8: \ |
| 264 | __put_user_x8(__pu_val, ptr, __ret_pu); \ |
| 265 | break; \ |
| 266 | default: \ |
| 267 | __put_user_x(X, __pu_val, ptr, __ret_pu); \ |
| 268 | break; \ |
| 269 | } \ |
| 270 | __ret_pu; \ |
| 271 | }) |
| 272 | |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 273 | #define __put_user_size(x, ptr, size, retval, errret) \ |
| 274 | do { \ |
| 275 | retval = 0; \ |
| 276 | __chk_user_ptr(ptr); \ |
| 277 | switch (size) { \ |
| 278 | case 1: \ |
| 279 | __put_user_asm(x, ptr, retval, "b", "b", "iq", errret); \ |
| 280 | break; \ |
| 281 | case 2: \ |
| 282 | __put_user_asm(x, ptr, retval, "w", "w", "ir", errret); \ |
| 283 | break; \ |
| 284 | case 4: \ |
Hiroshi Shimamoto | 4d5d783 | 2009-01-19 16:34:26 -0800 | [diff] [blame] | 285 | __put_user_asm(x, ptr, retval, "l", "k", "ir", errret); \ |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 286 | break; \ |
| 287 | case 8: \ |
Hiroshi Shimamoto | 18114f6 | 2009-01-30 18:16:46 -0800 | [diff] [blame] | 288 | __put_user_asm_u64((__typeof__(*ptr))(x), ptr, retval, \ |
| 289 | errret); \ |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 290 | break; \ |
| 291 | default: \ |
| 292 | __put_user_bad(); \ |
| 293 | } \ |
| 294 | } while (0) |
| 295 | |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 296 | #define __put_user_size_ex(x, ptr, size) \ |
| 297 | do { \ |
| 298 | __chk_user_ptr(ptr); \ |
| 299 | switch (size) { \ |
| 300 | case 1: \ |
| 301 | __put_user_asm_ex(x, ptr, "b", "b", "iq"); \ |
| 302 | break; \ |
| 303 | case 2: \ |
| 304 | __put_user_asm_ex(x, ptr, "w", "w", "ir"); \ |
| 305 | break; \ |
| 306 | case 4: \ |
| 307 | __put_user_asm_ex(x, ptr, "l", "k", "ir"); \ |
| 308 | break; \ |
| 309 | case 8: \ |
| 310 | __put_user_asm_ex_u64((__typeof__(*ptr))(x), ptr); \ |
| 311 | break; \ |
| 312 | default: \ |
| 313 | __put_user_bad(); \ |
| 314 | } \ |
| 315 | } while (0) |
| 316 | |
Glauber Costa | 3f16822 | 2008-06-25 12:48:47 -0300 | [diff] [blame] | 317 | #ifdef CONFIG_X86_32 |
| 318 | #define __get_user_asm_u64(x, ptr, retval, errret) (x) = __get_user_bad() |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 319 | #define __get_user_asm_ex_u64(x, ptr) (x) = __get_user_bad() |
Glauber Costa | 3f16822 | 2008-06-25 12:48:47 -0300 | [diff] [blame] | 320 | #else |
| 321 | #define __get_user_asm_u64(x, ptr, retval, errret) \ |
| 322 | __get_user_asm(x, ptr, retval, "q", "", "=r", errret) |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 323 | #define __get_user_asm_ex_u64(x, ptr) \ |
| 324 | __get_user_asm_ex(x, ptr, "q", "", "=r") |
Glauber Costa | 3f16822 | 2008-06-25 12:48:47 -0300 | [diff] [blame] | 325 | #endif |
| 326 | |
| 327 | #define __get_user_size(x, ptr, size, retval, errret) \ |
| 328 | do { \ |
| 329 | retval = 0; \ |
| 330 | __chk_user_ptr(ptr); \ |
| 331 | switch (size) { \ |
| 332 | case 1: \ |
| 333 | __get_user_asm(x, ptr, retval, "b", "b", "=q", errret); \ |
| 334 | break; \ |
| 335 | case 2: \ |
| 336 | __get_user_asm(x, ptr, retval, "w", "w", "=r", errret); \ |
| 337 | break; \ |
| 338 | case 4: \ |
| 339 | __get_user_asm(x, ptr, retval, "l", "k", "=r", errret); \ |
| 340 | break; \ |
| 341 | case 8: \ |
| 342 | __get_user_asm_u64(x, ptr, retval, errret); \ |
| 343 | break; \ |
| 344 | default: \ |
| 345 | (x) = __get_user_bad(); \ |
| 346 | } \ |
| 347 | } while (0) |
| 348 | |
| 349 | #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret) \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 350 | asm volatile(ASM_STAC "\n" \ |
| 351 | "1: mov"itype" %2,%"rtype"1\n" \ |
| 352 | "2: " ASM_CLAC "\n" \ |
Glauber Costa | 3f16822 | 2008-06-25 12:48:47 -0300 | [diff] [blame] | 353 | ".section .fixup,\"ax\"\n" \ |
| 354 | "3: mov %3,%0\n" \ |
| 355 | " xor"itype" %"rtype"1,%"rtype"1\n" \ |
| 356 | " jmp 2b\n" \ |
| 357 | ".previous\n" \ |
| 358 | _ASM_EXTABLE(1b, 3b) \ |
| 359 | : "=r" (err), ltype(x) \ |
| 360 | : "m" (__m(addr)), "i" (errret), "0" (err)) |
| 361 | |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 362 | #define __get_user_size_ex(x, ptr, size) \ |
| 363 | do { \ |
| 364 | __chk_user_ptr(ptr); \ |
| 365 | switch (size) { \ |
| 366 | case 1: \ |
| 367 | __get_user_asm_ex(x, ptr, "b", "b", "=q"); \ |
| 368 | break; \ |
| 369 | case 2: \ |
| 370 | __get_user_asm_ex(x, ptr, "w", "w", "=r"); \ |
| 371 | break; \ |
| 372 | case 4: \ |
| 373 | __get_user_asm_ex(x, ptr, "l", "k", "=r"); \ |
| 374 | break; \ |
| 375 | case 8: \ |
| 376 | __get_user_asm_ex_u64(x, ptr); \ |
| 377 | break; \ |
| 378 | default: \ |
| 379 | (x) = __get_user_bad(); \ |
| 380 | } \ |
| 381 | } while (0) |
| 382 | |
| 383 | #define __get_user_asm_ex(x, addr, itype, rtype, ltype) \ |
H. Peter Anvin | 5e88353 | 2012-09-21 12:43:15 -0700 | [diff] [blame] | 384 | asm volatile("1: mov"itype" %1,%"rtype"0\n" \ |
| 385 | "2:\n" \ |
H. Peter Anvin | 535c0c3 | 2012-04-20 16:57:35 -0700 | [diff] [blame] | 386 | _ASM_EXTABLE_EX(1b, 2b) \ |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 387 | : ltype(x) : "m" (__m(addr))) |
| 388 | |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 389 | #define __put_user_nocheck(x, ptr, size) \ |
| 390 | ({ \ |
Hiroshi Shimamoto | 16855f8 | 2008-12-08 19:18:38 -0800 | [diff] [blame] | 391 | int __pu_err; \ |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 392 | __put_user_size((x), (ptr), (size), __pu_err, -EFAULT); \ |
| 393 | __pu_err; \ |
| 394 | }) |
| 395 | |
Glauber Costa | 3f16822 | 2008-06-25 12:48:47 -0300 | [diff] [blame] | 396 | #define __get_user_nocheck(x, ptr, size) \ |
| 397 | ({ \ |
Hiroshi Shimamoto | 16855f8 | 2008-12-08 19:18:38 -0800 | [diff] [blame] | 398 | int __gu_err; \ |
Glauber Costa | 3f16822 | 2008-06-25 12:48:47 -0300 | [diff] [blame] | 399 | unsigned long __gu_val; \ |
| 400 | __get_user_size(__gu_val, (ptr), (size), __gu_err, -EFAULT); \ |
| 401 | (x) = (__force __typeof__(*(ptr)))__gu_val; \ |
| 402 | __gu_err; \ |
| 403 | }) |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 404 | |
| 405 | /* FIXME: this hack is definitely wrong -AK */ |
| 406 | struct __large_struct { unsigned long buf[100]; }; |
| 407 | #define __m(x) (*(struct __large_struct __user *)(x)) |
| 408 | |
| 409 | /* |
| 410 | * Tell gcc we read from memory instead of writing: this is because |
| 411 | * we do not write to any memory gcc knows about, so there are no |
| 412 | * aliasing issues. |
| 413 | */ |
| 414 | #define __put_user_asm(x, addr, err, itype, rtype, ltype, errret) \ |
H. Peter Anvin | 63bcff2 | 2012-09-21 12:43:12 -0700 | [diff] [blame] | 415 | asm volatile(ASM_STAC "\n" \ |
| 416 | "1: mov"itype" %"rtype"1,%2\n" \ |
| 417 | "2: " ASM_CLAC "\n" \ |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 418 | ".section .fixup,\"ax\"\n" \ |
| 419 | "3: mov %3,%0\n" \ |
| 420 | " jmp 2b\n" \ |
| 421 | ".previous\n" \ |
| 422 | _ASM_EXTABLE(1b, 3b) \ |
| 423 | : "=r"(err) \ |
| 424 | : ltype(x), "m" (__m(addr)), "i" (errret), "0" (err)) |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 425 | |
| 426 | #define __put_user_asm_ex(x, addr, itype, rtype, ltype) \ |
H. Peter Anvin | 5e88353 | 2012-09-21 12:43:15 -0700 | [diff] [blame] | 427 | asm volatile("1: mov"itype" %"rtype"0,%1\n" \ |
| 428 | "2:\n" \ |
H. Peter Anvin | 535c0c3 | 2012-04-20 16:57:35 -0700 | [diff] [blame] | 429 | _ASM_EXTABLE_EX(1b, 2b) \ |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 430 | : : ltype(x), "m" (__m(addr))) |
| 431 | |
| 432 | /* |
| 433 | * uaccess_try and catch |
| 434 | */ |
| 435 | #define uaccess_try do { \ |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 436 | current_thread_info()->uaccess_err = 0; \ |
H. Peter Anvin | 5e88353 | 2012-09-21 12:43:15 -0700 | [diff] [blame] | 437 | stac(); \ |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 438 | barrier(); |
| 439 | |
| 440 | #define uaccess_catch(err) \ |
H. Peter Anvin | 5e88353 | 2012-09-21 12:43:15 -0700 | [diff] [blame] | 441 | clac(); \ |
Andy Lutomirski | 4fc3490 | 2011-11-07 16:33:40 -0800 | [diff] [blame] | 442 | (err) |= (current_thread_info()->uaccess_err ? -EFAULT : 0); \ |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 443 | } while (0) |
| 444 | |
Glauber Costa | 8cb834e | 2008-06-25 14:43:30 -0300 | [diff] [blame] | 445 | /** |
| 446 | * __get_user: - Get a simple variable from user space, with less checking. |
| 447 | * @x: Variable to store result. |
| 448 | * @ptr: Source address, in user space. |
| 449 | * |
| 450 | * Context: User context only. This function may sleep. |
| 451 | * |
| 452 | * This macro copies a single simple variable from user space to kernel |
| 453 | * space. It supports simple types like char and int, but not larger |
| 454 | * data types like structures or arrays. |
| 455 | * |
| 456 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 457 | * dereferencing @ptr must be assignable to @x without a cast. |
| 458 | * |
| 459 | * Caller must check the pointer with access_ok() before calling this |
| 460 | * function. |
| 461 | * |
| 462 | * Returns zero on success, or -EFAULT on error. |
| 463 | * On error, the variable @x is set to zero. |
| 464 | */ |
Glauber Costa | dc70ddf | 2008-06-25 11:48:29 -0300 | [diff] [blame] | 465 | |
Glauber Costa | 8cb834e | 2008-06-25 14:43:30 -0300 | [diff] [blame] | 466 | #define __get_user(x, ptr) \ |
| 467 | __get_user_nocheck((x), (ptr), sizeof(*(ptr))) |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 468 | |
Glauber Costa | 8cb834e | 2008-06-25 14:43:30 -0300 | [diff] [blame] | 469 | /** |
| 470 | * __put_user: - Write a simple value into user space, with less checking. |
| 471 | * @x: Value to copy to user space. |
| 472 | * @ptr: Destination address, in user space. |
| 473 | * |
| 474 | * Context: User context only. This function may sleep. |
| 475 | * |
| 476 | * This macro copies a single simple value from kernel space to user |
| 477 | * space. It supports simple types like char and int, but not larger |
| 478 | * data types like structures or arrays. |
| 479 | * |
| 480 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 481 | * to the result of dereferencing @ptr. |
| 482 | * |
| 483 | * Caller must check the pointer with access_ok() before calling this |
| 484 | * function. |
| 485 | * |
| 486 | * Returns zero on success, or -EFAULT on error. |
| 487 | */ |
| 488 | |
| 489 | #define __put_user(x, ptr) \ |
| 490 | __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) |
| 491 | |
| 492 | #define __get_user_unaligned __get_user |
| 493 | #define __put_user_unaligned __put_user |
Glauber Costa | 865e5b7 | 2008-06-25 11:05:11 -0300 | [diff] [blame] | 494 | |
Glauber Costa | 8bc7de0c | 2008-06-25 14:53:41 -0300 | [diff] [blame] | 495 | /* |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 496 | * {get|put}_user_try and catch |
| 497 | * |
| 498 | * get_user_try { |
| 499 | * get_user_ex(...); |
| 500 | * } get_user_catch(err) |
| 501 | */ |
| 502 | #define get_user_try uaccess_try |
| 503 | #define get_user_catch(err) uaccess_catch(err) |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 504 | |
| 505 | #define get_user_ex(x, ptr) do { \ |
| 506 | unsigned long __gue_val; \ |
| 507 | __get_user_size_ex((__gue_val), (ptr), (sizeof(*(ptr)))); \ |
| 508 | (x) = (__force __typeof__(*(ptr)))__gue_val; \ |
| 509 | } while (0) |
| 510 | |
Hiroshi Shimamoto | 019a136 | 2009-01-29 11:49:18 -0800 | [diff] [blame] | 511 | #define put_user_try uaccess_try |
| 512 | #define put_user_catch(err) uaccess_catch(err) |
| 513 | |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 514 | #define put_user_ex(x, ptr) \ |
| 515 | __put_user_size_ex((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) |
| 516 | |
Robert Richter | 1ac2e6c | 2011-06-07 11:49:55 +0200 | [diff] [blame] | 517 | extern unsigned long |
| 518 | copy_from_user_nmi(void *to, const void __user *from, unsigned long n); |
Linus Torvalds | 92ae03f | 2012-04-06 14:32:32 -0700 | [diff] [blame] | 519 | extern __must_check long |
| 520 | strncpy_from_user(char *dst, const char __user *src, long count); |
Robert Richter | 1ac2e6c | 2011-06-07 11:49:55 +0200 | [diff] [blame] | 521 | |
Linus Torvalds | 5723aa9 | 2012-05-26 11:09:53 -0700 | [diff] [blame] | 522 | extern __must_check long strlen_user(const char __user *str); |
| 523 | extern __must_check long strnlen_user(const char __user *str, long n); |
| 524 | |
H. Peter Anvin | a052858 | 2012-09-21 12:43:11 -0700 | [diff] [blame] | 525 | unsigned long __must_check clear_user(void __user *mem, unsigned long len); |
| 526 | unsigned long __must_check __clear_user(void __user *mem, unsigned long len); |
| 527 | |
Hiroshi Shimamoto | fe40c0a | 2009-01-23 15:49:41 -0800 | [diff] [blame] | 528 | /* |
Glauber Costa | 8bc7de0c | 2008-06-25 14:53:41 -0300 | [diff] [blame] | 529 | * movsl can be slow when source and dest are not both 8-byte aligned |
| 530 | */ |
| 531 | #ifdef CONFIG_X86_INTEL_USERCOPY |
| 532 | extern struct movsl_mask { |
| 533 | int mask; |
| 534 | } ____cacheline_aligned_in_smp movsl_mask; |
| 535 | #endif |
| 536 | |
Glauber Costa | 22cac16 | 2008-06-25 14:56:53 -0300 | [diff] [blame] | 537 | #define ARCH_HAS_NOCACHE_UACCESS 1 |
| 538 | |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 539 | #ifdef CONFIG_X86_32 |
David Howells | a1ce392 | 2012-10-02 18:01:25 +0100 | [diff] [blame] | 540 | # include <asm/uaccess_32.h> |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 541 | #else |
David Howells | a1ce392 | 2012-10-02 18:01:25 +0100 | [diff] [blame] | 542 | # include <asm/uaccess_64.h> |
Thomas Gleixner | 96a388d | 2007-10-11 11:20:03 +0200 | [diff] [blame] | 543 | #endif |
Glauber Costa | ca23386 | 2008-06-13 14:39:25 -0300 | [diff] [blame] | 544 | |
H. Peter Anvin | 1965aae | 2008-10-22 22:26:29 -0700 | [diff] [blame] | 545 | #endif /* _ASM_X86_UACCESS_H */ |
Nick Piggin | 8174c43 | 2008-07-25 19:45:24 -0700 | [diff] [blame] | 546 | |