2 * Copyright (C) 2002 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
6 #include "linux/compiler.h"
7 #include "linux/stddef.h"
8 #include "linux/kernel.h"
9 #include "linux/string.h"
11 #include "linux/hardirq.h"
12 #include "linux/highmem.h"
14 #include "asm/pgtable.h"
15 #include "asm/uaccess.h"
16 #include "kern_util.h"
19 extern void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
22 static unsigned long maybe_map(unsigned long virt, int is_write)
27 void *phys = um_virt_to_phys(current, virt, &pte);
30 if(IS_ERR(phys) || (is_write && !pte_write(pte))){
31 err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
34 phys = um_virt_to_phys(current, virt, NULL);
39 return((unsigned long) phys);
42 static int do_op_one_page(unsigned long addr, int len, int is_write,
43 int (*op)(unsigned long addr, int len, void *arg), void *arg)
48 addr = maybe_map(addr, is_write);
52 page = phys_to_page(addr);
53 addr = (unsigned long) kmap_atomic(page, KM_UML_USERCOPY) + (addr & ~PAGE_MASK);
55 n = (*op)(addr, len, arg);
57 kunmap_atomic(page, KM_UML_USERCOPY);
62 static void do_buffer_op(void *jmpbuf, void *arg_ptr)
66 int len, is_write, size, remain, n;
67 int (*op)(unsigned long, int, void *);
71 va_copy(args, *(va_list *)arg_ptr);
72 addr = va_arg(args, unsigned long);
73 len = va_arg(args, int);
74 is_write = va_arg(args, int);
75 op = va_arg(args, void *);
76 arg = va_arg(args, void *);
77 res = va_arg(args, int *);
79 size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
82 current->thread.fault_catcher = jmpbuf;
83 n = do_op_one_page(addr, size, is_write, op, arg);
85 *res = (n < 0 ? remain : 0);
96 while(addr < ((addr + remain) & PAGE_MASK)){
97 n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
99 *res = (n < 0 ? remain : 0);
111 n = do_op_one_page(addr, remain, is_write, op, arg);
113 *res = (n < 0 ? remain : 0);
116 current->thread.fault_catcher = NULL;
119 static int buffer_op(unsigned long addr, int len, int is_write,
120 int (*op)(unsigned long addr, int len, void *arg),
125 faulted = setjmp_wrapper(do_buffer_op, addr, len, is_write, op, arg,
130 return(addr + len - (unsigned long) current->thread.fault_addr);
133 static int copy_chunk_from_user(unsigned long from, int len, void *arg)
135 unsigned long *to_ptr = arg, to = *to_ptr;
137 memcpy((void *) to, (void *) from, len);
142 int copy_from_user_skas(void *to, const void __user *from, int n)
144 if(segment_eq(get_fs(), KERNEL_DS)){
145 memcpy(to, (__force void*)from, n);
149 return(access_ok(VERIFY_READ, from, n) ?
150 buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to):
154 static int copy_chunk_to_user(unsigned long to, int len, void *arg)
156 unsigned long *from_ptr = arg, from = *from_ptr;
158 memcpy((void *) to, (void *) from, len);
163 int copy_to_user_skas(void __user *to, const void *from, int n)
165 if(segment_eq(get_fs(), KERNEL_DS)){
166 memcpy((__force void*)to, from, n);
170 return(access_ok(VERIFY_WRITE, to, n) ?
171 buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from) :
175 static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
177 char **to_ptr = arg, *to = *to_ptr;
180 strncpy(to, (void *) from, len);
181 n = strnlen(to, len);
189 int strncpy_from_user_skas(char *dst, const char __user *src, int count)
194 if(segment_eq(get_fs(), KERNEL_DS)){
195 strncpy(dst, (__force void*)src, count);
196 return(strnlen(dst, count));
199 if(!access_ok(VERIFY_READ, src, 1))
202 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
206 return(strnlen(dst, count));
209 static int clear_chunk(unsigned long addr, int len, void *unused)
211 memset((void *) addr, 0, len);
215 int __clear_user_skas(void __user *mem, int len)
217 return(buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL));
220 int clear_user_skas(void __user *mem, int len)
222 if(segment_eq(get_fs(), KERNEL_DS)){
223 memset((__force void*)mem, 0, len);
227 return(access_ok(VERIFY_WRITE, mem, len) ?
228 buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL) : len);
231 static int strnlen_chunk(unsigned long str, int len, void *arg)
233 int *len_ptr = arg, n;
235 n = strnlen((void *) str, len);
243 int strnlen_user_skas(const void __user *str, int len)
247 if(segment_eq(get_fs(), KERNEL_DS))
248 return(strnlen((__force char*)str, len) + 1);
250 n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
257 * Overrides for Emacs so that we follow Linus's tabbing style.
258 * Emacs will notice this stuff at the end of the file and automatically
259 * adjust the settings for this buffer only. This must remain at the end
261 * ---------------------------------------------------------------------------
263 * c-file-style: "linux"