APIC.cpp 20 KB

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