1 // SPDX-License-Identifier: GPL-2.0
3 * Standard user space access functions based on mvcp/mvcs and doing
4 * interesting things in the secondary space mode.
6 * Copyright IBM Corp. 2006,2014
7 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
8 * Gerald Schaefer (gerald.schaefer@de.ibm.com)
11 #include <linux/jump_label.h>
12 #include <linux/uaccess.h>
13 #include <linux/export.h>
14 #include <linux/errno.h>
16 #include <asm/mmu_context.h>
17 #include <asm/facility.h>
19 #ifndef CONFIG_HAVE_MARCH_Z10_FEATURES
20 static DEFINE_STATIC_KEY_FALSE(have_mvcos
);
22 static int __init
uaccess_init(void)
24 if (test_facility(27))
25 static_branch_enable(&have_mvcos
);
28 early_initcall(uaccess_init
);
30 static inline int copy_with_mvcos(void)
32 if (static_branch_likely(&have_mvcos
))
37 static inline int copy_with_mvcos(void)
43 void set_fs(mm_segment_t fs
)
45 current
->thread
.mm_segment
= fs
;
47 __ctl_load(S390_lowcore
.user_asce
, 1, 1);
48 clear_cpu_flag(CIF_ASCE_PRIMARY
);
50 __ctl_load(S390_lowcore
.kernel_asce
, 1, 1);
51 set_cpu_flag(CIF_ASCE_PRIMARY
);
54 if (fs
== USER_DS_SACF
)
55 __ctl_load(S390_lowcore
.user_asce
, 7, 7);
57 __ctl_load(S390_lowcore
.kernel_asce
, 7, 7);
58 set_cpu_flag(CIF_ASCE_SECONDARY
);
61 EXPORT_SYMBOL(set_fs
);
63 mm_segment_t
enable_sacf_uaccess(void)
66 unsigned long asce
, cr
;
68 old_fs
= current
->thread
.mm_segment
;
71 current
->thread
.mm_segment
|= 1;
72 asce
= S390_lowcore
.kernel_asce
;
73 if (likely(old_fs
== USER_DS
)) {
74 __ctl_store(cr
, 1, 1);
75 if (cr
!= S390_lowcore
.kernel_asce
) {
76 __ctl_load(S390_lowcore
.kernel_asce
, 1, 1);
77 set_cpu_flag(CIF_ASCE_PRIMARY
);
79 asce
= S390_lowcore
.user_asce
;
81 __ctl_store(cr
, 7, 7);
83 __ctl_load(asce
, 7, 7);
84 set_cpu_flag(CIF_ASCE_SECONDARY
);
88 EXPORT_SYMBOL(enable_sacf_uaccess
);
90 void disable_sacf_uaccess(mm_segment_t old_fs
)
92 current
->thread
.mm_segment
= old_fs
;
93 if (old_fs
== USER_DS
&& test_facility(27)) {
94 __ctl_load(S390_lowcore
.user_asce
, 1, 1);
95 clear_cpu_flag(CIF_ASCE_PRIMARY
);
98 EXPORT_SYMBOL(disable_sacf_uaccess
);
100 static inline unsigned long copy_from_user_mvcos(void *x
, const void __user
*ptr
,
103 register unsigned long reg0
asm("0") = 0x01UL
;
104 unsigned long tmp1
, tmp2
;
108 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
114 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
115 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
117 " clgr %0,%4\n" /* copy crosses next page boundary? */
119 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
124 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
) EX_TABLE(6b
,2b
) EX_TABLE(7b
,5b
)
125 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
126 : "d" (reg0
) : "cc", "memory");
130 static inline unsigned long copy_from_user_mvcp(void *x
, const void __user
*ptr
,
133 unsigned long tmp1
, tmp2
;
136 old_fs
= enable_sacf_uaccess();
140 "0: mvcp 0(%0,%2),0(%1),%3\n"
145 "2: mvcp 0(%0,%2),0(%1),%3\n"
148 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
150 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
152 " clgr %0,%4\n" /* copy crosses next page boundary? */
154 "4: mvcp 0(%4,%2),0(%1),%3\n"
159 EX_TABLE(0b
,3b
) EX_TABLE(2b
,3b
) EX_TABLE(4b
,6b
)
160 EX_TABLE(7b
,3b
) EX_TABLE(8b
,3b
) EX_TABLE(9b
,6b
)
161 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
163 disable_sacf_uaccess(old_fs
);
167 unsigned long raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
169 if (copy_with_mvcos())
170 return copy_from_user_mvcos(to
, from
, n
);
171 return copy_from_user_mvcp(to
, from
, n
);
173 EXPORT_SYMBOL(raw_copy_from_user
);
175 static inline unsigned long copy_to_user_mvcos(void __user
*ptr
, const void *x
,
178 register unsigned long reg0
asm("0") = 0x010000UL
;
179 unsigned long tmp1
, tmp2
;
183 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
189 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
190 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
192 " clgr %0,%4\n" /* copy crosses next page boundary? */
194 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
199 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
) EX_TABLE(6b
,2b
) EX_TABLE(7b
,5b
)
200 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
201 : "d" (reg0
) : "cc", "memory");
205 static inline unsigned long copy_to_user_mvcs(void __user
*ptr
, const void *x
,
208 unsigned long tmp1
, tmp2
;
211 old_fs
= enable_sacf_uaccess();
215 "0: mvcs 0(%0,%1),0(%2),%3\n"
220 "2: mvcs 0(%0,%1),0(%2),%3\n"
223 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
225 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
227 " clgr %0,%4\n" /* copy crosses next page boundary? */
229 "4: mvcs 0(%4,%1),0(%2),%3\n"
234 EX_TABLE(0b
,3b
) EX_TABLE(2b
,3b
) EX_TABLE(4b
,6b
)
235 EX_TABLE(7b
,3b
) EX_TABLE(8b
,3b
) EX_TABLE(9b
,6b
)
236 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
238 disable_sacf_uaccess(old_fs
);
242 unsigned long raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
244 if (copy_with_mvcos())
245 return copy_to_user_mvcos(to
, from
, n
);
246 return copy_to_user_mvcs(to
, from
, n
);
248 EXPORT_SYMBOL(raw_copy_to_user
);
250 static inline unsigned long copy_in_user_mvcos(void __user
*to
, const void __user
*from
,
253 register unsigned long reg0
asm("0") = 0x010001UL
;
254 unsigned long tmp1
, tmp2
;
257 /* FIXME: copy with reduced length. */
259 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
268 : "+a" (size
), "+a" (to
), "+a" (from
), "+a" (tmp1
), "=a" (tmp2
)
269 : "d" (reg0
) : "cc", "memory");
273 static inline unsigned long copy_in_user_mvc(void __user
*to
, const void __user
*from
,
279 old_fs
= enable_sacf_uaccess();
286 "1: mvc 0(1,%1),0(%2)\n"
292 "2: mvc 0(256,%1),0(%2)\n"
297 "4: ex %0,1b-0b(%3)\n"
300 EX_TABLE(1b
,6b
) EX_TABLE(2b
,0b
) EX_TABLE(4b
,0b
)
301 : "+a" (size
), "+a" (to
), "+a" (from
), "=a" (tmp1
)
303 disable_sacf_uaccess(old_fs
);
307 unsigned long raw_copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
309 if (copy_with_mvcos())
310 return copy_in_user_mvcos(to
, from
, n
);
311 return copy_in_user_mvc(to
, from
, n
);
313 EXPORT_SYMBOL(raw_copy_in_user
);
315 static inline unsigned long clear_user_mvcos(void __user
*to
, unsigned long size
)
317 register unsigned long reg0
asm("0") = 0x010000UL
;
318 unsigned long tmp1
, tmp2
;
322 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
327 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
328 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
330 " clgr %0,%3\n" /* copy crosses next page boundary? */
332 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
337 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
)
338 : "+a" (size
), "+a" (to
), "+a" (tmp1
), "=a" (tmp2
)
339 : "a" (empty_zero_page
), "d" (reg0
) : "cc", "memory");
343 static inline unsigned long clear_user_xc(void __user
*to
, unsigned long size
)
346 unsigned long tmp1
, tmp2
;
348 old_fs
= enable_sacf_uaccess();
354 " xc 0(1,%1),0(%1)\n"
356 " la %2,255(%1)\n" /* %2 = ptr + 255 */
358 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
360 " clgr %0,%2\n" /* clear crosses next page boundary? */
367 "2: xc 0(256,%1),0(%1)\n"
374 EX_TABLE(1b
,6b
) EX_TABLE(2b
,0b
) EX_TABLE(4b
,0b
)
375 : "+a" (size
), "+a" (to
), "=a" (tmp1
), "=a" (tmp2
)
377 disable_sacf_uaccess(old_fs
);
381 unsigned long __clear_user(void __user
*to
, unsigned long size
)
383 if (copy_with_mvcos())
384 return clear_user_mvcos(to
, size
);
385 return clear_user_xc(to
, size
);
387 EXPORT_SYMBOL(__clear_user
);
389 static inline unsigned long strnlen_user_srst(const char __user
*src
,
392 register unsigned long reg0
asm("0") = 0;
393 unsigned long tmp1
, tmp2
;
402 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
406 : "+a" (size
), "+a" (src
), "=a" (tmp1
), "=a" (tmp2
)
407 : "d" (reg0
) : "cc", "memory");
411 unsigned long __strnlen_user(const char __user
*src
, unsigned long size
)
418 old_fs
= enable_sacf_uaccess();
419 len
= strnlen_user_srst(src
, size
);
420 disable_sacf_uaccess(old_fs
);
423 EXPORT_SYMBOL(__strnlen_user
);
425 long __strncpy_from_user(char *dst
, const char __user
*src
, long size
)
427 size_t done
, len
, offset
, len_str
;
429 if (unlikely(size
<= 0))
433 offset
= (size_t)src
& (L1_CACHE_BYTES
- 1);
434 len
= min(size
- done
, L1_CACHE_BYTES
- offset
);
435 if (copy_from_user(dst
, src
, len
))
437 len_str
= strnlen(dst
, len
);
441 } while ((len_str
== len
) && (done
< size
));
444 EXPORT_SYMBOL(__strncpy_from_user
);