1 // SPDX-License-Identifier: GPL-2.0 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 11 #include <linux/uaccess.h> 12 #include <linux/export.h> 13 #include <linux/mm.h> 14 #include <asm/asm-extable.h> 15 #include <asm/ctlreg.h> 16 17 #ifdef CONFIG_DEBUG_ENTRY 18 void debug_user_asce(int exit) 19 { 20 struct ctlreg cr1, cr7; 21 22 local_ctl_store(1, &cr1); 23 local_ctl_store(7, &cr7); 24 if (cr1.val == get_lowcore()->kernel_asce.val && cr7.val == get_lowcore()->user_asce.val) 25 return; 26 panic("incorrect ASCE on kernel %s\n" 27 "cr1: %016lx cr7: %016lx\n" 28 "kernel: %016lx user: %016lx\n", 29 exit ? "exit" : "entry", cr1.val, cr7.val, 30 get_lowcore()->kernel_asce.val, get_lowcore()->user_asce.val); 31 } 32 #endif /*CONFIG_DEBUG_ENTRY */ 33 34 unsigned long _copy_from_user_key(void *to, const void __user *from, 35 unsigned long n, unsigned long key) 36 { 37 unsigned long res = n; 38 39 might_fault(); 40 if (!should_fail_usercopy()) { 41 instrument_copy_from_user_before(to, from, n); 42 res = raw_copy_from_user_key(to, from, n, key); 43 instrument_copy_from_user_after(to, from, n, res); 44 } 45 if (unlikely(res)) 46 memset(to + (n - res), 0, res); 47 return res; 48 } 49 EXPORT_SYMBOL(_copy_from_user_key); 50 51 unsigned long _copy_to_user_key(void __user *to, const void *from, 52 unsigned long n, unsigned long key) 53 { 54 might_fault(); 55 if (should_fail_usercopy()) 56 return n; 57 instrument_copy_to_user(to, from, n); 58 return raw_copy_to_user_key(to, from, n, key); 59 } 60 EXPORT_SYMBOL(_copy_to_user_key); 61 62 unsigned long __clear_user(void __user *to, unsigned long size) 63 { 64 unsigned long rem; 65 union oac spec = { 66 .oac1.as = PSW_BITS_AS_SECONDARY, 67 .oac1.a = 1, 68 }; 69 70 asm volatile( 71 " lr 0,%[spec]\n" 72 "0: mvcos 0(%[to]),0(%[zeropg]),%[size]\n" 73 "1: jz 5f\n" 74 " algr %[size],%[val]\n" 75 " slgr %[to],%[val]\n" 76 " j 0b\n" 77 "2: la %[rem],4095(%[to])\n" /* rem = to + 4095 */ 78 " nr %[rem],%[val]\n" /* rem = (to + 4095) & -4096 */ 79 " slgr %[rem],%[to]\n" 80 " clgr %[size],%[rem]\n" /* copy crosses next page boundary? */ 81 " jnh 6f\n" 82 "3: mvcos 0(%[to]),0(%[zeropg]),%[rem]\n" 83 "4: slgr %[size],%[rem]\n" 84 " j 6f\n" 85 "5: slgr %[size],%[size]\n" 86 "6:\n" 87 EX_TABLE(0b, 2b) 88 EX_TABLE(1b, 2b) 89 EX_TABLE(3b, 6b) 90 EX_TABLE(4b, 6b) 91 : [size] "+&a" (size), [to] "+&a" (to), [rem] "=&a" (rem) 92 : [val] "a" (-4096UL), [zeropg] "a" (empty_zero_page), [spec] "d" (spec.val) 93 : "cc", "memory", "0"); 94 return size; 95 } 96 EXPORT_SYMBOL(__clear_user); 97