1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0 */
2*4882a593Smuzhiyun #ifndef _ASM_IA64_UACCESS_H
3*4882a593Smuzhiyun #define _ASM_IA64_UACCESS_H
4*4882a593Smuzhiyun
5*4882a593Smuzhiyun /*
6*4882a593Smuzhiyun * This file defines various macros to transfer memory areas across
7*4882a593Smuzhiyun * the user/kernel boundary. This needs to be done carefully because
8*4882a593Smuzhiyun * this code is executed in kernel mode and uses user-specified
9*4882a593Smuzhiyun * addresses. Thus, we need to be careful not to let the user to
10*4882a593Smuzhiyun * trick us into accessing kernel memory that would normally be
11*4882a593Smuzhiyun * inaccessible. This code is also fairly performance sensitive,
12*4882a593Smuzhiyun * so we want to spend as little time doing safety checks as
13*4882a593Smuzhiyun * possible.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * To make matters a bit more interesting, these macros sometimes also
16*4882a593Smuzhiyun * called from within the kernel itself, in which case the address
17*4882a593Smuzhiyun * validity check must be skipped. The get_fs() macro tells us what
18*4882a593Smuzhiyun * to do: if get_fs()==USER_DS, checking is performed, if
19*4882a593Smuzhiyun * get_fs()==KERNEL_DS, checking is bypassed.
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * Note that even if the memory area specified by the user is in a
22*4882a593Smuzhiyun * valid address range, it is still possible that we'll get a page
23*4882a593Smuzhiyun * fault while accessing it. This is handled by filling out an
24*4882a593Smuzhiyun * exception handler fixup entry for each instruction that has the
25*4882a593Smuzhiyun * potential to fault. When such a fault occurs, the page fault
26*4882a593Smuzhiyun * handler checks to see whether the faulting instruction has a fixup
27*4882a593Smuzhiyun * associated and, if so, sets r8 to -EFAULT and clears r9 to 0 and
28*4882a593Smuzhiyun * then resumes execution at the continuation point.
29*4882a593Smuzhiyun *
30*4882a593Smuzhiyun * Based on <asm-alpha/uaccess.h>.
31*4882a593Smuzhiyun *
32*4882a593Smuzhiyun * Copyright (C) 1998, 1999, 2001-2004 Hewlett-Packard Co
33*4882a593Smuzhiyun * David Mosberger-Tang <davidm@hpl.hp.com>
34*4882a593Smuzhiyun */
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #include <linux/compiler.h>
37*4882a593Smuzhiyun #include <linux/page-flags.h>
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun #include <asm/intrinsics.h>
40*4882a593Smuzhiyun #include <linux/pgtable.h>
41*4882a593Smuzhiyun #include <asm/io.h>
42*4882a593Smuzhiyun #include <asm/extable.h>
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun /*
45*4882a593Smuzhiyun * For historical reasons, the following macros are grossly misnamed:
46*4882a593Smuzhiyun */
47*4882a593Smuzhiyun #define KERNEL_DS ((mm_segment_t) { ~0UL }) /* cf. access_ok() */
48*4882a593Smuzhiyun #define USER_DS ((mm_segment_t) { TASK_SIZE-1 }) /* cf. access_ok() */
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun #define get_fs() (current_thread_info()->addr_limit)
51*4882a593Smuzhiyun #define set_fs(x) (current_thread_info()->addr_limit = (x))
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun * When accessing user memory, we need to make sure the entire area really is in
57*4882a593Smuzhiyun * user-level space. In order to do this efficiently, we make sure that the page at
58*4882a593Smuzhiyun * address TASK_SIZE is never valid. We also need to make sure that the address doesn't
59*4882a593Smuzhiyun * point inside the virtually mapped linear page table.
60*4882a593Smuzhiyun */
__access_ok(const void __user * p,unsigned long size)61*4882a593Smuzhiyun static inline int __access_ok(const void __user *p, unsigned long size)
62*4882a593Smuzhiyun {
63*4882a593Smuzhiyun unsigned long addr = (unsigned long)p;
64*4882a593Smuzhiyun unsigned long seg = get_fs().seg;
65*4882a593Smuzhiyun return likely(addr <= seg) &&
66*4882a593Smuzhiyun (seg == KERNEL_DS.seg || likely(REGION_OFFSET(addr) < RGN_MAP_LIMIT));
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun #define access_ok(addr, size) __access_ok((addr), (size))
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun /*
71*4882a593Smuzhiyun * These are the main single-value transfer routines. They automatically
72*4882a593Smuzhiyun * use the right size if we just have the right pointer type.
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * Careful to not
75*4882a593Smuzhiyun * (a) re-use the arguments for side effects (sizeof/typeof is ok)
76*4882a593Smuzhiyun * (b) require any knowledge of processes at this stage
77*4882a593Smuzhiyun */
78*4882a593Smuzhiyun #define put_user(x, ptr) __put_user_check((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
79*4882a593Smuzhiyun #define get_user(x, ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun /*
82*4882a593Smuzhiyun * The "__xxx" versions do not do address space checking, useful when
83*4882a593Smuzhiyun * doing multiple accesses to the same area (the programmer has to do the
84*4882a593Smuzhiyun * checks by hand with "access_ok()")
85*4882a593Smuzhiyun */
86*4882a593Smuzhiyun #define __put_user(x, ptr) __put_user_nocheck((__typeof__(*(ptr))) (x), (ptr), sizeof(*(ptr)))
87*4882a593Smuzhiyun #define __get_user(x, ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun #ifdef ASM_SUPPORTED
90*4882a593Smuzhiyun struct __large_struct { unsigned long buf[100]; };
91*4882a593Smuzhiyun # define __m(x) (*(struct __large_struct __user *)(x))
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun /* We need to declare the __ex_table section before we can use it in .xdata. */
94*4882a593Smuzhiyun asm (".section \"__ex_table\", \"a\"\n\t.previous");
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun # define __get_user_size(val, addr, n, err) \
97*4882a593Smuzhiyun do { \
98*4882a593Smuzhiyun register long __gu_r8 asm ("r8") = 0; \
99*4882a593Smuzhiyun register long __gu_r9 asm ("r9"); \
100*4882a593Smuzhiyun asm ("\n[1:]\tld"#n" %0=%2%P2\t// %0 and %1 get overwritten by exception handler\n" \
101*4882a593Smuzhiyun "\t.xdata4 \"__ex_table\", 1b-., 1f-.+4\n" \
102*4882a593Smuzhiyun "[1:]" \
103*4882a593Smuzhiyun : "=r"(__gu_r9), "=r"(__gu_r8) : "m"(__m(addr)), "1"(__gu_r8)); \
104*4882a593Smuzhiyun (err) = __gu_r8; \
105*4882a593Smuzhiyun (val) = __gu_r9; \
106*4882a593Smuzhiyun } while (0)
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * The "__put_user_size()" macro tells gcc it reads from memory instead of writing it. This
110*4882a593Smuzhiyun * is because they do not write to any memory gcc knows about, so there are no aliasing
111*4882a593Smuzhiyun * issues.
112*4882a593Smuzhiyun */
113*4882a593Smuzhiyun # define __put_user_size(val, addr, n, err) \
114*4882a593Smuzhiyun do { \
115*4882a593Smuzhiyun register long __pu_r8 asm ("r8") = 0; \
116*4882a593Smuzhiyun asm volatile ("\n[1:]\tst"#n" %1=%r2%P1\t// %0 gets overwritten by exception handler\n" \
117*4882a593Smuzhiyun "\t.xdata4 \"__ex_table\", 1b-., 1f-.\n" \
118*4882a593Smuzhiyun "[1:]" \
119*4882a593Smuzhiyun : "=r"(__pu_r8) : "m"(__m(addr)), "rO"(val), "0"(__pu_r8)); \
120*4882a593Smuzhiyun (err) = __pu_r8; \
121*4882a593Smuzhiyun } while (0)
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun #else /* !ASM_SUPPORTED */
124*4882a593Smuzhiyun # define RELOC_TYPE 2 /* ip-rel */
125*4882a593Smuzhiyun # define __get_user_size(val, addr, n, err) \
126*4882a593Smuzhiyun do { \
127*4882a593Smuzhiyun __ld_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE); \
128*4882a593Smuzhiyun (err) = ia64_getreg(_IA64_REG_R8); \
129*4882a593Smuzhiyun (val) = ia64_getreg(_IA64_REG_R9); \
130*4882a593Smuzhiyun } while (0)
131*4882a593Smuzhiyun # define __put_user_size(val, addr, n, err) \
132*4882a593Smuzhiyun do { \
133*4882a593Smuzhiyun __st_user("__ex_table", (unsigned long) addr, n, RELOC_TYPE, \
134*4882a593Smuzhiyun (__force unsigned long) (val)); \
135*4882a593Smuzhiyun (err) = ia64_getreg(_IA64_REG_R8); \
136*4882a593Smuzhiyun } while (0)
137*4882a593Smuzhiyun #endif /* !ASM_SUPPORTED */
138*4882a593Smuzhiyun
139*4882a593Smuzhiyun extern void __get_user_unknown (void);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
143*4882a593Smuzhiyun * could clobber r8 and r9 (among others). Thus, be careful not to evaluate it while
144*4882a593Smuzhiyun * using r8/r9.
145*4882a593Smuzhiyun */
146*4882a593Smuzhiyun #define __do_get_user(check, x, ptr, size) \
147*4882a593Smuzhiyun ({ \
148*4882a593Smuzhiyun const __typeof__(*(ptr)) __user *__gu_ptr = (ptr); \
149*4882a593Smuzhiyun __typeof__ (size) __gu_size = (size); \
150*4882a593Smuzhiyun long __gu_err = -EFAULT; \
151*4882a593Smuzhiyun unsigned long __gu_val = 0; \
152*4882a593Smuzhiyun if (!check || __access_ok(__gu_ptr, size)) \
153*4882a593Smuzhiyun switch (__gu_size) { \
154*4882a593Smuzhiyun case 1: __get_user_size(__gu_val, __gu_ptr, 1, __gu_err); break; \
155*4882a593Smuzhiyun case 2: __get_user_size(__gu_val, __gu_ptr, 2, __gu_err); break; \
156*4882a593Smuzhiyun case 4: __get_user_size(__gu_val, __gu_ptr, 4, __gu_err); break; \
157*4882a593Smuzhiyun case 8: __get_user_size(__gu_val, __gu_ptr, 8, __gu_err); break; \
158*4882a593Smuzhiyun default: __get_user_unknown(); break; \
159*4882a593Smuzhiyun } \
160*4882a593Smuzhiyun (x) = (__force __typeof__(*(__gu_ptr))) __gu_val; \
161*4882a593Smuzhiyun __gu_err; \
162*4882a593Smuzhiyun })
163*4882a593Smuzhiyun
164*4882a593Smuzhiyun #define __get_user_nocheck(x, ptr, size) __do_get_user(0, x, ptr, size)
165*4882a593Smuzhiyun #define __get_user_check(x, ptr, size) __do_get_user(1, x, ptr, size)
166*4882a593Smuzhiyun
167*4882a593Smuzhiyun extern void __put_user_unknown (void);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun /*
170*4882a593Smuzhiyun * Evaluating arguments X, PTR, SIZE, and SEGMENT may involve subroutine-calls, which
171*4882a593Smuzhiyun * could clobber r8 (among others). Thus, be careful not to evaluate them while using r8.
172*4882a593Smuzhiyun */
173*4882a593Smuzhiyun #define __do_put_user(check, x, ptr, size) \
174*4882a593Smuzhiyun ({ \
175*4882a593Smuzhiyun __typeof__ (x) __pu_x = (x); \
176*4882a593Smuzhiyun __typeof__ (*(ptr)) __user *__pu_ptr = (ptr); \
177*4882a593Smuzhiyun __typeof__ (size) __pu_size = (size); \
178*4882a593Smuzhiyun long __pu_err = -EFAULT; \
179*4882a593Smuzhiyun \
180*4882a593Smuzhiyun if (!check || __access_ok(__pu_ptr, __pu_size)) \
181*4882a593Smuzhiyun switch (__pu_size) { \
182*4882a593Smuzhiyun case 1: __put_user_size(__pu_x, __pu_ptr, 1, __pu_err); break; \
183*4882a593Smuzhiyun case 2: __put_user_size(__pu_x, __pu_ptr, 2, __pu_err); break; \
184*4882a593Smuzhiyun case 4: __put_user_size(__pu_x, __pu_ptr, 4, __pu_err); break; \
185*4882a593Smuzhiyun case 8: __put_user_size(__pu_x, __pu_ptr, 8, __pu_err); break; \
186*4882a593Smuzhiyun default: __put_user_unknown(); break; \
187*4882a593Smuzhiyun } \
188*4882a593Smuzhiyun __pu_err; \
189*4882a593Smuzhiyun })
190*4882a593Smuzhiyun
191*4882a593Smuzhiyun #define __put_user_nocheck(x, ptr, size) __do_put_user(0, x, ptr, size)
192*4882a593Smuzhiyun #define __put_user_check(x, ptr, size) __do_put_user(1, x, ptr, size)
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun /*
195*4882a593Smuzhiyun * Complex access routines
196*4882a593Smuzhiyun */
197*4882a593Smuzhiyun extern unsigned long __must_check __copy_user (void __user *to, const void __user *from,
198*4882a593Smuzhiyun unsigned long count);
199*4882a593Smuzhiyun
200*4882a593Smuzhiyun static inline unsigned long
raw_copy_to_user(void __user * to,const void * from,unsigned long count)201*4882a593Smuzhiyun raw_copy_to_user(void __user *to, const void *from, unsigned long count)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun return __copy_user(to, (__force void __user *) from, count);
204*4882a593Smuzhiyun }
205*4882a593Smuzhiyun
206*4882a593Smuzhiyun static inline unsigned long
raw_copy_from_user(void * to,const void __user * from,unsigned long count)207*4882a593Smuzhiyun raw_copy_from_user(void *to, const void __user *from, unsigned long count)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun return __copy_user((__force void __user *) to, from, count);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun #define INLINE_COPY_FROM_USER
213*4882a593Smuzhiyun #define INLINE_COPY_TO_USER
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun extern unsigned long __do_clear_user (void __user *, unsigned long);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun #define __clear_user(to, n) __do_clear_user(to, n)
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun #define clear_user(to, n) \
220*4882a593Smuzhiyun ({ \
221*4882a593Smuzhiyun unsigned long __cu_len = (n); \
222*4882a593Smuzhiyun if (__access_ok(to, __cu_len)) \
223*4882a593Smuzhiyun __cu_len = __do_clear_user(to, __cu_len); \
224*4882a593Smuzhiyun __cu_len; \
225*4882a593Smuzhiyun })
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun
228*4882a593Smuzhiyun /*
229*4882a593Smuzhiyun * Returns: -EFAULT if exception before terminator, N if the entire buffer filled, else
230*4882a593Smuzhiyun * strlen.
231*4882a593Smuzhiyun */
232*4882a593Smuzhiyun extern long __must_check __strncpy_from_user (char *to, const char __user *from, long to_len);
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun #define strncpy_from_user(to, from, n) \
235*4882a593Smuzhiyun ({ \
236*4882a593Smuzhiyun const char __user * __sfu_from = (from); \
237*4882a593Smuzhiyun long __sfu_ret = -EFAULT; \
238*4882a593Smuzhiyun if (__access_ok(__sfu_from, 0)) \
239*4882a593Smuzhiyun __sfu_ret = __strncpy_from_user((to), __sfu_from, (n)); \
240*4882a593Smuzhiyun __sfu_ret; \
241*4882a593Smuzhiyun })
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun /*
244*4882a593Smuzhiyun * Returns: 0 if exception before NUL or reaching the supplied limit
245*4882a593Smuzhiyun * (N), a value greater than N if the limit would be exceeded, else
246*4882a593Smuzhiyun * strlen.
247*4882a593Smuzhiyun */
248*4882a593Smuzhiyun extern unsigned long __strnlen_user (const char __user *, long);
249*4882a593Smuzhiyun
250*4882a593Smuzhiyun #define strnlen_user(str, len) \
251*4882a593Smuzhiyun ({ \
252*4882a593Smuzhiyun const char __user *__su_str = (str); \
253*4882a593Smuzhiyun unsigned long __su_ret = 0; \
254*4882a593Smuzhiyun if (__access_ok(__su_str, 0)) \
255*4882a593Smuzhiyun __su_ret = __strnlen_user(__su_str, len); \
256*4882a593Smuzhiyun __su_ret; \
257*4882a593Smuzhiyun })
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun #define ARCH_HAS_TRANSLATE_MEM_PTR 1
260*4882a593Smuzhiyun static __inline__ void *
xlate_dev_mem_ptr(phys_addr_t p)261*4882a593Smuzhiyun xlate_dev_mem_ptr(phys_addr_t p)
262*4882a593Smuzhiyun {
263*4882a593Smuzhiyun struct page *page;
264*4882a593Smuzhiyun void *ptr;
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun page = pfn_to_page(p >> PAGE_SHIFT);
267*4882a593Smuzhiyun if (PageUncached(page))
268*4882a593Smuzhiyun ptr = (void *)p + __IA64_UNCACHED_OFFSET;
269*4882a593Smuzhiyun else
270*4882a593Smuzhiyun ptr = __va(p);
271*4882a593Smuzhiyun
272*4882a593Smuzhiyun return ptr;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /*
276*4882a593Smuzhiyun * Convert a virtual cached kernel memory pointer to an uncached pointer
277*4882a593Smuzhiyun */
278*4882a593Smuzhiyun static __inline__ void *
xlate_dev_kmem_ptr(void * p)279*4882a593Smuzhiyun xlate_dev_kmem_ptr(void *p)
280*4882a593Smuzhiyun {
281*4882a593Smuzhiyun struct page *page;
282*4882a593Smuzhiyun void *ptr;
283*4882a593Smuzhiyun
284*4882a593Smuzhiyun page = virt_to_page((unsigned long)p);
285*4882a593Smuzhiyun if (PageUncached(page))
286*4882a593Smuzhiyun ptr = (void *)__pa(p) + __IA64_UNCACHED_OFFSET;
287*4882a593Smuzhiyun else
288*4882a593Smuzhiyun ptr = p;
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun return ptr;
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun #endif /* _ASM_IA64_UACCESS_H */
294