Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | #ifndef __PARISC_UACCESS_H |
| 2 | #define __PARISC_UACCESS_H |
| 3 | |
| 4 | /* |
| 5 | * User space memory access functions |
| 6 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | #include <asm/page.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <asm/cache.h> |
Helge Deller | 888c31f | 2010-02-01 19:56:33 +0000 | [diff] [blame] | 9 | #include <asm/errno.h> |
Arnd Bergmann | 5b17e1c | 2009-05-13 22:56:30 +0000 | [diff] [blame] | 10 | #include <asm-generic/uaccess-unaligned.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | |
Helge Deller | 8dd95c6 | 2014-10-21 21:29:09 +0200 | [diff] [blame] | 12 | #include <linux/bug.h> |
| 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #define VERIFY_READ 0 |
| 15 | #define VERIFY_WRITE 1 |
| 16 | |
| 17 | #define KERNEL_DS ((mm_segment_t){0}) |
| 18 | #define USER_DS ((mm_segment_t){1}) |
| 19 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 20 | #define segment_eq(a, b) ((a).seg == (b).seg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | |
| 22 | #define get_ds() (KERNEL_DS) |
| 23 | #define get_fs() (current_thread_info()->addr_limit) |
| 24 | #define set_fs(x) (current_thread_info()->addr_limit = (x)) |
| 25 | |
| 26 | /* |
| 27 | * Note that since kernel addresses are in a separate address space on |
Jesper Juhl | e49332b | 2005-05-01 08:59:08 -0700 | [diff] [blame] | 28 | * parisc, we don't need to do anything for access_ok(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | * We just let the page fault handler do the right thing. This also means |
| 30 | * that put_user is the same as __put_user, etc. |
| 31 | */ |
| 32 | |
Helge Deller | a0ffa8f | 2013-11-19 23:31:35 +0100 | [diff] [blame] | 33 | static inline long access_ok(int type, const void __user * addr, |
| 34 | unsigned long size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
Helge Deller | a0ffa8f | 2013-11-19 23:31:35 +0100 | [diff] [blame] | 36 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | #define put_user __put_user |
| 40 | #define get_user __get_user |
| 41 | |
Helge Deller | ca72a22 | 2006-12-20 00:35:57 +0100 | [diff] [blame] | 42 | #if !defined(CONFIG_64BIT) |
Helge Deller | 8dd95c6 | 2014-10-21 21:29:09 +0200 | [diff] [blame] | 43 | #define LDD_KERNEL(ptr) BUILD_BUG() |
| 44 | #define LDD_USER(ptr) BUILD_BUG() |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 45 | #define STD_KERNEL(x, ptr) __put_kernel_asm64(x, ptr) |
| 46 | #define STD_USER(x, ptr) __put_user_asm64(x, ptr) |
Helge Deller | 94a1981 | 2006-12-19 22:33:58 +0100 | [diff] [blame] | 47 | #define ASM_WORD_INSN ".word\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #else |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 49 | #define LDD_KERNEL(ptr) __get_kernel_asm("ldd", ptr) |
| 50 | #define LDD_USER(ptr) __get_user_asm("ldd", ptr) |
| 51 | #define STD_KERNEL(x, ptr) __put_kernel_asm("std", x, ptr) |
| 52 | #define STD_USER(x, ptr) __put_user_asm("std", x, ptr) |
Helge Deller | 94a1981 | 2006-12-19 22:33:58 +0100 | [diff] [blame] | 53 | #define ASM_WORD_INSN ".dword\t" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #endif |
| 55 | |
| 56 | /* |
| 57 | * The exception table contains two values: the first is an address |
| 58 | * for an instruction that is allowed to fault, and the second is |
Helge Deller | 61dbbae | 2013-10-13 21:11:30 +0200 | [diff] [blame] | 59 | * the address to the fixup routine. Even on a 64bit kernel we could |
| 60 | * use a 32bit (unsigned int) address here. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | */ |
| 62 | |
Helge Deller | 0de7985 | 2016-03-23 16:00:46 +0100 | [diff] [blame^] | 63 | #define ARCH_HAS_RELATIVE_EXTABLE |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | struct exception_table_entry { |
Helge Deller | 0de7985 | 2016-03-23 16:00:46 +0100 | [diff] [blame^] | 65 | int insn; /* relative address of insn that is allowed to fault. */ |
| 66 | int fixup; /* relative address of fixup routine */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | }; |
| 68 | |
Helge Deller | 0b3d643 | 2007-01-28 14:52:57 +0100 | [diff] [blame] | 69 | #define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\ |
| 70 | ".section __ex_table,\"aw\"\n" \ |
Helge Deller | 0de7985 | 2016-03-23 16:00:46 +0100 | [diff] [blame^] | 71 | ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \ |
Helge Deller | 0b3d643 | 2007-01-28 14:52:57 +0100 | [diff] [blame] | 72 | ".previous\n" |
| 73 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | /* |
| 75 | * The page fault handler stores, in a per-cpu area, the following information |
| 76 | * if a fixup routine is available. |
| 77 | */ |
| 78 | struct exception_data { |
| 79 | unsigned long fault_ip; |
| 80 | unsigned long fault_space; |
| 81 | unsigned long fault_addr; |
| 82 | }; |
| 83 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 84 | #define __get_user(x, ptr) \ |
| 85 | ({ \ |
| 86 | register long __gu_err __asm__ ("r8") = 0; \ |
| 87 | register long __gu_val __asm__ ("r9") = 0; \ |
| 88 | \ |
| 89 | if (segment_eq(get_fs(), KERNEL_DS)) { \ |
| 90 | switch (sizeof(*(ptr))) { \ |
| 91 | case 1: __get_kernel_asm("ldb", ptr); break; \ |
| 92 | case 2: __get_kernel_asm("ldh", ptr); break; \ |
| 93 | case 4: __get_kernel_asm("ldw", ptr); break; \ |
| 94 | case 8: LDD_KERNEL(ptr); break; \ |
| 95 | default: BUILD_BUG(); break; \ |
| 96 | } \ |
| 97 | } \ |
| 98 | else { \ |
| 99 | switch (sizeof(*(ptr))) { \ |
| 100 | case 1: __get_user_asm("ldb", ptr); break; \ |
| 101 | case 2: __get_user_asm("ldh", ptr); break; \ |
| 102 | case 4: __get_user_asm("ldw", ptr); break; \ |
| 103 | case 8: LDD_USER(ptr); break; \ |
| 104 | default: BUILD_BUG(); break; \ |
| 105 | } \ |
| 106 | } \ |
| 107 | \ |
| 108 | (x) = (__force __typeof__(*(ptr))) __gu_val; \ |
| 109 | __gu_err; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | }) |
| 111 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 112 | #define __get_kernel_asm(ldx, ptr) \ |
Helge Deller | 0b3d643 | 2007-01-28 14:52:57 +0100 | [diff] [blame] | 113 | __asm__("\n1:\t" ldx "\t0(%2),%0\n\t" \ |
| 114 | ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_1)\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | : "=r"(__gu_val), "=r"(__gu_err) \ |
| 116 | : "r"(ptr), "1"(__gu_err) \ |
| 117 | : "r1"); |
| 118 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 119 | #define __get_user_asm(ldx, ptr) \ |
Helge Deller | 0b3d643 | 2007-01-28 14:52:57 +0100 | [diff] [blame] | 120 | __asm__("\n1:\t" ldx "\t0(%%sr3,%2),%0\n\t" \ |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 121 | ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_1)\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | : "=r"(__gu_val), "=r"(__gu_err) \ |
| 123 | : "r"(ptr), "1"(__gu_err) \ |
| 124 | : "r1"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 126 | #define __put_user(x, ptr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | ({ \ |
| 128 | register long __pu_err __asm__ ("r8") = 0; \ |
| 129 | __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \ |
| 130 | \ |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 131 | if (segment_eq(get_fs(), KERNEL_DS)) { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | switch (sizeof(*(ptr))) { \ |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 133 | case 1: __put_kernel_asm("stb", __x, ptr); break; \ |
| 134 | case 2: __put_kernel_asm("sth", __x, ptr); break; \ |
| 135 | case 4: __put_kernel_asm("stw", __x, ptr); break; \ |
| 136 | case 8: STD_KERNEL(__x, ptr); break; \ |
Helge Deller | 8dd95c6 | 2014-10-21 21:29:09 +0200 | [diff] [blame] | 137 | default: BUILD_BUG(); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } \ |
| 139 | } \ |
| 140 | else { \ |
| 141 | switch (sizeof(*(ptr))) { \ |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 142 | case 1: __put_user_asm("stb", __x, ptr); break; \ |
| 143 | case 2: __put_user_asm("sth", __x, ptr); break; \ |
| 144 | case 4: __put_user_asm("stw", __x, ptr); break; \ |
| 145 | case 8: STD_USER(__x, ptr); break; \ |
Helge Deller | 8dd95c6 | 2014-10-21 21:29:09 +0200 | [diff] [blame] | 146 | default: BUILD_BUG(); break; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | } \ |
| 148 | } \ |
| 149 | \ |
| 150 | __pu_err; \ |
| 151 | }) |
| 152 | |
| 153 | /* |
| 154 | * The "__put_user/kernel_asm()" macros tell gcc they read from memory |
| 155 | * instead of writing. This is because they do not write to any memory |
Carlos O'Donell | 3fd3a74 | 2006-04-22 14:47:21 -0600 | [diff] [blame] | 156 | * gcc knows about, so there are no aliasing issues. These macros must |
| 157 | * also be aware that "fixup_put_user_skip_[12]" are executed in the |
| 158 | * context of the fault, and any registers used there must be listed |
| 159 | * as clobbers. In this case only "r1" is used by the current routines. |
| 160 | * r8/r9 are already listed as err/val. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | */ |
| 162 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 163 | #define __put_kernel_asm(stx, x, ptr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | __asm__ __volatile__ ( \ |
Helge Deller | 0b3d643 | 2007-01-28 14:52:57 +0100 | [diff] [blame] | 165 | "\n1:\t" stx "\t%2,0(%1)\n\t" \ |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 166 | ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_1)\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | : "=r"(__pu_err) \ |
Carlos O'Donell | 3fd3a74 | 2006-04-22 14:47:21 -0600 | [diff] [blame] | 168 | : "r"(ptr), "r"(x), "0"(__pu_err) \ |
| 169 | : "r1") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 171 | #define __put_user_asm(stx, x, ptr) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 172 | __asm__ __volatile__ ( \ |
Helge Deller | 0b3d643 | 2007-01-28 14:52:57 +0100 | [diff] [blame] | 173 | "\n1:\t" stx "\t%2,0(%%sr3,%1)\n\t" \ |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 174 | ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_1)\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | : "=r"(__pu_err) \ |
| 176 | : "r"(ptr), "r"(x), "0"(__pu_err) \ |
| 177 | : "r1") |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | |
Helge Deller | ca72a22 | 2006-12-20 00:35:57 +0100 | [diff] [blame] | 180 | #if !defined(CONFIG_64BIT) |
Helge Deller | 94a1981 | 2006-12-19 22:33:58 +0100 | [diff] [blame] | 181 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 182 | #define __put_kernel_asm64(__val, ptr) do { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | __asm__ __volatile__ ( \ |
Helge Deller | 0b3d643 | 2007-01-28 14:52:57 +0100 | [diff] [blame] | 184 | "\n1:\tstw %2,0(%1)" \ |
Will Deacon | 0f28b62 | 2013-04-22 12:53:43 +0000 | [diff] [blame] | 185 | "\n2:\tstw %R2,4(%1)\n\t" \ |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 186 | ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_2)\ |
| 187 | ASM_EXCEPTIONTABLE_ENTRY(2b, fixup_put_user_skip_1)\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | : "=r"(__pu_err) \ |
Will Deacon | 0f28b62 | 2013-04-22 12:53:43 +0000 | [diff] [blame] | 189 | : "r"(ptr), "r"(__val), "0"(__pu_err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | : "r1"); \ |
| 191 | } while (0) |
| 192 | |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 193 | #define __put_user_asm64(__val, ptr) do { \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | __asm__ __volatile__ ( \ |
Helge Deller | 0b3d643 | 2007-01-28 14:52:57 +0100 | [diff] [blame] | 195 | "\n1:\tstw %2,0(%%sr3,%1)" \ |
Will Deacon | 0f28b62 | 2013-04-22 12:53:43 +0000 | [diff] [blame] | 196 | "\n2:\tstw %R2,4(%%sr3,%1)\n\t" \ |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 197 | ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_2)\ |
| 198 | ASM_EXCEPTIONTABLE_ENTRY(2b, fixup_put_user_skip_1)\ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | : "=r"(__pu_err) \ |
Will Deacon | 0f28b62 | 2013-04-22 12:53:43 +0000 | [diff] [blame] | 200 | : "r"(ptr), "r"(__val), "0"(__pu_err) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | : "r1"); \ |
| 202 | } while (0) |
| 203 | |
Helge Deller | ca72a22 | 2006-12-20 00:35:57 +0100 | [diff] [blame] | 204 | #endif /* !defined(CONFIG_64BIT) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
| 206 | |
| 207 | /* |
| 208 | * Complex access routines -- external declarations |
| 209 | */ |
| 210 | |
| 211 | extern unsigned long lcopy_to_user(void __user *, const void *, unsigned long); |
| 212 | extern unsigned long lcopy_from_user(void *, const void __user *, unsigned long); |
| 213 | extern unsigned long lcopy_in_user(void __user *, const void __user *, unsigned long); |
James Bottomley | b1195c0 | 2012-05-26 09:48:19 +0100 | [diff] [blame] | 214 | extern long strncpy_from_user(char *, const char __user *, long); |
Michael S. Tsirkin | b9762e7 | 2015-01-06 17:45:34 +0200 | [diff] [blame] | 215 | extern unsigned lclear_user(void __user *, unsigned long); |
| 216 | extern long lstrnlen_user(const char __user *, long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | /* |
| 218 | * Complex access routines -- macros |
| 219 | */ |
Helge Deller | a0ffa8f | 2013-11-19 23:31:35 +0100 | [diff] [blame] | 220 | #define user_addr_max() (~0UL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | #define strnlen_user lstrnlen_user |
| 223 | #define strlen_user(str) lstrnlen_user(str, 0x7fffffffL) |
| 224 | #define clear_user lclear_user |
| 225 | #define __clear_user lclear_user |
| 226 | |
| 227 | unsigned long copy_to_user(void __user *dst, const void *src, unsigned long len); |
| 228 | #define __copy_to_user copy_to_user |
Helge Deller | 888c31f | 2010-02-01 19:56:33 +0000 | [diff] [blame] | 229 | unsigned long __copy_from_user(void *dst, const void __user *src, unsigned long len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | unsigned long copy_in_user(void __user *dst, const void __user *src, unsigned long len); |
| 231 | #define __copy_in_user copy_in_user |
| 232 | #define __copy_to_user_inatomic __copy_to_user |
| 233 | #define __copy_from_user_inatomic __copy_from_user |
| 234 | |
Helge Deller | 888c31f | 2010-02-01 19:56:33 +0000 | [diff] [blame] | 235 | extern void copy_from_user_overflow(void) |
| 236 | #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS |
| 237 | __compiletime_error("copy_from_user() buffer size is not provably correct") |
| 238 | #else |
| 239 | __compiletime_warning("copy_from_user() buffer size is not provably correct") |
| 240 | #endif |
| 241 | ; |
| 242 | |
| 243 | static inline unsigned long __must_check copy_from_user(void *to, |
| 244 | const void __user *from, |
| 245 | unsigned long n) |
| 246 | { |
| 247 | int sz = __compiletime_object_size(to); |
| 248 | int ret = -EFAULT; |
| 249 | |
| 250 | if (likely(sz == -1 || !__builtin_constant_p(n) || sz >= n)) |
| 251 | ret = __copy_from_user(to, from, n); |
| 252 | else |
| 253 | copy_from_user_overflow(); |
| 254 | |
| 255 | return ret; |
| 256 | } |
| 257 | |
Helge Deller | e448372 | 2009-01-13 20:52:46 +0100 | [diff] [blame] | 258 | struct pt_regs; |
Kyle McMartin | c61c25e | 2008-12-20 02:29:06 +0000 | [diff] [blame] | 259 | int fixup_exception(struct pt_regs *regs); |
| 260 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | #endif /* __PARISC_UACCESS_H */ |