Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame^] | 1 | #ifndef _PPC64_UACCESS_H |
| 2 | #define _PPC64_UACCESS_H |
| 3 | |
| 4 | /* |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of the GNU General Public License |
| 7 | * as published by the Free Software Foundation; either version |
| 8 | * 2 of the License, or (at your option) any later version. |
| 9 | */ |
| 10 | |
| 11 | #ifndef __ASSEMBLY__ |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/errno.h> |
| 14 | #include <asm/processor.h> |
| 15 | |
| 16 | #define VERIFY_READ 0 |
| 17 | #define VERIFY_WRITE 1 |
| 18 | |
| 19 | /* |
| 20 | * The fs value determines whether argument validity checking should be |
| 21 | * performed or not. If get_fs() == USER_DS, checking is performed, with |
| 22 | * get_fs() == KERNEL_DS, checking is bypassed. |
| 23 | * |
| 24 | * For historical reasons, these macros are grossly misnamed. |
| 25 | */ |
| 26 | |
| 27 | #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) }) |
| 28 | |
| 29 | #define KERNEL_DS MAKE_MM_SEG(0UL) |
| 30 | #define USER_DS MAKE_MM_SEG(0xf000000000000000UL) |
| 31 | |
| 32 | #define get_ds() (KERNEL_DS) |
| 33 | #define get_fs() (current->thread.fs) |
| 34 | #define set_fs(val) (current->thread.fs = (val)) |
| 35 | |
| 36 | #define segment_eq(a,b) ((a).seg == (b).seg) |
| 37 | |
| 38 | /* |
| 39 | * Use the alpha trick for checking ranges: |
| 40 | * |
| 41 | * Is a address valid? This does a straightforward calculation rather |
| 42 | * than tests. |
| 43 | * |
| 44 | * Address valid if: |
| 45 | * - "addr" doesn't have any high-bits set |
| 46 | * - AND "size" doesn't have any high-bits set |
| 47 | * - OR we are in kernel mode. |
| 48 | * |
| 49 | * We dont have to check for high bits in (addr+size) because the first |
| 50 | * two checks force the maximum result to be below the start of the |
| 51 | * kernel region. |
| 52 | */ |
| 53 | #define __access_ok(addr,size,segment) \ |
| 54 | (((segment).seg & (addr | size )) == 0) |
| 55 | |
| 56 | #define access_ok(type,addr,size) \ |
| 57 | __access_ok(((__force unsigned long)(addr)),(size),get_fs()) |
| 58 | |
| 59 | /* this function will go away soon - use access_ok() instead */ |
| 60 | static inline int __deprecated verify_area(int type, const void __user *addr, unsigned long size) |
| 61 | { |
| 62 | return access_ok(type,addr,size) ? 0 : -EFAULT; |
| 63 | } |
| 64 | |
| 65 | |
| 66 | /* |
| 67 | * The exception table consists of pairs of addresses: the first is the |
| 68 | * address of an instruction that is allowed to fault, and the second is |
| 69 | * the address at which the program should continue. No registers are |
| 70 | * modified, so it is entirely up to the continuation code to figure out |
| 71 | * what to do. |
| 72 | * |
| 73 | * All the routines below use bits of fixup code that are out of line |
| 74 | * with the main instruction path. This means when everything is well, |
| 75 | * we don't even have to jump over them. Further, they do not intrude |
| 76 | * on our cache or tlb entries. |
| 77 | */ |
| 78 | |
| 79 | struct exception_table_entry |
| 80 | { |
| 81 | unsigned long insn, fixup; |
| 82 | }; |
| 83 | |
| 84 | /* Returns 0 if exception not found and fixup otherwise. */ |
| 85 | extern unsigned long search_exception_table(unsigned long); |
| 86 | |
| 87 | /* |
| 88 | * These are the main single-value transfer routines. They automatically |
| 89 | * use the right size if we just have the right pointer type. |
| 90 | * |
| 91 | * This gets kind of ugly. We want to return _two_ values in "get_user()" |
| 92 | * and yet we don't want to do any pointers, because that is too much |
| 93 | * of a performance impact. Thus we have a few rather ugly macros here, |
| 94 | * and hide all the ugliness from the user. |
| 95 | * |
| 96 | * The "__xxx" versions of the user access functions are versions that |
| 97 | * do not verify the address space, that must have been done previously |
| 98 | * with a separate "access_ok()" call (this is used when we do multiple |
| 99 | * accesses to the same area of user memory). |
| 100 | * |
| 101 | * As we use the same address space for kernel and user data on the |
| 102 | * PowerPC, we can just do these as direct assignments. (Of course, the |
| 103 | * exception handling means that it's no longer "just"...) |
| 104 | */ |
| 105 | #define get_user(x,ptr) \ |
| 106 | __get_user_check((x),(ptr),sizeof(*(ptr))) |
| 107 | #define put_user(x,ptr) \ |
| 108 | __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr))) |
| 109 | |
| 110 | #define __get_user(x,ptr) \ |
| 111 | __get_user_nocheck((x),(ptr),sizeof(*(ptr))) |
| 112 | #define __put_user(x,ptr) \ |
| 113 | __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr))) |
| 114 | |
| 115 | #define __get_user_unaligned __get_user |
| 116 | #define __put_user_unaligned __put_user |
| 117 | |
| 118 | extern long __put_user_bad(void); |
| 119 | |
| 120 | #define __put_user_nocheck(x,ptr,size) \ |
| 121 | ({ \ |
| 122 | long __pu_err; \ |
| 123 | might_sleep(); \ |
| 124 | __chk_user_ptr(ptr); \ |
| 125 | __put_user_size((x),(ptr),(size),__pu_err,-EFAULT); \ |
| 126 | __pu_err; \ |
| 127 | }) |
| 128 | |
| 129 | #define __put_user_check(x,ptr,size) \ |
| 130 | ({ \ |
| 131 | long __pu_err = -EFAULT; \ |
| 132 | void __user *__pu_addr = (ptr); \ |
| 133 | might_sleep(); \ |
| 134 | if (access_ok(VERIFY_WRITE,__pu_addr,size)) \ |
| 135 | __put_user_size((x),__pu_addr,(size),__pu_err,-EFAULT); \ |
| 136 | __pu_err; \ |
| 137 | }) |
| 138 | |
| 139 | #define __put_user_size(x,ptr,size,retval,errret) \ |
| 140 | do { \ |
| 141 | retval = 0; \ |
| 142 | switch (size) { \ |
| 143 | case 1: __put_user_asm(x,ptr,retval,"stb",errret); break; \ |
| 144 | case 2: __put_user_asm(x,ptr,retval,"sth",errret); break; \ |
| 145 | case 4: __put_user_asm(x,ptr,retval,"stw",errret); break; \ |
| 146 | case 8: __put_user_asm(x,ptr,retval,"std",errret); break; \ |
| 147 | default: __put_user_bad(); \ |
| 148 | } \ |
| 149 | } while (0) |
| 150 | |
| 151 | /* |
| 152 | * We don't tell gcc that we are accessing memory, but this is OK |
| 153 | * because we do not write to any memory gcc knows about, so there |
| 154 | * are no aliasing issues. |
| 155 | */ |
| 156 | #define __put_user_asm(x, addr, err, op, errret) \ |
| 157 | __asm__ __volatile__( \ |
| 158 | "1: "op" %1,0(%2) # put_user\n" \ |
| 159 | "2:\n" \ |
| 160 | ".section .fixup,\"ax\"\n" \ |
| 161 | "3: li %0,%3\n" \ |
| 162 | " b 2b\n" \ |
| 163 | ".previous\n" \ |
| 164 | ".section __ex_table,\"a\"\n" \ |
| 165 | " .align 3\n" \ |
| 166 | " .llong 1b,3b\n" \ |
| 167 | ".previous" \ |
| 168 | : "=r"(err) \ |
| 169 | : "r"(x), "b"(addr), "i"(errret), "0"(err)) |
| 170 | |
| 171 | |
| 172 | #define __get_user_nocheck(x,ptr,size) \ |
| 173 | ({ \ |
| 174 | long __gu_err, __gu_val; \ |
| 175 | might_sleep(); \ |
| 176 | __get_user_size(__gu_val,(ptr),(size),__gu_err,-EFAULT);\ |
| 177 | (x) = (__typeof__(*(ptr)))__gu_val; \ |
| 178 | __gu_err; \ |
| 179 | }) |
| 180 | |
| 181 | #define __get_user_check(x,ptr,size) \ |
| 182 | ({ \ |
| 183 | long __gu_err = -EFAULT, __gu_val = 0; \ |
| 184 | const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \ |
| 185 | might_sleep(); \ |
| 186 | if (access_ok(VERIFY_READ,__gu_addr,size)) \ |
| 187 | __get_user_size(__gu_val,__gu_addr,(size),__gu_err,-EFAULT);\ |
| 188 | (x) = (__typeof__(*(ptr)))__gu_val; \ |
| 189 | __gu_err; \ |
| 190 | }) |
| 191 | |
| 192 | extern long __get_user_bad(void); |
| 193 | |
| 194 | #define __get_user_size(x,ptr,size,retval,errret) \ |
| 195 | do { \ |
| 196 | retval = 0; \ |
| 197 | __chk_user_ptr(ptr); \ |
| 198 | switch (size) { \ |
| 199 | case 1: __get_user_asm(x,ptr,retval,"lbz",errret); break; \ |
| 200 | case 2: __get_user_asm(x,ptr,retval,"lhz",errret); break; \ |
| 201 | case 4: __get_user_asm(x,ptr,retval,"lwz",errret); break; \ |
| 202 | case 8: __get_user_asm(x,ptr,retval,"ld",errret); break; \ |
| 203 | default: (x) = __get_user_bad(); \ |
| 204 | } \ |
| 205 | } while (0) |
| 206 | |
| 207 | #define __get_user_asm(x, addr, err, op, errret) \ |
| 208 | __asm__ __volatile__( \ |
| 209 | "1: "op" %1,0(%2) # get_user\n" \ |
| 210 | "2:\n" \ |
| 211 | ".section .fixup,\"ax\"\n" \ |
| 212 | "3: li %0,%3\n" \ |
| 213 | " li %1,0\n" \ |
| 214 | " b 2b\n" \ |
| 215 | ".previous\n" \ |
| 216 | ".section __ex_table,\"a\"\n" \ |
| 217 | " .align 3\n" \ |
| 218 | " .llong 1b,3b\n" \ |
| 219 | ".previous" \ |
| 220 | : "=r"(err), "=r"(x) \ |
| 221 | : "b"(addr), "i"(errret), "0"(err)) |
| 222 | |
| 223 | /* more complex routines */ |
| 224 | |
| 225 | extern unsigned long __copy_tofrom_user(void __user *to, const void __user *from, |
| 226 | unsigned long size); |
| 227 | |
| 228 | static inline unsigned long |
| 229 | __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) |
| 230 | { |
| 231 | if (__builtin_constant_p(n)) { |
| 232 | unsigned long ret; |
| 233 | |
| 234 | switch (n) { |
| 235 | case 1: |
| 236 | __get_user_size(*(u8 *)to, from, 1, ret, 1); |
| 237 | return ret; |
| 238 | case 2: |
| 239 | __get_user_size(*(u16 *)to, from, 2, ret, 2); |
| 240 | return ret; |
| 241 | case 4: |
| 242 | __get_user_size(*(u32 *)to, from, 4, ret, 4); |
| 243 | return ret; |
| 244 | case 8: |
| 245 | __get_user_size(*(u64 *)to, from, 8, ret, 8); |
| 246 | return ret; |
| 247 | } |
| 248 | } |
| 249 | return __copy_tofrom_user((__force void __user *) to, from, n); |
| 250 | } |
| 251 | |
| 252 | static inline unsigned long |
| 253 | __copy_from_user(void *to, const void __user *from, unsigned long n) |
| 254 | { |
| 255 | might_sleep(); |
| 256 | return __copy_from_user_inatomic(to, from, n); |
| 257 | } |
| 258 | |
| 259 | static inline unsigned long |
| 260 | __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n) |
| 261 | { |
| 262 | if (__builtin_constant_p(n)) { |
| 263 | unsigned long ret; |
| 264 | |
| 265 | switch (n) { |
| 266 | case 1: |
| 267 | __put_user_size(*(u8 *)from, (u8 __user *)to, 1, ret, 1); |
| 268 | return ret; |
| 269 | case 2: |
| 270 | __put_user_size(*(u16 *)from, (u16 __user *)to, 2, ret, 2); |
| 271 | return ret; |
| 272 | case 4: |
| 273 | __put_user_size(*(u32 *)from, (u32 __user *)to, 4, ret, 4); |
| 274 | return ret; |
| 275 | case 8: |
| 276 | __put_user_size(*(u64 *)from, (u64 __user *)to, 8, ret, 8); |
| 277 | return ret; |
| 278 | } |
| 279 | } |
| 280 | return __copy_tofrom_user(to, (__force const void __user *) from, n); |
| 281 | } |
| 282 | |
| 283 | static inline unsigned long |
| 284 | __copy_to_user(void __user *to, const void *from, unsigned long n) |
| 285 | { |
| 286 | might_sleep(); |
| 287 | return __copy_to_user_inatomic(to, from, n); |
| 288 | } |
| 289 | |
| 290 | #define __copy_in_user(to, from, size) \ |
| 291 | __copy_tofrom_user((to), (from), (size)) |
| 292 | |
| 293 | extern unsigned long copy_from_user(void *to, const void __user *from, |
| 294 | unsigned long n); |
| 295 | extern unsigned long copy_to_user(void __user *to, const void *from, |
| 296 | unsigned long n); |
| 297 | extern unsigned long copy_in_user(void __user *to, const void __user *from, |
| 298 | unsigned long n); |
| 299 | |
| 300 | extern unsigned long __clear_user(void __user *addr, unsigned long size); |
| 301 | |
| 302 | static inline unsigned long |
| 303 | clear_user(void __user *addr, unsigned long size) |
| 304 | { |
| 305 | might_sleep(); |
| 306 | if (likely(access_ok(VERIFY_WRITE, addr, size))) |
| 307 | size = __clear_user(addr, size); |
| 308 | return size; |
| 309 | } |
| 310 | |
| 311 | extern int __strncpy_from_user(char *dst, const char __user *src, long count); |
| 312 | |
| 313 | static inline long |
| 314 | strncpy_from_user(char *dst, const char __user *src, long count) |
| 315 | { |
| 316 | might_sleep(); |
| 317 | if (likely(access_ok(VERIFY_READ, src, 1))) |
| 318 | return __strncpy_from_user(dst, src, count); |
| 319 | return -EFAULT; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | * Return the size of a string (including the ending 0) |
| 324 | * |
| 325 | * Return 0 for error |
| 326 | */ |
| 327 | extern int __strnlen_user(const char __user *str, long len); |
| 328 | |
| 329 | /* |
| 330 | * Returns the length of the string at str (including the null byte), |
| 331 | * or 0 if we hit a page we can't access, |
| 332 | * or something > len if we didn't find a null byte. |
| 333 | */ |
| 334 | static inline int strnlen_user(const char __user *str, long len) |
| 335 | { |
| 336 | might_sleep(); |
| 337 | if (likely(access_ok(VERIFY_READ, str, 1))) |
| 338 | return __strnlen_user(str, len); |
| 339 | return 0; |
| 340 | } |
| 341 | |
| 342 | #define strlen_user(str) strnlen_user((str), 0x7ffffffe) |
| 343 | |
| 344 | #endif /* __ASSEMBLY__ */ |
| 345 | |
| 346 | #endif /* _PPC64_UACCESS_H */ |