162306a36Sopenharmony_ciLinux Magic System Request Key Hacks
262306a36Sopenharmony_ci====================================
362306a36Sopenharmony_ci
462306a36Sopenharmony_ciDocumentation for sysrq.c
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciWhat is the magic SysRq key?
762306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~
862306a36Sopenharmony_ci
962306a36Sopenharmony_ciIt is a 'magical' key combo you can hit which the kernel will respond to
1062306a36Sopenharmony_ciregardless of whatever else it is doing, unless it is completely locked up.
1162306a36Sopenharmony_ci
1262306a36Sopenharmony_ciHow do I enable the magic SysRq key?
1362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ciYou need to say "yes" to 'Magic SysRq key (CONFIG_MAGIC_SYSRQ)' when
1662306a36Sopenharmony_ciconfiguring the kernel. When running a kernel with SysRq compiled in,
1762306a36Sopenharmony_ci/proc/sys/kernel/sysrq controls the functions allowed to be invoked via
1862306a36Sopenharmony_cithe SysRq key. The default value in this file is set by the
1962306a36Sopenharmony_ciCONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE config symbol, which itself defaults
2062306a36Sopenharmony_cito 1. Here is the list of possible values in /proc/sys/kernel/sysrq:
2162306a36Sopenharmony_ci
2262306a36Sopenharmony_ci   -  0 - disable sysrq completely
2362306a36Sopenharmony_ci   -  1 - enable all functions of sysrq
2462306a36Sopenharmony_ci   - >1 - bitmask of allowed sysrq functions (see below for detailed function
2562306a36Sopenharmony_ci     description)::
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci          2 =   0x2 - enable control of console logging level
2862306a36Sopenharmony_ci          4 =   0x4 - enable control of keyboard (SAK, unraw)
2962306a36Sopenharmony_ci          8 =   0x8 - enable debugging dumps of processes etc.
3062306a36Sopenharmony_ci         16 =  0x10 - enable sync command
3162306a36Sopenharmony_ci         32 =  0x20 - enable remount read-only
3262306a36Sopenharmony_ci         64 =  0x40 - enable signalling of processes (term, kill, oom-kill)
3362306a36Sopenharmony_ci        128 =  0x80 - allow reboot/poweroff
3462306a36Sopenharmony_ci        256 = 0x100 - allow nicing of all RT tasks
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ciYou can set the value in the file by the following command::
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_ci    echo "number" >/proc/sys/kernel/sysrq
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ciThe number may be written here either as decimal or as hexadecimal
4162306a36Sopenharmony_ciwith the 0x prefix. CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE must always be
4262306a36Sopenharmony_ciwritten in hexadecimal.
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ciNote that the value of ``/proc/sys/kernel/sysrq`` influences only the invocation
4562306a36Sopenharmony_civia a keyboard. Invocation of any operation via ``/proc/sysrq-trigger`` is
4662306a36Sopenharmony_cialways allowed (by a user with admin privileges).
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_ciHow do I use the magic SysRq key?
4962306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ciOn x86
5262306a36Sopenharmony_ci	You press the key combo :kbd:`ALT-SysRq-<command key>`.
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci	.. note::
5562306a36Sopenharmony_ci	   Some
5662306a36Sopenharmony_ci           keyboards may not have a key labeled 'SysRq'. The 'SysRq' key is
5762306a36Sopenharmony_ci           also known as the 'Print Screen' key. Also some keyboards cannot
5862306a36Sopenharmony_ci	   handle so many keys being pressed at the same time, so you might
5962306a36Sopenharmony_ci	   have better luck with press :kbd:`Alt`, press :kbd:`SysRq`,
6062306a36Sopenharmony_ci	   release :kbd:`SysRq`, press :kbd:`<command key>`, release everything.
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ciOn SPARC
6362306a36Sopenharmony_ci	You press :kbd:`ALT-STOP-<command key>`, I believe.
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ciOn the serial console (PC style standard serial ports only)
6662306a36Sopenharmony_ci        You send a ``BREAK``, then within 5 seconds a command key. Sending
6762306a36Sopenharmony_ci        ``BREAK`` twice is interpreted as a normal BREAK.
6862306a36Sopenharmony_ci
6962306a36Sopenharmony_ciOn PowerPC
7062306a36Sopenharmony_ci	Press :kbd:`ALT - Print Screen` (or :kbd:`F13`) - :kbd:`<command key>`.
7162306a36Sopenharmony_ci        :kbd:`Print Screen` (or :kbd:`F13`) - :kbd:`<command key>` may suffice.
7262306a36Sopenharmony_ci
7362306a36Sopenharmony_ciOn other
7462306a36Sopenharmony_ci	If you know of the key combos for other architectures, please
7562306a36Sopenharmony_ci	submit a patch to be included in this section.
7662306a36Sopenharmony_ci
7762306a36Sopenharmony_ciOn all
7862306a36Sopenharmony_ci	Write a character to /proc/sysrq-trigger.  e.g.::
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci		echo t > /proc/sysrq-trigger
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ciThe :kbd:`<command key>` is case sensitive.
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ciWhat are the 'command' keys?
8562306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8662306a36Sopenharmony_ci
8762306a36Sopenharmony_ci=========== ===================================================================
8862306a36Sopenharmony_ciCommand	    Function
8962306a36Sopenharmony_ci=========== ===================================================================
9062306a36Sopenharmony_ci``b``	    Will immediately reboot the system without syncing or unmounting
9162306a36Sopenharmony_ci            your disks.
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci``c``	    Will perform a system crash and a crashdump will be taken
9462306a36Sopenharmony_ci            if configured.
9562306a36Sopenharmony_ci
9662306a36Sopenharmony_ci``d``	    Shows all locks that are held.
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci``e``	    Send a SIGTERM to all processes, except for init.
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci``f``	    Will call the oom killer to kill a memory hog process, but do not
10162306a36Sopenharmony_ci	    panic if nothing can be killed.
10262306a36Sopenharmony_ci
10362306a36Sopenharmony_ci``g``	    Used by kgdb (kernel debugger)
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci``h``	    Will display help (actually any other key than those listed
10662306a36Sopenharmony_ci            here will display help. but ``h`` is easy to remember :-)
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_ci``i``	    Send a SIGKILL to all processes, except for init.
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci``j``	    Forcibly "Just thaw it" - filesystems frozen by the FIFREEZE ioctl.
11162306a36Sopenharmony_ci
11262306a36Sopenharmony_ci``k``	    Secure Access Key (SAK) Kills all programs on the current virtual
11362306a36Sopenharmony_ci            console. NOTE: See important comments below in SAK section.
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci``l``	    Shows a stack backtrace for all active CPUs.
11662306a36Sopenharmony_ci
11762306a36Sopenharmony_ci``m``	    Will dump current memory info to your console.
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci``n``	    Used to make RT tasks nice-able
12062306a36Sopenharmony_ci
12162306a36Sopenharmony_ci``o``	    Will shut your system off (if configured and supported).
12262306a36Sopenharmony_ci
12362306a36Sopenharmony_ci``p``	    Will dump the current registers and flags to your console.
12462306a36Sopenharmony_ci
12562306a36Sopenharmony_ci``q``	    Will dump per CPU lists of all armed hrtimers (but NOT regular
12662306a36Sopenharmony_ci            timer_list timers) and detailed information about all
12762306a36Sopenharmony_ci            clockevent devices.
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_ci``r``	    Turns off keyboard raw mode and sets it to XLATE.
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci``s``	    Will attempt to sync all mounted filesystems.
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci``t``	    Will dump a list of current tasks and their information to your
13462306a36Sopenharmony_ci            console.
13562306a36Sopenharmony_ci
13662306a36Sopenharmony_ci``u``	    Will attempt to remount all mounted filesystems read-only.
13762306a36Sopenharmony_ci
13862306a36Sopenharmony_ci``v``	    Forcefully restores framebuffer console
13962306a36Sopenharmony_ci``v``	    Causes ETM buffer dump [ARM-specific]
14062306a36Sopenharmony_ci
14162306a36Sopenharmony_ci``w``	    Dumps tasks that are in uninterruptible (blocked) state.
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci``x``	    Used by xmon interface on ppc/powerpc platforms.
14462306a36Sopenharmony_ci            Show global PMU Registers on sparc64.
14562306a36Sopenharmony_ci            Dump all TLB entries on MIPS.
14662306a36Sopenharmony_ci
14762306a36Sopenharmony_ci``y``	    Show global CPU Registers [SPARC-64 specific]
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_ci``z``	    Dump the ftrace buffer
15062306a36Sopenharmony_ci
15162306a36Sopenharmony_ci``0``-``9`` Sets the console log level, controlling which kernel messages
15262306a36Sopenharmony_ci            will be printed to your console. (``0``, for example would make
15362306a36Sopenharmony_ci            it so that only emergency messages like PANICs or OOPSes would
15462306a36Sopenharmony_ci            make it to your console.)
15562306a36Sopenharmony_ci=========== ===================================================================
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ciOkay, so what can I use them for?
15862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ciWell, unraw(r) is very handy when your X server or a svgalib program crashes.
16162306a36Sopenharmony_ci
16262306a36Sopenharmony_cisak(k) (Secure Access Key) is useful when you want to be sure there is no
16362306a36Sopenharmony_citrojan program running at console which could grab your password
16462306a36Sopenharmony_ciwhen you would try to login. It will kill all programs on given console,
16562306a36Sopenharmony_cithus letting you make sure that the login prompt you see is actually
16662306a36Sopenharmony_cithe one from init, not some trojan program.
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci.. important::
16962306a36Sopenharmony_ci
17062306a36Sopenharmony_ci   In its true form it is not a true SAK like the one in a
17162306a36Sopenharmony_ci   c2 compliant system, and it should not be mistaken as
17262306a36Sopenharmony_ci   such.
17362306a36Sopenharmony_ci
17462306a36Sopenharmony_ciIt seems others find it useful as (System Attention Key) which is
17562306a36Sopenharmony_ciuseful when you want to exit a program that will not let you switch consoles.
17662306a36Sopenharmony_ci(For example, X or a svgalib program.)
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci``reboot(b)`` is good when you're unable to shut down, it is an equivalent
17962306a36Sopenharmony_ciof pressing the "reset" button.
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci``crash(c)`` can be used to manually trigger a crashdump when the system is hung.
18262306a36Sopenharmony_ciNote that this just triggers a crash if there is no dump mechanism available.
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci``sync(s)`` is handy before yanking removable medium or after using a rescue
18562306a36Sopenharmony_cishell that provides no graceful shutdown -- it will ensure your data is
18662306a36Sopenharmony_cisafely written to the disk. Note that the sync hasn't taken place until you see
18762306a36Sopenharmony_cithe "OK" and "Done" appear on the screen.
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci``umount(u)`` can be used to mark filesystems as properly unmounted. From the
19062306a36Sopenharmony_cirunning system's point of view, they will be remounted read-only. The remount
19162306a36Sopenharmony_ciisn't complete until you see the "OK" and "Done" message appear on the screen.
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_ciThe loglevels ``0``-``9`` are useful when your console is being flooded with
19462306a36Sopenharmony_cikernel messages you do not want to see. Selecting ``0`` will prevent all but
19562306a36Sopenharmony_cithe most urgent kernel messages from reaching your console. (They will
19662306a36Sopenharmony_cistill be logged if syslogd/klogd are alive, though.)
19762306a36Sopenharmony_ci
19862306a36Sopenharmony_ci``term(e)`` and ``kill(i)`` are useful if you have some sort of runaway process
19962306a36Sopenharmony_ciyou are unable to kill any other way, especially if it's spawning other
20062306a36Sopenharmony_ciprocesses.
20162306a36Sopenharmony_ci
20262306a36Sopenharmony_ci"just thaw ``it(j)``" is useful if your system becomes unresponsive due to a
20362306a36Sopenharmony_cifrozen (probably root) filesystem via the FIFREEZE ioctl.
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ciSometimes SysRq seems to get 'stuck' after using it, what can I do?
20662306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ciWhen this happens, try tapping shift, alt and control on both sides of the
20962306a36Sopenharmony_cikeyboard, and hitting an invalid sysrq sequence again. (i.e., something like
21062306a36Sopenharmony_ci:kbd:`alt-sysrq-z`).
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_ciSwitching to another virtual console (:kbd:`ALT+Fn`) and then back again
21362306a36Sopenharmony_cishould also help.
21462306a36Sopenharmony_ci
21562306a36Sopenharmony_ciI hit SysRq, but nothing seems to happen, what's wrong?
21662306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ciThere are some keyboards that produce a different keycode for SysRq than the
21962306a36Sopenharmony_cipre-defined value of 99
22062306a36Sopenharmony_ci(see ``KEY_SYSRQ`` in ``include/uapi/linux/input-event-codes.h``), or
22162306a36Sopenharmony_ciwhich don't have a SysRq key at all. In these cases, run ``showkey -s`` to find
22262306a36Sopenharmony_cian appropriate scancode sequence, and use ``setkeycodes <sequence> 99`` to map
22362306a36Sopenharmony_cithis sequence to the usual SysRq code (e.g., ``setkeycodes e05b 99``). It's
22462306a36Sopenharmony_ciprobably best to put this command in a boot script. Oh, and by the way, you
22562306a36Sopenharmony_ciexit ``showkey`` by not typing anything for ten seconds.
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ciI want to add SysRQ key events to a module, how does it work?
22862306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ciIn order to register a basic function with the table, you must first include
23162306a36Sopenharmony_cithe header ``include/linux/sysrq.h``, this will define everything else you need.
23262306a36Sopenharmony_ciNext, you must create a ``sysrq_key_op`` struct, and populate it with A) the key
23362306a36Sopenharmony_cihandler function you will use, B) a help_msg string, that will print when SysRQ
23462306a36Sopenharmony_ciprints help, and C) an action_msg string, that will print right before your
23562306a36Sopenharmony_cihandler is called. Your handler must conform to the prototype in 'sysrq.h'.
23662306a36Sopenharmony_ci
23762306a36Sopenharmony_ciAfter the ``sysrq_key_op`` is created, you can call the kernel function
23862306a36Sopenharmony_ci``register_sysrq_key(int key, const struct sysrq_key_op *op_p);`` this will
23962306a36Sopenharmony_ciregister the operation pointed to by ``op_p`` at table key 'key',
24062306a36Sopenharmony_ciif that slot in the table is blank. At module unload time, you must call
24162306a36Sopenharmony_cithe function ``unregister_sysrq_key(int key, const struct sysrq_key_op *op_p)``,
24262306a36Sopenharmony_ciwhich will remove the key op pointed to by 'op_p' from the key 'key', if and
24362306a36Sopenharmony_cionly if it is currently registered in that slot. This is in case the slot has
24462306a36Sopenharmony_cibeen overwritten since you registered it.
24562306a36Sopenharmony_ci
24662306a36Sopenharmony_ciThe Magic SysRQ system works by registering key operations against a key op
24762306a36Sopenharmony_cilookup table, which is defined in 'drivers/tty/sysrq.c'. This key table has
24862306a36Sopenharmony_cia number of operations registered into it at compile time, but is mutable,
24962306a36Sopenharmony_ciand 2 functions are exported for interface to it::
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci	register_sysrq_key and unregister_sysrq_key.
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ciOf course, never ever leave an invalid pointer in the table. I.e., when
25462306a36Sopenharmony_ciyour module that called register_sysrq_key() exits, it must call
25562306a36Sopenharmony_ciunregister_sysrq_key() to clean up the sysrq key table entry that it used.
25662306a36Sopenharmony_ciNull pointers in the table are always safe. :)
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_ciIf for some reason you feel the need to call the handle_sysrq function from
25962306a36Sopenharmony_ciwithin a function called by handle_sysrq, you must be aware that you are in
26062306a36Sopenharmony_cia lock (you are also in an interrupt handler, which means don't sleep!), so
26162306a36Sopenharmony_ciyou must call ``__handle_sysrq_nolock`` instead.
26262306a36Sopenharmony_ci
26362306a36Sopenharmony_ciWhen I hit a SysRq key combination only the header appears on the console?
26462306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ciSysrq output is subject to the same console loglevel control as all
26762306a36Sopenharmony_ciother console output.  This means that if the kernel was booted 'quiet'
26862306a36Sopenharmony_cias is common on distro kernels the output may not appear on the actual
26962306a36Sopenharmony_ciconsole, even though it will appear in the dmesg buffer, and be accessible
27062306a36Sopenharmony_civia the dmesg command and to the consumers of ``/proc/kmsg``.  As a specific
27162306a36Sopenharmony_ciexception the header line from the sysrq command is passed to all console
27262306a36Sopenharmony_ciconsumers as if the current loglevel was maximum.  If only the header
27362306a36Sopenharmony_ciis emitted it is almost certain that the kernel loglevel is too low.
27462306a36Sopenharmony_ciShould you require the output on the console channel then you will need
27562306a36Sopenharmony_cito temporarily up the console loglevel using :kbd:`alt-sysrq-8` or::
27662306a36Sopenharmony_ci
27762306a36Sopenharmony_ci    echo 8 > /proc/sysrq-trigger
27862306a36Sopenharmony_ci
27962306a36Sopenharmony_ciRemember to return the loglevel to normal after triggering the sysrq
28062306a36Sopenharmony_cicommand you are interested in.
28162306a36Sopenharmony_ci
28262306a36Sopenharmony_ciI have more questions, who can I ask?
28362306a36Sopenharmony_ci~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28462306a36Sopenharmony_ci
28562306a36Sopenharmony_ciJust ask them on the linux-kernel mailing list:
28662306a36Sopenharmony_ci	linux-kernel@vger.kernel.org
28762306a36Sopenharmony_ci
28862306a36Sopenharmony_ciCredits
28962306a36Sopenharmony_ci~~~~~~~
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_ci- Written by Mydraal <vulpyne@vulpyne.net>
29262306a36Sopenharmony_ci- Updated by Adam Sulmicki <adam@cfar.umd.edu>
29362306a36Sopenharmony_ci- Updated by Jeremy M. Dolan <jmd@turbogeek.org> 2001/01/28 10:15:59
29462306a36Sopenharmony_ci- Added to by Crutcher Dunnavant <crutcher+kernel@datastacks.com>
295