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 | //! 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
//!
//! Handlers use the special `extern "x86-interrupt"` calling convention: the
//! compiler saves/restores all registers and returns with `iretq`.
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`).
// ---------------------------------------------------------------------------
// CPU exception handlers
// ---------------------------------------------------------------------------
/// `int3` (what debuggers use). Harmless: print and carry on.
extern "x86-interrupt"
/// Did the exception happen in user mode (ring 3)? Then it's the process's
/// fault, not the kernel's: kill the process and keep the kernel running.
/// The CPU puts the interrupted code's CS in the frame; its low 2 bits are
/// the privilege level the code ran at.
/// General protection fault: e.g. a privileged instruction (`hlt`, `cli`)
/// in ring 3, or a bad segment selector.
extern "x86-interrupt"
/// Invalid opcode (`ud2`, or garbage executed as code).
extern "x86-interrupt"
/// Page fault: accessing unmapped/protected memory. CR2 holds the bad address,
/// the error code says why (not present? write? user mode?). A real OS would
/// load the page from disk / swap here; we kill a faulting user process, and
/// stop if the kernel itself faulted.
extern "x86-interrupt"
/// 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;
/// IRQ 0. Fires ~18.2 times per second (the PIT's power-on default).
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"
// Runs under `cargo test`: if the handler were missing, this would fault.
|