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
)
59 int len
, is_write
, size
, remain
, n
;
60 int (*op
)(unsigned long, int, void *);
64 /* Some old gccs recognize __va_copy, but not va_copy */
65 __va_copy(args
, *(va_list *)arg_ptr
);
66 addr
= va_arg(args
, unsigned long);
67 len
= va_arg(args
, int);
68 is_write
= va_arg(args
, int);
69 op
= va_arg(args
, void *);
70 arg
= va_arg(args
, void *);
71 res
= va_arg(args
, int *);
73 size
= min(PAGE_ALIGN(addr
) - addr
, (unsigned long) len
);
76 current
->thread
.fault_catcher
= jmpbuf
;
77 n
= do_op(addr
, size
, is_write
, op
, arg
);
79 *res
= (n
< 0 ? remain
: 0);
90 while(addr
< ((addr
+ remain
) & PAGE_MASK
)){
91 n
= do_op(addr
, PAGE_SIZE
, is_write
, op
, arg
);
93 *res
= (n
< 0 ? remain
: 0);
105 n
= do_op(addr
, remain
, is_write
, op
, arg
);
107 *res
= (n
< 0 ? remain
: 0);
110 current
->thread
.fault_catcher
= NULL
;
113 static int buffer_op(unsigned long addr
, int len
, int is_write
,
114 int (*op
)(unsigned long addr
, int len
, void *arg
),
119 faulted
= setjmp_wrapper(do_buffer_op
, addr
, len
, is_write
, op
, arg
,
124 return(addr
+ len
- (unsigned long) current
->thread
.fault_addr
);
127 static int copy_chunk_from_user(unsigned long from
, int len
, void *arg
)
129 unsigned long *to_ptr
= arg
, to
= *to_ptr
;
131 memcpy((void *) to
, (void *) from
, len
);
136 int copy_from_user_skas(void *to
, const void __user
*from
, int n
)
138 if(segment_eq(get_fs(), KERNEL_DS
)){
139 memcpy(to
, (__force
void*)from
, n
);
143 return(access_ok_skas(VERIFY_READ
, from
, n
) ?
144 buffer_op((unsigned long) from
, n
, 0, copy_chunk_from_user
, &to
):
148 static int copy_chunk_to_user(unsigned long to
, int len
, void *arg
)
150 unsigned long *from_ptr
= arg
, from
= *from_ptr
;
152 memcpy((void *) to
, (void *) from
, len
);
157 int copy_to_user_skas(void __user
*to
, const void *from
, int n
)
159 if(segment_eq(get_fs(), KERNEL_DS
)){
160 memcpy((__force
void*)to
, from
, n
);
164 return(access_ok_skas(VERIFY_WRITE
, to
, n
) ?
165 buffer_op((unsigned long) to
, n
, 1, copy_chunk_to_user
, &from
) :
169 static int strncpy_chunk_from_user(unsigned long from
, int len
, void *arg
)
171 char **to_ptr
= arg
, *to
= *to_ptr
;
174 strncpy(to
, (void *) from
, len
);
175 n
= strnlen(to
, len
);
183 int strncpy_from_user_skas(char *dst
, const char __user
*src
, int count
)
188 if(segment_eq(get_fs(), KERNEL_DS
)){
189 strncpy(dst
, (__force
void*)src
, count
);
190 return(strnlen(dst
, count
));
193 if(!access_ok_skas(VERIFY_READ
, src
, 1))
196 n
= buffer_op((unsigned long) src
, count
, 0, strncpy_chunk_from_user
,
200 return(strnlen(dst
, count
));
203 static int clear_chunk(unsigned long addr
, int len
, void *unused
)
205 memset((void *) addr
, 0, len
);
209 int __clear_user_skas(void __user
*mem
, int len
)
211 return(buffer_op((unsigned long) mem
, len
, 1, clear_chunk
, NULL
));
214 int clear_user_skas(void __user
*mem
, int len
)
216 if(segment_eq(get_fs(), KERNEL_DS
)){
217 memset((__force
void*)mem
, 0, len
);
221 return(access_ok_skas(VERIFY_WRITE
, mem
, len
) ?
222 buffer_op((unsigned long) mem
, len
, 1, clear_chunk
, NULL
) : len
);
225 static int strnlen_chunk(unsigned long str
, int len
, void *arg
)
227 int *len_ptr
= arg
, n
;
229 n
= strnlen((void *) str
, len
);
237 int strnlen_user_skas(const void __user
*str
, int len
)
241 if(segment_eq(get_fs(), KERNEL_DS
))
242 return(strnlen((__force
char*)str
, len
) + 1);
244 n
= buffer_op((unsigned long) str
, len
, 0, strnlen_chunk
, &count
);
251 * Overrides for Emacs so that we follow Linus's tabbing style.
252 * Emacs will notice this stuff at the end of the file and automatically
253 * adjust the settings for this buffer only. This must remain at the end
255 * ---------------------------------------------------------------------------
257 * c-file-style: "linux"