2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
7 #include "linux/highmem.h"
9 #include "asm/current.h"
11 #include "asm/pgtable.h"
12 #include "kern_util.h"
15 extern void *um_virt_to_phys(struct task_struct
*task
, unsigned long addr
,
18 static unsigned long maybe_map(unsigned long virt
, int is_write
)
23 void *phys
= um_virt_to_phys(current
, virt
, &pte
);
26 if (IS_ERR(phys
) || (is_write
&& !pte_write(pte
))) {
27 err
= handle_page_fault(virt
, 0, is_write
, 1, &dummy_code
);
30 phys
= um_virt_to_phys(current
, virt
, NULL
);
35 return (unsigned long) phys
;
38 static int do_op_one_page(unsigned long addr
, int len
, int is_write
,
39 int (*op
)(unsigned long addr
, int len
, void *arg
), void *arg
)
44 addr
= maybe_map(addr
, is_write
);
48 page
= phys_to_page(addr
);
49 addr
= (unsigned long) kmap_atomic(page
, KM_UML_USERCOPY
) +
52 n
= (*op
)(addr
, len
, arg
);
54 kunmap_atomic(page
, KM_UML_USERCOPY
);
59 static void do_buffer_op(void *jmpbuf
, void *arg_ptr
)
63 int len
, is_write
, size
, remain
, n
;
64 int (*op
)(unsigned long, int, void *);
68 va_copy(args
, *(va_list *)arg_ptr
);
69 addr
= va_arg(args
, unsigned long);
70 len
= va_arg(args
, int);
71 is_write
= va_arg(args
, int);
72 op
= va_arg(args
, void *);
73 arg
= va_arg(args
, void *);
74 res
= va_arg(args
, int *);
76 size
= min(PAGE_ALIGN(addr
) - addr
, (unsigned long) len
);
79 current
->thread
.fault_catcher
= jmpbuf
;
80 n
= do_op_one_page(addr
, size
, is_write
, op
, arg
);
82 *res
= (n
< 0 ? remain
: 0);
93 while(addr
< ((addr
+ remain
) & PAGE_MASK
)) {
94 n
= do_op_one_page(addr
, PAGE_SIZE
, is_write
, op
, arg
);
96 *res
= (n
< 0 ? remain
: 0);
108 n
= do_op_one_page(addr
, remain
, is_write
, op
, arg
);
110 *res
= (n
< 0 ? remain
: 0);
113 current
->thread
.fault_catcher
= NULL
;
116 static int buffer_op(unsigned long addr
, int len
, int is_write
,
117 int (*op
)(unsigned long addr
, int len
, void *arg
),
122 faulted
= setjmp_wrapper(do_buffer_op
, addr
, len
, is_write
, op
, arg
,
127 return addr
+ len
- (unsigned long) current
->thread
.fault_addr
;
130 static int copy_chunk_from_user(unsigned long from
, int len
, void *arg
)
132 unsigned long *to_ptr
= arg
, to
= *to_ptr
;
134 memcpy((void *) to
, (void *) from
, len
);
139 int copy_from_user(void *to
, const void __user
*from
, int n
)
141 if (segment_eq(get_fs(), KERNEL_DS
)) {
142 memcpy(to
, (__force
void*)from
, n
);
146 return access_ok(VERIFY_READ
, from
, n
) ?
147 buffer_op((unsigned long) from
, n
, 0, copy_chunk_from_user
, &to
):
151 static int copy_chunk_to_user(unsigned long to
, int len
, void *arg
)
153 unsigned long *from_ptr
= arg
, from
= *from_ptr
;
155 memcpy((void *) to
, (void *) from
, len
);
160 int copy_to_user(void __user
*to
, const void *from
, int n
)
162 if (segment_eq(get_fs(), KERNEL_DS
)) {
163 memcpy((__force
void *) to
, from
, n
);
167 return access_ok(VERIFY_WRITE
, to
, n
) ?
168 buffer_op((unsigned long) to
, n
, 1, copy_chunk_to_user
, &from
) :
172 static int strncpy_chunk_from_user(unsigned long from
, int len
, void *arg
)
174 char **to_ptr
= arg
, *to
= *to_ptr
;
177 strncpy(to
, (void *) from
, len
);
178 n
= strnlen(to
, len
);
186 int strncpy_from_user(char *dst
, const char __user
*src
, int count
)
191 if (segment_eq(get_fs(), KERNEL_DS
)) {
192 strncpy(dst
, (__force
void *) src
, count
);
193 return strnlen(dst
, count
);
196 if (!access_ok(VERIFY_READ
, src
, 1))
199 n
= buffer_op((unsigned long) src
, count
, 0, strncpy_chunk_from_user
,
203 return strnlen(dst
, count
);
206 static int clear_chunk(unsigned long addr
, int len
, void *unused
)
208 memset((void *) addr
, 0, len
);
212 int __clear_user(void __user
*mem
, int len
)
214 return buffer_op((unsigned long) mem
, len
, 1, clear_chunk
, NULL
);
217 int clear_user(void __user
*mem
, int len
)
219 if (segment_eq(get_fs(), KERNEL_DS
)) {
220 memset((__force
void*)mem
, 0, len
);
224 return access_ok(VERIFY_WRITE
, mem
, len
) ?
225 buffer_op((unsigned long) mem
, len
, 1, clear_chunk
, NULL
) : len
;
228 static int strnlen_chunk(unsigned long str
, int len
, void *arg
)
230 int *len_ptr
= arg
, n
;
232 n
= strnlen((void *) str
, len
);
240 int strnlen_user(const void __user
*str
, int len
)
244 if (segment_eq(get_fs(), KERNEL_DS
))
245 return strnlen((__force
char*)str
, len
) + 1;
247 n
= buffer_op((unsigned long) str
, len
, 0, strnlen_chunk
, &count
);