ia64/xen-unstable

changeset 12777:bef3a5ffd78d

[IA64] import linux/include/asm-ia64/uaccess.h for /dev/mem paravirtualization

Signed-off-by: Isaku Yamahata <yamahata@valinux.co.jp>
author awilliam@xenbuild.aw
date Mon Dec 04 08:37:20 2006 -0700 (2006-12-04)
parents 82a581113669
children e5e6893ec699
files linux-2.6-xen-sparse/include/asm-ia64/uaccess.h
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/linux-2.6-xen-sparse/include/asm-ia64/uaccess.h	Mon Dec 04 08:37:20 2006 -0700
     1.3 @@ -0,0 +1,401 @@
     1.4 +#ifndef _ASM_IA64_UACCESS_H
     1.5 +#define _ASM_IA64_UACCESS_H
     1.6 +
     1.7 +/*
     1.8 + * This file defines various macros to transfer memory areas across
     1.9 + * the user/kernel boundary.  This needs to be done carefully because
    1.10 + * this code is executed in kernel mode and uses user-specified
    1.11 + * addresses.  Thus, we need to be careful not to let the user to
    1.12 + * trick us into accessing kernel memory that would normally be
    1.13 + * inaccessible.  This code is also fairly performance sensitive,
    1.14 + * so we want to spend as little time doing safety checks as
    1.15 + * possible.
    1.16 + *
    1.17 + * To make matters a bit more interesting, these macros sometimes also
    1.18 + * called from within the kernel itself, in which case the address
    1.19 + * validity check must be skipped.  The get_fs() macro tells us what
    1.20 + * to do: if get_fs()==USER_DS, checking is performed, if
    1.21 + * get_fs()==KERNEL_DS, checking is bypassed.
    1.22 + *
    1.23 + * Note that even if the memory area specified by the user is in a
    1.24 + * valid address range, it is still possible that we'll get a page
    1.25 + * fault while accessing it.  This is handled by filling out an
    1.26 + * exception handler fixup entry for each instruction that has the
    1.27 + * potential to fault.  When such a fault occurs, the page fault
    1.28 + * handler checks to see whether the faulting instruction has a fixup
    1.29 + * associated and, if so, sets r8 to -EFAULT and clears r9 to 0 and
    1.30 + * then resumes execution at the continuation point.
    1.31 + *
    1.32 + * Based on <asm-alpha/uaccess.h>.
    1.33 + *
    1.34 + * Copyright (C) 1998, 1999, 2001-2004 Hewlett-Packard Co
    1.35 + *	David Mosberger-Tang <davidm@hpl.hp.com>
    1.36 + */
    1.37 +
    1.38 +#include <linux/compiler.h>
    1.39 +#include <linux/errno.h>
    1.40 +#include <linux/sched.h>
    1.41 +#include <linux/page-flags.h>
    1.42 +#include <linux/mm.h>
    1.43 +
    1.44 +#include <asm/intrinsics.h>
    1.45 +#include <asm/pgtable.h>
    1.46 +#include <asm/io.h>
    1.47 +
    1.48 +/*
    1.49 + * For historical reasons, the following macros are grossly misnamed:
    1.50 + */
    1.51 +#define KERNEL_DS	((mm_segment_t) { ~0UL })		/* cf. access_ok() */
    1.52 +#define USER_DS		((mm_segment_t) { TASK_SIZE-1 })	/* cf. access_ok() */
    1.53 +
    1.54 +#define VERIFY_READ	0
    1.55 +#define VERIFY_WRITE	1
    1.56 +
    1.57 +#define get_ds()  (KERNEL_DS)
    1.58 +#define get_fs()  (current_thread_info()->addr_limit)
    1.59 +#define set_fs(x) (current_thread_info()->addr_limit = (x))
    1.60 +
    1.61 +#define segment_eq(a, b)	((a).seg == (b).seg)
    1.62 +
    1.63 +/*
    1.64 + * When accessing user memory, we need to make sure the entire area really is in
    1.65 + * user-level space.  In order to do this efficiently, we make sure that the page at
    1.66 + * address TASK_SIZE is never valid.  We also need to make sure that the address doesn't
    1.67 + * point inside the virtually mapped linear page table.
    1.68 + */
    1.69 +#define __access_ok(addr, size, segment)						\
    1.70 +({											\
    1.71 +	__chk_user_ptr(addr);								\
    1.72 +	(likely((unsigned long) (addr) <= (segment).seg)				\
    1.73 +	 && ((segment).seg == KERNEL_DS.seg						\
    1.74 +	     || likely(REGION_OFFSET((unsigned long) (addr)) < RGN_MAP_LIMIT)));	\
    1.75 +})
    1.76 +#define access_ok(type, addr, size)	__access_ok((addr), (size), get_fs())
    1.77 +
    1.78 +/*
    1.79 + * These are the main single-value transfer routines.  They automatically
    1.80 + * use the right size if we just have the right pointer type.
    1.81 + *
    1.82 + * Careful to not
    1.83 + * (a) re-use the arguments for side effects (sizeof/typeof is ok)
    1.84 + * (b) require any knowledge of processes at this stage
    1.85 + */
    1.86 +#define put_user(x, ptr)	__put_user_check((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)), get_fs())
    1.87 +#define get_user(x, ptr)	__get_user_check((x), (ptr), sizeof(*(ptr)), get_fs())
    1.88 +
    1.89 +/*
    1.90 + * The "__xxx" versions do not do address space checking, useful when
    1.91 + * doing multiple accesses to the same area (the programmer has to do the
    1.92 + * checks by hand with "access_ok()")
    1.93 + */
    1.94 +#define __put_user(x, ptr)	__put_user_nocheck((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
    1.95 +#define __get_user(x, ptr)	__get_user_nocheck((x), (ptr), sizeof(*(ptr)))
    1.96 +
    1.97 +extern long __put_user_unaligned_unknown (void);
    1.98 +
    1.99 +#define __put_user_unaligned(x, ptr)								\
   1.100 +({												\
   1.101 +	long __ret;										\
   1.102 +	switch (sizeof(*(ptr))) {								\
   1.103 +		case 1: __ret = __put_user((x), (ptr)); break;					\
   1.104 +		case 2: __ret = (__put_user((x), (u8 __user *)(ptr)))				\
   1.105 +			| (__put_user((x) >> 8, ((u8 __user *)(ptr) + 1))); break;		\
   1.106 +		case 4: __ret = (__put_user((x), (u16 __user *)(ptr)))				\
   1.107 +			| (__put_user((x) >> 16, ((u16 __user *)(ptr) + 1))); break;		\
   1.108 +		case 8: __ret = (__put_user((x), (u32 __user *)(ptr)))				\
   1.109 +			| (__put_user((x) >> 32, ((u32 __user *)(ptr) + 1))); break;		\
   1.110 +		default: __ret = __put_user_unaligned_unknown();				\
   1.111 +	}											\
   1.112 +	__ret;											\
   1.113 +})
   1.114 +
   1.115 +extern long __get_user_unaligned_unknown (void);
   1.116 +
   1.117 +#define __get_user_unaligned(x, ptr)								\
   1.118 +({												\
   1.119 +	long __ret;										\
   1.120 +	switch (sizeof(*(ptr))) {								\
   1.121 +		case 1: __ret = __get_user((x), (ptr)); break;					\
   1.122 +		case 2: __ret = (__get_user((x), (u8 __user *)(ptr)))				\
   1.123 +			| (__get_user((x) >> 8, ((u8 __user *)(ptr) + 1))); break;		\
   1.124 +		case 4: __ret = (__get_user((x), (u16 __user *)(ptr)))				\
   1.125 +			| (__get_user((x) >> 16, ((u16 __user *)(ptr) + 1))); break;		\
   1.126 +		case 8: __ret = (__get_user((x), (u32 __user *)(ptr)))				\
   1.127 +			| (__get_user((x) >> 32, ((u32 __user *)(ptr) + 1))); break;		\
   1.128 +		default: __ret = __get_user_unaligned_unknown();				\
   1.129 +	}											\
   1.130 +	__ret;											\
   1.131 +})
   1.132 +
   1.133 +#ifdef ASM_SUPPORTED
   1.134 +  struct __large_struct { unsigned long buf[100]; };
   1.135 +# define __m(x) (*(struct __large_struct __user *)(x))
   1.136 +
   1.137 +/* We need to declare the __ex_table section before we can use it in .xdata.  */
   1.138 +asm (".section \"__ex_table\", \"a\"\n\t.previous");
   1.139 +
   1.140 +# define __get_user_size(val, addr, n, err)							\
   1.141 +do {												\
   1.142 +	register long __gu_r8 asm ("r8") = 0;							\
   1.143 +	register long __gu_r9 asm ("r9");							\
   1.144 +	asm ("\n[1:]\tld"#n" %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n"	\
   1.145 +	     "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n"						\
   1.146 +	     "[1:]"										\
   1.147 +	     : "=r"(__gu_r9), "=r"(__gu_r8) : "m"(__m(addr)), "1"(__gu_r8));			\
   1.148 +	(err) = __gu_r8;									\
   1.149 +	(val) = __gu_r9;									\
   1.150 +} while (0)
   1.151 +
   1.152 +/*
   1.153 + * The "__put_user_size()" macro tells gcc it reads from memory instead of writing it.  This
   1.154 + * is because they do not write to any memory gcc knows about, so there are no aliasing
   1.155 + * issues.
   1.156 + */
   1.157 +# define __put_user_size(val, addr, n, err)							\
   1.158 +do {												\
   1.159 +	register long __pu_r8 asm ("r8") = 0;							\
   1.160 +	asm volatile ("\n[1:]\tst"#n" %1=%r2%P1\t// %0 gets overwritten by exception handler\n"	\
   1.161 +		      "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n"					\
   1.162 +		      "[1:]"									\
   1.163 +		      : "=r"(__pu_r8) : "m"(__m(addr)), "rO"(val), "0"(__pu_r8));		\
   1.164 +	(err) = __pu_r8;									\
   1.165 +} while (0)
   1.166 +
   1.167 +#else /* !ASM_SUPPORTED */
   1.168 +# define RELOC_TYPE	2	/* ip-rel */
   1.169 +# define __get_user_size(val, addr, n, err)				\
   1.170 +do {									\
   1.171 +	__ld_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE);	\
   1.172 +	(err) = ia64_getreg(_IA64_REG_R8);				\
   1.173 +	(val) = ia64_getreg(_IA64_REG_R9);				\
   1.174 +} while (0)
   1.175 +# define __put_user_size(val, addr, n, err)							\
   1.176 +do {												\
   1.177 +	__st_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE, (unsigned long) (val));	\
   1.178 +	(err) = ia64_getreg(_IA64_REG_R8);							\
   1.179 +} while (0)
   1.180 +#endif /* !ASM_SUPPORTED */
   1.181 +
   1.182 +extern void __get_user_unknown (void);
   1.183 +
   1.184 +/*
   1.185 + * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
   1.186 + * could clobber r8 and r9 (among others).  Thus, be careful not to evaluate it while
   1.187 + * using r8/r9.
   1.188 + */
   1.189 +#define __do_get_user(check, x, ptr, size, segment)					\
   1.190 +({											\
   1.191 +	const __typeof__(*(ptr)) __user *__gu_ptr = (ptr);				\
   1.192 +	__typeof__ (size) __gu_size = (size);						\
   1.193 +	long __gu_err = -EFAULT;							\
   1.194 +	unsigned long __gu_val = 0;							\
   1.195 +	if (!check || __access_ok(__gu_ptr, size, segment))				\
   1.196 +		switch (__gu_size) {							\
   1.197 +		      case 1: __get_user_size(__gu_val, __gu_ptr, 1, __gu_err); break;	\
   1.198 +		      case 2: __get_user_size(__gu_val, __gu_ptr, 2, __gu_err); break;	\
   1.199 +		      case 4: __get_user_size(__gu_val, __gu_ptr, 4, __gu_err); break;	\
   1.200 +		      case 8: __get_user_size(__gu_val, __gu_ptr, 8, __gu_err); break;	\
   1.201 +		      default: __get_user_unknown(); break;				\
   1.202 +		}									\
   1.203 +	(x) = (__typeof__(*(__gu_ptr))) __gu_val;					\
   1.204 +	__gu_err;									\
   1.205 +})
   1.206 +
   1.207 +#define __get_user_nocheck(x, ptr, size)	__do_get_user(0, x, ptr, size, KERNEL_DS)
   1.208 +#define __get_user_check(x, ptr, size, segment)	__do_get_user(1, x, ptr, size, segment)
   1.209 +
   1.210 +extern void __put_user_unknown (void);
   1.211 +
   1.212 +/*
   1.213 + * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
   1.214 + * could clobber r8 (among others).  Thus, be careful not to evaluate them while using r8.
   1.215 + */
   1.216 +#define __do_put_user(check, x, ptr, size, segment)					\
   1.217 +({											\
   1.218 +	__typeof__ (x) __pu_x = (x);							\
   1.219 +	__typeof__ (*(ptr)) __user *__pu_ptr = (ptr);					\
   1.220 +	__typeof__ (size) __pu_size = (size);						\
   1.221 +	long __pu_err = -EFAULT;							\
   1.222 +											\
   1.223 +	if (!check || __access_ok(__pu_ptr, __pu_size, segment))			\
   1.224 +		switch (__pu_size) {							\
   1.225 +		      case 1: __put_user_size(__pu_x, __pu_ptr, 1, __pu_err); break;	\
   1.226 +		      case 2: __put_user_size(__pu_x, __pu_ptr, 2, __pu_err); break;	\
   1.227 +		      case 4: __put_user_size(__pu_x, __pu_ptr, 4, __pu_err); break;	\
   1.228 +		      case 8: __put_user_size(__pu_x, __pu_ptr, 8, __pu_err); break;	\
   1.229 +		      default: __put_user_unknown(); break;				\
   1.230 +		}									\
   1.231 +	__pu_err;									\
   1.232 +})
   1.233 +
   1.234 +#define __put_user_nocheck(x, ptr, size)	__do_put_user(0, x, ptr, size, KERNEL_DS)
   1.235 +#define __put_user_check(x, ptr, size, segment)	__do_put_user(1, x, ptr, size, segment)
   1.236 +
   1.237 +/*
   1.238 + * Complex access routines
   1.239 + */
   1.240 +extern unsigned long __must_check __copy_user (void __user *to, const void __user *from,
   1.241 +					       unsigned long count);
   1.242 +
   1.243 +static inline unsigned long
   1.244 +__copy_to_user (void __user *to, const void *from, unsigned long count)
   1.245 +{
   1.246 +	return __copy_user(to, (__force void __user *) from, count);
   1.247 +}
   1.248 +
   1.249 +static inline unsigned long
   1.250 +__copy_from_user (void *to, const void __user *from, unsigned long count)
   1.251 +{
   1.252 +	return __copy_user((__force void __user *) to, from, count);
   1.253 +}
   1.254 +
   1.255 +#define __copy_to_user_inatomic		__copy_to_user
   1.256 +#define __copy_from_user_inatomic	__copy_from_user
   1.257 +#define copy_to_user(to, from, n)							\
   1.258 +({											\
   1.259 +	void __user *__cu_to = (to);							\
   1.260 +	const void *__cu_from = (from);							\
   1.261 +	long __cu_len = (n);								\
   1.262 +											\
   1.263 +	if (__access_ok(__cu_to, __cu_len, get_fs()))					\
   1.264 +		__cu_len = __copy_user(__cu_to, (__force void __user *) __cu_from, __cu_len);	\
   1.265 +	__cu_len;									\
   1.266 +})
   1.267 +
   1.268 +#define copy_from_user(to, from, n)							\
   1.269 +({											\
   1.270 +	void *__cu_to = (to);								\
   1.271 +	const void __user *__cu_from = (from);						\
   1.272 +	long __cu_len = (n);								\
   1.273 +											\
   1.274 +	__chk_user_ptr(__cu_from);							\
   1.275 +	if (__access_ok(__cu_from, __cu_len, get_fs()))					\
   1.276 +		__cu_len = __copy_user((__force void __user *) __cu_to, __cu_from, __cu_len);	\
   1.277 +	__cu_len;									\
   1.278 +})
   1.279 +
   1.280 +#define __copy_in_user(to, from, size)	__copy_user((to), (from), (size))
   1.281 +
   1.282 +static inline unsigned long
   1.283 +copy_in_user (void __user *to, const void __user *from, unsigned long n)
   1.284 +{
   1.285 +	if (likely(access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n)))
   1.286 +		n = __copy_user(to, from, n);
   1.287 +	return n;
   1.288 +}
   1.289 +
   1.290 +extern unsigned long __do_clear_user (void __user *, unsigned long);
   1.291 +
   1.292 +#define __clear_user(to, n)		__do_clear_user(to, n)
   1.293 +
   1.294 +#define clear_user(to, n)					\
   1.295 +({								\
   1.296 +	unsigned long __cu_len = (n);				\
   1.297 +	if (__access_ok(to, __cu_len, get_fs()))		\
   1.298 +		__cu_len = __do_clear_user(to, __cu_len);	\
   1.299 +	__cu_len;						\
   1.300 +})
   1.301 +
   1.302 +
   1.303 +/*
   1.304 + * Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else
   1.305 + * strlen.
   1.306 + */
   1.307 +extern long __must_check __strncpy_from_user (char *to, const char __user *from, long to_len);
   1.308 +
   1.309 +#define strncpy_from_user(to, from, n)					\
   1.310 +({									\
   1.311 +	const char __user * __sfu_from = (from);			\
   1.312 +	long __sfu_ret = -EFAULT;					\
   1.313 +	if (__access_ok(__sfu_from, 0, get_fs()))			\
   1.314 +		__sfu_ret = __strncpy_from_user((to), __sfu_from, (n));	\
   1.315 +	__sfu_ret;							\
   1.316 +})
   1.317 +
   1.318 +/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
   1.319 +extern unsigned long __strlen_user (const char __user *);
   1.320 +
   1.321 +#define strlen_user(str)				\
   1.322 +({							\
   1.323 +	const char __user *__su_str = (str);		\
   1.324 +	unsigned long __su_ret = 0;			\
   1.325 +	if (__access_ok(__su_str, 0, get_fs()))		\
   1.326 +		__su_ret = __strlen_user(__su_str);	\
   1.327 +	__su_ret;					\
   1.328 +})
   1.329 +
   1.330 +/*
   1.331 + * Returns: 0 if exception before NUL or reaching the supplied limit
   1.332 + * (N), a value greater than N if the limit would be exceeded, else
   1.333 + * strlen.
   1.334 + */
   1.335 +extern unsigned long __strnlen_user (const char __user *, long);
   1.336 +
   1.337 +#define strnlen_user(str, len)					\
   1.338 +({								\
   1.339 +	const char __user *__su_str = (str);			\
   1.340 +	unsigned long __su_ret = 0;				\
   1.341 +	if (__access_ok(__su_str, 0, get_fs()))			\
   1.342 +		__su_ret = __strnlen_user(__su_str, len);	\
   1.343 +	__su_ret;						\
   1.344 +})
   1.345 +
   1.346 +/* Generic code can't deal with the location-relative format that we use for compactness.  */
   1.347 +#define ARCH_HAS_SORT_EXTABLE
   1.348 +#define ARCH_HAS_SEARCH_EXTABLE
   1.349 +
   1.350 +struct exception_table_entry {
   1.351 +	int addr;	/* location-relative address of insn this fixup is for */
   1.352 +	int cont;	/* location-relative continuation addr.; if bit 2 is set, r9 is set to 0 */
   1.353 +};
   1.354 +
   1.355 +extern void ia64_handle_exception (struct pt_regs *regs, const struct exception_table_entry *e);
   1.356 +extern const struct exception_table_entry *search_exception_tables (unsigned long addr);
   1.357 +
   1.358 +static inline int
   1.359 +ia64_done_with_exception (struct pt_regs *regs)
   1.360 +{
   1.361 +	const struct exception_table_entry *e;
   1.362 +	e = search_exception_tables(regs->cr_iip + ia64_psr(regs)->ri);
   1.363 +	if (e) {
   1.364 +		ia64_handle_exception(regs, e);
   1.365 +		return 1;
   1.366 +	}
   1.367 +	return 0;
   1.368 +}
   1.369 +
   1.370 +#define ARCH_HAS_TRANSLATE_MEM_PTR	1
   1.371 +static __inline__ char *
   1.372 +xlate_dev_mem_ptr (unsigned long p)
   1.373 +{
   1.374 +	struct page *page;
   1.375 +	char * ptr;
   1.376 +
   1.377 +	page = pfn_to_page(p >> PAGE_SHIFT);
   1.378 +	if (PageUncached(page))
   1.379 +		ptr = (char *)p + __IA64_UNCACHED_OFFSET;
   1.380 +	else
   1.381 +		ptr = __va(p);
   1.382 +
   1.383 +	return ptr;
   1.384 +}
   1.385 +
   1.386 +/*
   1.387 + * Convert a virtual cached kernel memory pointer to an uncached pointer
   1.388 + */
   1.389 +static __inline__ char *
   1.390 +xlate_dev_kmem_ptr (char * p)
   1.391 +{
   1.392 +	struct page *page;
   1.393 +	char * ptr;
   1.394 +
   1.395 +	page = virt_to_page((unsigned long)p >> PAGE_SHIFT);
   1.396 +	if (PageUncached(page))
   1.397 +		ptr = (char *)__pa(p) + __IA64_UNCACHED_OFFSET;
   1.398 +	else
   1.399 +		ptr = p;
   1.400 +
   1.401 +	return ptr;
   1.402 +}
   1.403 +
   1.404 +#endif /* _ASM_IA64_UACCESS_H */