1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
// Copyright 2026 The percore Authors.
// This project is dual-licensed under Apache 2.0 and MIT terms.
// See LICENSE-APACHE and LICENSE-MIT for details.
//! Linker section based per-core variables
//!
//! The `percore` attribute places a variable's initial value in the `.percore` linker section and
//! exposes a `LinkedPerCore` wrapper that accesses the copy for the local CPU. The consuming
//! project must initialize each CPU's percore area and provide its offset by implementing
//! `PercoreLocalOffset` on a type marked with `percore_local_offset`.
//!
//! # Linker script
//!
//! You must include the `.percore` section in your linker script, with `__PERCORE_START__` and
//! `__PERCORE_END__` symbols to mark its boundaries. E.g.:
//!
//! ```ld
//! .percore : ALIGN(CACHE_LINE_SIZE) {
//! __PERCORE_START__ = .;
//! *(SORT_BY_ALIGNMENT(.percore .percore.*))
//! . = ALIGN(CACHE_LINE_SIZE);
//! __PERCORE_END__ = .;
//! } >image
//!
//! ASSERT(
//! ALIGNOF(.percore) <= CACHE_LINE_SIZE,
//! ".percore contains an object aligned to a larger boundary than the section's alignment."
//! )
//! ```
//!
//! # Initialisation
//!
//! Three possible ways to allocate and initialise each CPU's percore area are:
//!
//! 1. If you know the number of cores at build time, allocate a `.percore_secondary` section for it
//! in your linker script, and provide the `__PERCORE_SECONDARY_START__` and
//! `__PERCORE_SECONDARY_END__` symbols. Copy the appropriate number of copies of the `.percore`
//! section to this section in assembly code before any Rust code runs. On AArch64 bare-metal
//! targets [`aarch64::percore_copy_secondary_data`] is provided to implement this, and
//! [`aarch64::percore_calculate_local_offset`] to calculate the area's offset from a CPU linear
//! index. This must either be done before caches are enabled or with appropriate cache
//! maintenance operations to ensure that it is visible to all cores.
//! 2. In your Rust entry point before any access to percore variables, copy the appropriate number
//! of copies of the `.percore` section to an appropriately sized area of memory.
//! [`percore_copy_secondary_data`] is provided to implement this. In this case you must use Rust
//! synchronisation primitives (e.g. an AtomicBool) to ensure that this happens-before any access
//! to percore variables.
//! 3. Have each core initialise its own copy of the `.percore` section the first time it starts. In
//! this case there is no distinction between primary and secondary cores, and the original
//! `.percore` section must never be modified (or at least not until all cores have started and
//! initialised their copies).
//!
//! In any case, you must ensure that the alignmeant of each CPU's percore area is greater than or
//! equal to to the maximum alignment of any percore variable.
//!
//! # Usage
//!
//! All cores will have their own instance of `VARIABLE` which is initialized to 1.
//!
//! ```
//! # fn exception_free<T>(f: impl FnOnce(percore::ExceptionFree<'_>) -> T) {}
//! use core::cell::RefCell;
//! use percore::{ExceptionLock, derive::percore};
//!
//! #[percore]
//! static VARIABLE: ExceptionLock<RefCell<u64>> = ExceptionLock::new(RefCell::new(1));
//!
//! exception_free(|token| {
//! assert_eq!(1, *VARIABLE.get().borrow_mut(token));
//!
//! *VARIABLE.get().borrow_mut(token) = 2;
//! assert_eq!(2, *VARIABLE.get().borrow_mut(token));
//! });
//! ```
use crateExceptionLock;
use NonNull;
pub use percore;
unsafe extern "Rust"
/// Returns the size in bytes of a single core's `.percore` section.
/// Duplicates the contents of the initialised percore section into the secondary cores' percore
/// area.
///
/// The function calculates the size of the `.percore` section as the difference between the
/// `__PERCORE_START__` and `__PERCORE_END__` symbols. Then it copies this memory area into the
/// given `secondary_percore_area` as many times as it fits.
///
/// Panics if the length of `secondary_percore_area` isn't a multiple of the size of the `.percore`
/// section.
///
/// # Safety
///
/// This must only be called before any core accesses any percore variable.
///
/// `secondary_percore_area` must be valid for writes, and must not overlap with the `.percore
/// section.
///
/// You must ensure that this initialisation happens-before any percore variables are accessed
/// (according to Rust's memory model). This could for example be achieved by writing to an
/// AtomicBool with release semantics and having other cores wait until they see the written value
/// with acquire semantics.
pub unsafe
/// Provides the offset of the local core's percore area.
///
/// The consuming project must implement this trait for a type and mark that type with the
/// [`percore_local_offset!`](crate::percore_local_offset) macro.
///
/// # Safety
///
/// The offset must point to a valid and initialized percore memory area and it must not
/// overflow `isize` for any percore variable and core.
pub unsafe
unsafe extern "Rust"
/// A value stored in a linker section containing one copy for each CPU core.
///
/// The initial value (which may also be primary core's value) is stored directly in this wrapper.
/// [`get`](Self::get) adds the local per-core offset to its address to locate the current core's
/// copy.
///
/// This should generally not be constructed directly, but through the [`percore`] macro.
;
// Safety: `LinkedPerCore` is safe between different cores, because each core has its own
// core-local instance of the variable. `ExceptionLock` also prevents concurrent access from runtime
// and exception context.
unsafe
/// Marks the type that implements [`PercoreLocalOffset`].
///
/// This creates the `percore_local_offset` function used internally by `percore::derive`.
///
/// # Example
///
/// ```
/// use percore::{derive::PercoreLocalOffset, percore_local_offset};
///
/// percore_local_offset!(LocalOffsetImpl);
/// struct LocalOffsetImpl;
///
/// unsafe impl PercoreLocalOffset for LocalOffsetImpl {
/// fn percore_local_offset() -> isize {
/// todo!("Return the appropriate offset for the current core")
/// }
/// }
/// ```