allow coexistance of N build and AC build.
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / arch / um / kernel / skas / uaccess.c
blob8912cec0fe43380cb6761e088099c04dccd1bfaf
1 /*
2 * Copyright (C) 2002 - 2003 Jeff Dike (jdike@addtoit.com)
3 * Licensed under the GPL
4 */
6 #include "linux/compiler.h"
7 #include "linux/stddef.h"
8 #include "linux/kernel.h"
9 #include "linux/string.h"
10 #include "linux/fs.h"
11 #include "linux/hardirq.h"
12 #include "linux/highmem.h"
13 #include "asm/page.h"
14 #include "asm/pgtable.h"
15 #include "asm/uaccess.h"
16 #include "kern_util.h"
17 #include "os.h"
19 extern void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
20 pte_t *pte_out);
22 static unsigned long maybe_map(unsigned long virt, int is_write)
24 pte_t pte;
25 int err;
27 void *phys = um_virt_to_phys(current, virt, &pte);
28 int dummy_code;
30 if(IS_ERR(phys) || (is_write && !pte_write(pte))){
31 err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
32 if(err)
33 return(-1UL);
34 phys = um_virt_to_phys(current, virt, NULL);
36 if(IS_ERR(phys))
37 phys = (void *) -1;
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)
45 struct page *page;
46 int n;
48 addr = maybe_map(addr, is_write);
49 if(addr == -1UL)
50 return(-1);
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);
59 return(n);
62 static void do_buffer_op(void *jmpbuf, void *arg_ptr)
64 va_list args;
65 unsigned long addr;
66 int len, is_write, size, remain, n;
67 int (*op)(unsigned long, int, void *);
68 void *arg;
69 int *res;
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 *);
78 va_end(args);
79 size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
80 remain = len;
82 current->thread.fault_catcher = jmpbuf;
83 n = do_op_one_page(addr, size, is_write, op, arg);
84 if(n != 0){
85 *res = (n < 0 ? remain : 0);
86 goto out;
89 addr += size;
90 remain -= size;
91 if(remain == 0){
92 *res = 0;
93 goto out;
96 while(addr < ((addr + remain) & PAGE_MASK)){
97 n = do_op_one_page(addr, PAGE_SIZE, is_write, op, arg);
98 if(n != 0){
99 *res = (n < 0 ? remain : 0);
100 goto out;
103 addr += PAGE_SIZE;
104 remain -= PAGE_SIZE;
106 if(remain == 0){
107 *res = 0;
108 goto out;
111 n = do_op_one_page(addr, remain, is_write, op, arg);
112 if(n != 0)
113 *res = (n < 0 ? remain : 0);
114 else *res = 0;
115 out:
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),
121 void *arg)
123 int faulted, res;
125 faulted = setjmp_wrapper(do_buffer_op, addr, len, is_write, op, arg,
126 &res);
127 if(!faulted)
128 return(res);
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);
138 *to_ptr += len;
139 return(0);
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);
146 return(0);
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);
159 *from_ptr += len;
160 return(0);
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);
167 return(0);
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;
178 int n;
180 strncpy(to, (void *) from, len);
181 n = strnlen(to, len);
182 *to_ptr += n;
184 if(n < len)
185 return(1);
186 return(0);
189 int strncpy_from_user_skas(char *dst, const char __user *src, int count)
191 int n;
192 char *ptr = dst;
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))
200 return(-EFAULT);
202 n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
203 &ptr);
204 if(n != 0)
205 return(-EFAULT);
206 return(strnlen(dst, count));
209 static int clear_chunk(unsigned long addr, int len, void *unused)
211 memset((void *) addr, 0, len);
212 return(0);
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);
224 return(0);
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);
236 *len_ptr += n;
238 if(n < len)
239 return(1);
240 return(0);
243 int strnlen_user_skas(const void __user *str, int len)
245 int count = 0, n;
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);
251 if(n == 0)
252 return(count + 1);
253 return(-EFAULT);
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
260 * of the file.
261 * ---------------------------------------------------------------------------
262 * Local variables:
263 * c-file-style: "linux"
264 * End: