blob: 4c0e08685705db8d7f6f871c7360bf7460231225 [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>
41
42/* Whether we react on sysrq keys or just ignore them */
43int sysrq_enabled = 1;
44
David Howells7d12e782006-10-05 14:55:46 +010045static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
47 int i;
48 i = key - '0';
49 console_loglevel = 7;
50 printk("Loglevel set to %d\n", i);
51 console_loglevel = i;
Andrew Mortonbf36b902006-03-25 03:07:08 -080052}
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static 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,
58};
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#ifdef CONFIG_VT
David Howells7d12e782006-10-05 14:55:46 +010061static void sysrq_handle_SAK(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 if (tty)
64 do_SAK(tty);
65 reset_vc(vc_cons[fg_console].d);
66}
67static struct sysrq_key_op sysrq_SAK_op = {
68 .handler = sysrq_handle_SAK,
69 .help_msg = "saK",
70 .action_msg = "SAK",
71 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
72};
Andrew Mortonbf36b902006-03-25 03:07:08 -080073#else
74#define sysrq_SAK_op (*(struct sysrq_key_op *)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#endif
76
77#ifdef CONFIG_VT
David Howells7d12e782006-10-05 14:55:46 +010078static void sysrq_handle_unraw(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070079{
80 struct kbd_struct *kbd = &kbd_table[fg_console];
81
82 if (kbd)
83 kbd->kbdmode = VC_XLATE;
84}
85static struct sysrq_key_op sysrq_unraw_op = {
86 .handler = sysrq_handle_unraw,
87 .help_msg = "unRaw",
88 .action_msg = "Keyboard mode set to XLATE",
89 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
90};
Andrew Mortonbf36b902006-03-25 03:07:08 -080091#else
92#define sysrq_unraw_op (*(struct sysrq_key_op *)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#endif /* CONFIG_VT */
94
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070095#ifdef CONFIG_KEXEC
David Howells7d12e782006-10-05 14:55:46 +010096static void sysrq_handle_crashdump(int key, struct tty_struct *tty)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070097{
David Howells7d12e782006-10-05 14:55:46 +010098 crash_kexec(get_irq_regs());
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070099}
100static 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,
105};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800106#else
107#define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700108#endif
109
David Howells7d12e782006-10-05 14:55:46 +0100110static void sysrq_handle_reboot(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Peter Zijlstrab2e9c7d2006-09-30 23:28:02 -0700112 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600114 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116static 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,
121};
122
David Howells7d12e782006-10-05 14:55:46 +0100123static void sysrq_handle_sync(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 emergency_sync();
126}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127static struct sysrq_key_op sysrq_sync_op = {
128 .handler = sysrq_handle_sync,
129 .help_msg = "Sync",
130 .action_msg = "Emergency Sync",
131 .enable_mask = SYSRQ_ENABLE_SYNC,
132};
133
David Howells7d12e782006-10-05 14:55:46 +0100134static void sysrq_handle_mountro(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135{
136 emergency_remount();
137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138static 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,
143};
144
Ingo Molnar8c645802006-07-03 00:24:56 -0700145#ifdef CONFIG_LOCKDEP
David Howells7d12e782006-10-05 14:55:46 +0100146static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800147{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700148 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800149}
Ingo Molnar8c645802006-07-03 00:24:56 -0700150
Ingo Molnarde5097c2006-01-09 15:59:21 -0800151static 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",
155};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800156#else
157#define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800158#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
David Howells7d12e782006-10-05 14:55:46 +0100160static void sysrq_handle_showregs(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
David Howells7d12e782006-10-05 14:55:46 +0100162 struct pt_regs *regs = get_irq_regs();
163 if (regs)
164 show_regs(regs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166static 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,
171};
172
David Howells7d12e782006-10-05 14:55:46 +0100173static void sysrq_handle_showstate(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
175 show_state();
176}
177static 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,
182};
183
David Howells7d12e782006-10-05 14:55:46 +0100184static void sysrq_handle_showmem(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 show_mem();
187}
188static 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,
193};
194
Andrew Mortonbf36b902006-03-25 03:07:08 -0800195/*
196 * Signal sysrq helper function. Sends a signal to all user processes.
197 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static void send_sig_all(int sig)
199{
200 struct task_struct *p;
201
202 for_each_process(p) {
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700203 if (p->mm && !is_init(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 /* Not swapper, init nor kernel thread */
205 force_sig(sig, p);
206 }
207}
208
David Howells7d12e782006-10-05 14:55:46 +0100209static void sysrq_handle_term(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 send_sig_all(SIGTERM);
212 console_loglevel = 8;
213}
214static struct sysrq_key_op sysrq_term_op = {
215 .handler = sysrq_handle_term,
216 .help_msg = "tErm",
217 .action_msg = "Terminate All Tasks",
218 .enable_mask = SYSRQ_ENABLE_SIGNAL,
219};
220
221static void moom_callback(void *ignored)
222{
Andrew Mortonbf36b902006-03-25 03:07:08 -0800223 out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL],
224 GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227static DECLARE_WORK(moom_work, moom_callback, NULL);
228
David Howells7d12e782006-10-05 14:55:46 +0100229static void sysrq_handle_moom(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 schedule_work(&moom_work);
232}
233static struct sysrq_key_op sysrq_moom_op = {
234 .handler = sysrq_handle_moom,
235 .help_msg = "Full",
236 .action_msg = "Manual OOM execution",
237};
238
David Howells7d12e782006-10-05 14:55:46 +0100239static void sysrq_handle_kill(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 send_sig_all(SIGKILL);
242 console_loglevel = 8;
243}
244static struct sysrq_key_op sysrq_kill_op = {
245 .handler = sysrq_handle_kill,
246 .help_msg = "kIll",
247 .action_msg = "Kill All Tasks",
248 .enable_mask = SYSRQ_ENABLE_SIGNAL,
249};
250
David Howells7d12e782006-10-05 14:55:46 +0100251static void sysrq_handle_unrt(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 normalize_rt_tasks();
254}
255static struct sysrq_key_op sysrq_unrt_op = {
256 .handler = sysrq_handle_unrt,
257 .help_msg = "Nice",
258 .action_msg = "Nice All RT Tasks",
259 .enable_mask = SYSRQ_ENABLE_RTNICE,
260};
261
262/* Key Operations table and lock */
263static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800264
265static 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 */
276
277 /*
278 * Don't use for system provided sysrqs, it is handled specially on
279 * sparc and will never arrive
280 */
281 NULL, /* a */
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 */
287 NULL, /* g */
288 NULL, /* h */
289 &sysrq_kill_op, /* i */
290 NULL, /* j */
291 &sysrq_SAK_op, /* k */
292 NULL, /* l */
293 &sysrq_showmem_op, /* m */
294 &sysrq_unrt_op, /* n */
295 /* This will often be registered as 'Off' at init time */
296 NULL, /* o */
297 &sysrq_showregs_op, /* p */
298 NULL, /* q */
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 */
304 NULL, /* v */
305 NULL, /* w */
306 NULL, /* x */
307 NULL, /* y */
308 NULL /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309};
310
311/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800312static int sysrq_key_table_key2index(int key)
313{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800315
316 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800318 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800320 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 return retval;
323}
324
325/*
326 * get and put functions for the table, exposed to modules.
327 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800328struct sysrq_key_op *__sysrq_get_key_op(int key)
329{
330 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800334 if (i != -1)
335 op_p = sysrq_key_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 return op_p;
337}
338
Andrew Mortonbf36b902006-03-25 03:07:08 -0800339static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
340{
341 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 if (i != -1)
344 sysrq_key_table[i] = op_p;
345}
346
347/*
Andrew Mortonbf36b902006-03-25 03:07:08 -0800348 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 */
David Howells7d12e782006-10-05 14:55:46 +0100351void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
353 struct sysrq_key_op *op_p;
354 int orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800355 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 unsigned long flags;
357
358 spin_lock_irqsave(&sysrq_key_table_lock, flags);
359 orig_log_level = console_loglevel;
360 console_loglevel = 7;
361 printk(KERN_INFO "SysRq : ");
362
363 op_p = __sysrq_get_key_op(key);
364 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800365 /*
366 * Should we check for enabled operations (/proc/sysrq-trigger
367 * should not) and is the invoked operation enabled?
368 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (!check_mask || sysrq_enabled == 1 ||
370 (sysrq_enabled & op_p->enable_mask)) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800371 printk("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 console_loglevel = orig_log_level;
David Howells7d12e782006-10-05 14:55:46 +0100373 op_p->handler(key, tty);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800374 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 printk("This sysrq operation is disabled.\n");
Andrew Mortonbf36b902006-03-25 03:07:08 -0800376 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 } else {
378 printk("HELP : ");
379 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800380 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
381 if (sysrq_key_table[i]) {
382 int j;
383
384 for (j = 0; sysrq_key_table[i] !=
385 sysrq_key_table[j]; j++)
386 ;
387 if (j != i)
388 continue;
389 printk("%s ", sysrq_key_table[i]->help_msg);
390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Andrew Mortonbf36b902006-03-25 03:07:08 -0800392 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 console_loglevel = orig_log_level;
394 }
395 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
396}
397
398/*
399 * This function is called by the keyboard handler when SysRq is pressed
400 * and any other keycode arrives.
401 */
David Howells7d12e782006-10-05 14:55:46 +0100402void handle_sysrq(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403{
404 if (!sysrq_enabled)
405 return;
David Howells7d12e782006-10-05 14:55:46 +0100406 __handle_sysrq(key, tty, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800408EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Adrian Bunkcf62ddc2005-11-08 21:39:47 -0800410static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800411 struct sysrq_key_op *remove_op_p)
412{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 int retval;
415 unsigned long flags;
416
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);
420 retval = 0;
421 } else {
422 retval = -1;
423 }
424 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return retval;
426}
427
428int register_sysrq_key(int key, struct sysrq_key_op *op_p)
429{
430 return __sysrq_swap_key_ops(key, op_p, NULL);
431}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800432EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
435{
436 return __sysrq_swap_key_ops(key, NULL, op_p);
437}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438EXPORT_SYMBOL(unregister_sysrq_key);