Skip to content

Commit d82bf83

Browse files
committed
cpu: record an address of PerCpuShared that is globally valid
The address of a `PerCpuShared` as observed by the local CPU is different than the address of the same `PerCpuShared` observed by other CPUs in the global address space. There are cases where a local CPU needs to know the address that will be used by other CPUs. Signed-off-by: Jon Lange <[email protected]>
1 parent 2a3f21f commit d82bf83

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

kernel/src/cpu/percpu.rs

+14
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,10 @@ pub struct PerCpu {
322322
/// Per-CPU storage that might be accessed from other CPUs.
323323
shared: PerCpuShared,
324324

325+
/// Reference to the `PerCpuShared` that is valid in the global, shared
326+
/// address space.
327+
shared_global: OnceCell<&'static PerCpuShared>,
328+
325329
/// PerCpu IRQ state tracking
326330
irq_state: IrqState,
327331

@@ -383,6 +387,7 @@ impl PerCpu {
383387
apic: RefCell::new(None),
384388

385389
shared: PerCpuShared::new(apic_id, cpu_index),
390+
shared_global: OnceCell::new(),
386391
ghcb: OnceCell::new(),
387392
hypercall_pages: RefCell::new(None),
388393
hv_doorbell: Cell::new(None),
@@ -401,6 +406,7 @@ impl PerCpu {
401406
let cpu_index = PERCPU_AREAS.next_cpu_index();
402407
let page = PageBox::try_new(Self::new(apic_id, cpu_index))?;
403408
let percpu = PageBox::leak(page);
409+
percpu.set_shared_global();
404410
unsafe { PERCPU_AREAS.push(PerCpuInfo::new(apic_id, &percpu.shared)) };
405411
Ok(percpu)
406412
}
@@ -409,6 +415,14 @@ impl PerCpu {
409415
&self.shared
410416
}
411417

418+
fn set_shared_global(&'static self) {
419+
self.shared_global.set(&self.shared).expect("shared global set more than once");
420+
}
421+
422+
pub fn shared_global(&self) -> &'static PerCpuShared {
423+
self.shared_global.get().unwrap()
424+
}
425+
412426
/// Disables IRQs on the current CPU. Keeps track of the nesting level and
413427
/// the original IRQ state.
414428
///

0 commit comments

Comments
 (0)