uaccess.c 5.1 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2
 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
L
Linus Torvalds 已提交
3 4 5
 * Licensed under the GPL
 */

J
Jeff Dike 已提交
6 7 8
#include <linux/err.h>
#include <linux/highmem.h>
#include <linux/mm.h>
9
#include <linux/module.h>
J
Jeff Dike 已提交
10 11 12 13
#include <linux/sched.h>
#include <asm/current.h>
#include <asm/page.h>
#include <asm/pgtable.h>
14 15
#include <kern_util.h>
#include <os.h>
L
Linus Torvalds 已提交
16

J
Jeff Dike 已提交
17
pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr)
J
Jeff Dike 已提交
18 19 20 21 22
{
	pgd_t *pgd;
	pud_t *pud;
	pmd_t *pmd;

J
Jeff Dike 已提交
23 24 25 26
	if (mm == NULL)
		return NULL;

	pgd = pgd_offset(mm, addr);
J
Jeff Dike 已提交
27
	if (!pgd_present(*pgd))
J
Jeff Dike 已提交
28
		return NULL;
J
Jeff Dike 已提交
29 30 31

	pud = pud_offset(pgd, addr);
	if (!pud_present(*pud))
J
Jeff Dike 已提交
32
		return NULL;
J
Jeff Dike 已提交
33 34 35

	pmd = pmd_offset(pud, addr);
	if (!pmd_present(*pmd))
J
Jeff Dike 已提交
36
		return NULL;
J
Jeff Dike 已提交
37

J
Jeff Dike 已提交
38
	return pte_offset_kernel(pmd, addr);
J
Jeff Dike 已提交
39
}
L
Linus Torvalds 已提交
40

J
Jeff Dike 已提交
41
static pte_t *maybe_map(unsigned long virt, int is_write)
L
Linus Torvalds 已提交
42
{
J
Jeff Dike 已提交
43 44
	pte_t *pte = virt_to_pte(current->mm, virt);
	int err, dummy_code;
L
Linus Torvalds 已提交
45

J
Jeff Dike 已提交
46 47
	if ((pte == NULL) || !pte_present(*pte) ||
	    (is_write && !pte_write(*pte))) {
L
Linus Torvalds 已提交
48
		err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
49
		if (err)
J
Jeff Dike 已提交
50 51
			return NULL;
		pte = virt_to_pte(current->mm, virt);
L
Linus Torvalds 已提交
52
	}
J
Jeff Dike 已提交
53 54
	if (!pte_present(*pte))
		pte = NULL;
J
Jeff Dike 已提交
55

J
Jeff Dike 已提交
56
	return pte;
L
Linus Torvalds 已提交
57 58
}

59
static int do_op_one_page(unsigned long addr, int len, int is_write,
L
Linus Torvalds 已提交
60 61
		 int (*op)(unsigned long addr, int len, void *arg), void *arg)
{
J
Jeff Dike 已提交
62
	jmp_buf buf;
L
Linus Torvalds 已提交
63
	struct page *page;
J
Jeff Dike 已提交
64
	pte_t *pte;
J
Jeff Dike 已提交
65
	int n, faulted;
L
Linus Torvalds 已提交
66

J
Jeff Dike 已提交
67 68
	pte = maybe_map(addr, is_write);
	if (pte == NULL)
69
		return -1;
L
Linus Torvalds 已提交
70

J
Jeff Dike 已提交
71
	page = pte_page(*pte);
72
	addr = (unsigned long) kmap_atomic(page) +
73
		(addr & ~PAGE_MASK);
74

J
Jeff Dike 已提交
75 76 77 78 79 80 81 82 83
	current->thread.fault_catcher = &buf;

	faulted = UML_SETJMP(&buf);
	if (faulted == 0)
		n = (*op)(addr, len, arg);
	else
		n = -1;

	current->thread.fault_catcher = NULL;
84

85
	kunmap_atomic((void *)addr);
L
Linus Torvalds 已提交
86

87
	return n;
L
Linus Torvalds 已提交
88 89
}

J
Jeff Dike 已提交
90 91
static int buffer_op(unsigned long addr, int len, int is_write,
		     int (*op)(unsigned long, int, void *), void *arg)
L
Linus Torvalds 已提交
92
{
J
Jeff Dike 已提交
93 94
	int size, remain, n;

L
Linus Torvalds 已提交
95 96 97
	size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
	remain = len;

98
	n = do_op_one_page(addr, size, is_write, op, arg);
99
	if (n != 0) {
J
Jeff Dike 已提交
100
		remain = (n < 0 ? remain : 0);
L
Linus Torvalds 已提交
101 102 103 104 105
		goto out;
	}

	addr += size;
	remain -= size;
J
Jeff Dike 已提交
106
	if (remain == 0)
L
Linus Torvalds 已提交
107 108
		goto out;

J
Jeff Dike 已提交
109
	while (addr < ((addr + remain) & PAGE_MASK)) {
110
		n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
111
		if (n != 0) {
J
Jeff Dike 已提交
112
			remain = (n < 0 ? remain : 0);
L
Linus Torvalds 已提交
113 114 115 116 117 118
			goto out;
		}

		addr += PAGE_SIZE;
		remain -= PAGE_SIZE;
	}
J
Jeff Dike 已提交
119
	if (remain == 0)
L
Linus Torvalds 已提交
120 121
		goto out;

122
	n = do_op_one_page(addr, remain, is_write, op, arg);
J
Jeff Dike 已提交
123 124 125 126
	if (n != 0) {
		remain = (n < 0 ? remain : 0);
		goto out;
	}
L
Linus Torvalds 已提交
127

J
Jeff Dike 已提交
128 129 130
	return 0;
 out:
	return remain;
L
Linus Torvalds 已提交
131 132 133 134 135 136 137 138
}

static int copy_chunk_from_user(unsigned long from, int len, void *arg)
{
	unsigned long *to_ptr = arg, to = *to_ptr;

	memcpy((void *) to, (void *) from, len);
	*to_ptr += len;
139
	return 0;
L
Linus Torvalds 已提交
140 141
}

J
Jeff Dike 已提交
142
int copy_from_user(void *to, const void __user *from, int n)
L
Linus Torvalds 已提交
143
{
144
	if (segment_eq(get_fs(), KERNEL_DS)) {
L
Linus Torvalds 已提交
145
		memcpy(to, (__force void*)from, n);
146
		return 0;
L
Linus Torvalds 已提交
147 148
	}

149
	return access_ok(VERIFY_READ, from, n) ?
L
Linus Torvalds 已提交
150
	       buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to):
151
	       n;
L
Linus Torvalds 已提交
152
}
153
EXPORT_SYMBOL(copy_from_user);
L
Linus Torvalds 已提交
154 155 156 157 158 159 160

static int copy_chunk_to_user(unsigned long to, int len, void *arg)
{
	unsigned long *from_ptr = arg, from = *from_ptr;

	memcpy((void *) to, (void *) from, len);
	*from_ptr += len;
161
	return 0;
L
Linus Torvalds 已提交
162 163
}

J
Jeff Dike 已提交
164
int copy_to_user(void __user *to, const void *from, int n)
L
Linus Torvalds 已提交
165
{
166 167 168
	if (segment_eq(get_fs(), KERNEL_DS)) {
		memcpy((__force void *) to, from, n);
		return 0;
L
Linus Torvalds 已提交
169 170
	}

171
	return access_ok(VERIFY_WRITE, to, n) ?
L
Linus Torvalds 已提交
172
	       buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from) :
173
	       n;
L
Linus Torvalds 已提交
174
}
175
EXPORT_SYMBOL(copy_to_user);
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185

static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
{
	char **to_ptr = arg, *to = *to_ptr;
	int n;

	strncpy(to, (void *) from, len);
	n = strnlen(to, len);
	*to_ptr += n;

186 187 188
	if (n < len)
	        return 1;
	return 0;
L
Linus Torvalds 已提交
189 190
}

J
Jeff Dike 已提交
191
int strncpy_from_user(char *dst, const char __user *src, int count)
L
Linus Torvalds 已提交
192 193 194 195
{
	int n;
	char *ptr = dst;

196 197 198
	if (segment_eq(get_fs(), KERNEL_DS)) {
		strncpy(dst, (__force void *) src, count);
		return strnlen(dst, count);
L
Linus Torvalds 已提交
199 200
	}

201 202
	if (!access_ok(VERIFY_READ, src, 1))
		return -EFAULT;
L
Linus Torvalds 已提交
203 204 205

	n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
		      &ptr);
206 207 208
	if (n != 0)
		return -EFAULT;
	return strnlen(dst, count);
L
Linus Torvalds 已提交
209
}
210
EXPORT_SYMBOL(strncpy_from_user);
L
Linus Torvalds 已提交
211 212 213 214

static int clear_chunk(unsigned long addr, int len, void *unused)
{
	memset((void *) addr, 0, len);
215
	return 0;
L
Linus Torvalds 已提交
216 217
}

J
Jeff Dike 已提交
218
int __clear_user(void __user *mem, int len)
L
Linus Torvalds 已提交
219
{
220
	return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
L
Linus Torvalds 已提交
221 222
}

J
Jeff Dike 已提交
223
int clear_user(void __user *mem, int len)
L
Linus Torvalds 已提交
224
{
225
	if (segment_eq(get_fs(), KERNEL_DS)) {
L
Linus Torvalds 已提交
226
		memset((__force void*)mem, 0, len);
227
		return 0;
L
Linus Torvalds 已提交
228 229
	}

230 231
	return access_ok(VERIFY_WRITE, mem, len) ?
	       buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL) : len;
L
Linus Torvalds 已提交
232
}
233
EXPORT_SYMBOL(clear_user);
L
Linus Torvalds 已提交
234 235 236 237 238 239 240 241

static int strnlen_chunk(unsigned long str, int len, void *arg)
{
	int *len_ptr = arg, n;

	n = strnlen((void *) str, len);
	*len_ptr += n;

242 243 244
	if (n < len)
		return 1;
	return 0;
L
Linus Torvalds 已提交
245 246
}

J
Jeff Dike 已提交
247
int strnlen_user(const void __user *str, int len)
L
Linus Torvalds 已提交
248 249 250
{
	int count = 0, n;

251 252
	if (segment_eq(get_fs(), KERNEL_DS))
		return strnlen((__force char*)str, len) + 1;
L
Linus Torvalds 已提交
253 254

	n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
255 256 257
	if (n == 0)
		return count + 1;
	return -EFAULT;
L
Linus Torvalds 已提交
258
}
259
EXPORT_SYMBOL(strnlen_user);