^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * unaligned.c: Unaligned load/store trap handling with special
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) * cases for the kernel to do them more quickly.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 1996 Jakub Jelinek (jj@sunsite.mff.cuni.cz)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/sched/signal.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <asm/ptrace.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <asm/processor.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/smp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/perf_event.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <asm/setup.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include "kernel.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) enum direction {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) load, /* ld, ldd, ldh, ldsh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) store, /* st, std, sth, stsh */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) both, /* Swap, ldstub, etc. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) fpload,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) fpstore,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) invalid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) static inline enum direction decode_direction(unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) unsigned long tmp = (insn >> 21) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) if(!tmp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return load;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if(((insn>>19)&0x3f) == 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return both;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) return store;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) /* 8 = double-word, 4 = word, 2 = half-word */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) static inline int decode_access_size(unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) insn = (insn >> 19) & 3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) if(!insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) return 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) else if(insn == 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) return 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) else if(insn == 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) printk("Impossible unaligned trap. insn=%08x\n", insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) die_if_kernel("Byte sized unaligned access?!?!", current->thread.kregs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return 4; /* just to keep gcc happy. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) /* 0x400000 = signed, 0 = unsigned */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) static inline int decode_signedness(unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return (insn & 0x400000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static inline void maybe_flush_windows(unsigned int rs1, unsigned int rs2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) unsigned int rd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if(rs2 >= 16 || rs1 >= 16 || rd >= 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) /* Wheee... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) __asm__ __volatile__("save %sp, -0x40, %sp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) "save %sp, -0x40, %sp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) "save %sp, -0x40, %sp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) "save %sp, -0x40, %sp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) "save %sp, -0x40, %sp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) "save %sp, -0x40, %sp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) "save %sp, -0x40, %sp\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) "restore; restore; restore; restore;\n\t"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) "restore; restore; restore;\n\t");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) static inline int sign_extend_imm13(int imm)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) return imm << 19 >> 19;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) static inline unsigned long fetch_reg(unsigned int reg, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) struct reg_window32 *win;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) if(reg < 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return (!reg ? 0 : regs->u_regs[reg]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) /* Ho hum, the slightly complicated case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) win = (struct reg_window32 *) regs->u_regs[UREG_FP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) return win->locals[reg - 16]; /* yes, I know what this does... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static inline unsigned long safe_fetch_reg(unsigned int reg, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) struct reg_window32 __user *win;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) unsigned long ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) if (reg < 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return (!reg ? 0 : regs->u_regs[reg]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) /* Ho hum, the slightly complicated case. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) win = (struct reg_window32 __user *) regs->u_regs[UREG_FP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) if ((unsigned long)win & 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) if (get_user(ret, &win->locals[reg - 16]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) static inline unsigned long *fetch_reg_addr(unsigned int reg, struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) struct reg_window32 *win;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) if(reg < 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return ®s->u_regs[reg];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) win = (struct reg_window32 *) regs->u_regs[UREG_FP];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) return &win->locals[reg - 16];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) static unsigned long compute_effective_address(struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) unsigned int rs1 = (insn >> 14) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) unsigned int rs2 = insn & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) unsigned int rd = (insn >> 25) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) if(insn & 0x2000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) maybe_flush_windows(rs1, 0, rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) return (fetch_reg(rs1, regs) + sign_extend_imm13(insn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) maybe_flush_windows(rs1, rs2, rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) return (fetch_reg(rs1, regs) + fetch_reg(rs2, regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) unsigned long safe_compute_effective_address(struct pt_regs *regs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) unsigned int rs1 = (insn >> 14) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) unsigned int rs2 = insn & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) unsigned int rd = (insn >> 25) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if(insn & 0x2000) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) maybe_flush_windows(rs1, 0, rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) return (safe_fetch_reg(rs1, regs) + sign_extend_imm13(insn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) maybe_flush_windows(rs1, rs2, rd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) return (safe_fetch_reg(rs1, regs) + safe_fetch_reg(rs2, regs));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) /* This is just to make gcc think panic does return... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) static void unaligned_panic(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) panic("%s", str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* una_asm.S */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) extern int do_int_load(unsigned long *dest_reg, int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) unsigned long *saddr, int is_signed);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) extern int __do_int_store(unsigned long *dst_addr, int size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) unsigned long *src_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) static int do_int_store(int reg_num, int size, unsigned long *dst_addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) unsigned long zero[2] = { 0, 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned long *src_val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) if (reg_num)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) src_val = fetch_reg_addr(reg_num, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) src_val = &zero[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) if (size == 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) zero[1] = fetch_reg(1, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) return __do_int_store(dst_addr, size, src_val);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) extern void smp_capture(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) extern void smp_release(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) static inline void advance(struct pt_regs *regs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) regs->pc = regs->npc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) regs->npc += 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) static inline int floating_point_load_or_store_p(unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) return (insn >> 24) & 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) static inline int ok_for_kernel(unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) return !floating_point_load_or_store_p(insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) static void kernel_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) unsigned long g2 = regs->u_regs [UREG_G2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) unsigned long fixup = search_extables_range(regs->pc, &g2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) if (!fixup) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) unsigned long address = compute_effective_address(regs, insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if(address < PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) printk(KERN_ALERT "Unable to handle kernel NULL pointer dereference in mna handler");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) printk(KERN_ALERT "Unable to handle kernel paging request in mna handler");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) printk(KERN_ALERT " at virtual address %08lx\n",address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) printk(KERN_ALERT "current->{mm,active_mm}->context = %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) (current->mm ? current->mm->context :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) current->active_mm->context));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) printk(KERN_ALERT "current->{mm,active_mm}->pgd = %08lx\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) (current->mm ? (unsigned long) current->mm->pgd :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) (unsigned long) current->active_mm->pgd));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) die_if_kernel("Oops", regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) /* Not reached */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) regs->pc = fixup;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) regs->npc = regs->pc + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) regs->u_regs [UREG_G2] = g2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) asmlinkage void kernel_unaligned_trap(struct pt_regs *regs, unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) enum direction dir = decode_direction(insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) int size = decode_access_size(insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if(!ok_for_kernel(insn) || dir == both) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) printk("Unsupported unaligned load/store trap for kernel at <%08lx>.\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) regs->pc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) unaligned_panic("Wheee. Kernel does fpu/atomic unaligned load/store.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) unsigned long addr = compute_effective_address(regs, insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) switch (dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) case load:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) err = do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) regs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) size, (unsigned long *) addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) decode_signedness(insn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) case store:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) err = do_int_store(((insn>>25)&0x1f), size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) (unsigned long *) addr, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) panic("Impossible kernel unaligned trap.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) /* Not reached... */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) kernel_mna_trap_fault(regs, insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) advance(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) static inline int ok_for_user(struct pt_regs *regs, unsigned int insn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) enum direction dir)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) unsigned int reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) int size = ((insn >> 19) & 3) == 3 ? 8 : 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) if ((regs->pc | regs->npc) & 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* Must access_ok() in all the necessary places. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) #define WINREG_ADDR(regnum) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) ((void __user *)(((unsigned long *)regs->u_regs[UREG_FP])+(regnum)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) reg = (insn >> 25) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (reg >= 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) if (!access_ok(WINREG_ADDR(reg - 16), size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) reg = (insn >> 14) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (reg >= 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) if (!access_ok(WINREG_ADDR(reg - 16), size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (!(insn & 0x2000)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) reg = (insn & 0x1f);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) if (reg >= 16) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (!access_ok(WINREG_ADDR(reg - 16), size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) #undef WINREG_ADDR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) static void user_mna_trap_fault(struct pt_regs *regs, unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) send_sig_fault(SIGBUS, BUS_ADRALN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) (void __user *)safe_compute_effective_address(regs, insn),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 0, current);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) asmlinkage void user_unaligned_trap(struct pt_regs *regs, unsigned int insn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) enum direction dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) if(!(current->thread.flags & SPARC_FLAG_UNALIGNED) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) (((insn >> 30) & 3) != 3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) goto kill_user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) dir = decode_direction(insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if(!ok_for_user(regs, insn, dir)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) goto kill_user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) int err, size = decode_access_size(insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) unsigned long addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) if(floating_point_load_or_store_p(insn)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) printk("User FPU load/store unaligned unsupported.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) goto kill_user;
^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) addr = compute_effective_address(regs, insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) perf_sw_event(PERF_COUNT_SW_ALIGNMENT_FAULTS, 1, regs, addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) switch(dir) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) case load:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) err = do_int_load(fetch_reg_addr(((insn>>25)&0x1f),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) regs),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) size, (unsigned long *) addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) decode_signedness(insn));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) case store:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) err = do_int_store(((insn>>25)&0x1f), size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) (unsigned long *) addr, regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) case both:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) * This was supported in 2.4. However, we question
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) * the value of SWAP instruction across word boundaries.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) printk("Unaligned SWAP unsupported.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) err = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) unaligned_panic("Impossible user unaligned trap.");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) goto kill_user;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) advance(regs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) kill_user:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) user_mna_trap_fault(regs, insn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) }