blob: ebf8a845b01742f1fe75c8cace45b6e2ef4373ce [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070014/*
15 * Note that since kernel addresses are in a separate address space on
Jesper Juhle49332b2005-05-01 08:59:08 -070016 * parisc, we don't need to do anything for access_ok().
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 * We just let the page fault handler do the right thing. This also means
18 * that put_user is the same as __put_user, etc.
19 */
20
Linus Torvalds96d4f262019-01-03 18:57:57 -080021#define access_ok(uaddr, size) \
Helge Deller186ecf12017-03-15 21:48:42 +010022 ( (uaddr) == (uaddr) )
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#define put_user __put_user
25#define get_user __get_user
26
Helge Dellerca72a222006-12-20 00:35:57 +010027#if !defined(CONFIG_64BIT)
Helge Deller67102872021-09-09 12:47:00 +020028#define LDD_USER(sr, val, ptr) __get_user_asm64(sr, val, ptr)
29#define STD_USER(sr, x, ptr) __put_user_asm64(sr, x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#else
Helge Deller67102872021-09-09 12:47:00 +020031#define LDD_USER(sr, val, ptr) __get_user_asm(sr, val, "ldd", ptr)
32#define STD_USER(sr, x, ptr) __put_user_asm(sr, "std", x, ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#endif
34
35/*
Helge Dellercb910c12016-04-08 21:36:06 +020036 * The exception table contains two values: the first is the relative offset to
37 * the address of the instruction that is allowed to fault, and the second is
38 * the relative offset to the address of the fixup routine. Since relative
39 * addresses are used, 32bit values are sufficient even on 64bit kernel.
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 */
41
Helge Deller0de79852016-03-23 16:00:46 +010042#define ARCH_HAS_RELATIVE_EXTABLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070043struct exception_table_entry {
Helge Deller0de79852016-03-23 16:00:46 +010044 int insn; /* relative address of insn that is allowed to fault. */
45 int fixup; /* relative address of fixup routine */
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Helge Deller0b3d6432007-01-28 14:52:57 +010048#define ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr )\
49 ".section __ex_table,\"aw\"\n" \
Helge Deller0de79852016-03-23 16:00:46 +010050 ".word (" #fault_addr " - .), (" #except_addr " - .)\n\t" \
Helge Deller0b3d6432007-01-28 14:52:57 +010051 ".previous\n"
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053/*
Helge Dellerd19f5e42017-03-25 11:59:15 +010054 * ASM_EXCEPTIONTABLE_ENTRY_EFAULT() creates a special exception table entry
55 * (with lowest bit set) for which the fault handler in fixup_exception() will
Helge Deller4b9d2a72021-12-23 21:46:35 +010056 * load -EFAULT into %r29 for a read or write fault, and zeroes the target
Helge Dellerd19f5e42017-03-25 11:59:15 +010057 * register in case of a read fault in get_user().
58 */
Helge Deller4b9d2a72021-12-23 21:46:35 +010059#define ASM_EXCEPTIONTABLE_REG 29
60#define ASM_EXCEPTIONTABLE_VAR(__variable) \
61 register long __variable __asm__ ("r29") = 0
Helge Dellerd19f5e42017-03-25 11:59:15 +010062#define ASM_EXCEPTIONTABLE_ENTRY_EFAULT( fault_addr, except_addr )\
63 ASM_EXCEPTIONTABLE_ENTRY( fault_addr, except_addr + 1)
64
Helge Deller67102872021-09-09 12:47:00 +020065#define __get_user_internal(sr, val, ptr) \
Helge Deller3f795ce2017-04-16 10:00:14 +020066({ \
Helge Deller4b9d2a72021-12-23 21:46:35 +010067 ASM_EXCEPTIONTABLE_VAR(__gu_err); \
Helge Deller3f795ce2017-04-16 10:00:14 +020068 \
69 switch (sizeof(*(ptr))) { \
Helge Deller67102872021-09-09 12:47:00 +020070 case 1: __get_user_asm(sr, val, "ldb", ptr); break; \
71 case 2: __get_user_asm(sr, val, "ldh", ptr); break; \
72 case 4: __get_user_asm(sr, val, "ldw", ptr); break; \
73 case 8: LDD_USER(sr, val, ptr); break; \
Helge Deller3f795ce2017-04-16 10:00:14 +020074 default: BUILD_BUG(); \
75 } \
76 \
77 __gu_err; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070078})
79
Helge Deller3f795ce2017-04-16 10:00:14 +020080#define __get_user(val, ptr) \
81({ \
Helge Deller67102872021-09-09 12:47:00 +020082 __get_user_internal("%%sr3,", val, ptr); \
Helge Deller3f795ce2017-04-16 10:00:14 +020083})
84
Helge Deller67102872021-09-09 12:47:00 +020085#define __get_user_asm(sr, val, ldx, ptr) \
Helge Deller3f795ce2017-04-16 10:00:14 +020086{ \
87 register long __gu_val; \
88 \
Helge Deller67102872021-09-09 12:47:00 +020089 __asm__("1: " ldx " 0(" sr "%2),%0\n" \
Helge Dellerd19f5e42017-03-25 11:59:15 +010090 "9:\n" \
91 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 : "=r"(__gu_val), "=r"(__gu_err) \
Helge Deller3f795ce2017-04-16 10:00:14 +020093 : "r"(ptr), "1"(__gu_err)); \
94 \
95 (val) = (__force __typeof__(*(ptr))) __gu_val; \
96}
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
Helge Deller67102872021-09-09 12:47:00 +020098#define HAVE_GET_KERNEL_NOFAULT
99#define __get_kernel_nofault(dst, src, type, err_label) \
100{ \
101 type __z; \
102 long __err; \
103 __err = __get_user_internal("%%sr0,", __z, (type *)(src)); \
104 if (unlikely(__err)) \
105 goto err_label; \
106 else \
107 *(type *)(dst) = __z; \
108}
109
110
Helge Dellerd2ad8242016-04-09 08:27:08 +0200111#if !defined(CONFIG_64BIT)
112
Helge Deller67102872021-09-09 12:47:00 +0200113#define __get_user_asm64(sr, val, ptr) \
Helge Deller3f795ce2017-04-16 10:00:14 +0200114{ \
115 union { \
116 unsigned long long l; \
117 __typeof__(*(ptr)) t; \
118 } __gu_tmp; \
119 \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100120 __asm__(" copy %%r0,%R0\n" \
Helge Deller67102872021-09-09 12:47:00 +0200121 "1: ldw 0(" sr "%2),%0\n" \
122 "2: ldw 4(" sr "%2),%R0\n" \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100123 "9:\n" \
124 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
125 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
Helge Deller3f795ce2017-04-16 10:00:14 +0200126 : "=&r"(__gu_tmp.l), "=r"(__gu_err) \
127 : "r"(ptr), "1"(__gu_err)); \
128 \
129 (val) = __gu_tmp.t; \
130}
Helge Dellerd2ad8242016-04-09 08:27:08 +0200131
132#endif /* !defined(CONFIG_64BIT) */
133
134
Helge Deller67102872021-09-09 12:47:00 +0200135#define __put_user_internal(sr, x, ptr) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136({ \
Helge Deller4b9d2a72021-12-23 21:46:35 +0100137 ASM_EXCEPTIONTABLE_VAR(__pu_err); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 __typeof__(*(ptr)) __x = (__typeof__(*(ptr)))(x); \
139 \
Helge Deller06bff6b2016-04-09 08:26:14 +0200140 switch (sizeof(*(ptr))) { \
Helge Deller67102872021-09-09 12:47:00 +0200141 case 1: __put_user_asm(sr, "stb", __x, ptr); break; \
142 case 2: __put_user_asm(sr, "sth", __x, ptr); break; \
143 case 4: __put_user_asm(sr, "stw", __x, ptr); break; \
144 case 8: STD_USER(sr, __x, ptr); break; \
Helge Deller3f795ce2017-04-16 10:00:14 +0200145 default: BUILD_BUG(); \
146 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 \
148 __pu_err; \
149})
150
Helge Deller3f795ce2017-04-16 10:00:14 +0200151#define __put_user(x, ptr) \
152({ \
Helge Deller67102872021-09-09 12:47:00 +0200153 __put_user_internal("%%sr3,", x, ptr); \
Helge Deller3f795ce2017-04-16 10:00:14 +0200154})
155
Helge Deller67102872021-09-09 12:47:00 +0200156#define __put_kernel_nofault(dst, src, type, err_label) \
157{ \
158 type __z = *(type *)(src); \
159 long __err; \
160 __err = __put_user_internal("%%sr0,", __z, (type *)(dst)); \
161 if (unlikely(__err)) \
162 goto err_label; \
163}
164
165
166
Helge Deller3f795ce2017-04-16 10:00:14 +0200167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168/*
169 * The "__put_user/kernel_asm()" macros tell gcc they read from memory
170 * instead of writing. This is because they do not write to any memory
Carlos O'Donell3fd3a742006-04-22 14:47:21 -0600171 * gcc knows about, so there are no aliasing issues. These macros must
Helge Dellerd19f5e42017-03-25 11:59:15 +0100172 * also be aware that fixups are executed in the context of the fault,
173 * and any registers used there must be listed as clobbers.
Helge Deller4b9d2a72021-12-23 21:46:35 +0100174 * The register holding the possible EFAULT error (ASM_EXCEPTIONTABLE_REG)
175 * is already listed as input and output register.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 */
177
Helge Deller67102872021-09-09 12:47:00 +0200178#define __put_user_asm(sr, stx, x, ptr) \
179 __asm__ __volatile__ ( \
180 "1: " stx " %2,0(" sr "%1)\n" \
181 "9:\n" \
182 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
183 : "=r"(__pu_err) \
Helge Dellerd19f5e42017-03-25 11:59:15 +0100184 : "r"(ptr), "r"(x), "0"(__pu_err))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Helge Dellerca72a222006-12-20 00:35:57 +0100187#if !defined(CONFIG_64BIT)
Helge Deller94a19812006-12-19 22:33:58 +0100188
Helge Deller67102872021-09-09 12:47:00 +0200189#define __put_user_asm64(sr, __val, ptr) do { \
190 __asm__ __volatile__ ( \
191 "1: stw %2,0(" sr "%1)\n" \
192 "2: stw %R2,4(" sr "%1)\n" \
193 "9:\n" \
194 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(1b, 9b) \
195 ASM_EXCEPTIONTABLE_ENTRY_EFAULT(2b, 9b) \
196 : "=r"(__pu_err) \
197 : "r"(ptr), "r"(__val), "0"(__pu_err)); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198} while (0)
199
Helge Dellerca72a222006-12-20 00:35:57 +0100200#endif /* !defined(CONFIG_64BIT) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202
203/*
204 * Complex access routines -- external declarations
205 */
206
James Bottomleyb1195c02012-05-26 09:48:19 +0100207extern long strncpy_from_user(char *, const char __user *, long);
Helge Deller67102872021-09-09 12:47:00 +0200208extern __must_check unsigned lclear_user(void __user *, unsigned long);
Helge Deller1260dea2021-09-04 23:49:26 +0200209extern __must_check long strnlen_user(const char __user *src, long n);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/*
211 * Complex access routines -- macros
212 */
213
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214#define clear_user lclear_user
215#define __clear_user lclear_user
216
Al Virof64fd182017-03-24 23:11:26 -0400217unsigned long __must_check raw_copy_to_user(void __user *dst, const void *src,
218 unsigned long len);
219unsigned long __must_check raw_copy_from_user(void *dst, const void __user *src,
220 unsigned long len);
Al Virof64fd182017-03-24 23:11:26 -0400221#define INLINE_COPY_TO_USER
222#define INLINE_COPY_FROM_USER
Helge Deller888c31f2010-02-01 19:56:33 +0000223
Helge Dellere4483722009-01-13 20:52:46 +0100224struct pt_regs;
Kyle McMartinc61c25e2008-12-20 02:29:06 +0000225int fixup_exception(struct pt_regs *regs);
226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#endif /* __PARISC_UACCESS_H */