blob: 7897464e0c24260fe002e0ffac839444c35d5079 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Russell King4baa9922008-08-02 10:55:55 +01002 * arch/arm/include/asm/uaccess.h
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8#ifndef _ASMARM_UACCESS_H
9#define _ASMARM_UACCESS_H
10
11/*
12 * User space memory access functions
13 */
Russell King87c52572008-11-29 17:35:51 +000014#include <linux/string.h>
15#include <linux/thread_info.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <asm/errno.h>
17#include <asm/memory.h>
18#include <asm/domain.h>
19#include <asm/system.h>
20
21#define VERIFY_READ 0
22#define VERIFY_WRITE 1
23
24/*
25 * The exception table consists of pairs of addresses: the first is the
26 * address of an instruction that is allowed to fault, and the second is
27 * the address at which the program should continue. No registers are
28 * modified, so it is entirely up to the continuation code to figure out
29 * what to do.
30 *
31 * All the routines below use bits of fixup code that are out of line
32 * with the main instruction path. This means when everything is well,
33 * we don't even have to jump over them. Further, they do not intrude
34 * on our cache or tlb entries.
35 */
36
37struct exception_table_entry
38{
39 unsigned long insn, fixup;
40};
41
42extern int fixup_exception(struct pt_regs *regs);
43
44/*
Russell King9641c7c2006-06-21 20:38:17 +010045 * These two are intentionally not defined anywhere - if the kernel
46 * code generates any references to them, that's a bug.
47 */
48extern int __get_user_bad(void);
49extern int __put_user_bad(void);
50
51/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 * Note that this is actually 0x1,0000,0000
53 */
54#define KERNEL_DS 0x00000000
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#define get_ds() (KERNEL_DS)
Russell King9641c7c2006-06-21 20:38:17 +010056
57#ifdef CONFIG_MMU
58
59#define USER_DS TASK_SIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#define get_fs() (current_thread_info()->addr_limit)
61
Russell King9641c7c2006-06-21 20:38:17 +010062static inline void set_fs(mm_segment_t fs)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 current_thread_info()->addr_limit = fs;
65 modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
66}
67
68#define segment_eq(a,b) ((a) == (b))
69
70#define __addr_ok(addr) ({ \
71 unsigned long flag; \
72 __asm__("cmp %2, %0; movlo %0, #0" \
73 : "=&r" (flag) \
74 : "0" (current_thread_info()->addr_limit), "r" (addr) \
75 : "cc"); \
76 (flag == 0); })
77
78/* We use 33-bit arithmetic here... */
79#define __range_ok(addr,size) ({ \
Tilman Schmidt16cf5b32007-02-10 01:45:41 -080080 unsigned long flag, roksum; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 __chk_user_ptr(addr); \
82 __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
Tilman Schmidt16cf5b32007-02-10 01:45:41 -080083 : "=&r" (flag), "=&r" (roksum) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
85 : "cc"); \
86 flag; })
87
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * Single-value transfer routines. They automatically use the right
90 * size if we just have the right pointer type. Note that the functions
91 * which read from user space (*get_*) need to take care not to leak
92 * kernel data even if the calling code is buggy and fails to check
93 * the return value. This means zeroing out the destination variable
94 * or buffer on error. Normally this is done out of line by the
95 * fixup code, but there are a few places where it intrudes on the
96 * main code path. When we only write to user space, there is no
97 * problem.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070099extern int __get_user_1(void *);
100extern int __get_user_2(void *);
101extern int __get_user_4(void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103#define __get_user_x(__r2,__p,__e,__s,__i...) \
104 __asm__ __volatile__ ( \
105 __asmeq("%0", "r0") __asmeq("%1", "r2") \
106 "bl __get_user_" #__s \
107 : "=&r" (__e), "=r" (__r2) \
108 : "0" (__p) \
109 : __i, "cc")
110
111#define get_user(x,p) \
112 ({ \
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100113 register const typeof(*(p)) __user *__p asm("r0") = (p);\
Al Virofc048b52006-10-11 17:22:54 +0100114 register unsigned long __r2 asm("r2"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 register int __e asm("r0"); \
116 switch (sizeof(*(__p))) { \
117 case 1: \
118 __get_user_x(__r2, __p, __e, 1, "lr"); \
119 break; \
120 case 2: \
121 __get_user_x(__r2, __p, __e, 2, "r3", "lr"); \
122 break; \
123 case 4: \
124 __get_user_x(__r2, __p, __e, 4, "lr"); \
125 break; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 default: __e = __get_user_bad(); break; \
127 } \
Russell Kingd2c5b692005-11-18 14:22:03 +0000128 x = (typeof(*(p))) __r2; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 __e; \
130 })
131
Russell King9641c7c2006-06-21 20:38:17 +0100132extern int __put_user_1(void *, unsigned int);
133extern int __put_user_2(void *, unsigned int);
134extern int __put_user_4(void *, unsigned int);
135extern int __put_user_8(void *, unsigned long long);
136
137#define __put_user_x(__r2,__p,__e,__s) \
138 __asm__ __volatile__ ( \
139 __asmeq("%0", "r0") __asmeq("%2", "r2") \
140 "bl __put_user_" #__s \
141 : "=&r" (__e) \
142 : "0" (__p), "r" (__r2) \
143 : "ip", "lr", "cc")
144
145#define put_user(x,p) \
146 ({ \
Tobias Klauserc5a69d52007-02-17 20:11:19 +0100147 register const typeof(*(p)) __r2 asm("r2") = (x); \
148 register const typeof(*(p)) __user *__p asm("r0") = (p);\
Russell King9641c7c2006-06-21 20:38:17 +0100149 register int __e asm("r0"); \
150 switch (sizeof(*(__p))) { \
151 case 1: \
152 __put_user_x(__r2, __p, __e, 1); \
153 break; \
154 case 2: \
155 __put_user_x(__r2, __p, __e, 2); \
156 break; \
157 case 4: \
158 __put_user_x(__r2, __p, __e, 4); \
159 break; \
160 case 8: \
161 __put_user_x(__r2, __p, __e, 8); \
162 break; \
163 default: __e = __put_user_bad(); break; \
164 } \
165 __e; \
166 })
167
168#else /* CONFIG_MMU */
169
170/*
171 * uClinux has only one addr space, so has simplified address limits.
172 */
173#define USER_DS KERNEL_DS
174
175#define segment_eq(a,b) (1)
176#define __addr_ok(addr) (1)
177#define __range_ok(addr,size) (0)
178#define get_fs() (KERNEL_DS)
179
180static inline void set_fs(mm_segment_t fs)
181{
182}
183
184#define get_user(x,p) __get_user(x,p)
185#define put_user(x,p) __put_user(x,p)
186
187#endif /* CONFIG_MMU */
188
189#define access_ok(type,addr,size) (__range_ok(addr,size) == 0)
190
191/*
192 * The "__xxx" versions of the user access functions do not verify the
193 * address space - it must have been done previously with a separate
194 * "access_ok()" call.
195 *
196 * The "xxx_error" versions set the third argument to EFAULT if an
197 * error occurs, and leave it unchanged on success. Note that these
198 * versions are void (ie, don't return a value as such).
199 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200#define __get_user(x,ptr) \
201({ \
202 long __gu_err = 0; \
203 __get_user_err((x),(ptr),__gu_err); \
204 __gu_err; \
205})
206
207#define __get_user_error(x,ptr,err) \
208({ \
209 __get_user_err((x),(ptr),err); \
210 (void) 0; \
211})
212
213#define __get_user_err(x,ptr,err) \
214do { \
215 unsigned long __gu_addr = (unsigned long)(ptr); \
216 unsigned long __gu_val; \
217 __chk_user_ptr(ptr); \
218 switch (sizeof(*(ptr))) { \
219 case 1: __get_user_asm_byte(__gu_val,__gu_addr,err); break; \
220 case 2: __get_user_asm_half(__gu_val,__gu_addr,err); break; \
221 case 4: __get_user_asm_word(__gu_val,__gu_addr,err); break; \
222 default: (__gu_val) = __get_user_bad(); \
223 } \
224 (x) = (__typeof__(*(ptr)))__gu_val; \
225} while (0)
226
227#define __get_user_asm_byte(x,addr,err) \
228 __asm__ __volatile__( \
Catalin Marinas4e6a0c32008-08-29 18:31:15 +0100229 "1: ldrbt %1,[%2]\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 "2:\n" \
231 " .section .fixup,\"ax\"\n" \
232 " .align 2\n" \
233 "3: mov %0, %3\n" \
234 " mov %1, #0\n" \
235 " b 2b\n" \
236 " .previous\n" \
237 " .section __ex_table,\"a\"\n" \
238 " .align 3\n" \
239 " .long 1b, 3b\n" \
240 " .previous" \
241 : "+r" (err), "=&r" (x) \
242 : "r" (addr), "i" (-EFAULT) \
243 : "cc")
244
245#ifndef __ARMEB__
246#define __get_user_asm_half(x,__gu_addr,err) \
247({ \
248 unsigned long __b1, __b2; \
249 __get_user_asm_byte(__b1, __gu_addr, err); \
250 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
251 (x) = __b1 | (__b2 << 8); \
252})
253#else
254#define __get_user_asm_half(x,__gu_addr,err) \
255({ \
256 unsigned long __b1, __b2; \
257 __get_user_asm_byte(__b1, __gu_addr, err); \
258 __get_user_asm_byte(__b2, __gu_addr + 1, err); \
259 (x) = (__b1 << 8) | __b2; \
260})
261#endif
262
263#define __get_user_asm_word(x,addr,err) \
264 __asm__ __volatile__( \
Catalin Marinas4e6a0c32008-08-29 18:31:15 +0100265 "1: ldrt %1,[%2]\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 "2:\n" \
267 " .section .fixup,\"ax\"\n" \
268 " .align 2\n" \
269 "3: mov %0, %3\n" \
270 " mov %1, #0\n" \
271 " b 2b\n" \
272 " .previous\n" \
273 " .section __ex_table,\"a\"\n" \
274 " .align 3\n" \
275 " .long 1b, 3b\n" \
276 " .previous" \
277 : "+r" (err), "=&r" (x) \
278 : "r" (addr), "i" (-EFAULT) \
279 : "cc")
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281#define __put_user(x,ptr) \
282({ \
283 long __pu_err = 0; \
284 __put_user_err((x),(ptr),__pu_err); \
285 __pu_err; \
286})
287
288#define __put_user_error(x,ptr,err) \
289({ \
290 __put_user_err((x),(ptr),err); \
291 (void) 0; \
292})
293
294#define __put_user_err(x,ptr,err) \
295do { \
296 unsigned long __pu_addr = (unsigned long)(ptr); \
297 __typeof__(*(ptr)) __pu_val = (x); \
298 __chk_user_ptr(ptr); \
299 switch (sizeof(*(ptr))) { \
300 case 1: __put_user_asm_byte(__pu_val,__pu_addr,err); break; \
301 case 2: __put_user_asm_half(__pu_val,__pu_addr,err); break; \
302 case 4: __put_user_asm_word(__pu_val,__pu_addr,err); break; \
303 case 8: __put_user_asm_dword(__pu_val,__pu_addr,err); break; \
304 default: __put_user_bad(); \
305 } \
306} while (0)
307
308#define __put_user_asm_byte(x,__pu_addr,err) \
309 __asm__ __volatile__( \
Catalin Marinas4e6a0c32008-08-29 18:31:15 +0100310 "1: strbt %1,[%2]\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 "2:\n" \
312 " .section .fixup,\"ax\"\n" \
313 " .align 2\n" \
314 "3: mov %0, %3\n" \
315 " b 2b\n" \
316 " .previous\n" \
317 " .section __ex_table,\"a\"\n" \
318 " .align 3\n" \
319 " .long 1b, 3b\n" \
320 " .previous" \
321 : "+r" (err) \
322 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
323 : "cc")
324
325#ifndef __ARMEB__
326#define __put_user_asm_half(x,__pu_addr,err) \
327({ \
328 unsigned long __temp = (unsigned long)(x); \
329 __put_user_asm_byte(__temp, __pu_addr, err); \
330 __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
331})
332#else
333#define __put_user_asm_half(x,__pu_addr,err) \
334({ \
335 unsigned long __temp = (unsigned long)(x); \
336 __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
337 __put_user_asm_byte(__temp, __pu_addr + 1, err); \
338})
339#endif
340
341#define __put_user_asm_word(x,__pu_addr,err) \
342 __asm__ __volatile__( \
Catalin Marinas4e6a0c32008-08-29 18:31:15 +0100343 "1: strt %1,[%2]\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 "2:\n" \
345 " .section .fixup,\"ax\"\n" \
346 " .align 2\n" \
347 "3: mov %0, %3\n" \
348 " b 2b\n" \
349 " .previous\n" \
350 " .section __ex_table,\"a\"\n" \
351 " .align 3\n" \
352 " .long 1b, 3b\n" \
353 " .previous" \
354 : "+r" (err) \
355 : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
356 : "cc")
357
358#ifndef __ARMEB__
359#define __reg_oper0 "%R2"
360#define __reg_oper1 "%Q2"
361#else
362#define __reg_oper0 "%Q2"
363#define __reg_oper1 "%R2"
364#endif
365
366#define __put_user_asm_dword(x,__pu_addr,err) \
367 __asm__ __volatile__( \
368 "1: strt " __reg_oper1 ", [%1], #4\n" \
Catalin Marinas4e6a0c32008-08-29 18:31:15 +0100369 "2: strt " __reg_oper0 ", [%1]\n" \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 "3:\n" \
371 " .section .fixup,\"ax\"\n" \
372 " .align 2\n" \
373 "4: mov %0, %3\n" \
374 " b 3b\n" \
375 " .previous\n" \
376 " .section __ex_table,\"a\"\n" \
377 " .align 3\n" \
378 " .long 1b, 4b\n" \
379 " .long 2b, 4b\n" \
380 " .previous" \
381 : "+r" (err), "+r" (__pu_addr) \
382 : "r" (x), "i" (-EFAULT) \
383 : "cc")
384
Russell King02fcb972006-06-21 14:44:52 +0100385
Russell King9641c7c2006-06-21 20:38:17 +0100386#ifdef CONFIG_MMU
Russell King99573292006-10-26 10:27:42 +0100387extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
388extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
389extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
Russell King9641c7c2006-06-21 20:38:17 +0100390#else
391#define __copy_from_user(to,from,n) (memcpy(to, (void __force *)from, n), 0)
392#define __copy_to_user(to,from,n) (memcpy((void __force *)to, from, n), 0)
393#define __clear_user(addr,n) (memset((void __force *)addr, 0, n), 0)
394#endif
395
Russell King99573292006-10-26 10:27:42 +0100396extern unsigned long __must_check __strncpy_from_user(char *to, const char __user *from, unsigned long count);
397extern unsigned long __must_check __strnlen_user(const char __user *s, long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Russell King99573292006-10-26 10:27:42 +0100399static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 if (access_ok(VERIFY_READ, from, n))
Russell King02fcb972006-06-21 14:44:52 +0100402 n = __copy_from_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 else /* security hole - plug it */
Russell King59f0cb02008-10-27 11:24:09 +0000404 memset(to, 0, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 return n;
406}
407
Russell King99573292006-10-26 10:27:42 +0100408static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 if (access_ok(VERIFY_WRITE, to, n))
Russell King02fcb972006-06-21 14:44:52 +0100411 n = __copy_to_user(to, from, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 return n;
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415#define __copy_to_user_inatomic __copy_to_user
416#define __copy_from_user_inatomic __copy_from_user
417
Russell King99573292006-10-26 10:27:42 +0100418static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 if (access_ok(VERIFY_WRITE, to, n))
Russell King02fcb972006-06-21 14:44:52 +0100421 n = __clear_user(to, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 return n;
423}
424
Russell King99573292006-10-26 10:27:42 +0100425static inline long __must_check strncpy_from_user(char *dst, const char __user *src, long count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
427 long res = -EFAULT;
428 if (access_ok(VERIFY_READ, src, 1))
Russell King02fcb972006-06-21 14:44:52 +0100429 res = __strncpy_from_user(dst, src, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 return res;
431}
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433#define strlen_user(s) strnlen_user(s, ~0UL >> 1)
434
Russell King99573292006-10-26 10:27:42 +0100435static inline long __must_check strnlen_user(const char __user *s, long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
437 unsigned long res = 0;
438
439 if (__addr_ok(s))
Russell King02fcb972006-06-21 14:44:52 +0100440 res = __strnlen_user(s, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
442 return res;
443}
444
445#endif /* _ASMARM_UACCESS_H */