Remove 'type' argument from access_ok() function
[linux-2.6-block.git] / arch / nds32 / include / asm / uaccess.h
CommitLineData
ace02e2b
GH
1// SPDX-License-Identifier: GPL-2.0
2// Copyright (C) 2005-2017 Andes Technology Corporation
3
4#ifndef _ASMANDES_UACCESS_H
5#define _ASMANDES_UACCESS_H
6
7/*
8 * User space memory access functions
9 */
10#include <linux/sched.h>
11#include <asm/errno.h>
12#include <asm/memory.h>
13#include <asm/types.h>
14#include <linux/mm.h>
15
ace02e2b
GH
16#define __asmeq(x, y) ".ifnc " x "," y " ; .err ; .endif\n\t"
17
18/*
19 * The exception table consists of pairs of addresses: the first is the
20 * address of an instruction that is allowed to fault, and the second is
21 * the address at which the program should continue. No registers are
22 * modified, so it is entirely up to the continuation code to figure out
23 * what to do.
24 *
25 * All the routines below use bits of fixup code that are out of line
26 * with the main instruction path. This means when everything is well,
27 * we don't even have to jump over them. Further, they do not intrude
28 * on our cache or tlb entries.
29 */
30
31struct exception_table_entry {
32 unsigned long insn, fixup;
33};
34
35extern int fixup_exception(struct pt_regs *regs);
36
37#define KERNEL_DS ((mm_segment_t) { ~0UL })
7ef39548 38#define USER_DS ((mm_segment_t) {TASK_SIZE - 1})
ace02e2b
GH
39
40#define get_ds() (KERNEL_DS)
41#define get_fs() (current_thread_info()->addr_limit)
42#define user_addr_max get_fs
43
44static inline void set_fs(mm_segment_t fs)
45{
46 current_thread_info()->addr_limit = fs;
47}
48
7ef39548 49#define segment_eq(a, b) ((a) == (b))
ace02e2b
GH
50
51#define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs() -size))
52
96d4f267 53#define access_ok(addr, size) \
ace02e2b
GH
54 __range_ok((unsigned long)addr, (unsigned long)size)
55/*
56 * Single-value transfer routines. They automatically use the right
57 * size if we just have the right pointer type. Note that the functions
58 * which read from user space (*get_*) need to take care not to leak
59 * kernel data even if the calling code is buggy and fails to check
60 * the return value. This means zeroing out the destination variable
61 * or buffer on error. Normally this is done out of line by the
62 * fixup code, but there are a few places where it intrudes on the
63 * main code path. When we only write to user space, there is no
64 * problem.
65 *
66 * The "__xxx" versions of the user access functions do not verify the
67 * address space - it must have been done previously with a separate
68 * "access_ok()" call.
69 *
70 * The "xxx_error" versions set the third argument to EFAULT if an
71 * error occurs, and leave it unchanged on success. Note that these
72 * versions are void (ie, don't return a value as such).
73 */
74
487913ab 75#define get_user __get_user \
7ef39548
ZL
76
77#define __get_user(x, ptr) \
ace02e2b
GH
78({ \
79 long __gu_err = 0; \
487913ab 80 __get_user_check((x), (ptr), __gu_err); \
ace02e2b
GH
81 __gu_err; \
82})
83
7ef39548 84#define __get_user_error(x, ptr, err) \
ace02e2b 85({ \
487913ab 86 __get_user_check((x), (ptr), (err)); \
7ef39548 87 (void)0; \
ace02e2b
GH
88})
89
487913ab
ZL
90#define __get_user_check(x, ptr, err) \
91({ \
92 const __typeof__(*(ptr)) __user *__p = (ptr); \
93 might_fault(); \
96d4f267 94 if (access_ok(__p, sizeof(*__p))) { \
487913ab
ZL
95 __get_user_err((x), __p, (err)); \
96 } else { \
97 (x) = 0; (err) = -EFAULT; \
98 } \
99})
100
7ef39548 101#define __get_user_err(x, ptr, err) \
ace02e2b 102do { \
ace02e2b 103 unsigned long __gu_val; \
487913ab
ZL
104 __chk_user_ptr(ptr); \
105 switch (sizeof(*(ptr))) { \
ace02e2b 106 case 1: \
487913ab 107 __get_user_asm("lbi", __gu_val, (ptr), (err)); \
ace02e2b
GH
108 break; \
109 case 2: \
487913ab 110 __get_user_asm("lhi", __gu_val, (ptr), (err)); \
ace02e2b
GH
111 break; \
112 case 4: \
487913ab 113 __get_user_asm("lwi", __gu_val, (ptr), (err)); \
ace02e2b
GH
114 break; \
115 case 8: \
487913ab 116 __get_user_asm_dword(__gu_val, (ptr), (err)); \
ace02e2b
GH
117 break; \
118 default: \
119 BUILD_BUG(); \
120 break; \
121 } \
487913ab 122 (x) = (__force __typeof__(*(ptr)))__gu_val; \
ace02e2b
GH
123} while (0)
124
7ef39548
ZL
125#define __get_user_asm(inst, x, addr, err) \
126 __asm__ __volatile__ ( \
127 "1: "inst" %1,[%2]\n" \
128 "2:\n" \
129 " .section .fixup,\"ax\"\n" \
130 " .align 2\n" \
131 "3: move %0, %3\n" \
132 " move %1, #0\n" \
133 " b 2b\n" \
134 " .previous\n" \
135 " .section __ex_table,\"a\"\n" \
136 " .align 3\n" \
137 " .long 1b, 3b\n" \
138 " .previous" \
139 : "+r" (err), "=&r" (x) \
140 : "r" (addr), "i" (-EFAULT) \
141 : "cc")
ace02e2b
GH
142
143#ifdef __NDS32_EB__
144#define __gu_reg_oper0 "%H1"
145#define __gu_reg_oper1 "%L1"
146#else
147#define __gu_reg_oper0 "%L1"
148#define __gu_reg_oper1 "%H1"
149#endif
150
151#define __get_user_asm_dword(x, addr, err) \
7ef39548
ZL
152 __asm__ __volatile__ ( \
153 "\n1:\tlwi " __gu_reg_oper0 ",[%2]\n" \
154 "\n2:\tlwi " __gu_reg_oper1 ",[%2+4]\n" \
155 "3:\n" \
156 " .section .fixup,\"ax\"\n" \
157 " .align 2\n" \
158 "4: move %0, %3\n" \
159 " b 3b\n" \
160 " .previous\n" \
161 " .section __ex_table,\"a\"\n" \
162 " .align 3\n" \
163 " .long 1b, 4b\n" \
164 " .long 2b, 4b\n" \
165 " .previous" \
166 : "+r"(err), "=&r"(x) \
167 : "r"(addr), "i"(-EFAULT) \
168 : "cc")
169
487913ab 170#define put_user __put_user \
7ef39548
ZL
171
172#define __put_user(x, ptr) \
ace02e2b
GH
173({ \
174 long __pu_err = 0; \
7ef39548 175 __put_user_err((x), (ptr), __pu_err); \
ace02e2b
GH
176 __pu_err; \
177})
178
7ef39548 179#define __put_user_error(x, ptr, err) \
ace02e2b 180({ \
487913ab 181 __put_user_err((x), (ptr), (err)); \
7ef39548 182 (void)0; \
ace02e2b
GH
183})
184
487913ab
ZL
185#define __put_user_check(x, ptr, err) \
186({ \
187 __typeof__(*(ptr)) __user *__p = (ptr); \
188 might_fault(); \
96d4f267 189 if (access_ok(__p, sizeof(*__p))) { \
487913ab
ZL
190 __put_user_err((x), __p, (err)); \
191 } else { \
192 (err) = -EFAULT; \
193 } \
194})
195
7ef39548 196#define __put_user_err(x, ptr, err) \
ace02e2b 197do { \
487913ab
ZL
198 __typeof__(*(ptr)) __pu_val = (x); \
199 __chk_user_ptr(ptr); \
200 switch (sizeof(*(ptr))) { \
ace02e2b 201 case 1: \
487913ab 202 __put_user_asm("sbi", __pu_val, (ptr), (err)); \
ace02e2b
GH
203 break; \
204 case 2: \
487913ab 205 __put_user_asm("shi", __pu_val, (ptr), (err)); \
ace02e2b
GH
206 break; \
207 case 4: \
487913ab 208 __put_user_asm("swi", __pu_val, (ptr), (err)); \
ace02e2b
GH
209 break; \
210 case 8: \
487913ab 211 __put_user_asm_dword(__pu_val, (ptr), (err)); \
ace02e2b
GH
212 break; \
213 default: \
214 BUILD_BUG(); \
215 break; \
216 } \
217} while (0)
218
7ef39548
ZL
219#define __put_user_asm(inst, x, addr, err) \
220 __asm__ __volatile__ ( \
221 "1: "inst" %1,[%2]\n" \
222 "2:\n" \
223 " .section .fixup,\"ax\"\n" \
224 " .align 2\n" \
225 "3: move %0, %3\n" \
226 " b 2b\n" \
227 " .previous\n" \
228 " .section __ex_table,\"a\"\n" \
229 " .align 3\n" \
230 " .long 1b, 3b\n" \
231 " .previous" \
232 : "+r" (err) \
233 : "r" (x), "r" (addr), "i" (-EFAULT) \
234 : "cc")
ace02e2b
GH
235
236#ifdef __NDS32_EB__
237#define __pu_reg_oper0 "%H2"
238#define __pu_reg_oper1 "%L2"
239#else
240#define __pu_reg_oper0 "%L2"
241#define __pu_reg_oper1 "%H2"
242#endif
243
244#define __put_user_asm_dword(x, addr, err) \
7ef39548
ZL
245 __asm__ __volatile__ ( \
246 "\n1:\tswi " __pu_reg_oper0 ",[%1]\n" \
247 "\n2:\tswi " __pu_reg_oper1 ",[%1+4]\n" \
248 "3:\n" \
249 " .section .fixup,\"ax\"\n" \
250 " .align 2\n" \
251 "4: move %0, %3\n" \
252 " b 3b\n" \
253 " .previous\n" \
254 " .section __ex_table,\"a\"\n" \
255 " .align 3\n" \
256 " .long 1b, 4b\n" \
257 " .long 2b, 4b\n" \
258 " .previous" \
259 : "+r"(err) \
260 : "r"(addr), "r"(x), "i"(-EFAULT) \
261 : "cc")
262
ace02e2b
GH
263extern unsigned long __arch_clear_user(void __user * addr, unsigned long n);
264extern long strncpy_from_user(char *dest, const char __user * src, long count);
265extern __must_check long strlen_user(const char __user * str);
266extern __must_check long strnlen_user(const char __user * str, long n);
267extern unsigned long __arch_copy_from_user(void *to, const void __user * from,
268 unsigned long n);
269extern unsigned long __arch_copy_to_user(void __user * to, const void *from,
270 unsigned long n);
271
272#define raw_copy_from_user __arch_copy_from_user
273#define raw_copy_to_user __arch_copy_to_user
274
275#define INLINE_COPY_FROM_USER
276#define INLINE_COPY_TO_USER
277static inline unsigned long clear_user(void __user * to, unsigned long n)
278{
96d4f267 279 if (access_ok(to, n))
ace02e2b
GH
280 n = __arch_clear_user(to, n);
281 return n;
282}
283
284static inline unsigned long __clear_user(void __user * to, unsigned long n)
285{
286 return __arch_clear_user(to, n);
287}
288
289#endif /* _ASMNDS32_UACCESS_H */