blob: abb31aa1f8cad25a972dd06f30b295773243a15a [file] [log] [blame]
Thomas Gleixnercaab2772019-06-03 07:44:50 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Catalin Marinas0aea86a2012-03-05 11:49:32 +00002/*
3 * Based on arch/arm/include/asm/uaccess.h
4 *
5 * Copyright (C) 2012 ARM Ltd.
Catalin Marinas0aea86a2012-03-05 11:49:32 +00006 */
7#ifndef __ASM_UACCESS_H
8#define __ASM_UACCESS_H
9
Catalin Marinasbd389672016-07-01 14:58:21 +010010#include <asm/alternative.h>
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010011#include <asm/kernel-pgtable.h>
Catalin Marinasbd389672016-07-01 14:58:21 +010012#include <asm/sysreg.h>
13
Catalin Marinas0aea86a2012-03-05 11:49:32 +000014/*
15 * User space memory access functions
16 */
Andre Przywara87261d12016-10-19 14:40:54 +010017#include <linux/bitops.h>
Yang Shibffe1ba2016-06-08 14:40:56 -070018#include <linux/kasan-checks.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000019#include <linux/string.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000020
James Morse338d4f42015-07-22 19:05:54 +010021#include <asm/cpufeature.h>
Will Deacon5f1f7f62020-06-30 13:53:07 +010022#include <asm/mmu.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000023#include <asm/ptrace.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000024#include <asm/memory.h>
Al Viro46583932016-12-25 14:00:03 -050025#include <asm/extable.h>
Catalin Marinas0aea86a2012-03-05 11:49:32 +000026
Mark Rutlandfc703d82020-12-02 13:15:53 +000027#define HAVE_GET_KERNEL_NOFAULT
28
Catalin Marinas0aea86a2012-03-05 11:49:32 +000029/*
Catalin Marinas0aea86a2012-03-05 11:49:32 +000030 * Test whether a block of memory is a valid user space address.
31 * Returns 1 if the range is valid, 0 otherwise.
32 *
33 * This is equivalent to the following test:
Mark Rutland3d2403f2020-12-02 13:15:55 +000034 * (u65)addr + (u65)size <= (u65)TASK_SIZE_MAX
Catalin Marinas0aea86a2012-03-05 11:49:32 +000035 */
Robin Murphy9085b342018-02-19 13:38:00 +000036static inline unsigned long __range_ok(const void __user *addr, unsigned long size)
Robin Murphy51369e32018-02-05 15:34:18 +000037{
Mark Rutland3d2403f2020-12-02 13:15:55 +000038 unsigned long ret, limit = TASK_SIZE_MAX - 1;
Robin Murphy51369e32018-02-05 15:34:18 +000039
Catalin Marinasdf325e02019-12-05 13:57:36 +000040 /*
41 * Asynchronous I/O running in a kernel thread does not have the
42 * TIF_TAGGED_ADDR flag of the process owning the mm, so always untag
43 * the user address before checking.
44 */
Catalin Marinas63f0c602019-07-23 19:58:39 +020045 if (IS_ENABLED(CONFIG_ARM64_TAGGED_ADDR_ABI) &&
Catalin Marinasdf325e02019-12-05 13:57:36 +000046 (current->flags & PF_KTHREAD || test_thread_flag(TIF_TAGGED_ADDR)))
Catalin Marinas63f0c602019-07-23 19:58:39 +020047 addr = untagged_addr(addr);
Andrey Konovalov2b835e22019-07-23 19:58:38 +020048
Robin Murphy51369e32018-02-05 15:34:18 +000049 __chk_user_ptr(addr);
50 asm volatile(
51 // A + B <= C + 1 for all A,B,C, in four easy steps:
52 // 1: X = A + B; X' = X % 2^64
Robin Murphy9085b342018-02-19 13:38:00 +000053 " adds %0, %3, %2\n"
Robin Murphy51369e32018-02-05 15:34:18 +000054 // 2: Set C = 0 if X > 2^64, to guarantee X' > C in step 4
55 " csel %1, xzr, %1, hi\n"
56 // 3: Set X' = ~0 if X >= 2^64. For X == 2^64, this decrements X'
57 // to compensate for the carry flag being set in step 4. For
58 // X > 2^64, X' merely has to remain nonzero, which it does.
59 " csinv %0, %0, xzr, cc\n"
60 // 4: For X < 2^64, this gives us X' - C - 1 <= 0, where the -1
61 // comes from the carry in being clear. Otherwise, we are
62 // testing X' - C == 0, subject to the previous adjustments.
63 " sbcs xzr, %0, %1\n"
64 " cset %0, ls\n"
Robin Murphy9085b342018-02-19 13:38:00 +000065 : "=&r" (ret), "+r" (limit) : "Ir" (size), "0" (addr) : "cc");
Robin Murphy51369e32018-02-05 15:34:18 +000066
Robin Murphy9085b342018-02-19 13:38:00 +000067 return ret;
Robin Murphy51369e32018-02-05 15:34:18 +000068}
Catalin Marinas0aea86a2012-03-05 11:49:32 +000069
Linus Torvalds96d4f262019-01-03 18:57:57 -080070#define access_ok(addr, size) __range_ok(addr, size)
Catalin Marinas0aea86a2012-03-05 11:49:32 +000071
Ard Biesheuvel6c94f272016-01-01 15:02:12 +010072#define _ASM_EXTABLE(from, to) \
73 " .pushsection __ex_table, \"a\"\n" \
74 " .align 3\n" \
75 " .long (" #from " - .), (" #to " - .)\n" \
76 " .popsection\n"
77
Catalin Marinas0aea86a2012-03-05 11:49:32 +000078/*
Catalin Marinasbd389672016-07-01 14:58:21 +010079 * User access enabling/disabling.
80 */
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010081#ifdef CONFIG_ARM64_SW_TTBR0_PAN
82static inline void __uaccess_ttbr0_disable(void)
83{
Catalin Marinas6b88a322018-01-10 13:18:30 +000084 unsigned long flags, ttbr;
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010085
Catalin Marinas6b88a322018-01-10 13:18:30 +000086 local_irq_save(flags);
Will Deacon27a921e2017-08-10 13:58:16 +010087 ttbr = read_sysreg(ttbr1_el1);
Catalin Marinas6b88a322018-01-10 13:18:30 +000088 ttbr &= ~TTBR_ASID_MASK;
Mark Rutland833be852020-11-03 10:22:29 +000089 /* reserved_pg_dir placed before swapper_pg_dir */
90 write_sysreg(ttbr - PAGE_SIZE, ttbr0_el1);
Will Deacon27a921e2017-08-10 13:58:16 +010091 isb();
92 /* Set reserved ASID */
Will Deacon27a921e2017-08-10 13:58:16 +010093 write_sysreg(ttbr, ttbr1_el1);
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010094 isb();
Catalin Marinas6b88a322018-01-10 13:18:30 +000095 local_irq_restore(flags);
Catalin Marinas4b65a5d2016-07-01 16:53:00 +010096}
97
98static inline void __uaccess_ttbr0_enable(void)
99{
Will Deacon27a921e2017-08-10 13:58:16 +0100100 unsigned long flags, ttbr0, ttbr1;
Catalin Marinas4b65a5d2016-07-01 16:53:00 +0100101
102 /*
103 * Disable interrupts to avoid preemption between reading the 'ttbr0'
104 * variable and the MSR. A context switch could trigger an ASID
105 * roll-over and an update of 'ttbr0'.
106 */
107 local_irq_save(flags);
Catalin Marinas6b88a322018-01-10 13:18:30 +0000108 ttbr0 = READ_ONCE(current_thread_info()->ttbr0);
Will Deacon27a921e2017-08-10 13:58:16 +0100109
110 /* Restore active ASID */
111 ttbr1 = read_sysreg(ttbr1_el1);
Catalin Marinas6b88a322018-01-10 13:18:30 +0000112 ttbr1 &= ~TTBR_ASID_MASK; /* safety measure */
Will Deaconb5195382017-12-01 17:33:48 +0000113 ttbr1 |= ttbr0 & TTBR_ASID_MASK;
Will Deacon27a921e2017-08-10 13:58:16 +0100114 write_sysreg(ttbr1, ttbr1_el1);
115 isb();
116
117 /* Restore user page table */
118 write_sysreg(ttbr0, ttbr0_el1);
Catalin Marinas4b65a5d2016-07-01 16:53:00 +0100119 isb();
120 local_irq_restore(flags);
121}
122
123static inline bool uaccess_ttbr0_disable(void)
124{
125 if (!system_uses_ttbr0_pan())
126 return false;
127 __uaccess_ttbr0_disable();
128 return true;
129}
130
131static inline bool uaccess_ttbr0_enable(void)
132{
133 if (!system_uses_ttbr0_pan())
134 return false;
135 __uaccess_ttbr0_enable();
136 return true;
137}
138#else
139static inline bool uaccess_ttbr0_disable(void)
140{
141 return false;
142}
143
144static inline bool uaccess_ttbr0_enable(void)
145{
146 return false;
147}
148#endif
149
James Morsee1281f52018-01-08 15:38:11 +0000150static inline void __uaccess_disable_hw_pan(void)
151{
152 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(0), ARM64_HAS_PAN,
153 CONFIG_ARM64_PAN));
154}
155
156static inline void __uaccess_enable_hw_pan(void)
157{
158 asm(ALTERNATIVE("nop", SET_PSTATE_PAN(1), ARM64_HAS_PAN,
159 CONFIG_ARM64_PAN));
160}
161
Mark Rutland923e1e72020-12-02 13:15:50 +0000162static inline void uaccess_disable_privileged(void)
Catalin Marinasbd389672016-07-01 14:58:21 +0100163{
Mark Rutland7cf283c2020-12-02 13:15:57 +0000164 if (uaccess_ttbr0_disable())
165 return;
166
167 __uaccess_enable_hw_pan();
Catalin Marinasbd389672016-07-01 14:58:21 +0100168}
169
Mark Rutland923e1e72020-12-02 13:15:50 +0000170static inline void uaccess_enable_privileged(void)
Catalin Marinasbd389672016-07-01 14:58:21 +0100171{
Mark Rutland7cf283c2020-12-02 13:15:57 +0000172 if (uaccess_ttbr0_enable())
173 return;
Catalin Marinasbd389672016-07-01 14:58:21 +0100174
Mark Rutland7cf283c2020-12-02 13:15:57 +0000175 __uaccess_disable_hw_pan();
Catalin Marinasbd389672016-07-01 14:58:21 +0100176}
177
178/*
Mark Rutland3d2403f2020-12-02 13:15:55 +0000179 * Sanitise a uaccess pointer such that it becomes NULL if above the maximum
180 * user address. In case the pointer is tagged (has the top byte set), untag
181 * the pointer before checking.
Robin Murphy4d8efc22018-02-05 15:34:19 +0000182 */
183#define uaccess_mask_ptr(ptr) (__typeof__(ptr))__uaccess_mask_ptr(ptr)
184static inline void __user *__uaccess_mask_ptr(const void __user *ptr)
185{
186 void __user *safe_ptr;
187
188 asm volatile(
Andrey Konovalov2b835e22019-07-23 19:58:38 +0200189 " bics xzr, %3, %2\n"
Robin Murphy4d8efc22018-02-05 15:34:19 +0000190 " csel %0, %1, xzr, eq\n"
191 : "=&r" (safe_ptr)
Mark Rutland3d2403f2020-12-02 13:15:55 +0000192 : "r" (ptr), "r" (TASK_SIZE_MAX - 1),
Andrey Konovalov2b835e22019-07-23 19:58:38 +0200193 "r" (untagged_addr(ptr))
Robin Murphy4d8efc22018-02-05 15:34:19 +0000194 : "cc");
195
196 csdb();
197 return safe_ptr;
198}
199
200/*
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000201 * The "__xxx" versions of the user access functions do not verify the address
202 * space - it must have been done previously with a separate "access_ok()"
203 * call.
204 *
205 * The "__xxx_error" versions set the third argument to -EFAULT if an error
206 * occurs, and leave it unchanged on success.
207 */
Mark Rutlandfc703d82020-12-02 13:15:53 +0000208#define __get_mem_asm(load, reg, x, addr, err) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000209 asm volatile( \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000210 "1: " load " " reg "1, [%2]\n" \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000211 "2:\n" \
212 " .section .fixup, \"ax\"\n" \
213 " .align 2\n" \
214 "3: mov %w0, %3\n" \
215 " mov %1, #0\n" \
216 " b 2b\n" \
217 " .previous\n" \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100218 _ASM_EXTABLE(1b, 3b) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000219 : "+r" (err), "=&r" (x) \
220 : "r" (addr), "i" (-EFAULT))
221
Mark Rutlandfc703d82020-12-02 13:15:53 +0000222#define __raw_get_mem(ldr, x, ptr, err) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000223do { \
224 unsigned long __gu_val; \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000225 switch (sizeof(*(ptr))) { \
226 case 1: \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000227 __get_mem_asm(ldr "b", "%w", __gu_val, (ptr), (err)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000228 break; \
229 case 2: \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000230 __get_mem_asm(ldr "h", "%w", __gu_val, (ptr), (err)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000231 break; \
232 case 4: \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000233 __get_mem_asm(ldr, "%w", __gu_val, (ptr), (err)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000234 break; \
235 case 8: \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000236 __get_mem_asm(ldr, "%x", __gu_val, (ptr), (err)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000237 break; \
238 default: \
239 BUILD_BUG(); \
240 } \
Michael S. Tsirkin58fff512014-12-12 01:56:04 +0200241 (x) = (__force __typeof__(*(ptr)))__gu_val; \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000242} while (0)
243
Mark Rutlandf253d822020-12-02 13:15:52 +0000244#define __raw_get_user(x, ptr, err) \
245do { \
246 __chk_user_ptr(ptr); \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000247 uaccess_ttbr0_enable(); \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000248 __raw_get_mem("ldtr", x, ptr, err); \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000249 uaccess_ttbr0_disable(); \
Mark Rutlandf253d822020-12-02 13:15:52 +0000250} while (0)
251
Julien Thierry13e4cdd2019-01-15 13:58:26 +0000252#define __get_user_error(x, ptr, err) \
253do { \
Will Deacon84624082018-02-05 15:34:22 +0000254 __typeof__(*(ptr)) __user *__p = (ptr); \
255 might_fault(); \
Linus Torvalds96d4f262019-01-03 18:57:57 -0800256 if (access_ok(__p, sizeof(*__p))) { \
Will Deacon84624082018-02-05 15:34:22 +0000257 __p = uaccess_mask_ptr(__p); \
Catalin Marinas3cd0ddb2019-03-01 14:19:06 +0000258 __raw_get_user((x), __p, (err)); \
Will Deacon84624082018-02-05 15:34:22 +0000259 } else { \
Al Viro8cfb3472020-05-22 15:23:21 +0100260 (x) = (__force __typeof__(x))0; (err) = -EFAULT; \
Will Deacon84624082018-02-05 15:34:22 +0000261 } \
Julien Thierry13e4cdd2019-01-15 13:58:26 +0000262} while (0)
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000263
Will Deacon84624082018-02-05 15:34:22 +0000264#define __get_user(x, ptr) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000265({ \
Will Deacon84624082018-02-05 15:34:22 +0000266 int __gu_err = 0; \
Julien Thierry13e4cdd2019-01-15 13:58:26 +0000267 __get_user_error((x), (ptr), __gu_err); \
Will Deacon84624082018-02-05 15:34:22 +0000268 __gu_err; \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000269})
270
Will Deacon84624082018-02-05 15:34:22 +0000271#define get_user __get_user
272
Mark Rutlandfc703d82020-12-02 13:15:53 +0000273#define __get_kernel_nofault(dst, src, type, err_label) \
274do { \
275 int __gkn_err = 0; \
276 \
277 __raw_get_mem("ldr", *((type *)(dst)), \
278 (__force type *)(src), __gkn_err); \
279 if (unlikely(__gkn_err)) \
280 goto err_label; \
281} while (0)
282
283#define __put_mem_asm(store, reg, x, addr, err) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000284 asm volatile( \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000285 "1: " store " " reg "1, [%2]\n" \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000286 "2:\n" \
287 " .section .fixup,\"ax\"\n" \
288 " .align 2\n" \
289 "3: mov %w0, %3\n" \
290 " b 2b\n" \
291 " .previous\n" \
Ard Biesheuvel6c94f272016-01-01 15:02:12 +0100292 _ASM_EXTABLE(1b, 3b) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000293 : "+r" (err) \
294 : "r" (x), "r" (addr), "i" (-EFAULT))
295
Mark Rutlandfc703d82020-12-02 13:15:53 +0000296#define __raw_put_mem(str, x, ptr, err) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000297do { \
298 __typeof__(*(ptr)) __pu_val = (x); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000299 switch (sizeof(*(ptr))) { \
300 case 1: \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000301 __put_mem_asm(str "b", "%w", __pu_val, (ptr), (err)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000302 break; \
303 case 2: \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000304 __put_mem_asm(str "h", "%w", __pu_val, (ptr), (err)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000305 break; \
306 case 4: \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000307 __put_mem_asm(str, "%w", __pu_val, (ptr), (err)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000308 break; \
309 case 8: \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000310 __put_mem_asm(str, "%x", __pu_val, (ptr), (err)); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000311 break; \
312 default: \
313 BUILD_BUG(); \
314 } \
Mark Rutlandf253d822020-12-02 13:15:52 +0000315} while (0)
316
317#define __raw_put_user(x, ptr, err) \
318do { \
319 __chk_user_ptr(ptr); \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000320 uaccess_ttbr0_enable(); \
Mark Rutlandfc703d82020-12-02 13:15:53 +0000321 __raw_put_mem("sttr", x, ptr, err); \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000322 uaccess_ttbr0_disable(); \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000323} while (0)
324
Julien Thierry13e4cdd2019-01-15 13:58:26 +0000325#define __put_user_error(x, ptr, err) \
326do { \
Will Deacon84624082018-02-05 15:34:22 +0000327 __typeof__(*(ptr)) __user *__p = (ptr); \
328 might_fault(); \
Linus Torvalds96d4f262019-01-03 18:57:57 -0800329 if (access_ok(__p, sizeof(*__p))) { \
Will Deacon84624082018-02-05 15:34:22 +0000330 __p = uaccess_mask_ptr(__p); \
Catalin Marinas3cd0ddb2019-03-01 14:19:06 +0000331 __raw_put_user((x), __p, (err)); \
Will Deacon84624082018-02-05 15:34:22 +0000332 } else { \
333 (err) = -EFAULT; \
334 } \
Julien Thierry13e4cdd2019-01-15 13:58:26 +0000335} while (0)
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000336
Will Deacon84624082018-02-05 15:34:22 +0000337#define __put_user(x, ptr) \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000338({ \
Will Deacon84624082018-02-05 15:34:22 +0000339 int __pu_err = 0; \
Julien Thierry13e4cdd2019-01-15 13:58:26 +0000340 __put_user_error((x), (ptr), __pu_err); \
Will Deacon84624082018-02-05 15:34:22 +0000341 __pu_err; \
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000342})
343
Will Deacon84624082018-02-05 15:34:22 +0000344#define put_user __put_user
345
Mark Rutlandfc703d82020-12-02 13:15:53 +0000346#define __put_kernel_nofault(dst, src, type, err_label) \
347do { \
348 int __pkn_err = 0; \
349 \
350 __raw_put_mem("str", *((type *)(src)), \
351 (__force type *)(dst), __pkn_err); \
352 if (unlikely(__pkn_err)) \
353 goto err_label; \
354} while(0)
355
Yang Shibffe1ba2016-06-08 14:40:56 -0700356extern unsigned long __must_check __arch_copy_from_user(void *to, const void __user *from, unsigned long n);
Will Deaconf71c2ff2018-02-05 15:34:23 +0000357#define raw_copy_from_user(to, from, n) \
358({ \
Pavel Tatashine50be642019-11-20 12:07:40 -0500359 unsigned long __acfu_ret; \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000360 uaccess_ttbr0_enable(); \
Pavel Tatashine50be642019-11-20 12:07:40 -0500361 __acfu_ret = __arch_copy_from_user((to), \
362 __uaccess_mask_ptr(from), (n)); \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000363 uaccess_ttbr0_disable(); \
Pavel Tatashine50be642019-11-20 12:07:40 -0500364 __acfu_ret; \
Will Deaconf71c2ff2018-02-05 15:34:23 +0000365})
366
Yang Shibffe1ba2016-06-08 14:40:56 -0700367extern unsigned long __must_check __arch_copy_to_user(void __user *to, const void *from, unsigned long n);
Will Deaconf71c2ff2018-02-05 15:34:23 +0000368#define raw_copy_to_user(to, from, n) \
369({ \
Pavel Tatashine50be642019-11-20 12:07:40 -0500370 unsigned long __actu_ret; \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000371 uaccess_ttbr0_enable(); \
Pavel Tatashine50be642019-11-20 12:07:40 -0500372 __actu_ret = __arch_copy_to_user(__uaccess_mask_ptr(to), \
373 (from), (n)); \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000374 uaccess_ttbr0_disable(); \
Pavel Tatashine50be642019-11-20 12:07:40 -0500375 __actu_ret; \
Will Deaconf71c2ff2018-02-05 15:34:23 +0000376})
377
378extern unsigned long __must_check __arch_copy_in_user(void __user *to, const void __user *from, unsigned long n);
379#define raw_copy_in_user(to, from, n) \
380({ \
Pavel Tatashine50be642019-11-20 12:07:40 -0500381 unsigned long __aciu_ret; \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000382 uaccess_ttbr0_enable(); \
Pavel Tatashine50be642019-11-20 12:07:40 -0500383 __aciu_ret = __arch_copy_in_user(__uaccess_mask_ptr(to), \
384 __uaccess_mask_ptr(from), (n)); \
Mark Rutland7cf283c2020-12-02 13:15:57 +0000385 uaccess_ttbr0_disable(); \
Pavel Tatashine50be642019-11-20 12:07:40 -0500386 __aciu_ret; \
Will Deaconf71c2ff2018-02-05 15:34:23 +0000387})
388
Al Viro92430da2017-03-21 08:40:57 -0400389#define INLINE_COPY_TO_USER
390#define INLINE_COPY_FROM_USER
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000391
Will Deaconf71c2ff2018-02-05 15:34:23 +0000392extern unsigned long __must_check __arch_clear_user(void __user *to, unsigned long n);
393static inline unsigned long __must_check __clear_user(void __user *to, unsigned long n)
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000394{
Pavel Tatashine50be642019-11-20 12:07:40 -0500395 if (access_ok(to, n)) {
Mark Rutland7cf283c2020-12-02 13:15:57 +0000396 uaccess_ttbr0_enable();
Will Deaconf71c2ff2018-02-05 15:34:23 +0000397 n = __arch_clear_user(__uaccess_mask_ptr(to), n);
Mark Rutland7cf283c2020-12-02 13:15:57 +0000398 uaccess_ttbr0_disable();
Pavel Tatashine50be642019-11-20 12:07:40 -0500399 }
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000400 return n;
401}
Will Deaconf71c2ff2018-02-05 15:34:23 +0000402#define clear_user __clear_user
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000403
Will Deacon12a0ef72013-11-06 17:20:22 +0000404extern long strncpy_from_user(char *dest, const char __user *src, long count);
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000405
Will Deacon12a0ef72013-11-06 17:20:22 +0000406extern __must_check long strnlen_user(const char __user *str, long n);
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000407
Robin Murphy5d7bdeb2017-07-25 11:55:43 +0100408#ifdef CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE
409struct page;
410void memcpy_page_flushcache(char *to, struct page *page, size_t offset, size_t len);
411extern unsigned long __must_check __copy_user_flushcache(void *to, const void __user *from, unsigned long n);
412
413static inline int __copy_from_user_flushcache(void *dst, const void __user *src, unsigned size)
414{
415 kasan_check_write(dst, size);
Will Deaconf71c2ff2018-02-05 15:34:23 +0000416 return __copy_user_flushcache(dst, __uaccess_mask_ptr(src), size);
Robin Murphy5d7bdeb2017-07-25 11:55:43 +0100417}
418#endif
419
Catalin Marinas0aea86a2012-03-05 11:49:32 +0000420#endif /* __ASM_UACCESS_H */