|
28 | 28 | #define APIC_VERSION 0x0030 /**< LAPIC Version Register (read-only) */ |
29 | 29 |
|
30 | 30 | /** |
31 | | - * @brief Read a 32-bit value from a LAPIC register |
| 31 | + * @brief Read a Local APIC register. |
32 | 32 | * |
33 | | - * @param reg Register offset from LAPIC base |
34 | | - * @return uint32_t The value read from the register |
| 33 | + * Works in both xAPIC and x2APIC modes: |
| 34 | + * - In xAPIC mode, the register is memory‑mapped at the LAPIC base address. |
| 35 | + * - In x2APIC mode, registers are accessed via MSRs at 0x800 + (reg >> 4). |
| 36 | + * |
| 37 | + * @param reg Register offset (e.g. APIC_EOI = 0xB0). |
| 38 | + * @return uint32_t Value read from the register. |
35 | 39 | */ |
36 | 40 | uint32_t apic_read(uint64_t reg); |
37 | 41 |
|
38 | 42 | /** |
39 | | - * @brief Write a 32-bit value to a LAPIC register |
| 43 | + * @brief Write to a Local APIC register. |
| 44 | + * |
| 45 | + * Works in both xAPIC and x2APIC modes: |
| 46 | + * - In xAPIC mode, the register is memory‑mapped at the LAPIC base address. |
| 47 | + * - In x2APIC mode, registers are written via MSRs at 0x800 + (reg >> 4). |
40 | 48 | * |
41 | | - * @param reg Register offset from LAPIC base |
42 | | - * @param value The value to write |
| 49 | + * @param reg Register offset (e.g. APIC_EOI = 0xB0). |
| 50 | + * @param val Value to write. |
43 | 51 | */ |
44 | 52 | void apic_write(uint64_t reg, uint32_t value); |
45 | 53 |
|
46 | 54 | /** |
47 | | - * @brief Get the Local APIC ID of the current CPU |
| 55 | + * @brief Get the Local APIC ID of the current CPU. |
| 56 | + * |
| 57 | + * In xAPIC mode this is an 8-bit value read from the LAPIC ID register. |
| 58 | + * In x2APIC mode this is a 32-bit value read from MSR 0x802. |
| 59 | + * |
| 60 | + * @return uint32_t LAPIC ID of the current CPU. |
| 61 | + * @note LAPIC IDs are assigned by hardware/firmware and may be |
| 62 | + * sparse, non-contiguous, or not zero-based. |
| 63 | + */ |
| 64 | +uint32_t cpu_id(void); |
| 65 | + |
| 66 | +/** |
| 67 | + * @brief Get the OS-assigned logical CPU ID of the current CPU. |
| 68 | + * |
| 69 | + * Logical CPU IDs are zero-based, contiguous indices used internally |
| 70 | + * by the kernel. They are mapped to LAPIC IDs via cpu_id_mapping[]. |
| 71 | + * |
| 72 | + * @return uint8_t Logical CPU ID of the current CPU. |
| 73 | + */ |
| 74 | +uint8_t logical_cpu_id(void); |
| 75 | + |
| 76 | +/** |
| 77 | + * @brief Get the physical address of the Local APIC MMIO region. |
48 | 78 | * |
49 | | - * @return uint8_t APIC ID read from the LAPIC ID register. |
50 | | - * This value uniquely identifies the core in an SMP system and |
51 | | - * may be non-contiguous or BIOS-assigned. |
| 79 | + * This is only valid when xAPIC mode is active. In x2APIC mode, |
| 80 | + * LAPIC registers are accessed via MSRs instead of MMIO. |
| 81 | + * |
| 82 | + * @return uint64_t Physical address of the LAPIC MMIO base. |
| 83 | + */ |
| 84 | +uint64_t get_lapic_address(void); |
| 85 | + |
| 86 | +/** |
| 87 | + * @brief Determine if x2APIC mode is enabled. |
| 88 | + * |
| 89 | + * Checks both CPUID feature flags and the IA32_APIC_BASE MSR to confirm |
| 90 | + * whether x2APIC is active. |
| 91 | + * |
| 92 | + * @return int Non-zero if x2APIC mode is enabled, zero otherwise. |
52 | 93 | */ |
53 | | -uint8_t cpu_id(); |
| 94 | +int x2apic_enabled(void); |
54 | 95 |
|
| 96 | +/** |
| 97 | + * @brief Read a 64-bit Model Specific Register (MSR). |
| 98 | + * |
| 99 | + * @param msr The MSR address to read. |
| 100 | + * @return uint64_t The value read from the MSR. |
| 101 | + */ |
| 102 | +uint64_t rdmsr(uint32_t msr); |
55 | 103 |
|
56 | 104 | /** |
57 | | - * @brief Get the physical address of the Local APIC |
| 105 | + * @brief Map a logical CPU ID to its LAPIC ID. |
58 | 106 | * |
59 | | - * @return uint64_t Physical address of the LAPIC MMIO region |
| 107 | + * @param cpu_id Logical CPU ID (zero-based, kernel-assigned). |
| 108 | + * @return uint32_t LAPIC ID for the given logical CPU ID. |
60 | 109 | */ |
61 | | -uint64_t get_lapic_address(); |
| 110 | +uint32_t get_lapic_id_from_cpu_id(uint8_t cpu_id); |
62 | 111 |
|
| 112 | +/** |
| 113 | + * @brief Map a LAPIC ID to its logical CPU ID. |
| 114 | + * |
| 115 | + * @param lapic_id LAPIC ID as reported by hardware. |
| 116 | + * @return uint8_t Logical CPU ID assigned to this LAPIC ID, |
| 117 | + * or 255 if unmapped. |
| 118 | + */ |
| 119 | +uint8_t get_cpu_id_from_lapic_id(uint32_t lapic_id); |
0 commit comments