blob: 4132497d8ee68014b145a76a99fe037dd78d35e5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001#ifndef _ASM_IA64_UACCESS_H
2#define _ASM_IA64_UACCESS_H
3
4/*
5 * This file defines various macros to transfer memory areas across
6 * the user/kernel boundary. This needs to be done carefully because
7 * this code is executed in kernel mode and uses user-specified
8 * addresses. Thus, we need to be careful not to let the user to
9 * trick us into accessing kernel memory that would normally be
10 * inaccessible. This code is also fairly performance sensitive,
11 * so we want to spend as little time doing safety checks as
12 * possible.
13 *
14 * To make matters a bit more interesting, these macros sometimes also
15 * called from within the kernel itself, in which case the address
16 * validity check must be skipped. The get_fs() macro tells us what
17 * to do: if get_fs()==USER_DS, checking is performed, if
18 * get_fs()==KERNEL_DS, checking is bypassed.
19 *
20 * Note that even if the memory area specified by the user is in a
21 * valid address range, it is still possible that we'll get a page
22 * fault while accessing it. This is handled by filling out an
23 * exception handler fixup entry for each instruction that has the
24 * potential to fault. When such a fault occurs, the page fault
25 * handler checks to see whether the faulting instruction has a fixup
26 * associated and, if so, sets r8 to -EFAULT and clears r9 to 0 and
27 * then resumes execution at the continuation point.
28 *
29 * Based on <asm-alpha/uaccess.h>.
30 *
31 * Copyright (C) 1998, 1999, 2001-2004 Hewlett-Packard Co
32 * David Mosberger-Tang <davidm@hpl.hp.com>
33 */
34
35#include <linux/compiler.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/page-flags.h>
37#include <linux/mm.h>
38
39#include <asm/intrinsics.h>
40#include <asm/pgtable.h>
41#include <asm/io.h>
Al Viro8bec2712016-12-25 14:24:35 -050042#include <asm/extable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44/*
45 * For historical reasons, the following macros are grossly misnamed:
46 */
47#define KERNEL_DS ((mm_segment_t) { ~0UL }) /* cf. access_ok() */
48#define USER_DS ((mm_segment_t) { TASK_SIZE-1 }) /* cf. access_ok() */
49
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define get_ds() (KERNEL_DS)
51#define get_fs() (current_thread_info()->addr_limit)
52#define set_fs(x) (current_thread_info()->addr_limit = (x))
53
54#define segment_eq(a, b) ((a).seg == (b).seg)
55
56/*
57 * When accessing user memory, we need to make sure the entire area really is in
58 * user-level space. In order to do this efficiently, we make sure that the page at
59 * address TASK_SIZE is never valid. We also need to make sure that the address doesn't
60 * point inside the virtually mapped linear page table.
61 */
62#define __access_ok(addr, size, segment) \
63({ \
64 __chk_user_ptr(addr); \
65 (likely((unsigned long) (addr) <= (segment).seg) \
66 && ((segment).seg == KERNEL_DS.seg \
67 || likely(REGION_OFFSET((unsigned long) (addr)) < RGN_MAP_LIMIT))); \
68})
69#define access_ok(type, addr, size) __access_ok((addr), (size), get_fs())
70
Linus Torvalds1da177e2005-04-16 15:20:36 -070071/*
72 * These are the main single-value transfer routines. They automatically
73 * use the right size if we just have the right pointer type.
74 *
75 * Careful to not
76 * (a) re-use the arguments for side effects (sizeof/typeof is ok)
77 * (b) require any knowledge of processes at this stage
78 */
Al Viro1bd59862016-12-27 10:19:22 -050079#define put_user(x, ptr) __put_user_check((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
80#define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82/*
83 * The "__xxx" versions do not do address space checking, useful when
84 * doing multiple accesses to the same area (the programmer has to do the
85 * checks by hand with "access_ok()")
86 */
87#define __put_user(x, ptr) __put_user_nocheck((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
88#define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
89
90extern long __put_user_unaligned_unknown (void);
91
92#define __put_user_unaligned(x, ptr) \
93({ \
94 long __ret; \
95 switch (sizeof(*(ptr))) { \
96 case 1: __ret = __put_user((x), (ptr)); break; \
97 case 2: __ret = (__put_user((x), (u8 __user *)(ptr))) \
98 | (__put_user((x) >> 8, ((u8 __user *)(ptr) + 1))); break; \
99 case 4: __ret = (__put_user((x), (u16 __user *)(ptr))) \
100 | (__put_user((x) >> 16, ((u16 __user *)(ptr) + 1))); break; \
101 case 8: __ret = (__put_user((x), (u32 __user *)(ptr))) \
102 | (__put_user((x) >> 32, ((u32 __user *)(ptr) + 1))); break; \
103 default: __ret = __put_user_unaligned_unknown(); \
104 } \
105 __ret; \
106})
107
108extern long __get_user_unaligned_unknown (void);
109
110#define __get_user_unaligned(x, ptr) \
111({ \
112 long __ret; \
113 switch (sizeof(*(ptr))) { \
114 case 1: __ret = __get_user((x), (ptr)); break; \
115 case 2: __ret = (__get_user((x), (u8 __user *)(ptr))) \
116 | (__get_user((x) >> 8, ((u8 __user *)(ptr) + 1))); break; \
117 case 4: __ret = (__get_user((x), (u16 __user *)(ptr))) \
118 | (__get_user((x) >> 16, ((u16 __user *)(ptr) + 1))); break; \
119 case 8: __ret = (__get_user((x), (u32 __user *)(ptr))) \
120 | (__get_user((x) >> 32, ((u32 __user *)(ptr) + 1))); break; \
121 default: __ret = __get_user_unaligned_unknown(); \
122 } \
123 __ret; \
124})
125
126#ifdef ASM_SUPPORTED
127 struct __large_struct { unsigned long buf[100]; };
128# define __m(x) (*(struct __large_struct __user *)(x))
129
130/* We need to declare the __ex_table section before we can use it in .xdata. */
131asm (".section \"__ex_table\", \"a\"\n\t.previous");
132
133# define __get_user_size(val, addr, n, err) \
134do { \
135 register long __gu_r8 asm ("r8") = 0; \
136 register long __gu_r9 asm ("r9"); \
137 asm ("\n[1:]\tld"#n" %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n" \
138 "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n" \
139 "[1:]" \
140 : "=r"(__gu_r9), "=r"(__gu_r8) : "m"(__m(addr)), "1"(__gu_r8)); \
141 (err) = __gu_r8; \
142 (val) = __gu_r9; \
143} while (0)
144
145/*
146 * The "__put_user_size()" macro tells gcc it reads from memory instead of writing it. This
147 * is because they do not write to any memory gcc knows about, so there are no aliasing
148 * issues.
149 */
150# define __put_user_size(val, addr, n, err) \
151do { \
152 register long __pu_r8 asm ("r8") = 0; \
153 asm volatile ("\n[1:]\tst"#n" %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
154 "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n" \
155 "[1:]" \
156 : "=r"(__pu_r8) : "m"(__m(addr)), "rO"(val), "0"(__pu_r8)); \
157 (err) = __pu_r8; \
158} while (0)
159
160#else /* !ASM_SUPPORTED */
161# define RELOC_TYPE 2 /* ip-rel */
162# define __get_user_size(val, addr, n, err) \
163do { \
164 __ld_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE); \
165 (err) = ia64_getreg(_IA64_REG_R8); \
166 (val) = ia64_getreg(_IA64_REG_R9); \
167} while (0)
Michael S. Tsirkin9605ce72015-01-06 14:37:22 +0200168# define __put_user_size(val, addr, n, err) \
169do { \
170 __st_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE, \
171 (__force unsigned long) (val)); \
172 (err) = ia64_getreg(_IA64_REG_R8); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173} while (0)
174#endif /* !ASM_SUPPORTED */
175
176extern void __get_user_unknown (void);
177
178/*
179 * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
180 * could clobber r8 and r9 (among others). Thus, be careful not to evaluate it while
181 * using r8/r9.
182 */
183#define __do_get_user(check, x, ptr, size, segment) \
184({ \
185 const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
186 __typeof__ (size) __gu_size = (size); \
Al Viro0cc13a52005-09-29 00:12:13 +0100187 long __gu_err = -EFAULT; \
188 unsigned long __gu_val = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (!check || __access_ok(__gu_ptr, size, segment)) \
190 switch (__gu_size) { \
191 case 1: __get_user_size(__gu_val, __gu_ptr, 1, __gu_err); break; \
192 case 2: __get_user_size(__gu_val, __gu_ptr, 2, __gu_err); break; \
193 case 4: __get_user_size(__gu_val, __gu_ptr, 4, __gu_err); break; \
194 case 8: __get_user_size(__gu_val, __gu_ptr, 8, __gu_err); break; \
195 default: __get_user_unknown(); break; \
196 } \
Michael S. Tsirkina6325e72014-12-12 01:56:04 +0200197 (x) = (__force __typeof__(*(__gu_ptr))) __gu_val; \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 __gu_err; \
199})
200
201#define __get_user_nocheck(x, ptr, size) __do_get_user(0, x, ptr, size, KERNEL_DS)
Al Viro1bd59862016-12-27 10:19:22 -0500202#define __get_user_check(x, ptr, size) __do_get_user(1, x, ptr, size, get_fs())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
204extern void __put_user_unknown (void);
205
206/*
207 * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
208 * could clobber r8 (among others). Thus, be careful not to evaluate them while using r8.
209 */
210#define __do_put_user(check, x, ptr, size, segment) \
211({ \
212 __typeof__ (x) __pu_x = (x); \
213 __typeof__ (*(ptr)) __user *__pu_ptr = (ptr); \
214 __typeof__ (size) __pu_size = (size); \
215 long __pu_err = -EFAULT; \
216 \
217 if (!check || __access_ok(__pu_ptr, __pu_size, segment)) \
218 switch (__pu_size) { \
219 case 1: __put_user_size(__pu_x, __pu_ptr, 1, __pu_err); break; \
220 case 2: __put_user_size(__pu_x, __pu_ptr, 2, __pu_err); break; \
221 case 4: __put_user_size(__pu_x, __pu_ptr, 4, __pu_err); break; \
222 case 8: __put_user_size(__pu_x, __pu_ptr, 8, __pu_err); break; \
223 default: __put_user_unknown(); break; \
224 } \
225 __pu_err; \
226})
227
228#define __put_user_nocheck(x, ptr, size) __do_put_user(0, x, ptr, size, KERNEL_DS)
Al Viro1bd59862016-12-27 10:19:22 -0500229#define __put_user_check(x, ptr, size) __do_put_user(1, x, ptr, size, get_fs())
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
231/*
232 * Complex access routines
233 */
234extern unsigned long __must_check __copy_user (void __user *to, const void __user *from,
235 unsigned long count);
236
237static inline unsigned long
238__copy_to_user (void __user *to, const void *from, unsigned long count)
239{
Kees Cook81409e92016-08-31 16:04:21 -0700240 check_object_size(from, count, true);
Kees Cook73d35882016-06-23 15:09:50 -0700241
Al Viro0cc13a52005-09-29 00:12:13 +0100242 return __copy_user(to, (__force void __user *) from, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243}
244
245static inline unsigned long
246__copy_from_user (void *to, const void __user *from, unsigned long count)
247{
Kees Cook81409e92016-08-31 16:04:21 -0700248 check_object_size(to, count, false);
Kees Cook73d35882016-06-23 15:09:50 -0700249
Al Viro0cc13a52005-09-29 00:12:13 +0100250 return __copy_user((__force void __user *) to, from, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251}
252
253#define __copy_to_user_inatomic __copy_to_user
254#define __copy_from_user_inatomic __copy_from_user
255#define copy_to_user(to, from, n) \
256({ \
257 void __user *__cu_to = (to); \
258 const void *__cu_from = (from); \
259 long __cu_len = (n); \
260 \
Kees Cook73d35882016-06-23 15:09:50 -0700261 if (__access_ok(__cu_to, __cu_len, get_fs())) { \
Kees Cook81409e92016-08-31 16:04:21 -0700262 check_object_size(__cu_from, __cu_len, true); \
Kees Cook73d35882016-06-23 15:09:50 -0700263 __cu_len = __copy_user(__cu_to, (__force void __user *) __cu_from, __cu_len); \
264 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 __cu_len; \
266})
267
Al Viroa5e541f2016-08-18 21:31:41 -0400268static inline unsigned long
269copy_from_user(void *to, const void __user *from, unsigned long n)
270{
Linus Torvalds77e5bdf2016-09-14 09:35:05 -0700271 check_object_size(to, n, false);
Al Viroa5e541f2016-08-18 21:31:41 -0400272 if (likely(__access_ok(from, n, get_fs())))
273 n = __copy_user((__force void __user *) to, from, n);
274 else
275 memset(to, 0, n);
276 return n;
277}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279#define __copy_in_user(to, from, size) __copy_user((to), (from), (size))
280
281static inline unsigned long
282copy_in_user (void __user *to, const void __user *from, unsigned long n)
283{
284 if (likely(access_ok(VERIFY_READ, from, n) && access_ok(VERIFY_WRITE, to, n)))
285 n = __copy_user(to, from, n);
286 return n;
287}
288
289extern unsigned long __do_clear_user (void __user *, unsigned long);
290
291#define __clear_user(to, n) __do_clear_user(to, n)
292
293#define clear_user(to, n) \
294({ \
295 unsigned long __cu_len = (n); \
296 if (__access_ok(to, __cu_len, get_fs())) \
297 __cu_len = __do_clear_user(to, __cu_len); \
298 __cu_len; \
299})
300
301
302/*
303 * Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else
304 * strlen.
305 */
306extern long __must_check __strncpy_from_user (char *to, const char __user *from, long to_len);
307
308#define strncpy_from_user(to, from, n) \
309({ \
310 const char __user * __sfu_from = (from); \
311 long __sfu_ret = -EFAULT; \
312 if (__access_ok(__sfu_from, 0, get_fs())) \
313 __sfu_ret = __strncpy_from_user((to), __sfu_from, (n)); \
314 __sfu_ret; \
315})
316
317/* Returns: 0 if bad, string length+1 (memory size) of string if ok */
318extern unsigned long __strlen_user (const char __user *);
319
320#define strlen_user(str) \
321({ \
322 const char __user *__su_str = (str); \
323 unsigned long __su_ret = 0; \
324 if (__access_ok(__su_str, 0, get_fs())) \
325 __su_ret = __strlen_user(__su_str); \
326 __su_ret; \
327})
328
329/*
330 * Returns: 0 if exception before NUL or reaching the supplied limit
331 * (N), a value greater than N if the limit would be exceeded, else
332 * strlen.
333 */
334extern unsigned long __strnlen_user (const char __user *, long);
335
336#define strnlen_user(str, len) \
337({ \
338 const char __user *__su_str = (str); \
339 unsigned long __su_ret = 0; \
340 if (__access_ok(__su_str, 0, get_fs())) \
341 __su_ret = __strnlen_user(__su_str, len); \
342 __su_ret; \
343})
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345#define ARCH_HAS_TRANSLATE_MEM_PTR 1
Thierry Redingdc012012014-07-28 17:05:31 +0200346static __inline__ void *
347xlate_dev_mem_ptr(phys_addr_t p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
349 struct page *page;
Thierry Redingdc012012014-07-28 17:05:31 +0200350 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
352 page = pfn_to_page(p >> PAGE_SHIFT);
353 if (PageUncached(page))
Thierry Redingdc012012014-07-28 17:05:31 +0200354 ptr = (void *)p + __IA64_UNCACHED_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 else
356 ptr = __va(p);
357
358 return ptr;
359}
360
361/*
362 * Convert a virtual cached kernel memory pointer to an uncached pointer
363 */
Thierry Redingdc012012014-07-28 17:05:31 +0200364static __inline__ void *
365xlate_dev_kmem_ptr(void *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct page *page;
Thierry Redingdc012012014-07-28 17:05:31 +0200368 void *ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Jes Sorensenf2454a12006-10-25 05:49:53 -0400370 page = virt_to_page((unsigned long)p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 if (PageUncached(page))
Thierry Redingdc012012014-07-28 17:05:31 +0200372 ptr = (void *)__pa(p) + __IA64_UNCACHED_OFFSET;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 else
374 ptr = p;
375
376 return ptr;
377}
378
379#endif /* _ASM_IA64_UACCESS_H */