| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215 | /* * Copyright (C) 2004-2006 Atmel Corporation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */#ifndef __ASM_AVR32_UACCESS_H#define __ASM_AVR32_UACCESS_H#include <linux/errno.h>#include <linux/sched.h>#define VERIFY_READ	0#define VERIFY_WRITE	1typedef struct {	unsigned int is_user_space;} mm_segment_t;/* * 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 (Data Segment Register?), these macros are misnamed. */#define MAKE_MM_SEG(s)	((mm_segment_t) { (s) })#define segment_eq(a,b)	((a).is_user_space == (b).is_user_space)#define USER_ADDR_LIMIT 0x80000000#define KERNEL_DS	MAKE_MM_SEG(0)#define USER_DS		MAKE_MM_SEG(1)#define get_ds()	(KERNEL_DS)static inline mm_segment_t get_fs(void){	return MAKE_MM_SEG(test_thread_flag(TIF_USERSPACE));}static inline void set_fs(mm_segment_t s){	if (s.is_user_space)		set_thread_flag(TIF_USERSPACE);	else		clear_thread_flag(TIF_USERSPACE);}/* * Test whether a block of memory is a valid user space address. * Returns 0 if the range is valid, nonzero otherwise. * * We do the following checks: *   1. Is the access from kernel space? *   2. Does (addr + size) set the carry bit? *   3. Is (addr + size) a negative number (i.e. >= 0x80000000)? * * If yes on the first check, access is granted. * If no on any of the others, access is denied. */#define __range_ok(addr, size)						\	(test_thread_flag(TIF_USERSPACE)				\	 && (((unsigned long)(addr) >= 0x80000000)			\	     || ((unsigned long)(size) > 0x80000000)			\	     || (((unsigned long)(addr) + (unsigned long)(size)) > 0x80000000)))#define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))/* Generic arbitrary sized copy. Return the number of bytes NOT copied */extern __kernel_size_t __copy_user(void *to, const void *from,				   __kernel_size_t n);extern __kernel_size_t copy_to_user(void __user *to, const void *from,				    __kernel_size_t n);extern __kernel_size_t copy_from_user(void *to, const void __user *from,				      __kernel_size_t n);static inline __kernel_size_t __copy_to_user(void __user *to, const void *from,					     __kernel_size_t n){	return __copy_user((void __force *)to, from, n);}static inline __kernel_size_t __copy_from_user(void *to,					       const void __user *from,					       __kernel_size_t n){	return __copy_user(to, (const void __force *)from, n);}#define __copy_to_user_inatomic __copy_to_user#define __copy_from_user_inatomic __copy_from_user/* * put_user: - Write a simple value into user space. * @x:   Value to copy to user space. * @ptr: Destination address, in user space. * * Context: User context only.  This function may sleep. * * This macro copies a single simple value from kernel space to user * space.  It supports simple types like char and int, but not larger * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable * to the result of dereferencing @ptr. * * Returns zero on success, or -EFAULT on error. */#define put_user(x,ptr)	\	__put_user_check((x),(ptr),sizeof(*(ptr)))/* * get_user: - Get a simple variable from user space. * @x:   Variable to store result. * @ptr: Source address, in user space. * * Context: User context only.  This function may sleep. * * This macro copies a single simple variable from user space to kernel * space.  It supports simple types like char and int, but not larger * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and the result of * dereferencing @ptr must be assignable to @x without a cast. * * Returns zero on success, or -EFAULT on error. * On error, the variable @x is set to zero. */#define get_user(x,ptr) \	__get_user_check((x),(ptr),sizeof(*(ptr)))/* * __put_user: - Write a simple value into user space, with less checking. * @x:   Value to copy to user space. * @ptr: Destination address, in user space. * * Context: User context only.  This function may sleep. * * This macro copies a single simple value from kernel space to user * space.  It supports simple types like char and int, but not larger * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and @x must be assignable * to the result of dereferencing @ptr. * * Caller must check the pointer with access_ok() before calling this * function. * * Returns zero on success, or -EFAULT on error. */#define __put_user(x,ptr) \	__put_user_nocheck((x),(ptr),sizeof(*(ptr)))/* * __get_user: - Get a simple variable from user space, with less checking. * @x:   Variable to store result. * @ptr: Source address, in user space. * * Context: User context only.  This function may sleep. * * This macro copies a single simple variable from user space to kernel * space.  It supports simple types like char and int, but not larger * data types like structures or arrays. * * @ptr must have pointer-to-simple-variable type, and the result of * dereferencing @ptr must be assignable to @x without a cast. * * Caller must check the pointer with access_ok() before calling this * function. * * Returns zero on success, or -EFAULT on error. * On error, the variable @x is set to zero. */#define __get_user(x,ptr) \	__get_user_nocheck((x),(ptr),sizeof(*(ptr)))extern int __get_user_bad(void);extern int __put_user_bad(void);#define __get_user_nocheck(x, ptr, size)				\({									\	unsigned long __gu_val = 0;					\	int __gu_err = 0;						\									\	switch (size) {							\	case 1: __get_user_asm("ub", __gu_val, ptr, __gu_err); break;	\	case 2: __get_user_asm("uh", __gu_val, ptr, __gu_err); break;	\	case 4: __get_user_asm("w", __gu_val, ptr, __gu_err); break;	\	default: __gu_err = __get_user_bad(); break;			\	}								\									\	x = (typeof(*(ptr)))__gu_val;					\	__gu_err;							\})#define __get_user_check(x, ptr, size)					\({									\	unsigned long __gu_val = 0;					\	const typeof(*(ptr)) __user * __gu_addr = (ptr);		\	int __gu_err = 0;						\									\	if (access_ok(VERIFY_READ, __gu_addr, size)) {			\		switch (size) {						\		case 1:							\			__get_user_asm("ub", __gu_val, __gu_addr,	\				       __gu_err);			\			break;						\		case 2:							\			__get_user_asm("uh", __gu_val, __gu_addr,	\				       __gu_err);			\			break;						\		case 4:							\			__get_user_asm("w", __gu_val, __gu_addr,	\
 |