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>
28 #include <asm/extable.h>
31 * The fs value determines whether argument validity checking should be
32 * performed or not. If get_fs() == USER_DS, checking is performed, with
33 * get_fs() == KERNEL_DS, checking is bypassed.
35 * For historical reasons, these macros are grossly misnamed.
38 /* addr_limit is the maximum accessible address for the task. we misuse
39 * the KERNEL_DS and USER_DS values to both assign and compare the
40 * addr_limit values through the equally misnamed get/set_fs macros.
44 #define KERNEL_DS (~0UL)
45 #define get_ds() (KERNEL_DS)
47 #define USER_DS (TASK_SIZE)
48 #define get_fs() (current_thread_info()->addr_limit)
49 #define set_fs(x) (current_thread_info()->addr_limit = (x))
51 #define segment_eq(a, b) ((a) == (b))
53 /* Ensure that the range from addr to addr+size is all within the process'
56 #define __range_ok(addr, size) (size <= get_fs() && addr <= (get_fs()-size))
58 /* Ensure that addr is below task's addr_limit */
59 #define __addr_ok(addr) ((unsigned long) addr < get_fs())
61 #define access_ok(type, addr, size) \
63 unsigned long __ao_addr = (unsigned long)(addr); \
64 unsigned long __ao_size = (unsigned long)(size); \
65 __range_ok(__ao_addr, __ao_size); \
69 * These are the main single-value transfer routines. They automatically
70 * use the right size if we just have the right pointer type.
72 * This gets kind of ugly. We want to return _two_ values in "get_user()"
73 * and yet we don't want to do any pointers, because that is too much
74 * of a performance impact. Thus we have a few rather ugly macros here,
75 * and hide all the uglyness from the user.
77 * The "__xxx" versions of the user access functions are versions that
78 * do not verify the address space, that must have been done previously
79 * with a separate "access_ok()" call (this is used when we do multiple
80 * accesses to the same area of user memory).
82 * As we use the same address space for kernel and user data on the
83 * PowerPC, we can just do these as direct assignments. (Of course, the
84 * exception handling means that it's no longer "just"...)
86 #define get_user(x, ptr) \
87 __get_user_check((x), (ptr), sizeof(*(ptr)))
88 #define put_user(x, ptr) \
89 __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
91 #define __get_user(x, ptr) \
92 __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
93 #define __put_user(x, ptr) \
94 __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
96 extern long __put_user_bad(void);
98 #define __put_user_nocheck(x, ptr, size) \
101 __put_user_size((x), (ptr), (size), __pu_err); \
105 #define __put_user_check(x, ptr, size) \
107 long __pu_err = -EFAULT; \
108 __typeof__(*(ptr)) *__pu_addr = (ptr); \
109 if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
110 __put_user_size((x), __pu_addr, (size), __pu_err); \
114 #define __put_user_size(x, ptr, size, retval) \
118 case 1: __put_user_asm(x, ptr, retval, "l.sb"); break; \
119 case 2: __put_user_asm(x, ptr, retval, "l.sh"); break; \
120 case 4: __put_user_asm(x, ptr, retval, "l.sw"); break; \
121 case 8: __put_user_asm2(x, ptr, retval); break; \
122 default: __put_user_bad(); \
126 struct __large_struct {
127 unsigned long buf[100];
129 #define __m(x) (*(struct __large_struct *)(x))
132 * We don't tell gcc that we are accessing memory, but this is OK
133 * because we do not write to any memory gcc knows about, so there
134 * are no aliasing issues.
136 #define __put_user_asm(x, addr, err, op) \
137 __asm__ __volatile__( \
138 "1: "op" 0(%2),%1\n" \
140 ".section .fixup,\"ax\"\n" \
141 "3: l.addi %0,r0,%3\n" \
145 ".section __ex_table,\"a\"\n" \
150 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
152 #define __put_user_asm2(x, addr, err) \
153 __asm__ __volatile__( \
154 "1: l.sw 0(%2),%1\n" \
155 "2: l.sw 4(%2),%H1\n" \
157 ".section .fixup,\"ax\"\n" \
158 "4: l.addi %0,r0,%3\n" \
162 ".section __ex_table,\"a\"\n" \
168 : "r"(x), "r"(addr), "i"(-EFAULT), "0"(err))
170 #define __get_user_nocheck(x, ptr, size) \
172 long __gu_err, __gu_val; \
173 __get_user_size(__gu_val, (ptr), (size), __gu_err); \
174 (x) = (__force __typeof__(*(ptr)))__gu_val; \
178 #define __get_user_check(x, ptr, size) \
180 long __gu_err = -EFAULT, __gu_val = 0; \
181 const __typeof__(*(ptr)) * __gu_addr = (ptr); \
182 if (access_ok(VERIFY_READ, __gu_addr, size)) \
183 __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
184 (x) = (__force __typeof__(*(ptr)))__gu_val; \
188 extern long __get_user_bad(void);
190 #define __get_user_size(x, ptr, size, retval) \
194 case 1: __get_user_asm(x, ptr, retval, "l.lbz"); break; \
195 case 2: __get_user_asm(x, ptr, retval, "l.lhz"); break; \
196 case 4: __get_user_asm(x, ptr, retval, "l.lwz"); break; \
197 case 8: __get_user_asm2(x, ptr, retval); break; \
198 default: (x) = __get_user_bad(); \
202 #define __get_user_asm(x, addr, err, op) \
203 __asm__ __volatile__( \
204 "1: "op" %1,0(%2)\n" \
206 ".section .fixup,\"ax\"\n" \
207 "3: l.addi %0,r0,%3\n" \
208 " l.addi %1,r0,0\n" \
212 ".section __ex_table,\"a\"\n" \
216 : "=r"(err), "=r"(x) \
217 : "r"(addr), "i"(-EFAULT), "0"(err))
219 #define __get_user_asm2(x, addr, err) \
220 __asm__ __volatile__( \
221 "1: l.lwz %1,0(%2)\n" \
222 "2: l.lwz %H1,4(%2)\n" \
224 ".section .fixup,\"ax\"\n" \
225 "4: l.addi %0,r0,%3\n" \
226 " l.addi %1,r0,0\n" \
227 " l.addi %H1,r0,0\n" \
231 ".section __ex_table,\"a\"\n" \
236 : "=r"(err), "=&r"(x) \
237 : "r"(addr), "i"(-EFAULT), "0"(err))
239 /* more complex routines */
241 extern unsigned long __must_check
242 __copy_tofrom_user(void *to, const void *from, unsigned long size);
243 static inline unsigned long
244 raw_copy_from_user(void *to, const void __user *from, unsigned long size)
246 return __copy_tofrom_user(to, (__force const void *)from, size);
248 static inline unsigned long
249 raw_copy_to_user(void *to, const void __user *from, unsigned long size)
251 return __copy_tofrom_user((__force void *)to, from, size);
253 #define INLINE_COPY_FROM_USER
254 #define INLINE_COPY_TO_USER
256 extern unsigned long __clear_user(void *addr, unsigned long size);
258 static inline __must_check unsigned long
259 clear_user(void *addr, unsigned long size)
261 if (likely(access_ok(VERIFY_WRITE, addr, size)))
262 size = __clear_user(addr, size);
266 #define user_addr_max() \
267 (uaccess_kernel() ? ~0UL : TASK_SIZE)
269 extern long strncpy_from_user(char *dest, const char __user *src, long count);
271 extern __must_check long strnlen_user(const char __user *str, long n);
273 #endif /* __ASM_OPENRISC_UACCESS_H */