99#include " mem/mem.hpp"
1010#include " sched/process.hpp"
1111
12- static ManuallyInit< Cpu> CPUS[CONFIG_MAX_CPUS] {};
12+ static Cpu* CPUS[CONFIG_MAX_CPUS] {};
1313static kstd::atomic<u32 > NUM_CPUS {1 };
1414static Spinlock<void > SMP_LOCK {};
1515static constexpr bool LOG_SMP_STARTUP = true ;
@@ -135,6 +135,10 @@ extern "C" [[noreturn, gnu::used]] void aarch64_ap_entry_stage0() {
135135 __builtin_unreachable ();
136136}
137137
138+ using CtorFn = void (*)();
139+ extern CtorFn CPU_LOCAL_CTORS_START[];
140+ extern CtorFn CPU_LOCAL_CTORS_END[];
141+
138142static void aarch64_common_cpu_init (Cpu* self, u32 num, bool bsp) {
139143 self->number = num;
140144
@@ -166,6 +170,10 @@ static void aarch64_common_cpu_init(Cpu* self, u32 num, bool bsp) {
166170 u64 mpidr;
167171 asm volatile (" mrs %0, mpidr_el1" : " =r" (mpidr));
168172 self->affinity = (mpidr & 0xFFFFFF ) | (mpidr >> 32 & 0xFF ) << 24 ;
173+
174+ for (auto * ctor = CPU_LOCAL_CTORS_START; ctor != CPU_LOCAL_CTORS_END; ++ctor) {
175+ (*ctor)();
176+ }
169177}
170178
171179extern char EXCEPTION_HANDLERS_START[];
@@ -179,7 +187,6 @@ extern "C" [[noreturn, gnu::used]] void aarch64_ap_entry() {
179187 u32 num = NUM_CPUS.load (kstd::memory_order::relaxed);
180188
181189 auto lock = SMP_LOCK.lock ();
182- CPUS[num].initialize ();
183190 cpu = &*CPUS[num];
184191 aarch64_common_cpu_init (cpu, num, false );
185192 gic_init_on_cpu ();
@@ -208,8 +215,13 @@ static void aarch64_cpu_features_init() {
208215 CPU_FEATURES.pan = supports_pan;
209216}
210217
218+ extern char __CPU_LOCAL_START[];
219+ extern char __CPU_LOCAL_END[];
220+
211221void aarch64_bsp_init () {
212- CPUS[0 ].initialize ();
222+ usize cpu_local_size = __CPU_LOCAL_END - __CPU_LOCAL_START;
223+ auto storage = ALLOCATOR.alloc (sizeof (Cpu) + cpu_local_size);
224+ CPUS[0 ] = new (storage) Cpu {};
213225 aarch64_cpu_features_init ();
214226 aarch64_common_cpu_init (&*CPUS[0 ], 0 , true );
215227}
@@ -233,6 +245,8 @@ void aarch64_smp_init(dtb::Dtb& dtb) {
233245
234246 println (" [kernel][aarch64]: smp init" );
235247
248+ usize cpu_local_size = __CPU_LOCAL_END - __CPU_LOCAL_START;
249+
236250 for (auto child : cpus.child_iter ()) {
237251 if (NUM_CPUS.load (kstd::memory_order::relaxed) >= CONFIG_MAX_CPUS) {
238252 break ;
@@ -266,6 +280,9 @@ void aarch64_smp_init(dtb::Dtb& dtb) {
266280
267281 auto old = NUM_CPUS.load (kstd::memory_order::relaxed);
268282
283+ auto storage = ALLOCATOR.alloc (sizeof (Cpu) + cpu_local_size);
284+ CPUS[old] = new (storage) Cpu {};
285+
269286 if constexpr (LOG_SMP_STARTUP) {
270287 println (" [kernel][aarch64]: init cpu " , Fmt::Hex, mpidr, Fmt::Reset);
271288 }
0 commit comments