11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * linux/kernel/panic.c 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * Copyright (C) 1991, 1992 Linus Torvalds 51da177e4SLinus Torvalds */ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds /* 81da177e4SLinus Torvalds * This function is used through-out the kernel (including mm and fs) 91da177e4SLinus Torvalds * to indicate a major problem. 101da177e4SLinus Torvalds */ 111da177e4SLinus Torvalds #include <linux/module.h> 121da177e4SLinus Torvalds #include <linux/sched.h> 131da177e4SLinus Torvalds #include <linux/delay.h> 141da177e4SLinus Torvalds #include <linux/reboot.h> 151da177e4SLinus Torvalds #include <linux/notifier.h> 161da177e4SLinus Torvalds #include <linux/init.h> 171da177e4SLinus Torvalds #include <linux/sysrq.h> 181da177e4SLinus Torvalds #include <linux/interrupt.h> 191da177e4SLinus Torvalds #include <linux/nmi.h> 20dc009d92SEric W. Biederman #include <linux/kexec.h> 211da177e4SLinus Torvalds 221da177e4SLinus Torvalds int panic_on_oops; 231da177e4SLinus Torvalds int tainted; 24dd287796SAndrew Morton static int pause_on_oops; 25dd287796SAndrew Morton static int pause_on_oops_flag; 26dd287796SAndrew Morton static DEFINE_SPINLOCK(pause_on_oops_lock); 271da177e4SLinus Torvalds 28dd287796SAndrew Morton int panic_timeout; 291da177e4SLinus Torvalds 30e041c683SAlan Stern ATOMIC_NOTIFIER_HEAD(panic_notifier_list); 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds EXPORT_SYMBOL(panic_notifier_list); 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds static int __init panic_setup(char *str) 351da177e4SLinus Torvalds { 361da177e4SLinus Torvalds panic_timeout = simple_strtoul(str, NULL, 0); 371da177e4SLinus Torvalds return 1; 381da177e4SLinus Torvalds } 391da177e4SLinus Torvalds __setup("panic=", panic_setup); 401da177e4SLinus Torvalds 411da177e4SLinus Torvalds static long no_blink(long time) 421da177e4SLinus Torvalds { 431da177e4SLinus Torvalds return 0; 441da177e4SLinus Torvalds } 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* Returns how long it waited in ms */ 471da177e4SLinus Torvalds long (*panic_blink)(long time); 481da177e4SLinus Torvalds EXPORT_SYMBOL(panic_blink); 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds /** 511da177e4SLinus Torvalds * panic - halt the system 521da177e4SLinus Torvalds * @fmt: The text string to print 531da177e4SLinus Torvalds * 541da177e4SLinus Torvalds * Display a message, then perform cleanups. 551da177e4SLinus Torvalds * 561da177e4SLinus Torvalds * This function never returns. 571da177e4SLinus Torvalds */ 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds NORET_TYPE void panic(const char * fmt, ...) 601da177e4SLinus Torvalds { 611da177e4SLinus Torvalds long i; 621da177e4SLinus Torvalds static char buf[1024]; 631da177e4SLinus Torvalds va_list args; 64347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390) 651da177e4SLinus Torvalds unsigned long caller = (unsigned long) __builtin_return_address(0); 661da177e4SLinus Torvalds #endif 671da177e4SLinus Torvalds 68dc009d92SEric W. Biederman /* 69dc009d92SEric W. Biederman * It's possible to come here directly from a panic-assertion and not 70dc009d92SEric W. Biederman * have preempt disabled. Some functions called from here want 71dc009d92SEric W. Biederman * preempt to be disabled. No point enabling it later though... 72dc009d92SEric W. Biederman */ 73dc009d92SEric W. Biederman preempt_disable(); 74dc009d92SEric W. Biederman 751da177e4SLinus Torvalds bust_spinlocks(1); 761da177e4SLinus Torvalds va_start(args, fmt); 771da177e4SLinus Torvalds vsnprintf(buf, sizeof(buf), fmt, args); 781da177e4SLinus Torvalds va_end(args); 791da177e4SLinus Torvalds printk(KERN_EMERG "Kernel panic - not syncing: %s\n",buf); 801da177e4SLinus Torvalds bust_spinlocks(0); 811da177e4SLinus Torvalds 82dc009d92SEric W. Biederman /* 83dc009d92SEric W. Biederman * If we have crashed and we have a crash kernel loaded let it handle 84dc009d92SEric W. Biederman * everything else. 85dc009d92SEric W. Biederman * Do we want to call this before we try to display a message? 86dc009d92SEric W. Biederman */ 876e274d14SAlexander Nyberg crash_kexec(NULL); 88dc009d92SEric W. Biederman 891da177e4SLinus Torvalds #ifdef CONFIG_SMP 90dc009d92SEric W. Biederman /* 91dc009d92SEric W. Biederman * Note smp_send_stop is the usual smp shutdown function, which 92dc009d92SEric W. Biederman * unfortunately means it may not be hardened to work in a panic 93dc009d92SEric W. Biederman * situation. 94dc009d92SEric W. Biederman */ 951da177e4SLinus Torvalds smp_send_stop(); 961da177e4SLinus Torvalds #endif 971da177e4SLinus Torvalds 98e041c683SAlan Stern atomic_notifier_call_chain(&panic_notifier_list, 0, buf); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds if (!panic_blink) 1011da177e4SLinus Torvalds panic_blink = no_blink; 1021da177e4SLinus Torvalds 103dc009d92SEric W. Biederman if (panic_timeout > 0) { 1041da177e4SLinus Torvalds /* 1051da177e4SLinus Torvalds * Delay timeout seconds before rebooting the machine. 1061da177e4SLinus Torvalds * We can't use the "normal" timers since we just panicked.. 1071da177e4SLinus Torvalds */ 1081da177e4SLinus Torvalds printk(KERN_EMERG "Rebooting in %d seconds..",panic_timeout); 1091da177e4SLinus Torvalds for (i = 0; i < panic_timeout*1000; ) { 1101da177e4SLinus Torvalds touch_nmi_watchdog(); 1111da177e4SLinus Torvalds i += panic_blink(i); 1121da177e4SLinus Torvalds mdelay(1); 1131da177e4SLinus Torvalds i++; 1141da177e4SLinus Torvalds } 1152f048ea8SEric W. Biederman /* This will not be a clean reboot, with everything 1162f048ea8SEric W. Biederman * shutting down. But if there is a chance of 1172f048ea8SEric W. Biederman * rebooting the system it will be rebooted. 1181da177e4SLinus Torvalds */ 1192f048ea8SEric W. Biederman emergency_restart(); 1201da177e4SLinus Torvalds } 1211da177e4SLinus Torvalds #ifdef __sparc__ 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds extern int stop_a_enabled; 124a271c241STom 'spot' Callaway /* Make sure the user can actually press Stop-A (L1-A) */ 1251da177e4SLinus Torvalds stop_a_enabled = 1; 126a271c241STom 'spot' Callaway printk(KERN_EMERG "Press Stop-A (L1-A) to return to the boot prom\n"); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds #endif 129347a8dc3SMartin Schwidefsky #if defined(CONFIG_S390) 1301da177e4SLinus Torvalds disabled_wait(caller); 1311da177e4SLinus Torvalds #endif 1321da177e4SLinus Torvalds local_irq_enable(); 1331da177e4SLinus Torvalds for (i = 0;;) { 134c22db941SJan Beulich touch_softlockup_watchdog(); 1351da177e4SLinus Torvalds i += panic_blink(i); 1361da177e4SLinus Torvalds mdelay(1); 1371da177e4SLinus Torvalds i++; 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds } 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds EXPORT_SYMBOL(panic); 1421da177e4SLinus Torvalds 1431da177e4SLinus Torvalds /** 1441da177e4SLinus Torvalds * print_tainted - return a string to represent the kernel taint state. 1451da177e4SLinus Torvalds * 1461da177e4SLinus Torvalds * 'P' - Proprietary module has been loaded. 1471da177e4SLinus Torvalds * 'F' - Module has been forcibly loaded. 1481da177e4SLinus Torvalds * 'S' - SMP with CPUs not designed for SMP. 1491da177e4SLinus Torvalds * 'R' - User forced a module unload. 1501da177e4SLinus Torvalds * 'M' - Machine had a machine check experience. 1511da177e4SLinus Torvalds * 'B' - System has hit bad_page. 1521da177e4SLinus Torvalds * 1531da177e4SLinus Torvalds * The string is overwritten by the next call to print_taint(). 1541da177e4SLinus Torvalds */ 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds const char *print_tainted(void) 1571da177e4SLinus Torvalds { 1581da177e4SLinus Torvalds static char buf[20]; 1591da177e4SLinus Torvalds if (tainted) { 1601da177e4SLinus Torvalds snprintf(buf, sizeof(buf), "Tainted: %c%c%c%c%c%c", 1611da177e4SLinus Torvalds tainted & TAINT_PROPRIETARY_MODULE ? 'P' : 'G', 1621da177e4SLinus Torvalds tainted & TAINT_FORCED_MODULE ? 'F' : ' ', 1631da177e4SLinus Torvalds tainted & TAINT_UNSAFE_SMP ? 'S' : ' ', 1641da177e4SLinus Torvalds tainted & TAINT_FORCED_RMMOD ? 'R' : ' ', 1651da177e4SLinus Torvalds tainted & TAINT_MACHINE_CHECK ? 'M' : ' ', 1661da177e4SLinus Torvalds tainted & TAINT_BAD_PAGE ? 'B' : ' '); 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds else 1691da177e4SLinus Torvalds snprintf(buf, sizeof(buf), "Not tainted"); 1701da177e4SLinus Torvalds return(buf); 1711da177e4SLinus Torvalds } 1721da177e4SLinus Torvalds 1731da177e4SLinus Torvalds void add_taint(unsigned flag) 1741da177e4SLinus Torvalds { 175*2c16e9c8SArjan van de Ven debug_locks_off(); /* can't trust the integrity of the kernel anymore */ 1761da177e4SLinus Torvalds tainted |= flag; 1771da177e4SLinus Torvalds } 1781da177e4SLinus Torvalds EXPORT_SYMBOL(add_taint); 179dd287796SAndrew Morton 180dd287796SAndrew Morton static int __init pause_on_oops_setup(char *str) 181dd287796SAndrew Morton { 182dd287796SAndrew Morton pause_on_oops = simple_strtoul(str, NULL, 0); 183dd287796SAndrew Morton return 1; 184dd287796SAndrew Morton } 185dd287796SAndrew Morton __setup("pause_on_oops=", pause_on_oops_setup); 186dd287796SAndrew Morton 187dd287796SAndrew Morton static void spin_msec(int msecs) 188dd287796SAndrew Morton { 189dd287796SAndrew Morton int i; 190dd287796SAndrew Morton 191dd287796SAndrew Morton for (i = 0; i < msecs; i++) { 192dd287796SAndrew Morton touch_nmi_watchdog(); 193dd287796SAndrew Morton mdelay(1); 194dd287796SAndrew Morton } 195dd287796SAndrew Morton } 196dd287796SAndrew Morton 197dd287796SAndrew Morton /* 198dd287796SAndrew Morton * It just happens that oops_enter() and oops_exit() are identically 199dd287796SAndrew Morton * implemented... 200dd287796SAndrew Morton */ 201dd287796SAndrew Morton static void do_oops_enter_exit(void) 202dd287796SAndrew Morton { 203dd287796SAndrew Morton unsigned long flags; 204dd287796SAndrew Morton static int spin_counter; 205dd287796SAndrew Morton 206dd287796SAndrew Morton if (!pause_on_oops) 207dd287796SAndrew Morton return; 208dd287796SAndrew Morton 209dd287796SAndrew Morton spin_lock_irqsave(&pause_on_oops_lock, flags); 210dd287796SAndrew Morton if (pause_on_oops_flag == 0) { 211dd287796SAndrew Morton /* This CPU may now print the oops message */ 212dd287796SAndrew Morton pause_on_oops_flag = 1; 213dd287796SAndrew Morton } else { 214dd287796SAndrew Morton /* We need to stall this CPU */ 215dd287796SAndrew Morton if (!spin_counter) { 216dd287796SAndrew Morton /* This CPU gets to do the counting */ 217dd287796SAndrew Morton spin_counter = pause_on_oops; 218dd287796SAndrew Morton do { 219dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 220dd287796SAndrew Morton spin_msec(MSEC_PER_SEC); 221dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 222dd287796SAndrew Morton } while (--spin_counter); 223dd287796SAndrew Morton pause_on_oops_flag = 0; 224dd287796SAndrew Morton } else { 225dd287796SAndrew Morton /* This CPU waits for a different one */ 226dd287796SAndrew Morton while (spin_counter) { 227dd287796SAndrew Morton spin_unlock(&pause_on_oops_lock); 228dd287796SAndrew Morton spin_msec(1); 229dd287796SAndrew Morton spin_lock(&pause_on_oops_lock); 230dd287796SAndrew Morton } 231dd287796SAndrew Morton } 232dd287796SAndrew Morton } 233dd287796SAndrew Morton spin_unlock_irqrestore(&pause_on_oops_lock, flags); 234dd287796SAndrew Morton } 235dd287796SAndrew Morton 236dd287796SAndrew Morton /* 237dd287796SAndrew Morton * Return true if the calling CPU is allowed to print oops-related info. This 238dd287796SAndrew Morton * is a bit racy.. 239dd287796SAndrew Morton */ 240dd287796SAndrew Morton int oops_may_print(void) 241dd287796SAndrew Morton { 242dd287796SAndrew Morton return pause_on_oops_flag == 0; 243dd287796SAndrew Morton } 244dd287796SAndrew Morton 245dd287796SAndrew Morton /* 246dd287796SAndrew Morton * Called when the architecture enters its oops handler, before it prints 247dd287796SAndrew Morton * anything. If this is the first CPU to oops, and it's oopsing the first time 248dd287796SAndrew Morton * then let it proceed. 249dd287796SAndrew Morton * 250dd287796SAndrew Morton * This is all enabled by the pause_on_oops kernel boot option. We do all this 251dd287796SAndrew Morton * to ensure that oopses don't scroll off the screen. It has the side-effect 252dd287796SAndrew Morton * of preventing later-oopsing CPUs from mucking up the display, too. 253dd287796SAndrew Morton * 254dd287796SAndrew Morton * It turns out that the CPU which is allowed to print ends up pausing for the 255dd287796SAndrew Morton * right duration, whereas all the other CPUs pause for twice as long: once in 256dd287796SAndrew Morton * oops_enter(), once in oops_exit(). 257dd287796SAndrew Morton */ 258dd287796SAndrew Morton void oops_enter(void) 259dd287796SAndrew Morton { 260*2c16e9c8SArjan van de Ven debug_locks_off(); /* can't trust the integrity of the kernel anymore */ 261dd287796SAndrew Morton do_oops_enter_exit(); 262dd287796SAndrew Morton } 263dd287796SAndrew Morton 264dd287796SAndrew Morton /* 265dd287796SAndrew Morton * Called when the architecture exits its oops handler, after printing 266dd287796SAndrew Morton * everything. 267dd287796SAndrew Morton */ 268dd287796SAndrew Morton void oops_exit(void) 269dd287796SAndrew Morton { 270dd287796SAndrew Morton do_oops_enter_exit(); 271dd287796SAndrew Morton } 272