TimeManagement.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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/ACPIParser.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. bool TimeManagement::initialized()
  40. {
  41. return s_time_management != nullptr;
  42. }
  43. bool TimeManagement::is_system_timer(const HardwareTimer& timer) const
  44. {
  45. return &timer == m_system_timer.ptr();
  46. }
  47. void TimeManagement::set_epoch_time(time_t value)
  48. {
  49. InterruptDisabler disabler;
  50. m_epoch_time = value;
  51. }
  52. time_t TimeManagement::epoch_time() const
  53. {
  54. return m_epoch_time;
  55. }
  56. void TimeManagement::initialize(bool probe_non_legacy_hardware_timers)
  57. {
  58. ASSERT(!TimeManagement::initialized());
  59. s_time_management = new TimeManagement(probe_non_legacy_hardware_timers);
  60. }
  61. time_t TimeManagement::seconds_since_boot() const
  62. {
  63. return m_seconds_since_boot;
  64. }
  65. time_t TimeManagement::ticks_per_second() const
  66. {
  67. return m_system_timer->ticks_per_second();
  68. }
  69. time_t TimeManagement::ticks_this_second() const
  70. {
  71. return m_ticks_this_second;
  72. }
  73. time_t TimeManagement::boot_time() const
  74. {
  75. return RTC::boot_time();
  76. }
  77. void TimeManagement::stale_function(const RegisterState&)
  78. {
  79. }
  80. TimeManagement::TimeManagement(bool probe_non_legacy_hardware_timers)
  81. {
  82. if (ACPI::Parser::the().is_operable()) {
  83. if (!ACPI::Parser::the().x86_specific_flags().cmos_rtc_not_present) {
  84. RTC::initialize();
  85. m_epoch_time += boot_time();
  86. } else {
  87. klog() << "ACPI: RTC CMOS Not present";
  88. }
  89. } else {
  90. // We just assume that we can access RTC CMOS, if ACPI isn't usable.
  91. RTC::initialize();
  92. m_epoch_time += boot_time();
  93. }
  94. if (probe_non_legacy_hardware_timers) {
  95. if (!probe_and_set_non_legacy_hardware_timers())
  96. if (!probe_and_set_legacy_hardware_timers())
  97. ASSERT_NOT_REACHED();
  98. return;
  99. }
  100. if (probe_and_set_legacy_hardware_timers())
  101. return;
  102. ASSERT_NOT_REACHED();
  103. }
  104. Vector<size_t> TimeManagement::scan_and_initialize_periodic_timers()
  105. {
  106. bool enable_periodic_mode = is_hpet_periodic_mode_allowed();
  107. dbg() << "Scanning for Periodic timers";
  108. Vector<size_t> periodic_timers_indexes;
  109. periodic_timers_indexes.ensure_capacity(m_hardware_timers.size());
  110. for (size_t index = 0; index < m_hardware_timers.size(); index++) {
  111. if (!m_hardware_timers[index].is_null()) {
  112. if (m_hardware_timers[index]->is_periodic_capable()) {
  113. periodic_timers_indexes.append(index);
  114. if (enable_periodic_mode)
  115. m_hardware_timers[index]->set_periodic();
  116. }
  117. }
  118. }
  119. return periodic_timers_indexes;
  120. }
  121. Vector<size_t> TimeManagement::scan_for_non_periodic_timers()
  122. {
  123. dbg() << "Scanning for Non-Periodic timers";
  124. Vector<size_t> non_periodic_timers_indexes;
  125. non_periodic_timers_indexes.ensure_capacity(m_hardware_timers.size());
  126. for (size_t index = 0; index < m_hardware_timers.size(); index++) {
  127. if (!m_hardware_timers[index].is_null())
  128. if (!m_hardware_timers[index]->is_periodic_capable())
  129. non_periodic_timers_indexes.append(index);
  130. }
  131. return non_periodic_timers_indexes;
  132. }
  133. bool TimeManagement::is_hpet_periodic_mode_allowed()
  134. {
  135. if (!kernel_command_line().contains("hpet"))
  136. return true;
  137. auto hpet_mode = kernel_command_line().get("hpet");
  138. if (hpet_mode == "periodic")
  139. return true;
  140. if (hpet_mode == "nonperiodic")
  141. return false;
  142. ASSERT_NOT_REACHED();
  143. }
  144. bool TimeManagement::probe_and_set_non_legacy_hardware_timers()
  145. {
  146. if (!ACPI::Parser::the().is_operable())
  147. return false;
  148. if (!HPET::test_and_initialize())
  149. return false;
  150. if (!HPET::the().comparators().size()) {
  151. dbg() << "HPET initialization aborted.";
  152. return false;
  153. }
  154. dbg() << "HPET: Setting appropriate functions to timers.";
  155. m_hardware_timers.resize(HPET::the().comparators().size());
  156. for (size_t index = 0; index < m_hardware_timers.size(); index++) {
  157. m_hardware_timers[index] = HPET::the().comparators()[index];
  158. #ifdef TIME_DEBUG
  159. dbg() << m_hardware_timers[index].ptr() << " <- " << HPET::the().comparators()[index].ptr();
  160. #endif
  161. }
  162. auto periodic_timer_indexes = scan_and_initialize_periodic_timers();
  163. auto non_periodic_timer_indexes = scan_for_non_periodic_timers();
  164. if (is_hpet_periodic_mode_allowed())
  165. ASSERT(!periodic_timer_indexes.is_empty());
  166. ASSERT(periodic_timer_indexes.size() + non_periodic_timer_indexes.size() >= 2);
  167. if (periodic_timer_indexes.size() >= 2) {
  168. m_time_keeper_timer = m_hardware_timers[periodic_timer_indexes[1]];
  169. m_system_timer = m_hardware_timers[periodic_timer_indexes[0]];
  170. } else {
  171. if (periodic_timer_indexes.size() == 1) {
  172. m_time_keeper_timer = m_hardware_timers[periodic_timer_indexes[0]];
  173. m_system_timer = m_hardware_timers[non_periodic_timer_indexes[0]];
  174. } else {
  175. m_time_keeper_timer = m_hardware_timers[non_periodic_timer_indexes[1]];
  176. m_system_timer = m_hardware_timers[non_periodic_timer_indexes[0]];
  177. }
  178. }
  179. m_system_timer->change_function([](const RegisterState& regs) { update_scheduler_ticks(regs); });
  180. dbg() << "Reset timers";
  181. m_system_timer->try_to_set_frequency(m_system_timer->calculate_nearest_possible_frequency(1024));
  182. m_time_keeper_timer->change_function([](const RegisterState& regs) { update_time(regs); });
  183. m_time_keeper_timer->try_to_set_frequency(OPTIMAL_TICKS_PER_SECOND_RATE);
  184. return true;
  185. }
  186. bool TimeManagement::probe_and_set_legacy_hardware_timers()
  187. {
  188. if (ACPI::Parser::the().is_operable()) {
  189. if (ACPI::Parser::the().x86_specific_flags().cmos_rtc_not_present) {
  190. dbg() << "ACPI: CMOS RTC Not Present";
  191. return false;
  192. } else {
  193. dbg() << "ACPI: CMOS RTC Present";
  194. }
  195. }
  196. m_hardware_timers[0] = PIT::initialize([](const RegisterState& regs) { update_time(regs); });
  197. m_hardware_timers[1] = RealTimeClock::create([](const RegisterState& regs) { update_scheduler_ticks(regs); });
  198. m_time_keeper_timer = m_hardware_timers[0];
  199. m_system_timer = m_hardware_timers[1];
  200. return true;
  201. }
  202. TimeManagement& TimeManagement::the()
  203. {
  204. ASSERT(TimeManagement::initialized());
  205. return *s_time_management;
  206. }
  207. void TimeManagement::update_time(const RegisterState& regs)
  208. {
  209. TimeManagement::the().increment_time_since_boot(regs);
  210. }
  211. void TimeManagement::increment_time_since_boot(const RegisterState&)
  212. {
  213. ASSERT(!m_time_keeper_timer.is_null());
  214. if (++m_ticks_this_second >= m_time_keeper_timer->ticks_per_second()) {
  215. // FIXME: Synchronize with other clock somehow to prevent drifting apart.
  216. ++m_seconds_since_boot;
  217. ++m_epoch_time;
  218. m_ticks_this_second = 0;
  219. }
  220. }
  221. void TimeManagement::update_scheduler_ticks(const RegisterState& regs)
  222. {
  223. TimeManagement::the().update_ticks(regs);
  224. }
  225. void TimeManagement::update_ticks(const RegisterState& regs)
  226. {
  227. Scheduler::timer_tick(regs);
  228. }
  229. }