aboutsummaryrefslogtreecommitdiff
path: root/arch/x86_64/kapi/devices.cpp
blob: b2e9cba5a8e9361990633634bf05a4eb6cb64370 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <kapi/devices.hpp>

#include <arch/bus/cpu.hpp>
#include <arch/devices/init.hpp>

#include <kapi/devices/driver_registry.hpp>
#include <kapi/system.hpp>

#include <kstd/memory.hpp>
#include <kstd/print.hpp>

#include <new>
#include <ranges>
#include <span>
#include <utility>

namespace kapi::devices
{

  namespace
  {
    auto static constinit cpu_bus = kstd::shared_ptr<arch::bus::cpu>{};
  }

  extern "C"
  {
    // We need to suppress clang-tidy linting warnings here, since these symbols are generated by the linker and we
    // cannot choose their names, unless we want to write a bht linker script for no reason. Additionally, these symbols
    // need to be arrays, because otherwise any pointer arithmetic on them, beyond trivial cases of adding 0 or 1, is
    // undefined behavior by the rules of the C++ abstract machine. To ensure the compiler does not try to perform
    // constant folding, the pointers, these arrays of unknown bound decay to, need to be laundered before dereferencing
    // them.

    // NOLINTBEGIN(readability-identifier-naming)
    extern kstd::observer_ptr<kapi::devices::driver_descriptor> const __start_platform_drivers[];
    extern kstd::observer_ptr<kapi::devices::driver_descriptor> const __stop_platform_drivers[];
    // NOLINTEND(readability-identifier-naming)
  }

  auto init_platform_drivers() -> void
  {
    auto clean_start = std::launder(__start_platform_drivers);
    auto clean_stop = std::launder(__stop_platform_drivers);

    auto descriptors = std::span{clean_start, clean_stop} |  //
                       std::views::filter([](auto p) { return p != nullptr; });

    for (auto driver : descriptors)
    {
      auto instance = driver->make_instance();
      kstd::println("[ARCH:DRV] registering driver '{}' ({})", instance->name(), driver->name());
      kapi::devices::driver_registry::get().add(std::move(instance));
    }
  }

  auto init_platform_devices() -> void
  {
    arch::devices::init_acpi_devices();
    arch::devices::init_legacy_devices();
  }

  auto get_cpu_bus() -> kstd::shared_ptr<bus>
  {
    if (!cpu_bus)
    {
      system::panic("[ARCH:DEV] The CPU topology has not yet been initialized!");
    }

    return cpu_bus;
  }

  auto discover_cpu_topology() -> void
  {
    if (cpu_bus)
    {
      system::panic("[ARCH:DEV] The CPU topology has already been initialized!");
    }

    cpu_bus = kstd::make_shared<arch::bus::cpu>();
    cpu_bus->facet<kapi::devices::bus_protocol>()->enumerate(*cpu_bus);

    kapi::devices::get_root_bus()->add_child(cpu_bus);
  }

}  // namespace kapi::devices