blob: 61e2bfea7cf41b8a3e06a606001fbdcd58eefd3a [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 *
Wolfgang Denk8e7b7032006-03-12 02:55:22 +01006 * This file is based on
Wolfgang Denk6cb142f2006-03-12 02:12:27 +01007 * 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{
Aubrey.Li3f0606a2007-03-09 13:38:44 +080044 return ((unsigned long)addr < 0x10f00000); /* need final decision - Tony */
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010045}
46
Aubrey.Li3f0606a2007-03-09 13:38:44 +080047static inline int verify_area(int type, const void *addr, unsigned long size)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +010048{
49 return access_ok(type, addr, size) ? 0 : -EFAULT;
50}
51
52/*
53 * The exception table consists of pairs of addresses: the first is the
54 * address of an instruction that is allowed to fault, and the second is
55 * the address at which the program should continue. No registers are
56 * modified, so it is entirely up to the continuation code to figure out
57 * what to do.
58 *
59 * All the routines below use bits of fixup code that are out of line
60 * with the main instruction path. This means when everything is well,
61 * we don't even have to jump over them. Further, they do not intrude
62 * on our cache or tlb entries.
63 */
64
65struct exception_table_entry {
66 unsigned long insn, fixup;
67};
68
69/* Returns 0 if exception not found and fixup otherwise. */
70extern unsigned long search_exception_table(unsigned long);
71
72/*
73 * These are the main single-value transfer routines. They automatically
74 * use the right size if we just have the right pointer type.
75 */
76
77#define put_user(x, ptr) \
78({ \
79 int __pu_err = 0; \
80 typeof(*(ptr)) __pu_val = (x); \
81 switch (sizeof (*(ptr))) { \
82 case 1: \
83 __put_user_asm(__pu_err, __pu_val, ptr, B); \
84 break; \
85 case 2: \
86 __put_user_asm(__pu_err, __pu_val, ptr, W); \
87 break; \
88 case 4: \
89 __put_user_asm(__pu_err, __pu_val, ptr, ); \
90 break; \
91 default: \
92 __pu_err = __put_user_bad(); \
93 break; \
94 } \
95 __pu_err; \
96})
97/*
98 * [pregs] = dregs ==> 32bits
99 * H[pregs] = dregs ==> 16bits
100 * B[pregs] = dregs ==> 8 bits
101 */
102
103#define __put_user(x, ptr) put_user(x, ptr)
104
105static inline int bad_user_access_length(void)
106{
107 panic("bad_user_access_length");
108 return -1;
109}
110
111#define __put_user_bad() (bad_user_access_length(), (-EFAULT))
112
113/*
114 * Tell gcc we read from memory instead of writing: this is because
115 * we do not write to any memory gcc knows about, so there are no
116 * aliasing issues.
117 */
118
119#define __ptr(x) ((unsigned long *)(x))
120
121#define __put_user_asm(err,x,ptr,bhw) \
122 __asm__ (#bhw"[%1] = %0;\n\t" \
123 : /* no outputs */ \
124 :"d" (x),"a" (__ptr(ptr)) : "memory")
125
126#define get_user(x, ptr) \
127({ \
128 int __gu_err = 0; \
129 typeof(*(ptr)) __gu_val = 0; \
130 switch (sizeof(*(ptr))) { \
131 case 1: \
132 __get_user_asm(__gu_err, __gu_val, ptr, B, "=d",(Z)); \
133 break; \
134 case 2: \
135 __get_user_asm(__gu_err, __gu_val, ptr, W, "=r",(Z)); \
136 break; \
137 case 4: \
138 __get_user_asm(__gu_err, __gu_val, ptr, , "=r",); \
139 break; \
140 default: \
141 __gu_val = 0; \
142 __gu_err = __get_user_bad(); \
143 break; \
144 } \
145 (x) = __gu_val; \
146 __gu_err; \
147})
148
149/* dregs = [pregs] ==> 32bits
150 * H[pregs] ==> 16bits
151 * B[pregs] ==> 8 bits
152 */
153
154#define __get_user(x, ptr) get_user(x, ptr)
155#define __get_user_bad() (bad_user_access_length(), (-EFAULT))
156
157#define __get_user_asm(err,x,ptr,bhw,reg,option) \
158 __asm__ ("%0 =" #bhw "[%1]"#option";\n\t" \
159 : "=d" (x) \
160 : "a" (__ptr(ptr)))
161
162#define copy_from_user(to, from, n) (memcpy(to, from, n), 0)
163#define copy_to_user(to, from, n) (memcpy(to, from, n), 0)
164
165#define __copy_from_user(to, from, n) copy_from_user(to, from, n)
166#define __copy_to_user(to, from, n) copy_to_user(to, from, n)
167
168#define copy_to_user_ret(to,from,n,retval) ({ if (copy_to_user(to,from,n)) return retval; })
169#define copy_from_user_ret(to,from,n,retval) ({ if (copy_from_user(to,from,n)) return retval; })
170
171/*
172 * Copy a null terminated string from userspace.
173 */
174
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800175static inline long strncpy_from_user(char *dst, const char *src, long count)
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100176{
177 char *tmp;
178 strncpy(dst, src, count);
Aubrey.Li3f0606a2007-03-09 13:38:44 +0800179 for (tmp = dst; *tmp && count > 0; tmp++, count--) ;
Wolfgang Denk6cb142f2006-03-12 02:12:27 +0100180 return (tmp - dst); /* DAVIDM should we count a NUL ? check getname */
181}
182
183/*
184 * Return the size of a string (including the ending 0)
185 *
186 * Return 0 on exception, a value greater than N if too long
187 */
188static inline long strnlen_user(const char *src, long n)
189{
190 return (strlen(src) + 1); /* DAVIDM make safer */
191}
192
193#define strlen_user(str) strnlen_user(str, 32767)
194
195/*
196 * Zero Userspace
197 */
198
199static inline unsigned long clear_user(void *to, unsigned long n)
200{
201 memset(to, 0, n);
202 return (0);
203}
204
205#endif