Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  *  S390 version
0004  *    Copyright IBM Corp. 1999, 2000
0005  *    Author(s): Martin Schwidefsky (schwidefsky@de.ibm.com),
0006  *               Denis Joseph Barrow (djbarrow@de.ibm.com,barrow_dj@yahoo.com),
0007  *
0008  *  Derived from "arch/i386/kernel/traps.c"
0009  *    Copyright (C) 1991, 1992 Linus Torvalds
0010  */
0011 
0012 /*
0013  * 'Traps.c' handles hardware traps and faults after we have saved some
0014  * state in 'asm.s'.
0015  */
0016 #include "asm/irqflags.h"
0017 #include "asm/ptrace.h"
0018 #include <linux/kprobes.h>
0019 #include <linux/kdebug.h>
0020 #include <linux/randomize_kstack.h>
0021 #include <linux/extable.h>
0022 #include <linux/ptrace.h>
0023 #include <linux/sched.h>
0024 #include <linux/sched/debug.h>
0025 #include <linux/mm.h>
0026 #include <linux/slab.h>
0027 #include <linux/uaccess.h>
0028 #include <linux/cpu.h>
0029 #include <linux/entry-common.h>
0030 #include <asm/asm-extable.h>
0031 #include <asm/fpu/api.h>
0032 #include <asm/vtime.h>
0033 #include "entry.h"
0034 
0035 static inline void __user *get_trap_ip(struct pt_regs *regs)
0036 {
0037     unsigned long address;
0038 
0039     if (regs->int_code & 0x200)
0040         address = current->thread.trap_tdb.data[3];
0041     else
0042         address = regs->psw.addr;
0043     return (void __user *) (address - (regs->int_code >> 16));
0044 }
0045 
0046 int is_valid_bugaddr(unsigned long addr)
0047 {
0048     return 1;
0049 }
0050 
0051 void do_report_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
0052 {
0053     if (user_mode(regs)) {
0054         force_sig_fault(si_signo, si_code, get_trap_ip(regs));
0055         report_user_fault(regs, si_signo, 0);
0056         } else {
0057         if (!fixup_exception(regs))
0058             die(regs, str);
0059         }
0060 }
0061 
0062 static void do_trap(struct pt_regs *regs, int si_signo, int si_code, char *str)
0063 {
0064     if (notify_die(DIE_TRAP, str, regs, 0,
0065                regs->int_code, si_signo) == NOTIFY_STOP)
0066         return;
0067     do_report_trap(regs, si_signo, si_code, str);
0068 }
0069 NOKPROBE_SYMBOL(do_trap);
0070 
0071 void do_per_trap(struct pt_regs *regs)
0072 {
0073     if (notify_die(DIE_SSTEP, "sstep", regs, 0, 0, SIGTRAP) == NOTIFY_STOP)
0074         return;
0075     if (!current->ptrace)
0076         return;
0077     force_sig_fault(SIGTRAP, TRAP_HWBKPT,
0078         (void __force __user *) current->thread.per_event.address);
0079 }
0080 NOKPROBE_SYMBOL(do_per_trap);
0081 
0082 static void default_trap_handler(struct pt_regs *regs)
0083 {
0084     if (user_mode(regs)) {
0085         report_user_fault(regs, SIGSEGV, 0);
0086         force_exit_sig(SIGSEGV);
0087     } else
0088         die(regs, "Unknown program exception");
0089 }
0090 
0091 #define DO_ERROR_INFO(name, signr, sicode, str) \
0092 static void name(struct pt_regs *regs)      \
0093 {                       \
0094     do_trap(regs, signr, sicode, str);  \
0095 }
0096 
0097 DO_ERROR_INFO(addressing_exception, SIGILL, ILL_ILLADR,
0098           "addressing exception")
0099 DO_ERROR_INFO(execute_exception, SIGILL, ILL_ILLOPN,
0100           "execute exception")
0101 DO_ERROR_INFO(divide_exception, SIGFPE, FPE_INTDIV,
0102           "fixpoint divide exception")
0103 DO_ERROR_INFO(overflow_exception, SIGFPE, FPE_INTOVF,
0104           "fixpoint overflow exception")
0105 DO_ERROR_INFO(hfp_overflow_exception, SIGFPE, FPE_FLTOVF,
0106           "HFP overflow exception")
0107 DO_ERROR_INFO(hfp_underflow_exception, SIGFPE, FPE_FLTUND,
0108           "HFP underflow exception")
0109 DO_ERROR_INFO(hfp_significance_exception, SIGFPE, FPE_FLTRES,
0110           "HFP significance exception")
0111 DO_ERROR_INFO(hfp_divide_exception, SIGFPE, FPE_FLTDIV,
0112           "HFP divide exception")
0113 DO_ERROR_INFO(hfp_sqrt_exception, SIGFPE, FPE_FLTINV,
0114           "HFP square root exception")
0115 DO_ERROR_INFO(operand_exception, SIGILL, ILL_ILLOPN,
0116           "operand exception")
0117 DO_ERROR_INFO(privileged_op, SIGILL, ILL_PRVOPC,
0118           "privileged operation")
0119 DO_ERROR_INFO(special_op_exception, SIGILL, ILL_ILLOPN,
0120           "special operation exception")
0121 DO_ERROR_INFO(transaction_exception, SIGILL, ILL_ILLOPN,
0122           "transaction constraint exception")
0123 
0124 static inline void do_fp_trap(struct pt_regs *regs, __u32 fpc)
0125 {
0126     int si_code = 0;
0127     /* FPC[2] is Data Exception Code */
0128     if ((fpc & 0x00000300) == 0) {
0129         /* bits 6 and 7 of DXC are 0 iff IEEE exception */
0130         if (fpc & 0x8000) /* invalid fp operation */
0131             si_code = FPE_FLTINV;
0132         else if (fpc & 0x4000) /* div by 0 */
0133             si_code = FPE_FLTDIV;
0134         else if (fpc & 0x2000) /* overflow */
0135             si_code = FPE_FLTOVF;
0136         else if (fpc & 0x1000) /* underflow */
0137             si_code = FPE_FLTUND;
0138         else if (fpc & 0x0800) /* inexact */
0139             si_code = FPE_FLTRES;
0140     }
0141     do_trap(regs, SIGFPE, si_code, "floating point exception");
0142 }
0143 
0144 static void translation_specification_exception(struct pt_regs *regs)
0145 {
0146     /* May never happen. */
0147     panic("Translation-Specification Exception");
0148 }
0149 
0150 static void illegal_op(struct pt_regs *regs)
0151 {
0152         __u8 opcode[6];
0153     __u16 __user *location;
0154     int is_uprobe_insn = 0;
0155     int signal = 0;
0156 
0157     location = get_trap_ip(regs);
0158 
0159     if (user_mode(regs)) {
0160         if (get_user(*((__u16 *) opcode), (__u16 __user *) location))
0161             return;
0162         if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) {
0163             if (current->ptrace)
0164                 force_sig_fault(SIGTRAP, TRAP_BRKPT, location);
0165             else
0166                 signal = SIGILL;
0167 #ifdef CONFIG_UPROBES
0168         } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) {
0169             is_uprobe_insn = 1;
0170 #endif
0171         } else
0172             signal = SIGILL;
0173     }
0174     /*
0175      * We got either an illegal op in kernel mode, or user space trapped
0176      * on a uprobes illegal instruction. See if kprobes or uprobes picks
0177      * it up. If not, SIGILL.
0178      */
0179     if (is_uprobe_insn || !user_mode(regs)) {
0180         if (notify_die(DIE_BPT, "bpt", regs, 0,
0181                    3, SIGTRAP) != NOTIFY_STOP)
0182             signal = SIGILL;
0183     }
0184     if (signal)
0185         do_trap(regs, signal, ILL_ILLOPC, "illegal operation");
0186 }
0187 NOKPROBE_SYMBOL(illegal_op);
0188 
0189 DO_ERROR_INFO(specification_exception, SIGILL, ILL_ILLOPN,
0190           "specification exception");
0191 
0192 static void vector_exception(struct pt_regs *regs)
0193 {
0194     int si_code, vic;
0195 
0196     if (!MACHINE_HAS_VX) {
0197         do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation");
0198         return;
0199     }
0200 
0201     /* get vector interrupt code from fpc */
0202     save_fpu_regs();
0203     vic = (current->thread.fpu.fpc & 0xf00) >> 8;
0204     switch (vic) {
0205     case 1: /* invalid vector operation */
0206         si_code = FPE_FLTINV;
0207         break;
0208     case 2: /* division by zero */
0209         si_code = FPE_FLTDIV;
0210         break;
0211     case 3: /* overflow */
0212         si_code = FPE_FLTOVF;
0213         break;
0214     case 4: /* underflow */
0215         si_code = FPE_FLTUND;
0216         break;
0217     case 5: /* inexact */
0218         si_code = FPE_FLTRES;
0219         break;
0220     default: /* unknown cause */
0221         si_code = 0;
0222     }
0223     do_trap(regs, SIGFPE, si_code, "vector exception");
0224 }
0225 
0226 static void data_exception(struct pt_regs *regs)
0227 {
0228     save_fpu_regs();
0229     if (current->thread.fpu.fpc & FPC_DXC_MASK)
0230         do_fp_trap(regs, current->thread.fpu.fpc);
0231     else
0232         do_trap(regs, SIGILL, ILL_ILLOPN, "data exception");
0233 }
0234 
0235 static void space_switch_exception(struct pt_regs *regs)
0236 {
0237     /* Set user psw back to home space mode. */
0238     if (user_mode(regs))
0239         regs->psw.mask |= PSW_ASC_HOME;
0240     /* Send SIGILL. */
0241     do_trap(regs, SIGILL, ILL_PRVOPC, "space switch event");
0242 }
0243 
0244 static void monitor_event_exception(struct pt_regs *regs)
0245 {
0246     if (user_mode(regs))
0247         return;
0248 
0249     switch (report_bug(regs->psw.addr - (regs->int_code >> 16), regs)) {
0250     case BUG_TRAP_TYPE_NONE:
0251         fixup_exception(regs);
0252         break;
0253     case BUG_TRAP_TYPE_WARN:
0254         break;
0255     case BUG_TRAP_TYPE_BUG:
0256         die(regs, "monitor event");
0257         break;
0258     }
0259 }
0260 
0261 void kernel_stack_overflow(struct pt_regs *regs)
0262 {
0263     bust_spinlocks(1);
0264     printk("Kernel stack overflow.\n");
0265     show_regs(regs);
0266     bust_spinlocks(0);
0267     panic("Corrupt kernel stack, can't continue.");
0268 }
0269 NOKPROBE_SYMBOL(kernel_stack_overflow);
0270 
0271 static void __init test_monitor_call(void)
0272 {
0273     int val = 1;
0274 
0275     if (!IS_ENABLED(CONFIG_BUG))
0276         return;
0277     asm volatile(
0278         "   mc  0,0\n"
0279         "0: xgr %0,%0\n"
0280         "1:\n"
0281         EX_TABLE(0b,1b)
0282         : "+d" (val));
0283     if (!val)
0284         panic("Monitor call doesn't work!\n");
0285 }
0286 
0287 void __init trap_init(void)
0288 {
0289     local_mcck_enable();
0290     test_monitor_call();
0291 }
0292 
0293 static void (*pgm_check_table[128])(struct pt_regs *regs);
0294 
0295 void noinstr __do_pgm_check(struct pt_regs *regs)
0296 {
0297     unsigned int trapnr;
0298     irqentry_state_t state;
0299 
0300     regs->int_code = S390_lowcore.pgm_int_code;
0301     regs->int_parm_long = S390_lowcore.trans_exc_code;
0302 
0303     state = irqentry_enter(regs);
0304 
0305     if (user_mode(regs)) {
0306         update_timer_sys();
0307         if (!static_branch_likely(&cpu_has_bear)) {
0308             if (regs->last_break < 4096)
0309                 regs->last_break = 1;
0310         }
0311         current->thread.last_break = regs->last_break;
0312     }
0313 
0314     if (S390_lowcore.pgm_code & 0x0200) {
0315         /* transaction abort */
0316         current->thread.trap_tdb = S390_lowcore.pgm_tdb;
0317     }
0318 
0319     if (S390_lowcore.pgm_code & PGM_INT_CODE_PER) {
0320         if (user_mode(regs)) {
0321             struct per_event *ev = &current->thread.per_event;
0322 
0323             set_thread_flag(TIF_PER_TRAP);
0324             ev->address = S390_lowcore.per_address;
0325             ev->cause = S390_lowcore.per_code_combined;
0326             ev->paid = S390_lowcore.per_access_id;
0327         } else {
0328             /* PER event in kernel is kprobes */
0329             __arch_local_irq_ssm(regs->psw.mask & ~PSW_MASK_PER);
0330             do_per_trap(regs);
0331             goto out;
0332         }
0333     }
0334 
0335     if (!irqs_disabled_flags(regs->psw.mask))
0336         trace_hardirqs_on();
0337     __arch_local_irq_ssm(regs->psw.mask & ~PSW_MASK_PER);
0338 
0339     trapnr = regs->int_code & PGM_INT_CODE_MASK;
0340     if (trapnr)
0341         pgm_check_table[trapnr](regs);
0342 out:
0343     local_irq_disable();
0344     irqentry_exit(regs, state);
0345 }
0346 
0347 /*
0348  * The program check table contains exactly 128 (0x00-0x7f) entries. Each
0349  * line defines the function to be called corresponding to the program check
0350  * interruption code.
0351  */
0352 static void (*pgm_check_table[128])(struct pt_regs *regs) = {
0353     [0x00]      = default_trap_handler,
0354     [0x01]      = illegal_op,
0355     [0x02]      = privileged_op,
0356     [0x03]      = execute_exception,
0357     [0x04]      = do_protection_exception,
0358     [0x05]      = addressing_exception,
0359     [0x06]      = specification_exception,
0360     [0x07]      = data_exception,
0361     [0x08]      = overflow_exception,
0362     [0x09]      = divide_exception,
0363     [0x0a]      = overflow_exception,
0364     [0x0b]      = divide_exception,
0365     [0x0c]      = hfp_overflow_exception,
0366     [0x0d]      = hfp_underflow_exception,
0367     [0x0e]      = hfp_significance_exception,
0368     [0x0f]      = hfp_divide_exception,
0369     [0x10]      = do_dat_exception,
0370     [0x11]      = do_dat_exception,
0371     [0x12]      = translation_specification_exception,
0372     [0x13]      = special_op_exception,
0373     [0x14]      = default_trap_handler,
0374     [0x15]      = operand_exception,
0375     [0x16]      = default_trap_handler,
0376     [0x17]      = default_trap_handler,
0377     [0x18]      = transaction_exception,
0378     [0x19]      = default_trap_handler,
0379     [0x1a]      = default_trap_handler,
0380     [0x1b]      = vector_exception,
0381     [0x1c]      = space_switch_exception,
0382     [0x1d]      = hfp_sqrt_exception,
0383     [0x1e ... 0x37] = default_trap_handler,
0384     [0x38]      = do_dat_exception,
0385     [0x39]      = do_dat_exception,
0386     [0x3a]      = do_dat_exception,
0387     [0x3b]      = do_dat_exception,
0388     [0x3c]      = default_trap_handler,
0389     [0x3d]      = do_secure_storage_access,
0390     [0x3e]      = do_non_secure_storage_access,
0391     [0x3f]      = do_secure_storage_violation,
0392     [0x40]      = monitor_event_exception,
0393     [0x41 ... 0x7f] = default_trap_handler,
0394 };
0395 
0396 #define COND_TRAP(x) asm(           \
0397     ".weak " __stringify(x) "\n\t"      \
0398     ".set  " __stringify(x) ","     \
0399     __stringify(default_trap_handler))
0400 
0401 COND_TRAP(do_secure_storage_access);
0402 COND_TRAP(do_non_secure_storage_access);
0403 COND_TRAP(do_secure_storage_violation);