Skip to content

fixes #31. Migrate to naked_asm inside naked functions #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 12 additions & 5 deletions ch05/c-fibers/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/// FIX #31:
/// Inline assembly blocks inside naked functions now need to use
/// the `naked_asm` macro instead of the good old `asm` macro.
/// The `noreturn` option is implicitly set by the `naked_asm`
/// macro so there is no need to set that.
///
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
/// for more information.
#![feature(naked_functions)]
use std::arch::asm;
use std::arch::{asm, naked_asm};

const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
const MAX_THREADS: usize = 4;
Expand Down Expand Up @@ -141,7 +149,7 @@ fn guard() {

#[naked]
unsafe extern "C" fn skip() {
asm!("ret", options(noreturn))
naked_asm!("ret")
}

pub fn yield_thread() {
Expand All @@ -155,7 +163,7 @@ pub fn yield_thread() {
#[no_mangle]
#[cfg_attr(target_os = "macos", export_name = "\x01switch")] // see: How-to-MacOS-M.md for explanation
unsafe extern "C" fn switch() {
asm!(
naked_asm!(
"mov [rdi + 0x00], rsp",
"mov [rdi + 0x08], r15",
"mov [rdi + 0x10], r14",
Expand All @@ -170,8 +178,7 @@ unsafe extern "C" fn switch() {
"mov r12, [rsi + 0x20]",
"mov rbx, [rsi + 0x28]",
"mov rbp, [rsi + 0x30]",
"ret",
options(noreturn)
"ret"
);
}

Expand Down
21 changes: 14 additions & 7 deletions ch05/d-fibers-closure/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/// FIX #31:
/// Inline assembly blocks inside naked functions now need to use
/// the `naked_asm` macro instead of the good old `asm` macro.
/// The `noreturn` option is implicitly set by the `naked_asm`
/// macro so there is no need to set that.
///
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
/// for more information.
#![feature(naked_functions)]
use std::{arch::asm, ptr};
use std::{arch::{asm, naked_asm}};

const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
const MAX_THREADS: usize = 4;
Expand Down Expand Up @@ -152,8 +160,8 @@ fn call(thread: u64) {
}

#[naked]
unsafe fn skip() {
asm!("ret", options(noreturn))
unsafe extern "C" fn skip() {
naked_asm!("ret")
}

// this function is changed
Expand All @@ -175,8 +183,8 @@ pub fn yield_thread() {
#[naked]
#[no_mangle]
#[cfg_attr(target_os = "macos", export_name = "\x01switch")]
unsafe fn switch() {
asm!(
unsafe extern "C" fn switch() {
naked_asm!(
"mov 0x00[rdi], rsp",
"mov 0x08[rdi], r15",
"mov 0x10[rdi], r14",
Expand All @@ -192,8 +200,7 @@ unsafe fn switch() {
"mov rbx, 0x28[rsi]",
"mov rbp, 0x30[rsi]",
"mov rdi, 0x38[rsi]",
"ret",
options(noreturn)
"ret"
);
}

Expand Down
4 changes: 2 additions & 2 deletions ch05/e-fibers-windows/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Last we need to change our `switch()`function and update our assembly. After all
#[naked]
#[no_mangle]
unsafe extern "C" fn switch() {
asm!(
naked_asm!(
"movaps [rcx + 0x00], xmm6",
"movaps [rcx + 0x10], xmm7",
"movaps [rcx + 0x20], xmm8",
Expand Down Expand Up @@ -295,7 +295,7 @@ unsafe extern "C" fn switch() {
"mov gs:0x08, rax",
"mov rax, [rdx + 0xf0]",
"mov gs:0x10, rax",
"ret", options(noreturn)
"ret"
);
}
```
Expand Down
16 changes: 12 additions & 4 deletions ch05/e-fibers-windows/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/// FIX #31:
/// Inline assembly blocks inside naked functions now need to use
/// the `naked_asm` macro instead of the good old `asm` macro.
/// The `noreturn` option is implicitly set by the `naked_asm`
/// macro so there is no need to set that.
///
/// See: https://github.com/PacktPublishing/Asynchronous-Programming-in-Rust/issues/31
/// for more information.
#![feature(naked_functions)]
use std::arch::asm;
use std::arch::{asm, naked_asm};

const DEFAULT_STACK_SIZE: usize = 1024 * 1024 * 2;
const MAX_THREADS: usize = 4;
Expand Down Expand Up @@ -145,7 +153,7 @@ impl Runtime {

#[naked]
unsafe extern "C" fn skip() {
asm!("ret", options(noreturn))
naked_asm!("ret")
}


Expand All @@ -168,7 +176,7 @@ pub fn yield_thread() {
#[no_mangle]
#[cfg_attr(target_os = "macos", export_name = "\x01switch")]
unsafe extern "C" fn switch() {
asm!(
naked_asm!(
"mov [rdi + 0x00], rsp",
"mov [rdi + 0x08], r15",
"mov [rdi + 0x10], r14",
Expand All @@ -183,7 +191,7 @@ unsafe extern "C" fn switch() {
"mov r12, [rsi + 0x20]",
"mov rbx, [rsi + 0x28]",
"mov rbp, [rsi + 0x30]",
"ret", options(noreturn)
"ret"
);
}

Expand Down