blob: 5f49280779fb0a98c567ec12e2350a634d95eece [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sched.h>
16#include <linux/interrupt.h>
17#include <linux/mm.h>
18#include <linux/fs.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>
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070037#include <linux/kexec.h>
David Howells7d12e782006-10-05 14:55:46 +010038#include <linux/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40#include <asm/ptrace.h>
Heiko Carstens2033b0c2006-10-06 16:38:42 +020041#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43/* Whether we react on sysrq keys or just ignore them */
44int sysrq_enabled = 1;
45
David Howells7d12e782006-10-05 14:55:46 +010046static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 int i;
49 i = key - '0';
50 console_loglevel = 7;
51 printk("Loglevel set to %d\n", i);
52 console_loglevel = i;
Andrew Mortonbf36b902006-03-25 03:07:08 -080053}
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct sysrq_key_op sysrq_loglevel_op = {
55 .handler = sysrq_handle_loglevel,
56 .help_msg = "loglevel0-8",
57 .action_msg = "Changing Loglevel",
58 .enable_mask = SYSRQ_ENABLE_LOG,
59};
60
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#ifdef CONFIG_VT
David Howells7d12e782006-10-05 14:55:46 +010062static void sysrq_handle_SAK(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070063{
64 if (tty)
65 do_SAK(tty);
66 reset_vc(vc_cons[fg_console].d);
67}
68static struct sysrq_key_op sysrq_SAK_op = {
69 .handler = sysrq_handle_SAK,
70 .help_msg = "saK",
71 .action_msg = "SAK",
72 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
73};
Andrew Mortonbf36b902006-03-25 03:07:08 -080074#else
75#define sysrq_SAK_op (*(struct sysrq_key_op *)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070076#endif
77
78#ifdef CONFIG_VT
David Howells7d12e782006-10-05 14:55:46 +010079static void sysrq_handle_unraw(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 struct kbd_struct *kbd = &kbd_table[fg_console];
82
83 if (kbd)
84 kbd->kbdmode = VC_XLATE;
85}
86static struct sysrq_key_op sysrq_unraw_op = {
87 .handler = sysrq_handle_unraw,
88 .help_msg = "unRaw",
89 .action_msg = "Keyboard mode set to XLATE",
90 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
91};
Andrew Mortonbf36b902006-03-25 03:07:08 -080092#else
93#define sysrq_unraw_op (*(struct sysrq_key_op *)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070094#endif /* CONFIG_VT */
95
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070096#ifdef CONFIG_KEXEC
David Howells7d12e782006-10-05 14:55:46 +010097static void sysrq_handle_crashdump(int key, struct tty_struct *tty)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070098{
David Howells7d12e782006-10-05 14:55:46 +010099 crash_kexec(get_irq_regs());
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700100}
101static struct sysrq_key_op sysrq_crashdump_op = {
102 .handler = sysrq_handle_crashdump,
103 .help_msg = "Crashdump",
104 .action_msg = "Trigger a crashdump",
105 .enable_mask = SYSRQ_ENABLE_DUMP,
106};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800107#else
108#define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700109#endif
110
David Howells7d12e782006-10-05 14:55:46 +0100111static void sysrq_handle_reboot(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Peter Zijlstrab2e9c7d2006-09-30 23:28:02 -0700113 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600115 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117static struct sysrq_key_op sysrq_reboot_op = {
118 .handler = sysrq_handle_reboot,
119 .help_msg = "reBoot",
120 .action_msg = "Resetting",
121 .enable_mask = SYSRQ_ENABLE_BOOT,
122};
123
David Howells7d12e782006-10-05 14:55:46 +0100124static void sysrq_handle_sync(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 emergency_sync();
127}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static struct sysrq_key_op sysrq_sync_op = {
129 .handler = sysrq_handle_sync,
130 .help_msg = "Sync",
131 .action_msg = "Emergency Sync",
132 .enable_mask = SYSRQ_ENABLE_SYNC,
133};
134
David Howells7d12e782006-10-05 14:55:46 +0100135static void sysrq_handle_mountro(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 emergency_remount();
138}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static struct sysrq_key_op sysrq_mountro_op = {
140 .handler = sysrq_handle_mountro,
141 .help_msg = "Unmount",
142 .action_msg = "Emergency Remount R/O",
143 .enable_mask = SYSRQ_ENABLE_REMOUNT,
144};
145
Ingo Molnar8c645802006-07-03 00:24:56 -0700146#ifdef CONFIG_LOCKDEP
David Howells7d12e782006-10-05 14:55:46 +0100147static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800148{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700149 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800150}
Ingo Molnar8c645802006-07-03 00:24:56 -0700151
Ingo Molnarde5097c2006-01-09 15:59:21 -0800152static struct sysrq_key_op sysrq_showlocks_op = {
153 .handler = sysrq_handle_showlocks,
154 .help_msg = "show-all-locks(D)",
155 .action_msg = "Show Locks Held",
156};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800157#else
158#define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800159#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
David Howells7d12e782006-10-05 14:55:46 +0100161static void sysrq_handle_showregs(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
David Howells7d12e782006-10-05 14:55:46 +0100163 struct pt_regs *regs = get_irq_regs();
164 if (regs)
165 show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167static struct sysrq_key_op sysrq_showregs_op = {
168 .handler = sysrq_handle_showregs,
169 .help_msg = "showPc",
170 .action_msg = "Show Regs",
171 .enable_mask = SYSRQ_ENABLE_DUMP,
172};
173
David Howells7d12e782006-10-05 14:55:46 +0100174static void sysrq_handle_showstate(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 show_state();
177}
178static 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,
183};
184
David Howells7d12e782006-10-05 14:55:46 +0100185static void sysrq_handle_showmem(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 show_mem();
188}
189static struct sysrq_key_op sysrq_showmem_op = {
190 .handler = sysrq_handle_showmem,
191 .help_msg = "showMem",
192 .action_msg = "Show Memory",
193 .enable_mask = SYSRQ_ENABLE_DUMP,
194};
195
Andrew Mortonbf36b902006-03-25 03:07:08 -0800196/*
197 * Signal sysrq helper function. Sends a signal to all user processes.
198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199static void send_sig_all(int sig)
200{
201 struct task_struct *p;
202
203 for_each_process(p) {
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700204 if (p->mm && !is_init(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 /* Not swapper, init nor kernel thread */
206 force_sig(sig, p);
207 }
208}
209
David Howells7d12e782006-10-05 14:55:46 +0100210static void sysrq_handle_term(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 send_sig_all(SIGTERM);
213 console_loglevel = 8;
214}
215static struct sysrq_key_op sysrq_term_op = {
216 .handler = sysrq_handle_term,
217 .help_msg = "tErm",
218 .action_msg = "Terminate All Tasks",
219 .enable_mask = SYSRQ_ENABLE_SIGNAL,
220};
221
222static void moom_callback(void *ignored)
223{
Andrew Mortonbf36b902006-03-25 03:07:08 -0800224 out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL],
225 GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
228static DECLARE_WORK(moom_work, moom_callback, NULL);
229
David Howells7d12e782006-10-05 14:55:46 +0100230static void sysrq_handle_moom(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 schedule_work(&moom_work);
233}
234static struct sysrq_key_op sysrq_moom_op = {
235 .handler = sysrq_handle_moom,
236 .help_msg = "Full",
237 .action_msg = "Manual OOM execution",
238};
239
David Howells7d12e782006-10-05 14:55:46 +0100240static void sysrq_handle_kill(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 send_sig_all(SIGKILL);
243 console_loglevel = 8;
244}
245static struct sysrq_key_op sysrq_kill_op = {
246 .handler = sysrq_handle_kill,
247 .help_msg = "kIll",
248 .action_msg = "Kill All Tasks",
249 .enable_mask = SYSRQ_ENABLE_SIGNAL,
250};
251
David Howells7d12e782006-10-05 14:55:46 +0100252static void sysrq_handle_unrt(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 normalize_rt_tasks();
255}
256static struct sysrq_key_op sysrq_unrt_op = {
257 .handler = sysrq_handle_unrt,
258 .help_msg = "Nice",
259 .action_msg = "Nice All RT Tasks",
260 .enable_mask = SYSRQ_ENABLE_RTNICE,
261};
262
263/* Key Operations table and lock */
264static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800265
266static struct sysrq_key_op *sysrq_key_table[36] = {
267 &sysrq_loglevel_op, /* 0 */
268 &sysrq_loglevel_op, /* 1 */
269 &sysrq_loglevel_op, /* 2 */
270 &sysrq_loglevel_op, /* 3 */
271 &sysrq_loglevel_op, /* 4 */
272 &sysrq_loglevel_op, /* 5 */
273 &sysrq_loglevel_op, /* 6 */
274 &sysrq_loglevel_op, /* 7 */
275 &sysrq_loglevel_op, /* 8 */
276 &sysrq_loglevel_op, /* 9 */
277
278 /*
279 * Don't use for system provided sysrqs, it is handled specially on
280 * sparc and will never arrive
281 */
282 NULL, /* a */
283 &sysrq_reboot_op, /* b */
284 &sysrq_crashdump_op, /* c */
285 &sysrq_showlocks_op, /* d */
286 &sysrq_term_op, /* e */
287 &sysrq_moom_op, /* f */
288 NULL, /* g */
289 NULL, /* h */
290 &sysrq_kill_op, /* i */
291 NULL, /* j */
292 &sysrq_SAK_op, /* k */
293 NULL, /* l */
294 &sysrq_showmem_op, /* m */
295 &sysrq_unrt_op, /* n */
296 /* This will often be registered as 'Off' at init time */
297 NULL, /* o */
298 &sysrq_showregs_op, /* p */
299 NULL, /* q */
300 &sysrq_unraw_op, /* r */
301 &sysrq_sync_op, /* s */
302 &sysrq_showstate_op, /* t */
303 &sysrq_mountro_op, /* u */
304 /* May be assigned at init time by SMP VOYAGER */
305 NULL, /* v */
306 NULL, /* w */
307 NULL, /* x */
308 NULL, /* y */
309 NULL /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310};
311
312/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800313static int sysrq_key_table_key2index(int key)
314{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800316
317 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800319 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800321 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return retval;
324}
325
326/*
327 * get and put functions for the table, exposed to modules.
328 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800329struct sysrq_key_op *__sysrq_get_key_op(int key)
330{
331 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800333
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800335 if (i != -1)
336 op_p = sysrq_key_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return op_p;
338}
339
Andrew Mortonbf36b902006-03-25 03:07:08 -0800340static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
341{
342 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 if (i != -1)
345 sysrq_key_table[i] = op_p;
346}
347
348/*
Andrew Mortonbf36b902006-03-25 03:07:08 -0800349 * This is the non-locking version of handle_sysrq. It must/can only be called
350 * by sysrq key handlers, as they are inside of the lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 */
David Howells7d12e782006-10-05 14:55:46 +0100352void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 struct sysrq_key_op *op_p;
355 int orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800356 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 unsigned long flags;
358
359 spin_lock_irqsave(&sysrq_key_table_lock, flags);
360 orig_log_level = console_loglevel;
361 console_loglevel = 7;
362 printk(KERN_INFO "SysRq : ");
363
364 op_p = __sysrq_get_key_op(key);
365 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800366 /*
367 * Should we check for enabled operations (/proc/sysrq-trigger
368 * should not) and is the invoked operation enabled?
369 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (!check_mask || sysrq_enabled == 1 ||
371 (sysrq_enabled & op_p->enable_mask)) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800372 printk("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 console_loglevel = orig_log_level;
David Howells7d12e782006-10-05 14:55:46 +0100374 op_p->handler(key, tty);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800375 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 printk("This sysrq operation is disabled.\n");
Andrew Mortonbf36b902006-03-25 03:07:08 -0800377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 } else {
379 printk("HELP : ");
380 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800381 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
382 if (sysrq_key_table[i]) {
383 int j;
384
385 for (j = 0; sysrq_key_table[i] !=
386 sysrq_key_table[j]; j++)
387 ;
388 if (j != i)
389 continue;
390 printk("%s ", sysrq_key_table[i]->help_msg);
391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
Andrew Mortonbf36b902006-03-25 03:07:08 -0800393 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 console_loglevel = orig_log_level;
395 }
396 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
397}
398
399/*
400 * This function is called by the keyboard handler when SysRq is pressed
401 * and any other keycode arrives.
402 */
David Howells7d12e782006-10-05 14:55:46 +0100403void handle_sysrq(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404{
405 if (!sysrq_enabled)
406 return;
David Howells7d12e782006-10-05 14:55:46 +0100407 __handle_sysrq(key, tty, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800409EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Adrian Bunkcf62ddc2005-11-08 21:39:47 -0800411static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800412 struct sysrq_key_op *remove_op_p)
413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 int retval;
416 unsigned long flags;
417
418 spin_lock_irqsave(&sysrq_key_table_lock, flags);
419 if (__sysrq_get_key_op(key) == remove_op_p) {
420 __sysrq_put_key_op(key, insert_op_p);
421 retval = 0;
422 } else {
423 retval = -1;
424 }
425 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 return retval;
427}
428
429int register_sysrq_key(int key, struct sysrq_key_op *op_p)
430{
431 return __sysrq_swap_key_ops(key, op_p, NULL);
432}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800433EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
436{
437 return __sysrq_swap_key_ops(key, NULL, op_p);
438}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439EXPORT_SYMBOL(unregister_sysrq_key);