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 */
231 /* Key Operations table and lock */
232 static spinlock_t sysrq_key_table_lock = SPIN_LOCK_UNLOCKED;
233 #define SYSRQ_KEY_TABLE_LENGTH 36
234 static struct sysrq_key_op *sysrq_key_table[SYSRQ_KEY_TABLE_LENGTH] = {
235 /* 0 */ &sysrq_loglevel_op,
236 /* 1 */ &sysrq_loglevel_op,
237 /* 2 */ &sysrq_loglevel_op,
238 /* 3 */ &sysrq_loglevel_op,
239 /* 4 */ &sysrq_loglevel_op,
240 /* 5 */ &sysrq_loglevel_op,
241 /* 6 */ &sysrq_loglevel_op,
242 /* 7 */ &sysrq_loglevel_op,
243 /* 8 */ &sysrq_loglevel_op,
244 /* 9 */ &sysrq_loglevel_op,
245 /* a */ NULL, /* Don't use for system provided sysrqs,
246 it is handled specially on the sparc
247 and will never arrive */
248 /* b */ &sysrq_reboot_op,
249 /* c */ &sysrq_crash_op,
251 /* e */ &sysrq_term_op,
255 /* i */ &sysrq_kill_op,
258 /* k */ &sysrq_SAK_op,
263 /* m */ &sysrq_showmem_op,
265 /* o */ NULL, /* This will often be registered
266 as 'Off' at init time */
267 /* p */ &sysrq_showregs_op,
270 /* r */ &sysrq_unraw_op,
274 /* s */ &sysrq_sync_op,
275 /* t */ &sysrq_showstate_op,
276 /* u */ &sysrq_mountro_op,
277 /* v */ NULL, /* May be assigned at init time by SMP VOYAGER */
284 /* key2index calculation, -1 on invalid index */
285 static int sysrq_key_table_key2index(int key) {
287 if ((key >= '0') && (key <= '9')) {
289 } else if ((key >= 'a') && (key <= 'z')) {
290 retval = key + 10 - 'a';
298 * table lock and unlocking functions, exposed to modules
301 void __sysrq_lock_table (void) { spin_lock(&sysrq_key_table_lock); }
303 void __sysrq_unlock_table (void) { spin_unlock(&sysrq_key_table_lock); }
306 * get and put functions for the table, exposed to modules.
309 struct sysrq_key_op *__sysrq_get_key_op (int key) {
310 struct sysrq_key_op *op_p;
313 i = sysrq_key_table_key2index(key);
314 op_p = (i == -1) ? NULL : sysrq_key_table[i];
318 void __sysrq_put_key_op (int key, struct sysrq_key_op *op_p) {
321 i = sysrq_key_table_key2index(key);
323 sysrq_key_table[i] = op_p;
327 * This is the non-locking version of handle_sysrq
328 * It must/can only be called by sysrq key handlers,
329 * as they are inside of the lock
332 void __handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
334 struct sysrq_key_op *op_p;
338 __sysrq_lock_table();
339 orig_log_level = console_loglevel;
340 console_loglevel = 7;
341 printk(KERN_INFO "SysRq : ");
343 op_p = __sysrq_get_key_op(key);
345 printk ("%s\n", op_p->action_msg);
346 console_loglevel = orig_log_level;
347 op_p->handler(key, pt_regs, tty);
350 /* Only print the help msg once per handler */
351 for (i=0; i<SYSRQ_KEY_TABLE_LENGTH; i++)
352 if (sysrq_key_table[i]) {
353 for (j=0; sysrq_key_table[i] != sysrq_key_table[j]; j++);
355 printk ("%s ", sysrq_key_table[i]->help_msg);
358 console_loglevel = orig_log_level;
360 __sysrq_unlock_table();
364 * This function is called by the keyboard handler when SysRq is pressed
365 * and any other keycode arrives.
368 void handle_sysrq(int key, struct pt_regs *pt_regs, struct tty_struct *tty)
372 __handle_sysrq(key, pt_regs, tty);
375 EXPORT_SYMBOL(handle_sysrq);
376 EXPORT_SYMBOL(__sysrq_lock_table);
377 EXPORT_SYMBOL(__sysrq_unlock_table);
378 EXPORT_SYMBOL(__sysrq_get_key_op);
379 EXPORT_SYMBOL(__sysrq_put_key_op);