Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 | // SPDX-License-Identifier: GPL-2.0 // Copyright (C) 2018 Hangzhou C-SKY Microsystems co.,ltd. #include <linux/ptrace.h> #include <linux/uaccess.h> #include <abi/reg_ops.h> #define MTCR_MASK 0xFC00FFE0 #define MFCR_MASK 0xFC00FFE0 #define MTCR_DIST 0xC0006420 #define MFCR_DIST 0xC0006020 void __init init_fpu(void) { mtcr("cr<1, 2>", 0); } /* * fpu_libc_helper() is to help libc to excute: * - mfcr %a, cr<1, 2> * - mfcr %a, cr<2, 2> * - mtcr %a, cr<1, 2> * - mtcr %a, cr<2, 2> */ int fpu_libc_helper(struct pt_regs *regs) { int fault; unsigned long instrptr, regx = 0; unsigned long index = 0, tmp = 0; unsigned long tinstr = 0; u16 instr_hi, instr_low; instrptr = instruction_pointer(regs); if (instrptr & 1) return 0; fault = __get_user(instr_low, (u16 *)instrptr); if (fault) return 0; fault = __get_user(instr_hi, (u16 *)(instrptr + 2)); if (fault) return 0; tinstr = instr_hi | ((unsigned long)instr_low << 16); if (((tinstr >> 21) & 0x1F) != 2) return 0; if ((tinstr & MTCR_MASK) == MTCR_DIST) { index = (tinstr >> 16) & 0x1F; if (index > 13) return 0; tmp = tinstr & 0x1F; if (tmp > 2) return 0; regx = *(®s->a0 + index); if (tmp == 1) mtcr("cr<1, 2>", regx); else if (tmp == 2) mtcr("cr<2, 2>", regx); else return 0; regs->pc += 4; return 1; } if ((tinstr & MFCR_MASK) == MFCR_DIST) { index = tinstr & 0x1F; if (index > 13) return 0; tmp = ((tinstr >> 16) & 0x1F); if (tmp > 2) return 0; if (tmp == 1) regx = mfcr("cr<1, 2>"); else if (tmp == 2) regx = mfcr("cr<2, 2>"); else return 0; *(®s->a0 + index) = regx; regs->pc += 4; return 1; } return 0; } void fpu_fpe(struct pt_regs *regs) { int sig, code; unsigned int fesr; fesr = mfcr("cr<2, 2>"); sig = SIGFPE; code = FPE_FLTUNK; if (fesr & FPE_ILLE) { sig = SIGILL; code = ILL_ILLOPC; } else if (fesr & FPE_IDC) { sig = SIGILL; code = ILL_ILLOPN; } else if (fesr & FPE_FEC) { sig = SIGFPE; if (fesr & FPE_IOC) code = FPE_FLTINV; else if (fesr & FPE_DZC) code = FPE_FLTDIV; else if (fesr & FPE_UFC) code = FPE_FLTUND; else if (fesr & FPE_OFC) code = FPE_FLTOVF; else if (fesr & FPE_IXC) code = FPE_FLTRES; } force_sig_fault(sig, code, (void __user *)regs->pc, current); } #define FMFVR_FPU_REGS(vrx, vry) \ "fmfvrl %0, "#vrx"\n" \ "fmfvrh %1, "#vrx"\n" \ "fmfvrl %2, "#vry"\n" \ "fmfvrh %3, "#vry"\n" #define FMTVR_FPU_REGS(vrx, vry) \ "fmtvrl "#vrx", %0\n" \ "fmtvrh "#vrx", %1\n" \ "fmtvrl "#vry", %2\n" \ "fmtvrh "#vry", %3\n" #define STW_FPU_REGS(a, b, c, d) \ "stw %0, (%4, "#a")\n" \ "stw %1, (%4, "#b")\n" \ "stw %2, (%4, "#c")\n" \ "stw %3, (%4, "#d")\n" #define LDW_FPU_REGS(a, b, c, d) \ "ldw %0, (%4, "#a")\n" \ "ldw %1, (%4, "#b")\n" \ "ldw %2, (%4, "#c")\n" \ "ldw %3, (%4, "#d")\n" void save_to_user_fp(struct user_fp *user_fp) { unsigned long flg; unsigned long tmp1, tmp2; unsigned long *fpregs; local_irq_save(flg); tmp1 = mfcr("cr<1, 2>"); tmp2 = mfcr("cr<2, 2>"); user_fp->fcr = tmp1; user_fp->fesr = tmp2; fpregs = &user_fp->vr[0]; #ifdef CONFIG_CPU_HAS_FPUV2 #ifdef CONFIG_CPU_HAS_VDSP asm volatile( "vstmu.32 vr0-vr3, (%0)\n" "vstmu.32 vr4-vr7, (%0)\n" "vstmu.32 vr8-vr11, (%0)\n" "vstmu.32 vr12-vr15, (%0)\n" "fstmu.64 vr16-vr31, (%0)\n" : "+a"(fpregs) ::"memory"); #else asm volatile( "fstmu.64 vr0-vr31, (%0)\n" : "+a"(fpregs) ::"memory"); #endif #else { unsigned long tmp3, tmp4; asm volatile( FMFVR_FPU_REGS(vr0, vr1) STW_FPU_REGS(0, 4, 16, 20) FMFVR_FPU_REGS(vr2, vr3) STW_FPU_REGS(32, 36, 48, 52) FMFVR_FPU_REGS(vr4, vr5) STW_FPU_REGS(64, 68, 80, 84) FMFVR_FPU_REGS(vr6, vr7) STW_FPU_REGS(96, 100, 112, 116) "addi %4, 128\n" FMFVR_FPU_REGS(vr8, vr9) STW_FPU_REGS(0, 4, 16, 20) FMFVR_FPU_REGS(vr10, vr11) STW_FPU_REGS(32, 36, 48, 52) FMFVR_FPU_REGS(vr12, vr13) STW_FPU_REGS(64, 68, 80, 84) FMFVR_FPU_REGS(vr14, vr15) STW_FPU_REGS(96, 100, 112, 116) : "=a"(tmp1), "=a"(tmp2), "=a"(tmp3), "=a"(tmp4), "+a"(fpregs) ::"memory"); } #endif local_irq_restore(flg); } void restore_from_user_fp(struct user_fp *user_fp) { unsigned long flg; unsigned long tmp1, tmp2; unsigned long *fpregs; local_irq_save(flg); tmp1 = user_fp->fcr; tmp2 = user_fp->fesr; mtcr("cr<1, 2>", tmp1); mtcr("cr<2, 2>", tmp2); fpregs = &user_fp->vr[0]; #ifdef CONFIG_CPU_HAS_FPUV2 #ifdef CONFIG_CPU_HAS_VDSP asm volatile( "vldmu.32 vr0-vr3, (%0)\n" "vldmu.32 vr4-vr7, (%0)\n" "vldmu.32 vr8-vr11, (%0)\n" "vldmu.32 vr12-vr15, (%0)\n" "fldmu.64 vr16-vr31, (%0)\n" : "+a"(fpregs) ::"memory"); #else asm volatile( "fldmu.64 vr0-vr31, (%0)\n" : "+a"(fpregs) ::"memory"); #endif #else { unsigned long tmp3, tmp4; asm volatile( LDW_FPU_REGS(0, 4, 16, 20) FMTVR_FPU_REGS(vr0, vr1) LDW_FPU_REGS(32, 36, 48, 52) FMTVR_FPU_REGS(vr2, vr3) LDW_FPU_REGS(64, 68, 80, 84) FMTVR_FPU_REGS(vr4, vr5) LDW_FPU_REGS(96, 100, 112, 116) FMTVR_FPU_REGS(vr6, vr7) "addi %4, 128\n" LDW_FPU_REGS(0, 4, 16, 20) FMTVR_FPU_REGS(vr8, vr9) LDW_FPU_REGS(32, 36, 48, 52) FMTVR_FPU_REGS(vr10, vr11) LDW_FPU_REGS(64, 68, 80, 84) FMTVR_FPU_REGS(vr12, vr13) LDW_FPU_REGS(96, 100, 112, 116) FMTVR_FPU_REGS(vr14, vr15) : "=a"(tmp1), "=a"(tmp2), "=a"(tmp3), "=a"(tmp4), "+a"(fpregs) ::"memory"); } #endif local_irq_restore(flg); } |