blob: 84b4b4ead7c1d19562b49b0782ea580f6d608c9f [file] [log] [blame]
Wolfgang Denk6cb142f2006-03-12 02:12:27 +01001/*
2 * U-boot - uaccess.h
3 *
4 * Copyright (c) 2005 blackfin.uclinux.org
5 *
6 * This file is based on
7 * Based on: include/asm-m68knommu/uaccess.h
8 * Changes made by Lineo Inc. May 2001
9 *
10 * See file CREDITS for list of people who contributed to this
11 * project.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of
16 * the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26 * MA 02111-1307 USA
27 */
28
29#ifndef __BLACKFIN_UACCESS_H
30#define __BLACKFIN_UACCESS_H
31
32/*
33 * User space memory access functions
34 */
35#include <asm/segment.h>
36#include <asm/errno.h>
37
38#define VERIFY_READ 0
39#define VERIFY_WRITE 1
40
41/* We let the MMU do all checking */
42static inline int access_ok(int type, const void *addr, unsigned long size)
43{
44 return ((unsigned long) addr < 0x10f00000); /* need final decision - Tony */
45}
46
47static inline int verify_area(int type, const void *addr,
48 unsigned long size)
49{
50 return access_ok(type, addr, size) ? 0 : -EFAULT;
51}
52
53/*
54 * The exception table consists of pairs of addresses: the first is the
55 * address of an instruction that is allowed to fault, and the second is
56 * the address at which the program should continue. No registers are
57 * modified, so it is entirely up to the continuation code to figure out
58 * what to do.
59 *
60 * All the routines below use bits of fixup code that are out of line
61 * with the main instruction path. This means when everything is well,
62 * we don't even have to jump over them. Further, they do not intrude
63 * on our cache or tlb entries.
64 */
65
66struct exception_table_entry {
67 unsigned long insn, fixup;
68};
69
70/* Returns 0 if exception not found and fixup otherwise. */
71extern unsigned long search_exception_table(unsigned long);
72
73/*
74 * These are the main single-value transfer routines. They automatically
75 * use the right size if we just have the right pointer type.
76 */
77
78#define put_user(x, ptr) \
79({ \
80 int __pu_err = 0; \
81 typeof(*(ptr)) __pu_val = (x); \
82 switch (sizeof (*(ptr))) { \
83 case 1: \
84 __put_user_asm(__pu_err, __pu_val, ptr, B); \
85 break; \
86 case 2: \
87 __put_user_asm(__pu_err, __pu_val, ptr, W); \
88 break; \
89 case 4: \
90 __put_user_asm(__pu_err, __pu_val, ptr, ); \
91 break; \
92 default: \
93 __pu_err = __put_user_bad(); \
94 break; \
95 } \
96 __pu_err; \
97})
98/*
99 * [pregs] = dregs ==> 32bits
100 * H[pregs] = dregs ==> 16bits
101 * B[pregs] = dregs ==> 8 bits
102 */
103
104#define __put_user(x, ptr) put_user(x, ptr)
105
106static inline int bad_user_access_length(void)
107{
108 panic("bad_user_access_length");
109 return -1;
110}
111
112#define __put_user_bad() (bad_user_access_length(), (-EFAULT))
113
114/*
115 * Tell gcc we read from memory instead of writing: this is because
116 * we do not write to any memory gcc knows about, so there are no
117 * aliasing issues.
118 */
119
120#define __ptr(x) ((unsigned long *)(x))
121
122#define __put_user_asm(err,x,ptr,bhw) \
123 __asm__ (#bhw"[%1] = %0;\n\t" \
124 : /* no outputs */ \
125 :"d" (x),"a" (__ptr(ptr)) : "memory")
126
127#define get_user(x, ptr) \
128({ \
129 int __gu_err = 0; \
130 typeof(*(ptr)) __gu_val = 0; \
131 switch (sizeof(*(ptr))) { \
132 case 1: \
133 __get_user_asm(__gu_err, __gu_val, ptr, B, "=d",(Z)); \
134 break; \
135 case 2: \
136 __get_user_asm(__gu_err, __gu_val, ptr, W, "=r",(Z)); \
137 break; \
138 case 4: \
139 __get_user_asm(__gu_err, __gu_val, ptr, , "=r",); \
140 break; \
141 default: \
142 __gu_val = 0; \
143 __gu_err = __get_user_bad(); \
144 break; \
145 } \
146 (x) = __gu_val; \
147 __gu_err; \
148})
149
150/* dregs = [pregs] ==> 32bits
151 * H[pregs] ==> 16bits
152 * B[pregs] ==> 8 bits
153 */
154
155#define __get_user(x, ptr) get_user(x, ptr)
156#define __get_user_bad() (bad_user_access_length(), (-EFAULT))
157
158#define __get_user_asm(err,x,ptr,bhw,reg,option) \
159 __asm__ ("%0 =" #bhw "[%1]"#option";\n\t" \
160 : "=d" (x) \
161 : "a" (__ptr(ptr)))
162
163#define copy_from_user(to, from, n) (memcpy(to, from, n), 0)
164#define copy_to_user(to, from, n) (memcpy(to, from, n), 0)
165
166#define __copy_from_user(to, from, n) copy_from_user(to, from, n)
167#define __copy_to_user(to, from, n) copy_to_user(to, from, n)
168
169#define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n)) return retval; })
170#define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n)) return retval; })
171
172/*
173 * Copy a null terminated string from userspace.
174 */
175
176static inline long strncpy_from_user(char *dst, const char *src,
177 long count)
178{
179 char *tmp;
180 strncpy(dst, src, count);
181 for (tmp = dst; *tmp && count > 0; tmp++, count--);
182 return (tmp - dst); /* DAVIDM should we count a NUL ? check getname */
183}
184
185/*
186 * Return the size of a string (including the ending 0)
187 *
188 * Return 0 on exception, a value greater than N if too long
189 */
190static inline long strnlen_user(const char *src, long n)
191{
192 return (strlen(src) + 1); /* DAVIDM make safer */
193}
194
195#define strlen_user(str) strnlen_user(str, 32767)
196
197/*
198 * Zero Userspace
199 */
200
201static inline unsigned long clear_user(void *to, unsigned long n)
202{
203 memset(to, 0, n);
204 return (0);
205}
206
207#endif