aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--arch/x86_64/include/arch/bus/isa.hpp15
-rw-r--r--arch/x86_64/src/bus/isa.cpp44
-rw-r--r--kapi/include/kapi/devices/bus.hpp61
-rw-r--r--kernel/include/kernel/devices/root_bus.hpp18
-rw-r--r--kernel/kapi/devices.cpp4
-rw-r--r--kernel/src/devices/root_bus.cpp43
-rw-r--r--libs/kstd/include/kstd/bits/unique_ptr.hpp22
7 files changed, 66 insertions, 141 deletions
diff --git a/arch/x86_64/include/arch/bus/isa.hpp b/arch/x86_64/include/arch/bus/isa.hpp
index 41dda93..bd92b2e 100644
--- a/arch/x86_64/include/arch/bus/isa.hpp
+++ b/arch/x86_64/include/arch/bus/isa.hpp
@@ -2,10 +2,6 @@
#define TEACHOS_X86_64_BUS_ISA_HPP
#include "kapi/devices/bus.hpp"
-#include "kapi/devices/device.hpp"
-
-#include <kstd/memory>
-#include <kstd/vector>
namespace arch::bus
{
@@ -13,17 +9,6 @@ namespace arch::bus
struct isa final : public kapi::devices::bus
{
isa();
-
- auto add_child(kstd::unique_ptr<device> child) -> void override;
-
- [[nodiscard]] auto children() const -> kstd::vector<kstd::observer_ptr<device>> const & override;
-
- auto init() -> bool override;
-
- private:
- kstd::vector<kstd::unique_ptr<device>> m_devices{};
- kstd::vector<kstd::observer_ptr<device>> m_observers{};
- bool m_initialized{};
};
} // namespace arch::bus
diff --git a/arch/x86_64/src/bus/isa.cpp b/arch/x86_64/src/bus/isa.cpp
index 3fe4f6f..2ad4d21 100644
--- a/arch/x86_64/src/bus/isa.cpp
+++ b/arch/x86_64/src/bus/isa.cpp
@@ -1,15 +1,6 @@
#include "arch/bus/isa.hpp"
#include "kapi/devices.hpp"
-#include "kapi/devices/device.hpp"
-#include "kapi/system.hpp"
-
-#include <kstd/memory>
-#include <kstd/print>
-#include <kstd/vector>
-
-#include <algorithm>
-#include <utility>
namespace arch::bus
{
@@ -18,39 +9,4 @@ namespace arch::bus
: kapi::devices::bus(kapi::devices::allocate_major_number(), 0, "isa")
{}
- auto isa::add_child(kstd::unique_ptr<device> child) -> void
- {
- auto observer = m_observers.emplace_back(child.get());
- m_devices.push_back(std::move(child));
-
- if (m_initialized)
- {
- kstd::println("[bus:{}} Initializing child device '{}'", name(), observer->name());
- if (!observer->init())
- {
- kapi::system::panic("[x86_64:devices] Failed to initialize child device");
- }
- }
- }
-
- auto isa::children() const -> kstd::vector<kstd::observer_ptr<device>> const &
- {
- return m_observers;
- }
-
- auto isa::init() -> bool
- {
- if (m_initialized)
- {
- kapi::system::panic("[x86_64:devices] ISA bus already initialized!");
- }
-
- m_initialized = std::ranges::fold_left(m_devices, true, [](bool acc, auto & child) -> bool {
- kstd::println("[x86_64:devices] Initializing child device '{}'", child->name());
- return acc && child->init();
- });
-
- return m_initialized;
- }
-
} // namespace arch::bus \ No newline at end of file
diff --git a/kapi/include/kapi/devices/bus.hpp b/kapi/include/kapi/devices/bus.hpp
index a8d7df8..ccaf0f2 100644
--- a/kapi/include/kapi/devices/bus.hpp
+++ b/kapi/include/kapi/devices/bus.hpp
@@ -2,12 +2,17 @@
#define TEACHOS_KAPI_DEVICES_BUS_HPP
#include "kapi/devices/device.hpp"
+#include "kapi/system.hpp"
#include <kstd/memory>
+#include <kstd/print>
#include <kstd/string>
#include <kstd/vector>
+#include <algorithm>
+#include <atomic>
#include <cstddef>
+#include <utility>
namespace kapi::devices
{
@@ -23,17 +28,65 @@ namespace kapi::devices
: device(major, minor, name)
{}
+ //! Initialize the bus and all of its children.
+ //!
+ //! @return true iff. the bus and all of its children are healthy, false otherwise.
+ auto init() -> bool final
+ {
+ if (m_initialized.test_and_set())
+ {
+ return true;
+ }
+
+ if (!probe())
+ {
+ return false;
+ }
+
+ return std::ranges::fold_left(m_devices, true, [&](bool acc, auto & child) -> bool {
+ kstd::println("[kAPI:BUS] Initializing child device {}@{}", child->name(), name());
+ return acc && child->init();
+ });
+ }
+
//! Attach a child device to this bus.
//!
//! Whenever a device is attached to a bus, the bus takes sole ownership of the device.
//!
//! @param child The child device to attach.
- virtual auto add_child(kstd::unique_ptr<device> child) -> void = 0;
+ auto add_child(kstd::unique_ptr<device> child) -> void
+ {
+ auto observer = m_observers.emplace_back(child.get());
+ m_devices.push_back(std::move(child));
- //! Get a list of all child devices attached to this bus.
+ if (m_initialized.test())
+ {
+ kstd::println("[kAPI:BUS] Initializing child device {}@{}", observer->name(), name());
+ if (!observer->init())
+ {
+ kapi::system::panic("[kAPI:BUS] Failed to initialize child device");
+ }
+ }
+ }
+
+ [[nodiscard]] auto children() const -> kstd::vector<kstd::observer_ptr<device>> const &
+ {
+ return m_observers;
+ }
+
+ protected:
+ //! Probe the bus hardware state.
//!
- //! @return A reference to list of child devices of this bus.
- [[nodiscard]] virtual auto children() const -> kstd::vector<kstd::observer_ptr<device>> const & = 0;
+ //! @return true iff. the bus hardware is healthy, false otherwise.
+ auto virtual probe() -> bool
+ {
+ return true;
+ }
+
+ private:
+ kstd::vector<kstd::unique_ptr<device>> m_devices;
+ kstd::vector<kstd::observer_ptr<device>> m_observers;
+ std::atomic_flag m_initialized{};
};
} // namespace kapi::devices
diff --git a/kernel/include/kernel/devices/root_bus.hpp b/kernel/include/kernel/devices/root_bus.hpp
index d92914d..660b715 100644
--- a/kernel/include/kernel/devices/root_bus.hpp
+++ b/kernel/include/kernel/devices/root_bus.hpp
@@ -2,13 +2,6 @@
#define TEACHOS_KERNEL_DEVICES_ROOT_BUS_HPP
#include "kapi/devices/bus.hpp"
-#include "kapi/devices/device.hpp"
-
-#include <kstd/memory>
-#include <kstd/print>
-#include <kstd/vector>
-
-#include <atomic>
namespace kernel::devices
{
@@ -16,17 +9,6 @@ namespace kernel::devices
struct root_bus final : kapi::devices::bus
{
root_bus();
-
- auto add_child(kstd::unique_ptr<device> child) -> void override;
-
- [[nodiscard]] auto children() const -> kstd::vector<kstd::observer_ptr<device>> const & override;
-
- auto init() -> bool override;
-
- private:
- kstd::vector<kstd::unique_ptr<device>> m_children{};
- kstd::vector<kstd::observer_ptr<device>> m_observers{};
- std::atomic_flag m_initialized{};
};
} // namespace kernel::devices
diff --git a/kernel/kapi/devices.cpp b/kernel/kapi/devices.cpp
index dc19ab4..dbf5e68 100644
--- a/kernel/kapi/devices.cpp
+++ b/kernel/kapi/devices.cpp
@@ -39,7 +39,7 @@ namespace kapi::devices
{
if (!root_bus.has_value())
{
- kapi::system::panic("[kernel:devices] Root bus not initialized!");
+ kapi::system::panic("[OS:DEV] Root bus not initialized!");
}
return *root_bus;
}
@@ -56,7 +56,7 @@ namespace kapi::devices
auto unregister_device(device &) -> bool
{
- kstd::println("[kernel:devices] TODO: implement device deregistration");
+ kstd::println("[OS:DEV] TODO: implement device deregistration");
return false;
}
diff --git a/kernel/src/devices/root_bus.cpp b/kernel/src/devices/root_bus.cpp
index 75b5b80..d3ba23f 100644
--- a/kernel/src/devices/root_bus.cpp
+++ b/kernel/src/devices/root_bus.cpp
@@ -1,16 +1,6 @@
#include "kernel/devices/root_bus.hpp"
#include "kapi/devices.hpp"
-#include "kapi/devices/bus.hpp"
-#include "kapi/devices/device.hpp"
-#include "kapi/system.hpp"
-
-#include <kstd/memory>
-#include <kstd/print>
-#include <kstd/vector>
-
-#include <algorithm>
-#include <utility>
namespace kernel::devices
{
@@ -19,37 +9,4 @@ namespace kernel::devices
: kapi::devices::bus{kapi::devices::allocate_major_number(), 0, "system"}
{}
- auto root_bus::add_child(kstd::unique_ptr<device> child) -> void
- {
- auto observer = m_observers.emplace_back(child.get());
- m_children.push_back(std::move(child));
-
- if (m_initialized.test())
- {
- kstd::println("Initializing child device '{}'", observer->name());
- if (!observer->init())
- {
- kapi::system::panic("[kernel:devices] Failed to initialize child device");
- }
- }
- }
-
- auto root_bus::children() const -> kstd::vector<kstd::observer_ptr<device>> const &
- {
- return m_observers;
- }
-
- auto root_bus::init() -> bool
- {
- if (m_initialized.test_and_set())
- {
- kapi::system::panic("[kernel:devices] Root bus already initialized!");
- }
-
- return std::ranges::fold_left(m_children, true, [](bool acc, auto & child) -> bool {
- kstd::println("[kernel:devices] Initializing child device '{}'", child->name());
- return acc && child->init();
- });
- }
-
} // namespace kernel::devices \ No newline at end of file
diff --git a/libs/kstd/include/kstd/bits/unique_ptr.hpp b/libs/kstd/include/kstd/bits/unique_ptr.hpp
index f50335c..3d803b4 100644
--- a/libs/kstd/include/kstd/bits/unique_ptr.hpp
+++ b/libs/kstd/include/kstd/bits/unique_ptr.hpp
@@ -46,10 +46,8 @@ namespace kstd
template<typename U>
requires(std::is_convertible_v<U *, T *>)
unique_ptr(unique_ptr<U> && other) noexcept
- : pointer(other.pointer)
- {
- other.pointer = nullptr;
- }
+ : pointer{std::exchange(other.pointer, nullptr)}
+ {}
/**
* @brief Deleted copy assignment operator to enforce unique ownership.
@@ -62,10 +60,8 @@ namespace kstd
* @param other Unique pointer to move from.
*/
unique_ptr(unique_ptr && other) noexcept
- : pointer(other.pointer)
- {
- other.pointer = nullptr;
- }
+ : pointer{std::exchange(other.pointer, nullptr)}
+ {}
/**
* @brief Move assignment operator. Transfers ownership from other to *this as if by calling reset(r.release()).
@@ -78,8 +74,7 @@ namespace kstd
if (this != &other)
{
delete pointer;
- pointer = other.pointer;
- other.pointer = nullptr;
+ pointer = std::exchange(other.pointer, nullptr);
}
return *this;
}
@@ -134,9 +129,7 @@ namespace kstd
*/
auto release() -> T *
{
- T * temp = pointer;
- pointer = nullptr;
- return temp;
+ return std::exchange(pointer, nullptr);
}
/**
@@ -150,8 +143,7 @@ namespace kstd
*/
auto reset(T * ptr = nullptr) -> void
{
- delete pointer;
- pointer = ptr;
+ delete std::exchange(pointer, ptr);
}
/**