reboot.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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 <acpi/reboot.h>
  15. #include <asm/io.h>
  16. #include <asm/apic.h>
  17. #include <asm/io_apic.h>
  18. #include <asm/desc.h>
  19. #include <asm/hpet.h>
  20. #include <asm/proto.h>
  21. #include <asm/reboot_fixups.h>
  22. #include <asm/reboot.h>
  23. #include <asm/pci_x86.h>
  24. #include <asm/virtext.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. static void emergency_reboot_disable_virtualization(void)
  499. {
  500. /* Just make sure we won't change CPUs while doing this */
  501. local_irq_disable();
  502. /*
  503. * Disable virtualization on all CPUs before rebooting to avoid hanging
  504. * the system, as VMX and SVM block INIT when running in the host.
  505. *
  506. * We can't take any locks and we may be on an inconsistent state, so
  507. * use NMIs as IPIs to tell the other CPUs to disable VMX/SVM and halt.
  508. *
  509. * Do the NMI shootdown even if virtualization is off on _this_ CPU, as
  510. * other CPUs may have virtualization enabled.
  511. */
  512. if (cpu_has_vmx() || cpu_has_svm(NULL)) {
  513. /* Safely force _this_ CPU out of VMX/SVM operation. */
  514. cpu_emergency_disable_virtualization();
  515. /* Disable VMX/SVM and halt on other CPUs. */
  516. nmi_shootdown_cpus_on_restart();
  517. }
  518. }
  519. void __attribute__((weak)) mach_reboot_fixups(void)
  520. {
  521. }
  522. /*
  523. * To the best of our knowledge Windows compatible x86 hardware expects
  524. * the following on reboot:
  525. *
  526. * 1) If the FADT has the ACPI reboot register flag set, try it
  527. * 2) If still alive, write to the keyboard controller
  528. * 3) If still alive, write to the ACPI reboot register again
  529. * 4) If still alive, write to the keyboard controller again
  530. * 5) If still alive, call the EFI runtime service to reboot
  531. * 6) If no EFI runtime service, call the BIOS to do a reboot
  532. *
  533. * We default to following the same pattern. We also have
  534. * two other reboot methods: 'triple fault' and 'PCI', which
  535. * can be triggered via the reboot= kernel boot option or
  536. * via quirks.
  537. *
  538. * This means that this function can never return, it can misbehave
  539. * by not rebooting properly and hanging.
  540. */
  541. static void native_machine_emergency_restart(void)
  542. {
  543. int i;
  544. int attempt = 0;
  545. int orig_reboot_type = reboot_type;
  546. unsigned short mode;
  547. if (reboot_emergency)
  548. emergency_reboot_disable_virtualization();
  549. tboot_shutdown(TB_SHUTDOWN_REBOOT);
  550. /* Tell the BIOS if we want cold or warm reboot */
  551. mode = reboot_mode == REBOOT_WARM ? 0x1234 : 0;
  552. *((unsigned short *)__va(0x472)) = mode;
  553. /*
  554. * If an EFI capsule has been registered with the firmware then
  555. * override the reboot= parameter.
  556. */
  557. if (efi_capsule_pending(NULL)) {
  558. pr_info("EFI capsule is pending, forcing EFI reboot.\n");
  559. reboot_type = BOOT_EFI;
  560. }
  561. for (;;) {
  562. /* Could also try the reset bit in the Hammer NB */
  563. switch (reboot_type) {
  564. case BOOT_ACPI:
  565. acpi_reboot();
  566. reboot_type = BOOT_KBD;
  567. break;
  568. case BOOT_KBD:
  569. mach_reboot_fixups(); /* For board specific fixups */
  570. for (i = 0; i < 10; i++) {
  571. kb_wait();
  572. udelay(50);
  573. outb(0xfe, 0x64); /* Pulse reset low */
  574. udelay(50);
  575. }
  576. if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
  577. attempt = 1;
  578. reboot_type = BOOT_ACPI;
  579. } else {
  580. reboot_type = BOOT_EFI;
  581. }
  582. break;
  583. case BOOT_EFI:
  584. efi_reboot(reboot_mode, NULL);
  585. reboot_type = BOOT_BIOS;
  586. break;
  587. case BOOT_BIOS:
  588. machine_real_restart(MRR_BIOS);
  589. /* We're probably dead after this, but... */
  590. reboot_type = BOOT_CF9_SAFE;
  591. break;
  592. case BOOT_CF9_FORCE:
  593. port_cf9_safe = true;
  594. fallthrough;
  595. case BOOT_CF9_SAFE:
  596. if (port_cf9_safe) {
  597. u8 reboot_code = reboot_mode == REBOOT_WARM ? 0x06 : 0x0E;
  598. u8 cf9 = inb(0xcf9) & ~reboot_code;
  599. outb(cf9|2, 0xcf9); /* Request hard reset */
  600. udelay(50);
  601. /* Actually do the reset */
  602. outb(cf9|reboot_code, 0xcf9);
  603. udelay(50);
  604. }
  605. reboot_type = BOOT_TRIPLE;
  606. break;
  607. case BOOT_TRIPLE:
  608. idt_invalidate();
  609. __asm__ __volatile__("int3");
  610. /* We're probably dead after this, but... */
  611. reboot_type = BOOT_KBD;
  612. break;
  613. }
  614. }
  615. }
  616. void native_machine_shutdown(void)
  617. {
  618. /* Stop the cpus and apics */
  619. #ifdef CONFIG_X86_IO_APIC
  620. /*
  621. * Disabling IO APIC before local APIC is a workaround for
  622. * erratum AVR31 in "Intel Atom Processor C2000 Product Family
  623. * Specification Update". In this situation, interrupts that target
  624. * a Logical Processor whose Local APIC is either in the process of
  625. * being hardware disabled or software disabled are neither delivered
  626. * nor discarded. When this erratum occurs, the processor may hang.
  627. *
  628. * Even without the erratum, it still makes sense to quiet IO APIC
  629. * before disabling Local APIC.
  630. */
  631. clear_IO_APIC();
  632. #endif
  633. #ifdef CONFIG_SMP
  634. /*
  635. * Stop all of the others. Also disable the local irq to
  636. * not receive the per-cpu timer interrupt which may trigger
  637. * scheduler's load balance.
  638. */
  639. local_irq_disable();
  640. stop_other_cpus();
  641. #endif
  642. lapic_shutdown();
  643. restore_boot_irq_mode();
  644. #ifdef CONFIG_HPET_TIMER
  645. hpet_disable();
  646. #endif
  647. #ifdef CONFIG_X86_64
  648. x86_platform.iommu_shutdown();
  649. #endif
  650. }
  651. static void __machine_emergency_restart(int emergency)
  652. {
  653. reboot_emergency = emergency;
  654. machine_ops.emergency_restart();
  655. }
  656. static void native_machine_restart(char *__unused)
  657. {
  658. pr_notice("machine restart\n");
  659. if (!reboot_force)
  660. machine_shutdown();
  661. __machine_emergency_restart(0);
  662. }
  663. static void native_machine_halt(void)
  664. {
  665. /* Stop other cpus and apics */
  666. machine_shutdown();
  667. tboot_shutdown(TB_SHUTDOWN_HALT);
  668. stop_this_cpu(NULL);
  669. }
  670. static void native_machine_power_off(void)
  671. {
  672. if (kernel_can_power_off()) {
  673. if (!reboot_force)
  674. machine_shutdown();
  675. do_kernel_power_off();
  676. }
  677. /* A fallback in case there is no PM info available */
  678. tboot_shutdown(TB_SHUTDOWN_HALT);
  679. }
  680. struct machine_ops machine_ops __ro_after_init = {
  681. .power_off = native_machine_power_off,
  682. .shutdown = native_machine_shutdown,
  683. .emergency_restart = native_machine_emergency_restart,
  684. .restart = native_machine_restart,
  685. .halt = native_machine_halt,
  686. #ifdef CONFIG_KEXEC_CORE
  687. .crash_shutdown = native_machine_crash_shutdown,
  688. #endif
  689. };
  690. void machine_power_off(void)
  691. {
  692. machine_ops.power_off();
  693. }
  694. void machine_shutdown(void)
  695. {
  696. machine_ops.shutdown();
  697. }
  698. void machine_emergency_restart(void)
  699. {
  700. __machine_emergency_restart(1);
  701. }
  702. void machine_restart(char *cmd)
  703. {
  704. machine_ops.restart(cmd);
  705. }
  706. void machine_halt(void)
  707. {
  708. machine_ops.halt();
  709. }
  710. #ifdef CONFIG_KEXEC_CORE
  711. void machine_crash_shutdown(struct pt_regs *regs)
  712. {
  713. machine_ops.crash_shutdown(regs);
  714. }
  715. #endif
  716. /*
  717. * This is used to VMCLEAR all VMCSs loaded on the
  718. * processor. And when loading kvm_intel module, the
  719. * callback function pointer will be assigned.
  720. *
  721. * protected by rcu.
  722. */
  723. crash_vmclear_fn __rcu *crash_vmclear_loaded_vmcss;
  724. EXPORT_SYMBOL_GPL(crash_vmclear_loaded_vmcss);
  725. static inline void cpu_crash_vmclear_loaded_vmcss(void)
  726. {
  727. crash_vmclear_fn *do_vmclear_operation = NULL;
  728. rcu_read_lock();
  729. do_vmclear_operation = rcu_dereference(crash_vmclear_loaded_vmcss);
  730. if (do_vmclear_operation)
  731. do_vmclear_operation();
  732. rcu_read_unlock();
  733. }
  734. /* This is the CPU performing the emergency shutdown work. */
  735. int crashing_cpu = -1;
  736. /*
  737. * Disable virtualization, i.e. VMX or SVM, to ensure INIT is recognized during
  738. * reboot. VMX blocks INIT if the CPU is post-VMXON, and SVM blocks INIT if
  739. * GIF=0, i.e. if the crash occurred between CLGI and STGI.
  740. */
  741. void cpu_emergency_disable_virtualization(void)
  742. {
  743. cpu_crash_vmclear_loaded_vmcss();
  744. cpu_emergency_vmxoff();
  745. cpu_emergency_svm_disable();
  746. }
  747. #if defined(CONFIG_SMP)
  748. static nmi_shootdown_cb shootdown_callback;
  749. static atomic_t waiting_for_crash_ipi;
  750. static int crash_ipi_issued;
  751. static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
  752. {
  753. int cpu;
  754. cpu = raw_smp_processor_id();
  755. /*
  756. * Don't do anything if this handler is invoked on crashing cpu.
  757. * Otherwise, system will completely hang. Crashing cpu can get
  758. * an NMI if system was initially booted with nmi_watchdog parameter.
  759. */
  760. if (cpu == crashing_cpu)
  761. return NMI_HANDLED;
  762. local_irq_disable();
  763. if (shootdown_callback)
  764. shootdown_callback(cpu, regs);
  765. /*
  766. * Prepare the CPU for reboot _after_ invoking the callback so that the
  767. * callback can safely use virtualization instructions, e.g. VMCLEAR.
  768. */
  769. cpu_emergency_disable_virtualization();
  770. atomic_dec(&waiting_for_crash_ipi);
  771. /* Assume hlt works */
  772. halt();
  773. for (;;)
  774. cpu_relax();
  775. return NMI_HANDLED;
  776. }
  777. /**
  778. * nmi_shootdown_cpus - Stop other CPUs via NMI
  779. * @callback: Optional callback to be invoked from the NMI handler
  780. *
  781. * The NMI handler on the remote CPUs invokes @callback, if not
  782. * NULL, first and then disables virtualization to ensure that
  783. * INIT is recognized during reboot.
  784. *
  785. * nmi_shootdown_cpus() can only be invoked once. After the first
  786. * invocation all other CPUs are stuck in crash_nmi_callback() and
  787. * cannot respond to a second NMI.
  788. */
  789. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  790. {
  791. unsigned long msecs;
  792. local_irq_disable();
  793. /*
  794. * Avoid certain doom if a shootdown already occurred; re-registering
  795. * the NMI handler will cause list corruption, modifying the callback
  796. * will do who knows what, etc...
  797. */
  798. if (WARN_ON_ONCE(crash_ipi_issued))
  799. return;
  800. /* Make a note of crashing cpu. Will be used in NMI callback. */
  801. crashing_cpu = safe_smp_processor_id();
  802. shootdown_callback = callback;
  803. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  804. /* Would it be better to replace the trap vector here? */
  805. if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
  806. NMI_FLAG_FIRST, "crash"))
  807. return; /* Return what? */
  808. /*
  809. * Ensure the new callback function is set before sending
  810. * out the NMI
  811. */
  812. wmb();
  813. apic_send_IPI_allbutself(NMI_VECTOR);
  814. /* Kick CPUs looping in NMI context. */
  815. WRITE_ONCE(crash_ipi_issued, 1);
  816. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  817. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  818. mdelay(1);
  819. msecs--;
  820. }
  821. /*
  822. * Leave the nmi callback set, shootdown is a one-time thing. Clearing
  823. * the callback could result in a NULL pointer dereference if a CPU
  824. * (finally) responds after the timeout expires.
  825. */
  826. }
  827. static inline void nmi_shootdown_cpus_on_restart(void)
  828. {
  829. if (!crash_ipi_issued)
  830. nmi_shootdown_cpus(NULL);
  831. }
  832. /*
  833. * Check if the crash dumping IPI got issued and if so, call its callback
  834. * directly. This function is used when we have already been in NMI handler.
  835. * It doesn't return.
  836. */
  837. void run_crash_ipi_callback(struct pt_regs *regs)
  838. {
  839. if (crash_ipi_issued)
  840. crash_nmi_callback(0, regs);
  841. }
  842. /* Override the weak function in kernel/panic.c */
  843. void nmi_panic_self_stop(struct pt_regs *regs)
  844. {
  845. while (1) {
  846. /* If no CPU is preparing crash dump, we simply loop here. */
  847. run_crash_ipi_callback(regs);
  848. cpu_relax();
  849. }
  850. }
  851. #else /* !CONFIG_SMP */
  852. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  853. {
  854. /* No other CPUs to shoot down */
  855. }
  856. static inline void nmi_shootdown_cpus_on_restart(void) { }
  857. void run_crash_ipi_callback(struct pt_regs *regs)
  858. {
  859. }
  860. #endif