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>
20 #include <asm/extable.h>
25 #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
26 #define USER_DS MAKE_MM_SEG(0x80000000UL)
27 #define KERNEL_DS MAKE_MM_SEG(0)
30 #define get_fs() (current_thread_info()->addr_limit)
31 #define set_fs(seg) (current_thread_info()->addr_limit = (seg))
33 #define segment_eq(a, b) ((a).seg == (b).seg)
35 #define __access_ok(addr, len) \
36 (((signed long)(((long)get_fs().seg) & \
37 ((long)(addr) | (((long)(addr)) + (len)) | (len)))) == 0)
39 #define access_ok(addr, len) \
40 likely(__access_ok((unsigned long)(addr), (unsigned long)(len)))
42 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
44 #define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE)
50 static inline unsigned long __must_check
__clear_user(void __user
*to
,
53 __asm__
__volatile__ (
54 "1: stb zero, 0(%1)\n"
69 static inline unsigned long __must_check
clear_user(void __user
*to
,
72 if (!access_ok(to
, n
))
74 return __clear_user(to
, n
);
78 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
80 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
81 #define INLINE_COPY_FROM_USER
82 #define INLINE_COPY_TO_USER
84 extern long strncpy_from_user(char *__to
, const char __user
*__from
,
86 extern __must_check
long strlen_user(const char __user
*str
);
87 extern __must_check
long strnlen_user(const char __user
*s
, long n
);
89 /* Optimized macros */
90 #define __get_user_asm(val, insn, addr, err) \
92 __asm__ __volatile__( \
94 "1: " insn " %1, 0(%2)\n" \
97 " .section __ex_table,\"a\"\n" \
100 : "=&r" (err), "=r" (val) \
101 : "r" (addr), "i" (-EFAULT)); \
104 #define __get_user_unknown(val, size, ptr, err) do { \
106 if (__copy_from_user(&(val), ptr, size)) { \
111 #define __get_user_common(val, size, ptr, err) \
115 __get_user_asm(val, "ldbu", ptr, err); \
118 __get_user_asm(val, "ldhu", ptr, err); \
121 __get_user_asm(val, "ldw", ptr, err); \
124 __get_user_unknown(val, size, ptr, err); \
129 #define __get_user(x, ptr) \
131 long __gu_err = -EFAULT; \
132 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
133 unsigned long __gu_val = 0; \
134 __get_user_common(__gu_val, sizeof(*(ptr)), __gu_ptr, __gu_err);\
135 (x) = (__force __typeof__(x))__gu_val; \
139 #define get_user(x, ptr) \
141 long __gu_err = -EFAULT; \
142 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
143 unsigned long __gu_val = 0; \
144 if (access_ok( __gu_ptr, sizeof(*__gu_ptr))) \
145 __get_user_common(__gu_val, sizeof(*__gu_ptr), \
146 __gu_ptr, __gu_err); \
147 (x) = (__force __typeof__(x))__gu_val; \
151 #define __put_user_asm(val, insn, ptr, err) \
153 __asm__ __volatile__( \
155 "1: " insn " %1, 0(%2)\n" \
158 " .section __ex_table,\"a\"\n" \
162 : "r" (val), "r" (ptr), "i" (-EFAULT)); \
165 #define put_user(x, ptr) \
167 long __pu_err = -EFAULT; \
168 __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
169 __typeof__(*(ptr)) __pu_val = (__typeof(*ptr))(x); \
170 if (access_ok(__pu_ptr, sizeof(*__pu_ptr))) { \
171 switch (sizeof(*__pu_ptr)) { \
173 __put_user_asm(__pu_val, "stb", __pu_ptr, __pu_err); \
176 __put_user_asm(__pu_val, "sth", __pu_ptr, __pu_err); \
179 __put_user_asm(__pu_val, "stw", __pu_ptr, __pu_err); \
182 /* XXX: This looks wrong... */ \
184 if (copy_to_user(__pu_ptr, &(__pu_val), \
185 sizeof(*__pu_ptr))) \
186 __pu_err = -EFAULT; \
193 #define __put_user(x, ptr) put_user(x, ptr)
195 #endif /* _ASM_NIOS2_UACCESS_H */