📄 uaccess.h
字号:
/* * Authors: Bjorn Wesen (bjornw@axis.com) * Hans-Peter Nilsson (hp@axis.com) * * $Log: uaccess.h,v $ * Revision 1.8 2001/10/29 13:01:48 bjornw * Removed unused variable tmp2 in strnlen_user * * Revision 1.7 2001/10/02 12:44:52 hp * Add support for 64-bit put_user/get_user * * Revision 1.6 2001/10/01 14:51:17 bjornw * Added register prefixes and removed underscores * * Revision 1.5 2000/10/25 03:33:21 hp * - Provide implementation for everything else but get_user and put_user; * copying inline to/from user for constant length 0..16, 20, 24, and * clearing for 0..4, 8, 12, 16, 20, 24, strncpy_from_user and strnlen_user * always inline. * - Constraints for destination addr in get_user cannot be memory, only reg. * - Correct labels for PC at expected fault points. * - Nits with assembly code. * - Don't use statement expressions without value; use "do {} while (0)". * - Return correct values from __generic_... functions. * * Revision 1.4 2000/09/12 16:28:25 bjornw * * Removed comments from the get/put user asm code * * Constrains for destination addr in put_user cannot be memory, only reg * * Revision 1.3 2000/09/12 14:30:20 bjornw * MAX_ADDR_USER does not exist anymore * * Revision 1.2 2000/07/13 15:52:48 bjornw * New user-access functions * * Revision 1.1.1.1 2000/07/10 16:32:31 bjornw * CRIS architecture, working draft * * * *//* Asm:s have been tweaked (within the domain of correctness) to give satisfactory results for "gcc version 2.96 20000427 (experimental)". Check regularly... Register $r9 is chosen for temporaries, being a call-clobbered register first in line to be used (notably for local blocks), not colliding with parameter registers. */#ifndef _CRIS_UACCESS_H#define _CRIS_UACCESS_H#ifndef __ASSEMBLY__#include <linux/sched.h>#include <linux/errno.h>#include <asm/processor.h>#include <asm/page.h>#define VERIFY_READ 0#define VERIFY_WRITE 1/* * 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. */#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })/* addr_limit is the maximum accessible address for the task. we misuse * the KERNEL_DS and USER_DS values to both assign and compare the * addr_limit values through the equally misnamed get/set_fs macros. * (see above) */#define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)#define USER_DS MAKE_MM_SEG(TASK_SIZE)#define get_ds() (KERNEL_DS)#define get_fs() (current->addr_limit)#define set_fs(x) (current->addr_limit = (x))#define segment_eq(a,b) ((a).seg == (b).seg)#define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))#define __user_ok(addr,size) (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))#define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))#define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))extern inline int verify_area(int type, const void * addr, unsigned long size){ return access_ok(type,addr,size) ? 0 : -EFAULT;}/* * 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;};/* Returns 0 if exception not found and fixup otherwise. */extern unsigned long search_exception_table(unsigned long);/* * These are the main single-value transfer routines. They automatically * use the right size if we just have the right pointer type. * * This gets kind of ugly. We want to return _two_ values in "get_user()" * and yet we don't want to do any pointers, because that is too much * of a performance impact. Thus we have a few rather ugly macros here, * and hide all the uglyness from the user. * * The "__xxx" versions of the user access functions are versions that * do not verify the address space, that must have been done previously * with a separate "access_ok()" call (this is used when we do multiple * accesses to the same area of user memory). * * As we use the same address space for kernel and user data on * CRIS, we can just do these as direct assignments. (Of course, the * exception handling means that it's no longer "just"...) */#define get_user(x,ptr) \ __get_user_check((x),(ptr),sizeof(*(ptr)))#define put_user(x,ptr) \ __put_user_check((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))#define __get_user(x,ptr) \ __get_user_nocheck((x),(ptr),sizeof(*(ptr)))#define __put_user(x,ptr) \ __put_user_nocheck((__typeof__(*(ptr)))(x),(ptr),sizeof(*(ptr)))/* * The "xxx_ret" versions return constant specified in third argument, if * something bad happens. These macros can be optimized for the * case of just returning from the function xxx_ret is used. */#define put_user_ret(x,ptr,ret) \ do { if (put_user(x,ptr)) return ret; } while (0)#define get_user_ret(x,ptr,ret) \ do { if (get_user(x,ptr)) return ret; } while (0)#define __put_user_ret(x,ptr,ret) \ do { if (__put_user(x,ptr)) return ret; } while (0)#define __get_user_ret(x,ptr,ret) \ do { if (__get_user(x,ptr)) return ret; } while (0)extern long __put_user_bad(void);#define __put_user_nocheck(x,ptr,size) \({ \ long __pu_err; \ __put_user_size((x),(ptr),(size),__pu_err); \ __pu_err; \})#define __put_user_check(x,ptr,size) \({ \ long __pu_err = -EFAULT; \ __typeof__(*(ptr)) *__pu_addr = (ptr); \ if (access_ok(VERIFY_WRITE,__pu_addr,size)) \ __put_user_size((x),__pu_addr,(size),__pu_err); \ __pu_err; \})#define __put_user_size(x,ptr,size,retval) \do { \ retval = 0; \ switch (size) { \ case 1: __put_user_asm(x,ptr,retval,"move.b"); break; \ case 2: __put_user_asm(x,ptr,retval,"move.w"); break; \ case 4: __put_user_asm(x,ptr,retval,"move.d"); break; \ case 8: __put_user_asm_64(x,ptr,retval); break; \ default: __put_user_bad(); \ } \} while (0)struct __large_struct { unsigned long buf[100]; };#define __m(x) (*(struct __large_struct *)(x))/* * We don't tell gcc that we are accessing memory, but this is OK * because we do not write to any memory gcc knows about, so there * are no aliasing issues. * * Note that PC at a fault is the address *after* the faulting * instruction. */#define __put_user_asm(x, addr, err, op) \ __asm__ __volatile__( \ " "op" %1,[%2]\n" \ "2:\n" \ " .section .fixup,\"ax\"\n" \ "3: move.d %3,%0\n" \ " jump 2b\n" \ " .previous\n" \ " .section __ex_table,\"a\"\n" \ " .dword 2b,3b\n" \ " .previous\n" \ : "=r" (err) \ : "r" (x), "r" (addr), "g" (-EFAULT), "0" (err))#define __put_user_asm_64(x, addr, err) \ __asm__ __volatile__( \ " move.d %M1,[%2]\n" \ "2: move.d %H1,[%2+4]\n" \ "4:\n" \ " .section .fixup,\"ax\"\n" \ "3: move.d %3,%0\n" \ " jump 4b\n" \ " .previous\n" \ " .section __ex_table,\"a\"\n" \ " .dword 2b,3b\n" \ " .dword 4b,3b\n" \ " .previous\n" \ : "=r" (err) \ : "r" (x), "r" (addr), "g" (-EFAULT), "0" (err))#define __get_user_nocheck(x,ptr,size) \({ \ long __gu_err, __gu_val; \ __get_user_size(__gu_val,(ptr),(size),__gu_err); \ (x) = (__typeof__(*(ptr)))__gu_val; \ __gu_err; \})#define __get_user_check(x,ptr,size) \({ \ long __gu_err = -EFAULT, __gu_val = 0; \ const __typeof__(*(ptr)) *__gu_addr = (ptr); \ if (access_ok(VERIFY_READ,__gu_addr,size)) \ __get_user_size(__gu_val,__gu_addr,(size),__gu_err); \ (x) = (__typeof__(*(ptr)))__gu_val; \ __gu_err; \})extern long __get_user_bad(void);#define __get_user_size(x,ptr,size,retval) \do { \ retval = 0; \ switch (size) { \ case 1: __get_user_asm(x,ptr,retval,"move.b"); break; \ case 2: __get_user_asm(x,ptr,retval,"move.w"); break; \ case 4: __get_user_asm(x,ptr,retval,"move.d"); break; \ case 8: __get_user_asm_64(x,ptr,retval); break; \ default: (x) = __get_user_bad(); \ } \} while (0)/* See comment before __put_user_asm. */#define __get_user_asm(x, addr, err, op) \ __asm__ __volatile__( \ " "op" [%2],%1\n" \ "2:\n" \ " .section .fixup,\"ax\"\n" \ "3: move.d %3,%0\n" \ " moveq 0,%1\n" \ " jump 2b\n" \ " .previous\n" \ " .section __ex_table,\"a\"\n" \ " .dword 2b,3b\n" \ " .previous\n" \ : "=r" (err), "=r" (x) \ : "r" (addr), "g" (-EFAULT), "0" (err))#define __get_user_asm_64(x, addr, err) \ __asm__ __volatile__( \ " move.d [%2],%M1\n" \ "2: move.d [%2+4],%H1\n" \ "4:\n" \ " .section .fixup,\"ax\"\n" \ "3: move.d %3,%0\n" \ " moveq 0,%1\n" \ " jump 4b\n" \ " .previous\n" \ " .section __ex_table,\"a\"\n" \ " .dword 2b,3b\n" \ " .dword 4b,3b\n" \ " .previous\n" \ : "=r" (err), "=r" (x) \ : "r" (addr), "g" (-EFAULT), "0" (err))/* More complex functions. Most are inline, but some call functions that live in lib/usercopy.c */extern unsigned long __copy_user(void *to, const void *from, unsigned long n);extern unsigned long __copy_user_zeroing(void *to, const void *from, unsigned long n);extern unsigned long __do_clear_user(void *to, unsigned long n);/* * Copy a null terminated string from userspace. * * Must return: * -EFAULT for an exception * count if we hit the buffer limit * bytes copied if we hit a null byte * (without the null byte) */static inline long __do_strncpy_from_user(char *dst, const char *src, long count){ long res; if (count == 0) return 0; /* * Currently, in 2.4.0-test9, most ports use a simple byte-copy loop. * So do we. * * This code is deduced from: * * char tmp2; * long tmp1, tmp3 * tmp1 = count; * while ((*dst++ = (tmp2 = *src++)) != 0 * && --tmp1) * ; * * res = count - tmp1; * * with tweaks. */ __asm__ __volatile__ ( " move.d %3,%0\n" " move.b [%2+],$r9\n" "1: beq 2f\n" " move.b $r9,[%1+]\n" " subq 1,%0\n" " bne 1b\n" " move.b [%2+],$r9\n" "2: sub.d %3,%0\n" " neg.d %0,%0\n" "3:\n" " .section .fixup,\"ax\"\n" "4: move.d %7,%0\n" " jump 3b\n" /* There's one address for a fault at the first move, and two possible PC values for a fault at the second move, being a delay-slot filler. However, the branch-target for the second move is the same as the first address. Just so you don't get confused... */ " .previous\n" " .section __ex_table,\"a\"\n" " .dword 1b,4b\n"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -