fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / 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/quotaops.h>
27 #include <linux/smp_lock.h>
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/suspend.h>
31 #include <linux/writeback.h>
32 #include <linux/buffer_head.h>          /* for fsync_bdev() */
33 #include <linux/swap.h>
34 #include <linux/spinlock.h>
35 #include <linux/vt_kern.h>
36 #include <linux/workqueue.h>
37 #include <linux/kexec.h>
38 #include <linux/irq.h>
39 #include <linux/vserver/debug.h>
40
41 #include <asm/ptrace.h>
42 #include <asm/irq_regs.h>
43
44 /* Whether we react on sysrq keys or just ignore them */
45 int __read_mostly __sysrq_enabled = 1;
46
47 static int __read_mostly sysrq_always_enabled;
48
49 int sysrq_on(void)
50 {
51         return __sysrq_enabled || sysrq_always_enabled;
52 }
53
54 /*
55  * A value of 1 means 'all', other nonzero values are an op mask:
56  */
57 static inline int sysrq_on_mask(int mask)
58 {
59         return sysrq_always_enabled || __sysrq_enabled == 1 ||
60                                                 (__sysrq_enabled & mask);
61 }
62
63 static int __init sysrq_always_enabled_setup(char *str)
64 {
65         sysrq_always_enabled = 1;
66         printk(KERN_INFO "debug: sysrq always enabled.\n");
67
68         return 1;
69 }
70
71 __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
72
73
74 static void sysrq_handle_loglevel(int key, struct tty_struct *tty)
75 {
76         int i;
77         i = key - '0';
78         console_loglevel = 7;
79         printk("Loglevel set to %d\n", i);
80         console_loglevel = i;
81 }
82 static struct sysrq_key_op sysrq_loglevel_op = {
83         .handler        = sysrq_handle_loglevel,
84         .help_msg       = "loglevel0-8",
85         .action_msg     = "Changing Loglevel",
86         .enable_mask    = SYSRQ_ENABLE_LOG,
87 };
88
89 #ifdef CONFIG_VT
90 static void sysrq_handle_SAK(int key, struct tty_struct *tty)
91 {
92         if (tty)
93                 do_SAK(tty);
94         reset_vc(vc_cons[fg_console].d);
95 }
96 static struct sysrq_key_op sysrq_SAK_op = {
97         .handler        = sysrq_handle_SAK,
98         .help_msg       = "saK",
99         .action_msg     = "SAK",
100         .enable_mask    = SYSRQ_ENABLE_KEYBOARD,
101 };
102 #else
103 #define sysrq_SAK_op (*(struct sysrq_key_op *)0)
104 #endif
105
106 #ifdef CONFIG_VT
107 static void sysrq_handle_unraw(int key, struct tty_struct *tty)
108 {
109         struct kbd_struct *kbd = &kbd_table[fg_console];
110
111         if (kbd)
112                 kbd->kbdmode = VC_XLATE;
113 }
114 static struct sysrq_key_op sysrq_unraw_op = {
115         .handler        = sysrq_handle_unraw,
116         .help_msg       = "unRaw",
117         .action_msg     = "Keyboard mode set to XLATE",
118         .enable_mask    = SYSRQ_ENABLE_KEYBOARD,
119 };
120 #else
121 #define sysrq_unraw_op (*(struct sysrq_key_op *)0)
122 #endif /* CONFIG_VT */
123
124 static void sysrq_handle_crashdump(int key, struct tty_struct *tty)
125 {
126 #ifdef CONFIG_KEXEC
127         crash_kexec(get_irq_regs());
128         /* can't get here if crash image is loaded */
129         printk("Kexec: Warning: crash image not loaded\n");
130 #endif
131         if (panic_on_oops)
132                 panic("SysRq-triggered panic!\n");
133 }
134 static struct sysrq_key_op sysrq_crashdump_op = {
135         .handler        = sysrq_handle_crashdump,
136         .help_msg       = "Crashdump",
137         .action_msg     = "Trigger a crashdump",
138         .enable_mask    = SYSRQ_ENABLE_DUMP,
139 };
140
141 static void sysrq_handle_reboot(int key, struct tty_struct *tty)
142 {
143         lockdep_off();
144         local_irq_enable();
145         emergency_restart();
146 }
147 static struct sysrq_key_op sysrq_reboot_op = {
148         .handler        = sysrq_handle_reboot,
149         .help_msg       = "reBoot",
150         .action_msg     = "Resetting",
151         .enable_mask    = SYSRQ_ENABLE_BOOT,
152 };
153
154 static void sysrq_handle_sync(int key, struct tty_struct *tty)
155 {
156         emergency_sync();
157 }
158 static struct sysrq_key_op sysrq_sync_op = {
159         .handler        = sysrq_handle_sync,
160         .help_msg       = "Sync",
161         .action_msg     = "Emergency Sync",
162         .enable_mask    = SYSRQ_ENABLE_SYNC,
163 };
164
165 static void sysrq_handle_mountro(int key, struct tty_struct *tty)
166 {
167         emergency_remount();
168 }
169 static struct sysrq_key_op sysrq_mountro_op = {
170         .handler        = sysrq_handle_mountro,
171         .help_msg       = "Unmount",
172         .action_msg     = "Emergency Remount R/O",
173         .enable_mask    = SYSRQ_ENABLE_REMOUNT,
174 };
175
176 #ifdef CONFIG_LOCKDEP
177 static void sysrq_handle_showlocks(int key, struct tty_struct *tty)
178 {
179         debug_show_all_locks();
180 }
181
182 static struct sysrq_key_op sysrq_showlocks_op = {
183         .handler        = sysrq_handle_showlocks,
184         .help_msg       = "show-all-locks(D)",
185         .action_msg     = "Show Locks Held",
186 };
187 #else
188 #define sysrq_showlocks_op (*(struct sysrq_key_op *)0)
189 #endif
190
191 static void sysrq_handle_showregs(int key, struct tty_struct *tty)
192 {
193         struct pt_regs *regs = get_irq_regs();
194         if (regs)
195                 show_regs(regs);
196 }
197 static struct sysrq_key_op sysrq_showregs_op = {
198         .handler        = sysrq_handle_showregs,
199         .help_msg       = "showPc",
200         .action_msg     = "Show Regs",
201         .enable_mask    = SYSRQ_ENABLE_DUMP,
202 };
203
204 static void sysrq_handle_showstate(int key, struct tty_struct *tty)
205 {
206         show_state();
207 }
208 static struct sysrq_key_op sysrq_showstate_op = {
209         .handler        = sysrq_handle_showstate,
210         .help_msg       = "showTasks",
211         .action_msg     = "Show State",
212         .enable_mask    = SYSRQ_ENABLE_DUMP,
213 };
214
215 static void sysrq_handle_showstate_blocked(int key, struct tty_struct *tty)
216 {
217         show_state_filter(TASK_UNINTERRUPTIBLE);
218 }
219 static struct sysrq_key_op sysrq_showstate_blocked_op = {
220         .handler        = sysrq_handle_showstate_blocked,
221         .help_msg       = "shoW-blocked-tasks",
222         .action_msg     = "Show Blocked State",
223         .enable_mask    = SYSRQ_ENABLE_DUMP,
224 };
225
226
227 static void sysrq_handle_showmem(int key, struct tty_struct *tty)
228 {
229         show_mem();
230 }
231 static struct sysrq_key_op sysrq_showmem_op = {
232         .handler        = sysrq_handle_showmem,
233         .help_msg       = "showMem",
234         .action_msg     = "Show Memory",
235         .enable_mask    = SYSRQ_ENABLE_DUMP,
236 };
237
238 /*
239  * Signal sysrq helper function.  Sends a signal to all user processes.
240  */
241 static void send_sig_all(int sig)
242 {
243         struct task_struct *p;
244
245         for_each_process(p) {
246                 if (p->mm && !is_init(p))
247                         /* Not swapper, init nor kernel thread */
248                         force_sig(sig, p);
249         }
250 }
251
252 static void sysrq_handle_term(int key, struct tty_struct *tty)
253 {
254         send_sig_all(SIGTERM);
255         console_loglevel = 8;
256 }
257 static struct sysrq_key_op sysrq_term_op = {
258         .handler        = sysrq_handle_term,
259         .help_msg       = "tErm",
260         .action_msg     = "Terminate All Tasks",
261         .enable_mask    = SYSRQ_ENABLE_SIGNAL,
262 };
263
264 static void moom_callback(struct work_struct *ignored)
265 {
266         out_of_memory(&NODE_DATA(0)->node_zonelists[ZONE_NORMAL],
267                         GFP_KERNEL, 0);
268 }
269
270 static DECLARE_WORK(moom_work, moom_callback);
271
272 static void sysrq_handle_moom(int key, struct tty_struct *tty)
273 {
274         schedule_work(&moom_work);
275 }
276 static struct sysrq_key_op sysrq_moom_op = {
277         .handler        = sysrq_handle_moom,
278         .help_msg       = "Full",
279         .action_msg     = "Manual OOM execution",
280 };
281
282 static void sysrq_handle_kill(int key, struct tty_struct *tty)
283 {
284         send_sig_all(SIGKILL);
285         console_loglevel = 8;
286 }
287 static struct sysrq_key_op sysrq_kill_op = {
288         .handler        = sysrq_handle_kill,
289         .help_msg       = "kIll",
290         .action_msg     = "Kill All Tasks",
291         .enable_mask    = SYSRQ_ENABLE_SIGNAL,
292 };
293
294 static void sysrq_handle_unrt(int key, struct tty_struct *tty)
295 {
296         normalize_rt_tasks();
297 }
298 static struct sysrq_key_op sysrq_unrt_op = {
299         .handler        = sysrq_handle_unrt,
300         .help_msg       = "Nice",
301         .action_msg     = "Nice All RT Tasks",
302         .enable_mask    = SYSRQ_ENABLE_RTNICE,
303 };
304
305
306 #ifdef CONFIG_VSERVER_DEBUG
307 static void sysrq_handle_vxinfo(int key, struct tty_struct *tty)
308 {
309         dump_vx_info_inactive((key == 'x')?0:1);
310 }
311
312 static struct sysrq_key_op sysrq_showvxinfo_op = {
313         .handler        = sysrq_handle_vxinfo,
314         .help_msg       = "conteXt",
315         .action_msg     = "Show Context Info",
316         .enable_mask    = SYSRQ_ENABLE_DUMP,
317 };
318 #endif
319
320 /* Key Operations table and lock */
321 static DEFINE_SPINLOCK(sysrq_key_table_lock);
322
323 static struct sysrq_key_op *sysrq_key_table[36] = {
324         &sysrq_loglevel_op,             /* 0 */
325         &sysrq_loglevel_op,             /* 1 */
326         &sysrq_loglevel_op,             /* 2 */
327         &sysrq_loglevel_op,             /* 3 */
328         &sysrq_loglevel_op,             /* 4 */
329         &sysrq_loglevel_op,             /* 5 */
330         &sysrq_loglevel_op,             /* 6 */
331         &sysrq_loglevel_op,             /* 7 */
332         &sysrq_loglevel_op,             /* 8 */
333         &sysrq_loglevel_op,             /* 9 */
334
335         /*
336          * a: Don't use for system provided sysrqs, it is handled specially on
337          * sparc and will never arrive.
338          */
339         NULL,                           /* a */
340         &sysrq_reboot_op,               /* b */
341         &sysrq_crashdump_op,            /* c & ibm_emac driver debug */
342         &sysrq_showlocks_op,            /* d */
343         &sysrq_term_op,                 /* e */
344         &sysrq_moom_op,                 /* f */
345         /* g: May be registered by ppc for kgdb */
346         NULL,                           /* g */
347         NULL,                           /* h */
348         &sysrq_kill_op,                 /* i */
349         NULL,                           /* j */
350         &sysrq_SAK_op,                  /* k */
351         NULL,                           /* l */
352         &sysrq_showmem_op,              /* m */
353         &sysrq_unrt_op,                 /* n */
354         /* o: This will often be registered as 'Off' at init time */
355         NULL,                           /* o */
356         &sysrq_showregs_op,             /* p */
357         NULL,                           /* q */
358         &sysrq_unraw_op,                /* r */
359         &sysrq_sync_op,                 /* s */
360         &sysrq_showstate_op,            /* t */
361         &sysrq_mountro_op,              /* u */
362         /* v: May be registered at init time by SMP VOYAGER */
363         NULL,                           /* v */
364         &sysrq_showstate_blocked_op,    /* w */
365         /* x: May be registered on ppc/powerpc for xmon */
366         NULL,                           /* x */
367         NULL,                           /* y */
368 #ifdef CONFIG_VSERVER_DEBUG
369         &sysrq_showvxinfo_op,           /* z */
370 #else
371         NULL,                           /* z */
372 #endif
373 };
374
375 /* key2index calculation, -1 on invalid index */
376 static int sysrq_key_table_key2index(int key)
377 {
378         int retval;
379
380         if ((key >= '0') && (key <= '9'))
381                 retval = key - '0';
382         else if ((key >= 'a') && (key <= 'z'))
383                 retval = key + 10 - 'a';
384         else if ((key >= 'A') && (key <= 'Z'))
385                 retval = key + 10 - 'A';
386         else
387                 retval = -1;
388         return retval;
389 }
390
391 /*
392  * get and put functions for the table, exposed to modules.
393  */
394 struct sysrq_key_op *__sysrq_get_key_op(int key)
395 {
396         struct sysrq_key_op *op_p = NULL;
397         int i;
398
399         i = sysrq_key_table_key2index(key);
400         if (i != -1)
401                 op_p = sysrq_key_table[i];
402         return op_p;
403 }
404
405 static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
406 {
407         int i = sysrq_key_table_key2index(key);
408
409         if (i != -1)
410                 sysrq_key_table[i] = op_p;
411 }
412
413 /*
414  * This is the non-locking version of handle_sysrq.  It must/can only be called
415  * by sysrq key handlers, as they are inside of the lock
416  */
417 void __handle_sysrq(int key, struct tty_struct *tty, int check_mask)
418 {
419         struct sysrq_key_op *op_p;
420         int orig_log_level;
421         int i;
422         unsigned long flags;
423
424         spin_lock_irqsave(&sysrq_key_table_lock, flags);
425         orig_log_level = console_loglevel;
426         console_loglevel = 7;
427         printk(KERN_INFO "SysRq : ");
428
429         op_p = __sysrq_get_key_op(key);
430         if (op_p) {
431                 /*
432                  * Should we check for enabled operations (/proc/sysrq-trigger
433                  * should not) and is the invoked operation enabled?
434                  */
435                 if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
436                         printk("%s\n", op_p->action_msg);
437                         console_loglevel = orig_log_level;
438                         op_p->handler(key, tty);
439                 } else {
440                         printk("This sysrq operation is disabled.\n");
441                 }
442         } else {
443                 printk("HELP : ");
444                 /* Only print the help msg once per handler */
445                 for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
446                         if (sysrq_key_table[i]) {
447                                 int j;
448
449                                 for (j = 0; sysrq_key_table[i] !=
450                                                 sysrq_key_table[j]; j++)
451                                         ;
452                                 if (j != i)
453                                         continue;
454                                 printk("%s ", sysrq_key_table[i]->help_msg);
455                         }
456                 }
457                 printk("\n");
458                 console_loglevel = orig_log_level;
459         }
460         spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
461 }
462
463 /*
464  * This function is called by the keyboard handler when SysRq is pressed
465  * and any other keycode arrives.
466  */
467 void handle_sysrq(int key, struct tty_struct *tty)
468 {
469         if (sysrq_on())
470                 __handle_sysrq(key, tty, 1);
471 }
472 EXPORT_SYMBOL(handle_sysrq);
473
474 static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
475                                 struct sysrq_key_op *remove_op_p)
476 {
477
478         int retval;
479         unsigned long flags;
480
481         spin_lock_irqsave(&sysrq_key_table_lock, flags);
482         if (__sysrq_get_key_op(key) == remove_op_p) {
483                 __sysrq_put_key_op(key, insert_op_p);
484                 retval = 0;
485         } else {
486                 retval = -1;
487         }
488         spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
489         return retval;
490 }
491
492 int register_sysrq_key(int key, struct sysrq_key_op *op_p)
493 {
494         return __sysrq_swap_key_ops(key, op_p, NULL);
495 }
496 EXPORT_SYMBOL(register_sysrq_key);
497
498 int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
499 {
500         return __sysrq_swap_key_ops(key, NULL, op_p);
501 }
502 EXPORT_SYMBOL(unregister_sysrq_key);