APIC.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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/StringView.h>
  28. #include <AK/Types.h>
  29. #include <Kernel/Arch/i386/CPU.h>
  30. #include <Kernel/Interrupts/APIC.h>
  31. #include <Kernel/Interrupts/SpuriousInterruptHandler.h>
  32. #include <Kernel/VM/MemoryManager.h>
  33. #include <LibBareMetal/IO.h>
  34. #define IRQ_APIC_SPURIOUS 0x7f
  35. #define APIC_BASE_MSR 0x1b
  36. #define APIC_REG_EOI 0xb0
  37. #define APIC_REG_LD 0xd0
  38. #define APIC_REG_DF 0xe0
  39. #define APIC_REG_SIV 0xf0
  40. #define APIC_REG_TPR 0x80
  41. #define APIC_REG_ICR_LOW 0x300
  42. #define APIC_REG_ICR_HIGH 0x310
  43. #define APIC_REG_LVT_TIMER 0x320
  44. #define APIC_REG_LVT_THERMAL 0x330
  45. #define APIC_REG_LVT_PERFORMANCE_COUNTER 0x340
  46. #define APIC_REG_LVT_LINT0 0x350
  47. #define APIC_REG_LVT_LINT1 0x360
  48. #define APIC_REG_LVT_ERR 0x370
  49. namespace Kernel {
  50. namespace APIC {
  51. class ICRReg {
  52. u32 m_reg { 0 };
  53. public:
  54. enum DeliveryMode {
  55. Fixed = 0x0,
  56. LowPriority = 0x1,
  57. SMI = 0x2,
  58. NMI = 0x4,
  59. INIT = 0x5,
  60. StartUp = 0x6,
  61. };
  62. enum DestinationMode {
  63. Physical = 0x0,
  64. Logical = 0x0,
  65. };
  66. enum Level {
  67. DeAssert = 0x0,
  68. Assert = 0x1
  69. };
  70. enum class TriggerMode {
  71. Edge = 0x0,
  72. Level = 0x1,
  73. };
  74. enum DestinationShorthand {
  75. NoShorthand = 0x0,
  76. Self = 0x1,
  77. AllIncludingSelf = 0x2,
  78. AllExcludingSelf = 0x3,
  79. };
  80. ICRReg(u8 vector, DeliveryMode delivery_mode, DestinationMode destination_mode, Level level, TriggerMode trigger_mode, DestinationShorthand destination)
  81. : m_reg(vector | (delivery_mode << 8) | (destination_mode << 11) | (level << 14) | (static_cast<u32>(trigger_mode) << 15) | (destination << 18))
  82. {
  83. }
  84. u32 low() const { return m_reg; }
  85. u32 high() const { return 0; }
  86. };
  87. static volatile u8* g_apic_base = nullptr;
  88. static PhysicalAddress get_base()
  89. {
  90. u32 lo, hi;
  91. MSR msr(APIC_BASE_MSR);
  92. msr.get(lo, hi);
  93. return PhysicalAddress(lo & 0xfffff000);
  94. }
  95. static void set_base(const PhysicalAddress& base)
  96. {
  97. u32 hi = 0;
  98. u32 lo = base.get() | 0x800;
  99. MSR msr(APIC_BASE_MSR);
  100. msr.set(lo, hi);
  101. }
  102. static void write_register(u32 offset, u32 value)
  103. {
  104. auto lapic_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)g_apic_base)), PAGE_SIZE, "LAPIC Write Access", Region::Access::Read | Region::Access::Write, false, true);
  105. auto* lapic = (volatile u32*)lapic_region->vaddr().offset(offset_in_page((u32)g_apic_base)).offset(offset).as_ptr();
  106. *lapic = value;
  107. }
  108. static u32 read_register(u32 offset)
  109. {
  110. auto lapic_region = MM.allocate_kernel_region(PhysicalAddress(page_base_of((u32)g_apic_base)), PAGE_SIZE, "LAPIC Read Access", Region::Access::Read, false, true);
  111. auto* lapic = (volatile u32*)lapic_region->vaddr().offset(offset_in_page((u32)g_apic_base)).offset(offset).as_ptr();
  112. return *lapic;
  113. }
  114. static void write_icr(const ICRReg& icr)
  115. {
  116. write_register(APIC_REG_ICR_HIGH, icr.high());
  117. write_register(APIC_REG_ICR_LOW, icr.low());
  118. }
  119. #define APIC_LVT_MASKED (1 << 16)
  120. #define APIC_LVT_TRIGGER_LEVEL (1 << 14)
  121. #define APIC_LVT(iv, dm) ((iv & 0xff) | ((dm & 0x7) << 8))
  122. asm(
  123. ".globl apic_ap_start \n"
  124. ".type apic_ap_start, @function \n"
  125. "apic_ap_start: \n"
  126. ".set begin_apic_ap_start, . \n"
  127. " jmp apic_ap_start\n" // TODO: implement
  128. ".set end_apic_ap_start, . \n"
  129. "\n"
  130. ".globl apic_ap_start_size \n"
  131. "apic_ap_start_size: \n"
  132. ".word end_apic_ap_start - begin_apic_ap_start \n");
  133. extern "C" void apic_ap_start(void);
  134. extern "C" u16 apic_ap_start_size;
  135. void eoi()
  136. {
  137. write_register(APIC_REG_EOI, 0x0);
  138. }
  139. u8 spurious_interrupt_vector()
  140. {
  141. return IRQ_APIC_SPURIOUS;
  142. }
  143. bool init()
  144. {
  145. // FIXME: Use the ACPI MADT table
  146. if (!MSR::have())
  147. return false;
  148. // check if we support local apic
  149. CPUID id(1);
  150. if ((id.edx() & (1 << 9)) == 0)
  151. return false;
  152. PhysicalAddress apic_base = get_base();
  153. klog() << "Initializing APIC, base: " << apic_base;
  154. set_base(apic_base);
  155. g_apic_base = apic_base.as_ptr();
  156. return true;
  157. }
  158. void enable_bsp()
  159. {
  160. // FIXME: Ensure this method can only be executed by the BSP.
  161. enable(0);
  162. }
  163. void enable(u32 cpu)
  164. {
  165. klog() << "Enabling local APIC for cpu #" << cpu;
  166. // dummy read, apparently to avoid a bug in old CPUs.
  167. read_register(APIC_REG_SIV);
  168. // set spurious interrupt vector
  169. write_register(APIC_REG_SIV, (IRQ_APIC_SPURIOUS + IRQ_VECTOR_BASE) | 0x100);
  170. // local destination mode (flat mode)
  171. write_register(APIC_REG_DF, 0xf0000000);
  172. // set destination id (note that this limits it to 8 cpus)
  173. write_register(APIC_REG_LD, 0);
  174. SpuriousInterruptHandler::initialize(IRQ_APIC_SPURIOUS);
  175. write_register(APIC_REG_LVT_TIMER, APIC_LVT(0, 0) | APIC_LVT_MASKED);
  176. write_register(APIC_REG_LVT_THERMAL, APIC_LVT(0, 0) | APIC_LVT_MASKED);
  177. write_register(APIC_REG_LVT_PERFORMANCE_COUNTER, APIC_LVT(0, 0) | APIC_LVT_MASKED);
  178. write_register(APIC_REG_LVT_LINT0, APIC_LVT(0, 7) | APIC_LVT_MASKED);
  179. write_register(APIC_REG_LVT_LINT1, APIC_LVT(0, 0) | APIC_LVT_TRIGGER_LEVEL);
  180. write_register(APIC_REG_LVT_ERR, APIC_LVT(0, 0) | APIC_LVT_MASKED);
  181. write_register(APIC_REG_TPR, 0);
  182. if (cpu != 0) {
  183. // INIT
  184. write_icr(ICRReg(0, ICRReg::INIT, ICRReg::Physical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf));
  185. IO::delay(10 * 1000);
  186. for (int i = 0; i < 2; i++) {
  187. // SIPI
  188. write_icr(ICRReg(0x08, ICRReg::StartUp, ICRReg::Physical, ICRReg::Assert, ICRReg::TriggerMode::Edge, ICRReg::AllExcludingSelf)); // start execution at P8000
  189. IO::delay(200);
  190. }
  191. }
  192. }
  193. }
  194. }