blob: 8442727f28d2732eecb583d46bdcc88258a590dd [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 Deller9e91db62016-10-06 09:07:30 +020014#include <linux/thread_info.h>
Helge Deller8dd95c62014-10-21 21:29:09 +020015
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#define VERIFY_READ 0
17#define VERIFY_WRITE 1
18
19#define KERNEL_DS ((mm_segment_t){0})
20#define USER_DS ((mm_segment_t){1})
21
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020022#define segment_eq(a, b) ((a).seg == (b).seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#define get_ds() (KERNEL_DS)
25#define get_fs() (current_thread_info()->addr_limit)
26#define set_fs(x) (current_thread_info()->addr_limit = (x))
27
28/*
29 * Note that since kernel addresses are in a separate address space on
Jesper Juhle49332b2005-05-01 08:59:08 -070030 * parisc, we don't need to do anything for access_ok().
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * We just let the page fault handler do the right thing. This also means
32 * that put_user is the same as __put_user, etc.
33 */
34
Helge Deller186ecf12017-03-15 21:48:42 +010035#define access_ok(type, uaddr, size) \
36 ( (uaddr) == (uaddr) )
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#define put_user __put_user
39#define get_user __get_user
40
Helge Dellerca72a222006-12-20 00:35:57 +010041#if !defined(CONFIG_64BIT)
Helge Dellerd2ad8242016-04-09 08:27:08 +020042#define LDD_USER(ptr) __get_user_asm64(ptr)
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020043#define STD_USER(x, ptr) __put_user_asm64(x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#else
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020045#define LDD_USER(ptr) __get_user_asm("ldd", ptr)
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020046#define STD_USER(x, ptr) __put_user_asm("std", x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#endif
48
49/*
Helge Dellercb910c12016-04-08 21:36:06 +020050 * The exception table contains two values: the first is the relative offset to
51 * the address of the instruction that is allowed to fault, and the second is
52 * the relative offset to the address of the fixup routine. Since relative
53 * addresses are used, 32bit values are sufficient even on 64bit kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 */
55
Helge Deller0de79852016-03-23 16:00:46 +010056#define ARCH_HAS_RELATIVE_EXTABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070057struct exception_table_entry {
Helge Deller0de79852016-03-23 16:00:46 +010058 int insn; /* relative address of insn that is allowed to fault. */
59 int fixup; /* relative address of fixup routine */
Linus Torvalds1da177e2005-04-16 15:20:36 -070060};
61
Helge Deller0b3d6432007-01-28 14:52:57 +010062#define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
63 ".section __ex_table,\"aw\"\n" \
Helge Deller0de79852016-03-23 16:00:46 +010064 ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
Helge Deller0b3d6432007-01-28 14:52:57 +010065 ".previous\n"
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067/*
Helge Dellerd19f5e42017-03-25 11:59:15 +010068 * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
69 * (with lowest bit set) for which the fault handler in fixup_exception() will
70 * load -EFAULT into %r8 for a read or write fault, and zeroes the target
71 * register in case of a read fault in get_user().
72 */
73#define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
74 ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)
75
76/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 * The page fault handler stores, in a per-cpu area, the following information
78 * if a fixup routine is available.
79 */
80struct exception_data {
81 unsigned long fault_ip;
Helge Deller2ef4dfd2016-04-08 18:32:52 +020082 unsigned long fault_gp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 unsigned long fault_space;
84 unsigned long fault_addr;
85};
86
Helge Deller06bff6b2016-04-09 08:26:14 +020087/*
88 * load_sr2() preloads the space register %%sr2 - based on the value of
89 * get_fs() - with either a value of 0 to access kernel space (KERNEL_DS which
90 * is 0), or with the current value of %%sr3 to access user space (USER_DS)
91 * memory. The following __get_user_asm() and __put_user_asm() functions have
92 * %%sr2 hard-coded to access the requested memory.
93 */
94#define load_sr2() \
95 __asm__(" or,= %0,%%r0,%%r0\n\t" \
96 " mfsp %%sr3,%0\n\t" \
97 " mtsp %0,%%sr2\n\t" \
98 : : "r"(get_fs()) : )
99
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200100#define __get_user(x, ptr) \
101({ \
102 register long __gu_err __asm__ ("r8") = 0; \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100103 register long __gu_val; \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200104 \
Helge Deller06bff6b2016-04-09 08:26:14 +0200105 load_sr2(); \
106 switch (sizeof(*(ptr))) { \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200107 case 1: __get_user_asm("ldb", ptr); break; \
108 case 2: __get_user_asm("ldh", ptr); break; \
109 case 4: __get_user_asm("ldw", ptr); break; \
110 case 8: LDD_USER(ptr); break; \
111 default: BUILD_BUG(); break; \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200112 } \
113 \
114 (x) = (__force __typeof__(*(ptr))) __gu_val; \
115 __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116})
117
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200118#define __get_user_asm(ldx, ptr) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100119 __asm__("1: " ldx " 0(%%sr2,%2),%0\n" \
120 "9:\n" \
121 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 : "=r"(__gu_val), "=r"(__gu_err) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100123 : "r"(ptr), "1"(__gu_err));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Helge Dellerd2ad8242016-04-09 08:27:08 +0200125#if !defined(CONFIG_64BIT)
126
127#define __get_user_asm64(ptr) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100128 __asm__(" copy %%r0,%R0\n" \
129 "1: ldw 0(%%sr2,%2),%0\n" \
130 "2: ldw 4(%%sr2,%2),%R0\n" \
131 "9:\n" \
132 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
133 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
Helge Dellerd2ad8242016-04-09 08:27:08 +0200134 : "=r"(__gu_val), "=r"(__gu_err) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100135 : "r"(ptr), "1"(__gu_err));
Helge Dellerd2ad8242016-04-09 08:27:08 +0200136
137#endif /* !defined(CONFIG_64BIT) */
138
139
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200140#define __put_user(x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141({ \
142 register long __pu_err __asm__ ("r8") = 0; \
143 __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \
144 \
Helge Deller06bff6b2016-04-09 08:26:14 +0200145 load_sr2(); \
146 switch (sizeof(*(ptr))) { \
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200147 case 1: __put_user_asm("stb", __x, ptr); break; \
148 case 2: __put_user_asm("sth", __x, ptr); break; \
149 case 4: __put_user_asm("stw", __x, ptr); break; \
150 case 8: STD_USER(__x, ptr); break; \
Helge Deller8dd95c62014-10-21 21:29:09 +0200151 default: BUILD_BUG(); break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 } \
153 \
154 __pu_err; \
155})
156
157/*
158 * The "__put_user/kernel_asm()" macros tell gcc they read from memory
159 * instead of writing. This is because they do not write to any memory
Carlos O'Donell3fd3a742006-04-22 14:47:21 -0600160 * gcc knows about, so there are no aliasing issues. These macros must
Helge Dellerd19f5e42017-03-25 11:59:15 +0100161 * also be aware that fixups are executed in the context of the fault,
162 * and any registers used there must be listed as clobbers.
163 * r8 is already listed as err.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 */
165
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200166#define __put_user_asm(stx, x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 __asm__ __volatile__ ( \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100168 "1: " stx " %2,0(%%sr2,%1)\n" \
169 "9:\n" \
170 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 : "=r"(__pu_err) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100172 : "r"(ptr), "r"(x), "0"(__pu_err))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
Helge Dellerca72a222006-12-20 00:35:57 +0100175#if !defined(CONFIG_64BIT)
Helge Deller94a19812006-12-19 22:33:58 +0100176
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200177#define __put_user_asm64(__val, ptr) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 __asm__ __volatile__ ( \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100179 "1: stw %2,0(%%sr2,%1)\n" \
180 "2: stw %R2,4(%%sr2,%1)\n" \
181 "9:\n" \
182 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
183 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 : "=r"(__pu_err) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100185 : "r"(ptr), "r"(__val), "0"(__pu_err)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186} while (0)
187
Helge Dellerca72a222006-12-20 00:35:57 +0100188#endif /* !defined(CONFIG_64BIT) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189
190
191/*
192 * Complex access routines -- external declarations
193 */
194
195extern unsigned long lcopy_to_user(void __user *, const void *, unsigned long);
196extern unsigned long lcopy_from_user(void *, const void __user *, unsigned long);
197extern unsigned long lcopy_in_user(void __user *, const void __user *, unsigned long);
James Bottomleyb1195c02012-05-26 09:48:19 +0100198extern long strncpy_from_user(char *, const char __user *, long);
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200199extern unsigned lclear_user(void __user *, unsigned long);
200extern long lstrnlen_user(const char __user *, long);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201/*
202 * Complex access routines -- macros
203 */
Helge Dellera0ffa8f2013-11-19 23:31:35 +0100204#define user_addr_max() (~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206#define strnlen_user lstrnlen_user
207#define strlen_user(str) lstrnlen_user(str, 0x7fffffffL)
208#define clear_user lclear_user
209#define __clear_user lclear_user
210
Helge Deller9e91db62016-10-06 09:07:30 +0200211unsigned long __must_check __copy_to_user(void __user *dst, const void *src,
212 unsigned long len);
213unsigned long __must_check __copy_from_user(void *dst, const void __user *src,
214 unsigned long len);
215unsigned long copy_in_user(void __user *dst, const void __user *src,
216 unsigned long len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217#define __copy_in_user copy_in_user
218#define __copy_to_user_inatomic __copy_to_user
219#define __copy_from_user_inatomic __copy_from_user
220
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500221extern void __compiletime_error("usercopy buffer size is too small")
222__bad_copy_user(void);
223
224static inline void copy_user_overflow(int size, unsigned long count)
225{
226 WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
227}
Helge Deller888c31f2010-02-01 19:56:33 +0000228
Helge Deller9e91db62016-10-06 09:07:30 +0200229static __always_inline unsigned long __must_check
230copy_from_user(void *to, const void __user *from, unsigned long n)
Helge Deller888c31f2010-02-01 19:56:33 +0000231{
Helge Deller9e91db62016-10-06 09:07:30 +0200232 int sz = __compiletime_object_size(to);
233 unsigned long ret = n;
Helge Deller888c31f2010-02-01 19:56:33 +0000234
Helge Deller9e91db62016-10-06 09:07:30 +0200235 if (likely(sz < 0 || sz >= n)) {
236 check_object_size(to, n, false);
237 ret = __copy_from_user(to, from, n);
238 } else if (!__builtin_constant_p(n))
Josh Poimboeuf0d025d22016-08-30 08:04:16 -0500239 copy_user_overflow(sz, n);
240 else
Helge Deller9e91db62016-10-06 09:07:30 +0200241 __bad_copy_user();
Helge Deller888c31f2010-02-01 19:56:33 +0000242
Al Viroaace8802016-08-20 19:03:37 -0400243 if (unlikely(ret))
244 memset(to + (n - ret), 0, ret);
Helge Deller9e91db62016-10-06 09:07:30 +0200245
246 return ret;
247}
248
249static __always_inline unsigned long __must_check
250copy_to_user(void __user *to, const void *from, unsigned long n)
251{
252 int sz = __compiletime_object_size(from);
253
254 if (likely(sz < 0 || sz >= n)) {
255 check_object_size(from, n, true);
256 n = __copy_to_user(to, from, n);
257 } else if (!__builtin_constant_p(n))
258 copy_user_overflow(sz, n);
259 else
260 __bad_copy_user();
261
262 return n;
Helge Deller888c31f2010-02-01 19:56:33 +0000263}
264
Helge Dellere4483722009-01-13 20:52:46 +0100265struct pt_regs;
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000266int fixup_exception(struct pt_regs *regs);
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268#endif /* __PARISC_UACCESS_H */