1#include <linux/linkage.h> 2#include <linux/export.h> 3#include <asm/asm.h> 4#include <asm/asm-extable.h> 5#include <asm/csr.h> 6#include <asm/hwcap.h> 7#include <asm/alternative-macros.h> 8#include <asm/jump_label.h> 9 10 .macro fixup op reg addr lbl 11100: 12 \op \reg, \addr 13 _asm_extable 100b, \lbl 14 .endm 15 16SYM_FUNC_START(__asm_copy_to_user) 17#ifdef CONFIG_RISCV_ISA_V 18 ALTERNATIVE("j fallback_scalar_usercopy", "nop", 0, RISCV_ISA_EXT_ZVE32X, CONFIG_RISCV_ISA_V) 19 REG_L t0, riscv_v_usercopy_threshold 20 bltu a2, t0, fallback_scalar_usercopy 21 li a3, 1 22 tail enter_vector_usercopy 23#endif 24SYM_FUNC_END(__asm_copy_to_user) 25EXPORT_SYMBOL(__asm_copy_to_user) 26SYM_FUNC_ALIAS(__asm_copy_from_user, __asm_copy_to_user) 27EXPORT_SYMBOL(__asm_copy_from_user) 28 29SYM_FUNC_START(fallback_scalar_usercopy) 30 /* Enable access to user memory */ 31 li t6, SR_SUM 32 csrs CSR_STATUS, t6 33 mv t6, ra 34 35 call fallback_scalar_usercopy_sum_enabled 36 37 /* Disable access to user memory */ 38 mv ra, t6 39 li t6, SR_SUM 40 csrc CSR_STATUS, t6 41 ret 42SYM_FUNC_END(fallback_scalar_usercopy) 43 44SYM_FUNC_START(__asm_copy_to_user_sum_enabled) 45#ifdef CONFIG_RISCV_ISA_V 46 ALTERNATIVE("j fallback_scalar_usercopy_sum_enabled", "nop", 0, RISCV_ISA_EXT_ZVE32X, CONFIG_RISCV_ISA_V) 47 REG_L t0, riscv_v_usercopy_threshold 48 bltu a2, t0, fallback_scalar_usercopy_sum_enabled 49 li a3, 0 50 tail enter_vector_usercopy 51#endif 52SYM_FUNC_END(__asm_copy_to_user_sum_enabled) 53SYM_FUNC_ALIAS(__asm_copy_from_user_sum_enabled, __asm_copy_to_user_sum_enabled) 54EXPORT_SYMBOL(__asm_copy_from_user_sum_enabled) 55EXPORT_SYMBOL(__asm_copy_to_user_sum_enabled) 56 57SYM_FUNC_START(fallback_scalar_usercopy_sum_enabled) 58 /* 59 * Save the terminal address which will be used to compute the number 60 * of bytes copied in case of a fixup exception. 61 */ 62 add t5, a0, a2 63 64 /* 65 * Register allocation for code below: 66 * a0 - start of uncopied dst 67 * a1 - start of uncopied src 68 * a2 - size 69 * t0 - end of uncopied dst 70 */ 71 add t0, a0, a2 72 73 /* 74 * Use byte copy only if too small. 75 * SZREG holds 4 for RV32 and 8 for RV64 76 */ 77 li a3, 9*SZREG-1 /* size must >= (word_copy stride + SZREG-1) */ 78 bltu a2, a3, .Lbyte_copy_tail 79 80#if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 81#if defined(CONFIG_RISCV_PROBE_UNALIGNED_ACCESS) && defined(CONFIG_JUMP_LABEL) 82 ARCH_STATIC_BRANCH_JUMP_ASM(fast_unaligned_access_speed_key + 1, .Lalign_dst) 83#else 84 j .Lalign_dst 85#endif 86#endif 87.Lword_copy: 88 /* 89 * Both src and dst are aligned, unrolled word copy 90 * 91 * a0 - start of aligned dst 92 * a1 - start of aligned src 93 * t0 - end of aligned dst 94 */ 95 addi t0, t0, -(8*SZREG) /* not to over run */ 962: 97 fixup REG_L a4, 0(a1), 10f 98 fixup REG_L a5, SZREG(a1), 10f 99 fixup REG_L a6, 2*SZREG(a1), 10f 100 fixup REG_L a7, 3*SZREG(a1), 10f 101 fixup REG_L t1, 4*SZREG(a1), 10f 102 fixup REG_L t2, 5*SZREG(a1), 10f 103 fixup REG_L t3, 6*SZREG(a1), 10f 104 fixup REG_L t4, 7*SZREG(a1), 10f 105 fixup REG_S a4, 0(a0), 10f 106 fixup REG_S a5, SZREG(a0), 10f 107 fixup REG_S a6, 2*SZREG(a0), 10f 108 fixup REG_S a7, 3*SZREG(a0), 10f 109 fixup REG_S t1, 4*SZREG(a0), 10f 110 fixup REG_S t2, 5*SZREG(a0), 10f 111 fixup REG_S t3, 6*SZREG(a0), 10f 112 fixup REG_S t4, 7*SZREG(a0), 10f 113 addi a0, a0, 8*SZREG 114 addi a1, a1, 8*SZREG 115 bleu a0, t0, 2b 116 117 addi t0, t0, 8*SZREG /* revert to original value */ 118 j .Lbyte_copy_tail 119 120#if !defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS) 121.Lalign_dst: 122 /* 123 * Copy first bytes until dst is aligned to word boundary. 124 * a0 - start of dst 125 * t1 - start of aligned dst 126 */ 127 addi t1, a0, SZREG-1 128 andi t1, t1, ~(SZREG-1) 129 /* dst is already aligned, skip */ 130 beq a0, t1, .Lskip_align_dst 1311: 132 /* a5 - one byte for copying data */ 133 fixup lb a5, 0(a1), 10f 134 addi a1, a1, 1 /* src */ 135 fixup sb a5, 0(a0), 10f 136 addi a0, a0, 1 /* dst */ 137 bltu a0, t1, 1b /* t1 - start of aligned dst */ 138 139.Lskip_align_dst: 140 /* 141 * Now dst is aligned. 142 * Use shift-copy if src is misaligned. 143 * Use word-copy if both src and dst are aligned because 144 * can not use shift-copy which do not require shifting 145 */ 146 /* a1 - start of src */ 147 andi a3, a1, SZREG-1 148 beqz a3, .Lword_copy 149.Lshift_copy: 150 151 /* 152 * Word copy with shifting. 153 * For misaligned copy we still perform aligned word copy, but 154 * we need to use the value fetched from the previous iteration and 155 * do some shifts. 156 * This is safe because reading is less than a word size. 157 * 158 * a0 - start of aligned dst 159 * a1 - start of src 160 * a3 - a1 & mask:(SZREG-1) 161 * t0 - end of uncopied dst 162 * t1 - end of aligned dst 163 */ 164 /* calculating aligned word boundary for dst */ 165 andi t1, t0, ~(SZREG-1) 166 /* Converting unaligned src to aligned src */ 167 andi a1, a1, ~(SZREG-1) 168 169 /* 170 * Calculate shifts 171 * t3 - prev shift 172 * t4 - current shift 173 */ 174 slli t3, a3, 3 /* converting bytes in a3 to bits */ 175 li a5, SZREG*8 176 sub t4, a5, t3 177 178 /* Load the first word to combine with second word */ 179 fixup REG_L a5, 0(a1), 10f 180 1813: 182 /* Main shifting copy 183 * 184 * a0 - start of aligned dst 185 * a1 - start of aligned src 186 * t1 - end of aligned dst 187 */ 188 189 /* At least one iteration will be executed */ 190 srl a4, a5, t3 191 fixup REG_L a5, SZREG(a1), 10f 192 addi a1, a1, SZREG 193 sll a2, a5, t4 194 or a2, a2, a4 195 fixup REG_S a2, 0(a0), 10f 196 addi a0, a0, SZREG 197 bltu a0, t1, 3b 198 199 /* Revert src to original unaligned value */ 200 add a1, a1, a3 201#endif 202 203.Lbyte_copy_tail: 204 /* 205 * Byte copy anything left. 206 * 207 * a0 - start of remaining dst 208 * a1 - start of remaining src 209 * t0 - end of remaining dst 210 */ 211 bgeu a0, t0, .Lout_copy_user /* check if end of copy */ 2124: 213 fixup lb a5, 0(a1), 10f 214 addi a1, a1, 1 /* src */ 215 fixup sb a5, 0(a0), 10f 216 addi a0, a0, 1 /* dst */ 217 bltu a0, t0, 4b /* t0 - end of dst */ 218 219.Lout_copy_user: 220 li a0, 0 221 ret 22210: 223 sub a0, t5, a0 224 ret 225SYM_FUNC_END(fallback_scalar_usercopy_sum_enabled) 226 227SYM_FUNC_START(__clear_user) 228 229 /* Enable access to user memory */ 230 li t6, SR_SUM 231 csrs CSR_STATUS, t6 232 233 add a3, a0, a1 234 addi t0, a0, SZREG-1 235 andi t1, a3, ~(SZREG-1) 236 andi t0, t0, ~(SZREG-1) 237 /* 238 * a3: terminal address of target region 239 * t0: lowest doubleword-aligned address in target region 240 * t1: highest doubleword-aligned address in target region 241 */ 242 bgeu t0, t1, 2f 243 bltu a0, t0, 4f 2441: 245 fixup REG_S, zero, (a0), 11f 246 addi a0, a0, SZREG 247 bltu a0, t1, 1b 2482: 249 bltu a0, a3, 5f 250 2513: 252 /* Disable access to user memory */ 253 csrc CSR_STATUS, t6 254 li a0, 0 255 ret 2564: /* Edge case: unalignment */ 257 fixup sb, zero, (a0), 11f 258 addi a0, a0, 1 259 bltu a0, t0, 4b 260 j 1b 2615: /* Edge case: remainder */ 262 fixup sb, zero, (a0), 11f 263 addi a0, a0, 1 264 bltu a0, a3, 5b 265 j 3b 266 267 /* Exception fixup code */ 26811: 269 /* Disable access to user memory */ 270 csrc CSR_STATUS, t6 271 sub a0, a3, a0 272 ret 273SYM_FUNC_END(__clear_user) 274EXPORT_SYMBOL(__clear_user) 275