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/config.h>
16 #include <linux/sched.h>
17 #include <linux/interrupt.h>
20 #include <linux/tty.h>
21 #include <linux/mount.h>
22 #include <linux/kdev_t.h>
23 #include <linux/major.h>
24 #include <linux/reboot.h>
25 #include <linux/sysrq.h>
26 #include <linux/kbd_kern.h>
27 #include <linux/quotaops.h>
28 #include <linux/smp_lock.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/kexec.h>
40 #include <asm/ptrace.h>
42 /* Whether we react on sysrq keys or just ignore them */
43 int sysrq_enabled = 1;
45 /* Loglevel sysrq handler */
46 static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs,
47 struct tty_struct *tty)
52 printk("Loglevel set to %d\n", i);
55 static struct sysrq_key_op sysrq_loglevel_op = {
56 .handler = sysrq_handle_loglevel,
57 .help_msg = "loglevel0-8",
58 .action_msg = "Changing Loglevel",
59 .enable_mask = SYSRQ_ENABLE_LOG,
63 /* SAK sysrq handler */
65 static void sysrq_handle_SAK(int key, struct pt_regs *pt_regs,
66 struct tty_struct *tty)
70 reset_vc(vc_cons[fg_console].d);
72 static struct sysrq_key_op sysrq_SAK_op = {
73 .handler = sysrq_handle_SAK,
76 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
81 /* unraw sysrq handler */
82 static void sysrq_handle_unraw(int key, struct pt_regs *pt_regs,
83 struct tty_struct *tty)
85 struct kbd_struct *kbd = &kbd_table[fg_console];
88 kbd->kbdmode = VC_XLATE;
90 static struct sysrq_key_op sysrq_unraw_op = {
91 .handler = sysrq_handle_unraw,
93 .action_msg = "Keyboard mode set to XLATE",
94 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
96 #endif /* CONFIG_VT */
99 /* crashdump sysrq handler */
100 static void sysrq_handle_crashdump(int key, struct pt_regs *pt_regs,
101 struct tty_struct *tty)
103 crash_kexec(pt_regs);
105 static struct sysrq_key_op sysrq_crashdump_op = {
106 .handler = sysrq_handle_crashdump,
107 .help_msg = "Crashdump",
108 .action_msg = "Trigger a crashdump",
109 .enable_mask = SYSRQ_ENABLE_DUMP,
113 /* reboot sysrq handler */
114 static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs,
115 struct tty_struct *tty)
118 machine_restart(NULL);
121 static struct sysrq_key_op sysrq_reboot_op = {
122 .handler = sysrq_handle_reboot,
123 .help_msg = "reBoot",
124 .action_msg = "Resetting",
125 .enable_mask = SYSRQ_ENABLE_BOOT,
128 static void sysrq_handle_sync(int key, struct pt_regs *pt_regs,
129 struct tty_struct *tty)
134 static struct sysrq_key_op sysrq_sync_op = {
135 .handler = sysrq_handle_sync,
137 .action_msg = "Emergency Sync",
138 .enable_mask = SYSRQ_ENABLE_SYNC,
141 static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs,
142 struct tty_struct *tty)
147 static struct sysrq_key_op sysrq_mountro_op = {
148 .handler = sysrq_handle_mountro,
149 .help_msg = "Unmount",
150 .action_msg = "Emergency Remount R/O",
151 .enable_mask = SYSRQ_ENABLE_REMOUNT,
154 /* END SYNC SYSRQ HANDLERS BLOCK */
157 /* SHOW SYSRQ HANDLERS BLOCK */
159 static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
160 struct tty_struct *tty)
165 static struct sysrq_key_op sysrq_showregs_op = {
166 .handler = sysrq_handle_showregs,
167 .help_msg = "showPc",
168 .action_msg = "Show Regs",
169 .enable_mask = SYSRQ_ENABLE_DUMP,
173 static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
174 struct tty_struct *tty)
178 static struct sysrq_key_op sysrq_showstate_op = {
179 .handler = sysrq_handle_showstate,
180 .help_msg = "showTasks",
181 .action_msg = "Show State",
182 .enable_mask = SYSRQ_ENABLE_DUMP,
186 static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
187 struct tty_struct *tty)
191 static struct sysrq_key_op sysrq_showmem_op = {
192 .handler = sysrq_handle_showmem,
193 .help_msg = "showMem",
194 .action_msg = "Show Memory",
195 .enable_mask = SYSRQ_ENABLE_DUMP,
198 /* SHOW SYSRQ HANDLERS BLOCK */
201 /* SIGNAL SYSRQ HANDLERS BLOCK */
203 /* signal sysrq helper function
204 * Sends a signal to all user processes */
205 static void send_sig_all(int sig)
207 struct task_struct *p;
209 for_each_process(p) {
210 if (p->mm && p->pid != 1)
211 /* Not swapper, init nor kernel thread */
216 static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
217 struct tty_struct *tty)
219 send_sig_all(SIGTERM);
220 console_loglevel = 8;
222 static struct sysrq_key_op sysrq_term_op = {
223 .handler = sysrq_handle_term,
225 .action_msg = "Terminate All Tasks",
226 .enable_mask = SYSRQ_ENABLE_SIGNAL,
229 static void moom_callback(void *ignored)
231 out_of_memory(GFP_KERNEL);
234 static DECLARE_WORK(moom_work, moom_callback, NULL);
236 static void sysrq_handle_moom(int key, struct pt_regs *pt_regs,
237 struct tty_struct *tty)
239 schedule_work(&moom_work);
241 static struct sysrq_key_op sysrq_moom_op = {
242 .handler = sysrq_handle_moom,
244 .action_msg = "Manual OOM execution",
247 static void sysrq_handle_kill(int key, struct pt_regs *pt_regs,
248 struct tty_struct *tty)
250 send_sig_all(SIGKILL);
251 console_loglevel = 8;
253 static struct sysrq_key_op sysrq_kill_op = {
254 .handler = sysrq_handle_kill,
256 .action_msg = "Kill All Tasks",
257 .enable_mask = SYSRQ_ENABLE_SIGNAL,
260 /* END SIGNAL SYSRQ HANDLERS BLOCK */
262 static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
263 struct tty_struct *tty)
265 normalize_rt_tasks();
267 static struct sysrq_key_op sysrq_unrt_op = {
268 .handler = sysrq_handle_unrt,
270 .action_msg = "Nice All RT Tasks",
271 .enable_mask = SYSRQ_ENABLE_RTNICE,
274 /* Key Operations table and lock */
275 static DEFINE_SPINLOCK(sysrq_key_table_lock);
276 #define SYSRQ_KEY_TABLE_LENGTH 36
277 static struct sysrq_key_op *sysrq_key_table[SYSRQ_KEY_TABLE_LENGTH] = {
278 /* 0 */ &sysrq_loglevel_op,
279 /* 1 */ &sysrq_loglevel_op,
280 /* 2 */ &sysrq_loglevel_op,
281 /* 3 */ &sysrq_loglevel_op,
282 /* 4 */ &sysrq_loglevel_op,
283 /* 5 */ &sysrq_loglevel_op,
284 /* 6 */ &sysrq_loglevel_op,
285 /* 7 */ &sysrq_loglevel_op,
286 /* 8 */ &sysrq_loglevel_op,
287 /* 9 */ &sysrq_loglevel_op,
288 /* a */ NULL, /* Don't use for system provided sysrqs,
289 it is handled specially on the sparc
290 and will never arrive */
291 /* b */ &sysrq_reboot_op,
293 /* c */ &sysrq_crashdump_op,
298 /* e */ &sysrq_term_op,
299 /* f */ &sysrq_moom_op,
302 /* i */ &sysrq_kill_op,
305 /* k */ &sysrq_SAK_op,
310 /* m */ &sysrq_showmem_op,
311 /* n */ &sysrq_unrt_op,
312 /* o */ NULL, /* This will often be registered
313 as 'Off' at init time */
314 /* p */ &sysrq_showregs_op,
317 /* r */ &sysrq_unraw_op,
321 /* s */ &sysrq_sync_op,
322 /* t */ &sysrq_showstate_op,
323 /* u */ &sysrq_mountro_op,
324 /* v */ NULL, /* May be assigned at init time by SMP VOYAGER */
331 /* key2index calculation, -1 on invalid index */
332 static int sysrq_key_table_key2index(int key) {
334 if ((key >= '0') && (key <= '9')) {
336 } else if ((key >= 'a') && (key <= 'z')) {
337 retval = key + 10 - 'a';
345 * get and put functions for the table, exposed to modules.
348 struct sysrq_key_op *__sysrq_get_key_op (int key) {
349 struct sysrq_key_op *op_p;
352 i = sysrq_key_table_key2index(key);
353 op_p = (i == -1) ? NULL : sysrq_key_table[i];
357 void __sysrq_put_key_op (int key, struct sysrq_key_op *op_p) {
360 i = sysrq_key_table_key2index(key);
362 sysrq_key_table[i] = op_p;
366 * This is the non-locking version of handle_sysrq
367 * It must/can only be called by sysrq key handlers,
368 * as they are inside of the lock
371 void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty, int check_mask)
373 struct sysrq_key_op *op_p;
378 spin_lock_irqsave(&sysrq_key_table_lock, flags);
379 orig_log_level = console_loglevel;
380 console_loglevel = 7;
381 printk(KERN_INFO "SysRq : ");
383 op_p = __sysrq_get_key_op(key);
385 /* Should we check for enabled operations (/proc/sysrq-trigger should not)
386 * and is the invoked operation enabled? */
387 if (!check_mask || sysrq_enabled == 1 ||
388 (sysrq_enabled & op_p->enable_mask)) {
389 printk ("%s\n", op_p->action_msg);
390 console_loglevel = orig_log_level;
391 op_p->handler(key, pt_regs, tty);
394 printk("This sysrq operation is disabled.\n");
397 /* Only print the help msg once per handler */
398 for (i=0; i<SYSRQ_KEY_TABLE_LENGTH; i++)
399 if (sysrq_key_table[i]) {
400 for (j=0; sysrq_key_table[i] != sysrq_key_table[j]; j++);
402 printk ("%s ", sysrq_key_table[i]->help_msg);
405 console_loglevel = orig_log_level;
407 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
411 * This function is called by the keyboard handler when SysRq is pressed
412 * and any other keycode arrives.
415 void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
419 __handle_sysrq(key, pt_regs, tty, 1);
422 int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
423 struct sysrq_key_op *remove_op_p) {
428 spin_lock_irqsave(&sysrq_key_table_lock, flags);
429 if (__sysrq_get_key_op(key) == remove_op_p) {
430 __sysrq_put_key_op(key, insert_op_p);
435 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
440 int register_sysrq_key(int key, struct sysrq_key_op *op_p)
442 return __sysrq_swap_key_ops(key, op_p, NULL);
445 int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
447 return __sysrq_swap_key_ops(key, NULL, op_p);
450 EXPORT_SYMBOL(handle_sysrq);
451 EXPORT_SYMBOL(register_sysrq_key);
452 EXPORT_SYMBOL(unregister_sysrq_key);