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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 | //! Interrupt handling: the IDT, CPU exception handlers and hardware IRQs.
//!
//! The IDT (Interrupt Descriptor Table) maps each interrupt vector number to
//! a handler function:
//! * vectors 0..=31 are CPU exceptions (divide error, page fault, ...)
//! * vectors 32..=47 are where we remap the 8259 PIC's 16 hardware IRQs
//! * 0x40, 0x41, 0x42, 0xFF come from the local APIC (see apic.rs): its
//! timer, the "reschedule" and "TLB shootdown" IPIs one CPU sends
//! another, and spurious interrupts
//!
//! All CPUs share this one IDT. Device IRQs (PIT, keyboard) only ever
//! arrive on CPU 0; APIC timer ticks and IPIs arrive on every CPU.
//!
//! Most handlers use the special `extern "x86-interrupt"` calling convention:
//! the compiler saves/restores all registers and returns with `iretq`. The
//! ones that can interrupt a user program and must be able to send it a
//! signal (faults, the APIC timer) use trap.rs's stubs instead.
use crate;
use crate;
use crate::;
use ;
use lazy_static;
use ChainedPics;
use ;
// ---------------------------------------------------------------------------
// 8259 PIC (Programmable Interrupt Controller)
// ---------------------------------------------------------------------------
/// The PICs deliver IRQ 0..15 on vectors 0..15 by default, which clash with
/// CPU exceptions. We remap them to 32..47.
pub const PIC_1_OFFSET: u8 = 32;
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
/// Two PICs: primary handles IRQ 0-7, secondary IRQ 8-15 (chained via IRQ 2).
pub static PICS: Mutex =
new;
/// Vector numbers of the hardware interrupts we handle.
// ---------------------------------------------------------------------------
// IDT setup
// ---------------------------------------------------------------------------
lazy_static!
/// Load the IDT into the CPU (`lidt`). Every CPU does this once.
// ---------------------------------------------------------------------------
// CPU exception handlers
// ---------------------------------------------------------------------------
/// `int3` (what debuggers use). Harmless: print and carry on.
extern "x86-interrupt"
/// Rust side of trap.rs's stubs: CPU exceptions a program can cause, and
/// the APIC timer. On the way out, if we're returning to user mode, act on
/// pending signals (that may run a handler, or end the process).
pub extern "C"
/// SIGFPE's si_code for an SSE exception (#XM, only raised if the program
/// unmasked it in MXCSR): which one, from MXCSR's status flags.
/// An exception in user mode is the program's fault, not the kernel's: it
/// gets a signal (which kills it, unless it has a handler). In the kernel
/// it's a bug: stop.
/// Page fault: CR2 holds the address, the error code says why (page not
/// present? write? user mode? instruction fetch?). In user mode this is
/// usually *not* an error: demand paging and copy-on-write (mm.rs) fix the
/// page up and the instruction runs again. Only if the address isn't in
/// any region (or the region forbids the access) does the program get
/// SIGSEGV. The kernel itself faults on user memory only when another
/// thread changed it during a syscall (below); any other kernel-mode page
/// fault is a bug.
/// Double fault: an exception happened while handling another one. Must not
/// return (hence `-> !`). Runs on the IST stack set up in gdt.rs.
extern "x86-interrupt" !
// ---------------------------------------------------------------------------
// Hardware interrupt handlers
// ---------------------------------------------------------------------------
/// Timer ticks since boot. Atomic, so it's safe to touch from an interrupt.
pub static TICKS: AtomicU64 = new;
/// PIT ticks since boot (~18.2 per second, ~55 ms each).
/// IRQ 0 (CPU 0 only). Fires ~18.2 times per second (the PIT's power-on
/// default). It's the system clock: uptime, and waking sleeping threads.
extern "x86-interrupt"
/// Another CPU queued work and saw we were idle.
extern "x86-interrupt"
/// Another CPU changed page tables we may have cached (threads, smp.rs).
extern "x86-interrupt"
/// Spurious APIC interrupt: ignore it (and no EOI, by the spec).
extern "x86-interrupt"
/// IRQ 1. The keyboard controller has a scancode waiting at I/O port 0x60.
/// We must read it (or no more keyboard interrupts arrive), then just queue
/// it for the async keyboard task (task/keyboard.rs) to decode and print.
extern "x86-interrupt"
/// Unmask PIC IRQ line `irq` (and the cascade line 2 for IRQ 8-15), so a
/// PCI card's interrupts get through. Lines are masked until a driver
/// asks: an interrupt nobody handles would just fire again forever.
/// A PCI device interrupted. Only the network card uses interrupts (the
/// disks are polled), so ask it; then tell the PIC we're done.
/// *Spurious* PIC interrupts. With a level-triggered line (PCI) the device
/// can lower its line after the PIC told the CPU "interrupt!" but before
/// the CPU asked "which one?" (here: netd on another CPU read the card's ISR
/// register just then). The PIC then answers with its lowest-priority line,
/// 7 (primary) or 15 (secondary), without that line really firing. Without
/// these handlers the vector would be empty and the CPU would double fault.
/// A spurious IRQ 7 gets no end-of-interrupt (nothing is in service); a
/// spurious IRQ 15 still went through the primary's cascade line 2, so only
/// the primary gets one.
extern "x86-interrupt"
extern "x86-interrupt"
extern "x86-interrupt"
extern "x86-interrupt"
extern "x86-interrupt"
extern "x86-interrupt"
// Runs under `cargo test`: if the handler were missing, this would fault.
|