Skip to content

Commit

Permalink
cpu: record an address of PerCpuShared that is globally valid
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
msft-jlange committed Dec 18, 2024
1 parent 3fa1d3e commit dac5c98
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions kernel/src/cpu/percpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ pub struct PerCpu {
/// Per-CPU storage that might be accessed from other CPUs.
shared: PerCpuShared,

/// Reference to the `PerCpuShared` that is valid in the global, shared
/// address space.
shared_global: OnceCell<&'static PerCpuShared>,

/// PerCpu IRQ state tracking
irq_state: IrqState,

Expand Down Expand Up @@ -383,6 +387,7 @@ impl PerCpu {
apic: RefCell::new(None),

shared: PerCpuShared::new(apic_id, cpu_index),
shared_global: OnceCell::new(),
ghcb: OnceCell::new(),
hypercall_pages: RefCell::new(None),
hv_doorbell: Cell::new(None),
Expand All @@ -401,6 +406,7 @@ impl PerCpu {
let cpu_index = PERCPU_AREAS.next_cpu_index();
let page = PageBox::try_new(Self::new(apic_id, cpu_index))?;
let percpu = PageBox::leak(page);
percpu.set_shared_global();
unsafe { PERCPU_AREAS.push(PerCpuInfo::new(apic_id, &percpu.shared)) };
Ok(percpu)
}
Expand All @@ -409,6 +415,14 @@ impl PerCpu {
&self.shared
}

fn set_shared_global(&'static self) {
self.shared_global.set(&self.shared).expect("shared global set more than once");
}

pub fn shared_global(&self) -> &'static PerCpuShared {
self.shared_global.get().unwrap()
}

/// Disables IRQs on the current CPU. Keeps track of the nesting level and
/// the original IRQ state.
///
Expand Down

0 comments on commit dac5c98

Please sign in to comment.