From 8c1722d583a3985503044fe000e769960c1426bd Mon Sep 17 00:00:00 2001 From: Anhad Singh Date: Wed, 11 Dec 2024 16:51:51 +1100 Subject: [PATCH 1/4] fix(ld.so): redox Signed-off-by: Anhad Singh --- src/ld_so/linker.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index ace6508746..07a38d71d5 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -191,10 +191,60 @@ impl Linker { unsafe { if !dlopened { + #[cfg(target_os = "redox")] + let tcb = { + use super::tcb::OsSpecific; + use redox_rt::signal::tmp_disable_signals; + + let old_tcb = Tcb::current().expect("failed to get bootstrap TCB"); + let new_tcb = Tcb::new(self.tls_size)?; // This actually allocates TCB, TLS and ABI page. + + // Stash + let new_tls_end = new_tcb.generic.tls_end; + let new_tls_len = new_tcb.generic.tls_len; + let new_tcb_ptr = new_tcb.generic.tcb_ptr; + let new_tcb_len = new_tcb.generic.tcb_len; + + // Unmap just the TCB page. + Sys::munmap(new_tcb as *mut Tcb as *mut c_void, syscall::PAGE_SIZE).unwrap(); + + let new_addr = ptr::addr_of!(*new_tcb) as usize; + + assert_eq!( + syscall::syscall5( + syscall::SYS_MREMAP, + old_tcb as *mut Tcb as usize, + syscall::PAGE_SIZE, + new_addr, + syscall::PAGE_SIZE, + (syscall::MremapFlags::FIXED | syscall::MremapFlags::KEEP_OLD).bits() + | (syscall::MapFlags::PROT_READ | syscall::MapFlags::PROT_WRITE) + .bits(), + ) + .expect("mremap: failed to alias TCB"), + new_addr, + ); + + // New TCB is now at the same physical address as the old TCB. + drop(old_tcb); + + let _guard = tmp_disable_signals(); + // Restore + new_tcb.generic.tls_end = new_tls_end; + new_tcb.generic.tls_len = new_tls_len; + new_tcb.generic.tcb_ptr = new_tcb_ptr; + new_tcb.generic.tcb_len = new_tcb_len; + + drop(_guard); + new_tcb + }; + + #[cfg(not(target_os = "redox"))] + let tcb = Tcb::new(self.tls_size)?; + // We are now loading the main program or its dependencies. The TLS for all initially // loaded objects reside in the static TLS block. Depending on the architecture, the // static TLS block is either placed before the TP or after the TP. - let tcb = Tcb::new(self.tls_size)?; let tcb_ptr = tcb as *mut Tcb; // Setup the DTVs. From 20b9af633d7b31991635a8b47bbf4e9b51a4fde4 Mon Sep 17 00:00:00 2001 From: Anhad Singh Date: Thu, 12 Dec 2024 13:32:25 +1100 Subject: [PATCH 2/4] chore: bump redox_syscall Bumps redox_syscall to 0.5.8 Signed-off-by: Anhad Singh --- Cargo.lock | 4 ++-- Cargo.toml | 2 +- redox-rt/Cargo.toml | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 09b816edf6..ab80fb44fd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -343,9 +343,9 @@ dependencies = [ [[package]] name = "redox_syscall" -version = "0.5.7" +version = "0.5.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9b6dfecf2c74bce2466cabf93f6664d6998a69eb21e39f4207930065b27b771f" +checksum = "03a862b389f93e68874fbf580b9de08dd02facb9a788ebadaf4a3fd33cf58834" dependencies = [ "bitflags", ] diff --git a/Cargo.toml b/Cargo.toml index 78cf6445fa..f7ff066279 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,7 +59,7 @@ features = ["c_api"] sc = "0.2.3" [target.'cfg(target_os = "redox")'.dependencies] -redox_syscall = "0.5.5" +redox_syscall = "0.5.8" redox-rt = { path = "redox-rt" } redox-path = "0.2" redox_event = { git = "https://gitlab.redox-os.org/redox-os/event.git", default-features = false, features = ["redox_syscall"] } diff --git a/redox-rt/Cargo.toml b/redox-rt/Cargo.toml index 33502bb75f..a69851747e 100644 --- a/redox-rt/Cargo.toml +++ b/redox-rt/Cargo.toml @@ -12,6 +12,6 @@ description = "Libc-independent runtime for Redox" bitflags = "2" goblin = { version = "0.7", default-features = false, features = ["elf32", "elf64", "endian_fd"] } plain = "0.2" -redox_syscall = "0.5.3" +redox_syscall = "0.5.8" generic-rt = { path = "../generic-rt" } From d3bab6eeeeab58557c0cac640aa1114da5d70830 Mon Sep 17 00:00:00 2001 From: Anhad Singh Date: Thu, 12 Dec 2024 15:56:37 +1100 Subject: [PATCH 3/4] fix(redox: ld.so): unmap old TCB Signed-off-by: Anhad Singh --- src/ld_so/linker.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index 07a38d71d5..10330c44a5 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -192,7 +192,7 @@ impl Linker { unsafe { if !dlopened { #[cfg(target_os = "redox")] - let tcb = { + let (tcb, old_tcb) = { use super::tcb::OsSpecific; use redox_rt::signal::tmp_disable_signals; @@ -224,9 +224,7 @@ impl Linker { .expect("mremap: failed to alias TCB"), new_addr, ); - - // New TCB is now at the same physical address as the old TCB. - drop(old_tcb); + // XXX: New TCB is now at the same physical address as the old TCB. let _guard = tmp_disable_signals(); // Restore @@ -236,7 +234,7 @@ impl Linker { new_tcb.generic.tcb_len = new_tcb_len; drop(_guard); - new_tcb + (new_tcb, old_tcb as *mut Tcb as *mut c_void) }; #[cfg(not(target_os = "redox"))] @@ -277,6 +275,12 @@ impl Linker { // Copy the master data into the static TLS block. tcb.copy_masters()?; tcb.activate(); + + #[cfg(target_os = "redox")] + { + // Unmap the old TCB. + Sys::munmap(old_tcb, syscall::PAGE_SIZE).unwrap(); + } } else { let tcb = Tcb::current().expect("failed to get current tcb"); From 9928091aada940961e1719dcbb494f196bb4445e Mon Sep 17 00:00:00 2001 From: Anhad Singh Date: Thu, 12 Dec 2024 16:11:42 +1100 Subject: [PATCH 4/4] fix(ci) Signed-off-by: Anhad Singh --- src/ld_so/linker.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ld_so/linker.rs b/src/ld_so/linker.rs index 10330c44a5..5e95514da1 100644 --- a/src/ld_so/linker.rs +++ b/src/ld_so/linker.rs @@ -193,7 +193,6 @@ impl Linker { if !dlopened { #[cfg(target_os = "redox")] let (tcb, old_tcb) = { - use super::tcb::OsSpecific; use redox_rt::signal::tmp_disable_signals; let old_tcb = Tcb::current().expect("failed to get bootstrap TCB");