blob: 3687f0cad6421c01c4e0e62d2345165c80765dc9 [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>
Clark Williams8bd75c72013-02-07 09:47:07 -060018#include <linux/sched/rt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/interrupt.h>
20#include <linux/mm.h>
21#include <linux/fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036#include <linux/swap.h>
37#include <linux/spinlock.h>
38#include <linux/vt_kern.h>
39#include <linux/workqueue.h>
Ingo Molnar88ad0bf62007-02-16 01:28:16 -080040#include <linux/hrtimer.h>
David Rientjes5a3135c22007-10-16 23:25:53 -070041#include <linux/oom.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090042#include <linux/slab.h>
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070043#include <linux/input.h>
Al Viroff01bb482011-09-16 02:31:11 -040044#include <linux/uaccess.h>
Mathieu Poirier154b7a42013-01-06 23:23:33 -080045#include <linux/moduleparam.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#include <asm/ptrace.h>
Heiko Carstens2033b0c2006-10-06 16:38:42 +020048#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
50/* Whether we react on sysrq keys or just ignore them */
Andy Whitcroft8c6a98b2011-01-24 09:31:38 -080051static int __read_mostly sysrq_enabled = SYSRQ_DEFAULT_ENABLE;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070052static bool __read_mostly sysrq_always_enabled;
Ingo Molnar5d6f6472006-12-13 00:34:36 -080053
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070054static bool sysrq_on(void)
Ingo Molnar5d6f6472006-12-13 00:34:36 -080055{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070056 return sysrq_enabled || sysrq_always_enabled;
Ingo Molnar5d6f6472006-12-13 00:34:36 -080057}
58
59/*
60 * A value of 1 means 'all', other nonzero values are an op mask:
61 */
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070062static bool sysrq_on_mask(int mask)
Ingo Molnar5d6f6472006-12-13 00:34:36 -080063{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070064 return sysrq_always_enabled ||
65 sysrq_enabled == 1 ||
66 (sysrq_enabled & mask);
Ingo Molnar5d6f6472006-12-13 00:34:36 -080067}
68
69static int __init sysrq_always_enabled_setup(char *str)
70{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070071 sysrq_always_enabled = true;
72 pr_info("sysrq always enabled.\n");
Ingo Molnar5d6f6472006-12-13 00:34:36 -080073
74 return 1;
75}
76
77__setup("sysrq_always_enabled", sysrq_always_enabled_setup);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Dmitry Torokhov1495cc92010-08-17 21:15:46 -070080static void sysrq_handle_loglevel(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 int i;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 i = key - '0';
85 console_loglevel = 7;
86 printk("Loglevel set to %d\n", i);
87 console_loglevel = i;
Andrew Mortonbf36b902006-03-25 03:07:08 -080088}
Linus Torvalds1da177e2005-04-16 15:20:36 -070089static struct sysrq_key_op sysrq_loglevel_op = {
90 .handler = sysrq_handle_loglevel,
Randy Dunlap208b95c2009-01-06 14:41:13 -080091 .help_msg = "loglevel(0-9)",
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 .action_msg = "Changing Loglevel",
93 .enable_mask = SYSRQ_ENABLE_LOG,
94};
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#ifdef CONFIG_VT
Dmitry Torokhov1495cc92010-08-17 21:15:46 -070097static void sysrq_handle_SAK(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Eric W. Biederman8b6312f2007-02-10 01:44:34 -080099 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -0800100 schedule_work(SAK_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102static struct sysrq_key_op sysrq_SAK_op = {
103 .handler = sysrq_handle_SAK,
104 .help_msg = "saK",
105 .action_msg = "SAK",
106 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
107};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800108#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700109#define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110#endif
111
112#ifdef CONFIG_VT
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700113static void sysrq_handle_unraw(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
Alan Cox079c9532012-02-28 14:49:23 +0000115 vt_reset_unicode(fg_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
Alan Cox079c9532012-02-28 14:49:23 +0000117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static struct sysrq_key_op sysrq_unraw_op = {
119 .handler = sysrq_handle_unraw,
120 .help_msg = "unRaw",
Bill Nottingham2e8ecb92007-10-16 23:29:38 -0700121 .action_msg = "Keyboard mode set to system default",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
123};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800124#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700125#define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126#endif /* CONFIG_VT */
127
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700128static void sysrq_handle_crash(int key)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700129{
Neil Hormand6580a92009-06-17 16:28:17 -0700130 char *killer = NULL;
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700131
132 panic_on_oops = 1; /* force panic */
133 wmb();
Neil Hormand6580a92009-06-17 16:28:17 -0700134 *killer = 1;
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700135}
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700136static struct sysrq_key_op sysrq_crash_op = {
Neil Hormand6580a92009-06-17 16:28:17 -0700137 .handler = sysrq_handle_crash,
138 .help_msg = "Crash",
139 .action_msg = "Trigger a crash",
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700140 .enable_mask = SYSRQ_ENABLE_DUMP,
141};
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700142
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700143static void sysrq_handle_reboot(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Peter Zijlstrab2e9c7d2006-09-30 23:28:02 -0700145 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600147 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149static struct sysrq_key_op sysrq_reboot_op = {
150 .handler = sysrq_handle_reboot,
151 .help_msg = "reBoot",
152 .action_msg = "Resetting",
153 .enable_mask = SYSRQ_ENABLE_BOOT,
154};
155
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700156static void sysrq_handle_sync(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
158 emergency_sync();
159}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160static struct sysrq_key_op sysrq_sync_op = {
161 .handler = sysrq_handle_sync,
162 .help_msg = "Sync",
163 .action_msg = "Emergency Sync",
164 .enable_mask = SYSRQ_ENABLE_SYNC,
165};
166
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700167static void sysrq_handle_show_timers(int key)
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800168{
169 sysrq_timer_list_show();
170}
171
172static struct sysrq_key_op sysrq_show_timers_op = {
173 .handler = sysrq_handle_show_timers,
174 .help_msg = "show-all-timers(Q)",
Thomas Gleixner322acf62008-10-20 12:33:14 +0200175 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800176};
177
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700178static void sysrq_handle_mountro(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 emergency_remount();
181}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182static struct sysrq_key_op sysrq_mountro_op = {
183 .handler = sysrq_handle_mountro,
184 .help_msg = "Unmount",
185 .action_msg = "Emergency Remount R/O",
186 .enable_mask = SYSRQ_ENABLE_REMOUNT,
187};
188
Ingo Molnar8c645802006-07-03 00:24:56 -0700189#ifdef CONFIG_LOCKDEP
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700190static void sysrq_handle_showlocks(int key)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800191{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700192 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800193}
Ingo Molnar8c645802006-07-03 00:24:56 -0700194
Ingo Molnarde5097c2006-01-09 15:59:21 -0800195static struct sysrq_key_op sysrq_showlocks_op = {
196 .handler = sysrq_handle_showlocks,
197 .help_msg = "show-all-locks(D)",
198 .action_msg = "Show Locks Held",
199};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800200#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700201#define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800202#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Rik van Riel5045bca2008-04-29 00:59:21 -0700204#ifdef CONFIG_SMP
205static DEFINE_SPINLOCK(show_lock);
206
207static void showacpu(void *dummy)
208{
209 unsigned long flags;
210
211 /* Idle CPUs have no interesting backtrace. */
212 if (idle_cpu(smp_processor_id()))
213 return;
214
215 spin_lock_irqsave(&show_lock, flags);
216 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
217 show_stack(NULL, NULL);
218 spin_unlock_irqrestore(&show_lock, flags);
219}
220
221static void sysrq_showregs_othercpus(struct work_struct *dummy)
222{
Ingo Molnar8b604d52008-06-27 11:52:45 +0200223 smp_call_function(showacpu, NULL, 0);
Rik van Riel5045bca2008-04-29 00:59:21 -0700224}
225
226static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
227
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700228static void sysrq_handle_showallcpus(int key)
Rik van Riel5045bca2008-04-29 00:59:21 -0700229{
Ingo Molnar47cab6a2009-08-03 09:31:54 +0200230 /*
231 * Fall back to the workqueue based printing if the
232 * backtrace printing did not succeed or the
233 * architecture has no support for it:
234 */
235 if (!trigger_all_cpu_backtrace()) {
236 struct pt_regs *regs = get_irq_regs();
237
238 if (regs) {
239 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
240 show_regs(regs);
241 }
242 schedule_work(&sysrq_showallcpus);
243 }
Rik van Riel5045bca2008-04-29 00:59:21 -0700244}
245
246static struct sysrq_key_op sysrq_showallcpus_op = {
247 .handler = sysrq_handle_showallcpus,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800248 .help_msg = "show-backtrace-all-active-cpus(L)",
Rik van Riel5045bca2008-04-29 00:59:21 -0700249 .action_msg = "Show backtrace of all active CPUs",
250 .enable_mask = SYSRQ_ENABLE_DUMP,
251};
252#endif
253
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700254static void sysrq_handle_showregs(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
David Howells7d12e782006-10-05 14:55:46 +0100256 struct pt_regs *regs = get_irq_regs();
257 if (regs)
258 show_regs(regs);
Ingo Molnarcdd6c482009-09-21 12:02:48 +0200259 perf_event_print_debug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260}
261static struct sysrq_key_op sysrq_showregs_op = {
262 .handler = sysrq_handle_showregs,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800263 .help_msg = "show-registers(P)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .action_msg = "Show Regs",
265 .enable_mask = SYSRQ_ENABLE_DUMP,
266};
267
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700268static void sysrq_handle_showstate(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 show_state();
271}
272static struct sysrq_key_op sysrq_showstate_op = {
273 .handler = sysrq_handle_showstate,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800274 .help_msg = "show-task-states(T)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 .action_msg = "Show State",
276 .enable_mask = SYSRQ_ENABLE_DUMP,
277};
278
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700279static void sysrq_handle_showstate_blocked(int key)
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800280{
281 show_state_filter(TASK_UNINTERRUPTIBLE);
282}
283static struct sysrq_key_op sysrq_showstate_blocked_op = {
284 .handler = sysrq_handle_showstate_blocked,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800285 .help_msg = "show-blocked-tasks(W)",
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800286 .action_msg = "Show Blocked State",
287 .enable_mask = SYSRQ_ENABLE_DUMP,
288};
289
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100290#ifdef CONFIG_TRACING
291#include <linux/ftrace.h>
292
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700293static void sysrq_ftrace_dump(int key)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100294{
Frederic Weisbeckercecbca92010-04-18 19:08:41 +0200295 ftrace_dump(DUMP_ALL);
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100296}
297static struct sysrq_key_op sysrq_ftrace_dump_op = {
298 .handler = sysrq_ftrace_dump,
Randy Dunlap3871f2f2008-12-24 16:06:57 -0800299 .help_msg = "dump-ftrace-buffer(Z)",
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100300 .action_msg = "Dump ftrace buffer",
301 .enable_mask = SYSRQ_ENABLE_DUMP,
302};
303#else
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700304#define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100305#endif
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800306
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700307static void sysrq_handle_showmem(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
David Rientjesb2b755b2011-03-24 15:18:15 -0700309 show_mem(0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311static struct sysrq_key_op sysrq_showmem_op = {
312 .handler = sysrq_handle_showmem,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800313 .help_msg = "show-memory-usage(M)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 .action_msg = "Show Memory",
315 .enable_mask = SYSRQ_ENABLE_DUMP,
316};
317
Andrew Mortonbf36b902006-03-25 03:07:08 -0800318/*
319 * Signal sysrq helper function. Sends a signal to all user processes.
320 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321static void send_sig_all(int sig)
322{
323 struct task_struct *p;
324
Anton Vorontsove502bab2012-02-07 10:49:39 +0400325 read_lock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 for_each_process(p) {
Anton Vorontsovd3a532a2012-02-07 10:49:51 +0400327 if (p->flags & PF_KTHREAD)
328 continue;
329 if (is_global_init(p))
330 continue;
331
Anton Vorontsovb82c3282012-04-05 14:25:05 -0700332 do_send_sig_info(sig, SEND_SIG_FORCED, p, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 }
Anton Vorontsove502bab2012-02-07 10:49:39 +0400334 read_unlock(&tasklist_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335}
336
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700337static void sysrq_handle_term(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 send_sig_all(SIGTERM);
340 console_loglevel = 8;
341}
342static struct sysrq_key_op sysrq_term_op = {
343 .handler = sysrq_handle_term,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800344 .help_msg = "terminate-all-tasks(E)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 .action_msg = "Terminate All Tasks",
346 .enable_mask = SYSRQ_ENABLE_SIGNAL,
347};
348
David Howells65f27f32006-11-22 14:55:48 +0000349static void moom_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350{
David Rientjes53186092012-11-14 01:15:19 -0800351 out_of_memory(node_zonelist(first_online_node, GFP_KERNEL), GFP_KERNEL,
352 0, NULL, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
David Howells65f27f32006-11-22 14:55:48 +0000355static DECLARE_WORK(moom_work, moom_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700357static void sysrq_handle_moom(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
359 schedule_work(&moom_work);
360}
361static struct sysrq_key_op sysrq_moom_op = {
362 .handler = sysrq_handle_moom,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800363 .help_msg = "memory-full-oom-kill(F)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 .action_msg = "Manual OOM execution",
Naohiro Ooiwab4236f82008-10-15 22:01:43 -0700365 .enable_mask = SYSRQ_ENABLE_SIGNAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366};
367
Eric Sandeenc2d75432009-03-31 15:23:46 -0700368#ifdef CONFIG_BLOCK
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700369static void sysrq_handle_thaw(int key)
Eric Sandeenc2d75432009-03-31 15:23:46 -0700370{
371 emergency_thaw_all();
372}
373static struct sysrq_key_op sysrq_thaw_op = {
374 .handler = sysrq_handle_thaw,
375 .help_msg = "thaw-filesystems(J)",
376 .action_msg = "Emergency Thaw of all frozen filesystems",
377 .enable_mask = SYSRQ_ENABLE_SIGNAL,
378};
379#endif
380
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700381static void sysrq_handle_kill(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382{
383 send_sig_all(SIGKILL);
384 console_loglevel = 8;
385}
386static struct sysrq_key_op sysrq_kill_op = {
387 .handler = sysrq_handle_kill,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800388 .help_msg = "kill-all-tasks(I)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 .action_msg = "Kill All Tasks",
390 .enable_mask = SYSRQ_ENABLE_SIGNAL,
391};
392
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700393static void sysrq_handle_unrt(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
395 normalize_rt_tasks();
396}
397static struct sysrq_key_op sysrq_unrt_op = {
398 .handler = sysrq_handle_unrt,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800399 .help_msg = "nice-all-RT-tasks(N)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 .action_msg = "Nice All RT Tasks",
401 .enable_mask = SYSRQ_ENABLE_RTNICE,
402};
403
404/* Key Operations table and lock */
405static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800406
407static struct sysrq_key_op *sysrq_key_table[36] = {
408 &sysrq_loglevel_op, /* 0 */
409 &sysrq_loglevel_op, /* 1 */
410 &sysrq_loglevel_op, /* 2 */
411 &sysrq_loglevel_op, /* 3 */
412 &sysrq_loglevel_op, /* 4 */
413 &sysrq_loglevel_op, /* 5 */
414 &sysrq_loglevel_op, /* 6 */
415 &sysrq_loglevel_op, /* 7 */
416 &sysrq_loglevel_op, /* 8 */
417 &sysrq_loglevel_op, /* 9 */
418
419 /*
Randy Dunlapd346cce2007-01-31 23:48:17 -0800420 * a: Don't use for system provided sysrqs, it is handled specially on
421 * sparc and will never arrive.
Andrew Mortonbf36b902006-03-25 03:07:08 -0800422 */
423 NULL, /* a */
424 &sysrq_reboot_op, /* b */
Hidetoshi Setocab8bd32009-07-29 15:04:14 -0700425 &sysrq_crash_op, /* c & ibm_emac driver debug */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800426 &sysrq_showlocks_op, /* d */
427 &sysrq_term_op, /* e */
428 &sysrq_moom_op, /* f */
Jason Wessel364b5b72009-05-13 21:56:59 -0500429 /* g: May be registered for the kernel debugger */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800430 NULL, /* g */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700431 NULL, /* h - reserved for help */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800432 &sysrq_kill_op, /* i */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700433#ifdef CONFIG_BLOCK
434 &sysrq_thaw_op, /* j */
435#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800436 NULL, /* j */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700437#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800438 &sysrq_SAK_op, /* k */
Rik van Riel5045bca2008-04-29 00:59:21 -0700439#ifdef CONFIG_SMP
440 &sysrq_showallcpus_op, /* l */
441#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800442 NULL, /* l */
Rik van Riel5045bca2008-04-29 00:59:21 -0700443#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800444 &sysrq_showmem_op, /* m */
445 &sysrq_unrt_op, /* n */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800446 /* o: This will often be registered as 'Off' at init time */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800447 NULL, /* o */
448 &sysrq_showregs_op, /* p */
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800449 &sysrq_show_timers_op, /* q */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800450 &sysrq_unraw_op, /* r */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800451 &sysrq_sync_op, /* s */
452 &sysrq_showstate_op, /* t */
453 &sysrq_mountro_op, /* u */
Jason Wessel364b5b72009-05-13 21:56:59 -0500454 /* v: May be registered for frame buffer console restore */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800455 NULL, /* v */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800456 &sysrq_showstate_blocked_op, /* w */
457 /* x: May be registered on ppc/powerpc for xmon */
David S. Miller916ca142012-10-16 09:34:01 -0700458 /* x: May be registered on sparc64 for global PMU dump */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800459 NULL, /* x */
David S. Miller93dae5b2008-05-19 23:46:00 -0700460 /* y: May be registered on sparc64 for global register dump */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800461 NULL, /* y */
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100462 &sysrq_ftrace_dump_op, /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463};
464
465/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800466static int sysrq_key_table_key2index(int key)
467{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800469
470 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800472 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800474 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return retval;
477}
478
479/*
480 * get and put functions for the table, exposed to modules.
481 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800482struct sysrq_key_op *__sysrq_get_key_op(int key)
483{
484 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800488 if (i != -1)
489 op_p = sysrq_key_table[i];
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700490
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 return op_p;
492}
493
Andrew Mortonbf36b902006-03-25 03:07:08 -0800494static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
495{
496 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 if (i != -1)
499 sysrq_key_table[i] = op_p;
500}
501
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700502void __handle_sysrq(int key, bool check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
504 struct sysrq_key_op *op_p;
505 int orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800506 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 unsigned long flags;
508
509 spin_lock_irqsave(&sysrq_key_table_lock, flags);
Andy Whitcroftfb144ad2009-01-15 13:50:52 -0800510 /*
511 * Raise the apparent loglevel to maximum so that the sysrq header
512 * is shown to provide the user with positive feedback. We do not
513 * simply emit this at KERN_EMERG as that would change message
514 * routing in the consumers of /proc/kmsg.
515 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 orig_log_level = console_loglevel;
517 console_loglevel = 7;
518 printk(KERN_INFO "SysRq : ");
519
520 op_p = __sysrq_get_key_op(key);
521 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800522 /*
523 * Should we check for enabled operations (/proc/sysrq-trigger
524 * should not) and is the invoked operation enabled?
525 */
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800526 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800527 printk("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 console_loglevel = orig_log_level;
Dmitry Torokhov1495cc92010-08-17 21:15:46 -0700529 op_p->handler(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800530 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 printk("This sysrq operation is disabled.\n");
Andrew Mortonbf36b902006-03-25 03:07:08 -0800532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 } else {
534 printk("HELP : ");
535 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800536 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
537 if (sysrq_key_table[i]) {
538 int j;
539
540 for (j = 0; sysrq_key_table[i] !=
541 sysrq_key_table[j]; j++)
542 ;
543 if (j != i)
544 continue;
545 printk("%s ", sysrq_key_table[i]->help_msg);
546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 }
Andrew Mortonbf36b902006-03-25 03:07:08 -0800548 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 console_loglevel = orig_log_level;
550 }
551 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
552}
553
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700554void handle_sysrq(int key)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800556 if (sysrq_on())
Dmitry Torokhovf3353972010-08-17 21:15:47 -0700557 __handle_sysrq(key, true);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800559EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700561#ifdef CONFIG_INPUT
562
563/* Simple translation table for the SysRq keys */
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800564static const unsigned char sysrq_xlate[KEY_CNT] =
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700565 "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
566 "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
567 "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
568 "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
569 "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
570 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
571 "\r\000/"; /* 0x60 - 0x6f */
572
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800573struct sysrq_state {
574 struct input_handle handle;
575 struct work_struct reinject_work;
576 unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
577 unsigned int alt;
578 unsigned int alt_use;
579 bool active;
580 bool need_reinject;
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800581 bool reinjecting;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800582
583 /* reset sequence handling */
584 bool reset_canceled;
585 unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
586 int reset_seq_len;
587 int reset_seq_cnt;
588 int reset_seq_version;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800589};
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700590
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800591#define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
592static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
593static unsigned int sysrq_reset_seq_len;
594static unsigned int sysrq_reset_seq_version = 1;
595
596static void sysrq_parse_reset_sequence(struct sysrq_state *state)
597{
598 int i;
599 unsigned short key;
600
601 state->reset_seq_cnt = 0;
602
603 for (i = 0; i < sysrq_reset_seq_len; i++) {
604 key = sysrq_reset_seq[i];
605
606 if (key == KEY_RESERVED || key > KEY_MAX)
607 break;
608
609 __set_bit(key, state->reset_keybit);
610 state->reset_seq_len++;
611
612 if (test_bit(key, state->key_down))
613 state->reset_seq_cnt++;
614 }
615
616 /* Disable reset until old keys are not released */
617 state->reset_canceled = state->reset_seq_cnt != 0;
618
619 state->reset_seq_version = sysrq_reset_seq_version;
620}
621
622static bool sysrq_detect_reset_sequence(struct sysrq_state *state,
623 unsigned int code, int value)
624{
625 if (!test_bit(code, state->reset_keybit)) {
626 /*
627 * Pressing any key _not_ in reset sequence cancels
628 * the reset sequence.
629 */
630 if (value && state->reset_seq_cnt)
631 state->reset_canceled = true;
632 } else if (value == 0) {
633 /* key release */
634 if (--state->reset_seq_cnt == 0)
635 state->reset_canceled = false;
636 } else if (value == 1) {
637 /* key press, not autorepeat */
638 if (++state->reset_seq_cnt == state->reset_seq_len &&
639 !state->reset_canceled) {
640 return true;
641 }
642 }
643
644 return false;
645}
646
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800647static void sysrq_reinject_alt_sysrq(struct work_struct *work)
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700648{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800649 struct sysrq_state *sysrq =
650 container_of(work, struct sysrq_state, reinject_work);
651 struct input_handle *handle = &sysrq->handle;
652 unsigned int alt_code = sysrq->alt_use;
653
654 if (sysrq->need_reinject) {
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800655 /* we do not want the assignment to be reordered */
656 sysrq->reinjecting = true;
657 mb();
658
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800659 /* Simulate press and release of Alt + SysRq */
660 input_inject_event(handle, EV_KEY, alt_code, 1);
661 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
662 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
663
664 input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
665 input_inject_event(handle, EV_KEY, alt_code, 0);
666 input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800667
668 mb();
669 sysrq->reinjecting = false;
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800670 }
671}
672
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800673static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
674 unsigned int code, int value)
675{
676 bool was_active = sysrq->active;
677 bool suppress;
678
679 switch (code) {
680
681 case KEY_LEFTALT:
682 case KEY_RIGHTALT:
683 if (!value) {
684 /* One of ALTs is being released */
685 if (sysrq->active && code == sysrq->alt_use)
686 sysrq->active = false;
687
688 sysrq->alt = KEY_RESERVED;
689
690 } else if (value != 2) {
691 sysrq->alt = code;
692 sysrq->need_reinject = false;
693 }
694 break;
695
696 case KEY_SYSRQ:
697 if (value == 1 && sysrq->alt != KEY_RESERVED) {
698 sysrq->active = true;
699 sysrq->alt_use = sysrq->alt;
700 /*
701 * If nothing else will be pressed we'll need
702 * to re-inject Alt-SysRq keysroke.
703 */
704 sysrq->need_reinject = true;
705 }
706
707 /*
708 * Pretend that sysrq was never pressed at all. This
709 * is needed to properly handle KGDB which will try
710 * to release all keys after exiting debugger. If we
711 * do not clear key bit it KGDB will end up sending
712 * release events for Alt and SysRq, potentially
713 * triggering print screen function.
714 */
715 if (sysrq->active)
716 clear_bit(KEY_SYSRQ, sysrq->handle.dev->key);
717
718 break;
719
720 default:
721 if (sysrq->active && value && value != 2) {
722 sysrq->need_reinject = false;
723 __handle_sysrq(sysrq_xlate[code], true);
724 }
725 break;
726 }
727
728 suppress = sysrq->active;
729
730 if (!sysrq->active) {
731
732 /*
733 * See if reset sequence has changed since the last time.
734 */
735 if (sysrq->reset_seq_version != sysrq_reset_seq_version)
736 sysrq_parse_reset_sequence(sysrq);
737
738 /*
739 * If we are not suppressing key presses keep track of
740 * keyboard state so we can release keys that have been
741 * pressed before entering SysRq mode.
742 */
743 if (value)
744 set_bit(code, sysrq->key_down);
745 else
746 clear_bit(code, sysrq->key_down);
747
748 if (was_active)
749 schedule_work(&sysrq->reinject_work);
750
751 if (sysrq_detect_reset_sequence(sysrq, code, value)) {
752 /* Force emergency reboot */
753 __handle_sysrq(sysrq_xlate[KEY_B], false);
754 }
755
756 } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) {
757 /*
758 * Pass on release events for keys that was pressed before
759 * entering SysRq mode.
760 */
761 suppress = false;
762 }
763
764 return suppress;
765}
766
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800767static bool sysrq_filter(struct input_handle *handle,
768 unsigned int type, unsigned int code, int value)
769{
770 struct sysrq_state *sysrq = handle->private;
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700771 bool suppress;
772
Dmitry Torokhov7ab7b5a2011-02-02 22:59:54 -0800773 /*
774 * Do not filter anything if we are in the process of re-injecting
775 * Alt+SysRq combination.
776 */
777 if (sysrq->reinjecting)
778 return false;
779
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800780 switch (type) {
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700781
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800782 case EV_SYN:
783 suppress = false;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700784 break;
785
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800786 case EV_KEY:
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800787 suppress = sysrq_handle_keypress(sysrq, code, value);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700788 break;
789
790 default:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800791 suppress = sysrq->active;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700792 break;
793 }
794
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700795 return suppress;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700796}
797
798static int sysrq_connect(struct input_handler *handler,
799 struct input_dev *dev,
800 const struct input_device_id *id)
801{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800802 struct sysrq_state *sysrq;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700803 int error;
804
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800805 sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
806 if (!sysrq)
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700807 return -ENOMEM;
808
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800809 INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700810
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800811 sysrq->handle.dev = dev;
812 sysrq->handle.handler = handler;
813 sysrq->handle.name = "sysrq";
814 sysrq->handle.private = sysrq;
815
816 error = input_register_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700817 if (error) {
818 pr_err("Failed to register input sysrq handler, error %d\n",
819 error);
820 goto err_free;
821 }
822
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800823 error = input_open_device(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700824 if (error) {
825 pr_err("Failed to open input device, error %d\n", error);
826 goto err_unregister;
827 }
828
829 return 0;
830
831 err_unregister:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800832 input_unregister_handle(&sysrq->handle);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700833 err_free:
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800834 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700835 return error;
836}
837
838static void sysrq_disconnect(struct input_handle *handle)
839{
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800840 struct sysrq_state *sysrq = handle->private;
841
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700842 input_close_device(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800843 cancel_work_sync(&sysrq->reinject_work);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700844 input_unregister_handle(handle);
Dmitry Torokhovfcb71932010-11-15 00:23:42 -0800845 kfree(sysrq);
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700846}
847
848/*
Dmitry Torokhov1966cb22010-09-29 18:04:21 -0700849 * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
850 * keyboards have SysRq key predefined and so user may add it to keymap
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700851 * later, but we expect all such keyboards to have left alt.
852 */
853static const struct input_device_id sysrq_ids[] = {
854 {
855 .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
856 INPUT_DEVICE_ID_MATCH_KEYBIT,
857 .evbit = { BIT_MASK(EV_KEY) },
858 .keybit = { BIT_MASK(KEY_LEFTALT) },
859 },
860 { },
861};
862
863static struct input_handler sysrq_handler = {
864 .filter = sysrq_filter,
865 .connect = sysrq_connect,
866 .disconnect = sysrq_disconnect,
867 .name = "sysrq",
868 .id_table = sysrq_ids,
869};
870
871static bool sysrq_handler_registered;
872
Linus Torvaldsadf96e62013-02-27 09:59:50 -0800873unsigned short platform_sysrq_reset_seq[] __weak = { KEY_RESERVED };
874
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700875static inline void sysrq_register_handler(void)
876{
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800877 unsigned short key;
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700878 int error;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800879 int i;
880
Linus Torvaldsadf96e62013-02-27 09:59:50 -0800881 for (i = 0; i < ARRAY_SIZE(sysrq_reset_seq); i++) {
882 key = platform_sysrq_reset_seq[i];
883 if (key == KEY_RESERVED || key > KEY_MAX)
884 break;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800885
Linus Torvaldsadf96e62013-02-27 09:59:50 -0800886 sysrq_reset_seq[sysrq_reset_seq_len++] = key;
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800887 }
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700888
889 error = input_register_handler(&sysrq_handler);
890 if (error)
891 pr_err("Failed to register input handler, error %d", error);
892 else
893 sysrq_handler_registered = true;
894}
895
896static inline void sysrq_unregister_handler(void)
897{
898 if (sysrq_handler_registered) {
899 input_unregister_handler(&sysrq_handler);
900 sysrq_handler_registered = false;
901 }
902}
903
Mathieu Poirier154b7a42013-01-06 23:23:33 -0800904static int sysrq_reset_seq_param_set(const char *buffer,
905 const struct kernel_param *kp)
906{
907 unsigned long val;
908 int error;
909
910 error = strict_strtoul(buffer, 0, &val);
911 if (error < 0)
912 return error;
913
914 if (val > KEY_MAX)
915 return -EINVAL;
916
917 *((unsigned short *)kp->arg) = val;
918 sysrq_reset_seq_version++;
919
920 return 0;
921}
922
923static struct kernel_param_ops param_ops_sysrq_reset_seq = {
924 .get = param_get_ushort,
925 .set = sysrq_reset_seq_param_set,
926};
927
928#define param_check_sysrq_reset_seq(name, p) \
929 __param_check(name, p, unsigned short)
930
931module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
932 &sysrq_reset_seq_len, 0644);
933
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -0700934#else
935
936static inline void sysrq_register_handler(void)
937{
938}
939
940static inline void sysrq_unregister_handler(void)
941{
942}
943
944#endif /* CONFIG_INPUT */
945
946int sysrq_toggle_support(int enable_mask)
947{
948 bool was_enabled = sysrq_on();
949
950 sysrq_enabled = enable_mask;
951
952 if (was_enabled != sysrq_on()) {
953 if (sysrq_on())
954 sysrq_register_handler();
955 else
956 sysrq_unregister_handler();
957 }
958
959 return 0;
960}
961
Adrian Bunkcf62ddc2005-11-08 21:39:47 -0800962static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800963 struct sysrq_key_op *remove_op_p)
964{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 int retval;
966 unsigned long flags;
967
968 spin_lock_irqsave(&sysrq_key_table_lock, flags);
969 if (__sysrq_get_key_op(key) == remove_op_p) {
970 __sysrq_put_key_op(key, insert_op_p);
971 retval = 0;
972 } else {
973 retval = -1;
974 }
975 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 return retval;
977}
978
979int register_sysrq_key(int key, struct sysrq_key_op *op_p)
980{
981 return __sysrq_swap_key_ops(key, op_p, NULL);
982}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800983EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
985int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
986{
987 return __sysrq_swap_key_ops(key, NULL, op_p);
988}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989EXPORT_SYMBOL(unregister_sysrq_key);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -0700990
991#ifdef CONFIG_PROC_FS
992/*
993 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
994 */
995static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
996 size_t count, loff_t *ppos)
997{
998 if (count) {
999 char c;
1000
1001 if (get_user(c, buf))
1002 return -EFAULT;
Dmitry Torokhovf3353972010-08-17 21:15:47 -07001003 __handle_sysrq(c, false);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001004 }
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001005
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001006 return count;
1007}
1008
1009static const struct file_operations proc_sysrq_trigger_operations = {
1010 .write = write_sysrq_trigger,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001011 .llseek = noop_llseek,
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001012};
1013
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001014static void sysrq_init_procfs(void)
1015{
1016 if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
1017 &proc_sysrq_trigger_operations))
1018 pr_err("Failed to register proc interface\n");
1019}
1020
1021#else
1022
1023static inline void sysrq_init_procfs(void)
1024{
1025}
1026
1027#endif /* CONFIG_PROC_FS */
1028
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001029static int __init sysrq_init(void)
1030{
Dmitry Torokhov97f5f0c2010-03-21 22:31:26 -07001031 sysrq_init_procfs();
1032
1033 if (sysrq_on())
1034 sysrq_register_handler();
1035
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -07001036 return 0;
1037}
1038module_init(sysrq_init);