1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * S390 version 4 * Copyright IBM Corp. 1999, 2000 5 * Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com), 6 * Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com), 7 * 8 * Derived from "arch/i386/kernel/traps.c" 9 * Copyright (C) 1991, 1992 Linus Torvalds 10 */ 11 12 /* 13 * 'Traps.c' handles hardware traps and faults after we have saved some 14 * state in 'asm.s'. 15 */ 16 #include <linux/kprobes.h> 17 #include <linux/kdebug.h> 18 #include <linux/extable.h> 19 #include <linux/ptrace.h> 20 #include <linux/sched.h> 21 #include <linux/sched/debug.h> 22 #include <linux/mm.h> 23 #include <linux/slab.h> 24 #include <linux/uaccess.h> 25 #include <linux/cpu.h> 26 #include <asm/fpu/api.h> 27 #include "entry.h" 28 29 static inline void __user *get_trap_ip(struct pt_regs *regs) 30 { 31 unsigned long address; 32 33 if (regs->int_code & 0x200) 34 address = *(unsigned long *)(current->thread.trap_tdb + 24); 35 else 36 address = regs->psw.addr; 37 return (void __user *) (address - (regs->int_code >> 16)); 38 } 39 40 int is_valid_bugaddr(unsigned long addr) 41 { 42 return 1; 43 } 44 45 void do_report_trap(struct pt_regs *regs, int si_signo, int si_code, char *str) 46 { 47 if (user_mode(regs)) { 48 force_sig_fault(si_signo, si_code, get_trap_ip(regs)); 49 report_user_fault(regs, si_signo, 0); 50 } else { 51 const struct exception_table_entry *fixup; 52 fixup = s390_search_extables(regs->psw.addr); 53 if (fixup) 54 regs->psw.addr = extable_fixup(fixup); 55 else { 56 die(regs, str); 57 } 58 } 59 } 60 61 static void do_trap(struct pt_regs *regs, int si_signo, int si_code, char *str) 62 { 63 if (notify_die(DIE_TRAP, str, regs, 0, 64 regs->int_code, si_signo) == NOTIFY_STOP) 65 return; 66 do_report_trap(regs, si_signo, si_code, str); 67 } 68 NOKPROBE_SYMBOL(do_trap); 69 70 void do_per_trap(struct pt_regs *regs) 71 { 72 if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP) 73 return; 74 if (!current->ptrace) 75 return; 76 force_sig_fault(SIGTRAP, TRAP_HWBKPT, 77 (void __force __user *) current->thread.per_event.address); 78 } 79 NOKPROBE_SYMBOL(do_per_trap); 80 81 void default_trap_handler(struct pt_regs *regs) 82 { 83 if (user_mode(regs)) { 84 report_user_fault(regs, SIGSEGV, 0); 85 do_exit(SIGSEGV); 86 } else 87 die(regs, "Unknown program exception"); 88 } 89 90 #define DO_ERROR_INFO(name, signr, sicode, str) \ 91 void name(struct pt_regs *regs) \ 92 { \ 93 do_trap(regs, signr, sicode, str); \ 94 } 95 96 DO_ERROR_INFO(addressing_exception, SIGILL, ILL_ILLADR, 97 "addressing exception") 98 DO_ERROR_INFO(execute_exception, SIGILL, ILL_ILLOPN, 99 "execute exception") 100 DO_ERROR_INFO(divide_exception, SIGFPE, FPE_INTDIV, 101 "fixpoint divide exception") 102 DO_ERROR_INFO(overflow_exception, SIGFPE, FPE_INTOVF, 103 "fixpoint overflow exception") 104 DO_ERROR_INFO(hfp_overflow_exception, SIGFPE, FPE_FLTOVF, 105 "HFP overflow exception") 106 DO_ERROR_INFO(hfp_underflow_exception, SIGFPE, FPE_FLTUND, 107 "HFP underflow exception") 108 DO_ERROR_INFO(hfp_significance_exception, SIGFPE, FPE_FLTRES, 109 "HFP significance exception") 110 DO_ERROR_INFO(hfp_divide_exception, SIGFPE, FPE_FLTDIV, 111 "HFP divide exception") 112 DO_ERROR_INFO(hfp_sqrt_exception, SIGFPE, FPE_FLTINV, 113 "HFP square root exception") 114 DO_ERROR_INFO(operand_exception, SIGILL, ILL_ILLOPN, 115 "operand exception") 116 DO_ERROR_INFO(privileged_op, SIGILL, ILL_PRVOPC, 117 "privileged operation") 118 DO_ERROR_INFO(special_op_exception, SIGILL, ILL_ILLOPN, 119 "special operation exception") 120 DO_ERROR_INFO(transaction_exception, SIGILL, ILL_ILLOPN, 121 "transaction constraint exception") 122 123 static inline void do_fp_trap(struct pt_regs *regs, __u32 fpc) 124 { 125 int si_code = 0; 126 /* FPC[2] is Data Exception Code */ 127 if ((fpc & 0x00000300) == 0) { 128 /* bits 6 and 7 of DXC are 0 iff IEEE exception */ 129 if (fpc & 0x8000) /* invalid fp operation */ 130 si_code = FPE_FLTINV; 131 else if (fpc & 0x4000) /* div by 0 */ 132 si_code = FPE_FLTDIV; 133 else if (fpc & 0x2000) /* overflow */ 134 si_code = FPE_FLTOVF; 135 else if (fpc & 0x1000) /* underflow */ 136 si_code = FPE_FLTUND; 137 else if (fpc & 0x0800) /* inexact */ 138 si_code = FPE_FLTRES; 139 } 140 do_trap(regs, SIGFPE, si_code, "floating point exception"); 141 } 142 143 void translation_exception(struct pt_regs *regs) 144 { 145 /* May never happen. */ 146 panic("Translation exception"); 147 } 148 149 void illegal_op(struct pt_regs *regs) 150 { 151 __u8 opcode[6]; 152 __u16 __user *location; 153 int is_uprobe_insn = 0; 154 int signal = 0; 155 156 location = get_trap_ip(regs); 157 158 if (user_mode(regs)) { 159 if (get_user(*((__u16 *) opcode), (__u16 __user *) location)) 160 return; 161 if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) { 162 if (current->ptrace) 163 force_sig_fault(SIGTRAP, TRAP_BRKPT, location); 164 else 165 signal = SIGILL; 166 #ifdef CONFIG_UPROBES 167 } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) { 168 is_uprobe_insn = 1; 169 #endif 170 } else 171 signal = SIGILL; 172 } 173 /* 174 * We got either an illegal op in kernel mode, or user space trapped 175 * on a uprobes illegal instruction. See if kprobes or uprobes picks 176 * it up. If not, SIGILL. 177 */ 178 if (is_uprobe_insn || !user_mode(regs)) { 179 if (notify_die(DIE_BPT, "bpt", regs, 0, 180 3, SIGTRAP) != NOTIFY_STOP) 181 signal = SIGILL; 182 } 183 if (signal) 184 do_trap(regs, signal, ILL_ILLOPC, "illegal operation"); 185 } 186 NOKPROBE_SYMBOL(illegal_op); 187 188 DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN, 189 "specification exception"); 190 191 void vector_exception(struct pt_regs *regs) 192 { 193 int si_code, vic; 194 195 if (!MACHINE_HAS_VX) { 196 do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation"); 197 return; 198 } 199 200 /* get vector interrupt code from fpc */ 201 save_fpu_regs(); 202 vic = (current->thread.fpu.fpc & 0xf00) >> 8; 203 switch (vic) { 204 case 1: /* invalid vector operation */ 205 si_code = FPE_FLTINV; 206 break; 207 case 2: /* division by zero */ 208 si_code = FPE_FLTDIV; 209 break; 210 case 3: /* overflow */ 211 si_code = FPE_FLTOVF; 212 break; 213 case 4: /* underflow */ 214 si_code = FPE_FLTUND; 215 break; 216 case 5: /* inexact */ 217 si_code = FPE_FLTRES; 218 break; 219 default: /* unknown cause */ 220 si_code = 0; 221 } 222 do_trap(regs, SIGFPE, si_code, "vector exception"); 223 } 224 225 void data_exception(struct pt_regs *regs) 226 { 227 save_fpu_regs(); 228 if (current->thread.fpu.fpc & FPC_DXC_MASK) 229 do_fp_trap(regs, current->thread.fpu.fpc); 230 else 231 do_trap(regs, SIGILL, ILL_ILLOPN, "data exception"); 232 } 233 234 void space_switch_exception(struct pt_regs *regs) 235 { 236 /* Set user psw back to home space mode. */ 237 if (user_mode(regs)) 238 regs->psw.mask |= PSW_ASC_HOME; 239 /* Send SIGILL. */ 240 do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event"); 241 } 242 243 void monitor_event_exception(struct pt_regs *regs) 244 { 245 const struct exception_table_entry *fixup; 246 247 if (user_mode(regs)) 248 return; 249 250 switch (report_bug(regs->psw.addr - (regs->int_code >> 16), regs)) { 251 case BUG_TRAP_TYPE_NONE: 252 fixup = s390_search_extables(regs->psw.addr); 253 if (fixup) 254 regs->psw.addr = extable_fixup(fixup); 255 break; 256 case BUG_TRAP_TYPE_WARN: 257 break; 258 case BUG_TRAP_TYPE_BUG: 259 die(regs, "monitor event"); 260 break; 261 } 262 } 263 264 void kernel_stack_overflow(struct pt_regs *regs) 265 { 266 bust_spinlocks(1); 267 printk("Kernel stack overflow.\n"); 268 show_regs(regs); 269 bust_spinlocks(0); 270 panic("Corrupt kernel stack, can't continue."); 271 } 272 NOKPROBE_SYMBOL(kernel_stack_overflow); 273 274 static void test_monitor_call(void) 275 { 276 int val = 1; 277 278 asm volatile( 279 " mc 0,0\n" 280 "0: xgr %0,%0\n" 281 "1:\n" 282 EX_TABLE(0b,1b) 283 : "+d" (val)); 284 if (!val) 285 panic("Monitor call doesn't work!\n"); 286 } 287 288 void __init trap_init(void) 289 { 290 sort_extable(__start_dma_ex_table, __stop_dma_ex_table); 291 local_mcck_enable(); 292 test_monitor_call(); 293 } 294