vserver 1.9.3
[linux-2.6.git] / arch / um / kernel / skas / uaccess.c
1 /*
2  * Copyright (C) 2002 - 2003 Jeff Dike (jdike@addtoit.com)
3  * Licensed under the GPL
4  */
5
6 #include "linux/stddef.h"
7 #include "linux/kernel.h"
8 #include "linux/string.h"
9 #include "linux/fs.h"
10 #include "linux/highmem.h"
11 #include "asm/page.h"
12 #include "asm/pgtable.h"
13 #include "asm/uaccess.h"
14 #include "kern_util.h"
15 #include "user_util.h"
16
17 extern void *um_virt_to_phys(struct task_struct *task, unsigned long addr,
18                              pte_t *pte_out);
19
20 static unsigned long maybe_map(unsigned long virt, int is_write)
21 {
22         pte_t pte;
23         int err;
24
25         void *phys = um_virt_to_phys(current, virt, &pte);
26         int dummy_code;
27
28         if(IS_ERR(phys) || (is_write && !pte_write(pte))){
29                 err = handle_page_fault(virt, 0, is_write, 1, &dummy_code);
30                 if(err)
31                         return(0);
32                 phys = um_virt_to_phys(current, virt, NULL);
33         }
34         return((unsigned long) phys);
35 }
36
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)
39 {
40         struct page *page;
41         int n;
42
43         addr = maybe_map(addr, is_write);
44         if(addr == -1)
45                 return(-1);
46
47         page = phys_to_page(addr);
48         addr = (unsigned long) kmap(page) + (addr & ~PAGE_MASK);
49         n = (*op)(addr, len, arg);
50         kunmap(page);
51
52         return(n);
53 }
54
55 static void do_buffer_op(void *jmpbuf, void *arg_ptr)
56 {
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);
65         int remain = len, n;
66
67         current->thread.fault_catcher = jmpbuf;
68         n = do_op(addr, size, is_write, op, arg);
69         if(n != 0){
70                 *res = (n < 0 ? remain : 0);
71                 goto out;
72         }
73
74         addr += size;
75         remain -= size;
76         if(remain == 0){
77                 *res = 0;
78                 goto out;
79         }
80
81         while(addr < ((addr + remain) & PAGE_MASK)){
82                 n = do_op(addr, PAGE_SIZE, is_write, op, arg);
83                 if(n != 0){
84                         *res = (n < 0 ? remain : 0);
85                         goto out;
86                 }
87
88                 addr += PAGE_SIZE;
89                 remain -= PAGE_SIZE;
90         }
91         if(remain == 0){
92                 *res = 0;
93                 goto out;
94         }
95
96         n = do_op(addr, remain, is_write, op, arg);
97         if(n != 0)
98                 *res = (n < 0 ? remain : 0);
99         else *res = 0;
100  out:
101         current->thread.fault_catcher = NULL;
102 }
103
104 static int buffer_op(unsigned long addr, int len, int is_write,
105                      int (*op)(unsigned long addr, int len, void *arg),
106                      void *arg)
107 {
108         int faulted, res;
109
110         faulted = setjmp_wrapper(do_buffer_op, addr, len, is_write, op, arg,
111                                  &res);
112         if(!faulted)
113                 return(res);
114
115         return(addr + len - (unsigned long) current->thread.fault_addr);
116 }
117
118 static int copy_chunk_from_user(unsigned long from, int len, void *arg)
119 {
120         unsigned long *to_ptr = arg, to = *to_ptr;
121
122         memcpy((void *) to, (void *) from, len);
123         *to_ptr += len;
124         return(0);
125 }
126
127 int copy_from_user_skas(void *to, const void *from, int n)
128 {
129         if(segment_eq(get_fs(), KERNEL_DS)){
130                 memcpy(to, from, n);
131                 return(0);
132         }
133
134         return(access_ok_skas(VERIFY_READ, from, n) ?
135                buffer_op((unsigned long) from, n, 0, copy_chunk_from_user, &to):
136                n);
137 }
138
139 static int copy_chunk_to_user(unsigned long to, int len, void *arg)
140 {
141         unsigned long *from_ptr = arg, from = *from_ptr;
142
143         memcpy((void *) to, (void *) from, len);
144         *from_ptr += len;
145         return(0);
146 }
147
148 int copy_to_user_skas(void *to, const void *from, int n)
149 {
150         if(segment_eq(get_fs(), KERNEL_DS)){
151                 memcpy(to, from, n);
152                 return(0);
153         }
154
155         return(access_ok_skas(VERIFY_WRITE, to, n) ?
156                buffer_op((unsigned long) to, n, 1, copy_chunk_to_user, &from) :
157                n);
158 }
159
160 static int strncpy_chunk_from_user(unsigned long from, int len, void *arg)
161 {
162         char **to_ptr = arg, *to = *to_ptr;
163         int n;
164
165         strncpy(to, (void *) from, len);
166         n = strnlen(to, len);
167         *to_ptr += n;
168
169         if(n < len)
170                 return(1);
171         return(0);
172 }
173
174 int strncpy_from_user_skas(char *dst, const char *src, int count)
175 {
176         int n;
177         char *ptr = dst;
178
179         if(segment_eq(get_fs(), KERNEL_DS)){
180                 strncpy(dst, src, count);
181                 return(strnlen(dst, count));
182         }
183
184         if(!access_ok_skas(VERIFY_READ, src, 1))
185                 return(-EFAULT);
186
187         n = buffer_op((unsigned long) src, count, 0, strncpy_chunk_from_user,
188                       &ptr);
189         if(n != 0)
190                 return(-EFAULT);
191         return(strnlen(dst, count));
192 }
193
194 static int clear_chunk(unsigned long addr, int len, void *unused)
195 {
196         memset((void *) addr, 0, len);
197         return(0);
198 }
199
200 int __clear_user_skas(void *mem, int len)
201 {
202         return(buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL));
203 }
204
205 int clear_user_skas(void *mem, int len)
206 {
207         if(segment_eq(get_fs(), KERNEL_DS)){
208                 memset(mem, 0, len);
209                 return(0);
210         }
211
212         return(access_ok_skas(VERIFY_WRITE, mem, len) ?
213                buffer_op((unsigned long) mem, len, 1, clear_chunk, NULL) : len);
214 }
215
216 static int strnlen_chunk(unsigned long str, int len, void *arg)
217 {
218         int *len_ptr = arg, n;
219
220         n = strnlen((void *) str, len);
221         *len_ptr += n;
222
223         if(n < len)
224                 return(1);
225         return(0);
226 }
227
228 int strnlen_user_skas(const void *str, int len)
229 {
230         int count = 0, n;
231
232         if(segment_eq(get_fs(), KERNEL_DS))
233                 return(strnlen(str, len) + 1);
234
235         n = buffer_op((unsigned long) str, len, 0, strnlen_chunk, &count);
236         if(n == 0)
237                 return(count + 1);
238         return(-EFAULT);
239 }
240
241 /*
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
245  * of the file.
246  * ---------------------------------------------------------------------------
247  * Local variables:
248  * c-file-style: "linux"
249  * End:
250  */