blob: 05b41dbc1dd98620292912689cddbce551cb492b [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 Dike8192ab42008-02-04 22:30:53 -08006#include <linux/err.h>
7#include <linux/highmem.h>
8#include <linux/mm.h>
9#include <linux/sched.h>
10#include <asm/current.h>
11#include <asm/page.h>
12#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "kern_util.h"
Gennady Sharapov4fef0c12006-01-18 17:42:41 -080014#include "os.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Jeff Dikeca77b552008-02-04 22:30:55 -080016pte_t *virt_to_pte(struct mm_struct *mm, unsigned long addr)
Jeff Dike9157f902008-02-04 22:30:52 -080017{
18 pgd_t *pgd;
19 pud_t *pud;
20 pmd_t *pmd;
Jeff Dike9157f902008-02-04 22:30:52 -080021
Jeff Dikeca77b552008-02-04 22:30:55 -080022 if (mm == NULL)
23 return NULL;
24
25 pgd = pgd_offset(mm, addr);
Jeff Dike9157f902008-02-04 22:30:52 -080026 if (!pgd_present(*pgd))
Jeff Dikeca77b552008-02-04 22:30:55 -080027 return NULL;
Jeff Dike9157f902008-02-04 22:30:52 -080028
29 pud = pud_offset(pgd, addr);
30 if (!pud_present(*pud))
Jeff Dikeca77b552008-02-04 22:30:55 -080031 return NULL;
Jeff Dike9157f902008-02-04 22:30:52 -080032
33 pmd = pmd_offset(pud, addr);
34 if (!pmd_present(*pmd))
Jeff Dikeca77b552008-02-04 22:30:55 -080035 return NULL;
Jeff Dike9157f902008-02-04 22:30:52 -080036
Jeff Dikeca77b552008-02-04 22:30:55 -080037 return pte_offset_kernel(pmd, addr);
Jeff Dike9157f902008-02-04 22:30:52 -080038}
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Jeff Dikeca77b552008-02-04 22:30:55 -080040static pte_t *maybe_map(unsigned long virt, int is_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Jeff Dikeca77b552008-02-04 22:30:55 -080042 pte_t *pte = virt_to_pte(current->mm, virt);
43 int err, dummy_code;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Jeff Dikeca77b552008-02-04 22:30:55 -080045 if ((pte == NULL) || !pte_present(*pte) ||
46 (is_write && !pte_write(*pte))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
Jeff Dike8ca842c2007-10-16 01:27:08 -070048 if (err)
Jeff Dikeca77b552008-02-04 22:30:55 -080049 return NULL;
50 pte = virt_to_pte(current->mm, virt);
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
Jeff Dikeca77b552008-02-04 22:30:55 -080052 if (!pte_present(*pte))
53 pte = NULL;
Jeff Dike2d58cc92005-05-06 21:30:55 -070054
Jeff Dikeca77b552008-02-04 22:30:55 -080055 return pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056}
57
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070058static int do_op_one_page(unsigned long addr, int len, int is_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 int (*op)(unsigned long addr, int len, void *arg), void *arg)
60{
61 struct page *page;
Jeff Dikeca77b552008-02-04 22:30:55 -080062 pte_t *pte;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 int n;
64
Jeff Dikeca77b552008-02-04 22:30:55 -080065 pte = maybe_map(addr, is_write);
66 if (pte == NULL)
Jeff Dike8ca842c2007-10-16 01:27:08 -070067 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Jeff Dikeca77b552008-02-04 22:30:55 -080069 page = pte_page(*pte);
Jeff Dike8ca842c2007-10-16 01:27:08 -070070 addr = (unsigned long) kmap_atomic(page, KM_UML_USERCOPY) +
71 (addr & ~PAGE_MASK);
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 n = (*op)(addr, len, arg);
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070074
75 kunmap_atomic(page, KM_UML_USERCOPY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Jeff Dike8ca842c2007-10-16 01:27:08 -070077 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078}
79
80static void do_buffer_op(void *jmpbuf, void *arg_ptr)
81{
82 va_list args;
83 unsigned long addr;
84 int len, is_write, size, remain, n;
85 int (*op)(unsigned long, int, void *);
86 void *arg;
87 int *res;
88
Paolo 'Blaisorblade' Giarrussoe9c52712005-05-01 08:58:54 -070089 va_copy(args, *(va_list *)arg_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 addr = va_arg(args, unsigned long);
91 len = va_arg(args, int);
92 is_write = va_arg(args, int);
93 op = va_arg(args, void *);
94 arg = va_arg(args, void *);
95 res = va_arg(args, int *);
96 va_end(args);
97 size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
98 remain = len;
99
100 current->thread.fault_catcher = jmpbuf;
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -0700101 n = do_op_one_page(addr, size, is_write, op, arg);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700102 if (n != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 *res = (n < 0 ? remain : 0);
104 goto out;
105 }
106
107 addr += size;
108 remain -= size;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700109 if (remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 *res = 0;
111 goto out;
112 }
113
Jeff Dike8ca842c2007-10-16 01:27:08 -0700114 while(addr < ((addr + remain) & PAGE_MASK)) {
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -0700115 n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700116 if (n != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 *res = (n < 0 ? remain : 0);
118 goto out;
119 }
120
121 addr += PAGE_SIZE;
122 remain -= PAGE_SIZE;
123 }
Jeff Dike8ca842c2007-10-16 01:27:08 -0700124 if (remain == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 *res = 0;
126 goto out;
127 }
128
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -0700129 n = do_op_one_page(addr, remain, is_write, op, arg);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700130 if (n != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 *res = (n < 0 ? remain : 0);
132 else *res = 0;
133 out:
134 current->thread.fault_catcher = NULL;
135}
136
137static int buffer_op(unsigned long addr, int len, int is_write,
138 int (*op)(unsigned long addr, int len, void *arg),
139 void *arg)
140{
141 int faulted, res;
142
143 faulted = setjmp_wrapper(do_buffer_op, addr, len, is_write, op, arg,
144 &res);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700145 if (!faulted)
146 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Jeff Dike8ca842c2007-10-16 01:27:08 -0700148 return addr + len - (unsigned long) current->thread.fault_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151static int copy_chunk_from_user(unsigned long from, int len, void *arg)
152{
153 unsigned long *to_ptr = arg, to = *to_ptr;
154
155 memcpy((void *) to, (void *) from, len);
156 *to_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_from_user(void *to, const void __user *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)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 memcpy(to, (__force void*)from, n);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700164 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
Jeff Dike8ca842c2007-10-16 01:27:08 -0700167 return access_ok(VERIFY_READ, from, n) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to):
Jeff Dike8ca842c2007-10-16 01:27:08 -0700169 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
172static int copy_chunk_to_user(unsigned long to, int len, void *arg)
173{
174 unsigned long *from_ptr = arg, from = *from_ptr;
175
176 memcpy((void *) to, (void *) from, len);
177 *from_ptr += len;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Jeff Dike6aa802c2007-10-16 01:26:56 -0700181int copy_to_user(void __user *to, const void *from, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700183 if (segment_eq(get_fs(), KERNEL_DS)) {
184 memcpy((__force void *) to, from, n);
185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
Jeff Dike8ca842c2007-10-16 01:27:08 -0700188 return access_ok(VERIFY_WRITE, to, n) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from) :
Jeff Dike8ca842c2007-10-16 01:27:08 -0700190 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
193static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
194{
195 char **to_ptr = arg, *to = *to_ptr;
196 int n;
197
198 strncpy(to, (void *) from, len);
199 n = strnlen(to, len);
200 *to_ptr += n;
201
Jeff Dike8ca842c2007-10-16 01:27:08 -0700202 if (n < len)
203 return 1;
204 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Jeff Dike6aa802c2007-10-16 01:26:56 -0700207int strncpy_from_user(char *dst, const char __user *src, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 int n;
210 char *ptr = dst;
211
Jeff Dike8ca842c2007-10-16 01:27:08 -0700212 if (segment_eq(get_fs(), KERNEL_DS)) {
213 strncpy(dst, (__force void *) src, count);
214 return strnlen(dst, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
Jeff Dike8ca842c2007-10-16 01:27:08 -0700217 if (!access_ok(VERIFY_READ, src, 1))
218 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
221 &ptr);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700222 if (n != 0)
223 return -EFAULT;
224 return strnlen(dst, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227static int clear_chunk(unsigned long addr, int len, void *unused)
228{
229 memset((void *) addr, 0, len);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Jeff Dike6aa802c2007-10-16 01:26:56 -0700233int __clear_user(void __user *mem, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700235 return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236}
237
Jeff Dike6aa802c2007-10-16 01:26:56 -0700238int clear_user(void __user *mem, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700240 if (segment_eq(get_fs(), KERNEL_DS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 memset((__force void*)mem, 0, len);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Jeff Dike8ca842c2007-10-16 01:27:08 -0700245 return access_ok(VERIFY_WRITE, mem, len) ?
246 buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL) : len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249static int strnlen_chunk(unsigned long str, int len, void *arg)
250{
251 int *len_ptr = arg, n;
252
253 n = strnlen((void *) str, len);
254 *len_ptr += n;
255
Jeff Dike8ca842c2007-10-16 01:27:08 -0700256 if (n < len)
257 return 1;
258 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Jeff Dike6aa802c2007-10-16 01:26:56 -0700261int strnlen_user(const void __user *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 int count = 0, n;
264
Jeff Dike8ca842c2007-10-16 01:27:08 -0700265 if (segment_eq(get_fs(), KERNEL_DS))
266 return strnlen((__force char*)str, len) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700269 if (n == 0)
270 return count + 1;
271 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}