1 /* 2 * User space memory access functions for Nios II 3 * 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 7 * 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 10 * for more details. 11 */ 12 13 #ifndef _ASM_NIOS2_UACCESS_H 14 #define _ASM_NIOS2_UACCESS_H 15 16 #include <linux/string.h> 17 18 #include <asm/page.h> 19 20 #include <asm/extable.h> 21 22 /* 23 * Segment stuff 24 */ 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) 28 29 30 #define get_fs() (current_thread_info()->addr_limit) 31 #define set_fs(seg) (current_thread_info()->addr_limit = (seg)) 32 33 #define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg) 34 35 #define __access_ok(addr, len) \ 36 (((signed long)(((long)get_fs().seg) & \ 37 ((long)(addr) | (((long)(addr)) + (len)) | (len)))) == 0) 38 39 #define access_ok(addr, len) \ 40 likely(__access_ok((unsigned long)(addr), (unsigned long)(len))) 41 42 # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n" 43 44 #define user_addr_max() (uaccess_kernel() ? ~0UL : TASK_SIZE) 45 46 /* 47 * Zero Userspace 48 */ 49 50 static inline unsigned long __must_check __clear_user(void __user *to, 51 unsigned long n) 52 { 53 __asm__ __volatile__ ( 54 "1: stb zero, 0(%1)\n" 55 " addi %0, %0, -1\n" 56 " addi %1, %1, 1\n" 57 " bne %0, zero, 1b\n" 58 "2:\n" 59 __EX_TABLE_SECTION 60 ".word 1b, 2b\n" 61 ".previous\n" 62 : "=r" (n), "=r" (to) 63 : "0" (n), "1" (to) 64 ); 65 66 return n; 67 } 68 69 static inline unsigned long __must_check clear_user(void __user *to, 70 unsigned long n) 71 { 72 if (!access_ok(to, n)) 73 return n; 74 return __clear_user(to, n); 75 } 76 77 extern unsigned long 78 raw_copy_from_user(void *to, const void __user *from, unsigned long n); 79 extern unsigned long 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 83 84 extern long strncpy_from_user(char *__to, const char __user *__from, 85 long __len); 86 extern __must_check long strnlen_user(const char __user *s, long n); 87 88 /* Optimized macros */ 89 #define __get_user_asm(val, insn, addr, err) \ 90 { \ 91 __asm__ __volatile__( \ 92 " movi %0, %3\n" \ 93 "1: " insn " %1, 0(%2)\n" \ 94 " movi %0, 0\n" \ 95 "2:\n" \ 96 " .section __ex_table,\"a\"\n" \ 97 " .word 1b, 2b\n" \ 98 " .previous" \ 99 : "=&r" (err), "=r" (val) \ 100 : "r" (addr), "i" (-EFAULT)); \ 101 } 102 103 #define __get_user_unknown(val, size, ptr, err) do { \ 104 err = 0; \ 105 if (__copy_from_user(&(val), ptr, size)) { \ 106 err = -EFAULT; \ 107 } \ 108 } while (0) 109 110 #define __get_user_common(val, size, ptr, err) \ 111 do { \ 112 switch (size) { \ 113 case 1: \ 114 __get_user_asm(val, "ldbu", ptr, err); \ 115 break; \ 116 case 2: \ 117 __get_user_asm(val, "ldhu", ptr, err); \ 118 break; \ 119 case 4: \ 120 __get_user_asm(val, "ldw", ptr, err); \ 121 break; \ 122 default: \ 123 __get_user_unknown(val, size, ptr, err); \ 124 break; \ 125 } \ 126 } while (0) 127 128 #define __get_user(x, ptr) \ 129 ({ \ 130 long __gu_err = -EFAULT; \ 131 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ 132 unsigned long __gu_val = 0; \ 133 __get_user_common(__gu_val, sizeof(*(ptr)), __gu_ptr, __gu_err);\ 134 (x) = (__force __typeof__(x))__gu_val; \ 135 __gu_err; \ 136 }) 137 138 #define get_user(x, ptr) \ 139 ({ \ 140 long __gu_err = -EFAULT; \ 141 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \ 142 unsigned long __gu_val = 0; \ 143 if (access_ok( __gu_ptr, sizeof(*__gu_ptr))) \ 144 __get_user_common(__gu_val, sizeof(*__gu_ptr), \ 145 __gu_ptr, __gu_err); \ 146 (x) = (__force __typeof__(x))__gu_val; \ 147 __gu_err; \ 148 }) 149 150 #define __put_user_asm(val, insn, ptr, err) \ 151 { \ 152 __asm__ __volatile__( \ 153 " movi %0, %3\n" \ 154 "1: " insn " %1, 0(%2)\n" \ 155 " movi %0, 0\n" \ 156 "2:\n" \ 157 " .section __ex_table,\"a\"\n" \ 158 " .word 1b, 2b\n" \ 159 " .previous\n" \ 160 : "=&r" (err) \ 161 : "r" (val), "r" (ptr), "i" (-EFAULT)); \ 162 } 163 164 #define put_user(x, ptr) \ 165 ({ \ 166 long __pu_err = -EFAULT; \ 167 __typeof__(*(ptr)) __user *__pu_ptr = (ptr); \ 168 __typeof__(*(ptr)) __pu_val = (__typeof(*ptr))(x); \ 169 if (access_ok(__pu_ptr, sizeof(*__pu_ptr))) { \ 170 switch (sizeof(*__pu_ptr)) { \ 171 case 1: \ 172 __put_user_asm(__pu_val, "stb", __pu_ptr, __pu_err); \ 173 break; \ 174 case 2: \ 175 __put_user_asm(__pu_val, "sth", __pu_ptr, __pu_err); \ 176 break; \ 177 case 4: \ 178 __put_user_asm(__pu_val, "stw", __pu_ptr, __pu_err); \ 179 break; \ 180 default: \ 181 /* XXX: This looks wrong... */ \ 182 __pu_err = 0; \ 183 if (copy_to_user(__pu_ptr, &(__pu_val), \ 184 sizeof(*__pu_ptr))) \ 185 __pu_err = -EFAULT; \ 186 break; \ 187 } \ 188 } \ 189 __pu_err; \ 190 }) 191 192 #define __put_user(x, ptr) put_user(x, ptr) 193 194 #endif /* _ASM_NIOS2_UACCESS_H */ 195