]>
Commit | Line | Data |
---|---|---|
97f5f0cd | 1 | /* |
1da177e4 LT |
2 | * 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 | |
97f5f0cd DT |
10 | * |
11 | * Copyright (c) 2010 Dmitry Torokhov | |
12 | * Input handler conversion | |
1da177e4 LT |
13 | */ |
14 | ||
97f5f0cd DT |
15 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
16 | ||
3f07c014 | 17 | #include <linux/sched/signal.h> |
8bd75c77 | 18 | #include <linux/sched/rt.h> |
b17b0153 | 19 | #include <linux/sched/debug.h> |
29930025 | 20 | #include <linux/sched/task.h> |
1da177e4 LT |
21 | #include <linux/interrupt.h> |
22 | #include <linux/mm.h> | |
23 | #include <linux/fs.h> | |
1da177e4 LT |
24 | #include <linux/mount.h> |
25 | #include <linux/kdev_t.h> | |
26 | #include <linux/major.h> | |
27 | #include <linux/reboot.h> | |
28 | #include <linux/sysrq.h> | |
29 | #include <linux/kbd_kern.h> | |
f40cbaa5 | 30 | #include <linux/proc_fs.h> |
c1dc0b9c | 31 | #include <linux/nmi.h> |
1da177e4 | 32 | #include <linux/quotaops.h> |
cdd6c482 | 33 | #include <linux/perf_event.h> |
1da177e4 LT |
34 | #include <linux/kernel.h> |
35 | #include <linux/module.h> | |
36 | #include <linux/suspend.h> | |
37 | #include <linux/writeback.h> | |
1da177e4 LT |
38 | #include <linux/swap.h> |
39 | #include <linux/spinlock.h> | |
40 | #include <linux/vt_kern.h> | |
41 | #include <linux/workqueue.h> | |
88ad0bf6 | 42 | #include <linux/hrtimer.h> |
5a3135c2 | 43 | #include <linux/oom.h> |
5a0e3ad6 | 44 | #include <linux/slab.h> |
97f5f0cd | 45 | #include <linux/input.h> |
ff01bb48 | 46 | #include <linux/uaccess.h> |
154b7a48 | 47 | #include <linux/moduleparam.h> |
39030786 | 48 | #include <linux/jiffies.h> |
3d289517 | 49 | #include <linux/syscalls.h> |
4c076eb0 | 50 | #include <linux/of.h> |
722773af | 51 | #include <linux/rcupdate.h> |
1da177e4 LT |
52 | |
53 | #include <asm/ptrace.h> | |
2033b0c3 | 54 | #include <asm/irq_regs.h> |
1da177e4 LT |
55 | |
56 | /* Whether we react on sysrq keys or just ignore them */ | |
8eaede49 | 57 | static int __read_mostly sysrq_enabled = CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE; |
97f5f0cd | 58 | static bool __read_mostly sysrq_always_enabled; |
5d6f647f | 59 | |
97f5f0cd | 60 | static bool sysrq_on(void) |
5d6f647f | 61 | { |
97f5f0cd | 62 | return sysrq_enabled || sysrq_always_enabled; |
5d6f647f IM |
63 | } |
64 | ||
65 | /* | |
66 | * A value of 1 means 'all', other nonzero values are an op mask: | |
67 | */ | |
97f5f0cd | 68 | static bool sysrq_on_mask(int mask) |
5d6f647f | 69 | { |
97f5f0cd DT |
70 | return sysrq_always_enabled || |
71 | sysrq_enabled == 1 || | |
72 | (sysrq_enabled & mask); | |
5d6f647f IM |
73 | } |
74 | ||
75 | static int __init sysrq_always_enabled_setup(char *str) | |
76 | { | |
97f5f0cd DT |
77 | sysrq_always_enabled = true; |
78 | pr_info("sysrq always enabled.\n"); | |
5d6f647f IM |
79 | |
80 | return 1; | |
81 | } | |
82 | ||
83 | __setup("sysrq_always_enabled", sysrq_always_enabled_setup); | |
84 | ||
1da177e4 | 85 | |
1495cc9d | 86 | static void sysrq_handle_loglevel(int key) |
1da177e4 LT |
87 | { |
88 | int i; | |
97f5f0cd | 89 | |
1da177e4 | 90 | i = key - '0'; |
a8fe19eb | 91 | console_loglevel = CONSOLE_LOGLEVEL_DEFAULT; |
401e4a7c | 92 | pr_info("Loglevel set to %d\n", i); |
1da177e4 | 93 | console_loglevel = i; |
bf36b901 | 94 | } |
1da177e4 LT |
95 | static struct sysrq_key_op sysrq_loglevel_op = { |
96 | .handler = sysrq_handle_loglevel, | |
208b95ce | 97 | .help_msg = "loglevel(0-9)", |
1da177e4 LT |
98 | .action_msg = "Changing Loglevel", |
99 | .enable_mask = SYSRQ_ENABLE_LOG, | |
100 | }; | |
101 | ||
1da177e4 | 102 | #ifdef CONFIG_VT |
1495cc9d | 103 | static void sysrq_handle_SAK(int key) |
1da177e4 | 104 | { |
8b6312f4 | 105 | struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work; |
8b6312f4 | 106 | schedule_work(SAK_work); |
1da177e4 LT |
107 | } |
108 | static struct sysrq_key_op sysrq_SAK_op = { | |
109 | .handler = sysrq_handle_SAK, | |
afa80ccb | 110 | .help_msg = "sak(k)", |
1da177e4 LT |
111 | .action_msg = "SAK", |
112 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, | |
113 | }; | |
bf36b901 | 114 | #else |
97f5f0cd | 115 | #define sysrq_SAK_op (*(struct sysrq_key_op *)NULL) |
1da177e4 LT |
116 | #endif |
117 | ||
118 | #ifdef CONFIG_VT | |
1495cc9d | 119 | static void sysrq_handle_unraw(int key) |
1da177e4 | 120 | { |
079c9534 | 121 | vt_reset_unicode(fg_console); |
1da177e4 | 122 | } |
079c9534 | 123 | |
1da177e4 LT |
124 | static struct sysrq_key_op sysrq_unraw_op = { |
125 | .handler = sysrq_handle_unraw, | |
afa80ccb | 126 | .help_msg = "unraw(r)", |
2e8ecb9d | 127 | .action_msg = "Keyboard mode set to system default", |
1da177e4 LT |
128 | .enable_mask = SYSRQ_ENABLE_KEYBOARD, |
129 | }; | |
bf36b901 | 130 | #else |
97f5f0cd | 131 | #define sysrq_unraw_op (*(struct sysrq_key_op *)NULL) |
1da177e4 LT |
132 | #endif /* CONFIG_VT */ |
133 | ||
1495cc9d | 134 | static void sysrq_handle_crash(int key) |
86b1ae38 | 135 | { |
d6580a9f | 136 | char *killer = NULL; |
cab8bd34 | 137 | |
984cf355 AS |
138 | /* we need to release the RCU read lock here, |
139 | * otherwise we get an annoying | |
140 | * 'BUG: sleeping function called from invalid context' | |
141 | * complaint from the kernel before the panic. | |
142 | */ | |
143 | rcu_read_unlock(); | |
cab8bd34 HS |
144 | panic_on_oops = 1; /* force panic */ |
145 | wmb(); | |
d6580a9f | 146 | *killer = 1; |
86b1ae38 | 147 | } |
cab8bd34 | 148 | static struct sysrq_key_op sysrq_crash_op = { |
d6580a9f | 149 | .handler = sysrq_handle_crash, |
afa80ccb | 150 | .help_msg = "crash(c)", |
d6580a9f | 151 | .action_msg = "Trigger a crash", |
86b1ae38 HN |
152 | .enable_mask = SYSRQ_ENABLE_DUMP, |
153 | }; | |
86b1ae38 | 154 | |
1495cc9d | 155 | static void sysrq_handle_reboot(int key) |
1da177e4 | 156 | { |
b2e9c7d0 | 157 | lockdep_off(); |
1da177e4 | 158 | local_irq_enable(); |
4de8b9b7 | 159 | emergency_restart(); |
1da177e4 | 160 | } |
1da177e4 LT |
161 | static struct sysrq_key_op sysrq_reboot_op = { |
162 | .handler = sysrq_handle_reboot, | |
afa80ccb | 163 | .help_msg = "reboot(b)", |
1da177e4 LT |
164 | .action_msg = "Resetting", |
165 | .enable_mask = SYSRQ_ENABLE_BOOT, | |
166 | }; | |
167 | ||
1495cc9d | 168 | static void sysrq_handle_sync(int key) |
1da177e4 LT |
169 | { |
170 | emergency_sync(); | |
171 | } | |
1da177e4 LT |
172 | static struct sysrq_key_op sysrq_sync_op = { |
173 | .handler = sysrq_handle_sync, | |
afa80ccb | 174 | .help_msg = "sync(s)", |
1da177e4 LT |
175 | .action_msg = "Emergency Sync", |
176 | .enable_mask = SYSRQ_ENABLE_SYNC, | |
177 | }; | |
178 | ||
1495cc9d | 179 | static void sysrq_handle_show_timers(int key) |
88ad0bf6 IM |
180 | { |
181 | sysrq_timer_list_show(); | |
182 | } | |
183 | ||
184 | static struct sysrq_key_op sysrq_show_timers_op = { | |
185 | .handler = sysrq_handle_show_timers, | |
afa80ccb | 186 | .help_msg = "show-all-timers(q)", |
322acf65 | 187 | .action_msg = "Show clockevent devices & pending hrtimers (no others)", |
88ad0bf6 IM |
188 | }; |
189 | ||
1495cc9d | 190 | static void sysrq_handle_mountro(int key) |
1da177e4 LT |
191 | { |
192 | emergency_remount(); | |
193 | } | |
1da177e4 LT |
194 | static struct sysrq_key_op sysrq_mountro_op = { |
195 | .handler = sysrq_handle_mountro, | |
afa80ccb | 196 | .help_msg = "unmount(u)", |
1da177e4 LT |
197 | .action_msg = "Emergency Remount R/O", |
198 | .enable_mask = SYSRQ_ENABLE_REMOUNT, | |
199 | }; | |
200 | ||
8c64580d | 201 | #ifdef CONFIG_LOCKDEP |
1495cc9d | 202 | static void sysrq_handle_showlocks(int key) |
de5097c2 | 203 | { |
9a11b49a | 204 | debug_show_all_locks(); |
de5097c2 | 205 | } |
8c64580d | 206 | |
de5097c2 IM |
207 | static struct sysrq_key_op sysrq_showlocks_op = { |
208 | .handler = sysrq_handle_showlocks, | |
afa80ccb | 209 | .help_msg = "show-all-locks(d)", |
de5097c2 IM |
210 | .action_msg = "Show Locks Held", |
211 | }; | |
bf36b901 | 212 | #else |
97f5f0cd | 213 | #define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL) |
de5097c2 | 214 | #endif |
1da177e4 | 215 | |
5045bcae RR |
216 | #ifdef CONFIG_SMP |
217 | static DEFINE_SPINLOCK(show_lock); | |
218 | ||
219 | static void showacpu(void *dummy) | |
220 | { | |
221 | unsigned long flags; | |
222 | ||
223 | /* Idle CPUs have no interesting backtrace. */ | |
224 | if (idle_cpu(smp_processor_id())) | |
225 | return; | |
226 | ||
227 | spin_lock_irqsave(&show_lock, flags); | |
401e4a7c | 228 | pr_info("CPU%d:\n", smp_processor_id()); |
5045bcae RR |
229 | show_stack(NULL, NULL); |
230 | spin_unlock_irqrestore(&show_lock, flags); | |
231 | } | |
232 | ||
233 | static void sysrq_showregs_othercpus(struct work_struct *dummy) | |
234 | { | |
8b604d52 | 235 | smp_call_function(showacpu, NULL, 0); |
5045bcae RR |
236 | } |
237 | ||
238 | static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus); | |
239 | ||
1495cc9d | 240 | static void sysrq_handle_showallcpus(int key) |
5045bcae | 241 | { |
47cab6a7 IM |
242 | /* |
243 | * Fall back to the workqueue based printing if the | |
244 | * backtrace printing did not succeed or the | |
245 | * architecture has no support for it: | |
246 | */ | |
247 | if (!trigger_all_cpu_backtrace()) { | |
248 | struct pt_regs *regs = get_irq_regs(); | |
249 | ||
250 | if (regs) { | |
401e4a7c | 251 | pr_info("CPU%d:\n", smp_processor_id()); |
47cab6a7 IM |
252 | show_regs(regs); |
253 | } | |
254 | schedule_work(&sysrq_showallcpus); | |
255 | } | |
5045bcae RR |
256 | } |
257 | ||
258 | static struct sysrq_key_op sysrq_showallcpus_op = { | |
259 | .handler = sysrq_handle_showallcpus, | |
afa80ccb | 260 | .help_msg = "show-backtrace-all-active-cpus(l)", |
5045bcae RR |
261 | .action_msg = "Show backtrace of all active CPUs", |
262 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
263 | }; | |
264 | #endif | |
265 | ||
1495cc9d | 266 | static void sysrq_handle_showregs(int key) |
1da177e4 | 267 | { |
7d12e780 DH |
268 | struct pt_regs *regs = get_irq_regs(); |
269 | if (regs) | |
270 | show_regs(regs); | |
cdd6c482 | 271 | perf_event_print_debug(); |
1da177e4 LT |
272 | } |
273 | static struct sysrq_key_op sysrq_showregs_op = { | |
274 | .handler = sysrq_handle_showregs, | |
afa80ccb | 275 | .help_msg = "show-registers(p)", |
1da177e4 LT |
276 | .action_msg = "Show Regs", |
277 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
278 | }; | |
279 | ||
1495cc9d | 280 | static void sysrq_handle_showstate(int key) |
1da177e4 LT |
281 | { |
282 | show_state(); | |
3494fc30 | 283 | show_workqueue_state(); |
1da177e4 LT |
284 | } |
285 | static struct sysrq_key_op sysrq_showstate_op = { | |
286 | .handler = sysrq_handle_showstate, | |
afa80ccb | 287 | .help_msg = "show-task-states(t)", |
1da177e4 LT |
288 | .action_msg = "Show State", |
289 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
290 | }; | |
291 | ||
1495cc9d | 292 | static void sysrq_handle_showstate_blocked(int key) |
e59e2ae2 IM |
293 | { |
294 | show_state_filter(TASK_UNINTERRUPTIBLE); | |
295 | } | |
296 | static struct sysrq_key_op sysrq_showstate_blocked_op = { | |
297 | .handler = sysrq_handle_showstate_blocked, | |
afa80ccb | 298 | .help_msg = "show-blocked-tasks(w)", |
e59e2ae2 IM |
299 | .action_msg = "Show Blocked State", |
300 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
301 | }; | |
302 | ||
69f698ad PZ |
303 | #ifdef CONFIG_TRACING |
304 | #include <linux/ftrace.h> | |
305 | ||
1495cc9d | 306 | static void sysrq_ftrace_dump(int key) |
69f698ad | 307 | { |
cecbca96 | 308 | ftrace_dump(DUMP_ALL); |
69f698ad PZ |
309 | } |
310 | static struct sysrq_key_op sysrq_ftrace_dump_op = { | |
311 | .handler = sysrq_ftrace_dump, | |
afa80ccb | 312 | .help_msg = "dump-ftrace-buffer(z)", |
69f698ad PZ |
313 | .action_msg = "Dump ftrace buffer", |
314 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
315 | }; | |
316 | #else | |
97f5f0cd | 317 | #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL) |
69f698ad | 318 | #endif |
e59e2ae2 | 319 | |
1495cc9d | 320 | static void sysrq_handle_showmem(int key) |
1da177e4 | 321 | { |
9af744d7 | 322 | show_mem(0, NULL); |
1da177e4 LT |
323 | } |
324 | static struct sysrq_key_op sysrq_showmem_op = { | |
325 | .handler = sysrq_handle_showmem, | |
afa80ccb | 326 | .help_msg = "show-memory-usage(m)", |
1da177e4 LT |
327 | .action_msg = "Show Memory", |
328 | .enable_mask = SYSRQ_ENABLE_DUMP, | |
329 | }; | |
330 | ||
bf36b901 AM |
331 | /* |
332 | * Signal sysrq helper function. Sends a signal to all user processes. | |
333 | */ | |
1da177e4 LT |
334 | static void send_sig_all(int sig) |
335 | { | |
336 | struct task_struct *p; | |
337 | ||
e502babe | 338 | read_lock(&tasklist_lock); |
1da177e4 | 339 | for_each_process(p) { |
d3a532a9 AV |
340 | if (p->flags & PF_KTHREAD) |
341 | continue; | |
342 | if (is_global_init(p)) | |
343 | continue; | |
344 | ||
b82c3287 | 345 | do_send_sig_info(sig, SEND_SIG_FORCED, p, true); |
1da177e4 | 346 | } |
e502babe | 347 | read_unlock(&tasklist_lock); |
1da177e4 LT |
348 | } |
349 | ||
1495cc9d | 350 | static void sysrq_handle_term(int key) |
1da177e4 LT |
351 | { |
352 | send_sig_all(SIGTERM); | |
a8fe19eb | 353 | console_loglevel = CONSOLE_LOGLEVEL_DEBUG; |
1da177e4 LT |
354 | } |
355 | static struct sysrq_key_op sysrq_term_op = { | |
356 | .handler = sysrq_handle_term, | |
afa80ccb | 357 | .help_msg = "terminate-all-tasks(e)", |
1da177e4 LT |
358 | .action_msg = "Terminate All Tasks", |
359 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | |
360 | }; | |
361 | ||
65f27f38 | 362 | static void moom_callback(struct work_struct *ignored) |
1da177e4 | 363 | { |
6e0fc46d DR |
364 | const gfp_t gfp_mask = GFP_KERNEL; |
365 | struct oom_control oc = { | |
366 | .zonelist = node_zonelist(first_memory_node, gfp_mask), | |
367 | .nodemask = NULL, | |
2a966b77 | 368 | .memcg = NULL, |
6e0fc46d | 369 | .gfp_mask = gfp_mask, |
54e9e291 | 370 | .order = -1, |
6e0fc46d DR |
371 | }; |
372 | ||
dc56401f | 373 | mutex_lock(&oom_lock); |
6e0fc46d | 374 | if (!out_of_memory(&oc)) |
d75da004 | 375 | pr_info("OOM request ignored. No task eligible\n"); |
dc56401f | 376 | mutex_unlock(&oom_lock); |
1da177e4 LT |
377 | } |
378 | ||
65f27f38 | 379 | static DECLARE_WORK(moom_work, moom_callback); |
1da177e4 | 380 | |
1495cc9d | 381 | static void sysrq_handle_moom(int key) |
1da177e4 LT |
382 | { |
383 | schedule_work(&moom_work); | |
384 | } | |
385 | static struct sysrq_key_op sysrq_moom_op = { | |
386 | .handler = sysrq_handle_moom, | |
afa80ccb | 387 | .help_msg = "memory-full-oom-kill(f)", |
1da177e4 | 388 | .action_msg = "Manual OOM execution", |
b4236f81 | 389 | .enable_mask = SYSRQ_ENABLE_SIGNAL, |
1da177e4 LT |
390 | }; |
391 | ||
c2d75438 | 392 | #ifdef CONFIG_BLOCK |
1495cc9d | 393 | static void sysrq_handle_thaw(int key) |
c2d75438 ES |
394 | { |
395 | emergency_thaw_all(); | |
396 | } | |
397 | static struct sysrq_key_op sysrq_thaw_op = { | |
398 | .handler = sysrq_handle_thaw, | |
afa80ccb | 399 | .help_msg = "thaw-filesystems(j)", |
c2d75438 ES |
400 | .action_msg = "Emergency Thaw of all frozen filesystems", |
401 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | |
402 | }; | |
403 | #endif | |
404 | ||
1495cc9d | 405 | static void sysrq_handle_kill(int key) |
1da177e4 LT |
406 | { |
407 | send_sig_all(SIGKILL); | |
a8fe19eb | 408 | console_loglevel = CONSOLE_LOGLEVEL_DEBUG; |
1da177e4 LT |
409 | } |
410 | static struct sysrq_key_op sysrq_kill_op = { | |
411 | .handler = sysrq_handle_kill, | |
afa80ccb | 412 | .help_msg = "kill-all-tasks(i)", |
1da177e4 LT |
413 | .action_msg = "Kill All Tasks", |
414 | .enable_mask = SYSRQ_ENABLE_SIGNAL, | |
415 | }; | |
416 | ||
1495cc9d | 417 | static void sysrq_handle_unrt(int key) |
1da177e4 LT |
418 | { |
419 | normalize_rt_tasks(); | |
420 | } | |
421 | static struct sysrq_key_op sysrq_unrt_op = { | |
422 | .handler = sysrq_handle_unrt, | |
afa80ccb | 423 | .help_msg = "nice-all-RT-tasks(n)", |
1da177e4 LT |
424 | .action_msg = "Nice All RT Tasks", |
425 | .enable_mask = SYSRQ_ENABLE_RTNICE, | |
426 | }; | |
427 | ||
428 | /* Key Operations table and lock */ | |
429 | static DEFINE_SPINLOCK(sysrq_key_table_lock); | |
bf36b901 AM |
430 | |
431 | static struct sysrq_key_op *sysrq_key_table[36] = { | |
432 | &sysrq_loglevel_op, /* 0 */ | |
433 | &sysrq_loglevel_op, /* 1 */ | |
434 | &sysrq_loglevel_op, /* 2 */ | |
435 | &sysrq_loglevel_op, /* 3 */ | |
436 | &sysrq_loglevel_op, /* 4 */ | |
437 | &sysrq_loglevel_op, /* 5 */ | |
438 | &sysrq_loglevel_op, /* 6 */ | |
439 | &sysrq_loglevel_op, /* 7 */ | |
440 | &sysrq_loglevel_op, /* 8 */ | |
441 | &sysrq_loglevel_op, /* 9 */ | |
442 | ||
443 | /* | |
d346cce3 RD |
444 | * a: Don't use for system provided sysrqs, it is handled specially on |
445 | * sparc and will never arrive. | |
bf36b901 AM |
446 | */ |
447 | NULL, /* a */ | |
448 | &sysrq_reboot_op, /* b */ | |
5e351410 | 449 | &sysrq_crash_op, /* c */ |
bf36b901 AM |
450 | &sysrq_showlocks_op, /* d */ |
451 | &sysrq_term_op, /* e */ | |
452 | &sysrq_moom_op, /* f */ | |
364b5b7b | 453 | /* g: May be registered for the kernel debugger */ |
bf36b901 | 454 | NULL, /* g */ |
c2d75438 | 455 | NULL, /* h - reserved for help */ |
bf36b901 | 456 | &sysrq_kill_op, /* i */ |
c2d75438 ES |
457 | #ifdef CONFIG_BLOCK |
458 | &sysrq_thaw_op, /* j */ | |
459 | #else | |
bf36b901 | 460 | NULL, /* j */ |
c2d75438 | 461 | #endif |
bf36b901 | 462 | &sysrq_SAK_op, /* k */ |
5045bcae RR |
463 | #ifdef CONFIG_SMP |
464 | &sysrq_showallcpus_op, /* l */ | |
465 | #else | |
bf36b901 | 466 | NULL, /* l */ |
5045bcae | 467 | #endif |
bf36b901 AM |
468 | &sysrq_showmem_op, /* m */ |
469 | &sysrq_unrt_op, /* n */ | |
d346cce3 | 470 | /* o: This will often be registered as 'Off' at init time */ |
bf36b901 AM |
471 | NULL, /* o */ |
472 | &sysrq_showregs_op, /* p */ | |
88ad0bf6 | 473 | &sysrq_show_timers_op, /* q */ |
d346cce3 | 474 | &sysrq_unraw_op, /* r */ |
bf36b901 AM |
475 | &sysrq_sync_op, /* s */ |
476 | &sysrq_showstate_op, /* t */ | |
477 | &sysrq_mountro_op, /* u */ | |
364b5b7b | 478 | /* v: May be registered for frame buffer console restore */ |
bf36b901 | 479 | NULL, /* v */ |
d346cce3 | 480 | &sysrq_showstate_blocked_op, /* w */ |
d1e9a4f5 | 481 | /* x: May be registered on mips for TLB dump */ |
d346cce3 | 482 | /* x: May be registered on ppc/powerpc for xmon */ |
916ca14a | 483 | /* x: May be registered on sparc64 for global PMU dump */ |
bfd27940 | 484 | /* x: May be registered on x86_64 for disabling secure boot */ |
d346cce3 | 485 | NULL, /* x */ |
93dae5b7 | 486 | /* y: May be registered on sparc64 for global register dump */ |
bf36b901 | 487 | NULL, /* y */ |
69f698ad | 488 | &sysrq_ftrace_dump_op, /* z */ |
1da177e4 LT |
489 | }; |
490 | ||
491 | /* key2index calculation, -1 on invalid index */ | |
bf36b901 AM |
492 | static int sysrq_key_table_key2index(int key) |
493 | { | |
1da177e4 | 494 | int retval; |
bf36b901 AM |
495 | |
496 | if ((key >= '0') && (key <= '9')) | |
1da177e4 | 497 | retval = key - '0'; |
bf36b901 | 498 | else if ((key >= 'a') && (key <= 'z')) |
1da177e4 | 499 | retval = key + 10 - 'a'; |
bf36b901 | 500 | else |
1da177e4 | 501 | retval = -1; |
1da177e4 LT |
502 | return retval; |
503 | } | |
504 | ||
505 | /* | |
506 | * get and put functions for the table, exposed to modules. | |
507 | */ | |
bf36b901 AM |
508 | struct sysrq_key_op *__sysrq_get_key_op(int key) |
509 | { | |
510 | struct sysrq_key_op *op_p = NULL; | |
1da177e4 | 511 | int i; |
bf36b901 | 512 | |
1da177e4 | 513 | i = sysrq_key_table_key2index(key); |
bf36b901 AM |
514 | if (i != -1) |
515 | op_p = sysrq_key_table[i]; | |
97f5f0cd | 516 | |
1da177e4 LT |
517 | return op_p; |
518 | } | |
519 | ||
bf36b901 AM |
520 | static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p) |
521 | { | |
522 | int i = sysrq_key_table_key2index(key); | |
1da177e4 | 523 | |
1da177e4 LT |
524 | if (i != -1) |
525 | sysrq_key_table[i] = op_p; | |
526 | } | |
527 | ||
bfd27940 | 528 | void __handle_sysrq(int key, unsigned int from) |
1da177e4 LT |
529 | { |
530 | struct sysrq_key_op *op_p; | |
531 | int orig_log_level; | |
bf36b901 | 532 | int i; |
1da177e4 | 533 | |
722773af | 534 | rcu_sysrq_start(); |
984d74a7 | 535 | rcu_read_lock(); |
fb144adc AW |
536 | /* |
537 | * Raise the apparent loglevel to maximum so that the sysrq header | |
538 | * is shown to provide the user with positive feedback. We do not | |
539 | * simply emit this at KERN_EMERG as that would change message | |
540 | * routing in the consumers of /proc/kmsg. | |
541 | */ | |
1da177e4 | 542 | orig_log_level = console_loglevel; |
a8fe19eb | 543 | console_loglevel = CONSOLE_LOGLEVEL_DEFAULT; |
401e4a7c | 544 | pr_info("SysRq : "); |
1da177e4 LT |
545 | |
546 | op_p = __sysrq_get_key_op(key); | |
547 | if (op_p) { | |
bfd27940 KM |
548 | /* Ban synthetic events from some sysrq functionality */ |
549 | if ((from == SYSRQ_FROM_PROC || from == SYSRQ_FROM_SYNTHETIC) && | |
550 | op_p->enable_mask & SYSRQ_DISABLE_USERSPACE) | |
551 | printk("This sysrq operation is disabled from userspace.\n"); | |
bf36b901 AM |
552 | /* |
553 | * Should we check for enabled operations (/proc/sysrq-trigger | |
554 | * should not) and is the invoked operation enabled? | |
555 | */ | |
bfd27940 | 556 | if (from == SYSRQ_FROM_KERNEL || sysrq_on_mask(op_p->enable_mask)) { |
401e4a7c | 557 | pr_cont("%s\n", op_p->action_msg); |
1da177e4 | 558 | console_loglevel = orig_log_level; |
1495cc9d | 559 | op_p->handler(key); |
bf36b901 | 560 | } else { |
401e4a7c | 561 | pr_cont("This sysrq operation is disabled.\n"); |
bf36b901 | 562 | } |
1da177e4 | 563 | } else { |
401e4a7c | 564 | pr_cont("HELP : "); |
1da177e4 | 565 | /* Only print the help msg once per handler */ |
bf36b901 AM |
566 | for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) { |
567 | if (sysrq_key_table[i]) { | |
568 | int j; | |
569 | ||
570 | for (j = 0; sysrq_key_table[i] != | |
571 | sysrq_key_table[j]; j++) | |
572 | ; | |
573 | if (j != i) | |
574 | continue; | |
401e4a7c | 575 | pr_cont("%s ", sysrq_key_table[i]->help_msg); |
bf36b901 | 576 | } |
1da177e4 | 577 | } |
401e4a7c | 578 | pr_cont("\n"); |
1da177e4 LT |
579 | console_loglevel = orig_log_level; |
580 | } | |
984d74a7 | 581 | rcu_read_unlock(); |
722773af | 582 | rcu_sysrq_end(); |
1da177e4 LT |
583 | } |
584 | ||
f335397d | 585 | void handle_sysrq(int key) |
1da177e4 | 586 | { |
5d6f647f | 587 | if (sysrq_on()) |
bfd27940 | 588 | __handle_sysrq(key, SYSRQ_FROM_KERNEL); |
1da177e4 | 589 | } |
bf36b901 | 590 | EXPORT_SYMBOL(handle_sysrq); |
1da177e4 | 591 | |
97f5f0cd | 592 | #ifdef CONFIG_INPUT |
ffb6e0c9 | 593 | static int sysrq_reset_downtime_ms; |
97f5f0cd DT |
594 | |
595 | /* Simple translation table for the SysRq keys */ | |
fcb71930 | 596 | static const unsigned char sysrq_xlate[KEY_CNT] = |
97f5f0cd DT |
597 | "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */ |
598 | "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */ | |
599 | "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */ | |
600 | "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */ | |
601 | "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */ | |
602 | "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */ | |
603 | "\r\000/"; /* 0x60 - 0x6f */ | |
604 | ||
fcb71930 DT |
605 | struct sysrq_state { |
606 | struct input_handle handle; | |
607 | struct work_struct reinject_work; | |
608 | unsigned long key_down[BITS_TO_LONGS(KEY_CNT)]; | |
609 | unsigned int alt; | |
610 | unsigned int alt_use; | |
611 | bool active; | |
612 | bool need_reinject; | |
7ab7b5ad | 613 | bool reinjecting; |
154b7a48 MP |
614 | |
615 | /* reset sequence handling */ | |
616 | bool reset_canceled; | |
3d289517 | 617 | bool reset_requested; |
154b7a48 MP |
618 | unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)]; |
619 | int reset_seq_len; | |
620 | int reset_seq_cnt; | |
621 | int reset_seq_version; | |
39030786 | 622 | struct timer_list keyreset_timer; |
fcb71930 DT |
623 | }; |
624 | ||
154b7a48 MP |
625 | #define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */ |
626 | static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX]; | |
627 | static unsigned int sysrq_reset_seq_len; | |
628 | static unsigned int sysrq_reset_seq_version = 1; | |
629 | ||
630 | static void sysrq_parse_reset_sequence(struct sysrq_state *state) | |
631 | { | |
632 | int i; | |
633 | unsigned short key; | |
634 | ||
635 | state->reset_seq_cnt = 0; | |
636 | ||
637 | for (i = 0; i < sysrq_reset_seq_len; i++) { | |
638 | key = sysrq_reset_seq[i]; | |
639 | ||
640 | if (key == KEY_RESERVED || key > KEY_MAX) | |
641 | break; | |
642 | ||
643 | __set_bit(key, state->reset_keybit); | |
644 | state->reset_seq_len++; | |
645 | ||
646 | if (test_bit(key, state->key_down)) | |
647 | state->reset_seq_cnt++; | |
648 | } | |
649 | ||
650 | /* Disable reset until old keys are not released */ | |
651 | state->reset_canceled = state->reset_seq_cnt != 0; | |
652 | ||
653 | state->reset_seq_version = sysrq_reset_seq_version; | |
654 | } | |
655 | ||
3d289517 | 656 | static void sysrq_do_reset(unsigned long _state) |
39030786 | 657 | { |
3d289517 MP |
658 | struct sysrq_state *state = (struct sysrq_state *) _state; |
659 | ||
660 | state->reset_requested = true; | |
661 | ||
662 | sys_sync(); | |
663 | kernel_restart(NULL); | |
39030786 MP |
664 | } |
665 | ||
666 | static void sysrq_handle_reset_request(struct sysrq_state *state) | |
667 | { | |
3d289517 | 668 | if (state->reset_requested) |
bfd27940 | 669 | __handle_sysrq(sysrq_xlate[KEY_B], SYSRQ_FROM_KERNEL); |
3d289517 | 670 | |
39030786 MP |
671 | if (sysrq_reset_downtime_ms) |
672 | mod_timer(&state->keyreset_timer, | |
673 | jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms)); | |
674 | else | |
3d289517 | 675 | sysrq_do_reset((unsigned long)state); |
39030786 MP |
676 | } |
677 | ||
678 | static void sysrq_detect_reset_sequence(struct sysrq_state *state, | |
154b7a48 MP |
679 | unsigned int code, int value) |
680 | { | |
681 | if (!test_bit(code, state->reset_keybit)) { | |
682 | /* | |
683 | * Pressing any key _not_ in reset sequence cancels | |
39030786 MP |
684 | * the reset sequence. Also cancelling the timer in |
685 | * case additional keys were pressed after a reset | |
686 | * has been requested. | |
154b7a48 | 687 | */ |
39030786 | 688 | if (value && state->reset_seq_cnt) { |
154b7a48 | 689 | state->reset_canceled = true; |
39030786 MP |
690 | del_timer(&state->keyreset_timer); |
691 | } | |
154b7a48 | 692 | } else if (value == 0) { |
39030786 MP |
693 | /* |
694 | * Key release - all keys in the reset sequence need | |
695 | * to be pressed and held for the reset timeout | |
696 | * to hold. | |
697 | */ | |
698 | del_timer(&state->keyreset_timer); | |
699 | ||
154b7a48 MP |
700 | if (--state->reset_seq_cnt == 0) |
701 | state->reset_canceled = false; | |
702 | } else if (value == 1) { | |
703 | /* key press, not autorepeat */ | |
704 | if (++state->reset_seq_cnt == state->reset_seq_len && | |
705 | !state->reset_canceled) { | |
39030786 | 706 | sysrq_handle_reset_request(state); |
154b7a48 MP |
707 | } |
708 | } | |
154b7a48 MP |
709 | } |
710 | ||
4c076eb0 MP |
711 | #ifdef CONFIG_OF |
712 | static void sysrq_of_get_keyreset_config(void) | |
713 | { | |
714 | u32 key; | |
715 | struct device_node *np; | |
716 | struct property *prop; | |
717 | const __be32 *p; | |
718 | ||
719 | np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq"); | |
720 | if (!np) { | |
721 | pr_debug("No sysrq node found"); | |
722 | return; | |
723 | } | |
724 | ||
725 | /* Reset in case a __weak definition was present */ | |
726 | sysrq_reset_seq_len = 0; | |
727 | ||
728 | of_property_for_each_u32(np, "keyset", prop, p, key) { | |
729 | if (key == KEY_RESERVED || key > KEY_MAX || | |
730 | sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX) | |
731 | break; | |
732 | ||
733 | sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key; | |
734 | } | |
735 | ||
736 | /* Get reset timeout if any. */ | |
737 | of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms); | |
738 | } | |
739 | #else | |
740 | static void sysrq_of_get_keyreset_config(void) | |
741 | { | |
742 | } | |
743 | #endif | |
744 | ||
fcb71930 DT |
745 | static void sysrq_reinject_alt_sysrq(struct work_struct *work) |
746 | { | |
747 | struct sysrq_state *sysrq = | |
748 | container_of(work, struct sysrq_state, reinject_work); | |
749 | struct input_handle *handle = &sysrq->handle; | |
750 | unsigned int alt_code = sysrq->alt_use; | |
751 | ||
752 | if (sysrq->need_reinject) { | |
7ab7b5ad DT |
753 | /* we do not want the assignment to be reordered */ |
754 | sysrq->reinjecting = true; | |
755 | mb(); | |
756 | ||
fcb71930 DT |
757 | /* Simulate press and release of Alt + SysRq */ |
758 | input_inject_event(handle, EV_KEY, alt_code, 1); | |
759 | input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1); | |
760 | input_inject_event(handle, EV_SYN, SYN_REPORT, 1); | |
761 | ||
762 | input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0); | |
763 | input_inject_event(handle, EV_KEY, alt_code, 0); | |
764 | input_inject_event(handle, EV_SYN, SYN_REPORT, 1); | |
7ab7b5ad DT |
765 | |
766 | mb(); | |
767 | sysrq->reinjecting = false; | |
fcb71930 DT |
768 | } |
769 | } | |
97f5f0cd | 770 | |
154b7a48 MP |
771 | static bool sysrq_handle_keypress(struct sysrq_state *sysrq, |
772 | unsigned int code, int value) | |
97f5f0cd | 773 | { |
fcb71930 | 774 | bool was_active = sysrq->active; |
1966cb22 DT |
775 | bool suppress; |
776 | ||
154b7a48 | 777 | switch (code) { |
7ab7b5ad | 778 | |
154b7a48 MP |
779 | case KEY_LEFTALT: |
780 | case KEY_RIGHTALT: | |
781 | if (!value) { | |
782 | /* One of ALTs is being released */ | |
783 | if (sysrq->active && code == sysrq->alt_use) | |
784 | sysrq->active = false; | |
1966cb22 | 785 | |
154b7a48 MP |
786 | sysrq->alt = KEY_RESERVED; |
787 | ||
788 | } else if (value != 2) { | |
789 | sysrq->alt = code; | |
790 | sysrq->need_reinject = false; | |
791 | } | |
fcb71930 | 792 | break; |
97f5f0cd | 793 | |
154b7a48 MP |
794 | case KEY_SYSRQ: |
795 | if (value == 1 && sysrq->alt != KEY_RESERVED) { | |
796 | sysrq->active = true; | |
797 | sysrq->alt_use = sysrq->alt; | |
798 | /* | |
799 | * If nothing else will be pressed we'll need | |
800 | * to re-inject Alt-SysRq keysroke. | |
801 | */ | |
802 | sysrq->need_reinject = true; | |
803 | } | |
97f5f0cd | 804 | |
154b7a48 MP |
805 | /* |
806 | * Pretend that sysrq was never pressed at all. This | |
807 | * is needed to properly handle KGDB which will try | |
808 | * to release all keys after exiting debugger. If we | |
809 | * do not clear key bit it KGDB will end up sending | |
810 | * release events for Alt and SysRq, potentially | |
811 | * triggering print screen function. | |
812 | */ | |
813 | if (sysrq->active) | |
814 | clear_bit(KEY_SYSRQ, sysrq->handle.dev->key); | |
f5dec511 | 815 | |
154b7a48 | 816 | break; |
fcb71930 | 817 | |
154b7a48 MP |
818 | default: |
819 | if (sysrq->active && value && value != 2) { | |
bfd27940 KM |
820 | int from = sysrq->handle.dev->flags & INPUTDEV_FLAGS_SYNTHETIC ? |
821 | SYSRQ_FROM_SYNTHETIC : 0; | |
154b7a48 | 822 | sysrq->need_reinject = false; |
bfd27940 | 823 | __handle_sysrq(sysrq_xlate[code], from); |
154b7a48 MP |
824 | } |
825 | break; | |
826 | } | |
fcb71930 | 827 | |
154b7a48 | 828 | suppress = sysrq->active; |
fcb71930 | 829 | |
154b7a48 | 830 | if (!sysrq->active) { |
fcb71930 | 831 | |
154b7a48 MP |
832 | /* |
833 | * See if reset sequence has changed since the last time. | |
834 | */ | |
835 | if (sysrq->reset_seq_version != sysrq_reset_seq_version) | |
836 | sysrq_parse_reset_sequence(sysrq); | |
fcb71930 | 837 | |
154b7a48 MP |
838 | /* |
839 | * If we are not suppressing key presses keep track of | |
840 | * keyboard state so we can release keys that have been | |
841 | * pressed before entering SysRq mode. | |
842 | */ | |
843 | if (value) | |
844 | set_bit(code, sysrq->key_down); | |
845 | else | |
846 | clear_bit(code, sysrq->key_down); | |
847 | ||
848 | if (was_active) | |
849 | schedule_work(&sysrq->reinject_work); | |
850 | ||
39030786 MP |
851 | /* Check for reset sequence */ |
852 | sysrq_detect_reset_sequence(sysrq, code, value); | |
97f5f0cd | 853 | |
154b7a48 MP |
854 | } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) { |
855 | /* | |
856 | * Pass on release events for keys that was pressed before | |
857 | * entering SysRq mode. | |
858 | */ | |
859 | suppress = false; | |
860 | } | |
fcb71930 | 861 | |
154b7a48 MP |
862 | return suppress; |
863 | } | |
fcb71930 | 864 | |
154b7a48 MP |
865 | static bool sysrq_filter(struct input_handle *handle, |
866 | unsigned int type, unsigned int code, int value) | |
867 | { | |
868 | struct sysrq_state *sysrq = handle->private; | |
869 | bool suppress; | |
fcb71930 | 870 | |
154b7a48 MP |
871 | /* |
872 | * Do not filter anything if we are in the process of re-injecting | |
873 | * Alt+SysRq combination. | |
874 | */ | |
875 | if (sysrq->reinjecting) | |
876 | return false; | |
877 | ||
878 | switch (type) { | |
879 | ||
880 | case EV_SYN: | |
881 | suppress = false; | |
882 | break; | |
883 | ||
884 | case EV_KEY: | |
885 | suppress = sysrq_handle_keypress(sysrq, code, value); | |
97f5f0cd DT |
886 | break; |
887 | ||
888 | default: | |
fcb71930 | 889 | suppress = sysrq->active; |
97f5f0cd DT |
890 | break; |
891 | } | |
892 | ||
1966cb22 | 893 | return suppress; |
97f5f0cd DT |
894 | } |
895 | ||
896 | static int sysrq_connect(struct input_handler *handler, | |
897 | struct input_dev *dev, | |
898 | const struct input_device_id *id) | |
899 | { | |
fcb71930 | 900 | struct sysrq_state *sysrq; |
97f5f0cd DT |
901 | int error; |
902 | ||
fcb71930 DT |
903 | sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL); |
904 | if (!sysrq) | |
97f5f0cd DT |
905 | return -ENOMEM; |
906 | ||
fcb71930 DT |
907 | INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq); |
908 | ||
909 | sysrq->handle.dev = dev; | |
910 | sysrq->handle.handler = handler; | |
911 | sysrq->handle.name = "sysrq"; | |
912 | sysrq->handle.private = sysrq; | |
3d289517 MP |
913 | setup_timer(&sysrq->keyreset_timer, |
914 | sysrq_do_reset, (unsigned long)sysrq); | |
97f5f0cd | 915 | |
fcb71930 | 916 | error = input_register_handle(&sysrq->handle); |
97f5f0cd DT |
917 | if (error) { |
918 | pr_err("Failed to register input sysrq handler, error %d\n", | |
919 | error); | |
920 | goto err_free; | |
921 | } | |
922 | ||
fcb71930 | 923 | error = input_open_device(&sysrq->handle); |
97f5f0cd DT |
924 | if (error) { |
925 | pr_err("Failed to open input device, error %d\n", error); | |
926 | goto err_unregister; | |
927 | } | |
928 | ||
929 | return 0; | |
930 | ||
931 | err_unregister: | |
fcb71930 | 932 | input_unregister_handle(&sysrq->handle); |
97f5f0cd | 933 | err_free: |
fcb71930 | 934 | kfree(sysrq); |
97f5f0cd DT |
935 | return error; |
936 | } | |
937 | ||
938 | static void sysrq_disconnect(struct input_handle *handle) | |
939 | { | |
fcb71930 DT |
940 | struct sysrq_state *sysrq = handle->private; |
941 | ||
97f5f0cd | 942 | input_close_device(handle); |
fcb71930 | 943 | cancel_work_sync(&sysrq->reinject_work); |
39030786 | 944 | del_timer_sync(&sysrq->keyreset_timer); |
97f5f0cd | 945 | input_unregister_handle(handle); |
fcb71930 | 946 | kfree(sysrq); |
97f5f0cd DT |
947 | } |
948 | ||
949 | /* | |
1966cb22 DT |
950 | * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all |
951 | * keyboards have SysRq key predefined and so user may add it to keymap | |
97f5f0cd DT |
952 | * later, but we expect all such keyboards to have left alt. |
953 | */ | |
954 | static const struct input_device_id sysrq_ids[] = { | |
955 | { | |
956 | .flags = INPUT_DEVICE_ID_MATCH_EVBIT | | |
957 | INPUT_DEVICE_ID_MATCH_KEYBIT, | |
802c0388 AM |
958 | .evbit = { [BIT_WORD(EV_KEY)] = BIT_MASK(EV_KEY) }, |
959 | .keybit = { [BIT_WORD(KEY_LEFTALT)] = BIT_MASK(KEY_LEFTALT) }, | |
97f5f0cd DT |
960 | }, |
961 | { }, | |
962 | }; | |
963 | ||
964 | static struct input_handler sysrq_handler = { | |
965 | .filter = sysrq_filter, | |
966 | .connect = sysrq_connect, | |
967 | .disconnect = sysrq_disconnect, | |
968 | .name = "sysrq", | |
969 | .id_table = sysrq_ids, | |
970 | }; | |
971 | ||
972 | static bool sysrq_handler_registered; | |
973 | ||
974 | static inline void sysrq_register_handler(void) | |
975 | { | |
976 | int error; | |
977 | ||
4c076eb0 MP |
978 | sysrq_of_get_keyreset_config(); |
979 | ||
97f5f0cd DT |
980 | error = input_register_handler(&sysrq_handler); |
981 | if (error) | |
982 | pr_err("Failed to register input handler, error %d", error); | |
983 | else | |
984 | sysrq_handler_registered = true; | |
985 | } | |
986 | ||
987 | static inline void sysrq_unregister_handler(void) | |
988 | { | |
989 | if (sysrq_handler_registered) { | |
990 | input_unregister_handler(&sysrq_handler); | |
991 | sysrq_handler_registered = false; | |
992 | } | |
993 | } | |
994 | ||
154b7a48 MP |
995 | static int sysrq_reset_seq_param_set(const char *buffer, |
996 | const struct kernel_param *kp) | |
997 | { | |
998 | unsigned long val; | |
999 | int error; | |
1000 | ||
86b40567 | 1001 | error = kstrtoul(buffer, 0, &val); |
154b7a48 MP |
1002 | if (error < 0) |
1003 | return error; | |
1004 | ||
1005 | if (val > KEY_MAX) | |
1006 | return -EINVAL; | |
1007 | ||
1008 | *((unsigned short *)kp->arg) = val; | |
1009 | sysrq_reset_seq_version++; | |
1010 | ||
1011 | return 0; | |
1012 | } | |
1013 | ||
9c27847d | 1014 | static const struct kernel_param_ops param_ops_sysrq_reset_seq = { |
154b7a48 MP |
1015 | .get = param_get_ushort, |
1016 | .set = sysrq_reset_seq_param_set, | |
1017 | }; | |
1018 | ||
1019 | #define param_check_sysrq_reset_seq(name, p) \ | |
1020 | __param_check(name, p, unsigned short) | |
1021 | ||
3bce6f64 PG |
1022 | /* |
1023 | * not really modular, but the easiest way to keep compat with existing | |
1024 | * bootargs behaviour is to continue using module_param here. | |
1025 | */ | |
154b7a48 MP |
1026 | module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq, |
1027 | &sysrq_reset_seq_len, 0644); | |
1028 | ||
39030786 MP |
1029 | module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644); |
1030 | ||
97f5f0cd DT |
1031 | #else |
1032 | ||
1033 | static inline void sysrq_register_handler(void) | |
1034 | { | |
1035 | } | |
1036 | ||
1037 | static inline void sysrq_unregister_handler(void) | |
1038 | { | |
1039 | } | |
1040 | ||
1041 | #endif /* CONFIG_INPUT */ | |
1042 | ||
1043 | int sysrq_toggle_support(int enable_mask) | |
1044 | { | |
1045 | bool was_enabled = sysrq_on(); | |
1046 | ||
1047 | sysrq_enabled = enable_mask; | |
1048 | ||
1049 | if (was_enabled != sysrq_on()) { | |
1050 | if (sysrq_on()) | |
1051 | sysrq_register_handler(); | |
1052 | else | |
1053 | sysrq_unregister_handler(); | |
1054 | } | |
1055 | ||
1056 | return 0; | |
1057 | } | |
1058 | ||
cf62ddce | 1059 | static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p, |
bf36b901 AM |
1060 | struct sysrq_key_op *remove_op_p) |
1061 | { | |
1da177e4 | 1062 | int retval; |
1da177e4 | 1063 | |
984d74a7 | 1064 | spin_lock(&sysrq_key_table_lock); |
1da177e4 LT |
1065 | if (__sysrq_get_key_op(key) == remove_op_p) { |
1066 | __sysrq_put_key_op(key, insert_op_p); | |
1067 | retval = 0; | |
1068 | } else { | |
1069 | retval = -1; | |
1070 | } | |
984d74a7 RR |
1071 | spin_unlock(&sysrq_key_table_lock); |
1072 | ||
1073 | /* | |
1074 | * A concurrent __handle_sysrq either got the old op or the new op. | |
1075 | * Wait for it to go away before returning, so the code for an old | |
1076 | * op is not freed (eg. on module unload) while it is in use. | |
1077 | */ | |
1078 | synchronize_rcu(); | |
1079 | ||
1da177e4 LT |
1080 | return retval; |
1081 | } | |
1082 | ||
1083 | int register_sysrq_key(int key, struct sysrq_key_op *op_p) | |
1084 | { | |
1085 | return __sysrq_swap_key_ops(key, op_p, NULL); | |
1086 | } | |
bf36b901 | 1087 | EXPORT_SYMBOL(register_sysrq_key); |
1da177e4 LT |
1088 | |
1089 | int unregister_sysrq_key(int key, struct sysrq_key_op *op_p) | |
1090 | { | |
1091 | return __sysrq_swap_key_ops(key, NULL, op_p); | |
1092 | } | |
1da177e4 | 1093 | EXPORT_SYMBOL(unregister_sysrq_key); |
f40cbaa5 AD |
1094 | |
1095 | #ifdef CONFIG_PROC_FS | |
1096 | /* | |
1097 | * writing 'C' to /proc/sysrq-trigger is like sysrq-C | |
1098 | */ | |
1099 | static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf, | |
1100 | size_t count, loff_t *ppos) | |
1101 | { | |
1102 | if (count) { | |
1103 | char c; | |
1104 | ||
1105 | if (get_user(c, buf)) | |
1106 | return -EFAULT; | |
bfd27940 | 1107 | __handle_sysrq(c, SYSRQ_FROM_PROC); |
f40cbaa5 | 1108 | } |
97f5f0cd | 1109 | |
f40cbaa5 AD |
1110 | return count; |
1111 | } | |
1112 | ||
1113 | static const struct file_operations proc_sysrq_trigger_operations = { | |
1114 | .write = write_sysrq_trigger, | |
6038f373 | 1115 | .llseek = noop_llseek, |
f40cbaa5 AD |
1116 | }; |
1117 | ||
97f5f0cd DT |
1118 | static void sysrq_init_procfs(void) |
1119 | { | |
1120 | if (!proc_create("sysrq-trigger", S_IWUSR, NULL, | |
1121 | &proc_sysrq_trigger_operations)) | |
1122 | pr_err("Failed to register proc interface\n"); | |
1123 | } | |
1124 | ||
1125 | #else | |
1126 | ||
1127 | static inline void sysrq_init_procfs(void) | |
1128 | { | |
1129 | } | |
1130 | ||
1131 | #endif /* CONFIG_PROC_FS */ | |
1132 | ||
f40cbaa5 AD |
1133 | static int __init sysrq_init(void) |
1134 | { | |
97f5f0cd DT |
1135 | sysrq_init_procfs(); |
1136 | ||
1137 | if (sysrq_on()) | |
1138 | sysrq_register_handler(); | |
1139 | ||
f40cbaa5 AD |
1140 | return 0; |
1141 | } | |
3bce6f64 | 1142 | device_initcall(sysrq_init); |