Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef _ASM_M32R_UACCESS_H |
| 2 | #define _ASM_M32R_UACCESS_H |
| 3 | |
| 4 | /* |
| 5 | * linux/include/asm-m32r/uaccess.h |
| 6 | * |
| 7 | * M32R version. |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 8 | * Copyright (C) 2004, 2006 Hirokazu Takata <takata at linux-m32r.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | */ |
| 10 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | /* |
| 12 | * User space memory access functions |
| 13 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <asm/page.h> |
Adrian Bunk | ec0ced1 | 2008-06-05 22:46:48 -0700 | [diff] [blame] | 15 | #include <asm/setup.h> |
Al Viro | 9a67734 | 2017-03-21 12:02:25 -0400 | [diff] [blame] | 16 | #include <linux/prefetch.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | /* |
| 19 | * The fs value determines whether argument validity checking should be |
| 20 | * performed or not. If get_fs() == USER_DS, checking is performed, with |
| 21 | * get_fs() == KERNEL_DS, checking is bypassed. |
| 22 | * |
| 23 | * For historical reasons, these macros are grossly misnamed. |
| 24 | */ |
| 25 | |
| 26 | #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) |
| 27 | |
| 28 | #ifdef CONFIG_MMU |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 29 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF) |
| 31 | #define USER_DS MAKE_MM_SEG(PAGE_OFFSET) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #define get_ds() (KERNEL_DS) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #define get_fs() (current_thread_info()->addr_limit) |
| 34 | #define set_fs(x) (current_thread_info()->addr_limit = (x)) |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 35 | |
| 36 | #else /* not CONFIG_MMU */ |
| 37 | |
| 38 | #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF) |
| 39 | #define USER_DS MAKE_MM_SEG(0xFFFFFFFF) |
| 40 | #define get_ds() (KERNEL_DS) |
| 41 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | static inline mm_segment_t get_fs(void) |
| 43 | { |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 44 | return USER_DS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static inline void set_fs(mm_segment_t s) |
| 48 | { |
| 49 | } |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 50 | |
| 51 | #endif /* not CONFIG_MMU */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 53 | #define segment_eq(a, b) ((a).seg == (b).seg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | |
| 55 | #define __addr_ok(addr) \ |
| 56 | ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg)) |
| 57 | |
| 58 | /* |
| 59 | * Test whether a block of memory is a valid user space address. |
| 60 | * Returns 0 if the range is valid, nonzero otherwise. |
| 61 | * |
| 62 | * This is equivalent to the following test: |
| 63 | * (u33)addr + (u33)size >= (u33)current->addr_limit.seg |
| 64 | * |
| 65 | * This needs 33-bit arithmetic. We have a carry... |
| 66 | */ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 67 | #define __range_ok(addr, size) ({ \ |
Tilman Schmidt | 16cf5b3 | 2007-02-10 01:45:41 -0800 | [diff] [blame] | 68 | unsigned long flag, roksum; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | __chk_user_ptr(addr); \ |
| 70 | asm ( \ |
| 71 | " cmpu %1, %1 ; clear cbit\n" \ |
| 72 | " addx %1, %3 ; set cbit if overflow\n" \ |
| 73 | " subx %0, %0\n" \ |
| 74 | " cmpu %4, %1\n" \ |
| 75 | " subx %0, %5\n" \ |
Tilman Schmidt | 16cf5b3 | 2007-02-10 01:45:41 -0800 | [diff] [blame] | 76 | : "=&r" (flag), "=r" (roksum) \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 77 | : "1" (addr), "r" ((int)(size)), \ |
| 78 | "r" (current_thread_info()->addr_limit.seg), "r" (0) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | : "cbit" ); \ |
| 80 | flag; }) |
| 81 | |
| 82 | /** |
| 83 | * access_ok: - Checks if a user space pointer is valid |
| 84 | * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that |
| 85 | * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe |
| 86 | * to write to a block, it is always safe to read from it. |
| 87 | * @addr: User space pointer to start of block to check |
| 88 | * @size: Size of block to check |
| 89 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 90 | * Context: User context only. This function may sleep if pagefaults are |
| 91 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | * |
| 93 | * Checks if a pointer to a block of memory in user space is valid. |
| 94 | * |
| 95 | * Returns true (nonzero) if the memory block may be valid, false (zero) |
| 96 | * if it is definitely invalid. |
| 97 | * |
| 98 | * Note that, depending on architecture, this function probably just |
| 99 | * checks that the pointer is in the user space range - after calling |
| 100 | * this function, memory access functions may still return -EFAULT. |
| 101 | */ |
| 102 | #ifdef CONFIG_MMU |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 103 | #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | #else |
| 105 | static inline int access_ok(int type, const void *addr, unsigned long size) |
| 106 | { |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 107 | unsigned long val = (unsigned long)addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 109 | return ((val >= memory_start) && ((val + size) < memory_end)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |
| 111 | #endif /* CONFIG_MMU */ |
| 112 | |
Al Viro | 35f8acd | 2016-12-25 03:33:03 -0500 | [diff] [blame] | 113 | #include <asm/extable.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | /* |
| 116 | * These are the main single-value transfer routines. They automatically |
| 117 | * use the right size if we just have the right pointer type. |
| 118 | * |
| 119 | * This gets kind of ugly. We want to return _two_ values in "get_user()" |
| 120 | * and yet we don't want to do any pointers, because that is too much |
| 121 | * of a performance impact. Thus we have a few rather ugly macros here, |
| 122 | * and hide all the uglyness from the user. |
| 123 | * |
| 124 | * The "__xxx" versions of the user access functions are versions that |
| 125 | * do not verify the address space, that must have been done previously |
| 126 | * with a separate "access_ok()" call (this is used when we do multiple |
| 127 | * accesses to the same area of user memory). |
| 128 | */ |
| 129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | /* Careful: we have to cast the result to the type of the pointer for sign |
| 131 | reasons */ |
| 132 | /** |
| 133 | * get_user: - Get a simple variable from user space. |
| 134 | * @x: Variable to store result. |
| 135 | * @ptr: Source address, in user space. |
| 136 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 137 | * Context: User context only. This function may sleep if pagefaults are |
| 138 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | * |
| 140 | * This macro copies a single simple variable from user space to kernel |
| 141 | * space. It supports simple types like char and int, but not larger |
| 142 | * data types like structures or arrays. |
| 143 | * |
| 144 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 145 | * dereferencing @ptr must be assignable to @x without a cast. |
| 146 | * |
| 147 | * Returns zero on success, or -EFAULT on error. |
| 148 | * On error, the variable @x is set to zero. |
| 149 | */ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 150 | #define get_user(x, ptr) \ |
| 151 | __get_user_check((x), (ptr), sizeof(*(ptr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | |
| 153 | /** |
| 154 | * put_user: - Write a simple value into user space. |
| 155 | * @x: Value to copy to user space. |
| 156 | * @ptr: Destination address, in user space. |
| 157 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 158 | * Context: User context only. This function may sleep if pagefaults are |
| 159 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | * |
| 161 | * This macro copies a single simple value from kernel space to user |
| 162 | * space. It supports simple types like char and int, but not larger |
| 163 | * data types like structures or arrays. |
| 164 | * |
| 165 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 166 | * to the result of dereferencing @ptr. |
| 167 | * |
| 168 | * Returns zero on success, or -EFAULT on error. |
| 169 | */ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 170 | #define put_user(x, ptr) \ |
| 171 | __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | |
| 173 | /** |
| 174 | * __get_user: - Get a simple variable from user space, with less checking. |
| 175 | * @x: Variable to store result. |
| 176 | * @ptr: Source address, in user space. |
| 177 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 178 | * Context: User context only. This function may sleep if pagefaults are |
| 179 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | * |
| 181 | * This macro copies a single simple variable from user space to kernel |
| 182 | * space. It supports simple types like char and int, but not larger |
| 183 | * data types like structures or arrays. |
| 184 | * |
| 185 | * @ptr must have pointer-to-simple-variable type, and the result of |
| 186 | * dereferencing @ptr must be assignable to @x without a cast. |
| 187 | * |
| 188 | * Caller must check the pointer with access_ok() before calling this |
| 189 | * function. |
| 190 | * |
| 191 | * Returns zero on success, or -EFAULT on error. |
| 192 | * On error, the variable @x is set to zero. |
| 193 | */ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 194 | #define __get_user(x, ptr) \ |
| 195 | __get_user_nocheck((x), (ptr), sizeof(*(ptr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 197 | #define __get_user_nocheck(x, ptr, size) \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 198 | ({ \ |
| 199 | long __gu_err = 0; \ |
Al Viro | c90a3bc | 2016-09-09 19:20:13 -0400 | [diff] [blame] | 200 | unsigned long __gu_val = 0; \ |
Michael S. Tsirkin | 0168257 | 2013-05-26 17:30:51 +0300 | [diff] [blame] | 201 | might_fault(); \ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 202 | __get_user_size(__gu_val, (ptr), (size), __gu_err); \ |
Michael S. Tsirkin | a618337 | 2014-12-12 01:56:04 +0200 | [diff] [blame] | 203 | (x) = (__force __typeof__(*(ptr)))__gu_val; \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 204 | __gu_err; \ |
| 205 | }) |
| 206 | |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 207 | #define __get_user_check(x, ptr, size) \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 208 | ({ \ |
| 209 | long __gu_err = -EFAULT; \ |
| 210 | unsigned long __gu_val = 0; \ |
| 211 | const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \ |
Michael S. Tsirkin | 0168257 | 2013-05-26 17:30:51 +0300 | [diff] [blame] | 212 | might_fault(); \ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 213 | if (access_ok(VERIFY_READ, __gu_addr, size)) \ |
| 214 | __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \ |
Michael S. Tsirkin | a618337 | 2014-12-12 01:56:04 +0200 | [diff] [blame] | 215 | (x) = (__force __typeof__(*(ptr)))__gu_val; \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 216 | __gu_err; \ |
| 217 | }) |
| 218 | |
| 219 | extern long __get_user_bad(void); |
| 220 | |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 221 | #define __get_user_size(x, ptr, size, retval) \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 222 | do { \ |
| 223 | retval = 0; \ |
| 224 | __chk_user_ptr(ptr); \ |
| 225 | switch (size) { \ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 226 | case 1: __get_user_asm(x, ptr, retval, "ub"); break; \ |
| 227 | case 2: __get_user_asm(x, ptr, retval, "uh"); break; \ |
| 228 | case 4: __get_user_asm(x, ptr, retval, ""); break; \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 229 | default: (x) = __get_user_bad(); \ |
| 230 | } \ |
| 231 | } while (0) |
| 232 | |
| 233 | #define __get_user_asm(x, addr, err, itype) \ |
| 234 | __asm__ __volatile__( \ |
| 235 | " .fillinsn\n" \ |
| 236 | "1: ld"itype" %1,@%2\n" \ |
| 237 | " .fillinsn\n" \ |
| 238 | "2:\n" \ |
| 239 | ".section .fixup,\"ax\"\n" \ |
| 240 | " .balign 4\n" \ |
| 241 | "3: ldi %0,%3\n" \ |
| 242 | " seth r14,#high(2b)\n" \ |
| 243 | " or3 r14,r14,#low(2b)\n" \ |
| 244 | " jmp r14\n" \ |
| 245 | ".previous\n" \ |
| 246 | ".section __ex_table,\"a\"\n" \ |
| 247 | " .balign 4\n" \ |
| 248 | " .long 1b,3b\n" \ |
| 249 | ".previous" \ |
| 250 | : "=&r" (err), "=&r" (x) \ |
| 251 | : "r" (addr), "i" (-EFAULT), "0" (err) \ |
| 252 | : "r14", "memory") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | |
| 254 | /** |
| 255 | * __put_user: - Write a simple value into user space, with less checking. |
| 256 | * @x: Value to copy to user space. |
| 257 | * @ptr: Destination address, in user space. |
| 258 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 259 | * Context: User context only. This function may sleep if pagefaults are |
| 260 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | * |
| 262 | * This macro copies a single simple value from kernel space to user |
| 263 | * space. It supports simple types like char and int, but not larger |
| 264 | * data types like structures or arrays. |
| 265 | * |
| 266 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable |
| 267 | * to the result of dereferencing @ptr. |
| 268 | * |
| 269 | * Caller must check the pointer with access_ok() before calling this |
| 270 | * function. |
| 271 | * |
| 272 | * Returns zero on success, or -EFAULT on error. |
| 273 | */ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 274 | #define __put_user(x, ptr) \ |
| 275 | __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr))) |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 276 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 278 | #define __put_user_nocheck(x, ptr, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | ({ \ |
| 280 | long __pu_err; \ |
Michael S. Tsirkin | 0168257 | 2013-05-26 17:30:51 +0300 | [diff] [blame] | 281 | might_fault(); \ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 282 | __put_user_size((x), (ptr), (size), __pu_err); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | __pu_err; \ |
| 284 | }) |
| 285 | |
| 286 | |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 287 | #define __put_user_check(x, ptr, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | ({ \ |
| 289 | long __pu_err = -EFAULT; \ |
| 290 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
Michael S. Tsirkin | 0168257 | 2013-05-26 17:30:51 +0300 | [diff] [blame] | 291 | might_fault(); \ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 292 | if (access_ok(VERIFY_WRITE, __pu_addr, size)) \ |
| 293 | __put_user_size((x), __pu_addr, (size), __pu_err); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | __pu_err; \ |
| 295 | }) |
| 296 | |
| 297 | #if defined(__LITTLE_ENDIAN__) |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 298 | #define __put_user_u64(x, addr, err) \ |
| 299 | __asm__ __volatile__( \ |
| 300 | " .fillinsn\n" \ |
| 301 | "1: st %L1,@%2\n" \ |
| 302 | " .fillinsn\n" \ |
| 303 | "2: st %H1,@(4,%2)\n" \ |
| 304 | " .fillinsn\n" \ |
| 305 | "3:\n" \ |
| 306 | ".section .fixup,\"ax\"\n" \ |
| 307 | " .balign 4\n" \ |
| 308 | "4: ldi %0,%3\n" \ |
| 309 | " seth r14,#high(3b)\n" \ |
| 310 | " or3 r14,r14,#low(3b)\n" \ |
| 311 | " jmp r14\n" \ |
| 312 | ".previous\n" \ |
| 313 | ".section __ex_table,\"a\"\n" \ |
| 314 | " .balign 4\n" \ |
| 315 | " .long 1b,4b\n" \ |
| 316 | " .long 2b,4b\n" \ |
| 317 | ".previous" \ |
| 318 | : "=&r" (err) \ |
| 319 | : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | : "r14", "memory") |
| 321 | |
| 322 | #elif defined(__BIG_ENDIAN__) |
| 323 | #define __put_user_u64(x, addr, err) \ |
| 324 | __asm__ __volatile__( \ |
| 325 | " .fillinsn\n" \ |
| 326 | "1: st %H1,@%2\n" \ |
| 327 | " .fillinsn\n" \ |
| 328 | "2: st %L1,@(4,%2)\n" \ |
| 329 | " .fillinsn\n" \ |
| 330 | "3:\n" \ |
| 331 | ".section .fixup,\"ax\"\n" \ |
| 332 | " .balign 4\n" \ |
| 333 | "4: ldi %0,%3\n" \ |
| 334 | " seth r14,#high(3b)\n" \ |
| 335 | " or3 r14,r14,#low(3b)\n" \ |
| 336 | " jmp r14\n" \ |
| 337 | ".previous\n" \ |
| 338 | ".section __ex_table,\"a\"\n" \ |
| 339 | " .balign 4\n" \ |
| 340 | " .long 1b,4b\n" \ |
| 341 | " .long 2b,4b\n" \ |
| 342 | ".previous" \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 343 | : "=&r" (err) \ |
| 344 | : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | : "r14", "memory") |
| 346 | #else |
| 347 | #error no endian defined |
| 348 | #endif |
| 349 | |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 350 | extern void __put_user_bad(void); |
| 351 | |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 352 | #define __put_user_size(x, ptr, size, retval) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | do { \ |
| 354 | retval = 0; \ |
| 355 | __chk_user_ptr(ptr); \ |
| 356 | switch (size) { \ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 357 | case 1: __put_user_asm(x, ptr, retval, "b"); break; \ |
| 358 | case 2: __put_user_asm(x, ptr, retval, "h"); break; \ |
| 359 | case 4: __put_user_asm(x, ptr, retval, ""); break; \ |
| 360 | case 8: __put_user_u64((__typeof__(*ptr))(x), ptr, retval); break;\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | default: __put_user_bad(); \ |
| 362 | } \ |
| 363 | } while (0) |
| 364 | |
| 365 | struct __large_struct { unsigned long buf[100]; }; |
| 366 | #define __m(x) (*(struct __large_struct *)(x)) |
| 367 | |
| 368 | /* |
| 369 | * Tell gcc we read from memory instead of writing: this is because |
| 370 | * we do not write to any memory gcc knows about, so there are no |
| 371 | * aliasing issues. |
| 372 | */ |
| 373 | #define __put_user_asm(x, addr, err, itype) \ |
| 374 | __asm__ __volatile__( \ |
| 375 | " .fillinsn\n" \ |
| 376 | "1: st"itype" %1,@%2\n" \ |
| 377 | " .fillinsn\n" \ |
| 378 | "2:\n" \ |
| 379 | ".section .fixup,\"ax\"\n" \ |
| 380 | " .balign 4\n" \ |
| 381 | "3: ldi %0,%3\n" \ |
| 382 | " seth r14,#high(2b)\n" \ |
| 383 | " or3 r14,r14,#low(2b)\n" \ |
| 384 | " jmp r14\n" \ |
| 385 | ".previous\n" \ |
| 386 | ".section __ex_table,\"a\"\n" \ |
| 387 | " .balign 4\n" \ |
| 388 | " .long 1b,3b\n" \ |
| 389 | ".previous" \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 390 | : "=&r" (err) \ |
| 391 | : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | : "r14", "memory") |
| 393 | |
| 394 | /* |
| 395 | * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault |
| 396 | * we return the initial request size (1, 2 or 4), as copy_*_user should do. |
| 397 | * If a store crosses a page boundary and gets a fault, the m32r will not write |
| 398 | * anything, so this is accurate. |
| 399 | */ |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | /* |
| 402 | * Copy To/From Userspace |
| 403 | */ |
| 404 | |
| 405 | /* Generic arbitrary sized copy. */ |
| 406 | /* Return the number of bytes NOT copied. */ |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 407 | #define __copy_user(to, from, size) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 408 | do { \ |
| 409 | unsigned long __dst, __src, __c; \ |
| 410 | __asm__ __volatile__ ( \ |
| 411 | " mv r14, %0\n" \ |
| 412 | " or r14, %1\n" \ |
| 413 | " beq %0, %1, 9f\n" \ |
| 414 | " beqz %2, 9f\n" \ |
| 415 | " and3 r14, r14, #3\n" \ |
| 416 | " bnez r14, 2f\n" \ |
| 417 | " and3 %2, %2, #3\n" \ |
| 418 | " beqz %3, 2f\n" \ |
| 419 | " addi %0, #-4 ; word_copy \n" \ |
| 420 | " .fillinsn\n" \ |
| 421 | "0: ld r14, @%1+\n" \ |
| 422 | " addi %3, #-1\n" \ |
| 423 | " .fillinsn\n" \ |
| 424 | "1: st r14, @+%0\n" \ |
| 425 | " bnez %3, 0b\n" \ |
| 426 | " beqz %2, 9f\n" \ |
| 427 | " addi %0, #4\n" \ |
| 428 | " .fillinsn\n" \ |
| 429 | "2: ldb r14, @%1 ; byte_copy \n" \ |
| 430 | " .fillinsn\n" \ |
| 431 | "3: stb r14, @%0\n" \ |
| 432 | " addi %1, #1\n" \ |
| 433 | " addi %2, #-1\n" \ |
| 434 | " addi %0, #1\n" \ |
| 435 | " bnez %2, 2b\n" \ |
| 436 | " .fillinsn\n" \ |
| 437 | "9:\n" \ |
| 438 | ".section .fixup,\"ax\"\n" \ |
| 439 | " .balign 4\n" \ |
| 440 | "5: addi %3, #1\n" \ |
| 441 | " addi %1, #-4\n" \ |
| 442 | " .fillinsn\n" \ |
| 443 | "6: slli %3, #2\n" \ |
| 444 | " add %2, %3\n" \ |
| 445 | " addi %0, #4\n" \ |
| 446 | " .fillinsn\n" \ |
| 447 | "7: seth r14, #high(9b)\n" \ |
| 448 | " or3 r14, r14, #low(9b)\n" \ |
| 449 | " jmp r14\n" \ |
| 450 | ".previous\n" \ |
| 451 | ".section __ex_table,\"a\"\n" \ |
| 452 | " .balign 4\n" \ |
| 453 | " .long 0b,6b\n" \ |
| 454 | " .long 1b,5b\n" \ |
| 455 | " .long 2b,9b\n" \ |
| 456 | " .long 3b,9b\n" \ |
| 457 | ".previous\n" \ |
Hirokazu Takata | 04dfd0d | 2006-04-10 22:53:20 -0700 | [diff] [blame] | 458 | : "=&r" (__dst), "=&r" (__src), "=&r" (size), \ |
| 459 | "=&r" (__c) \ |
| 460 | : "0" (to), "1" (from), "2" (size), "3" (size / 4) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | : "r14", "memory"); \ |
| 462 | } while (0) |
| 463 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | /* We let the __ versions of copy_from/to_user inline, because they're often |
| 465 | * used in fast paths and have only a small space overhead. |
| 466 | */ |
Al Viro | 9a67734 | 2017-03-21 12:02:25 -0400 | [diff] [blame] | 467 | static inline unsigned long |
| 468 | raw_copy_from_user(void *to, const void __user *from, unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
Al Viro | 9a67734 | 2017-03-21 12:02:25 -0400 | [diff] [blame] | 470 | prefetchw(to); |
Al Viro | 8cd920f | 2017-03-19 16:11:13 -0400 | [diff] [blame] | 471 | __copy_user(to, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | return n; |
| 473 | } |
| 474 | |
Al Viro | 9a67734 | 2017-03-21 12:02:25 -0400 | [diff] [blame] | 475 | static inline unsigned long |
| 476 | raw_copy_to_user(void __user *to, const void *from, unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | { |
Al Viro | 9a67734 | 2017-03-21 12:02:25 -0400 | [diff] [blame] | 478 | prefetch(from); |
Michael S. Tsirkin | 0ab0660 | 2015-01-06 15:11:13 +0200 | [diff] [blame] | 479 | __copy_user(to, from, n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | return n; |
| 481 | } |
| 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | long __must_check strncpy_from_user(char *dst, const char __user *src, |
| 484 | long count); |
| 485 | long __must_check __strncpy_from_user(char *dst, |
| 486 | const char __user *src, long count); |
| 487 | |
| 488 | /** |
| 489 | * __clear_user: - Zero a block of memory in user space, with less checking. |
| 490 | * @to: Destination address, in user space. |
| 491 | * @n: Number of bytes to zero. |
| 492 | * |
| 493 | * Zero a block of memory in user space. Caller must check |
| 494 | * the specified block with access_ok() before calling this function. |
| 495 | * |
| 496 | * Returns number of bytes that could not be cleared. |
| 497 | * On success, this will be zero. |
| 498 | */ |
| 499 | unsigned long __clear_user(void __user *mem, unsigned long len); |
| 500 | |
| 501 | /** |
| 502 | * clear_user: - Zero a block of memory in user space. |
| 503 | * @to: Destination address, in user space. |
| 504 | * @n: Number of bytes to zero. |
| 505 | * |
| 506 | * Zero a block of memory in user space. Caller must check |
| 507 | * the specified block with access_ok() before calling this function. |
| 508 | * |
| 509 | * Returns number of bytes that could not be cleared. |
| 510 | * On success, this will be zero. |
| 511 | */ |
| 512 | unsigned long clear_user(void __user *mem, unsigned long len); |
| 513 | |
| 514 | /** |
| 515 | * strlen_user: - Get the size of a string in user space. |
| 516 | * @str: The string to measure. |
| 517 | * |
David Hildenbrand | b3c395ef | 2015-05-11 17:52:08 +0200 | [diff] [blame] | 518 | * Context: User context only. This function may sleep if pagefaults are |
| 519 | * enabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | * |
| 521 | * Get the size of a NUL-terminated string in user space. |
| 522 | * |
| 523 | * Returns the size of the string INCLUDING the terminating NUL. |
| 524 | * On exception, returns 0. |
| 525 | * |
| 526 | * If there is a limit on the length of a valid string, you may wish to |
| 527 | * consider using strnlen_user() instead. |
| 528 | */ |
| 529 | #define strlen_user(str) strnlen_user(str, ~0UL >> 1) |
| 530 | long strnlen_user(const char __user *str, long n); |
| 531 | |
| 532 | #endif /* _ASM_M32R_UACCESS_H */ |