APIC.cpp 7.5 KB

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