15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 #include <linux/sched.h>
22 #include <linux/kdev_t.h>
24 #include <linux/reboot.h>
30 #include <linux/perf_event.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
39 #include <linux/hrtimer.h>
40 #include <linux/oom.h>
41 #include <linux/slab.h>
42 #include <linux/input.h>
45 #include <asm/ptrace.h>
46 #include <asm/irq_regs.h>
52 static bool sysrq_on(
void)
54 return sysrq_enabled || sysrq_always_enabled;
60 static bool sysrq_on_mask(
int mask)
62 return sysrq_always_enabled ||
64 (sysrq_enabled &
mask);
67 static int __init sysrq_always_enabled_setup(
char *
str)
69 sysrq_always_enabled =
true;
70 pr_info(
"sysrq always enabled.\n");
75 __setup(
"sysrq_always_enabled", sysrq_always_enabled_setup);
78 static void sysrq_handle_loglevel(
int key)
84 printk(
"Loglevel set to %d\n", i);
88 .handler = sysrq_handle_loglevel,
89 .help_msg =
"loglevel(0-9)",
90 .action_msg =
"Changing Loglevel",
95 static void sysrq_handle_SAK(
int key)
101 .handler = sysrq_handle_SAK,
107 #define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
111 static void sysrq_handle_unraw(
int key)
117 .handler = sysrq_handle_unraw,
119 .action_msg =
"Keyboard mode set to system default",
123 #define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
126 static void sysrq_handle_crash(
int key)
135 .handler = sysrq_handle_crash,
137 .action_msg =
"Trigger a crash",
141 static void sysrq_handle_reboot(
int key)
148 .handler = sysrq_handle_reboot,
149 .help_msg =
"reBoot",
150 .action_msg =
"Resetting",
154 static void sysrq_handle_sync(
int key)
159 .handler = sysrq_handle_sync,
161 .action_msg =
"Emergency Sync",
165 static void sysrq_handle_show_timers(
int key)
171 .handler = sysrq_handle_show_timers,
172 .help_msg =
"show-all-timers(Q)",
173 .action_msg =
"Show clockevent devices & pending hrtimers (no others)",
176 static void sysrq_handle_mountro(
int key)
181 .handler = sysrq_handle_mountro,
182 .help_msg =
"Unmount",
183 .action_msg =
"Emergency Remount R/O",
187 #ifdef CONFIG_LOCKDEP
188 static void sysrq_handle_showlocks(
int key)
194 .handler = sysrq_handle_showlocks,
195 .help_msg =
"show-all-locks(D)",
196 .action_msg =
"Show Locks Held",
199 #define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
205 static void showacpu(
void *
dummy)
216 spin_unlock_irqrestore(&show_lock, flags);
219 static void sysrq_showregs_othercpus(
struct work_struct *dummy)
224 static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
226 static void sysrq_handle_showallcpus(
int key)
233 if (!trigger_all_cpu_backtrace()) {
245 .
handler = sysrq_handle_showallcpus,
246 .help_msg =
"show-backtrace-all-active-cpus(L)",
247 .action_msg =
"Show backtrace of all active CPUs",
252 static void sysrq_handle_showregs(
int key)
260 .handler = sysrq_handle_showregs,
261 .help_msg =
"show-registers(P)",
262 .action_msg =
"Show Regs",
266 static void sysrq_handle_showstate(
int key)
271 .handler = sysrq_handle_showstate,
272 .help_msg =
"show-task-states(T)",
273 .action_msg =
"Show State",
277 static void sysrq_handle_showstate_blocked(
int key)
281 static struct sysrq_key_op sysrq_showstate_blocked_op = {
282 .handler = sysrq_handle_showstate_blocked,
283 .help_msg =
"show-blocked-tasks(W)",
284 .action_msg =
"Show Blocked State",
288 #ifdef CONFIG_TRACING
291 static void sysrq_ftrace_dump(
int key)
296 .handler = sysrq_ftrace_dump,
297 .help_msg =
"dump-ftrace-buffer(Z)",
298 .action_msg =
"Dump ftrace buffer",
302 #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
305 static void sysrq_handle_showmem(
int key)
310 .handler = sysrq_handle_showmem,
311 .help_msg =
"show-memory-usage(M)",
312 .action_msg =
"Show Memory",
319 static void send_sig_all(
int sig)
327 if (is_global_init(p))
335 static void sysrq_handle_term(
int key)
341 .handler = sysrq_handle_term,
342 .help_msg =
"terminate-all-tasks(E)",
343 .action_msg =
"Terminate All Tasks",
354 static void sysrq_handle_moom(
int key)
359 .handler = sysrq_handle_moom,
360 .help_msg =
"memory-full-oom-kill(F)",
361 .action_msg =
"Manual OOM execution",
366 static void sysrq_handle_thaw(
int key)
372 .help_msg =
"thaw-filesystems(J)",
373 .action_msg =
"Emergency Thaw of all frozen filesystems",
378 static void sysrq_handle_kill(
int key)
384 .handler = sysrq_handle_kill,
385 .help_msg =
"kill-all-tasks(I)",
386 .action_msg =
"Kill All Tasks",
390 static void sysrq_handle_unrt(
int key)
395 .handler = sysrq_handle_unrt,
396 .help_msg =
"nice-all-RT-tasks(N)",
397 .action_msg =
"Nice All RT Tasks",
437 &sysrq_showallcpus_op,
446 &sysrq_show_timers_op,
453 &sysrq_showstate_blocked_op,
463 static int sysrq_key_table_key2index(
int key)
467 if ((key >=
'0') && (key <=
'9'))
469 else if ((key >=
'a') && (key <=
'z'))
470 retval = key + 10 -
'a';
484 i = sysrq_key_table_key2index(key);
486 op_p = sysrq_key_table[
i];
491 static void __sysrq_put_key_op(
int key,
struct sysrq_key_op *op_p)
493 int i = sysrq_key_table_key2index(key);
496 sysrq_key_table[
i] = op_p;
523 if (!check_mask || sysrq_on_mask(op_p->
enable_mask)) {
528 printk(
"This sysrq operation is disabled.\n");
533 for (i = 0; i <
ARRAY_SIZE(sysrq_key_table); i++) {
534 if (sysrq_key_table[i]) {
537 for (j = 0; sysrq_key_table[
i] !=
538 sysrq_key_table[
j]; j++)
548 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
561 static const unsigned char sysrq_xlate[
KEY_CNT] =
562 "\000\0331234567890-=\177\t"
563 "qwertyuiop[]\r\000as"
564 "dfghjkl;'`\000\\zxcv"
565 "bnm,./\000*\000 \000\201\202\203\204\205"
566 "\206\207\210\211\212\000\000789-456+1"
567 "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000"
571 struct input_handle
handle;
575 unsigned int alt_use;
583 struct sysrq_state *sysrq =
585 struct input_handle *
handle = &sysrq->handle;
586 unsigned int alt_code = sysrq->alt_use;
588 if (sysrq->need_reinject) {
590 sysrq->reinjecting =
true;
594 input_inject_event(handle,
EV_KEY, alt_code, 1);
599 input_inject_event(handle,
EV_KEY, alt_code, 0);
603 sysrq->reinjecting =
false;
607 static bool sysrq_filter(
struct input_handle *handle,
610 struct sysrq_state *sysrq = handle->private;
611 bool was_active = sysrq->active;
618 if (sysrq->reinjecting)
634 if (sysrq->active && code == sysrq->alt_use)
635 sysrq->active =
false;
639 }
else if (value != 2) {
641 sysrq->need_reinject =
false;
647 sysrq->active =
true;
648 sysrq->alt_use = sysrq->alt;
653 sysrq->need_reinject =
true;
670 if (sysrq->active && value && value != 2) {
671 sysrq->need_reinject =
false;
677 suppress = sysrq->active;
679 if (!sysrq->active) {
686 set_bit(code, sysrq->key_down);
693 }
else if (value == 0 &&
704 suppress = sysrq->active;
711 static int sysrq_connect(
struct input_handler *handler,
712 struct input_dev *
dev,
715 struct sysrq_state *sysrq;
718 sysrq = kzalloc(
sizeof(
struct sysrq_state),
GFP_KERNEL);
722 INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
724 sysrq->handle.dev =
dev;
725 sysrq->handle.handler = handler;
726 sysrq->handle.name =
"sysrq";
727 sysrq->handle.private = sysrq;
729 error = input_register_handle(&sysrq->handle);
731 pr_err(
"Failed to register input sysrq handler, error %d\n",
736 error = input_open_device(&sysrq->handle);
738 pr_err(
"Failed to open input device, error %d\n", error);
745 input_unregister_handle(&sysrq->handle);
751 static void sysrq_disconnect(
struct input_handle *handle)
753 struct sysrq_state *sysrq = handle->private;
755 input_close_device(handle);
757 input_unregister_handle(handle);
776 static struct input_handler sysrq_handler = {
777 .filter = sysrq_filter,
778 .connect = sysrq_connect,
779 .disconnect = sysrq_disconnect,
781 .id_table = sysrq_ids,
784 static bool sysrq_handler_registered;
786 static inline void sysrq_register_handler(
void)
790 error = input_register_handler(&sysrq_handler);
792 pr_err(
"Failed to register input handler, error %d", error);
794 sysrq_handler_registered =
true;
797 static inline void sysrq_unregister_handler(
void)
799 if (sysrq_handler_registered) {
800 input_unregister_handler(&sysrq_handler);
801 sysrq_handler_registered =
false;
807 static inline void sysrq_register_handler(
void)
811 static inline void sysrq_unregister_handler(
void)
819 bool was_enabled = sysrq_on();
821 sysrq_enabled = enable_mask;
823 if (was_enabled != sysrq_on()) {
825 sysrq_register_handler();
827 sysrq_unregister_handler();
833 static int __sysrq_swap_key_ops(
int key,
struct sysrq_key_op *insert_op_p,
841 __sysrq_put_key_op(key, insert_op_p);
846 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
852 return __sysrq_swap_key_ops(key, op_p,
NULL);
858 return __sysrq_swap_key_ops(key,
NULL, op_p);
862 #ifdef CONFIG_PROC_FS
867 size_t count, loff_t *ppos)
881 .
write = write_sysrq_trigger,
885 static void sysrq_init_procfs(
void)
888 &proc_sysrq_trigger_operations))
889 pr_err(
"Failed to register proc interface\n");
894 static inline void sysrq_init_procfs(
void)
900 static int __init sysrq_init(
void)
905 sysrq_register_handler();