uaccess.h 8.2 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8
#ifndef __PARISC_UACCESS_H
#define __PARISC_UACCESS_H

/*
 * User space memory access functions
 */
#include <asm/page.h>
#include <asm/cache.h>
9
#include <asm/errno.h>
10
#include <asm-generic/uaccess-unaligned.h>
L
Linus Torvalds 已提交
11

12
#include <linux/bug.h>
A
Al Viro 已提交
13
#include <linux/string.h>
14
#include <linux/thread_info.h>
15

L
Linus Torvalds 已提交
16 17 18 19 20 21
#define VERIFY_READ 0
#define VERIFY_WRITE 1

#define KERNEL_DS	((mm_segment_t){0})
#define USER_DS 	((mm_segment_t){1})

22
#define segment_eq(a, b) ((a).seg == (b).seg)
L
Linus Torvalds 已提交
23 24 25 26 27 28 29

#define get_ds()	(KERNEL_DS)
#define get_fs()	(current_thread_info()->addr_limit)
#define set_fs(x)	(current_thread_info()->addr_limit = (x))

/*
 * Note that since kernel addresses are in a separate address space on
J
Jesper Juhl 已提交
30
 * parisc, we don't need to do anything for access_ok().
L
Linus Torvalds 已提交
31 32 33 34
 * We just let the page fault handler do the right thing. This also means
 * that put_user is the same as __put_user, etc.
 */

35 36
#define access_ok(type, uaddr, size)	\
	( (uaddr) == (uaddr) )
L
Linus Torvalds 已提交
37 38 39 40

#define put_user __put_user
#define get_user __get_user

41
#if !defined(CONFIG_64BIT)
42
#define LDD_USER(ptr)		__get_user_asm64(ptr)
43
#define STD_USER(x, ptr)	__put_user_asm64(x, ptr)
L
Linus Torvalds 已提交
44
#else
45 46
#define LDD_USER(ptr)		__get_user_asm("ldd", ptr)
#define STD_USER(x, ptr)	__put_user_asm("std", x, ptr)
L
Linus Torvalds 已提交
47 48 49
#endif

/*
50 51 52 53
 * The exception table contains two values: the first is the relative offset to
 * the address of the instruction that is allowed to fault, and the second is
 * the relative offset to the address of the fixup routine. Since relative
 * addresses are used, 32bit values are sufficient even on 64bit kernel.
L
Linus Torvalds 已提交
54 55
 */

56
#define ARCH_HAS_RELATIVE_EXTABLE
L
Linus Torvalds 已提交
57
struct exception_table_entry {
58 59
	int insn;	/* relative address of insn that is allowed to fault. */
	int fixup;	/* relative address of fixup routine */
L
Linus Torvalds 已提交
60 61
};

62 63
#define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
	".section __ex_table,\"aw\"\n"			   \
64
	".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
65 66
	".previous\n"

67 68 69 70 71 72 73 74 75
/*
 * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
 * (with lowest bit set) for which the fault handler in fixup_exception() will
 * load -EFAULT into %r8 for a read or write fault, and zeroes the target
 * register in case of a read fault in get_user().
 */
#define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
	ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)

L
Linus Torvalds 已提交
76 77 78 79 80 81
/*
 * The page fault handler stores, in a per-cpu area, the following information
 * if a fixup routine is available.
 */
struct exception_data {
	unsigned long fault_ip;
82
	unsigned long fault_gp;
L
Linus Torvalds 已提交
83 84 85 86
	unsigned long fault_space;
	unsigned long fault_addr;
};

87 88 89 90 91 92 93 94 95 96 97 98 99
/*
 * load_sr2() preloads the space register %%sr2 - based on the value of
 * get_fs() - with either a value of 0 to access kernel space (KERNEL_DS which
 * is 0), or with the current value of %%sr3 to access user space (USER_DS)
 * memory. The following __get_user_asm() and __put_user_asm() functions have
 * %%sr2 hard-coded to access the requested memory.
 */
#define load_sr2() \
	__asm__(" or,=  %0,%%r0,%%r0\n\t"	\
		" mfsp %%sr3,%0\n\t"		\
		" mtsp %0,%%sr2\n\t"		\
		: : "r"(get_fs()) : )

100 101 102
#define __get_user(x, ptr)                               \
({                                                       \
	register long __gu_err __asm__ ("r8") = 0;       \
103
	register long __gu_val;				 \
104
							 \
105 106
	load_sr2();					 \
	switch (sizeof(*(ptr))) {			 \
107 108 109 110 111 112 113 114 115
	    case 1: __get_user_asm("ldb", ptr); break;   \
	    case 2: __get_user_asm("ldh", ptr); break;   \
	    case 4: __get_user_asm("ldw", ptr); break;   \
	    case 8: LDD_USER(ptr);  break;		 \
	    default: BUILD_BUG(); break;		 \
	}                                                \
							 \
	(x) = (__force __typeof__(*(ptr))) __gu_val;	 \
	__gu_err;                                        \
L
Linus Torvalds 已提交
116 117
})

118
#define __get_user_asm(ldx, ptr)                        \
119 120 121
	__asm__("1: " ldx " 0(%%sr2,%2),%0\n"		\
		"9:\n"					\
		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	\
L
Linus Torvalds 已提交
122
		: "=r"(__gu_val), "=r"(__gu_err)        \
123
		: "r"(ptr), "1"(__gu_err));
L
Linus Torvalds 已提交
124

125 126 127
#if !defined(CONFIG_64BIT)

#define __get_user_asm64(ptr) 				\
128 129 130 131 132 133
	__asm__("   copy %%r0,%R0\n"			\
		"1: ldw 0(%%sr2,%2),%0\n"		\
		"2: ldw 4(%%sr2,%2),%R0\n"		\
		"9:\n"					\
		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	\
		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)	\
134
		: "=r"(__gu_val), "=r"(__gu_err)	\
135
		: "r"(ptr), "1"(__gu_err));
136 137 138 139

#endif /* !defined(CONFIG_64BIT) */


140
#define __put_user(x, ptr)                                      \
L
Linus Torvalds 已提交
141 142 143 144
({								\
	register long __pu_err __asm__ ("r8") = 0;      	\
        __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x);	\
								\
145 146
	load_sr2();						\
	switch (sizeof(*(ptr))) {				\
147 148 149 150
	    case 1: __put_user_asm("stb", __x, ptr); break;     \
	    case 2: __put_user_asm("sth", __x, ptr); break;     \
	    case 4: __put_user_asm("stw", __x, ptr); break;     \
	    case 8: STD_USER(__x, ptr); break;			\
151
	    default: BUILD_BUG(); break;			\
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159
	}                                                       \
								\
	__pu_err;						\
})

/*
 * The "__put_user/kernel_asm()" macros tell gcc they read from memory
 * instead of writing. This is because they do not write to any memory
160
 * gcc knows about, so there are no aliasing issues. These macros must
161 162 163
 * also be aware that fixups are executed in the context of the fault,
 * and any registers used there must be listed as clobbers.
 * r8 is already listed as err.
L
Linus Torvalds 已提交
164 165
 */

166
#define __put_user_asm(stx, x, ptr)                         \
L
Linus Torvalds 已提交
167
	__asm__ __volatile__ (                              \
168 169 170
		"1: " stx " %2,0(%%sr2,%1)\n"		    \
		"9:\n"					    \
		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	    \
L
Linus Torvalds 已提交
171
		: "=r"(__pu_err)                            \
172
		: "r"(ptr), "r"(x), "0"(__pu_err))
L
Linus Torvalds 已提交
173 174


175
#if !defined(CONFIG_64BIT)
176

177
#define __put_user_asm64(__val, ptr) do {	    	    \
L
Linus Torvalds 已提交
178
	__asm__ __volatile__ (				    \
179 180 181 182 183
		"1: stw %2,0(%%sr2,%1)\n"		    \
		"2: stw %R2,4(%%sr2,%1)\n"		    \
		"9:\n"					    \
		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b)	    \
		ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b)	    \
L
Linus Torvalds 已提交
184
		: "=r"(__pu_err)                            \
185
		: "r"(ptr), "r"(__val), "0"(__pu_err));	    \
L
Linus Torvalds 已提交
186 187
} while (0)

188
#endif /* !defined(CONFIG_64BIT) */
L
Linus Torvalds 已提交
189 190 191 192 193 194 195 196 197


/*
 * Complex access routines -- external declarations
 */

extern unsigned long lcopy_to_user(void __user *, const void *, unsigned long);
extern unsigned long lcopy_from_user(void *, const void __user *, unsigned long);
extern unsigned long lcopy_in_user(void __user *, const void __user *, unsigned long);
198
extern long strncpy_from_user(char *, const char __user *, long);
199 200
extern unsigned lclear_user(void __user *, unsigned long);
extern long lstrnlen_user(const char __user *, long);
L
Linus Torvalds 已提交
201 202 203
/*
 * Complex access routines -- macros
 */
204
#define user_addr_max() (~0UL)
L
Linus Torvalds 已提交
205 206 207 208 209 210

#define strnlen_user lstrnlen_user
#define strlen_user(str) lstrnlen_user(str, 0x7fffffffL)
#define clear_user lclear_user
#define __clear_user lclear_user

211 212 213 214 215 216
unsigned long __must_check __copy_to_user(void __user *dst, const void *src,
					  unsigned long len);
unsigned long __must_check __copy_from_user(void *dst, const void __user *src,
					  unsigned long len);
unsigned long copy_in_user(void __user *dst, const void __user *src,
			   unsigned long len);
L
Linus Torvalds 已提交
217 218 219 220
#define __copy_in_user copy_in_user
#define __copy_to_user_inatomic __copy_to_user
#define __copy_from_user_inatomic __copy_from_user

221 222 223 224 225 226 227
extern void __compiletime_error("usercopy buffer size is too small")
__bad_copy_user(void);

static inline void copy_user_overflow(int size, unsigned long count)
{
	WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
}
228

229 230
static __always_inline unsigned long __must_check
copy_from_user(void *to, const void __user *from, unsigned long n)
231
{
232 233
	int sz = __compiletime_object_size(to);
	unsigned long ret = n;
234

235 236 237 238
	if (likely(sz < 0 || sz >= n)) {
		check_object_size(to, n, false);
		ret = __copy_from_user(to, from, n);
	} else if (!__builtin_constant_p(n))
239 240
		copy_user_overflow(sz, n);
	else
241
		__bad_copy_user();
242

A
Al Viro 已提交
243 244
	if (unlikely(ret))
		memset(to + (n - ret), 0, ret);
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

	return ret;
}

static __always_inline unsigned long __must_check
copy_to_user(void __user *to, const void *from, unsigned long n)
{
	int sz = __compiletime_object_size(from);

	if (likely(sz < 0 || sz >= n)) {
		check_object_size(from, n, true);
		n = __copy_to_user(to, from, n);
	} else if (!__builtin_constant_p(n))
		copy_user_overflow(sz, n);
	else
		__bad_copy_user();

	return n;
263 264
}

265
struct pt_regs;
266 267
int fixup_exception(struct pt_regs *regs);

L
Linus Torvalds 已提交
268
#endif /* __PARISC_UACCESS_H */