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 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 | //! Paging: page tables, address translation and a physical frame allocator.
//!
//! x86_64 uses 4-level page tables (L4 -> L3 -> L2 -> L1 -> 4 KiB page).
//! CR3 holds the *physical* address of the active level-4 table. But the CPU
//! (and our code) only uses *virtual* addresses, so to read or edit a page
//! table we need it mapped somewhere. The bootloader's `map_physical_memory`
//! feature maps all of physical RAM at `physical_memory_offset`, so
//! virtual address = physical_memory_offset + physical address.
//! `OffsetPageTable` from the x86_64 crate implements exactly that.
//!
//! Address spaces (for user processes): every process gets its own level-4
//! table. Its entries are copied from the kernel's level-4 table, so the
//! kernel (code, heap, stacks, the physical-memory window) is mapped
//! identically everywhere -- just *without* the USER_ACCESSIBLE flag, so ring
//! 3 code can't touch it. User code and stack go into L4 slot 1
//! (`USER_START..USER_END`), which the kernel doesn't use. Switching CR3 then
//! swaps only the user part. (Linux does the same with the upper half; our
//! bootloader put the kernel in the *lower* half, so we use a free slot.)
use BTreeMap;
use ;
use ;
use Mutex;
use without_interrupts;
use ;
/// User space: level-4 slot 1, i.e. 512 GiB starting at 512 GiB.
pub const USER_START: u64 = 0x0000_0080_0000_0000;
pub const USER_END: u64 = 0x0000_0100_0000_0000; // exclusive
const USER_L4_INDEX: usize = 1;
/// Where all physical memory is mapped (set by `init`).
static PHYS_OFFSET: AtomicU64 = new;
/// Physical address of the kernel's (boot) level-4 table (set by `init`).
static KERNEL_L4: AtomicU64 = new;
/// The frame allocator, once `init_frame_allocator` hands it over.
static FRAME_ALLOCATOR: = new;
/// Create an `OffsetPageTable` for the currently active page tables.
///
/// Unsafe: the caller must guarantee that all physical memory really is
/// mapped at `physical_memory_offset`, and must call this only once (two
/// `&mut` to the same page table would be undefined behaviour).
pub unsafe
/// Find the active level-4 table via CR3 and return a reference to it.
unsafe
/// Demo: map the virtual page containing `page` to the VGA text buffer's
/// physical frame (0xb8000). Writing through that page then shows on screen.
/// Hands out unused physical 4 KiB frames, taken from the memory map the
/// bootloader got from the BIOS (E820). Very simple: it never frees, and
/// walks the map from the start on every call (fine for now).
unsafe
/// Total usable RAM in bytes, according to the memory map (for printing).
// ---------------------------------------------------------------------------
// Global frame allocator + per-process address spaces
// ---------------------------------------------------------------------------
/// Hand the frame allocator over to the kernel (after the heap is set up),
/// so processes can get page tables and pages later.
/// Frames given back with `free_frame`, as a linked list threaded through
/// the free frames themselves: the first 8 bytes of each free frame hold the
/// physical address of the next one (0 = end). No heap memory needed.
static FREE_FRAMES: = new;
/// Frames handed out by the boot allocator so far (for statistics).
static BOOT_FRAMES_USED: AtomicU64 = new;
/// One physical frame (contents undefined), or None if out of memory / not
/// initialized. Recycled frames first, then fresh ones from the memory map.
///
/// The frame locks are only ever taken with interrupts off (see the locking
/// rule in thread.rs): the scheduler and interrupt handlers free frames too.
/// A never-used frame straight from the boot allocator.
/// Give a frame back. The caller promises nothing uses it any more (no page
/// table maps it, no device writes to it).
/// Usable RAM in 4 KiB frames, and frames used before `init_frame_allocator`.
static TOTAL_FRAMES: AtomicU64 = new;
static EARLY_FRAMES: AtomicU64 = new;
/// (total, free) RAM in 4 KiB frames, for sysinfo and the ramfs's statfs.
/// How many frames are in use right now (handed out and not freed).
/// `FrameAllocator` adapter around the global allocator (for `map_to`, which
/// may need frames for new page tables).
;
unsafe
/// Virtual address through which the kernel can reach physical address `phys`.
/// The kernel's level-4 table frame (what kernel threads run with).
/// Does the CPU honour the NO_EXECUTE page flag? (EFER.NXE, which our
/// bootloader turns on. With it off, setting the bit would be an error.)
/// `count` physically *contiguous*, zeroed frames (device DMA buffers like
/// virtio queues must be contiguous in physical memory). Our allocator hands
/// out frames in address order, so consecutive frames are usually adjacent;
/// if a run gets broken (end of a memory region) we start over and simply
/// leak the frames we skipped. Fine for a few boot-time allocations.
/// A frame filled with zeros (new page tables and user pages must not leak
/// old data).
/// Page-table view of the address space whose level-4 table is `l4`.
///
/// Unsafe: the caller must not create two of these for the same table at the
/// same time (both would hand out `&mut` to the same memory).
unsafe
/// Create a new address space: a fresh level-4 table that shares all kernel
/// mappings (entries copied from the kernel's table) and has an empty user part.
/// Free everything a process's address space owns: every user page's frame
/// (or just our share of it, see `release_frame`), the page tables of the
/// user slot (levels 3, 2, 1) and the level-4 table. The kernel entries
/// are shared with every other address space, so they're left alone.
///
/// Unsafe: `l4` must not be in use by any thread (not loaded in CR3, and the
/// thread that ran in it must have finished).
pub unsafe
/// A page table, reached through the physical-memory map.
/// Remove the whole user part of address space `l4` (execve does this to
/// the running process: the caller must flush the TLB afterwards). The
/// level-4 table itself stays.
// ---------------------------------------------------------------------------
// Shared frames (copy-on-write and shared memory)
// ---------------------------------------------------------------------------
//
// After fork, parent and child map the *same* frames until one of them
// writes (copy-on-write); MAP_SHARED memory is mapped by several processes
// for good. A frame may only be freed when the last of them lets go, so
// shared frames carry a reference count. Most frames are never shared, so
// instead of a counter for every frame of RAM (Linux's `struct page`) we
// keep a small map with only the shared ones: no entry = one owner.
/// Physical address -> number of owners (always >= 2). Locked with
/// interrupts off (frames are released in the scheduler, see thread.rs).
static SHARED_FRAMES: = new;
/// One more address space maps `frame`.
/// How many address spaces map `frame` (1 if it isn't shared).
/// An address space stops using `frame`: free it if that was the last one.
/// Number of frames with more than one owner right now (for tests).
// ---------------------------------------------------------------------------
// User page-table entries, one page at a time (mm.rs decides what goes where)
// ---------------------------------------------------------------------------
/// Flags for the L3/L2/L1 tables above user pages: always present, writable
/// and user-accessible. The *leaf* entry alone decides what's allowed (the
/// CPU combines all levels, so the upper ones must allow everything).
const USER_TABLE_FLAGS: PageTableFlags =
PRESENT.union.union;
/// The frame and flags mapped at user address `addr` in `l4`, if any.
/// Map `frame` at user address `addr` (must be unmapped) with `flags`.
/// Change the flags of the page at `addr` (must be mapped).
/// Point the (mapped) page at `addr` to another frame. Returns the old one.
/// Remove the mapping at `addr`; returns its frame (the caller releases it).
/// The page tables stay (they're freed with the address space).
/// Drop this CPU's cached translation of user page `addr` (call after
/// changing a mapping of the *active* address space; for an inactive one
/// it's harmless). Other CPUs can't have it cached: a process runs on one
/// CPU at a time and switching CR3 flushes user mappings.
/// Copy the user part of address space `src` into the (empty) `dst`, for
/// fork. Both end up mapping the same frames (each frame's reference count
/// goes up). `flags(addr, old)` decides the new flags of each page, in
/// *both* address spaces -- fork uses it to make private pages read-only,
/// so the first write to them faults and gets its own copy.
/// On failure (out of memory) `dst` may be partly filled: free it.
/// Make sure `page` (in user space) is mapped in address space `l4` with at
/// least `flags` (USER_ACCESSIBLE + PRESENT are added). A new page gets a
/// zeroed frame; an already mapped one keeps its frame and gets the union
/// of old and new flags. Returns the backing frame.
/// Can user code legitimately access `len` bytes at `addr` in the *current*
/// address space? Checks the range is inside user space and every page is
/// mapped USER_ACCESSIBLE (and WRITABLE if `write`). Syscalls call this
/// before touching any pointer a user program hands them; otherwise a program
/// could make the kernel read or overwrite kernel memory for it.
// ---------------------------------------------------------------------------
// Kernel-space mappings for kernel stacks (see stack.rs)
// ---------------------------------------------------------------------------
/// The L4 slot that holds every kernel stack (0x0000_6400_0000_0000).
pub const STACKS_L4_INDEX: usize = 200;
/// Protects the kernel's own page tables. Taken with interrupts off.
static KERNEL_TABLES: = new;
/// Give the kernel-stack slot its level-3 table *now*, before any process
/// address space is created. New address spaces copy the kernel's L4
/// entries, so they all point at this one L3 table, and every stack mapped
/// later shows up in every address space automatically.
/// Map `page` (inside the kernel stack area) to a fresh zeroed frame:
/// present, writable, not executable, kernel-only.
/// Unmap a page mapped by `map_kernel_page` and free its frame. (The page
/// tables stay: the next stack in the same place reuses them.)
/// Make device registers at physical `phys..phys + len` (a PCI memory BAR)
/// reachable and return their virtual address. The bootloader's physical
/// memory map usually covers them already (it maps up to 4 GiB, holes
/// included); BARs placed above that get mapped here, page by page, with
/// caching off (a device register must be read from the device every time).
|