Kernel: Simplify reboot & poweroff code flow a bit

Instead of using ifdefs to use the correct platform-specific methods, we
can just use the same pattern we use for the microseconds_delay function
which has specific implementations for each Arch CPU subdirectory.

When linking a kernel image, the actual correct and platform-specific
power-state changing methods will be called in Firmware/PowerState.cpp
file.
This commit is contained in:
Liav A 2023-06-09 22:13:04 +03:00 committed by Jelle Raaijmakers
parent 5c8405c455
commit 9b8b8c0e04
Notes: sideshowbarker 2024-07-16 23:03:06 +09:00
8 changed files with 138 additions and 53 deletions

16
Kernel/Arch/PowerState.h Normal file
View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
namespace Kernel {
void arch_specific_reboot();
void arch_specific_poweroff();
}

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Arch/PowerState.h>
#include <Kernel/Arch/aarch64/RPi/Watchdog.h>
namespace Kernel {
void arch_specific_reboot()
{
}
void arch_specific_poweroff()
{
RPi::Watchdog::the().system_shutdown();
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Arch/PowerState.h>
#include <Kernel/Arch/x86_64/I8042Reboot.h>
#include <Kernel/Arch/x86_64/Shutdown.h>
namespace Kernel {
void arch_specific_reboot()
{
i8042_reboot();
}
void arch_specific_poweroff()
{
qemu_shutdown();
virtualbox_shutdown();
}
}

View file

@ -209,6 +209,7 @@ set(KERNEL_SOURCES
Firmware/ACPI/Initialize.cpp
Firmware/ACPI/Parser.cpp
Firmware/ACPI/StaticParsing.cpp
Firmware/PowerState.cpp
Interrupts/GenericInterruptHandler.cpp
Interrupts/IRQHandler.cpp
Interrupts/PCIIRQHandler.cpp
@ -402,6 +403,7 @@ if ("${SERENITY_ARCH}" STREQUAL "x86_64")
Arch/x86_64/VGA/IOArbiter.cpp
Arch/x86_64/PowerState.cpp
Arch/x86_64/RTC.cpp
Arch/x86_64/Shutdown.cpp
Arch/x86_64/SmapDisabler.cpp
@ -477,6 +479,7 @@ elseif("${SERENITY_ARCH}" STREQUAL "aarch64")
Arch/aarch64/PageDirectory.cpp
Arch/aarch64/Panic.cpp
Arch/aarch64/Processor.cpp
Arch/aarch64/PowerState.cpp
Arch/aarch64/SafeMem.cpp
Arch/aarch64/SmapDisabler.cpp
Arch/aarch64/TrapFrame.cpp

View file

@ -6,15 +6,10 @@
*/
#include <AK/Platform.h>
#if ARCH(X86_64)
# include <Kernel/Arch/x86_64/I8042Reboot.h>
# include <Kernel/Arch/x86_64/Shutdown.h>
#elif ARCH(AARCH64)
# include <Kernel/Arch/aarch64/RPi/Watchdog.h>
#endif
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/PowerStateSwitch.h>
#include <Kernel/Firmware/ACPI/Parser.h>
#include <Kernel/Firmware/PowerState.h>
#include <Kernel/Sections.h>
#include <Kernel/TTY/ConsoleManagement.h>
#include <Kernel/Tasks/Process.h>
@ -60,55 +55,13 @@ ErrorOr<size_t> SysFSPowerStateSwitchNode::write_bytes(off_t offset, size_t coun
TRY(data.read(buf, 1));
switch (buf[0]) {
case '1':
reboot();
Firmware::reboot();
VERIFY_NOT_REACHED();
case '2':
poweroff();
Firmware::poweroff();
VERIFY_NOT_REACHED();
default:
return Error::from_errno(EINVAL);
}
}
void SysFSPowerStateSwitchNode::reboot()
{
MutexLocker locker(Process::current().big_lock());
dbgln("acquiring FS locks...");
FileSystem::lock_all();
dbgln("syncing mounted filesystems...");
FileSystem::sync();
dbgln("attempting reboot via ACPI");
if (ACPI::is_enabled())
ACPI::Parser::the()->try_acpi_reboot();
#if ARCH(X86_64)
i8042_reboot();
#endif
dbgln("reboot attempts failed, applications will stop responding.");
dmesgln("Reboot can't be completed. It's safe to turn off the computer!");
Processor::halt();
}
void SysFSPowerStateSwitchNode::poweroff()
{
MutexLocker locker(Process::current().big_lock());
ConsoleManagement::the().switch_to_debug();
dbgln("acquiring FS locks...");
FileSystem::lock_all();
dbgln("syncing mounted filesystems...");
FileSystem::sync();
dbgln("attempting system shutdown...");
#if ARCH(X86_64)
qemu_shutdown();
virtualbox_shutdown();
#elif ARCH(AARCH64)
RPi::Watchdog::the().system_shutdown();
#endif
dbgln("shutdown attempts failed, applications will stop responding.");
dmesgln("Shutdown can't be completed. It's safe to turn off the computer!");
Processor::halt();
}
}

View file

@ -29,9 +29,6 @@ public:
private:
explicit SysFSPowerStateSwitchNode(SysFSDirectory const&);
void reboot();
void poweroff();
};
}

View file

@ -0,0 +1,55 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <Kernel/Arch/PowerState.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/Firmware/ACPI/Parser.h>
#include <Kernel/Firmware/PowerState.h>
#include <Kernel/TTY/ConsoleManagement.h>
#include <Kernel/Tasks/Process.h>
namespace Kernel::Firmware {
void reboot()
{
MutexLocker locker(Process::current().big_lock());
dbgln("acquiring FS locks...");
FileSystem::lock_all();
dbgln("syncing mounted filesystems...");
FileSystem::sync();
dbgln("attempting reboot via ACPI");
if (ACPI::is_enabled())
ACPI::Parser::the()->try_acpi_reboot();
arch_specific_reboot();
dbgln("reboot attempts failed, applications will stop responding.");
dmesgln("Reboot can't be completed. It's safe to turn off the computer!");
Processor::halt();
}
void poweroff()
{
MutexLocker locker(Process::current().big_lock());
ConsoleManagement::the().switch_to_debug();
dbgln("acquiring FS locks...");
FileSystem::lock_all();
dbgln("syncing mounted filesystems...");
FileSystem::sync();
dbgln("attempting system shutdown...");
arch_specific_poweroff();
dbgln("shutdown attempts failed, applications will stop responding.");
dmesgln("Shutdown can't be completed. It's safe to turn off the computer!");
Processor::halt();
}
}

View file

@ -0,0 +1,16 @@
/*
* Copyright (c) 2023, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
namespace Kernel::Firmware {
void reboot();
void poweroff();
}