TimeManagement.cpp 8.6 KB

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