1#[cfg(not(all(target_arch = "aarch64", not(feature = "host-test"))))]
2use core::sync::atomic::Ordering;
3use core::{
4 mem::{MaybeUninit, align_of, offset_of, size_of},
5 ptr::NonNull,
6 sync::atomic::AtomicUsize,
7};
8
9use crate::{CpuIndex, CpuLocalError, ExecutionContextHeader, preempt::PreemptionState};
10
11const fn runtime_anchor_reserved_size() -> usize {
12 64 - 5 * size_of::<usize>() - size_of::<PreemptionState>()
13}
14
15#[repr(C, align(64))]
17pub struct CpuRuntimeAnchor {
18 current_context: AtomicUsize,
19 architecture_state: [AtomicUsize; 4],
20 preemption_state: PreemptionState,
21 reserved: [u8; runtime_anchor_reserved_size()],
22}
23
24impl CpuRuntimeAnchor {
25 const fn for_boot_context(boot_context: usize) -> Self {
26 let current_context = if cfg!(all(target_arch = "x86_64", not(feature = "host-test")))
27 || cfg!(all(
28 feature = "tls",
29 not(all(target_arch = "aarch64", not(feature = "host-test")))
30 )) {
31 boot_context
32 } else {
33 0
34 };
35 Self {
36 current_context: AtomicUsize::new(current_context),
37 architecture_state: [const { AtomicUsize::new(0) }; 4],
38 preemption_state: PreemptionState::bootstrap_disabled(),
39 reserved: [0; runtime_anchor_reserved_size()],
40 }
41 }
42
43 #[cfg(not(all(target_arch = "aarch64", not(feature = "host-test"))))]
45 pub(crate) fn current_context_raw(&self) -> usize {
46 self.current_context.load(Ordering::Acquire)
47 }
48
49 #[cfg(not(all(target_arch = "aarch64", not(feature = "host-test"))))]
50 pub(crate) const fn current_context_slot(&self) -> &AtomicUsize {
51 &self.current_context
52 }
53
54 #[cfg(all(target_arch = "x86_64", not(feature = "host-test")))]
55 pub(crate) const fn preemption_state(&self) -> &PreemptionState {
56 &self.preemption_state
57 }
58}
59
60#[repr(transparent)]
62pub struct BootContextHeader(ExecutionContextHeader);
63
64impl BootContextHeader {
65 const fn for_area(area_base: usize) -> Self {
66 Self(ExecutionContextHeader::boot(area_base))
67 }
68
69 pub const fn header(&self) -> &ExecutionContextHeader {
71 &self.0
72 }
73}
74
75const fn area_header_reserved_size() -> usize {
76 64 - size_of::<u32>() * 2 - size_of::<usize>()
77}
78
79#[repr(C, align(64))]
81pub struct CpuAreaHeader {
82 cpu_index: u32,
83 reserved_word: u32,
84 self_base: usize,
85 reserved: [u8; area_header_reserved_size()],
86}
87
88impl CpuAreaHeader {
89 const fn new(cpu_index: CpuIndex, self_base: usize) -> Self {
90 Self {
91 cpu_index: cpu_index.as_u32(),
92 reserved_word: 0,
93 self_base,
94 reserved: [0; area_header_reserved_size()],
95 }
96 }
97
98 pub const fn cpu_index(&self) -> CpuIndex {
100 match CpuIndex::from_u32(self.cpu_index) {
101 Some(index) => index,
102 None => panic!("initialized CPU area contains the reserved CPU index"),
103 }
104 }
105
106 pub const fn self_base(&self) -> usize {
108 self.self_base
109 }
110}
111
112#[repr(C, align(64))]
114pub struct CpuAreaPrefix {
115 header: CpuAreaHeader,
116 runtime: CpuRuntimeAnchor,
117 boot_context: BootContextHeader,
118}
119
120impl CpuAreaPrefix {
121 pub fn initialize(cpu_index: CpuIndex, area_base: usize) -> Result<Self, CpuLocalError> {
128 validate_area_base(area_base)?;
129 area_base
130 .checked_add(CPU_AREA_BOOT_CONTEXT_OFFSET)
131 .ok_or(CpuLocalError::AddressOverflow)?;
132 Ok(Self {
133 header: CpuAreaHeader::new(cpu_index, area_base),
134 runtime: CpuRuntimeAnchor::for_boot_context(area_base + CPU_AREA_BOOT_CONTEXT_OFFSET),
135 boot_context: BootContextHeader::for_area(area_base),
136 })
137 }
138
139 pub const fn header(&self) -> &CpuAreaHeader {
141 &self.header
142 }
143
144 pub const fn runtime_anchor(&self) -> &CpuRuntimeAnchor {
146 &self.runtime
147 }
148
149 pub const fn boot_context(&self) -> &BootContextHeader {
151 &self.boot_context
152 }
153}
154
155#[derive(Clone, Copy, Debug, Eq, PartialEq)]
157pub struct CpuAreaRef {
158 prefix: NonNull<CpuAreaPrefix>,
159 cpu_index: CpuIndex,
160}
161
162unsafe impl Send for CpuAreaRef {}
166unsafe impl Sync for CpuAreaRef {}
169
170impl CpuAreaRef {
171 #[doc(hidden)]
178 pub unsafe fn from_initialized_base(area_base: usize) -> Result<Self, CpuLocalError> {
179 #[cfg(feature = "host-test")]
180 crate::register::host_test::record_initialized_area_validation();
181 validate_area_base(area_base)?;
182 let prefix = NonNull::new(area_base as *mut CpuAreaPrefix)
183 .ok_or(CpuLocalError::InvalidAreaBase { base: area_base })?;
184 let header = unsafe { prefix.as_ref() }.header();
186 let cpu_index =
187 CpuIndex::from_u32(header.cpu_index).ok_or(CpuLocalError::AreaIdentityMismatch)?;
188 if header.self_base != area_base {
189 return Err(CpuLocalError::AreaIdentityMismatch);
190 }
191 let expected_boot = area_base
192 .checked_add(CPU_AREA_BOOT_CONTEXT_OFFSET)
193 .ok_or(CpuLocalError::AddressOverflow)?;
194 if unsafe { prefix.as_ref() }
195 .boot_context()
196 .header()
197 .raw_cpu_binding()
198 .map(|(boot_area, _)| boot_area)
199 != Some(area_base)
200 || expected_boot
201 != core::ptr::addr_of!(unsafe { prefix.as_ref() }.boot_context.0) as usize
202 {
203 return Err(CpuLocalError::AreaIdentityMismatch);
204 }
205 Ok(Self { prefix, cpu_index })
206 }
207
208 pub const fn cpu_index(self) -> CpuIndex {
210 self.cpu_index
211 }
212
213 pub fn base(self) -> usize {
215 self.prefix.as_ptr() as usize
216 }
217
218 pub fn prefix(self) -> &'static CpuAreaPrefix {
220 unsafe { self.prefix.as_ref() }
222 }
223
224 pub fn runtime_anchor(self) -> &'static CpuRuntimeAnchor {
226 self.prefix().runtime_anchor()
227 }
228}
229
230fn validate_area_base(area_base: usize) -> Result<(), CpuLocalError> {
231 if area_base == 0 || !area_base.is_multiple_of(align_of::<CpuAreaPrefix>()) {
232 Err(CpuLocalError::InvalidAreaBase { base: area_base })
233 } else {
234 Ok(())
235 }
236}
237
238pub const CPU_AREA_HEADER_SIZE: usize = size_of::<CpuAreaHeader>();
240pub const CPU_AREA_RUNTIME_ANCHOR_OFFSET: usize = offset_of!(CpuAreaPrefix, runtime);
242pub const CPU_AREA_BOOT_CONTEXT_OFFSET: usize = offset_of!(CpuAreaPrefix, boot_context);
244pub const CPU_AREA_SELF_BASE_OFFSET: usize = offset_of!(CpuAreaHeader, self_base);
246pub const CPU_AREA_CPU_INDEX_OFFSET: usize = offset_of!(CpuAreaHeader, cpu_index);
248pub const CPU_AREA_CURRENT_CONTEXT_OFFSET: usize =
250 CPU_AREA_RUNTIME_ANCHOR_OFFSET + offset_of!(CpuRuntimeAnchor, current_context);
251pub const CPU_AREA_ARCH_STATE_OFFSET: usize =
253 CPU_AREA_RUNTIME_ANCHOR_OFFSET + offset_of!(CpuRuntimeAnchor, architecture_state);
254pub const CPU_AREA_ARCH_STATE_SIZE: usize = 4 * size_of::<usize>();
256pub const CPU_AREA_PREEMPTION_STATE_OFFSET: usize =
258 CPU_AREA_RUNTIME_ANCHOR_OFFSET + offset_of!(CpuRuntimeAnchor, preemption_state);
259
260const _: () = {
261 assert!(size_of::<CpuAreaHeader>() == 64);
262 assert!(align_of::<CpuAreaHeader>() == 64);
263 assert!(size_of::<CpuRuntimeAnchor>() == 64);
264 assert!(align_of::<CpuRuntimeAnchor>() == 64);
265 assert!(size_of::<BootContextHeader>() == 64);
266 assert!(align_of::<BootContextHeader>() == 64);
267 assert!(size_of::<CpuAreaPrefix>() == 192);
268 assert!(align_of::<CpuAreaPrefix>() == 64);
269 assert!(CPU_AREA_RUNTIME_ANCHOR_OFFSET == 64);
270 assert!(CPU_AREA_BOOT_CONTEXT_OFFSET == 128);
271};
272
273#[doc(hidden)]
274#[used]
275#[unsafe(no_mangle)]
276#[unsafe(link_section = ".percpu.template.header")]
277pub static mut __CPU_LOCAL_AREA_PREFIX: MaybeUninit<CpuAreaPrefix> = MaybeUninit::uninit();
278
279#[doc(hidden)]
280#[used]
281#[unsafe(no_mangle)]
282#[unsafe(link_section = ".percpu.template.end")]
283pub static __CPU_LOCAL_TEMPLATE_END: u8 = 0;