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