sysrq.c 26 KB

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