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 | //! Waiting for several files at once: poll, select and epoll.
//!
//! A server talking to many clients can't call `read` on one of them and
//! block -- the others would wait. Instead it asks the kernel "tell me
//! which of these fds are ready" and then only reads/writes those, which
//! never blocks. That loop ("wait for events, handle each, repeat") is an
//! **event loop**; every GUI toolkit, web server and terminal emulator is
//! built around one. Three interfaces for the same idea:
//!
//! * `poll(fds, n, timeout)`: an array of {fd, events wanted, events
//! returned}. Simple; the kernel looks at every fd on every call.
//! * `select(n, readfds, writefds, exceptfds, timeout)`: the 1983 BSD
//! original, with bitmaps of fds instead of an array.
//! * `epoll`: register the fds *once* in an epoll instance (itself an
//! fd) with epoll_ctl, then epoll_wait returns just the ready ones.
//! Linux's answer to servers with 10 000 connections.
//!
//! **How our kernel does it.** Every file can say what's ready right now
//! (`File::poll`). Anything that might make a file ready -- data into a
//! pipe, a key typed, a packet for a socket -- calls `notify()`, which
//! bumps a global counter and wakes every poller. A poller checks all its
//! fds; if none is ready it sleeps until the counter changes (or its
//! timeout), then checks again. Simple, and never misses an event; a big
//! kernel instead keeps per-file wait lists so one packet only wakes the
//! pollers of that socket.
//!
//! Simplification: epoll is *level-triggered* only (EPOLLET is accepted
//! but means the same thing: "ready" is reported as long as it lasts).
//! Regular files are always ready (Linux too), and epoll refuses them
//! with EPERM like Linux does.
use crateErrno;
use crate;
use crate;
use crateWaitQueue;
use ;
use Vec;
use ;
use Mutex;
// poll(2) event bits (Linux's values). epoll uses the same numbers.
pub const POLLIN: u16 = 0x1; // data to read (or EOF / a connection to accept)
pub const POLLPRI: u16 = 0x2; // urgent data
pub const POLLOUT: u16 = 0x4; // room to write
pub const POLLERR: u16 = 0x8; // error (e.g. pipe without readers)
pub const POLLHUP: u16 = 0x10; // other side hung up
pub const POLLNVAL: u16 = 0x20; // not an open fd
pub const POLLRDNORM: u16 = 0x40;
pub const POLLWRNORM: u16 = 0x100;
pub const POLLRDHUP: u16 = 0x2000;
/// Bumped on every possible readiness change (see the top).
static GENERATION: AtomicU64 = new;
/// Pollers sleep here.
static POLLERS: WaitQueue = new;
/// Something may have become ready: wake all pollers to look again.
/// Add the "normal data" aliases a program may ask for instead.
/// The poll loop: call `check` (with interrupts on) until it returns
/// Some, the deadline (PIT tick; Some(0) = don't wait) passes -> Ok(None),
/// or a signal arrives -> Err.
/// A timeout in milliseconds (negative = forever) as a deadline.
/// A `struct timespec *` timeout (NULL = forever) as a deadline.
/// ppoll / pselect6 / epoll_pwait: run with signal mask `mask_ptr` (if
/// given) while waiting. If a signal interrupts, the old mask comes back
/// only after its handler ran (signal.rs), like Linux -- otherwise the
/// signal we waited for could end up blocked again before delivery.
// ---------------------------------------------------------------------------
// poll / ppoll
// ---------------------------------------------------------------------------
/// poll(fds, nfds, timeout_ms) (7) and ppoll(fds, nfds, *timespec, *sigmask,
/// 8) (271). `struct pollfd {int fd; short events; short revents;}`.
// ---------------------------------------------------------------------------
// select / pselect6
// ---------------------------------------------------------------------------
/// select(nfds, *readfds, *writefds, *exceptfds, *timeval) (23). Each set
/// is a bitmap: bit n = fd n. On return the sets hold only the ready fds.
/// select's `struct timeval {seconds, microseconds}` (NULL = forever).
/// pselect6(nfds, r, w, e, *timespec, *{sigmask ptr, size}) (270).
// ---------------------------------------------------------------------------
// epoll
// ---------------------------------------------------------------------------
pub const EPOLL_CTL_ADD: u64 = 1;
pub const EPOLL_CTL_DEL: u64 = 2;
pub const EPOLL_CTL_MOD: u64 = 3;
pub const EPOLLET: u32 = 1 << 31;
pub const EPOLLONESHOT: u32 = 1 << 30;
pub const EPOLL_CLOEXEC: u64 = 0o2000000;
/// One watched fd.
/// An epoll instance: the "interest list".
/// epoll_create1(flags) (291); epoll_create(size) (213) is the same
/// without flags.
/// `struct epoll_event {u32 events; u64 data;}` -- packed: 12 bytes.
/// epoll_ctl(epfd, op, fd, *event) (233).
/// epoll_wait(epfd, *events, max, timeout_ms) (232) and epoll_pwait(...,
/// *sigmask, 8) (281).
|