Merge commit 'v2.6.29-rc1' into perfcounters/core
[linux-2.6] / drivers / char / sysrq.c
1 /* -*- 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
15 #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>
26 #include <linux/proc_fs.h>
27 #include <linux/quotaops.h>
28 #include <linux/perf_counter.h>
29 #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>
38 #include <linux/kexec.h>
39 #include <linux/irq.h>
40 #include <linux/hrtimer.h>
41 #include <linux/oom.h>
42
43 #include <asm/ptrace.h>
44 #include <asm/irq_regs.h>
45
46 /* Whether we react on sysrq keys or just ignore them */
47 int __read_mostly __sysrq_enabled = 1;
48
49 static int __read_mostly sysrq_always_enabled;
50
51 int 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  */
59 static inline int sysrq_on_mask(int mask)
60 {
61         return sysrq_always_enabled || __sysrq_enabled == 1 ||
62                                                 (__sysrq_enabled & mask);
63 }
64
65 static 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
75
76 static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
77 {
78         int i;
79         i = key - '0';
80         console_loglevel = 7;
81         printk("Loglevel set to %d\n", i);
82         console_loglevel = i;
83 }
84 static struct sysrq_key_op sysrq_loglevel_op = {
85         .handler        = sysrq_handle_loglevel,
86         .help_msg       = "loglevel(0-9)",
87         .action_msg     = "Changing Loglevel",
88         .enable_mask    = SYSRQ_ENABLE_LOG,
89 };
90
91 #ifdef CONFIG_VT
92 static void sysrq_handle_SAK(int key, struct tty_struct *tty)
93 {
94         struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
95         schedule_work(SAK_work);
96 }
97 static 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 };
103 #else
104 #define sysrq_SAK_op (*(struct sysrq_key_op *)0)
105 #endif
106
107 #ifdef CONFIG_VT
108 static void sysrq_handle_unraw(int key, struct tty_struct *tty)
109 {
110         struct kbd_struct *kbd = &kbd_table[fg_console];
111
112         if (kbd)
113                 kbd->kbdmode = default_utf8 ? VC_UNICODE : VC_XLATE;
114 }
115 static struct sysrq_key_op sysrq_unraw_op = {
116         .handler        = sysrq_handle_unraw,
117         .help_msg       = "unRaw",
118         .action_msg     = "Keyboard mode set to system default",
119         .enable_mask    = SYSRQ_ENABLE_KEYBOARD,
120 };
121 #else
122 #define sysrq_unraw_op (*(struct sysrq_key_op *)0)
123 #endif /* CONFIG_VT */
124
125 #ifdef CONFIG_KEXEC
126 static void sysrq_handle_crashdump(int key, struct tty_struct *tty)
127 {
128         crash_kexec(get_irq_regs());
129 }
130 static 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 };
136 #else
137 #define sysrq_crashdump_op (*(struct sysrq_key_op *)0)
138 #endif
139
140 static void sysrq_handle_reboot(int key, struct tty_struct *tty)
141 {
142         lockdep_off();
143         local_irq_enable();
144         emergency_restart();
145 }
146 static 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
153 static void sysrq_handle_sync(int key, struct tty_struct *tty)
154 {
155         emergency_sync();
156 }
157 static 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
164 static void sysrq_handle_show_timers(int key, struct tty_struct *tty)
165 {
166         sysrq_timer_list_show();
167 }
168
169 static struct sysrq_key_op sysrq_show_timers_op = {
170         .handler        = sysrq_handle_show_timers,
171         .help_msg       = "show-all-timers(Q)",
172         .action_msg     = "Show clockevent devices & pending hrtimers (no others)",
173 };
174
175 static void sysrq_handle_mountro(int key, struct tty_struct *tty)
176 {
177         emergency_remount();
178 }
179 static 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
186 #ifdef CONFIG_LOCKDEP
187 static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
188 {
189         debug_show_all_locks();
190 }
191
192 static 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 };
197 #else
198 #define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
199 #endif
200
201 #ifdef CONFIG_SMP
202 static DEFINE_SPINLOCK(show_lock);
203
204 static 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
218 static void sysrq_showregs_othercpus(struct work_struct *dummy)
219 {
220         smp_call_function(showacpu, NULL, 0);
221 }
222
223 static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
224
225 static 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
235 static struct sysrq_key_op sysrq_showallcpus_op = {
236         .handler        = sysrq_handle_showallcpus,
237         .help_msg       = "show-backtrace-all-active-cpus(L)",
238         .action_msg     = "Show backtrace of all active CPUs",
239         .enable_mask    = SYSRQ_ENABLE_DUMP,
240 };
241 #endif
242
243 static void sysrq_handle_showregs(int key, struct tty_struct *tty)
244 {
245         struct pt_regs *regs = get_irq_regs();
246         if (regs)
247                 show_regs(regs);
248         perf_counter_print_debug();
249 }
250 static struct sysrq_key_op sysrq_showregs_op = {
251         .handler        = sysrq_handle_showregs,
252         .help_msg       = "show-registers(P)",
253         .action_msg     = "Show Regs",
254         .enable_mask    = SYSRQ_ENABLE_DUMP,
255 };
256
257 static void sysrq_handle_showstate(int key, struct tty_struct *tty)
258 {
259         show_state();
260 }
261 static struct sysrq_key_op sysrq_showstate_op = {
262         .handler        = sysrq_handle_showstate,
263         .help_msg       = "show-task-states(T)",
264         .action_msg     = "Show State",
265         .enable_mask    = SYSRQ_ENABLE_DUMP,
266 };
267
268 static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty)
269 {
270         show_state_filter(TASK_UNINTERRUPTIBLE);
271 }
272 static struct sysrq_key_op sysrq_showstate_blocked_op = {
273         .handler        = sysrq_handle_showstate_blocked,
274         .help_msg       = "show-blocked-tasks(W)",
275         .action_msg     = "Show Blocked State",
276         .enable_mask    = SYSRQ_ENABLE_DUMP,
277 };
278
279 #ifdef CONFIG_TRACING
280 #include <linux/ftrace.h>
281
282 static void sysrq_ftrace_dump(int key, struct tty_struct *tty)
283 {
284         ftrace_dump();
285 }
286 static struct sysrq_key_op sysrq_ftrace_dump_op = {
287         .handler        = sysrq_ftrace_dump,
288         .help_msg       = "dumpZ-ftrace-buffer",
289         .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
295
296 static void sysrq_handle_showmem(int key, struct tty_struct *tty)
297 {
298         show_mem();
299 }
300 static struct sysrq_key_op sysrq_showmem_op = {
301         .handler        = sysrq_handle_showmem,
302         .help_msg       = "show-memory-usage(M)",
303         .action_msg     = "Show Memory",
304         .enable_mask    = SYSRQ_ENABLE_DUMP,
305 };
306
307 /*
308  * Signal sysrq helper function.  Sends a signal to all user processes.
309  */
310 static void send_sig_all(int sig)
311 {
312         struct task_struct *p;
313
314         for_each_process(p) {
315                 if (p->mm && !is_global_init(p))
316                         /* Not swapper, init nor kernel thread */
317                         force_sig(sig, p);
318         }
319 }
320
321 static void sysrq_handle_term(int key, struct tty_struct *tty)
322 {
323         send_sig_all(SIGTERM);
324         console_loglevel = 8;
325 }
326 static struct sysrq_key_op sysrq_term_op = {
327         .handler        = sysrq_handle_term,
328         .help_msg       = "terminate-all-tasks(E)",
329         .action_msg     = "Terminate All Tasks",
330         .enable_mask    = SYSRQ_ENABLE_SIGNAL,
331 };
332
333 static void moom_callback(struct work_struct *ignored)
334 {
335         out_of_memory(node_zonelist(0, GFP_KERNEL), GFP_KERNEL, 0);
336 }
337
338 static DECLARE_WORK(moom_work, moom_callback);
339
340 static void sysrq_handle_moom(int key, struct tty_struct *tty)
341 {
342         schedule_work(&moom_work);
343 }
344 static struct sysrq_key_op sysrq_moom_op = {
345         .handler        = sysrq_handle_moom,
346         .help_msg       = "memory-full-oom-kill(F)",
347         .action_msg     = "Manual OOM execution",
348         .enable_mask    = SYSRQ_ENABLE_SIGNAL,
349 };
350
351 static void sysrq_handle_kill(int key, struct tty_struct *tty)
352 {
353         send_sig_all(SIGKILL);
354         console_loglevel = 8;
355 }
356 static struct sysrq_key_op sysrq_kill_op = {
357         .handler        = sysrq_handle_kill,
358         .help_msg       = "kill-all-tasks(I)",
359         .action_msg     = "Kill All Tasks",
360         .enable_mask    = SYSRQ_ENABLE_SIGNAL,
361 };
362
363 static void sysrq_handle_unrt(int key, struct tty_struct *tty)
364 {
365         normalize_rt_tasks();
366 }
367 static struct sysrq_key_op sysrq_unrt_op = {
368         .handler        = sysrq_handle_unrt,
369         .help_msg       = "nice-all-RT-tasks(N)",
370         .action_msg     = "Nice All RT Tasks",
371         .enable_mask    = SYSRQ_ENABLE_RTNICE,
372 };
373
374 /* Key Operations table and lock */
375 static DEFINE_SPINLOCK(sysrq_key_table_lock);
376
377 static struct sysrq_key_op *sysrq_key_table[36] = {
378         &sysrq_loglevel_op,             /* 0 */
379         &sysrq_loglevel_op,             /* 1 */
380         &sysrq_loglevel_op,             /* 2 */
381         &sysrq_loglevel_op,             /* 3 */
382         &sysrq_loglevel_op,             /* 4 */
383         &sysrq_loglevel_op,             /* 5 */
384         &sysrq_loglevel_op,             /* 6 */
385         &sysrq_loglevel_op,             /* 7 */
386         &sysrq_loglevel_op,             /* 8 */
387         &sysrq_loglevel_op,             /* 9 */
388
389         /*
390          * a: Don't use for system provided sysrqs, it is handled specially on
391          * sparc and will never arrive.
392          */
393         NULL,                           /* a */
394         &sysrq_reboot_op,               /* b */
395         &sysrq_crashdump_op,            /* c & ibm_emac driver debug */
396         &sysrq_showlocks_op,            /* d */
397         &sysrq_term_op,                 /* e */
398         &sysrq_moom_op,                 /* f */
399         /* g: May be registered by ppc for kgdb */
400         NULL,                           /* g */
401         NULL,                           /* h */
402         &sysrq_kill_op,                 /* i */
403         NULL,                           /* j */
404         &sysrq_SAK_op,                  /* k */
405 #ifdef CONFIG_SMP
406         &sysrq_showallcpus_op,          /* l */
407 #else
408         NULL,                           /* l */
409 #endif
410         &sysrq_showmem_op,              /* m */
411         &sysrq_unrt_op,                 /* n */
412         /* o: This will often be registered as 'Off' at init time */
413         NULL,                           /* o */
414         &sysrq_showregs_op,             /* p */
415         &sysrq_show_timers_op,          /* q */
416         &sysrq_unraw_op,                /* r */
417         &sysrq_sync_op,                 /* s */
418         &sysrq_showstate_op,            /* t */
419         &sysrq_mountro_op,              /* u */
420         /* v: May be registered at init time by SMP VOYAGER */
421         NULL,                           /* v */
422         &sysrq_showstate_blocked_op,    /* w */
423         /* x: May be registered on ppc/powerpc for xmon */
424         NULL,                           /* x */
425         /* y: May be registered on sparc64 for global register dump */
426         NULL,                           /* y */
427         &sysrq_ftrace_dump_op,          /* z */
428 };
429
430 /* key2index calculation, -1 on invalid index */
431 static int sysrq_key_table_key2index(int key)
432 {
433         int retval;
434
435         if ((key >= '0') && (key <= '9'))
436                 retval = key - '0';
437         else if ((key >= 'a') && (key <= 'z'))
438                 retval = key + 10 - 'a';
439         else
440                 retval = -1;
441         return retval;
442 }
443
444 /*
445  * get and put functions for the table, exposed to modules.
446  */
447 struct sysrq_key_op *__sysrq_get_key_op(int key)
448 {
449         struct sysrq_key_op *op_p = NULL;
450         int i;
451
452         i = sysrq_key_table_key2index(key);
453         if (i != -1)
454                 op_p = sysrq_key_table[i];
455         return op_p;
456 }
457
458 static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
459 {
460         int i = sysrq_key_table_key2index(key);
461
462         if (i != -1)
463                 sysrq_key_table[i] = op_p;
464 }
465
466 /*
467  * This is the non-locking version of handle_sysrq.  It must/can only be called
468  * by sysrq key handlers, as they are inside of the lock
469  */
470 void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
471 {
472         struct sysrq_key_op *op_p;
473         int orig_log_level;
474         int i;
475         unsigned long flags;
476
477         spin_lock_irqsave(&sysrq_key_table_lock, flags);
478         orig_log_level = console_loglevel;
479         console_loglevel = 7;
480         printk(KERN_INFO "SysRq : ");
481
482         op_p = __sysrq_get_key_op(key);
483         if (op_p) {
484                 /*
485                  * Should we check for enabled operations (/proc/sysrq-trigger
486                  * should not) and is the invoked operation enabled?
487                  */
488                 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
489                         printk("%s\n", op_p->action_msg);
490                         console_loglevel = orig_log_level;
491                         op_p->handler(key, tty);
492                 } else {
493                         printk("This sysrq operation is disabled.\n");
494                 }
495         } else {
496                 printk("HELP : ");
497                 /* Only print the help msg once per handler */
498                 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
499                         if (sysrq_key_table[i]) {
500                                 int j;
501
502                                 for (j = 0; sysrq_key_table[i] !=
503                                                 sysrq_key_table[j]; j++)
504                                         ;
505                                 if (j != i)
506                                         continue;
507                                 printk("%s ", sysrq_key_table[i]->help_msg);
508                         }
509                 }
510                 printk("\n");
511                 console_loglevel = orig_log_level;
512         }
513         spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
514 }
515
516 /*
517  * This function is called by the keyboard handler when SysRq is pressed
518  * and any other keycode arrives.
519  */
520 void handle_sysrq(int key, struct tty_struct *tty)
521 {
522         if (sysrq_on())
523                 __handle_sysrq(key, tty, 1);
524 }
525 EXPORT_SYMBOL(handle_sysrq);
526
527 static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
528                                 struct sysrq_key_op *remove_op_p)
529 {
530
531         int retval;
532         unsigned long flags;
533
534         spin_lock_irqsave(&sysrq_key_table_lock, flags);
535         if (__sysrq_get_key_op(key) == remove_op_p) {
536                 __sysrq_put_key_op(key, insert_op_p);
537                 retval = 0;
538         } else {
539                 retval = -1;
540         }
541         spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
542         return retval;
543 }
544
545 int register_sysrq_key(int key, struct sysrq_key_op *op_p)
546 {
547         return __sysrq_swap_key_ops(key, op_p, NULL);
548 }
549 EXPORT_SYMBOL(register_sysrq_key);
550
551 int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
552 {
553         return __sysrq_swap_key_ops(key, NULL, op_p);
554 }
555 EXPORT_SYMBOL(unregister_sysrq_key);
556
557 #ifdef CONFIG_PROC_FS
558 /*
559  * writing 'C' to /proc/sysrq-trigger is like sysrq-C
560  */
561 static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
562                                    size_t count, loff_t *ppos)
563 {
564         if (count) {
565                 char c;
566
567                 if (get_user(c, buf))
568                         return -EFAULT;
569                 __handle_sysrq(c, NULL, 0);
570         }
571         return count;
572 }
573
574 static const struct file_operations proc_sysrq_trigger_operations = {
575         .write          = write_sysrq_trigger,
576 };
577
578 static int __init sysrq_init(void)
579 {
580         proc_create("sysrq-trigger", S_IWUSR, NULL, &proc_sysrq_trigger_operations);
581         return 0;
582 }
583 module_init(sysrq_init);
584 #endif