uaccess.h 10.1 KB
Newer Older
M
Michal Simek 已提交
1
/*
2 3
 * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
 * Copyright (C) 2008-2009 PetaLogix
M
Michal Simek 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * Copyright (C) 2006 Atmark Techno, Inc.
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License. See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#ifndef _ASM_MICROBLAZE_UACCESS_H
#define _ASM_MICROBLAZE_UACCESS_H

#ifdef __KERNEL__
#ifndef __ASSEMBLY__

#include <linux/kernel.h>
#include <linux/errno.h>
#include <linux/sched.h> /* RLIMIT_FSIZE */
#include <linux/mm.h>

#include <asm/mmu.h>
#include <asm/page.h>
#include <asm/pgtable.h>
#include <linux/string.h>

#define VERIFY_READ	0
#define VERIFY_WRITE	1

M
Michal Simek 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/*
 * On Microblaze the fs value is actually the top of the corresponding
 * address space.
 *
 * The fs value determines whether argument validity checking should be
 * performed or not. If get_fs() == USER_DS, checking is performed, with
 * get_fs() == KERNEL_DS, checking is bypassed.
 *
 * For historical reasons, these macros are grossly misnamed.
 *
 * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal.
 */
# define MAKE_MM_SEG(s)       ((mm_segment_t) { (s) })

#  ifndef CONFIG_MMU
#  define KERNEL_DS	MAKE_MM_SEG(0)
#  define USER_DS	KERNEL_DS
#  else
#  define KERNEL_DS	MAKE_MM_SEG(0xFFFFFFFF)
#  define USER_DS	MAKE_MM_SEG(TASK_SIZE - 1)
#  endif

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

# define segment_eq(a, b)	((a).seg == (b).seg)

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
/*
 * The exception table consists of pairs of addresses: the first is the
 * address of an instruction that is allowed to fault, and the second is
 * the address at which the program should continue. No registers are
 * modified, so it is entirely up to the continuation code to figure out
 * what to do.
 *
 * All the routines below use bits of fixup code that are out of line
 * with the main instruction path. This means when everything is well,
 * we don't even have to jump over them. Further, they do not intrude
 * on our cache or tlb entries.
 */
struct exception_table_entry {
	unsigned long insn, fixup;
};
M
Michal Simek 已提交
73

74 75
#ifndef CONFIG_MMU

76 77 78 79 80 81
/* Check against bounds of physical memory */
static inline int ___range_ok(unsigned long addr, unsigned long size)
{
	return ((addr < memory_start) ||
		((addr + size) > memory_end));
}
M
Michal Simek 已提交
82 83 84 85 86

#define __range_ok(addr, size) \
		___range_ok((unsigned long)(addr), (unsigned long)(size))

#define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

#else

/*
 * Address is valid if:
 *  - "addr", "addr + size" and "size" are all below the limit
 */
#define access_ok(type, addr, size) \
	(get_fs().seg > (((unsigned long)(addr)) | \
		(size) | ((unsigned long)(addr) + (size))))

/* || printk("access_ok failed for %s at 0x%08lx (size %d), seg 0x%08x\n",
 type?"WRITE":"READ",addr,size,get_fs().seg)) */

#endif

#ifdef CONFIG_MMU
# define __FIXUP_SECTION	".section .fixup,\"ax\"\n"
# define __EX_TABLE_SECTION	".section __ex_table,\"a\"\n"
#else
# define __FIXUP_SECTION	".section .discard,\"ax\"\n"
# define __EX_TABLE_SECTION	".section .discard,\"a\"\n"
#endif

#ifndef CONFIG_MMU
M
Michal Simek 已提交
112

113 114 115
/* Undefined function to trigger linker error */
extern int bad_user_access_length(void);

M
Michal Simek 已提交
116
/* FIXME this is function for optimalization -> memcpy */
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
#define __get_user(var, ptr)				\
({							\
	int __gu_err = 0;				\
	switch (sizeof(*(ptr))) {			\
	case 1:						\
	case 2:						\
	case 4:						\
		(var) = *(ptr);				\
		break;					\
	case 8:						\
		memcpy((void *) &(var), (ptr), 8);	\
		break;					\
	default:					\
		(var) = 0;				\
		__gu_err = __get_user_bad();		\
		break;					\
	}						\
	__gu_err;					\
})
M
Michal Simek 已提交
136 137 138

#define __get_user_bad()	(bad_user_access_length(), (-EFAULT))

139
/* FIXME is not there defined __pu_val */
M
Michal Simek 已提交
140
#define __put_user(var, ptr)					\
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
({								\
	int __pu_err = 0;					\
	switch (sizeof(*(ptr))) {				\
	case 1:							\
	case 2:							\
	case 4:							\
		*(ptr) = (var);					\
		break;						\
	case 8: {						\
		typeof(*(ptr)) __pu_val = (var);		\
		memcpy(ptr, &__pu_val, sizeof(__pu_val));	\
		}						\
		break;						\
	default:						\
		__pu_err = __put_user_bad();			\
		break;						\
	}							\
	__pu_err;						\
})
M
Michal Simek 已提交
160 161 162

#define __put_user_bad()	(bad_user_access_length(), (-EFAULT))

163 164
#define put_user(x, ptr)	__put_user((x), (ptr))
#define get_user(x, ptr)	__get_user((x), (ptr))
M
Michal Simek 已提交
165

166 167
#define copy_to_user(to, from, n)	(memcpy((to), (from), (n)), 0)
#define copy_from_user(to, from, n)	(memcpy((to), (from), (n)), 0)
M
Michal Simek 已提交
168

169 170 171 172 173 174
#define __copy_to_user(to, from, n)	(copy_to_user((to), (from), (n)))
#define __copy_from_user(to, from, n)	(copy_from_user((to), (from), (n)))
#define __copy_to_user_inatomic(to, from, n) \
			(__copy_to_user((to), (from), (n)))
#define __copy_from_user_inatomic(to, from, n) \
			(__copy_from_user((to), (from), (n)))
M
Michal Simek 已提交
175

176 177 178
#define __clear_user(addr, n)	(memset((void *)(addr), 0, (n)), 0)

/* stejne s MMU */
M
Michal Simek 已提交
179 180 181 182 183 184 185
static inline unsigned long clear_user(void *addr, unsigned long size)
{
	if (access_ok(VERIFY_WRITE, addr, size))
		size = __clear_user(addr, size);
	return size;
}

186
/* Returns 0 if exception not found and fixup otherwise.  */
M
Michal Simek 已提交
187 188
extern unsigned long search_exception_table(unsigned long);

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
extern long strncpy_from_user(char *dst, const char *src, long count);
extern long strnlen_user(const char *src, long count);

#else /* CONFIG_MMU */

/*
 * All the __XXX versions macros/functions below do not perform
 * access checking. It is assumed that the necessary checks have been
 * already performed before the finction (macro) is called.
 */

#define get_user(x, ptr)						\
({									\
	access_ok(VERIFY_READ, (ptr), sizeof(*(ptr)))			\
		? __get_user((x), (ptr)) : -EFAULT;			\
})

#define put_user(x, ptr)						\
({									\
	access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr)))			\
		? __put_user((x), (ptr)) : -EFAULT;			\
})

#define __get_user(x, ptr)						\
({									\
	unsigned long __gu_val;						\
	/*unsigned long __gu_ptr = (unsigned long)(ptr);*/		\
	long __gu_err;							\
	switch (sizeof(*(ptr))) {					\
	case 1:								\
		__get_user_asm("lbu", (ptr), __gu_val, __gu_err);	\
		break;							\
	case 2:								\
		__get_user_asm("lhu", (ptr), __gu_val, __gu_err);	\
		break;							\
	case 4:								\
		__get_user_asm("lw", (ptr), __gu_val, __gu_err);	\
		break;							\
	default:							\
		__gu_val = 0; __gu_err = -EINVAL;			\
	}								\
	x = (__typeof__(*(ptr))) __gu_val;				\
	__gu_err;							\
})

234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
#define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err)	\
({								\
	__asm__ __volatile__ (					\
			"1:"	insn	" %1, %2, r0;"		\
			"	addk	%0, r0, r0;"		\
			"2:			"		\
			__FIXUP_SECTION				\
			"3:	brid	2b;	"		\
			"	addik	%0, r0, %3;"		\
			".previous;"				\
			__EX_TABLE_SECTION			\
			".word	1b,3b;"				\
			".previous;"				\
		: "=&r"(__gu_err), "=r"(__gu_val)		\
		: "r"(__gu_ptr), "i"(-EFAULT)			\
	);							\
250 251
})

252

253 254
#define __put_user(x, ptr)						\
({									\
255
	__typeof__(*(ptr)) volatile __gu_val = (x);			\
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	long __gu_err = 0;						\
	switch (sizeof(__gu_val)) {					\
	case 1:								\
		__put_user_asm("sb", (ptr), __gu_val, __gu_err);	\
		break;							\
	case 2: 							\
		__put_user_asm("sh", (ptr), __gu_val, __gu_err);	\
		break;							\
	case 4:								\
		__put_user_asm("sw", (ptr), __gu_val, __gu_err);	\
		break;							\
	case 8:								\
		__put_user_asm_8((ptr), __gu_val, __gu_err);		\
		break;							\
	default:							\
		__gu_err = -EINVAL;					\
	}								\
	__gu_err;							\
})

#define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err)	\
({							\
__asm__ __volatile__ ("	lwi	%0, %1, 0;		\
		1:	swi	%0, %2, 0;		\
			lwi	%0, %1, 4;		\
		2:	swi	%0, %2, 4;		\
			addk	%0,r0,r0;		\
		3:					\
		.section .fixup,\"ax\";			\
		4:	brid	3b;			\
			addik	%0, r0, %3;		\
		.previous;				\
		.section __ex_table,\"a\";		\
		.word	1b,4b,2b,4b;			\
		.previous;"				\
	: "=&r"(__gu_err)				\
	: "r"(&__gu_val),				\
	"r"(__gu_ptr), "i"(-EFAULT)			\
	);						\
})

#define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err)	\
({								\
	__asm__ __volatile__ (					\
			"1:"	insn	" %1, %2, r0;		\
				addk	%0, r0, r0;		\
			2:					\
			.section .fixup,\"ax\";			\
			3:	brid	2b;			\
				addik	%0, r0, %3;		\
			.previous;				\
			.section __ex_table,\"a\";		\
			.word	1b,3b;				\
			.previous;"				\
		: "=r"(__gu_err)				\
		: "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT)	\
	);							\
})

315 316 317
/* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
static inline unsigned long __must_check __clear_user(void __user *to,
							unsigned long n)
318
{
319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	/* normal memset with two words to __ex_table */
	__asm__ __volatile__ (				\
			"1:	sb	r0, %2, r0;"	\
			"	addik	%0, %0, -1;"	\
			"	bneid	%0, 1b;"	\
			"	addik	%2, %2, 1;"	\
			"2:			"	\
			__EX_TABLE_SECTION		\
			".word	1b,2b;"			\
			".previous;"			\
		: "=r"(n)				\
		: "0"(n), "r"(to)
	);
	return n;
}

static inline unsigned long __must_check clear_user(void __user *to,
							unsigned long n)
{
	might_sleep();
	if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
		return n;

	return __clear_user(to, n);
343 344
}

345 346 347
#define __copy_from_user(to, from, n)	copy_from_user((to), (from), (n))
#define __copy_from_user_inatomic(to, from, n) \
		copy_from_user((to), (from), (n))
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

#define copy_to_user(to, from, n)					\
	(access_ok(VERIFY_WRITE, (to), (n)) ?				\
		__copy_tofrom_user((void __user *)(to),			\
			(__force const void __user *)(from), (n))	\
		: -EFAULT)

#define __copy_to_user(to, from, n)	copy_to_user((to), (from), (n))
#define __copy_to_user_inatomic(to, from, n)	copy_to_user((to), (from), (n))

#define copy_from_user(to, from, n)					\
	(access_ok(VERIFY_READ, (from), (n)) ?				\
		__copy_tofrom_user((__force void __user *)(to),		\
			(void __user *)(from), (n))			\
		: -EFAULT)

extern int __strncpy_user(char *to, const char __user *from, int len);
extern int __strnlen_user(const char __user *sstr, int len);

#define strncpy_from_user(to, from, len)	\
		(access_ok(VERIFY_READ, from, 1) ?	\
			__strncpy_user(to, from, len) : -EFAULT)
#define strnlen_user(str, len)	\
		(access_ok(VERIFY_READ, str, 1) ? __strnlen_user(str, len) : 0)
M
Michal Simek 已提交
372

373
#endif /* CONFIG_MMU */
M
Michal Simek 已提交
374

375 376 377
extern unsigned long __copy_tofrom_user(void __user *to,
		const void __user *from, unsigned long size);

M
Michal Simek 已提交
378 379 380 381
#endif  /* __ASSEMBLY__ */
#endif /* __KERNEL__ */

#endif /* _ASM_MICROBLAZE_UACCESS_H */