blob: cfb4ca7e39fe2869129f0b6df1511ee63d43017d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __PARISC_UACCESS_H
3#define __PARISC_UACCESS_H
4
5/*
6 * User space memory access functions
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <asm/cache.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010
Helge Deller8dd95c62014-10-21 21:29:09 +020011#include <linux/bug.h>
Al Viroaace8802016-08-20 19:03:37 -040012#include <linux/string.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
14#define KERNEL_DS ((mm_segment_t){0})
15#define USER_DS ((mm_segment_t){1})
16
Christoph Hellwig428e2972020-08-11 18:33:44 -070017#define uaccess_kernel() (get_fs().seg == KERNEL_DS.seg)
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#define get_fs() (current_thread_info()->addr_limit)
20#define set_fs(x) (current_thread_info()->addr_limit = (x))
21
22/*
23 * Note that since kernel addresses are in a separate address space on
Jesper Juhle49332b2005-05-01 08:59:08 -070024 * parisc, we don't need to do anything for access_ok().
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 * We just let the page fault handler do the right thing. This also means
26 * that put_user is the same as __put_user, etc.
27 */
28
Linus Torvalds96d4f262019-01-03 18:57:57 -080029#define access_ok(uaddr, size) \
Helge Deller186ecf12017-03-15 21:48:42 +010030 ( (uaddr) == (uaddr) )
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#define put_user __put_user
33#define get_user __get_user
34
Helge Dellerca72a222006-12-20 00:35:57 +010035#if !defined(CONFIG_64BIT)
Helge Deller3f795ce2017-04-16 10:00:14 +020036#define LDD_USER(val, ptr) __get_user_asm64(val, ptr)
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020037#define STD_USER(x, ptr) __put_user_asm64(x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#else
Helge Deller3f795ce2017-04-16 10:00:14 +020039#define LDD_USER(val, ptr) __get_user_asm(val, "ldd", ptr)
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +020040#define STD_USER(x, ptr) __put_user_asm("std", x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#endif
42
43/*
Helge Dellercb910c12016-04-08 21:36:06 +020044 * The exception table contains two values: the first is the relative offset to
45 * the address of the instruction that is allowed to fault, and the second is
46 * the relative offset to the address of the fixup routine. Since relative
47 * addresses are used, 32bit values are sufficient even on 64bit kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 */
49
Helge Deller0de79852016-03-23 16:00:46 +010050#define ARCH_HAS_RELATIVE_EXTABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070051struct exception_table_entry {
Helge Deller0de79852016-03-23 16:00:46 +010052 int insn; /* relative address of insn that is allowed to fault. */
53 int fixup; /* relative address of fixup routine */
Linus Torvalds1da177e2005-04-16 15:20:36 -070054};
55
Helge Deller0b3d6432007-01-28 14:52:57 +010056#define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
57 ".section __ex_table,\"aw\"\n" \
Helge Deller0de79852016-03-23 16:00:46 +010058 ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
Helge Deller0b3d6432007-01-28 14:52:57 +010059 ".previous\n"
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061/*
Helge Dellerd19f5e42017-03-25 11:59:15 +010062 * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
63 * (with lowest bit set) for which the fault handler in fixup_exception() will
64 * load -EFAULT into %r8 for a read or write fault, and zeroes the target
65 * register in case of a read fault in get_user().
66 */
67#define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
68 ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)
69
70/*
Helge Deller06bff6b2016-04-09 08:26:14 +020071 * load_sr2() preloads the space register %%sr2 - based on the value of
72 * get_fs() - with either a value of 0 to access kernel space (KERNEL_DS which
73 * is 0), or with the current value of %%sr3 to access user space (USER_DS)
74 * memory. The following __get_user_asm() and __put_user_asm() functions have
75 * %%sr2 hard-coded to access the requested memory.
76 */
77#define load_sr2() \
78 __asm__(" or,= %0,%%r0,%%r0\n\t" \
79 " mfsp %%sr3,%0\n\t" \
80 " mtsp %0,%%sr2\n\t" \
81 : : "r"(get_fs()) : )
82
Helge Deller3f795ce2017-04-16 10:00:14 +020083#define __get_user_internal(val, ptr) \
84({ \
85 register long __gu_err __asm__ ("r8") = 0; \
86 \
87 switch (sizeof(*(ptr))) { \
88 case 1: __get_user_asm(val, "ldb", ptr); break; \
89 case 2: __get_user_asm(val, "ldh", ptr); break; \
90 case 4: __get_user_asm(val, "ldw", ptr); break; \
91 case 8: LDD_USER(val, ptr); break; \
92 default: BUILD_BUG(); \
93 } \
94 \
95 __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070096})
97
Helge Deller3f795ce2017-04-16 10:00:14 +020098#define __get_user(val, ptr) \
99({ \
100 load_sr2(); \
101 __get_user_internal(val, ptr); \
102})
103
104#define __get_user_asm(val, ldx, ptr) \
105{ \
106 register long __gu_val; \
107 \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100108 __asm__("1: " ldx " 0(%%sr2,%2),%0\n" \
109 "9:\n" \
110 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 : "=r"(__gu_val), "=r"(__gu_err) \
Helge Deller3f795ce2017-04-16 10:00:14 +0200112 : "r"(ptr), "1"(__gu_err)); \
113 \
114 (val) = (__force __typeof__(*(ptr))) __gu_val; \
115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Helge Dellerd2ad8242016-04-09 08:27:08 +0200117#if !defined(CONFIG_64BIT)
118
Helge Deller3f795ce2017-04-16 10:00:14 +0200119#define __get_user_asm64(val, ptr) \
120{ \
121 union { \
122 unsigned long long l; \
123 __typeof__(*(ptr)) t; \
124 } __gu_tmp; \
125 \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100126 __asm__(" copy %%r0,%R0\n" \
127 "1: ldw 0(%%sr2,%2),%0\n" \
128 "2: ldw 4(%%sr2,%2),%R0\n" \
129 "9:\n" \
130 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
131 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
Helge Deller3f795ce2017-04-16 10:00:14 +0200132 : "=&r"(__gu_tmp.l), "=r"(__gu_err) \
133 : "r"(ptr), "1"(__gu_err)); \
134 \
135 (val) = __gu_tmp.t; \
136}
Helge Dellerd2ad8242016-04-09 08:27:08 +0200137
138#endif /* !defined(CONFIG_64BIT) */
139
140
Helge Deller3f795ce2017-04-16 10:00:14 +0200141#define __put_user_internal(x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142({ \
143 register long __pu_err __asm__ ("r8") = 0; \
144 __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \
145 \
Helge Deller06bff6b2016-04-09 08:26:14 +0200146 switch (sizeof(*(ptr))) { \
Helge Deller3f795ce2017-04-16 10:00:14 +0200147 case 1: __put_user_asm("stb", __x, ptr); break; \
148 case 2: __put_user_asm("sth", __x, ptr); break; \
149 case 4: __put_user_asm("stw", __x, ptr); break; \
150 case 8: STD_USER(__x, ptr); break; \
151 default: BUILD_BUG(); \
152 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 \
154 __pu_err; \
155})
156
Helge Deller3f795ce2017-04-16 10:00:14 +0200157#define __put_user(x, ptr) \
158({ \
159 load_sr2(); \
160 __put_user_internal(x, ptr); \
161})
162
163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164/*
165 * The "__put_user/kernel_asm()" macros tell gcc they read from memory
166 * instead of writing. This is because they do not write to any memory
Carlos O'Donell3fd3a742006-04-22 14:47:21 -0600167 * gcc knows about, so there are no aliasing issues. These macros must
Helge Dellerd19f5e42017-03-25 11:59:15 +0100168 * also be aware that fixups are executed in the context of the fault,
169 * and any registers used there must be listed as clobbers.
170 * r8 is already listed as err.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 */
172
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200173#define __put_user_asm(stx, x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 __asm__ __volatile__ ( \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100175 "1: " stx " %2,0(%%sr2,%1)\n" \
176 "9:\n" \
177 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 : "=r"(__pu_err) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100179 : "r"(ptr), "r"(x), "0"(__pu_err))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Helge Dellerca72a222006-12-20 00:35:57 +0100182#if !defined(CONFIG_64BIT)
Helge Deller94a19812006-12-19 22:33:58 +0100183
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200184#define __put_user_asm64(__val, ptr) do { \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 __asm__ __volatile__ ( \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100186 "1: stw %2,0(%%sr2,%1)\n" \
187 "2: stw %R2,4(%%sr2,%1)\n" \
188 "9:\n" \
189 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
190 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 : "=r"(__pu_err) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100192 : "r"(ptr), "r"(__val), "0"(__pu_err)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193} while (0)
194
Helge Dellerca72a222006-12-20 00:35:57 +0100195#endif /* !defined(CONFIG_64BIT) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197
198/*
199 * Complex access routines -- external declarations
200 */
201
James Bottomleyb1195c02012-05-26 09:48:19 +0100202extern long strncpy_from_user(char *, const char __user *, long);
Michael S. Tsirkinb9762e72015-01-06 17:45:34 +0200203extern unsigned lclear_user(void __user *, unsigned long);
Helge Deller1260dea2021-09-04 23:49:26 +0200204extern __must_check long strnlen_user(const char __user *src, long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * Complex access routines -- macros
207 */
Helge Dellera0ffa8f2013-11-19 23:31:35 +0100208#define user_addr_max() (~0UL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210#define clear_user lclear_user
211#define __clear_user lclear_user
212
Al Virof64fd182017-03-24 23:11:26 -0400213unsigned long __must_check raw_copy_to_user(void __user *dst, const void *src,
214 unsigned long len);
215unsigned long __must_check raw_copy_from_user(void *dst, const void __user *src,
216 unsigned long len);
Al Virof64fd182017-03-24 23:11:26 -0400217#define INLINE_COPY_TO_USER
218#define INLINE_COPY_FROM_USER
Helge Deller888c31f2010-02-01 19:56:33 +0000219
Helge Dellere4483722009-01-13 20:52:46 +0100220struct pt_regs;
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000221int fixup_exception(struct pt_regs *regs);
222
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#endif /* __PARISC_UACCESS_H */