reboot.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  1. #include <linux/module.h>
  2. #include <linux/reboot.h>
  3. #include <linux/init.h>
  4. #include <linux/pm.h>
  5. #include <linux/efi.h>
  6. #include <linux/dmi.h>
  7. #include <linux/sched.h>
  8. #include <linux/tboot.h>
  9. #include <linux/delay.h>
  10. #include <acpi/reboot.h>
  11. #include <asm/io.h>
  12. #include <asm/apic.h>
  13. #include <asm/desc.h>
  14. #include <asm/hpet.h>
  15. #include <asm/pgtable.h>
  16. #include <asm/proto.h>
  17. #include <asm/reboot_fixups.h>
  18. #include <asm/reboot.h>
  19. #include <asm/pci_x86.h>
  20. #include <asm/virtext.h>
  21. #include <asm/cpu.h>
  22. #include <asm/nmi.h>
  23. #ifdef CONFIG_X86_32
  24. # include <linux/ctype.h>
  25. # include <linux/mc146818rtc.h>
  26. #else
  27. # include <asm/x86_init.h>
  28. #endif
  29. /*
  30. * Power off function, if any
  31. */
  32. void (*pm_power_off)(void);
  33. EXPORT_SYMBOL(pm_power_off);
  34. static const struct desc_ptr no_idt = {};
  35. static int reboot_mode;
  36. enum reboot_type reboot_type = BOOT_ACPI;
  37. int reboot_force;
  38. /* This variable is used privately to keep track of whether or not
  39. * reboot_type is still set to its default value (i.e., reboot= hasn't
  40. * been set on the command line). This is needed so that we can
  41. * suppress DMI scanning for reboot quirks. Without it, it's
  42. * impossible to override a faulty reboot quirk without recompiling.
  43. */
  44. static int reboot_default = 1;
  45. #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
  46. static int reboot_cpu = -1;
  47. #endif
  48. /* This is set if we need to go through the 'emergency' path.
  49. * When machine_emergency_restart() is called, we may be on
  50. * an inconsistent state and won't be able to do a clean cleanup
  51. */
  52. static int reboot_emergency;
  53. /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
  54. bool port_cf9_safe = false;
  55. /* reboot=b[ios] | s[mp] | t[riple] | k[bd] | e[fi] [, [w]arm | [c]old] | p[ci]
  56. warm Don't set the cold reboot flag
  57. cold Set the cold reboot flag
  58. bios Reboot by jumping through the BIOS (only for X86_32)
  59. smp Reboot by executing reset on BSP or other CPU (only for X86_32)
  60. triple Force a triple fault (init)
  61. kbd Use the keyboard controller. cold reset (default)
  62. acpi Use the RESET_REG in the FADT
  63. efi Use efi reset_system runtime service
  64. pci Use the so-called "PCI reset register", CF9
  65. force Avoid anything that could hang.
  66. */
  67. static int __init reboot_setup(char *str)
  68. {
  69. for (;;) {
  70. /* Having anything passed on the command line via
  71. * reboot= will cause us to disable DMI checking
  72. * below.
  73. */
  74. reboot_default = 0;
  75. switch (*str) {
  76. case 'w':
  77. reboot_mode = 0x1234;
  78. break;
  79. case 'c':
  80. reboot_mode = 0;
  81. break;
  82. #ifdef CONFIG_X86_32
  83. #ifdef CONFIG_SMP
  84. case 's':
  85. if (isdigit(*(str+1))) {
  86. reboot_cpu = (int) (*(str+1) - '0');
  87. if (isdigit(*(str+2)))
  88. reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
  89. }
  90. /* we will leave sorting out the final value
  91. when we are ready to reboot, since we might not
  92. have detected BSP APIC ID or smp_num_cpu */
  93. break;
  94. #endif /* CONFIG_SMP */
  95. case 'b':
  96. #endif
  97. case 'a':
  98. case 'k':
  99. case 't':
  100. case 'e':
  101. case 'p':
  102. reboot_type = *str;
  103. break;
  104. case 'f':
  105. reboot_force = 1;
  106. break;
  107. }
  108. str = strchr(str, ',');
  109. if (str)
  110. str++;
  111. else
  112. break;
  113. }
  114. return 1;
  115. }
  116. __setup("reboot=", reboot_setup);
  117. #ifdef CONFIG_X86_32
  118. /*
  119. * Reboot options and system auto-detection code provided by
  120. * Dell Inc. so their systems "just work". :-)
  121. */
  122. /*
  123. * Some machines require the "reboot=b" or "reboot=k" commandline options,
  124. * this quirk makes that automatic.
  125. */
  126. static int __init set_bios_reboot(const struct dmi_system_id *d)
  127. {
  128. if (reboot_type != BOOT_BIOS) {
  129. reboot_type = BOOT_BIOS;
  130. printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
  131. }
  132. return 0;
  133. }
  134. static int __init set_kbd_reboot(const struct dmi_system_id *d)
  135. {
  136. if (reboot_type != BOOT_KBD) {
  137. reboot_type = BOOT_KBD;
  138. printk(KERN_INFO "%s series board detected. Selecting KBD-method for reboot.\n", d->ident);
  139. }
  140. return 0;
  141. }
  142. static struct dmi_system_id __initdata reboot_dmi_table[] = {
  143. { /* Handle problems with rebooting on Dell E520's */
  144. .callback = set_bios_reboot,
  145. .ident = "Dell E520",
  146. .matches = {
  147. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  148. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
  149. },
  150. },
  151. { /* Handle problems with rebooting on Dell 1300's */
  152. .callback = set_bios_reboot,
  153. .ident = "Dell PowerEdge 1300",
  154. .matches = {
  155. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  156. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
  157. },
  158. },
  159. { /* Handle problems with rebooting on Dell 300's */
  160. .callback = set_bios_reboot,
  161. .ident = "Dell PowerEdge 300",
  162. .matches = {
  163. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  164. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
  165. },
  166. },
  167. { /* Handle problems with rebooting on Dell Optiplex 745's SFF*/
  168. .callback = set_bios_reboot,
  169. .ident = "Dell OptiPlex 745",
  170. .matches = {
  171. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  172. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  173. },
  174. },
  175. { /* Handle problems with rebooting on Dell Optiplex 745's DFF*/
  176. .callback = set_bios_reboot,
  177. .ident = "Dell OptiPlex 745",
  178. .matches = {
  179. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  180. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  181. DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
  182. },
  183. },
  184. { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
  185. .callback = set_bios_reboot,
  186. .ident = "Dell OptiPlex 745",
  187. .matches = {
  188. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  189. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  190. DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
  191. },
  192. },
  193. { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
  194. .callback = set_bios_reboot,
  195. .ident = "Dell OptiPlex 330",
  196. .matches = {
  197. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  198. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
  199. DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
  200. },
  201. },
  202. { /* Handle problems with rebooting on Dell Optiplex 360 with 0T656F */
  203. .callback = set_bios_reboot,
  204. .ident = "Dell OptiPlex 360",
  205. .matches = {
  206. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  207. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 360"),
  208. DMI_MATCH(DMI_BOARD_NAME, "0T656F"),
  209. },
  210. },
  211. { /* Handle problems with rebooting on Dell OptiPlex 760 with 0G919G*/
  212. .callback = set_bios_reboot,
  213. .ident = "Dell OptiPlex 760",
  214. .matches = {
  215. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  216. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 760"),
  217. DMI_MATCH(DMI_BOARD_NAME, "0G919G"),
  218. },
  219. },
  220. { /* Handle problems with rebooting on Dell 2400's */
  221. .callback = set_bios_reboot,
  222. .ident = "Dell PowerEdge 2400",
  223. .matches = {
  224. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  225. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
  226. },
  227. },
  228. { /* Handle problems with rebooting on Dell T5400's */
  229. .callback = set_bios_reboot,
  230. .ident = "Dell Precision T5400",
  231. .matches = {
  232. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  233. DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
  234. },
  235. },
  236. { /* Handle problems with rebooting on Dell T7400's */
  237. .callback = set_bios_reboot,
  238. .ident = "Dell Precision T7400",
  239. .matches = {
  240. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  241. DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T7400"),
  242. },
  243. },
  244. { /* Handle problems with rebooting on HP laptops */
  245. .callback = set_bios_reboot,
  246. .ident = "HP Compaq Laptop",
  247. .matches = {
  248. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  249. DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
  250. },
  251. },
  252. { /* Handle problems with rebooting on Dell XPS710 */
  253. .callback = set_bios_reboot,
  254. .ident = "Dell XPS710",
  255. .matches = {
  256. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  257. DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
  258. },
  259. },
  260. { /* Handle problems with rebooting on Dell DXP061 */
  261. .callback = set_bios_reboot,
  262. .ident = "Dell DXP061",
  263. .matches = {
  264. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  265. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
  266. },
  267. },
  268. { /* Handle problems with rebooting on Sony VGN-Z540N */
  269. .callback = set_bios_reboot,
  270. .ident = "Sony VGN-Z540N",
  271. .matches = {
  272. DMI_MATCH(DMI_SYS_VENDOR, "Sony Corporation"),
  273. DMI_MATCH(DMI_PRODUCT_NAME, "VGN-Z540N"),
  274. },
  275. },
  276. { /* Handle problems with rebooting on CompuLab SBC-FITPC2 */
  277. .callback = set_bios_reboot,
  278. .ident = "CompuLab SBC-FITPC2",
  279. .matches = {
  280. DMI_MATCH(DMI_SYS_VENDOR, "CompuLab"),
  281. DMI_MATCH(DMI_PRODUCT_NAME, "SBC-FITPC2"),
  282. },
  283. },
  284. { /* Handle problems with rebooting on ASUS P4S800 */
  285. .callback = set_bios_reboot,
  286. .ident = "ASUS P4S800",
  287. .matches = {
  288. DMI_MATCH(DMI_BOARD_VENDOR, "ASUSTeK Computer INC."),
  289. DMI_MATCH(DMI_BOARD_NAME, "P4S800"),
  290. },
  291. },
  292. { /* Handle reboot issue on Acer Aspire one */
  293. .callback = set_kbd_reboot,
  294. .ident = "Acer Aspire One A110",
  295. .matches = {
  296. DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
  297. DMI_MATCH(DMI_PRODUCT_NAME, "AOA110"),
  298. },
  299. },
  300. { }
  301. };
  302. static int __init reboot_init(void)
  303. {
  304. /* Only do the DMI check if reboot_type hasn't been overridden
  305. * on the command line
  306. */
  307. if (reboot_default) {
  308. dmi_check_system(reboot_dmi_table);
  309. }
  310. return 0;
  311. }
  312. core_initcall(reboot_init);
  313. extern const unsigned char machine_real_restart_asm[];
  314. extern const u64 machine_real_restart_gdt[3];
  315. void machine_real_restart(unsigned int type)
  316. {
  317. void *restart_va;
  318. unsigned long restart_pa;
  319. void (*restart_lowmem)(unsigned int);
  320. u64 *lowmem_gdt;
  321. local_irq_disable();
  322. /* Write zero to CMOS register number 0x0f, which the BIOS POST
  323. routine will recognize as telling it to do a proper reboot. (Well
  324. that's what this book in front of me says -- it may only apply to
  325. the Phoenix BIOS though, it's not clear). At the same time,
  326. disable NMIs by setting the top bit in the CMOS address register,
  327. as we're about to do peculiar things to the CPU. I'm not sure if
  328. `outb_p' is needed instead of just `outb'. Use it to be on the
  329. safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
  330. */
  331. spin_lock(&rtc_lock);
  332. CMOS_WRITE(0x00, 0x8f);
  333. spin_unlock(&rtc_lock);
  334. /*
  335. * Switch back to the initial page table.
  336. */
  337. load_cr3(initial_page_table);
  338. /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
  339. this on booting to tell it to "Bypass memory test (also warm
  340. boot)". This seems like a fairly standard thing that gets set by
  341. REBOOT.COM programs, and the previous reset routine did this
  342. too. */
  343. *((unsigned short *)0x472) = reboot_mode;
  344. /* Patch the GDT in the low memory trampoline */
  345. lowmem_gdt = TRAMPOLINE_SYM(machine_real_restart_gdt);
  346. restart_va = TRAMPOLINE_SYM(machine_real_restart_asm);
  347. restart_pa = virt_to_phys(restart_va);
  348. restart_lowmem = (void (*)(unsigned int))restart_pa;
  349. /* GDT[0]: GDT self-pointer */
  350. lowmem_gdt[0] =
  351. (u64)(sizeof(machine_real_restart_gdt) - 1) +
  352. ((u64)virt_to_phys(lowmem_gdt) << 16);
  353. /* GDT[1]: 64K real mode code segment */
  354. lowmem_gdt[1] =
  355. GDT_ENTRY(0x009b, restart_pa, 0xffff);
  356. /* Jump to the identity-mapped low memory code */
  357. restart_lowmem(type);
  358. }
  359. #ifdef CONFIG_APM_MODULE
  360. EXPORT_SYMBOL(machine_real_restart);
  361. #endif
  362. #endif /* CONFIG_X86_32 */
  363. /*
  364. * Some Apple MacBook and MacBookPro's needs reboot=p to be able to reboot
  365. */
  366. static int __init set_pci_reboot(const struct dmi_system_id *d)
  367. {
  368. if (reboot_type != BOOT_CF9) {
  369. reboot_type = BOOT_CF9;
  370. printk(KERN_INFO "%s series board detected. "
  371. "Selecting PCI-method for reboots.\n", d->ident);
  372. }
  373. return 0;
  374. }
  375. static struct dmi_system_id __initdata pci_reboot_dmi_table[] = {
  376. { /* Handle problems with rebooting on Apple MacBook5 */
  377. .callback = set_pci_reboot,
  378. .ident = "Apple MacBook5",
  379. .matches = {
  380. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  381. DMI_MATCH(DMI_PRODUCT_NAME, "MacBook5"),
  382. },
  383. },
  384. { /* Handle problems with rebooting on Apple MacBookPro5 */
  385. .callback = set_pci_reboot,
  386. .ident = "Apple MacBookPro5",
  387. .matches = {
  388. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  389. DMI_MATCH(DMI_PRODUCT_NAME, "MacBookPro5"),
  390. },
  391. },
  392. { /* Handle problems with rebooting on Apple Macmini3,1 */
  393. .callback = set_pci_reboot,
  394. .ident = "Apple Macmini3,1",
  395. .matches = {
  396. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  397. DMI_MATCH(DMI_PRODUCT_NAME, "Macmini3,1"),
  398. },
  399. },
  400. { /* Handle problems with rebooting on the iMac9,1. */
  401. .callback = set_pci_reboot,
  402. .ident = "Apple iMac9,1",
  403. .matches = {
  404. DMI_MATCH(DMI_SYS_VENDOR, "Apple Inc."),
  405. DMI_MATCH(DMI_PRODUCT_NAME, "iMac9,1"),
  406. },
  407. },
  408. /* ASRock */
  409. { /* Handle problems with rebooting on ASRock Q1900DC-ITX */
  410. .callback = set_pci_reboot,
  411. .ident = "ASRock Q1900DC-ITX",
  412. .matches = {
  413. DMI_MATCH(DMI_BOARD_VENDOR, "ASRock"),
  414. DMI_MATCH(DMI_BOARD_NAME, "Q1900DC-ITX"),
  415. },
  416. },
  417. { /* Handle problems with rebooting on the Latitude E6320. */
  418. .callback = set_pci_reboot,
  419. .ident = "Dell Latitude E6320",
  420. .matches = {
  421. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  422. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6320"),
  423. },
  424. },
  425. { /* Handle problems with rebooting on the Latitude E5420. */
  426. .callback = set_pci_reboot,
  427. .ident = "Dell Latitude E5420",
  428. .matches = {
  429. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  430. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E5420"),
  431. },
  432. },
  433. { /* Handle problems with rebooting on the Latitude E6420. */
  434. .callback = set_pci_reboot,
  435. .ident = "Dell Latitude E6420",
  436. .matches = {
  437. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  438. DMI_MATCH(DMI_PRODUCT_NAME, "Latitude E6420"),
  439. },
  440. },
  441. { /* Handle problems with rebooting on the OptiPlex 990. */
  442. .callback = set_pci_reboot,
  443. .ident = "Dell OptiPlex 990",
  444. .matches = {
  445. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  446. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 990"),
  447. },
  448. },
  449. { /* Handle problems with rebooting on the Precision M6600. */
  450. .callback = set_pci_reboot,
  451. .ident = "Dell OptiPlex 990",
  452. .matches = {
  453. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  454. DMI_MATCH(DMI_PRODUCT_NAME, "Precision M6600"),
  455. },
  456. },
  457. { /* Handle problems with rebooting on the Dell PowerEdge C6100. */
  458. .callback = set_pci_reboot,
  459. .ident = "Dell PowerEdge C6100",
  460. .matches = {
  461. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  462. DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
  463. },
  464. },
  465. { /* Some C6100 machines were shipped with vendor being 'Dell'. */
  466. .callback = set_pci_reboot,
  467. .ident = "Dell PowerEdge C6100",
  468. .matches = {
  469. DMI_MATCH(DMI_SYS_VENDOR, "Dell"),
  470. DMI_MATCH(DMI_PRODUCT_NAME, "C6100"),
  471. },
  472. },
  473. { }
  474. };
  475. static int __init pci_reboot_init(void)
  476. {
  477. /* Only do the DMI check if reboot_type hasn't been overridden
  478. * on the command line
  479. */
  480. if (reboot_default) {
  481. dmi_check_system(pci_reboot_dmi_table);
  482. }
  483. return 0;
  484. }
  485. core_initcall(pci_reboot_init);
  486. static inline void kb_wait(void)
  487. {
  488. int i;
  489. for (i = 0; i < 0x10000; i++) {
  490. if ((inb(0x64) & 0x02) == 0)
  491. break;
  492. udelay(2);
  493. }
  494. }
  495. static void vmxoff_nmi(int cpu, struct pt_regs *regs)
  496. {
  497. cpu_emergency_vmxoff();
  498. }
  499. /* Use NMIs as IPIs to tell all CPUs to disable virtualization
  500. */
  501. static void emergency_vmx_disable_all(void)
  502. {
  503. /* Just make sure we won't change CPUs while doing this */
  504. local_irq_disable();
  505. /* We need to disable VMX on all CPUs before rebooting, otherwise
  506. * we risk hanging up the machine, because the CPU ignore INIT
  507. * signals when VMX is enabled.
  508. *
  509. * We can't take any locks and we may be on an inconsistent
  510. * state, so we use NMIs as IPIs to tell the other CPUs to disable
  511. * VMX and halt.
  512. *
  513. * For safety, we will avoid running the nmi_shootdown_cpus()
  514. * stuff unnecessarily, but we don't have a way to check
  515. * if other CPUs have VMX enabled. So we will call it only if the
  516. * CPU we are running on has VMX enabled.
  517. *
  518. * We will miss cases where VMX is not enabled on all CPUs. This
  519. * shouldn't do much harm because KVM always enable VMX on all
  520. * CPUs anyway. But we can miss it on the small window where KVM
  521. * is still enabling VMX.
  522. */
  523. if (cpu_has_vmx() && cpu_vmx_enabled()) {
  524. /* Disable VMX on this CPU.
  525. */
  526. cpu_vmxoff();
  527. /* Halt and disable VMX on the other CPUs */
  528. nmi_shootdown_cpus(vmxoff_nmi);
  529. }
  530. }
  531. void __attribute__((weak)) mach_reboot_fixups(void)
  532. {
  533. }
  534. /*
  535. * Windows compatible x86 hardware expects the following on reboot:
  536. *
  537. * 1) If the FADT has the ACPI reboot register flag set, try it
  538. * 2) If still alive, write to the keyboard controller
  539. * 3) If still alive, write to the ACPI reboot register again
  540. * 4) If still alive, write to the keyboard controller again
  541. *
  542. * If the machine is still alive at this stage, it gives up. We default to
  543. * following the same pattern, except that if we're still alive after (4) we'll
  544. * try to force a triple fault and then cycle between hitting the keyboard
  545. * controller and doing that
  546. */
  547. static void native_machine_emergency_restart(void)
  548. {
  549. int i;
  550. int attempt = 0;
  551. int orig_reboot_type = reboot_type;
  552. if (reboot_emergency)
  553. emergency_vmx_disable_all();
  554. tboot_shutdown(TB_SHUTDOWN_REBOOT);
  555. /* Tell the BIOS if we want cold or warm reboot */
  556. *((unsigned short *)__va(0x472)) = reboot_mode;
  557. for (;;) {
  558. /* Could also try the reset bit in the Hammer NB */
  559. switch (reboot_type) {
  560. case BOOT_KBD:
  561. mach_reboot_fixups(); /* for board specific fixups */
  562. for (i = 0; i < 10; i++) {
  563. kb_wait();
  564. udelay(50);
  565. outb(0xfe, 0x64); /* pulse reset low */
  566. udelay(50);
  567. }
  568. if (attempt == 0 && orig_reboot_type == BOOT_ACPI) {
  569. attempt = 1;
  570. reboot_type = BOOT_ACPI;
  571. } else {
  572. reboot_type = BOOT_TRIPLE;
  573. }
  574. break;
  575. case BOOT_TRIPLE:
  576. load_idt(&no_idt);
  577. __asm__ __volatile__("int3");
  578. reboot_type = BOOT_KBD;
  579. break;
  580. #ifdef CONFIG_X86_32
  581. case BOOT_BIOS:
  582. machine_real_restart(MRR_BIOS);
  583. reboot_type = BOOT_KBD;
  584. break;
  585. #endif
  586. case BOOT_ACPI:
  587. acpi_reboot();
  588. reboot_type = BOOT_KBD;
  589. break;
  590. case BOOT_EFI:
  591. if (efi_enabled(EFI_RUNTIME_SERVICES))
  592. efi.reset_system(reboot_mode ?
  593. EFI_RESET_WARM :
  594. EFI_RESET_COLD,
  595. EFI_SUCCESS, 0, NULL);
  596. reboot_type = BOOT_KBD;
  597. break;
  598. case BOOT_CF9:
  599. port_cf9_safe = true;
  600. /* fall through */
  601. case BOOT_CF9_COND:
  602. if (port_cf9_safe) {
  603. u8 cf9 = inb(0xcf9) & ~6;
  604. outb(cf9|2, 0xcf9); /* Request hard reset */
  605. udelay(50);
  606. outb(cf9|6, 0xcf9); /* Actually do the reset */
  607. udelay(50);
  608. }
  609. reboot_type = BOOT_KBD;
  610. break;
  611. }
  612. }
  613. }
  614. void native_machine_shutdown(void)
  615. {
  616. /* Stop the cpus and apics */
  617. #ifdef CONFIG_SMP
  618. /* The boot cpu is always logical cpu 0 */
  619. int reboot_cpu_id = 0;
  620. #endif
  621. #ifdef CONFIG_X86_IO_APIC
  622. disable_IO_APIC();
  623. #endif
  624. #ifdef CONFIG_SMP
  625. #ifdef CONFIG_X86_32
  626. /* See if there has been given a command line override */
  627. if ((reboot_cpu != -1) && (reboot_cpu < nr_cpu_ids) &&
  628. cpu_online(reboot_cpu))
  629. reboot_cpu_id = reboot_cpu;
  630. #endif
  631. /* Make certain the cpu I'm about to reboot on is online */
  632. if (!cpu_online(reboot_cpu_id))
  633. reboot_cpu_id = smp_processor_id();
  634. /* Make certain I only run on the appropriate processor */
  635. set_cpus_allowed_ptr(current, cpumask_of(reboot_cpu_id));
  636. /*
  637. * O.K Now that I'm on the appropriate processor, stop all of the
  638. * others. Also disable the local irq to not receive the per-cpu
  639. * timer interrupt which may trigger scheduler's load balance.
  640. */
  641. local_irq_disable();
  642. stop_other_cpus();
  643. #endif
  644. lapic_shutdown();
  645. #ifdef CONFIG_HPET_TIMER
  646. hpet_disable();
  647. #endif
  648. #ifdef CONFIG_X86_64
  649. x86_platform.iommu_shutdown();
  650. #endif
  651. }
  652. static void __machine_emergency_restart(int emergency)
  653. {
  654. reboot_emergency = emergency;
  655. machine_ops.emergency_restart();
  656. }
  657. static void native_machine_restart(char *__unused)
  658. {
  659. printk("machine restart\n");
  660. if (!reboot_force)
  661. machine_shutdown();
  662. __machine_emergency_restart(0);
  663. }
  664. static void native_machine_halt(void)
  665. {
  666. /* stop other cpus and apics */
  667. machine_shutdown();
  668. tboot_shutdown(TB_SHUTDOWN_HALT);
  669. /* stop this cpu */
  670. stop_this_cpu(NULL);
  671. }
  672. static void native_machine_power_off(void)
  673. {
  674. if (pm_power_off) {
  675. if (!reboot_force)
  676. machine_shutdown();
  677. pm_power_off();
  678. }
  679. /* a fallback in case there is no PM info available */
  680. tboot_shutdown(TB_SHUTDOWN_HALT);
  681. }
  682. struct machine_ops machine_ops = {
  683. .power_off = native_machine_power_off,
  684. .shutdown = native_machine_shutdown,
  685. .emergency_restart = native_machine_emergency_restart,
  686. .restart = native_machine_restart,
  687. .halt = native_machine_halt,
  688. #ifdef CONFIG_KEXEC
  689. .crash_shutdown = native_machine_crash_shutdown,
  690. #endif
  691. };
  692. void machine_power_off(void)
  693. {
  694. machine_ops.power_off();
  695. }
  696. void machine_shutdown(void)
  697. {
  698. machine_ops.shutdown();
  699. }
  700. void machine_emergency_restart(void)
  701. {
  702. __machine_emergency_restart(1);
  703. }
  704. void machine_restart(char *cmd)
  705. {
  706. machine_ops.restart(cmd);
  707. }
  708. void machine_halt(void)
  709. {
  710. machine_ops.halt();
  711. }
  712. #ifdef CONFIG_KEXEC
  713. void machine_crash_shutdown(struct pt_regs *regs)
  714. {
  715. machine_ops.crash_shutdown(regs);
  716. }
  717. #endif
  718. #if defined(CONFIG_SMP)
  719. /* This keeps a track of which one is crashing cpu. */
  720. static int crashing_cpu;
  721. static nmi_shootdown_cb shootdown_callback;
  722. static atomic_t waiting_for_crash_ipi;
  723. static int crash_nmi_callback(unsigned int val, struct pt_regs *regs)
  724. {
  725. int cpu;
  726. cpu = raw_smp_processor_id();
  727. /* Don't do anything if this handler is invoked on crashing cpu.
  728. * Otherwise, system will completely hang. Crashing cpu can get
  729. * an NMI if system was initially booted with nmi_watchdog parameter.
  730. */
  731. if (cpu == crashing_cpu)
  732. return NMI_HANDLED;
  733. local_irq_disable();
  734. shootdown_callback(cpu, regs);
  735. atomic_dec(&waiting_for_crash_ipi);
  736. /* Assume hlt works */
  737. halt();
  738. for (;;)
  739. cpu_relax();
  740. return NMI_HANDLED;
  741. }
  742. static void smp_send_nmi_allbutself(void)
  743. {
  744. apic->send_IPI_allbutself(NMI_VECTOR);
  745. }
  746. /* Halt all other CPUs, calling the specified function on each of them
  747. *
  748. * This function can be used to halt all other CPUs on crash
  749. * or emergency reboot time. The function passed as parameter
  750. * will be called inside a NMI handler on all CPUs.
  751. */
  752. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  753. {
  754. unsigned long msecs;
  755. local_irq_disable();
  756. /* Make a note of crashing cpu. Will be used in NMI callback.*/
  757. crashing_cpu = safe_smp_processor_id();
  758. shootdown_callback = callback;
  759. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  760. /* Would it be better to replace the trap vector here? */
  761. if (register_nmi_handler(NMI_LOCAL, crash_nmi_callback,
  762. NMI_FLAG_FIRST, "crash"))
  763. return; /* return what? */
  764. /* Ensure the new callback function is set before sending
  765. * out the NMI
  766. */
  767. wmb();
  768. smp_send_nmi_allbutself();
  769. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  770. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  771. mdelay(1);
  772. msecs--;
  773. }
  774. /* Leave the nmi callback set */
  775. }
  776. #else /* !CONFIG_SMP */
  777. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  778. {
  779. /* No other CPUs to shoot down */
  780. }
  781. #endif