APIC.cpp 20 KB

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