reboot.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  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 <acpi/reboot.h>
  7. #include <asm/io.h>
  8. #include <asm/apic.h>
  9. #include <asm/desc.h>
  10. #include <asm/hpet.h>
  11. #include <asm/pgtable.h>
  12. #include <asm/proto.h>
  13. #include <asm/reboot_fixups.h>
  14. #include <asm/reboot.h>
  15. #include <asm/pci_x86.h>
  16. #include <asm/virtext.h>
  17. #include <asm/cpu.h>
  18. #ifdef CONFIG_X86_32
  19. # include <linux/dmi.h>
  20. # include <linux/ctype.h>
  21. # include <linux/mc146818rtc.h>
  22. #else
  23. # include <asm/iommu.h>
  24. #endif
  25. /*
  26. * Power off function, if any
  27. */
  28. void (*pm_power_off)(void);
  29. EXPORT_SYMBOL(pm_power_off);
  30. static const struct desc_ptr no_idt = {};
  31. static int reboot_mode;
  32. enum reboot_type reboot_type = BOOT_KBD;
  33. int reboot_force;
  34. #if defined(CONFIG_X86_32) && defined(CONFIG_SMP)
  35. static int reboot_cpu = -1;
  36. #endif
  37. /* This is set if we need to go through the 'emergency' path.
  38. * When machine_emergency_restart() is called, we may be on
  39. * an inconsistent state and won't be able to do a clean cleanup
  40. */
  41. static int reboot_emergency;
  42. /* This is set by the PCI code if either type 1 or type 2 PCI is detected */
  43. bool port_cf9_safe = false;
  44. /* reboot=b[ios] | s[mp] | t[riple] | k[bd] | e[fi] [, [w]arm | [c]old] | p[ci]
  45. warm Don't set the cold reboot flag
  46. cold Set the cold reboot flag
  47. bios Reboot by jumping through the BIOS (only for X86_32)
  48. smp Reboot by executing reset on BSP or other CPU (only for X86_32)
  49. triple Force a triple fault (init)
  50. kbd Use the keyboard controller. cold reset (default)
  51. acpi Use the RESET_REG in the FADT
  52. efi Use efi reset_system runtime service
  53. pci Use the so-called "PCI reset register", CF9
  54. force Avoid anything that could hang.
  55. */
  56. static int __init reboot_setup(char *str)
  57. {
  58. for (;;) {
  59. switch (*str) {
  60. case 'w':
  61. reboot_mode = 0x1234;
  62. break;
  63. case 'c':
  64. reboot_mode = 0;
  65. break;
  66. #ifdef CONFIG_X86_32
  67. #ifdef CONFIG_SMP
  68. case 's':
  69. if (isdigit(*(str+1))) {
  70. reboot_cpu = (int) (*(str+1) - '0');
  71. if (isdigit(*(str+2)))
  72. reboot_cpu = reboot_cpu*10 + (int)(*(str+2) - '0');
  73. }
  74. /* we will leave sorting out the final value
  75. when we are ready to reboot, since we might not
  76. have set up boot_cpu_id or smp_num_cpu */
  77. break;
  78. #endif /* CONFIG_SMP */
  79. case 'b':
  80. #endif
  81. case 'a':
  82. case 'k':
  83. case 't':
  84. case 'e':
  85. case 'p':
  86. reboot_type = *str;
  87. break;
  88. case 'f':
  89. reboot_force = 1;
  90. break;
  91. }
  92. str = strchr(str, ',');
  93. if (str)
  94. str++;
  95. else
  96. break;
  97. }
  98. return 1;
  99. }
  100. __setup("reboot=", reboot_setup);
  101. #ifdef CONFIG_X86_32
  102. /*
  103. * Reboot options and system auto-detection code provided by
  104. * Dell Inc. so their systems "just work". :-)
  105. */
  106. /*
  107. * Some machines require the "reboot=b" commandline option,
  108. * this quirk makes that automatic.
  109. */
  110. static int __init set_bios_reboot(const struct dmi_system_id *d)
  111. {
  112. if (reboot_type != BOOT_BIOS) {
  113. reboot_type = BOOT_BIOS;
  114. printk(KERN_INFO "%s series board detected. Selecting BIOS-method for reboots.\n", d->ident);
  115. }
  116. return 0;
  117. }
  118. static struct dmi_system_id __initdata reboot_dmi_table[] = {
  119. { /* Handle problems with rebooting on Dell E520's */
  120. .callback = set_bios_reboot,
  121. .ident = "Dell E520",
  122. .matches = {
  123. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  124. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DM061"),
  125. },
  126. },
  127. { /* Handle problems with rebooting on Dell 1300's */
  128. .callback = set_bios_reboot,
  129. .ident = "Dell PowerEdge 1300",
  130. .matches = {
  131. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  132. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 1300/"),
  133. },
  134. },
  135. { /* Handle problems with rebooting on Dell 300's */
  136. .callback = set_bios_reboot,
  137. .ident = "Dell PowerEdge 300",
  138. .matches = {
  139. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  140. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 300/"),
  141. },
  142. },
  143. { /* Handle problems with rebooting on Dell Optiplex 745's SFF*/
  144. .callback = set_bios_reboot,
  145. .ident = "Dell OptiPlex 745",
  146. .matches = {
  147. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  148. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  149. },
  150. },
  151. { /* Handle problems with rebooting on Dell Optiplex 745's DFF*/
  152. .callback = set_bios_reboot,
  153. .ident = "Dell OptiPlex 745",
  154. .matches = {
  155. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  156. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  157. DMI_MATCH(DMI_BOARD_NAME, "0MM599"),
  158. },
  159. },
  160. { /* Handle problems with rebooting on Dell Optiplex 745 with 0KW626 */
  161. .callback = set_bios_reboot,
  162. .ident = "Dell OptiPlex 745",
  163. .matches = {
  164. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  165. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 745"),
  166. DMI_MATCH(DMI_BOARD_NAME, "0KW626"),
  167. },
  168. },
  169. { /* Handle problems with rebooting on Dell Optiplex 330 with 0KP561 */
  170. .callback = set_bios_reboot,
  171. .ident = "Dell OptiPlex 330",
  172. .matches = {
  173. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  174. DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 330"),
  175. DMI_MATCH(DMI_BOARD_NAME, "0KP561"),
  176. },
  177. },
  178. { /* Handle problems with rebooting on Dell 2400's */
  179. .callback = set_bios_reboot,
  180. .ident = "Dell PowerEdge 2400",
  181. .matches = {
  182. DMI_MATCH(DMI_SYS_VENDOR, "Dell Computer Corporation"),
  183. DMI_MATCH(DMI_PRODUCT_NAME, "PowerEdge 2400"),
  184. },
  185. },
  186. { /* Handle problems with rebooting on Dell T5400's */
  187. .callback = set_bios_reboot,
  188. .ident = "Dell Precision T5400",
  189. .matches = {
  190. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  191. DMI_MATCH(DMI_PRODUCT_NAME, "Precision WorkStation T5400"),
  192. },
  193. },
  194. { /* Handle problems with rebooting on HP laptops */
  195. .callback = set_bios_reboot,
  196. .ident = "HP Compaq Laptop",
  197. .matches = {
  198. DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
  199. DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq"),
  200. },
  201. },
  202. { /* Handle problems with rebooting on Dell XPS710 */
  203. .callback = set_bios_reboot,
  204. .ident = "Dell XPS710",
  205. .matches = {
  206. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  207. DMI_MATCH(DMI_PRODUCT_NAME, "Dell XPS710"),
  208. },
  209. },
  210. { /* Handle problems with rebooting on Dell DXP061 */
  211. .callback = set_bios_reboot,
  212. .ident = "Dell DXP061",
  213. .matches = {
  214. DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
  215. DMI_MATCH(DMI_PRODUCT_NAME, "Dell DXP061"),
  216. },
  217. },
  218. { }
  219. };
  220. static int __init reboot_init(void)
  221. {
  222. dmi_check_system(reboot_dmi_table);
  223. return 0;
  224. }
  225. core_initcall(reboot_init);
  226. /* The following code and data reboots the machine by switching to real
  227. mode and jumping to the BIOS reset entry point, as if the CPU has
  228. really been reset. The previous version asked the keyboard
  229. controller to pulse the CPU reset line, which is more thorough, but
  230. doesn't work with at least one type of 486 motherboard. It is easy
  231. to stop this code working; hence the copious comments. */
  232. static const unsigned long long
  233. real_mode_gdt_entries [3] =
  234. {
  235. 0x0000000000000000ULL, /* Null descriptor */
  236. 0x00009b000000ffffULL, /* 16-bit real-mode 64k code at 0x00000000 */
  237. 0x000093000100ffffULL /* 16-bit real-mode 64k data at 0x00000100 */
  238. };
  239. static const struct desc_ptr
  240. real_mode_gdt = { sizeof (real_mode_gdt_entries) - 1, (long)real_mode_gdt_entries },
  241. real_mode_idt = { 0x3ff, 0 };
  242. /* This is 16-bit protected mode code to disable paging and the cache,
  243. switch to real mode and jump to the BIOS reset code.
  244. The instruction that switches to real mode by writing to CR0 must be
  245. followed immediately by a far jump instruction, which set CS to a
  246. valid value for real mode, and flushes the prefetch queue to avoid
  247. running instructions that have already been decoded in protected
  248. mode.
  249. Clears all the flags except ET, especially PG (paging), PE
  250. (protected-mode enable) and TS (task switch for coprocessor state
  251. save). Flushes the TLB after paging has been disabled. Sets CD and
  252. NW, to disable the cache on a 486, and invalidates the cache. This
  253. is more like the state of a 486 after reset. I don't know if
  254. something else should be done for other chips.
  255. More could be done here to set up the registers as if a CPU reset had
  256. occurred; hopefully real BIOSs don't assume much. */
  257. static const unsigned char real_mode_switch [] =
  258. {
  259. 0x66, 0x0f, 0x20, 0xc0, /* movl %cr0,%eax */
  260. 0x66, 0x83, 0xe0, 0x11, /* andl $0x00000011,%eax */
  261. 0x66, 0x0d, 0x00, 0x00, 0x00, 0x60, /* orl $0x60000000,%eax */
  262. 0x66, 0x0f, 0x22, 0xc0, /* movl %eax,%cr0 */
  263. 0x66, 0x0f, 0x22, 0xd8, /* movl %eax,%cr3 */
  264. 0x66, 0x0f, 0x20, 0xc3, /* movl %cr0,%ebx */
  265. 0x66, 0x81, 0xe3, 0x00, 0x00, 0x00, 0x60, /* andl $0x60000000,%ebx */
  266. 0x74, 0x02, /* jz f */
  267. 0x0f, 0x09, /* wbinvd */
  268. 0x24, 0x10, /* f: andb $0x10,al */
  269. 0x66, 0x0f, 0x22, 0xc0 /* movl %eax,%cr0 */
  270. };
  271. static const unsigned char jump_to_bios [] =
  272. {
  273. 0xea, 0x00, 0x00, 0xff, 0xff /* ljmp $0xffff,$0x0000 */
  274. };
  275. /*
  276. * Switch to real mode and then execute the code
  277. * specified by the code and length parameters.
  278. * We assume that length will aways be less that 100!
  279. */
  280. void machine_real_restart(const unsigned char *code, int length)
  281. {
  282. local_irq_disable();
  283. /* Write zero to CMOS register number 0x0f, which the BIOS POST
  284. routine will recognize as telling it to do a proper reboot. (Well
  285. that's what this book in front of me says -- it may only apply to
  286. the Phoenix BIOS though, it's not clear). At the same time,
  287. disable NMIs by setting the top bit in the CMOS address register,
  288. as we're about to do peculiar things to the CPU. I'm not sure if
  289. `outb_p' is needed instead of just `outb'. Use it to be on the
  290. safe side. (Yes, CMOS_WRITE does outb_p's. - Paul G.)
  291. */
  292. spin_lock(&rtc_lock);
  293. CMOS_WRITE(0x00, 0x8f);
  294. spin_unlock(&rtc_lock);
  295. /* Remap the kernel at virtual address zero, as well as offset zero
  296. from the kernel segment. This assumes the kernel segment starts at
  297. virtual address PAGE_OFFSET. */
  298. memcpy(swapper_pg_dir, swapper_pg_dir + KERNEL_PGD_BOUNDARY,
  299. sizeof(swapper_pg_dir [0]) * KERNEL_PGD_PTRS);
  300. /*
  301. * Use `swapper_pg_dir' as our page directory.
  302. */
  303. load_cr3(swapper_pg_dir);
  304. /* Write 0x1234 to absolute memory location 0x472. The BIOS reads
  305. this on booting to tell it to "Bypass memory test (also warm
  306. boot)". This seems like a fairly standard thing that gets set by
  307. REBOOT.COM programs, and the previous reset routine did this
  308. too. */
  309. *((unsigned short *)0x472) = reboot_mode;
  310. /* For the switch to real mode, copy some code to low memory. It has
  311. to be in the first 64k because it is running in 16-bit mode, and it
  312. has to have the same physical and virtual address, because it turns
  313. off paging. Copy it near the end of the first page, out of the way
  314. of BIOS variables. */
  315. memcpy((void *)(0x1000 - sizeof(real_mode_switch) - 100),
  316. real_mode_switch, sizeof (real_mode_switch));
  317. memcpy((void *)(0x1000 - 100), code, length);
  318. /* Set up the IDT for real mode. */
  319. load_idt(&real_mode_idt);
  320. /* Set up a GDT from which we can load segment descriptors for real
  321. mode. The GDT is not used in real mode; it is just needed here to
  322. prepare the descriptors. */
  323. load_gdt(&real_mode_gdt);
  324. /* Load the data segment registers, and thus the descriptors ready for
  325. real mode. The base address of each segment is 0x100, 16 times the
  326. selector value being loaded here. This is so that the segment
  327. registers don't have to be reloaded after switching to real mode:
  328. the values are consistent for real mode operation already. */
  329. __asm__ __volatile__ ("movl $0x0010,%%eax\n"
  330. "\tmovl %%eax,%%ds\n"
  331. "\tmovl %%eax,%%es\n"
  332. "\tmovl %%eax,%%fs\n"
  333. "\tmovl %%eax,%%gs\n"
  334. "\tmovl %%eax,%%ss" : : : "eax");
  335. /* Jump to the 16-bit code that we copied earlier. It disables paging
  336. and the cache, switches to real mode, and jumps to the BIOS reset
  337. entry point. */
  338. __asm__ __volatile__ ("ljmp $0x0008,%0"
  339. :
  340. : "i" ((void *)(0x1000 - sizeof (real_mode_switch) - 100)));
  341. }
  342. #ifdef CONFIG_APM_MODULE
  343. EXPORT_SYMBOL(machine_real_restart);
  344. #endif
  345. #endif /* CONFIG_X86_32 */
  346. static inline void kb_wait(void)
  347. {
  348. int i;
  349. for (i = 0; i < 0x10000; i++) {
  350. if ((inb(0x64) & 0x02) == 0)
  351. break;
  352. udelay(2);
  353. }
  354. }
  355. static void vmxoff_nmi(int cpu, struct die_args *args)
  356. {
  357. cpu_emergency_vmxoff();
  358. }
  359. /* Use NMIs as IPIs to tell all CPUs to disable virtualization
  360. */
  361. static void emergency_vmx_disable_all(void)
  362. {
  363. /* Just make sure we won't change CPUs while doing this */
  364. local_irq_disable();
  365. /* We need to disable VMX on all CPUs before rebooting, otherwise
  366. * we risk hanging up the machine, because the CPU ignore INIT
  367. * signals when VMX is enabled.
  368. *
  369. * We can't take any locks and we may be on an inconsistent
  370. * state, so we use NMIs as IPIs to tell the other CPUs to disable
  371. * VMX and halt.
  372. *
  373. * For safety, we will avoid running the nmi_shootdown_cpus()
  374. * stuff unnecessarily, but we don't have a way to check
  375. * if other CPUs have VMX enabled. So we will call it only if the
  376. * CPU we are running on has VMX enabled.
  377. *
  378. * We will miss cases where VMX is not enabled on all CPUs. This
  379. * shouldn't do much harm because KVM always enable VMX on all
  380. * CPUs anyway. But we can miss it on the small window where KVM
  381. * is still enabling VMX.
  382. */
  383. if (cpu_has_vmx() && cpu_vmx_enabled()) {
  384. /* Disable VMX on this CPU.
  385. */
  386. cpu_vmxoff();
  387. /* Halt and disable VMX on the other CPUs */
  388. nmi_shootdown_cpus(vmxoff_nmi);
  389. }
  390. }
  391. void __attribute__((weak)) mach_reboot_fixups(void)
  392. {
  393. }
  394. static void native_machine_emergency_restart(void)
  395. {
  396. int i;
  397. if (reboot_emergency)
  398. emergency_vmx_disable_all();
  399. /* Tell the BIOS if we want cold or warm reboot */
  400. *((unsigned short *)__va(0x472)) = reboot_mode;
  401. for (;;) {
  402. /* Could also try the reset bit in the Hammer NB */
  403. switch (reboot_type) {
  404. case BOOT_KBD:
  405. mach_reboot_fixups(); /* for board specific fixups */
  406. for (i = 0; i < 10; i++) {
  407. kb_wait();
  408. udelay(50);
  409. outb(0xfe, 0x64); /* pulse reset low */
  410. udelay(50);
  411. }
  412. case BOOT_TRIPLE:
  413. load_idt(&no_idt);
  414. __asm__ __volatile__("int3");
  415. reboot_type = BOOT_KBD;
  416. break;
  417. #ifdef CONFIG_X86_32
  418. case BOOT_BIOS:
  419. machine_real_restart(jump_to_bios, sizeof(jump_to_bios));
  420. reboot_type = BOOT_KBD;
  421. break;
  422. #endif
  423. case BOOT_ACPI:
  424. acpi_reboot();
  425. reboot_type = BOOT_KBD;
  426. break;
  427. case BOOT_EFI:
  428. if (efi_enabled)
  429. efi.reset_system(reboot_mode ?
  430. EFI_RESET_WARM :
  431. EFI_RESET_COLD,
  432. EFI_SUCCESS, 0, NULL);
  433. reboot_type = BOOT_KBD;
  434. break;
  435. case BOOT_CF9:
  436. port_cf9_safe = true;
  437. /* fall through */
  438. case BOOT_CF9_COND:
  439. if (port_cf9_safe) {
  440. u8 cf9 = inb(0xcf9) & ~6;
  441. outb(cf9|2, 0xcf9); /* Request hard reset */
  442. udelay(50);
  443. outb(cf9|6, 0xcf9); /* Actually do the reset */
  444. udelay(50);
  445. }
  446. reboot_type = BOOT_KBD;
  447. break;
  448. }
  449. }
  450. }
  451. void native_machine_shutdown(void)
  452. {
  453. /* Stop the cpus and apics */
  454. #ifdef CONFIG_SMP
  455. /* The boot cpu is always logical cpu 0 */
  456. int reboot_cpu_id = 0;
  457. #ifdef CONFIG_X86_32
  458. /* See if there has been given a command line override */
  459. if ((reboot_cpu != -1) && (reboot_cpu < nr_cpu_ids) &&
  460. cpu_online(reboot_cpu))
  461. reboot_cpu_id = reboot_cpu;
  462. #endif
  463. /* Make certain the cpu I'm about to reboot on is online */
  464. if (!cpu_online(reboot_cpu_id))
  465. reboot_cpu_id = smp_processor_id();
  466. /* Make certain I only run on the appropriate processor */
  467. set_cpus_allowed_ptr(current, cpumask_of(reboot_cpu_id));
  468. /* O.K Now that I'm on the appropriate processor,
  469. * stop all of the others.
  470. */
  471. smp_send_stop();
  472. #endif
  473. lapic_shutdown();
  474. #ifdef CONFIG_X86_IO_APIC
  475. disable_IO_APIC();
  476. #endif
  477. #ifdef CONFIG_HPET_TIMER
  478. hpet_disable();
  479. #endif
  480. #ifdef CONFIG_X86_64
  481. pci_iommu_shutdown();
  482. #endif
  483. }
  484. static void __machine_emergency_restart(int emergency)
  485. {
  486. reboot_emergency = emergency;
  487. machine_ops.emergency_restart();
  488. }
  489. static void native_machine_restart(char *__unused)
  490. {
  491. printk("machine restart\n");
  492. if (!reboot_force)
  493. machine_shutdown();
  494. __machine_emergency_restart(0);
  495. }
  496. static void native_machine_halt(void)
  497. {
  498. /* stop other cpus and apics */
  499. machine_shutdown();
  500. /* stop this cpu */
  501. stop_this_cpu(NULL);
  502. }
  503. static void native_machine_power_off(void)
  504. {
  505. if (pm_power_off) {
  506. if (!reboot_force)
  507. machine_shutdown();
  508. pm_power_off();
  509. }
  510. }
  511. struct machine_ops machine_ops = {
  512. .power_off = native_machine_power_off,
  513. .shutdown = native_machine_shutdown,
  514. .emergency_restart = native_machine_emergency_restart,
  515. .restart = native_machine_restart,
  516. .halt = native_machine_halt,
  517. #ifdef CONFIG_KEXEC
  518. .crash_shutdown = native_machine_crash_shutdown,
  519. #endif
  520. };
  521. void machine_power_off(void)
  522. {
  523. machine_ops.power_off();
  524. }
  525. void machine_shutdown(void)
  526. {
  527. machine_ops.shutdown();
  528. }
  529. void machine_emergency_restart(void)
  530. {
  531. __machine_emergency_restart(1);
  532. }
  533. void machine_restart(char *cmd)
  534. {
  535. machine_ops.restart(cmd);
  536. }
  537. void machine_halt(void)
  538. {
  539. machine_ops.halt();
  540. }
  541. #ifdef CONFIG_KEXEC
  542. void machine_crash_shutdown(struct pt_regs *regs)
  543. {
  544. machine_ops.crash_shutdown(regs);
  545. }
  546. #endif
  547. #if defined(CONFIG_SMP)
  548. /* This keeps a track of which one is crashing cpu. */
  549. static int crashing_cpu;
  550. static nmi_shootdown_cb shootdown_callback;
  551. static atomic_t waiting_for_crash_ipi;
  552. static int crash_nmi_callback(struct notifier_block *self,
  553. unsigned long val, void *data)
  554. {
  555. int cpu;
  556. if (val != DIE_NMI_IPI)
  557. return NOTIFY_OK;
  558. cpu = raw_smp_processor_id();
  559. /* Don't do anything if this handler is invoked on crashing cpu.
  560. * Otherwise, system will completely hang. Crashing cpu can get
  561. * an NMI if system was initially booted with nmi_watchdog parameter.
  562. */
  563. if (cpu == crashing_cpu)
  564. return NOTIFY_STOP;
  565. local_irq_disable();
  566. shootdown_callback(cpu, (struct die_args *)data);
  567. atomic_dec(&waiting_for_crash_ipi);
  568. /* Assume hlt works */
  569. halt();
  570. for (;;)
  571. cpu_relax();
  572. return 1;
  573. }
  574. static void smp_send_nmi_allbutself(void)
  575. {
  576. apic->send_IPI_allbutself(NMI_VECTOR);
  577. }
  578. static struct notifier_block crash_nmi_nb = {
  579. .notifier_call = crash_nmi_callback,
  580. };
  581. /* Halt all other CPUs, calling the specified function on each of them
  582. *
  583. * This function can be used to halt all other CPUs on crash
  584. * or emergency reboot time. The function passed as parameter
  585. * will be called inside a NMI handler on all CPUs.
  586. */
  587. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  588. {
  589. unsigned long msecs;
  590. local_irq_disable();
  591. /* Make a note of crashing cpu. Will be used in NMI callback.*/
  592. crashing_cpu = safe_smp_processor_id();
  593. shootdown_callback = callback;
  594. atomic_set(&waiting_for_crash_ipi, num_online_cpus() - 1);
  595. /* Would it be better to replace the trap vector here? */
  596. if (register_die_notifier(&crash_nmi_nb))
  597. return; /* return what? */
  598. /* Ensure the new callback function is set before sending
  599. * out the NMI
  600. */
  601. wmb();
  602. smp_send_nmi_allbutself();
  603. msecs = 1000; /* Wait at most a second for the other cpus to stop */
  604. while ((atomic_read(&waiting_for_crash_ipi) > 0) && msecs) {
  605. mdelay(1);
  606. msecs--;
  607. }
  608. /* Leave the nmi callback set */
  609. }
  610. #else /* !CONFIG_SMP */
  611. void nmi_shootdown_cpus(nmi_shootdown_cb callback)
  612. {
  613. /* No other CPUs to shoot down */
  614. }
  615. #endif