|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 Waldemar Kozaczuk |
| 3 | + * |
| 4 | + * This work is open source software, licensed under the terms of the |
| 5 | + * BSD license as described in the LICENSE file in the top-level directory. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <osv/numa.hh> |
| 9 | +#include <osv/sched.hh> |
| 10 | +#include <osv/debug.hh> |
| 11 | + |
| 12 | +#include <unordered_map> |
| 13 | +#include <algorithm> |
| 14 | + |
| 15 | +#include <osv/drivers_config.h> |
| 16 | + |
| 17 | +#if CONF_drivers_acpi |
| 18 | +extern "C" { |
| 19 | +#include "acpi.h" |
| 20 | +} |
| 21 | +#include <boost/intrusive/parent_from_member.hpp> |
| 22 | +#endif |
| 23 | + |
| 24 | +namespace numa { |
| 25 | + |
| 26 | +static bool s_available = false; |
| 27 | +static unsigned s_nr_nodes = 1; |
| 28 | +// Map from a raw APIC id to the NUMA node it belongs to (from SRAT). |
| 29 | +static std::unordered_map<uint32_t, unsigned> s_apic_to_node; |
| 30 | +// Map from a sched cpu id to its NUMA node (resolved via APIC id). |
| 31 | +static std::unordered_map<unsigned, unsigned> s_cpu_to_node; |
| 32 | +// SLIT distance matrix, row-major, s_nr_nodes x s_nr_nodes; empty if no SLIT. |
| 33 | +static std::vector<uint8_t> s_distances; |
| 34 | +static std::vector<mem_range> s_mem_ranges; |
| 35 | +static bool s_initialized = false; |
| 36 | + |
| 37 | +unsigned nr_nodes() { return s_nr_nodes; } |
| 38 | +bool available() { return s_available; } |
| 39 | +const std::vector<mem_range>& memory_ranges() { return s_mem_ranges; } |
| 40 | + |
| 41 | +unsigned node_of_cpu(unsigned cpu_id) |
| 42 | +{ |
| 43 | + auto it = s_cpu_to_node.find(cpu_id); |
| 44 | + return it == s_cpu_to_node.end() ? 0 : it->second; |
| 45 | +} |
| 46 | + |
| 47 | +unsigned distance(unsigned from, unsigned to) |
| 48 | +{ |
| 49 | + if (from == to) { |
| 50 | + return 10; // ACPI convention: 10 == local. |
| 51 | + } |
| 52 | + if (!s_distances.empty() && from < s_nr_nodes && to < s_nr_nodes) { |
| 53 | + return s_distances[from * s_nr_nodes + to]; |
| 54 | + } |
| 55 | + return 20; // Default remote distance when no SLIT is present. |
| 56 | +} |
| 57 | + |
| 58 | +#if CONF_drivers_acpi |
| 59 | +using boost::intrusive::get_parent_from_member; |
| 60 | + |
| 61 | +// Record the (apic id -> node) mapping and track the highest node seen. |
| 62 | +static void record_cpu_affinity(uint32_t apic_id, unsigned node, unsigned& max_node) |
| 63 | +{ |
| 64 | + s_apic_to_node[apic_id] = node; |
| 65 | + max_node = std::max(max_node, node); |
| 66 | +} |
| 67 | + |
| 68 | +static void parse_srat() |
| 69 | +{ |
| 70 | + char sig[] = ACPI_SIG_SRAT; |
| 71 | + ACPI_TABLE_HEADER* header; |
| 72 | + if (AcpiGetTable(sig, 0, &header) != AE_OK) { |
| 73 | + return; // No SRAT: leave the single-node fallback in place. |
| 74 | + } |
| 75 | + auto srat = get_parent_from_member(header, &ACPI_TABLE_SRAT::Header); |
| 76 | + void* sub = srat + 1; |
| 77 | + void* end = static_cast<void*>(srat) + srat->Header.Length; |
| 78 | + unsigned max_node = 0; |
| 79 | + |
| 80 | + while (sub < end) { |
| 81 | + auto s = static_cast<ACPI_SUBTABLE_HEADER*>(sub); |
| 82 | + if (s->Length == 0) { |
| 83 | + break; // Guard against a malformed zero-length subtable. |
| 84 | + } |
| 85 | + switch (s->Type) { |
| 86 | + case ACPI_SRAT_TYPE_CPU_AFFINITY: { |
| 87 | + auto a = get_parent_from_member(s, &ACPI_SRAT_CPU_AFFINITY::Header); |
| 88 | + if (a->Flags & ACPI_SRAT_CPU_ENABLED) { |
| 89 | + unsigned node = a->ProximityDomainLo | |
| 90 | + (a->ProximityDomainHi[0] << 8) | |
| 91 | + (a->ProximityDomainHi[1] << 16) | |
| 92 | + (a->ProximityDomainHi[2] << 24); |
| 93 | + record_cpu_affinity(a->ApicId, node, max_node); |
| 94 | + } |
| 95 | + break; |
| 96 | + } |
| 97 | + case ACPI_SRAT_TYPE_X2APIC_CPU_AFFINITY: { |
| 98 | + auto a = get_parent_from_member(s, &ACPI_SRAT_X2APIC_CPU_AFFINITY::Header); |
| 99 | + if (a->Flags & ACPI_SRAT_CPU_ENABLED) { |
| 100 | + record_cpu_affinity(a->ApicId, a->ProximityDomain, max_node); |
| 101 | + } |
| 102 | + break; |
| 103 | + } |
| 104 | + case ACPI_SRAT_TYPE_MEMORY_AFFINITY: { |
| 105 | + auto m = get_parent_from_member(s, &ACPI_SRAT_MEM_AFFINITY::Header); |
| 106 | + if (m->Flags & ACPI_SRAT_MEM_ENABLED) { |
| 107 | + s_mem_ranges.push_back(mem_range{ |
| 108 | + m->BaseAddress, m->Length, m->ProximityDomain, |
| 109 | + (m->Flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) != 0}); |
| 110 | + max_node = std::max(max_node, (unsigned)m->ProximityDomain); |
| 111 | + } |
| 112 | + break; |
| 113 | + } |
| 114 | + default: |
| 115 | + break; |
| 116 | + } |
| 117 | + sub = static_cast<void*>(sub) + s->Length; |
| 118 | + } |
| 119 | + |
| 120 | + if (!s_apic_to_node.empty() || !s_mem_ranges.empty()) { |
| 121 | + s_available = true; |
| 122 | + s_nr_nodes = max_node + 1; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +static void parse_slit() |
| 127 | +{ |
| 128 | + char sig[] = ACPI_SIG_SLIT; |
| 129 | + ACPI_TABLE_HEADER* header; |
| 130 | + if (AcpiGetTable(sig, 0, &header) != AE_OK) { |
| 131 | + return; |
| 132 | + } |
| 133 | + auto slit = get_parent_from_member(header, &ACPI_TABLE_SLIT::Header); |
| 134 | + uint64_t n = slit->LocalityCount; |
| 135 | + // Only trust SLIT if it agrees with the node count we saw in SRAT. |
| 136 | + if (n == 0 || n != s_nr_nodes) { |
| 137 | + return; |
| 138 | + } |
| 139 | + s_distances.assign(slit->Entry, slit->Entry + n * n); |
| 140 | +} |
| 141 | + |
| 142 | +// Resolve the (apic id -> node) map into a (sched cpu id -> node) map. |
| 143 | +static void resolve_cpus() |
| 144 | +{ |
| 145 | + for (auto* c : sched::cpus) { |
| 146 | + auto it = s_apic_to_node.find(c->arch.apic_id); |
| 147 | + if (it != s_apic_to_node.end()) { |
| 148 | + s_cpu_to_node[c->id] = it->second; |
| 149 | + } |
| 150 | + } |
| 151 | +} |
| 152 | +#endif |
| 153 | + |
| 154 | +void init() |
| 155 | +{ |
| 156 | + if (s_initialized) { |
| 157 | + return; |
| 158 | + } |
| 159 | + s_initialized = true; |
| 160 | + |
| 161 | +#if CONF_drivers_acpi |
| 162 | + parse_srat(); |
| 163 | + if (s_available) { |
| 164 | + parse_slit(); |
| 165 | + resolve_cpus(); |
| 166 | + } |
| 167 | +#endif |
| 168 | + |
| 169 | + if (s_available) { |
| 170 | + debugf("NUMA: %u node(s), %zu CPU(s) mapped, %zu memory range(s)%s\n", |
| 171 | + s_nr_nodes, s_cpu_to_node.size(), s_mem_ranges.size(), |
| 172 | + s_distances.empty() ? ", no SLIT" : ""); |
| 173 | + } else { |
| 174 | + debugf("NUMA: no SRAT, assuming a single flat node\n"); |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +} |
0 commit comments