x86: merge common parts of uaccess.
[linux-2.6-block.git] / include / asm-x86 / uaccess.h
CommitLineData
ca233862
GC
1#ifndef _ASM_UACCES_H_
2#define _ASM_UACCES_H_
3/*
4 * User space memory access functions
5 */
6#include <linux/errno.h>
7#include <linux/compiler.h>
8#include <linux/thread_info.h>
9#include <linux/prefetch.h>
10#include <linux/string.h>
11#include <asm/asm.h>
12#include <asm/page.h>
13
14#define VERIFY_READ 0
15#define VERIFY_WRITE 1
16
17/*
18 * The fs value determines whether argument validity checking should be
19 * performed or not. If get_fs() == USER_DS, checking is performed, with
20 * get_fs() == KERNEL_DS, checking is bypassed.
21 *
22 * For historical reasons, these macros are grossly misnamed.
23 */
24
25#define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
26
27#define KERNEL_DS MAKE_MM_SEG(-1UL)
28#define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
29
30#define get_ds() (KERNEL_DS)
31#define get_fs() (current_thread_info()->addr_limit)
32#define set_fs(x) (current_thread_info()->addr_limit = (x))
33
34#define segment_eq(a, b) ((a).seg == (b).seg)
35
36/*
37 * Test whether a block of memory is a valid user space address.
38 * Returns 0 if the range is valid, nonzero otherwise.
39 *
40 * This is equivalent to the following test:
41 * (u33)addr + (u33)size >= (u33)current->addr_limit.seg (u65 for x86_64)
42 *
43 * This needs 33-bit (65-bit for x86_64) arithmetic. We have a carry...
44 */
45
46#define __range_not_ok(addr, size) \
47({ \
48 unsigned long flag, roksum; \
49 __chk_user_ptr(addr); \
50 asm("add %3,%1 ; sbb %0,%0 ; cmp %1,%4 ; sbb $0,%0" \
51 : "=&r" (flag), "=r" (roksum) \
52 : "1" (addr), "g" ((long)(size)), \
53 "rm" (current_thread_info()->addr_limit.seg)); \
54 flag; \
55})
56
57/**
58 * access_ok: - Checks if a user space pointer is valid
59 * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
60 * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
61 * to write to a block, it is always safe to read from it.
62 * @addr: User space pointer to start of block to check
63 * @size: Size of block to check
64 *
65 * Context: User context only. This function may sleep.
66 *
67 * Checks if a pointer to a block of memory in user space is valid.
68 *
69 * Returns true (nonzero) if the memory block may be valid, false (zero)
70 * if it is definitely invalid.
71 *
72 * Note that, depending on architecture, this function probably just
73 * checks that the pointer is in the user space range - after calling
74 * this function, memory access functions may still return -EFAULT.
75 */
76#define access_ok(type, addr, size) (likely(__range_not_ok(addr, size) == 0))
77
78/*
79 * The exception table consists of pairs of addresses: the first is the
80 * address of an instruction that is allowed to fault, and the second is
81 * the address at which the program should continue. No registers are
82 * modified, so it is entirely up to the continuation code to figure out
83 * what to do.
84 *
85 * All the routines below use bits of fixup code that are out of line
86 * with the main instruction path. This means when everything is well,
87 * we don't even have to jump over them. Further, they do not intrude
88 * on our cache or tlb entries.
89 */
90
91struct exception_table_entry {
92 unsigned long insn, fixup;
93};
94
95extern int fixup_exception(struct pt_regs *regs);
96
97/*
98 * These are the main single-value transfer routines. They automatically
99 * use the right size if we just have the right pointer type.
100 *
101 * This gets kind of ugly. We want to return _two_ values in "get_user()"
102 * and yet we don't want to do any pointers, because that is too much
103 * of a performance impact. Thus we have a few rather ugly macros here,
104 * and hide all the ugliness from the user.
105 *
106 * The "__xxx" versions of the user access functions are versions that
107 * do not verify the address space, that must have been done previously
108 * with a separate "access_ok()" call (this is used when we do multiple
109 * accesses to the same area of user memory).
110 */
111
112extern int __get_user_1(void);
113extern int __get_user_2(void);
114extern int __get_user_4(void);
115extern int __get_user_8(void);
116extern int __get_user_bad(void);
117
118#define __get_user_x(size, ret, x, ptr) \
119 asm volatile("call __get_user_" #size \
120 : "=a" (ret),"=d" (x) \
121 : "0" (ptr)) \
122
96a388de
TG
123#ifdef CONFIG_X86_32
124# include "uaccess_32.h"
125#else
126# include "uaccess_64.h"
127#endif
ca233862
GC
128
129#endif