| @@ -7,10 +7,13 @@ |
| 7 | 7 | //! - **Stacks** are the runtime's own: an anonymous mapping with a guard |
| 8 | 8 | //! page at its low end, committed lazily by the kernel. |
| 9 | 9 | //! - **The switch** is the runtime's own on x86_64 Linux: it saves the |
| 10 | | −//! callee-saved registers (and the SSE/x87 control words) on the current |
| 11 | | −//! stack, stores the stack pointer, and loads the other one. A suspended |
| 12 | | −//! fiber is just its saved stack pointer, so it may resume on any |
| 13 | | −//! thread. Other targets use corosensei on the same stacks. |
| 10 | +//! callee-saved registers (and the floating-point control words) on the |
| 11 | +//! current stack, stores the stack pointer, and loads the other one. A |
| 12 | +//! suspended fiber is just its saved stack pointer, so it may resume on |
| 13 | +//! any thread. The aarch64 Linux switch is written the same way but is |
| 14 | +//! untested on hardware, so it is used only with |
| 15 | +//! `--cfg tilrt_aarch64_switch` (ADR 0011). Other targets, and aarch64 by |
| 16 | +//! default, use corosensei on the same stacks. |
| 14 | 17 | //! |
| 15 | 18 | //! Rules (as before): |
| 16 | 19 | //! - a fiber is never dropped while suspended: its frames would never run |
| @@ -64,7 +67,13 @@ impl Stack { |
| 64 | 67 | } |
| 65 | 68 | |
| 66 | 69 | /// The lowest usable address (just above the guard page). |
| 67 | | − #[cfg_attr(all(target_arch = "x86_64", target_os = "linux"), allow(dead_code))] |
| 70 | + #[cfg_attr( |
| 71 | + any( |
| 72 | + all(target_arch = "x86_64", target_os = "linux"), |
| 73 | + all(target_arch = "aarch64", target_os = "linux", tilrt_aarch64_switch) |
| 74 | + ), |
| 75 | + allow(dead_code) |
| 76 | + )] |
| 68 | 77 | fn limit(&self) -> usize { |
| 69 | 78 | self.base as usize + PAGE |
| 70 | 79 | } |
| @@ -81,7 +90,14 @@ impl Drop for Stack { |
| 81 | 90 | /// copy that can be written back in place to resume the fiber there again |
| 82 | 91 | /// (multi-shot continuations, ADR 0017). Restoring at the same address |
| 83 | 92 | /// keeps every pointer into the stack valid; the references the frames |
| 84 | | −/// hold are the caller's business. |
| 93 | +/// hold are the caller's business. (corosensei takes none.) |
| 94 | +#[cfg_attr( |
| 95 | + not(any( |
| 96 | + all(target_arch = "x86_64", target_os = "linux"), |
| 97 | + all(target_arch = "aarch64", target_os = "linux", tilrt_aarch64_switch) |
| 98 | + )), |
| 99 | + allow(dead_code) |
| 100 | +)] |
| 85 | 101 | pub(crate) struct Snapshot { |
| 86 | 102 | sp: usize, |
| 87 | 103 | words: Vec<usize>, |
| @@ -94,7 +110,10 @@ pub(crate) struct Snapshot { |
| 94 | 110 | /// # Safety |
| 95 | 111 | /// Both ranges must be valid and not overlap. |
| 96 | 112 | #[cfg_attr( |
| 97 | | − not(all(target_arch = "x86_64", target_os = "linux")), |
| 113 | + not(any( |
| 114 | + all(target_arch = "x86_64", target_os = "linux"), |
| 115 | + all(target_arch = "aarch64", target_os = "linux", tilrt_aarch64_switch) |
| 116 | + )), |
| 98 | 117 | allow(dead_code) |
| 99 | 118 | )] |
| 100 | 119 | unsafe fn copy_words(src: *const usize, dst: *mut usize, n: usize) { |
| @@ -123,7 +142,10 @@ pub(crate) use imp::{Coroutine, Yielder}; |
| 123 | 142 | /// the code after it, which is true of a switch on one thread. Moving a |
| 124 | 143 | /// fiber to another thread goes through a run queue, which TSan sees. |
| 125 | 144 | #[cfg_attr( |
| 126 | | − not(all(target_arch = "x86_64", target_os = "linux")), |
| 145 | + not(any( |
| 146 | + all(target_arch = "x86_64", target_os = "linux"), |
| 147 | + all(target_arch = "aarch64", target_os = "linux", tilrt_aarch64_switch) |
| 148 | + )), |
| 127 | 149 | allow(dead_code) |
| 128 | 150 | )] |
| 129 | 151 | mod tsan { |
| @@ -187,10 +209,16 @@ mod tsan { |
| 187 | 209 | } |
| 188 | 210 | } |
| 189 | 211 | |
| 190 | | −#[cfg(all(target_arch = "x86_64", target_os = "linux"))] |
| 212 | +/// The runtime's own switch (x86_64 Linux; aarch64 Linux with |
| 213 | +/// `--cfg tilrt_aarch64_switch`): what is common to both architectures. |
| 214 | +#[cfg(any( |
| 215 | + all(target_arch = "x86_64", target_os = "linux"), |
| 216 | + all(target_arch = "aarch64", target_os = "linux", tilrt_aarch64_switch) |
| 217 | +))] |
| 191 | 218 | mod imp { |
| 192 | 219 | use std::marker::PhantomData; |
| 193 | 220 | |
| 221 | + use self::arch::{initial_frame, tl_fiber_switch}; |
| 194 | 222 | use super::{CoroutineResult, Snapshot, Stack, tsan}; |
| 195 | 223 | |
| 196 | 224 | /// Where a fiber and the code resuming it exchange values. Boxed, so |
| @@ -381,88 +409,201 @@ mod imp { |
| 381 | 409 | } |
| 382 | 410 | } |
| 383 | 411 | |
| 384 | | − /// Lay out the frame `tl_fiber_switch` pops on the first resume: the |
| 385 | | − /// control words, six callee-saved registers (rbx = `start`, r12 = |
| 386 | | − /// `entry`), then the return address `tl_fiber_trampoline`, which |
| 387 | | − /// calls `entry(start)` with the stack aligned as after a call. |
| 388 | | − fn initial_frame(stack: &Stack, start: *mut u8, entry: usize) -> usize { |
| 389 | | − let top = stack.top() & !15; |
| 390 | | − let words: [usize; 10] = [ |
| 391 | | − 0x037F_0000_1F80, // MXCSR (low half) and the x87 control word |
| 392 | | − 0, // r15 |
| 393 | | − 0, // r14 |
| 394 | | − 0, // r13 |
| 395 | | − entry, // r12 |
| 396 | | − start as usize, // rbx |
| 397 | | − 0, // rbp |
| 398 | | − tl_fiber_trampoline as *const () as usize, |
| 399 | | − 0, // the trampoline's "return address": ends backtraces |
| 400 | | − 0, // padding (keeps `top` 16-aligned) |
| 401 | | − ]; |
| 402 | | − let sp = top - words.len() * 8; |
| 403 | | − // SAFETY: the words fit in the stack's top 80 bytes. |
| 404 | | − unsafe { |
| 405 | | − std::ptr::copy_nonoverlapping(words.as_ptr(), sp as *mut usize, words.len()); |
| 412 | + /// The architecture's half: the switch, and the first frame it pops. |
| 413 | + #[cfg(target_arch = "x86_64")] |
| 414 | + mod arch { |
| 415 | + use super::Stack; |
| 416 | + |
| 417 | + /// Lay out the frame `tl_fiber_switch` pops on the first resume: |
| 418 | + /// the control words, six callee-saved registers (rbx = `start`, |
| 419 | + /// r12 = `entry`), then the return address `tl_fiber_trampoline`, |
| 420 | + /// which calls `entry(start)` with the stack aligned as after a |
| 421 | + /// call. |
| 422 | + pub(super) fn initial_frame(stack: &Stack, start: *mut u8, entry: usize) -> usize { |
| 423 | + let top = stack.top() & !15; |
| 424 | + let words: [usize; 10] = [ |
| 425 | + 0x037F_0000_1F80, // MXCSR (low half) and the x87 control word |
| 426 | + 0, // r15 |
| 427 | + 0, // r14 |
| 428 | + 0, // r13 |
| 429 | + entry, // r12 |
| 430 | + start as usize, // rbx |
| 431 | + 0, // rbp |
| 432 | + tl_fiber_trampoline as *const () as usize, |
| 433 | + 0, // the trampoline's "return address": ends backtraces |
| 434 | + 0, // padding (keeps `top` 16-aligned) |
| 435 | + ]; |
| 436 | + let sp = top - words.len() * 8; |
| 437 | + // SAFETY: the words fit in the stack's top 80 bytes. |
| 438 | + unsafe { |
| 439 | + std::ptr::copy_nonoverlapping(words.as_ptr(), sp as *mut usize, words.len()); |
| 440 | + } |
| 441 | + sp |
| 406 | 442 | } |
| 407 | | − sp |
| 408 | | − } |
| 409 | 443 | |
| 410 | | − unsafe extern "C" { |
| 411 | | − /// Save the current context, storing its stack pointer in `save`, |
| 412 | | − /// and continue the one suspended at `to`. |
| 413 | | − fn tl_fiber_switch(save: *mut usize, to: usize); |
| 414 | | − fn tl_fiber_trampoline(); |
| 444 | + unsafe extern "C" { |
| 445 | + /// Save the current context, storing its stack pointer in |
| 446 | + /// `save`, and continue the one suspended at `to`. |
| 447 | + pub(super) fn tl_fiber_switch(save: *mut usize, to: usize); |
| 448 | + fn tl_fiber_trampoline(); |
| 449 | + } |
| 450 | + |
| 451 | + // System V x86_64: rbx, rbp, r12–r15, MXCSR's control bits and the |
| 452 | + // x87 control word are callee-saved; everything else may be |
| 453 | + // clobbered by a call, which is what the switch looks like to its |
| 454 | + // callers. |
| 455 | + std::arch::global_asm!( |
| 456 | + ".text", |
| 457 | + ".globl tl_fiber_switch", |
| 458 | + ".type tl_fiber_switch,@function", |
| 459 | + ".p2align 4", |
| 460 | + "tl_fiber_switch:", |
| 461 | + " .cfi_startproc", |
| 462 | + " push rbp", |
| 463 | + " push rbx", |
| 464 | + " push r12", |
| 465 | + " push r13", |
| 466 | + " push r14", |
| 467 | + " push r15", |
| 468 | + " sub rsp, 8", |
| 469 | + " stmxcsr [rsp]", |
| 470 | + " fnstcw [rsp + 4]", |
| 471 | + " mov [rdi], rsp", |
| 472 | + " mov rsp, rsi", |
| 473 | + " ldmxcsr [rsp]", |
| 474 | + " fldcw [rsp + 4]", |
| 475 | + " add rsp, 8", |
| 476 | + " pop r15", |
| 477 | + " pop r14", |
| 478 | + " pop r13", |
| 479 | + " pop r12", |
| 480 | + " pop rbx", |
| 481 | + " pop rbp", |
| 482 | + " ret", |
| 483 | + " .cfi_endproc", |
| 484 | + ".size tl_fiber_switch, .-tl_fiber_switch", |
| 485 | + "", |
| 486 | + ".globl tl_fiber_trampoline", |
| 487 | + ".type tl_fiber_trampoline,@function", |
| 488 | + ".p2align 4", |
| 489 | + "tl_fiber_trampoline:", |
| 490 | + " .cfi_startproc", |
| 491 | + " .cfi_undefined rip", |
| 492 | + " mov rdi, rbx", |
| 493 | + " call r12", |
| 494 | + " ud2", |
| 495 | + " .cfi_endproc", |
| 496 | + ".size tl_fiber_trampoline, .-tl_fiber_trampoline", |
| 497 | + ); |
| 415 | 498 | } |
| 416 | 499 | |
| 417 | | − // System V x86_64: rbx, rbp, r12–r15, MXCSR's control bits and the x87 |
| 418 | | − // control word are callee-saved; everything else may be clobbered by a |
| 419 | | − // call, which is what the switch looks like to its callers. |
| 420 | | − std::arch::global_asm!( |
| 421 | | − ".text", |
| 422 | | − ".globl tl_fiber_switch", |
| 423 | | − ".type tl_fiber_switch,@function", |
| 424 | | − ".p2align 4", |
| 425 | | − "tl_fiber_switch:", |
| 426 | | − " .cfi_startproc", |
| 427 | | − " push rbp", |
| 428 | | − " push rbx", |
| 429 | | − " push r12", |
| 430 | | − " push r13", |
| 431 | | − " push r14", |
| 432 | | − " push r15", |
| 433 | | − " sub rsp, 8", |
| 434 | | − " stmxcsr [rsp]", |
| 435 | | − " fnstcw [rsp + 4]", |
| 436 | | − " mov [rdi], rsp", |
| 437 | | − " mov rsp, rsi", |
| 438 | | − " ldmxcsr [rsp]", |
| 439 | | − " fldcw [rsp + 4]", |
| 440 | | − " add rsp, 8", |
| 441 | | − " pop r15", |
| 442 | | − " pop r14", |
| 443 | | − " pop r13", |
| 444 | | − " pop r12", |
| 445 | | − " pop rbx", |
| 446 | | − " pop rbp", |
| 447 | | − " ret", |
| 448 | | − " .cfi_endproc", |
| 449 | | − ".size tl_fiber_switch, .-tl_fiber_switch", |
| 450 | | − "", |
| 451 | | − ".globl tl_fiber_trampoline", |
| 452 | | − ".type tl_fiber_trampoline,@function", |
| 453 | | − ".p2align 4", |
| 454 | | − "tl_fiber_trampoline:", |
| 455 | | − " .cfi_startproc", |
| 456 | | − " .cfi_undefined rip", |
| 457 | | − " mov rdi, rbx", |
| 458 | | − " call r12", |
| 459 | | − " ud2", |
| 460 | | − " .cfi_endproc", |
| 461 | | − ".size tl_fiber_trampoline, .-tl_fiber_trampoline", |
| 462 | | − ); |
| 500 | + /// **Untested on hardware** (ADR 0011, aarch64 note): there is no |
| 501 | + /// aarch64 machine or emulator here. CI assembles it |
| 502 | + /// (`--cfg tilrt_aarch64_switch`), and it is used only with that cfg; |
| 503 | + /// by default aarch64 keeps corosensei. Run `fiber::tests` and the |
| 504 | + /// native tests on aarch64 before making it the default. |
| 505 | + #[cfg(target_arch = "aarch64")] |
| 506 | + mod arch { |
| 507 | + use super::Stack; |
| 508 | + |
| 509 | + /// The frame `tl_fiber_switch` saves (22 words, 176 bytes; a |
| 510 | + /// multiple of 16, so `sp` stays aligned). Indices in words. |
| 511 | + const FRAME: usize = 22; |
| 512 | + const X19: usize = 0; // x19–x28 at 0..10, then fp (x29) and lr (x30) |
| 513 | + const X20: usize = 1; |
| 514 | + const LR: usize = 11; // d8–d15 at 12..20, FPCR at 20, padding at 21 |
| 515 | + |
| 516 | + /// Lay out the frame `tl_fiber_switch` pops on the first resume: |
| 517 | + /// x19 = `start`, x20 = `entry`, fp = 0 (ends frame-pointer |
| 518 | + /// backtraces), lr = `tl_fiber_trampoline`, which calls |
| 519 | + /// `entry(start)`; d8–d15 zero, FPCR 0 (Linux's default: round to |
| 520 | + /// nearest, no traps). |
| 521 | + pub(super) fn initial_frame(stack: &Stack, start: *mut u8, entry: usize) -> usize { |
| 522 | + let top = stack.top() & !15; |
| 523 | + let mut words = [0usize; FRAME]; |
| 524 | + words[X19] = start as usize; |
| 525 | + words[X20] = entry; |
| 526 | + words[LR] = tl_fiber_trampoline as *const () as usize; |
| 527 | + let sp = top - FRAME * 8; |
| 528 | + // SAFETY: the words fit in the stack's top 176 bytes. |
| 529 | + unsafe { |
| 530 | + std::ptr::copy_nonoverlapping(words.as_ptr(), sp as *mut usize, FRAME); |
| 531 | + } |
| 532 | + sp |
| 533 | + } |
| 534 | + |
| 535 | + unsafe extern "C" { |
| 536 | + /// Save the current context, storing its stack pointer in |
| 537 | + /// `save`, and continue the one suspended at `to`. |
| 538 | + pub(super) fn tl_fiber_switch(save: *mut usize, to: usize); |
| 539 | + fn tl_fiber_trampoline(); |
| 540 | + } |
| 541 | + |
| 542 | + // AAPCS64: x19–x28, x29 (fp), x30 (lr), the low halves of v8–v15 |
| 543 | + // (d8–d15) and FPCR are callee-saved; everything else may be |
| 544 | + // clobbered by a call, which is what the switch looks like to its |
| 545 | + // callers. FPSR holds status flags only, and is not preserved. |
| 546 | + // `ret` returns to the restored lr: into the other context's call |
| 547 | + // of `tl_fiber_switch`, or, the first time, to the trampoline. |
| 548 | + std::arch::global_asm!( |
| 549 | + ".text", |
| 550 | + ".globl tl_fiber_switch", |
| 551 | + ".type tl_fiber_switch,%function", |
| 552 | + ".p2align 4", |
| 553 | + "tl_fiber_switch:", |
| 554 | + " .cfi_startproc", |
| 555 | + " sub sp, sp, #176", |
| 556 | + " stp x19, x20, [sp, #0]", |
| 557 | + " stp x21, x22, [sp, #16]", |
| 558 | + " stp x23, x24, [sp, #32]", |
| 559 | + " stp x25, x26, [sp, #48]", |
| 560 | + " stp x27, x28, [sp, #64]", |
| 561 | + " stp x29, x30, [sp, #80]", |
| 562 | + " stp d8, d9, [sp, #96]", |
| 563 | + " stp d10, d11, [sp, #112]", |
| 564 | + " stp d12, d13, [sp, #128]", |
| 565 | + " stp d14, d15, [sp, #144]", |
| 566 | + " mrs x9, fpcr", |
| 567 | + " str x9, [sp, #160]", |
| 568 | + " mov x9, sp", |
| 569 | + " str x9, [x0]", |
| 570 | + " mov sp, x1", |
| 571 | + " ldr x9, [sp, #160]", |
| 572 | + " msr fpcr, x9", |
| 573 | + " ldp d14, d15, [sp, #144]", |
| 574 | + " ldp d12, d13, [sp, #128]", |
| 575 | + " ldp d10, d11, [sp, #112]", |
| 576 | + " ldp d8, d9, [sp, #96]", |
| 577 | + " ldp x29, x30, [sp, #80]", |
| 578 | + " ldp x27, x28, [sp, #64]", |
| 579 | + " ldp x25, x26, [sp, #48]", |
| 580 | + " ldp x23, x24, [sp, #32]", |
| 581 | + " ldp x21, x22, [sp, #16]", |
| 582 | + " ldp x19, x20, [sp, #0]", |
| 583 | + " add sp, sp, #176", |
| 584 | + " ret", |
| 585 | + " .cfi_endproc", |
| 586 | + ".size tl_fiber_switch, .-tl_fiber_switch", |
| 587 | + "", |
| 588 | + ".globl tl_fiber_trampoline", |
| 589 | + ".type tl_fiber_trampoline,%function", |
| 590 | + ".p2align 4", |
| 591 | + "tl_fiber_trampoline:", |
| 592 | + " .cfi_startproc", |
| 593 | + " .cfi_undefined x30", |
| 594 | + " mov x0, x19", |
| 595 | + " blr x20", |
| 596 | + " brk #0", |
| 597 | + " .cfi_endproc", |
| 598 | + ".size tl_fiber_trampoline, .-tl_fiber_trampoline", |
| 599 | + ); |
| 600 | + } |
| 463 | 601 | } |
| 464 | 602 | |
| 465 | | −#[cfg(not(all(target_arch = "x86_64", target_os = "linux")))] |
| 603 | +#[cfg(not(any( |
| 604 | + all(target_arch = "x86_64", target_os = "linux"), |
| 605 | + all(target_arch = "aarch64", target_os = "linux", tilrt_aarch64_switch) |
| 606 | +)))] |
| 466 | 607 | mod imp { |
| 467 | 608 | //! corosensei on the runtime's stacks. |
| 468 | 609 | use super::{CoroutineResult, Snapshot, Stack}; |