blob: 120f5005461b812466cf351ea4b3de5c3efb9ac9 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-or-later */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * OpenRISC Linux
4 *
5 * Linux architectural port borrowing liberally from similar works of
6 * others. All original copyrights apply as per the original source
7 * declaration.
8 *
9 * OpenRISC implementation:
10 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
11 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
12 * et al.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000013 */
14
15#ifndef __ASM_OPENRISC_UACCESS_H
16#define __ASM_OPENRISC_UACCESS_H
17
18/*
19 * User space memory access functions
20 */
21#include <linux/prefetch.h>
22#include <linux/string.h>
23#include <asm/page.h>
24#include <asm/extable.h>
25
26/*
27 * The fs value determines whether argument validity checking should be
28 * performed or not. If get_fs() == USER_DS, checking is performed, with
29 * get_fs() == KERNEL_DS, checking is bypassed.
30 *
31 * For historical reasons, these macros are grossly misnamed.
32 */
33
34/* addr_limit is the maximum accessible address for the task. we misuse
35 * the KERNEL_DS and USER_DS values to both assign and compare the
36 * addr_limit values through the equally misnamed get/set_fs macros.
37 * (see above)
38 */
39
40#define KERNEL_DS (~0UL)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000041
42#define USER_DS (TASK_SIZE)
43#define get_fs() (current_thread_info()->addr_limit)
44#define set_fs(x) (current_thread_info()->addr_limit = (x))
45
Olivier Deprez157378f2022-04-04 15:47:50 +020046#define uaccess_kernel() (get_fs() == KERNEL_DS)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000047
48/* Ensure that the range from addr to addr+size is all within the process'
49 * address space
50 */
Olivier Deprez157378f2022-04-04 15:47:50 +020051static inline int __range_ok(unsigned long addr, unsigned long size)
52{
53 const mm_segment_t fs = get_fs();
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000054
Olivier Deprez157378f2022-04-04 15:47:50 +020055 return size <= fs && addr <= (fs - size);
56}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000057
David Brazdil0f672f62019-12-10 10:32:29 +000058#define access_ok(addr, size) \
59({ \
Olivier Deprez157378f2022-04-04 15:47:50 +020060 __chk_user_ptr(addr); \
61 __range_ok((unsigned long)(addr), (size)); \
David Brazdil0f672f62019-12-10 10:32:29 +000062})
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000063
64/*
65 * These are the main single-value transfer routines. They automatically
66 * use the right size if we just have the right pointer type.
67 *
68 * This gets kind of ugly. We want to return _two_ values in "get_user()"
69 * and yet we don't want to do any pointers, because that is too much
70 * of a performance impact. Thus we have a few rather ugly macros here,
71 * and hide all the uglyness from the user.
72 *
73 * The "__xxx" versions of the user access functions are versions that
74 * do not verify the address space, that must have been done previously
75 * with a separate "access_ok()" call (this is used when we do multiple
76 * accesses to the same area of user memory).
77 *
78 * As we use the same address space for kernel and user data on the
79 * PowerPC, we can just do these as direct assignments. (Of course, the
80 * exception handling means that it's no longer "just"...)
81 */
82#define get_user(x, ptr) \
83 __get_user_check((x), (ptr), sizeof(*(ptr)))
84#define put_user(x, ptr) \
85 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
86
87#define __get_user(x, ptr) \
88 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
89#define __put_user(x, ptr) \
90 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
91
92extern long __put_user_bad(void);
93
94#define __put_user_nocheck(x, ptr, size) \
95({ \
96 long __pu_err; \
97 __put_user_size((x), (ptr), (size), __pu_err); \
98 __pu_err; \
99})
100
101#define __put_user_check(x, ptr, size) \
102({ \
103 long __pu_err = -EFAULT; \
Olivier Deprez157378f2022-04-04 15:47:50 +0200104 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
David Brazdil0f672f62019-12-10 10:32:29 +0000105 if (access_ok(__pu_addr, size)) \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000106 __put_user_size((x), __pu_addr, (size), __pu_err); \
107 __pu_err; \
108})
109
110#define __put_user_size(x, ptr, size, retval) \
111do { \
112 retval = 0; \
113 switch (size) { \
114 case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
115 case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
116 case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
117 case 8: __put_user_asm2(x, ptr, retval); break; \
118 default: __put_user_bad(); \
119 } \
120} while (0)
121
122struct __large_struct {
123 unsigned long buf[100];
124};
125#define __m(x) (*(struct __large_struct *)(x))
126
127/*
128 * We don't tell gcc that we are accessing memory, but this is OK
129 * because we do not write to any memory gcc knows about, so there
130 * are no aliasing issues.
131 */
132#define __put_user_asm(x, addr, err, op) \
133 __asm__ __volatile__( \
134 "1: "op" 0(%2),%1\n" \
135 "2:\n" \
136 ".section .fixup,\"ax\"\n" \
137 "3: l.addi %0,r0,%3\n" \
138 " l.j 2b\n" \
139 " l.nop\n" \
140 ".previous\n" \
141 ".section __ex_table,\"a\"\n" \
142 " .align 2\n" \
143 " .long 1b,3b\n" \
144 ".previous" \
145 : "=r"(err) \
146 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
147
148#define __put_user_asm2(x, addr, err) \
149 __asm__ __volatile__( \
150 "1: l.sw 0(%2),%1\n" \
151 "2: l.sw 4(%2),%H1\n" \
152 "3:\n" \
153 ".section .fixup,\"ax\"\n" \
154 "4: l.addi %0,r0,%3\n" \
155 " l.j 3b\n" \
156 " l.nop\n" \
157 ".previous\n" \
158 ".section __ex_table,\"a\"\n" \
159 " .align 2\n" \
160 " .long 1b,4b\n" \
161 " .long 2b,4b\n" \
162 ".previous" \
163 : "=r"(err) \
164 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
165
166#define __get_user_nocheck(x, ptr, size) \
167({ \
Olivier Deprez0e641232021-09-23 10:07:05 +0200168 long __gu_err; \
169 __get_user_size((x), (ptr), (size), __gu_err); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000170 __gu_err; \
171})
172
173#define __get_user_check(x, ptr, size) \
174({ \
Olivier Deprez0e641232021-09-23 10:07:05 +0200175 long __gu_err = -EFAULT; \
Olivier Deprez157378f2022-04-04 15:47:50 +0200176 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
Olivier Deprez0e641232021-09-23 10:07:05 +0200177 if (access_ok(__gu_addr, size)) \
178 __get_user_size((x), __gu_addr, (size), __gu_err); \
179 else \
180 (x) = (__typeof__(*(ptr))) 0; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000181 __gu_err; \
182})
183
184extern long __get_user_bad(void);
185
186#define __get_user_size(x, ptr, size, retval) \
187do { \
188 retval = 0; \
189 switch (size) { \
190 case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
191 case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
192 case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
193 case 8: __get_user_asm2(x, ptr, retval); break; \
Olivier Deprez0e641232021-09-23 10:07:05 +0200194 default: (x) = (__typeof__(*(ptr)))__get_user_bad(); \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000195 } \
196} while (0)
197
198#define __get_user_asm(x, addr, err, op) \
Olivier Deprez0e641232021-09-23 10:07:05 +0200199{ \
200 unsigned long __gu_tmp; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000201 __asm__ __volatile__( \
202 "1: "op" %1,0(%2)\n" \
203 "2:\n" \
204 ".section .fixup,\"ax\"\n" \
205 "3: l.addi %0,r0,%3\n" \
206 " l.addi %1,r0,0\n" \
207 " l.j 2b\n" \
208 " l.nop\n" \
209 ".previous\n" \
210 ".section __ex_table,\"a\"\n" \
211 " .align 2\n" \
212 " .long 1b,3b\n" \
213 ".previous" \
Olivier Deprez0e641232021-09-23 10:07:05 +0200214 : "=r"(err), "=r"(__gu_tmp) \
215 : "r"(addr), "i"(-EFAULT), "0"(err)); \
216 (x) = (__typeof__(*(addr)))__gu_tmp; \
217}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000218
219#define __get_user_asm2(x, addr, err) \
Olivier Deprez0e641232021-09-23 10:07:05 +0200220{ \
221 unsigned long long __gu_tmp; \
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000222 __asm__ __volatile__( \
223 "1: l.lwz %1,0(%2)\n" \
224 "2: l.lwz %H1,4(%2)\n" \
225 "3:\n" \
226 ".section .fixup,\"ax\"\n" \
227 "4: l.addi %0,r0,%3\n" \
228 " l.addi %1,r0,0\n" \
229 " l.addi %H1,r0,0\n" \
230 " l.j 3b\n" \
231 " l.nop\n" \
232 ".previous\n" \
233 ".section __ex_table,\"a\"\n" \
234 " .align 2\n" \
235 " .long 1b,4b\n" \
236 " .long 2b,4b\n" \
237 ".previous" \
Olivier Deprez0e641232021-09-23 10:07:05 +0200238 : "=r"(err), "=&r"(__gu_tmp) \
239 : "r"(addr), "i"(-EFAULT), "0"(err)); \
240 (x) = (__typeof__(*(addr)))( \
241 (__typeof__((x)-(x)))__gu_tmp); \
242}
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000243
244/* more complex routines */
245
246extern unsigned long __must_check
247__copy_tofrom_user(void *to, const void *from, unsigned long size);
248static inline unsigned long
249raw_copy_from_user(void *to, const void __user *from, unsigned long size)
250{
251 return __copy_tofrom_user(to, (__force const void *)from, size);
252}
253static inline unsigned long
Olivier Deprez157378f2022-04-04 15:47:50 +0200254raw_copy_to_user(void __user *to, const void *from, unsigned long size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000255{
256 return __copy_tofrom_user((__force void *)to, from, size);
257}
258#define INLINE_COPY_FROM_USER
259#define INLINE_COPY_TO_USER
260
Olivier Deprez157378f2022-04-04 15:47:50 +0200261extern unsigned long __clear_user(void __user *addr, unsigned long size);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000262
263static inline __must_check unsigned long
Olivier Deprez157378f2022-04-04 15:47:50 +0200264clear_user(void __user *addr, unsigned long size)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000265{
David Brazdil0f672f62019-12-10 10:32:29 +0000266 if (likely(access_ok(addr, size)))
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 size = __clear_user(addr, size);
268 return size;
269}
270
271#define user_addr_max() \
272 (uaccess_kernel() ? ~0UL : TASK_SIZE)
273
274extern long strncpy_from_user(char *dest, const char __user *src, long count);
275
276extern __must_check long strnlen_user(const char __user *str, long n);
277
278#endif /* __ASM_OPENRISC_UACCESS_H */