aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / core / smp.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* smp.c -- secondary core bring-up through PSCI CPU_ON (CK gate SMP). |
| 18 | * |
| 19 | * The boot core reads the MPIDR of every enabled or online-capable core from |
| 20 | * the ACPI MADT GICC entries (the firmware's own CPU list; QEMU virt with |
| 21 | * AAVMF gives ACPI, not a device tree, to this kernel), then for each |
| 22 | * secondary core: takes a 16 KiB stack from the frame allocator, fills a |
| 23 | * per-core context (our page-table root, MAIR, TCR, SCTLR, stack top), cleans |
| 24 | * it to the point of coherency (the core reads it with its MMU off) and calls |
| 25 | * PSCI CPU_ON (SMC64 function 0xC4000003) on the conduit the FADT names |
| 26 | * (report.c ck_psci_call). The core starts in arch/smp_entry.S at the |
| 27 | * firmware's EL (QEMU 8.2.2 target/arm/tcg/psci.c:144 starts it at EL2 when |
| 28 | * EL2 exists, else EL1), goes through the same EL2 -> EL1h path as the boot |
| 29 | * core (ck_enter_el1, ADR 0009) or the EL1 table switch (ck_switch_el1), and |
| 30 | * lands in ck_smp_secondary_main at EL1 with the MMU on our tables. There it |
| 31 | * records what it sees (MPIDR, EL, SCTLR, TTBR0, SP), publishes "checked in" |
| 32 | * with a release store and parks in WFE with all interrupts masked. |
| 33 | * |
| 34 | * The boot core waits at most CK_SMP_TIMEOUT_US for every started core and |
| 35 | * prints one line per core and a summary. It never panics: a core that never |
| 36 | * checks in, a PSCI error code, a duplicate or missing MPIDR, or a core that |
| 37 | * reports the wrong EL/MMU/tables/stack is printed and the summary says |
| 38 | * result=FAIL. scripts/qemu_ck_smp_test.sh judges the lines itself. |
| 39 | * |
| 40 | * TEST-ONLY mutation: a build with -DCK_TEST_SMP_SKIP_CPU=1 (make |
| 41 | * CK_TEST_SMP_SKIP_CPU=1, own OUT, refused with CK_HARDWARE_STAGING) never |
| 42 | * calls CPU_ON for the last secondary core in the MADT but still waits for |
| 43 | * it, so the gate must FAIL on that image. QEMU qualifies nothing physical. */ |
| 44 | #include "arch.h" |
| 45 | #include "ck_internal.h" |
| 46 | |
| 47 | #if defined(CK_TEST_SMP_SKIP_CPU) && defined(CK_HARDWARE_STAGING) |
| 48 | #error "CK_TEST_SMP_SKIP_CPU (TEST-only SMP mutation) cannot be combined with CK_HARDWARE_STAGING" |
| 49 | #endif |
| 50 | |
| 51 | #define CK_SMP_MAX 16u |
| 52 | #define CK_SMP_STACK_PAGES 4u |
| 53 | #define CK_SMP_TIMEOUT_US 5000000ull |
| 54 | #define PSCI_VERSION 0x84000000u |
| 55 | #define PSCI_CPU_ON_64 0xC4000003u |
| 56 | #define TTBR_BADDR_MASK 0x0000fffffffffffeull |
| 57 | |
| 58 | enum { SMP_IDLE = 0, SMP_ON_CALLED = 1, SMP_CHECKED_IN = 2 }; |
| 59 | |
| 60 | /* Field offsets 0..40 are read by arch/smp_entry.S with the MMU off. */ |
| 61 | struct ck_smp_cpu { |
| 62 | uint64_t root, mair, tcr, sctlr, sp; /* 0, 8, 16, 24, 32 */ |
| 63 | uint64_t target; /* 40: MPIDR from the MADT */ |
| 64 | uint64_t state; /* 48: SMP_* (atomic) */ |
| 65 | uint64_t seen_mpidr, seen_el, seen_sctlr, seen_ttbr0, seen_sp; |
| 66 | uint64_t stack_lo, stack_hi; |
| 67 | } __attribute__((aligned(64))); |
| 68 | _Static_assert(__builtin_offsetof(struct ck_smp_cpu, sp) == 32, "smp_entry.S offsets"); |
| 69 | _Static_assert(__builtin_offsetof(struct ck_smp_cpu, state) == 48, "smp_entry.S offsets"); |
| 70 | |
| 71 | static struct ck_smp_cpu cpus[CK_SMP_MAX]; |
| 72 | |
| 73 | /* arch/smp_entry.S: PSCI CPU_ON entry point, x0 = &cpus[i]. */ |
| 74 | extern char ck_smp_entry[]; |
| 75 | void ck_smp_secondary_main(void *arg); |
| 76 | |
| 77 | /* Runs on the secondary core at EL1, MMU on, its own stack, DAIF masked. */ |
| 78 | __attribute__((noreturn)) void ck_smp_secondary_main(void *arg) |
| 79 | { |
| 80 | struct ck_smp_cpu *c = arg; |
| 81 | uint64_t sp; |
| 82 | __asm__ volatile("mov %0, sp" : "=r"(sp)); |
| 83 | c->seen_mpidr = ck_rd(mpidr_el1) & CK_MPIDR_AFF_MASK; |
| 84 | c->seen_el = ck_current_el(); |
| 85 | c->seen_sctlr = ck_rd(sctlr_el1); |
| 86 | c->seen_ttbr0 = ck_rd(ttbr0_el1) & TTBR_BADDR_MASK; |
| 87 | c->seen_sp = sp; |
| 88 | __atomic_store_n(&c->state, (uint64_t)SMP_CHECKED_IN, __ATOMIC_RELEASE); |
| 89 | __asm__ volatile("dsb ish\n\tsev" ::: "memory"); |
| 90 | for (;;) |
| 91 | __asm__ volatile("wfe" ::: "memory"); |
| 92 | } |
| 93 | |
| 94 | static void clean_to_poc(const void *p, size_t len) |
| 95 | { |
| 96 | uint64_t line = 4ull << ((ck_rd(ctr_el0) >> 16) & 0xf); |
| 97 | uint64_t a = (uint64_t)(uintptr_t)p & ~(line - 1), end = (uint64_t)(uintptr_t)p + len; |
| 98 | for (; a < end; a += line) |
| 99 | __asm__ volatile("dc cvac, %0" ::"r"(a) : "memory"); |
| 100 | __asm__ volatile("dsb sy" ::: "memory"); |
| 101 | } |
| 102 | |
| 103 | static const char *psci_err(int64_t rc) |
| 104 | { |
| 105 | switch (rc) { |
| 106 | case 0: return "SUCCESS"; |
| 107 | case -1: return "NOT_SUPPORTED"; |
| 108 | case -2: return "INVALID_PARAMETERS"; |
| 109 | case -3: return "DENIED"; |
| 110 | case -4: return "ALREADY_ON"; |
| 111 | case -5: return "ON_PENDING"; |
| 112 | case -6: return "INTERNAL_FAILURE"; |
| 113 | case -9: return "INVALID_ADDRESS"; |
| 114 | default: return "UNKNOWN"; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void ck_smp_run(void) |
| 119 | { |
| 120 | ck_set_stage("smp"); |
| 121 | uint64_t self = ck_rd(mpidr_el1) & CK_MPIDR_AFF_MASK; |
| 122 | const struct ck_mm_report *mm = ck_mm_report(); |
| 123 | int fail = 0; |
| 124 | #ifdef CK_TEST_SMP_SKIP_CPU |
| 125 | ck_puts("smp: TEST-ONLY SMP mutation build: CPU_ON is skipped for one core; this image never counts toward a PASS\n"); |
| 126 | #endif |
| 127 | |
| 128 | const void *madt = ck_acpi_find("APIC"); |
| 129 | uint64_t mp[CK_SMP_MAX]; |
| 130 | unsigned n = 0; |
| 131 | int prc = madt ? ck_madt_mpidrs(madt, mp, CK_SMP_MAX, &n) : -1; |
| 132 | int64_t ver = ck_psci_call(PSCI_VERSION, 0, 0, 0); |
| 133 | unsigned vmaj = ver < 0 ? 0 : (unsigned)((uint64_t)ver >> 16) & 0x7fff; |
| 134 | unsigned vmin = ver < 0 ? 0 : (unsigned)((uint64_t)ver & 0xffff); |
| 135 | ck_printf("smp: boot_mpidr=0x%llx madt_cpus=%u conduit=%s psci_version=%u.%u\n", |
| 136 | (unsigned long long)self, n, ck_psci_conduit(), vmaj, vmin); |
| 137 | if (prc != 0) { |
| 138 | ck_printf("smp: FAIL madt %s (rc=%d, %u cores, max %u)\n", |
| 139 | !madt ? "absent" : prc == -2 ? "lists more cores than supported" : "malformed", prc, |
| 140 | n, CK_SMP_MAX); |
| 141 | ck_printf("smp: cpus=%u checked_in=0 psci_errors=0 distinct_mpidrs=0 wait_us=0 result=FAIL\n", n); |
| 142 | return; |
| 143 | } |
| 144 | if (ver < 0 || (vmaj == 0 && vmin < 2)) { |
| 145 | ck_printf("smp: FAIL psci version %lld has no 64-bit CPU_ON\n", (long long)ver); |
| 146 | fail = 1; |
| 147 | } |
| 148 | |
| 149 | /* Distinct MPIDRs, and the boot core among them. */ |
| 150 | unsigned distinct = 0, boot_listed = 0; |
| 151 | for (unsigned i = 0; i < n; i++) { |
| 152 | unsigned dup = 0; |
| 153 | for (unsigned j = 0; j < i; j++) |
| 154 | if (mp[j] == mp[i]) |
| 155 | dup = 1; |
| 156 | if (dup) { |
| 157 | ck_printf("smp: FAIL duplicate mpidr=0x%llx in the MADT\n", (unsigned long long)mp[i]); |
| 158 | fail = 1; |
| 159 | } else { |
| 160 | distinct++; |
| 161 | } |
| 162 | if (mp[i] == self) |
| 163 | boot_listed = 1; |
| 164 | } |
| 165 | if (!boot_listed) { |
| 166 | ck_printf("smp: FAIL boot core mpidr=0x%llx missing from the MADT\n", (unsigned long long)self); |
| 167 | fail = 1; |
| 168 | } |
| 169 | |
| 170 | /* Start every secondary core. */ |
| 171 | unsigned started = 0, psci_errors = 0, last = n; |
| 172 | for (unsigned i = 0; i < n; i++) |
| 173 | if (mp[i] != self) |
| 174 | last = i; |
| 175 | for (unsigned i = 0; i < n && !fail; i++) { |
| 176 | struct ck_smp_cpu *c = &cpus[i]; |
| 177 | if (mp[i] == self) |
| 178 | continue; |
| 179 | uint64_t pa = 0; |
| 180 | if (ck_mm_frames_alloc(CK_SMP_STACK_PAGES, &pa) || !ck_mm_mapped(pa, CK_SMP_STACK_PAGES * 4096ull)) { |
| 181 | ck_printf("smp_cpu_on: target=0x%llx stack=unavailable\n", (unsigned long long)mp[i]); |
| 182 | fail = 1; |
| 183 | continue; |
| 184 | } |
| 185 | memset(c, 0, sizeof *c); |
| 186 | c->root = mm->root; |
| 187 | c->mair = ck_mm_mair(); |
| 188 | c->tcr = ck_mm_tcr(); |
| 189 | c->sctlr = ck_mm_sctlr(); |
| 190 | c->stack_lo = pa; |
| 191 | c->stack_hi = pa + CK_SMP_STACK_PAGES * 4096ull; |
| 192 | c->sp = c->stack_hi; |
| 193 | c->target = mp[i]; |
| 194 | c->state = SMP_ON_CALLED; |
| 195 | clean_to_poc(c, sizeof *c); |
| 196 | #ifdef CK_TEST_SMP_SKIP_CPU |
| 197 | if (i == last) { |
| 198 | ck_printf("smp_cpu_on: target=0x%llx psci_rc=SKIPPED (TEST-ONLY SMP mutation)\n", |
| 199 | (unsigned long long)mp[i]); |
| 200 | started++; |
| 201 | continue; |
| 202 | } |
| 203 | #endif |
| 204 | int64_t rc = ck_psci_call(PSCI_CPU_ON_64, mp[i], (uint64_t)(uintptr_t)ck_smp_entry, |
| 205 | (uint64_t)(uintptr_t)c); |
| 206 | ck_printf("smp_cpu_on: target=0x%llx psci_rc=%d (%s)\n", (unsigned long long)mp[i], (int)rc, |
| 207 | psci_err(rc)); |
| 208 | if (rc != 0) { |
| 209 | psci_errors++; |
| 210 | c->state = SMP_IDLE; |
| 211 | continue; |
| 212 | } |
| 213 | started++; |
| 214 | } |
| 215 | (void)last; |
| 216 | |
| 217 | /* Bounded wait for every started core. */ |
| 218 | uint64_t t0 = ck_time_us(), waited = 0; |
| 219 | for (;;) { |
| 220 | unsigned pending = 0; |
| 221 | for (unsigned i = 0; i < n; i++) |
| 222 | if (mp[i] != self && __atomic_load_n(&cpus[i].state, __ATOMIC_ACQUIRE) == SMP_ON_CALLED) |
| 223 | pending++; |
| 224 | waited = ck_time_us() - t0; |
| 225 | if (!pending || waited >= CK_SMP_TIMEOUT_US) |
| 226 | break; |
| 227 | ck_udelay(100); |
| 228 | } |
| 229 | |
| 230 | /* One line per secondary core, then the summary. */ |
| 231 | unsigned checked = 1; /* the boot core */ |
| 232 | for (unsigned i = 0; i < n; i++) { |
| 233 | struct ck_smp_cpu *c = &cpus[i]; |
| 234 | if (mp[i] == self) |
| 235 | continue; |
| 236 | if (__atomic_load_n(&c->state, __ATOMIC_ACQUIRE) != SMP_CHECKED_IN) { |
| 237 | ck_printf("smp_cpu: target=0x%llx checked_in=no\n", (unsigned long long)mp[i]); |
| 238 | fail = 1; |
| 239 | continue; |
| 240 | } |
| 241 | int el_ok = c->seen_el == 1, mmu_ok = (c->seen_sctlr & 1) != 0; |
| 242 | int tt_ok = c->seen_ttbr0 == mm->root; |
| 243 | int sp_ok = c->seen_sp > c->stack_lo && c->seen_sp <= c->stack_hi; |
| 244 | int id_ok = c->seen_mpidr == mp[i]; |
| 245 | ck_printf("smp_cpu: target=0x%llx checked_in=yes seen_mpidr=0x%llx el=%u mmu=%s ttbr0_match=%s " |
| 246 | "stack_ok=%s parked=wfe\n", |
| 247 | (unsigned long long)mp[i], (unsigned long long)c->seen_mpidr, (unsigned)c->seen_el, |
| 248 | mmu_ok ? "on" : "off", tt_ok ? "yes" : "no", sp_ok ? "yes" : "no"); |
| 249 | if (!(el_ok && mmu_ok && tt_ok && sp_ok && id_ok)) |
| 250 | fail = 1; |
| 251 | checked++; |
| 252 | } |
| 253 | if (checked != n || psci_errors) |
| 254 | fail = 1; |
| 255 | ck_printf("smp: cpus=%u started=%u checked_in=%u psci_errors=%u distinct_mpidrs=%u wait_us=%llu result=%s\n", n, |
| 256 | started + 1, checked, psci_errors, distinct, (unsigned long long)waited, fail ? "FAIL" : "ok"); |
| 257 | } |