Merge tag 'libnvdimm-for-6.6' of git://git.kernel.org/pub/scm/linux/kernel/git/nvdimm...
[linux-block.git] / arch / s390 / lib / uaccess.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
457f2180
HC
2/*
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
457f2180
HC
11#include <linux/uaccess.h>
12#include <linux/export.h>
457f2180 13#include <linux/mm.h>
d09a307f 14#include <asm/asm-extable.h>
457f2180 15
56e62a73
SS
16#ifdef CONFIG_DEBUG_ENTRY
17void debug_user_asce(int exit)
062e5279
HC
18{
19 unsigned long cr1, cr7;
20
21 __ctl_store(cr1, 1, 1);
22 __ctl_store(cr7, 7, 7);
23 if (cr1 == S390_lowcore.kernel_asce && cr7 == S390_lowcore.user_asce)
24 return;
56e62a73 25 panic("incorrect ASCE on kernel %s\n"
062e5279
HC
26 "cr1: %016lx cr7: %016lx\n"
27 "kernel: %016llx user: %016llx\n",
56e62a73
SS
28 exit ? "exit" : "entry", cr1, cr7,
29 S390_lowcore.kernel_asce, S390_lowcore.user_asce);
062e5279 30}
56e62a73 31#endif /*CONFIG_DEBUG_ENTRY */
062e5279 32
4efd417f
VG
33static unsigned long raw_copy_from_user_key(void *to, const void __user *from,
34 unsigned long size, unsigned long key)
457f2180 35{
c3bd8343 36 unsigned long rem;
012a224e 37 union oac spec = {
432b1cc7 38 .oac2.key = key,
012a224e 39 .oac2.as = PSW_BITS_AS_SECONDARY,
432b1cc7 40 .oac2.k = 1,
012a224e
NB
41 .oac2.a = 1,
42 };
457f2180 43
457f2180 44 asm volatile(
10679e4d
HC
45 " lr 0,%[spec]\n"
46 "0: mvcos 0(%[to]),0(%[from]),%[size]\n"
4e0b0ad4 47 "1: jz 5f\n"
7f65d183
HC
48 " algr %[size],%[val]\n"
49 " slgr %[from],%[val]\n"
50 " slgr %[to],%[val]\n"
10679e4d 51 " j 0b\n"
7f65d183
HC
52 "2: la %[rem],4095(%[from])\n" /* rem = from + 4095 */
53 " nr %[rem],%[val]\n" /* rem = (from + 4095) & -4096 */
54 " slgr %[rem],%[from]\n"
55 " clgr %[size],%[rem]\n" /* copy crosses next page boundary? */
4e0b0ad4 56 " jnh 6f\n"
7f65d183
HC
57 "3: mvcos 0(%[to]),0(%[from]),%[rem]\n"
58 "4: slgr %[size],%[rem]\n"
4e0b0ad4
HC
59 " j 6f\n"
60 "5: slgr %[size],%[size]\n"
61 "6:\n"
62 EX_TABLE(0b, 2b)
4e0b0ad4 63 EX_TABLE(1b, 2b)
afdcc2ce 64 EX_TABLE(3b, 6b)
4e0b0ad4 65 EX_TABLE(4b, 6b)
c3bd8343
HC
66 : [size] "+&a" (size), [from] "+&a" (from), [to] "+&a" (to), [rem] "=&a" (rem)
67 : [val] "a" (-4096UL), [spec] "d" (spec.val)
d1e18efa 68 : "cc", "memory", "0");
457f2180
HC
69 return size;
70}
71
432b1cc7
JSG
72unsigned long raw_copy_from_user(void *to, const void __user *from, unsigned long n)
73{
74 return raw_copy_from_user_key(to, from, n, 0);
457f2180 75}
37096003 76EXPORT_SYMBOL(raw_copy_from_user);
457f2180 77
432b1cc7
JSG
78unsigned long _copy_from_user_key(void *to, const void __user *from,
79 unsigned long n, unsigned long key)
80{
81 unsigned long res = n;
82
83 might_fault();
84 if (!should_fail_usercopy()) {
33b75c1d 85 instrument_copy_from_user_before(to, from, n);
432b1cc7 86 res = raw_copy_from_user_key(to, from, n, key);
33b75c1d 87 instrument_copy_from_user_after(to, from, n, res);
432b1cc7
JSG
88 }
89 if (unlikely(res))
90 memset(to + (n - res), 0, res);
91 return res;
92}
93EXPORT_SYMBOL(_copy_from_user_key);
94
4efd417f
VG
95static unsigned long raw_copy_to_user_key(void __user *to, const void *from,
96 unsigned long size, unsigned long key)
457f2180 97{
c3bd8343 98 unsigned long rem;
012a224e 99 union oac spec = {
432b1cc7 100 .oac1.key = key,
012a224e 101 .oac1.as = PSW_BITS_AS_SECONDARY,
432b1cc7 102 .oac1.k = 1,
012a224e
NB
103 .oac1.a = 1,
104 };
457f2180 105
457f2180 106 asm volatile(
10679e4d
HC
107 " lr 0,%[spec]\n"
108 "0: mvcos 0(%[to]),0(%[from]),%[size]\n"
4e0b0ad4 109 "1: jz 5f\n"
7f65d183
HC
110 " algr %[size],%[val]\n"
111 " slgr %[to],%[val]\n"
112 " slgr %[from],%[val]\n"
10679e4d 113 " j 0b\n"
7f65d183
HC
114 "2: la %[rem],4095(%[to])\n" /* rem = to + 4095 */
115 " nr %[rem],%[val]\n" /* rem = (to + 4095) & -4096 */
116 " slgr %[rem],%[to]\n"
117 " clgr %[size],%[rem]\n" /* copy crosses next page boundary? */
4e0b0ad4 118 " jnh 6f\n"
7f65d183
HC
119 "3: mvcos 0(%[to]),0(%[from]),%[rem]\n"
120 "4: slgr %[size],%[rem]\n"
4e0b0ad4
HC
121 " j 6f\n"
122 "5: slgr %[size],%[size]\n"
123 "6:\n"
124 EX_TABLE(0b, 2b)
4e0b0ad4 125 EX_TABLE(1b, 2b)
afdcc2ce 126 EX_TABLE(3b, 6b)
4e0b0ad4 127 EX_TABLE(4b, 6b)
c3bd8343
HC
128 : [size] "+&a" (size), [to] "+&a" (to), [from] "+&a" (from), [rem] "=&a" (rem)
129 : [val] "a" (-4096UL), [spec] "d" (spec.val)
d1e18efa 130 : "cc", "memory", "0");
457f2180
HC
131 return size;
132}
133
432b1cc7
JSG
134unsigned long raw_copy_to_user(void __user *to, const void *from, unsigned long n)
135{
136 return raw_copy_to_user_key(to, from, n, 0);
457f2180 137}
37096003 138EXPORT_SYMBOL(raw_copy_to_user);
457f2180 139
432b1cc7
JSG
140unsigned long _copy_to_user_key(void __user *to, const void *from,
141 unsigned long n, unsigned long key)
142{
143 might_fault();
144 if (should_fail_usercopy())
145 return n;
146 instrument_copy_to_user(to, from, n);
147 return raw_copy_to_user_key(to, from, n, key);
148}
149EXPORT_SYMBOL(_copy_to_user_key);
150
4efd417f 151unsigned long __clear_user(void __user *to, unsigned long size)
457f2180 152{
c3bd8343 153 unsigned long rem;
012a224e
NB
154 union oac spec = {
155 .oac1.as = PSW_BITS_AS_SECONDARY,
156 .oac1.a = 1,
157 };
457f2180 158
457f2180 159 asm volatile(
10679e4d
HC
160 " lr 0,%[spec]\n"
161 "0: mvcos 0(%[to]),0(%[zeropg]),%[size]\n"
4e0b0ad4 162 "1: jz 5f\n"
7f65d183
HC
163 " algr %[size],%[val]\n"
164 " slgr %[to],%[val]\n"
10679e4d 165 " j 0b\n"
7f65d183
HC
166 "2: la %[rem],4095(%[to])\n" /* rem = to + 4095 */
167 " nr %[rem],%[val]\n" /* rem = (to + 4095) & -4096 */
168 " slgr %[rem],%[to]\n"
169 " clgr %[size],%[rem]\n" /* copy crosses next page boundary? */
4e0b0ad4 170 " jnh 6f\n"
7f65d183
HC
171 "3: mvcos 0(%[to]),0(%[zeropg]),%[rem]\n"
172 "4: slgr %[size],%[rem]\n"
4e0b0ad4
HC
173 " j 6f\n"
174 "5: slgr %[size],%[size]\n"
175 "6:\n"
176 EX_TABLE(0b, 2b)
177 EX_TABLE(1b, 2b)
178 EX_TABLE(3b, 6b)
179 EX_TABLE(4b, 6b)
c3bd8343
HC
180 : [size] "+&a" (size), [to] "+&a" (to), [rem] "=&a" (rem)
181 : [val] "a" (-4096UL), [zeropg] "a" (empty_zero_page), [spec] "d" (spec.val)
d1e18efa 182 : "cc", "memory", "0");
457f2180
HC
183 return size;
184}
457f2180 185EXPORT_SYMBOL(__clear_user);