blob: 0540d5de2c17c867e68b1315475bfe687cc44133 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* -*- linux-c -*-
2 *
3 * $Id: sysrq.c,v 1.15 1998/08/23 14:56:41 mj Exp $
4 *
5 * Linux Magic System Request Key Hacks
6 *
7 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
9 *
10 * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
11 * overhauled to use key registration
12 * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
13 */
14
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/sched.h>
16#include <linux/interrupt.h>
17#include <linux/mm.h>
18#include <linux/fs.h>
19#include <linux/tty.h>
20#include <linux/mount.h>
21#include <linux/kdev_t.h>
22#include <linux/major.h>
23#include <linux/reboot.h>
24#include <linux/sysrq.h>
25#include <linux/kbd_kern.h>
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -070026#include <linux/proc_fs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/quotaops.h>
Thomas Gleixner0793a612008-12-04 20:12:29 +010028#include <linux/perf_counter.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kernel.h>
30#include <linux/module.h>
31#include <linux/suspend.h>
32#include <linux/writeback.h>
33#include <linux/buffer_head.h> /* for fsync_bdev() */
34#include <linux/swap.h>
35#include <linux/spinlock.h>
36#include <linux/vt_kern.h>
37#include <linux/workqueue.h>
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -070038#include <linux/kexec.h>
Heiko Carstens5886cea2009-03-31 19:13:13 +020039#include <linux/interrupt.h>
Ingo Molnar88ad0bf62007-02-16 01:28:16 -080040#include <linux/hrtimer.h>
David Rientjes5a3135c2007-10-16 23:25:53 -070041#include <linux/oom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43#include <asm/ptrace.h>
Heiko Carstens2033b0c2006-10-06 16:38:42 +020044#include <asm/irq_regs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46/* Whether we react on sysrq keys or just ignore them */
Ingo Molnar5d6f6472006-12-13 00:34:36 -080047int __read_mostly __sysrq_enabled = 1;
48
49static int __read_mostly sysrq_always_enabled;
50
51int sysrq_on(void)
52{
53 return __sysrq_enabled || sysrq_always_enabled;
54}
55
56/*
57 * A value of 1 means 'all', other nonzero values are an op mask:
58 */
59static inline int sysrq_on_mask(int mask)
60{
61 return sysrq_always_enabled || __sysrq_enabled == 1 ||
62 (__sysrq_enabled & mask);
63}
64
65static int __init sysrq_always_enabled_setup(char *str)
66{
67 sysrq_always_enabled = 1;
68 printk(KERN_INFO "debug: sysrq always enabled.\n");
69
70 return 1;
71}
72
73__setup("sysrq_always_enabled", sysrq_always_enabled_setup);
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
David Howells7d12e782006-10-05 14:55:46 +010076static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 int i;
79 i = key - '0';
80 console_loglevel = 7;
81 printk("Loglevel set to %d\n", i);
82 console_loglevel = i;
Andrew Mortonbf36b902006-03-25 03:07:08 -080083}
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static struct sysrq_key_op sysrq_loglevel_op = {
85 .handler = sysrq_handle_loglevel,
Randy Dunlap208b95c2009-01-06 14:41:13 -080086 .help_msg = "loglevel(0-9)",
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 .action_msg = "Changing Loglevel",
88 .enable_mask = SYSRQ_ENABLE_LOG,
89};
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091#ifdef CONFIG_VT
David Howells7d12e782006-10-05 14:55:46 +010092static void sysrq_handle_SAK(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Eric W. Biederman8b6312f2007-02-10 01:44:34 -080094 struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
Eric W. Biederman8b6312f2007-02-10 01:44:34 -080095 schedule_work(SAK_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97static struct sysrq_key_op sysrq_SAK_op = {
98 .handler = sysrq_handle_SAK,
99 .help_msg = "saK",
100 .action_msg = "SAK",
101 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
102};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800103#else
104#define sysrq_SAK_op (*(struct sysrq_key_op *)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105#endif
106
107#ifdef CONFIG_VT
David Howells7d12e782006-10-05 14:55:46 +0100108static void sysrq_handle_unraw(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct kbd_struct *kbd = &kbd_table[fg_console];
111
112 if (kbd)
Bill Nottingham2e8ecb92007-10-16 23:29:38 -0700113 kbd->kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115static struct sysrq_key_op sysrq_unraw_op = {
116 .handler = sysrq_handle_unraw,
117 .help_msg = "unRaw",
Bill Nottingham2e8ecb92007-10-16 23:29:38 -0700118 .action_msg = "Keyboard mode set to system default",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .enable_mask = SYSRQ_ENABLE_KEYBOARD,
120};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800121#else
122#define sysrq_unraw_op (*(struct sysrq_key_op *)0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123#endif /* CONFIG_VT */
124
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700125#ifdef CONFIG_KEXEC
David Howells7d12e782006-10-05 14:55:46 +0100126static void sysrq_handle_crashdump(int key, struct tty_struct *tty)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700127{
David Howells7d12e782006-10-05 14:55:46 +0100128 crash_kexec(get_irq_regs());
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700129}
130static struct sysrq_key_op sysrq_crashdump_op = {
131 .handler = sysrq_handle_crashdump,
132 .help_msg = "Crashdump",
133 .action_msg = "Trigger a crashdump",
134 .enable_mask = SYSRQ_ENABLE_DUMP,
135};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800136#else
137#define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
Hariprasad Nellitheertha86b1ae32005-06-25 14:58:25 -0700138#endif
139
David Howells7d12e782006-10-05 14:55:46 +0100140static void sysrq_handle_reboot(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Peter Zijlstrab2e9c7d2006-09-30 23:28:02 -0700142 lockdep_off();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 local_irq_enable();
Eric W. Biederman4de8b9b2005-07-26 11:51:06 -0600144 emergency_restart();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146static struct sysrq_key_op sysrq_reboot_op = {
147 .handler = sysrq_handle_reboot,
148 .help_msg = "reBoot",
149 .action_msg = "Resetting",
150 .enable_mask = SYSRQ_ENABLE_BOOT,
151};
152
David Howells7d12e782006-10-05 14:55:46 +0100153static void sysrq_handle_sync(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154{
155 emergency_sync();
156}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157static struct sysrq_key_op sysrq_sync_op = {
158 .handler = sysrq_handle_sync,
159 .help_msg = "Sync",
160 .action_msg = "Emergency Sync",
161 .enable_mask = SYSRQ_ENABLE_SYNC,
162};
163
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800164static void sysrq_handle_show_timers(int key, struct tty_struct *tty)
165{
166 sysrq_timer_list_show();
167}
168
169static struct sysrq_key_op sysrq_show_timers_op = {
170 .handler = sysrq_handle_show_timers,
171 .help_msg = "show-all-timers(Q)",
Thomas Gleixner322acf62008-10-20 12:33:14 +0200172 .action_msg = "Show clockevent devices & pending hrtimers (no others)",
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800173};
174
David Howells7d12e782006-10-05 14:55:46 +0100175static void sysrq_handle_mountro(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 emergency_remount();
178}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179static struct sysrq_key_op sysrq_mountro_op = {
180 .handler = sysrq_handle_mountro,
181 .help_msg = "Unmount",
182 .action_msg = "Emergency Remount R/O",
183 .enable_mask = SYSRQ_ENABLE_REMOUNT,
184};
185
Ingo Molnar8c645802006-07-03 00:24:56 -0700186#ifdef CONFIG_LOCKDEP
David Howells7d12e782006-10-05 14:55:46 +0100187static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800188{
Ingo Molnar9a11b49a2006-07-03 00:24:33 -0700189 debug_show_all_locks();
Ingo Molnarde5097c2006-01-09 15:59:21 -0800190}
Ingo Molnar8c645802006-07-03 00:24:56 -0700191
Ingo Molnarde5097c2006-01-09 15:59:21 -0800192static struct sysrq_key_op sysrq_showlocks_op = {
193 .handler = sysrq_handle_showlocks,
194 .help_msg = "show-all-locks(D)",
195 .action_msg = "Show Locks Held",
196};
Andrew Mortonbf36b902006-03-25 03:07:08 -0800197#else
198#define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
Ingo Molnarde5097c2006-01-09 15:59:21 -0800199#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
Rik van Riel5045bca2008-04-29 00:59:21 -0700201#ifdef CONFIG_SMP
202static DEFINE_SPINLOCK(show_lock);
203
204static void showacpu(void *dummy)
205{
206 unsigned long flags;
207
208 /* Idle CPUs have no interesting backtrace. */
209 if (idle_cpu(smp_processor_id()))
210 return;
211
212 spin_lock_irqsave(&show_lock, flags);
213 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
214 show_stack(NULL, NULL);
215 spin_unlock_irqrestore(&show_lock, flags);
216}
217
218static void sysrq_showregs_othercpus(struct work_struct *dummy)
219{
Ingo Molnar8b604d52008-06-27 11:52:45 +0200220 smp_call_function(showacpu, NULL, 0);
Rik van Riel5045bca2008-04-29 00:59:21 -0700221}
222
223static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
224
225static void sysrq_handle_showallcpus(int key, struct tty_struct *tty)
226{
227 struct pt_regs *regs = get_irq_regs();
228 if (regs) {
229 printk(KERN_INFO "CPU%d:\n", smp_processor_id());
230 show_regs(regs);
231 }
232 schedule_work(&sysrq_showallcpus);
233}
234
235static struct sysrq_key_op sysrq_showallcpus_op = {
236 .handler = sysrq_handle_showallcpus,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800237 .help_msg = "show-backtrace-all-active-cpus(L)",
Rik van Riel5045bca2008-04-29 00:59:21 -0700238 .action_msg = "Show backtrace of all active CPUs",
239 .enable_mask = SYSRQ_ENABLE_DUMP,
240};
241#endif
242
David Howells7d12e782006-10-05 14:55:46 +0100243static void sysrq_handle_showregs(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
David Howells7d12e782006-10-05 14:55:46 +0100245 struct pt_regs *regs = get_irq_regs();
246 if (regs)
247 show_regs(regs);
Thomas Gleixner0793a612008-12-04 20:12:29 +0100248 perf_counter_print_debug();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250static struct sysrq_key_op sysrq_showregs_op = {
251 .handler = sysrq_handle_showregs,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800252 .help_msg = "show-registers(P)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 .action_msg = "Show Regs",
254 .enable_mask = SYSRQ_ENABLE_DUMP,
255};
256
David Howells7d12e782006-10-05 14:55:46 +0100257static void sysrq_handle_showstate(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 show_state();
260}
261static struct sysrq_key_op sysrq_showstate_op = {
262 .handler = sysrq_handle_showstate,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800263 .help_msg = "show-task-states(T)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264 .action_msg = "Show State",
265 .enable_mask = SYSRQ_ENABLE_DUMP,
266};
267
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800268static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty)
269{
270 show_state_filter(TASK_UNINTERRUPTIBLE);
271}
272static struct sysrq_key_op sysrq_showstate_blocked_op = {
273 .handler = sysrq_handle_showstate_blocked,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800274 .help_msg = "show-blocked-tasks(W)",
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800275 .action_msg = "Show Blocked State",
276 .enable_mask = SYSRQ_ENABLE_DUMP,
277};
278
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100279#ifdef CONFIG_TRACING
280#include <linux/ftrace.h>
281
282static void sysrq_ftrace_dump(int key, struct tty_struct *tty)
283{
284 ftrace_dump();
285}
286static struct sysrq_key_op sysrq_ftrace_dump_op = {
287 .handler = sysrq_ftrace_dump,
Randy Dunlap3871f2f2008-12-24 16:06:57 -0800288 .help_msg = "dump-ftrace-buffer(Z)",
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100289 .action_msg = "Dump ftrace buffer",
290 .enable_mask = SYSRQ_ENABLE_DUMP,
291};
292#else
293#define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)0)
294#endif
Ingo Molnare59e2ae2006-12-06 20:35:59 -0800295
David Howells7d12e782006-10-05 14:55:46 +0100296static void sysrq_handle_showmem(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 show_mem();
299}
300static struct sysrq_key_op sysrq_showmem_op = {
301 .handler = sysrq_handle_showmem,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800302 .help_msg = "show-memory-usage(M)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 .action_msg = "Show Memory",
304 .enable_mask = SYSRQ_ENABLE_DUMP,
305};
306
Andrew Mortonbf36b902006-03-25 03:07:08 -0800307/*
308 * Signal sysrq helper function. Sends a signal to all user processes.
309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310static void send_sig_all(int sig)
311{
312 struct task_struct *p;
313
314 for_each_process(p) {
Serge E. Hallynb460cbc2007-10-18 23:39:52 -0700315 if (p->mm && !is_global_init(p))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* Not swapper, init nor kernel thread */
317 force_sig(sig, p);
318 }
319}
320
David Howells7d12e782006-10-05 14:55:46 +0100321static void sysrq_handle_term(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 send_sig_all(SIGTERM);
324 console_loglevel = 8;
325}
326static struct sysrq_key_op sysrq_term_op = {
327 .handler = sysrq_handle_term,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800328 .help_msg = "terminate-all-tasks(E)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 .action_msg = "Terminate All Tasks",
330 .enable_mask = SYSRQ_ENABLE_SIGNAL,
331};
332
David Howells65f27f32006-11-22 14:55:48 +0000333static void moom_callback(struct work_struct *ignored)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
Mel Gorman0e884602008-04-28 02:12:14 -0700335 out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336}
337
David Howells65f27f32006-11-22 14:55:48 +0000338static DECLARE_WORK(moom_work, moom_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
David Howells7d12e782006-10-05 14:55:46 +0100340static void sysrq_handle_moom(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
342 schedule_work(&moom_work);
343}
344static struct sysrq_key_op sysrq_moom_op = {
345 .handler = sysrq_handle_moom,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800346 .help_msg = "memory-full-oom-kill(F)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 .action_msg = "Manual OOM execution",
Naohiro Ooiwab4236f82008-10-15 22:01:43 -0700348 .enable_mask = SYSRQ_ENABLE_SIGNAL,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349};
350
Eric Sandeenc2d75432009-03-31 15:23:46 -0700351#ifdef CONFIG_BLOCK
352static void sysrq_handle_thaw(int key, struct tty_struct *tty)
353{
354 emergency_thaw_all();
355}
356static struct sysrq_key_op sysrq_thaw_op = {
357 .handler = sysrq_handle_thaw,
358 .help_msg = "thaw-filesystems(J)",
359 .action_msg = "Emergency Thaw of all frozen filesystems",
360 .enable_mask = SYSRQ_ENABLE_SIGNAL,
361};
362#endif
363
David Howells7d12e782006-10-05 14:55:46 +0100364static void sysrq_handle_kill(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 send_sig_all(SIGKILL);
367 console_loglevel = 8;
368}
369static struct sysrq_key_op sysrq_kill_op = {
370 .handler = sysrq_handle_kill,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800371 .help_msg = "kill-all-tasks(I)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 .action_msg = "Kill All Tasks",
373 .enable_mask = SYSRQ_ENABLE_SIGNAL,
374};
375
David Howells7d12e782006-10-05 14:55:46 +0100376static void sysrq_handle_unrt(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377{
378 normalize_rt_tasks();
379}
380static struct sysrq_key_op sysrq_unrt_op = {
381 .handler = sysrq_handle_unrt,
Randy Dunlap208b95c2009-01-06 14:41:13 -0800382 .help_msg = "nice-all-RT-tasks(N)",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 .action_msg = "Nice All RT Tasks",
384 .enable_mask = SYSRQ_ENABLE_RTNICE,
385};
386
387/* Key Operations table and lock */
388static DEFINE_SPINLOCK(sysrq_key_table_lock);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800389
390static struct sysrq_key_op *sysrq_key_table[36] = {
391 &sysrq_loglevel_op, /* 0 */
392 &sysrq_loglevel_op, /* 1 */
393 &sysrq_loglevel_op, /* 2 */
394 &sysrq_loglevel_op, /* 3 */
395 &sysrq_loglevel_op, /* 4 */
396 &sysrq_loglevel_op, /* 5 */
397 &sysrq_loglevel_op, /* 6 */
398 &sysrq_loglevel_op, /* 7 */
399 &sysrq_loglevel_op, /* 8 */
400 &sysrq_loglevel_op, /* 9 */
401
402 /*
Randy Dunlapd346cce2007-01-31 23:48:17 -0800403 * a: Don't use for system provided sysrqs, it is handled specially on
404 * sparc and will never arrive.
Andrew Mortonbf36b902006-03-25 03:07:08 -0800405 */
406 NULL, /* a */
407 &sysrq_reboot_op, /* b */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800408 &sysrq_crashdump_op, /* c & ibm_emac driver debug */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800409 &sysrq_showlocks_op, /* d */
410 &sysrq_term_op, /* e */
411 &sysrq_moom_op, /* f */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800412 /* g: May be registered by ppc for kgdb */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800413 NULL, /* g */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700414 NULL, /* h - reserved for help */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800415 &sysrq_kill_op, /* i */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700416#ifdef CONFIG_BLOCK
417 &sysrq_thaw_op, /* j */
418#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800419 NULL, /* j */
Eric Sandeenc2d75432009-03-31 15:23:46 -0700420#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800421 &sysrq_SAK_op, /* k */
Rik van Riel5045bca2008-04-29 00:59:21 -0700422#ifdef CONFIG_SMP
423 &sysrq_showallcpus_op, /* l */
424#else
Andrew Mortonbf36b902006-03-25 03:07:08 -0800425 NULL, /* l */
Rik van Riel5045bca2008-04-29 00:59:21 -0700426#endif
Andrew Mortonbf36b902006-03-25 03:07:08 -0800427 &sysrq_showmem_op, /* m */
428 &sysrq_unrt_op, /* n */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800429 /* o: This will often be registered as 'Off' at init time */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800430 NULL, /* o */
431 &sysrq_showregs_op, /* p */
Ingo Molnar88ad0bf62007-02-16 01:28:16 -0800432 &sysrq_show_timers_op, /* q */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800433 &sysrq_unraw_op, /* r */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800434 &sysrq_sync_op, /* s */
435 &sysrq_showstate_op, /* t */
436 &sysrq_mountro_op, /* u */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800437 /* v: May be registered at init time by SMP VOYAGER */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800438 NULL, /* v */
Randy Dunlapd346cce2007-01-31 23:48:17 -0800439 &sysrq_showstate_blocked_op, /* w */
440 /* x: May be registered on ppc/powerpc for xmon */
441 NULL, /* x */
David S. Miller93dae5b2008-05-19 23:46:00 -0700442 /* y: May be registered on sparc64 for global register dump */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800443 NULL, /* y */
Peter Zijlstra69f698a2008-11-01 19:53:34 +0100444 &sysrq_ftrace_dump_op, /* z */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445};
446
447/* key2index calculation, -1 on invalid index */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800448static int sysrq_key_table_key2index(int key)
449{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 int retval;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800451
452 if ((key >= '0') && (key <= '9'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 retval = key - '0';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800454 else if ((key >= 'a') && (key <= 'z'))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 retval = key + 10 - 'a';
Andrew Mortonbf36b902006-03-25 03:07:08 -0800456 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 retval = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return retval;
459}
460
461/*
462 * get and put functions for the table, exposed to modules.
463 */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800464struct sysrq_key_op *__sysrq_get_key_op(int key)
465{
466 struct sysrq_key_op *op_p = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 int i;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 i = sysrq_key_table_key2index(key);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800470 if (i != -1)
471 op_p = sysrq_key_table[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return op_p;
473}
474
Andrew Mortonbf36b902006-03-25 03:07:08 -0800475static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
476{
477 int i = sysrq_key_table_key2index(key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 if (i != -1)
480 sysrq_key_table[i] = op_p;
481}
482
483/*
Andrew Mortonbf36b902006-03-25 03:07:08 -0800484 * This is the non-locking version of handle_sysrq. It must/can only be called
485 * by sysrq key handlers, as they are inside of the lock
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 */
David Howells7d12e782006-10-05 14:55:46 +0100487void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct sysrq_key_op *op_p;
490 int orig_log_level;
Andrew Mortonbf36b902006-03-25 03:07:08 -0800491 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 unsigned long flags;
493
494 spin_lock_irqsave(&sysrq_key_table_lock, flags);
Andy Whitcroftfb144ad2009-01-15 13:50:52 -0800495 /*
496 * Raise the apparent loglevel to maximum so that the sysrq header
497 * is shown to provide the user with positive feedback. We do not
498 * simply emit this at KERN_EMERG as that would change message
499 * routing in the consumers of /proc/kmsg.
500 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 orig_log_level = console_loglevel;
502 console_loglevel = 7;
503 printk(KERN_INFO "SysRq : ");
504
505 op_p = __sysrq_get_key_op(key);
506 if (op_p) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800507 /*
508 * Should we check for enabled operations (/proc/sysrq-trigger
509 * should not) and is the invoked operation enabled?
510 */
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800511 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
Andrew Mortonbf36b902006-03-25 03:07:08 -0800512 printk("%s\n", op_p->action_msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 console_loglevel = orig_log_level;
David Howells7d12e782006-10-05 14:55:46 +0100514 op_p->handler(key, tty);
Andrew Mortonbf36b902006-03-25 03:07:08 -0800515 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 printk("This sysrq operation is disabled.\n");
Andrew Mortonbf36b902006-03-25 03:07:08 -0800517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 } else {
519 printk("HELP : ");
520 /* Only print the help msg once per handler */
Andrew Mortonbf36b902006-03-25 03:07:08 -0800521 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
522 if (sysrq_key_table[i]) {
523 int j;
524
525 for (j = 0; sysrq_key_table[i] !=
526 sysrq_key_table[j]; j++)
527 ;
528 if (j != i)
529 continue;
530 printk("%s ", sysrq_key_table[i]->help_msg);
531 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
Andrew Mortonbf36b902006-03-25 03:07:08 -0800533 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 console_loglevel = orig_log_level;
535 }
536 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
537}
538
539/*
540 * This function is called by the keyboard handler when SysRq is pressed
541 * and any other keycode arrives.
542 */
David Howells7d12e782006-10-05 14:55:46 +0100543void handle_sysrq(int key, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Ingo Molnar5d6f6472006-12-13 00:34:36 -0800545 if (sysrq_on())
546 __handle_sysrq(key, tty, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800548EXPORT_SYMBOL(handle_sysrq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Adrian Bunkcf62ddc2005-11-08 21:39:47 -0800550static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
Andrew Mortonbf36b902006-03-25 03:07:08 -0800551 struct sysrq_key_op *remove_op_p)
552{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 int retval;
555 unsigned long flags;
556
557 spin_lock_irqsave(&sysrq_key_table_lock, flags);
558 if (__sysrq_get_key_op(key) == remove_op_p) {
559 __sysrq_put_key_op(key, insert_op_p);
560 retval = 0;
561 } else {
562 retval = -1;
563 }
564 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 return retval;
566}
567
568int register_sysrq_key(int key, struct sysrq_key_op *op_p)
569{
570 return __sysrq_swap_key_ops(key, op_p, NULL);
571}
Andrew Mortonbf36b902006-03-25 03:07:08 -0800572EXPORT_SYMBOL(register_sysrq_key);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
575{
576 return __sysrq_swap_key_ops(key, NULL, op_p);
577}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578EXPORT_SYMBOL(unregister_sysrq_key);
Alexey Dobriyanf40cbaa2008-10-15 22:04:23 -0700579
580#ifdef CONFIG_PROC_FS
581/*
582 * writing 'C' to /proc/sysrq-trigger is like sysrq-C
583 */
584static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
585 size_t count, loff_t *ppos)
586{
587 if (count) {
588 char c;
589
590 if (get_user(c, buf))
591 return -EFAULT;
592 __handle_sysrq(c, NULL, 0);
593 }
594 return count;
595}
596
597static const struct file_operations proc_sysrq_trigger_operations = {
598 .write = write_sysrq_trigger,
599};
600
601static int __init sysrq_init(void)
602{
603 proc_create("sysrq-trigger", S_IWUSR, NULL, &proc_sysrq_trigger_operations);
604 return 0;
605}
606module_init(sysrq_init);
607#endif