halt.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2008 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/cpu/io.h>
  19. #include <grub/misc.h>
  20. #include <grub/acpi.h>
  21. #include <grub/i18n.h>
  22. #include <grub/pci.h>
  23. #include <grub/mm.h>
  24. const char bochs_shutdown[] = "Shutdown";
  25. /*
  26. * This call is special... it never returns... in fact it should simply
  27. * hang at this point!
  28. */
  29. static inline void __attribute__ ((noreturn))
  30. stop (void)
  31. {
  32. asm volatile ("cli");
  33. while (1)
  34. {
  35. asm volatile ("hlt");
  36. }
  37. }
  38. static int
  39. grub_shutdown_pci_iter (grub_pci_device_t dev, grub_pci_id_t pciid,
  40. void *data __attribute__ ((unused)))
  41. {
  42. /* QEMU. */
  43. if (pciid == 0x71138086)
  44. {
  45. grub_pci_address_t addr;
  46. addr = grub_pci_make_address (dev, 0x40);
  47. grub_pci_write (addr, 0x7001);
  48. addr = grub_pci_make_address (dev, 0x80);
  49. grub_pci_write (addr, grub_pci_read (addr) | 1);
  50. grub_outw (0x2000, 0x7004);
  51. }
  52. return 0;
  53. }
  54. void
  55. grub_halt (void)
  56. {
  57. unsigned int i;
  58. #if defined (GRUB_MACHINE_COREBOOT) || defined (GRUB_MACHINE_MULTIBOOT)
  59. grub_acpi_halt ();
  60. #endif
  61. /* Disable interrupts. */
  62. asm volatile ("cli");
  63. /* Bochs, QEMU, etc. Removed in newer QEMU releases. */
  64. for (i = 0; i < sizeof (bochs_shutdown) - 1; i++)
  65. grub_outb (bochs_shutdown[i], 0x8900);
  66. grub_pci_iterate (grub_shutdown_pci_iter, NULL);
  67. grub_puts_ (N_("GRUB doesn't know how to halt this machine yet!"));
  68. /* In order to return we'd have to check what the previous status of IF
  69. flag was. But user most likely doesn't want to return anyway ... */
  70. stop ();
  71. }