xref: /OK3568_Linux_fs/kernel/Documentation/admin-guide/sysrq.rst (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593SmuzhiyunLinux Magic System Request Key Hacks
2*4882a593Smuzhiyun====================================
3*4882a593Smuzhiyun
4*4882a593SmuzhiyunDocumentation for sysrq.c
5*4882a593Smuzhiyun
6*4882a593SmuzhiyunWhat is the magic SysRq key?
7*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8*4882a593Smuzhiyun
9*4882a593SmuzhiyunIt is a 'magical' key combo you can hit which the kernel will respond to
10*4882a593Smuzhiyunregardless of whatever else it is doing, unless it is completely locked up.
11*4882a593Smuzhiyun
12*4882a593SmuzhiyunHow do I enable the magic SysRq key?
13*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14*4882a593Smuzhiyun
15*4882a593SmuzhiyunYou need to say "yes" to 'Magic SysRq key (CONFIG_MAGIC_SYSRQ)' when
16*4882a593Smuzhiyunconfiguring the kernel. When running a kernel with SysRq compiled in,
17*4882a593Smuzhiyun/proc/sys/kernel/sysrq controls the functions allowed to be invoked via
18*4882a593Smuzhiyunthe SysRq key. The default value in this file is set by the
19*4882a593SmuzhiyunCONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE config symbol, which itself defaults
20*4882a593Smuzhiyunto 1. Here is the list of possible values in /proc/sys/kernel/sysrq:
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun   -  0 - disable sysrq completely
23*4882a593Smuzhiyun   -  1 - enable all functions of sysrq
24*4882a593Smuzhiyun   - >1 - bitmask of allowed sysrq functions (see below for detailed function
25*4882a593Smuzhiyun     description)::
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun          2 =   0x2 - enable control of console logging level
28*4882a593Smuzhiyun          4 =   0x4 - enable control of keyboard (SAK, unraw)
29*4882a593Smuzhiyun          8 =   0x8 - enable debugging dumps of processes etc.
30*4882a593Smuzhiyun         16 =  0x10 - enable sync command
31*4882a593Smuzhiyun         32 =  0x20 - enable remount read-only
32*4882a593Smuzhiyun         64 =  0x40 - enable signalling of processes (term, kill, oom-kill)
33*4882a593Smuzhiyun        128 =  0x80 - allow reboot/poweroff
34*4882a593Smuzhiyun        256 = 0x100 - allow nicing of all RT tasks
35*4882a593Smuzhiyun
36*4882a593SmuzhiyunYou can set the value in the file by the following command::
37*4882a593Smuzhiyun
38*4882a593Smuzhiyun    echo "number" >/proc/sys/kernel/sysrq
39*4882a593Smuzhiyun
40*4882a593SmuzhiyunThe number may be written here either as decimal or as hexadecimal
41*4882a593Smuzhiyunwith the 0x prefix. CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE must always be
42*4882a593Smuzhiyunwritten in hexadecimal.
43*4882a593Smuzhiyun
44*4882a593SmuzhiyunNote that the value of ``/proc/sys/kernel/sysrq`` influences only the invocation
45*4882a593Smuzhiyunvia a keyboard. Invocation of any operation via ``/proc/sysrq-trigger`` is
46*4882a593Smuzhiyunalways allowed (by a user with admin privileges).
47*4882a593Smuzhiyun
48*4882a593SmuzhiyunHow do I use the magic SysRq key?
49*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
50*4882a593Smuzhiyun
51*4882a593SmuzhiyunOn x86
52*4882a593Smuzhiyun	You press the key combo :kbd:`ALT-SysRq-<command key>`.
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun	.. note::
55*4882a593Smuzhiyun	   Some
56*4882a593Smuzhiyun           keyboards may not have a key labeled 'SysRq'. The 'SysRq' key is
57*4882a593Smuzhiyun           also known as the 'Print Screen' key. Also some keyboards cannot
58*4882a593Smuzhiyun	   handle so many keys being pressed at the same time, so you might
59*4882a593Smuzhiyun	   have better luck with press :kbd:`Alt`, press :kbd:`SysRq`,
60*4882a593Smuzhiyun	   release :kbd:`SysRq`, press :kbd:`<command key>`, release everything.
61*4882a593Smuzhiyun
62*4882a593SmuzhiyunOn SPARC
63*4882a593Smuzhiyun	You press :kbd:`ALT-STOP-<command key>`, I believe.
64*4882a593Smuzhiyun
65*4882a593SmuzhiyunOn the serial console (PC style standard serial ports only)
66*4882a593Smuzhiyun        You send a ``BREAK``, then within 5 seconds a command key. Sending
67*4882a593Smuzhiyun        ``BREAK`` twice is interpreted as a normal BREAK.
68*4882a593Smuzhiyun
69*4882a593SmuzhiyunOn PowerPC
70*4882a593Smuzhiyun	Press :kbd:`ALT - Print Screen` (or :kbd:`F13`) - :kbd:`<command key>`.
71*4882a593Smuzhiyun        :kbd:`Print Screen` (or :kbd:`F13`) - :kbd:`<command key>` may suffice.
72*4882a593Smuzhiyun
73*4882a593SmuzhiyunOn other
74*4882a593Smuzhiyun	If you know of the key combos for other architectures, please
75*4882a593Smuzhiyun        let me know so I can add them to this section.
76*4882a593Smuzhiyun
77*4882a593SmuzhiyunOn all
78*4882a593Smuzhiyun	Write a character to /proc/sysrq-trigger.  e.g.::
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun		echo t > /proc/sysrq-trigger
81*4882a593Smuzhiyun
82*4882a593SmuzhiyunThe :kbd:`<command key>` is case sensitive.
83*4882a593Smuzhiyun
84*4882a593SmuzhiyunWhat are the 'command' keys?
85*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun=========== ===================================================================
88*4882a593SmuzhiyunCommand	    Function
89*4882a593Smuzhiyun=========== ===================================================================
90*4882a593Smuzhiyun``b``	    Will immediately reboot the system without syncing or unmounting
91*4882a593Smuzhiyun            your disks.
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun``c``	    Will perform a system crash by a NULL pointer dereference.
94*4882a593Smuzhiyun            A crashdump will be taken if configured.
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun``d``	    Shows all locks that are held.
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun``e``	    Send a SIGTERM to all processes, except for init.
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun``f``	    Will call the oom killer to kill a memory hog process, but do not
101*4882a593Smuzhiyun	    panic if nothing can be killed.
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun``g``	    Used by kgdb (kernel debugger)
104*4882a593Smuzhiyun
105*4882a593Smuzhiyun``h``	    Will display help (actually any other key than those listed
106*4882a593Smuzhiyun            here will display help. but ``h`` is easy to remember :-)
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun``i``	    Send a SIGKILL to all processes, except for init.
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun``j``	    Forcibly "Just thaw it" - filesystems frozen by the FIFREEZE ioctl.
111*4882a593Smuzhiyun
112*4882a593Smuzhiyun``k``	    Secure Access Key (SAK) Kills all programs on the current virtual
113*4882a593Smuzhiyun            console. NOTE: See important comments below in SAK section.
114*4882a593Smuzhiyun
115*4882a593Smuzhiyun``l``	    Shows a stack backtrace for all active CPUs.
116*4882a593Smuzhiyun
117*4882a593Smuzhiyun``m``	    Will dump current memory info to your console.
118*4882a593Smuzhiyun
119*4882a593Smuzhiyun``n``	    Used to make RT tasks nice-able
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun``o``	    Will shut your system off (if configured and supported).
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun``p``	    Will dump the current registers and flags to your console.
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun``q``	    Will dump per CPU lists of all armed hrtimers (but NOT regular
126*4882a593Smuzhiyun            timer_list timers) and detailed information about all
127*4882a593Smuzhiyun            clockevent devices.
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun``r``	    Turns off keyboard raw mode and sets it to XLATE.
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun``s``	    Will attempt to sync all mounted filesystems.
132*4882a593Smuzhiyun
133*4882a593Smuzhiyun``t``	    Will dump a list of current tasks and their information to your
134*4882a593Smuzhiyun            console.
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun``u``	    Will attempt to remount all mounted filesystems read-only.
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun``v``	    Forcefully restores framebuffer console
139*4882a593Smuzhiyun``v``	    Causes ETM buffer dump [ARM-specific]
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun``w``	    Dumps tasks that are in uninterruptable (blocked) state.
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun``x``	    Used by xmon interface on ppc/powerpc platforms.
144*4882a593Smuzhiyun            Show global PMU Registers on sparc64.
145*4882a593Smuzhiyun            Dump all TLB entries on MIPS.
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun``y``	    Show global CPU Registers [SPARC-64 specific]
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun``z``	    Dump the ftrace buffer
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun``0``-``9`` Sets the console log level, controlling which kernel messages
152*4882a593Smuzhiyun            will be printed to your console. (``0``, for example would make
153*4882a593Smuzhiyun            it so that only emergency messages like PANICs or OOPSes would
154*4882a593Smuzhiyun            make it to your console.)
155*4882a593Smuzhiyun=========== ===================================================================
156*4882a593Smuzhiyun
157*4882a593SmuzhiyunOkay, so what can I use them for?
158*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
159*4882a593Smuzhiyun
160*4882a593SmuzhiyunWell, unraw(r) is very handy when your X server or a svgalib program crashes.
161*4882a593Smuzhiyun
162*4882a593Smuzhiyunsak(k) (Secure Access Key) is useful when you want to be sure there is no
163*4882a593Smuzhiyuntrojan program running at console which could grab your password
164*4882a593Smuzhiyunwhen you would try to login. It will kill all programs on given console,
165*4882a593Smuzhiyunthus letting you make sure that the login prompt you see is actually
166*4882a593Smuzhiyunthe one from init, not some trojan program.
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun.. important::
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun   In its true form it is not a true SAK like the one in a
171*4882a593Smuzhiyun   c2 compliant system, and it should not be mistaken as
172*4882a593Smuzhiyun   such.
173*4882a593Smuzhiyun
174*4882a593SmuzhiyunIt seems others find it useful as (System Attention Key) which is
175*4882a593Smuzhiyunuseful when you want to exit a program that will not let you switch consoles.
176*4882a593Smuzhiyun(For example, X or a svgalib program.)
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun``reboot(b)`` is good when you're unable to shut down, it is an equivalent
179*4882a593Smuzhiyunof pressing the "reset" button.
180*4882a593Smuzhiyun
181*4882a593Smuzhiyun``crash(c)`` can be used to manually trigger a crashdump when the system is hung.
182*4882a593SmuzhiyunNote that this just triggers a crash if there is no dump mechanism available.
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun``sync(s)`` is handy before yanking removable medium or after using a rescue
185*4882a593Smuzhiyunshell that provides no graceful shutdown -- it will ensure your data is
186*4882a593Smuzhiyunsafely written to the disk. Note that the sync hasn't taken place until you see
187*4882a593Smuzhiyunthe "OK" and "Done" appear on the screen.
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun``umount(u)`` can be used to mark filesystems as properly unmounted. From the
190*4882a593Smuzhiyunrunning system's point of view, they will be remounted read-only. The remount
191*4882a593Smuzhiyunisn't complete until you see the "OK" and "Done" message appear on the screen.
192*4882a593Smuzhiyun
193*4882a593SmuzhiyunThe loglevels ``0``-``9`` are useful when your console is being flooded with
194*4882a593Smuzhiyunkernel messages you do not want to see. Selecting ``0`` will prevent all but
195*4882a593Smuzhiyunthe most urgent kernel messages from reaching your console. (They will
196*4882a593Smuzhiyunstill be logged if syslogd/klogd are alive, though.)
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun``term(e)`` and ``kill(i)`` are useful if you have some sort of runaway process
199*4882a593Smuzhiyunyou are unable to kill any other way, especially if it's spawning other
200*4882a593Smuzhiyunprocesses.
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun"just thaw ``it(j)``" is useful if your system becomes unresponsive due to a
203*4882a593Smuzhiyunfrozen (probably root) filesystem via the FIFREEZE ioctl.
204*4882a593Smuzhiyun
205*4882a593SmuzhiyunSometimes SysRq seems to get 'stuck' after using it, what can I do?
206*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
207*4882a593Smuzhiyun
208*4882a593SmuzhiyunThat happens to me, also. I've found that tapping shift, alt, and control
209*4882a593Smuzhiyunon both sides of the keyboard, and hitting an invalid sysrq sequence again
210*4882a593Smuzhiyunwill fix the problem. (i.e., something like :kbd:`alt-sysrq-z`). Switching to
211*4882a593Smuzhiyunanother virtual console (:kbd:`ALT+Fn`) and then back again should also help.
212*4882a593Smuzhiyun
213*4882a593SmuzhiyunI hit SysRq, but nothing seems to happen, what's wrong?
214*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
215*4882a593Smuzhiyun
216*4882a593SmuzhiyunThere are some keyboards that produce a different keycode for SysRq than the
217*4882a593Smuzhiyunpre-defined value of 99
218*4882a593Smuzhiyun(see ``KEY_SYSRQ`` in ``include/uapi/linux/input-event-codes.h``), or
219*4882a593Smuzhiyunwhich don't have a SysRq key at all. In these cases, run ``showkey -s`` to find
220*4882a593Smuzhiyunan appropriate scancode sequence, and use ``setkeycodes <sequence> 99`` to map
221*4882a593Smuzhiyunthis sequence to the usual SysRq code (e.g., ``setkeycodes e05b 99``). It's
222*4882a593Smuzhiyunprobably best to put this command in a boot script. Oh, and by the way, you
223*4882a593Smuzhiyunexit ``showkey`` by not typing anything for ten seconds.
224*4882a593Smuzhiyun
225*4882a593SmuzhiyunI want to add SysRQ key events to a module, how does it work?
226*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
227*4882a593Smuzhiyun
228*4882a593SmuzhiyunIn order to register a basic function with the table, you must first include
229*4882a593Smuzhiyunthe header ``include/linux/sysrq.h``, this will define everything else you need.
230*4882a593SmuzhiyunNext, you must create a ``sysrq_key_op`` struct, and populate it with A) the key
231*4882a593Smuzhiyunhandler function you will use, B) a help_msg string, that will print when SysRQ
232*4882a593Smuzhiyunprints help, and C) an action_msg string, that will print right before your
233*4882a593Smuzhiyunhandler is called. Your handler must conform to the prototype in 'sysrq.h'.
234*4882a593Smuzhiyun
235*4882a593SmuzhiyunAfter the ``sysrq_key_op`` is created, you can call the kernel function
236*4882a593Smuzhiyun``register_sysrq_key(int key, const struct sysrq_key_op *op_p);`` this will
237*4882a593Smuzhiyunregister the operation pointed to by ``op_p`` at table key 'key',
238*4882a593Smuzhiyunif that slot in the table is blank. At module unload time, you must call
239*4882a593Smuzhiyunthe function ``unregister_sysrq_key(int key, const struct sysrq_key_op *op_p)``,
240*4882a593Smuzhiyunwhich will remove the key op pointed to by 'op_p' from the key 'key', if and
241*4882a593Smuzhiyunonly if it is currently registered in that slot. This is in case the slot has
242*4882a593Smuzhiyunbeen overwritten since you registered it.
243*4882a593Smuzhiyun
244*4882a593SmuzhiyunThe Magic SysRQ system works by registering key operations against a key op
245*4882a593Smuzhiyunlookup table, which is defined in 'drivers/tty/sysrq.c'. This key table has
246*4882a593Smuzhiyuna number of operations registered into it at compile time, but is mutable,
247*4882a593Smuzhiyunand 2 functions are exported for interface to it::
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun	register_sysrq_key and unregister_sysrq_key.
250*4882a593Smuzhiyun
251*4882a593SmuzhiyunOf course, never ever leave an invalid pointer in the table. I.e., when
252*4882a593Smuzhiyunyour module that called register_sysrq_key() exits, it must call
253*4882a593Smuzhiyununregister_sysrq_key() to clean up the sysrq key table entry that it used.
254*4882a593SmuzhiyunNull pointers in the table are always safe. :)
255*4882a593Smuzhiyun
256*4882a593SmuzhiyunIf for some reason you feel the need to call the handle_sysrq function from
257*4882a593Smuzhiyunwithin a function called by handle_sysrq, you must be aware that you are in
258*4882a593Smuzhiyuna lock (you are also in an interrupt handler, which means don't sleep!), so
259*4882a593Smuzhiyunyou must call ``__handle_sysrq_nolock`` instead.
260*4882a593Smuzhiyun
261*4882a593SmuzhiyunWhen I hit a SysRq key combination only the header appears on the console?
262*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
263*4882a593Smuzhiyun
264*4882a593SmuzhiyunSysrq output is subject to the same console loglevel control as all
265*4882a593Smuzhiyunother console output.  This means that if the kernel was booted 'quiet'
266*4882a593Smuzhiyunas is common on distro kernels the output may not appear on the actual
267*4882a593Smuzhiyunconsole, even though it will appear in the dmesg buffer, and be accessible
268*4882a593Smuzhiyunvia the dmesg command and to the consumers of ``/proc/kmsg``.  As a specific
269*4882a593Smuzhiyunexception the header line from the sysrq command is passed to all console
270*4882a593Smuzhiyunconsumers as if the current loglevel was maximum.  If only the header
271*4882a593Smuzhiyunis emitted it is almost certain that the kernel loglevel is too low.
272*4882a593SmuzhiyunShould you require the output on the console channel then you will need
273*4882a593Smuzhiyunto temporarily up the console loglevel using :kbd:`alt-sysrq-8` or::
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun    echo 8 > /proc/sysrq-trigger
276*4882a593Smuzhiyun
277*4882a593SmuzhiyunRemember to return the loglevel to normal after triggering the sysrq
278*4882a593Smuzhiyuncommand you are interested in.
279*4882a593Smuzhiyun
280*4882a593SmuzhiyunI have more questions, who can I ask?
281*4882a593Smuzhiyun~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
282*4882a593Smuzhiyun
283*4882a593SmuzhiyunJust ask them on the linux-kernel mailing list:
284*4882a593Smuzhiyun	linux-kernel@vger.kernel.org
285*4882a593Smuzhiyun
286*4882a593SmuzhiyunCredits
287*4882a593Smuzhiyun~~~~~~~
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun- Written by Mydraal <vulpyne@vulpyne.net>
290*4882a593Smuzhiyun- Updated by Adam Sulmicki <adam@cfar.umd.edu>
291*4882a593Smuzhiyun- Updated by Jeremy M. Dolan <jmd@turbogeek.org> 2001/01/28 10:15:59
292*4882a593Smuzhiyun- Added to by Crutcher Dunnavant <crutcher+kernel@datastacks.com>
293