1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */ 2deae26bfSKyle McMartin #ifndef __PARISC_UACCESS_H 3deae26bfSKyle McMartin #define __PARISC_UACCESS_H 4deae26bfSKyle McMartin 5deae26bfSKyle McMartin /* 6deae26bfSKyle McMartin * User space memory access functions 7deae26bfSKyle McMartin */ 8deae26bfSKyle McMartin #include <asm/page.h> 9deae26bfSKyle McMartin #include <asm/cache.h> 10deae26bfSKyle McMartin 118dd95c68SHelge Deller #include <linux/bug.h> 12aace880fSAl Viro #include <linux/string.h> 13deae26bfSKyle McMartin 14deae26bfSKyle McMartin #define KERNEL_DS ((mm_segment_t){0}) 15deae26bfSKyle McMartin #define USER_DS ((mm_segment_t){1}) 16deae26bfSKyle McMartin 17428e2976SChristoph Hellwig #define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg) 18deae26bfSKyle McMartin 19deae26bfSKyle McMartin #define get_fs() (current_thread_info()->addr_limit) 20deae26bfSKyle McMartin #define set_fs(x) (current_thread_info()->addr_limit = (x)) 21deae26bfSKyle McMartin 22deae26bfSKyle McMartin /* 23deae26bfSKyle McMartin * Note that since kernel addresses are in a separate address space on 24deae26bfSKyle McMartin * parisc, we don't need to do anything for access_ok(). 25deae26bfSKyle McMartin * We just let the page fault handler do the right thing. This also means 26deae26bfSKyle McMartin * that put_user is the same as __put_user, etc. 27deae26bfSKyle McMartin */ 28deae26bfSKyle McMartin 2996d4f267SLinus Torvalds #define access_ok(uaddr, size) \ 30186ecf14SHelge Deller ( (uaddr) == (uaddr) ) 31deae26bfSKyle McMartin 32deae26bfSKyle McMartin #define put_user __put_user 33deae26bfSKyle McMartin #define get_user __get_user 34deae26bfSKyle McMartin 35deae26bfSKyle McMartin #if !defined(CONFIG_64BIT) 363f795cefSHelge Deller #define LDD_USER(val, ptr) __get_user_asm64(val, ptr) 37deae26bfSKyle McMartin #define STD_USER(x, ptr) __put_user_asm64(x, ptr) 38deae26bfSKyle McMartin #else 393f795cefSHelge Deller #define LDD_USER(val, ptr) __get_user_asm(val, "ldd", ptr) 40deae26bfSKyle McMartin #define STD_USER(x, ptr) __put_user_asm("std", x, ptr) 41deae26bfSKyle McMartin #endif 42deae26bfSKyle McMartin 43deae26bfSKyle McMartin /* 44cb910c17SHelge Deller * The exception table contains two values: the first is the relative offset to 45cb910c17SHelge Deller * the address of the instruction that is allowed to fault, and the second is 46cb910c17SHelge Deller * the relative offset to the address of the fixup routine. Since relative 47cb910c17SHelge Deller * addresses are used, 32bit values are sufficient even on 64bit kernel. 48deae26bfSKyle McMartin */ 49deae26bfSKyle McMartin 500de79858SHelge Deller #define ARCH_HAS_RELATIVE_EXTABLE 51deae26bfSKyle McMartin struct exception_table_entry { 520de79858SHelge Deller int insn; /* relative address of insn that is allowed to fault. */ 530de79858SHelge Deller int fixup; /* relative address of fixup routine */ 54deae26bfSKyle McMartin }; 55deae26bfSKyle McMartin 56deae26bfSKyle McMartin #define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\ 57deae26bfSKyle McMartin ".section __ex_table,\"aw\"\n" \ 580de79858SHelge Deller ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \ 59deae26bfSKyle McMartin ".previous\n" 60deae26bfSKyle McMartin 61deae26bfSKyle McMartin /* 62d19f5e41SHelge Deller * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry 63d19f5e41SHelge Deller * (with lowest bit set) for which the fault handler in fixup_exception() will 64d19f5e41SHelge Deller * load -EFAULT into %r8 for a read or write fault, and zeroes the target 65d19f5e41SHelge Deller * register in case of a read fault in get_user(). 66d19f5e41SHelge Deller */ 67d19f5e41SHelge Deller #define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\ 68d19f5e41SHelge Deller ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1) 69d19f5e41SHelge Deller 70d19f5e41SHelge Deller /* 7106bff6b9SHelge Deller * load_sr2() preloads the space register %%sr2 - based on the value of 7206bff6b9SHelge Deller * get_fs() - with either a value of 0 to access kernel space (KERNEL_DS which 7306bff6b9SHelge Deller * is 0), or with the current value of %%sr3 to access user space (USER_DS) 7406bff6b9SHelge Deller * memory. The following __get_user_asm() and __put_user_asm() functions have 7506bff6b9SHelge Deller * %%sr2 hard-coded to access the requested memory. 7606bff6b9SHelge Deller */ 7706bff6b9SHelge Deller #define load_sr2() \ 7806bff6b9SHelge Deller __asm__(" or,= %0,%%r0,%%r0\n\t" \ 7906bff6b9SHelge Deller " mfsp %%sr3,%0\n\t" \ 8006bff6b9SHelge Deller " mtsp %0,%%sr2\n\t" \ 8106bff6b9SHelge Deller : : "r"(get_fs()) : ) 8206bff6b9SHelge Deller 833f795cefSHelge Deller #define __get_user_internal(val, ptr) \ 84deae26bfSKyle McMartin ({ \ 85deae26bfSKyle McMartin register long __gu_err __asm__ ("r8") = 0; \ 86deae26bfSKyle McMartin \ 87deae26bfSKyle McMartin switch (sizeof(*(ptr))) { \ 883f795cefSHelge Deller case 1: __get_user_asm(val, "ldb", ptr); break; \ 893f795cefSHelge Deller case 2: __get_user_asm(val, "ldh", ptr); break; \ 903f795cefSHelge Deller case 4: __get_user_asm(val, "ldw", ptr); break; \ 913f795cefSHelge Deller case 8: LDD_USER(val, ptr); break; \ 923f795cefSHelge Deller default: BUILD_BUG(); \ 93deae26bfSKyle McMartin } \ 94deae26bfSKyle McMartin \ 95deae26bfSKyle McMartin __gu_err; \ 96deae26bfSKyle McMartin }) 97deae26bfSKyle McMartin 983f795cefSHelge Deller #define __get_user(val, ptr) \ 993f795cefSHelge Deller ({ \ 1003f795cefSHelge Deller load_sr2(); \ 1013f795cefSHelge Deller __get_user_internal(val, ptr); \ 1023f795cefSHelge Deller }) 1033f795cefSHelge Deller 1043f795cefSHelge Deller #define __get_user_asm(val, ldx, ptr) \ 1053f795cefSHelge Deller { \ 1063f795cefSHelge Deller register long __gu_val; \ 1073f795cefSHelge Deller \ 108d19f5e41SHelge Deller __asm__("1: " ldx " 0(%%sr2,%2),%0\n" \ 109d19f5e41SHelge Deller "9:\n" \ 110d19f5e41SHelge Deller ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \ 111deae26bfSKyle McMartin : "=r"(__gu_val), "=r"(__gu_err) \ 1123f795cefSHelge Deller : "r"(ptr), "1"(__gu_err)); \ 1133f795cefSHelge Deller \ 1143f795cefSHelge Deller (val) = (__force __typeof__(*(ptr))) __gu_val; \ 1153f795cefSHelge Deller } 116deae26bfSKyle McMartin 117d2ad824fSHelge Deller #if !defined(CONFIG_64BIT) 118d2ad824fSHelge Deller 1193f795cefSHelge Deller #define __get_user_asm64(val, ptr) \ 1203f795cefSHelge Deller { \ 1213f795cefSHelge Deller union { \ 1223f795cefSHelge Deller unsigned long long l; \ 1233f795cefSHelge Deller __typeof__(*(ptr)) t; \ 1243f795cefSHelge Deller } __gu_tmp; \ 1253f795cefSHelge Deller \ 126d19f5e41SHelge Deller __asm__(" copy %%r0,%R0\n" \ 127d19f5e41SHelge Deller "1: ldw 0(%%sr2,%2),%0\n" \ 128d19f5e41SHelge Deller "2: ldw 4(%%sr2,%2),%R0\n" \ 129d19f5e41SHelge Deller "9:\n" \ 130d19f5e41SHelge Deller ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \ 131d19f5e41SHelge Deller ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \ 1323f795cefSHelge Deller : "=&r"(__gu_tmp.l), "=r"(__gu_err) \ 1333f795cefSHelge Deller : "r"(ptr), "1"(__gu_err)); \ 1343f795cefSHelge Deller \ 1353f795cefSHelge Deller (val) = __gu_tmp.t; \ 1363f795cefSHelge Deller } 137d2ad824fSHelge Deller 138d2ad824fSHelge Deller #endif /* !defined(CONFIG_64BIT) */ 139d2ad824fSHelge Deller 140d2ad824fSHelge Deller 1413f795cefSHelge Deller #define __put_user_internal(x, ptr) \ 142deae26bfSKyle McMartin ({ \ 143deae26bfSKyle McMartin register long __pu_err __asm__ ("r8") = 0; \ 144deae26bfSKyle McMartin __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \ 145deae26bfSKyle McMartin \ 146deae26bfSKyle McMartin switch (sizeof(*(ptr))) { \ 147deae26bfSKyle McMartin case 1: __put_user_asm("stb", __x, ptr); break; \ 148deae26bfSKyle McMartin case 2: __put_user_asm("sth", __x, ptr); break; \ 149deae26bfSKyle McMartin case 4: __put_user_asm("stw", __x, ptr); break; \ 150deae26bfSKyle McMartin case 8: STD_USER(__x, ptr); break; \ 1513f795cefSHelge Deller default: BUILD_BUG(); \ 152deae26bfSKyle McMartin } \ 153deae26bfSKyle McMartin \ 154deae26bfSKyle McMartin __pu_err; \ 155deae26bfSKyle McMartin }) 156deae26bfSKyle McMartin 1573f795cefSHelge Deller #define __put_user(x, ptr) \ 1583f795cefSHelge Deller ({ \ 1593f795cefSHelge Deller load_sr2(); \ 1603f795cefSHelge Deller __put_user_internal(x, ptr); \ 1613f795cefSHelge Deller }) 1623f795cefSHelge Deller 1633f795cefSHelge Deller 164deae26bfSKyle McMartin /* 165deae26bfSKyle McMartin * The "__put_user/kernel_asm()" macros tell gcc they read from memory 166deae26bfSKyle McMartin * instead of writing. This is because they do not write to any memory 167deae26bfSKyle McMartin * gcc knows about, so there are no aliasing issues. These macros must 168d19f5e41SHelge Deller * also be aware that fixups are executed in the context of the fault, 169d19f5e41SHelge Deller * and any registers used there must be listed as clobbers. 170d19f5e41SHelge Deller * r8 is already listed as err. 171deae26bfSKyle McMartin */ 172deae26bfSKyle McMartin 173deae26bfSKyle McMartin #define __put_user_asm(stx, x, ptr) \ 174deae26bfSKyle McMartin __asm__ __volatile__ ( \ 175d19f5e41SHelge Deller "1: " stx " %2,0(%%sr2,%1)\n" \ 176d19f5e41SHelge Deller "9:\n" \ 177d19f5e41SHelge Deller ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \ 178deae26bfSKyle McMartin : "=r"(__pu_err) \ 179d19f5e41SHelge Deller : "r"(ptr), "r"(x), "0"(__pu_err)) 180deae26bfSKyle McMartin 181deae26bfSKyle McMartin 182deae26bfSKyle McMartin #if !defined(CONFIG_64BIT) 183deae26bfSKyle McMartin 184deae26bfSKyle McMartin #define __put_user_asm64(__val, ptr) do { \ 185deae26bfSKyle McMartin __asm__ __volatile__ ( \ 186d19f5e41SHelge Deller "1: stw %2,0(%%sr2,%1)\n" \ 187d19f5e41SHelge Deller "2: stw %R2,4(%%sr2,%1)\n" \ 188d19f5e41SHelge Deller "9:\n" \ 189d19f5e41SHelge Deller ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \ 190d19f5e41SHelge Deller ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \ 191deae26bfSKyle McMartin : "=r"(__pu_err) \ 192d19f5e41SHelge Deller : "r"(ptr), "r"(__val), "0"(__pu_err)); \ 193deae26bfSKyle McMartin } while (0) 194deae26bfSKyle McMartin 195deae26bfSKyle McMartin #endif /* !defined(CONFIG_64BIT) */ 196deae26bfSKyle McMartin 197deae26bfSKyle McMartin 198deae26bfSKyle McMartin /* 199deae26bfSKyle McMartin * Complex access routines -- external declarations 200deae26bfSKyle McMartin */ 201deae26bfSKyle McMartin 202b1195c0eSJames Bottomley extern long strncpy_from_user(char *, const char __user *, long); 203deae26bfSKyle McMartin extern unsigned lclear_user(void __user *, unsigned long); 204*1260dea6SHelge Deller extern __must_check long strnlen_user(const char __user *src, long n); 205deae26bfSKyle McMartin /* 206deae26bfSKyle McMartin * Complex access routines -- macros 207deae26bfSKyle McMartin */ 208a0ffa8f0SHelge Deller #define user_addr_max() (~0UL) 209deae26bfSKyle McMartin 210deae26bfSKyle McMartin #define clear_user lclear_user 211deae26bfSKyle McMartin #define __clear_user lclear_user 212deae26bfSKyle McMartin 213f64fd180SAl Viro unsigned long __must_check raw_copy_to_user(void __user *dst, const void *src, 2149e91db6bSHelge Deller unsigned long len); 215f64fd180SAl Viro unsigned long __must_check raw_copy_from_user(void *dst, const void __user *src, 2169e91db6bSHelge Deller unsigned long len); 217f64fd180SAl Viro #define INLINE_COPY_TO_USER 218f64fd180SAl Viro #define INLINE_COPY_FROM_USER 2199e91db6bSHelge Deller 220e448372cSHelge Deller struct pt_regs; 221c61c25ebSKyle McMartin int fixup_exception(struct pt_regs *regs); 222c61c25ebSKyle McMartin 223deae26bfSKyle McMartin #endif /* __PARISC_UACCESS_H */ 224