xref: /openbmc/linux/arch/parisc/include/asm/uaccess.h (revision 236a9bf2)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __PARISC_UACCESS_H
3 #define __PARISC_UACCESS_H
4 
5 /*
6  * User space memory access functions
7  */
8 #include <asm/page.h>
9 #include <asm/cache.h>
10 
11 #include <linux/bug.h>
12 #include <linux/string.h>
13 
14 #define TASK_SIZE_MAX DEFAULT_TASK_SIZE
15 #include <asm/pgtable.h>
16 #include <asm-generic/access_ok.h>
17 
18 #define put_user __put_user
19 #define get_user __get_user
20 
21 #if !defined(CONFIG_64BIT)
22 #define LDD_USER(sr, val, ptr)	__get_user_asm64(sr, val, ptr)
23 #define STD_USER(sr, x, ptr)	__put_user_asm64(sr, x, ptr)
24 #else
25 #define LDD_USER(sr, val, ptr)	__get_user_asm(sr, val, "ldd", ptr)
26 #define STD_USER(sr, x, ptr)	__put_user_asm(sr, "std", x, ptr)
27 #endif
28 
29 /*
30  * The exception table contains two values: the first is the relative offset to
31  * the address of the instruction that is allowed to fault, and the second is
32  * the relative offset to the address of the fixup routine. Since relative
33  * addresses are used, 32bit values are sufficient even on 64bit kernel.
34  */
35 
36 #define ARCH_HAS_RELATIVE_EXTABLE
37 struct exception_table_entry {
38 	int insn;	/* relative address of insn that is allowed to fault. */
39 	int fixup;	/* relative address of fixup routine */
40 };
41 
42 #define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
43 	".section __ex_table,\"aw\"\n"			   \
44 	".align 4\n"					   \
45 	".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
46 	".previous\n"
47 
48 /*
49  * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
50  * (with lowest bit set) for which the fault handler in fixup_exception() will
51  * load -EFAULT into %r29 for a read or write fault, and zeroes the target
52  * register in case of a read fault in get_user().
53  */
54 #define ASM_EXCEPTIONTABLE_REG	29
55 #define ASM_EXCEPTIONTABLE_VAR(__variable)		\
56 	register long __variable __asm__ ("r29") = 0
57 #define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
58 	ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)
59 
60 #define __get_user_internal(sr, val, ptr)		\
61 ({							\
62 	ASM_EXCEPTIONTABLE_VAR(__gu_err);		\
63 							\
64 	switch (sizeof(*(ptr))) {			\
65 	case 1: __get_user_asm(sr, val, "ldb", ptr); break; \
66 	case 2: __get_user_asm(sr, val, "ldh", ptr); break; \
67 	case 4: __get_user_asm(sr, val, "ldw", ptr); break; \
68 	case 8: LDD_USER(sr, val, ptr); break;		\
69 	default: BUILD_BUG();				\
70 	}						\
71 							\
72 	__gu_err;					\
73 })
74 
75 #define __get_user(val, ptr)				\
76 ({							\
77 	__get_user_internal(SR_USER, val, ptr);	\
78 })
79 
80 #define __get_user_asm(sr, val, ldx, ptr)		\
81 {							\
82 	register long __gu_val;				\
83 							\
84 	__asm__("1: " ldx " 0(%%sr%2,%3),%0\n"		\
85 		"9:\n"					\
86 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	\
87 		: "=r"(__gu_val), "+r"(__gu_err)        \
88 		: "i"(sr), "r"(ptr));			\
89 							\
90 	(val) = (__force __typeof__(*(ptr))) __gu_val;	\
91 }
92 
93 #define __get_kernel_nofault(dst, src, type, err_label)	\
94 {							\
95 	type __z;					\
96 	long __err;					\
97 	__err = __get_user_internal(SR_KERNEL, __z, (type *)(src)); \
98 	if (unlikely(__err))				\
99 		goto err_label;				\
100 	else						\
101 		*(type *)(dst) = __z;			\
102 }
103 
104 
105 #if !defined(CONFIG_64BIT)
106 
107 #define __get_user_asm64(sr, val, ptr)			\
108 {							\
109 	union {						\
110 		unsigned long long	l;		\
111 		__typeof__(*(ptr))	t;		\
112 	} __gu_tmp;					\
113 							\
114 	__asm__("   copy %%r0,%R0\n"			\
115 		"1: ldw 0(%%sr%2,%3),%0\n"		\
116 		"2: ldw 4(%%sr%2,%3),%R0\n"		\
117 		"9:\n"					\
118 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	\
119 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)	\
120 		: "=&r"(__gu_tmp.l), "+r"(__gu_err)	\
121 		: "i"(sr), "r"(ptr));			\
122 							\
123 	(val) = __gu_tmp.t;				\
124 }
125 
126 #endif /* !defined(CONFIG_64BIT) */
127 
128 
129 #define __put_user_internal(sr, x, ptr)				\
130 ({								\
131 	ASM_EXCEPTIONTABLE_VAR(__pu_err);		      	\
132 								\
133 	switch (sizeof(*(ptr))) {				\
134 	case 1: __put_user_asm(sr, "stb", x, ptr); break;	\
135 	case 2: __put_user_asm(sr, "sth", x, ptr); break;	\
136 	case 4: __put_user_asm(sr, "stw", x, ptr); break;	\
137 	case 8: STD_USER(sr, x, ptr); break;			\
138 	default: BUILD_BUG();					\
139 	}							\
140 								\
141 	__pu_err;						\
142 })
143 
144 #define __put_user(x, ptr)					\
145 ({								\
146 	__typeof__(&*(ptr)) __ptr = ptr;			\
147 	__typeof__(*(__ptr)) __x = (__typeof__(*(__ptr)))(x);	\
148 	__put_user_internal(SR_USER, __x, __ptr);		\
149 })
150 
151 #define __put_kernel_nofault(dst, src, type, err_label)		\
152 {								\
153 	type __z = *(type *)(src);				\
154 	long __err;						\
155 	__err = __put_user_internal(SR_KERNEL, __z, (type *)(dst)); \
156 	if (unlikely(__err))					\
157 		goto err_label;					\
158 }
159 
160 
161 
162 
163 /*
164  * The "__put_user/kernel_asm()" macros tell gcc they read from memory
165  * instead of writing. This is because they do not write to any memory
166  * gcc knows about, so there are no aliasing issues. These macros must
167  * also be aware that fixups are executed in the context of the fault,
168  * and any registers used there must be listed as clobbers.
169  * The register holding the possible EFAULT error (ASM_EXCEPTIONTABLE_REG)
170  * is already listed as input and output register.
171  */
172 
173 #define __put_user_asm(sr, stx, x, ptr)				\
174 	__asm__ __volatile__ (					\
175 		"1: " stx " %1,0(%%sr%2,%3)\n"			\
176 		"9:\n"						\
177 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)		\
178 		: "+r"(__pu_err)				\
179 		: "r"(x), "i"(sr), "r"(ptr))
180 
181 
182 #if !defined(CONFIG_64BIT)
183 
184 #define __put_user_asm64(sr, __val, ptr) do {			\
185 	__asm__ __volatile__ (					\
186 		"1: stw %1,0(%%sr%2,%3)\n"			\
187 		"2: stw %R1,4(%%sr%2,%3)\n"			\
188 		"9:\n"						\
189 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)		\
190 		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)		\
191 		: "+r"(__pu_err)				\
192 		: "r"(__val), "i"(sr), "r"(ptr));		\
193 } while (0)
194 
195 #endif /* !defined(CONFIG_64BIT) */
196 
197 
198 /*
199  * Complex access routines -- external declarations
200  */
201 
202 extern long strncpy_from_user(char *, const char __user *, long);
203 extern __must_check unsigned lclear_user(void __user *, unsigned long);
204 extern __must_check long strnlen_user(const char __user *src, long n);
205 /*
206  * Complex access routines -- macros
207  */
208 
209 #define clear_user lclear_user
210 #define __clear_user lclear_user
211 
212 unsigned long __must_check raw_copy_to_user(void __user *dst, const void *src,
213 					    unsigned long len);
214 unsigned long __must_check raw_copy_from_user(void *dst, const void __user *src,
215 					    unsigned long len);
216 #define INLINE_COPY_TO_USER
217 #define INLINE_COPY_FROM_USER
218 
219 struct pt_regs;
220 int fixup_exception(struct pt_regs *regs);
221 
222 #endif /* __PARISC_UACCESS_H */
223