Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 1 | /* $Id: uaccess.h,v 1.11 2003/10/13 07:21:20 lethal Exp $ |
| 2 | * |
| 3 | * User space memory access functions |
| 4 | * |
| 5 | * Copyright (C) 1999, 2002 Niibe Yutaka |
| 6 | * Copyright (C) 2003 Paul Mundt |
| 7 | * |
| 8 | * Based on: |
| 9 | * MIPS implementation version 1.15 by |
| 10 | * Copyright (C) 1996, 1997, 1998 by Ralf Baechle |
| 11 | * and i386 version. |
| 12 | */ |
Magnus Damm | 1e6760c | 2008-02-07 19:50:52 +0900 | [diff] [blame] | 13 | #ifndef __ASM_SH_UACCESS_32_H |
| 14 | #define __ASM_SH_UACCESS_32_H |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 15 | |
| 16 | #include <linux/errno.h> |
| 17 | #include <linux/sched.h> |
| 18 | |
| 19 | #define VERIFY_READ 0 |
| 20 | #define VERIFY_WRITE 1 |
| 21 | |
| 22 | /* |
| 23 | * The fs value determines whether argument validity checking should be |
| 24 | * performed or not. If get_fs() == USER_DS, checking is performed, with |
| 25 | * get_fs() == KERNEL_DS, checking is bypassed. |
| 26 | * |
| 27 | * For historical reasons (Data Segment Register?), these macros are misnamed. |
| 28 | */ |
| 29 | |
| 30 | #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) |
| 31 | |
| 32 | #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFFUL) |
| 33 | #define USER_DS MAKE_MM_SEG(PAGE_OFFSET) |
| 34 | |
| 35 | #define segment_eq(a,b) ((a).seg == (b).seg) |
| 36 | |
| 37 | #define get_ds() (KERNEL_DS) |
| 38 | |
| 39 | #if !defined(CONFIG_MMU) |
| 40 | /* NOMMU is always true */ |
| 41 | #define __addr_ok(addr) (1) |
| 42 | |
| 43 | static inline mm_segment_t get_fs(void) |
| 44 | { |
| 45 | return USER_DS; |
| 46 | } |
| 47 | |
| 48 | static inline void set_fs(mm_segment_t s) |
| 49 | { |
| 50 | } |
| 51 | |
| 52 | /* |
| 53 | * __access_ok: Check if address with size is OK or not. |
| 54 | * |
| 55 | * If we don't have an MMU (or if its disabled) the only thing we really have |
| 56 | * to look out for is if the address resides somewhere outside of what |
| 57 | * available RAM we have. |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 58 | */ |
| 59 | static inline int __access_ok(unsigned long addr, unsigned long size) |
| 60 | { |
Paul Mundt | 9460c0c | 2008-03-21 18:08:58 +0900 | [diff] [blame] | 61 | return 1; |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 62 | } |
| 63 | #else /* CONFIG_MMU */ |
| 64 | #define __addr_ok(addr) \ |
| 65 | ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg)) |
| 66 | |
| 67 | #define get_fs() (current_thread_info()->addr_limit) |
| 68 | #define set_fs(x) (current_thread_info()->addr_limit = (x)) |
| 69 | |
| 70 | /* |
| 71 | * __access_ok: Check if address with size is OK or not. |
| 72 | * |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 73 | * Uhhuh, this needs 33-bit arithmetic. We have a carry.. |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 74 | * |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 75 | * sum := addr + size; carry? --> flag = true; |
| 76 | * if (sum >= addr_limit) flag = true; |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 77 | */ |
| 78 | static inline int __access_ok(unsigned long addr, unsigned long size) |
| 79 | { |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 80 | unsigned long flag, sum; |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 81 | |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 82 | __asm__("clrt\n\t" |
| 83 | "addc %3, %1\n\t" |
| 84 | "movt %0\n\t" |
| 85 | "cmp/hi %4, %1\n\t" |
| 86 | "rotcl %0" |
| 87 | :"=&r" (flag), "=r" (sum) |
| 88 | :"1" (addr), "r" (size), |
| 89 | "r" (current_thread_info()->addr_limit.seg) |
| 90 | :"t"); |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 91 | return flag == 0; |
| 92 | } |
| 93 | #endif /* CONFIG_MMU */ |
| 94 | |
Paul Mundt | 0465b9f | 2007-12-26 18:37:16 +0900 | [diff] [blame] | 95 | #define access_ok(type, addr, size) \ |
| 96 | (__chk_user_ptr(addr), \ |
| 97 | __access_ok((unsigned long __force)(addr), (size))) |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * Uh, these should become the main single-value transfer routines ... |
| 101 | * They automatically use the right size if we just have the right |
| 102 | * pointer type ... |
| 103 | * |
| 104 | * As SuperH uses the same address space for kernel and user data, we |
| 105 | * can just do these as direct assignments. |
| 106 | * |
| 107 | * Careful to not |
| 108 | * (a) re-use the arguments for side effects (sizeof is ok) |
| 109 | * (b) require any knowledge of processes at this stage |
| 110 | */ |
Paul Mundt | 0465b9f | 2007-12-26 18:37:16 +0900 | [diff] [blame] | 111 | #define put_user(x,ptr) __put_user_check((x), (ptr), sizeof(*(ptr))) |
| 112 | #define get_user(x,ptr) __get_user_check((x), (ptr), sizeof(*(ptr))) |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 113 | |
| 114 | /* |
| 115 | * The "__xxx" versions do not do address space checking, useful when |
| 116 | * doing multiple accesses to the same area (the user has to do the |
| 117 | * checks by hand with "access_ok()") |
| 118 | */ |
Paul Mundt | 0465b9f | 2007-12-26 18:37:16 +0900 | [diff] [blame] | 119 | #define __put_user(x,ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr))) |
| 120 | #define __get_user(x,ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr))) |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 121 | |
| 122 | struct __large_struct { unsigned long buf[100]; }; |
| 123 | #define __m(x) (*(struct __large_struct __user *)(x)) |
| 124 | |
| 125 | #define __get_user_size(x,ptr,size,retval) \ |
| 126 | do { \ |
| 127 | retval = 0; \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 128 | switch (size) { \ |
| 129 | case 1: \ |
| 130 | __get_user_asm(x, ptr, retval, "b"); \ |
| 131 | break; \ |
| 132 | case 2: \ |
| 133 | __get_user_asm(x, ptr, retval, "w"); \ |
| 134 | break; \ |
| 135 | case 4: \ |
| 136 | __get_user_asm(x, ptr, retval, "l"); \ |
| 137 | break; \ |
| 138 | default: \ |
| 139 | __get_user_unknown(); \ |
| 140 | break; \ |
| 141 | } \ |
| 142 | } while (0) |
| 143 | |
| 144 | #define __get_user_nocheck(x,ptr,size) \ |
| 145 | ({ \ |
Paul Mundt | 0465b9f | 2007-12-26 18:37:16 +0900 | [diff] [blame] | 146 | long __gu_err; \ |
| 147 | unsigned long __gu_val; \ |
| 148 | const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \ |
| 149 | __chk_user_ptr(ptr); \ |
| 150 | __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 151 | (x) = (__typeof__(*(ptr)))__gu_val; \ |
| 152 | __gu_err; \ |
| 153 | }) |
| 154 | |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 155 | #define __get_user_check(x,ptr,size) \ |
| 156 | ({ \ |
Paul Mundt | 0465b9f | 2007-12-26 18:37:16 +0900 | [diff] [blame] | 157 | long __gu_err = -EFAULT; \ |
| 158 | unsigned long __gu_val = 0; \ |
| 159 | const __typeof__(*(ptr)) *__gu_addr = (ptr); \ |
| 160 | if (likely(access_ok(VERIFY_READ, __gu_addr, (size)))) \ |
| 161 | __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 162 | (x) = (__typeof__(*(ptr)))__gu_val; \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 163 | __gu_err; \ |
| 164 | }) |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 165 | |
| 166 | #define __get_user_asm(x, addr, err, insn) \ |
| 167 | ({ \ |
| 168 | __asm__ __volatile__( \ |
| 169 | "1:\n\t" \ |
| 170 | "mov." insn " %2, %1\n\t" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 171 | "2:\n" \ |
| 172 | ".section .fixup,\"ax\"\n" \ |
| 173 | "3:\n\t" \ |
| 174 | "mov #0, %1\n\t" \ |
| 175 | "mov.l 4f, %0\n\t" \ |
| 176 | "jmp @%0\n\t" \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 177 | " mov %3, %0\n\t" \ |
| 178 | ".balign 4\n" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 179 | "4: .long 2b\n\t" \ |
| 180 | ".previous\n" \ |
| 181 | ".section __ex_table,\"a\"\n\t" \ |
| 182 | ".long 1b, 3b\n\t" \ |
| 183 | ".previous" \ |
| 184 | :"=&r" (err), "=&r" (x) \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 185 | :"m" (__m(addr)), "i" (-EFAULT), "0" (err)); }) |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 186 | |
| 187 | extern void __get_user_unknown(void); |
| 188 | |
| 189 | #define __put_user_size(x,ptr,size,retval) \ |
| 190 | do { \ |
| 191 | retval = 0; \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 192 | switch (size) { \ |
| 193 | case 1: \ |
| 194 | __put_user_asm(x, ptr, retval, "b"); \ |
| 195 | break; \ |
| 196 | case 2: \ |
| 197 | __put_user_asm(x, ptr, retval, "w"); \ |
| 198 | break; \ |
| 199 | case 4: \ |
| 200 | __put_user_asm(x, ptr, retval, "l"); \ |
| 201 | break; \ |
| 202 | case 8: \ |
| 203 | __put_user_u64(x, ptr, retval); \ |
| 204 | break; \ |
| 205 | default: \ |
| 206 | __put_user_unknown(); \ |
| 207 | } \ |
| 208 | } while (0) |
| 209 | |
Paul Mundt | 0465b9f | 2007-12-26 18:37:16 +0900 | [diff] [blame] | 210 | #define __put_user_nocheck(x,ptr,size) \ |
| 211 | ({ \ |
| 212 | long __pu_err; \ |
| 213 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
| 214 | __chk_user_ptr(ptr); \ |
| 215 | __put_user_size((x), __pu_addr, (size), __pu_err); \ |
| 216 | __pu_err; \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 217 | }) |
| 218 | |
| 219 | #define __put_user_check(x,ptr,size) \ |
| 220 | ({ \ |
Paul Mundt | 0465b9f | 2007-12-26 18:37:16 +0900 | [diff] [blame] | 221 | long __pu_err = -EFAULT; \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 222 | __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ |
Paul Mundt | 0465b9f | 2007-12-26 18:37:16 +0900 | [diff] [blame] | 223 | if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \ |
| 224 | __put_user_size((x), __pu_addr, (size), \ |
| 225 | __pu_err); \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 226 | __pu_err; \ |
| 227 | }) |
| 228 | |
| 229 | #define __put_user_asm(x, addr, err, insn) \ |
| 230 | ({ \ |
| 231 | __asm__ __volatile__( \ |
| 232 | "1:\n\t" \ |
| 233 | "mov." insn " %1, %2\n\t" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 234 | "2:\n" \ |
| 235 | ".section .fixup,\"ax\"\n" \ |
| 236 | "3:\n\t" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 237 | "mov.l 4f, %0\n\t" \ |
| 238 | "jmp @%0\n\t" \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 239 | " mov %3, %0\n\t" \ |
| 240 | ".balign 4\n" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 241 | "4: .long 2b\n\t" \ |
| 242 | ".previous\n" \ |
| 243 | ".section __ex_table,\"a\"\n\t" \ |
| 244 | ".long 1b, 3b\n\t" \ |
| 245 | ".previous" \ |
| 246 | :"=&r" (err) \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 247 | :"r" (x), "m" (__m(addr)), "i" (-EFAULT), "0" (err) \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 248 | :"memory"); }) |
| 249 | |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 250 | #if defined(CONFIG_CPU_LITTLE_ENDIAN) |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 251 | #define __put_user_u64(val,addr,retval) \ |
| 252 | ({ \ |
| 253 | __asm__ __volatile__( \ |
| 254 | "1:\n\t" \ |
| 255 | "mov.l %R1,%2\n\t" \ |
| 256 | "mov.l %S1,%T2\n\t" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 257 | "2:\n" \ |
| 258 | ".section .fixup,\"ax\"\n" \ |
| 259 | "3:\n\t" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 260 | "mov.l 4f,%0\n\t" \ |
| 261 | "jmp @%0\n\t" \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 262 | " mov %3,%0\n\t" \ |
| 263 | ".balign 4\n" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 264 | "4: .long 2b\n\t" \ |
| 265 | ".previous\n" \ |
| 266 | ".section __ex_table,\"a\"\n\t" \ |
| 267 | ".long 1b, 3b\n\t" \ |
| 268 | ".previous" \ |
| 269 | : "=r" (retval) \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 270 | : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 271 | : "memory"); }) |
| 272 | #else |
| 273 | #define __put_user_u64(val,addr,retval) \ |
| 274 | ({ \ |
| 275 | __asm__ __volatile__( \ |
| 276 | "1:\n\t" \ |
| 277 | "mov.l %S1,%2\n\t" \ |
| 278 | "mov.l %R1,%T2\n\t" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 279 | "2:\n" \ |
| 280 | ".section .fixup,\"ax\"\n" \ |
| 281 | "3:\n\t" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 282 | "mov.l 4f,%0\n\t" \ |
| 283 | "jmp @%0\n\t" \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 284 | " mov %3,%0\n\t" \ |
| 285 | ".balign 4\n" \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 286 | "4: .long 2b\n\t" \ |
| 287 | ".previous\n" \ |
| 288 | ".section __ex_table,\"a\"\n\t" \ |
| 289 | ".long 1b, 3b\n\t" \ |
| 290 | ".previous" \ |
| 291 | : "=r" (retval) \ |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 292 | : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 293 | : "memory"); }) |
| 294 | #endif |
| 295 | |
| 296 | extern void __put_user_unknown(void); |
| 297 | |
| 298 | /* Generic arbitrary sized copy. */ |
| 299 | /* Return the number of bytes NOT copied */ |
| 300 | __kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n); |
| 301 | |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 302 | |
| 303 | static __always_inline unsigned long |
| 304 | __copy_from_user(void *to, const void __user *from, unsigned long n) |
| 305 | { |
| 306 | return __copy_user(to, (__force void *)from, n); |
| 307 | } |
| 308 | |
| 309 | static __always_inline unsigned long __must_check |
| 310 | __copy_to_user(void __user *to, const void *from, unsigned long n) |
| 311 | { |
| 312 | return __copy_user((__force void *)to, from, n); |
| 313 | } |
| 314 | |
| 315 | #define __copy_to_user_inatomic __copy_to_user |
| 316 | #define __copy_from_user_inatomic __copy_from_user |
| 317 | |
| 318 | /* |
| 319 | * Clear the area and return remaining number of bytes |
| 320 | * (on failure. Usually it's 0.) |
| 321 | */ |
| 322 | extern __kernel_size_t __clear_user(void *addr, __kernel_size_t size); |
| 323 | |
| 324 | #define clear_user(addr,n) ({ \ |
| 325 | void * __cl_addr = (addr); \ |
| 326 | unsigned long __cl_size = (n); \ |
| 327 | if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \ |
| 328 | __cl_size = __clear_user(__cl_addr, __cl_size); \ |
| 329 | __cl_size; }) |
| 330 | |
| 331 | static __inline__ int |
| 332 | __strncpy_from_user(unsigned long __dest, unsigned long __user __src, int __count) |
| 333 | { |
| 334 | __kernel_size_t res; |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 335 | unsigned long __dummy, _d, _s, _c; |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 336 | |
| 337 | __asm__ __volatile__( |
| 338 | "9:\n" |
| 339 | "mov.b @%2+, %1\n\t" |
| 340 | "cmp/eq #0, %1\n\t" |
| 341 | "bt/s 2f\n" |
| 342 | "1:\n" |
| 343 | "mov.b %1, @%3\n\t" |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 344 | "dt %4\n\t" |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 345 | "bf/s 9b\n\t" |
| 346 | " add #1, %3\n\t" |
| 347 | "2:\n\t" |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 348 | "sub %4, %0\n" |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 349 | "3:\n" |
| 350 | ".section .fixup,\"ax\"\n" |
| 351 | "4:\n\t" |
| 352 | "mov.l 5f, %1\n\t" |
| 353 | "jmp @%1\n\t" |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 354 | " mov %9, %0\n\t" |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 355 | ".balign 4\n" |
| 356 | "5: .long 3b\n" |
| 357 | ".previous\n" |
| 358 | ".section __ex_table,\"a\"\n" |
| 359 | " .balign 4\n" |
| 360 | " .long 9b,4b\n" |
| 361 | ".previous" |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 362 | : "=r" (res), "=&z" (__dummy), "=r" (_s), "=r" (_d), "=r"(_c) |
| 363 | : "0" (__count), "2" (__src), "3" (__dest), "4" (__count), |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 364 | "i" (-EFAULT) |
| 365 | : "memory", "t"); |
| 366 | |
| 367 | return res; |
| 368 | } |
| 369 | |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 370 | /** |
| 371 | * strncpy_from_user: - Copy a NUL terminated string from userspace. |
| 372 | * @dst: Destination address, in kernel space. This buffer must be at |
| 373 | * least @count bytes long. |
| 374 | * @src: Source address, in user space. |
| 375 | * @count: Maximum number of bytes to copy, including the trailing NUL. |
| 376 | * |
| 377 | * Copies a NUL-terminated string from userspace to kernel space. |
| 378 | * |
| 379 | * On success, returns the length of the string (not including the trailing |
| 380 | * NUL). |
| 381 | * |
| 382 | * If access to userspace fails, returns -EFAULT (some data may have been |
| 383 | * copied). |
| 384 | * |
| 385 | * If @count is smaller than the length of the string, copies @count bytes |
| 386 | * and returns @count. |
| 387 | */ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 388 | #define strncpy_from_user(dest,src,count) ({ \ |
| 389 | unsigned long __sfu_src = (unsigned long) (src); \ |
| 390 | int __sfu_count = (int) (count); \ |
| 391 | long __sfu_res = -EFAULT; \ |
| 392 | if(__access_ok(__sfu_src, __sfu_count)) { \ |
| 393 | __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \ |
| 394 | } __sfu_res; }) |
| 395 | |
| 396 | /* |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 397 | * Return the size of a string (including the ending 0 even when we have |
| 398 | * exceeded the maximum string length). |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 399 | */ |
| 400 | static __inline__ long __strnlen_user(const char __user *__s, long __n) |
| 401 | { |
| 402 | unsigned long res; |
| 403 | unsigned long __dummy; |
| 404 | |
| 405 | __asm__ __volatile__( |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 406 | "1:\t" |
| 407 | "mov.b @(%0,%3), %1\n\t" |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 408 | "cmp/eq %4, %0\n\t" |
| 409 | "bt/s 2f\n\t" |
| 410 | " add #1, %0\n\t" |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 411 | "tst %1, %1\n\t" |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 412 | "bf 1b\n\t" |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 413 | "2:\n" |
| 414 | ".section .fixup,\"ax\"\n" |
| 415 | "3:\n\t" |
| 416 | "mov.l 4f, %1\n\t" |
| 417 | "jmp @%1\n\t" |
| 418 | " mov #0, %0\n" |
| 419 | ".balign 4\n" |
| 420 | "4: .long 2b\n" |
| 421 | ".previous\n" |
| 422 | ".section __ex_table,\"a\"\n" |
| 423 | " .balign 4\n" |
| 424 | " .long 1b,3b\n" |
| 425 | ".previous" |
| 426 | : "=z" (res), "=&r" (__dummy) |
| 427 | : "0" (0), "r" (__s), "r" (__n) |
| 428 | : "t"); |
| 429 | return res; |
| 430 | } |
| 431 | |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 432 | /** |
| 433 | * strnlen_user: - Get the size of a string in user space. |
| 434 | * @s: The string to measure. |
| 435 | * @n: The maximum valid length |
| 436 | * |
| 437 | * Context: User context only. This function may sleep. |
| 438 | * |
| 439 | * Get the size of a NUL-terminated string in user space. |
| 440 | * |
| 441 | * Returns the size of the string INCLUDING the terminating NUL. |
| 442 | * On exception, returns 0. |
| 443 | * If the string is too long, returns a value greater than @n. |
| 444 | */ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 445 | static __inline__ long strnlen_user(const char __user *s, long n) |
| 446 | { |
| 447 | if (!__addr_ok(s)) |
| 448 | return 0; |
| 449 | else |
| 450 | return __strnlen_user(s, n); |
| 451 | } |
| 452 | |
Stuart Menefy | 0fb19dc | 2007-11-30 18:16:23 +0900 | [diff] [blame] | 453 | /** |
| 454 | * strlen_user: - Get the size of a string in user space. |
| 455 | * @str: The string to measure. |
| 456 | * |
| 457 | * Context: User context only. This function may sleep. |
| 458 | * |
| 459 | * Get the size of a NUL-terminated string in user space. |
| 460 | * |
| 461 | * Returns the size of the string INCLUDING the terminating NUL. |
| 462 | * On exception, returns 0. |
| 463 | * |
| 464 | * If there is a limit on the length of a valid string, you may wish to |
| 465 | * consider using strnlen_user() instead. |
| 466 | */ |
Paul Mundt | 9b01bd9 | 2007-11-10 19:55:50 +0900 | [diff] [blame] | 467 | #define strlen_user(str) strnlen_user(str, ~0UL >> 1) |
| 468 | |
| 469 | /* |
| 470 | * The exception table consists of pairs of addresses: the first is the |
| 471 | * address of an instruction that is allowed to fault, and the second is |
| 472 | * the address at which the program should continue. No registers are |
| 473 | * modified, so it is entirely up to the continuation code to figure out |
| 474 | * what to do. |
| 475 | * |
| 476 | * All the routines below use bits of fixup code that are out of line |
| 477 | * with the main instruction path. This means when everything is well, |
| 478 | * we don't even have to jump over them. Further, they do not intrude |
| 479 | * on our cache or tlb entries. |
| 480 | */ |
| 481 | |
| 482 | struct exception_table_entry |
| 483 | { |
| 484 | unsigned long insn, fixup; |
| 485 | }; |
| 486 | |
| 487 | extern int fixup_exception(struct pt_regs *regs); |
| 488 | |
Magnus Damm | 1e6760c | 2008-02-07 19:50:52 +0900 | [diff] [blame] | 489 | #endif /* __ASM_SH_UACCESS_32_H */ |