sysrq.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Linux Magic System Request Key Hacks
  4. *
  5. * (c) 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
  6. * based on ideas by Pavel Machek <pavel@atrey.karlin.mff.cuni.cz>
  7. *
  8. * (c) 2000 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
  9. * overhauled to use key registration
  10. * based upon discusions in irc://irc.openprojects.net/#kernelnewbies
  11. *
  12. * Copyright (c) 2010 Dmitry Torokhov
  13. * Input handler conversion
  14. */
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/sched/signal.h>
  17. #include <linux/sched/rt.h>
  18. #include <linux/sched/debug.h>
  19. #include <linux/sched/task.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mm.h>
  22. #include <linux/fs.h>
  23. #include <linux/mount.h>
  24. #include <linux/kdev_t.h>
  25. #include <linux/major.h>
  26. #include <linux/reboot.h>
  27. #include <linux/sysrq.h>
  28. #include <linux/kbd_kern.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/nmi.h>
  31. #include <linux/quotaops.h>
  32. #include <linux/perf_event.h>
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/suspend.h>
  36. #include <linux/writeback.h>
  37. #include <linux/swap.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/vt_kern.h>
  40. #include <linux/workqueue.h>
  41. #include <linux/hrtimer.h>
  42. #include <linux/oom.h>
  43. #include <linux/slab.h>
  44. #include <linux/input.h>
  45. #include <linux/uaccess.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/jiffies.h>
  48. #include <linux/syscalls.h>
  49. #include <linux/of.h>
  50. #include <linux/rcupdate.h>
  51. #include <asm/ptrace.h>
  52. #include <asm/irq_regs.h>
  53. /* Whether we react on sysrq keys or just ignore them */
  54. static int __read_mostly sysrq_enabled = CONFIG_MAGIC_SYSRQ_DEFAULT_ENABLE;
  55. static bool __read_mostly sysrq_always_enabled;
  56. static bool sysrq_on(void)
  57. {
  58. return sysrq_enabled || sysrq_always_enabled;
  59. }
  60. /*
  61. * A value of 1 means 'all', other nonzero values are an op mask:
  62. */
  63. static bool sysrq_on_mask(int mask)
  64. {
  65. return sysrq_always_enabled ||
  66. sysrq_enabled == 1 ||
  67. (sysrq_enabled & mask);
  68. }
  69. static int __init sysrq_always_enabled_setup(char *str)
  70. {
  71. sysrq_always_enabled = true;
  72. pr_info("sysrq always enabled.\n");
  73. return 1;
  74. }
  75. __setup("sysrq_always_enabled", sysrq_always_enabled_setup);
  76. static void sysrq_handle_loglevel(int key)
  77. {
  78. int i;
  79. i = key - '0';
  80. console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
  81. pr_info("Loglevel set to %d\n", i);
  82. console_loglevel = i;
  83. }
  84. static struct sysrq_key_op sysrq_loglevel_op = {
  85. .handler = sysrq_handle_loglevel,
  86. .help_msg = "loglevel(0-9)",
  87. .action_msg = "Changing Loglevel",
  88. .enable_mask = SYSRQ_ENABLE_LOG,
  89. };
  90. #ifdef CONFIG_VT
  91. static void sysrq_handle_SAK(int key)
  92. {
  93. struct work_struct *SAK_work = &vc_cons[fg_console].SAK_work;
  94. schedule_work(SAK_work);
  95. }
  96. static struct sysrq_key_op sysrq_SAK_op = {
  97. .handler = sysrq_handle_SAK,
  98. .help_msg = "sak(k)",
  99. .action_msg = "SAK",
  100. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  101. };
  102. #else
  103. #define sysrq_SAK_op (*(struct sysrq_key_op *)NULL)
  104. #endif
  105. #ifdef CONFIG_VT
  106. static void sysrq_handle_unraw(int key)
  107. {
  108. vt_reset_unicode(fg_console);
  109. }
  110. static struct sysrq_key_op sysrq_unraw_op = {
  111. .handler = sysrq_handle_unraw,
  112. .help_msg = "unraw(r)",
  113. .action_msg = "Keyboard mode set to system default",
  114. .enable_mask = SYSRQ_ENABLE_KEYBOARD,
  115. };
  116. #else
  117. #define sysrq_unraw_op (*(struct sysrq_key_op *)NULL)
  118. #endif /* CONFIG_VT */
  119. static void sysrq_handle_crash(int key)
  120. {
  121. /* release the RCU read lock before crashing */
  122. rcu_read_unlock();
  123. panic("sysrq triggered crash\n");
  124. }
  125. static struct sysrq_key_op sysrq_crash_op = {
  126. .handler = sysrq_handle_crash,
  127. .help_msg = "crash(c)",
  128. .action_msg = "Trigger a crash",
  129. .enable_mask = SYSRQ_ENABLE_DUMP,
  130. };
  131. static void sysrq_handle_reboot(int key)
  132. {
  133. lockdep_off();
  134. local_irq_enable();
  135. emergency_restart();
  136. }
  137. static struct sysrq_key_op sysrq_reboot_op = {
  138. .handler = sysrq_handle_reboot,
  139. .help_msg = "reboot(b)",
  140. .action_msg = "Resetting",
  141. .enable_mask = SYSRQ_ENABLE_BOOT,
  142. };
  143. static void sysrq_handle_sync(int key)
  144. {
  145. emergency_sync();
  146. }
  147. static struct sysrq_key_op sysrq_sync_op = {
  148. .handler = sysrq_handle_sync,
  149. .help_msg = "sync(s)",
  150. .action_msg = "Emergency Sync",
  151. .enable_mask = SYSRQ_ENABLE_SYNC,
  152. };
  153. static void sysrq_handle_show_timers(int key)
  154. {
  155. sysrq_timer_list_show();
  156. }
  157. static struct sysrq_key_op sysrq_show_timers_op = {
  158. .handler = sysrq_handle_show_timers,
  159. .help_msg = "show-all-timers(q)",
  160. .action_msg = "Show clockevent devices & pending hrtimers (no others)",
  161. };
  162. static void sysrq_handle_mountro(int key)
  163. {
  164. emergency_remount();
  165. }
  166. static struct sysrq_key_op sysrq_mountro_op = {
  167. .handler = sysrq_handle_mountro,
  168. .help_msg = "unmount(u)",
  169. .action_msg = "Emergency Remount R/O",
  170. .enable_mask = SYSRQ_ENABLE_REMOUNT,
  171. };
  172. #ifdef CONFIG_LOCKDEP
  173. static void sysrq_handle_showlocks(int key)
  174. {
  175. debug_show_all_locks();
  176. }
  177. static struct sysrq_key_op sysrq_showlocks_op = {
  178. .handler = sysrq_handle_showlocks,
  179. .help_msg = "show-all-locks(d)",
  180. .action_msg = "Show Locks Held",
  181. };
  182. #else
  183. #define sysrq_showlocks_op (*(struct sysrq_key_op *)NULL)
  184. #endif
  185. #ifdef CONFIG_SMP
  186. static DEFINE_RAW_SPINLOCK(show_lock);
  187. static void showacpu(void *dummy)
  188. {
  189. unsigned long flags;
  190. /* Idle CPUs have no interesting backtrace. */
  191. if (idle_cpu(smp_processor_id()))
  192. return;
  193. raw_spin_lock_irqsave(&show_lock, flags);
  194. pr_info("CPU%d:\n", smp_processor_id());
  195. show_stack(NULL, NULL);
  196. raw_spin_unlock_irqrestore(&show_lock, flags);
  197. }
  198. static void sysrq_showregs_othercpus(struct work_struct *dummy)
  199. {
  200. smp_call_function(showacpu, NULL, 0);
  201. }
  202. static DECLARE_WORK(sysrq_showallcpus, sysrq_showregs_othercpus);
  203. static void sysrq_handle_showallcpus(int key)
  204. {
  205. /*
  206. * Fall back to the workqueue based printing if the
  207. * backtrace printing did not succeed or the
  208. * architecture has no support for it:
  209. */
  210. if (!trigger_all_cpu_backtrace()) {
  211. struct pt_regs *regs = NULL;
  212. if (in_irq())
  213. regs = get_irq_regs();
  214. if (regs) {
  215. pr_info("CPU%d:\n", smp_processor_id());
  216. show_regs(regs);
  217. }
  218. schedule_work(&sysrq_showallcpus);
  219. }
  220. }
  221. static struct sysrq_key_op sysrq_showallcpus_op = {
  222. .handler = sysrq_handle_showallcpus,
  223. .help_msg = "show-backtrace-all-active-cpus(l)",
  224. .action_msg = "Show backtrace of all active CPUs",
  225. .enable_mask = SYSRQ_ENABLE_DUMP,
  226. };
  227. #endif
  228. static void sysrq_handle_showregs(int key)
  229. {
  230. struct pt_regs *regs = NULL;
  231. if (in_irq())
  232. regs = get_irq_regs();
  233. if (regs)
  234. show_regs(regs);
  235. perf_event_print_debug();
  236. }
  237. static struct sysrq_key_op sysrq_showregs_op = {
  238. .handler = sysrq_handle_showregs,
  239. .help_msg = "show-registers(p)",
  240. .action_msg = "Show Regs",
  241. .enable_mask = SYSRQ_ENABLE_DUMP,
  242. };
  243. static void sysrq_handle_showstate(int key)
  244. {
  245. show_state();
  246. show_workqueue_state();
  247. }
  248. static struct sysrq_key_op sysrq_showstate_op = {
  249. .handler = sysrq_handle_showstate,
  250. .help_msg = "show-task-states(t)",
  251. .action_msg = "Show State",
  252. .enable_mask = SYSRQ_ENABLE_DUMP,
  253. };
  254. static void sysrq_handle_showstate_blocked(int key)
  255. {
  256. show_state_filter(TASK_UNINTERRUPTIBLE);
  257. }
  258. static struct sysrq_key_op sysrq_showstate_blocked_op = {
  259. .handler = sysrq_handle_showstate_blocked,
  260. .help_msg = "show-blocked-tasks(w)",
  261. .action_msg = "Show Blocked State",
  262. .enable_mask = SYSRQ_ENABLE_DUMP,
  263. };
  264. #ifdef CONFIG_TRACING
  265. #include <linux/ftrace.h>
  266. static void sysrq_ftrace_dump(int key)
  267. {
  268. ftrace_dump(DUMP_ALL);
  269. }
  270. static struct sysrq_key_op sysrq_ftrace_dump_op = {
  271. .handler = sysrq_ftrace_dump,
  272. .help_msg = "dump-ftrace-buffer(z)",
  273. .action_msg = "Dump ftrace buffer",
  274. .enable_mask = SYSRQ_ENABLE_DUMP,
  275. };
  276. #else
  277. #define sysrq_ftrace_dump_op (*(struct sysrq_key_op *)NULL)
  278. #endif
  279. static void sysrq_handle_showmem(int key)
  280. {
  281. show_mem(0, NULL);
  282. }
  283. static struct sysrq_key_op sysrq_showmem_op = {
  284. .handler = sysrq_handle_showmem,
  285. .help_msg = "show-memory-usage(m)",
  286. .action_msg = "Show Memory",
  287. .enable_mask = SYSRQ_ENABLE_DUMP,
  288. };
  289. /*
  290. * Signal sysrq helper function. Sends a signal to all user processes.
  291. */
  292. static void send_sig_all(int sig)
  293. {
  294. struct task_struct *p;
  295. read_lock(&tasklist_lock);
  296. for_each_process(p) {
  297. if (p->flags & PF_KTHREAD)
  298. continue;
  299. if (is_global_init(p))
  300. continue;
  301. do_send_sig_info(sig, SEND_SIG_PRIV, p, PIDTYPE_MAX);
  302. }
  303. read_unlock(&tasklist_lock);
  304. }
  305. static void sysrq_handle_term(int key)
  306. {
  307. send_sig_all(SIGTERM);
  308. console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
  309. }
  310. static struct sysrq_key_op sysrq_term_op = {
  311. .handler = sysrq_handle_term,
  312. .help_msg = "terminate-all-tasks(e)",
  313. .action_msg = "Terminate All Tasks",
  314. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  315. };
  316. static void moom_callback(struct work_struct *ignored)
  317. {
  318. const gfp_t gfp_mask = GFP_KERNEL;
  319. struct oom_control oc = {
  320. .zonelist = node_zonelist(first_memory_node, gfp_mask),
  321. .nodemask = NULL,
  322. .memcg = NULL,
  323. .gfp_mask = gfp_mask,
  324. .order = -1,
  325. };
  326. mutex_lock(&oom_lock);
  327. if (!out_of_memory(&oc))
  328. pr_info("OOM request ignored. No task eligible\n");
  329. mutex_unlock(&oom_lock);
  330. }
  331. static DECLARE_WORK(moom_work, moom_callback);
  332. static void sysrq_handle_moom(int key)
  333. {
  334. schedule_work(&moom_work);
  335. }
  336. static struct sysrq_key_op sysrq_moom_op = {
  337. .handler = sysrq_handle_moom,
  338. .help_msg = "memory-full-oom-kill(f)",
  339. .action_msg = "Manual OOM execution",
  340. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  341. };
  342. #ifdef CONFIG_BLOCK
  343. static void sysrq_handle_thaw(int key)
  344. {
  345. emergency_thaw_all();
  346. }
  347. static struct sysrq_key_op sysrq_thaw_op = {
  348. .handler = sysrq_handle_thaw,
  349. .help_msg = "thaw-filesystems(j)",
  350. .action_msg = "Emergency Thaw of all frozen filesystems",
  351. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  352. };
  353. #endif
  354. static void sysrq_handle_kill(int key)
  355. {
  356. send_sig_all(SIGKILL);
  357. console_loglevel = CONSOLE_LOGLEVEL_DEBUG;
  358. }
  359. static struct sysrq_key_op sysrq_kill_op = {
  360. .handler = sysrq_handle_kill,
  361. .help_msg = "kill-all-tasks(i)",
  362. .action_msg = "Kill All Tasks",
  363. .enable_mask = SYSRQ_ENABLE_SIGNAL,
  364. };
  365. static void sysrq_handle_unrt(int key)
  366. {
  367. normalize_rt_tasks();
  368. }
  369. static struct sysrq_key_op sysrq_unrt_op = {
  370. .handler = sysrq_handle_unrt,
  371. .help_msg = "nice-all-RT-tasks(n)",
  372. .action_msg = "Nice All RT Tasks",
  373. .enable_mask = SYSRQ_ENABLE_RTNICE,
  374. };
  375. /* Key Operations table and lock */
  376. static DEFINE_SPINLOCK(sysrq_key_table_lock);
  377. static struct sysrq_key_op *sysrq_key_table[36] = {
  378. &sysrq_loglevel_op, /* 0 */
  379. &sysrq_loglevel_op, /* 1 */
  380. &sysrq_loglevel_op, /* 2 */
  381. &sysrq_loglevel_op, /* 3 */
  382. &sysrq_loglevel_op, /* 4 */
  383. &sysrq_loglevel_op, /* 5 */
  384. &sysrq_loglevel_op, /* 6 */
  385. &sysrq_loglevel_op, /* 7 */
  386. &sysrq_loglevel_op, /* 8 */
  387. &sysrq_loglevel_op, /* 9 */
  388. /*
  389. * a: Don't use for system provided sysrqs, it is handled specially on
  390. * sparc and will never arrive.
  391. */
  392. NULL, /* a */
  393. &sysrq_reboot_op, /* b */
  394. &sysrq_crash_op, /* c */
  395. &sysrq_showlocks_op, /* d */
  396. &sysrq_term_op, /* e */
  397. &sysrq_moom_op, /* f */
  398. /* g: May be registered for the kernel debugger */
  399. NULL, /* g */
  400. NULL, /* h - reserved for help */
  401. &sysrq_kill_op, /* i */
  402. #ifdef CONFIG_BLOCK
  403. &sysrq_thaw_op, /* j */
  404. #else
  405. NULL, /* j */
  406. #endif
  407. &sysrq_SAK_op, /* k */
  408. #ifdef CONFIG_SMP
  409. &sysrq_showallcpus_op, /* l */
  410. #else
  411. NULL, /* l */
  412. #endif
  413. &sysrq_showmem_op, /* m */
  414. &sysrq_unrt_op, /* n */
  415. /* o: This will often be registered as 'Off' at init time */
  416. NULL, /* o */
  417. &sysrq_showregs_op, /* p */
  418. &sysrq_show_timers_op, /* q */
  419. &sysrq_unraw_op, /* r */
  420. &sysrq_sync_op, /* s */
  421. &sysrq_showstate_op, /* t */
  422. &sysrq_mountro_op, /* u */
  423. /* v: May be registered for frame buffer console restore */
  424. NULL, /* v */
  425. &sysrq_showstate_blocked_op, /* w */
  426. /* x: May be registered on mips for TLB dump */
  427. /* x: May be registered on ppc/powerpc for xmon */
  428. /* x: May be registered on sparc64 for global PMU dump */
  429. NULL, /* x */
  430. /* y: May be registered on sparc64 for global register dump */
  431. NULL, /* y */
  432. &sysrq_ftrace_dump_op, /* z */
  433. };
  434. /* key2index calculation, -1 on invalid index */
  435. static int sysrq_key_table_key2index(int key)
  436. {
  437. int retval;
  438. if ((key >= '0') && (key <= '9'))
  439. retval = key - '0';
  440. else if ((key >= 'a') && (key <= 'z'))
  441. retval = key + 10 - 'a';
  442. else
  443. retval = -1;
  444. return retval;
  445. }
  446. /*
  447. * get and put functions for the table, exposed to modules.
  448. */
  449. struct sysrq_key_op *__sysrq_get_key_op(int key)
  450. {
  451. struct sysrq_key_op *op_p = NULL;
  452. int i;
  453. i = sysrq_key_table_key2index(key);
  454. if (i != -1)
  455. op_p = sysrq_key_table[i];
  456. return op_p;
  457. }
  458. static void __sysrq_put_key_op(int key, struct sysrq_key_op *op_p)
  459. {
  460. int i = sysrq_key_table_key2index(key);
  461. if (i != -1)
  462. sysrq_key_table[i] = op_p;
  463. }
  464. void __handle_sysrq(int key, bool check_mask)
  465. {
  466. struct sysrq_key_op *op_p;
  467. int orig_log_level;
  468. int orig_suppress_printk;
  469. int i;
  470. orig_suppress_printk = suppress_printk;
  471. suppress_printk = 0;
  472. rcu_sysrq_start();
  473. rcu_read_lock();
  474. /*
  475. * Raise the apparent loglevel to maximum so that the sysrq header
  476. * is shown to provide the user with positive feedback. We do not
  477. * simply emit this at KERN_EMERG as that would change message
  478. * routing in the consumers of /proc/kmsg.
  479. */
  480. orig_log_level = console_loglevel;
  481. console_loglevel = CONSOLE_LOGLEVEL_DEFAULT;
  482. op_p = __sysrq_get_key_op(key);
  483. if (op_p) {
  484. /*
  485. * Should we check for enabled operations (/proc/sysrq-trigger
  486. * should not) and is the invoked operation enabled?
  487. */
  488. if (!check_mask || sysrq_on_mask(op_p->enable_mask)) {
  489. pr_info("%s\n", op_p->action_msg);
  490. console_loglevel = orig_log_level;
  491. op_p->handler(key);
  492. } else {
  493. pr_info("This sysrq operation is disabled.\n");
  494. console_loglevel = orig_log_level;
  495. }
  496. } else {
  497. pr_info("HELP : ");
  498. /* Only print the help msg once per handler */
  499. for (i = 0; i < ARRAY_SIZE(sysrq_key_table); i++) {
  500. if (sysrq_key_table[i]) {
  501. int j;
  502. for (j = 0; sysrq_key_table[i] !=
  503. sysrq_key_table[j]; j++)
  504. ;
  505. if (j != i)
  506. continue;
  507. pr_cont("%s ", sysrq_key_table[i]->help_msg);
  508. }
  509. }
  510. pr_cont("\n");
  511. console_loglevel = orig_log_level;
  512. }
  513. rcu_read_unlock();
  514. rcu_sysrq_end();
  515. suppress_printk = orig_suppress_printk;
  516. }
  517. void handle_sysrq(int key)
  518. {
  519. if (sysrq_on())
  520. __handle_sysrq(key, true);
  521. }
  522. EXPORT_SYMBOL(handle_sysrq);
  523. #ifdef CONFIG_INPUT
  524. static int sysrq_reset_downtime_ms;
  525. /* Simple translation table for the SysRq keys */
  526. static const unsigned char sysrq_xlate[KEY_CNT] =
  527. "\000\0331234567890-=\177\t" /* 0x00 - 0x0f */
  528. "qwertyuiop[]\r\000as" /* 0x10 - 0x1f */
  529. "dfghjkl;'`\000\\zxcv" /* 0x20 - 0x2f */
  530. "bnm,./\000*\000 \000\201\202\203\204\205" /* 0x30 - 0x3f */
  531. "\206\207\210\211\212\000\000789-456+1" /* 0x40 - 0x4f */
  532. "230\177\000\000\213\214\000\000\000\000\000\000\000\000\000\000" /* 0x50 - 0x5f */
  533. "\r\000/"; /* 0x60 - 0x6f */
  534. struct sysrq_state {
  535. struct input_handle handle;
  536. struct work_struct reinject_work;
  537. unsigned long key_down[BITS_TO_LONGS(KEY_CNT)];
  538. unsigned int alt;
  539. unsigned int alt_use;
  540. bool active;
  541. bool need_reinject;
  542. bool reinjecting;
  543. /* reset sequence handling */
  544. bool reset_canceled;
  545. bool reset_requested;
  546. unsigned long reset_keybit[BITS_TO_LONGS(KEY_CNT)];
  547. int reset_seq_len;
  548. int reset_seq_cnt;
  549. int reset_seq_version;
  550. struct timer_list keyreset_timer;
  551. };
  552. #define SYSRQ_KEY_RESET_MAX 20 /* Should be plenty */
  553. static unsigned short sysrq_reset_seq[SYSRQ_KEY_RESET_MAX];
  554. static unsigned int sysrq_reset_seq_len;
  555. static unsigned int sysrq_reset_seq_version = 1;
  556. static void sysrq_parse_reset_sequence(struct sysrq_state *state)
  557. {
  558. int i;
  559. unsigned short key;
  560. state->reset_seq_cnt = 0;
  561. for (i = 0; i < sysrq_reset_seq_len; i++) {
  562. key = sysrq_reset_seq[i];
  563. if (key == KEY_RESERVED || key > KEY_MAX)
  564. break;
  565. __set_bit(key, state->reset_keybit);
  566. state->reset_seq_len++;
  567. if (test_bit(key, state->key_down))
  568. state->reset_seq_cnt++;
  569. }
  570. /* Disable reset until old keys are not released */
  571. state->reset_canceled = state->reset_seq_cnt != 0;
  572. state->reset_seq_version = sysrq_reset_seq_version;
  573. }
  574. static void sysrq_do_reset(struct timer_list *t)
  575. {
  576. struct sysrq_state *state = from_timer(state, t, keyreset_timer);
  577. state->reset_requested = true;
  578. orderly_reboot();
  579. }
  580. static void sysrq_handle_reset_request(struct sysrq_state *state)
  581. {
  582. if (state->reset_requested)
  583. __handle_sysrq(sysrq_xlate[KEY_B], false);
  584. if (sysrq_reset_downtime_ms)
  585. mod_timer(&state->keyreset_timer,
  586. jiffies + msecs_to_jiffies(sysrq_reset_downtime_ms));
  587. else
  588. sysrq_do_reset(&state->keyreset_timer);
  589. }
  590. static void sysrq_detect_reset_sequence(struct sysrq_state *state,
  591. unsigned int code, int value)
  592. {
  593. if (!test_bit(code, state->reset_keybit)) {
  594. /*
  595. * Pressing any key _not_ in reset sequence cancels
  596. * the reset sequence. Also cancelling the timer in
  597. * case additional keys were pressed after a reset
  598. * has been requested.
  599. */
  600. if (value && state->reset_seq_cnt) {
  601. state->reset_canceled = true;
  602. del_timer(&state->keyreset_timer);
  603. }
  604. } else if (value == 0) {
  605. /*
  606. * Key release - all keys in the reset sequence need
  607. * to be pressed and held for the reset timeout
  608. * to hold.
  609. */
  610. del_timer(&state->keyreset_timer);
  611. if (--state->reset_seq_cnt == 0)
  612. state->reset_canceled = false;
  613. } else if (value == 1) {
  614. /* key press, not autorepeat */
  615. if (++state->reset_seq_cnt == state->reset_seq_len &&
  616. !state->reset_canceled) {
  617. sysrq_handle_reset_request(state);
  618. }
  619. }
  620. }
  621. #ifdef CONFIG_OF
  622. static void sysrq_of_get_keyreset_config(void)
  623. {
  624. u32 key;
  625. struct device_node *np;
  626. struct property *prop;
  627. const __be32 *p;
  628. np = of_find_node_by_path("/chosen/linux,sysrq-reset-seq");
  629. if (!np) {
  630. pr_debug("No sysrq node found");
  631. return;
  632. }
  633. /* Reset in case a __weak definition was present */
  634. sysrq_reset_seq_len = 0;
  635. of_property_for_each_u32(np, "keyset", prop, p, key) {
  636. if (key == KEY_RESERVED || key > KEY_MAX ||
  637. sysrq_reset_seq_len == SYSRQ_KEY_RESET_MAX)
  638. break;
  639. sysrq_reset_seq[sysrq_reset_seq_len++] = (unsigned short)key;
  640. }
  641. /* Get reset timeout if any. */
  642. of_property_read_u32(np, "timeout-ms", &sysrq_reset_downtime_ms);
  643. of_node_put(np);
  644. }
  645. #else
  646. static void sysrq_of_get_keyreset_config(void)
  647. {
  648. }
  649. #endif
  650. static void sysrq_reinject_alt_sysrq(struct work_struct *work)
  651. {
  652. struct sysrq_state *sysrq =
  653. container_of(work, struct sysrq_state, reinject_work);
  654. struct input_handle *handle = &sysrq->handle;
  655. unsigned int alt_code = sysrq->alt_use;
  656. if (sysrq->need_reinject) {
  657. /* we do not want the assignment to be reordered */
  658. sysrq->reinjecting = true;
  659. mb();
  660. /* Simulate press and release of Alt + SysRq */
  661. input_inject_event(handle, EV_KEY, alt_code, 1);
  662. input_inject_event(handle, EV_KEY, KEY_SYSRQ, 1);
  663. input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
  664. input_inject_event(handle, EV_KEY, KEY_SYSRQ, 0);
  665. input_inject_event(handle, EV_KEY, alt_code, 0);
  666. input_inject_event(handle, EV_SYN, SYN_REPORT, 1);
  667. mb();
  668. sysrq->reinjecting = false;
  669. }
  670. }
  671. static bool sysrq_handle_keypress(struct sysrq_state *sysrq,
  672. unsigned int code, int value)
  673. {
  674. bool was_active = sysrq->active;
  675. bool suppress;
  676. switch (code) {
  677. case KEY_LEFTALT:
  678. case KEY_RIGHTALT:
  679. if (!value) {
  680. /* One of ALTs is being released */
  681. if (sysrq->active && code == sysrq->alt_use)
  682. sysrq->active = false;
  683. sysrq->alt = KEY_RESERVED;
  684. } else if (value != 2) {
  685. sysrq->alt = code;
  686. sysrq->need_reinject = false;
  687. }
  688. break;
  689. case KEY_SYSRQ:
  690. if (value == 1 && sysrq->alt != KEY_RESERVED) {
  691. sysrq->active = true;
  692. sysrq->alt_use = sysrq->alt;
  693. /*
  694. * If nothing else will be pressed we'll need
  695. * to re-inject Alt-SysRq keysroke.
  696. */
  697. sysrq->need_reinject = true;
  698. }
  699. /*
  700. * Pretend that sysrq was never pressed at all. This
  701. * is needed to properly handle KGDB which will try
  702. * to release all keys after exiting debugger. If we
  703. * do not clear key bit it KGDB will end up sending
  704. * release events for Alt and SysRq, potentially
  705. * triggering print screen function.
  706. */
  707. if (sysrq->active)
  708. clear_bit(KEY_SYSRQ, sysrq->handle.dev->key);
  709. break;
  710. default:
  711. if (sysrq->active && value && value != 2) {
  712. sysrq->need_reinject = false;
  713. __handle_sysrq(sysrq_xlate[code], true);
  714. }
  715. break;
  716. }
  717. suppress = sysrq->active;
  718. if (!sysrq->active) {
  719. /*
  720. * See if reset sequence has changed since the last time.
  721. */
  722. if (sysrq->reset_seq_version != sysrq_reset_seq_version)
  723. sysrq_parse_reset_sequence(sysrq);
  724. /*
  725. * If we are not suppressing key presses keep track of
  726. * keyboard state so we can release keys that have been
  727. * pressed before entering SysRq mode.
  728. */
  729. if (value)
  730. set_bit(code, sysrq->key_down);
  731. else
  732. clear_bit(code, sysrq->key_down);
  733. if (was_active)
  734. schedule_work(&sysrq->reinject_work);
  735. /* Check for reset sequence */
  736. sysrq_detect_reset_sequence(sysrq, code, value);
  737. } else if (value == 0 && test_and_clear_bit(code, sysrq->key_down)) {
  738. /*
  739. * Pass on release events for keys that was pressed before
  740. * entering SysRq mode.
  741. */
  742. suppress = false;
  743. }
  744. return suppress;
  745. }
  746. static bool sysrq_filter(struct input_handle *handle,
  747. unsigned int type, unsigned int code, int value)
  748. {
  749. struct sysrq_state *sysrq = handle->private;
  750. bool suppress;
  751. /*
  752. * Do not filter anything if we are in the process of re-injecting
  753. * Alt+SysRq combination.
  754. */
  755. if (sysrq->reinjecting)
  756. return false;
  757. switch (type) {
  758. case EV_SYN:
  759. suppress = false;
  760. break;
  761. case EV_KEY:
  762. suppress = sysrq_handle_keypress(sysrq, code, value);
  763. break;
  764. default:
  765. suppress = sysrq->active;
  766. break;
  767. }
  768. return suppress;
  769. }
  770. static int sysrq_connect(struct input_handler *handler,
  771. struct input_dev *dev,
  772. const struct input_device_id *id)
  773. {
  774. struct sysrq_state *sysrq;
  775. int error;
  776. sysrq = kzalloc(sizeof(struct sysrq_state), GFP_KERNEL);
  777. if (!sysrq)
  778. return -ENOMEM;
  779. INIT_WORK(&sysrq->reinject_work, sysrq_reinject_alt_sysrq);
  780. sysrq->handle.dev = dev;
  781. sysrq->handle.handler = handler;
  782. sysrq->handle.name = "sysrq";
  783. sysrq->handle.private = sysrq;
  784. timer_setup(&sysrq->keyreset_timer, sysrq_do_reset, 0);
  785. error = input_register_handle(&sysrq->handle);
  786. if (error) {
  787. pr_err("Failed to register input sysrq handler, error %d\n",
  788. error);
  789. goto err_free;
  790. }
  791. error = input_open_device(&sysrq->handle);
  792. if (error) {
  793. pr_err("Failed to open input device, error %d\n", error);
  794. goto err_unregister;
  795. }
  796. return 0;
  797. err_unregister:
  798. input_unregister_handle(&sysrq->handle);
  799. err_free:
  800. kfree(sysrq);
  801. return error;
  802. }
  803. static void sysrq_disconnect(struct input_handle *handle)
  804. {
  805. struct sysrq_state *sysrq = handle->private;
  806. input_close_device(handle);
  807. cancel_work_sync(&sysrq->reinject_work);
  808. del_timer_sync(&sysrq->keyreset_timer);
  809. input_unregister_handle(handle);
  810. kfree(sysrq);
  811. }
  812. /*
  813. * We are matching on KEY_LEFTALT instead of KEY_SYSRQ because not all
  814. * keyboards have SysRq key predefined and so user may add it to keymap
  815. * later, but we expect all such keyboards to have left alt.
  816. */
  817. static const struct input_device_id sysrq_ids[] = {
  818. {
  819. .flags = INPUT_DEVICE_ID_MATCH_EVBIT |
  820. INPUT_DEVICE_ID_MATCH_KEYBIT,
  821. .evbit = { [BIT_WORD(EV_KEY)] = BIT_MASK(EV_KEY) },
  822. .keybit = { [BIT_WORD(KEY_LEFTALT)] = BIT_MASK(KEY_LEFTALT) },
  823. },
  824. { },
  825. };
  826. static struct input_handler sysrq_handler = {
  827. .filter = sysrq_filter,
  828. .connect = sysrq_connect,
  829. .disconnect = sysrq_disconnect,
  830. .name = "sysrq",
  831. .id_table = sysrq_ids,
  832. };
  833. static bool sysrq_handler_registered;
  834. static inline void sysrq_register_handler(void)
  835. {
  836. int error;
  837. sysrq_of_get_keyreset_config();
  838. error = input_register_handler(&sysrq_handler);
  839. if (error)
  840. pr_err("Failed to register input handler, error %d", error);
  841. else
  842. sysrq_handler_registered = true;
  843. }
  844. static inline void sysrq_unregister_handler(void)
  845. {
  846. if (sysrq_handler_registered) {
  847. input_unregister_handler(&sysrq_handler);
  848. sysrq_handler_registered = false;
  849. }
  850. }
  851. static int sysrq_reset_seq_param_set(const char *buffer,
  852. const struct kernel_param *kp)
  853. {
  854. unsigned long val;
  855. int error;
  856. error = kstrtoul(buffer, 0, &val);
  857. if (error < 0)
  858. return error;
  859. if (val > KEY_MAX)
  860. return -EINVAL;
  861. *((unsigned short *)kp->arg) = val;
  862. sysrq_reset_seq_version++;
  863. return 0;
  864. }
  865. static const struct kernel_param_ops param_ops_sysrq_reset_seq = {
  866. .get = param_get_ushort,
  867. .set = sysrq_reset_seq_param_set,
  868. };
  869. #define param_check_sysrq_reset_seq(name, p) \
  870. __param_check(name, p, unsigned short)
  871. /*
  872. * not really modular, but the easiest way to keep compat with existing
  873. * bootargs behaviour is to continue using module_param here.
  874. */
  875. module_param_array_named(reset_seq, sysrq_reset_seq, sysrq_reset_seq,
  876. &sysrq_reset_seq_len, 0644);
  877. module_param_named(sysrq_downtime_ms, sysrq_reset_downtime_ms, int, 0644);
  878. #else
  879. static inline void sysrq_register_handler(void)
  880. {
  881. }
  882. static inline void sysrq_unregister_handler(void)
  883. {
  884. }
  885. #endif /* CONFIG_INPUT */
  886. int sysrq_toggle_support(int enable_mask)
  887. {
  888. bool was_enabled = sysrq_on();
  889. sysrq_enabled = enable_mask;
  890. if (was_enabled != sysrq_on()) {
  891. if (sysrq_on())
  892. sysrq_register_handler();
  893. else
  894. sysrq_unregister_handler();
  895. }
  896. return 0;
  897. }
  898. static int __sysrq_swap_key_ops(int key, struct sysrq_key_op *insert_op_p,
  899. struct sysrq_key_op *remove_op_p)
  900. {
  901. int retval;
  902. spin_lock(&sysrq_key_table_lock);
  903. if (__sysrq_get_key_op(key) == remove_op_p) {
  904. __sysrq_put_key_op(key, insert_op_p);
  905. retval = 0;
  906. } else {
  907. retval = -1;
  908. }
  909. spin_unlock(&sysrq_key_table_lock);
  910. /*
  911. * A concurrent __handle_sysrq either got the old op or the new op.
  912. * Wait for it to go away before returning, so the code for an old
  913. * op is not freed (eg. on module unload) while it is in use.
  914. */
  915. synchronize_rcu();
  916. return retval;
  917. }
  918. int register_sysrq_key(int key, struct sysrq_key_op *op_p)
  919. {
  920. return __sysrq_swap_key_ops(key, op_p, NULL);
  921. }
  922. EXPORT_SYMBOL(register_sysrq_key);
  923. int unregister_sysrq_key(int key, struct sysrq_key_op *op_p)
  924. {
  925. return __sysrq_swap_key_ops(key, NULL, op_p);
  926. }
  927. EXPORT_SYMBOL(unregister_sysrq_key);
  928. #ifdef CONFIG_PROC_FS
  929. /*
  930. * writing 'C' to /proc/sysrq-trigger is like sysrq-C
  931. */
  932. static ssize_t write_sysrq_trigger(struct file *file, const char __user *buf,
  933. size_t count, loff_t *ppos)
  934. {
  935. if (count) {
  936. char c;
  937. if (get_user(c, buf))
  938. return -EFAULT;
  939. __handle_sysrq(c, false);
  940. }
  941. return count;
  942. }
  943. static const struct file_operations proc_sysrq_trigger_operations = {
  944. .write = write_sysrq_trigger,
  945. .llseek = noop_llseek,
  946. };
  947. static void sysrq_init_procfs(void)
  948. {
  949. if (!proc_create("sysrq-trigger", S_IWUSR, NULL,
  950. &proc_sysrq_trigger_operations))
  951. pr_err("Failed to register proc interface\n");
  952. }
  953. #else
  954. static inline void sysrq_init_procfs(void)
  955. {
  956. }
  957. #endif /* CONFIG_PROC_FS */
  958. static int __init sysrq_init(void)
  959. {
  960. sysrq_init_procfs();
  961. if (sysrq_on())
  962. sysrq_register_handler();
  963. return 0;
  964. }
  965. device_initcall(sysrq_init);