ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / kernel / skas / include / uaccess.h
1 /* 
2  * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #ifndef __SKAS_UACCESS_H
7 #define __SKAS_UACCESS_H
8
9 #include "linux/string.h"
10 #include "linux/sched.h"
11 #include "linux/err.h"
12 #include "asm/processor.h"
13 #include "asm/pgtable.h"
14 #include "asm/errno.h"
15 #include "asm/current.h"
16 #include "asm/a.out.h"
17 #include "kern_util.h"
18
19 #define access_ok_skas(type, addr, size) \
20         ((segment_eq(get_fs(), KERNEL_DS)) || \
21          (((unsigned long) (addr) < TASK_SIZE) && \
22           ((unsigned long) (addr) + (size) < TASK_SIZE)))
23
24 static inline int verify_area_skas(int type, const void * addr, 
25                                    unsigned long size)
26 {
27         return(access_ok_skas(type, addr, size) ? 0 : -EFAULT);
28 }
29
30 static inline unsigned long maybe_map(unsigned long virt, int is_write)
31 {
32         pte_t pte;
33
34         void *phys = um_virt_to_phys(current, virt, &pte);
35         int dummy_code;
36
37         if(IS_ERR(phys) || (is_write && !pte_write(pte))){
38                 if(handle_page_fault(virt, 0, is_write, 0, &dummy_code))
39                         return(0);
40                 phys = um_virt_to_phys(current, virt, NULL);
41         }
42         return((unsigned long) __va((unsigned long) phys));
43 }
44
45 static inline int buffer_op(unsigned long addr, int len, 
46                             int (*op)(unsigned long addr, int len, void *arg),
47                             void *arg)
48 {
49         int size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
50         int remain = len, n;
51
52         n = (*op)(addr, size, arg);
53         if(n != 0)
54                 return(n < 0 ? remain : 0);
55
56         addr += size;
57         remain -= size;
58         if(remain == 0) 
59                 return(0);
60
61         while(addr < ((addr + remain) & PAGE_MASK)){
62                 n = (*op)(addr, PAGE_SIZE, arg);
63                 if(n != 0)
64                         return(n < 0 ? remain : 0);
65
66                 addr += PAGE_SIZE;
67                 remain -= PAGE_SIZE;
68         }
69         if(remain == 0)
70                 return(0);
71
72         n = (*op)(addr, remain, arg);
73         if(n != 0)
74                 return(n < 0 ? remain : 0);
75         return(0);
76 }
77
78 static inline int copy_chunk_from_user(unsigned long from, int len, void *arg)
79 {
80         unsigned long *to_ptr = arg, to = *to_ptr;
81
82         from = maybe_map(from, 0);
83         if(from == 0)
84                 return(-1);
85
86         memcpy((void *) to, (void *) from, len);
87         *to_ptr += len;
88         return(0);
89 }
90
91 static inline int copy_from_user_skas(void *to, const void *from, int n)
92 {
93         if(segment_eq(get_fs(), KERNEL_DS)){
94                 memcpy(to, from, n);
95                 return(0);
96         }
97
98         return(access_ok_skas(VERIFY_READ, from, n) ?
99                buffer_op((unsigned long) from, n, copy_chunk_from_user, &to) :
100                n);
101 }
102
103 static inline int copy_chunk_to_user(unsigned long to, int len, void *arg)
104 {
105         unsigned long *from_ptr = arg, from = *from_ptr;
106
107         to = maybe_map(to, 1);
108         if(to == 0)
109                 return(-1);
110
111         memcpy((void *) to, (void *) from, len);
112         *from_ptr += len;
113         return(0);
114 }
115
116 static inline int copy_to_user_skas(void *to, const void *from, int n)
117 {
118         if(segment_eq(get_fs(), KERNEL_DS)){
119                 memcpy(to, from, n);
120                 return(0);
121         }
122
123         return(access_ok_skas(VERIFY_WRITE, to, n) ?
124                buffer_op((unsigned long) to, n, copy_chunk_to_user, &from) :
125                n);
126 }
127
128 static inline int strncpy_chunk_from_user(unsigned long from, int len, 
129                                           void *arg)
130 {
131         char **to_ptr = arg, *to = *to_ptr;
132         int n;
133
134         from = maybe_map(from, 0);
135         if(from == 0)
136                 return(-1);
137
138         strncpy(to, (void *) from, len);
139         n = strnlen(to, len);
140         *to_ptr += n;
141
142         if(n < len) 
143                 return(1);
144         return(0);
145 }
146
147 static inline int strncpy_from_user_skas(char *dst, const char *src, int count)
148 {
149         int n;
150         char *ptr = dst;
151
152         if(segment_eq(get_fs(), KERNEL_DS)){
153                 strncpy(dst, src, count);
154                 return(strnlen(dst, count));
155         }
156
157         if(!access_ok_skas(VERIFY_READ, src, 1))
158                 return(-EFAULT);
159
160         n = buffer_op((unsigned long) src, count, strncpy_chunk_from_user, 
161                       &ptr);
162         if(n != 0)
163                 return(-EFAULT);
164         return(strnlen(dst, count));
165 }
166
167 static inline int clear_chunk(unsigned long addr, int len, void *unused)
168 {
169         addr = maybe_map(addr, 1);
170         if(addr == 0) 
171                 return(-1);
172
173         memset((void *) addr, 0, len);
174         return(0);
175 }
176
177 static inline int __clear_user_skas(void *mem, int len)
178 {
179         return(buffer_op((unsigned long) mem, len, clear_chunk, NULL));
180 }
181
182 static inline int clear_user_skas(void *mem, int len)
183 {
184         if(segment_eq(get_fs(), KERNEL_DS)){
185                 memset(mem, 0, len);
186                 return(0);
187         }
188
189         return(access_ok_skas(VERIFY_WRITE, mem, len) ? 
190                buffer_op((unsigned long) mem, len, clear_chunk, NULL) : len);
191 }
192
193 static inline int strnlen_chunk(unsigned long str, int len, void *arg)
194 {
195         int *len_ptr = arg, n;
196
197         str = maybe_map(str, 0);
198         if(str == 0) 
199                 return(-1);
200
201         n = strnlen((void *) str, len);
202         *len_ptr += n;
203
204         if(n < len)
205                 return(1);
206         return(0);
207 }
208
209 static inline int strnlen_user_skas(const void *str, int len)
210 {
211         int count = 0, n;
212
213         if(segment_eq(get_fs(), KERNEL_DS))
214                 return(strnlen(str, len) + 1);
215
216         n = buffer_op((unsigned long) str, len, strnlen_chunk, &count);
217         if(n == 0)
218                 return(count + 1);
219         return(-EFAULT);
220 }
221
222 #endif
223
224 /*
225  * Overrides for Emacs so that we follow Linus's tabbing style.
226  * Emacs will notice this stuff at the end of the file and automatically
227  * adjust the settings for this buffer only.  This must remain at the end
228  * of the file.
229  * ---------------------------------------------------------------------------
230  * Local variables:
231  * c-file-style: "linux"
232  * End:
233  */