blob: e22c96993db3b07545e107adb57f2ef1d8ddb608 [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{
Jeff Dike8efa3c92008-02-04 22:31:07 -080061 jmp_buf buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 struct page *page;
Jeff Dikeca77b552008-02-04 22:30:55 -080063 pte_t *pte;
Jeff Dike8efa3c92008-02-04 22:31:07 -080064 int n, faulted;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Jeff Dikeca77b552008-02-04 22:30:55 -080066 pte = maybe_map(addr, is_write);
67 if (pte == NULL)
Jeff Dike8ca842c2007-10-16 01:27:08 -070068 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Jeff Dikeca77b552008-02-04 22:30:55 -080070 page = pte_page(*pte);
Jeff Dike8ca842c2007-10-16 01:27:08 -070071 addr = (unsigned long) kmap_atomic(page, KM_UML_USERCOPY) +
72 (addr & ~PAGE_MASK);
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070073
Jeff Dike8efa3c92008-02-04 22:31:07 -080074 current->thread.fault_catcher = &buf;
75
76 faulted = UML_SETJMP(&buf);
77 if (faulted == 0)
78 n = (*op)(addr, len, arg);
79 else
80 n = -1;
81
82 current->thread.fault_catcher = NULL;
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070083
84 kunmap_atomic(page, KM_UML_USERCOPY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Jeff Dike8ca842c2007-10-16 01:27:08 -070086 return n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Jeff Dike8efa3c92008-02-04 22:31:07 -080089static int buffer_op(unsigned long addr, int len, int is_write,
90 int (*op)(unsigned long, int, void *), void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Jeff Dike8efa3c92008-02-04 22:31:07 -080092 int size, remain, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
95 remain = len;
96
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -070097 n = do_op_one_page(addr, size, is_write, op, arg);
Jeff Dike8ca842c2007-10-16 01:27:08 -070098 if (n != 0) {
Jeff Dike8efa3c92008-02-04 22:31:07 -080099 remain = (n < 0 ? remain : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 goto out;
101 }
102
103 addr += size;
104 remain -= size;
Jeff Dike8efa3c92008-02-04 22:31:07 -0800105 if (remain == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Jeff Dike8efa3c92008-02-04 22:31:07 -0800108 while (addr < ((addr + remain) & PAGE_MASK)) {
Paolo 'Blaisorblade' Giarrusso47e52432006-07-01 04:36:19 -0700109 n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700110 if (n != 0) {
Jeff Dike8efa3c92008-02-04 22:31:07 -0800111 remain = (n < 0 ? remain : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 goto out;
113 }
114
115 addr += PAGE_SIZE;
116 remain -= PAGE_SIZE;
117 }
Jeff Dike8efa3c92008-02-04 22:31:07 -0800118 if (remain == 0)
119 goto out;
120
121 n = do_op_one_page(addr, remain, is_write, op, arg);
122 if (n != 0) {
123 remain = (n < 0 ? remain : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 goto out;
125 }
126
Jeff Dike8efa3c92008-02-04 22:31:07 -0800127 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 out:
Jeff Dike8efa3c92008-02-04 22:31:07 -0800129 return remain;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
132static int copy_chunk_from_user(unsigned long from, int len, void *arg)
133{
134 unsigned long *to_ptr = arg, to = *to_ptr;
135
136 memcpy((void *) to, (void *) from, len);
137 *to_ptr += len;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700138 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139}
140
Jeff Dike6aa802c2007-10-16 01:26:56 -0700141int copy_from_user(void *to, const void __user *from, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700143 if (segment_eq(get_fs(), KERNEL_DS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 memcpy(to, (__force void*)from, n);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
Jeff Dike8ca842c2007-10-16 01:27:08 -0700148 return access_ok(VERIFY_READ, from, n) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to):
Jeff Dike8ca842c2007-10-16 01:27:08 -0700150 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153static int copy_chunk_to_user(unsigned long to, int len, void *arg)
154{
155 unsigned long *from_ptr = arg, from = *from_ptr;
156
157 memcpy((void *) to, (void *) from, len);
158 *from_ptr += len;
Jeff Dike8ca842c2007-10-16 01:27:08 -0700159 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Jeff Dike6aa802c2007-10-16 01:26:56 -0700162int copy_to_user(void __user *to, const void *from, int n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700164 if (segment_eq(get_fs(), KERNEL_DS)) {
165 memcpy((__force void *) to, from, n);
166 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 }
168
Jeff Dike8ca842c2007-10-16 01:27:08 -0700169 return access_ok(VERIFY_WRITE, to, n) ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from) :
Jeff Dike8ca842c2007-10-16 01:27:08 -0700171 n;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172}
173
174static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
175{
176 char **to_ptr = arg, *to = *to_ptr;
177 int n;
178
179 strncpy(to, (void *) from, len);
180 n = strnlen(to, len);
181 *to_ptr += n;
182
Jeff Dike8ca842c2007-10-16 01:27:08 -0700183 if (n < len)
184 return 1;
185 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Jeff Dike6aa802c2007-10-16 01:26:56 -0700188int strncpy_from_user(char *dst, const char __user *src, int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 int n;
191 char *ptr = dst;
192
Jeff Dike8ca842c2007-10-16 01:27:08 -0700193 if (segment_eq(get_fs(), KERNEL_DS)) {
194 strncpy(dst, (__force void *) src, count);
195 return strnlen(dst, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
Jeff Dike8ca842c2007-10-16 01:27:08 -0700198 if (!access_ok(VERIFY_READ, src, 1))
199 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
202 &ptr);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700203 if (n != 0)
204 return -EFAULT;
205 return strnlen(dst, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208static int clear_chunk(unsigned long addr, int len, void *unused)
209{
210 memset((void *) addr, 0, len);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Jeff Dike6aa802c2007-10-16 01:26:56 -0700214int __clear_user(void __user *mem, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700216 return buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Jeff Dike6aa802c2007-10-16 01:26:56 -0700219int clear_user(void __user *mem, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Jeff Dike8ca842c2007-10-16 01:27:08 -0700221 if (segment_eq(get_fs(), KERNEL_DS)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 memset((__force void*)mem, 0, len);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700223 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
225
Jeff Dike8ca842c2007-10-16 01:27:08 -0700226 return access_ok(VERIFY_WRITE, mem, len) ?
227 buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL) : len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
230static int strnlen_chunk(unsigned long str, int len, void *arg)
231{
232 int *len_ptr = arg, n;
233
234 n = strnlen((void *) str, len);
235 *len_ptr += n;
236
Jeff Dike8ca842c2007-10-16 01:27:08 -0700237 if (n < len)
238 return 1;
239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Jeff Dike6aa802c2007-10-16 01:26:56 -0700242int strnlen_user(const void __user *str, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int count = 0, n;
245
Jeff Dike8ca842c2007-10-16 01:27:08 -0700246 if (segment_eq(get_fs(), KERNEL_DS))
247 return strnlen((__force char*)str, len) + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
249 n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
Jeff Dike8ca842c2007-10-16 01:27:08 -0700250 if (n == 0)
251 return count + 1;
252 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}