blob: 8b38b0de3e8bb02e27f613355e93b82a7aef9fcf [file] [log] [blame]
Catalin Marinas0aea86a2012-03-05 11:49:32 +00001/*
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
Catalin Marinas2962f1d2016-07-01 14:58:21 +010021#include <asm/alternative.h>
Catalin Marinas005bf1a2016-07-01 16:53:00 +010022#include <asm/kernel-pgtable.h>
Will Deacon4345a532017-12-01 17:33:48 +000023#include <asm/mmu.h>
Catalin Marinas2962f1d2016-07-01 14:58:21 +010024#include <asm/sysreg.h>
25
Catalin Marinas7c93e722016-07-01 14:58:21 +010026#ifndef __ASSEMBLY__
27
Catalin Marinas0aea86a2012-03-05 11:49:32 +000028/*
29 * User space memory access functions
30 */
Andre Przywara87261d12016-10-19 14:40:54 +010031#include <linux/bitops.h>
Yang Shibffe1baff2016-06-08 14:40:56 -070032#include <linux/kasan-checks.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000033#include <linux/string.h>
34#include <linux/thread_info.h>
35
James Morse338d4f42015-07-22 19:05:54 +010036#include <asm/cpufeature.h>
Catalin Marinase4cbde72016-07-01 16:53:00 +010037#include <asm/kernel-pgtable.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000038#include <asm/ptrace.h>
39#include <asm/errno.h>
40#include <asm/memory.h>
41#include <asm/compiler.h>
42
43#define VERIFY_READ 0
44#define VERIFY_WRITE 1
45
46/*
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010047 * The exception table consists of pairs of relative offsets: the first
48 * is the relative offset to an instruction that is allowed to fault,
49 * and the second is the relative offset at which the program should
50 * continue. No registers are modified, so it is entirely up to the
51 * continuation code to figure out what to do.
Catalin Marinas0aea86a2012-03-05 11:49:32 +000052 *
53 * All the routines below use bits of fixup code that are out of line
54 * with the main instruction path. This means when everything is well,
55 * we don't even have to jump over them. Further, they do not intrude
56 * on our cache or tlb entries.
57 */
58
59struct exception_table_entry
60{
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010061 int insn, fixup;
Catalin Marinas0aea86a2012-03-05 11:49:32 +000062};
63
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010064#define ARCH_HAS_RELATIVE_EXTABLE
65
Catalin Marinas0aea86a2012-03-05 11:49:32 +000066extern int fixup_exception(struct pt_regs *regs);
67
68#define KERNEL_DS (-1UL)
69#define get_ds() (KERNEL_DS)
70
71#define USER_DS TASK_SIZE_64
72#define get_fs() (current_thread_info()->addr_limit)
73
74static inline void set_fs(mm_segment_t fs)
75{
76 current_thread_info()->addr_limit = fs;
James Morse57f49592016-02-05 14:58:48 +000077
78 /*
79 * Enable/disable UAO so that copy_to_user() etc can access
80 * kernel memory with the unprivileged instructions.
81 */
82 if (IS_ENABLED(CONFIG_ARM64_UAO) && fs == KERNEL_DS)
83 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(1), ARM64_HAS_UAO));
84 else
85 asm(ALTERNATIVE("nop", SET_PSTATE_UAO(0), ARM64_HAS_UAO,
86 CONFIG_ARM64_UAO));
Catalin Marinas0aea86a2012-03-05 11:49:32 +000087}
88
Michael S. Tsirkin967f0e52015-01-06 15:11:13 +020089#define segment_eq(a, b) ((a) == (b))
Catalin Marinas0aea86a2012-03-05 11:49:32 +000090
91/*
Catalin Marinas0aea86a2012-03-05 11:49:32 +000092 * Test whether a block of memory is a valid user space address.
93 * Returns 1 if the range is valid, 0 otherwise.
94 *
95 * This is equivalent to the following test:
Christopher Covington31b1e942014-03-19 16:29:37 +000096 * (u65)addr + (u65)size <= current->addr_limit
Catalin Marinas0aea86a2012-03-05 11:49:32 +000097 *
98 * This needs 65-bit arithmetic.
99 */
100#define __range_ok(addr, size) \
101({ \
Mark Rutlande817a7f2017-05-03 16:09:35 +0100102 unsigned long __addr = (unsigned long __force)(addr); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000103 unsigned long flag, roksum; \
104 __chk_user_ptr(addr); \
Christopher Covington31b1e942014-03-19 16:29:37 +0000105 asm("adds %1, %1, %3; ccmp %1, %4, #2, cc; cset %0, ls" \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000106 : "=&r" (flag), "=&r" (roksum) \
Mark Rutlande817a7f2017-05-03 16:09:35 +0100107 : "1" (__addr), "Ir" (size), \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000108 "r" (current_thread_info()->addr_limit) \
109 : "cc"); \
110 flag; \
111})
112
Andre Przywara87261d12016-10-19 14:40:54 +0100113/*
Kristina Martsenko1d61ccb2017-06-06 20:14:09 +0100114 * When dealing with data aborts, watchpoints, or instruction traps we may end
115 * up with a tagged userland pointer. Clear the tag to get a sane pointer to
116 * pass on to access_ok(), for instance.
Andre Przywara87261d12016-10-19 14:40:54 +0100117 */
118#define untagged_addr(addr) sign_extend64(addr, 55)
119
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000120#define access_ok(type, addr, size) __range_ok(addr, size)
Will Deacon12a0ef72013-11-06 17:20:22 +0000121#define user_addr_max get_fs
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000122
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100123#define _ASM_EXTABLE(from, to) \
124 " .pushsection __ex_table, \"a\"\n" \
125 " .align 3\n" \
126 " .long (" #from " - .), (" #to " - .)\n" \
127 " .popsection\n"
128
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000129/*
Catalin Marinas7c93e722016-07-01 14:58:21 +0100130 * User access enabling/disabling.
131 */
Catalin Marinase4cbde72016-07-01 16:53:00 +0100132#ifdef CONFIG_ARM64_SW_TTBR0_PAN
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100133static inline void __uaccess_ttbr0_disable(void)
Catalin Marinase4cbde72016-07-01 16:53:00 +0100134{
135 unsigned long ttbr;
136
Will Deaconf7aa82e2017-08-10 13:58:16 +0100137 ttbr = read_sysreg(ttbr1_el1);
Catalin Marinase4cbde72016-07-01 16:53:00 +0100138 /* reserved_ttbr0 placed at the end of swapper_pg_dir */
Will Deaconf7aa82e2017-08-10 13:58:16 +0100139 write_sysreg(ttbr + SWAPPER_DIR_SIZE, ttbr0_el1);
140 isb();
141 /* Set reserved ASID */
Will Deacon4345a532017-12-01 17:33:48 +0000142 ttbr &= ~TTBR_ASID_MASK;
Will Deaconf7aa82e2017-08-10 13:58:16 +0100143 write_sysreg(ttbr, ttbr1_el1);
Catalin Marinase4cbde72016-07-01 16:53:00 +0100144 isb();
145}
146
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100147static inline void __uaccess_ttbr0_enable(void)
Catalin Marinase4cbde72016-07-01 16:53:00 +0100148{
Will Deaconf7aa82e2017-08-10 13:58:16 +0100149 unsigned long flags, ttbr0, ttbr1;
Catalin Marinase4cbde72016-07-01 16:53:00 +0100150
151 /*
152 * Disable interrupts to avoid preemption between reading the 'ttbr0'
153 * variable and the MSR. A context switch could trigger an ASID
154 * roll-over and an update of 'ttbr0'.
155 */
156 local_irq_save(flags);
Will Deaconf7aa82e2017-08-10 13:58:16 +0100157 ttbr0 = current_thread_info()->ttbr0;
158
159 /* Restore active ASID */
160 ttbr1 = read_sysreg(ttbr1_el1);
Will Deacon4345a532017-12-01 17:33:48 +0000161 ttbr1 |= ttbr0 & TTBR_ASID_MASK;
Will Deaconf7aa82e2017-08-10 13:58:16 +0100162 write_sysreg(ttbr1, ttbr1_el1);
163 isb();
164
165 /* Restore user page table */
166 write_sysreg(ttbr0, ttbr0_el1);
Catalin Marinase4cbde72016-07-01 16:53:00 +0100167 isb();
168 local_irq_restore(flags);
169}
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100170
171static inline bool uaccess_ttbr0_disable(void)
Catalin Marinase4cbde72016-07-01 16:53:00 +0100172{
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100173 if (!system_uses_ttbr0_pan())
174 return false;
175 __uaccess_ttbr0_disable();
176 return true;
Catalin Marinase4cbde72016-07-01 16:53:00 +0100177}
178
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100179static inline bool uaccess_ttbr0_enable(void)
Catalin Marinase4cbde72016-07-01 16:53:00 +0100180{
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100181 if (!system_uses_ttbr0_pan())
182 return false;
183 __uaccess_ttbr0_enable();
184 return true;
185}
186#else
187static inline bool uaccess_ttbr0_disable(void)
188{
189 return false;
190}
191
192static inline bool uaccess_ttbr0_enable(void)
193{
194 return false;
Catalin Marinase4cbde72016-07-01 16:53:00 +0100195}
196#endif
197
Catalin Marinas7c93e722016-07-01 14:58:21 +0100198#define __uaccess_disable(alt) \
199do { \
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100200 if (!uaccess_ttbr0_disable()) \
Catalin Marinase4cbde72016-07-01 16:53:00 +0100201 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), alt, \
202 CONFIG_ARM64_PAN)); \
Catalin Marinas7c93e722016-07-01 14:58:21 +0100203} while (0)
204
205#define __uaccess_enable(alt) \
206do { \
Marc Zyngier093284e2016-12-12 13:50:26 +0000207 if (!uaccess_ttbr0_enable()) \
Catalin Marinase4cbde72016-07-01 16:53:00 +0100208 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), alt, \
209 CONFIG_ARM64_PAN)); \
Catalin Marinas7c93e722016-07-01 14:58:21 +0100210} while (0)
211
212static inline void uaccess_disable(void)
213{
214 __uaccess_disable(ARM64_HAS_PAN);
215}
216
217static inline void uaccess_enable(void)
218{
219 __uaccess_enable(ARM64_HAS_PAN);
220}
221
222/*
223 * These functions are no-ops when UAO is present.
224 */
225static inline void uaccess_disable_not_uao(void)
226{
227 __uaccess_disable(ARM64_ALT_PAN_NOT_UAO);
228}
229
230static inline void uaccess_enable_not_uao(void)
231{
232 __uaccess_enable(ARM64_ALT_PAN_NOT_UAO);
233}
234
235/*
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000236 * The "__xxx" versions of the user access functions do not verify the address
237 * space - it must have been done previously with a separate "access_ok()"
238 * call.
239 *
240 * The "__xxx_error" versions set the third argument to -EFAULT if an error
241 * occurs, and leave it unchanged on success.
242 */
James Morse57f49592016-02-05 14:58:48 +0000243#define __get_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000244 asm volatile( \
James Morse57f49592016-02-05 14:58:48 +0000245 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
246 alt_instr " " reg "1, [%2]\n", feature) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000247 "2:\n" \
248 " .section .fixup, \"ax\"\n" \
249 " .align 2\n" \
250 "3: mov %w0, %3\n" \
251 " mov %1, #0\n" \
252 " b 2b\n" \
253 " .previous\n" \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100254 _ASM_EXTABLE(1b, 3b) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000255 : "+r" (err), "=&r" (x) \
256 : "r" (addr), "i" (-EFAULT))
257
258#define __get_user_err(x, ptr, err) \
259do { \
260 unsigned long __gu_val; \
261 __chk_user_ptr(ptr); \
Catalin Marinas7c93e722016-07-01 14:58:21 +0100262 uaccess_enable_not_uao(); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000263 switch (sizeof(*(ptr))) { \
264 case 1: \
James Morse57f49592016-02-05 14:58:48 +0000265 __get_user_asm("ldrb", "ldtrb", "%w", __gu_val, (ptr), \
266 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000267 break; \
268 case 2: \
James Morse57f49592016-02-05 14:58:48 +0000269 __get_user_asm("ldrh", "ldtrh", "%w", __gu_val, (ptr), \
270 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000271 break; \
272 case 4: \
James Morse57f49592016-02-05 14:58:48 +0000273 __get_user_asm("ldr", "ldtr", "%w", __gu_val, (ptr), \
274 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000275 break; \
276 case 8: \
James Morse57f49592016-02-05 14:58:48 +0000277 __get_user_asm("ldr", "ldtr", "%", __gu_val, (ptr), \
278 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000279 break; \
280 default: \
281 BUILD_BUG(); \
282 } \
Catalin Marinas7c93e722016-07-01 14:58:21 +0100283 uaccess_disable_not_uao(); \
Michael S. Tsirkin58fff512014-12-12 01:56:04 +0200284 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000285} while (0)
286
287#define __get_user(x, ptr) \
288({ \
289 int __gu_err = 0; \
290 __get_user_err((x), (ptr), __gu_err); \
291 __gu_err; \
292})
293
294#define __get_user_error(x, ptr, err) \
295({ \
296 __get_user_err((x), (ptr), (err)); \
297 (void)0; \
298})
299
300#define __get_user_unaligned __get_user
301
302#define get_user(x, ptr) \
303({ \
AKASHI Takahiro1f65c132013-09-24 10:00:50 +0100304 __typeof__(*(ptr)) __user *__p = (ptr); \
Michael S. Tsirkin56d2ef72013-05-26 17:30:42 +0300305 might_fault(); \
AKASHI Takahiro1f65c132013-09-24 10:00:50 +0100306 access_ok(VERIFY_READ, __p, sizeof(*__p)) ? \
307 __get_user((x), __p) : \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000308 ((x) = 0, -EFAULT); \
309})
310
James Morse57f49592016-02-05 14:58:48 +0000311#define __put_user_asm(instr, alt_instr, reg, x, addr, err, feature) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000312 asm volatile( \
James Morse57f49592016-02-05 14:58:48 +0000313 "1:"ALTERNATIVE(instr " " reg "1, [%2]\n", \
314 alt_instr " " reg "1, [%2]\n", feature) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000315 "2:\n" \
316 " .section .fixup,\"ax\"\n" \
317 " .align 2\n" \
318 "3: mov %w0, %3\n" \
319 " b 2b\n" \
320 " .previous\n" \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100321 _ASM_EXTABLE(1b, 3b) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000322 : "+r" (err) \
323 : "r" (x), "r" (addr), "i" (-EFAULT))
324
325#define __put_user_err(x, ptr, err) \
326do { \
327 __typeof__(*(ptr)) __pu_val = (x); \
328 __chk_user_ptr(ptr); \
Catalin Marinas7c93e722016-07-01 14:58:21 +0100329 uaccess_enable_not_uao(); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000330 switch (sizeof(*(ptr))) { \
331 case 1: \
James Morse57f49592016-02-05 14:58:48 +0000332 __put_user_asm("strb", "sttrb", "%w", __pu_val, (ptr), \
333 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000334 break; \
335 case 2: \
James Morse57f49592016-02-05 14:58:48 +0000336 __put_user_asm("strh", "sttrh", "%w", __pu_val, (ptr), \
337 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000338 break; \
339 case 4: \
James Morse57f49592016-02-05 14:58:48 +0000340 __put_user_asm("str", "sttr", "%w", __pu_val, (ptr), \
341 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000342 break; \
343 case 8: \
James Morse57f49592016-02-05 14:58:48 +0000344 __put_user_asm("str", "sttr", "%", __pu_val, (ptr), \
345 (err), ARM64_HAS_UAO); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000346 break; \
347 default: \
348 BUILD_BUG(); \
349 } \
Catalin Marinas7c93e722016-07-01 14:58:21 +0100350 uaccess_disable_not_uao(); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000351} while (0)
352
353#define __put_user(x, ptr) \
354({ \
355 int __pu_err = 0; \
356 __put_user_err((x), (ptr), __pu_err); \
357 __pu_err; \
358})
359
360#define __put_user_error(x, ptr, err) \
361({ \
362 __put_user_err((x), (ptr), (err)); \
363 (void)0; \
364})
365
366#define __put_user_unaligned __put_user
367
368#define put_user(x, ptr) \
369({ \
AKASHI Takahiro1f65c132013-09-24 10:00:50 +0100370 __typeof__(*(ptr)) __user *__p = (ptr); \
Michael S. Tsirkin56d2ef72013-05-26 17:30:42 +0300371 might_fault(); \
AKASHI Takahiro1f65c132013-09-24 10:00:50 +0100372 access_ok(VERIFY_WRITE, __p, sizeof(*__p)) ? \
373 __put_user((x), __p) : \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000374 -EFAULT; \
375})
376
Yang Shibffe1baff2016-06-08 14:40:56 -0700377extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
378extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000379extern unsigned long __must_check __copy_in_user(void __user *to, const void __user *from, unsigned long n);
380extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
381
Yang Shibffe1baff2016-06-08 14:40:56 -0700382static inline unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n)
383{
384 kasan_check_write(to, n);
Kees Cookfaf5b632016-06-23 15:59:42 -0700385 check_object_size(to, n, false);
386 return __arch_copy_from_user(to, from, n);
Yang Shibffe1baff2016-06-08 14:40:56 -0700387}
388
389static inline unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n)
390{
391 kasan_check_read(from, n);
Kees Cookfaf5b632016-06-23 15:59:42 -0700392 check_object_size(from, n, true);
393 return __arch_copy_to_user(to, from, n);
Yang Shibffe1baff2016-06-08 14:40:56 -0700394}
395
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000396static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
397{
Al Viro4855bd22016-09-10 16:50:00 -0400398 unsigned long res = n;
Yang Shibffe1baff2016-06-08 14:40:56 -0700399 kasan_check_write(to, n);
Mark Rutlandd0a00572017-02-07 12:33:55 +0000400 check_object_size(to, n, false);
Yang Shibffe1baff2016-06-08 14:40:56 -0700401
Kees Cookfaf5b632016-06-23 15:59:42 -0700402 if (access_ok(VERIFY_READ, from, n)) {
Al Viro4855bd22016-09-10 16:50:00 -0400403 res = __arch_copy_from_user(to, from, n);
404 }
405 if (unlikely(res))
406 memset(to + (n - res), 0, res);
407 return res;
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000408}
409
410static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
411{
Yang Shibffe1baff2016-06-08 14:40:56 -0700412 kasan_check_read(from, n);
Mark Rutlandd0a00572017-02-07 12:33:55 +0000413 check_object_size(from, n, true);
Yang Shibffe1baff2016-06-08 14:40:56 -0700414
Kees Cookfaf5b632016-06-23 15:59:42 -0700415 if (access_ok(VERIFY_WRITE, to, n)) {
Yang Shibffe1baff2016-06-08 14:40:56 -0700416 n = __arch_copy_to_user(to, from, n);
Kees Cookfaf5b632016-06-23 15:59:42 -0700417 }
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000418 return n;
419}
420
421static inline unsigned long __must_check copy_in_user(void __user *to, const void __user *from, unsigned long n)
422{
423 if (access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n))
424 n = __copy_in_user(to, from, n);
425 return n;
426}
427
428#define __copy_to_user_inatomic __copy_to_user
429#define __copy_from_user_inatomic __copy_from_user
430
431static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
432{
433 if (access_ok(VERIFY_WRITE, to, n))
434 n = __clear_user(to, n);
435 return n;
436}
437
Will Deacon12a0ef72013-11-06 17:20:22 +0000438extern long strncpy_from_user(char *dest, const char __user *src, long count);
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000439
Will Deacon12a0ef72013-11-06 17:20:22 +0000440extern __must_check long strlen_user(const char __user *str);
441extern __must_check long strnlen_user(const char __user *str, long n);
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000442
Catalin Marinas7c93e722016-07-01 14:58:21 +0100443#else /* __ASSEMBLY__ */
444
Catalin Marinas7c93e722016-07-01 14:58:21 +0100445#include <asm/assembler.h>
446
447/*
Catalin Marinase4cbde72016-07-01 16:53:00 +0100448 * User access enabling/disabling macros.
449 */
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100450#ifdef CONFIG_ARM64_SW_TTBR0_PAN
451 .macro __uaccess_ttbr0_disable, tmp1
Catalin Marinase4cbde72016-07-01 16:53:00 +0100452 mrs \tmp1, ttbr1_el1 // swapper_pg_dir
453 add \tmp1, \tmp1, #SWAPPER_DIR_SIZE // reserved_ttbr0 at the end of swapper_pg_dir
454 msr ttbr0_el1, \tmp1 // set reserved TTBR0_EL1
455 isb
Will Deaconf7aa82e2017-08-10 13:58:16 +0100456 sub \tmp1, \tmp1, #SWAPPER_DIR_SIZE
Will Deacon4345a532017-12-01 17:33:48 +0000457 bic \tmp1, \tmp1, #TTBR_ASID_MASK
Will Deaconf7aa82e2017-08-10 13:58:16 +0100458 msr ttbr1_el1, \tmp1 // set reserved ASID
459 isb
Catalin Marinase4cbde72016-07-01 16:53:00 +0100460 .endm
461
Will Deaconf7aa82e2017-08-10 13:58:16 +0100462 .macro __uaccess_ttbr0_enable, tmp1, tmp2
Catalin Marinase4cbde72016-07-01 16:53:00 +0100463 get_thread_info \tmp1
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100464 ldr \tmp1, [\tmp1, #TSK_TI_TTBR0] // load saved TTBR0_EL1
Will Deaconf7aa82e2017-08-10 13:58:16 +0100465 mrs \tmp2, ttbr1_el1
466 extr \tmp2, \tmp2, \tmp1, #48
467 ror \tmp2, \tmp2, #16
468 msr ttbr1_el1, \tmp2 // set the active ASID
469 isb
Catalin Marinase4cbde72016-07-01 16:53:00 +0100470 msr ttbr0_el1, \tmp1 // set the non-PAN TTBR0_EL1
471 isb
472 .endm
473
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100474 .macro uaccess_ttbr0_disable, tmp1
475alternative_if_not ARM64_HAS_PAN
476 __uaccess_ttbr0_disable \tmp1
477alternative_else_nop_endif
478 .endm
479
Will Deaconf7aa82e2017-08-10 13:58:16 +0100480 .macro uaccess_ttbr0_enable, tmp1, tmp2, tmp3
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100481alternative_if_not ARM64_HAS_PAN
Will Deaconf7aa82e2017-08-10 13:58:16 +0100482 save_and_disable_irq \tmp3 // avoid preemption
483 __uaccess_ttbr0_enable \tmp1, \tmp2
484 restore_irq \tmp3
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100485alternative_else_nop_endif
486 .endm
487#else
488 .macro uaccess_ttbr0_disable, tmp1
489 .endm
490
Will Deaconf7aa82e2017-08-10 13:58:16 +0100491 .macro uaccess_ttbr0_enable, tmp1, tmp2, tmp3
Catalin Marinas005bf1a2016-07-01 16:53:00 +0100492 .endm
493#endif
494
Catalin Marinase4cbde72016-07-01 16:53:00 +0100495/*
496 * These macros are no-ops when UAO is present.
Catalin Marinas7c93e722016-07-01 14:58:21 +0100497 */
498 .macro uaccess_disable_not_uao, tmp1
Catalin Marinase4cbde72016-07-01 16:53:00 +0100499 uaccess_ttbr0_disable \tmp1
Catalin Marinas2962f1d2016-07-01 14:58:21 +0100500alternative_if ARM64_ALT_PAN_NOT_UAO
Catalin Marinas7c93e722016-07-01 14:58:21 +0100501 SET_PSTATE_PAN(1)
Catalin Marinas2962f1d2016-07-01 14:58:21 +0100502alternative_else_nop_endif
Catalin Marinas7c93e722016-07-01 14:58:21 +0100503 .endm
504
Will Deaconf7aa82e2017-08-10 13:58:16 +0100505 .macro uaccess_enable_not_uao, tmp1, tmp2, tmp3
506 uaccess_ttbr0_enable \tmp1, \tmp2, \tmp3
Catalin Marinas2962f1d2016-07-01 14:58:21 +0100507alternative_if ARM64_ALT_PAN_NOT_UAO
Catalin Marinas7c93e722016-07-01 14:58:21 +0100508 SET_PSTATE_PAN(0)
Catalin Marinas2962f1d2016-07-01 14:58:21 +0100509alternative_else_nop_endif
Catalin Marinas7c93e722016-07-01 14:58:21 +0100510 .endm
511
512#endif /* __ASSEMBLY__ */
513
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000514#endif /* __ASM_UACCESS_H */