Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 1 | /* Changes made by Lineo Inc. May 2001 |
| 2 | * |
| 3 | * Based on: include/asm-m68knommu/uaccess.h |
| 4 | */ |
| 5 | |
| 6 | #ifndef __BLACKFIN_UACCESS_H |
| 7 | #define __BLACKFIN_UACCESS_H |
| 8 | |
| 9 | /* |
| 10 | * User space memory access functions |
| 11 | */ |
| 12 | #include <linux/sched.h> |
| 13 | #include <linux/mm.h> |
| 14 | #include <linux/string.h> |
| 15 | |
| 16 | #include <asm/segment.h> |
Sonic Zhang | bde7db8 | 2007-05-21 18:09:34 +0800 | [diff] [blame] | 17 | #ifdef CONFIG_ACCESS_CHECK |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 18 | # include <asm/bfin-global.h> |
| 19 | #endif |
| 20 | |
| 21 | #define get_ds() (KERNEL_DS) |
| 22 | #define get_fs() (current_thread_info()->addr_limit) |
| 23 | |
| 24 | static inline void set_fs(mm_segment_t fs) |
| 25 | { |
| 26 | current_thread_info()->addr_limit = fs; |
| 27 | } |
| 28 | |
| 29 | #define segment_eq(a,b) ((a) == (b)) |
| 30 | |
| 31 | #define VERIFY_READ 0 |
| 32 | #define VERIFY_WRITE 1 |
| 33 | |
Bernd Schmidt | 3ca32c1 | 2007-12-24 12:40:29 +0800 | [diff] [blame] | 34 | #define access_ok(type, addr, size) _access_ok((unsigned long)(addr), (size)) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 35 | |
| 36 | static inline int is_in_rom(unsigned long addr) |
| 37 | { |
| 38 | /* |
| 39 | * What we are really trying to do is determine if addr is |
| 40 | * in an allocated kernel memory region. If not then assume |
| 41 | * we cannot free it or otherwise de-allocate it. Ideally |
| 42 | * we could restrict this to really being in a ROM or flash, |
| 43 | * but that would need to be done on a board by board basis, |
| 44 | * not globally. |
| 45 | */ |
| 46 | if ((addr < _ramstart) || (addr >= _ramend)) |
| 47 | return (1); |
| 48 | |
| 49 | /* Default case, not in ROM */ |
| 50 | return (0); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * The fs value determines whether argument validity checking should be |
| 55 | * performed or not. If get_fs() == USER_DS, checking is performed, with |
| 56 | * get_fs() == KERNEL_DS, checking is bypassed. |
| 57 | */ |
| 58 | |
Sonic Zhang | bde7db8 | 2007-05-21 18:09:34 +0800 | [diff] [blame] | 59 | #ifndef CONFIG_ACCESS_CHECK |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 60 | static inline int _access_ok(unsigned long addr, unsigned long size) { return 1; } |
| 61 | #else |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 62 | extern int _access_ok(unsigned long addr, unsigned long size); |
| 63 | #endif |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 64 | |
| 65 | /* |
| 66 | * The exception table consists of pairs of addresses: the first is the |
| 67 | * address of an instruction that is allowed to fault, and the second is |
| 68 | * the address at which the program should continue. No registers are |
| 69 | * modified, so it is entirely up to the continuation code to figure out |
| 70 | * what to do. |
| 71 | * |
| 72 | * All the routines below use bits of fixup code that are out of line |
| 73 | * with the main instruction path. This means when everything is well, |
| 74 | * we don't even have to jump over them. Further, they do not intrude |
| 75 | * on our cache or tlb entries. |
| 76 | */ |
| 77 | |
| 78 | struct exception_table_entry { |
| 79 | unsigned long insn, fixup; |
| 80 | }; |
| 81 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 82 | /* |
| 83 | * These are the main single-value transfer routines. They automatically |
| 84 | * use the right size if we just have the right pointer type. |
| 85 | */ |
| 86 | |
| 87 | #define put_user(x,p) \ |
| 88 | ({ \ |
| 89 | int _err = 0; \ |
| 90 | typeof(*(p)) _x = (x); \ |
| 91 | typeof(*(p)) *_p = (p); \ |
| 92 | if (!access_ok(VERIFY_WRITE, _p, sizeof(*(_p)))) {\ |
| 93 | _err = -EFAULT; \ |
| 94 | } \ |
| 95 | else { \ |
| 96 | switch (sizeof (*(_p))) { \ |
| 97 | case 1: \ |
| 98 | __put_user_asm(_x, _p, B); \ |
| 99 | break; \ |
| 100 | case 2: \ |
| 101 | __put_user_asm(_x, _p, W); \ |
| 102 | break; \ |
| 103 | case 4: \ |
| 104 | __put_user_asm(_x, _p, ); \ |
| 105 | break; \ |
| 106 | case 8: { \ |
| 107 | long _xl, _xh; \ |
| 108 | _xl = ((long *)&_x)[0]; \ |
| 109 | _xh = ((long *)&_x)[1]; \ |
| 110 | __put_user_asm(_xl, ((long *)_p)+0, ); \ |
| 111 | __put_user_asm(_xh, ((long *)_p)+1, ); \ |
| 112 | } break; \ |
| 113 | default: \ |
| 114 | _err = __put_user_bad(); \ |
| 115 | break; \ |
| 116 | } \ |
| 117 | } \ |
| 118 | _err; \ |
| 119 | }) |
| 120 | |
| 121 | #define __put_user(x,p) put_user(x,p) |
| 122 | static inline int bad_user_access_length(void) |
| 123 | { |
| 124 | panic("bad_user_access_length"); |
| 125 | return -1; |
| 126 | } |
| 127 | |
| 128 | #define __put_user_bad() (printk(KERN_INFO "put_user_bad %s:%d %s\n",\ |
Harvey Harrison | b85d858 | 2008-04-23 09:39:01 +0800 | [diff] [blame] | 129 | __FILE__, __LINE__, __func__),\ |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 130 | bad_user_access_length(), (-EFAULT)) |
| 131 | |
| 132 | /* |
| 133 | * Tell gcc we read from memory instead of writing: this is because |
| 134 | * we do not write to any memory gcc knows about, so there are no |
| 135 | * aliasing issues. |
| 136 | */ |
| 137 | |
| 138 | #define __ptr(x) ((unsigned long *)(x)) |
| 139 | |
| 140 | #define __put_user_asm(x,p,bhw) \ |
| 141 | __asm__ (#bhw"[%1] = %0;\n\t" \ |
| 142 | : /* no outputs */ \ |
| 143 | :"d" (x),"a" (__ptr(p)) : "memory") |
| 144 | |
Mike Frysinger | 5ff294f | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 145 | #define get_user(x, ptr) \ |
| 146 | ({ \ |
| 147 | int _err = 0; \ |
| 148 | unsigned long _val = 0; \ |
| 149 | const typeof(*(ptr)) __user *_p = (ptr); \ |
| 150 | const size_t ptr_size = sizeof(*(_p)); \ |
| 151 | if (likely(access_ok(VERIFY_READ, _p, ptr_size))) { \ |
| 152 | BUILD_BUG_ON(ptr_size >= 8); \ |
| 153 | switch (ptr_size) { \ |
| 154 | case 1: \ |
| 155 | __get_user_asm(_val, _p, B,(Z)); \ |
| 156 | break; \ |
| 157 | case 2: \ |
| 158 | __get_user_asm(_val, _p, W,(Z)); \ |
| 159 | break; \ |
| 160 | case 4: \ |
| 161 | __get_user_asm(_val, _p, , ); \ |
| 162 | break; \ |
| 163 | } \ |
| 164 | } else \ |
| 165 | _err = -EFAULT; \ |
| 166 | x = (typeof(*(ptr)))_val; \ |
| 167 | _err; \ |
| 168 | }) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 169 | |
| 170 | #define __get_user(x,p) get_user(x,p) |
| 171 | |
| 172 | #define __get_user_bad() (bad_user_access_length(), (-EFAULT)) |
| 173 | |
Mike Frysinger | 5ff294f | 2008-11-18 17:48:22 +0800 | [diff] [blame] | 174 | #define __get_user_asm(x, ptr, bhw, option) \ |
| 175 | ({ \ |
| 176 | __asm__ __volatile__ ( \ |
| 177 | "%0 =" #bhw "[%1]" #option ";" \ |
| 178 | : "=d" (x) \ |
| 179 | : "a" (__ptr(ptr))); \ |
| 180 | }) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 181 | |
| 182 | #define __copy_from_user(to, from, n) copy_from_user(to, from, n) |
| 183 | #define __copy_to_user(to, from, n) copy_to_user(to, from, n) |
| 184 | #define __copy_to_user_inatomic __copy_to_user |
| 185 | #define __copy_from_user_inatomic __copy_from_user |
| 186 | |
| 187 | #define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n))\ |
| 188 | return retval; }) |
| 189 | |
| 190 | #define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n))\ |
| 191 | return retval; }) |
| 192 | |
Mike Frysinger | 75aca61 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 193 | static inline unsigned long __must_check |
| 194 | copy_from_user(void *to, const void __user *from, unsigned long n) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 195 | { |
| 196 | if (access_ok(VERIFY_READ, from, n)) |
| 197 | memcpy(to, from, n); |
| 198 | else |
| 199 | return n; |
| 200 | return 0; |
| 201 | } |
| 202 | |
Mike Frysinger | 75aca61 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 203 | static inline unsigned long __must_check |
| 204 | copy_to_user(void *to, const void __user *from, unsigned long n) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 205 | { |
| 206 | if (access_ok(VERIFY_WRITE, to, n)) |
| 207 | memcpy(to, from, n); |
| 208 | else |
| 209 | return n; |
| 210 | return 0; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Copy a null terminated string from userspace. |
| 215 | */ |
| 216 | |
Mike Frysinger | 75aca61 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 217 | static inline long __must_check |
| 218 | strncpy_from_user(char *dst, const char *src, long count) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 219 | { |
| 220 | char *tmp; |
| 221 | if (!access_ok(VERIFY_READ, src, 1)) |
| 222 | return -EFAULT; |
| 223 | strncpy(dst, src, count); |
| 224 | for (tmp = dst; *tmp && count > 0; tmp++, count--) ; |
| 225 | return (tmp - dst); |
| 226 | } |
| 227 | |
| 228 | /* |
Robin Getz | a8372b5 | 2009-06-04 00:32:59 +0000 | [diff] [blame] | 229 | * Get the size of a string in user space. |
| 230 | * src: The string to measure |
| 231 | * n: The maximum valid length |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 232 | * |
Robin Getz | a8372b5 | 2009-06-04 00:32:59 +0000 | [diff] [blame] | 233 | * Get the size of a NUL-terminated string in user space. |
| 234 | * |
| 235 | * Returns the size of the string INCLUDING the terminating NUL. |
| 236 | * On exception, returns 0. |
| 237 | * If the string is too long, returns a value greater than n. |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 238 | */ |
Robin Getz | a8372b5 | 2009-06-04 00:32:59 +0000 | [diff] [blame] | 239 | static inline long __must_check strnlen_user(const char *src, long n) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 240 | { |
Robin Getz | a8372b5 | 2009-06-04 00:32:59 +0000 | [diff] [blame] | 241 | if (!access_ok(VERIFY_READ, src, 1)) |
| 242 | return 0; |
| 243 | return strnlen(src, n) + 1; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 244 | } |
| 245 | |
Robin Getz | a8372b5 | 2009-06-04 00:32:59 +0000 | [diff] [blame] | 246 | static inline long __must_check strlen_user(const char *src) |
| 247 | { |
| 248 | if (!access_ok(VERIFY_READ, src, 1)) |
| 249 | return 0; |
| 250 | return strlen(src) + 1; |
| 251 | } |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 252 | |
| 253 | /* |
| 254 | * Zero Userspace |
| 255 | */ |
| 256 | |
Mike Frysinger | 75aca61 | 2009-01-07 23:14:38 +0800 | [diff] [blame] | 257 | static inline unsigned long __must_check |
| 258 | __clear_user(void *to, unsigned long n) |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 259 | { |
Robin Getz | a8372b5 | 2009-06-04 00:32:59 +0000 | [diff] [blame] | 260 | if (!access_ok(VERIFY_WRITE, to, n)) |
| 261 | return n; |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 262 | memset(to, 0, n); |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | #define clear_user(to, n) __clear_user(to, n) |
| 267 | |
Mike Frysinger | e56e03b | 2009-06-07 16:31:52 -0400 | [diff] [blame] | 268 | /* How to interpret these return values: |
| 269 | * CORE: can be accessed by core load or dma memcpy |
| 270 | * CORE_ONLY: can only be accessed by core load |
| 271 | * DMA: can only be accessed by dma memcpy |
| 272 | * IDMA: can only be accessed by interprocessor dma memcpy (BF561) |
| 273 | * ITEST: can be accessed by isram memcpy or dma memcpy |
| 274 | */ |
| 275 | enum { |
| 276 | BFIN_MEM_ACCESS_CORE = 0, |
| 277 | BFIN_MEM_ACCESS_CORE_ONLY, |
| 278 | BFIN_MEM_ACCESS_DMA, |
| 279 | BFIN_MEM_ACCESS_IDMA, |
| 280 | BFIN_MEM_ACCESS_ITEST, |
| 281 | }; |
| 282 | /** |
| 283 | * bfin_mem_access_type() - what kind of memory access is required |
| 284 | * @addr: the address to check |
| 285 | * @size: number of bytes needed |
| 286 | * @return: <0 is error, >=0 is BFIN_MEM_ACCESS_xxx enum (see above) |
| 287 | */ |
| 288 | int bfin_mem_access_type(unsigned long addr, unsigned long size); |
| 289 | |
Bryan Wu | 1394f03 | 2007-05-06 14:50:22 -0700 | [diff] [blame] | 290 | #endif /* _BLACKFIN_UACCESS_H */ |