cregit-Linux how code gets into the kernel

Release 4.14 arch/xtensa/kernel/traps.c

/*
 * arch/xtensa/kernel/traps.c
 *
 * Exception handling.
 *
 * Derived from code with the following copyrights:
 * Copyright (C) 1994 - 1999 by Ralf Baechle
 * Modified for R3000 by Paul M. Antoine, 1995, 1996
 * Complete output from die() by Ulf Carlsson, 1998
 * Copyright (C) 1999 Silicon Graphics, Inc.
 *
 * Essentially rewritten for the Xtensa architecture port.
 *
 * Copyright (C) 2001 - 2013 Tensilica Inc.
 *
 * Joe Taylor   <joe@tensilica.com, joetylr@yahoo.com>
 * Chris Zankel <chris@zankel.net>
 * Marc Gauthier<marc@tensilica.com, marc@alumni.uwaterloo.ca>
 * Kevin Chea
 *
 * This file is subject to the terms and conditions of the GNU General Public
 * License.  See the file "COPYING" in the main directory of this archive
 * for more details.
 */

#include <linux/kernel.h>
#include <linux/sched/signal.h>
#include <linux/sched/debug.h>
#include <linux/sched/task_stack.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/stringify.h>
#include <linux/kallsyms.h>
#include <linux/delay.h>
#include <linux/hardirq.h>

#include <asm/stacktrace.h>
#include <asm/ptrace.h>
#include <asm/timex.h>
#include <linux/uaccess.h>
#include <asm/pgtable.h>
#include <asm/processor.h>
#include <asm/traps.h>
#include <asm/hw_breakpoint.h>

/*
 * Machine specific interrupt handlers
 */

extern void kernel_exception(void);
extern void user_exception(void);

extern void fast_syscall_kernel(void);
extern void fast_syscall_user(void);
extern void fast_alloca(void);
extern void fast_unaligned(void);
extern void fast_second_level_miss(void);
extern void fast_store_prohibited(void);
extern void fast_coprocessor(void);

extern void do_illegal_instruction (struct pt_regs*);
extern void do_interrupt (struct pt_regs*);
extern void do_nmi(struct pt_regs *);
extern void do_unaligned_user (struct pt_regs*);
extern void do_multihit (struct pt_regs*, unsigned long);
extern void do_page_fault (struct pt_regs*, unsigned long);
extern void do_debug (struct pt_regs*);
extern void system_call (struct pt_regs*);

/*
 * The vector table must be preceded by a save area (which
 * implies it must be in RAM, unless one places RAM immediately
 * before a ROM and puts the vector at the start of the ROM (!))
 */


#define KRNL		0x01

#define USER		0x02


#define COPROCESSOR(x)							\
{ EXCCAUSE_COPROCESSOR ## x ## _DISABLED, USER, fast_coprocessor }

typedef struct {
	
int cause;
	
int fast;
	
void* handler;

} dispatch_init_table_t;


static dispatch_init_table_t __initdata dispatch_init_table[] = {

{ EXCCAUSE_ILLEGAL_INSTRUCTION,	0,	   do_illegal_instruction},
{ EXCCAUSE_SYSTEM_CALL,		KRNL,	   fast_syscall_kernel },
{ EXCCAUSE_SYSTEM_CALL,		USER,	   fast_syscall_user },
{ EXCCAUSE_SYSTEM_CALL,		0,	   system_call },
/* EXCCAUSE_INSTRUCTION_FETCH unhandled */
/* EXCCAUSE_LOAD_STORE_ERROR unhandled*/
{ EXCCAUSE_LEVEL1_INTERRUPT,	0,	   do_interrupt },
{ EXCCAUSE_ALLOCA,		USER|KRNL, fast_alloca },
/* EXCCAUSE_INTEGER_DIVIDE_BY_ZERO unhandled */
/* EXCCAUSE_PRIVILEGED unhandled */
#if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
#ifdef CONFIG_XTENSA_UNALIGNED_USER
{ EXCCAUSE_UNALIGNED,		USER,	   fast_unaligned },
#endif
{ EXCCAUSE_UNALIGNED,		0,	   do_unaligned_user },
{ EXCCAUSE_UNALIGNED,		KRNL,	   fast_unaligned },
#endif
#ifdef CONFIG_MMU
{ EXCCAUSE_ITLB_MISS,		0,	   do_page_fault },
{ EXCCAUSE_ITLB_MISS,		USER|KRNL, fast_second_level_miss},
{ EXCCAUSE_ITLB_MULTIHIT,		0,	   do_multihit },
{ EXCCAUSE_ITLB_PRIVILEGE,	0,	   do_page_fault },
/* EXCCAUSE_SIZE_RESTRICTION unhandled */
{ EXCCAUSE_FETCH_CACHE_ATTRIBUTE,	0,	   do_page_fault },
{ EXCCAUSE_DTLB_MISS,		USER|KRNL, fast_second_level_miss},
{ EXCCAUSE_DTLB_MISS,		0,	   do_page_fault },
{ EXCCAUSE_DTLB_MULTIHIT,		0,	   do_multihit },
{ EXCCAUSE_DTLB_PRIVILEGE,	0,	   do_page_fault },
/* EXCCAUSE_DTLB_SIZE_RESTRICTION unhandled */
{ EXCCAUSE_STORE_CACHE_ATTRIBUTE,	USER|KRNL, fast_store_prohibited },
{ EXCCAUSE_STORE_CACHE_ATTRIBUTE,	0,	   do_page_fault },
{ EXCCAUSE_LOAD_CACHE_ATTRIBUTE,	0,	   do_page_fault },
#endif /* CONFIG_MMU */
/* XCCHAL_EXCCAUSE_FLOATING_POINT unhandled */
#if XTENSA_HAVE_COPROCESSOR(0)
COPROCESSOR(0),
#endif
#if XTENSA_HAVE_COPROCESSOR(1)
COPROCESSOR(1),
#endif
#if XTENSA_HAVE_COPROCESSOR(2)
COPROCESSOR(2),
#endif
#if XTENSA_HAVE_COPROCESSOR(3)
COPROCESSOR(3),
#endif
#if XTENSA_HAVE_COPROCESSOR(4)
COPROCESSOR(4),
#endif
#if XTENSA_HAVE_COPROCESSOR(5)
COPROCESSOR(5),
#endif
#if XTENSA_HAVE_COPROCESSOR(6)
COPROCESSOR(6),
#endif
#if XTENSA_HAVE_COPROCESSOR(7)
COPROCESSOR(7),
#endif
#if XTENSA_FAKE_NMI
{ EXCCAUSE_MAPPED_NMI,			0,		do_nmi },
#endif
{ EXCCAUSE_MAPPED_DEBUG,		0,		do_debug },
{ -1, -1, 0 }

};

/* The exception table <exc_table> serves two functions:
 * 1. it contains three dispatch tables (fast_user, fast_kernel, default-c)
 * 2. it is a temporary memory buffer for the exception handlers.
 */

DEFINE_PER_CPU(unsigned long, exc_table[EXC_TABLE_SIZE/4]);

DEFINE_PER_CPU(struct debug_table, debug_table);

void die(const char*, struct pt_regs*, long);


static inline void __die_if_kernel(const char *str, struct pt_regs *regs, long err) { if (!user_mode(regs)) die(str, regs, err); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel37100.00%1100.00%
Total37100.00%1100.00%

/* * Unhandled Exceptions. Kill user task or panic if in kernel space. */
void do_unhandled(struct pt_regs *regs, unsigned long exccause) { __die_if_kernel("Caught unhandled exception - should not happen", regs, SIGKILL); /* If in user mode, send SIGILL signal to current process */ printk("Caught unhandled exception in '%s' " "(pid = %d, pc = %#010lx) - should not happen\n" "\tEXCCAUSE is %ld\n", current->comm, task_pid_nr(current), regs->pc, exccause); force_sig(SIGILL, current); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel5094.34%150.00%
Alexey Dobriyan35.66%150.00%
Total53100.00%2100.00%

/* * Multi-hit exception. This if fatal! */
void do_multihit(struct pt_regs *regs, unsigned long exccause) { die("Caught multihit exception", regs, SIGKILL); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel23100.00%1100.00%
Total23100.00%1100.00%

/* * IRQ handler. */ extern void do_IRQ(int, struct pt_regs *); #if XTENSA_FAKE_NMI #define IS_POW2(v) (((v) & ((v) - 1)) == 0) #if !(PROFILING_INTLEVEL == XCHAL_EXCM_LEVEL && \ IS_POW2(XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL))) #warning "Fake NMI is requested for PMM, but there are other IRQs at or above its level." #warning "Fake NMI will be used, but there will be a bugcheck if one of those IRQs fire."
static inline void check_valid_nmi(void) { unsigned intread = get_sr(interrupt); unsigned intenable = get_sr(intenable); BUG_ON(intread & intenable & ~(XTENSA_INTLEVEL_ANDBELOW_MASK(PROFILING_INTLEVEL) ^ XTENSA_INTLEVEL_MASK(PROFILING_INTLEVEL) ^ BIT(XCHAL_PROFILING_INTERRUPT))); }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov50100.00%1100.00%
Total50100.00%1100.00%

#else
static inline void check_valid_nmi(void) { }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov8100.00%1100.00%
Total8100.00%1100.00%

#endif irqreturn_t xtensa_pmu_irq_handler(int irq, void *dev_id); DEFINE_PER_CPU(unsigned long, nmi_count);
void do_nmi(struct pt_regs *regs) { struct pt_regs *old_regs; if ((regs->ps & PS_INTLEVEL_MASK) < LOCKLEVEL) trace_hardirqs_off(); old_regs = set_irq_regs(regs); nmi_enter(); ++*this_cpu_ptr(&nmi_count); check_valid_nmi(); xtensa_pmu_irq_handler(0, NULL); nmi_exit(); set_irq_regs(old_regs); }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov66100.00%2100.00%
Total66100.00%2100.00%

#endif
void do_interrupt(struct pt_regs *regs) { static const unsigned int_level_mask[] = { 0, XCHAL_INTLEVEL1_MASK, XCHAL_INTLEVEL2_MASK, XCHAL_INTLEVEL3_MASK, XCHAL_INTLEVEL4_MASK, XCHAL_INTLEVEL5_MASK, XCHAL_INTLEVEL6_MASK, XCHAL_INTLEVEL7_MASK, }; struct pt_regs *old_regs; trace_hardirqs_off(); old_regs = set_irq_regs(regs); irq_enter(); for (;;) { unsigned intread = get_sr(interrupt); unsigned intenable = get_sr(intenable); unsigned int_at_level = intread & intenable; unsigned level; for (level = LOCKLEVEL; level > 0; --level) { if (int_at_level & int_level_mask[level]) { int_at_level &= int_level_mask[level]; break; } } if (level == 0) break; do_IRQ(__ffs(int_at_level), regs); } irq_exit(); set_irq_regs(old_regs); }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov6645.83%466.67%
Marc Gauthier4833.33%116.67%
Chris Zankel3020.83%116.67%
Total144100.00%6100.00%

/* * Illegal instruction. Fatal if in kernel space. */
void do_illegal_instruction(struct pt_regs *regs) { __die_if_kernel("Illegal instruction in kernel", regs, SIGKILL); /* If in user mode, send SIGILL signal to current process. */ printk("Illegal Instruction in '%s' (pid = %d, pc = %#010lx)\n", current->comm, task_pid_nr(current), regs->pc); force_sig(SIGILL, current); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel4293.33%150.00%
Alexey Dobriyan36.67%150.00%
Total45100.00%2100.00%

/* * Handle unaligned memory accesses from user space. Kill task. * * If CONFIG_UNALIGNED_USER is not set, we don't allow unaligned memory * accesses causes from user space. */ #if XCHAL_UNALIGNED_LOAD_EXCEPTION || XCHAL_UNALIGNED_STORE_EXCEPTION
void do_unaligned_user (struct pt_regs *regs) { siginfo_t info; __die_if_kernel("Unhandled unaligned exception in kernel", regs, SIGKILL); current->thread.bad_vaddr = regs->excvaddr; current->thread.error_code = -3; printk("Unaligned memory access to %08lx in '%s' " "(pid = %d, pc = %#010lx)\n", regs->excvaddr, current->comm, task_pid_nr(current), regs->pc); info.si_signo = SIGBUS; info.si_errno = 0; info.si_code = BUS_ADRALN; info.si_addr = (void *) regs->excvaddr; force_sig_info(SIGSEGV, &info, current); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel10197.12%150.00%
Alexey Dobriyan32.88%150.00%
Total104100.00%2100.00%

#endif /* Handle debug events. * When CONFIG_HAVE_HW_BREAKPOINT is on this handler is called with * preemption disabled to avoid rescheduling and keep mapping of hardware * breakpoint structures to debug registers intact, so that * DEBUGCAUSE.DBNUM could be used in case of data breakpoint hit. */
void do_debug(struct pt_regs *regs) { #ifdef CONFIG_HAVE_HW_BREAKPOINT int ret = check_hw_breakpoint(regs); preempt_enable(); if (ret == 0) return; #endif __die_if_kernel("Breakpoint in kernel", regs, SIGKILL); /* If in user mode, send SIGTRAP signal to current process */ force_sig(SIGTRAP, current); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel2754.00%150.00%
Max Filippov2346.00%150.00%
Total50100.00%2100.00%


static void set_handler(int idx, void *handler) { unsigned int cpu; for_each_possible_cpu(cpu) per_cpu(exc_table, cpu)[idx] = (unsigned long)handler; }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov37100.00%1100.00%
Total37100.00%1100.00%

/* Set exception C handler - for temporary use when probing exceptions */
void * __init trap_set_handler(int cause, void *handler) { void *previous = (void *)per_cpu(exc_table, 0)[ EXC_TABLE_DEFAULT / 4 + cause]; set_handler(EXC_TABLE_DEFAULT / 4 + cause, handler); return previous; }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov50100.00%2100.00%
Total50100.00%2100.00%


static void trap_init_excsave(void) { unsigned long excsave1 = (unsigned long)this_cpu_ptr(exc_table); __asm__ __volatile__("wsr %0, excsave1\n" : : "a" (excsave1)); }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov24100.00%2100.00%
Total24100.00%2100.00%


static void trap_init_debug(void) { unsigned long debugsave = (unsigned long)this_cpu_ptr(&debug_table); this_cpu_ptr(&debug_table)->debug_exception = debug_exception; __asm__ __volatile__("wsr %0, excsave" __stringify(XCHAL_DEBUGLEVEL) :: "a"(debugsave)); }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov35100.00%1100.00%
Total35100.00%1100.00%

/* * Initialize dispatch tables. * * The exception vectors are stored compressed the __init section in the * dispatch_init_table. This function initializes the following three tables * from that compressed table: * - fast user first dispatch table for user exceptions * - fast kernel first dispatch table for kernel exceptions * - default C-handler C-handler called by the default fast handler. * * See vectors.S for more details. */
void __init trap_init(void) { int i; /* Setup default vectors. */ for(i = 0; i < 64; i++) { set_handler(EXC_TABLE_FAST_USER/4 + i, user_exception); set_handler(EXC_TABLE_FAST_KERNEL/4 + i, kernel_exception); set_handler(EXC_TABLE_DEFAULT/4 + i, do_unhandled); } /* Setup specific handlers. */ for(i = 0; dispatch_init_table[i].cause >= 0; i++) { int fast = dispatch_init_table[i].fast; int cause = dispatch_init_table[i].cause; void *handler = dispatch_init_table[i].handler; if (fast == 0) set_handler (EXC_TABLE_DEFAULT/4 + cause, handler); if (fast && fast & USER) set_handler (EXC_TABLE_FAST_USER/4 + cause, handler); if (fast && fast & KRNL) set_handler (EXC_TABLE_FAST_KERNEL/4 + cause, handler); } /* Initialize EXCSAVE_1 to hold the address of the exception table. */ trap_init_excsave(); trap_init_debug(); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel16997.13%250.00%
Max Filippov52.87%250.00%
Total174100.00%4100.00%

#ifdef CONFIG_SMP
void secondary_trap_init(void) { trap_init_excsave(); trap_init_debug(); }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov13100.00%2100.00%
Total13100.00%2100.00%

#endif /* * This function dumps the current valid window frame and other base registers. */
void show_regs(struct pt_regs * regs) { int i, wmask; show_regs_print_info(KERN_DEFAULT); wmask = regs->wmask & ~1; for (i = 0; i < 16; i++) { if ((i % 8) == 0) pr_info("a%02d:", i); pr_cont(" %08lx", regs->areg[i]); } pr_cont("\n"); pr_info("pc: %08lx, ps: %08lx, depc: %08lx, excvaddr: %08lx\n", regs->pc, regs->ps, regs->depc, regs->excvaddr); pr_info("lbeg: %08lx, lend: %08lx lcount: %08lx, sar: %08lx\n", regs->lbeg, regs->lend, regs->lcount, regs->sar); if (user_mode(regs)) pr_cont("wb: %08lx, ws: %08lx, wmask: %08lx, syscall: %ld\n", regs->windowbase, regs->windowstart, regs->wmask, regs->syscall); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel13591.22%240.00%
Max Filippov64.05%120.00%
Tejun Heo53.38%120.00%
Joe Perches21.35%120.00%
Total148100.00%5100.00%


static int show_trace_cb(struct stackframe *frame, void *data) { if (kernel_text_address(frame->pc)) pr_cont(" [<%08lx>] %pB\n", frame->pc, (void *)frame->pc); return 0; }

Contributors

PersonTokensPropCommitsCommitProp
Max Filippov3068.18%375.00%
Johannes Weiner1431.82%125.00%
Total44100.00%4100.00%


void show_trace(struct task_struct *task, unsigned long *sp) { if (!sp) sp = stack_pointer(task); pr_info("Call Trace:\n"); walk_stackframe(sp, show_trace_cb, NULL); #ifndef CONFIG_KALLSYMS pr_cont("\n"); #endif }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel2854.90%120.00%
Max Filippov1325.49%240.00%
Johannes Weiner1019.61%240.00%
Total51100.00%5100.00%

static int kstack_depth_to_print = 24;
void show_stack(struct task_struct *task, unsigned long *sp) { int i = 0; unsigned long *stack; if (!sp) sp = stack_pointer(task); stack = sp; pr_info("Stack:\n"); for (i = 0; i < kstack_depth_to_print; i++) { if (kstack_end(sp)) break; pr_cont(" %08lx", *sp++); if (i % 8 == 7) pr_cont("\n"); } show_trace(task, stack); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel7778.57%125.00%
Max Filippov1313.27%125.00%
Johannes Weiner88.16%250.00%
Total98100.00%4100.00%

DEFINE_SPINLOCK(die_lock);
void die(const char * str, struct pt_regs * regs, long err) { static int die_counter; console_verbose(); spin_lock_irq(&die_lock); pr_info("%s: sig: %ld [#%d]%s\n", str, err, ++die_counter, IS_ENABLED(CONFIG_PREEMPT) ? " PREEMPT" : ""); show_regs(regs); if (!user_mode(regs)) show_stack(NULL, (unsigned long*)regs->areg[1]); add_taint(TAINT_DIE, LOCKDEP_NOW_UNRELIABLE); spin_unlock_irq(&die_lock); if (in_interrupt()) panic("Fatal exception in interrupt"); if (panic_on_oops) panic("Fatal exception"); do_exit(err); }

Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel10285.71%120.00%
Max Filippov97.56%120.00%
Pavel Emelyanov54.20%120.00%
Rusty Russell21.68%120.00%
Simon Horman10.84%120.00%
Total119100.00%5100.00%


Overall Contributors

PersonTokensPropCommitsCommitProp
Chris Zankel138766.71%513.16%
Max Filippov56427.13%1642.11%
Marc Gauthier482.31%12.63%
Johannes Weiner381.83%37.89%
Alexey Dobriyan120.58%25.26%
Ingo Molnar110.53%410.53%
Tejun Heo50.24%12.63%
Pavel Emelyanov50.24%12.63%
Nishanth Aravamudan30.14%12.63%
Joe Perches20.10%12.63%
Rusty Russell20.10%12.63%
Simon Horman10.05%12.63%
Linus Torvalds10.05%12.63%
Total2079100.00%38100.00%
Information contained on this website is for historical information purposes only and does not indicate or represent copyright ownership.
Created with cregit.