sysrq.c 15 KB

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