blob: 878ac0c2cc6864a84c7df230c5c12efee1944607 [file] [log] [blame]
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Linux Magic System Request Key Hacks
3 *
4 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
5 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
6 *
7 * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
8 * overhauled to use key registration
9 * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070010 *
11 * Copyright (c) 2010 Dmitry Torokhov
12 * Input handler conversion
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070015#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/sched.h>
18#include <linux/interrupt.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/tty.h>
22#include <linux/mount.h>
23#include <linux/kdev_t.h>
24#include <linux/major.h>
25#include <linux/reboot.h>
26#include <linux/sysrq.h>
27#include <linux/kbd_kern.h>
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -070028#include <linux/proc_fs.h>
Ingo Molnarc1dc0b92009-08-02 11:28:21 +020029#include <linux/nmi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/quotaops.h>
Ingo Molnarcdd6c482009-09-21 12:02:48 +020031#include <linux/perf_event.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/kernel.h>
33#include <linux/module.h>
34#include <linux/suspend.h>
35#include <linux/writeback.h>
36#include <linux/buffer_head.h> /* for fsync_bdev() */
37#include <linux/swap.h>
38#include <linux/spinlock.h>
39#include <linux/vt_kern.h>
40#include <linux/workqueue.h>
Ingo Molnar88ad0bf62007-02-16 01:28:16 -080041#include <linux/hrtimer.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070042#include <linux/oom.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090043#include <linux/slab.h>
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070044#include <linux/input.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include <asm/ptrace.h>
Heiko Carstens2033b0c2006-10-06 16:38:42 +020047#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49/* Whether we react on sysrq keys or just ignore them */
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070050static int __read_mostly sysrq_enabled = 1;
51static bool __read_mostly sysrq_always_enabled;
Ingo Molnar5d6f6472006-12-13 00:34:36 -080052
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070053static bool sysrq_on(void)
Ingo Molnar5d6f6472006-12-13 00:34:36 -080054{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070055 return sysrq_enabled || sysrq_always_enabled;
Ingo Molnar5d6f6472006-12-13 00:34:36 -080056}
57
58/*
59 * A value of 1 means 'all', other nonzero values are an op mask:
60 */
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070061static bool sysrq_on_mask(int mask)
Ingo Molnar5d6f6472006-12-13 00:34:36 -080062{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070063 return sysrq_always_enabled ||
64 sysrq_enabled == 1 ||
65 (sysrq_enabled & mask);
Ingo Molnar5d6f6472006-12-13 00:34:36 -080066}
67
68static int __init sysrq_always_enabled_setup(char *str)
69{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070070 sysrq_always_enabled = true;
71 pr_info("sysrq always enabled.\n");
Ingo Molnar5d6f6472006-12-13 00:34:36 -080072
73 return 1;
74}
75
76__setup("sysrq_always_enabled", sysrq_always_enabled_setup);
77
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
David Howells7d12e782006-10-05 14:55:46 +010079static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070080{
81 int i;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 i = key - '0';
84 console_loglevel = 7;
85 printk("Loglevel set to %d\n", i);
86 console_loglevel = i;
Andrew Mortonbf36b902006-03-25 03:07:08 -080087}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088static struct sysrq_key_op sysrq_loglevel_op = {
89 .handler = sysrq_handle_loglevel,
Randy Dunlap208b95c2009-01-06 14:41:13 -080090 .help_msg = "loglevel(0-9)",
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 .action_msg = "Changing Loglevel",
92 .enable_mask = SYSRQ_ENABLE_LOG,
93};
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095#ifdef CONFIG_VT
David Howells7d12e782006-10-05 14:55:46 +010096static void sysrq_handle_SAK(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
Eric W. Biederman8b6312f2007-02-10 01:44:34 -080098 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -080099 schedule_work(SAK_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101static struct sysrq_key_op sysrq_SAK_op = {
102 .handler = sysrq_handle_SAK,
103 .help_msg = "saK",
104 .action_msg = "SAK",
105 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
106};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800107#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700108#define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109#endif
110
111#ifdef CONFIG_VT
David Howells7d12e782006-10-05 14:55:46 +0100112static void sysrq_handle_unraw(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 struct kbd_struct *kbd = &kbd_table[fg_console];
115
116 if (kbd)
Bill Nottingham2e8ecb92007-10-16 23:29:38 -0700117 kbd->kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119static struct sysrq_key_op sysrq_unraw_op = {
120 .handler = sysrq_handle_unraw,
121 .help_msg = "unRaw",
Bill Nottingham2e8ecb92007-10-16 23:29:38 -0700122 .action_msg = "Keyboard mode set to system default",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
124};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800125#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700126#define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127#endif /* CONFIG_VT */
128
Neil Hormand6580a92009-06-17 16:28:17 -0700129static void sysrq_handle_crash(int key, struct tty_struct *tty)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700130{
Neil Hormand6580a92009-06-17 16:28:17 -0700131 char *killer = NULL;
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700132
133 panic_on_oops = 1; /* force panic */
134 wmb();
Neil Hormand6580a92009-06-17 16:28:17 -0700135 *killer = 1;
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700136}
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700137static struct sysrq_key_op sysrq_crash_op = {
Neil Hormand6580a92009-06-17 16:28:17 -0700138 .handler = sysrq_handle_crash,
139 .help_msg = "Crash",
140 .action_msg = "Trigger a crash",
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700141 .enable_mask = SYSRQ_ENABLE_DUMP,
142};
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700143
David Howells7d12e782006-10-05 14:55:46 +0100144static void sysrq_handle_reboot(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Peter Zijlstrab2e9c7d2006-09-30 23:28:02 -0700146 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600148 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150static struct sysrq_key_op sysrq_reboot_op = {
151 .handler = sysrq_handle_reboot,
152 .help_msg = "reBoot",
153 .action_msg = "Resetting",
154 .enable_mask = SYSRQ_ENABLE_BOOT,
155};
156
David Howells7d12e782006-10-05 14:55:46 +0100157static void sysrq_handle_sync(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 emergency_sync();
160}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161static struct sysrq_key_op sysrq_sync_op = {
162 .handler = sysrq_handle_sync,
163 .help_msg = "Sync",
164 .action_msg = "Emergency Sync",
165 .enable_mask = SYSRQ_ENABLE_SYNC,
166};
167
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800168static void sysrq_handle_show_timers(int key, struct tty_struct *tty)
169{
170 sysrq_timer_list_show();
171}
172
173static struct sysrq_key_op sysrq_show_timers_op = {
174 .handler = sysrq_handle_show_timers,
175 .help_msg = "show-all-timers(Q)",
Thomas Gleixner322acf62008-10-20 12:33:14 +0200176 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800177};
178
David Howells7d12e782006-10-05 14:55:46 +0100179static void sysrq_handle_mountro(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
181 emergency_remount();
182}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183static struct sysrq_key_op sysrq_mountro_op = {
184 .handler = sysrq_handle_mountro,
185 .help_msg = "Unmount",
186 .action_msg = "Emergency Remount R/O",
187 .enable_mask = SYSRQ_ENABLE_REMOUNT,
188};
189
Ingo Molnar8c645802006-07-03 00:24:56 -0700190#ifdef CONFIG_LOCKDEP
David Howells7d12e782006-10-05 14:55:46 +0100191static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800192{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700193 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800194}
Ingo Molnar8c645802006-07-03 00:24:56 -0700195
Ingo Molnarde5097c2006-01-09 15:59:21 -0800196static struct sysrq_key_op sysrq_showlocks_op = {
197 .handler = sysrq_handle_showlocks,
198 .help_msg = "show-all-locks(D)",
199 .action_msg = "Show Locks Held",
200};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800201#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700202#define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800203#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Rik van Riel5045bca2008-04-29 00:59:21 -0700205#ifdef CONFIG_SMP
206static DEFINE_SPINLOCK(show_lock);
207
208static void showacpu(void *dummy)
209{
210 unsigned long flags;
211
212 /* Idle CPUs have no interesting backtrace. */
213 if (idle_cpu(smp_processor_id()))
214 return;
215
216 spin_lock_irqsave(&show_lock, flags);
217 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
218 show_stack(NULL, NULL);
219 spin_unlock_irqrestore(&show_lock, flags);
220}
221
222static void sysrq_showregs_othercpus(struct work_struct *dummy)
223{
Ingo Molnar8b604d52008-06-27 11:52:45 +0200224 smp_call_function(showacpu, NULL, 0);
Rik van Riel5045bca2008-04-29 00:59:21 -0700225}
226
227static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
228
229static void sysrq_handle_showallcpus(int key, struct tty_struct *tty)
230{
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200231 /*
232 * Fall back to the workqueue based printing if the
233 * backtrace printing did not succeed or the
234 * architecture has no support for it:
235 */
236 if (!trigger_all_cpu_backtrace()) {
237 struct pt_regs *regs = get_irq_regs();
238
239 if (regs) {
240 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
241 show_regs(regs);
242 }
243 schedule_work(&sysrq_showallcpus);
244 }
Rik van Riel5045bca2008-04-29 00:59:21 -0700245}
246
247static struct sysrq_key_op sysrq_showallcpus_op = {
248 .handler = sysrq_handle_showallcpus,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800249 .help_msg = "show-backtrace-all-active-cpus(L)",
Rik van Riel5045bca2008-04-29 00:59:21 -0700250 .action_msg = "Show backtrace of all active CPUs",
251 .enable_mask = SYSRQ_ENABLE_DUMP,
252};
253#endif
254
David Howells7d12e782006-10-05 14:55:46 +0100255static void sysrq_handle_showregs(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
David Howells7d12e782006-10-05 14:55:46 +0100257 struct pt_regs *regs = get_irq_regs();
258 if (regs)
259 show_regs(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200260 perf_event_print_debug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262static struct sysrq_key_op sysrq_showregs_op = {
263 .handler = sysrq_handle_showregs,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800264 .help_msg = "show-registers(P)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 .action_msg = "Show Regs",
266 .enable_mask = SYSRQ_ENABLE_DUMP,
267};
268
David Howells7d12e782006-10-05 14:55:46 +0100269static void sysrq_handle_showstate(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 show_state();
272}
273static struct sysrq_key_op sysrq_showstate_op = {
274 .handler = sysrq_handle_showstate,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800275 .help_msg = "show-task-states(T)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 .action_msg = "Show State",
277 .enable_mask = SYSRQ_ENABLE_DUMP,
278};
279
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800280static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty)
281{
282 show_state_filter(TASK_UNINTERRUPTIBLE);
283}
284static struct sysrq_key_op sysrq_showstate_blocked_op = {
285 .handler = sysrq_handle_showstate_blocked,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800286 .help_msg = "show-blocked-tasks(W)",
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800287 .action_msg = "Show Blocked State",
288 .enable_mask = SYSRQ_ENABLE_DUMP,
289};
290
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100291#ifdef CONFIG_TRACING
292#include <linux/ftrace.h>
293
294static void sysrq_ftrace_dump(int key, struct tty_struct *tty)
295{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200296 ftrace_dump(DUMP_ALL);
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100297}
298static struct sysrq_key_op sysrq_ftrace_dump_op = {
299 .handler = sysrq_ftrace_dump,
Randy Dunlap3871f2f2008-12-24 16:06:57 -0800300 .help_msg = "dump-ftrace-buffer(Z)",
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100301 .action_msg = "Dump ftrace buffer",
302 .enable_mask = SYSRQ_ENABLE_DUMP,
303};
304#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700305#define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100306#endif
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800307
David Howells7d12e782006-10-05 14:55:46 +0100308static void sysrq_handle_showmem(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 show_mem();
311}
312static struct sysrq_key_op sysrq_showmem_op = {
313 .handler = sysrq_handle_showmem,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800314 .help_msg = "show-memory-usage(M)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 .action_msg = "Show Memory",
316 .enable_mask = SYSRQ_ENABLE_DUMP,
317};
318
Andrew Mortonbf36b902006-03-25 03:07:08 -0800319/*
320 * Signal sysrq helper function. Sends a signal to all user processes.
321 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322static void send_sig_all(int sig)
323{
324 struct task_struct *p;
325
326 for_each_process(p) {
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700327 if (p->mm && !is_global_init(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 /* Not swapper, init nor kernel thread */
329 force_sig(sig, p);
330 }
331}
332
David Howells7d12e782006-10-05 14:55:46 +0100333static void sysrq_handle_term(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 send_sig_all(SIGTERM);
336 console_loglevel = 8;
337}
338static struct sysrq_key_op sysrq_term_op = {
339 .handler = sysrq_handle_term,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800340 .help_msg = "terminate-all-tasks(E)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 .action_msg = "Terminate All Tasks",
342 .enable_mask = SYSRQ_ENABLE_SIGNAL,
343};
344
David Howells65f27f32006-11-22 14:55:48 +0000345static void moom_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
KAMEZAWA Hiroyuki4365a562009-12-15 16:45:33 -0800347 out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
David Howells65f27f32006-11-22 14:55:48 +0000350static DECLARE_WORK(moom_work, moom_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
David Howells7d12e782006-10-05 14:55:46 +0100352static void sysrq_handle_moom(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353{
354 schedule_work(&moom_work);
355}
356static struct sysrq_key_op sysrq_moom_op = {
357 .handler = sysrq_handle_moom,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800358 .help_msg = "memory-full-oom-kill(F)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 .action_msg = "Manual OOM execution",
Naohiro Ooiwab4236f82008-10-15 22:01:43 -0700360 .enable_mask = SYSRQ_ENABLE_SIGNAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361};
362
Eric Sandeenc2d75432009-03-31 15:23:46 -0700363#ifdef CONFIG_BLOCK
364static void sysrq_handle_thaw(int key, struct tty_struct *tty)
365{
366 emergency_thaw_all();
367}
368static struct sysrq_key_op sysrq_thaw_op = {
369 .handler = sysrq_handle_thaw,
370 .help_msg = "thaw-filesystems(J)",
371 .action_msg = "Emergency Thaw of all frozen filesystems",
372 .enable_mask = SYSRQ_ENABLE_SIGNAL,
373};
374#endif
375
David Howells7d12e782006-10-05 14:55:46 +0100376static void sysrq_handle_kill(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 send_sig_all(SIGKILL);
379 console_loglevel = 8;
380}
381static struct sysrq_key_op sysrq_kill_op = {
382 .handler = sysrq_handle_kill,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800383 .help_msg = "kill-all-tasks(I)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 .action_msg = "Kill All Tasks",
385 .enable_mask = SYSRQ_ENABLE_SIGNAL,
386};
387
David Howells7d12e782006-10-05 14:55:46 +0100388static void sysrq_handle_unrt(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 normalize_rt_tasks();
391}
392static struct sysrq_key_op sysrq_unrt_op = {
393 .handler = sysrq_handle_unrt,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800394 .help_msg = "nice-all-RT-tasks(N)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 .action_msg = "Nice All RT Tasks",
396 .enable_mask = SYSRQ_ENABLE_RTNICE,
397};
398
399/* Key Operations table and lock */
400static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800401
402static struct sysrq_key_op *sysrq_key_table[36] = {
403 &sysrq_loglevel_op, /* 0 */
404 &sysrq_loglevel_op, /* 1 */
405 &sysrq_loglevel_op, /* 2 */
406 &sysrq_loglevel_op, /* 3 */
407 &sysrq_loglevel_op, /* 4 */
408 &sysrq_loglevel_op, /* 5 */
409 &sysrq_loglevel_op, /* 6 */
410 &sysrq_loglevel_op, /* 7 */
411 &sysrq_loglevel_op, /* 8 */
412 &sysrq_loglevel_op, /* 9 */
413
414 /*
Randy Dunlapd346cce2007-01-31 23:48:17 -0800415 * a: Don't use for system provided sysrqs, it is handled specially on
416 * sparc and will never arrive.
Andrew Mortonbf36b902006-03-25 03:07:08 -0800417 */
418 NULL, /* a */
419 &sysrq_reboot_op, /* b */
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700420 &sysrq_crash_op, /* c & ibm_emac driver debug */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800421 &sysrq_showlocks_op, /* d */
422 &sysrq_term_op, /* e */
423 &sysrq_moom_op, /* f */
Jason Wessel364b5b72009-05-13 21:56:59 -0500424 /* g: May be registered for the kernel debugger */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800425 NULL, /* g */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700426 NULL, /* h - reserved for help */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800427 &sysrq_kill_op, /* i */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700428#ifdef CONFIG_BLOCK
429 &sysrq_thaw_op, /* j */
430#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800431 NULL, /* j */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700432#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800433 &sysrq_SAK_op, /* k */
Rik van Riel5045bca2008-04-29 00:59:21 -0700434#ifdef CONFIG_SMP
435 &sysrq_showallcpus_op, /* l */
436#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800437 NULL, /* l */
Rik van Riel5045bca2008-04-29 00:59:21 -0700438#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800439 &sysrq_showmem_op, /* m */
440 &sysrq_unrt_op, /* n */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800441 /* o: This will often be registered as 'Off' at init time */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800442 NULL, /* o */
443 &sysrq_showregs_op, /* p */
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800444 &sysrq_show_timers_op, /* q */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800445 &sysrq_unraw_op, /* r */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800446 &sysrq_sync_op, /* s */
447 &sysrq_showstate_op, /* t */
448 &sysrq_mountro_op, /* u */
Jason Wessel364b5b72009-05-13 21:56:59 -0500449 /* v: May be registered for frame buffer console restore */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800450 NULL, /* v */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800451 &sysrq_showstate_blocked_op, /* w */
452 /* x: May be registered on ppc/powerpc for xmon */
453 NULL, /* x */
David S. Miller93dae5b2008-05-19 23:46:00 -0700454 /* y: May be registered on sparc64 for global register dump */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800455 NULL, /* y */
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100456 &sysrq_ftrace_dump_op, /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457};
458
459/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800460static int sysrq_key_table_key2index(int key)
461{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800463
464 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800466 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800468 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return retval;
471}
472
473/*
474 * get and put functions for the table, exposed to modules.
475 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800476struct sysrq_key_op *__sysrq_get_key_op(int key)
477{
478 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800482 if (i != -1)
483 op_p = sysrq_key_table[i];
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return op_p;
486}
487
Andrew Mortonbf36b902006-03-25 03:07:08 -0800488static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
489{
490 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (i != -1)
493 sysrq_key_table[i] = op_p;
494}
495
Jason Wesseledd63cb2010-07-21 19:27:07 -0500496void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct sysrq_key_op *op_p;
499 int orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800500 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 unsigned long flags;
502
503 spin_lock_irqsave(&sysrq_key_table_lock, flags);
Andy Whitcroftfb144ad2009-01-15 13:50:52 -0800504 /*
505 * Raise the apparent loglevel to maximum so that the sysrq header
506 * is shown to provide the user with positive feedback. We do not
507 * simply emit this at KERN_EMERG as that would change message
508 * routing in the consumers of /proc/kmsg.
509 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 orig_log_level = console_loglevel;
511 console_loglevel = 7;
512 printk(KERN_INFO "SysRq : ");
513
514 op_p = __sysrq_get_key_op(key);
515 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800516 /*
517 * Should we check for enabled operations (/proc/sysrq-trigger
518 * should not) and is the invoked operation enabled?
519 */
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800520 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800521 printk("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 console_loglevel = orig_log_level;
David Howells7d12e782006-10-05 14:55:46 +0100523 op_p->handler(key, tty);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800524 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 printk("This sysrq operation is disabled.\n");
Andrew Mortonbf36b902006-03-25 03:07:08 -0800526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 } else {
528 printk("HELP : ");
529 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800530 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
531 if (sysrq_key_table[i]) {
532 int j;
533
534 for (j = 0; sysrq_key_table[i] !=
535 sysrq_key_table[j]; j++)
536 ;
537 if (j != i)
538 continue;
539 printk("%s ", sysrq_key_table[i]->help_msg);
540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 }
Andrew Mortonbf36b902006-03-25 03:07:08 -0800542 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 console_loglevel = orig_log_level;
544 }
545 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
546}
547
David Howells7d12e782006-10-05 14:55:46 +0100548void handle_sysrq(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800550 if (sysrq_on())
551 __handle_sysrq(key, tty, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800553EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700555#ifdef CONFIG_INPUT
556
557/* Simple translation table for the SysRq keys */
558static const unsigned char sysrq_xlate[KEY_MAX + 1] =
559 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
560 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
561 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
562 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
563 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
564 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
565 "\r\000/"; /* 0x60 - 0x6f */
566
567static bool sysrq_down;
568static int sysrq_alt_use;
569static int sysrq_alt;
570
571static bool sysrq_filter(struct input_handle *handle, unsigned int type,
572 unsigned int code, int value)
573{
574 if (type != EV_KEY)
575 goto out;
576
577 switch (code) {
578
579 case KEY_LEFTALT:
580 case KEY_RIGHTALT:
581 if (value)
582 sysrq_alt = code;
Dmitry Torokhovf5dec512010-06-09 08:13:06 -0700583 else {
584 if (sysrq_down && code == sysrq_alt_use)
585 sysrq_down = false;
586
587 sysrq_alt = 0;
588 }
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700589 break;
590
591 case KEY_SYSRQ:
592 if (value == 1 && sysrq_alt) {
593 sysrq_down = true;
594 sysrq_alt_use = sysrq_alt;
595 }
596 break;
597
598 default:
599 if (sysrq_down && value && value != 2)
600 __handle_sysrq(sysrq_xlate[code], NULL, 1);
601 break;
602 }
603
604out:
605 return sysrq_down;
606}
607
608static int sysrq_connect(struct input_handler *handler,
609 struct input_dev *dev,
610 const struct input_device_id *id)
611{
612 struct input_handle *handle;
613 int error;
614
615 sysrq_down = false;
616 sysrq_alt = 0;
617
618 handle = kzalloc(sizeof(struct input_handle), GFP_KERNEL);
619 if (!handle)
620 return -ENOMEM;
621
622 handle->dev = dev;
623 handle->handler = handler;
624 handle->name = "sysrq";
625
626 error = input_register_handle(handle);
627 if (error) {
628 pr_err("Failed to register input sysrq handler, error %d\n",
629 error);
630 goto err_free;
631 }
632
633 error = input_open_device(handle);
634 if (error) {
635 pr_err("Failed to open input device, error %d\n", error);
636 goto err_unregister;
637 }
638
639 return 0;
640
641 err_unregister:
642 input_unregister_handle(handle);
643 err_free:
644 kfree(handle);
645 return error;
646}
647
648static void sysrq_disconnect(struct input_handle *handle)
649{
650 input_close_device(handle);
651 input_unregister_handle(handle);
652 kfree(handle);
653}
654
655/*
656 * We are matching on KEY_LEFTALT insteard of KEY_SYSRQ because not all
657 * keyboards have SysRq ikey predefined and so user may add it to keymap
658 * later, but we expect all such keyboards to have left alt.
659 */
660static const struct input_device_id sysrq_ids[] = {
661 {
662 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
663 INPUT_DEVICE_ID_MATCH_KEYBIT,
664 .evbit = { BIT_MASK(EV_KEY) },
665 .keybit = { BIT_MASK(KEY_LEFTALT) },
666 },
667 { },
668};
669
670static struct input_handler sysrq_handler = {
671 .filter = sysrq_filter,
672 .connect = sysrq_connect,
673 .disconnect = sysrq_disconnect,
674 .name = "sysrq",
675 .id_table = sysrq_ids,
676};
677
678static bool sysrq_handler_registered;
679
680static inline void sysrq_register_handler(void)
681{
682 int error;
683
684 error = input_register_handler(&sysrq_handler);
685 if (error)
686 pr_err("Failed to register input handler, error %d", error);
687 else
688 sysrq_handler_registered = true;
689}
690
691static inline void sysrq_unregister_handler(void)
692{
693 if (sysrq_handler_registered) {
694 input_unregister_handler(&sysrq_handler);
695 sysrq_handler_registered = false;
696 }
697}
698
699#else
700
701static inline void sysrq_register_handler(void)
702{
703}
704
705static inline void sysrq_unregister_handler(void)
706{
707}
708
709#endif /* CONFIG_INPUT */
710
711int sysrq_toggle_support(int enable_mask)
712{
713 bool was_enabled = sysrq_on();
714
715 sysrq_enabled = enable_mask;
716
717 if (was_enabled != sysrq_on()) {
718 if (sysrq_on())
719 sysrq_register_handler();
720 else
721 sysrq_unregister_handler();
722 }
723
724 return 0;
725}
726
Adrian Bunkcf62ddc2005-11-08 21:39:47 -0800727static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800728 struct sysrq_key_op *remove_op_p)
729{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 int retval;
731 unsigned long flags;
732
733 spin_lock_irqsave(&sysrq_key_table_lock, flags);
734 if (__sysrq_get_key_op(key) == remove_op_p) {
735 __sysrq_put_key_op(key, insert_op_p);
736 retval = 0;
737 } else {
738 retval = -1;
739 }
740 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return retval;
742}
743
744int register_sysrq_key(int key, struct sysrq_key_op *op_p)
745{
746 return __sysrq_swap_key_ops(key, op_p, NULL);
747}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800748EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
750int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
751{
752 return __sysrq_swap_key_ops(key, NULL, op_p);
753}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754EXPORT_SYMBOL(unregister_sysrq_key);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -0700755
756#ifdef CONFIG_PROC_FS
757/*
758 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
759 */
760static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
761 size_t count, loff_t *ppos)
762{
763 if (count) {
764 char c;
765
766 if (get_user(c, buf))
767 return -EFAULT;
768 __handle_sysrq(c, NULL, 0);
769 }
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700770
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -0700771 return count;
772}
773
774static const struct file_operations proc_sysrq_trigger_operations = {
775 .write = write_sysrq_trigger,
776};
777
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700778static void sysrq_init_procfs(void)
779{
780 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
781 &proc_sysrq_trigger_operations))
782 pr_err("Failed to register proc interface\n");
783}
784
785#else
786
787static inline void sysrq_init_procfs(void)
788{
789}
790
791#endif /* CONFIG_PROC_FS */
792
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -0700793static int __init sysrq_init(void)
794{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700795 sysrq_init_procfs();
796
797 if (sysrq_on())
798 sysrq_register_handler();
799
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -0700800 return 0;
801}
802module_init(sysrq_init);