3 * $Id: sysrq.c,v 1.15 1998/08/23 14:56:41 mj Exp $
5 * Linux Magic System Request Key Hacks
7 * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
8 * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
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
15 #include <linux/config.h>
16 #include <linux/sched.h>
17 #include <linux/interrupt.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() */
35 #include <linux/spinlock.h>
37 #include <asm/ptrace.h>
39 extern void reset_vc(unsigned int);
41 /* Whether we react on sysrq keys or just ignore them */
42 int sysrq_enabled = 1;
44 /* Machine specific power off function */
45 void (*sysrq_power_off)(void);
47 /* Loglevel sysrq handler */
48 static void sysrq_handle_loglevel(int key, struct pt_regs *pt_regs,
49 struct tty_struct *tty)
54 printk("Loglevel set to %d\n", i);
57 static struct sysrq_key_op sysrq_loglevel_op = {
58 .handler = sysrq_handle_loglevel,
59 .help_msg = "loglevel0-8",
60 .action_msg = "Changing Loglevel",
64 /* SAK sysrq handler */
66 static void sysrq_handle_SAK(int key, struct pt_regs *pt_regs,
67 struct tty_struct *tty)
73 static struct sysrq_key_op sysrq_SAK_op = {
74 .handler = sysrq_handle_SAK,
81 /* unraw sysrq handler */
82 static void sysrq_handle_unraw(int key, struct pt_regs *pt_regs,
83 struct tty_struct *tty)
85 struct kbd_struct *kbd = &kbd_table[fg_console];
88 kbd->kbdmode = VC_XLATE;
90 static struct sysrq_key_op sysrq_unraw_op = {
91 .handler = sysrq_handle_unraw,
93 .action_msg = "Keyboard mode set to XLATE",
95 #endif /* CONFIG_VT */
97 /* reboot sysrq handler */
98 static void sysrq_handle_reboot(int key, struct pt_regs *pt_regs,
99 struct tty_struct *tty)
101 machine_restart(NULL);
104 static struct sysrq_key_op sysrq_reboot_op = {
105 .handler = sysrq_handle_reboot,
106 .help_msg = "reBoot",
107 .action_msg = "Resetting",
110 /* crash sysrq handler */
111 static void sysrq_handle_crash(int key, struct pt_regs *pt_regs,
112 struct tty_struct *tty) {
115 static struct sysrq_key_op sysrq_crash_op = {
116 .handler = sysrq_handle_crash,
118 .action_msg = "Crashing the kernel by request",
121 static void sysrq_handle_sync(int key, struct pt_regs *pt_regs,
122 struct tty_struct *tty)
127 static struct sysrq_key_op sysrq_sync_op = {
128 .handler = sysrq_handle_sync,
130 .action_msg = "Emergency Sync",
133 static void sysrq_handle_mountro(int key, struct pt_regs *pt_regs,
134 struct tty_struct *tty)
139 static struct sysrq_key_op sysrq_mountro_op = {
140 .handler = sysrq_handle_mountro,
141 .help_msg = "Unmount",
142 .action_msg = "Emergency Remount R/O",
145 /* END SYNC SYSRQ HANDLERS BLOCK */
148 /* SHOW SYSRQ HANDLERS BLOCK */
150 static void sysrq_handle_showregs(int key, struct pt_regs *pt_regs,
151 struct tty_struct *tty)
156 static struct sysrq_key_op sysrq_showregs_op = {
157 .handler = sysrq_handle_showregs,
158 .help_msg = "showPc",
159 .action_msg = "Show Regs",
163 static void sysrq_handle_showstate(int key, struct pt_regs *pt_regs,
164 struct tty_struct *tty)
168 static struct sysrq_key_op sysrq_showstate_op = {
169 .handler = sysrq_handle_showstate,
170 .help_msg = "showTasks",
171 .action_msg = "Show State",
175 static void sysrq_handle_showmem(int key, struct pt_regs *pt_regs,
176 struct tty_struct *tty)
180 static struct sysrq_key_op sysrq_showmem_op = {
181 .handler = sysrq_handle_showmem,
182 .help_msg = "showMem",
183 .action_msg = "Show Memory",
186 /* SHOW SYSRQ HANDLERS BLOCK */
189 /* SIGNAL SYSRQ HANDLERS BLOCK */
191 /* signal sysrq helper function
192 * Sends a signal to all user processes */
193 static void send_sig_all(int sig)
195 struct task_struct *p;
197 for_each_process(p) {
198 if (p->mm && p->pid != 1)
199 /* Not swapper, init nor kernel thread */
204 static void sysrq_handle_term(int key, struct pt_regs *pt_regs,
205 struct tty_struct *tty)
207 send_sig_all(SIGTERM);
208 console_loglevel = 8;
210 static struct sysrq_key_op sysrq_term_op = {
211 .handler = sysrq_handle_term,
213 .action_msg = "Terminate All Tasks",
216 static void sysrq_handle_kill(int key, struct pt_regs *pt_regs,
217 struct tty_struct *tty)
219 send_sig_all(SIGKILL);
220 console_loglevel = 8;
222 static struct sysrq_key_op sysrq_kill_op = {
223 .handler = sysrq_handle_kill,
225 .action_msg = "Kill All Tasks",
228 /* END SIGNAL SYSRQ HANDLERS BLOCK */
230 static void sysrq_handle_unrt(int key, struct pt_regs *pt_regs,
231 struct tty_struct *tty)
233 normalize_rt_tasks();
235 static struct sysrq_key_op sysrq_unrt_op = {
236 .handler = sysrq_handle_unrt,
238 .action_msg = "Nice All RT Tasks"
241 /* Key Operations table and lock */
242 static spinlock_t sysrq_key_table_lock = SPIN_LOCK_UNLOCKED;
243 #define SYSRQ_KEY_TABLE_LENGTH 36
244 static struct sysrq_key_op *sysrq_key_table[SYSRQ_KEY_TABLE_LENGTH] = {
245 /* 0 */ &sysrq_loglevel_op,
246 /* 1 */ &sysrq_loglevel_op,
247 /* 2 */ &sysrq_loglevel_op,
248 /* 3 */ &sysrq_loglevel_op,
249 /* 4 */ &sysrq_loglevel_op,
250 /* 5 */ &sysrq_loglevel_op,
251 /* 6 */ &sysrq_loglevel_op,
252 /* 7 */ &sysrq_loglevel_op,
253 /* 8 */ &sysrq_loglevel_op,
254 /* 9 */ &sysrq_loglevel_op,
255 /* a */ NULL, /* Don't use for system provided sysrqs,
256 it is handled specially on the sparc
257 and will never arrive */
258 /* b */ &sysrq_reboot_op,
259 /* c */ &sysrq_crash_op,
261 /* e */ &sysrq_term_op,
265 /* i */ &sysrq_kill_op,
268 /* k */ &sysrq_SAK_op,
273 /* m */ &sysrq_showmem_op,
274 /* n */ &sysrq_unrt_op,
275 /* o */ NULL, /* This will often be registered
276 as 'Off' at init time */
277 /* p */ &sysrq_showregs_op,
280 /* r */ &sysrq_unraw_op,
284 /* s */ &sysrq_sync_op,
285 /* t */ &sysrq_showstate_op,
286 /* u */ &sysrq_mountro_op,
287 /* v */ NULL, /* May be assigned at init time by SMP VOYAGER */
294 /* key2index calculation, -1 on invalid index */
295 static int sysrq_key_table_key2index(int key) {
297 if ((key >= '0') && (key <= '9')) {
299 } else if ((key >= 'a') && (key <= 'z')) {
300 retval = key + 10 - 'a';
308 * get and put functions for the table, exposed to modules.
311 struct sysrq_key_op *__sysrq_get_key_op (int key) {
312 struct sysrq_key_op *op_p;
315 i = sysrq_key_table_key2index(key);
316 op_p = (i == -1) ? NULL : sysrq_key_table[i];
320 void __sysrq_put_key_op (int key, struct sysrq_key_op *op_p) {
323 i = sysrq_key_table_key2index(key);
325 sysrq_key_table[i] = op_p;
329 * This is the non-locking version of handle_sysrq
330 * It must/can only be called by sysrq key handlers,
331 * as they are inside of the lock
334 void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
336 struct sysrq_key_op *op_p;
341 spin_lock_irqsave(&sysrq_key_table_lock, flags);
342 orig_log_level = console_loglevel;
343 console_loglevel = 7;
344 printk(KERN_INFO "SysRq : ");
346 op_p = __sysrq_get_key_op(key);
348 printk ("%s\n", op_p->action_msg);
349 console_loglevel = orig_log_level;
350 op_p->handler(key, pt_regs, tty);
353 /* Only print the help msg once per handler */
354 for (i=0; i<SYSRQ_KEY_TABLE_LENGTH; i++)
355 if (sysrq_key_table[i]) {
356 for (j=0; sysrq_key_table[i] != sysrq_key_table[j]; j++);
358 printk ("%s ", sysrq_key_table[i]->help_msg);
361 console_loglevel = orig_log_level;
363 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
367 * This function is called by the keyboard handler when SysRq is pressed
368 * and any other keycode arrives.
371 void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
375 __handle_sysrq(key, pt_regs, tty);
378 int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
379 struct sysrq_key_op *remove_op_p) {
384 spin_lock_irqsave(&sysrq_key_table_lock, flags);
385 if (__sysrq_get_key_op(key) == remove_op_p) {
386 __sysrq_put_key_op(key, insert_op_p);
391 spin_unlock_irqrestore(&sysrq_key_table_lock, flags);
396 int register_sysrq_key(int key, struct sysrq_key_op *op_p)
398 return __sysrq_swap_key_ops(key, op_p, NULL);
401 int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
403 return __sysrq_swap_key_ops(key, NULL, op_p);
406 EXPORT_SYMBOL(handle_sysrq);
407 EXPORT_SYMBOL(register_sysrq_key);
408 EXPORT_SYMBOL(unregister_sysrq_key);