sysrq.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. /* -*- linux-c -*-
  2. *
  3. * $Id: sysrq.c,v 1.15 1998/08/23 14:56:41 mj Exp $
  4. *
  5. * Linux Magic System Request Key Hacks
  6. *
  7. * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  8. * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
  9. *
  10. * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
  11. * overhauled to use key registration
  12. * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
  13. */
  14. #include <linux/sched.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/mm.h>
  17. #include <linux/fs.h>
  18. #include <linux/tty.h>
  19. #include <linux/mount.h>
  20. #include <linux/kdev_t.h>
  21. #include <linux/major.h>
  22. #include <linux/reboot.h>
  23. #include <linux/sysrq.h>
  24. #include <linux/kbd_kern.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/quotaops.h>
  27. #include <linux/perf_counter.h>
  28. #include <linux/kernel.h>
  29. #include <linux/module.h>
  30. #include <linux/suspend.h>
  31. #include <linux/writeback.h>
  32. #include <linux/buffer_head.h> /* for fsync_bdev() */
  33. #include <linux/swap.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/vt_kern.h>
  36. #include <linux/workqueue.h>
  37. #include <linux/kexec.h>
  38. #include <linux/hrtimer.h>
  39. #include <linux/oom.h>
  40. #include <asm/ptrace.h>
  41. #include <asm/irq_regs.h>
  42. /* Whether we react on sysrq keys or just ignore them */
  43. int __read_mostly __sysrq_enabled = 1;
  44. static int __read_mostly sysrq_always_enabled;
  45. int sysrq_on(void)
  46. {
  47. return __sysrq_enabled || sysrq_always_enabled;
  48. }
  49. /*
  50. * A value of 1 means 'all', other nonzero values are an op mask:
  51. */
  52. static inline int sysrq_on_mask(int mask)
  53. {
  54. return sysrq_always_enabled || __sysrq_enabled == 1 ||
  55. (__sysrq_enabled & mask);
  56. }
  57. static int __init sysrq_always_enabled_setup(char *str)
  58. {
  59. sysrq_always_enabled = 1;
  60. printk(KERN_INFO "debug: sysrq always enabled.\n");
  61. return 1;
  62. }
  63. __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
  64. static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
  65. {
  66. int i;
  67. i = key - '0';
  68. console_loglevel = 7;
  69. printk("Loglevel set to %d\n", i);
  70. console_loglevel = i;
  71. }
  72. static struct sysrq_key_op sysrq_loglevel_op = {
  73. .handler = sysrq_handle_loglevel,
  74. .help_msg = "loglevel(0-9)",
  75. .action_msg = "Changing Loglevel",
  76. .enable_mask = SYSRQ_ENABLE_LOG,
  77. };
  78. #ifdef CONFIG_VT
  79. static void sysrq_handle_SAK(int key, struct tty_struct *tty)
  80. {
  81. struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
  82. schedule_work(SAK_work);
  83. }
  84. static struct sysrq_key_op sysrq_SAK_op = {
  85. .handler = sysrq_handle_SAK,
  86. .help_msg = "saK",
  87. .action_msg = "SAK",
  88. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  89. };
  90. #else
  91. #define sysrq_SAK_op (*(struct sysrq_key_op *)0)
  92. #endif
  93. #ifdef CONFIG_VT
  94. static void sysrq_handle_unraw(int key, struct tty_struct *tty)
  95. {
  96. struct kbd_struct *kbd = &kbd_table[fg_console];
  97. if (kbd)
  98. kbd->kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
  99. }
  100. static struct sysrq_key_op sysrq_unraw_op = {
  101. .handler = sysrq_handle_unraw,
  102. .help_msg = "unRaw",
  103. .action_msg = "Keyboard mode set to system default",
  104. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  105. };
  106. #else
  107. #define sysrq_unraw_op (*(struct sysrq_key_op *)0)
  108. #endif /* CONFIG_VT */
  109. static void sysrq_handle_crash(int key, struct tty_struct *tty)
  110. {
  111. char *killer = NULL;
  112. *killer = 1;
  113. }
  114. static struct sysrq_key_op sysrq_crashdump_op = {
  115. .handler = sysrq_handle_crash,
  116. .help_msg = "Crash",
  117. .action_msg = "Trigger a crash",
  118. .enable_mask = SYSRQ_ENABLE_DUMP,
  119. };
  120. static void sysrq_handle_reboot(int key, struct tty_struct *tty)
  121. {
  122. lockdep_off();
  123. local_irq_enable();
  124. emergency_restart();
  125. }
  126. static struct sysrq_key_op sysrq_reboot_op = {
  127. .handler = sysrq_handle_reboot,
  128. .help_msg = "reBoot",
  129. .action_msg = "Resetting",
  130. .enable_mask = SYSRQ_ENABLE_BOOT,
  131. };
  132. static void sysrq_handle_sync(int key, struct tty_struct *tty)
  133. {
  134. emergency_sync();
  135. }
  136. static struct sysrq_key_op sysrq_sync_op = {
  137. .handler = sysrq_handle_sync,
  138. .help_msg = "Sync",
  139. .action_msg = "Emergency Sync",
  140. .enable_mask = SYSRQ_ENABLE_SYNC,
  141. };
  142. static void sysrq_handle_show_timers(int key, struct tty_struct *tty)
  143. {
  144. sysrq_timer_list_show();
  145. }
  146. static struct sysrq_key_op sysrq_show_timers_op = {
  147. .handler = sysrq_handle_show_timers,
  148. .help_msg = "show-all-timers(Q)",
  149. .action_msg = "Show clockevent devices & pending hrtimers (no others)",
  150. };
  151. static void sysrq_handle_mountro(int key, struct tty_struct *tty)
  152. {
  153. emergency_remount();
  154. }
  155. static struct sysrq_key_op sysrq_mountro_op = {
  156. .handler = sysrq_handle_mountro,
  157. .help_msg = "Unmount",
  158. .action_msg = "Emergency Remount R/O",
  159. .enable_mask = SYSRQ_ENABLE_REMOUNT,
  160. };
  161. #ifdef CONFIG_LOCKDEP
  162. static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
  163. {
  164. debug_show_all_locks();
  165. }
  166. static struct sysrq_key_op sysrq_showlocks_op = {
  167. .handler = sysrq_handle_showlocks,
  168. .help_msg = "show-all-locks(D)",
  169. .action_msg = "Show Locks Held",
  170. };
  171. #else
  172. #define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
  173. #endif
  174. #ifdef CONFIG_SMP
  175. static DEFINE_SPINLOCK(show_lock);
  176. static void showacpu(void *dummy)
  177. {
  178. unsigned long flags;
  179. /* Idle CPUs have no interesting backtrace. */
  180. if (idle_cpu(smp_processor_id()))
  181. return;
  182. spin_lock_irqsave(&show_lock, flags);
  183. printk(KERN_INFO "CPU%d:\n", smp_processor_id());
  184. show_stack(NULL, NULL);
  185. spin_unlock_irqrestore(&show_lock, flags);
  186. }
  187. static void sysrq_showregs_othercpus(struct work_struct *dummy)
  188. {
  189. smp_call_function(showacpu, NULL, 0);
  190. }
  191. static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
  192. static void sysrq_handle_showallcpus(int key, struct tty_struct *tty)
  193. {
  194. struct pt_regs *regs = get_irq_regs();
  195. if (regs) {
  196. printk(KERN_INFO "CPU%d:\n", smp_processor_id());
  197. show_regs(regs);
  198. }
  199. schedule_work(&sysrq_showallcpus);
  200. }
  201. static struct sysrq_key_op sysrq_showallcpus_op = {
  202. .handler = sysrq_handle_showallcpus,
  203. .help_msg = "show-backtrace-all-active-cpus(L)",
  204. .action_msg = "Show backtrace of all active CPUs",
  205. .enable_mask = SYSRQ_ENABLE_DUMP,
  206. };
  207. #endif
  208. static void sysrq_handle_showregs(int key, struct tty_struct *tty)
  209. {
  210. struct pt_regs *regs = get_irq_regs();
  211. if (regs)
  212. show_regs(regs);
  213. perf_counter_print_debug();
  214. }
  215. static struct sysrq_key_op sysrq_showregs_op = {
  216. .handler = sysrq_handle_showregs,
  217. .help_msg = "show-registers(P)",
  218. .action_msg = "Show Regs",
  219. .enable_mask = SYSRQ_ENABLE_DUMP,
  220. };
  221. static void sysrq_handle_showstate(int key, struct tty_struct *tty)
  222. {
  223. show_state();
  224. }
  225. static struct sysrq_key_op sysrq_showstate_op = {
  226. .handler = sysrq_handle_showstate,
  227. .help_msg = "show-task-states(T)",
  228. .action_msg = "Show State",
  229. .enable_mask = SYSRQ_ENABLE_DUMP,
  230. };
  231. static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty)
  232. {
  233. show_state_filter(TASK_UNINTERRUPTIBLE);
  234. }
  235. static struct sysrq_key_op sysrq_showstate_blocked_op = {
  236. .handler = sysrq_handle_showstate_blocked,
  237. .help_msg = "show-blocked-tasks(W)",
  238. .action_msg = "Show Blocked State",
  239. .enable_mask = SYSRQ_ENABLE_DUMP,
  240. };
  241. #ifdef CONFIG_TRACING
  242. #include <linux/ftrace.h>
  243. static void sysrq_ftrace_dump(int key, struct tty_struct *tty)
  244. {
  245. ftrace_dump();
  246. }
  247. static struct sysrq_key_op sysrq_ftrace_dump_op = {
  248. .handler = sysrq_ftrace_dump,
  249. .help_msg = "dump-ftrace-buffer(Z)",
  250. .action_msg = "Dump ftrace buffer",
  251. .enable_mask = SYSRQ_ENABLE_DUMP,
  252. };
  253. #else
  254. #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)0)
  255. #endif
  256. static void sysrq_handle_showmem(int key, struct tty_struct *tty)
  257. {
  258. show_mem();
  259. }
  260. static struct sysrq_key_op sysrq_showmem_op = {
  261. .handler = sysrq_handle_showmem,
  262. .help_msg = "show-memory-usage(M)",
  263. .action_msg = "Show Memory",
  264. .enable_mask = SYSRQ_ENABLE_DUMP,
  265. };
  266. /*
  267. * Signal sysrq helper function. Sends a signal to all user processes.
  268. */
  269. static void send_sig_all(int sig)
  270. {
  271. struct task_struct *p;
  272. for_each_process(p) {
  273. if (p->mm && !is_global_init(p))
  274. /* Not swapper, init nor kernel thread */
  275. force_sig(sig, p);
  276. }
  277. }
  278. static void sysrq_handle_term(int key, struct tty_struct *tty)
  279. {
  280. send_sig_all(SIGTERM);
  281. console_loglevel = 8;
  282. }
  283. static struct sysrq_key_op sysrq_term_op = {
  284. .handler = sysrq_handle_term,
  285. .help_msg = "terminate-all-tasks(E)",
  286. .action_msg = "Terminate All Tasks",
  287. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  288. };
  289. static void moom_callback(struct work_struct *ignored)
  290. {
  291. out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0);
  292. }
  293. static DECLARE_WORK(moom_work, moom_callback);
  294. static void sysrq_handle_moom(int key, struct tty_struct *tty)
  295. {
  296. schedule_work(&moom_work);
  297. }
  298. static struct sysrq_key_op sysrq_moom_op = {
  299. .handler = sysrq_handle_moom,
  300. .help_msg = "memory-full-oom-kill(F)",
  301. .action_msg = "Manual OOM execution",
  302. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  303. };
  304. #ifdef CONFIG_BLOCK
  305. static void sysrq_handle_thaw(int key, struct tty_struct *tty)
  306. {
  307. emergency_thaw_all();
  308. }
  309. static struct sysrq_key_op sysrq_thaw_op = {
  310. .handler = sysrq_handle_thaw,
  311. .help_msg = "thaw-filesystems(J)",
  312. .action_msg = "Emergency Thaw of all frozen filesystems",
  313. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  314. };
  315. #endif
  316. static void sysrq_handle_kill(int key, struct tty_struct *tty)
  317. {
  318. send_sig_all(SIGKILL);
  319. console_loglevel = 8;
  320. }
  321. static struct sysrq_key_op sysrq_kill_op = {
  322. .handler = sysrq_handle_kill,
  323. .help_msg = "kill-all-tasks(I)",
  324. .action_msg = "Kill All Tasks",
  325. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  326. };
  327. static void sysrq_handle_unrt(int key, struct tty_struct *tty)
  328. {
  329. normalize_rt_tasks();
  330. }
  331. static struct sysrq_key_op sysrq_unrt_op = {
  332. .handler = sysrq_handle_unrt,
  333. .help_msg = "nice-all-RT-tasks(N)",
  334. .action_msg = "Nice All RT Tasks",
  335. .enable_mask = SYSRQ_ENABLE_RTNICE,
  336. };
  337. /* Key Operations table and lock */
  338. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  339. static struct sysrq_key_op *sysrq_key_table[36] = {
  340. &sysrq_loglevel_op, /* 0 */
  341. &sysrq_loglevel_op, /* 1 */
  342. &sysrq_loglevel_op, /* 2 */
  343. &sysrq_loglevel_op, /* 3 */
  344. &sysrq_loglevel_op, /* 4 */
  345. &sysrq_loglevel_op, /* 5 */
  346. &sysrq_loglevel_op, /* 6 */
  347. &sysrq_loglevel_op, /* 7 */
  348. &sysrq_loglevel_op, /* 8 */
  349. &sysrq_loglevel_op, /* 9 */
  350. /*
  351. * a: Don't use for system provided sysrqs, it is handled specially on
  352. * sparc and will never arrive.
  353. */
  354. NULL, /* a */
  355. &sysrq_reboot_op, /* b */
  356. &sysrq_crashdump_op, /* c & ibm_emac driver debug */
  357. &sysrq_showlocks_op, /* d */
  358. &sysrq_term_op, /* e */
  359. &sysrq_moom_op, /* f */
  360. /* g: May be registered for the kernel debugger */
  361. NULL, /* g */
  362. NULL, /* h - reserved for help */
  363. &sysrq_kill_op, /* i */
  364. #ifdef CONFIG_BLOCK
  365. &sysrq_thaw_op, /* j */
  366. #else
  367. NULL, /* j */
  368. #endif
  369. &sysrq_SAK_op, /* k */
  370. #ifdef CONFIG_SMP
  371. &sysrq_showallcpus_op, /* l */
  372. #else
  373. NULL, /* l */
  374. #endif
  375. &sysrq_showmem_op, /* m */
  376. &sysrq_unrt_op, /* n */
  377. /* o: This will often be registered as 'Off' at init time */
  378. NULL, /* o */
  379. &sysrq_showregs_op, /* p */
  380. &sysrq_show_timers_op, /* q */
  381. &sysrq_unraw_op, /* r */
  382. &sysrq_sync_op, /* s */
  383. &sysrq_showstate_op, /* t */
  384. &sysrq_mountro_op, /* u */
  385. /* v: May be registered for frame buffer console restore */
  386. NULL, /* v */
  387. &sysrq_showstate_blocked_op, /* w */
  388. /* x: May be registered on ppc/powerpc for xmon */
  389. NULL, /* x */
  390. /* y: May be registered on sparc64 for global register dump */
  391. NULL, /* y */
  392. &sysrq_ftrace_dump_op, /* z */
  393. };
  394. /* key2index calculation, -1 on invalid index */
  395. static int sysrq_key_table_key2index(int key)
  396. {
  397. int retval;
  398. if ((key >= '0') && (key <= '9'))
  399. retval = key - '0';
  400. else if ((key >= 'a') && (key <= 'z'))
  401. retval = key + 10 - 'a';
  402. else
  403. retval = -1;
  404. return retval;
  405. }
  406. /*
  407. * get and put functions for the table, exposed to modules.
  408. */
  409. struct sysrq_key_op *__sysrq_get_key_op(int key)
  410. {
  411. struct sysrq_key_op *op_p = NULL;
  412. int i;
  413. i = sysrq_key_table_key2index(key);
  414. if (i != -1)
  415. op_p = sysrq_key_table[i];
  416. return op_p;
  417. }
  418. static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
  419. {
  420. int i = sysrq_key_table_key2index(key);
  421. if (i != -1)
  422. sysrq_key_table[i] = op_p;
  423. }
  424. /*
  425. * This is the non-locking version of handle_sysrq. It must/can only be called
  426. * by sysrq key handlers, as they are inside of the lock
  427. */
  428. void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
  429. {
  430. struct sysrq_key_op *op_p;
  431. int orig_log_level;
  432. int i;
  433. unsigned long flags;
  434. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  435. /*
  436. * Raise the apparent loglevel to maximum so that the sysrq header
  437. * is shown to provide the user with positive feedback. We do not
  438. * simply emit this at KERN_EMERG as that would change message
  439. * routing in the consumers of /proc/kmsg.
  440. */
  441. orig_log_level = console_loglevel;
  442. console_loglevel = 7;
  443. printk(KERN_INFO "SysRq : ");
  444. op_p = __sysrq_get_key_op(key);
  445. if (op_p) {
  446. /*
  447. * Should we check for enabled operations (/proc/sysrq-trigger
  448. * should not) and is the invoked operation enabled?
  449. */
  450. if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
  451. printk("%s\n", op_p->action_msg);
  452. console_loglevel = orig_log_level;
  453. op_p->handler(key, tty);
  454. } else {
  455. printk("This sysrq operation is disabled.\n");
  456. }
  457. } else {
  458. printk("HELP : ");
  459. /* Only print the help msg once per handler */
  460. for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
  461. if (sysrq_key_table[i]) {
  462. int j;
  463. for (j = 0; sysrq_key_table[i] !=
  464. sysrq_key_table[j]; j++)
  465. ;
  466. if (j != i)
  467. continue;
  468. printk("%s ", sysrq_key_table[i]->help_msg);
  469. }
  470. }
  471. printk("\n");
  472. console_loglevel = orig_log_level;
  473. }
  474. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  475. }
  476. /*
  477. * This function is called by the keyboard handler when SysRq is pressed
  478. * and any other keycode arrives.
  479. */
  480. void handle_sysrq(int key, struct tty_struct *tty)
  481. {
  482. if (sysrq_on())
  483. __handle_sysrq(key, tty, 1);
  484. }
  485. EXPORT_SYMBOL(handle_sysrq);
  486. static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  487. struct sysrq_key_op *remove_op_p)
  488. {
  489. int retval;
  490. unsigned long flags;
  491. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  492. if (__sysrq_get_key_op(key) == remove_op_p) {
  493. __sysrq_put_key_op(key, insert_op_p);
  494. retval = 0;
  495. } else {
  496. retval = -1;
  497. }
  498. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  499. return retval;
  500. }
  501. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  502. {
  503. return __sysrq_swap_key_ops(key, op_p, NULL);
  504. }
  505. EXPORT_SYMBOL(register_sysrq_key);
  506. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  507. {
  508. return __sysrq_swap_key_ops(key, NULL, op_p);
  509. }
  510. EXPORT_SYMBOL(unregister_sysrq_key);
  511. #ifdef CONFIG_PROC_FS
  512. /*
  513. * writing 'C' to /proc/sysrq-trigger is like sysrq-C
  514. */
  515. static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
  516. size_t count, loff_t *ppos)
  517. {
  518. if (count) {
  519. char c;
  520. if (get_user(c, buf))
  521. return -EFAULT;
  522. __handle_sysrq(c, NULL, 0);
  523. }
  524. return count;
  525. }
  526. static const struct file_operations proc_sysrq_trigger_operations = {
  527. .write = write_sysrq_trigger,
  528. };
  529. static int __init sysrq_init(void)
  530. {
  531. proc_create("sysrq-trigger", S_IWUSR, NULL, &proc_sysrq_trigger_operations);
  532. return 0;
  533. }
  534. module_init(sysrq_init);
  535. #endif