shutdown.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. if (!is_superuser())
  15. return EPERM;
  16. REQUIRE_NO_PROMISES;
  17. dbgln("acquiring FS locks...");
  18. FS::lock_all();
  19. dbgln("syncing mounted filesystems...");
  20. FS::sync();
  21. dbgln("attempting reboot via ACPI");
  22. if (ACPI::is_enabled())
  23. ACPI::Parser::the()->try_acpi_reboot();
  24. dbgln("attempting reboot via KB Controller...");
  25. IO::out8(0x64, 0xFE);
  26. return 0;
  27. }
  28. KResultOr<FlatPtr> Process::sys$halt()
  29. {
  30. if (!is_superuser())
  31. return EPERM;
  32. REQUIRE_NO_PROMISES;
  33. ConsoleManagement::the().switch_to_debug();
  34. dbgln("acquiring FS locks...");
  35. FS::lock_all();
  36. dbgln("syncing mounted filesystems...");
  37. FS::sync();
  38. dbgln("attempting system shutdown...");
  39. // QEMU Shutdown
  40. IO::out16(0x604, 0x2000);
  41. // If we're here, the shutdown failed. Try VirtualBox shutdown.
  42. IO::out16(0x4004, 0x3400);
  43. // VirtualBox shutdown failed. Try Bochs/Old QEMU shutdown.
  44. IO::out16(0xb004, 0x2000);
  45. dbgln("shutdown attempts failed, applications will stop responding.");
  46. dmesgln("Shutdown can't be completed. It's safe to turn off the computer!");
  47. Processor::halt();
  48. }
  49. }