blob: 123d5f16cd9db2381c95247e20dc5dc18a78d33a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __PARISC_UACCESS_H
3#define __PARISC_UACCESS_H
4
5/*
6 * User space memory access functions
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Helge Deller8dd95c62014-10-21 21:29:09 +020011#include <linux/bug.h>
Al Viroaace8802016-08-20 19:03:37 -040012#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * Note that since kernel addresses are in a separate address space on
Jesper Juhle49332b2005-05-01 08:59:08 -070016 * parisc, we don't need to do anything for access_ok().
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * We just let the page fault handler do the right thing. This also means
18 * that put_user is the same as __put_user, etc.
19 */
20
Linus Torvalds96d4f262019-01-03 18:57:57 -080021#define access_ok(uaddr, size) \
Helge Deller186ecf12017-03-15 21:48:42 +010022 ( (uaddr) == (uaddr) )
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define put_user __put_user
25#define get_user __get_user
26
Helge Dellerca72a222006-12-20 00:35:57 +010027#if !defined(CONFIG_64BIT)
Helge Deller67102872021-09-09 12:47:00 +020028#define LDD_USER(sr, val, ptr) __get_user_asm64(sr, val, ptr)
29#define STD_USER(sr, x, ptr) __put_user_asm64(sr, x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#else
Helge Deller67102872021-09-09 12:47:00 +020031#define LDD_USER(sr, val, ptr) __get_user_asm(sr, val, "ldd", ptr)
32#define STD_USER(sr, x, ptr) __put_user_asm(sr, "std", x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
34
35/*
Helge Dellercb910c12016-04-08 21:36:06 +020036 * The exception table contains two values: the first is the relative offset to
37 * the address of the instruction that is allowed to fault, and the second is
38 * the relative offset to the address of the fixup routine. Since relative
39 * addresses are used, 32bit values are sufficient even on 64bit kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
Helge Deller0de79852016-03-23 16:00:46 +010042#define ARCH_HAS_RELATIVE_EXTABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070043struct exception_table_entry {
Helge Deller0de79852016-03-23 16:00:46 +010044 int insn; /* relative address of insn that is allowed to fault. */
45 int fixup; /* relative address of fixup routine */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Helge Deller0b3d6432007-01-28 14:52:57 +010048#define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
49 ".section __ex_table,\"aw\"\n" \
Helge Deller0de79852016-03-23 16:00:46 +010050 ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
Helge Deller0b3d6432007-01-28 14:52:57 +010051 ".previous\n"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
Helge Dellerd19f5e42017-03-25 11:59:15 +010054 * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
55 * (with lowest bit set) for which the fault handler in fixup_exception() will
Helge Deller4b9d2a72021-12-23 21:46:35 +010056 * load -EFAULT into %r29 for a read or write fault, and zeroes the target
Helge Dellerd19f5e42017-03-25 11:59:15 +010057 * register in case of a read fault in get_user().
58 */
Helge Deller4b9d2a72021-12-23 21:46:35 +010059#define ASM_EXCEPTIONTABLE_REG 29
60#define ASM_EXCEPTIONTABLE_VAR(__variable) \
61 register long __variable __asm__ ("r29") = 0
Helge Dellerd19f5e42017-03-25 11:59:15 +010062#define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
63 ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)
64
Helge Deller67102872021-09-09 12:47:00 +020065#define __get_user_internal(sr, val, ptr) \
Helge Deller3f795ce2017-04-16 10:00:14 +020066({ \
Helge Deller4b9d2a72021-12-23 21:46:35 +010067 ASM_EXCEPTIONTABLE_VAR(__gu_err); \
Helge Deller3f795ce2017-04-16 10:00:14 +020068 \
69 switch (sizeof(*(ptr))) { \
Helge Deller67102872021-09-09 12:47:00 +020070 case 1: __get_user_asm(sr, val, "ldb", ptr); break; \
71 case 2: __get_user_asm(sr, val, "ldh", ptr); break; \
72 case 4: __get_user_asm(sr, val, "ldw", ptr); break; \
73 case 8: LDD_USER(sr, val, ptr); break; \
Helge Deller3f795ce2017-04-16 10:00:14 +020074 default: BUILD_BUG(); \
75 } \
76 \
77 __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070078})
79
Helge Deller3f795ce2017-04-16 10:00:14 +020080#define __get_user(val, ptr) \
81({ \
Helge Deller67102872021-09-09 12:47:00 +020082 __get_user_internal("%%sr3,", val, ptr); \
Helge Deller3f795ce2017-04-16 10:00:14 +020083})
84
Helge Deller67102872021-09-09 12:47:00 +020085#define __get_user_asm(sr, val, ldx, ptr) \
Helge Deller3f795ce2017-04-16 10:00:14 +020086{ \
87 register long __gu_val; \
88 \
Helge Deller67102872021-09-09 12:47:00 +020089 __asm__("1: " ldx " 0(" sr "%2),%0\n" \
Helge Dellerd19f5e42017-03-25 11:59:15 +010090 "9:\n" \
91 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
Helge Dellerdbd0b422022-02-13 22:52:11 +010092 : "=r"(__gu_val), "+r"(__gu_err) \
93 : "r"(ptr)); \
Helge Deller3f795ce2017-04-16 10:00:14 +020094 \
95 (val) = (__force __typeof__(*(ptr))) __gu_val; \
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Helge Deller67102872021-09-09 12:47:00 +020098#define HAVE_GET_KERNEL_NOFAULT
99#define __get_kernel_nofault(dst, src, type, err_label) \
100{ \
101 type __z; \
102 long __err; \
103 __err = __get_user_internal("%%sr0,", __z, (type *)(src)); \
104 if (unlikely(__err)) \
105 goto err_label; \
106 else \
107 *(type *)(dst) = __z; \
108}
109
110
Helge Dellerd2ad8242016-04-09 08:27:08 +0200111#if !defined(CONFIG_64BIT)
112
Helge Deller67102872021-09-09 12:47:00 +0200113#define __get_user_asm64(sr, val, ptr) \
Helge Deller3f795ce2017-04-16 10:00:14 +0200114{ \
115 union { \
116 unsigned long long l; \
117 __typeof__(*(ptr)) t; \
118 } __gu_tmp; \
119 \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100120 __asm__(" copy %%r0,%R0\n" \
Helge Deller67102872021-09-09 12:47:00 +0200121 "1: ldw 0(" sr "%2),%0\n" \
122 "2: ldw 4(" sr "%2),%R0\n" \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100123 "9:\n" \
124 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
125 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
Helge Dellerdbd0b422022-02-13 22:52:11 +0100126 : "=&r"(__gu_tmp.l), "+r"(__gu_err) \
127 : "r"(ptr)); \
Helge Deller3f795ce2017-04-16 10:00:14 +0200128 \
129 (val) = __gu_tmp.t; \
130}
Helge Dellerd2ad8242016-04-09 08:27:08 +0200131
132#endif /* !defined(CONFIG_64BIT) */
133
134
Helge Deller67102872021-09-09 12:47:00 +0200135#define __put_user_internal(sr, x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136({ \
Helge Deller4b9d2a72021-12-23 21:46:35 +0100137 ASM_EXCEPTIONTABLE_VAR(__pu_err); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 \
Helge Deller06bff6b2016-04-09 08:26:14 +0200139 switch (sizeof(*(ptr))) { \
Helge Dellerdbd0b422022-02-13 22:52:11 +0100140 case 1: __put_user_asm(sr, "stb", x, ptr); break; \
141 case 2: __put_user_asm(sr, "sth", x, ptr); break; \
142 case 4: __put_user_asm(sr, "stw", x, ptr); break; \
143 case 8: STD_USER(sr, x, ptr); break; \
Helge Deller3f795ce2017-04-16 10:00:14 +0200144 default: BUILD_BUG(); \
145 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 \
147 __pu_err; \
148})
149
Helge Deller3f795ce2017-04-16 10:00:14 +0200150#define __put_user(x, ptr) \
151({ \
Helge Dellerdbd0b422022-02-13 22:52:11 +0100152 __typeof__(&*(ptr)) __ptr = ptr; \
153 __typeof__(*(__ptr)) __x = (__typeof__(*(__ptr)))(x); \
154 __put_user_internal("%%sr3,", __x, __ptr); \
Helge Deller3f795ce2017-04-16 10:00:14 +0200155})
156
Helge Deller67102872021-09-09 12:47:00 +0200157#define __put_kernel_nofault(dst, src, type, err_label) \
158{ \
159 type __z = *(type *)(src); \
160 long __err; \
161 __err = __put_user_internal("%%sr0,", __z, (type *)(dst)); \
162 if (unlikely(__err)) \
163 goto err_label; \
164}
165
166
167
Helge Deller3f795ce2017-04-16 10:00:14 +0200168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169/*
170 * The "__put_user/kernel_asm()" macros tell gcc they read from memory
171 * instead of writing. This is because they do not write to any memory
Carlos O'Donell3fd3a742006-04-22 14:47:21 -0600172 * gcc knows about, so there are no aliasing issues. These macros must
Helge Dellerd19f5e42017-03-25 11:59:15 +0100173 * also be aware that fixups are executed in the context of the fault,
174 * and any registers used there must be listed as clobbers.
Helge Deller4b9d2a72021-12-23 21:46:35 +0100175 * The register holding the possible EFAULT error (ASM_EXCEPTIONTABLE_REG)
176 * is already listed as input and output register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 */
178
Helge Deller67102872021-09-09 12:47:00 +0200179#define __put_user_asm(sr, stx, x, ptr) \
180 __asm__ __volatile__ ( \
181 "1: " stx " %2,0(" sr "%1)\n" \
182 "9:\n" \
183 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
Helge Dellerdbd0b422022-02-13 22:52:11 +0100184 : "+r"(__pu_err) \
185 : "r"(ptr), "r"(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Helge Dellerca72a222006-12-20 00:35:57 +0100188#if !defined(CONFIG_64BIT)
Helge Deller94a19812006-12-19 22:33:58 +0100189
Helge Deller67102872021-09-09 12:47:00 +0200190#define __put_user_asm64(sr, __val, ptr) do { \
191 __asm__ __volatile__ ( \
192 "1: stw %2,0(" sr "%1)\n" \
193 "2: stw %R2,4(" sr "%1)\n" \
194 "9:\n" \
195 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
196 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
Helge Dellerdbd0b422022-02-13 22:52:11 +0100197 : "+r"(__pu_err) \
198 : "r"(ptr), "r"(__val)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199} while (0)
200
Helge Dellerca72a222006-12-20 00:35:57 +0100201#endif /* !defined(CONFIG_64BIT) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203
204/*
205 * Complex access routines -- external declarations
206 */
207
James Bottomleyb1195c02012-05-26 09:48:19 +0100208extern long strncpy_from_user(char *, const char __user *, long);
Helge Deller67102872021-09-09 12:47:00 +0200209extern __must_check unsigned lclear_user(void __user *, unsigned long);
Helge Deller1260dea2021-09-04 23:49:26 +0200210extern __must_check long strnlen_user(const char __user *src, long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * Complex access routines -- macros
213 */
214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215#define clear_user lclear_user
216#define __clear_user lclear_user
217
Al Virof64fd182017-03-24 23:11:26 -0400218unsigned long __must_check raw_copy_to_user(void __user *dst, const void *src,
219 unsigned long len);
220unsigned long __must_check raw_copy_from_user(void *dst, const void __user *src,
221 unsigned long len);
Al Virof64fd182017-03-24 23:11:26 -0400222#define INLINE_COPY_TO_USER
223#define INLINE_COPY_FROM_USER
Helge Deller888c31f2010-02-01 19:56:33 +0000224
Helge Dellere4483722009-01-13 20:52:46 +0100225struct pt_regs;
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000226int fixup_exception(struct pt_regs *regs);
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#endif /* __PARISC_UACCESS_H */