xref: /openbmc/linux/arch/parisc/include/asm/uaccess.h (revision a0ffa8f06f77a6197405521324501614d281bee1)
1deae26bfSKyle McMartin #ifndef __PARISC_UACCESS_H
2deae26bfSKyle McMartin #define __PARISC_UACCESS_H
3deae26bfSKyle McMartin 
4deae26bfSKyle McMartin /*
5deae26bfSKyle McMartin  * User space memory access functions
6deae26bfSKyle McMartin  */
7deae26bfSKyle McMartin #include <asm/page.h>
8deae26bfSKyle McMartin #include <asm/cache.h>
9888c31fcSHelge Deller #include <asm/errno.h>
105b17e1cdSArnd Bergmann #include <asm-generic/uaccess-unaligned.h>
11deae26bfSKyle McMartin 
12deae26bfSKyle McMartin #define VERIFY_READ 0
13deae26bfSKyle McMartin #define VERIFY_WRITE 1
14deae26bfSKyle McMartin 
15deae26bfSKyle McMartin #define KERNEL_DS	((mm_segment_t){0})
16deae26bfSKyle McMartin #define USER_DS 	((mm_segment_t){1})
17deae26bfSKyle McMartin 
18deae26bfSKyle McMartin #define segment_eq(a,b)	((a).seg == (b).seg)
19deae26bfSKyle McMartin 
20deae26bfSKyle McMartin #define get_ds()	(KERNEL_DS)
21deae26bfSKyle McMartin #define get_fs()	(current_thread_info()->addr_limit)
22deae26bfSKyle McMartin #define set_fs(x)	(current_thread_info()->addr_limit = (x))
23deae26bfSKyle McMartin 
24deae26bfSKyle McMartin /*
25deae26bfSKyle McMartin  * Note that since kernel addresses are in a separate address space on
26deae26bfSKyle McMartin  * parisc, we don't need to do anything for access_ok().
27deae26bfSKyle McMartin  * We just let the page fault handler do the right thing. This also means
28deae26bfSKyle McMartin  * that put_user is the same as __put_user, etc.
29deae26bfSKyle McMartin  */
30deae26bfSKyle McMartin 
31deae26bfSKyle McMartin extern int __get_kernel_bad(void);
32deae26bfSKyle McMartin extern int __get_user_bad(void);
33deae26bfSKyle McMartin extern int __put_kernel_bad(void);
34deae26bfSKyle McMartin extern int __put_user_bad(void);
35deae26bfSKyle McMartin 
36*a0ffa8f0SHelge Deller static inline long access_ok(int type, const void __user * addr,
37*a0ffa8f0SHelge Deller 		unsigned long size)
38deae26bfSKyle McMartin {
39*a0ffa8f0SHelge Deller 	return 1;
40deae26bfSKyle McMartin }
41deae26bfSKyle McMartin 
42deae26bfSKyle McMartin #define put_user __put_user
43deae26bfSKyle McMartin #define get_user __get_user
44deae26bfSKyle McMartin 
45deae26bfSKyle McMartin #if !defined(CONFIG_64BIT)
46deae26bfSKyle McMartin #define LDD_KERNEL(ptr)		__get_kernel_bad();
47deae26bfSKyle McMartin #define LDD_USER(ptr)		__get_user_bad();
48deae26bfSKyle McMartin #define STD_KERNEL(x, ptr)	__put_kernel_asm64(x,ptr)
49deae26bfSKyle McMartin #define STD_USER(x, ptr)	__put_user_asm64(x,ptr)
50deae26bfSKyle McMartin #define ASM_WORD_INSN		".word\t"
51deae26bfSKyle McMartin #else
52deae26bfSKyle McMartin #define LDD_KERNEL(ptr)		__get_kernel_asm("ldd",ptr)
53deae26bfSKyle McMartin #define LDD_USER(ptr)		__get_user_asm("ldd",ptr)
54deae26bfSKyle McMartin #define STD_KERNEL(x, ptr)	__put_kernel_asm("std",x,ptr)
55deae26bfSKyle McMartin #define STD_USER(x, ptr)	__put_user_asm("std",x,ptr)
56deae26bfSKyle McMartin #define ASM_WORD_INSN		".dword\t"
57deae26bfSKyle McMartin #endif
58deae26bfSKyle McMartin 
59deae26bfSKyle McMartin /*
60deae26bfSKyle McMartin  * The exception table contains two values: the first is an address
61deae26bfSKyle McMartin  * for an instruction that is allowed to fault, and the second is
6261dbbaebSHelge Deller  * the address to the fixup routine. Even on a 64bit kernel we could
6361dbbaebSHelge Deller  * use a 32bit (unsigned int) address here.
64deae26bfSKyle McMartin  */
65deae26bfSKyle McMartin 
66deae26bfSKyle McMartin struct exception_table_entry {
67deae26bfSKyle McMartin 	unsigned long insn;	/* address of insn that is allowed to fault. */
6861dbbaebSHelge Deller 	unsigned long fixup;	/* fixup routine */
69deae26bfSKyle McMartin };
70deae26bfSKyle McMartin 
71deae26bfSKyle McMartin #define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
72deae26bfSKyle McMartin 	".section __ex_table,\"aw\"\n"			   \
73deae26bfSKyle McMartin 	ASM_WORD_INSN #fault_addr ", " #except_addr "\n\t" \
74deae26bfSKyle McMartin 	".previous\n"
75deae26bfSKyle McMartin 
76deae26bfSKyle McMartin /*
77deae26bfSKyle McMartin  * The page fault handler stores, in a per-cpu area, the following information
78deae26bfSKyle McMartin  * if a fixup routine is available.
79deae26bfSKyle McMartin  */
80deae26bfSKyle McMartin struct exception_data {
81deae26bfSKyle McMartin 	unsigned long fault_ip;
82deae26bfSKyle McMartin 	unsigned long fault_space;
83deae26bfSKyle McMartin 	unsigned long fault_addr;
84deae26bfSKyle McMartin };
85deae26bfSKyle McMartin 
86deae26bfSKyle McMartin #define __get_user(x,ptr)                               \
87deae26bfSKyle McMartin ({                                                      \
88deae26bfSKyle McMartin 	register long __gu_err __asm__ ("r8") = 0;      \
89deae26bfSKyle McMartin 	register long __gu_val __asm__ ("r9") = 0;      \
90deae26bfSKyle McMartin 							\
91deae26bfSKyle McMartin 	if (segment_eq(get_fs(),KERNEL_DS)) {           \
92deae26bfSKyle McMartin 	    switch (sizeof(*(ptr))) {                   \
93deae26bfSKyle McMartin 	    case 1: __get_kernel_asm("ldb",ptr); break; \
94deae26bfSKyle McMartin 	    case 2: __get_kernel_asm("ldh",ptr); break; \
95deae26bfSKyle McMartin 	    case 4: __get_kernel_asm("ldw",ptr); break; \
96deae26bfSKyle McMartin 	    case 8: LDD_KERNEL(ptr); break;		\
97deae26bfSKyle McMartin 	    default: __get_kernel_bad(); break;         \
98deae26bfSKyle McMartin 	    }                                           \
99deae26bfSKyle McMartin 	}                                               \
100deae26bfSKyle McMartin 	else {                                          \
101deae26bfSKyle McMartin 	    switch (sizeof(*(ptr))) {                   \
102deae26bfSKyle McMartin 	    case 1: __get_user_asm("ldb",ptr); break;   \
103deae26bfSKyle McMartin 	    case 2: __get_user_asm("ldh",ptr); break;   \
104deae26bfSKyle McMartin 	    case 4: __get_user_asm("ldw",ptr); break;   \
105deae26bfSKyle McMartin 	    case 8: LDD_USER(ptr);  break;		\
106deae26bfSKyle McMartin 	    default: __get_user_bad(); break;           \
107deae26bfSKyle McMartin 	    }                                           \
108deae26bfSKyle McMartin 	}                                               \
109deae26bfSKyle McMartin 							\
110deae26bfSKyle McMartin 	(x) = (__typeof__(*(ptr))) __gu_val;            \
111deae26bfSKyle McMartin 	__gu_err;                                       \
112deae26bfSKyle McMartin })
113deae26bfSKyle McMartin 
114deae26bfSKyle McMartin #define __get_kernel_asm(ldx,ptr)                       \
115deae26bfSKyle McMartin 	__asm__("\n1:\t" ldx "\t0(%2),%0\n\t"		\
116deae26bfSKyle McMartin 		ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_1)\
117deae26bfSKyle McMartin 		: "=r"(__gu_val), "=r"(__gu_err)        \
118deae26bfSKyle McMartin 		: "r"(ptr), "1"(__gu_err)		\
119deae26bfSKyle McMartin 		: "r1");
120deae26bfSKyle McMartin 
121deae26bfSKyle McMartin #define __get_user_asm(ldx,ptr)                         \
122deae26bfSKyle McMartin 	__asm__("\n1:\t" ldx "\t0(%%sr3,%2),%0\n\t"	\
123deae26bfSKyle McMartin 		ASM_EXCEPTIONTABLE_ENTRY(1b,fixup_get_user_skip_1)\
124deae26bfSKyle McMartin 		: "=r"(__gu_val), "=r"(__gu_err)        \
125deae26bfSKyle McMartin 		: "r"(ptr), "1"(__gu_err)		\
126deae26bfSKyle McMartin 		: "r1");
127deae26bfSKyle McMartin 
128deae26bfSKyle McMartin #define __put_user(x,ptr)                                       \
129deae26bfSKyle McMartin ({								\
130deae26bfSKyle McMartin 	register long __pu_err __asm__ ("r8") = 0;      	\
131deae26bfSKyle McMartin         __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);	\
132deae26bfSKyle McMartin 								\
133deae26bfSKyle McMartin 	if (segment_eq(get_fs(),KERNEL_DS)) {                   \
134deae26bfSKyle McMartin 	    switch (sizeof(*(ptr))) {                           \
135deae26bfSKyle McMartin 	    case 1: __put_kernel_asm("stb",__x,ptr); break;     \
136deae26bfSKyle McMartin 	    case 2: __put_kernel_asm("sth",__x,ptr); break;     \
137deae26bfSKyle McMartin 	    case 4: __put_kernel_asm("stw",__x,ptr); break;     \
138deae26bfSKyle McMartin 	    case 8: STD_KERNEL(__x,ptr); break;			\
139deae26bfSKyle McMartin 	    default: __put_kernel_bad(); break;			\
140deae26bfSKyle McMartin 	    }                                                   \
141deae26bfSKyle McMartin 	}                                                       \
142deae26bfSKyle McMartin 	else {                                                  \
143deae26bfSKyle McMartin 	    switch (sizeof(*(ptr))) {                           \
144deae26bfSKyle McMartin 	    case 1: __put_user_asm("stb",__x,ptr); break;       \
145deae26bfSKyle McMartin 	    case 2: __put_user_asm("sth",__x,ptr); break;       \
146deae26bfSKyle McMartin 	    case 4: __put_user_asm("stw",__x,ptr); break;       \
147deae26bfSKyle McMartin 	    case 8: STD_USER(__x,ptr); break;			\
148deae26bfSKyle McMartin 	    default: __put_user_bad(); break;			\
149deae26bfSKyle McMartin 	    }                                                   \
150deae26bfSKyle McMartin 	}                                                       \
151deae26bfSKyle McMartin 								\
152deae26bfSKyle McMartin 	__pu_err;						\
153deae26bfSKyle McMartin })
154deae26bfSKyle McMartin 
155deae26bfSKyle McMartin /*
156deae26bfSKyle McMartin  * The "__put_user/kernel_asm()" macros tell gcc they read from memory
157deae26bfSKyle McMartin  * instead of writing. This is because they do not write to any memory
158deae26bfSKyle McMartin  * gcc knows about, so there are no aliasing issues. These macros must
159deae26bfSKyle McMartin  * also be aware that "fixup_put_user_skip_[12]" are executed in the
160deae26bfSKyle McMartin  * context of the fault, and any registers used there must be listed
161deae26bfSKyle McMartin  * as clobbers. In this case only "r1" is used by the current routines.
162deae26bfSKyle McMartin  * r8/r9 are already listed as err/val.
163deae26bfSKyle McMartin  */
164deae26bfSKyle McMartin 
165deae26bfSKyle McMartin #define __put_kernel_asm(stx,x,ptr)                         \
166deae26bfSKyle McMartin 	__asm__ __volatile__ (                              \
167deae26bfSKyle McMartin 		"\n1:\t" stx "\t%2,0(%1)\n\t"		    \
168deae26bfSKyle McMartin 		ASM_EXCEPTIONTABLE_ENTRY(1b,fixup_put_user_skip_1)\
169deae26bfSKyle McMartin 		: "=r"(__pu_err)                            \
170deae26bfSKyle McMartin 		: "r"(ptr), "r"(x), "0"(__pu_err)	    \
171deae26bfSKyle McMartin 	    	: "r1")
172deae26bfSKyle McMartin 
173deae26bfSKyle McMartin #define __put_user_asm(stx,x,ptr)                           \
174deae26bfSKyle McMartin 	__asm__ __volatile__ (                              \
175deae26bfSKyle McMartin 		"\n1:\t" stx "\t%2,0(%%sr3,%1)\n\t"	    \
176deae26bfSKyle McMartin 		ASM_EXCEPTIONTABLE_ENTRY(1b,fixup_put_user_skip_1)\
177deae26bfSKyle McMartin 		: "=r"(__pu_err)                            \
178deae26bfSKyle McMartin 		: "r"(ptr), "r"(x), "0"(__pu_err)	    \
179deae26bfSKyle McMartin 		: "r1")
180deae26bfSKyle McMartin 
181deae26bfSKyle McMartin 
182deae26bfSKyle McMartin #if !defined(CONFIG_64BIT)
183deae26bfSKyle McMartin 
184deae26bfSKyle McMartin #define __put_kernel_asm64(__val,ptr) do {		    \
185deae26bfSKyle McMartin 	__asm__ __volatile__ (				    \
186deae26bfSKyle McMartin 		"\n1:\tstw %2,0(%1)"			    \
1870f28b628SWill Deacon 		"\n2:\tstw %R2,4(%1)\n\t"		    \
188deae26bfSKyle McMartin 		ASM_EXCEPTIONTABLE_ENTRY(1b,fixup_put_user_skip_2)\
189deae26bfSKyle McMartin 		ASM_EXCEPTIONTABLE_ENTRY(2b,fixup_put_user_skip_1)\
190deae26bfSKyle McMartin 		: "=r"(__pu_err)                            \
1910f28b628SWill Deacon 		: "r"(ptr), "r"(__val), "0"(__pu_err) \
192deae26bfSKyle McMartin 		: "r1");				    \
193deae26bfSKyle McMartin } while (0)
194deae26bfSKyle McMartin 
195deae26bfSKyle McMartin #define __put_user_asm64(__val,ptr) do {	    	    \
196deae26bfSKyle McMartin 	__asm__ __volatile__ (				    \
197deae26bfSKyle McMartin 		"\n1:\tstw %2,0(%%sr3,%1)"		    \
1980f28b628SWill Deacon 		"\n2:\tstw %R2,4(%%sr3,%1)\n\t"		    \
199deae26bfSKyle McMartin 		ASM_EXCEPTIONTABLE_ENTRY(1b,fixup_put_user_skip_2)\
200deae26bfSKyle McMartin 		ASM_EXCEPTIONTABLE_ENTRY(2b,fixup_put_user_skip_1)\
201deae26bfSKyle McMartin 		: "=r"(__pu_err)                            \
2020f28b628SWill Deacon 		: "r"(ptr), "r"(__val), "0"(__pu_err) \
203deae26bfSKyle McMartin 		: "r1");				    \
204deae26bfSKyle McMartin } while (0)
205deae26bfSKyle McMartin 
206deae26bfSKyle McMartin #endif /* !defined(CONFIG_64BIT) */
207deae26bfSKyle McMartin 
208deae26bfSKyle McMartin 
209deae26bfSKyle McMartin /*
210deae26bfSKyle McMartin  * Complex access routines -- external declarations
211deae26bfSKyle McMartin  */
212deae26bfSKyle McMartin 
213deae26bfSKyle McMartin extern unsigned long lcopy_to_user(void __user *, const void *, unsigned long);
214deae26bfSKyle McMartin extern unsigned long lcopy_from_user(void *, const void __user *, unsigned long);
215deae26bfSKyle McMartin extern unsigned long lcopy_in_user(void __user *, const void __user *, unsigned long);
216b1195c0eSJames Bottomley extern long strncpy_from_user(char *, const char __user *, long);
217deae26bfSKyle McMartin extern unsigned lclear_user(void __user *,unsigned long);
218deae26bfSKyle McMartin extern long lstrnlen_user(const char __user *,long);
219deae26bfSKyle McMartin /*
220deae26bfSKyle McMartin  * Complex access routines -- macros
221deae26bfSKyle McMartin  */
222*a0ffa8f0SHelge Deller #define user_addr_max() (~0UL)
223deae26bfSKyle McMartin 
224deae26bfSKyle McMartin #define strnlen_user lstrnlen_user
225deae26bfSKyle McMartin #define strlen_user(str) lstrnlen_user(str, 0x7fffffffL)
226deae26bfSKyle McMartin #define clear_user lclear_user
227deae26bfSKyle McMartin #define __clear_user lclear_user
228deae26bfSKyle McMartin 
229deae26bfSKyle McMartin unsigned long copy_to_user(void __user *dst, const void *src, unsigned long len);
230deae26bfSKyle McMartin #define __copy_to_user copy_to_user
231888c31fcSHelge Deller unsigned long __copy_from_user(void *dst, const void __user *src, unsigned long len);
232deae26bfSKyle McMartin unsigned long copy_in_user(void __user *dst, const void __user *src, unsigned long len);
233deae26bfSKyle McMartin #define __copy_in_user copy_in_user
234deae26bfSKyle McMartin #define __copy_to_user_inatomic __copy_to_user
235deae26bfSKyle McMartin #define __copy_from_user_inatomic __copy_from_user
236deae26bfSKyle McMartin 
237888c31fcSHelge Deller extern void copy_from_user_overflow(void)
238888c31fcSHelge Deller #ifdef CONFIG_DEBUG_STRICT_USER_COPY_CHECKS
239888c31fcSHelge Deller         __compiletime_error("copy_from_user() buffer size is not provably correct")
240888c31fcSHelge Deller #else
241888c31fcSHelge Deller         __compiletime_warning("copy_from_user() buffer size is not provably correct")
242888c31fcSHelge Deller #endif
243888c31fcSHelge Deller ;
244888c31fcSHelge Deller 
245888c31fcSHelge Deller static inline unsigned long __must_check copy_from_user(void *to,
246888c31fcSHelge Deller                                           const void __user *from,
247888c31fcSHelge Deller                                           unsigned long n)
248888c31fcSHelge Deller {
249888c31fcSHelge Deller         int sz = __compiletime_object_size(to);
250888c31fcSHelge Deller         int ret = -EFAULT;
251888c31fcSHelge Deller 
252888c31fcSHelge Deller         if (likely(sz == -1 || !__builtin_constant_p(n) || sz >= n))
253888c31fcSHelge Deller                 ret = __copy_from_user(to, from, n);
254888c31fcSHelge Deller         else
255888c31fcSHelge Deller                 copy_from_user_overflow();
256888c31fcSHelge Deller 
257888c31fcSHelge Deller         return ret;
258888c31fcSHelge Deller }
259888c31fcSHelge Deller 
260e448372cSHelge Deller struct pt_regs;
261c61c25ebSKyle McMartin int fixup_exception(struct pt_regs *regs);
262c61c25ebSKyle McMartin 
263deae26bfSKyle McMartin #endif /* __PARISC_UACCESS_H */
264