Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: uaccess.h,v 1.35 2002/02/09 19:49:31 davem Exp $ */ |
| 2 | #ifndef _ASM_UACCESS_H |
| 3 | #define _ASM_UACCESS_H |
| 4 | |
| 5 | /* |
| 6 | * User space memory access functions |
| 7 | */ |
| 8 | |
| 9 | #ifdef __KERNEL__ |
| 10 | #include <linux/compiler.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/string.h> |
| 13 | #include <asm/a.out.h> |
| 14 | #include <asm/asi.h> |
| 15 | #include <asm/system.h> |
| 16 | #include <asm/spitfire.h> |
| 17 | #include <asm-generic/uaccess.h> |
| 18 | #endif |
| 19 | |
| 20 | #ifndef __ASSEMBLY__ |
| 21 | |
| 22 | /* |
| 23 | * Sparc64 is segmented, though more like the M68K than the I386. |
| 24 | * We use the secondary ASI to address user memory, which references a |
| 25 | * completely different VM map, thus there is zero chance of the user |
| 26 | * doing something queer and tricking us into poking kernel memory. |
| 27 | * |
| 28 | * What is left here is basically what is needed for the other parts of |
| 29 | * the kernel that expect to be able to manipulate, erum, "segments". |
| 30 | * Or perhaps more properly, permissions. |
| 31 | * |
| 32 | * "For historical reasons, these macros are grossly misnamed." -Linus |
| 33 | */ |
| 34 | |
| 35 | #define KERNEL_DS ((mm_segment_t) { ASI_P }) |
| 36 | #define USER_DS ((mm_segment_t) { ASI_AIUS }) /* har har har */ |
| 37 | |
| 38 | #define VERIFY_READ 0 |
| 39 | #define VERIFY_WRITE 1 |
| 40 | |
| 41 | #define get_fs() ((mm_segment_t) { get_thread_current_ds() }) |
| 42 | #define get_ds() (KERNEL_DS) |
| 43 | |
| 44 | #define segment_eq(a,b) ((a).seg == (b).seg) |
| 45 | |
| 46 | #define set_fs(val) \ |
| 47 | do { \ |
| 48 | set_thread_current_ds((val).seg); \ |
| 49 | __asm__ __volatile__ ("wr %%g0, %0, %%asi" : : "r" ((val).seg)); \ |
| 50 | } while(0) |
| 51 | |
| 52 | static inline int __access_ok(const void __user * addr, unsigned long size) |
| 53 | { |
| 54 | return 1; |
| 55 | } |
| 56 | |
| 57 | static inline int access_ok(int type, const void __user * addr, unsigned long size) |
| 58 | { |
| 59 | return 1; |
| 60 | } |
| 61 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 62 | /* |
| 63 | * The exception table consists of pairs of addresses: the first is the |
| 64 | * address of an instruction that is allowed to fault, and the second is |
| 65 | * the address at which the program should continue. No registers are |
| 66 | * modified, so it is entirely up to the continuation code to figure out |
| 67 | * what to do. |
| 68 | * |
| 69 | * All the routines below use bits of fixup code that are out of line |
| 70 | * with the main instruction path. This means when everything is well, |
| 71 | * we don't even have to jump over them. Further, they do not intrude |
| 72 | * on our cache or tlb entries. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
| 74 | |
David S. Miller | 8cf14af | 2005-09-28 20:21:11 -0700 | [diff] [blame] | 75 | struct exception_table_entry { |
| 76 | unsigned int insn, fixup; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | }; |
| 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | extern void __ret_efault(void); |
David S. Miller | 5fd2975 | 2005-09-28 20:41:45 -0700 | [diff] [blame] | 80 | extern void __retl_efault(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | /* Uh, these should become the main single-value transfer routines.. |
| 83 | * They automatically use the right size if we just have the right |
| 84 | * pointer type.. |
| 85 | * |
| 86 | * This gets kind of ugly. We want to return _two_ values in "get_user()" |
| 87 | * and yet we don't want to do any pointers, because that is too much |
| 88 | * of a performance impact. Thus we have a few rather ugly macros here, |
| 89 | * and hide all the ugliness from the user. |
| 90 | */ |
| 91 | #define put_user(x,ptr) ({ \ |
| 92 | unsigned long __pu_addr = (unsigned long)(ptr); \ |
| 93 | __chk_user_ptr(ptr); \ |
| 94 | __put_user_nocheck((__typeof__(*(ptr)))(x),__pu_addr,sizeof(*(ptr))); }) |
| 95 | |
| 96 | #define get_user(x,ptr) ({ \ |
| 97 | unsigned long __gu_addr = (unsigned long)(ptr); \ |
| 98 | __chk_user_ptr(ptr); \ |
| 99 | __get_user_nocheck((x),__gu_addr,sizeof(*(ptr)),__typeof__(*(ptr))); }) |
| 100 | |
| 101 | #define __put_user(x,ptr) put_user(x,ptr) |
| 102 | #define __get_user(x,ptr) get_user(x,ptr) |
| 103 | |
| 104 | struct __large_struct { unsigned long buf[100]; }; |
| 105 | #define __m(x) ((struct __large_struct *)(x)) |
| 106 | |
| 107 | #define __put_user_nocheck(data,addr,size) ({ \ |
| 108 | register int __pu_ret; \ |
| 109 | switch (size) { \ |
| 110 | case 1: __put_user_asm(data,b,addr,__pu_ret); break; \ |
| 111 | case 2: __put_user_asm(data,h,addr,__pu_ret); break; \ |
| 112 | case 4: __put_user_asm(data,w,addr,__pu_ret); break; \ |
| 113 | case 8: __put_user_asm(data,x,addr,__pu_ret); break; \ |
| 114 | default: __pu_ret = __put_user_bad(); break; \ |
| 115 | } __pu_ret; }) |
| 116 | |
| 117 | #define __put_user_nocheck_ret(data,addr,size,retval) ({ \ |
| 118 | register int __foo __asm__ ("l1"); \ |
| 119 | switch (size) { \ |
| 120 | case 1: __put_user_asm_ret(data,b,addr,retval,__foo); break; \ |
| 121 | case 2: __put_user_asm_ret(data,h,addr,retval,__foo); break; \ |
| 122 | case 4: __put_user_asm_ret(data,w,addr,retval,__foo); break; \ |
| 123 | case 8: __put_user_asm_ret(data,x,addr,retval,__foo); break; \ |
| 124 | default: if (__put_user_bad()) return retval; break; \ |
| 125 | } }) |
| 126 | |
| 127 | #define __put_user_asm(x,size,addr,ret) \ |
| 128 | __asm__ __volatile__( \ |
| 129 | "/* Put user asm, inline. */\n" \ |
| 130 | "1:\t" "st"#size "a %1, [%2] %%asi\n\t" \ |
| 131 | "clr %0\n" \ |
| 132 | "2:\n\n\t" \ |
| 133 | ".section .fixup,#alloc,#execinstr\n\t" \ |
| 134 | ".align 4\n" \ |
| 135 | "3:\n\t" \ |
| 136 | "b 2b\n\t" \ |
| 137 | " mov %3, %0\n\n\t" \ |
| 138 | ".previous\n\t" \ |
David S. Miller | 4d000d5 | 2006-03-04 23:23:56 -0800 | [diff] [blame^] | 139 | ".section __ex_table,\"a\"\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | ".align 4\n\t" \ |
| 141 | ".word 1b, 3b\n\t" \ |
| 142 | ".previous\n\n\t" \ |
| 143 | : "=r" (ret) : "r" (x), "r" (__m(addr)), \ |
| 144 | "i" (-EFAULT)) |
| 145 | |
| 146 | #define __put_user_asm_ret(x,size,addr,ret,foo) \ |
| 147 | if (__builtin_constant_p(ret) && ret == -EFAULT) \ |
| 148 | __asm__ __volatile__( \ |
| 149 | "/* Put user asm ret, inline. */\n" \ |
| 150 | "1:\t" "st"#size "a %1, [%2] %%asi\n\n\t" \ |
David S. Miller | 4d000d5 | 2006-03-04 23:23:56 -0800 | [diff] [blame^] | 151 | ".section __ex_table,\"a\"\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | ".align 4\n\t" \ |
| 153 | ".word 1b, __ret_efault\n\n\t" \ |
| 154 | ".previous\n\n\t" \ |
| 155 | : "=r" (foo) : "r" (x), "r" (__m(addr))); \ |
| 156 | else \ |
| 157 | __asm__ __volatile__( \ |
| 158 | "/* Put user asm ret, inline. */\n" \ |
| 159 | "1:\t" "st"#size "a %1, [%2] %%asi\n\n\t" \ |
| 160 | ".section .fixup,#alloc,#execinstr\n\t" \ |
| 161 | ".align 4\n" \ |
| 162 | "3:\n\t" \ |
| 163 | "ret\n\t" \ |
| 164 | " restore %%g0, %3, %%o0\n\n\t" \ |
| 165 | ".previous\n\t" \ |
David S. Miller | 4d000d5 | 2006-03-04 23:23:56 -0800 | [diff] [blame^] | 166 | ".section __ex_table,\"a\"\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | ".align 4\n\t" \ |
| 168 | ".word 1b, 3b\n\n\t" \ |
| 169 | ".previous\n\n\t" \ |
| 170 | : "=r" (foo) : "r" (x), "r" (__m(addr)), \ |
| 171 | "i" (ret)) |
| 172 | |
| 173 | extern int __put_user_bad(void); |
| 174 | |
| 175 | #define __get_user_nocheck(data,addr,size,type) ({ \ |
| 176 | register int __gu_ret; \ |
| 177 | register unsigned long __gu_val; \ |
| 178 | switch (size) { \ |
| 179 | case 1: __get_user_asm(__gu_val,ub,addr,__gu_ret); break; \ |
| 180 | case 2: __get_user_asm(__gu_val,uh,addr,__gu_ret); break; \ |
| 181 | case 4: __get_user_asm(__gu_val,uw,addr,__gu_ret); break; \ |
| 182 | case 8: __get_user_asm(__gu_val,x,addr,__gu_ret); break; \ |
| 183 | default: __gu_val = 0; __gu_ret = __get_user_bad(); break; \ |
| 184 | } data = (type) __gu_val; __gu_ret; }) |
| 185 | |
| 186 | #define __get_user_nocheck_ret(data,addr,size,type,retval) ({ \ |
| 187 | register unsigned long __gu_val __asm__ ("l1"); \ |
| 188 | switch (size) { \ |
| 189 | case 1: __get_user_asm_ret(__gu_val,ub,addr,retval); break; \ |
| 190 | case 2: __get_user_asm_ret(__gu_val,uh,addr,retval); break; \ |
| 191 | case 4: __get_user_asm_ret(__gu_val,uw,addr,retval); break; \ |
| 192 | case 8: __get_user_asm_ret(__gu_val,x,addr,retval); break; \ |
| 193 | default: if (__get_user_bad()) return retval; \ |
| 194 | } data = (type) __gu_val; }) |
| 195 | |
| 196 | #define __get_user_asm(x,size,addr,ret) \ |
| 197 | __asm__ __volatile__( \ |
| 198 | "/* Get user asm, inline. */\n" \ |
| 199 | "1:\t" "ld"#size "a [%2] %%asi, %1\n\t" \ |
| 200 | "clr %0\n" \ |
| 201 | "2:\n\n\t" \ |
| 202 | ".section .fixup,#alloc,#execinstr\n\t" \ |
| 203 | ".align 4\n" \ |
| 204 | "3:\n\t" \ |
| 205 | "clr %1\n\t" \ |
| 206 | "b 2b\n\t" \ |
| 207 | " mov %3, %0\n\n\t" \ |
| 208 | ".previous\n\t" \ |
David S. Miller | 4d000d5 | 2006-03-04 23:23:56 -0800 | [diff] [blame^] | 209 | ".section __ex_table,\"a\"\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | ".align 4\n\t" \ |
| 211 | ".word 1b, 3b\n\n\t" \ |
| 212 | ".previous\n\t" \ |
| 213 | : "=r" (ret), "=r" (x) : "r" (__m(addr)), \ |
| 214 | "i" (-EFAULT)) |
| 215 | |
| 216 | #define __get_user_asm_ret(x,size,addr,retval) \ |
| 217 | if (__builtin_constant_p(retval) && retval == -EFAULT) \ |
| 218 | __asm__ __volatile__( \ |
| 219 | "/* Get user asm ret, inline. */\n" \ |
| 220 | "1:\t" "ld"#size "a [%1] %%asi, %0\n\n\t" \ |
David S. Miller | 4d000d5 | 2006-03-04 23:23:56 -0800 | [diff] [blame^] | 221 | ".section __ex_table,\"a\"\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | ".align 4\n\t" \ |
| 223 | ".word 1b,__ret_efault\n\n\t" \ |
| 224 | ".previous\n\t" \ |
| 225 | : "=r" (x) : "r" (__m(addr))); \ |
| 226 | else \ |
| 227 | __asm__ __volatile__( \ |
| 228 | "/* Get user asm ret, inline. */\n" \ |
| 229 | "1:\t" "ld"#size "a [%1] %%asi, %0\n\n\t" \ |
| 230 | ".section .fixup,#alloc,#execinstr\n\t" \ |
| 231 | ".align 4\n" \ |
| 232 | "3:\n\t" \ |
| 233 | "ret\n\t" \ |
| 234 | " restore %%g0, %2, %%o0\n\n\t" \ |
| 235 | ".previous\n\t" \ |
David S. Miller | 4d000d5 | 2006-03-04 23:23:56 -0800 | [diff] [blame^] | 236 | ".section __ex_table,\"a\"\n\t" \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | ".align 4\n\t" \ |
| 238 | ".word 1b, 3b\n\n\t" \ |
| 239 | ".previous\n\t" \ |
| 240 | : "=r" (x) : "r" (__m(addr)), "i" (retval)) |
| 241 | |
| 242 | extern int __get_user_bad(void); |
| 243 | |
| 244 | extern unsigned long __must_check ___copy_from_user(void *to, |
| 245 | const void __user *from, |
| 246 | unsigned long size); |
| 247 | extern unsigned long copy_from_user_fixup(void *to, const void __user *from, |
| 248 | unsigned long size); |
| 249 | static inline unsigned long __must_check |
| 250 | copy_from_user(void *to, const void __user *from, unsigned long size) |
| 251 | { |
| 252 | unsigned long ret = ___copy_from_user(to, from, size); |
| 253 | |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 254 | if (unlikely(ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | ret = copy_from_user_fixup(to, from, size); |
| 256 | return ret; |
| 257 | } |
| 258 | #define __copy_from_user copy_from_user |
| 259 | |
| 260 | extern unsigned long __must_check ___copy_to_user(void __user *to, |
| 261 | const void *from, |
| 262 | unsigned long size); |
| 263 | extern unsigned long copy_to_user_fixup(void __user *to, const void *from, |
| 264 | unsigned long size); |
| 265 | static inline unsigned long __must_check |
| 266 | copy_to_user(void __user *to, const void *from, unsigned long size) |
| 267 | { |
| 268 | unsigned long ret = ___copy_to_user(to, from, size); |
| 269 | |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 270 | if (unlikely(ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | ret = copy_to_user_fixup(to, from, size); |
| 272 | return ret; |
| 273 | } |
| 274 | #define __copy_to_user copy_to_user |
| 275 | |
| 276 | extern unsigned long __must_check ___copy_in_user(void __user *to, |
| 277 | const void __user *from, |
| 278 | unsigned long size); |
| 279 | extern unsigned long copy_in_user_fixup(void __user *to, void __user *from, |
| 280 | unsigned long size); |
| 281 | static inline unsigned long __must_check |
| 282 | copy_in_user(void __user *to, void __user *from, unsigned long size) |
| 283 | { |
| 284 | unsigned long ret = ___copy_in_user(to, from, size); |
| 285 | |
David S. Miller | efdc1e2 | 2005-09-28 21:06:47 -0700 | [diff] [blame] | 286 | if (unlikely(ret)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | ret = copy_in_user_fixup(to, from, size); |
| 288 | return ret; |
| 289 | } |
| 290 | #define __copy_in_user copy_in_user |
| 291 | |
| 292 | extern unsigned long __must_check __bzero_noasi(void __user *, unsigned long); |
| 293 | |
| 294 | static inline unsigned long __must_check |
| 295 | __clear_user(void __user *addr, unsigned long size) |
| 296 | { |
| 297 | |
| 298 | return __bzero_noasi(addr, size); |
| 299 | } |
| 300 | |
| 301 | #define clear_user __clear_user |
| 302 | |
| 303 | extern long __must_check __strncpy_from_user(char *dest, const char __user *src, long count); |
| 304 | |
| 305 | #define strncpy_from_user __strncpy_from_user |
| 306 | |
| 307 | extern long __strlen_user(const char __user *); |
| 308 | extern long __strnlen_user(const char __user *, long len); |
| 309 | |
| 310 | #define strlen_user __strlen_user |
| 311 | #define strnlen_user __strnlen_user |
| 312 | #define __copy_to_user_inatomic __copy_to_user |
| 313 | #define __copy_from_user_inatomic __copy_from_user |
| 314 | |
| 315 | #endif /* __ASSEMBLY__ */ |
| 316 | |
| 317 | #endif /* _ASM_UACCESS_H */ |