blob: e9150487e20dbb2832f5bc703ce209943471f6b1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef __PARISC_UACCESS_H
2#define __PARISC_UACCESS_H
3
4/*
5 * User space memory access functions
6 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <asm/cache.h>
Helge Deller888c31f2010-02-01 19:56:33 +00009#include <asm/errno.h>
Arnd Bergmann5b17e1c2009-05-13 22:56:30 +000010#include <asm-generic/uaccess-unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Helge Deller8dd95c62014-10-21 21:29:09 +020012#include <linux/bug.h>
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#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. Tsirkinb9762e72015-01-06 17:45:34 +020020#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
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 Juhle49332b2005-05-01 08:59:08 -070028 * parisc, we don't need to do anything for access_ok().
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 * 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 Dellera0ffa8f2013-11-19 23:31:35 +010033static inline long access_ok(int type, const void __user * addr,
34 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Helge Dellera0ffa8f2013-11-19 23:31:35 +010036 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037}
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#define put_user __put_user
40#define get_user __get_user
41
Helge Dellerca72a222006-12-20 00:35:57 +010042#if !defined(CONFIG_64BIT)
Helge Dellerd2ad8242016-04-09 08:27:08 +020043#define LDD_USER(ptr) __get_user_asm64(ptr)
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020044#define STD_USER(x, ptr) __put_user_asm64(x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#else
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020046#define LDD_USER(ptr) __get_user_asm("ldd", ptr)
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020047#define STD_USER(x, ptr) __put_user_asm("std", x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#endif
49
50/*
Helge Dellercb910c12016-04-08 21:36:06 +020051 * The exception table contains two values: the first is the relative offset to
52 * the address of the instruction that is allowed to fault, and the second is
53 * the relative offset to the address of the fixup routine. Since relative
54 * addresses are used, 32bit values are sufficient even on 64bit kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 */
56
Helge Deller0de79852016-03-23 16:00:46 +010057#define ARCH_HAS_RELATIVE_EXTABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070058struct exception_table_entry {
Helge Deller0de79852016-03-23 16:00:46 +010059 int insn; /* relative address of insn that is allowed to fault. */
60 int fixup; /* relative address of fixup routine */
Linus Torvalds1da177e2005-04-16 15:20:36 -070061};
62
Helge Deller0b3d6432007-01-28 14:52:57 +010063#define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
64 ".section __ex_table,\"aw\"\n" \
Helge Deller0de79852016-03-23 16:00:46 +010065 ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
Helge Deller0b3d6432007-01-28 14:52:57 +010066 ".previous\n"
67
Linus Torvalds1da177e2005-04-16 15:20:36 -070068/*
69 * The page fault handler stores, in a per-cpu area, the following information
70 * if a fixup routine is available.
71 */
72struct exception_data {
73 unsigned long fault_ip;
Helge Deller2ef4dfd2016-04-08 18:32:52 +020074 unsigned long fault_gp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 unsigned long fault_space;
76 unsigned long fault_addr;
77};
78
Helge Deller06bff6b2016-04-09 08:26:14 +020079/*
80 * load_sr2() preloads the space register %%sr2 - based on the value of
81 * get_fs() - with either a value of 0 to access kernel space (KERNEL_DS which
82 * is 0), or with the current value of %%sr3 to access user space (USER_DS)
83 * memory. The following __get_user_asm() and __put_user_asm() functions have
84 * %%sr2 hard-coded to access the requested memory.
85 */
86#define load_sr2() \
87 __asm__(" or,= %0,%%r0,%%r0\n\t" \
88 " mfsp %%sr3,%0\n\t" \
89 " mtsp %0,%%sr2\n\t" \
90 : : "r"(get_fs()) : )
91
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020092#define __get_user(x, ptr) \
93({ \
94 register long __gu_err __asm__ ("r8") = 0; \
95 register long __gu_val __asm__ ("r9") = 0; \
96 \
Helge Deller06bff6b2016-04-09 08:26:14 +020097 load_sr2(); \
98 switch (sizeof(*(ptr))) { \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020099 case 1: __get_user_asm("ldb", ptr); break; \
100 case 2: __get_user_asm("ldh", ptr); break; \
101 case 4: __get_user_asm("ldw", ptr); break; \
102 case 8: LDD_USER(ptr); break; \
103 default: BUILD_BUG(); break; \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200104 } \
105 \
106 (x) = (__force __typeof__(*(ptr))) __gu_val; \
107 __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108})
109
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200110#define __get_user_asm(ldx, ptr) \
Helge Deller06bff6b2016-04-09 08:26:14 +0200111 __asm__("\n1:\t" ldx "\t0(%%sr2,%2),%0\n\t" \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200112 ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_1)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 : "=r"(__gu_val), "=r"(__gu_err) \
114 : "r"(ptr), "1"(__gu_err) \
115 : "r1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Helge Dellerd2ad8242016-04-09 08:27:08 +0200117#if !defined(CONFIG_64BIT)
118
119#define __get_user_asm64(ptr) \
120 __asm__("\n1:\tldw 0(%%sr2,%2),%0" \
121 "\n2:\tldw 4(%%sr2,%2),%R0\n\t" \
122 ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_2)\
123 ASM_EXCEPTIONTABLE_ENTRY(2b, fixup_get_user_skip_1)\
124 : "=r"(__gu_val), "=r"(__gu_err) \
125 : "r"(ptr), "1"(__gu_err) \
126 : "r1");
127
128#endif /* !defined(CONFIG_64BIT) */
129
130
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200131#define __put_user(x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132({ \
133 register long __pu_err __asm__ ("r8") = 0; \
134 __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \
135 \
Helge Deller06bff6b2016-04-09 08:26:14 +0200136 load_sr2(); \
137 switch (sizeof(*(ptr))) { \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200138 case 1: __put_user_asm("stb", __x, ptr); break; \
139 case 2: __put_user_asm("sth", __x, ptr); break; \
140 case 4: __put_user_asm("stw", __x, ptr); break; \
141 case 8: STD_USER(__x, ptr); break; \
Helge Deller8dd95c62014-10-21 21:29:09 +0200142 default: BUILD_BUG(); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 } \
144 \
145 __pu_err; \
146})
147
148/*
149 * The "__put_user/kernel_asm()" macros tell gcc they read from memory
150 * instead of writing. This is because they do not write to any memory
Carlos O'Donell3fd3a742006-04-22 14:47:21 -0600151 * gcc knows about, so there are no aliasing issues. These macros must
152 * also be aware that "fixup_put_user_skip_[12]" are executed in the
153 * context of the fault, and any registers used there must be listed
154 * as clobbers. In this case only "r1" is used by the current routines.
155 * r8/r9 are already listed as err/val.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 */
157
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200158#define __put_user_asm(stx, x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 __asm__ __volatile__ ( \
Helge Deller06bff6b2016-04-09 08:26:14 +0200160 "\n1:\t" stx "\t%2,0(%%sr2,%1)\n\t" \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200161 ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_1)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 : "=r"(__pu_err) \
163 : "r"(ptr), "r"(x), "0"(__pu_err) \
164 : "r1")
165
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Helge Dellerca72a222006-12-20 00:35:57 +0100167#if !defined(CONFIG_64BIT)
Helge Deller94a19812006-12-19 22:33:58 +0100168
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200169#define __put_user_asm64(__val, ptr) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 __asm__ __volatile__ ( \
Helge Deller06bff6b2016-04-09 08:26:14 +0200171 "\n1:\tstw %2,0(%%sr2,%1)" \
172 "\n2:\tstw %R2,4(%%sr2,%1)\n\t" \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200173 ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_2)\
174 ASM_EXCEPTIONTABLE_ENTRY(2b, fixup_put_user_skip_1)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 : "=r"(__pu_err) \
Will Deacon0f28b622013-04-22 12:53:43 +0000176 : "r"(ptr), "r"(__val), "0"(__pu_err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 : "r1"); \
178} while (0)
179
Helge Dellerca72a222006-12-20 00:35:57 +0100180#endif /* !defined(CONFIG_64BIT) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182
183/*
184 * Complex access routines -- external declarations
185 */
186
187extern unsigned long lcopy_to_user(void __user *, const void *, unsigned long);
188extern unsigned long lcopy_from_user(void *, const void __user *, unsigned long);
189extern unsigned long lcopy_in_user(void __user *, const void __user *, unsigned long);
James Bottomleyb1195c02012-05-26 09:48:19 +0100190extern long strncpy_from_user(char *, const char __user *, long);
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200191extern unsigned lclear_user(void __user *, unsigned long);
192extern long lstrnlen_user(const char __user *, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*
194 * Complex access routines -- macros
195 */
Helge Dellera0ffa8f2013-11-19 23:31:35 +0100196#define user_addr_max() (~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198#define strnlen_user lstrnlen_user
199#define strlen_user(str) lstrnlen_user(str, 0x7fffffffL)
200#define clear_user lclear_user
201#define __clear_user lclear_user
202
203unsigned long copy_to_user(void __user *dst, const void *src, unsigned long len);
204#define __copy_to_user copy_to_user
Helge Deller888c31f2010-02-01 19:56:33 +0000205unsigned long __copy_from_user(void *dst, const void __user *src, unsigned long len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206unsigned long copy_in_user(void __user *dst, const void __user *src, unsigned long len);
207#define __copy_in_user copy_in_user
208#define __copy_to_user_inatomic __copy_to_user
209#define __copy_from_user_inatomic __copy_from_user
210
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500211extern void __compiletime_error("usercopy buffer size is too small")
212__bad_copy_user(void);
213
214static inline void copy_user_overflow(int size, unsigned long count)
215{
216 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
217}
Helge Deller888c31f2010-02-01 19:56:33 +0000218
219static inline unsigned long __must_check copy_from_user(void *to,
220 const void __user *from,
221 unsigned long n)
222{
223 int sz = __compiletime_object_size(to);
224 int ret = -EFAULT;
225
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500226 if (likely(sz == -1 || sz >= n))
Helge Deller888c31f2010-02-01 19:56:33 +0000227 ret = __copy_from_user(to, from, n);
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500228 else if (!__builtin_constant_p(n))
229 copy_user_overflow(sz, n);
230 else
231 __bad_copy_user();
Helge Deller888c31f2010-02-01 19:56:33 +0000232
233 return ret;
234}
235
Helge Dellere4483722009-01-13 20:52:46 +0100236struct pt_regs;
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000237int fixup_exception(struct pt_regs *regs);
238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239#endif /* __PARISC_UACCESS_H */