TimeManagement.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. void TimeManagement::stale_function(const RegisterState&)
  82. {
  83. }
  84. TimeManagement::TimeManagement(bool probe_non_legacy_hardware_timers)
  85. {
  86. if (ACPI::is_enabled()) {
  87. if (!ACPI::Parser::the()->x86_specific_flags().cmos_rtc_not_present) {
  88. RTC::initialize();
  89. m_epoch_time += boot_time();
  90. } else {
  91. klog() << "ACPI: RTC CMOS Not present";
  92. }
  93. } else {
  94. // We just assume that we can access RTC CMOS, if ACPI isn't usable.
  95. RTC::initialize();
  96. m_epoch_time += boot_time();
  97. }
  98. if (probe_non_legacy_hardware_timers) {
  99. if (!probe_and_set_non_legacy_hardware_timers())
  100. if (!probe_and_set_legacy_hardware_timers())
  101. ASSERT_NOT_REACHED();
  102. return;
  103. }
  104. if (probe_and_set_legacy_hardware_timers())
  105. return;
  106. ASSERT_NOT_REACHED();
  107. }
  108. Vector<HardwareTimer*> TimeManagement::scan_and_initialize_periodic_timers()
  109. {
  110. bool should_enable = is_hpet_periodic_mode_allowed();
  111. dbg() << "Time: Scanning for periodic timers";
  112. Vector<HardwareTimer*> timers;
  113. for (auto& hardware_timer : m_hardware_timers) {
  114. if (hardware_timer && hardware_timer->is_periodic_capable()) {
  115. timers.append(hardware_timer);
  116. if (should_enable)
  117. hardware_timer->set_periodic();
  118. }
  119. }
  120. return timers;
  121. }
  122. Vector<HardwareTimer*> TimeManagement::scan_for_non_periodic_timers()
  123. {
  124. dbg() << "Time: Scanning for non-periodic timers";
  125. Vector<HardwareTimer*> timers;
  126. for (auto& hardware_timer : m_hardware_timers) {
  127. if (hardware_timer && !hardware_timer->is_periodic_capable())
  128. timers.append(hardware_timer);
  129. }
  130. return timers;
  131. }
  132. bool TimeManagement::is_hpet_periodic_mode_allowed()
  133. {
  134. if (!kernel_command_line().contains("hpet"))
  135. return true;
  136. auto hpet_mode = kernel_command_line().get("hpet");
  137. if (hpet_mode == "periodic")
  138. return true;
  139. if (hpet_mode == "nonperiodic")
  140. return false;
  141. ASSERT_NOT_REACHED();
  142. }
  143. bool TimeManagement::probe_and_set_non_legacy_hardware_timers()
  144. {
  145. if (!ACPI::is_enabled())
  146. return false;
  147. if (!HPET::test_and_initialize())
  148. return false;
  149. if (!HPET::the().comparators().size()) {
  150. dbg() << "HPET initialization aborted.";
  151. return false;
  152. }
  153. dbg() << "HPET: Setting appropriate functions to timers.";
  154. for (auto& hpet_comparator : HPET::the().comparators())
  155. m_hardware_timers.append(hpet_comparator);
  156. auto periodic_timers = scan_and_initialize_periodic_timers();
  157. auto non_periodic_timers = scan_for_non_periodic_timers();
  158. if (is_hpet_periodic_mode_allowed())
  159. ASSERT(!periodic_timers.is_empty());
  160. ASSERT(periodic_timers.size() + non_periodic_timers.size() >= 2);
  161. if (periodic_timers.size() >= 2) {
  162. m_time_keeper_timer = periodic_timers[1];
  163. m_system_timer = periodic_timers[0];
  164. } else {
  165. if (periodic_timers.size() == 1) {
  166. m_time_keeper_timer = periodic_timers[0];
  167. m_system_timer = non_periodic_timers[0];
  168. } else {
  169. m_time_keeper_timer = non_periodic_timers[1];
  170. m_system_timer = non_periodic_timers[0];
  171. }
  172. }
  173. m_system_timer->change_function(Scheduler::timer_tick);
  174. dbg() << "Reset timers";
  175. m_system_timer->try_to_set_frequency(m_system_timer->calculate_nearest_possible_frequency(1024));
  176. m_time_keeper_timer->change_function(TimeManagement::update_time);
  177. m_time_keeper_timer->try_to_set_frequency(OPTIMAL_TICKS_PER_SECOND_RATE);
  178. return true;
  179. }
  180. bool TimeManagement::probe_and_set_legacy_hardware_timers()
  181. {
  182. if (ACPI::is_enabled()) {
  183. if (ACPI::Parser::the()->x86_specific_flags().cmos_rtc_not_present) {
  184. dbg() << "ACPI: CMOS RTC Not Present";
  185. return false;
  186. } else {
  187. dbg() << "ACPI: CMOS RTC Present";
  188. }
  189. }
  190. m_hardware_timers.append(PIT::initialize(TimeManagement::update_time));
  191. m_hardware_timers.append(RealTimeClock::create(Scheduler::timer_tick));
  192. m_time_keeper_timer = m_hardware_timers[0];
  193. m_system_timer = m_hardware_timers[1];
  194. return true;
  195. }
  196. void TimeManagement::update_time(const RegisterState& regs)
  197. {
  198. TimeManagement::the().increment_time_since_boot(regs);
  199. }
  200. void TimeManagement::increment_time_since_boot(const RegisterState&)
  201. {
  202. ASSERT(!m_time_keeper_timer.is_null());
  203. if (++m_ticks_this_second >= m_time_keeper_timer->ticks_per_second()) {
  204. // FIXME: Synchronize with other clock somehow to prevent drifting apart.
  205. ++m_seconds_since_boot;
  206. ++m_epoch_time;
  207. m_ticks_this_second = 0;
  208. }
  209. }
  210. }