/* * Process an internal exception (non maskable) */ static int process_iexcept(struct pt_regs *regs) { unsigned int iexcept_report = get_iexcept(); unsigned int iexcept_num; ack_exception(EXCEPT_TYPE_IXF); pr_err("IEXCEPT: PC[0x%lx]\n", regs->pc); while (iexcept_report) { iexcept_num = __ffs(iexcept_report); iexcept_report &= ~(1 << iexcept_num); set_iexcept(iexcept_report); if (*(unsigned int *)regs->pc == BKPT_OPCODE) { /* This is a breakpoint */ struct exception_info bkpt_exception = { "Oops - undefined instruction", SIGTRAP, TRAP_BRKPT }; do_trap(&bkpt_exception, regs); iexcept_report &= ~(0xFF); set_iexcept(iexcept_report); continue; } do_trap(&iexcept_table[iexcept_num], regs); } return 0; }
void vector_exception(struct pt_regs *regs) { int si_code, vic; if (!MACHINE_HAS_VX) { do_trap(regs, SIGILL, ILL_ILLOPN, "illegal operation"); return; } /* get vector interrupt code from fpc */ save_fpu_regs(); vic = (current->thread.fpu.fpc & 0xf00) >> 8; switch (vic) { case 1: /* invalid vector operation */ si_code = FPE_FLTINV; break; case 2: /* division by zero */ si_code = FPE_FLTDIV; break; case 3: /* overflow */ si_code = FPE_FLTOVF; break; case 4: /* underflow */ si_code = FPE_FLTUND; break; case 5: /* inexact */ si_code = FPE_FLTRES; break; default: /* unknown cause */ si_code = 0; } do_trap(regs, SIGFPE, si_code, "vector exception"); }
asmlinkage void __kprobes do_int3(struct pt_regs * regs, long error_code) { if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) { return; } do_trap(3, SIGTRAP, "int3", regs, error_code, NULL); return; }
int main() { do_rect(); do_square(); do_tri(); do_circle(); do_trap(); return 0; }
/* Runs on IST stack */ dotraplinkage void do_stack_segment(struct pt_regs *regs, long error_code) { if (notify_die(DIE_TRAP, "stack segment", regs, error_code, 12, SIGBUS) == NOTIFY_STOP) return; preempt_conditional_sti(regs); do_trap(12, SIGBUS, "stack segment", regs, error_code, NULL); preempt_conditional_cli(regs); }
/* * Process an external exception (maskable) */ static void process_eexcept(struct pt_regs *regs) { int evt; pr_err("EEXCEPT: PC[0x%lx]\n", regs->pc); while ((evt = soc_get_exception()) >= 0) do_trap(&eexcept_table[evt], regs); ack_exception(EXCEPT_TYPE_EXC); }
dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code) { siginfo_t info; local_irq_enable(); info.si_signo = SIGILL; info.si_errno = 0; info.si_code = ILL_BADSTK; info.si_addr = 0; if (notify_die(DIE_TRAP, "iret exception", regs, error_code, 32, SIGILL) == NOTIFY_STOP) return; do_trap(32, SIGILL, "iret exception", regs, error_code, &info); }
/* May run on IST stack. */ dotraplinkage void __kprobes do_int3(struct pt_regs *regs, long error_code) { #ifdef CONFIG_KPROBES if (notify_die(DIE_INT3, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) return; #else if (notify_die(DIE_TRAP, "int3", regs, error_code, 3, SIGTRAP) == NOTIFY_STOP) return; #endif preempt_conditional_sti(regs); do_trap(3, SIGTRAP, "int3", regs, error_code, NULL); preempt_conditional_cli(regs); }
void float_down() { trap_t *trap; message("You float gently to the ground."); trap = trap_at(you.ux, you.uy); if (trap) switch(get_trap_type(trap->trap_info)) { case PIERC: break; case TRAPDOOR: if (!xdnstair || you.ustuck) break; /* fall into next case */ default: do_trap(trap); } pickup(true); }
static inline void do_fp_trap(struct pt_regs *regs, __u32 fpc) { int si_code = 0; /* FPC[2] is Data Exception Code */ if ((fpc & 0x00000300) == 0) { /* bits 6 and 7 of DXC are 0 iff IEEE exception */ if (fpc & 0x8000) /* invalid fp operation */ si_code = FPE_FLTINV; else if (fpc & 0x4000) /* div by 0 */ si_code = FPE_FLTDIV; else if (fpc & 0x2000) /* overflow */ si_code = FPE_FLTOVF; else if (fpc & 0x1000) /* underflow */ si_code = FPE_FLTUND; else if (fpc & 0x0800) /* inexact */ si_code = FPE_FLTRES; } do_trap(regs, SIGFPE, si_code, "floating point exception"); }
void illegal_op(struct pt_regs *regs) { siginfo_t info; __u8 opcode[6]; __u16 __user *location; int is_uprobe_insn = 0; int signal = 0; location = get_trap_ip(regs); if (user_mode(regs)) { if (get_user(*((__u16 *) opcode), (__u16 __user *) location)) return; if (*((__u16 *) opcode) == S390_BREAKPOINT_U16) { if (current->ptrace) { info.si_signo = SIGTRAP; info.si_errno = 0; info.si_code = TRAP_BRKPT; info.si_addr = location; force_sig_info(SIGTRAP, &info, current); } else signal = SIGILL; #ifdef CONFIG_UPROBES } else if (*((__u16 *) opcode) == UPROBE_SWBP_INSN) { is_uprobe_insn = 1; #endif } else signal = SIGILL; } /* * We got either an illegal op in kernel mode, or user space trapped * on a uprobes illegal instruction. See if kprobes or uprobes picks * it up. If not, SIGILL. */ if (is_uprobe_insn || !user_mode(regs)) { if (notify_die(DIE_BPT, "bpt", regs, 0, 3, SIGTRAP) != NOTIFY_STOP) signal = SIGILL; } if (signal) do_trap(regs, signal, ILL_ILLOPC, "illegal operation"); }
/* * The fixup code for errors in iret jumps to here (iret_exc). It loses * the original trap number and erorr code. The bogus trap 32 and error * code 0 are what the vanilla kernel delivers via: * DO_ERROR_INFO(32, SIGSEGV, "iret exception", iret_error, ILL_BADSTK, 0, 1) * * NOTE: Because of the final "1" in the macro we need to enable interrupts. * * In case of a general protection fault in the iret instruction, we * need to check for a lazy CS update for exec-shield. */ dotraplinkage void do_iret_error(struct pt_regs *regs, long error_code) { int ok; int cpu; local_irq_enable(); cpu = get_cpu(); ok = check_lazy_exec_limit(cpu, regs, error_code); put_cpu(); if (!ok && notify_die(DIE_TRAP, "iret exception", regs, error_code, 32, SIGSEGV) != NOTIFY_STOP) { siginfo_t info; info.si_signo = SIGSEGV; info.si_errno = 0; info.si_code = ILL_BADSTK; info.si_addr = 0; do_trap(32, SIGSEGV, "iret exception", regs, error_code, &info); } }
/* * Main exception processing */ asmlinkage int process_exception(struct pt_regs *regs) { unsigned int type; unsigned int type_num; unsigned int ie_num = 9; /* default is unknown exception */ while ((type = get_except_type()) != 0) { type_num = fls(type) - 1; switch (type_num) { case EXCEPT_TYPE_NXF: ack_exception(EXCEPT_TYPE_NXF); if (c6x_nmi_handler) (c6x_nmi_handler)(regs); else pr_alert("NMI interrupt!\n"); break; case EXCEPT_TYPE_IXF: if (process_iexcept(regs)) return 1; break; case EXCEPT_TYPE_EXC: process_eexcept(regs); break; case EXCEPT_TYPE_SXF: ie_num = 8; default: ack_exception(type_num); do_trap(&iexcept_table[ie_num], regs); break; } } return 0; }
/* * This routine handles page faults. It determines the address and the * problem, and then passes it off to one of the appropriate routines. */ asmlinkage void do_page_fault(struct pt_regs *regs) { struct task_struct *tsk; struct vm_area_struct *vma; struct mm_struct *mm; unsigned long addr, cause; unsigned int flags = FAULT_FLAG_ALLOW_RETRY | FAULT_FLAG_KILLABLE; int fault, code = SEGV_MAPERR; cause = regs->scause; addr = regs->sbadaddr; tsk = current; mm = tsk->mm; /* * Fault-in kernel-space virtual memory on-demand. * The 'reference' page table is init_mm.pgd. * * NOTE! We MUST NOT take any locks for this case. We may * be in an interrupt or a critical region, and should * only copy the information from the master page table, * nothing more. */ if (unlikely((addr >= VMALLOC_START) && (addr <= VMALLOC_END))) goto vmalloc_fault; /* Enable interrupts if they were enabled in the parent context. */ if (likely(regs->sstatus & SR_PIE)) local_irq_enable(); /* * If we're in an interrupt, have no user context, or are running * in an atomic region, then we must not take the fault. */ if (unlikely(in_atomic() || !mm)) goto no_context; if (user_mode(regs)) flags |= FAULT_FLAG_USER; perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS, 1, regs, addr); retry: down_read(&mm->mmap_sem); vma = find_vma(mm, addr); if (unlikely(!vma)) goto bad_area; if (likely(vma->vm_start <= addr)) goto good_area; if (unlikely(!(vma->vm_flags & VM_GROWSDOWN))) goto bad_area; if (unlikely(expand_stack(vma, addr))) goto bad_area; /* * Ok, we have a good vm_area for this memory access, so * we can handle it. */ good_area: code = SEGV_ACCERR; switch (cause) { case EXC_INST_ACCESS: if (!(vma->vm_flags & VM_EXEC)) goto bad_area; break; case EXC_LOAD_ACCESS: if (!(vma->vm_flags & VM_READ)) goto bad_area; break; case EXC_STORE_ACCESS: if (!(vma->vm_flags & VM_WRITE)) goto bad_area; flags |= FAULT_FLAG_WRITE; break; default: panic("%s: unhandled cause %lu", __func__, cause); } /* * If for any reason at all we could not handle the fault, * make sure we exit gracefully rather than endlessly redo * the fault. */ fault = handle_mm_fault(mm, vma, addr, flags); /* * If we need to retry but a fatal signal is pending, handle the * signal first. We do not need to release the mmap_sem because it * would already be released in __lock_page_or_retry in mm/filemap.c. */ if ((fault & VM_FAULT_RETRY) && fatal_signal_pending(tsk)) return; if (unlikely(fault & VM_FAULT_ERROR)) { if (fault & VM_FAULT_OOM) goto out_of_memory; else if (fault & VM_FAULT_SIGBUS) goto do_sigbus; BUG(); } /* * Major/minor page fault accounting is only done on the * initial attempt. If we go through a retry, it is extremely * likely that the page will be found in page cache at that point. */ if (flags & FAULT_FLAG_ALLOW_RETRY) { if (fault & VM_FAULT_MAJOR) { tsk->maj_flt++; perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MAJ, 1, regs, addr); } else { tsk->min_flt++; perf_sw_event(PERF_COUNT_SW_PAGE_FAULTS_MIN, 1, regs, addr); } if (fault & VM_FAULT_RETRY) { /* * Clear FAULT_FLAG_ALLOW_RETRY to avoid any risk * of starvation. */ flags &= ~(FAULT_FLAG_ALLOW_RETRY); flags |= FAULT_FLAG_TRIED; /* * No need to up_read(&mm->mmap_sem) as we would * have already released it in __lock_page_or_retry * in mm/filemap.c. */ goto retry; } } up_read(&mm->mmap_sem); return; /* * Something tried to access memory that isn't in our memory map. * Fix it, but check if it's kernel or user first. */ bad_area: up_read(&mm->mmap_sem); /* User mode accesses just cause a SIGSEGV */ if (user_mode(regs)) { do_trap(regs, SIGSEGV, code, addr, tsk); return; } no_context: /* Are we prepared to handle this kernel fault? */ if (fixup_exception(regs)) { return; } /* * Oops. The kernel tried to access some bad page. We'll have to * terminate things with extreme prejudice. */ bust_spinlocks(1); pr_alert("Unable to handle kernel %s at virtual address " REG_FMT "\n", (addr < PAGE_SIZE) ? "NULL pointer dereference" : "paging request", addr); die(regs, "Oops"); do_exit(SIGKILL); /* * We ran out of memory, call the OOM killer, and return the userspace * (which will retry the fault, or kill us if we got oom-killed). */ out_of_memory: up_read(&mm->mmap_sem); if (!user_mode(regs)) goto no_context; pagefault_out_of_memory(); return; do_sigbus: up_read(&mm->mmap_sem); /* Kernel mode? Handle exceptions or die */ if (!user_mode(regs)) goto no_context; do_trap(regs, SIGBUS, BUS_ADRERR, addr, tsk); return; vmalloc_fault: { pgd_t *pgd, *pgd_k; pud_t *pud, *pud_k; pmd_t *pmd, *pmd_k; pte_t *pte_k; int index; if (user_mode(regs)) goto bad_area; /* * Synchronize this task's top level page-table * with the 'reference' page table. * * Do _not_ use "tsk->active_mm->pgd" here. * We might be inside an interrupt in the middle * of a task switch. */ index = pgd_index(addr); pgd = (pgd_t *)pfn_to_virt(csr_read(sptbr)) + index; pgd_k = init_mm.pgd + index; if (!pgd_present(*pgd_k)) goto no_context; set_pgd(pgd, *pgd_k); pud = pud_offset(pgd, addr); pud_k = pud_offset(pgd_k, addr); if (!pud_present(*pud_k)) goto no_context; /* Since the vmalloc area is global, it is unnecessary to copy individual PTEs */ pmd = pmd_offset(pud, addr); pmd_k = pmd_offset(pud_k, addr); if (!pmd_present(*pmd_k)) goto no_context; set_pmd(pmd, *pmd_k); /* Make sure the actual PTE exists as well to * catch kernel vmalloc-area accesses to non-mapped * addresses. If we don't do this, this will just * silently loop forever. */ pte_k = pte_offset_kernel(pmd_k, addr); if (!pte_present(*pte_k)) goto no_context; return; } }