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"
16 extern void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
19 static unsigned long maybe_map(unsigned long virt, int is_write)
24 void *phys = um_virt_to_phys(current, virt, &pte);
27 if(IS_ERR(phys) || (is_write && !pte_write(pte))){
28 err = handle_page_fault(virt, 0, is_write, 0, &dummy_code);
31 phys = um_virt_to_phys(current, virt, NULL);
33 return((unsigned long) phys);
36 static int do_op(unsigned long addr, int len, int is_write,
37 int (*op)(unsigned long addr, int len, void *arg), void *arg)
42 addr = maybe_map(addr, is_write);
46 page = phys_to_page(addr);
47 addr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
48 n = (*op)(addr, len, arg);
54 static int buffer_op(unsigned long addr, int len, int is_write,
55 int (*op)(unsigned long addr, int len, void *arg),
58 int size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
61 n = do_op(addr, size, is_write, op, arg);
63 return(n < 0 ? remain : 0);
70 while(addr < ((addr + remain) & PAGE_MASK)){
71 n = do_op(addr, PAGE_SIZE, is_write, op, arg);
73 return(n < 0 ? remain : 0);
81 n = do_op(addr, remain, is_write, op, arg);
83 return(n < 0 ? remain : 0);
87 static int copy_chunk_from_user(unsigned long from, int len, void *arg)
89 unsigned long *to_ptr = arg, to = *to_ptr;
91 memcpy((void *) to, (void *) from, len);
96 int copy_from_user_skas(void *to, const void *from, int n)
98 if(segment_eq(get_fs(), KERNEL_DS)){
103 return(access_ok_skas(VERIFY_READ, from, n) ?
104 buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to):
108 static int copy_chunk_to_user(unsigned long to, int len, void *arg)
110 unsigned long *from_ptr = arg, from = *from_ptr;
112 memcpy((void *) to, (void *) from, len);
117 int copy_to_user_skas(void *to, const void *from, int n)
119 if(segment_eq(get_fs(), KERNEL_DS)){
124 return(access_ok_skas(VERIFY_WRITE, to, n) ?
125 buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from) :
129 static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
131 char **to_ptr = arg, *to = *to_ptr;
134 strncpy(to, (void *) from, len);
135 n = strnlen(to, len);
143 int strncpy_from_user_skas(char *dst, const char *src, int count)
148 if(segment_eq(get_fs(), KERNEL_DS)){
149 strncpy(dst, src, count);
150 return(strnlen(dst, count));
153 if(!access_ok_skas(VERIFY_READ, src, 1))
156 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
160 return(strnlen(dst, count));
163 static int clear_chunk(unsigned long addr, int len, void *unused)
165 memset((void *) addr, 0, len);
169 int __clear_user_skas(void *mem, int len)
171 return(buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL));
174 int clear_user_skas(void *mem, int len)
176 if(segment_eq(get_fs(), KERNEL_DS)){
181 return(access_ok_skas(VERIFY_WRITE, mem, len) ?
182 buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL) : len);
185 static int strnlen_chunk(unsigned long str, int len, void *arg)
187 int *len_ptr = arg, n;
189 n = strnlen((void *) str, len);
197 int strnlen_user_skas(const void *str, int len)
201 if(segment_eq(get_fs(), KERNEL_DS))
202 return(strnlen(str, len) + 1);
204 n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
211 * Overrides for Emacs so that we follow Linus's tabbing style.
212 * Emacs will notice this stuff at the end of the file and automatically
213 * adjust the settings for this buffer only. This must remain at the end
215 * ---------------------------------------------------------------------------
217 * c-file-style: "linux"