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 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 | //! Signals: asynchronous notifications to a process.
//!
//! A signal is a small number (SIGINT = 2, SIGSEGV = 11, ...) sent to a
//! process by the kernel (a bad memory access, Ctrl+C, a child exited, a
//! write to a pipe nobody reads) or by another process (`kill`). For each
//! signal a process chooses what happens (`rt_sigaction`):
//! * the default action -- terminate, terminate "with core dump" (we don't
//! write core files, so the same), ignore, stop or continue;
//! * ignore it;
//! * or run a *handler*: a function of the program, called in the middle
//! of whatever it was doing, like a hardware interrupt but in user space.
//! It can also *block* signals for a while (`rt_sigprocmask`): they stay
//! pending until unblocked. SIGKILL and SIGSTOP can't be caught, blocked or
//! ignored -- that's how you get rid of any process.
//!
//! **When is a signal acted on?** Sending only sets a bit in the target's
//! `pending` set (and wakes it if it sleeps). The target acts on it the
//! next time it returns from the kernel to user mode: after a syscall, a
//! page fault, or a timer interrupt (`deliver`, called from trap.rs's
//! return paths). A process blocked in a syscall (reading the keyboard,
//! waiting for a child) is woken up; the syscall gives up with EINTR, or is
//! transparently restarted if the handler was installed with SA_RESTART.
//!
//! **Running a handler** (`setup_frame`): we push a *signal frame* on the
//! user's stack holding all its registers (`ucontext`), the blocked mask
//! and a `siginfo` (who sent it, which address faulted...), then change the
//! saved registers so that "returning" to user mode lands in the handler:
//!
//! rip = handler, rdi = signal number, rsi = &siginfo, rdx = &ucontext
//! rsp = the frame, whose first word is the return address: `restorer`
//!
//! The restorer is a two-instruction function in the C library (`mov rax,
//! 15; syscall` = rt_sigreturn). When the handler returns, it runs, and
//! rt_sigreturn copies the saved registers back from the frame: the program
//! continues exactly where it was interrupted. The frame layout is Linux's
//! `struct rt_sigframe`, so real libcs (musl) will work unchanged.
//!
//! **Stop and continue** (job control): SIGSTOP / SIGTSTP (Ctrl+Z) stop a
//! process -- its threads just sleep -- until SIGCONT. The parent learns of
//! it from `wait4(WUNTRACED)` and SIGCHLD.
//!
//! **Threads.** Handlers (and so what a signal *does*) belong to the
//! process, but each thread has its own *mask* and its own pending set.
//! `kill` and Ctrl+C send to the process: whichever thread doesn't block
//! it takes it. A fault (SIGSEGV) or `tgkill` targets one thread. A fatal
//! signal kills all threads; a stop stops them all.
use crateErrno;
use crate;
use crate;
use crateTrapFrame;
use Arc;
use Vec;
use without_interrupts;
pub const SIGHUP: u32 = 1;
pub const SIGINT: u32 = 2;
pub const SIGQUIT: u32 = 3;
pub const SIGILL: u32 = 4;
pub const SIGTRAP: u32 = 5;
pub const SIGABRT: u32 = 6;
pub const SIGBUS: u32 = 7;
pub const SIGFPE: u32 = 8;
pub const SIGKILL: u32 = 9;
pub const SIGUSR1: u32 = 10;
pub const SIGSEGV: u32 = 11;
pub const SIGUSR2: u32 = 12;
pub const SIGPIPE: u32 = 13;
pub const SIGALRM: u32 = 14;
pub const SIGTERM: u32 = 15;
pub const SIGCHLD: u32 = 17;
pub const SIGCONT: u32 = 18;
pub const SIGSTOP: u32 = 19;
pub const SIGTSTP: u32 = 20;
pub const SIGTTIN: u32 = 21;
pub const SIGTTOU: u32 = 22;
pub const SIGURG: u32 = 23;
pub const SIGXCPU: u32 = 24;
pub const SIGXFSZ: u32 = 25;
pub const SIGWINCH: u32 = 28;
pub const SIGSYS: u32 = 31;
/// Signals 1..=64 (32..64 are the "real-time" ones: no default meaning).
pub const NSIG: u32 = 64;
/// Names for `kill -l`, messages and the shell.
/// What happens to a signal nobody installed a handler for.
// sigaction handler values and flags (Linux's).
pub const SIG_DFL: u64 = 0;
pub const SIG_IGN: u64 = 1;
pub const SA_NOCLDSTOP: u64 = 0x1;
pub const SA_NOCLDWAIT: u64 = 0x2;
pub const SA_SIGINFO: u64 = 0x4;
pub const SA_RESTORER: u64 = 0x0400_0000;
pub const SA_ONSTACK: u64 = 0x0800_0000;
pub const SA_RESTART: u64 = 0x1000_0000;
pub const SA_NODEFER: u64 = 0x4000_0000;
pub const SA_RESETHAND: u64 = 0x8000_0000;
// rt_sigprocmask `how`.
pub const SIG_BLOCK: u64 = 0;
pub const SIG_UNBLOCK: u64 = 1;
pub const SIG_SETMASK: u64 = 2;
// siginfo codes.
pub const SI_USER: i32 = 0;
pub const SI_KERNEL: i32 = 0x80;
pub const SEGV_MAPERR: i32 = 1; // no mapping at that address
pub const SEGV_ACCERR: i32 = 2; // mapped, but not allowed (e.g. write to read-only)
pub const CLD_EXITED: i32 = 1;
pub const CLD_KILLED: i32 = 2;
pub const CLD_DUMPED: i32 = 3;
pub const CLD_STOPPED: i32 = 5;
pub const CLD_CONTINUED: i32 = 6;
/// The set bit for signal `sig` in a 64-bit signal set (signal 1 = bit 0).
pub const
/// Never blockable.
const UNBLOCKABLE: u64 = bit | bit;
/// The kernel's `struct sigaction` (what rt_sigaction reads and writes;
/// the field order differs from the libc one).
/// Linux's `siginfo_t`: 128 bytes. Which fields follow the first three
/// depends on the signal: kill() -> sender pid + uid; SIGCHLD -> child pid,
/// uid, exit status; SIGSEGV -> faulting address.
/// What the parent's wait4(WUNTRACED / WCONTINUED) can report besides exits.
/// Pending signals with their siginfo (standard signals don't queue: one
/// pending instance each, like Linux).
/// One thread's signal state (`Task::sig`, locked with interrupts off).
/// A process's signal state. Locked with interrupts off (`Process::signals`).
/// Threads of a stopped process wait here for SIGCONT / SIGKILL.
static STOPPED: WaitQueue = new;
/// Make stopped threads look at their state again (process.rs: a thread
/// group is exiting).
/// Signals that make a stopped process continue / that stop it.
const STOP_SIGNALS: u64 = bit | bit | bit | bit;
/// Send signal `sig` to `p`. Returns at once; `p` acts on it on its way
/// back to user mode.
/// Send `sig` to one thread (tgkill). Its effect (handler, death, stop)
/// is still the process's; only who takes it differs.
/// Send to every process in process group `pgid`. ESRCH if it's empty.
/// The current process caused a fault (SIGSEGV, SIGFPE, SIGILL, SIGBUS).
/// It can't be ignored or blocked -- the program can't just continue at
/// the faulting instruction -- so if it is, the default action (death)
/// applies, like Linux's force_sig.
/// Should the current thread stop sleeping in a syscall: a signal it can
/// take, its process stopping, or being told to exit? (Called from
/// thread.rs with interrupts off.)
/// Tell `child`'s parent that it stopped/continued/exited: wake its wait4
/// and send it SIGCHLD.
/// The system-call results that mean "interrupted by a signal".
/// Make the interrupted syscall run again when we return to user mode:
/// restore its number and back up rip over the 2-byte `syscall` instruction.
/// On the way back to user mode: act on pending signals. Runs handlers
/// (by rewriting `frame`), applies default actions (which may end the
/// process right here), and sorts out interrupted syscalls.
/// ppoll / pselect6 / epoll_pwait / (sigsuspend-like) waits: block exactly
/// `mask` until `restore_temporary_mask`. If a signal interrupts the wait,
/// the syscall leaves the temporary mask in place and `deliver` restores
/// the real one -- after the handler, through the signal frame -- so the
/// signal that ended the wait can't be blocked again before it's handled.
/// Put the real mask back (no-op without a temporary one).
/// Stop the current process (all its threads) until SIGCONT (or SIGKILL).
/// Sleep until the process is continued, killed, or told to exit.
// ---------------------------------------------------------------------------
// The signal frame (Linux's struct rt_sigframe, x86_64)
// ---------------------------------------------------------------------------
/// The saved registers (`struct sigcontext`; this order is Linux's ABI).
/// User-settable RFLAGS bits (carry, parity, aux, zero, sign, trap,
/// direction, overflow, alignment check, resume). The rest (interrupt
/// enable, I/O privilege level...) only the kernel decides.
const USER_RFLAGS: u64 = 0x0004_0DD5 | ;
// The FPU/SSE/AVX state in a signal frame (Linux's layout, stage 15.1).
// It sits above the rt_sigframe, 64-byte aligned, and
// `uc.mcontext.fpstate` points at it. Its first 512 bytes are the FXSAVE
// format; bytes 464..512 (which FXSAVE leaves unused) hold
// `struct _fpx_sw_bytes`, saying whether an XSAVE area follows:
//
// u32 magic1 = FP_XSTATE_MAGIC1 u32 extended_size (state + 4)
// u64 xfeatures (= XCR0) u32 xstate_size + 7 u32 padding
//
// and FP_XSTATE_MAGIC2 is stored right after the XSAVE area. With only
// FXSAVE, magic1 is 0. A handler may read and even change the saved
// values: sigreturn loads whatever is there (after checking it).
const FP_XSTATE_MAGIC1: u32 = 0x4650_5853;
const FP_XSTATE_MAGIC2: u32 = 0x4650_5845;
const SW_BYTES: usize = 464;
/// uc_flags: an XSAVE area is there (UC_FP_XSTATE), and the two flags
/// Linux always sets on x86_64 (UC_SIGCONTEXT_SS | UC_STRICT_RESTORE_SS).
const UC_FP_XSTATE: u64 = 1;
const UC_SS_FLAGS: u64 = 2 | 4;
/// Bytes the FPU state takes in a signal frame.
/// Push a signal frame on the user stack and point `frame` at the handler.
/// The stack, from higher to lower addresses:
///
/// [interrupted code's stack] [128-byte red zone] [FPU state, 64-aligned]
/// [rt_sigframe: pretcode, ucontext, siginfo] <- rsp of the handler
/// rt_sigreturn (15): the handler returned into the restorer. The frame
/// starts 8 bytes below rsp (`ret` popped `pretcode`). Put back the saved
/// registers and mask -- after checking them: the program may have changed
/// the frame (that's allowed and used), but must not get kernel privileges
/// or a kernel rip out of it.
/// sigreturn's FPU part: load the state `fpstate` points to (0 = clean
/// registers, like Linux). An XSAVE area is used only if both magic numbers
/// are there; otherwise just the 512-byte FXSAVE part (the AVX halves are
/// then cleared). Err if it's unreadable or has an impossible MXCSR.
// ---------------------------------------------------------------------------
// Syscalls
// ---------------------------------------------------------------------------
/// rt_sigaction(sig, *new, *old, sigsetsize = 8).
/// rt_sigprocmask(how, *set, *old, sigsetsize = 8): the calling thread's
/// mask. Returns the old mask.
/// kill(pid, sig): pid > 0 one process, 0 = my process group, -1 = every
/// process but me, < -1 = process group -pid. sig 0 = just check it exists.
|