4 * Linux architectural port borrowing liberally from similar works of
5 * others. All original copyrights apply as per the original source
8 * OpenRISC implementation:
9 * Copyright (C) 2003 Matjaz Breskvar <phoenix@bsemi.com>
10 * Copyright (C) 2010-2011 Jonas Bonn <jonas@southpole.se>
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.
19 #ifndef __ASM_OPENRISC_UACCESS_H
20 #define __ASM_OPENRISC_UACCESS_H
23 * User space memory access functions
25 #include <linux/errno.h>
26 #include <linux/thread_info.h>
27 #include <linux/prefetch.h>
28 #include <linux/string.h>
32 #define VERIFY_WRITE 1
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.
39 * For historical reasons, these macros are grossly misnamed.
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.
48 #define KERNEL_DS (~0UL)
49 #define get_ds() (KERNEL_DS)
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))
55 #define segment_eq(a, b) ((a) == (b))
57 /* Ensure that the range from addr to addr+size is all within the process'
60 #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs()-size))
62 /* Ensure that addr is below task's addr_limit */
63 #define __addr_ok(addr) ((unsigned long) addr < get_fs())
65 #define access_ok(type, addr, size) \
66 __range_ok((unsigned long)addr, (unsigned long)size)
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
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.
81 struct exception_table_entry
{
82 unsigned long insn
, fixup
;
85 /* Returns 0 if exception not found and fixup otherwise. */
86 extern unsigned long search_exception_table(unsigned long);
87 extern void sort_exception_table(void);
90 * These are the main single-value transfer routines. They automatically
91 * use the right size if we just have the right pointer type.
93 * This gets kind of ugly. We want to return _two_ values in "get_user()"
94 * and yet we don't want to do any pointers, because that is too much
95 * of a performance impact. Thus we have a few rather ugly macros here,
96 * and hide all the uglyness from the user.
98 * The "__xxx" versions of the user access functions are versions that
99 * do not verify the address space, that must have been done previously
100 * with a separate "access_ok()" call (this is used when we do multiple
101 * accesses to the same area of user memory).
103 * As we use the same address space for kernel and user data on the
104 * PowerPC, we can just do these as direct assignments. (Of course, the
105 * exception handling means that it's no longer "just"...)
107 #define get_user(x, ptr) \
108 __get_user_check((x), (ptr), sizeof(*(ptr)))
109 #define put_user(x, ptr) \
110 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
112 #define __get_user(x, ptr) \
113 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
114 #define __put_user(x, ptr) \
115 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
117 extern long __put_user_bad(void);
119 #define __put_user_nocheck(x, ptr, size) \
122 __put_user_size((x), (ptr), (size), __pu_err); \
126 #define __put_user_check(x, ptr, size) \
128 long __pu_err = -EFAULT; \
129 __typeof__(*(ptr)) *__pu_addr = (ptr); \
130 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
131 __put_user_size((x), __pu_addr, (size), __pu_err); \
135 #define __put_user_size(x, ptr, size, retval) \
139 case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
140 case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
141 case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
142 case 8: __put_user_asm2(x, ptr, retval); break; \
143 default: __put_user_bad(); \
147 struct __large_struct
{
148 unsigned long buf
[100];
150 #define __m(x) (*(struct __large_struct *)(x))
153 * We don't tell gcc that we are accessing memory, but this is OK
154 * because we do not write to any memory gcc knows about, so there
155 * are no aliasing issues.
157 #define __put_user_asm(x, addr, err, op) \
158 __asm__ __volatile__( \
159 "1: "op" 0(%2),%1\n" \
161 ".section .fixup,\"ax\"\n" \
162 "3: l.addi %0,r0,%3\n" \
166 ".section __ex_table,\"a\"\n" \
171 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
173 #define __put_user_asm2(x, addr, err) \
174 __asm__ __volatile__( \
175 "1: l.sw 0(%2),%1\n" \
176 "2: l.sw 4(%2),%H1\n" \
178 ".section .fixup,\"ax\"\n" \
179 "4: l.addi %0,r0,%3\n" \
183 ".section __ex_table,\"a\"\n" \
189 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
191 #define __get_user_nocheck(x, ptr, size) \
193 long __gu_err, __gu_val; \
194 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
195 (x) = (__typeof__(*(ptr)))__gu_val; \
199 #define __get_user_check(x, ptr, size) \
201 long __gu_err = -EFAULT, __gu_val = 0; \
202 const __typeof__(*(ptr)) * __gu_addr = (ptr); \
203 if (access_ok(VERIFY_READ, __gu_addr, size)) \
204 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
205 (x) = (__typeof__(*(ptr)))__gu_val; \
209 extern long __get_user_bad(void);
211 #define __get_user_size(x, ptr, size, retval) \
215 case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
216 case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
217 case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
218 case 8: __get_user_asm2(x, ptr, retval); \
219 default: (x) = __get_user_bad(); \
223 #define __get_user_asm(x, addr, err, op) \
224 __asm__ __volatile__( \
225 "1: "op" %1,0(%2)\n" \
227 ".section .fixup,\"ax\"\n" \
228 "3: l.addi %0,r0,%3\n" \
229 " l.addi %1,r0,0\n" \
233 ".section __ex_table,\"a\"\n" \
237 : "=r"(err), "=r"(x) \
238 : "r"(addr), "i"(-EFAULT), "0"(err))
240 #define __get_user_asm2(x, addr, err) \
241 __asm__ __volatile__( \
242 "1: l.lwz %1,0(%2)\n" \
243 "2: l.lwz %H1,4(%2)\n" \
245 ".section .fixup,\"ax\"\n" \
246 "4: l.addi %0,r0,%3\n" \
247 " l.addi %1,r0,0\n" \
248 " l.addi %H1,r0,0\n" \
252 ".section __ex_table,\"a\"\n" \
257 : "=r"(err), "=&r"(x) \
258 : "r"(addr), "i"(-EFAULT), "0"(err))
260 /* more complex routines */
262 extern unsigned long __must_check
263 __copy_tofrom_user(void *to
, const void *from
, unsigned long size
);
265 #define __copy_from_user(to, from, size) \
266 __copy_tofrom_user(to, from, size)
267 #define __copy_to_user(to, from, size) \
268 __copy_tofrom_user(to, from, size)
270 #define __copy_to_user_inatomic __copy_to_user
271 #define __copy_from_user_inatomic __copy_from_user
273 static inline unsigned long
274 copy_from_user(void *to
, const void *from
, unsigned long n
)
278 if (access_ok(VERIFY_READ
, from
, n
))
279 return __copy_tofrom_user(to
, from
, n
);
280 if ((unsigned long)from
< TASK_SIZE
) {
281 over
= (unsigned long)from
+ n
- TASK_SIZE
;
282 return __copy_tofrom_user(to
, from
, n
- over
) + over
;
287 static inline unsigned long
288 copy_to_user(void *to
, const void *from
, unsigned long n
)
292 if (access_ok(VERIFY_WRITE
, to
, n
))
293 return __copy_tofrom_user(to
, from
, n
);
294 if ((unsigned long)to
< TASK_SIZE
) {
295 over
= (unsigned long)to
+ n
- TASK_SIZE
;
296 return __copy_tofrom_user(to
, from
, n
- over
) + over
;
301 extern unsigned long __clear_user(void *addr
, unsigned long size
);
303 static inline __must_check
unsigned long
304 clear_user(void *addr
, unsigned long size
)
307 if (access_ok(VERIFY_WRITE
, addr
, size
))
308 return __clear_user(addr
, size
);
309 if ((unsigned long)addr
< TASK_SIZE
) {
310 unsigned long over
= (unsigned long)addr
+ size
- TASK_SIZE
;
311 return __clear_user(addr
, size
- over
) + over
;
316 #define user_addr_max() \
317 (segment_eq(get_fs(), USER_DS) ? TASK_SIZE : ~0UL)
319 extern long strncpy_from_user(char *dest
, const char __user
*src
, long count
);
321 extern __must_check
long strlen_user(const char __user
*str
);
322 extern __must_check
long strnlen_user(const char __user
*str
, long n
);
324 #endif /* __ASM_OPENRISC_UACCESS_H */