1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:37:35 +00:00

Kernel: Introduce global variables and stats in /sys/kernel directory

The ProcFS is an utter mess currently, so let's start move things that
are not related to processes-info. To ensure it's done in a sane manner,
we start by duplicating all /proc/ global nodes to the /sys/kernel/
directory, then we will move Userland to use the new directory so the
old directory nodes can be removed from the /proc directory.
This commit is contained in:
Liav A 2022-10-14 20:51:51 +03:00 committed by Andrew Kaster
parent db2e1bfa02
commit a91589c09b
58 changed files with 2122 additions and 1 deletions

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.h>
#include <Kernel/Sections.h>
namespace Kernel {
ErrorOr<void> SysFSSystemBoolean::try_generate(KBufferBuilder& builder)
{
return builder.appendff("{}\n", static_cast<int>(value()));
}
ErrorOr<size_t> SysFSSystemBoolean::write_bytes(off_t, size_t count, UserOrKernelBuffer const& buffer, OpenFileDescription*)
{
if (count != 1)
return EINVAL;
MutexLocker locker(m_refresh_lock);
char value = 0;
TRY(buffer.read(&value, 1));
if (value == '0')
set_value(false);
else if (value == '1')
set_value(true);
else
return EINVAL;
return 1;
}
ErrorOr<void> SysFSSystemBoolean::truncate(u64 size)
{
if (size != 0)
return EPERM;
return {};
}
}

View file

@ -0,0 +1,44 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/AtomicRefCounted.h>
#include <AK/Error.h>
#include <AK/Function.h>
#include <AK/Types.h>
#include <Kernel/FileSystem/File.h>
#include <Kernel/FileSystem/FileSystem.h>
#include <Kernel/FileSystem/OpenFileDescription.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/GlobalInformation.h>
#include <Kernel/KBufferBuilder.h>
#include <Kernel/Library/LockRefPtr.h>
#include <Kernel/Locking/Mutex.h>
#include <Kernel/Time/TimeManagement.h>
#include <Kernel/UserOrKernelBuffer.h>
namespace Kernel {
class SysFSSystemBoolean : public SysFSGlobalInformation {
protected:
explicit SysFSSystemBoolean(SysFSDirectory const& parent_directory)
: SysFSGlobalInformation(parent_directory)
{
}
virtual bool value() const = 0;
virtual void set_value(bool new_value) = 0;
private:
// ^SysFSGlobalInformation
virtual ErrorOr<void> try_generate(KBufferBuilder&) override final;
// ^SysFSExposedComponent
virtual ErrorOr<size_t> write_bytes(off_t, size_t, UserOrKernelBuffer const&, OpenFileDescription*) override final;
virtual mode_t permissions() const override final { return 0644; }
virtual ErrorOr<void> truncate(u64) override final;
};
}

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/Devices/HID/HIDManagement.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/CapsLockRemap.h>
#include <Kernel/Sections.h>
namespace Kernel {
UNMAP_AFTER_INIT SysFSCapsLockRemap::SysFSCapsLockRemap(SysFSDirectory const& parent_directory)
: SysFSSystemBoolean(parent_directory)
{
}
UNMAP_AFTER_INIT NonnullLockRefPtr<SysFSCapsLockRemap> SysFSCapsLockRemap::must_create(SysFSDirectory const& parent_directory)
{
return adopt_lock_ref_if_nonnull(new (nothrow) SysFSCapsLockRemap(parent_directory)).release_nonnull();
}
bool SysFSCapsLockRemap::value() const
{
MutexLocker locker(m_lock);
return g_caps_lock_remapped_to_ctrl.load();
}
void SysFSCapsLockRemap::set_value(bool new_value)
{
MutexLocker locker(m_lock);
g_caps_lock_remapped_to_ctrl.exchange(new_value);
}
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.h>
#include <Kernel/Library/LockRefPtr.h>
#include <Kernel/UserOrKernelBuffer.h>
namespace Kernel {
class SysFSCapsLockRemap final : public SysFSSystemBoolean {
public:
virtual StringView name() const override { return "caps_lock_to_ctrl"sv; }
static NonnullLockRefPtr<SysFSCapsLockRemap> must_create(SysFSDirectory const&);
private:
virtual bool value() const override;
virtual void set_value(bool new_value) override;
explicit SysFSCapsLockRemap(SysFSDirectory const&);
mutable Mutex m_lock;
};
}

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Error.h>
#include <AK/Try.h>
#include <Kernel/FileSystem/SysFS/Component.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/CapsLockRemap.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/Directory.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/DumpKmallocStack.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/UBSANDeadly.h>
namespace Kernel {
UNMAP_AFTER_INIT NonnullLockRefPtr<SysFSGlobalKernelVariablesDirectory> SysFSGlobalKernelVariablesDirectory::must_create(SysFSDirectory const& parent_directory)
{
auto global_variables_directory = adopt_lock_ref_if_nonnull(new (nothrow) SysFSGlobalKernelVariablesDirectory(parent_directory)).release_nonnull();
MUST(global_variables_directory->m_child_components.with([&](auto& list) -> ErrorOr<void> {
list.append(SysFSCapsLockRemap::must_create(*global_variables_directory));
list.append(SysFSDumpKmallocStacks::must_create(*global_variables_directory));
list.append(SysFSUBSANDeadly::must_create(*global_variables_directory));
return {};
}));
return global_variables_directory;
}
UNMAP_AFTER_INIT SysFSGlobalKernelVariablesDirectory::SysFSGlobalKernelVariablesDirectory(SysFSDirectory const& parent_directory)
: SysFSDirectory(parent_directory)
{
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2021, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <Kernel/FileSystem/SysFS/Component.h>
#include <Kernel/FileSystem/SysFS/RootDirectory.h>
namespace Kernel {
class SysFSGlobalKernelVariablesDirectory : public SysFSDirectory {
public:
static NonnullLockRefPtr<SysFSGlobalKernelVariablesDirectory> must_create(SysFSDirectory const&);
virtual StringView name() const override { return "variables"sv; }
private:
explicit SysFSGlobalKernelVariablesDirectory(SysFSDirectory const&);
};
}

View file

@ -0,0 +1,35 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/DumpKmallocStack.h>
#include <Kernel/Process.h>
#include <Kernel/Sections.h>
namespace Kernel {
UNMAP_AFTER_INIT SysFSDumpKmallocStacks::SysFSDumpKmallocStacks(SysFSDirectory const& parent_directory)
: SysFSSystemBoolean(parent_directory)
{
}
UNMAP_AFTER_INIT NonnullLockRefPtr<SysFSDumpKmallocStacks> SysFSDumpKmallocStacks::must_create(SysFSDirectory const& parent_directory)
{
return adopt_lock_ref_if_nonnull(new (nothrow) SysFSDumpKmallocStacks(parent_directory)).release_nonnull();
}
bool SysFSDumpKmallocStacks::value() const
{
MutexLocker locker(m_lock);
return g_dump_kmalloc_stacks;
}
void SysFSDumpKmallocStacks::set_value(bool new_value)
{
MutexLocker locker(m_lock);
g_dump_kmalloc_stacks = new_value;
}
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.h>
#include <Kernel/Library/LockRefPtr.h>
#include <Kernel/UserOrKernelBuffer.h>
namespace Kernel {
class SysFSDumpKmallocStacks final : public SysFSSystemBoolean {
public:
virtual StringView name() const override { return "kmalloc_stacks"sv; }
static NonnullLockRefPtr<SysFSDumpKmallocStacks> must_create(SysFSDirectory const&);
private:
virtual bool value() const override;
virtual void set_value(bool new_value) override;
explicit SysFSDumpKmallocStacks(SysFSDirectory const&);
mutable Mutex m_lock;
};
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/UBSanitizer.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/UBSANDeadly.h>
#include <Kernel/Sections.h>
namespace Kernel {
UNMAP_AFTER_INIT SysFSUBSANDeadly::SysFSUBSANDeadly(SysFSDirectory const& parent_directory)
: SysFSSystemBoolean(parent_directory)
{
}
UNMAP_AFTER_INIT NonnullLockRefPtr<SysFSUBSANDeadly> SysFSUBSANDeadly::must_create(SysFSDirectory const& parent_directory)
{
return adopt_lock_ref_if_nonnull(new (nothrow) SysFSUBSANDeadly(parent_directory)).release_nonnull();
}
bool SysFSUBSANDeadly::value() const
{
return AK::UBSanitizer::g_ubsan_is_deadly;
}
void SysFSUBSANDeadly::set_value(bool new_value)
{
AK::UBSanitizer::g_ubsan_is_deadly = new_value;
}
}

View file

@ -0,0 +1,28 @@
/*
* Copyright (c) 2022, Liav A. <liavalb@hotmail.co.il>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
#include <Kernel/FileSystem/SysFS/Subsystems/Kernel/Variables/BooleanVariable.h>
#include <Kernel/Library/LockRefPtr.h>
#include <Kernel/UserOrKernelBuffer.h>
namespace Kernel {
class SysFSUBSANDeadly final : public SysFSSystemBoolean {
public:
virtual StringView name() const override { return "ubsan_is_deadly"sv; }
static NonnullLockRefPtr<SysFSUBSANDeadly> must_create(SysFSDirectory const&);
private:
virtual bool value() const override;
virtual void set_value(bool new_value) override;
explicit SysFSUBSANDeadly(SysFSDirectory const&);
};
}