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)
29 #define get_ds() (KERNEL_DS)
31 #define get_fs() (current_thread_info()->addr_limit)
32 #define set_fs(seg) (current_thread_info()->addr_limit = (seg))
34 #define segment_eq(a, b) ((a).seg == (b).seg)
36 #define __access_ok(addr, len) \
37 (((signed long)(((long)get_fs().seg) & \
38 ((long)(addr) | (((long)(addr)) + (len)) | (len)))) == 0)
40 #define access_ok(type, addr, len) \
41 likely(__access_ok((unsigned long)(addr), (unsigned long)(len)))
43 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
45 #define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE)
51 static inline unsigned long __must_check
__clear_user(void __user
*to
,
54 __asm__
__volatile__ (
55 "1: stb zero, 0(%1)\n"
70 static inline unsigned long __must_check
clear_user(void __user
*to
,
73 if (!access_ok(VERIFY_WRITE
, to
, n
))
75 return __clear_user(to
, n
);
79 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
81 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
82 #define INLINE_COPY_FROM_USER
83 #define INLINE_COPY_TO_USER
85 extern long strncpy_from_user(char *__to
, const char __user
*__from
,
87 extern __must_check
long strlen_user(const char __user
*str
);
88 extern __must_check
long strnlen_user(const char __user
*s
, long n
);
90 /* Optimized macros */
91 #define __get_user_asm(val, insn, addr, err) \
93 __asm__ __volatile__( \
95 "1: " insn " %1, 0(%2)\n" \
98 " .section __ex_table,\"a\"\n" \
101 : "=&r" (err), "=r" (val) \
102 : "r" (addr), "i" (-EFAULT)); \
105 #define __get_user_unknown(val, size, ptr, err) do { \
107 if (__copy_from_user(&(val), ptr, size)) { \
112 #define __get_user_common(val, size, ptr, err) \
116 __get_user_asm(val, "ldbu", ptr, err); \
119 __get_user_asm(val, "ldhu", ptr, err); \
122 __get_user_asm(val, "ldw", ptr, err); \
125 __get_user_unknown(val, size, ptr, err); \
130 #define __get_user(x, ptr) \
132 long __gu_err = -EFAULT; \
133 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
134 unsigned long __gu_val = 0; \
135 __get_user_common(__gu_val, sizeof(*(ptr)), __gu_ptr, __gu_err);\
136 (x) = (__force __typeof__(x))__gu_val; \
140 #define get_user(x, ptr) \
142 long __gu_err = -EFAULT; \
143 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
144 unsigned long __gu_val = 0; \
145 if (access_ok(VERIFY_READ, __gu_ptr, sizeof(*__gu_ptr))) \
146 __get_user_common(__gu_val, sizeof(*__gu_ptr), \
147 __gu_ptr, __gu_err); \
148 (x) = (__force __typeof__(x))__gu_val; \
152 #define __put_user_asm(val, insn, ptr, err) \
154 __asm__ __volatile__( \
156 "1: " insn " %1, 0(%2)\n" \
159 " .section __ex_table,\"a\"\n" \
163 : "r" (val), "r" (ptr), "i" (-EFAULT)); \
166 #define put_user(x, ptr) \
168 long __pu_err = -EFAULT; \
169 __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \
170 __typeof__(*(ptr)) __pu_val = (__typeof(*ptr))(x); \
171 if (access_ok(VERIFY_WRITE, __pu_ptr, sizeof(*__pu_ptr))) { \
172 switch (sizeof(*__pu_ptr)) { \
174 __put_user_asm(__pu_val, "stb", __pu_ptr, __pu_err); \
177 __put_user_asm(__pu_val, "sth", __pu_ptr, __pu_err); \
180 __put_user_asm(__pu_val, "stw", __pu_ptr, __pu_err); \
183 /* XXX: This looks wrong... */ \
185 if (copy_to_user(__pu_ptr, &(__pu_val), \
186 sizeof(*__pu_ptr))) \
187 __pu_err = -EFAULT; \
194 #define __put_user(x, ptr) put_user(x, ptr)
196 #endif /* _ASM_NIOS2_UACCESS_H */