reboot.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  3. #include <linux/export.h>
  4. #include <linux/reboot.h>
  5. #include <linux/init.h>
  6. #include <linux/pm.h>
  7. #include <linux/efi.h>
  8. #include <linux/dmi.h>
  9. #include <linux/sched.h>
  10. #include <linux/tboot.h>
  11. #include <linux/delay.h>
  12. #include <linux/objtool.h>
  13. #include <linux/pgtable.h>
  14. #include <linux/kexec.h>
  15. #include <acpi/reboot.h>
  16. #include <asm/io.h>
  17. #include <asm/apic.h>
  18. #include <asm/io_apic.h>
  19. #include <asm/desc.h>
  20. #include <asm/hpet.h>
  21. #include <asm/proto.h>
  22. #include <asm/reboot_fixups.h>
  23. #include <asm/reboot.h>
  24. #include <asm/pci_x86.h>
  25. #include <asm/cpu.h>
  26. #include <asm/nmi.h>
  27. #include <asm/smp.h>
  28. #include <linux/ctype.h>
  29. #include <linux/mc146818rtc.h>
  30. #include <asm/realmode.h>
  31. #include <asm/x86_init.h>
  32. #include <asm/efi.h>
  33. /*
  34. * Power off function, if any
  35. */
  36. void (*pm_power_off)(void);
  37. EXPORT_SYMBOL(pm_power_off);
  38. /*
  39. * This is set if we need to go through the 'emergency' path.
  40. * When machine_emergency_restart() is called, we may be on
  41. * an inconsistent state and won't be able to do a clean cleanup
  42. */
  43. static int reboot_emergency;
  44. /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
  45. bool port_cf9_safe = false;
  46. /*
  47. * Reboot options and system auto-detection code provided by
  48. * Dell Inc. so their systems "just work". :-)
  49. */
  50. /*
  51. * Some machines require the "reboot=a" commandline options
  52. */
  53. static int __init set_acpi_reboot(const struct dmi_system_id *d)
  54. {
  55. if (reboot_type != BOOT_ACPI) {
  56. reboot_type = BOOT_ACPI;
  57. pr_info("%s series board detected. Selecting %s-method for reboots.\n",
  58. d->ident, "ACPI");
  59. }
  60. return 0;
  61. }
  62. /*
  63. * Some machines require the "reboot=b" or "reboot=k" commandline options,
  64. * this quirk makes that automatic.
  65. */
  66. static int __init set_bios_reboot(const struct dmi_system_id *d)
  67. {
  68. if (reboot_type != BOOT_BIOS) {
  69. reboot_type = BOOT_BIOS;
  70. pr_info("%s series board detected. Selecting %s-method for reboots.\n",
  71. d->ident, "BIOS");
  72. }
  73. return 0;
  74. }
  75. /*
  76. * Some machines don't handle the default ACPI reboot method and
  77. * require the EFI reboot method:
  78. */
  79. static int __init set_efi_reboot(const struct dmi_system_id *d)
  80. {
  81. if (reboot_type != BOOT_EFI && !efi_runtime_disabled()) {
  82. reboot_type = BOOT_EFI;
  83. pr_info("%s series board detected. Selecting EFI-method for reboot.\n", d->ident);
  84. }
  85. return 0;
  86. }
  87. void __noreturn machine_real_restart(unsigned int type)
  88. {
  89. local_irq_disable();
  90. /*
  91. * Write zero to CMOS register number 0x0f, which the BIOS POST
  92. * routine will recognize as telling it to do a proper reboot. (Well
  93. * that's what this book in front of me says -- it may only apply to
  94. * the Phoenix BIOS though, it's not clear). At the same time,
  95. * disable NMIs by setting the top bit in the CMOS address register,
  96. * as we're about to do peculiar things to the CPU. I'm not sure if
  97. * `outb_p' is needed instead of just `outb'. Use it to be on the
  98. * safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
  99. */
  100. spin_lock(&rtc_lock);
  101. CMOS_WRITE(0x00, 0x8f);
  102. spin_unlock(&rtc_lock);
  103. /*
  104. * Switch to the trampoline page table.
  105. */
  106. load_trampoline_pgtable();
  107. /* Jump to the identity-mapped low memory code */
  108. #ifdef CONFIG_X86_32
  109. asm volatile("jmpl *%0" : :
  110. "rm" (real_mode_header->machine_real_restart_asm),
  111. "a" (type));
  112. #else
  113. asm volatile("ljmpl *%0" : :
  114. "m" (real_mode_header->machine_real_restart_asm),
  115. "D" (type));
  116. #endif
  117. unreachable();
  118. }
  119. #ifdef CONFIG_APM_MODULE
  120. EXPORT_SYMBOL(machine_real_restart);
  121. #endif
  122. STACK_FRAME_NON_STANDARD(machine_real_restart);
  123. /*
  124. * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
  125. */
  126. static int __init set_pci_reboot(const struct dmi_system_id *d)
  127. {
  128. if (reboot_type != BOOT_CF9_FORCE) {
  129. reboot_type = BOOT_CF9_FORCE;
  130. pr_info("%s series board detected. Selecting %s-method for reboots.\n",
  131. d->ident, "PCI");
  132. }
  133. return 0;
  134. }
  135. static int __init set_kbd_reboot(const struct dmi_system_id *d)
  136. {
  137. if (reboot_type != BOOT_KBD) {
  138. reboot_type = BOOT_KBD;
  139. pr_info("%s series board detected. Selecting %s-method for reboot.\n",
  140. d->ident, "KBD");
  141. }
  142. return 0;
  143. }
  144. /*
  145. * This is a single dmi_table handling all reboot quirks.
  146. */
  147. static const struct dmi_system_id reboot_dmi_table[] __initconst = {
  148. /* Acer */
  149. { /* Handle reboot issue on Acer Aspire one */
  150. .callback = set_kbd_reboot,
  151. .ident = "Acer Aspire One A110",
  152. .matches = {
  153. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  154. DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
  155. },
  156. },
  157. { /* Handle reboot issue on Acer TravelMate X514-51T */
  158. .callback = set_efi_reboot,
  159. .ident = "Acer TravelMate X514-51T",
  160. .matches = {
  161. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  162. DMI_MATCH(DMI_PRODUCT_NAME, "TravelMate X514-51T"),
  163. },
  164. },
  165. /* Apple */
  166. { /* Handle problems with rebooting on Apple MacBook5 */
  167. .callback = set_pci_reboot,
  168. .ident = "Apple MacBook5",
  169. .matches = {
  170. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  171. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
  172. },
  173. },
  174. { /* Handle problems with rebooting on Apple MacBook6,1 */
  175. .callback = set_pci_reboot,
  176. .ident = "Apple MacBook6,1",
  177. .matches = {
  178. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  179. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook6,1"),
  180. },
  181. },
  182. { /* Handle problems with rebooting on Apple MacBookPro5 */
  183. .callback = set_pci_reboot,
  184. .ident = "Apple MacBookPro5",
  185. .matches = {
  186. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  187. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
  188. },
  189. },
  190. { /* Handle problems with rebooting on Apple Macmini3,1 */
  191. .callback = set_pci_reboot,
  192. .ident = "Apple Macmini3,1",
  193. .matches = {
  194. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  195. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
  196. },
  197. },
  198. { /* Handle problems with rebooting on the iMac9,1. */
  199. .callback = set_pci_reboot,
  200. .ident = "Apple iMac9,1",
  201. .matches = {
  202. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  203. DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
  204. },
  205. },
  206. { /* Handle problems with rebooting on the iMac10,1. */
  207. .callback = set_pci_reboot,
  208. .ident = "Apple iMac10,1",
  209. .matches = {
  210. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  211. DMI_MATCH(DMI_PRODUCT_NAME, "iMac10,1"),
  212. },
  213. },
  214. /* ASRock */
  215. { /* Handle problems with rebooting on ASRock Q1900DC-ITX */
  216. .callback = set_pci_reboot,
  217. .ident = "ASRock Q1900DC-ITX",
  218. .matches = {
  219. DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
  220. DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
  221. },
  222. },
  223. /* ASUS */
  224. { /* Handle problems with rebooting on ASUS P4S800 */
  225. .callback = set_bios_reboot,
  226. .ident = "ASUS P4S800",
  227. .matches = {
  228. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  229. DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
  230. },
  231. },
  232. { /* Handle problems with rebooting on ASUS EeeBook X205TA */
  233. .callback = set_acpi_reboot,
  234. .ident = "ASUS EeeBook X205TA",
  235. .matches = {
  236. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  237. DMI_MATCH(DMI_PRODUCT_NAME, "X205TA"),
  238. },
  239. },
  240. { /* Handle problems with rebooting on ASUS EeeBook X205TAW */
  241. .callback = set_acpi_reboot,
  242. .ident = "ASUS EeeBook X205TAW",
  243. .matches = {
  244. DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK COMPUTER INC."),
  245. DMI_MATCH(DMI_PRODUCT_NAME, "X205TAW"),
  246. },
  247. },
  248. /* Certec */
  249. { /* Handle problems with rebooting on Certec BPC600 */
  250. .callback = set_pci_reboot,
  251. .ident = "Certec BPC600",
  252. .matches = {
  253. DMI_MATCH(DMI_SYS_VENDOR, "Certec"),
  254. DMI_MATCH(DMI_PRODUCT_NAME, "BPC600"),
  255. },
  256. },
  257. /* Dell */
  258. { /* Handle problems with rebooting on Dell DXP061 */
  259. .callback = set_bios_reboot,
  260. .ident = "Dell DXP061",
  261. .matches = {
  262. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  263. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
  264. },
  265. },
  266. { /* Handle problems with rebooting on Dell E520's */
  267. .callback = set_bios_reboot,
  268. .ident = "Dell E520",
  269. .matches = {
  270. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  271. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
  272. },
  273. },
  274. { /* Handle problems with rebooting on the Latitude E5410. */
  275. .callback = set_pci_reboot,
  276. .ident = "Dell Latitude E5410",
  277. .matches = {
  278. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  279. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5410"),
  280. },
  281. },
  282. { /* Handle problems with rebooting on the Latitude E5420. */
  283. .callback = set_pci_reboot,
  284. .ident = "Dell Latitude E5420",
  285. .matches = {
  286. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  287. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
  288. },
  289. },
  290. { /* Handle problems with rebooting on the Latitude E6320. */
  291. .callback = set_pci_reboot,
  292. .ident = "Dell Latitude E6320",
  293. .matches = {
  294. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  295. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
  296. },
  297. },
  298. { /* Handle problems with rebooting on the Latitude E6420. */
  299. .callback = set_pci_reboot,
  300. .ident = "Dell Latitude E6420",
  301. .matches = {
  302. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  303. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
  304. },
  305. },
  306. { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
  307. .callback = set_bios_reboot,
  308. .ident = "Dell OptiPlex 330",
  309. .matches = {
  310. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  311. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
  312. DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
  313. },
  314. },
  315. { /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
  316. .callback = set_bios_reboot,
  317. .ident = "Dell OptiPlex 360",
  318. .matches = {
  319. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  320. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
  321. DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
  322. },
  323. },
  324. { /* Handle problems with rebooting on Dell Optiplex 745's SFF */
  325. .callback = set_bios_reboot,
  326. .ident = "Dell OptiPlex 745",
  327. .matches = {
  328. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  329. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  330. },
  331. },
  332. { /* Handle problems with rebooting on Dell Optiplex 745's DFF */
  333. .callback = set_bios_reboot,
  334. .ident = "Dell OptiPlex 745",
  335. .matches = {
  336. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  337. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  338. DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
  339. },
  340. },
  341. { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
  342. .callback = set_bios_reboot,
  343. .ident = "Dell OptiPlex 745",
  344. .matches = {
  345. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  346. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  347. DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
  348. },
  349. },
  350. { /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G */
  351. .callback = set_bios_reboot,
  352. .ident = "Dell OptiPlex 760",
  353. .matches = {
  354. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  355. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
  356. DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
  357. },
  358. },
  359. { /* Handle problems with rebooting on the OptiPlex 990. */
  360. .callback = set_pci_reboot,
  361. .ident = "Dell OptiPlex 990 BIOS A0x",
  362. .matches = {
  363. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  364. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
  365. DMI_MATCH(DMI_BIOS_VERSION, "A0"),
  366. },
  367. },
  368. { /* Handle problems with rebooting on Dell 300's */
  369. .callback = set_bios_reboot,
  370. .ident = "Dell PowerEdge 300",
  371. .matches = {
  372. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  373. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
  374. },
  375. },
  376. { /* Handle problems with rebooting on Dell 1300's */
  377. .callback = set_bios_reboot,
  378. .ident = "Dell PowerEdge 1300",
  379. .matches = {
  380. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  381. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
  382. },
  383. },
  384. { /* Handle problems with rebooting on Dell 2400's */
  385. .callback = set_bios_reboot,
  386. .ident = "Dell PowerEdge 2400",
  387. .matches = {
  388. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  389. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
  390. },
  391. },
  392. { /* Handle problems with rebooting on the Dell PowerEdge C6100. */
  393. .callback = set_pci_reboot,
  394. .ident = "Dell PowerEdge C6100",
  395. .matches = {
  396. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  397. DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
  398. },
  399. },
  400. { /* Handle problems with rebooting on the Precision M6600. */
  401. .callback = set_pci_reboot,
  402. .ident = "Dell Precision M6600",
  403. .matches = {
  404. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  405. DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
  406. },
  407. },
  408. { /* Handle problems with rebooting on Dell T5400's */
  409. .callback = set_bios_reboot,
  410. .ident = "Dell Precision T5400",
  411. .matches = {
  412. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  413. DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
  414. },
  415. },
  416. { /* Handle problems with rebooting on Dell T7400's */
  417. .callback = set_bios_reboot,
  418. .ident = "Dell Precision T7400",
  419. .matches = {
  420. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  421. DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
  422. },
  423. },
  424. { /* Handle problems with rebooting on Dell XPS710 */
  425. .callback = set_bios_reboot,
  426. .ident = "Dell XPS710",
  427. .matches = {
  428. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  429. DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
  430. },
  431. },
  432. { /* Handle problems with rebooting on Dell Optiplex 7450 AIO */
  433. .callback = set_acpi_reboot,
  434. .ident = "Dell OptiPlex 7450 AIO",
  435. .matches = {
  436. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  437. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 7450 AIO"),
  438. },
  439. },
  440. /* Hewlett-Packard */
  441. { /* Handle problems with rebooting on HP laptops */
  442. .callback = set_bios_reboot,
  443. .ident = "HP Compaq Laptop",
  444. .matches = {
  445. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  446. DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
  447. },
  448. },
  449. { /* PCIe Wifi card isn't detected after reboot otherwise */
  450. .callback = set_pci_reboot,
  451. .ident = "Zotac ZBOX CI327 nano",
  452. .matches = {
  453. DMI_MATCH(DMI_SYS_VENDOR, "NA"),
  454. DMI_MATCH(DMI_PRODUCT_NAME, "ZBOX-CI327NANO-GS-01"),
  455. },
  456. },
  457. /* Sony */
  458. { /* Handle problems with rebooting on Sony VGN-Z540N */
  459. .callback = set_bios_reboot,
  460. .ident = "Sony VGN-Z540N",
  461. .matches = {
  462. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  463. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
  464. },
  465. },
  466. { }
  467. };
  468. static int __init reboot_init(void)
  469. {
  470. int rv;
  471. /*
  472. * Only do the DMI check if reboot_type hasn't been overridden
  473. * on the command line
  474. */
  475. if (!reboot_default)
  476. return 0;
  477. /*
  478. * The DMI quirks table takes precedence. If no quirks entry
  479. * matches and the ACPI Hardware Reduced bit is set and EFI
  480. * runtime services are enabled, force EFI reboot.
  481. */
  482. rv = dmi_check_system(reboot_dmi_table);
  483. if (!rv && efi_reboot_required() && !efi_runtime_disabled())
  484. reboot_type = BOOT_EFI;
  485. return 0;
  486. }
  487. core_initcall(reboot_init);
  488. static inline void kb_wait(void)
  489. {
  490. int i;
  491. for (i = 0; i < 0x10000; i++) {
  492. if ((inb(0x64) & 0x02) == 0)
  493. break;
  494. udelay(2);
  495. }
  496. }
  497. static inline void nmi_shootdown_cpus_on_restart(void);
  498. #if IS_ENABLED(CONFIG_KVM_X86)
  499. /* RCU-protected callback to disable virtualization prior to reboot. */
  500. static cpu_emergency_virt_cb __rcu *cpu_emergency_virt_callback;
  501. void cpu_emergency_register_virt_callback(cpu_emergency_virt_cb *callback)
  502. {
  503. if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback)))
  504. return;
  505. rcu_assign_pointer(cpu_emergency_virt_callback, callback);
  506. }
  507. EXPORT_SYMBOL_GPL(cpu_emergency_register_virt_callback);
  508. void cpu_emergency_unregister_virt_callback(cpu_emergency_virt_cb *callback)
  509. {
  510. if (WARN_ON_ONCE(rcu_access_pointer(cpu_emergency_virt_callback) != callback))
  511. return;
  512. rcu_assign_pointer(cpu_emergency_virt_callback, NULL);
  513. synchronize_rcu();
  514. }
  515. EXPORT_SYMBOL_GPL(cpu_emergency_unregister_virt_callback);
  516. /*
  517. * Disable virtualization, i.e. VMX or SVM, to ensure INIT is recognized during
  518. * reboot. VMX blocks INIT if the CPU is post-VMXON, and SVM blocks INIT if
  519. * GIF=0, i.e. if the crash occurred between CLGI and STGI.
  520. */
  521. void cpu_emergency_disable_virtualization(void)
  522. {
  523. cpu_emergency_virt_cb *callback;
  524. /*
  525. * IRQs must be disabled as KVM enables virtualization in hardware via
  526. * function call IPIs, i.e. IRQs need to be disabled to guarantee
  527. * virtualization stays disabled.
  528. */
  529. lockdep_assert_irqs_disabled();
  530. rcu_read_lock();
  531. callback = rcu_dereference(cpu_emergency_virt_callback);
  532. if (callback)
  533. callback();
  534. rcu_read_unlock();
  535. }
  536. static void emergency_reboot_disable_virtualization(void)
  537. {
  538. local_irq_disable();
  539. /*
  540. * Disable virtualization on all CPUs before rebooting to avoid hanging
  541. * the system, as VMX and SVM block INIT when running in the host.
  542. *
  543. * We can't take any locks and we may be on an inconsistent state, so
  544. * use NMIs as IPIs to tell the other CPUs to disable VMX/SVM and halt.
  545. *
  546. * Do the NMI shootdown even if virtualization is off on _this_ CPU, as
  547. * other CPUs may have virtualization enabled.
  548. */
  549. if (rcu_access_pointer(cpu_emergency_virt_callback)) {
  550. /* Safely force _this_ CPU out of VMX/SVM operation. */
  551. cpu_emergency_disable_virtualization();
  552. /* Disable VMX/SVM and halt on other CPUs. */
  553. nmi_shootdown_cpus_on_restart();
  554. }
  555. }
  556. #else
  557. static void emergency_reboot_disable_virtualization(void) { }
  558. #endif /* CONFIG_KVM_X86 */
  559. void __attribute__((weak)) mach_reboot_fixups(void)
  560. {
  561. }
  562. /*
  563. * To the best of our knowledge Windows compatible x86 hardware expects
  564. * the following on reboot:
  565. *
  566. * 1) If the FADT has the ACPI reboot register flag set, try it
  567. * 2) If still alive, write to the keyboard controller
  568. * 3) If still alive, write to the ACPI reboot register again
  569. * 4) If still alive, write to the keyboard controller again
  570. * 5) If still alive, call the EFI runtime service to reboot
  571. * 6) If no EFI runtime service, call the BIOS to do a reboot
  572. *
  573. * We default to following the same pattern. We also have
  574. * two other reboot methods: 'triple fault' and 'PCI', which
  575. * can be triggered via the reboot= kernel boot option or
  576. * via quirks.
  577. *
  578. * This means that this function can never return, it can misbehave
  579. * by not rebooting properly and hanging.
  580. */
  581. static void native_machine_emergency_restart(void)
  582. {
  583. int i;
  584. int attempt = 0;
  585. int orig_reboot_type = reboot_type;
  586. unsigned short mode;
  587. if (reboot_emergency)
  588. emergency_reboot_disable_virtualization();
  589. tboot_shutdown(TB_SHUTDOWN_REBOOT);
  590. /* Tell the BIOS if we want cold or warm reboot */
  591. mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
  592. *((unsigned short *)__va(0x472)) = mode;
  593. /*
  594. * If an EFI capsule has been registered with the firmware then
  595. * override the reboot= parameter.
  596. */
  597. if (efi_capsule_pending(NULL)) {
  598. pr_info("EFI capsule is pending, forcing EFI reboot.\n");
  599. reboot_type = BOOT_EFI;
  600. }
  601. for (;;) {
  602. /* Could also try the reset bit in the Hammer NB */
  603. switch (reboot_type) {
  604. case BOOT_ACPI:
  605. acpi_reboot();
  606. reboot_type = BOOT_KBD;
  607. break;
  608. case BOOT_KBD:
  609. mach_reboot_fixups(); /* For board specific fixups */
  610. for (i = 0; i < 10; i++) {
  611. kb_wait();
  612. udelay(50);
  613. outb(0xfe, 0x64); /* Pulse reset low */
  614. udelay(50);
  615. }
  616. if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
  617. attempt = 1;
  618. reboot_type = BOOT_ACPI;
  619. } else {
  620. reboot_type = BOOT_EFI;
  621. }
  622. break;
  623. case BOOT_EFI:
  624. efi_reboot(reboot_mode, NULL);
  625. reboot_type = BOOT_BIOS;
  626. break;
  627. case BOOT_BIOS:
  628. machine_real_restart(MRR_BIOS);
  629. /* We're probably dead after this, but... */
  630. reboot_type = BOOT_CF9_SAFE;
  631. break;
  632. case BOOT_CF9_FORCE:
  633. port_cf9_safe = true;
  634. fallthrough;
  635. case BOOT_CF9_SAFE:
  636. if (port_cf9_safe) {
  637. u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E;
  638. u8 cf9 = inb(0xcf9) & ~reboot_code;
  639. outb(cf9|2, 0xcf9); /* Request hard reset */
  640. udelay(50);
  641. /* Actually do the reset */
  642. outb(cf9|reboot_code, 0xcf9);
  643. udelay(50);
  644. }
  645. reboot_type = BOOT_TRIPLE;
  646. break;
  647. case BOOT_TRIPLE:
  648. idt_invalidate();
  649. __asm__ __volatile__("int3");
  650. /* We're probably dead after this, but... */
  651. reboot_type = BOOT_KBD;
  652. break;
  653. }
  654. }
  655. }
  656. void native_machine_shutdown(void)
  657. {
  658. /*
  659. * Call enc_kexec_begin() while all CPUs are still active and
  660. * interrupts are enabled. This will allow all in-flight memory
  661. * conversions to finish cleanly.
  662. */
  663. if (kexec_in_progress)
  664. x86_platform.guest.enc_kexec_begin();
  665. /* Stop the cpus and apics */
  666. #ifdef CONFIG_X86_IO_APIC
  667. /*
  668. * Disabling IO APIC before local APIC is a workaround for
  669. * erratum AVR31 in "Intel Atom Processor C2000 Product Family
  670. * Specification Update". In this situation, interrupts that target
  671. * a Logical Processor whose Local APIC is either in the process of
  672. * being hardware disabled or software disabled are neither delivered
  673. * nor discarded. When this erratum occurs, the processor may hang.
  674. *
  675. * Even without the erratum, it still makes sense to quiet IO APIC
  676. * before disabling Local APIC.
  677. */
  678. clear_IO_APIC();
  679. #endif
  680. #ifdef CONFIG_SMP
  681. /*
  682. * Stop all of the others. Also disable the local irq to
  683. * not receive the per-cpu timer interrupt which may trigger
  684. * scheduler's load balance.
  685. */
  686. local_irq_disable();
  687. stop_other_cpus();
  688. #endif
  689. lapic_shutdown();
  690. restore_boot_irq_mode();
  691. #ifdef CONFIG_HPET_TIMER
  692. hpet_disable();
  693. #endif
  694. #ifdef CONFIG_X86_64
  695. x86_platform.iommu_shutdown();
  696. #endif
  697. if (kexec_in_progress)
  698. x86_platform.guest.enc_kexec_finish();
  699. }
  700. static void __machine_emergency_restart(int emergency)
  701. {
  702. reboot_emergency = emergency;
  703. machine_ops.emergency_restart();
  704. }
  705. static void native_machine_restart(char *__unused)
  706. {
  707. pr_notice("machine restart\n");
  708. if (!reboot_force)
  709. machine_shutdown();
  710. __machine_emergency_restart(0);
  711. }
  712. static void native_machine_halt(void)
  713. {
  714. /* Stop other cpus and apics */
  715. machine_shutdown();
  716. tboot_shutdown(TB_SHUTDOWN_HALT);
  717. stop_this_cpu(NULL);
  718. }
  719. static void native_machine_power_off(void)
  720. {
  721. if (kernel_can_power_off()) {
  722. if (!reboot_force)
  723. machine_shutdown();
  724. do_kernel_power_off();
  725. }
  726. /* A fallback in case there is no PM info available */
  727. tboot_shutdown(TB_SHUTDOWN_HALT);
  728. }
  729. struct machine_ops machine_ops __ro_after_init = {
  730. .power_off = native_machine_power_off,
  731. .shutdown = native_machine_shutdown,
  732. .emergency_restart = native_machine_emergency_restart,
  733. .restart = native_machine_restart,
  734. .halt = native_machine_halt,
  735. #ifdef CONFIG_CRASH_DUMP
  736. .crash_shutdown = native_machine_crash_shutdown,
  737. #endif
  738. };
  739. void machine_power_off(void)
  740. {
  741. machine_ops.power_off();
  742. }
  743. void machine_shutdown(void)
  744. {
  745. machine_ops.shutdown();
  746. }
  747. void machine_emergency_restart(void)
  748. {
  749. __machine_emergency_restart(1);
  750. }
  751. void machine_restart(char *cmd)
  752. {
  753. machine_ops.restart(cmd);
  754. }
  755. void machine_halt(void)
  756. {
  757. machine_ops.halt();
  758. }
  759. #ifdef CONFIG_CRASH_DUMP
  760. void machine_crash_shutdown(struct pt_regs *regs)
  761. {
  762. machine_ops.crash_shutdown(regs);
  763. }
  764. #endif
  765. /* This is the CPU performing the emergency shutdown work. */
  766. int crashing_cpu = -1;
  767. #if defined(CONFIG_SMP)
  768. static nmi_shootdown_cb shootdown_callback;
  769. static atomic_t waiting_for_crash_ipi;
  770. static int crash_ipi_issued;
  771. static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
  772. {
  773. int cpu;
  774. cpu = raw_smp_processor_id();
  775. /*
  776. * Don't do anything if this handler is invoked on crashing cpu.
  777. * Otherwise, system will completely hang. Crashing cpu can get
  778. * an NMI if system was initially booted with nmi_watchdog parameter.
  779. */
  780. if (cpu == crashing_cpu)
  781. return NMI_HANDLED;
  782. local_irq_disable();
  783. if (shootdown_callback)
  784. shootdown_callback(cpu, regs);
  785. /*
  786. * Prepare the CPU for reboot _after_ invoking the callback so that the
  787. * callback can safely use virtualization instructions, e.g. VMCLEAR.
  788. */
  789. cpu_emergency_disable_virtualization();
  790. atomic_dec(&waiting_for_crash_ipi);
  791. if (smp_ops.stop_this_cpu) {
  792. smp_ops.stop_this_cpu();
  793. BUG();
  794. }
  795. /* Assume hlt works */
  796. halt();
  797. for (;;)
  798. cpu_relax();
  799. return NMI_HANDLED;
  800. }
  801. /**
  802. * nmi_shootdown_cpus - Stop other CPUs via NMI
  803. * @callback: Optional callback to be invoked from the NMI handler
  804. *
  805. * The NMI handler on the remote CPUs invokes @callback, if not
  806. * NULL, first and then disables virtualization to ensure that
  807. * INIT is recognized during reboot.
  808. *
  809. * nmi_shootdown_cpus() can only be invoked once. After the first
  810. * invocation all other CPUs are stuck in crash_nmi_callback() and
  811. * cannot respond to a second NMI.
  812. */
  813. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  814. {
  815. unsigned long msecs;
  816. local_irq_disable();
  817. /*
  818. * Avoid certain doom if a shootdown already occurred; re-registering
  819. * the NMI handler will cause list corruption, modifying the callback
  820. * will do who knows what, etc...
  821. */
  822. if (WARN_ON_ONCE(crash_ipi_issued))
  823. return;
  824. /* Make a note of crashing cpu. Will be used in NMI callback. */
  825. crashing_cpu = safe_smp_processor_id();
  826. shootdown_callback = callback;
  827. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  828. /*
  829. * Set emergency handler to preempt other handlers.
  830. */
  831. set_emergency_nmi_handler(NMI_LOCAL, crash_nmi_callback);
  832. apic_send_IPI_allbutself(NMI_VECTOR);
  833. /* Kick CPUs looping in NMI context. */
  834. WRITE_ONCE(crash_ipi_issued, 1);
  835. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  836. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  837. mdelay(1);
  838. msecs--;
  839. }
  840. /*
  841. * Leave the nmi callback set, shootdown is a one-time thing. Clearing
  842. * the callback could result in a NULL pointer dereference if a CPU
  843. * (finally) responds after the timeout expires.
  844. */
  845. }
  846. static inline void nmi_shootdown_cpus_on_restart(void)
  847. {
  848. if (!crash_ipi_issued)
  849. nmi_shootdown_cpus(NULL);
  850. }
  851. /*
  852. * Check if the crash dumping IPI got issued and if so, call its callback
  853. * directly. This function is used when we have already been in NMI handler.
  854. * It doesn't return.
  855. */
  856. void run_crash_ipi_callback(struct pt_regs *regs)
  857. {
  858. if (crash_ipi_issued)
  859. crash_nmi_callback(0, regs);
  860. }
  861. /* Override the weak function in kernel/panic.c */
  862. void __noreturn nmi_panic_self_stop(struct pt_regs *regs)
  863. {
  864. while (1) {
  865. /* If no CPU is preparing crash dump, we simply loop here. */
  866. run_crash_ipi_callback(regs);
  867. cpu_relax();
  868. }
  869. }
  870. #else /* !CONFIG_SMP */
  871. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  872. {
  873. /* No other CPUs to shoot down */
  874. }
  875. static inline void nmi_shootdown_cpus_on_restart(void) { }
  876. void run_crash_ipi_callback(struct pt_regs *regs)
  877. {
  878. }
  879. #endif