blob: 6b4d4d1e343da3332f413b8d47c65b00e9ff4a0b [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#include <asm/ptrace.h>
40
41/* Whether we react on sysrq keys or just ignore them */
42int sysrq_enabled = 1;
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -080045 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
61static void sysrq_handle_SAK(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -080062 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070079static void sysrq_handle_unraw(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -080080 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct kbd_struct *kbd = &kbd_table[fg_console];
83
84 if (kbd)
85 kbd->kbdmode = VC_XLATE;
86}
87static struct sysrq_key_op sysrq_unraw_op = {
88 .handler = sysrq_handle_unraw,
89 .help_msg = "unRaw",
90 .action_msg = "Keyboard mode set to XLATE",
91 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
92};
Andrew Mortonbf36b902006-03-25 03:07:08 -080093#else
94#define sysrq_unraw_op (*(struct sysrq_key_op *)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#endif /* CONFIG_VT */
96
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070097#ifdef CONFIG_KEXEC
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070098static void sysrq_handle_crashdump(int key, struct pt_regs *pt_regs,
99 struct tty_struct *tty)
100{
Alexander Nyberg6e274d12005-06-25 14:58:26 -0700101 crash_kexec(pt_regs);
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700102}
103static struct sysrq_key_op sysrq_crashdump_op = {
104 .handler = sysrq_handle_crashdump,
105 .help_msg = "Crashdump",
106 .action_msg = "Trigger a crashdump",
107 .enable_mask = SYSRQ_ENABLE_DUMP,
108};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800109#else
110#define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700111#endif
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800114 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Peter Zijlstrab2e9c7d2006-09-30 23:28:02 -0700116 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600118 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120static struct sysrq_key_op sysrq_reboot_op = {
121 .handler = sysrq_handle_reboot,
122 .help_msg = "reBoot",
123 .action_msg = "Resetting",
124 .enable_mask = SYSRQ_ENABLE_BOOT,
125};
126
127static void sysrq_handle_sync(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800128 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 emergency_sync();
131}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132static struct sysrq_key_op sysrq_sync_op = {
133 .handler = sysrq_handle_sync,
134 .help_msg = "Sync",
135 .action_msg = "Emergency Sync",
136 .enable_mask = SYSRQ_ENABLE_SYNC,
137};
138
139static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800140 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 emergency_remount();
143}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144static struct sysrq_key_op sysrq_mountro_op = {
145 .handler = sysrq_handle_mountro,
146 .help_msg = "Unmount",
147 .action_msg = "Emergency Remount R/O",
148 .enable_mask = SYSRQ_ENABLE_REMOUNT,
149};
150
Ingo Molnar8c645802006-07-03 00:24:56 -0700151#ifdef CONFIG_LOCKDEP
Andrew Mortonbf36b902006-03-25 03:07:08 -0800152static void sysrq_handle_showlocks(int key, struct pt_regs *pt_regs,
153 struct tty_struct *tty)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800154{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700155 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800156}
Ingo Molnar8c645802006-07-03 00:24:56 -0700157
Ingo Molnarde5097c2006-01-09 15:59:21 -0800158static struct sysrq_key_op sysrq_showlocks_op = {
159 .handler = sysrq_handle_showlocks,
160 .help_msg = "show-all-locks(D)",
161 .action_msg = "Show Locks Held",
162};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800163#else
164#define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800165#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800168 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
170 if (pt_regs)
171 show_regs(pt_regs);
172}
173static struct sysrq_key_op sysrq_showregs_op = {
174 .handler = sysrq_handle_showregs,
175 .help_msg = "showPc",
176 .action_msg = "Show Regs",
177 .enable_mask = SYSRQ_ENABLE_DUMP,
178};
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800181 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 show_state();
184}
185static struct sysrq_key_op sysrq_showstate_op = {
186 .handler = sysrq_handle_showstate,
187 .help_msg = "showTasks",
188 .action_msg = "Show State",
189 .enable_mask = SYSRQ_ENABLE_DUMP,
190};
191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800193 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 show_mem();
196}
197static struct sysrq_key_op sysrq_showmem_op = {
198 .handler = sysrq_handle_showmem,
199 .help_msg = "showMem",
200 .action_msg = "Show Memory",
201 .enable_mask = SYSRQ_ENABLE_DUMP,
202};
203
Andrew Mortonbf36b902006-03-25 03:07:08 -0800204/*
205 * Signal sysrq helper function. Sends a signal to all user processes.
206 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207static void send_sig_all(int sig)
208{
209 struct task_struct *p;
210
211 for_each_process(p) {
Sukadev Bhattiproluf400e192006-09-29 02:00:07 -0700212 if (p->mm && !is_init(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 /* Not swapper, init nor kernel thread */
214 force_sig(sig, p);
215 }
216}
217
218static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800219 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 send_sig_all(SIGTERM);
222 console_loglevel = 8;
223}
224static struct sysrq_key_op sysrq_term_op = {
225 .handler = sysrq_handle_term,
226 .help_msg = "tErm",
227 .action_msg = "Terminate All Tasks",
228 .enable_mask = SYSRQ_ENABLE_SIGNAL,
229};
230
231static void moom_callback(void *ignored)
232{
Andrew Mortonbf36b902006-03-25 03:07:08 -0800233 out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL],
234 GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
237static DECLARE_WORK(moom_work, moom_callback, NULL);
238
239static void sysrq_handle_moom(int key, struct pt_regs *pt_regs,
240 struct tty_struct *tty)
241{
242 schedule_work(&moom_work);
243}
244static struct sysrq_key_op sysrq_moom_op = {
245 .handler = sysrq_handle_moom,
246 .help_msg = "Full",
247 .action_msg = "Manual OOM execution",
248};
249
250static void sysrq_handle_kill(int key, struct pt_regs *pt_regs,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800251 struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 send_sig_all(SIGKILL);
254 console_loglevel = 8;
255}
256static struct sysrq_key_op sysrq_kill_op = {
257 .handler = sysrq_handle_kill,
258 .help_msg = "kIll",
259 .action_msg = "Kill All Tasks",
260 .enable_mask = SYSRQ_ENABLE_SIGNAL,
261};
262
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
264 struct tty_struct *tty)
265{
266 normalize_rt_tasks();
267}
268static struct sysrq_key_op sysrq_unrt_op = {
269 .handler = sysrq_handle_unrt,
270 .help_msg = "Nice",
271 .action_msg = "Nice All RT Tasks",
272 .enable_mask = SYSRQ_ENABLE_RTNICE,
273};
274
275/* Key Operations table and lock */
276static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800277
278static struct sysrq_key_op *sysrq_key_table[36] = {
279 &sysrq_loglevel_op, /* 0 */
280 &sysrq_loglevel_op, /* 1 */
281 &sysrq_loglevel_op, /* 2 */
282 &sysrq_loglevel_op, /* 3 */
283 &sysrq_loglevel_op, /* 4 */
284 &sysrq_loglevel_op, /* 5 */
285 &sysrq_loglevel_op, /* 6 */
286 &sysrq_loglevel_op, /* 7 */
287 &sysrq_loglevel_op, /* 8 */
288 &sysrq_loglevel_op, /* 9 */
289
290 /*
291 * Don't use for system provided sysrqs, it is handled specially on
292 * sparc and will never arrive
293 */
294 NULL, /* a */
295 &sysrq_reboot_op, /* b */
296 &sysrq_crashdump_op, /* c */
297 &sysrq_showlocks_op, /* d */
298 &sysrq_term_op, /* e */
299 &sysrq_moom_op, /* f */
300 NULL, /* g */
301 NULL, /* h */
302 &sysrq_kill_op, /* i */
303 NULL, /* j */
304 &sysrq_SAK_op, /* k */
305 NULL, /* l */
306 &sysrq_showmem_op, /* m */
307 &sysrq_unrt_op, /* n */
308 /* This will often be registered as 'Off' at init time */
309 NULL, /* o */
310 &sysrq_showregs_op, /* p */
311 NULL, /* q */
312 &sysrq_unraw_op, /* r */
313 &sysrq_sync_op, /* s */
314 &sysrq_showstate_op, /* t */
315 &sysrq_mountro_op, /* u */
316 /* May be assigned at init time by SMP VOYAGER */
317 NULL, /* v */
318 NULL, /* w */
319 NULL, /* x */
320 NULL, /* y */
321 NULL /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322};
323
324/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800325static int sysrq_key_table_key2index(int key)
326{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800328
329 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800331 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800333 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return retval;
336}
337
338/*
339 * get and put functions for the table, exposed to modules.
340 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800341struct sysrq_key_op *__sysrq_get_key_op(int key)
342{
343 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800347 if (i != -1)
348 op_p = sysrq_key_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 return op_p;
350}
351
Andrew Mortonbf36b902006-03-25 03:07:08 -0800352static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
353{
354 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (i != -1)
357 sysrq_key_table[i] = op_p;
358}
359
360/*
Andrew Mortonbf36b902006-03-25 03:07:08 -0800361 * This is the non-locking version of handle_sysrq. It must/can only be called
362 * by sysrq key handlers, as they are inside of the lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800364void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty,
365 int check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 struct sysrq_key_op *op_p;
368 int orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800369 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 unsigned long flags;
371
372 spin_lock_irqsave(&sysrq_key_table_lock, flags);
373 orig_log_level = console_loglevel;
374 console_loglevel = 7;
375 printk(KERN_INFO "SysRq : ");
376
377 op_p = __sysrq_get_key_op(key);
378 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800379 /*
380 * Should we check for enabled operations (/proc/sysrq-trigger
381 * should not) and is the invoked operation enabled?
382 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (!check_mask || sysrq_enabled == 1 ||
384 (sysrq_enabled & op_p->enable_mask)) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800385 printk("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 console_loglevel = orig_log_level;
387 op_p->handler(key, pt_regs, tty);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800388 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 printk("This sysrq operation is disabled.\n");
Andrew Mortonbf36b902006-03-25 03:07:08 -0800390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 } else {
392 printk("HELP : ");
393 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800394 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
395 if (sysrq_key_table[i]) {
396 int j;
397
398 for (j = 0; sysrq_key_table[i] !=
399 sysrq_key_table[j]; j++)
400 ;
401 if (j != i)
402 continue;
403 printk("%s ", sysrq_key_table[i]->help_msg);
404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 }
Andrew Mortonbf36b902006-03-25 03:07:08 -0800406 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 console_loglevel = orig_log_level;
408 }
409 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
410}
411
412/*
413 * This function is called by the keyboard handler when SysRq is pressed
414 * and any other keycode arrives.
415 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
417{
418 if (!sysrq_enabled)
419 return;
420 __handle_sysrq(key, pt_regs, tty, 1);
421}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800422EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
Adrian Bunkcf62ddc2005-11-08 21:39:47 -0800424static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800425 struct sysrq_key_op *remove_op_p)
426{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 int retval;
429 unsigned long flags;
430
431 spin_lock_irqsave(&sysrq_key_table_lock, flags);
432 if (__sysrq_get_key_op(key) == remove_op_p) {
433 __sysrq_put_key_op(key, insert_op_p);
434 retval = 0;
435 } else {
436 retval = -1;
437 }
438 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return retval;
440}
441
442int register_sysrq_key(int key, struct sysrq_key_op *op_p)
443{
444 return __sysrq_swap_key_ops(key, op_p, NULL);
445}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800446EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
449{
450 return __sysrq_swap_key_ops(key, NULL, op_p);
451}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452EXPORT_SYMBOL(unregister_sysrq_key);