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>
40 #include <asm/ptrace.h>
42 /* Whether we react on sysrq keys or just ignore them */
43 int sysrq_enabled
= 1;
45 static void sysrq_handle_loglevel(int key
, struct tty_struct
*tty
)
50 printk("Loglevel set to %d\n", i
);
53 static struct sysrq_key_op sysrq_loglevel_op
= {
54 .handler
= sysrq_handle_loglevel
,
55 .help_msg
= "loglevel0-8",
56 .action_msg
= "Changing Loglevel",
57 .enable_mask
= SYSRQ_ENABLE_LOG
,
61 static void sysrq_handle_SAK(int key
, struct tty_struct
*tty
)
65 reset_vc(vc_cons
[fg_console
].d
);
67 static struct sysrq_key_op sysrq_SAK_op
= {
68 .handler
= sysrq_handle_SAK
,
71 .enable_mask
= SYSRQ_ENABLE_KEYBOARD
,
74 #define sysrq_SAK_op (*(struct sysrq_key_op *)0)
78 static void sysrq_handle_unraw(int key
, struct tty_struct
*tty
)
80 struct kbd_struct
*kbd
= &kbd_table
[fg_console
];
83 kbd
->kbdmode
= VC_XLATE
;
85 static struct sysrq_key_op sysrq_unraw_op
= {
86 .handler
= sysrq_handle_unraw
,
88 .action_msg
= "Keyboard mode set to XLATE",
89 .enable_mask
= SYSRQ_ENABLE_KEYBOARD
,
92 #define sysrq_unraw_op (*(struct sysrq_key_op *)0)
93 #endif /* CONFIG_VT */
96 static void sysrq_handle_crashdump(int key
, struct tty_struct
*tty
)
98 crash_kexec(get_irq_regs());
100 static struct sysrq_key_op sysrq_crashdump_op
= {
101 .handler
= sysrq_handle_crashdump
,
102 .help_msg
= "Crashdump",
103 .action_msg
= "Trigger a crashdump",
104 .enable_mask
= SYSRQ_ENABLE_DUMP
,
107 #define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
110 static void sysrq_handle_reboot(int key
, struct tty_struct
*tty
)
116 static struct sysrq_key_op sysrq_reboot_op
= {
117 .handler
= sysrq_handle_reboot
,
118 .help_msg
= "reBoot",
119 .action_msg
= "Resetting",
120 .enable_mask
= SYSRQ_ENABLE_BOOT
,
123 static void sysrq_handle_sync(int key
, struct tty_struct
*tty
)
127 static struct sysrq_key_op sysrq_sync_op
= {
128 .handler
= sysrq_handle_sync
,
130 .action_msg
= "Emergency Sync",
131 .enable_mask
= SYSRQ_ENABLE_SYNC
,
134 static void sysrq_handle_mountro(int key
, struct tty_struct
*tty
)
138 static struct sysrq_key_op sysrq_mountro_op
= {
139 .handler
= sysrq_handle_mountro
,
140 .help_msg
= "Unmount",
141 .action_msg
= "Emergency Remount R/O",
142 .enable_mask
= SYSRQ_ENABLE_REMOUNT
,
145 #ifdef CONFIG_LOCKDEP
146 static void sysrq_handle_showlocks(int key
, struct tty_struct
*tty
)
148 debug_show_all_locks();
151 static struct sysrq_key_op sysrq_showlocks_op
= {
152 .handler
= sysrq_handle_showlocks
,
153 .help_msg
= "show-all-locks(D)",
154 .action_msg
= "Show Locks Held",
157 #define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
160 static void sysrq_handle_showregs(int key
, struct tty_struct
*tty
)
162 struct pt_regs
*regs
= get_irq_regs();
166 static struct sysrq_key_op sysrq_showregs_op
= {
167 .handler
= sysrq_handle_showregs
,
168 .help_msg
= "showPc",
169 .action_msg
= "Show Regs",
170 .enable_mask
= SYSRQ_ENABLE_DUMP
,
173 static void sysrq_handle_showstate(int key
, struct tty_struct
*tty
)
177 static struct sysrq_key_op sysrq_showstate_op
= {
178 .handler
= sysrq_handle_showstate
,
179 .help_msg
= "showTasks",
180 .action_msg
= "Show State",
181 .enable_mask
= SYSRQ_ENABLE_DUMP
,
184 static void sysrq_handle_showmem(int key
, struct tty_struct
*tty
)
188 static struct sysrq_key_op sysrq_showmem_op
= {
189 .handler
= sysrq_handle_showmem
,
190 .help_msg
= "showMem",
191 .action_msg
= "Show Memory",
192 .enable_mask
= SYSRQ_ENABLE_DUMP
,
196 * Signal sysrq helper function. Sends a signal to all user processes.
198 static void send_sig_all(int sig
)
200 struct task_struct
*p
;
202 for_each_process(p
) {
203 if (p
->mm
&& !is_init(p
))
204 /* Not swapper, init nor kernel thread */
209 static void sysrq_handle_term(int key
, struct tty_struct
*tty
)
211 send_sig_all(SIGTERM
);
212 console_loglevel
= 8;
214 static struct sysrq_key_op sysrq_term_op
= {
215 .handler
= sysrq_handle_term
,
217 .action_msg
= "Terminate All Tasks",
218 .enable_mask
= SYSRQ_ENABLE_SIGNAL
,
221 static void moom_callback(void *ignored
)
223 out_of_memory(&NODE_DATA(0)->node_zonelists
[ZONE_NORMAL
],
227 static DECLARE_WORK(moom_work
, moom_callback
, NULL
);
229 static void sysrq_handle_moom(int key
, struct tty_struct
*tty
)
231 schedule_work(&moom_work
);
233 static struct sysrq_key_op sysrq_moom_op
= {
234 .handler
= sysrq_handle_moom
,
236 .action_msg
= "Manual OOM execution",
239 static void sysrq_handle_kill(int key
, struct tty_struct
*tty
)
241 send_sig_all(SIGKILL
);
242 console_loglevel
= 8;
244 static struct sysrq_key_op sysrq_kill_op
= {
245 .handler
= sysrq_handle_kill
,
247 .action_msg
= "Kill All Tasks",
248 .enable_mask
= SYSRQ_ENABLE_SIGNAL
,
251 static void sysrq_handle_unrt(int key
, struct tty_struct
*tty
)
253 normalize_rt_tasks();
255 static struct sysrq_key_op sysrq_unrt_op
= {
256 .handler
= sysrq_handle_unrt
,
258 .action_msg
= "Nice All RT Tasks",
259 .enable_mask
= SYSRQ_ENABLE_RTNICE
,
262 /* Key Operations table and lock */
263 static DEFINE_SPINLOCK(sysrq_key_table_lock
);
265 static struct sysrq_key_op
*sysrq_key_table
[36] = {
266 &sysrq_loglevel_op
, /* 0 */
267 &sysrq_loglevel_op
, /* 1 */
268 &sysrq_loglevel_op
, /* 2 */
269 &sysrq_loglevel_op
, /* 3 */
270 &sysrq_loglevel_op
, /* 4 */
271 &sysrq_loglevel_op
, /* 5 */
272 &sysrq_loglevel_op
, /* 6 */
273 &sysrq_loglevel_op
, /* 7 */
274 &sysrq_loglevel_op
, /* 8 */
275 &sysrq_loglevel_op
, /* 9 */
278 * Don't use for system provided sysrqs, it is handled specially on
279 * sparc and will never arrive
282 &sysrq_reboot_op
, /* b */
283 &sysrq_crashdump_op
, /* c */
284 &sysrq_showlocks_op
, /* d */
285 &sysrq_term_op
, /* e */
286 &sysrq_moom_op
, /* f */
289 &sysrq_kill_op
, /* i */
291 &sysrq_SAK_op
, /* k */
293 &sysrq_showmem_op
, /* m */
294 &sysrq_unrt_op
, /* n */
295 /* This will often be registered as 'Off' at init time */
297 &sysrq_showregs_op
, /* p */
299 &sysrq_unraw_op
, /* r */
300 &sysrq_sync_op
, /* s */
301 &sysrq_showstate_op
, /* t */
302 &sysrq_mountro_op
, /* u */
303 /* May be assigned at init time by SMP VOYAGER */
311 /* key2index calculation, -1 on invalid index */
312 static int sysrq_key_table_key2index(int key
)
316 if ((key
>= '0') && (key
<= '9'))
318 else if ((key
>= 'a') && (key
<= 'z'))
319 retval
= key
+ 10 - 'a';
326 * get and put functions for the table, exposed to modules.
328 struct sysrq_key_op
*__sysrq_get_key_op(int key
)
330 struct sysrq_key_op
*op_p
= NULL
;
333 i
= sysrq_key_table_key2index(key
);
335 op_p
= sysrq_key_table
[i
];
339 static void __sysrq_put_key_op(int key
, struct sysrq_key_op
*op_p
)
341 int i
= sysrq_key_table_key2index(key
);
344 sysrq_key_table
[i
] = op_p
;
348 * This is the non-locking version of handle_sysrq. It must/can only be called
349 * by sysrq key handlers, as they are inside of the lock
351 void __handle_sysrq(int key
, struct tty_struct
*tty
, int check_mask
)
353 struct sysrq_key_op
*op_p
;
358 spin_lock_irqsave(&sysrq_key_table_lock
, flags
);
359 orig_log_level
= console_loglevel
;
360 console_loglevel
= 7;
361 printk(KERN_INFO
"SysRq : ");
363 op_p
= __sysrq_get_key_op(key
);
366 * Should we check for enabled operations (/proc/sysrq-trigger
367 * should not) and is the invoked operation enabled?
369 if (!check_mask
|| sysrq_enabled
== 1 ||
370 (sysrq_enabled
& op_p
->enable_mask
)) {
371 printk("%s\n", op_p
->action_msg
);
372 console_loglevel
= orig_log_level
;
373 op_p
->handler(key
, tty
);
375 printk("This sysrq operation is disabled.\n");
379 /* Only print the help msg once per handler */
380 for (i
= 0; i
< ARRAY_SIZE(sysrq_key_table
); i
++) {
381 if (sysrq_key_table
[i
]) {
384 for (j
= 0; sysrq_key_table
[i
] !=
385 sysrq_key_table
[j
]; j
++)
389 printk("%s ", sysrq_key_table
[i
]->help_msg
);
393 console_loglevel
= orig_log_level
;
395 spin_unlock_irqrestore(&sysrq_key_table_lock
, flags
);
399 * This function is called by the keyboard handler when SysRq is pressed
400 * and any other keycode arrives.
402 void handle_sysrq(int key
, struct tty_struct
*tty
)
406 __handle_sysrq(key
, tty
, 1);
408 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
)
417 spin_lock_irqsave(&sysrq_key_table_lock
, flags
);
418 if (__sysrq_get_key_op(key
) == remove_op_p
) {
419 __sysrq_put_key_op(key
, insert_op_p
);
424 spin_unlock_irqrestore(&sysrq_key_table_lock
, flags
);
428 int register_sysrq_key(int key
, struct sysrq_key_op
*op_p
)
430 return __sysrq_swap_key_ops(key
, op_p
, NULL
);
432 EXPORT_SYMBOL(register_sysrq_key
);
434 int unregister_sysrq_key(int key
, struct sysrq_key_op
*op_p
)
436 return __sysrq_swap_key_ops(key
, NULL
, op_p
);
438 EXPORT_SYMBOL(unregister_sysrq_key
);