1367b8112SChris Zankel /* 2367b8112SChris Zankel * include/asm-xtensa/uaccess.h 3367b8112SChris Zankel * 4367b8112SChris Zankel * User space memory access functions 5367b8112SChris Zankel * 6367b8112SChris Zankel * These routines provide basic accessing functions to the user memory 7eef35c2dSStefan Weil * space for the kernel. This header file provides functions such as: 8367b8112SChris Zankel * 9367b8112SChris Zankel * This file is subject to the terms and conditions of the GNU General Public 10367b8112SChris Zankel * License. See the file "COPYING" in the main directory of this archive 11367b8112SChris Zankel * for more details. 12367b8112SChris Zankel * 13367b8112SChris Zankel * Copyright (C) 2001 - 2005 Tensilica Inc. 14367b8112SChris Zankel */ 15367b8112SChris Zankel 16367b8112SChris Zankel #ifndef _XTENSA_UACCESS_H 17367b8112SChris Zankel #define _XTENSA_UACCESS_H 18367b8112SChris Zankel 1990b03f50SWANG Cong #include <linux/prefetch.h> 20e44ba033SVitaliy Ivanov #include <asm/types.h> 210b46a94eSAl Viro #include <asm/extable.h> 22367b8112SChris Zankel 23367b8112SChris Zankel /* 24367b8112SChris Zankel * The fs value determines whether argument validity checking should 25367b8112SChris Zankel * be performed or not. If get_fs() == USER_DS, checking is 26367b8112SChris Zankel * performed, with get_fs() == KERNEL_DS, checking is bypassed. 27367b8112SChris Zankel * 28367b8112SChris Zankel * For historical reasons (Data Segment Register?), these macros are 29367b8112SChris Zankel * grossly misnamed. 30367b8112SChris Zankel */ 31367b8112SChris Zankel 32367b8112SChris Zankel #define KERNEL_DS ((mm_segment_t) { 0 }) 33367b8112SChris Zankel #define USER_DS ((mm_segment_t) { 1 }) 34367b8112SChris Zankel 35367b8112SChris Zankel #define get_fs() (current->thread.current_ds) 36367b8112SChris Zankel #define set_fs(val) (current->thread.current_ds = (val)) 37367b8112SChris Zankel 38*12700c17SArnd Bergmann #include <asm-generic/access_ok.h> 3957358ba9SMax Filippov 40367b8112SChris Zankel /* 41367b8112SChris Zankel * These are the main single-value transfer routines. They 42367b8112SChris Zankel * automatically use the right size if we just have the right pointer 43367b8112SChris Zankel * type. 44367b8112SChris Zankel * 45367b8112SChris Zankel * This gets kind of ugly. We want to return _two_ values in 46367b8112SChris Zankel * "get_user()" and yet we don't want to do any pointers, because that 47367b8112SChris Zankel * is too much of a performance impact. Thus we have a few rather ugly 48367b8112SChris Zankel * macros here, and hide all the uglyness from the user. 49367b8112SChris Zankel * 50367b8112SChris Zankel * Careful to not 51367b8112SChris Zankel * (a) re-use the arguments for side effects (sizeof is ok) 52367b8112SChris Zankel * (b) require any knowledge of processes at this stage 53367b8112SChris Zankel */ 54367b8112SChris Zankel #define put_user(x, ptr) __put_user_check((x), (ptr), sizeof(*(ptr))) 55367b8112SChris Zankel #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr))) 56367b8112SChris Zankel 57367b8112SChris Zankel /* 58367b8112SChris Zankel * The "__xxx" versions of the user access functions are versions that 59367b8112SChris Zankel * do not verify the address space, that must have been done previously 60367b8112SChris Zankel * with a separate "access_ok()" call (this is used when we do multiple 61367b8112SChris Zankel * accesses to the same area of user memory). 62367b8112SChris Zankel */ 63367b8112SChris Zankel #define __put_user(x, ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr))) 64367b8112SChris Zankel #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr))) 65367b8112SChris Zankel 66367b8112SChris Zankel 67367b8112SChris Zankel extern long __put_user_bad(void); 68367b8112SChris Zankel 69367b8112SChris Zankel #define __put_user_nocheck(x, ptr, size) \ 70367b8112SChris Zankel ({ \ 71367b8112SChris Zankel long __pu_err; \ 72367b8112SChris Zankel __put_user_size((x), (ptr), (size), __pu_err); \ 73367b8112SChris Zankel __pu_err; \ 74367b8112SChris Zankel }) 75367b8112SChris Zankel 76367b8112SChris Zankel #define __put_user_check(x, ptr, size) \ 77367b8112SChris Zankel ({ \ 78367b8112SChris Zankel long __pu_err = -EFAULT; \ 793ac4a615SMax Filippov __typeof__(*(ptr)) __user *__pu_addr = (ptr); \ 8096d4f267SLinus Torvalds if (access_ok(__pu_addr, size)) \ 81367b8112SChris Zankel __put_user_size((x), __pu_addr, (size), __pu_err); \ 82367b8112SChris Zankel __pu_err; \ 83367b8112SChris Zankel }) 84367b8112SChris Zankel 85367b8112SChris Zankel #define __put_user_size(x, ptr, size, retval) \ 86367b8112SChris Zankel do { \ 87367b8112SChris Zankel int __cb; \ 88367b8112SChris Zankel retval = 0; \ 89367b8112SChris Zankel switch (size) { \ 90367b8112SChris Zankel case 1: __put_user_asm(x, ptr, retval, 1, "s8i", __cb); break; \ 91367b8112SChris Zankel case 2: __put_user_asm(x, ptr, retval, 2, "s16i", __cb); break; \ 92367b8112SChris Zankel case 4: __put_user_asm(x, ptr, retval, 4, "s32i", __cb); break; \ 93367b8112SChris Zankel case 8: { \ 94367b8112SChris Zankel __typeof__(*ptr) __v64 = x; \ 956595d144SAl Viro retval = __copy_to_user(ptr, &__v64, 8) ? -EFAULT : 0; \ 96367b8112SChris Zankel break; \ 97367b8112SChris Zankel } \ 98367b8112SChris Zankel default: __put_user_bad(); \ 99367b8112SChris Zankel } \ 100367b8112SChris Zankel } while (0) 101367b8112SChris Zankel 102367b8112SChris Zankel 103367b8112SChris Zankel /* 104367b8112SChris Zankel * Consider a case of a user single load/store would cause both an 105367b8112SChris Zankel * unaligned exception and an MMU-related exception (unaligned 106367b8112SChris Zankel * exceptions happen first): 107367b8112SChris Zankel * 108367b8112SChris Zankel * User code passes a bad variable ptr to a system call. 109367b8112SChris Zankel * Kernel tries to access the variable. 110367b8112SChris Zankel * Unaligned exception occurs. 111367b8112SChris Zankel * Unaligned exception handler tries to make aligned accesses. 112367b8112SChris Zankel * Double exception occurs for MMU-related cause (e.g., page not mapped). 113367b8112SChris Zankel * do_page_fault() thinks the fault address belongs to the kernel, not the 114367b8112SChris Zankel * user, and panics. 115367b8112SChris Zankel * 116367b8112SChris Zankel * The kernel currently prohibits user unaligned accesses. We use the 117367b8112SChris Zankel * __check_align_* macros to check for unaligned addresses before 118367b8112SChris Zankel * accessing user space so we don't crash the kernel. Both 119367b8112SChris Zankel * __put_user_asm and __get_user_asm use these alignment macros, so 120367b8112SChris Zankel * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in 121367b8112SChris Zankel * sync. 122367b8112SChris Zankel */ 123367b8112SChris Zankel 124367b8112SChris Zankel #define __check_align_1 "" 125367b8112SChris Zankel 126367b8112SChris Zankel #define __check_align_2 \ 127cbc6e287SMax Filippov " _bbci.l %[mem] * 0, 1f \n" \ 128c0437642SMax Filippov " movi %[err], %[efault] \n" \ 129367b8112SChris Zankel " _j 2f \n" 130367b8112SChris Zankel 131367b8112SChris Zankel #define __check_align_4 \ 132cbc6e287SMax Filippov " _bbsi.l %[mem] * 0, 0f \n" \ 133cbc6e287SMax Filippov " _bbci.l %[mem] * 0 + 1, 1f \n" \ 134c0437642SMax Filippov "0: movi %[err], %[efault] \n" \ 135367b8112SChris Zankel " _j 2f \n" 136367b8112SChris Zankel 137367b8112SChris Zankel 138367b8112SChris Zankel /* 139367b8112SChris Zankel * We don't tell gcc that we are accessing memory, but this is OK 140367b8112SChris Zankel * because we do not write to any memory gcc knows about, so there 141367b8112SChris Zankel * are no aliasing issues. 142367b8112SChris Zankel * 143367b8112SChris Zankel * WARNING: If you modify this macro at all, verify that the 144367b8112SChris Zankel * __check_align_* macros still work. 145367b8112SChris Zankel */ 146c0437642SMax Filippov #define __put_user_asm(x_, addr_, err_, align, insn, cb)\ 147367b8112SChris Zankel __asm__ __volatile__( \ 148367b8112SChris Zankel __check_align_##align \ 149cbc6e287SMax Filippov "1: "insn" %[x], %[mem] \n" \ 150367b8112SChris Zankel "2: \n" \ 151367b8112SChris Zankel " .section .fixup,\"ax\" \n" \ 152367b8112SChris Zankel " .align 4 \n" \ 15303760270SMax Filippov " .literal_position \n" \ 154367b8112SChris Zankel "5: \n" \ 155c0437642SMax Filippov " movi %[tmp], 2b \n" \ 156c0437642SMax Filippov " movi %[err], %[efault] \n" \ 157c0437642SMax Filippov " jx %[tmp] \n" \ 158367b8112SChris Zankel " .previous \n" \ 159367b8112SChris Zankel " .section __ex_table,\"a\" \n" \ 160367b8112SChris Zankel " .long 1b, 5b \n" \ 161367b8112SChris Zankel " .previous" \ 162cbc6e287SMax Filippov :[err] "+r"(err_), [tmp] "=r"(cb), [mem] "=m"(*(addr_)) \ 163cbc6e287SMax Filippov :[x] "r"(x_), [efault] "i"(-EFAULT)) 164367b8112SChris Zankel 165367b8112SChris Zankel #define __get_user_nocheck(x, ptr, size) \ 166367b8112SChris Zankel ({ \ 167c9c63f3cSMax Filippov long __gu_err; \ 168c9c63f3cSMax Filippov __get_user_size((x), (ptr), (size), __gu_err); \ 169367b8112SChris Zankel __gu_err; \ 170367b8112SChris Zankel }) 171367b8112SChris Zankel 172367b8112SChris Zankel #define __get_user_check(x, ptr, size) \ 173367b8112SChris Zankel ({ \ 174c9c63f3cSMax Filippov long __gu_err = -EFAULT; \ 1753ac4a615SMax Filippov const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \ 17696d4f267SLinus Torvalds if (access_ok(__gu_addr, size)) \ 177c9c63f3cSMax Filippov __get_user_size((x), __gu_addr, (size), __gu_err); \ 178c9c63f3cSMax Filippov else \ 1799afcc71bSMax Filippov (x) = (__typeof__(*(ptr)))0; \ 180367b8112SChris Zankel __gu_err; \ 181367b8112SChris Zankel }) 182367b8112SChris Zankel 183367b8112SChris Zankel extern long __get_user_bad(void); 184367b8112SChris Zankel 185367b8112SChris Zankel #define __get_user_size(x, ptr, size, retval) \ 186367b8112SChris Zankel do { \ 187367b8112SChris Zankel int __cb; \ 188367b8112SChris Zankel retval = 0; \ 189367b8112SChris Zankel switch (size) { \ 190367b8112SChris Zankel case 1: __get_user_asm(x, ptr, retval, 1, "l8ui", __cb); break;\ 191367b8112SChris Zankel case 2: __get_user_asm(x, ptr, retval, 2, "l16ui", __cb); break;\ 192367b8112SChris Zankel case 4: __get_user_asm(x, ptr, retval, 4, "l32i", __cb); break;\ 1936595d144SAl Viro case 8: { \ 1946595d144SAl Viro u64 __x; \ 1956595d144SAl Viro if (unlikely(__copy_from_user(&__x, ptr, 8))) { \ 1966595d144SAl Viro retval = -EFAULT; \ 1979afcc71bSMax Filippov (x) = (__typeof__(*(ptr)))0; \ 1986595d144SAl Viro } else { \ 199c22f9075SMax Filippov (x) = *(__force __typeof__(*(ptr)) *)&__x; \ 2006595d144SAl Viro } \ 2016595d144SAl Viro break; \ 2026595d144SAl Viro } \ 2039afcc71bSMax Filippov default: \ 2049afcc71bSMax Filippov (x) = (__typeof__(*(ptr)))0; \ 2059afcc71bSMax Filippov __get_user_bad(); \ 206367b8112SChris Zankel } \ 207367b8112SChris Zankel } while (0) 208367b8112SChris Zankel 209367b8112SChris Zankel 210367b8112SChris Zankel /* 211367b8112SChris Zankel * WARNING: If you modify this macro at all, verify that the 212367b8112SChris Zankel * __check_align_* macros still work. 213367b8112SChris Zankel */ 214c0437642SMax Filippov #define __get_user_asm(x_, addr_, err_, align, insn, cb) \ 215c9c63f3cSMax Filippov do { \ 216c9c63f3cSMax Filippov u32 __x = 0; \ 217367b8112SChris Zankel __asm__ __volatile__( \ 218367b8112SChris Zankel __check_align_##align \ 219cbc6e287SMax Filippov "1: "insn" %[x], %[mem] \n" \ 220367b8112SChris Zankel "2: \n" \ 221367b8112SChris Zankel " .section .fixup,\"ax\" \n" \ 222367b8112SChris Zankel " .align 4 \n" \ 22303760270SMax Filippov " .literal_position \n" \ 224367b8112SChris Zankel "5: \n" \ 225c0437642SMax Filippov " movi %[tmp], 2b \n" \ 226c0437642SMax Filippov " movi %[err], %[efault] \n" \ 227c0437642SMax Filippov " jx %[tmp] \n" \ 228367b8112SChris Zankel " .previous \n" \ 229367b8112SChris Zankel " .section __ex_table,\"a\" \n" \ 230367b8112SChris Zankel " .long 1b, 5b \n" \ 231367b8112SChris Zankel " .previous" \ 232c9c63f3cSMax Filippov :[err] "+r"(err_), [tmp] "=r"(cb), [x] "+r"(__x) \ 233cbc6e287SMax Filippov :[mem] "m"(*(addr_)), [efault] "i"(-EFAULT)); \ 234c9c63f3cSMax Filippov (x_) = (__force __typeof__(*(addr_)))__x; \ 235c9c63f3cSMax Filippov } while (0) 236367b8112SChris Zankel 237367b8112SChris Zankel 238367b8112SChris Zankel /* 239367b8112SChris Zankel * Copy to/from user space 240367b8112SChris Zankel */ 241367b8112SChris Zankel 242367b8112SChris Zankel extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n); 243367b8112SChris Zankel 244367b8112SChris Zankel static inline unsigned long 2453a0e75adSAl Viro raw_copy_from_user(void *to, const void __user *from, unsigned long n) 246367b8112SChris Zankel { 247367b8112SChris Zankel prefetchw(to); 2483a0e75adSAl Viro return __xtensa_copy_user(to, (__force const void *)from, n); 249367b8112SChris Zankel } 2503a0e75adSAl Viro static inline unsigned long 2513a0e75adSAl Viro raw_copy_to_user(void __user *to, const void *from, unsigned long n) 2523a0e75adSAl Viro { 2537d4914dbSMax Filippov prefetch(from); 2543a0e75adSAl Viro return __xtensa_copy_user((__force void *)to, from, n); 2553a0e75adSAl Viro } 2563a0e75adSAl Viro #define INLINE_COPY_FROM_USER 2573a0e75adSAl Viro #define INLINE_COPY_TO_USER 258367b8112SChris Zankel 259367b8112SChris Zankel /* 260367b8112SChris Zankel * We need to return the number of bytes not cleared. Our memset() 261367b8112SChris Zankel * returns zero if a problem occurs while accessing user-space memory. 262367b8112SChris Zankel * In that event, return no memory cleared. Otherwise, zero for 263367b8112SChris Zankel * success. 264367b8112SChris Zankel */ 265367b8112SChris Zankel 266367b8112SChris Zankel static inline unsigned long 2672adf5352SMax Filippov __xtensa_clear_user(void __user *addr, unsigned long size) 268367b8112SChris Zankel { 2692adf5352SMax Filippov if (!__memset((void __force *)addr, 0, size)) 270367b8112SChris Zankel return size; 271367b8112SChris Zankel return 0; 272367b8112SChris Zankel } 273367b8112SChris Zankel 274367b8112SChris Zankel static inline unsigned long 2752adf5352SMax Filippov clear_user(void __user *addr, unsigned long size) 276367b8112SChris Zankel { 27796d4f267SLinus Torvalds if (access_ok(addr, size)) 278367b8112SChris Zankel return __xtensa_clear_user(addr, size); 279367b8112SChris Zankel return size ? -EFAULT : 0; 280367b8112SChris Zankel } 281367b8112SChris Zankel 282367b8112SChris Zankel #define __clear_user __xtensa_clear_user 283367b8112SChris Zankel 284367b8112SChris Zankel 285e6226997SArnd Bergmann #ifdef CONFIG_ARCH_HAS_STRNCPY_FROM_USER 2862adf5352SMax Filippov extern long __strncpy_user(char *dst, const char __user *src, long count); 287367b8112SChris Zankel 288367b8112SChris Zankel static inline long 2892adf5352SMax Filippov strncpy_from_user(char *dst, const char __user *src, long count) 290367b8112SChris Zankel { 29196d4f267SLinus Torvalds if (access_ok(src, 1)) 29210503bf9SAl Viro return __strncpy_user(dst, src, count); 293367b8112SChris Zankel return -EFAULT; 294367b8112SChris Zankel } 29557358ba9SMax Filippov #else 296dc293f21SLaurent Pinchart long strncpy_from_user(char *dst, const char __user *src, long count); 29757358ba9SMax Filippov #endif 298367b8112SChris Zankel 299367b8112SChris Zankel /* 300367b8112SChris Zankel * Return the size of a string (including the ending 0!) 301367b8112SChris Zankel */ 3022adf5352SMax Filippov extern long __strnlen_user(const char __user *str, long len); 303367b8112SChris Zankel 3042adf5352SMax Filippov static inline long strnlen_user(const char __user *str, long len) 305367b8112SChris Zankel { 3062adf5352SMax Filippov if (!access_ok(str, 1)) 307367b8112SChris Zankel return 0; 308367b8112SChris Zankel return __strnlen_user(str, len); 309367b8112SChris Zankel } 310367b8112SChris Zankel 311367b8112SChris Zankel #endif /* _XTENSA_UACCESS_H */ 312