f2fc026cffb48c5d4b07d118a89d00c462ae39e7
[linux-2.6-microblaze.git] / arch / arm64 / include / asm / uaccess.h
1 /*
2  * Based on arch/arm/include/asm/uaccess.h
3  *
4  * Copyright (C) 2012 ARM Ltd.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 #ifndef __ASM_UACCESS_H
19 #define __ASM_UACCESS_H
20
21 #include <asm/alternative.h>
22 #include <asm/kernel-pgtable.h>
23 #include <asm/sysreg.h>
24
25 /*
26  * User space memory access functions
27  */
28 #include <linux/bitops.h>
29 #include <linux/kasan-checks.h>
30 #include <linux/string.h>
31
32 #include <asm/cpufeature.h>
33 #include <asm/ptrace.h>
34 #include <asm/memory.h>
35 #include <asm/compiler.h>
36 #include <asm/extable.h>
37
38 #define get_ds()        (KERNEL_DS)
39 #define get_fs()        (current_thread_info()->addr_limit)
40
41 static inline void set_fs(mm_segment_t fs)
42 {
43         current_thread_info()->addr_limit = fs;
44
45         /* On user-mode return, check fs is correct */
46         set_thread_flag(TIF_FSCHECK);
47
48         /*
49          * Enable/disable UAO so that copy_to_user() etc can access
50          * kernel memory with the unprivileged instructions.
51          */
52         if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
53                 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
54         else
55                 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
56                                 CONFIG_ARM64_UAO));
57 }
58
59 #define segment_eq(a, b)        ((a) == (b))
60
61 /*
62  * Test whether a block of memory is a valid user space address.
63  * Returns 1 if the range is valid, 0 otherwise.
64  *
65  * This is equivalent to the following test:
66  * (u65)addr + (u65)size <= (u65)current->addr_limit + 1
67  */
68 static inline unsigned long __range_ok(unsigned long addr, unsigned long size)
69 {
70         unsigned long limit = current_thread_info()->addr_limit;
71
72         __chk_user_ptr(addr);
73         asm volatile(
74         // A + B <= C + 1 for all A,B,C, in four easy steps:
75         // 1: X = A + B; X' = X % 2^64
76         "       adds    %0, %0, %2\n"
77         // 2: Set C = 0 if X > 2^64, to guarantee X' > C in step 4
78         "       csel    %1, xzr, %1, hi\n"
79         // 3: Set X' = ~0 if X >= 2^64. For X == 2^64, this decrements X'
80         //    to compensate for the carry flag being set in step 4. For
81         //    X > 2^64, X' merely has to remain nonzero, which it does.
82         "       csinv   %0, %0, xzr, cc\n"
83         // 4: For X < 2^64, this gives us X' - C - 1 <= 0, where the -1
84         //    comes from the carry in being clear. Otherwise, we are
85         //    testing X' - C == 0, subject to the previous adjustments.
86         "       sbcs    xzr, %0, %1\n"
87         "       cset    %0, ls\n"
88         : "+r" (addr), "+r" (limit) : "Ir" (size) : "cc");
89
90         return addr;
91 }
92
93 /*
94  * When dealing with data aborts, watchpoints, or instruction traps we may end
95  * up with a tagged userland pointer. Clear the tag to get a sane pointer to
96  * pass on to access_ok(), for instance.
97  */
98 #define untagged_addr(addr)             sign_extend64(addr, 55)
99
100 #define access_ok(type, addr, size)     __range_ok((unsigned long)(addr), size)
101 #define user_addr_max                   get_fs
102
103 #define _ASM_EXTABLE(from, to)                                          \
104         "       .pushsection    __ex_table, \"a\"\n"                    \
105         "       .align          3\n"                                    \
106         "       .long           (" #from " - .), (" #to " - .)\n"       \
107         "       .popsection\n"
108
109 /*
110  * User access enabling/disabling.
111  */
112 #ifdef CONFIG_ARM64_SW_TTBR0_PAN
113 static inline void __uaccess_ttbr0_disable(void)
114 {
115         unsigned long flags, ttbr;
116
117         local_irq_save(flags);
118         ttbr = read_sysreg(ttbr1_el1);
119         ttbr &= ~TTBR_ASID_MASK;
120         /* reserved_ttbr0 placed before swapper_pg_dir */
121         write_sysreg(ttbr - RESERVED_TTBR0_SIZE, ttbr0_el1);
122         isb();
123         /* Set reserved ASID */
124         write_sysreg(ttbr, ttbr1_el1);
125         isb();
126         local_irq_restore(flags);
127 }
128
129 static inline void __uaccess_ttbr0_enable(void)
130 {
131         unsigned long flags, ttbr0, ttbr1;
132
133         /*
134          * Disable interrupts to avoid preemption between reading the 'ttbr0'
135          * variable and the MSR. A context switch could trigger an ASID
136          * roll-over and an update of 'ttbr0'.
137          */
138         local_irq_save(flags);
139         ttbr0 = READ_ONCE(current_thread_info()->ttbr0);
140
141         /* Restore active ASID */
142         ttbr1 = read_sysreg(ttbr1_el1);
143         ttbr1 &= ~TTBR_ASID_MASK;               /* safety measure */
144         ttbr1 |= ttbr0 & TTBR_ASID_MASK;
145         write_sysreg(ttbr1, ttbr1_el1);
146         isb();
147
148         /* Restore user page table */
149         write_sysreg(ttbr0, ttbr0_el1);
150         isb();
151         local_irq_restore(flags);
152 }
153
154 static inline bool uaccess_ttbr0_disable(void)
155 {
156         if (!system_uses_ttbr0_pan())
157                 return false;
158         __uaccess_ttbr0_disable();
159         return true;
160 }
161
162 static inline bool uaccess_ttbr0_enable(void)
163 {
164         if (!system_uses_ttbr0_pan())
165                 return false;
166         __uaccess_ttbr0_enable();
167         return true;
168 }
169 #else
170 static inline bool uaccess_ttbr0_disable(void)
171 {
172         return false;
173 }
174
175 static inline bool uaccess_ttbr0_enable(void)
176 {
177         return false;
178 }
179 #endif
180
181 static inline void __uaccess_disable_hw_pan(void)
182 {
183         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,
184                         CONFIG_ARM64_PAN));
185 }
186
187 static inline void __uaccess_enable_hw_pan(void)
188 {
189         asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
190                         CONFIG_ARM64_PAN));
191 }
192
193 #define __uaccess_disable(alt)                                          \
194 do {                                                                    \
195         if (!uaccess_ttbr0_disable())                                   \
196                 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt,          \
197                                 CONFIG_ARM64_PAN));                     \
198 } while (0)
199
200 #define __uaccess_enable(alt)                                           \
201 do {                                                                    \
202         if (!uaccess_ttbr0_enable())                                    \
203                 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt,          \
204                                 CONFIG_ARM64_PAN));                     \
205 } while (0)
206
207 static inline void uaccess_disable(void)
208 {
209         __uaccess_disable(ARM64_HAS_PAN);
210 }
211
212 static inline void uaccess_enable(void)
213 {
214         __uaccess_enable(ARM64_HAS_PAN);
215 }
216
217 /*
218  * These functions are no-ops when UAO is present.
219  */
220 static inline void uaccess_disable_not_uao(void)
221 {
222         __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
223 }
224
225 static inline void uaccess_enable_not_uao(void)
226 {
227         __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
228 }
229
230 /*
231  * The "__xxx" versions of the user access functions do not verify the address
232  * space - it must have been done previously with a separate "access_ok()"
233  * call.
234  *
235  * The "__xxx_error" versions set the third argument to -EFAULT if an error
236  * occurs, and leave it unchanged on success.
237  */
238 #define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
239         asm volatile(                                                   \
240         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
241                         alt_instr " " reg "1, [%2]\n", feature)         \
242         "2:\n"                                                          \
243         "       .section .fixup, \"ax\"\n"                              \
244         "       .align  2\n"                                            \
245         "3:     mov     %w0, %3\n"                                      \
246         "       mov     %1, #0\n"                                       \
247         "       b       2b\n"                                           \
248         "       .previous\n"                                            \
249         _ASM_EXTABLE(1b, 3b)                                            \
250         : "+r" (err), "=&r" (x)                                         \
251         : "r" (addr), "i" (-EFAULT))
252
253 #define __get_user_err(x, ptr, err)                                     \
254 do {                                                                    \
255         unsigned long __gu_val;                                         \
256         __chk_user_ptr(ptr);                                            \
257         uaccess_enable_not_uao();                                       \
258         switch (sizeof(*(ptr))) {                                       \
259         case 1:                                                         \
260                 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr),  \
261                                (err), ARM64_HAS_UAO);                   \
262                 break;                                                  \
263         case 2:                                                         \
264                 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr),  \
265                                (err), ARM64_HAS_UAO);                   \
266                 break;                                                  \
267         case 4:                                                         \
268                 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr),    \
269                                (err), ARM64_HAS_UAO);                   \
270                 break;                                                  \
271         case 8:                                                         \
272                 __get_user_asm("ldr", "ldtr", "%x",  __gu_val, (ptr),   \
273                                (err), ARM64_HAS_UAO);                   \
274                 break;                                                  \
275         default:                                                        \
276                 BUILD_BUG();                                            \
277         }                                                               \
278         uaccess_disable_not_uao();                                      \
279         (x) = (__force __typeof__(*(ptr)))__gu_val;                     \
280 } while (0)
281
282 #define __get_user(x, ptr)                                              \
283 ({                                                                      \
284         int __gu_err = 0;                                               \
285         __get_user_err((x), (ptr), __gu_err);                           \
286         __gu_err;                                                       \
287 })
288
289 #define __get_user_error(x, ptr, err)                                   \
290 ({                                                                      \
291         __get_user_err((x), (ptr), (err));                              \
292         (void)0;                                                        \
293 })
294
295 #define get_user(x, ptr)                                                \
296 ({                                                                      \
297         __typeof__(*(ptr)) __user *__p = (ptr);                         \
298         might_fault();                                                  \
299         access_ok(VERIFY_READ, __p, sizeof(*__p)) ?                     \
300                 __get_user((x), __p) :                                  \
301                 ((x) = 0, -EFAULT);                                     \
302 })
303
304 #define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature)    \
305         asm volatile(                                                   \
306         "1:"ALTERNATIVE(instr "     " reg "1, [%2]\n",                  \
307                         alt_instr " " reg "1, [%2]\n", feature)         \
308         "2:\n"                                                          \
309         "       .section .fixup,\"ax\"\n"                               \
310         "       .align  2\n"                                            \
311         "3:     mov     %w0, %3\n"                                      \
312         "       b       2b\n"                                           \
313         "       .previous\n"                                            \
314         _ASM_EXTABLE(1b, 3b)                                            \
315         : "+r" (err)                                                    \
316         : "r" (x), "r" (addr), "i" (-EFAULT))
317
318 #define __put_user_err(x, ptr, err)                                     \
319 do {                                                                    \
320         __typeof__(*(ptr)) __pu_val = (x);                              \
321         __chk_user_ptr(ptr);                                            \
322         uaccess_enable_not_uao();                                       \
323         switch (sizeof(*(ptr))) {                                       \
324         case 1:                                                         \
325                 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr),  \
326                                (err), ARM64_HAS_UAO);                   \
327                 break;                                                  \
328         case 2:                                                         \
329                 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr),  \
330                                (err), ARM64_HAS_UAO);                   \
331                 break;                                                  \
332         case 4:                                                         \
333                 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr),    \
334                                (err), ARM64_HAS_UAO);                   \
335                 break;                                                  \
336         case 8:                                                         \
337                 __put_user_asm("str", "sttr", "%x", __pu_val, (ptr),    \
338                                (err), ARM64_HAS_UAO);                   \
339                 break;                                                  \
340         default:                                                        \
341                 BUILD_BUG();                                            \
342         }                                                               \
343         uaccess_disable_not_uao();                                      \
344 } while (0)
345
346 #define __put_user(x, ptr)                                              \
347 ({                                                                      \
348         int __pu_err = 0;                                               \
349         __put_user_err((x), (ptr), __pu_err);                           \
350         __pu_err;                                                       \
351 })
352
353 #define __put_user_error(x, ptr, err)                                   \
354 ({                                                                      \
355         __put_user_err((x), (ptr), (err));                              \
356         (void)0;                                                        \
357 })
358
359 #define put_user(x, ptr)                                                \
360 ({                                                                      \
361         __typeof__(*(ptr)) __user *__p = (ptr);                         \
362         might_fault();                                                  \
363         access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ?                    \
364                 __put_user((x), __p) :                                  \
365                 -EFAULT;                                                \
366 })
367
368 extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
369 #define raw_copy_from_user __arch_copy_from_user
370 extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
371 #define raw_copy_to_user __arch_copy_to_user
372 extern unsigned long __must_check raw_copy_in_user(void __user *to, const void __user *from, unsigned long n);
373 extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
374 #define INLINE_COPY_TO_USER
375 #define INLINE_COPY_FROM_USER
376
377 static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
378 {
379         if (access_ok(VERIFY_WRITE, to, n))
380                 n = __clear_user(to, n);
381         return n;
382 }
383
384 extern long strncpy_from_user(char *dest, const char __user *src, long count);
385
386 extern __must_check long strnlen_user(const char __user *str, long n);
387
388 #ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
389 struct page;
390 void memcpy_page_flushcache(char *to, struct page *page, size_t offset, size_t len);
391 extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n);
392
393 static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
394 {
395         kasan_check_write(dst, size);
396         return __copy_user_flushcache(dst, src, size);
397 }
398 #endif
399
400 #endif /* __ASM_UACCESS_H */