blob: 482847865dac26ffd890ca529be741cddea4e747 [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>
Al Viroaace8802016-08-20 19:03:37 -040013#include <linux/string.h>
Helge Deller8dd95c62014-10-21 21:29:09 +020014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#define VERIFY_READ 0
16#define VERIFY_WRITE 1
17
18#define KERNEL_DS ((mm_segment_t){0})
19#define USER_DS ((mm_segment_t){1})
20
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020021#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23#define get_ds() (KERNEL_DS)
24#define get_fs() (current_thread_info()->addr_limit)
25#define set_fs(x) (current_thread_info()->addr_limit = (x))
26
27/*
28 * Note that since kernel addresses are in a separate address space on
Jesper Juhle49332b2005-05-01 08:59:08 -070029 * parisc, we don't need to do anything for access_ok().
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * We just let the page fault handler do the right thing. This also means
31 * that put_user is the same as __put_user, etc.
32 */
33
Helge Dellera0ffa8f2013-11-19 23:31:35 +010034static inline long access_ok(int type, const void __user * addr,
35 unsigned long size)
Linus Torvalds1da177e2005-04-16 15:20:36 -070036{
Helge Dellera0ffa8f2013-11-19 23:31:35 +010037 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#define put_user __put_user
41#define get_user __get_user
42
Helge Dellerca72a222006-12-20 00:35:57 +010043#if !defined(CONFIG_64BIT)
Helge Dellerd2ad8242016-04-09 08:27:08 +020044#define LDD_USER(ptr) __get_user_asm64(ptr)
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020045#define STD_USER(x, ptr) __put_user_asm64(x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#else
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020047#define LDD_USER(ptr) __get_user_asm("ldd", ptr)
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020048#define STD_USER(x, ptr) __put_user_asm("std", x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#endif
50
51/*
Helge Dellercb910c12016-04-08 21:36:06 +020052 * The exception table contains two values: the first is the relative offset to
53 * the address of the instruction that is allowed to fault, and the second is
54 * the relative offset to the address of the fixup routine. Since relative
55 * addresses are used, 32bit values are sufficient even on 64bit kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 */
57
Helge Deller0de79852016-03-23 16:00:46 +010058#define ARCH_HAS_RELATIVE_EXTABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070059struct exception_table_entry {
Helge Deller0de79852016-03-23 16:00:46 +010060 int insn; /* relative address of insn that is allowed to fault. */
61 int fixup; /* relative address of fixup routine */
Linus Torvalds1da177e2005-04-16 15:20:36 -070062};
63
Helge Deller0b3d6432007-01-28 14:52:57 +010064#define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
65 ".section __ex_table,\"aw\"\n" \
Helge Deller0de79852016-03-23 16:00:46 +010066 ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
Helge Deller0b3d6432007-01-28 14:52:57 +010067 ".previous\n"
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069/*
70 * The page fault handler stores, in a per-cpu area, the following information
71 * if a fixup routine is available.
72 */
73struct exception_data {
74 unsigned long fault_ip;
Helge Deller2ef4dfd2016-04-08 18:32:52 +020075 unsigned long fault_gp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 unsigned long fault_space;
77 unsigned long fault_addr;
78};
79
Helge Deller06bff6b2016-04-09 08:26:14 +020080/*
81 * load_sr2() preloads the space register %%sr2 - based on the value of
82 * get_fs() - with either a value of 0 to access kernel space (KERNEL_DS which
83 * is 0), or with the current value of %%sr3 to access user space (USER_DS)
84 * memory. The following __get_user_asm() and __put_user_asm() functions have
85 * %%sr2 hard-coded to access the requested memory.
86 */
87#define load_sr2() \
88 __asm__(" or,= %0,%%r0,%%r0\n\t" \
89 " mfsp %%sr3,%0\n\t" \
90 " mtsp %0,%%sr2\n\t" \
91 : : "r"(get_fs()) : )
92
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020093#define __get_user(x, ptr) \
94({ \
95 register long __gu_err __asm__ ("r8") = 0; \
96 register long __gu_val __asm__ ("r9") = 0; \
97 \
Helge Deller06bff6b2016-04-09 08:26:14 +020098 load_sr2(); \
99 switch (sizeof(*(ptr))) { \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200100 case 1: __get_user_asm("ldb", ptr); break; \
101 case 2: __get_user_asm("ldh", ptr); break; \
102 case 4: __get_user_asm("ldw", ptr); break; \
103 case 8: LDD_USER(ptr); break; \
104 default: BUILD_BUG(); break; \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200105 } \
106 \
107 (x) = (__force __typeof__(*(ptr))) __gu_val; \
108 __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109})
110
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200111#define __get_user_asm(ldx, ptr) \
Helge Deller06bff6b2016-04-09 08:26:14 +0200112 __asm__("\n1:\t" ldx "\t0(%%sr2,%2),%0\n\t" \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200113 ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_1)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 : "=r"(__gu_val), "=r"(__gu_err) \
115 : "r"(ptr), "1"(__gu_err) \
116 : "r1");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Helge Dellerd2ad8242016-04-09 08:27:08 +0200118#if !defined(CONFIG_64BIT)
119
120#define __get_user_asm64(ptr) \
121 __asm__("\n1:\tldw 0(%%sr2,%2),%0" \
122 "\n2:\tldw 4(%%sr2,%2),%R0\n\t" \
123 ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_get_user_skip_2)\
124 ASM_EXCEPTIONTABLE_ENTRY(2b, fixup_get_user_skip_1)\
125 : "=r"(__gu_val), "=r"(__gu_err) \
126 : "r"(ptr), "1"(__gu_err) \
127 : "r1");
128
129#endif /* !defined(CONFIG_64BIT) */
130
131
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200132#define __put_user(x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133({ \
134 register long __pu_err __asm__ ("r8") = 0; \
135 __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \
136 \
Helge Deller06bff6b2016-04-09 08:26:14 +0200137 load_sr2(); \
138 switch (sizeof(*(ptr))) { \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200139 case 1: __put_user_asm("stb", __x, ptr); break; \
140 case 2: __put_user_asm("sth", __x, ptr); break; \
141 case 4: __put_user_asm("stw", __x, ptr); break; \
142 case 8: STD_USER(__x, ptr); break; \
Helge Deller8dd95c62014-10-21 21:29:09 +0200143 default: BUILD_BUG(); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 } \
145 \
146 __pu_err; \
147})
148
149/*
150 * The "__put_user/kernel_asm()" macros tell gcc they read from memory
151 * instead of writing. This is because they do not write to any memory
Carlos O'Donell3fd3a742006-04-22 14:47:21 -0600152 * gcc knows about, so there are no aliasing issues. These macros must
153 * also be aware that "fixup_put_user_skip_[12]" are executed in the
154 * context of the fault, and any registers used there must be listed
155 * as clobbers. In this case only "r1" is used by the current routines.
156 * r8/r9 are already listed as err/val.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
158
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200159#define __put_user_asm(stx, x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 __asm__ __volatile__ ( \
Helge Deller06bff6b2016-04-09 08:26:14 +0200161 "\n1:\t" stx "\t%2,0(%%sr2,%1)\n\t" \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200162 ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_1)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 : "=r"(__pu_err) \
164 : "r"(ptr), "r"(x), "0"(__pu_err) \
165 : "r1")
166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Helge Dellerca72a222006-12-20 00:35:57 +0100168#if !defined(CONFIG_64BIT)
Helge Deller94a19812006-12-19 22:33:58 +0100169
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200170#define __put_user_asm64(__val, ptr) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 __asm__ __volatile__ ( \
Helge Deller06bff6b2016-04-09 08:26:14 +0200172 "\n1:\tstw %2,0(%%sr2,%1)" \
173 "\n2:\tstw %R2,4(%%sr2,%1)\n\t" \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200174 ASM_EXCEPTIONTABLE_ENTRY(1b, fixup_put_user_skip_2)\
175 ASM_EXCEPTIONTABLE_ENTRY(2b, fixup_put_user_skip_1)\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 : "=r"(__pu_err) \
Will Deacon0f28b622013-04-22 12:53:43 +0000177 : "r"(ptr), "r"(__val), "0"(__pu_err) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 : "r1"); \
179} while (0)
180
Helge Dellerca72a222006-12-20 00:35:57 +0100181#endif /* !defined(CONFIG_64BIT) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183
184/*
185 * Complex access routines -- external declarations
186 */
187
188extern unsigned long lcopy_to_user(void __user *, const void *, unsigned long);
189extern unsigned long lcopy_from_user(void *, const void __user *, unsigned long);
190extern unsigned long lcopy_in_user(void __user *, const void __user *, unsigned long);
James Bottomleyb1195c02012-05-26 09:48:19 +0100191extern long strncpy_from_user(char *, const char __user *, long);
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200192extern unsigned lclear_user(void __user *, unsigned long);
193extern long lstrnlen_user(const char __user *, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*
195 * Complex access routines -- macros
196 */
Helge Dellera0ffa8f2013-11-19 23:31:35 +0100197#define user_addr_max() (~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199#define strnlen_user lstrnlen_user
200#define strlen_user(str) lstrnlen_user(str, 0x7fffffffL)
201#define clear_user lclear_user
202#define __clear_user lclear_user
203
204unsigned long copy_to_user(void __user *dst, const void *src, unsigned long len);
205#define __copy_to_user copy_to_user
Helge Deller888c31f2010-02-01 19:56:33 +0000206unsigned long __copy_from_user(void *dst, const void __user *src, unsigned long len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207unsigned long copy_in_user(void __user *dst, const void __user *src, unsigned long len);
208#define __copy_in_user copy_in_user
209#define __copy_to_user_inatomic __copy_to_user
210#define __copy_from_user_inatomic __copy_from_user
211
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500212extern void __compiletime_error("usercopy buffer size is too small")
213__bad_copy_user(void);
214
215static inline void copy_user_overflow(int size, unsigned long count)
216{
217 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
218}
Helge Deller888c31f2010-02-01 19:56:33 +0000219
220static inline unsigned long __must_check copy_from_user(void *to,
221 const void __user *from,
222 unsigned long n)
223{
224 int sz = __compiletime_object_size(to);
Al Viroaace8802016-08-20 19:03:37 -0400225 unsigned long ret = n;
Helge Deller888c31f2010-02-01 19:56:33 +0000226
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500227 if (likely(sz == -1 || sz >= n))
Helge Deller888c31f2010-02-01 19:56:33 +0000228 ret = __copy_from_user(to, from, n);
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500229 else if (!__builtin_constant_p(n))
230 copy_user_overflow(sz, n);
231 else
232 __bad_copy_user();
Helge Deller888c31f2010-02-01 19:56:33 +0000233
Al Viroaace8802016-08-20 19:03:37 -0400234 if (unlikely(ret))
235 memset(to + (n - ret), 0, ret);
Helge Deller888c31f2010-02-01 19:56:33 +0000236 return ret;
237}
238
Helge Dellere4483722009-01-13 20:52:46 +0100239struct pt_regs;
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000240int fixup_exception(struct pt_regs *regs);
241
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#endif /* __PARISC_UACCESS_H */