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