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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 | //! Timers that send signals (stage 17): alarm, setitimer/getitimer and
//! the POSIX timers (timer_create, timer_settime, ...).
//!
//! A timer is a deadline, maybe with an interval to repeat at, and what to
//! do when it's reached: send a signal (SIGALRM for alarm and setitimer;
//! any signal, maybe to one thread, for timer_create).
//!
//! All timers live in one table. One kernel thread, "timerd", sleeps until
//! the nearest deadline, sends the signals that are due, and goes back to
//! sleep. Why a thread and not the timer interrupt? Sending a signal takes
//! locks and may wake threads; doing that from normal thread context keeps
//! the interrupt handler tiny and follows the locking rules in thread.rs.
//! timerd is started at boot (stage 18; it used to start the first time
//! anyone armed a timer).
//!
//! Resolution: timerd sleeps in PIT ticks (~55 ms), so a timer fires up
//! to ~110 ms late, never early. A periodic timer whose signals come late
//! counts the periods it missed as "overruns" (timer_getoverrun), like
//! Linux does when a signal is still pending.
//!
//! Lifetime, as on Linux: fork doesn't copy timers; execve deletes the
//! POSIX timers but keeps the alarm / ITIMER_REAL; exit deletes them all.
//!
//! Stage 20: timerfd. A timer that is a *file*: instead of a signal, each
//! expiration adds 1 to a counter that `read` returns (and resets), and
//! poll/select/epoll see the fd as readable while the counter isn't 0. It
//! lives in the same table (owner `TIMERFD_PID`, kind `NOTIFY_TIMERFD`), is
//! fired by the same timerd, and goes away when its last fd is closed --
//! not at exec or exit, since an fd can outlive (or be passed to) a process.
//! Same ~55 ms resolution as the rest.
use crateErrno;
use crateprocess;
use crate;
use crateWaitQueue;
use cratetime;
use Vec;
use ;
use Mutex;
use without_interrupts;
/// si_code for a POSIX timer's signal.
pub const SI_TIMER: i32 = -2;
// The clocks timer_create understands.
pub const CLOCK_REALTIME: u64 = 0;
pub const CLOCK_MONOTONIC: u64 = 1;
pub const CLOCK_BOOTTIME: u64 = 7;
// sigev_notify: what to do when it fires.
pub const SIGEV_SIGNAL: i32 = 0; // send sigev_signo to the process
pub const SIGEV_NONE: i32 = 1; // nothing (the program polls timer_gettime)
pub const SIGEV_THREAD_ID: i32 = 4; // send sigev_signo to thread sigev_tid
/// Ours, not Linux's: "count it in a timerfd" (user space can't ask for it).
const NOTIFY_TIMERFD: i32 = -1;
/// The owner of every timerfd's table entry (no real process has this pid).
const TIMERFD_PID: u64 = u64MAX;
/// At most this many POSIX timers per process: EAGAIN after that.
const MAX_TIMERS: usize = 32;
/// The id we use for a process's ITIMER_REAL (POSIX ids are 0..MAX_TIMERS).
const ITIMER_ID: i32 = -1;
static TIMERS: = new;
/// timerd sleeps here; poked when the table changes.
static CHANGED_QUEUE: WaitQueue = new;
static CHANGED: AtomicBool = new;
static STARTED: AtomicBool = new;
/// Start timerd. The kernel does this at boot (stage 18: before, the
/// first timer started it, so its stack showed up in the frame count of
/// whatever program armed a timer first). Calling it again does nothing.
/// Tell timerd to look again (starting it, if nobody did yet: the test
/// kernels in tests/ don't run the boot code).
/// How far CLOCK_REALTIME is ahead of the monotonic clock (µs), to turn
/// absolute wall-clock deadlines into ours.
/// The kernel thread that fires timers (see the top).
/// Send a due timer's signal.
/// (value, interval) of a timer in microseconds: time left, and the period.
// --- setitimer / getitimer / alarm ---------------------------------------
/// getitimer(ITIMER_REAL): (time left, interval), in microseconds.
/// setitimer(ITIMER_REAL, value, interval) (µs; value 0 disarms). Returns
/// the old (value, interval).
// --- POSIX timers --------------------------------------------------------
/// What timer_create's `struct sigevent` asked for.
/// timer_create: a new, disarmed timer. Returns its id.
/// timer_settime: arm (value > 0) or disarm. `absolute`: value is a time on
/// the timer's clock, not a delay. Returns the old (value, interval).
/// timer_gettime: (time left, interval) in microseconds.
/// timer_getoverrun: the overrun count of the last *delivered* signal.
/// A signal was just taken off a pending set to be delivered (or picked up
/// by sigwaitinfo). If it is a POSIX timer's, latch that timer's overrun
/// count: it goes into si_overrun and becomes what timer_getoverrun says,
/// and counting starts again from 0. Call without signal locks held.
/// timer_delete.
/// execve: POSIX timers go, the alarm stays.
/// The process ended: all its timers go.
/// How many timers exist (tests: nothing left behind).
// --- timerfd (stage 20) ---------------------------------------------------
use crate;
use AtomicI32;
/// Readers of every timerfd sleep here (one queue for all: timerfds are
/// few, and a reader that wakes for another fd's timer just sleeps again).
static TIMERFD_WAIT: WaitQueue = new;
/// An open timerfd. Its timer is the table entry (TIMERFD_PID, `id`).
/// timerfd_create: a new, disarmed timer on `clock` (already checked).
|