From 8bb35195765838694a9f6d732625675feed1f243 Mon Sep 17 00:00:00 2001 From: Kurtis Charnock Date: Tue, 18 Mar 2025 10:49:05 +0000 Subject: [PATCH 1/3] RISCV: Add macros to wrap a single-argument system call Adds macros that can wrap a single-argument system call. Adds a conversion from Infallible for Error. --- src/riscv/Cargo.lock | 7 +++++++ src/riscv/Cargo.toml | 1 + src/riscv/lib/Cargo.toml | 1 + src/riscv/lib/src/pvm/linux.rs | 16 ++++++++++++++++ src/riscv/lib/src/pvm/linux/error.rs | 7 +++++++ src/rust_deps/Cargo.lock | 7 +++++++ 6 files changed, 39 insertions(+) diff --git a/src/riscv/Cargo.lock b/src/riscv/Cargo.lock index 3a86316eaf4b..87d8c4c66b6e 100644 --- a/src/riscv/Cargo.lock +++ b/src/riscv/Cargo.lock @@ -1574,6 +1574,7 @@ dependencies = [ "tezos-smart-rollup-utils", "tezos_crypto_rs", "thiserror", + "try-blocks", "vm-fdt", ] @@ -2731,6 +2732,12 @@ dependencies = [ "winnow", ] +[[package]] +name = "try-blocks" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "296cc7892cc05ae83e113b20c113d3fd9020eac7abbbaaeaf69c424ef872be7a" + [[package]] name = "typenum" version = "1.17.0" diff --git a/src/riscv/Cargo.toml b/src/riscv/Cargo.toml index db90df4293bf..a239a7fc56ac 100644 --- a/src/riscv/Cargo.toml +++ b/src/riscv/Cargo.toml @@ -45,6 +45,7 @@ tezos-smart-rollup-constants.path = "../kernel_sdk/constants" tezos-smart-rollup-encoding.path = "../kernel_sdk/encoding" tezos-smart-rollup-utils.path = "../kernel_sdk/utils" thiserror = "1.0.57" +try-blocks = "0.1.4" vm-fdt = "0.3.0" goldenfile = "1.7.1" arbitrary-int = "1.2.7" diff --git a/src/riscv/lib/Cargo.toml b/src/riscv/lib/Cargo.toml index 9e1bf0e569c4..0023ab3809b4 100644 --- a/src/riscv/lib/Cargo.toml +++ b/src/riscv/lib/Cargo.toml @@ -30,6 +30,7 @@ tezos_crypto_rs.workspace = true tezos-smart-rollup-constants.workspace = true tezos-smart-rollup-utils.workspace = true thiserror.workspace = true +try-blocks.workspace = true vm-fdt.workspace = true itertools.workspace = true range-collections.workspace = true diff --git a/src/riscv/lib/src/pvm/linux.rs b/src/riscv/lib/src/pvm/linux.rs index 65242aff01d5..7c350f45552a 100644 --- a/src/riscv/lib/src/pvm/linux.rs +++ b/src/riscv/lib/src/pvm/linux.rs @@ -465,6 +465,22 @@ impl SupervisorState { MC: MemoryConfig, M: ManagerReadWrite, { + // `dispatch1!(system_call_no)` + // Converts the system call name to the handler + #[allow(unused_macros)] + macro_rules! dispatch1 { + ($system_call:ty) => {{ + try_blocks::try_block! { + paste::paste! { + let arg1 = core.hart.xregisters.try_read(registers::a0)?; + let result = self.[](arg1)?; + core.hart.xregisters.write(registers::a0, result.into()); + true + } + } + }}; + } + // We need to jump to the next instruction. The ECall instruction which triggered this // function is 4 byte wide. let pc = core.hart.pc.read().saturating_add(4); diff --git a/src/riscv/lib/src/pvm/linux/error.rs b/src/riscv/lib/src/pvm/linux/error.rs index d206043616d5..006b347d0c72 100644 --- a/src/riscv/lib/src/pvm/linux/error.rs +++ b/src/riscv/lib/src/pvm/linux/error.rs @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: MIT +use std::convert::Infallible; use std::num::TryFromIntError; use arbitrary_int::TryNewError; @@ -67,6 +68,12 @@ impl Error { } } +impl From for Error { + fn from(infallible: Infallible) -> Self { + match infallible {} + } +} + impl From for Error { fn from(_: OutOfBounds) -> Self { Self::Fault diff --git a/src/rust_deps/Cargo.lock b/src/rust_deps/Cargo.lock index 3102531d12e7..bee2fff6bb4c 100644 --- a/src/rust_deps/Cargo.lock +++ b/src/rust_deps/Cargo.lock @@ -2588,6 +2588,7 @@ dependencies = [ "tezos-smart-rollup-utils", "tezos_crypto_rs", "thiserror", + "try-blocks", "vm-fdt", ] @@ -4149,6 +4150,12 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "try-blocks" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "296cc7892cc05ae83e113b20c113d3fd9020eac7abbbaaeaf69c424ef872be7a" + [[package]] name = "try-lock" version = "0.2.5" -- GitLab From 1d83b96d2470b646c539e8fb0b26b9b9a59ce9d9 Mon Sep 17 00:00:00 2001 From: Kurtis Charnock Date: Tue, 18 Mar 2025 15:50:00 +0000 Subject: [PATCH 2/3] RISCV: Use dispatch1/wrap1 macros with SET_TID_ADDRESS Changes the SET_TID_ADDRESS handler to return the value of the call and ignore the registers. Wraps the SET_TID_ADDRESS handler with the macros that handle reading from and writing to the a0 register. --- src/riscv/lib/src/pvm/linux.rs | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) diff --git a/src/riscv/lib/src/pvm/linux.rs b/src/riscv/lib/src/pvm/linux.rs index 7c350f45552a..2f33fafada03 100644 --- a/src/riscv/lib/src/pvm/linux.rs +++ b/src/riscv/lib/src/pvm/linux.rs @@ -467,7 +467,6 @@ impl SupervisorState { { // `dispatch1!(system_call_no)` // Converts the system call name to the handler - #[allow(unused_macros)] macro_rules! dispatch1 { ($system_call:ty) => {{ try_blocks::try_block! { @@ -497,7 +496,7 @@ impl SupervisorState { PPOLL => self.handle_ppoll(core), READLINKAT => self.handle_readlinkat(), EXIT | EXITGROUP => self.handle_exit(core), - SET_TID_ADDRESS => self.handle_set_tid_address(core), + SET_TID_ADDRESS => dispatch1!(set_tid_address), TKILL => self.handle_tkill(core), SIGALTSTACK => self.handle_sigaltstack(core), RT_SIGACTION => self.handle_rt_sigaction(core), @@ -543,10 +542,7 @@ impl SupervisorState { /// Handle `set_tid_address` system call. /// /// See: - fn handle_set_tid_address( - &mut self, - core: &mut MachineCoreState, - ) -> Result + fn handle_set_tid_address(&mut self, tid_address: VirtAddr) -> Result where M: ManagerRead + ManagerWrite, { @@ -555,14 +551,9 @@ impl SupervisorState { // In the future, when we add threading, this system call needs to be implemented to // support informing other (waiting) threads of termination. - // The address is passed as the first and only parameter - let tid_address = core.hart.xregisters.read(registers::a0).into(); self.tid_address.write(tid_address); - // The caller expects the Thread ID to be returned - core.hart.xregisters.write(registers::a0, MAIN_THREAD_ID); - - Ok(true) + Ok(MAIN_THREAD_ID) } fn handle_exit( -- GitLab From d3cba73a5e8ff54b9509ae9372b811803cef236a Mon Sep 17 00:00:00 2001 From: Kurtis Charnock Date: Fri, 21 Mar 2025 14:39:32 +0000 Subject: [PATCH 3/3] RISC-V: Add try-blocks dependency to lib_wasm_runtime --- etherlink/lib_wasm_runtime/Cargo.lock | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/etherlink/lib_wasm_runtime/Cargo.lock b/etherlink/lib_wasm_runtime/Cargo.lock index 65c99b41372e..800443e8abd0 100644 --- a/etherlink/lib_wasm_runtime/Cargo.lock +++ b/etherlink/lib_wasm_runtime/Cargo.lock @@ -3156,6 +3156,7 @@ dependencies = [ "tezos-smart-rollup-utils", "tezos_crypto_rs", "thiserror", + "try-blocks", "vm-fdt", ] @@ -5166,6 +5167,12 @@ dependencies = [ "rlp", ] +[[package]] +name = "try-blocks" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "296cc7892cc05ae83e113b20c113d3fd9020eac7abbbaaeaf69c424ef872be7a" + [[package]] name = "try-lock" version = "0.2.5" -- GitLab