sysrq.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/quotaops.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/suspend.h>
  29. #include <linux/writeback.h>
  30. #include <linux/buffer_head.h> /* for fsync_bdev() */
  31. #include <linux/swap.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/vt_kern.h>
  34. #include <linux/workqueue.h>
  35. #include <linux/kexec.h>
  36. #include <linux/irq.h>
  37. #include <linux/hrtimer.h>
  38. #include <asm/ptrace.h>
  39. #include <asm/irq_regs.h>
  40. /* Whether we react on sysrq keys or just ignore them */
  41. int __read_mostly __sysrq_enabled = 1;
  42. static int __read_mostly sysrq_always_enabled;
  43. int sysrq_on(void)
  44. {
  45. return __sysrq_enabled || sysrq_always_enabled;
  46. }
  47. /*
  48. * A value of 1 means 'all', other nonzero values are an op mask:
  49. */
  50. static inline int sysrq_on_mask(int mask)
  51. {
  52. return sysrq_always_enabled || __sysrq_enabled == 1 ||
  53. (__sysrq_enabled & mask);
  54. }
  55. static int __init sysrq_always_enabled_setup(char *str)
  56. {
  57. sysrq_always_enabled = 1;
  58. printk(KERN_INFO "debug: sysrq always enabled.\n");
  59. return 1;
  60. }
  61. __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
  62. static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
  63. {
  64. int i;
  65. i = key - '0';
  66. console_loglevel = 7;
  67. printk("Loglevel set to %d\n", i);
  68. console_loglevel = i;
  69. }
  70. static struct sysrq_key_op sysrq_loglevel_op = {
  71. .handler = sysrq_handle_loglevel,
  72. .help_msg = "loglevel0-8",
  73. .action_msg = "Changing Loglevel",
  74. .enable_mask = SYSRQ_ENABLE_LOG,
  75. };
  76. #ifdef CONFIG_VT
  77. static void sysrq_handle_SAK(int key, struct tty_struct *tty)
  78. {
  79. struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
  80. schedule_work(SAK_work);
  81. }
  82. static struct sysrq_key_op sysrq_SAK_op = {
  83. .handler = sysrq_handle_SAK,
  84. .help_msg = "saK",
  85. .action_msg = "SAK",
  86. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  87. };
  88. #else
  89. #define sysrq_SAK_op (*(struct sysrq_key_op *)0)
  90. #endif
  91. #ifdef CONFIG_VT
  92. static void sysrq_handle_unraw(int key, struct tty_struct *tty)
  93. {
  94. struct kbd_struct *kbd = &kbd_table[fg_console];
  95. if (kbd)
  96. kbd->kbdmode = VC_XLATE;
  97. }
  98. static struct sysrq_key_op sysrq_unraw_op = {
  99. .handler = sysrq_handle_unraw,
  100. .help_msg = "unRaw",
  101. .action_msg = "Keyboard mode set to XLATE",
  102. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  103. };
  104. #else
  105. #define sysrq_unraw_op (*(struct sysrq_key_op *)0)
  106. #endif /* CONFIG_VT */
  107. #ifdef CONFIG_KEXEC
  108. static void sysrq_handle_crashdump(int key, struct tty_struct *tty)
  109. {
  110. crash_kexec(get_irq_regs());
  111. }
  112. static struct sysrq_key_op sysrq_crashdump_op = {
  113. .handler = sysrq_handle_crashdump,
  114. .help_msg = "Crashdump",
  115. .action_msg = "Trigger a crashdump",
  116. .enable_mask = SYSRQ_ENABLE_DUMP,
  117. };
  118. #else
  119. #define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
  120. #endif
  121. static void sysrq_handle_reboot(int key, struct tty_struct *tty)
  122. {
  123. lockdep_off();
  124. local_irq_enable();
  125. emergency_restart();
  126. }
  127. static struct sysrq_key_op sysrq_reboot_op = {
  128. .handler = sysrq_handle_reboot,
  129. .help_msg = "reBoot",
  130. .action_msg = "Resetting",
  131. .enable_mask = SYSRQ_ENABLE_BOOT,
  132. };
  133. static void sysrq_handle_sync(int key, struct tty_struct *tty)
  134. {
  135. emergency_sync();
  136. }
  137. static struct sysrq_key_op sysrq_sync_op = {
  138. .handler = sysrq_handle_sync,
  139. .help_msg = "Sync",
  140. .action_msg = "Emergency Sync",
  141. .enable_mask = SYSRQ_ENABLE_SYNC,
  142. };
  143. static void sysrq_handle_show_timers(int key, struct tty_struct *tty)
  144. {
  145. sysrq_timer_list_show();
  146. }
  147. static struct sysrq_key_op sysrq_show_timers_op = {
  148. .handler = sysrq_handle_show_timers,
  149. .help_msg = "show-all-timers(Q)",
  150. .action_msg = "Show Pending Timers",
  151. };
  152. static void sysrq_handle_mountro(int key, struct tty_struct *tty)
  153. {
  154. emergency_remount();
  155. }
  156. static struct sysrq_key_op sysrq_mountro_op = {
  157. .handler = sysrq_handle_mountro,
  158. .help_msg = "Unmount",
  159. .action_msg = "Emergency Remount R/O",
  160. .enable_mask = SYSRQ_ENABLE_REMOUNT,
  161. };
  162. #ifdef CONFIG_LOCKDEP
  163. static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
  164. {
  165. debug_show_all_locks();
  166. }
  167. static struct sysrq_key_op sysrq_showlocks_op = {
  168. .handler = sysrq_handle_showlocks,
  169. .help_msg = "show-all-locks(D)",
  170. .action_msg = "Show Locks Held",
  171. };
  172. #else
  173. #define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
  174. #endif
  175. static void sysrq_handle_showregs(int key, struct tty_struct *tty)
  176. {
  177. struct pt_regs *regs = get_irq_regs();
  178. if (regs)
  179. show_regs(regs);
  180. }
  181. static struct sysrq_key_op sysrq_showregs_op = {
  182. .handler = sysrq_handle_showregs,
  183. .help_msg = "showPc",
  184. .action_msg = "Show Regs",
  185. .enable_mask = SYSRQ_ENABLE_DUMP,
  186. };
  187. static void sysrq_handle_showstate(int key, struct tty_struct *tty)
  188. {
  189. show_state();
  190. }
  191. static struct sysrq_key_op sysrq_showstate_op = {
  192. .handler = sysrq_handle_showstate,
  193. .help_msg = "showTasks",
  194. .action_msg = "Show State",
  195. .enable_mask = SYSRQ_ENABLE_DUMP,
  196. };
  197. static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty)
  198. {
  199. show_state_filter(TASK_UNINTERRUPTIBLE);
  200. }
  201. static struct sysrq_key_op sysrq_showstate_blocked_op = {
  202. .handler = sysrq_handle_showstate_blocked,
  203. .help_msg = "shoW-blocked-tasks",
  204. .action_msg = "Show Blocked State",
  205. .enable_mask = SYSRQ_ENABLE_DUMP,
  206. };
  207. static void sysrq_handle_showmem(int key, struct tty_struct *tty)
  208. {
  209. show_mem();
  210. }
  211. static struct sysrq_key_op sysrq_showmem_op = {
  212. .handler = sysrq_handle_showmem,
  213. .help_msg = "showMem",
  214. .action_msg = "Show Memory",
  215. .enable_mask = SYSRQ_ENABLE_DUMP,
  216. };
  217. /*
  218. * Signal sysrq helper function. Sends a signal to all user processes.
  219. */
  220. static void send_sig_all(int sig)
  221. {
  222. struct task_struct *p;
  223. for_each_process(p) {
  224. if (p->mm && !is_init(p))
  225. /* Not swapper, init nor kernel thread */
  226. force_sig(sig, p);
  227. }
  228. }
  229. static void sysrq_handle_term(int key, struct tty_struct *tty)
  230. {
  231. send_sig_all(SIGTERM);
  232. console_loglevel = 8;
  233. }
  234. static struct sysrq_key_op sysrq_term_op = {
  235. .handler = sysrq_handle_term,
  236. .help_msg = "tErm",
  237. .action_msg = "Terminate All Tasks",
  238. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  239. };
  240. static void moom_callback(struct work_struct *ignored)
  241. {
  242. out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL],
  243. GFP_KERNEL, 0);
  244. }
  245. static DECLARE_WORK(moom_work, moom_callback);
  246. static void sysrq_handle_moom(int key, struct tty_struct *tty)
  247. {
  248. schedule_work(&moom_work);
  249. }
  250. static struct sysrq_key_op sysrq_moom_op = {
  251. .handler = sysrq_handle_moom,
  252. .help_msg = "Full",
  253. .action_msg = "Manual OOM execution",
  254. };
  255. static void sysrq_handle_kill(int key, struct tty_struct *tty)
  256. {
  257. send_sig_all(SIGKILL);
  258. console_loglevel = 8;
  259. }
  260. static struct sysrq_key_op sysrq_kill_op = {
  261. .handler = sysrq_handle_kill,
  262. .help_msg = "kIll",
  263. .action_msg = "Kill All Tasks",
  264. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  265. };
  266. static void sysrq_handle_unrt(int key, struct tty_struct *tty)
  267. {
  268. normalize_rt_tasks();
  269. }
  270. static struct sysrq_key_op sysrq_unrt_op = {
  271. .handler = sysrq_handle_unrt,
  272. .help_msg = "Nice",
  273. .action_msg = "Nice All RT Tasks",
  274. .enable_mask = SYSRQ_ENABLE_RTNICE,
  275. };
  276. /* Key Operations table and lock */
  277. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  278. static struct sysrq_key_op *sysrq_key_table[36] = {
  279. &sysrq_loglevel_op, /* 0 */
  280. &sysrq_loglevel_op, /* 1 */
  281. &sysrq_loglevel_op, /* 2 */
  282. &sysrq_loglevel_op, /* 3 */
  283. &sysrq_loglevel_op, /* 4 */
  284. &sysrq_loglevel_op, /* 5 */
  285. &sysrq_loglevel_op, /* 6 */
  286. &sysrq_loglevel_op, /* 7 */
  287. &sysrq_loglevel_op, /* 8 */
  288. &sysrq_loglevel_op, /* 9 */
  289. /*
  290. * a: Don't use for system provided sysrqs, it is handled specially on
  291. * sparc and will never arrive.
  292. */
  293. NULL, /* a */
  294. &sysrq_reboot_op, /* b */
  295. &sysrq_crashdump_op, /* c & ibm_emac driver debug */
  296. &sysrq_showlocks_op, /* d */
  297. &sysrq_term_op, /* e */
  298. &sysrq_moom_op, /* f */
  299. /* g: May be registered by ppc for kgdb */
  300. NULL, /* g */
  301. NULL, /* h */
  302. &sysrq_kill_op, /* i */
  303. NULL, /* j */
  304. &sysrq_SAK_op, /* k */
  305. NULL, /* l */
  306. &sysrq_showmem_op, /* m */
  307. &sysrq_unrt_op, /* n */
  308. /* o: This will often be registered as 'Off' at init time */
  309. NULL, /* o */
  310. &sysrq_showregs_op, /* p */
  311. &sysrq_show_timers_op, /* q */
  312. &sysrq_unraw_op, /* r */
  313. &sysrq_sync_op, /* s */
  314. &sysrq_showstate_op, /* t */
  315. &sysrq_mountro_op, /* u */
  316. /* v: May be registered at init time by SMP VOYAGER */
  317. NULL, /* v */
  318. &sysrq_showstate_blocked_op, /* w */
  319. /* x: May be registered on ppc/powerpc for xmon */
  320. NULL, /* x */
  321. NULL, /* y */
  322. NULL /* z */
  323. };
  324. /* key2index calculation, -1 on invalid index */
  325. static int sysrq_key_table_key2index(int key)
  326. {
  327. int retval;
  328. if ((key >= '0') && (key <= '9'))
  329. retval = key - '0';
  330. else if ((key >= 'a') && (key <= 'z'))
  331. retval = key + 10 - 'a';
  332. else
  333. retval = -1;
  334. return retval;
  335. }
  336. /*
  337. * get and put functions for the table, exposed to modules.
  338. */
  339. struct sysrq_key_op *__sysrq_get_key_op(int key)
  340. {
  341. struct sysrq_key_op *op_p = NULL;
  342. int i;
  343. i = sysrq_key_table_key2index(key);
  344. if (i != -1)
  345. op_p = sysrq_key_table[i];
  346. return op_p;
  347. }
  348. static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
  349. {
  350. int i = sysrq_key_table_key2index(key);
  351. if (i != -1)
  352. sysrq_key_table[i] = op_p;
  353. }
  354. /*
  355. * This is the non-locking version of handle_sysrq. It must/can only be called
  356. * by sysrq key handlers, as they are inside of the lock
  357. */
  358. void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
  359. {
  360. struct sysrq_key_op *op_p;
  361. int orig_log_level;
  362. int i;
  363. unsigned long flags;
  364. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  365. orig_log_level = console_loglevel;
  366. console_loglevel = 7;
  367. printk(KERN_INFO "SysRq : ");
  368. op_p = __sysrq_get_key_op(key);
  369. if (op_p) {
  370. /*
  371. * Should we check for enabled operations (/proc/sysrq-trigger
  372. * should not) and is the invoked operation enabled?
  373. */
  374. if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
  375. printk("%s\n", op_p->action_msg);
  376. console_loglevel = orig_log_level;
  377. op_p->handler(key, tty);
  378. } else {
  379. printk("This sysrq operation is disabled.\n");
  380. }
  381. } else {
  382. printk("HELP : ");
  383. /* Only print the help msg once per handler */
  384. for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
  385. if (sysrq_key_table[i]) {
  386. int j;
  387. for (j = 0; sysrq_key_table[i] !=
  388. sysrq_key_table[j]; j++)
  389. ;
  390. if (j != i)
  391. continue;
  392. printk("%s ", sysrq_key_table[i]->help_msg);
  393. }
  394. }
  395. printk("\n");
  396. console_loglevel = orig_log_level;
  397. }
  398. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  399. }
  400. /*
  401. * This function is called by the keyboard handler when SysRq is pressed
  402. * and any other keycode arrives.
  403. */
  404. void handle_sysrq(int key, struct tty_struct *tty)
  405. {
  406. if (sysrq_on())
  407. __handle_sysrq(key, tty, 1);
  408. }
  409. EXPORT_SYMBOL(handle_sysrq);
  410. static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  411. struct sysrq_key_op *remove_op_p)
  412. {
  413. int retval;
  414. unsigned long flags;
  415. spin_lock_irqsave(&sysrq_key_table_lock, flags);
  416. if (__sysrq_get_key_op(key) == remove_op_p) {
  417. __sysrq_put_key_op(key, insert_op_p);
  418. retval = 0;
  419. } else {
  420. retval = -1;
  421. }
  422. spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
  423. return retval;
  424. }
  425. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  426. {
  427. return __sysrq_swap_key_ops(key, op_p, NULL);
  428. }
  429. EXPORT_SYMBOL(register_sysrq_key);
  430. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  431. {
  432. return __sysrq_swap_key_ops(key, NULL, op_p);
  433. }
  434. EXPORT_SYMBOL(unregister_sysrq_key);