APIC.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Assertions.h>
  7. #include <AK/Memory.h>
  8. #include <AK/Singleton.h>
  9. #include <AK/Types.h>
  10. #include <Kernel/ACPI/Parser.h>
  11. #include <Kernel/Arch/x86/MSR.h>
  12. #include <Kernel/Arch/x86/ProcessorInfo.h>
  13. #include <Kernel/Debug.h>
  14. #include <Kernel/IO.h>
  15. #include <Kernel/Interrupts/APIC.h>
  16. #include <Kernel/Interrupts/SpuriousInterruptHandler.h>
  17. #include <Kernel/Panic.h>
  18. #include <Kernel/Sections.h>
  19. #include <Kernel/Thread.h>
  20. #include <Kernel/Time/APICTimer.h>
  21. #include <Kernel/VM/MemoryManager.h>
  22. #include <Kernel/VM/PageDirectory.h>
  23. #include <Kernel/VM/TypedMapping.h>
  24. #define IRQ_APIC_TIMER (0xfc - IRQ_VECTOR_BASE)
  25. #define IRQ_APIC_IPI (0xfd - IRQ_VECTOR_BASE)
  26. #define IRQ_APIC_ERR (0xfe - IRQ_VECTOR_BASE)
  27. #define IRQ_APIC_SPURIOUS (0xff - IRQ_VECTOR_BASE)
  28. #define APIC_ICR_DELIVERY_PENDING (1 << 12)
  29. #define APIC_ENABLED (1 << 8)
  30. #define APIC_BASE_MSR 0x1b
  31. #define APIC_REG_EOI 0xb0
  32. #define APIC_REG_LD 0xd0
  33. #define APIC_REG_DF 0xe0
  34. #define APIC_REG_SIV 0xf0
  35. #define APIC_REG_TPR 0x80
  36. #define APIC_REG_ICR_LOW 0x300
  37. #define APIC_REG_ICR_HIGH 0x310
  38. #define APIC_REG_LVT_TIMER 0x320
  39. #define APIC_REG_LVT_THERMAL 0x330
  40. #define APIC_REG_LVT_PERFORMANCE_COUNTER 0x340
  41. #define APIC_REG_LVT_LINT0 0x350
  42. #define APIC_REG_LVT_LINT1 0x360
  43. #define APIC_REG_LVT_ERR 0x370
  44. #define APIC_REG_TIMER_INITIAL_COUNT 0x380
  45. #define APIC_REG_TIMER_CURRENT_COUNT 0x390
  46. #define APIC_REG_TIMER_CONFIGURATION 0x3e0
  47. namespace Kernel {
  48. static AK::Singleton<APIC> s_apic;
  49. class APICIPIInterruptHandler final : public GenericInterruptHandler {
  50. public:
  51. explicit APICIPIInterruptHandler(u8 interrupt_vector)
  52. : GenericInterruptHandler(interrupt_vector, true)
  53. {
  54. }
  55. virtual ~APICIPIInterruptHandler()
  56. {
  57. }
  58. static void initialize(u8 interrupt_number)
  59. {
  60. auto* handler = new APICIPIInterruptHandler(interrupt_number);
  61. handler->register_interrupt_handler();
  62. }
  63. virtual bool handle_interrupt(const RegisterState&) override;
  64. virtual bool eoi() override;
  65. virtual HandlerType type() const override { return HandlerType::IRQHandler; }
  66. virtual const char* purpose() const override { return "IPI Handler"; }
  67. virtual const char* controller() const override { return nullptr; }
  68. virtual size_t sharing_devices_count() const override { return 0; }
  69. virtual bool is_shared_handler() const override { return false; }
  70. virtual bool is_sharing_with_others() const override { return false; }
  71. private:
  72. };
  73. class APICErrInterruptHandler final : public GenericInterruptHandler {
  74. public:
  75. explicit APICErrInterruptHandler(u8 interrupt_vector)
  76. : GenericInterruptHandler(interrupt_vector, true)
  77. {
  78. }
  79. virtual ~APICErrInterruptHandler()
  80. {
  81. }
  82. static void initialize(u8 interrupt_number)
  83. {
  84. auto* handler = new APICErrInterruptHandler(interrupt_number);
  85. handler->register_interrupt_handler();
  86. }
  87. virtual bool handle_interrupt(const RegisterState&) override;
  88. virtual bool eoi() override;
  89. virtual HandlerType type() const override { return HandlerType::IRQHandler; }
  90. virtual const char* purpose() const override { return "SMP Error Handler"; }
  91. virtual const char* controller() const override { return nullptr; }
  92. virtual size_t sharing_devices_count() const override { return 0; }
  93. virtual bool is_shared_handler() const override { return false; }
  94. virtual bool is_sharing_with_others() const override { return false; }
  95. private:
  96. };
  97. bool APIC::initialized()
  98. {
  99. return s_apic.is_initialized();
  100. }
  101. APIC& APIC::the()
  102. {
  103. VERIFY(APIC::initialized());
  104. return *s_apic;
  105. }
  106. UNMAP_AFTER_INIT void APIC::initialize()
  107. {
  108. VERIFY(!APIC::initialized());
  109. s_apic.ensure_instance();
  110. }
  111. PhysicalAddress APIC::get_base()
  112. {
  113. MSR msr(APIC_BASE_MSR);
  114. auto base = msr.get();
  115. return PhysicalAddress(base & 0xfffff000);
  116. }
  117. void APIC::set_base(const PhysicalAddress& base)
  118. {
  119. MSR msr(APIC_BASE_MSR);
  120. msr.set(base.get() | 0x800);
  121. }
  122. void APIC::write_register(u32 offset, u32 value)
  123. {
  124. *reinterpret_cast<volatile u32*>(m_apic_base->vaddr().offset(offset).as_ptr()) = value;
  125. }
  126. u32 APIC::read_register(u32 offset)
  127. {
  128. return *reinterpret_cast<volatile u32*>(m_apic_base->vaddr().offset(offset).as_ptr());
  129. }
  130. void APIC::set_lvt(u32 offset, u8 interrupt)
  131. {
  132. write_register(offset, (read_register(offset) & 0xffffffff) | interrupt);
  133. }
  134. void APIC::set_siv(u32 offset, u8 interrupt)
  135. {
  136. write_register(offset, (read_register(offset) & 0xffffffff) | interrupt | APIC_ENABLED);
  137. }
  138. void APIC::wait_for_pending_icr()
  139. {
  140. while ((read_register(APIC_REG_ICR_LOW) & APIC_ICR_DELIVERY_PENDING) != 0) {
  141. IO::delay(200);
  142. }
  143. }
  144. void APIC::write_icr(const ICRReg& icr)
  145. {
  146. write_register(APIC_REG_ICR_HIGH, icr.high());
  147. write_register(APIC_REG_ICR_LOW, icr.low());
  148. }
  149. #define APIC_LVT_TIMER_ONESHOT 0
  150. #define APIC_LVT_TIMER_PERIODIC (1 << 17)
  151. #define APIC_LVT_TIMER_TSCDEADLINE (1 << 18)
  152. #define APIC_LVT_MASKED (1 << 16)
  153. #define APIC_LVT_TRIGGER_LEVEL (1 << 14)
  154. #define APIC_LVT(iv, dm) (((iv)&0xff) | (((dm)&0x7) << 8))
  155. extern "C" void apic_ap_start(void);
  156. extern "C" u16 apic_ap_start_size;
  157. extern "C" u32 ap_cpu_init_stacks;
  158. extern "C" u32 ap_cpu_init_processor_info_array;
  159. extern "C" u32 ap_cpu_init_cr0;
  160. extern "C" u32 ap_cpu_init_cr3;
  161. extern "C" u32 ap_cpu_init_cr4;
  162. extern "C" u32 ap_cpu_gdtr;
  163. extern "C" u32 ap_cpu_idtr;
  164. void APIC::eoi()
  165. {
  166. write_register(APIC_REG_EOI, 0x0);
  167. }
  168. u8 APIC::spurious_interrupt_vector()
  169. {
  170. return IRQ_APIC_SPURIOUS;
  171. }
  172. #define APIC_INIT_VAR_PTR(tpe, vaddr, varname) \
  173. reinterpret_cast<volatile tpe*>(reinterpret_cast<ptrdiff_t>(vaddr) \
  174. + reinterpret_cast<ptrdiff_t>(&varname) \
  175. - reinterpret_cast<ptrdiff_t>(&apic_ap_start))
  176. UNMAP_AFTER_INIT bool APIC::init_bsp()
  177. {
  178. // FIXME: Use the ACPI MADT table
  179. if (!MSR::have())
  180. return false;
  181. // check if we support local apic
  182. CPUID id(1);
  183. if ((id.edx() & (1 << 9)) == 0)
  184. return false;
  185. PhysicalAddress apic_base = get_base();
  186. dbgln_if(APIC_DEBUG, "Initializing APIC, base: {}", apic_base);
  187. set_base(apic_base);
  188. m_apic_base = MM.allocate_kernel_region(apic_base.page_base(), PAGE_SIZE, {}, Region::Access::Read | Region::Access::Write);
  189. if (!m_apic_base) {
  190. dbgln("APIC: Failed to allocate memory for APIC base");
  191. return false;
  192. }
  193. auto rsdp = ACPI::StaticParsing::find_rsdp();
  194. if (!rsdp.has_value()) {
  195. dbgln("APIC: RSDP not found");
  196. return false;
  197. }
  198. auto madt_address = ACPI::StaticParsing::find_table(rsdp.value(), "APIC");
  199. if (madt_address.is_null()) {
  200. dbgln("APIC: MADT table not found");
  201. return false;
  202. }
  203. auto madt = map_typed<ACPI::Structures::MADT>(madt_address);
  204. size_t entry_index = 0;
  205. size_t entries_length = madt->h.length - sizeof(ACPI::Structures::MADT);
  206. auto* madt_entry = madt->entries;
  207. while (entries_length > 0) {
  208. size_t entry_length = madt_entry->length;
  209. if (madt_entry->type == (u8)ACPI::Structures::MADTEntryType::LocalAPIC) {
  210. auto* plapic_entry = (const ACPI::Structures::MADTEntries::ProcessorLocalAPIC*)madt_entry;
  211. dbgln_if(APIC_DEBUG, "APIC: AP found @ MADT entry {}, processor ID: {}, APIC ID: {}, flags: {:#08x}", entry_index, plapic_entry->acpi_processor_id, plapic_entry->apic_id, plapic_entry->flags);
  212. m_processor_cnt++;
  213. if ((plapic_entry->flags & 0x1) != 0)
  214. m_processor_enabled_cnt++;
  215. }
  216. madt_entry = (ACPI::Structures::MADTEntryHeader*)(VirtualAddress(madt_entry).offset(entry_length).get());
  217. entries_length -= entry_length;
  218. entry_index++;
  219. }
  220. if (m_processor_enabled_cnt < 1)
  221. m_processor_enabled_cnt = 1;
  222. if (m_processor_cnt < 1)
  223. m_processor_cnt = 1;
  224. dbgln("APIC processors found: {}, enabled: {}", m_processor_cnt, m_processor_enabled_cnt);
  225. enable(0);
  226. return true;
  227. }
  228. UNMAP_AFTER_INIT void APIC::do_boot_aps()
  229. {
  230. VERIFY(m_processor_enabled_cnt > 1);
  231. u32 aps_to_enable = m_processor_enabled_cnt - 1;
  232. // Copy the APIC startup code and variables to P0x00008000
  233. // Also account for the data appended to:
  234. // * aps_to_enable u32 values for ap_cpu_init_stacks
  235. // * aps_to_enable u32 values for ap_cpu_init_processor_info_array
  236. auto apic_startup_region = MM.allocate_kernel_region_identity(PhysicalAddress(0x8000), page_round_up(apic_ap_start_size + (2 * aps_to_enable * sizeof(u32))), {}, Region::Access::Read | Region::Access::Write | Region::Access::Execute);
  237. memcpy(apic_startup_region->vaddr().as_ptr(), reinterpret_cast<const void*>(apic_ap_start), apic_ap_start_size);
  238. // Allocate enough stacks for all APs
  239. Vector<OwnPtr<Region>> apic_ap_stacks;
  240. for (u32 i = 0; i < aps_to_enable; i++) {
  241. auto stack_region = MM.allocate_kernel_region(Thread::default_kernel_stack_size, {}, Region::Access::Read | Region::Access::Write, AllocationStrategy::AllocateNow);
  242. if (!stack_region) {
  243. dbgln("APIC: Failed to allocate stack for AP #{}", i);
  244. return;
  245. }
  246. stack_region->set_stack(true);
  247. apic_ap_stacks.append(move(stack_region));
  248. }
  249. // Store pointers to all stacks for the APs to use
  250. auto ap_stack_array = APIC_INIT_VAR_PTR(u32, apic_startup_region->vaddr().as_ptr(), ap_cpu_init_stacks);
  251. VERIFY(aps_to_enable == apic_ap_stacks.size());
  252. for (size_t i = 0; i < aps_to_enable; i++) {
  253. ap_stack_array[i] = apic_ap_stacks[i]->vaddr().get() + Thread::default_kernel_stack_size;
  254. dbgln_if(APIC_DEBUG, "APIC: CPU[{}] stack at {}", i + 1, VirtualAddress { ap_stack_array[i] });
  255. }
  256. // Allocate Processor structures for all APs and store the pointer to the data
  257. m_ap_processor_info.resize(aps_to_enable);
  258. for (size_t i = 0; i < aps_to_enable; i++)
  259. m_ap_processor_info[i] = make<Processor>();
  260. auto ap_processor_info_array = &ap_stack_array[aps_to_enable];
  261. for (size_t i = 0; i < aps_to_enable; i++) {
  262. ap_processor_info_array[i] = FlatPtr(m_ap_processor_info[i].ptr());
  263. dbgln_if(APIC_DEBUG, "APIC: CPU[{}] processor at {}", i + 1, VirtualAddress { ap_processor_info_array[i] });
  264. }
  265. *APIC_INIT_VAR_PTR(u32, apic_startup_region->vaddr().as_ptr(), ap_cpu_init_processor_info_array) = FlatPtr(&ap_processor_info_array[0]);
  266. // Store the BSP's CR3 value for the APs to use
  267. *APIC_INIT_VAR_PTR(u32, apic_startup_region->vaddr().as_ptr(), ap_cpu_init_cr3) = MM.kernel_page_directory().cr3();
  268. // Store the BSP's GDT and IDT for the APs to use
  269. const auto& gdtr = Processor::current().get_gdtr();
  270. *APIC_INIT_VAR_PTR(u32, apic_startup_region->vaddr().as_ptr(), ap_cpu_gdtr) = FlatPtr(&gdtr);
  271. const auto& idtr = get_idtr();
  272. *APIC_INIT_VAR_PTR(u32, apic_startup_region->vaddr().as_ptr(), ap_cpu_idtr) = FlatPtr(&idtr);
  273. // Store the BSP's CR0 and CR4 values for the APs to use
  274. *APIC_INIT_VAR_PTR(u32, apic_startup_region->vaddr().as_ptr(), ap_cpu_init_cr0) = read_cr0();
  275. *APIC_INIT_VAR_PTR(u32, apic_startup_region->vaddr().as_ptr(), ap_cpu_init_cr4) = read_cr4();
  276. // Create an idle thread for each processor. We have to do this here
  277. // because we won't be able to send FlushTLB messages, so we have to
  278. // have all memory set up for the threads so that when the APs are
  279. // starting up, they can access all the memory properly
  280. m_ap_idle_threads.resize(aps_to_enable);
  281. for (u32 i = 0; i < aps_to_enable; i++)
  282. m_ap_idle_threads[i] = Scheduler::create_ap_idle_thread(i + 1);
  283. dbgln_if(APIC_DEBUG, "APIC: Starting {} AP(s)", aps_to_enable);
  284. // INIT
  285. write_icr(ICRReg(0, ICRReg::INIT, ICRReg::Physical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf));
  286. IO::delay(10 * 1000);
  287. for (int i = 0; i < 2; i++) {
  288. // SIPI
  289. write_icr(ICRReg(0x08, ICRReg::StartUp, ICRReg::Physical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf)); // start execution at P8000
  290. IO::delay(200);
  291. }
  292. // Now wait until the ap_cpu_init_pending variable dropped to 0, which means all APs are initialized and no longer need these special mappings
  293. if (m_apic_ap_count.load(AK::MemoryOrder::memory_order_consume) != aps_to_enable) {
  294. dbgln_if(APIC_DEBUG, "APIC: Waiting for {} AP(s) to finish initialization...", aps_to_enable);
  295. do {
  296. // Wait a little bit
  297. IO::delay(200);
  298. } while (m_apic_ap_count.load(AK::MemoryOrder::memory_order_consume) != aps_to_enable);
  299. }
  300. dbgln_if(APIC_DEBUG, "APIC: {} processors are initialized and running", m_processor_enabled_cnt);
  301. }
  302. UNMAP_AFTER_INIT void APIC::boot_aps()
  303. {
  304. if (m_processor_enabled_cnt <= 1)
  305. return;
  306. // We split this into another call because do_boot_aps() will cause
  307. // MM calls upon exit, and we don't want to call smp_enable before that
  308. do_boot_aps();
  309. // Enable SMP, which means IPIs may now be sent
  310. Processor::smp_enable();
  311. dbgln_if(APIC_DEBUG, "All processors initialized and waiting, trigger all to continue");
  312. // Now trigger all APs to continue execution (need to do this after
  313. // the regions have been freed so that we don't trigger IPIs
  314. m_apic_ap_continue.store(1, AK::MemoryOrder::memory_order_release);
  315. }
  316. UNMAP_AFTER_INIT void APIC::enable(u32 cpu)
  317. {
  318. if (cpu >= 8) {
  319. // TODO: x2apic support?
  320. PANIC("SMP support is currently limited to 8 CPUs!");
  321. }
  322. // Use the CPU# as logical apic id
  323. VERIFY(cpu <= 0xff);
  324. write_register(APIC_REG_LD, (read_register(APIC_REG_LD) & 0x00ffffff) | (cpu << 24)); // TODO: only if not in x2apic mode
  325. // read it back to make sure it's actually set
  326. auto apic_id = read_register(APIC_REG_LD) >> 24;
  327. Processor::current().info().set_apic_id(apic_id);
  328. dbgln_if(APIC_DEBUG, "Enabling local APIC for CPU #{}, logical APIC ID: {}", cpu, apic_id);
  329. if (cpu == 0) {
  330. SpuriousInterruptHandler::initialize(IRQ_APIC_SPURIOUS);
  331. // set error interrupt vector
  332. set_lvt(APIC_REG_LVT_ERR, IRQ_APIC_ERR);
  333. APICErrInterruptHandler::initialize(IRQ_APIC_ERR);
  334. // register IPI interrupt vector
  335. APICIPIInterruptHandler::initialize(IRQ_APIC_IPI);
  336. }
  337. // set spurious interrupt vector
  338. set_siv(APIC_REG_SIV, IRQ_APIC_SPURIOUS);
  339. // local destination mode (flat mode)
  340. write_register(APIC_REG_DF, 0xf0000000);
  341. write_register(APIC_REG_LVT_TIMER, APIC_LVT(0, 0) | APIC_LVT_MASKED);
  342. write_register(APIC_REG_LVT_THERMAL, APIC_LVT(0, 0) | APIC_LVT_MASKED);
  343. write_register(APIC_REG_LVT_PERFORMANCE_COUNTER, APIC_LVT(0, 0) | APIC_LVT_MASKED);
  344. write_register(APIC_REG_LVT_LINT0, APIC_LVT(0, 7) | APIC_LVT_MASKED);
  345. write_register(APIC_REG_LVT_LINT1, APIC_LVT(0, 0) | APIC_LVT_TRIGGER_LEVEL);
  346. write_register(APIC_REG_TPR, 0);
  347. }
  348. Thread* APIC::get_idle_thread(u32 cpu) const
  349. {
  350. VERIFY(cpu > 0);
  351. return m_ap_idle_threads[cpu - 1];
  352. }
  353. UNMAP_AFTER_INIT void APIC::init_finished(u32 cpu)
  354. {
  355. // This method is called once the boot stack is no longer needed
  356. VERIFY(cpu > 0);
  357. VERIFY(cpu < m_processor_enabled_cnt);
  358. // Since we're waiting on other APs here, we shouldn't have the
  359. // scheduler lock
  360. VERIFY(!g_scheduler_lock.own_lock());
  361. // Notify the BSP that we are done initializing. It will unmap the startup data at P8000
  362. m_apic_ap_count.fetch_add(1, AK::MemoryOrder::memory_order_acq_rel);
  363. dbgln_if(APIC_DEBUG, "APIC: CPU #{} initialized, waiting for all others", cpu);
  364. // The reason we're making all APs wait until the BSP signals them is that
  365. // we don't want APs to trigger IPIs (e.g. through MM) while the BSP
  366. // is unable to process them
  367. while (!m_apic_ap_continue.load(AK::MemoryOrder::memory_order_consume)) {
  368. IO::delay(200);
  369. }
  370. dbgln_if(APIC_DEBUG, "APIC: CPU #{} continues, all others are initialized", cpu);
  371. // do_boot_aps() freed memory, so we need to update our tlb
  372. Processor::flush_entire_tlb_local();
  373. // Now enable all the interrupts
  374. APIC::the().enable(cpu);
  375. }
  376. void APIC::broadcast_ipi()
  377. {
  378. dbgln_if(APIC_SMP_DEBUG, "SMP: Broadcast IPI from CPU #{}", Processor::id());
  379. wait_for_pending_icr();
  380. write_icr(ICRReg(IRQ_APIC_IPI + IRQ_VECTOR_BASE, ICRReg::Fixed, ICRReg::Logical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf));
  381. }
  382. void APIC::send_ipi(u32 cpu)
  383. {
  384. dbgln_if(APIC_SMP_DEBUG, "SMP: Send IPI from CPU #{} to CPU #{}", Processor::id(), cpu);
  385. VERIFY(cpu != Processor::id());
  386. VERIFY(cpu < 8);
  387. wait_for_pending_icr();
  388. write_icr(ICRReg(IRQ_APIC_IPI + IRQ_VECTOR_BASE, ICRReg::Fixed, ICRReg::Logical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::NoShorthand, cpu));
  389. }
  390. UNMAP_AFTER_INIT APICTimer* APIC::initialize_timers(HardwareTimerBase& calibration_timer)
  391. {
  392. if (!m_apic_base)
  393. return nullptr;
  394. // We should only initialize and calibrate the APIC timer once on the BSP!
  395. VERIFY(Processor::is_bootstrap_processor());
  396. VERIFY(!m_apic_timer);
  397. m_apic_timer = APICTimer::initialize(IRQ_APIC_TIMER, calibration_timer);
  398. return m_apic_timer;
  399. }
  400. void APIC::setup_local_timer(u32 ticks, TimerMode timer_mode, bool enable)
  401. {
  402. u32 flags = 0;
  403. switch (timer_mode) {
  404. case TimerMode::OneShot:
  405. flags |= APIC_LVT_TIMER_ONESHOT;
  406. break;
  407. case TimerMode::Periodic:
  408. flags |= APIC_LVT_TIMER_PERIODIC;
  409. break;
  410. case TimerMode::TSCDeadline:
  411. flags |= APIC_LVT_TIMER_TSCDEADLINE;
  412. break;
  413. }
  414. if (!enable)
  415. flags |= APIC_LVT_MASKED;
  416. write_register(APIC_REG_LVT_TIMER, APIC_LVT(IRQ_APIC_TIMER + IRQ_VECTOR_BASE, 0) | flags);
  417. u32 config = read_register(APIC_REG_TIMER_CONFIGURATION);
  418. config &= ~0xf; // clear divisor (bits 0-3)
  419. switch (get_timer_divisor()) {
  420. case 1:
  421. config |= (1 << 3) | 3;
  422. break;
  423. case 2:
  424. break;
  425. case 4:
  426. config |= 1;
  427. break;
  428. case 8:
  429. config |= 2;
  430. break;
  431. case 16:
  432. config |= 3;
  433. break;
  434. case 32:
  435. config |= (1 << 3);
  436. break;
  437. case 64:
  438. config |= (1 << 3) | 1;
  439. break;
  440. case 128:
  441. config |= (1 << 3) | 2;
  442. break;
  443. default:
  444. VERIFY_NOT_REACHED();
  445. }
  446. write_register(APIC_REG_TIMER_CONFIGURATION, config);
  447. if (timer_mode == TimerMode::Periodic)
  448. write_register(APIC_REG_TIMER_INITIAL_COUNT, ticks / get_timer_divisor());
  449. }
  450. u32 APIC::get_timer_current_count()
  451. {
  452. return read_register(APIC_REG_TIMER_CURRENT_COUNT);
  453. }
  454. u32 APIC::get_timer_divisor()
  455. {
  456. return 16;
  457. }
  458. bool APICIPIInterruptHandler::handle_interrupt(const RegisterState&)
  459. {
  460. dbgln_if(APIC_SMP_DEBUG, "APIC IPI on CPU #{}", Processor::id());
  461. return true;
  462. }
  463. bool APICIPIInterruptHandler::eoi()
  464. {
  465. dbgln_if(APIC_SMP_DEBUG, "SMP: IPI EOI");
  466. APIC::the().eoi();
  467. return true;
  468. }
  469. bool APICErrInterruptHandler::handle_interrupt(const RegisterState&)
  470. {
  471. dbgln("APIC: SMP error on CPU #{}", Processor::id());
  472. return true;
  473. }
  474. bool APICErrInterruptHandler::eoi()
  475. {
  476. APIC::the().eoi();
  477. return true;
  478. }
  479. bool HardwareTimer<GenericInterruptHandler>::eoi()
  480. {
  481. APIC::the().eoi();
  482. return true;
  483. }
  484. }