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
;
69 old_fs
= current
->thread
.mm_segment
;
72 /* protect against a concurrent page table upgrade */
73 local_irq_save(flags
);
74 current
->thread
.mm_segment
|= 1;
75 asce
= S390_lowcore
.kernel_asce
;
76 if (likely(old_fs
== USER_DS
)) {
77 __ctl_store(cr
, 1, 1);
78 if (cr
!= S390_lowcore
.kernel_asce
) {
79 __ctl_load(S390_lowcore
.kernel_asce
, 1, 1);
80 set_cpu_flag(CIF_ASCE_PRIMARY
);
82 asce
= S390_lowcore
.user_asce
;
84 __ctl_store(cr
, 7, 7);
86 __ctl_load(asce
, 7, 7);
87 set_cpu_flag(CIF_ASCE_SECONDARY
);
89 local_irq_restore(flags
);
92 EXPORT_SYMBOL(enable_sacf_uaccess
);
94 void disable_sacf_uaccess(mm_segment_t old_fs
)
96 current
->thread
.mm_segment
= old_fs
;
97 if (old_fs
== USER_DS
&& test_facility(27)) {
98 __ctl_load(S390_lowcore
.user_asce
, 1, 1);
99 clear_cpu_flag(CIF_ASCE_PRIMARY
);
102 EXPORT_SYMBOL(disable_sacf_uaccess
);
104 static inline unsigned long copy_from_user_mvcos(void *x
, const void __user
*ptr
,
107 register unsigned long reg0
asm("0") = 0x01UL
;
108 unsigned long tmp1
, tmp2
;
112 "0: .insn ss,0xc80000000000,0(%0,%2),0(%1),0\n"
118 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
119 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
121 " clgr %0,%4\n" /* copy crosses next page boundary? */
123 "3: .insn ss,0xc80000000000,0(%4,%2),0(%1),0\n"
128 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
) EX_TABLE(6b
,2b
) EX_TABLE(7b
,5b
)
129 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
130 : "d" (reg0
) : "cc", "memory");
134 static inline unsigned long copy_from_user_mvcp(void *x
, const void __user
*ptr
,
137 unsigned long tmp1
, tmp2
;
140 old_fs
= enable_sacf_uaccess();
144 "0: mvcp 0(%0,%2),0(%1),%3\n"
149 "2: mvcp 0(%0,%2),0(%1),%3\n"
152 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
154 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
156 " clgr %0,%4\n" /* copy crosses next page boundary? */
158 "4: mvcp 0(%4,%2),0(%1),%3\n"
163 EX_TABLE(0b
,3b
) EX_TABLE(2b
,3b
) EX_TABLE(4b
,6b
)
164 EX_TABLE(7b
,3b
) EX_TABLE(8b
,3b
) EX_TABLE(9b
,6b
)
165 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
167 disable_sacf_uaccess(old_fs
);
171 unsigned long raw_copy_from_user(void *to
, const void __user
*from
, unsigned long n
)
173 if (copy_with_mvcos())
174 return copy_from_user_mvcos(to
, from
, n
);
175 return copy_from_user_mvcp(to
, from
, n
);
177 EXPORT_SYMBOL(raw_copy_from_user
);
179 static inline unsigned long copy_to_user_mvcos(void __user
*ptr
, const void *x
,
182 register unsigned long reg0
asm("0") = 0x010000UL
;
183 unsigned long tmp1
, tmp2
;
187 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
193 "2: la %4,4095(%1)\n"/* %4 = ptr + 4095 */
194 " nr %4,%3\n" /* %4 = (ptr + 4095) & -4096 */
196 " clgr %0,%4\n" /* copy crosses next page boundary? */
198 "3: .insn ss,0xc80000000000,0(%4,%1),0(%2),0\n"
203 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
) EX_TABLE(6b
,2b
) EX_TABLE(7b
,5b
)
204 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
205 : "d" (reg0
) : "cc", "memory");
209 static inline unsigned long copy_to_user_mvcs(void __user
*ptr
, const void *x
,
212 unsigned long tmp1
, tmp2
;
215 old_fs
= enable_sacf_uaccess();
219 "0: mvcs 0(%0,%1),0(%2),%3\n"
224 "2: mvcs 0(%0,%1),0(%2),%3\n"
227 "3: la %4,255(%1)\n" /* %4 = ptr + 255 */
229 " nr %4,%3\n" /* %4 = (ptr + 255) & -4096 */
231 " clgr %0,%4\n" /* copy crosses next page boundary? */
233 "4: mvcs 0(%4,%1),0(%2),%3\n"
238 EX_TABLE(0b
,3b
) EX_TABLE(2b
,3b
) EX_TABLE(4b
,6b
)
239 EX_TABLE(7b
,3b
) EX_TABLE(8b
,3b
) EX_TABLE(9b
,6b
)
240 : "+a" (size
), "+a" (ptr
), "+a" (x
), "+a" (tmp1
), "=a" (tmp2
)
242 disable_sacf_uaccess(old_fs
);
246 unsigned long raw_copy_to_user(void __user
*to
, const void *from
, unsigned long n
)
248 if (copy_with_mvcos())
249 return copy_to_user_mvcos(to
, from
, n
);
250 return copy_to_user_mvcs(to
, from
, n
);
252 EXPORT_SYMBOL(raw_copy_to_user
);
254 static inline unsigned long copy_in_user_mvcos(void __user
*to
, const void __user
*from
,
257 register unsigned long reg0
asm("0") = 0x010001UL
;
258 unsigned long tmp1
, tmp2
;
261 /* FIXME: copy with reduced length. */
263 "0: .insn ss,0xc80000000000,0(%0,%1),0(%2),0\n"
272 : "+a" (size
), "+a" (to
), "+a" (from
), "+a" (tmp1
), "=a" (tmp2
)
273 : "d" (reg0
) : "cc", "memory");
277 static inline unsigned long copy_in_user_mvc(void __user
*to
, const void __user
*from
,
283 old_fs
= enable_sacf_uaccess();
290 "1: mvc 0(1,%1),0(%2)\n"
296 "2: mvc 0(256,%1),0(%2)\n"
301 "4: ex %0,1b-0b(%3)\n"
304 EX_TABLE(1b
,6b
) EX_TABLE(2b
,0b
) EX_TABLE(4b
,0b
)
305 : "+a" (size
), "+a" (to
), "+a" (from
), "=a" (tmp1
)
307 disable_sacf_uaccess(old_fs
);
311 unsigned long raw_copy_in_user(void __user
*to
, const void __user
*from
, unsigned long n
)
313 if (copy_with_mvcos())
314 return copy_in_user_mvcos(to
, from
, n
);
315 return copy_in_user_mvc(to
, from
, n
);
317 EXPORT_SYMBOL(raw_copy_in_user
);
319 static inline unsigned long clear_user_mvcos(void __user
*to
, unsigned long size
)
321 register unsigned long reg0
asm("0") = 0x010000UL
;
322 unsigned long tmp1
, tmp2
;
326 "0: .insn ss,0xc80000000000,0(%0,%1),0(%4),0\n"
331 "2: la %3,4095(%1)\n"/* %4 = to + 4095 */
332 " nr %3,%2\n" /* %4 = (to + 4095) & -4096 */
334 " clgr %0,%3\n" /* copy crosses next page boundary? */
336 "3: .insn ss,0xc80000000000,0(%3,%1),0(%4),0\n"
341 EX_TABLE(0b
,2b
) EX_TABLE(3b
,5b
)
342 : "+a" (size
), "+a" (to
), "+a" (tmp1
), "=a" (tmp2
)
343 : "a" (empty_zero_page
), "d" (reg0
) : "cc", "memory");
347 static inline unsigned long clear_user_xc(void __user
*to
, unsigned long size
)
350 unsigned long tmp1
, tmp2
;
352 old_fs
= enable_sacf_uaccess();
358 " xc 0(1,%1),0(%1)\n"
360 " la %2,255(%1)\n" /* %2 = ptr + 255 */
362 " sll %2,12\n" /* %2 = (ptr + 255) & -4096 */
364 " clgr %0,%2\n" /* clear crosses next page boundary? */
371 "2: xc 0(256,%1),0(%1)\n"
378 EX_TABLE(1b
,6b
) EX_TABLE(2b
,0b
) EX_TABLE(4b
,0b
)
379 : "+a" (size
), "+a" (to
), "=a" (tmp1
), "=a" (tmp2
)
381 disable_sacf_uaccess(old_fs
);
385 unsigned long __clear_user(void __user
*to
, unsigned long size
)
387 if (copy_with_mvcos())
388 return clear_user_mvcos(to
, size
);
389 return clear_user_xc(to
, size
);
391 EXPORT_SYMBOL(__clear_user
);
393 static inline unsigned long strnlen_user_srst(const char __user
*src
,
396 register unsigned long reg0
asm("0") = 0;
397 unsigned long tmp1
, tmp2
;
406 " la %0,1(%3)\n" /* strnlen_user results includes \0 */
410 : "+a" (size
), "+a" (src
), "=a" (tmp1
), "=a" (tmp2
)
411 : "d" (reg0
) : "cc", "memory");
415 unsigned long __strnlen_user(const char __user
*src
, unsigned long size
)
422 old_fs
= enable_sacf_uaccess();
423 len
= strnlen_user_srst(src
, size
);
424 disable_sacf_uaccess(old_fs
);
427 EXPORT_SYMBOL(__strnlen_user
);
429 long __strncpy_from_user(char *dst
, const char __user
*src
, long size
)
431 size_t done
, len
, offset
, len_str
;
433 if (unlikely(size
<= 0))
437 offset
= (size_t)src
& (L1_CACHE_BYTES
- 1);
438 len
= min(size
- done
, L1_CACHE_BYTES
- offset
);
439 if (copy_from_user(dst
, src
, len
))
441 len_str
= strnlen(dst
, len
);
445 } while ((len_str
== len
) && (done
< size
));
448 EXPORT_SYMBOL(__strncpy_from_user
);