blob: 1d8b119f2d0e12f9930b5f3092df793f8d5c1803 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Dike8ca842c2007-10-16 01:27:08 -07002 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 * Licensed under the GPL
4 */
5
Jeff Dike8ca842c2007-10-16 01:27:08 -07006#include "linux/err.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include "linux/highmem.h"
Jeff Dike8ca842c2007-10-16 01:27:08 -07008#include "linux/mm.h"
9#include "asm/current.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "asm/page.h"
11#include "asm/pgtable.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include "kern_util.h"
Gennady Sharapov4fef0c12006-01-18 17:42:41 -080013#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15extern void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
16 pte_t *pte_out);
17
18static unsigned long maybe_map(unsigned long virt, int is_write)
19{
20 pte_t pte;
21 int err;
22
23 void *phys = um_virt_to_phys(current, virt, &pte);
24 int dummy_code;
25
Jeff Dike8ca842c2007-10-16 01:27:08 -070026 if (IS_ERR(phys) || (is_write && !pte_write(pte))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
Jeff Dike8ca842c2007-10-16 01:27:08 -070028 if (err)
29 return -1UL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 phys = um_virt_to_phys(current, virt, NULL);
31 }
Jeff Dike8ca842c2007-10-16 01:27:08 -070032 if (IS_ERR(phys))
33 phys = (void *) -1;
Jeff Dike2d58cc92005-05-06 21:30:55 -070034
Jeff Dike8ca842c2007-10-16 01:27:08 -070035 return (unsigned long) phys;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036}
37
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070038static int do_op_one_page(unsigned long addr, int len, int is_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 int (*op)(unsigned long addr, int len, void *arg), void *arg)
40{
41 struct page *page;
42 int n;
43
44 addr = maybe_map(addr, is_write);
Jeff Dike8ca842c2007-10-16 01:27:08 -070045 if (addr == -1UL)
46 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48 page = phys_to_page(addr);
Jeff Dike8ca842c2007-10-16 01:27:08 -070049 addr = (unsigned long) kmap_atomic(page, KM_UML_USERCOPY) +
50 (addr & ~PAGE_MASK);
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 n = (*op)(addr, len, arg);
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070053
54 kunmap_atomic(page, KM_UML_USERCOPY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Jeff Dike8ca842c2007-10-16 01:27:08 -070056 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057}
58
59static void do_buffer_op(void *jmpbuf, void *arg_ptr)
60{
61 va_list args;
62 unsigned long addr;
63 int len, is_write, size, remain, n;
64 int (*op)(unsigned long, int, void *);
65 void *arg;
66 int *res;
67
Paolo 'Blaisorblade' Giarrussoe9c52712005-05-01 08:58:54 -070068 va_copy(args, *(va_list *)arg_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 addr = va_arg(args, unsigned long);
70 len = va_arg(args, int);
71 is_write = va_arg(args, int);
72 op = va_arg(args, void *);
73 arg = va_arg(args, void *);
74 res = va_arg(args, int *);
75 va_end(args);
76 size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
77 remain = len;
78
79 current->thread.fault_catcher = jmpbuf;
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070080 n = do_op_one_page(addr, size, is_write, op, arg);
Jeff Dike8ca842c2007-10-16 01:27:08 -070081 if (n != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *res = (n < 0 ? remain : 0);
83 goto out;
84 }
85
86 addr += size;
87 remain -= size;
Jeff Dike8ca842c2007-10-16 01:27:08 -070088 if (remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 *res = 0;
90 goto out;
91 }
92
Jeff Dike8ca842c2007-10-16 01:27:08 -070093 while(addr < ((addr + remain) & PAGE_MASK)) {
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070094 n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
Jeff Dike8ca842c2007-10-16 01:27:08 -070095 if (n != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *res = (n < 0 ? remain : 0);
97 goto out;
98 }
99
100 addr += PAGE_SIZE;
101 remain -= PAGE_SIZE;
102 }
Jeff Dike8ca842c2007-10-16 01:27:08 -0700103 if (remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 *res = 0;
105 goto out;
106 }
107
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -0700108 n = do_op_one_page(addr, remain, is_write, op, arg);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700109 if (n != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *res = (n < 0 ? remain : 0);
111 else *res = 0;
112 out:
113 current->thread.fault_catcher = NULL;
114}
115
116static int buffer_op(unsigned long addr, int len, int is_write,
117 int (*op)(unsigned long addr, int len, void *arg),
118 void *arg)
119{
120 int faulted, res;
121
122 faulted = setjmp_wrapper(do_buffer_op, addr, len, is_write, op, arg,
123 &res);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700124 if (!faulted)
125 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Jeff Dike8ca842c2007-10-16 01:27:08 -0700127 return addr + len - (unsigned long) current->thread.fault_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
130static int copy_chunk_from_user(unsigned long from, int len, void *arg)
131{
132 unsigned long *to_ptr = arg, to = *to_ptr;
133
134 memcpy((void *) to, (void *) from, len);
135 *to_ptr += len;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700136 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Jeff Dike6aa802c2007-10-16 01:26:56 -0700139int copy_from_user(void *to, const void __user *from, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700141 if (segment_eq(get_fs(), KERNEL_DS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 memcpy(to, (__force void*)from, n);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 }
145
Jeff Dike8ca842c2007-10-16 01:27:08 -0700146 return access_ok(VERIFY_READ, from, n) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to):
Jeff Dike8ca842c2007-10-16 01:27:08 -0700148 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static int copy_chunk_to_user(unsigned long to, int len, void *arg)
152{
153 unsigned long *from_ptr = arg, from = *from_ptr;
154
155 memcpy((void *) to, (void *) from, len);
156 *from_ptr += len;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Jeff Dike6aa802c2007-10-16 01:26:56 -0700160int copy_to_user(void __user *to, const void *from, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700162 if (segment_eq(get_fs(), KERNEL_DS)) {
163 memcpy((__force void *) to, from, n);
164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Jeff Dike8ca842c2007-10-16 01:27:08 -0700167 return access_ok(VERIFY_WRITE, to, n) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from) :
Jeff Dike8ca842c2007-10-16 01:27:08 -0700169 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
173{
174 char **to_ptr = arg, *to = *to_ptr;
175 int n;
176
177 strncpy(to, (void *) from, len);
178 n = strnlen(to, len);
179 *to_ptr += n;
180
Jeff Dike8ca842c2007-10-16 01:27:08 -0700181 if (n < len)
182 return 1;
183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Jeff Dike6aa802c2007-10-16 01:26:56 -0700186int strncpy_from_user(char *dst, const char __user *src, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187{
188 int n;
189 char *ptr = dst;
190
Jeff Dike8ca842c2007-10-16 01:27:08 -0700191 if (segment_eq(get_fs(), KERNEL_DS)) {
192 strncpy(dst, (__force void *) src, count);
193 return strnlen(dst, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 }
195
Jeff Dike8ca842c2007-10-16 01:27:08 -0700196 if (!access_ok(VERIFY_READ, src, 1))
197 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198
199 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
200 &ptr);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700201 if (n != 0)
202 return -EFAULT;
203 return strnlen(dst, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
206static int clear_chunk(unsigned long addr, int len, void *unused)
207{
208 memset((void *) addr, 0, len);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Jeff Dike6aa802c2007-10-16 01:26:56 -0700212int __clear_user(void __user *mem, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700214 return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Jeff Dike6aa802c2007-10-16 01:26:56 -0700217int clear_user(void __user *mem, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700219 if (segment_eq(get_fs(), KERNEL_DS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 memset((__force void*)mem, 0, len);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700221 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
223
Jeff Dike8ca842c2007-10-16 01:27:08 -0700224 return access_ok(VERIFY_WRITE, mem, len) ?
225 buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL) : len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228static int strnlen_chunk(unsigned long str, int len, void *arg)
229{
230 int *len_ptr = arg, n;
231
232 n = strnlen((void *) str, len);
233 *len_ptr += n;
234
Jeff Dike8ca842c2007-10-16 01:27:08 -0700235 if (n < len)
236 return 1;
237 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Jeff Dike6aa802c2007-10-16 01:26:56 -0700240int strnlen_user(const void __user *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 int count = 0, n;
243
Jeff Dike8ca842c2007-10-16 01:27:08 -0700244 if (segment_eq(get_fs(), KERNEL_DS))
245 return strnlen((__force char*)str, len) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700248 if (n == 0)
249 return count + 1;
250 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}