reboot.c 18 KB

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