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 | //! 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, then
//! removes the child from the process table ("reaps" it -- until then it's
//! what Unix calls a zombie).
//!
//! Not done yet (exercises): freeing a dead process's frames and page tables,
//! reparenting orphans, 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;
/// 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.
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.
/// 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.
/// End the current process (exit syscall, or killed by an exception).
!
/// Block (the calling thread) until `process` has ended, then remove it from
/// the process table. Used by the kernel and by the `wait` syscall.
/// The `wait(pid)` syscall: only a process's parent may wait for it.
|