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>
21 #include <asm-generic/access_ok.h>
23 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
29 static inline unsigned long __must_check
__clear_user(void __user
*to
,
32 __asm__
__volatile__ (
33 "1: stb zero, 0(%1)\n"
48 static inline unsigned long __must_check
clear_user(void __user
*to
,
51 if (!access_ok(to
, n
))
53 return __clear_user(to
, n
);
57 raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
);
59 raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
);
60 #define INLINE_COPY_FROM_USER
61 #define INLINE_COPY_TO_USER
63 extern long strncpy_from_user(char *__to
, const char __user
*__from
,
65 extern __must_check
long strnlen_user(const char __user
*s
, long n
);
67 /* Optimized macros */
68 #define __get_user_asm(val, insn, addr, err) \
70 unsigned long __gu_val; \
71 __asm__ __volatile__( \
73 "1: " insn " %1, 0(%2)\n" \
76 " .section __ex_table,\"a\"\n" \
79 : "=&r" (err), "=r" (__gu_val) \
80 : "r" (addr), "i" (-EFAULT)); \
81 val = (__force __typeof__(*(addr)))__gu_val; \
84 extern void __get_user_unknown(void);
86 #define __get_user_8(val, ptr, err) do { \
89 if (raw_copy_from_user(&(__val), ptr, sizeof(val))) { \
92 val = (typeof(val))(typeof((val) - (val)))__val; \
96 #define __get_user_common(val, size, ptr, err) \
100 __get_user_asm(val, "ldbu", ptr, err); \
103 __get_user_asm(val, "ldhu", ptr, err); \
106 __get_user_asm(val, "ldw", ptr, err); \
109 __get_user_8(val, ptr, err); \
112 __get_user_unknown(); \
117 #define __get_user(x, ptr) \
119 long __gu_err = -EFAULT; \
120 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
121 __get_user_common(x, sizeof(*(ptr)), __gu_ptr, __gu_err); \
125 #define get_user(x, ptr) \
127 long __gu_err = -EFAULT; \
128 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
129 if (access_ok( __gu_ptr, sizeof(*__gu_ptr))) \
130 __get_user_common(x, sizeof(*__gu_ptr), \
131 __gu_ptr, __gu_err); \
135 #define __put_user_asm(val, insn, ptr, err) \
137 __asm__ __volatile__( \
139 "1: " insn " %1, 0(%2)\n" \
142 " .section __ex_table,\"a\"\n" \
146 : "r" (val), "r" (ptr), "i" (-EFAULT)); \
149 #define __put_user_common(__pu_val, __pu_ptr) \
151 long __pu_err = -EFAULT; \
152 switch (sizeof(*__pu_ptr)) { \
154 __put_user_asm(__pu_val, "stb", __pu_ptr, __pu_err); \
157 __put_user_asm(__pu_val, "sth", __pu_ptr, __pu_err); \
160 __put_user_asm(__pu_val, "stw", __pu_ptr, __pu_err); \
163 /* XXX: This looks wrong... */ \
165 if (__copy_to_user(__pu_ptr, &(__pu_val), \
166 sizeof(*__pu_ptr))) \
167 __pu_err = -EFAULT; \
173 #define __put_user(x, ptr) \
175 __auto_type __pu_ptr = (ptr); \
176 typeof(*__pu_ptr) __pu_val = (typeof(*__pu_ptr))(x); \
177 __put_user_common(__pu_val, __pu_ptr); \
180 #define put_user(x, ptr) \
182 __auto_type __pu_ptr = (ptr); \
183 typeof(*__pu_ptr) __pu_val = (typeof(*__pu_ptr))(x); \
184 access_ok(__pu_ptr, sizeof(*__pu_ptr)) ? \
185 __put_user_common(__pu_val, __pu_ptr) : \
189 #endif /* _ASM_NIOS2_UACCESS_H */