diff --git a/riscv-semihosting/CHANGELOG.md b/riscv-semihosting/CHANGELOG.md index c9d61fe6..80e008a3 100644 --- a/riscv-semihosting/CHANGELOG.md +++ b/riscv-semihosting/CHANGELOG.md @@ -7,6 +7,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Changed +- Apply clippy changes - Bump riscv dependency version - Made `cfg` variable selection more robust for custom targets - Fixed debug::exit() on riscv64 QEMU simulation diff --git a/riscv-semihosting/README.md b/riscv-semihosting/README.md index 0768aeba..56e89eb6 100644 --- a/riscv-semihosting/README.md +++ b/riscv-semihosting/README.md @@ -17,7 +17,7 @@ Given this, the generally sufficient for using this library. A major difference between this library and `cortex-m-semihosting` is that there -are mandatory features to choose the privilege level at which the semihosting +are features to choose the privilege level at which the semihosting calls are executed. The *machine-mode (M-mode)* feature will cause the macros in `export` to execute the semihosting operation in an interrupt-free context, while *user-mode (U-mode)* causes them to just execute the operation. diff --git a/riscv-semihosting/src/export.rs b/riscv-semihosting/src/export.rs index c56d6d48..3aefa178 100644 --- a/riscv-semihosting/src/export.rs +++ b/riscv-semihosting/src/export.rs @@ -1,7 +1,9 @@ //! IMPLEMENTATION DETAILS USED BY MACROS - use crate::hio::{self, HostStream}; -use core::fmt::{self, Write}; +use core::{ + fmt::{self, Write}, + ptr::addr_of_mut, +}; static mut HSTDOUT: Option = None; static mut HSTDERR: Option = None; @@ -11,42 +13,46 @@ mod machine { use super::*; pub fn hstdout_str(s: &str) { - let _result = critical_section::with(|_| unsafe { - if HSTDOUT.is_none() { - HSTDOUT = Some(hio::hstdout()?); + let _result = critical_section::with(|_| { + let hstdout = unsafe { &mut *addr_of_mut!(HSTDOUT) }; + if hstdout.is_none() { + *hstdout = Some(hio::hstdout()?); } - HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop) + hstdout.as_mut().unwrap().write_str(s).map_err(drop) }); } pub fn hstdout_fmt(args: fmt::Arguments) { - let _result = critical_section::with(|_| unsafe { - if HSTDOUT.is_none() { - HSTDOUT = Some(hio::hstdout()?); + let _result = critical_section::with(|_| { + let hstdout = unsafe { &mut *addr_of_mut!(HSTDOUT) }; + if hstdout.is_none() { + *hstdout = Some(hio::hstdout()?); } - HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop) + hstdout.as_mut().unwrap().write_fmt(args).map_err(drop) }); } pub fn hstderr_str(s: &str) { - let _result = critical_section::with(|_| unsafe { - if HSTDERR.is_none() { - HSTDERR = Some(hio::hstderr()?); + let _result = critical_section::with(|_| { + let hstderr = unsafe { &mut *addr_of_mut!(HSTDERR) }; + if hstderr.is_none() { + *hstderr = Some(hio::hstderr()?); } - HSTDERR.as_mut().unwrap().write_str(s).map_err(drop) + hstderr.as_mut().unwrap().write_str(s).map_err(drop) }); } pub fn hstderr_fmt(args: fmt::Arguments) { - let _result = critical_section::with(|_| unsafe { - if HSTDERR.is_none() { - HSTDERR = Some(hio::hstderr()?); + let _result = critical_section::with(|_| { + let hstderr = unsafe { &mut *addr_of_mut!(HSTDERR) }; + if hstderr.is_none() { + *hstderr = Some(hio::hstderr()?); } - HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop) + hstderr.as_mut().unwrap().write_fmt(args).map_err(drop) }); } } @@ -59,41 +65,45 @@ mod user { pub fn hstdout_str(s: &str) { let _result = unsafe { - if HSTDOUT.is_none() { - HSTDOUT = Some(hio::hstdout().unwrap()); + let hstdout = &mut *addr_of_mut!(HSTDOUT); + if hstdout.is_none() { + *hstdout = Some(hio::hstdout().unwrap()); } - HSTDOUT.as_mut().unwrap().write_str(s).map_err(drop) + hstdout.as_mut().unwrap().write_str(s).map_err(drop) }; } pub fn hstdout_fmt(args: fmt::Arguments) { let _result = unsafe { - if HSTDOUT.is_none() { - HSTDOUT = Some(hio::hstdout().unwrap()); + let hstdout = &mut *addr_of_mut!(HSTDOUT); + if hstdout.is_none() { + *hstdout = Some(hio::hstdout().unwrap()); } - HSTDOUT.as_mut().unwrap().write_fmt(args).map_err(drop) + hstdout.as_mut().unwrap().write_fmt(args).map_err(drop) }; } pub fn hstderr_str(s: &str) { let _result = unsafe { - if HSTDERR.is_none() { - HSTDERR = Some(hio::hstderr().unwrap()); + let hstderr = &mut *addr_of_mut!(HSTDERR); + if hstderr.is_none() { + *hstderr = Some(hio::hstderr().unwrap()); } - HSTDERR.as_mut().unwrap().write_str(s).map_err(drop) + hstderr.as_mut().unwrap().write_str(s).map_err(drop) }; } pub fn hstderr_fmt(args: fmt::Arguments) { let _result = unsafe { - if HSTDERR.is_none() { - HSTDERR = Some(hio::hstderr().unwrap()); + let hstderr = &mut *addr_of_mut!(HSTDERR); + if hstderr.is_none() { + *hstderr = Some(hio::hstderr().unwrap()); } - HSTDERR.as_mut().unwrap().write_fmt(args).map_err(drop) + hstderr.as_mut().unwrap().write_fmt(args).map_err(drop) }; } }