2 * Copyright (C) 2002 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
6 #include "linux/stddef.h"
7 #include "linux/kernel.h"
8 #include "linux/string.h"
10 #include "linux/highmem.h"
12 #include "asm/pgtable.h"
13 #include "asm/uaccess.h"
14 #include "kern_util.h"
15 #include "user_util.h"
17 extern void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
20 static unsigned long maybe_map(unsigned long virt, int is_write)
25 void *phys = um_virt_to_phys(current, virt, &pte);
28 if(IS_ERR(phys) || (is_write && !pte_write(pte))){
29 err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
32 phys = um_virt_to_phys(current, virt, NULL);
34 return((unsigned long) phys);
37 static int do_op(unsigned long addr, int len, int is_write,
38 int (*op)(unsigned long addr, int len, void *arg), void *arg)
43 addr = maybe_map(addr, is_write);
47 page = phys_to_page(addr);
48 addr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
49 n = (*op)(addr, len, arg);
55 static void do_buffer_op(void *jmpbuf, void *arg_ptr)
57 va_list args = *((va_list *) arg_ptr);
58 unsigned long addr = va_arg(args, unsigned long);
59 int len = va_arg(args, int);
60 int is_write = va_arg(args, int);
61 int (*op)(unsigned long, int, void *) = va_arg(args, void *);
62 void *arg = va_arg(args, void *);
63 int *res = va_arg(args, int *);
64 int size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
67 current->thread.fault_catcher = jmpbuf;
68 n = do_op(addr, size, is_write, op, arg);
70 *res = (n < 0 ? remain : 0);
81 while(addr < ((addr + remain) & PAGE_MASK)){
82 n = do_op(addr, PAGE_SIZE, is_write, op, arg);
84 *res = (n < 0 ? remain : 0);
96 n = do_op(addr, remain, is_write, op, arg);
98 *res = (n < 0 ? remain : 0);
101 current->thread.fault_catcher = NULL;
104 static int buffer_op(unsigned long addr, int len, int is_write,
105 int (*op)(unsigned long addr, int len, void *arg),
110 faulted = setjmp_wrapper(do_buffer_op, addr, len, is_write, op, arg,
115 return(addr + len - (unsigned long) current->thread.fault_addr);
118 static int copy_chunk_from_user(unsigned long from, int len, void *arg)
120 unsigned long *to_ptr = arg, to = *to_ptr;
122 memcpy((void *) to, (void *) from, len);
127 int copy_from_user_skas(void *to, const void *from, int n)
129 if(segment_eq(get_fs(), KERNEL_DS)){
134 return(access_ok_skas(VERIFY_READ, from, n) ?
135 buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to):
139 static int copy_chunk_to_user(unsigned long to, int len, void *arg)
141 unsigned long *from_ptr = arg, from = *from_ptr;
143 memcpy((void *) to, (void *) from, len);
148 int copy_to_user_skas(void *to, const void *from, int n)
150 if(segment_eq(get_fs(), KERNEL_DS)){
155 return(access_ok_skas(VERIFY_WRITE, to, n) ?
156 buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from) :
160 static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
162 char **to_ptr = arg, *to = *to_ptr;
165 strncpy(to, (void *) from, len);
166 n = strnlen(to, len);
174 int strncpy_from_user_skas(char *dst, const char *src, int count)
179 if(segment_eq(get_fs(), KERNEL_DS)){
180 strncpy(dst, src, count);
181 return(strnlen(dst, count));
184 if(!access_ok_skas(VERIFY_READ, src, 1))
187 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
191 return(strnlen(dst, count));
194 static int clear_chunk(unsigned long addr, int len, void *unused)
196 memset((void *) addr, 0, len);
200 int __clear_user_skas(void *mem, int len)
202 return(buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL));
205 int clear_user_skas(void *mem, int len)
207 if(segment_eq(get_fs(), KERNEL_DS)){
212 return(access_ok_skas(VERIFY_WRITE, mem, len) ?
213 buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL) : len);
216 static int strnlen_chunk(unsigned long str, int len, void *arg)
218 int *len_ptr = arg, n;
220 n = strnlen((void *) str, len);
228 int strnlen_user_skas(const void *str, int len)
232 if(segment_eq(get_fs(), KERNEL_DS))
233 return(strnlen(str, len) + 1);
235 n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
242 * Overrides for Emacs so that we follow Linus's tabbing style.
243 * Emacs will notice this stuff at the end of the file and automatically
244 * adjust the settings for this buffer only. This must remain at the end
246 * ---------------------------------------------------------------------------
248 * c-file-style: "linux"