uaccess.h

来自「Linux Kernel 2.6.9 for OMAP1710」· C头文件 代码 · 共 826 行 · 第 1/2 页

H
826
字号
/* * 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. * * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle * Copyright (C) 1999, 2000 Silicon Graphics, Inc. */#ifndef _ASM_UACCESS_H#define _ASM_UACCESS_H#include <linux/config.h>#include <linux/compiler.h>#include <linux/errno.h>#include <linux/thread_info.h>#include <asm-generic/uaccess.h>/* * 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. */#ifdef CONFIG_MIPS32#define __UA_LIMIT	0x80000000UL#define __UA_ADDR	".word"#define __UA_LA		"la"#define __UA_ADDU	"addu"#define __UA_t0		"$8"#define __UA_t1		"$9"#endif /* CONFIG_MIPS32 */#ifdef CONFIG_MIPS64#define __UA_LIMIT	(- TASK_SIZE)#define __UA_ADDR	".dword"#define __UA_LA		"dla"#define __UA_ADDU	"daddu"#define __UA_t0		"$12"#define __UA_t1		"$13"#endif /* CONFIG_MIPS64 *//* * USER_DS is a bitmask that has the bits set that may not be set in a valid * userspace address.  Note that we limit 32-bit userspace to 0x7fff8000 but * the arithmetic we're doing only works if the limit is a power of two, so * we use 0x80000000 here on 32-bit kernels.  If a process passes an invalid * address in this range it's the process's problem, not ours :-) */#define KERNEL_DS	((mm_segment_t) { 0UL })#define USER_DS		((mm_segment_t) { __UA_LIMIT })#define VERIFY_READ    0#define VERIFY_WRITE   1#define get_ds()	(KERNEL_DS)#define get_fs()	(current_thread_info()->addr_limit)#define set_fs(x)	(current_thread_info()->addr_limit = (x))#define segment_eq(a,b)	((a).seg == (b).seg)/* * Is a address valid? This does a straighforward calculation rather * than tests. * * Address valid if: *  - "addr" doesn't have any high-bits set *  - AND "size" doesn't have any high-bits set *  - AND "addr+size" doesn't have any high-bits set *  - OR we are in kernel mode. * * __ua_size() is a trick to avoid runtime checking of positive constant * sizes; for those we already know at compile time that the size is ok. */#define __ua_size(size)							\	((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))/* * access_ok: - Checks if a user space pointer is valid * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe *        to write to a block, it is always safe to read from it. * @addr: User space pointer to start of block to check * @size: Size of block to check * * Context: User context only.  This function may sleep. * * Checks if a pointer to a block of memory in user space is valid. * * Returns true (nonzero) if the memory block may be valid, false (zero) * if it is definitely invalid. * * Note that, depending on architecture, this function probably just * checks that the pointer is in the user space range - after calling * this function, memory access functions may still return -EFAULT. */#define __access_mask get_fs().seg#define __access_ok(addr, size, mask)					\	(((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)#define access_ok(type, addr, size)					\	likely(__access_ok((unsigned long)(addr), (size),__access_mask))/* * verify_area: - Obsolete, use access_ok() * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE * @addr: User space pointer to start of block to check * @size: Size of block to check * * Context: User context only.  This function may sleep. * * This function has been replaced by access_ok(). * * Checks if a pointer to a block of memory in user space is valid. * * Returns zero if the memory block may be valid, -EFAULT * if it is definitely invalid. * * See access_ok() for more details. */static inline int verify_area(int type, const void * addr, unsigned long size){	return access_ok(type, addr, size) ? 0 : -EFAULT;}/* * 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((__typeof__(*(ptr)))(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((__typeof__(*(ptr)))(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((__typeof__(*(ptr)))(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((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))struct __large_struct { unsigned long buf[100]; };#define __m(x) (*(struct __large_struct *)(x))/* * Yuck.  We need two variants, one for 64bit operation and one * for 32 bit mode and old iron. */#ifdef __mips64#define __GET_USER_DW __get_user_asm("ld")#else#define __GET_USER_DW __get_user_asm_ll32#endif#define __get_user_nocheck(x,ptr,size)				\({								\	long __gu_err;						\	__typeof(*(ptr)) __gu_val;				\	long __gu_addr;						\	might_sleep();						\	__asm__("":"=r" (__gu_val));				\	__gu_addr = (long) (ptr);				\	__asm__("":"=r" (__gu_err));				\	switch (size) {						\	case 1: __get_user_asm("lb"); break;			\	case 2: __get_user_asm("lh"); break;			\	case 4: __get_user_asm("lw"); break;			\	case 8: __GET_USER_DW; break;				\	default: __get_user_unknown(); break;			\	} x = (__typeof__(*(ptr))) __gu_val; __gu_err;		\})#define __get_user_check(x,ptr,size)					\({									\	long __gu_err;							\	__typeof__(*(ptr)) __gu_val;					\	long __gu_addr;							\	might_sleep();							\	__asm__("":"=r" (__gu_val));					\	__gu_addr = (long) (ptr);					\	__asm__("":"=r" (__gu_err));					\	if (access_ok(VERIFY_READ,__gu_addr,size)) {			\		switch (size) {						\		case 1: __get_user_asm("lb"); break;			\		case 2: __get_user_asm("lh"); break;			\		case 4: __get_user_asm("lw"); break;			\		case 8: __GET_USER_DW; break;				\		default: __get_user_unknown(); break;			\		}							\	} x = (__typeof__(*(ptr))) __gu_val; __gu_err;			\})#define __get_user_asm(insn)						\({									\	__asm__ __volatile__(						\	"1:\t" insn "\t%1,%2\n\t"					\	"move\t%0,$0\n"							\	"2:\n\t"							\	".section\t.fixup,\"ax\"\n"					\	"3:\tli\t%0,%3\n\t"						\	"move\t%1,$0\n\t"						\	"j\t2b\n\t"							\	".previous\n\t"							\	".section\t__ex_table,\"a\"\n\t"				\	__UA_ADDR "\t1b,3b\n\t"						\	".previous"							\	:"=r" (__gu_err), "=r" (__gu_val)				\	:"o" (__m(__gu_addr)), "i" (-EFAULT));				\})/* * Get a long long 64 using 32 bit registers. */#define __get_user_asm_ll32						\({									\	__asm__ __volatile__(						\	"1:\tlw\t%1,%2\n"						\	"2:\tlw\t%D1,%3\n\t"						\	"move\t%0,$0\n"							\	"3:\t.section\t.fixup,\"ax\"\n"					\	"4:\tli\t%0,%4\n\t"						\	"move\t%1,$0\n\t"						\	"move\t%D1,$0\n\t"						\	"j\t3b\n\t"							\	".previous\n\t"							\	".section\t__ex_table,\"a\"\n\t"				\	__UA_ADDR "\t1b,4b\n\t"						\	__UA_ADDR "\t2b,4b\n\t"						\	".previous"							\	:"=r" (__gu_err), "=&r" (__gu_val)				\	:"o" (__m(__gu_addr)), "o" (__m(__gu_addr + 4)),		\	 "i" (-EFAULT));						\})extern void __get_user_unknown(void);/* * Yuck.  We need two variants, one for 64bit operation and one * for 32 bit mode and old iron. */#ifdef __mips64#define __PUT_USER_DW __put_user_asm("sd")#else#define __PUT_USER_DW __put_user_asm_ll32#endif#define __put_user_nocheck(x,ptr,size)					\({									\	long __pu_err;							\	__typeof__(*(ptr)) __pu_val;					\	long __pu_addr;							\	might_sleep();							\	__pu_val = (x);							\	__pu_addr = (long) (ptr);					\	__asm__("":"=r" (__pu_err));					\	switch (size) {							\	case 1: __put_user_asm("sb"); break;				\	case 2: __put_user_asm("sh"); break;				\	case 4: __put_user_asm("sw"); break;				\	case 8: __PUT_USER_DW; break;					\	default: __put_user_unknown(); break;				\	}								\	__pu_err;							\})#define __put_user_check(x,ptr,size)					\({									\	long __pu_err;							\	__typeof__(*(ptr)) __pu_val;					\	long __pu_addr;							\	might_sleep();							\	__pu_val = (x);							\	__pu_addr = (long) (ptr);					\	__asm__("":"=r" (__pu_err));					\	if (access_ok(VERIFY_WRITE, __pu_addr, size)) {			\		switch (size) {						\		case 1: __put_user_asm("sb"); break;			\		case 2: __put_user_asm("sh"); break;			\		case 4: __put_user_asm("sw"); break;			\		case 8: __PUT_USER_DW; break;				\		default: __put_user_unknown(); break;			\		}							\	}								\	__pu_err;							\})#define __put_user_asm(insn)						\({									\	__asm__ __volatile__(						\	"1:\t" insn "\t%z1, %2\t\t\t# __put_user_asm\n\t"		\	"move\t%0, $0\n"						\	"2:\n\t"							\	".section\t.fixup,\"ax\"\n"					\	"3:\tli\t%0,%3\n\t"						\	"j\t2b\n\t"							\	".previous\n\t"							\	".section\t__ex_table,\"a\"\n\t"				\	__UA_ADDR "\t1b,3b\n\t"						\	".previous"							\	:"=r" (__pu_err)						\	:"Jr" (__pu_val), "o" (__m(__pu_addr)), "i" (-EFAULT));		\})#define __put_user_asm_ll32						\({									\	__asm__ __volatile__(						\	"1:\tsw\t%1, %2\t\t\t# __put_user_asm_ll32\n\t"			\	"2:\tsw\t%D1, %3\n"						\	"move\t%0, $0\n"						\	"3:\n\t"							\	".section\t.fixup,\"ax\"\n"					\	"4:\tli\t%0,%4\n\t"						\	"j\t3b\n\t"							\	".previous\n\t"							\	".section\t__ex_table,\"a\"\n\t"				\	__UA_ADDR "\t1b,4b\n\t"						\	__UA_ADDR "\t2b,4b\n\t"						\	".previous"							\	:"=r" (__pu_err)						\	:"r" (__pu_val), "o" (__m(__pu_addr)),				\	 "o" (__m(__pu_addr + 4)), "i" (-EFAULT));			\})extern void __put_user_unknown(void);/* * We're generating jump to subroutines which will be outside the range of * jump instructions */#ifdef MODULE#define __MODULE_JAL(destination)					\	".set\tnoat\n\t"						\	__UA_LA "\t$1, " #destination "\n\t" 				\	"jalr\t$1\n\t"							\	".set\tat\n\t"

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?