blob: a596e69d3c47446b7d4b6f4d660da67b7965e8c8 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Heiko Carstens457f2182014-03-21 10:42:25 +01002/*
3 * Standard user space access functions based on mvcp/mvcs and doing
4 * interesting things in the secondary space mode.
5 *
6 * Copyright IBM Corp. 2006,2014
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
8 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
9 */
10
11#include <linux/jump_label.h>
12#include <linux/uaccess.h>
13#include <linux/export.h>
14#include <linux/errno.h>
15#include <linux/mm.h>
16#include <asm/mmu_context.h>
17#include <asm/facility.h>
18
Sven Schnelle56e62a72020-11-21 11:14:56 +010019#ifdef CONFIG_DEBUG_ENTRY
20void debug_user_asce(int exit)
Heiko Carstens062e5272020-11-16 08:06:41 +010021{
22 unsigned long cr1, cr7;
23
24 __ctl_store(cr1, 1, 1);
25 __ctl_store(cr7, 7, 7);
26 if (cr1 == S390_lowcore.kernel_asce && cr7 == S390_lowcore.user_asce)
27 return;
Sven Schnelle56e62a72020-11-21 11:14:56 +010028 panic("incorrect ASCE on kernel %s\n"
Heiko Carstens062e5272020-11-16 08:06:41 +010029 "cr1: %016lx cr7: %016lx\n"
30 "kernel: %016llx user: %016llx\n",
Sven Schnelle56e62a72020-11-21 11:14:56 +010031 exit ? "exit" : "entry", cr1, cr7,
32 S390_lowcore.kernel_asce, S390_lowcore.user_asce);
33
Heiko Carstens062e5272020-11-16 08:06:41 +010034}
Sven Schnelle56e62a72020-11-21 11:14:56 +010035#endif /*CONFIG_DEBUG_ENTRY */
Heiko Carstens062e5272020-11-16 08:06:41 +010036
Martin Schwidefskyd66bf802017-08-21 14:47:04 +020037#ifndef CONFIG_HAVE_MARCH_Z10_FEATURES
Heiko Carstensed79e942015-07-29 08:31:24 +020038static DEFINE_STATIC_KEY_FALSE(have_mvcos);
Heiko Carstens457f2182014-03-21 10:42:25 +010039
Martin Schwidefskyd66bf802017-08-21 14:47:04 +020040static int __init uaccess_init(void)
41{
42 if (test_facility(27))
43 static_branch_enable(&have_mvcos);
44 return 0;
45}
46early_initcall(uaccess_init);
47
48static inline int copy_with_mvcos(void)
49{
50 if (static_branch_likely(&have_mvcos))
51 return 1;
52 return 0;
53}
54#else
55static inline int copy_with_mvcos(void)
56{
57 return 1;
58}
59#endif
60
Heiko Carstens457f2182014-03-21 10:42:25 +010061static inline unsigned long copy_from_user_mvcos(void *x, const void __user *ptr,
62 unsigned long size)
63{
Heiko Carstens457f2182014-03-21 10:42:25 +010064 unsigned long tmp1, tmp2;
65
66 tmp1 = -4096UL;
67 asm volatile(
Heiko Carstensd1e18ef2021-06-21 13:38:45 +020068 " lghi 0,%[spec]\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010069 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
Al Viro37096002017-03-28 15:06:24 -040070 "6: jz 4f\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +010071 "1: algr %0,%3\n"
72 " slgr %1,%3\n"
73 " slgr %2,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010074 " j 0b\n"
75 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
76 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +010077 " slgr %4,%1\n"
78 " clgr %0,%4\n" /* copy crosses next page boundary? */
Al Viro37096002017-03-28 15:06:24 -040079 " jnh 5f\n"
Heiko Carstens457f2182014-03-21 10:42:25 +010080 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
Al Viro37096002017-03-28 15:06:24 -040081 "7: slgr %0,%4\n"
82 " j 5f\n"
83 "4: slgr %0,%0\n"
84 "5:\n"
85 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
Heiko Carstens457f2182014-03-21 10:42:25 +010086 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
Heiko Carstensd1e18ef2021-06-21 13:38:45 +020087 : [spec] "K" (0x81UL)
88 : "cc", "memory", "0");
Heiko Carstens457f2182014-03-21 10:42:25 +010089 return size;
90}
91
92static inline unsigned long copy_from_user_mvcp(void *x, const void __user *ptr,
93 unsigned long size)
94{
95 unsigned long tmp1, tmp2;
96
Heiko Carstens457f2182014-03-21 10:42:25 +010097 tmp1 = -256UL;
98 asm volatile(
99 " sacf 0\n"
100 "0: mvcp 0(%0,%2),0(%1),%3\n"
Al Viro37096002017-03-28 15:06:24 -0400101 "7: jz 5f\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100102 "1: algr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100103 " la %1,256(%1)\n"
104 " la %2,256(%2)\n"
105 "2: mvcp 0(%0,%2),0(%1),%3\n"
Al Viro37096002017-03-28 15:06:24 -0400106 "8: jnz 1b\n"
107 " j 5f\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100108 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100109 " lghi %3,-4096\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100110 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100111 " slgr %4,%1\n"
112 " clgr %0,%4\n" /* copy crosses next page boundary? */
Al Viro37096002017-03-28 15:06:24 -0400113 " jnh 6f\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100114 "4: mvcp 0(%4,%2),0(%1),%3\n"
Al Viro37096002017-03-28 15:06:24 -0400115 "9: slgr %0,%4\n"
116 " j 6f\n"
117 "5: slgr %0,%0\n"
118 "6: sacf 768\n"
119 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
120 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
Heiko Carstens457f2182014-03-21 10:42:25 +0100121 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
122 : : "cc", "memory");
123 return size;
124}
125
Al Viro37096002017-03-28 15:06:24 -0400126unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
Heiko Carstens457f2182014-03-21 10:42:25 +0100127{
Martin Schwidefskyd66bf802017-08-21 14:47:04 +0200128 if (copy_with_mvcos())
Heiko Carstens457f2182014-03-21 10:42:25 +0100129 return copy_from_user_mvcos(to, from, n);
130 return copy_from_user_mvcp(to, from, n);
131}
Al Viro37096002017-03-28 15:06:24 -0400132EXPORT_SYMBOL(raw_copy_from_user);
Heiko Carstens457f2182014-03-21 10:42:25 +0100133
134static inline unsigned long copy_to_user_mvcos(void __user *ptr, const void *x,
135 unsigned long size)
136{
Heiko Carstens457f2182014-03-21 10:42:25 +0100137 unsigned long tmp1, tmp2;
138
139 tmp1 = -4096UL;
140 asm volatile(
Heiko Carstensd1e18ef2021-06-21 13:38:45 +0200141 " llilh 0,%[spec]\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100142 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
143 "6: jz 4f\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100144 "1: algr %0,%3\n"
145 " slgr %1,%3\n"
146 " slgr %2,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100147 " j 0b\n"
148 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
149 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100150 " slgr %4,%1\n"
151 " clgr %0,%4\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100152 " jnh 5f\n"
153 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100154 "7: slgr %0,%4\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100155 " j 5f\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100156 "4: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100157 "5:\n"
158 EX_TABLE(0b,2b) EX_TABLE(3b,5b) EX_TABLE(6b,2b) EX_TABLE(7b,5b)
159 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
Heiko Carstensd1e18ef2021-06-21 13:38:45 +0200160 : [spec] "K" (0x81UL)
161 : "cc", "memory", "0");
Heiko Carstens457f2182014-03-21 10:42:25 +0100162 return size;
163}
164
165static inline unsigned long copy_to_user_mvcs(void __user *ptr, const void *x,
166 unsigned long size)
167{
168 unsigned long tmp1, tmp2;
169
Heiko Carstens457f2182014-03-21 10:42:25 +0100170 tmp1 = -256UL;
171 asm volatile(
172 " sacf 0\n"
173 "0: mvcs 0(%0,%1),0(%2),%3\n"
174 "7: jz 5f\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100175 "1: algr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100176 " la %1,256(%1)\n"
177 " la %2,256(%2)\n"
178 "2: mvcs 0(%0,%1),0(%2),%3\n"
179 "8: jnz 1b\n"
180 " j 5f\n"
181 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100182 " lghi %3,-4096\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100183 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100184 " slgr %4,%1\n"
185 " clgr %0,%4\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100186 " jnh 6f\n"
187 "4: mvcs 0(%4,%1),0(%2),%3\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100188 "9: slgr %0,%4\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100189 " j 6f\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100190 "5: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100191 "6: sacf 768\n"
192 EX_TABLE(0b,3b) EX_TABLE(2b,3b) EX_TABLE(4b,6b)
193 EX_TABLE(7b,3b) EX_TABLE(8b,3b) EX_TABLE(9b,6b)
194 : "+a" (size), "+a" (ptr), "+a" (x), "+a" (tmp1), "=a" (tmp2)
195 : : "cc", "memory");
196 return size;
197}
198
Al Viro37096002017-03-28 15:06:24 -0400199unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
Heiko Carstens457f2182014-03-21 10:42:25 +0100200{
Martin Schwidefskyd66bf802017-08-21 14:47:04 +0200201 if (copy_with_mvcos())
Heiko Carstens457f2182014-03-21 10:42:25 +0100202 return copy_to_user_mvcos(to, from, n);
203 return copy_to_user_mvcs(to, from, n);
204}
Al Viro37096002017-03-28 15:06:24 -0400205EXPORT_SYMBOL(raw_copy_to_user);
Heiko Carstens457f2182014-03-21 10:42:25 +0100206
Heiko Carstens457f2182014-03-21 10:42:25 +0100207static inline unsigned long clear_user_mvcos(void __user *to, unsigned long size)
208{
Heiko Carstens457f2182014-03-21 10:42:25 +0100209 unsigned long tmp1, tmp2;
210
211 tmp1 = -4096UL;
212 asm volatile(
Heiko Carstensd1e18ef2021-06-21 13:38:45 +0200213 " llilh 0,%[spec]\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100214 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
215 " jz 4f\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100216 "1: algr %0,%2\n"
217 " slgr %1,%2\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100218 " j 0b\n"
219 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
220 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100221 " slgr %3,%1\n"
222 " clgr %0,%3\n" /* copy crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100223 " jnh 5f\n"
224 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100225 " slgr %0,%3\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100226 " j 5f\n"
Heiko Carstensb8ac5e22016-06-10 09:35:51 +0200227 "4: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100228 "5:\n"
229 EX_TABLE(0b,2b) EX_TABLE(3b,5b)
230 : "+a" (size), "+a" (to), "+a" (tmp1), "=a" (tmp2)
Heiko Carstensd1e18ef2021-06-21 13:38:45 +0200231 : "a" (empty_zero_page), [spec] "K" (0x81UL)
232 : "cc", "memory", "0");
Heiko Carstens457f2182014-03-21 10:42:25 +0100233 return size;
234}
235
236static inline unsigned long clear_user_xc(void __user *to, unsigned long size)
237{
238 unsigned long tmp1, tmp2;
239
Heiko Carstens457f2182014-03-21 10:42:25 +0100240 asm volatile(
241 " sacf 256\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100242 " aghi %0,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100243 " jo 5f\n"
244 " bras %3,3f\n"
245 " xc 0(1,%1),0(%1)\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100246 "0: aghi %0,257\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100247 " la %2,255(%1)\n" /* %2 = ptr + 255 */
248 " srl %2,12\n"
249 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100250 " slgr %2,%1\n"
251 " clgr %0,%2\n" /* clear crosses next page boundary? */
Heiko Carstens457f2182014-03-21 10:42:25 +0100252 " jnh 5f\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100253 " aghi %2,-1\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100254 "1: ex %2,0(%3)\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100255 " aghi %2,1\n"
256 " slgr %0,%2\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100257 " j 5f\n"
258 "2: xc 0(256,%1),0(%1)\n"
259 " la %1,256(%1)\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100260 "3: aghi %0,-256\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100261 " jnm 2b\n"
262 "4: ex %0,0(%3)\n"
Heiko Carstens5a79859a2015-02-12 13:08:27 +0100263 "5: slgr %0,%0\n"
Heiko Carstens457f2182014-03-21 10:42:25 +0100264 "6: sacf 768\n"
265 EX_TABLE(1b,6b) EX_TABLE(2b,0b) EX_TABLE(4b,0b)
266 : "+a" (size), "+a" (to), "=a" (tmp1), "=a" (tmp2)
267 : : "cc", "memory");
268 return size;
269}
270
271unsigned long __clear_user(void __user *to, unsigned long size)
272{
Martin Schwidefskyd66bf802017-08-21 14:47:04 +0200273 if (copy_with_mvcos())
Heiko Carstens457f2182014-03-21 10:42:25 +0100274 return clear_user_mvcos(to, size);
275 return clear_user_xc(to, size);
276}
277EXPORT_SYMBOL(__clear_user);