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