2 * Copyright (C) 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
6 #ifndef __SKAS_UACCESS_H
7 #define __SKAS_UACCESS_H
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"
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)))
24 static inline int verify_area_skas(int type, const void * addr,
27 return(access_ok_skas(type, addr, size) ? 0 : -EFAULT);
30 static inline unsigned long maybe_map(unsigned long virt, int is_write)
34 void *phys = um_virt_to_phys(current, virt, &pte);
37 if(IS_ERR(phys) || (is_write && !pte_write(pte))){
38 if(handle_page_fault(virt, 0, is_write, 0, &dummy_code))
40 phys = um_virt_to_phys(current, virt, NULL);
42 return((unsigned long) __va((unsigned long) phys));
45 static inline int buffer_op(unsigned long addr, int len,
46 int (*op)(unsigned long addr, int len, void *arg),
49 int size = min(PAGE_ALIGN(addr) - addr, (unsigned long) len);
52 n = (*op)(addr, size, arg);
54 return(n < 0 ? remain : 0);
61 while(addr < ((addr + remain) & PAGE_MASK)){
62 n = (*op)(addr, PAGE_SIZE, arg);
64 return(n < 0 ? remain : 0);
72 n = (*op)(addr, remain, arg);
74 return(n < 0 ? remain : 0);
78 static inline int copy_chunk_from_user(unsigned long from, int len, void *arg)
80 unsigned long *to_ptr = arg, to = *to_ptr;
82 from = maybe_map(from, 0);
86 memcpy((void *) to, (void *) from, len);
91 static inline int copy_from_user_skas(void *to, const void *from, int n)
93 if(segment_eq(get_fs(), KERNEL_DS)){
98 return(access_ok_skas(VERIFY_READ, from, n) ?
99 buffer_op((unsigned long) from, n, copy_chunk_from_user, &to) :
103 static inline int copy_chunk_to_user(unsigned long to, int len, void *arg)
105 unsigned long *from_ptr = arg, from = *from_ptr;
107 to = maybe_map(to, 1);
111 memcpy((void *) to, (void *) from, len);
116 static inline int copy_to_user_skas(void *to, const void *from, int n)
118 if(segment_eq(get_fs(), KERNEL_DS)){
123 return(access_ok_skas(VERIFY_WRITE, to, n) ?
124 buffer_op((unsigned long) to, n, copy_chunk_to_user, &from) :
128 static inline int strncpy_chunk_from_user(unsigned long from, int len,
131 char **to_ptr = arg, *to = *to_ptr;
134 from = maybe_map(from, 0);
138 strncpy(to, (void *) from, len);
139 n = strnlen(to, len);
147 static inline int strncpy_from_user_skas(char *dst, const char *src, int count)
152 if(segment_eq(get_fs(), KERNEL_DS)){
153 strncpy(dst, src, count);
154 return(strnlen(dst, count));
157 if(!access_ok_skas(VERIFY_READ, src, 1))
160 n = buffer_op((unsigned long) src, count, strncpy_chunk_from_user,
164 return(strnlen(dst, count));
167 static inline int clear_chunk(unsigned long addr, int len, void *unused)
169 addr = maybe_map(addr, 1);
173 memset((void *) addr, 0, len);
177 static inline int __clear_user_skas(void *mem, int len)
179 return(buffer_op((unsigned long) mem, len, clear_chunk, NULL));
182 static inline int clear_user_skas(void *mem, int len)
184 if(segment_eq(get_fs(), KERNEL_DS)){
189 return(access_ok_skas(VERIFY_WRITE, mem, len) ?
190 buffer_op((unsigned long) mem, len, clear_chunk, NULL) : len);
193 static inline int strnlen_chunk(unsigned long str, int len, void *arg)
195 int *len_ptr = arg, n;
197 str = maybe_map(str, 0);
201 n = strnlen((void *) str, len);
209 static inline int strnlen_user_skas(const void *str, int len)
213 if(segment_eq(get_fs(), KERNEL_DS))
214 return(strnlen(str, len) + 1);
216 n = buffer_op((unsigned long) str, len, strnlen_chunk, &count);
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
229 * ---------------------------------------------------------------------------
231 * c-file-style: "linux"