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/prefetch.h>
26 #include <linux/string.h>
30 * The fs value determines whether argument validity checking should be
31 * performed or not. If get_fs() == USER_DS, checking is performed, with
32 * get_fs() == KERNEL_DS, checking is bypassed.
34 * For historical reasons, these macros are grossly misnamed.
37 /* addr_limit is the maximum accessible address for the task. we misuse
38 * the KERNEL_DS and USER_DS values to both assign and compare the
39 * addr_limit values through the equally misnamed get/set_fs macros.
43 #define KERNEL_DS (~0UL)
44 #define get_ds() (KERNEL_DS)
46 #define USER_DS (TASK_SIZE)
47 #define get_fs() (current_thread_info()->addr_limit)
48 #define set_fs(x) (current_thread_info()->addr_limit = (x))
50 #define segment_eq(a, b) ((a) == (b))
52 /* Ensure that the range from addr to addr+size is all within the process'
55 #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs()-size))
57 /* Ensure that addr is below task's addr_limit */
58 #define __addr_ok(addr) ((unsigned long) addr < get_fs())
60 #define access_ok(type, addr, size) \
61 __range_ok((unsigned long)addr, (unsigned long)size)
64 * The exception table consists of pairs of addresses: the first is the
65 * address of an instruction that is allowed to fault, and the second is
66 * the address at which the program should continue. No registers are
67 * modified, so it is entirely up to the continuation code to figure out
70 * All the routines below use bits of fixup code that are out of line
71 * with the main instruction path. This means when everything is well,
72 * we don't even have to jump over them. Further, they do not intrude
73 * on our cache or tlb entries.
76 struct exception_table_entry {
77 unsigned long insn, fixup;
81 * These are the main single-value transfer routines. They automatically
82 * use the right size if we just have the right pointer type.
84 * This gets kind of ugly. We want to return _two_ values in "get_user()"
85 * and yet we don't want to do any pointers, because that is too much
86 * of a performance impact. Thus we have a few rather ugly macros here,
87 * and hide all the uglyness from the user.
89 * The "__xxx" versions of the user access functions are versions that
90 * do not verify the address space, that must have been done previously
91 * with a separate "access_ok()" call (this is used when we do multiple
92 * accesses to the same area of user memory).
94 * As we use the same address space for kernel and user data on the
95 * PowerPC, we can just do these as direct assignments. (Of course, the
96 * exception handling means that it's no longer "just"...)
98 #define get_user(x, ptr) \
99 __get_user_check((x), (ptr), sizeof(*(ptr)))
100 #define put_user(x, ptr) \
101 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
103 #define __get_user(x, ptr) \
104 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
105 #define __put_user(x, ptr) \
106 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
108 extern long __put_user_bad(void);
110 #define __put_user_nocheck(x, ptr, size) \
113 __put_user_size((x), (ptr), (size), __pu_err); \
117 #define __put_user_check(x, ptr, size) \
119 long __pu_err = -EFAULT; \
120 __typeof__(*(ptr)) *__pu_addr = (ptr); \
121 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
122 __put_user_size((x), __pu_addr, (size), __pu_err); \
126 #define __put_user_size(x, ptr, size, retval) \
130 case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
131 case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
132 case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
133 case 8: __put_user_asm2(x, ptr, retval); break; \
134 default: __put_user_bad(); \
138 struct __large_struct {
139 unsigned long buf[100];
141 #define __m(x) (*(struct __large_struct *)(x))
144 * We don't tell gcc that we are accessing memory, but this is OK
145 * because we do not write to any memory gcc knows about, so there
146 * are no aliasing issues.
148 #define __put_user_asm(x, addr, err, op) \
149 __asm__ __volatile__( \
150 "1: "op" 0(%2),%1\n" \
152 ".section .fixup,\"ax\"\n" \
153 "3: l.addi %0,r0,%3\n" \
157 ".section __ex_table,\"a\"\n" \
162 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
164 #define __put_user_asm2(x, addr, err) \
165 __asm__ __volatile__( \
166 "1: l.sw 0(%2),%1\n" \
167 "2: l.sw 4(%2),%H1\n" \
169 ".section .fixup,\"ax\"\n" \
170 "4: l.addi %0,r0,%3\n" \
174 ".section __ex_table,\"a\"\n" \
180 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
182 #define __get_user_nocheck(x, ptr, size) \
184 long __gu_err, __gu_val; \
185 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
186 (x) = (__force __typeof__(*(ptr)))__gu_val; \
190 #define __get_user_check(x, ptr, size) \
192 long __gu_err = -EFAULT, __gu_val = 0; \
193 const __typeof__(*(ptr)) * __gu_addr = (ptr); \
194 if (access_ok(VERIFY_READ, __gu_addr, size)) \
195 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
196 (x) = (__force __typeof__(*(ptr)))__gu_val; \
200 extern long __get_user_bad(void);
202 #define __get_user_size(x, ptr, size, retval) \
206 case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
207 case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
208 case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
209 case 8: __get_user_asm2(x, ptr, retval); \
210 default: (x) = __get_user_bad(); \
214 #define __get_user_asm(x, addr, err, op) \
215 __asm__ __volatile__( \
216 "1: "op" %1,0(%2)\n" \
218 ".section .fixup,\"ax\"\n" \
219 "3: l.addi %0,r0,%3\n" \
220 " l.addi %1,r0,0\n" \
224 ".section __ex_table,\"a\"\n" \
228 : "=r"(err), "=r"(x) \
229 : "r"(addr), "i"(-EFAULT), "0"(err))
231 #define __get_user_asm2(x, addr, err) \
232 __asm__ __volatile__( \
233 "1: l.lwz %1,0(%2)\n" \
234 "2: l.lwz %H1,4(%2)\n" \
236 ".section .fixup,\"ax\"\n" \
237 "4: l.addi %0,r0,%3\n" \
238 " l.addi %1,r0,0\n" \
239 " l.addi %H1,r0,0\n" \
243 ".section __ex_table,\"a\"\n" \
248 : "=r"(err), "=&r"(x) \
249 : "r"(addr), "i"(-EFAULT), "0"(err))
251 /* more complex routines */
253 extern unsigned long __must_check
254 __copy_tofrom_user(void *to, const void *from, unsigned long size);
256 #define __copy_from_user(to, from, size) \
257 __copy_tofrom_user(to, from, size)
258 #define __copy_to_user(to, from, size) \
259 __copy_tofrom_user(to, from, size)
261 #define __copy_to_user_inatomic __copy_to_user
262 #define __copy_from_user_inatomic __copy_from_user
264 static inline unsigned long
265 copy_from_user(void *to, const void *from, unsigned long n)
267 unsigned long res = n;
269 if (likely(access_ok(VERIFY_READ, from, n)))
270 res = __copy_tofrom_user(to, from, n);
272 memset(to + (n - res), 0, res);
276 static inline unsigned long
277 copy_to_user(void *to, const void *from, unsigned long n)
279 if (likely(access_ok(VERIFY_WRITE, to, n)))
280 n = __copy_tofrom_user(to, from, n);
284 extern unsigned long __clear_user(void *addr, unsigned long size);
286 static inline __must_check unsigned long
287 clear_user(void *addr, unsigned long size)
289 if (likely(access_ok(VERIFY_WRITE, addr, size)))
290 size = __clear_user(addr, size);
294 #define user_addr_max() \
295 (uaccess_kernel() ? ~0UL : TASK_SIZE)
297 extern long strncpy_from_user(char *dest, const char __user *src, long count);
299 extern __must_check long strlen_user(const char __user *str);
300 extern __must_check long strnlen_user(const char __user *str, long n);
302 #endif /* __ASM_OPENRISC_UACCESS_H */