shutdown.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <Kernel/ACPI/Parser.h>
  7. #include <Kernel/FileSystem/FileSystem.h>
  8. #include <Kernel/IO.h>
  9. #include <Kernel/Process.h>
  10. #include <Kernel/TTY/ConsoleManagement.h>
  11. namespace Kernel {
  12. KResultOr<FlatPtr> Process::sys$reboot()
  13. {
  14. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  15. if (!is_superuser())
  16. return EPERM;
  17. REQUIRE_NO_PROMISES;
  18. dbgln("acquiring FS locks...");
  19. FileSystem::lock_all();
  20. dbgln("syncing mounted filesystems...");
  21. FileSystem::sync();
  22. dbgln("attempting reboot via ACPI");
  23. if (ACPI::is_enabled())
  24. ACPI::Parser::the()->try_acpi_reboot();
  25. dbgln("attempting reboot via KB Controller...");
  26. IO::out8(0x64, 0xFE);
  27. return 0;
  28. }
  29. KResultOr<FlatPtr> Process::sys$halt()
  30. {
  31. VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
  32. if (!is_superuser())
  33. return EPERM;
  34. REQUIRE_NO_PROMISES;
  35. ConsoleManagement::the().switch_to_debug();
  36. dbgln("acquiring FS locks...");
  37. FileSystem::lock_all();
  38. dbgln("syncing mounted filesystems...");
  39. FileSystem::sync();
  40. dbgln("attempting system shutdown...");
  41. // QEMU Shutdown
  42. IO::out16(0x604, 0x2000);
  43. // If we're here, the shutdown failed. Try VirtualBox shutdown.
  44. IO::out16(0x4004, 0x3400);
  45. // VirtualBox shutdown failed. Try Bochs/Old QEMU shutdown.
  46. IO::out16(0xb004, 0x2000);
  47. dbgln("shutdown attempts failed, applications will stop responding.");
  48. dmesgln("Shutdown can't be completed. It's safe to turn off the computer!");
  49. Processor::halt();
  50. }
  51. }