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/string.h>
21 * The exception table consists of pairs of addresses: the first is the
22 * address of an instruction that is allowed to fault, and the second is
23 * the address at which the program should continue. No registers are
24 * modified, so it is entirely up to the continuation code to figure out
27 * All the routines below use bits of fixup code that are out of line
28 * with the main instruction path. This means when everything is well,
29 * we don't even have to jump over them. Further, they do not intrude
30 * on our cache or tlb entries.
32 struct exception_table_entry {
37 extern int fixup_exception(struct pt_regs *regs);
42 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
43 #define USER_DS MAKE_MM_SEG(0x80000000UL)
44 #define KERNEL_DS MAKE_MM_SEG(0)
46 #define get_ds() (KERNEL_DS)
48 #define get_fs() (current_thread_info()->addr_limit)
49 #define set_fs(seg) (current_thread_info()->addr_limit = (seg))
51 #define segment_eq(a, b) ((a).seg == (b).seg)
53 #define __access_ok(addr, len) \
54 (((signed long)(((long)get_fs().seg) & \
55 ((long)(addr) | (((long)(addr)) + (len)) | (len)))) == 0)
57 #define access_ok(type, addr, len) \
58 likely(__access_ok((unsigned long)(addr), (unsigned long)(len)))
60 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
66 static inline unsigned long __must_check __clear_user(void __user *to,
69 __asm__ __volatile__ (
70 "1: stb zero, 0(%1)\n"
85 static inline unsigned long __must_check clear_user(void __user *to,
88 if (!access_ok(VERIFY_WRITE, to, n))
90 return __clear_user(to, n);
93 extern long __copy_from_user(void *to, const void __user *from,
95 extern long __copy_to_user(void __user *to, const void *from, unsigned long n);
97 static inline long copy_from_user(void *to, const void __user *from,
100 unsigned long res = n;
101 if (access_ok(VERIFY_READ, from, n))
102 res = __copy_from_user(to, from, n);
104 memset(to + (n - res), 0, res);
108 static inline long copy_to_user(void __user *to, const void *from,
111 if (!access_ok(VERIFY_WRITE, to, n))
113 return __copy_to_user(to, from, n);
116 extern long strncpy_from_user(char *__to, const char __user *__from,
118 extern long strnlen_user(const char __user *s, long n);
120 #define __copy_from_user_inatomic __copy_from_user
121 #define __copy_to_user_inatomic __copy_to_user
123 /* Optimized macros */
124 #define __get_user_asm(val, insn, addr, err) \
126 __asm__ __volatile__( \
128 "1: " insn " %1, 0(%2)\n" \
131 " .section __ex_table,\"a\"\n" \
134 : "=&r" (err), "=r" (val) \
135 : "r" (addr), "i" (-EFAULT)); \
138 #define __get_user_unknown(val, size, ptr, err) do { \
140 if (__copy_from_user(&(val), ptr, size)) { \
145 #define __get_user_common(val, size, ptr, err) \
149 __get_user_asm(val, "ldbu", ptr, err); \
152 __get_user_asm(val, "ldhu", ptr, err); \
155 __get_user_asm(val, "ldw", ptr, err); \
158 __get_user_unknown(val, size, ptr, err); \
163 #define __get_user(x, ptr) \
165 long __gu_err = -EFAULT; \
166 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
167 unsigned long __gu_val = 0; \
168 __get_user_common(__gu_val, sizeof(*(ptr)), __gu_ptr, __gu_err);\
169 (x) = (__force __typeof__(x))__gu_val; \
173 #define get_user(x, ptr) \
175 long __gu_err = -EFAULT; \
176 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
177 unsigned long __gu_val = 0; \
178 if (access_ok(VERIFY_READ, __gu_ptr, sizeof(*__gu_ptr))) \
179 __get_user_common(__gu_val, sizeof(*__gu_ptr), \
180 __gu_ptr, __gu_err); \
181 (x) = (__force __typeof__(x))__gu_val; \
185 #define __put_user_asm(val, insn, ptr, err) \
187 __asm__ __volatile__( \
189 "1: " insn " %1, 0(%2)\n" \
192 " .section __ex_table,\"a\"\n" \
196 : "r" (val), "r" (ptr), "i" (-EFAULT)); \
199 #define put_user(x, ptr) \
201 long __pu_err = -EFAULT; \
202 __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
203 __typeof__(*(ptr)) __pu_val = (__typeof(*ptr))(x); \
204 if (access_ok(VERIFY_WRITE, __pu_ptr, sizeof(*__pu_ptr))) { \
205 switch (sizeof(*__pu_ptr)) { \
207 __put_user_asm(__pu_val, "stb", __pu_ptr, __pu_err); \
210 __put_user_asm(__pu_val, "sth", __pu_ptr, __pu_err); \
213 __put_user_asm(__pu_val, "stw", __pu_ptr, __pu_err); \
216 /* XXX: This looks wrong... */ \
218 if (copy_to_user(__pu_ptr, &(__pu_val), \
219 sizeof(*__pu_ptr))) \
220 __pu_err = -EFAULT; \
227 #define __put_user(x, ptr) put_user(x, ptr)
229 #endif /* _ASM_NIOS2_UACCESS_H */