TimeManagement.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/NonnullRefPtrVector.h>
  8. #include <AK/OwnPtr.h>
  9. #include <AK/RefPtr.h>
  10. #include <AK/Time.h>
  11. #include <AK/Types.h>
  12. #include <Kernel/API/KResult.h>
  13. #include <Kernel/API/TimePage.h>
  14. #include <Kernel/Arch/x86/RegisterState.h>
  15. #include <Kernel/UnixTypes.h>
  16. namespace Kernel {
  17. #define OPTIMAL_TICKS_PER_SECOND_RATE 250
  18. #define OPTIMAL_PROFILE_TICKS_PER_SECOND_RATE 1000
  19. class HardwareTimerBase;
  20. enum class TimePrecision {
  21. Coarse = 0,
  22. Precise
  23. };
  24. class TimeManagement {
  25. AK_MAKE_ETERNAL;
  26. public:
  27. TimeManagement();
  28. static void initialize(u32 cpu);
  29. static TimeManagement& the();
  30. static bool is_valid_clock_id(clockid_t);
  31. Time current_time(clockid_t) const;
  32. Time monotonic_time(TimePrecision = TimePrecision::Coarse) const;
  33. Time monotonic_time_raw() const
  34. {
  35. // TODO: implement
  36. return monotonic_time(TimePrecision::Precise);
  37. }
  38. Time epoch_time(TimePrecision = TimePrecision::Precise) const;
  39. void set_epoch_time(Time);
  40. time_t ticks_per_second() const;
  41. time_t boot_time() const;
  42. bool is_system_timer(const HardwareTimerBase&) const;
  43. static void update_time(const RegisterState&);
  44. static void update_time_hpet(const RegisterState&);
  45. void increment_time_since_boot_hpet();
  46. void increment_time_since_boot();
  47. static bool is_hpet_periodic_mode_allowed();
  48. bool enable_profile_timer();
  49. bool disable_profile_timer();
  50. u64 uptime_ms() const;
  51. static Time now();
  52. // FIXME: Should use AK::Time internally
  53. // FIXME: Also, most likely broken, because it does not check m_update[12] for in-progress updates.
  54. timespec remaining_epoch_time_adjustment() const { return m_remaining_epoch_time_adjustment; }
  55. // FIXME: Should use AK::Time internally
  56. // FIXME: Also, most likely broken, because it does not check m_update[12] for in-progress updates.
  57. void set_remaining_epoch_time_adjustment(const timespec& adjustment) { m_remaining_epoch_time_adjustment = adjustment; }
  58. bool can_query_precise_time() const { return m_can_query_precise_time; }
  59. Memory::VMObject& time_page_vmobject();
  60. private:
  61. TimePage* time_page();
  62. void update_time_page();
  63. bool probe_and_set_legacy_hardware_timers();
  64. bool probe_and_set_non_legacy_hardware_timers();
  65. Vector<HardwareTimerBase*> scan_and_initialize_periodic_timers();
  66. Vector<HardwareTimerBase*> scan_for_non_periodic_timers();
  67. NonnullRefPtrVector<HardwareTimerBase> m_hardware_timers;
  68. void set_system_timer(HardwareTimerBase&);
  69. static void system_timer_tick(const RegisterState&);
  70. static u64 scheduling_current_time(bool);
  71. // Variables between m_update1 and m_update2 are synchronized
  72. Atomic<u32> m_update1 { 0 };
  73. u32 m_ticks_this_second { 0 };
  74. u64 m_seconds_since_boot { 0 };
  75. // FIXME: Should use AK::Time internally
  76. timespec m_epoch_time { 0, 0 };
  77. timespec m_remaining_epoch_time_adjustment { 0, 0 };
  78. Atomic<u32> m_update2 { 0 };
  79. u32 m_time_ticks_per_second { 0 }; // may be different from interrupts/second (e.g. hpet)
  80. bool m_can_query_precise_time { false };
  81. bool m_updating_time { false }; // may only be accessed from the BSP!
  82. RefPtr<HardwareTimerBase> m_system_timer;
  83. RefPtr<HardwareTimerBase> m_time_keeper_timer;
  84. Atomic<u32> m_profile_enable_count { 0 };
  85. RefPtr<HardwareTimerBase> m_profile_timer;
  86. OwnPtr<Memory::Region> m_time_page_region;
  87. };
  88. }