sysrq.c 15 KB

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