rust-kernel
Rustbelt: a hobby x86_64 kernel written in Rust
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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | //! SMP (symmetric multiprocessing): starting the other CPUs, and per-CPU data.
//!
//! At power-on only one CPU runs: the BSP (bootstrap processor, our CPU 0).
//! The others, the APs (application processors), sit halted, waiting for
//! a wake-up call. To start one:
//!
//! 1. Copy a small piece of 16-bit code, the **trampoline**, to physical
//! address 0x8000 (below 1 MiB: a starting AP is in 16-bit "real mode",
//! the mode the original 8086 had, and can only reach the first MiB).
//! 2. Send it an INIT IPI (reset), wait 10+ ms, then a STARTUP IPI with
//! vector 0x08: the AP starts executing at 0x08 * 4096 = 0x8000.
//! 3. The trampoline switches straight to 64-bit long mode using the BSP's
//! own settings (page tables, CR0/CR4/EFER) and calls `ap_entry` in
//! Rust on a fresh stack.
//! 4. `ap_entry` gives the CPU its own GDT/TSS, loads the shared IDT, sets
//! up `syscall` and its APIC timer, and turns the CPU's current
//! execution into its *idle thread*. From then on the scheduler hands
//! it threads like any other CPU.
//!
//! Which CPUs exist? The ACPI MADT table lists them (see acpi.rs).
//!
//! **Which CPU am I?** Every CPU reads the APIC ID register of its own local
//! APIC; a table maps APIC IDs to our CPU numbers 0, 1, 2, ...
use crate::;
use global_asm;
use ;
use Mutex;
use without_interrupts;
/// Most CPUs we handle (bitmasks are u32; 16 keeps the arrays small).
pub const MAX_CPUS: usize = 16;
/// APIC ID -> CPU number.
static APIC_TO_CPU: = ;
/// CPU number -> APIC ID.
static CPU_TO_APIC: = ;
/// How many CPUs are running (CPU 0 plus started APs).
static CPU_COUNT: AtomicUsize = new;
/// CPUs halted in their idle loop (bit per CPU), so new work can wake one.
static IDLE_CPUS: AtomicU32 = new;
/// Per-CPU data the syscall entry reaches through GS (see syscall.rs).
/// The field order is part of the assembly code: offsets 0 and 8.
static PER_CPU: = ;
/// The number (0, 1, ...) of the CPU this code runs on. Only meaningful
/// with interrupts off, or as a hint: the scheduler may move a thread to
/// another CPU at any timer tick.
/// The address space (CR3 value) each CPU is running right now.
static ACTIVE_CR3: = ;
/// Set by a CPU asking CPU n to flush its TLB; CPU n clears it when done.
static FLUSH_PENDING: = ;
/// The scheduler tells us which address space `cpu` switched to.
/// **TLB shootdown.** Each CPU caches page-table entries in its TLB. When
/// a page is unmapped, made read-only or replaced (copy-on-write) in an
/// address space that threads are using on *other* CPUs right now, their
/// TLBs may still hold the old entry -- a thread could keep writing to a
/// page that was just freed. So: interrupt every other CPU running the
/// address space `cr3` and wait until each has flushed its TLB. (One
/// thread per process never needs this: no other CPU runs its address
/// space at the same time.) Call with interrupts *on*, after changing the
/// page tables and before freeing any frame they pointed to.
/// The shootdown IPI arrived (interrupts.rs): forget all user translations.
/// How many CPUs are running.
/// This CPU's `PerCpu` block.
/// Mark this CPU as idle (about to `hlt`) or busy.
/// New work is ready: if another CPU is idle, interrupt it so it picks the
/// work up now rather than at its next timer tick (up to 10 ms later).
// ---------------------------------------------------------------------------
// Starting the other CPUs
// ---------------------------------------------------------------------------
/// Physical address the trampoline is copied to (must be below 1 MiB and
/// 4 KiB-aligned; the STARTUP IPI's vector is this / 4096).
const TRAMPOLINE: u64 = 0x8000;
/// The trampoline's parameter block (filled in by the BSP for each AP).
/// Must match the `.quad`s at `ap_trampoline_params` below.
unsafe extern "C"
// The trampoline. It runs at physical 0x8000 (not where the linker put it),
// so every address is computed as 0x8000 + (label - ap_trampoline_start).
// AT&T syntax, because that's what most 16-bit examples use.
global_asm!;
/// The boot stack of the AP being started (it becomes its idle thread's).
static AP_STACK: = new;
/// Set by a starting AP as soon as it reaches Rust code.
static AP_ALIVE: AtomicBool = new;
/// Set by a starting AP when it's fully set up.
static AP_READY: AtomicBool = new;
/// Bring up multi-core: enable CPU 0's APIC and timer, create CPU 0's idle
/// thread, then start every other CPU listed in the ACPI tables, one by
/// one. Needs the heap and interrupts on. Returns the number of CPUs.
/// Start one AP and wait (up to ~1 s) until it reports in.
/// Wait up to `ticks` PIT ticks for `done()`; returns its final value.
/// Where each AP arrives from the trampoline: 64-bit mode, interrupts off,
/// on its boot stack, with the trampoline's temporary GDT.
extern "C" !
|