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 | //! Processes = a thread + its own address space + a file descriptor table
//! (+ arguments, a current directory and a parent).
//!
//! Starting one (`spawn_image`):
//! 1. new level-4 page table sharing the kernel mappings (memory.rs)
//! 2. load the ELF program's segments into its user part (elf.rs)
//! 3. map a user stack just below `USER_END`
//! 4. fd table: 0, 1, 2 = whatever the parent chose (console by default,
//! or e.g. a pipe -- that's how the shell will do `a | b` and `> file`)
//! 5. put it in the process table and spawn a thread with that CR3. The
//! thread starts in the kernel (`user_thread_entry`), copies the
//! arguments onto the user stack and "returns" into ring 3 with `iretq`.
//!
//! Why `spawn(path, argv)` instead of Unix `fork()` + `exec()`? fork copies
//! the whole address space (or needs copy-on-write page tables to be fast),
//! and then exec throws the copy away again. spawn ("create a new process
//! running this program") is one simple step, and it's what Windows
//! (CreateProcess) and POSIX's own `posix_spawn` offer. Redirection, which
//! Unix shells do between fork and exec, is done with spawn's stdio options.
//!
//! Exit and wait: a process's exit status is stored in it and its `exited`
//! wait queue is woken; the parent's `wait(pid)` blocks on that queue, waits
//! until the thread is completely gone, then removes the child from the
//! process table ("reaps" it -- until then it's what Unix calls a zombie).
//!
//! Memory: a process's frames and page tables are freed when the last
//! `Arc<Process>` goes away (`impl Drop`). That's always after its thread is
//! gone: the thread holds an Arc until the scheduler frees it, which happens
//! only once the CPU has switched to another thread and address space. So
//! no CPU can still be using the page tables we free.
//!
//! Orphans: when a parent exits first, its running children become orphans
//! (parent = ORPHAN); nobody will wait for them, so they remove themselves
//! from the table when they exit. Unix hands them to `init` instead.
//!
//! Not done yet: signals/kill, more than one thread per process.
use crate;
use crateErrno;
use crate;
use crate;
use crate;
use crate::;
use ;
use ;
use Mutex;
use VirtAddr;
use without_interrupts;
use ;
/// The user stack: 16 KiB, ending one (unmapped) page below USER_END.
pub const USER_STACK_TOP: u64 = USER_END - 0x1000;
pub const USER_STACK_SIZE: u64 = 16 * 1024;
/// Below the stack: a 64 KiB guard zone that is never mapped, so a runaway
/// recursion hits it and gets killed with "stack overflow" (instead of
/// scribbling over the program's data). Big, so that one large stack frame
/// can't jump over it.
pub const USER_STACK_GUARD: u64 = 64 * 1024;
/// Lowest address of stack + guard: program segments must end below this.
pub const USER_STACK_ZONE_START: u64 = USER_STACK_TOP - USER_STACK_SIZE - USER_STACK_GUARD;
/// `parent` of a process whose parent exited first.
pub const ORPHAN: u64 = u64MAX;
/// Is user address `addr` in the guard zone under the user stack?
/// Max open files per process.
const MAX_FDS: usize = 16;
/// Max total size of a program's arguments (they go on its 16 KiB stack).
pub const MAX_ARGS_SIZE: usize = 2048;
/// Exit code `wait` reports for a process the kernel killed (like a shell
/// shows 128 + SIGSEGV(11) for a segfault).
pub const KILLED_EXIT_CODE: i64 = 139;
static NEXT_PID: AtomicU64 = new;
/// All processes that haven't been reaped yet, by pid. Locked with
/// interrupts off only (exit_current takes it with interrupts off).
static PROCESSES: = new;
/// Everything needed to start a process besides the program itself.
/// Load the program at `path` (absolute) from the VFS and start it.
/// Start ELF program `elf_image` from the kernel (console stdio, no args).
/// Load `elf_image` into a new address space and start it.
/// Load the program and map its stack; returns the entry point.
/// Create the `Process` and its thread.
/// First code of every process thread (still ring 0, but already running
/// with the process's CR3 and kernel stack).
/// Jump to ring 3. `iretq` pops RIP, CS, RFLAGS, RSP, SS -- we push a fake
/// interrupt frame with user selectors (RPL 3), so "returning" from it drops
/// the privilege level. RFLAGS = 0x202: interrupts enabled (+ the always-1 bit).
/// rdi/rsi carry `_start(argc, argv)`'s arguments.
unsafe !
/// Look up a live (or not yet reaped) process.
/// How many processes are in the table (running or not yet reaped).
/// End the current process (exit syscall, or killed by an exception).
!
/// Block (the calling thread) until `process` has ended and its thread is
/// gone, then remove it from the process table. Used by the kernel and by
/// the `wait` syscall. Once the caller drops its own `Arc`, all of the
/// process's memory is free again.
/// Wait for the (exited) process's thread to be freed, then remove the
/// process from the table.
/// `wait` flag: don't block; `EAGAIN` if the child is still running.
pub const WNOHANG: u64 = 1;
/// The `wait(pid, flags)` syscall: only a process's parent may wait for it.
|