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