| 1 |
|
| 2 | |
| 3 | * Copyright (C) 2008-2009 Michal Simek <[email protected]>
|
| 4 | * Copyright (C) 2008-2009 PetaLogix
|
| 5 | * Copyright (C) 2006 Atmark Techno, Inc.
|
| 6 |
|
| 7 |
|
| 8 | #ifndef _ASM_MICROBLAZE_UACCESS_H
|
| 9 | #define _ASM_MICROBLAZE_UACCESS_H
|
| 10 |
|
| 11 | #include <linux/kernel.h>
|
| 12 |
|
| 13 | #include <asm/mmu.h>
|
| 14 | #include <asm/page.h>
|
| 15 | #include <linux/pgtable.h>
|
| 16 | #include <asm/extable.h>
|
| 17 | #include <linux/string.h>
|
| 18 | #include <asm-generic/access_ok.h>
|
| 19 |
|
| 20 | # define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
|
| 21 | # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
|
| 22 |
|
| 23 | extern unsigned long __copy_tofrom_user(void __user *to,
|
| 24 | const void __user *from, unsigned long size);
|
| 25 |
|
| 26 |
|
| 27 | static inline unsigned long __must_check __clear_user(void __user *to,
|
| 28 | unsigned long n)
|
| 29 | {
|
| 30 |
|
| 31 | __asm__ __volatile__ ( \
|
| 32 | "1: sb r0, %1, r0;" \
|
| 33 | " addik %0, %0, -1;" \
|
| 34 | " bneid %0, 1b;" \
|
| 35 | " addik %1, %1, 1;" \
|
| 36 | "2: " \
|
| 37 | __EX_TABLE_SECTION \
|
| 38 | ".word 1b,2b;" \
|
| 39 | ".previous;" \
|
| 40 | : "=r"(n), "=r"(to) \
|
| 41 | : "0"(n), "1"(to)
|
| 42 | );
|
| 43 | return n;
|
| 44 | }
|
| 45 |
|
| 46 | static inline unsigned long __must_check clear_user(void __user *to,
|
| 47 | unsigned long n)
|
| 48 | {
|
| 49 | might_fault();
|
| 50 | if (unlikely(!access_ok(to, n)))
|
| 51 | return n;
|
| 52 |
|
| 53 | return __clear_user(to, n);
|
| 54 | }
|
| 55 |
|
| 56 |
|
| 57 | extern long __user_bad(void);
|
| 58 |
|
| 59 | #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
|
| 60 | ({ \
|
| 61 | __asm__ __volatile__ ( \
|
| 62 | "1:" insn " %1, %2, r0;" \
|
| 63 | " addk %0, r0, r0;" \
|
| 64 | "2: " \
|
| 65 | __FIXUP_SECTION \
|
| 66 | "3: brid 2b;" \
|
| 67 | " addik %0, r0, %3;" \
|
| 68 | ".previous;" \
|
| 69 | __EX_TABLE_SECTION \
|
| 70 | ".word 1b,3b;" \
|
| 71 | ".previous;" \
|
| 72 | : "=&r"(__gu_err), "=r"(__gu_val) \
|
| 73 | : "r"(__gu_ptr), "i"(-EFAULT) \
|
| 74 | ); \
|
| 75 | })
|
| 76 |
|
| 77 | |
| 78 | * get_user: - Get a simple variable from user space.
|
| 79 | * @x: Variable to store result.
|
| 80 | * @ptr: Source address, in user space.
|
| 81 | *
|
| 82 | * Context: User context only. This function may sleep if pagefaults are
|
| 83 | * enabled.
|
| 84 | *
|
| 85 | * This macro copies a single simple variable from user space to kernel
|
| 86 | * space. It supports simple types like char and int, but not larger
|
| 87 | * data types like structures or arrays.
|
| 88 | *
|
| 89 | * @ptr must have pointer-to-simple-variable type, and the result of
|
| 90 | * dereferencing @ptr must be assignable to @x without a cast.
|
| 91 | *
|
| 92 | * Returns zero on success, or -EFAULT on error.
|
| 93 | * On error, the variable @x is set to zero.
|
| 94 |
|
| 95 | #define get_user(x, ptr) ({ \
|
| 96 | const typeof(*(ptr)) __user *__gu_ptr = (ptr); \
|
| 97 | access_ok(__gu_ptr, sizeof(*__gu_ptr)) ? \
|
| 98 | __get_user(x, __gu_ptr) : \
|
| 99 | ((x) = 0, -EFAULT); \
|
| 100 | })
|
| 101 |
|
| 102 | #define __get_user(x, ptr) \
|
| 103 | ({ \
|
| 104 | long __gu_err; \
|
| 105 | switch (sizeof(*(ptr))) { \
|
| 106 | case 1: \
|
| 107 | __get_user_asm("lbu", (ptr), x, __gu_err); \
|
| 108 | break; \
|
| 109 | case 2: \
|
| 110 | __get_user_asm("lhu", (ptr), x, __gu_err); \
|
| 111 | break; \
|
| 112 | case 4: \
|
| 113 | __get_user_asm("lw", (ptr), x, __gu_err); \
|
| 114 | break; \
|
| 115 | case 8: { \
|
| 116 | __u64 __x = 0; \
|
| 117 | __gu_err = raw_copy_from_user(&__x, ptr, 8) ? \
|
| 118 | -EFAULT : 0; \
|
| 119 | (x) = (typeof(x))(typeof((x) - (x)))__x; \
|
| 120 | break; \
|
| 121 | } \
|
| 122 | default: \
|
| 123 | __gu_err = __user_bad();\
|
| 124 | } \
|
| 125 | __gu_err; \
|
| 126 | })
|
| 127 |
|
| 128 |
|
| 129 | #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
|
| 130 | ({ \
|
| 131 | __asm__ __volatile__ ( \
|
| 132 | "1:" insn " %1, %2, r0;" \
|
| 133 | " addk %0, r0, r0;" \
|
| 134 | "2: " \
|
| 135 | __FIXUP_SECTION \
|
| 136 | "3: brid 2b;" \
|
| 137 | " addik %0, r0, %3;" \
|
| 138 | ".previous;" \
|
| 139 | __EX_TABLE_SECTION \
|
| 140 | ".word 1b,3b;" \
|
| 141 | ".previous;" \
|
| 142 | : "=&r"(__gu_err) \
|
| 143 | : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
|
| 144 | ); \
|
| 145 | })
|
| 146 |
|
| 147 | #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \
|
| 148 | ({ \
|
| 149 | __asm__ __volatile__ (" lwi %0, %1, 0;" \
|
| 150 | "1: swi %0, %2, 0;" \
|
| 151 | " lwi %0, %1, 4;" \
|
| 152 | "2: swi %0, %2, 4;" \
|
| 153 | " addk %0, r0, r0;" \
|
| 154 | "3: " \
|
| 155 | __FIXUP_SECTION \
|
| 156 | "4: brid 3b;" \
|
| 157 | " addik %0, r0, %3;" \
|
| 158 | ".previous;" \
|
| 159 | __EX_TABLE_SECTION \
|
| 160 | ".word 1b,4b,2b,4b;" \
|
| 161 | ".previous;" \
|
| 162 | : "=&r"(__gu_err) \
|
| 163 | : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
|
| 164 | ); \
|
| 165 | })
|
| 166 |
|
| 167 | |
| 168 | * put_user: - Write a simple value into user space.
|
| 169 | * @x: Value to copy to user space.
|
| 170 | * @ptr: Destination address, in user space.
|
| 171 | *
|
| 172 | * Context: User context only. This function may sleep if pagefaults are
|
| 173 | * enabled.
|
| 174 | *
|
| 175 | * This macro copies a single simple value from kernel space to user
|
| 176 | * space. It supports simple types like char and int, but not larger
|
| 177 | * data types like structures or arrays.
|
| 178 | *
|
| 179 | * @ptr must have pointer-to-simple-variable type, and @x must be assignable
|
| 180 | * to the result of dereferencing @ptr.
|
| 181 | *
|
| 182 | * Returns zero on success, or -EFAULT on error.
|
| 183 |
|
| 184 | #define put_user(x, ptr) \
|
| 185 | __put_user_check((x), (ptr), sizeof(*(ptr)))
|
| 186 |
|
| 187 | #define __put_user_check(x, ptr, size) \
|
| 188 | ({ \
|
| 189 | typeof(*(ptr)) volatile __pu_val = x; \
|
| 190 | typeof(*(ptr)) __user *__pu_addr = (ptr); \
|
| 191 | int __pu_err = 0; \
|
| 192 | \
|
| 193 | if (access_ok(__pu_addr, size)) { \
|
| 194 | switch (size) { \
|
| 195 | case 1: \
|
| 196 | __put_user_asm("sb", __pu_addr, __pu_val, \
|
| 197 | __pu_err); \
|
| 198 | break; \
|
| 199 | case 2: \
|
| 200 | __put_user_asm("sh", __pu_addr, __pu_val, \
|
| 201 | __pu_err); \
|
| 202 | break; \
|
| 203 | case 4: \
|
| 204 | __put_user_asm("sw", __pu_addr, __pu_val, \
|
| 205 | __pu_err); \
|
| 206 | break; \
|
| 207 | case 8: \
|
| 208 | __put_user_asm_8(__pu_addr, __pu_val, __pu_err);\
|
| 209 | break; \
|
| 210 | default: \
|
| 211 | __pu_err = __user_bad(); \
|
| 212 | break; \
|
| 213 | } \
|
| 214 | } else { \
|
| 215 | __pu_err = -EFAULT; \
|
| 216 | } \
|
| 217 | __pu_err; \
|
| 218 | })
|
| 219 |
|
| 220 | #define __put_user(x, ptr) \
|
| 221 | ({ \
|
| 222 | __typeof__(*(ptr)) volatile __gu_val = (x); \
|
| 223 | long __gu_err = 0; \
|
| 224 | switch (sizeof(__gu_val)) { \
|
| 225 | case 1: \
|
| 226 | __put_user_asm("sb", (ptr), __gu_val, __gu_err); \
|
| 227 | break; \
|
| 228 | case 2: \
|
| 229 | __put_user_asm("sh", (ptr), __gu_val, __gu_err); \
|
| 230 | break; \
|
| 231 | case 4: \
|
| 232 | __put_user_asm("sw", (ptr), __gu_val, __gu_err); \
|
| 233 | break; \
|
| 234 | case 8: \
|
| 235 | __put_user_asm_8((ptr), __gu_val, __gu_err); \
|
| 236 | break; \
|
| 237 | default: \
|
| 238 | __gu_err = __user_bad(); \
|
| 239 | } \
|
| 240 | __gu_err; \
|
| 241 | })
|
| 242 |
|
| 243 | static inline unsigned long
|
| 244 | raw_copy_from_user(void *to, const void __user *from, unsigned long n)
|
| 245 | {
|
| 246 | return __copy_tofrom_user((__force void __user *)to, from, n);
|
| 247 | }
|
| 248 |
|
| 249 | static inline unsigned long
|
| 250 | raw_copy_to_user(void __user *to, const void *from, unsigned long n)
|
| 251 | {
|
| 252 | return __copy_tofrom_user(to, (__force const void __user *)from, n);
|
| 253 | }
|
| 254 | #define INLINE_COPY_USER
|
| 255 |
|
| 256 | |
| 257 | * Copy a null terminated string from userspace.
|
| 258 |
|
| 259 | __must_check long strncpy_from_user(char *dst, const char __user *src,
|
| 260 | long count);
|
| 261 |
|
| 262 | |
| 263 | * Return the size of a string (including the ending 0)
|
| 264 | *
|
| 265 | * Return 0 on exception, a value greater than N if too long
|
| 266 |
|
| 267 | __must_check long strnlen_user(const char __user *sstr, long len);
|
| 268 |
|
| 269 | #endif
|