TimeManagement.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2020, Liav A. <liavalb@hotmail.co.il>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <Kernel/ACPI/Parser.h>
  27. #include <Kernel/CommandLine.h>
  28. #include <Kernel/Scheduler.h>
  29. #include <Kernel/Time/HPET.h>
  30. #include <Kernel/Time/HPETComparator.h>
  31. #include <Kernel/Time/HardwareTimer.h>
  32. #include <Kernel/Time/PIT.h>
  33. #include <Kernel/Time/RTC.h>
  34. #include <Kernel/Time/TimeManagement.h>
  35. #include <Kernel/VM/MemoryManager.h>
  36. //#define TIME_DEBUG
  37. namespace Kernel {
  38. static TimeManagement* s_time_management;
  39. TimeManagement& TimeManagement::the()
  40. {
  41. ASSERT(s_time_management);
  42. return *s_time_management;
  43. }
  44. bool TimeManagement::is_system_timer(const HardwareTimer& timer) const
  45. {
  46. return &timer == m_system_timer.ptr();
  47. }
  48. void TimeManagement::set_epoch_time(time_t value)
  49. {
  50. InterruptDisabler disabler;
  51. m_epoch_time = value;
  52. }
  53. time_t TimeManagement::epoch_time() const
  54. {
  55. return m_epoch_time;
  56. }
  57. void TimeManagement::initialize()
  58. {
  59. ASSERT(!s_time_management);
  60. if (kernel_command_line().lookup("time").value_or("modern") == "legacy")
  61. s_time_management = new TimeManagement(false);
  62. else
  63. s_time_management = new TimeManagement(true);
  64. }
  65. time_t TimeManagement::seconds_since_boot() const
  66. {
  67. return m_seconds_since_boot;
  68. }
  69. time_t TimeManagement::ticks_per_second() const
  70. {
  71. return m_system_timer->ticks_per_second();
  72. }
  73. time_t TimeManagement::ticks_this_second() const
  74. {
  75. return m_ticks_this_second;
  76. }
  77. time_t TimeManagement::boot_time() const
  78. {
  79. return RTC::boot_time();
  80. }
  81. TimeManagement::TimeManagement(bool probe_non_legacy_hardware_timers)
  82. {
  83. if (ACPI::is_enabled()) {
  84. if (!ACPI::Parser::the()->x86_specific_flags().cmos_rtc_not_present) {
  85. RTC::initialize();
  86. m_epoch_time += boot_time();
  87. } else {
  88. klog() << "ACPI: RTC CMOS Not present";
  89. }
  90. } else {
  91. // We just assume that we can access RTC CMOS, if ACPI isn't usable.
  92. RTC::initialize();
  93. m_epoch_time += boot_time();
  94. }
  95. if (probe_non_legacy_hardware_timers) {
  96. if (!probe_and_set_non_legacy_hardware_timers())
  97. if (!probe_and_set_legacy_hardware_timers())
  98. ASSERT_NOT_REACHED();
  99. return;
  100. }
  101. if (probe_and_set_legacy_hardware_timers())
  102. return;
  103. ASSERT_NOT_REACHED();
  104. }
  105. Vector<HardwareTimer*> TimeManagement::scan_and_initialize_periodic_timers()
  106. {
  107. bool should_enable = is_hpet_periodic_mode_allowed();
  108. dbg() << "Time: Scanning for periodic timers";
  109. Vector<HardwareTimer*> timers;
  110. for (auto& hardware_timer : m_hardware_timers) {
  111. if (hardware_timer && hardware_timer->is_periodic_capable()) {
  112. timers.append(hardware_timer);
  113. if (should_enable)
  114. hardware_timer->set_periodic();
  115. }
  116. }
  117. return timers;
  118. }
  119. Vector<HardwareTimer*> TimeManagement::scan_for_non_periodic_timers()
  120. {
  121. dbg() << "Time: Scanning for non-periodic timers";
  122. Vector<HardwareTimer*> timers;
  123. for (auto& hardware_timer : m_hardware_timers) {
  124. if (hardware_timer && !hardware_timer->is_periodic_capable())
  125. timers.append(hardware_timer);
  126. }
  127. return timers;
  128. }
  129. bool TimeManagement::is_hpet_periodic_mode_allowed()
  130. {
  131. if (!kernel_command_line().contains("hpet"))
  132. return true;
  133. auto hpet_mode = kernel_command_line().get("hpet");
  134. if (hpet_mode == "periodic")
  135. return true;
  136. if (hpet_mode == "nonperiodic")
  137. return false;
  138. ASSERT_NOT_REACHED();
  139. }
  140. bool TimeManagement::probe_and_set_non_legacy_hardware_timers()
  141. {
  142. if (!ACPI::is_enabled())
  143. return false;
  144. if (!HPET::test_and_initialize())
  145. return false;
  146. if (!HPET::the().comparators().size()) {
  147. dbg() << "HPET initialization aborted.";
  148. return false;
  149. }
  150. dbg() << "HPET: Setting appropriate functions to timers.";
  151. for (auto& hpet_comparator : HPET::the().comparators())
  152. m_hardware_timers.append(hpet_comparator);
  153. auto periodic_timers = scan_and_initialize_periodic_timers();
  154. auto non_periodic_timers = scan_for_non_periodic_timers();
  155. if (is_hpet_periodic_mode_allowed())
  156. ASSERT(!periodic_timers.is_empty());
  157. ASSERT(periodic_timers.size() + non_periodic_timers.size() >= 2);
  158. if (periodic_timers.size() >= 2) {
  159. m_time_keeper_timer = periodic_timers[1];
  160. m_system_timer = periodic_timers[0];
  161. } else {
  162. if (periodic_timers.size() == 1) {
  163. m_time_keeper_timer = periodic_timers[0];
  164. m_system_timer = non_periodic_timers[0];
  165. } else {
  166. m_time_keeper_timer = non_periodic_timers[1];
  167. m_system_timer = non_periodic_timers[0];
  168. }
  169. }
  170. m_system_timer->change_function(Scheduler::timer_tick);
  171. dbg() << "Reset timers";
  172. m_system_timer->try_to_set_frequency(m_system_timer->calculate_nearest_possible_frequency(1024));
  173. m_time_keeper_timer->change_function(TimeManagement::update_time);
  174. m_time_keeper_timer->try_to_set_frequency(OPTIMAL_TICKS_PER_SECOND_RATE);
  175. return true;
  176. }
  177. bool TimeManagement::probe_and_set_legacy_hardware_timers()
  178. {
  179. if (ACPI::is_enabled()) {
  180. if (ACPI::Parser::the()->x86_specific_flags().cmos_rtc_not_present) {
  181. dbg() << "ACPI: CMOS RTC Not Present";
  182. return false;
  183. } else {
  184. dbg() << "ACPI: CMOS RTC Present";
  185. }
  186. }
  187. m_hardware_timers.append(PIT::initialize(TimeManagement::update_time));
  188. m_hardware_timers.append(RealTimeClock::create(Scheduler::timer_tick));
  189. m_time_keeper_timer = m_hardware_timers[0];
  190. m_system_timer = m_hardware_timers[1];
  191. return true;
  192. }
  193. void TimeManagement::update_time(const RegisterState& regs)
  194. {
  195. TimeManagement::the().increment_time_since_boot(regs);
  196. }
  197. void TimeManagement::increment_time_since_boot(const RegisterState&)
  198. {
  199. ASSERT(!m_time_keeper_timer.is_null());
  200. if (++m_ticks_this_second >= m_time_keeper_timer->ticks_per_second()) {
  201. // FIXME: Synchronize with other clock somehow to prevent drifting apart.
  202. ++m_seconds_since_boot;
  203. ++m_epoch_time;
  204. m_ticks_this_second = 0;
  205. }
  206. }
  207. }