3 * $Id: sysrq.c,v 1.15 1998/08/23 14:56:41 mj Exp $
5 * Linux Magic System Request Key Hacks
7 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
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
15 #include <linux/sched.h>
16 #include <linux/interrupt.h>
19 #include <linux/tty.h>
20 #include <linux/mount.h>
21 #include <linux/kdev_t.h>
22 #include <linux/major.h>
23 #include <linux/reboot.h>
24 #include <linux/sysrq.h>
25 #include <linux/kbd_kern.h>
26 #include <linux/quotaops.h>
27 #include <linux/smp_lock.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/irq.h>
39 #include <linux/vserver/debug.h>
41 #include <asm/ptrace.h>
42 #include <asm/irq_regs.h>
44 /* Whether we react on sysrq keys or just ignore them */
45 int __read_mostly __sysrq_enabled = 1;
47 static int __read_mostly sysrq_always_enabled;
51 return __sysrq_enabled || sysrq_always_enabled;
55 * A value of 1 means 'all', other nonzero values are an op mask:
57 static inline int sysrq_on_mask(int mask)
59 return sysrq_always_enabled || __sysrq_enabled == 1 ||
60 (__sysrq_enabled & mask);
63 static int __init sysrq_always_enabled_setup(char *str)
65 sysrq_always_enabled = 1;
66 printk(KERN_INFO "debug: sysrq always enabled.\n");
71 __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
74 static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
79 printk("Loglevel set to %d\n", i);
82 static struct sysrq_key_op sysrq_loglevel_op = {
83 .handler = sysrq_handle_loglevel,
84 .help_msg = "loglevel0-8",
85 .action_msg = "Changing Loglevel",
86 .enable_mask = SYSRQ_ENABLE_LOG,
90 static void sysrq_handle_SAK(int key, struct tty_struct *tty)
94 reset_vc(vc_cons[fg_console].d);
96 static struct sysrq_key_op sysrq_SAK_op = {
97 .handler = sysrq_handle_SAK,
100 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
103 #define sysrq_SAK_op (*(struct sysrq_key_op *)0)
107 static void sysrq_handle_unraw(int key, struct tty_struct *tty)
109 struct kbd_struct *kbd = &kbd_table[fg_console];
112 kbd->kbdmode = VC_XLATE;
114 static struct sysrq_key_op sysrq_unraw_op = {
115 .handler = sysrq_handle_unraw,
117 .action_msg = "Keyboard mode set to XLATE",
118 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
121 #define sysrq_unraw_op (*(struct sysrq_key_op *)0)
122 #endif /* CONFIG_VT */
124 static void sysrq_handle_crashdump(int key, struct tty_struct *tty)
127 crash_kexec(get_irq_regs());
128 /* can't get here if crash image is loaded */
129 printk("Kexec: Warning: crash image not loaded\n");
132 panic("SysRq-triggered panic!\n");
134 static struct sysrq_key_op sysrq_crashdump_op = {
135 .handler = sysrq_handle_crashdump,
136 .help_msg = "Crashdump",
137 .action_msg = "Trigger a crashdump",
138 .enable_mask = SYSRQ_ENABLE_DUMP,
141 static void sysrq_handle_reboot(int key, struct tty_struct *tty)
147 static struct sysrq_key_op sysrq_reboot_op = {
148 .handler = sysrq_handle_reboot,
149 .help_msg = "reBoot",
150 .action_msg = "Resetting",
151 .enable_mask = SYSRQ_ENABLE_BOOT,
154 static void sysrq_handle_sync(int key, struct tty_struct *tty)
158 static struct sysrq_key_op sysrq_sync_op = {
159 .handler = sysrq_handle_sync,
161 .action_msg = "Emergency Sync",
162 .enable_mask = SYSRQ_ENABLE_SYNC,
165 static void sysrq_handle_mountro(int key, struct tty_struct *tty)
169 static struct sysrq_key_op sysrq_mountro_op = {
170 .handler = sysrq_handle_mountro,
171 .help_msg = "Unmount",
172 .action_msg = "Emergency Remount R/O",
173 .enable_mask = SYSRQ_ENABLE_REMOUNT,
176 #ifdef CONFIG_LOCKDEP
177 static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
179 debug_show_all_locks();
182 static struct sysrq_key_op sysrq_showlocks_op = {
183 .handler = sysrq_handle_showlocks,
184 .help_msg = "show-all-locks(D)",
185 .action_msg = "Show Locks Held",
188 #define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
191 static void sysrq_handle_showregs(int key, struct tty_struct *tty)
193 struct pt_regs *regs = get_irq_regs();
197 static struct sysrq_key_op sysrq_showregs_op = {
198 .handler = sysrq_handle_showregs,
199 .help_msg = "showPc",
200 .action_msg = "Show Regs",
201 .enable_mask = SYSRQ_ENABLE_DUMP,
204 static void sysrq_handle_showstate(int key, struct tty_struct *tty)
208 static struct sysrq_key_op sysrq_showstate_op = {
209 .handler = sysrq_handle_showstate,
210 .help_msg = "showTasks",
211 .action_msg = "Show State",
212 .enable_mask = SYSRQ_ENABLE_DUMP,
215 static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty)
217 show_state_filter(TASK_UNINTERRUPTIBLE);
219 static struct sysrq_key_op sysrq_showstate_blocked_op = {
220 .handler = sysrq_handle_showstate_blocked,
221 .help_msg = "shoW-blocked-tasks",
222 .action_msg = "Show Blocked State",
223 .enable_mask = SYSRQ_ENABLE_DUMP,
227 static void sysrq_handle_showmem(int key, struct tty_struct *tty)
231 static struct sysrq_key_op sysrq_showmem_op = {
232 .handler = sysrq_handle_showmem,
233 .help_msg = "showMem",
234 .action_msg = "Show Memory",
235 .enable_mask = SYSRQ_ENABLE_DUMP,
239 * Signal sysrq helper function. Sends a signal to all user processes.
241 static void send_sig_all(int sig)
243 struct task_struct *p;
245 for_each_process(p) {
246 if (p->mm && !is_init(p))
247 /* Not swapper, init nor kernel thread */
252 static void sysrq_handle_term(int key, struct tty_struct *tty)
254 send_sig_all(SIGTERM);
255 console_loglevel = 8;
257 static struct sysrq_key_op sysrq_term_op = {
258 .handler = sysrq_handle_term,
260 .action_msg = "Terminate All Tasks",
261 .enable_mask = SYSRQ_ENABLE_SIGNAL,
264 static void moom_callback(struct work_struct *ignored)
266 out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL],
270 static DECLARE_WORK(moom_work, moom_callback);
272 static void sysrq_handle_moom(int key, struct tty_struct *tty)
274 schedule_work(&moom_work);
276 static struct sysrq_key_op sysrq_moom_op = {
277 .handler = sysrq_handle_moom,
279 .action_msg = "Manual OOM execution",
282 static void sysrq_handle_kill(int key, struct tty_struct *tty)
284 send_sig_all(SIGKILL);
285 console_loglevel = 8;
287 static struct sysrq_key_op sysrq_kill_op = {
288 .handler = sysrq_handle_kill,
290 .action_msg = "Kill All Tasks",
291 .enable_mask = SYSRQ_ENABLE_SIGNAL,
294 static void sysrq_handle_unrt(int key, struct tty_struct *tty)
296 normalize_rt_tasks();
298 static struct sysrq_key_op sysrq_unrt_op = {
299 .handler = sysrq_handle_unrt,
301 .action_msg = "Nice All RT Tasks",
302 .enable_mask = SYSRQ_ENABLE_RTNICE,
306 #ifdef CONFIG_VSERVER_DEBUG
307 static void sysrq_handle_vxinfo(int key, struct tty_struct *tty)
309 dump_vx_info_inactive((key == 'x')?0:1);
312 static struct sysrq_key_op sysrq_showvxinfo_op = {
313 .handler = sysrq_handle_vxinfo,
314 .help_msg = "conteXt",
315 .action_msg = "Show Context Info",
316 .enable_mask = SYSRQ_ENABLE_DUMP,
320 /* Key Operations table and lock */
321 static DEFINE_SPINLOCK(sysrq_key_table_lock);
323 static struct sysrq_key_op *sysrq_key_table[36] = {
324 &sysrq_loglevel_op, /* 0 */
325 &sysrq_loglevel_op, /* 1 */
326 &sysrq_loglevel_op, /* 2 */
327 &sysrq_loglevel_op, /* 3 */
328 &sysrq_loglevel_op, /* 4 */
329 &sysrq_loglevel_op, /* 5 */
330 &sysrq_loglevel_op, /* 6 */
331 &sysrq_loglevel_op, /* 7 */
332 &sysrq_loglevel_op, /* 8 */
333 &sysrq_loglevel_op, /* 9 */
336 * a: Don't use for system provided sysrqs, it is handled specially on
337 * sparc and will never arrive.
340 &sysrq_reboot_op, /* b */
341 &sysrq_crashdump_op, /* c & ibm_emac driver debug */
342 &sysrq_showlocks_op, /* d */
343 &sysrq_term_op, /* e */
344 &sysrq_moom_op, /* f */
345 /* g: May be registered by ppc for kgdb */
348 &sysrq_kill_op, /* i */
350 &sysrq_SAK_op, /* k */
352 &sysrq_showmem_op, /* m */
353 &sysrq_unrt_op, /* n */
354 /* o: This will often be registered as 'Off' at init time */
356 &sysrq_showregs_op, /* p */
358 &sysrq_unraw_op, /* r */
359 &sysrq_sync_op, /* s */
360 &sysrq_showstate_op, /* t */
361 &sysrq_mountro_op, /* u */
362 /* v: May be registered at init time by SMP VOYAGER */
364 &sysrq_showstate_blocked_op, /* w */
365 /* x: May be registered on ppc/powerpc for xmon */
368 #ifdef CONFIG_VSERVER_DEBUG
369 &sysrq_showvxinfo_op, /* z */
375 /* key2index calculation, -1 on invalid index */
376 static int sysrq_key_table_key2index(int key)
380 if ((key >= '0') && (key <= '9'))
382 else if ((key >= 'a') && (key <= 'z'))
383 retval = key + 10 - 'a';
384 else if ((key >= 'A') && (key <= 'Z'))
385 retval = key + 10 - 'A';
392 * get and put functions for the table, exposed to modules.
394 struct sysrq_key_op *__sysrq_get_key_op(int key)
396 struct sysrq_key_op *op_p = NULL;
399 i = sysrq_key_table_key2index(key);
401 op_p = sysrq_key_table[i];
405 static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
407 int i = sysrq_key_table_key2index(key);
410 sysrq_key_table[i] = op_p;
414 * This is the non-locking version of handle_sysrq. It must/can only be called
415 * by sysrq key handlers, as they are inside of the lock
417 void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
419 struct sysrq_key_op *op_p;
424 spin_lock_irqsave(&sysrq_key_table_lock, flags);
425 orig_log_level = console_loglevel;
426 console_loglevel = 7;
427 printk(KERN_INFO "SysRq : ");
429 op_p = __sysrq_get_key_op(key);
432 * Should we check for enabled operations (/proc/sysrq-trigger
433 * should not) and is the invoked operation enabled?
435 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
436 printk("%s\n", op_p->action_msg);
437 console_loglevel = orig_log_level;
438 op_p->handler(key, tty);
440 printk("This sysrq operation is disabled.\n");
444 /* Only print the help msg once per handler */
445 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
446 if (sysrq_key_table[i]) {
449 for (j = 0; sysrq_key_table[i] !=
450 sysrq_key_table[j]; j++)
454 printk("%s ", sysrq_key_table[i]->help_msg);
458 console_loglevel = orig_log_level;
460 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
464 * This function is called by the keyboard handler when SysRq is pressed
465 * and any other keycode arrives.
467 void handle_sysrq(int key, struct tty_struct *tty)
470 __handle_sysrq(key, tty, 1);
472 EXPORT_SYMBOL(handle_sysrq);
474 static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
475 struct sysrq_key_op *remove_op_p)
481 spin_lock_irqsave(&sysrq_key_table_lock, flags);
482 if (__sysrq_get_key_op(key) == remove_op_p) {
483 __sysrq_put_key_op(key, insert_op_p);
488 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
492 int register_sysrq_key(int key, struct sysrq_key_op *op_p)
494 return __sysrq_swap_key_ops(key, op_p, NULL);
496 EXPORT_SYMBOL(register_sysrq_key);
498 int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
500 return __sysrq_swap_key_ops(key, NULL, op_p);
502 EXPORT_SYMBOL(unregister_sysrq_key);