feat: add timer calibration

This commit is contained in:
Katharina 2026-07-02 22:38:16 +02:00
parent da848544c0
commit 43c87e5904
5 changed files with 144 additions and 5 deletions

View file

@ -1,11 +1,19 @@
#pragma once
#include "acpi/madt.h"
#include "util/optional.h"
namespace apic {
struct lapic_timer_calibration_t {};
struct lapic_timer_calibration_t {
uint64_t tsc_frequency_hz;
};
lapic_timer_calibration_t calibrateTimer(const acpi::Madt& madt);
optional<lapic_timer_calibration_t> calibrateTimer(const acpi::Madt& madt);
// TODO: when dynamic allocation and lists are available
// struct Timer {};
//
// Timer getCoreTimerInstance();
} // namespace apic

View file

@ -0,0 +1,21 @@
#pragma once
#include <cpuid.h>
#include <stdint.h>
#include "util/optional.h"
struct cpuid_result_t {
uint32_t eax;
uint32_t ebx;
uint32_t ecx;
uint32_t edx;
};
inline optional<cpuid_result_t> cpuid(uint32_t leaf, uint32_t subleaf = 0) {
cpuid_result_t result{};
if (__get_cpuid_count(leaf, subleaf, &result.eax, &result.ebx, &result.ecx, &result.edx) == 0) {
return {};
}
return result;
}

View file

@ -1,5 +1,7 @@
#pragma once
#include "util/utility.h"
template<typename... Ts>
struct overloaded : Ts... {
using Ts::operator()...;
@ -7,3 +9,17 @@ struct overloaded : Ts... {
template<typename... Ts>
overloaded(Ts...) -> overloaded<Ts...>;
template<typename Func, typename... BoundArgs>
auto partial(Func func, BoundArgs... bound_args) {
return [func, bound_args...](auto&&... call_args) {
return func(bound_args..., forward<decltype(call_args)>(call_args)...);
};
}
template<typename Type>
auto construct() {
return [](auto&&... args) {
return Type{forward<decltype(args)>(args)...};
};
}

View file

@ -9,6 +9,9 @@
// instead of <utility>. The CrackOS3 value_or_panic() helper is omitted because
// there is no global panic() in this tree yet.
struct nullopt_t {};
inline constexpr nullopt_t nullopt{};
template<typename T>
struct optional {
private:
@ -26,6 +29,7 @@ public:
using value_type = T;
constexpr optional() : initialized(false) {}
constexpr optional(nullopt_t) : initialized(false) {}
optional(const T& value) : initialized(true) {
new (buffer) T(value);
}
@ -135,10 +139,45 @@ public:
return !initialized;
}
template<typename R, typename Func, typename... ArgT>
optional<R> map(Func func, ArgT... args) {
template<typename Func>
auto map(Func&& func) -> optional<decltype(func(value()))> {
if (initialized) {
return func(value(), forward<ArgT>(args)...);
return func(value());
} else {
return {};
}
}
template<typename Func>
auto map(Func&& func) const -> optional<decltype(func(value()))> {
if (initialized) {
return func(value());
} else {
return {};
}
}
template<typename Func>
optional or_else(Func&& func) const {
if (initialized) {
return *this;
}
return func();
}
template<typename Func>
auto bind(Func&& func) -> decltype(func(value())) {
if (initialized) {
return func(value());
} else {
return {};
}
}
template<typename Func>
auto bind(Func&& func) const -> decltype(func(value())) {
if (initialized) {
return func(value());
} else {
return {};
}