2 * User space memory access functions for Nios II
4 * Copyright (C) 2010-2011, Tobias Klauser <tklauser@distanz.ch>
5 * Copyright (C) 2009, Wind River Systems Inc
6 * Implemented by fredrik.markstrom@gmail.com and ivarholmqvist@gmail.com
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file "COPYING" in the main directory of this archive
13 #ifndef _ASM_NIOS2_UACCESS_H
14 #define _ASM_NIOS2_UACCESS_H
16 #include <linux/errno.h>
17 #include <linux/thread_info.h>
18 #include <linux/string.h>
23 #define VERIFY_WRITE 1
26 * The exception table consists of pairs of addresses: the first is the
27 * address of an instruction that is allowed to fault, and the second is
28 * the address at which the program should continue. No registers are
29 * modified, so it is entirely up to the continuation code to figure out
32 * All the routines below use bits of fixup code that are out of line
33 * with the main instruction path. This means when everything is well,
34 * we don't even have to jump over them. Further, they do not intrude
35 * on our cache or tlb entries.
37 struct exception_table_entry
{
42 extern int fixup_exception(struct pt_regs
*regs
);
47 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
48 #define USER_DS MAKE_MM_SEG(0x80000000UL)
49 #define KERNEL_DS MAKE_MM_SEG(0)
51 #define get_ds() (KERNEL_DS)
53 #define get_fs() (current_thread_info()->addr_limit)
54 #define set_fs(seg) (current_thread_info()->addr_limit = (seg))
56 #define segment_eq(a, b) ((a).seg == (b).seg)
58 #define __access_ok(addr, len) \
59 (((signed long)(((long)get_fs().seg) & \
60 ((long)(addr) | (((long)(addr)) + (len)) | (len)))) == 0)
62 #define access_ok(type, addr, len) \
63 likely(__access_ok((unsigned long)(addr), (unsigned long)(len)))
65 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
71 static inline unsigned long __must_check
__clear_user(void __user
*to
,
74 __asm__
__volatile__ (
75 "1: stb zero, 0(%1)\n"
90 static inline unsigned long __must_check
clear_user(void __user
*to
,
93 if (!access_ok(VERIFY_WRITE
, to
, n
))
95 return __clear_user(to
, n
);
98 extern long __copy_from_user(void *to
, const void __user
*from
,
100 extern long __copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
102 static inline long copy_from_user(void *to
, const void __user
*from
,
105 if (!access_ok(VERIFY_READ
, from
, n
))
107 return __copy_from_user(to
, from
, n
);
110 static inline long copy_to_user(void __user
*to
, const void *from
,
113 if (!access_ok(VERIFY_WRITE
, to
, n
))
115 return __copy_to_user(to
, from
, n
);
118 extern long strncpy_from_user(char *__to
, const char __user
*__from
,
120 extern long strnlen_user(const char __user
*s
, long n
);
122 #define __copy_from_user_inatomic __copy_from_user
123 #define __copy_to_user_inatomic __copy_to_user
125 /* Optimized macros */
126 #define __get_user_asm(val, insn, addr, err) \
128 __asm__ __volatile__( \
130 "1: " insn " %1, 0(%2)\n" \
133 " .section __ex_table,\"a\"\n" \
136 : "=&r" (err), "=r" (val) \
137 : "r" (addr), "i" (-EFAULT)); \
140 #define __get_user_unknown(val, size, ptr, err) do { \
142 if (copy_from_user(&(val), ptr, size)) { \
147 #define __get_user_common(val, size, ptr, err) \
151 __get_user_asm(val, "ldbu", ptr, err); \
154 __get_user_asm(val, "ldhu", ptr, err); \
157 __get_user_asm(val, "ldw", ptr, err); \
160 __get_user_unknown(val, size, ptr, err); \
165 #define __get_user(x, ptr) \
167 long __gu_err = -EFAULT; \
168 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
169 unsigned long __gu_val; \
170 __get_user_common(__gu_val, sizeof(*(ptr)), __gu_ptr, __gu_err);\
171 (x) = (__force __typeof__(x))__gu_val; \
175 #define get_user(x, ptr) \
177 long __gu_err = -EFAULT; \
178 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
179 unsigned long __gu_val = 0; \
180 if (access_ok(VERIFY_READ, __gu_ptr, sizeof(*__gu_ptr))) \
181 __get_user_common(__gu_val, sizeof(*__gu_ptr), \
182 __gu_ptr, __gu_err); \
183 (x) = (__force __typeof__(x))__gu_val; \
187 #define __put_user_asm(val, insn, ptr, err) \
189 __asm__ __volatile__( \
191 "1: " insn " %1, 0(%2)\n" \
194 " .section __ex_table,\"a\"\n" \
198 : "r" (val), "r" (ptr), "i" (-EFAULT)); \
201 #define put_user(x, ptr) \
203 long __pu_err = -EFAULT; \
204 __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
205 __typeof__(*(ptr)) __pu_val = (__typeof(*ptr))(x); \
206 if (access_ok(VERIFY_WRITE, __pu_ptr, sizeof(*__pu_ptr))) { \
207 switch (sizeof(*__pu_ptr)) { \
209 __put_user_asm(__pu_val, "stb", __pu_ptr, __pu_err); \
212 __put_user_asm(__pu_val, "sth", __pu_ptr, __pu_err); \
215 __put_user_asm(__pu_val, "stw", __pu_ptr, __pu_err); \
218 /* XXX: This looks wrong... */ \
220 if (copy_to_user(__pu_ptr, &(__pu_val), \
221 sizeof(*__pu_ptr))) \
222 __pu_err = -EFAULT; \
229 #define __put_user(x, ptr) put_user(x, ptr)
231 #endif /* _ASM_NIOS2_UACCESS_H */