^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) * This file is subject to the terms and conditions of the GNU General Public
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * License. See the file "COPYING" in the main directory of this archive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * for more details.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2014 Lemote Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * written by Huacai Chen <chenhc@lemote.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * based on arch/mips/cavium-octeon/cpu.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Copyright (C) 2009 Wind River Systems,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * written by Ralf Baechle <ralf@linux-mips.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/notifier.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/fpu.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <asm/cop2.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <asm/inst.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <asm/branch.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <asm/current.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <asm/mipsregs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <asm/unaligned-emul.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) static int loongson_cu2_call(struct notifier_block *nfb, unsigned long action,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) void *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) unsigned int res, fpu_owned;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned long ra, value, value_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) union mips_instruction insn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) int fr = !test_thread_flag(TIF_32BIT_FPREGS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct pt_regs *regs = (struct pt_regs *)data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) void __user *addr = (void __user *)regs->cp0_badvaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) unsigned int __user *pc = (unsigned int __user *)exception_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) ra = regs->regs[31];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) __get_user(insn.word, pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) switch (action) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) case CU2_EXCEPTION:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) preempt_disable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) fpu_owned = __is_fpu_owner();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) if (!fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) set_c0_status(ST0_CU1 | ST0_CU2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) set_c0_status(ST0_CU1 | ST0_CU2 | ST0_FR);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) enable_fpu_hazard();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) KSTK_STATUS(current) |= (ST0_CU1 | ST0_CU2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if (fr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) KSTK_STATUS(current) |= ST0_FR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) KSTK_STATUS(current) &= ~ST0_FR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /* If FPU is owned, we needn't init or restore fp */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) if (!fpu_owned) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) set_thread_flag(TIF_USEDFPU);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) init_fp_ctx(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) _restore_fp(current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) preempt_enable();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return NOTIFY_STOP; /* Don't call default notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) case CU2_LWC2_OP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) if (insn.loongson3_lswc2_format.ls == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) if (insn.loongson3_lswc2_format.fr == 0) { /* gslq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) if (!access_ok(addr, 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) LoadDW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) LoadDW(addr + 8, value_next, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) regs->regs[insn.loongson3_lswc2_format.rt] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) regs->regs[insn.loongson3_lswc2_format.rq] = value_next;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) } else { /* gslqc1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (!access_ok(addr, 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) lose_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) LoadDW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) LoadDW(addr + 8, value_next, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) set_fpr64(¤t->thread.fpu.fpr[insn.loongson3_lswc2_format.rt], 0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) set_fpr64(¤t->thread.fpu.fpr[insn.loongson3_lswc2_format.rq], 0, value_next);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) own_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return NOTIFY_STOP; /* Don't call default notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) case CU2_SWC2_OP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (insn.loongson3_lswc2_format.ls == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (insn.loongson3_lswc2_format.fr == 0) { /* gssq */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (!access_ok(addr, 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* write upper 8 bytes first */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) value_next = regs->regs[insn.loongson3_lswc2_format.rq];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) StoreDW(addr + 8, value_next, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) value = regs->regs[insn.loongson3_lswc2_format.rt];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) StoreDW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) } else { /* gssqc1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) if (!access_ok(addr, 16))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) lose_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) value_next = get_fpr64(¤t->thread.fpu.fpr[insn.loongson3_lswc2_format.rq], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) StoreDW(addr + 8, value_next, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) value = get_fpr64(¤t->thread.fpu.fpr[insn.loongson3_lswc2_format.rt], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) StoreDW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) own_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) return NOTIFY_STOP; /* Don't call default notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) case CU2_LDC2_OP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) switch (insn.loongson3_lsdc2_format.opcode1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * Loongson-3 overridden ldc2 instructions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * opcode1 instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) * 0x1 gslhx: load 2 bytes to GPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * 0x2 gslwx: load 4 bytes to GPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * 0x3 gsldx: load 8 bytes to GPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * 0x6 gslwxc1: load 4 bytes to FPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * 0x7 gsldxc1: load 8 bytes to FPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) case 0x1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) if (!access_ok(addr, 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) LoadHW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) regs->regs[insn.loongson3_lsdc2_format.rt] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) case 0x2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (!access_ok(addr, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) LoadW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) regs->regs[insn.loongson3_lsdc2_format.rt] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) case 0x3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (!access_ok(addr, 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) LoadDW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) regs->regs[insn.loongson3_lsdc2_format.rt] = value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) case 0x6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) die_if_kernel("Unaligned FP access in kernel code", regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) BUG_ON(!used_math());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) if (!access_ok(addr, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) lose_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) LoadW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) set_fpr64(¤t->thread.fpu.fpr[insn.loongson3_lsdc2_format.rt], 0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) own_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) case 0x7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) die_if_kernel("Unaligned FP access in kernel code", regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) BUG_ON(!used_math());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!access_ok(addr, 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) lose_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) LoadDW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) set_fpr64(¤t->thread.fpu.fpr[insn.loongson3_lsdc2_format.rt], 0, value);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) own_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) return NOTIFY_STOP; /* Don't call default notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) case CU2_SDC2_OP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) switch (insn.loongson3_lsdc2_format.opcode1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) * Loongson-3 overridden sdc2 instructions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) * opcode1 instruction
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) * 0x1 gsshx: store 2 bytes from GPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) * 0x2 gsswx: store 4 bytes from GPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) * 0x3 gssdx: store 8 bytes from GPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) * 0x6 gsswxc1: store 4 bytes from FPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) * 0x7 gssdxc1: store 8 bytes from FPR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) case 0x1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) if (!access_ok(addr, 2))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) value = regs->regs[insn.loongson3_lsdc2_format.rt];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) StoreHW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) case 0x2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (!access_ok(addr, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) value = regs->regs[insn.loongson3_lsdc2_format.rt];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) StoreW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case 0x3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (!access_ok(addr, 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) value = regs->regs[insn.loongson3_lsdc2_format.rt];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) StoreDW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) case 0x6:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) die_if_kernel("Unaligned FP access in kernel code", regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) BUG_ON(!used_math());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) if (!access_ok(addr, 4))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) lose_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) value = get_fpr64(¤t->thread.fpu.fpr[insn.loongson3_lsdc2_format.rt], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) StoreW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) own_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) case 0x7:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) die_if_kernel("Unaligned FP access in kernel code", regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) BUG_ON(!used_math());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!access_ok(addr, 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) goto sigbus;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) lose_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) value = get_fpr64(¤t->thread.fpu.fpr[insn.loongson3_lsdc2_format.rt], 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) StoreDW(addr, value, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) if (res)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) goto fault;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) compute_return_epc(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) own_fpu(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) return NOTIFY_STOP; /* Don't call default notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return NOTIFY_OK; /* Let default notifier send signals */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) fault:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* roll back jump/branch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) regs->regs[31] = ra;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) regs->cp0_epc = (unsigned long)pc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) /* Did we have an exception handler installed? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if (fixup_exception(regs))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) return NOTIFY_STOP; /* Don't call default notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) die_if_kernel("Unhandled kernel unaligned access", regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) force_sig(SIGSEGV);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) return NOTIFY_STOP; /* Don't call default notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) sigbus:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) die_if_kernel("Unhandled kernel unaligned access", regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) force_sig(SIGBUS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return NOTIFY_STOP; /* Don't call default notifier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static int __init loongson_cu2_setup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) return cu2_notifier(loongson_cu2_call, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) early_initcall(loongson_cu2_setup);