Loading...
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*
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.
13 */
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#include <asm-generic/access_ok.h>
26
27/*
28 * These are the main single-value transfer routines. They automatically
29 * use the right size if we just have the right pointer type.
30 *
31 * This gets kind of ugly. We want to return _two_ values in "get_user()"
32 * and yet we don't want to do any pointers, because that is too much
33 * of a performance impact. Thus we have a few rather ugly macros here,
34 * and hide all the uglyness from the user.
35 *
36 * The "__xxx" versions of the user access functions are versions that
37 * do not verify the address space, that must have been done previously
38 * with a separate "access_ok()" call (this is used when we do multiple
39 * accesses to the same area of user memory).
40 *
41 * As we use the same address space for kernel and user data on the
42 * PowerPC, we can just do these as direct assignments. (Of course, the
43 * exception handling means that it's no longer "just"...)
44 */
45#define get_user(x, ptr) \
46 __get_user_check((x), (ptr), sizeof(*(ptr)))
47#define put_user(x, ptr) \
48 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
49
50#define __get_user(x, ptr) \
51 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
52#define __put_user(x, ptr) \
53 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
54
55extern long __put_user_bad(void);
56
57#define __put_user_nocheck(x, ptr, size) \
58({ \
59 long __pu_err; \
60 __put_user_size((x), (ptr), (size), __pu_err); \
61 __pu_err; \
62})
63
64#define __put_user_check(x, ptr, size) \
65({ \
66 long __pu_err = -EFAULT; \
67 __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
68 if (access_ok(__pu_addr, size)) \
69 __put_user_size((x), __pu_addr, (size), __pu_err); \
70 __pu_err; \
71})
72
73#define __put_user_size(x, ptr, size, retval) \
74do { \
75 retval = 0; \
76 switch (size) { \
77 case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
78 case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
79 case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
80 case 8: __put_user_asm2(x, ptr, retval); break; \
81 default: __put_user_bad(); \
82 } \
83} while (0)
84
85struct __large_struct {
86 unsigned long buf[100];
87};
88#define __m(x) (*(struct __large_struct *)(x))
89
90/*
91 * We don't tell gcc that we are accessing memory, but this is OK
92 * because we do not write to any memory gcc knows about, so there
93 * are no aliasing issues.
94 */
95#define __put_user_asm(x, addr, err, op) \
96 __asm__ __volatile__( \
97 "1: "op" 0(%2),%1\n" \
98 "2:\n" \
99 ".section .fixup,\"ax\"\n" \
100 "3: l.addi %0,r0,%3\n" \
101 " l.j 2b\n" \
102 " l.nop\n" \
103 ".previous\n" \
104 ".section __ex_table,\"a\"\n" \
105 " .align 2\n" \
106 " .long 1b,3b\n" \
107 ".previous" \
108 : "=r"(err) \
109 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
110
111#define __put_user_asm2(x, addr, err) \
112 __asm__ __volatile__( \
113 "1: l.sw 0(%2),%1\n" \
114 "2: l.sw 4(%2),%H1\n" \
115 "3:\n" \
116 ".section .fixup,\"ax\"\n" \
117 "4: l.addi %0,r0,%3\n" \
118 " l.j 3b\n" \
119 " l.nop\n" \
120 ".previous\n" \
121 ".section __ex_table,\"a\"\n" \
122 " .align 2\n" \
123 " .long 1b,4b\n" \
124 " .long 2b,4b\n" \
125 ".previous" \
126 : "=r"(err) \
127 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
128
129#define __get_user_nocheck(x, ptr, size) \
130({ \
131 long __gu_err; \
132 __get_user_size((x), (ptr), (size), __gu_err); \
133 __gu_err; \
134})
135
136#define __get_user_check(x, ptr, size) \
137({ \
138 long __gu_err = -EFAULT; \
139 const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
140 if (access_ok(__gu_addr, size)) \
141 __get_user_size((x), __gu_addr, (size), __gu_err); \
142 else \
143 (x) = (__typeof__(*(ptr))) 0; \
144 __gu_err; \
145})
146
147extern long __get_user_bad(void);
148
149#define __get_user_size(x, ptr, size, retval) \
150do { \
151 retval = 0; \
152 switch (size) { \
153 case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
154 case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
155 case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
156 case 8: __get_user_asm2(x, ptr, retval); break; \
157 default: (x) = (__typeof__(*(ptr)))__get_user_bad(); \
158 } \
159} while (0)
160
161#define __get_user_asm(x, addr, err, op) \
162{ \
163 unsigned long __gu_tmp; \
164 __asm__ __volatile__( \
165 "1: "op" %1,0(%2)\n" \
166 "2:\n" \
167 ".section .fixup,\"ax\"\n" \
168 "3: l.addi %0,r0,%3\n" \
169 " l.addi %1,r0,0\n" \
170 " l.j 2b\n" \
171 " l.nop\n" \
172 ".previous\n" \
173 ".section __ex_table,\"a\"\n" \
174 " .align 2\n" \
175 " .long 1b,3b\n" \
176 ".previous" \
177 : "=r"(err), "=r"(__gu_tmp) \
178 : "r"(addr), "i"(-EFAULT), "0"(err)); \
179 (x) = (__typeof__(*(addr)))__gu_tmp; \
180}
181
182#define __get_user_asm2(x, addr, err) \
183{ \
184 unsigned long long __gu_tmp; \
185 __asm__ __volatile__( \
186 "1: l.lwz %1,0(%2)\n" \
187 "2: l.lwz %H1,4(%2)\n" \
188 "3:\n" \
189 ".section .fixup,\"ax\"\n" \
190 "4: l.addi %0,r0,%3\n" \
191 " l.addi %1,r0,0\n" \
192 " l.addi %H1,r0,0\n" \
193 " l.j 3b\n" \
194 " l.nop\n" \
195 ".previous\n" \
196 ".section __ex_table,\"a\"\n" \
197 " .align 2\n" \
198 " .long 1b,4b\n" \
199 " .long 2b,4b\n" \
200 ".previous" \
201 : "=r"(err), "=&r"(__gu_tmp) \
202 : "r"(addr), "i"(-EFAULT), "0"(err)); \
203 (x) = (__typeof__(*(addr)))( \
204 (__typeof__((x)-(x)))__gu_tmp); \
205}
206
207/* more complex routines */
208
209extern unsigned long __must_check
210__copy_tofrom_user(void *to, const void *from, unsigned long size);
211static inline unsigned long
212raw_copy_from_user(void *to, const void __user *from, unsigned long size)
213{
214 return __copy_tofrom_user(to, (__force const void *)from, size);
215}
216static inline unsigned long
217raw_copy_to_user(void __user *to, const void *from, unsigned long size)
218{
219 return __copy_tofrom_user((__force void *)to, from, size);
220}
221#define INLINE_COPY_FROM_USER
222#define INLINE_COPY_TO_USER
223
224extern unsigned long __clear_user(void __user *addr, unsigned long size);
225
226static inline __must_check unsigned long
227clear_user(void __user *addr, unsigned long size)
228{
229 if (likely(access_ok(addr, size)))
230 size = __clear_user(addr, size);
231 return size;
232}
233
234extern long strncpy_from_user(char *dest, const char __user *src, long count);
235
236extern __must_check long strnlen_user(const char __user *str, long n);
237
238#endif /* __ASM_OPENRISC_UACCESS_H */
1/*
2 * OpenRISC Linux
3 *
4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
6 * declaration.
7 *
8 * OpenRISC implementation:
9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
11 * et al.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 */
18
19#ifndef __ASM_OPENRISC_UACCESS_H
20#define __ASM_OPENRISC_UACCESS_H
21
22/*
23 * User space memory access functions
24 */
25#include <linux/errno.h>
26#include <linux/thread_info.h>
27#include <linux/prefetch.h>
28#include <linux/string.h>
29#include <asm/page.h>
30
31#define VERIFY_READ 0
32#define VERIFY_WRITE 1
33
34/*
35 * The fs value determines whether argument validity checking should be
36 * performed or not. If get_fs() == USER_DS, checking is performed, with
37 * get_fs() == KERNEL_DS, checking is bypassed.
38 *
39 * For historical reasons, these macros are grossly misnamed.
40 */
41
42/* addr_limit is the maximum accessible address for the task. we misuse
43 * the KERNEL_DS and USER_DS values to both assign and compare the
44 * addr_limit values through the equally misnamed get/set_fs macros.
45 * (see above)
46 */
47
48#define KERNEL_DS (~0UL)
49#define get_ds() (KERNEL_DS)
50
51#define USER_DS (TASK_SIZE)
52#define get_fs() (current_thread_info()->addr_limit)
53#define set_fs(x) (current_thread_info()->addr_limit = (x))
54
55#define segment_eq(a, b) ((a) == (b))
56
57/* Ensure that the range from addr to addr+size is all within the process'
58 * address space
59 */
60#define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs()-size))
61
62/* Ensure that addr is below task's addr_limit */
63#define __addr_ok(addr) ((unsigned long) addr < get_fs())
64
65#define access_ok(type, addr, size) \
66 __range_ok((unsigned long)addr, (unsigned long)size)
67
68/*
69 * The exception table consists of pairs of addresses: the first is the
70 * address of an instruction that is allowed to fault, and the second is
71 * the address at which the program should continue. No registers are
72 * modified, so it is entirely up to the continuation code to figure out
73 * what to do.
74 *
75 * All the routines below use bits of fixup code that are out of line
76 * with the main instruction path. This means when everything is well,
77 * we don't even have to jump over them. Further, they do not intrude
78 * on our cache or tlb entries.
79 */
80
81struct exception_table_entry {
82 unsigned long insn, fixup;
83};
84
85/*
86 * These are the main single-value transfer routines. They automatically
87 * use the right size if we just have the right pointer type.
88 *
89 * This gets kind of ugly. We want to return _two_ values in "get_user()"
90 * and yet we don't want to do any pointers, because that is too much
91 * of a performance impact. Thus we have a few rather ugly macros here,
92 * and hide all the uglyness from the user.
93 *
94 * The "__xxx" versions of the user access functions are versions that
95 * do not verify the address space, that must have been done previously
96 * with a separate "access_ok()" call (this is used when we do multiple
97 * accesses to the same area of user memory).
98 *
99 * As we use the same address space for kernel and user data on the
100 * PowerPC, we can just do these as direct assignments. (Of course, the
101 * exception handling means that it's no longer "just"...)
102 */
103#define get_user(x, ptr) \
104 __get_user_check((x), (ptr), sizeof(*(ptr)))
105#define put_user(x, ptr) \
106 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
107
108#define __get_user(x, ptr) \
109 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
110#define __put_user(x, ptr) \
111 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
112
113extern long __put_user_bad(void);
114
115#define __put_user_nocheck(x, ptr, size) \
116({ \
117 long __pu_err; \
118 __put_user_size((x), (ptr), (size), __pu_err); \
119 __pu_err; \
120})
121
122#define __put_user_check(x, ptr, size) \
123({ \
124 long __pu_err = -EFAULT; \
125 __typeof__(*(ptr)) *__pu_addr = (ptr); \
126 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
127 __put_user_size((x), __pu_addr, (size), __pu_err); \
128 __pu_err; \
129})
130
131#define __put_user_size(x, ptr, size, retval) \
132do { \
133 retval = 0; \
134 switch (size) { \
135 case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
136 case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
137 case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
138 case 8: __put_user_asm2(x, ptr, retval); break; \
139 default: __put_user_bad(); \
140 } \
141} while (0)
142
143struct __large_struct {
144 unsigned long buf[100];
145};
146#define __m(x) (*(struct __large_struct *)(x))
147
148/*
149 * We don't tell gcc that we are accessing memory, but this is OK
150 * because we do not write to any memory gcc knows about, so there
151 * are no aliasing issues.
152 */
153#define __put_user_asm(x, addr, err, op) \
154 __asm__ __volatile__( \
155 "1: "op" 0(%2),%1\n" \
156 "2:\n" \
157 ".section .fixup,\"ax\"\n" \
158 "3: l.addi %0,r0,%3\n" \
159 " l.j 2b\n" \
160 " l.nop\n" \
161 ".previous\n" \
162 ".section __ex_table,\"a\"\n" \
163 " .align 2\n" \
164 " .long 1b,3b\n" \
165 ".previous" \
166 : "=r"(err) \
167 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
168
169#define __put_user_asm2(x, addr, err) \
170 __asm__ __volatile__( \
171 "1: l.sw 0(%2),%1\n" \
172 "2: l.sw 4(%2),%H1\n" \
173 "3:\n" \
174 ".section .fixup,\"ax\"\n" \
175 "4: l.addi %0,r0,%3\n" \
176 " l.j 3b\n" \
177 " l.nop\n" \
178 ".previous\n" \
179 ".section __ex_table,\"a\"\n" \
180 " .align 2\n" \
181 " .long 1b,4b\n" \
182 " .long 2b,4b\n" \
183 ".previous" \
184 : "=r"(err) \
185 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
186
187#define __get_user_nocheck(x, ptr, size) \
188({ \
189 long __gu_err, __gu_val; \
190 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
191 (x) = (__force __typeof__(*(ptr)))__gu_val; \
192 __gu_err; \
193})
194
195#define __get_user_check(x, ptr, size) \
196({ \
197 long __gu_err = -EFAULT, __gu_val = 0; \
198 const __typeof__(*(ptr)) * __gu_addr = (ptr); \
199 if (access_ok(VERIFY_READ, __gu_addr, size)) \
200 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
201 (x) = (__force __typeof__(*(ptr)))__gu_val; \
202 __gu_err; \
203})
204
205extern long __get_user_bad(void);
206
207#define __get_user_size(x, ptr, size, retval) \
208do { \
209 retval = 0; \
210 switch (size) { \
211 case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
212 case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
213 case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
214 case 8: __get_user_asm2(x, ptr, retval); \
215 default: (x) = __get_user_bad(); \
216 } \
217} while (0)
218
219#define __get_user_asm(x, addr, err, op) \
220 __asm__ __volatile__( \
221 "1: "op" %1,0(%2)\n" \
222 "2:\n" \
223 ".section .fixup,\"ax\"\n" \
224 "3: l.addi %0,r0,%3\n" \
225 " l.addi %1,r0,0\n" \
226 " l.j 2b\n" \
227 " l.nop\n" \
228 ".previous\n" \
229 ".section __ex_table,\"a\"\n" \
230 " .align 2\n" \
231 " .long 1b,3b\n" \
232 ".previous" \
233 : "=r"(err), "=r"(x) \
234 : "r"(addr), "i"(-EFAULT), "0"(err))
235
236#define __get_user_asm2(x, addr, err) \
237 __asm__ __volatile__( \
238 "1: l.lwz %1,0(%2)\n" \
239 "2: l.lwz %H1,4(%2)\n" \
240 "3:\n" \
241 ".section .fixup,\"ax\"\n" \
242 "4: l.addi %0,r0,%3\n" \
243 " l.addi %1,r0,0\n" \
244 " l.addi %H1,r0,0\n" \
245 " l.j 3b\n" \
246 " l.nop\n" \
247 ".previous\n" \
248 ".section __ex_table,\"a\"\n" \
249 " .align 2\n" \
250 " .long 1b,4b\n" \
251 " .long 2b,4b\n" \
252 ".previous" \
253 : "=r"(err), "=&r"(x) \
254 : "r"(addr), "i"(-EFAULT), "0"(err))
255
256/* more complex routines */
257
258extern unsigned long __must_check
259__copy_tofrom_user(void *to, const void *from, unsigned long size);
260
261#define __copy_from_user(to, from, size) \
262 __copy_tofrom_user(to, from, size)
263#define __copy_to_user(to, from, size) \
264 __copy_tofrom_user(to, from, size)
265
266#define __copy_to_user_inatomic __copy_to_user
267#define __copy_from_user_inatomic __copy_from_user
268
269static inline unsigned long
270copy_from_user(void *to, const void *from, unsigned long n)
271{
272 unsigned long res = n;
273
274 if (likely(access_ok(VERIFY_READ, from, n)))
275 res = __copy_tofrom_user(to, from, n);
276 if (unlikely(res))
277 memset(to + (n - res), 0, res);
278 return res;
279}
280
281static inline unsigned long
282copy_to_user(void *to, const void *from, unsigned long n)
283{
284 if (likely(access_ok(VERIFY_WRITE, to, n)))
285 n = __copy_tofrom_user(to, from, n);
286 return n;
287}
288
289extern unsigned long __clear_user(void *addr, unsigned long size);
290
291static inline __must_check unsigned long
292clear_user(void *addr, unsigned long size)
293{
294 if (likely(access_ok(VERIFY_WRITE, addr, size)))
295 size = __clear_user(addr, size);
296 return size;
297}
298
299#define user_addr_max() \
300 (segment_eq(get_fs(), USER_DS) ? TASK_SIZE : ~0UL)
301
302extern long strncpy_from_user(char *dest, const char __user *src, long count);
303
304extern __must_check long strlen_user(const char __user *str);
305extern __must_check long strnlen_user(const char __user *str, long n);
306
307#endif /* __ASM_OPENRISC_UACCESS_H */