Skip to content

Commit 4b7d9a8

Browse files
committed
x86, riscv64: mark constructors as unsafe
1 parent c2ea2b4 commit 4b7d9a8

File tree

6 files changed

+15
-10
lines changed

6 files changed

+15
-10
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

1212
- Added a changelog.
1313
- Added support for 32-bit ARM architectures ([#33]).
14+
- Changed `X86::new` and `RISCV64::new` to unsafe.
1415

1516
[#33]: https://github.com/rust-embedded/qemu-exit/pull/33
1617

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ let qemu_exit_handle = qemu_exit::AArch64::new();
2121

2222
// addr: The address of sifive_test.
2323
#[cfg(target_arch = "riscv64")]
24-
let qemu_exit_handle = qemu_exit::RISCV64::new(addr);
24+
let qemu_exit_handle = unsafe { qemu_exit::RISCV64::new(addr) };
2525

2626
// io_base: I/O-base of isa-debug-exit.
2727
// custom_exit_success: A custom success code; Must be an odd number.
2828
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
29-
let qemu_exit_handle = qemu_exit::X86::new(io_base, custom_exit_success);
29+
let qemu_exit_handle = unsafe { qemu_exit::X86::new(io_base, custom_exit_success) };
3030

3131
qemu_exit_handle.exit(1337);
3232
qemu_exit_handle.exit_success();
@@ -38,6 +38,7 @@ qemu_exit_handle.exit_failure();
3838
### AArch64/AArch32
3939

4040
Pass the `-semihosting` argument to the QEMU invocation, e.g.:
41+
4142
```
4243
qemu-system-aarch64 -M raspi3 -serial stdio -semihosting -kernel kernel8.img
4344
qemu-system-arm -nographic -M mps2-an500 -cpu cortex-m7 -serial mon:stdio -semihosting -kernel
@@ -47,13 +48,15 @@ kernel.img
4748
### RISCV64
4849

4950
You need to chose a machine with the `sifive_test` device, for exemple `-M virt`:
51+
5052
```
5153
qemu-system-riscv64 -M virt -nographic -monitor none -serial stdio -kernel kernel.elf
5254
```
5355

5456
### x86/x86_64
5557

5658
Add the special ISA debug exit device by passing the flags:
59+
5760
```
5861
-device isa-debug-exit,iobase=0xf4,iosize=0x04
5962
```
@@ -67,14 +70,15 @@ binary-OR'ed with `0x1`. This is hardcoded and therefore, with `isa-debug-exit`,
6770
possible to let QEMU invoke `exit(0)`.
6871

6972
```rust
70-
let qemu_exit_handle = qemu_exit::X86::new(io_base, custom_exit_success);
73+
let qemu_exit_handle = unsafe { qemu_exit::X86::new(io_base, custom_exit_success) };
7174
```
7275

7376
#### x86/x86_64 Linux
7477

7578
To use this mechanism from Linux userspace, the kernel must be compiled with
7679
`CONFIG_X86_IOPL_IOPERM=y` (which is the default) and the process must start with root privileges
7780
(or `CAP_SYS_RAWIO`) and call: [`ioperm(2)`](https://man7.org/linux/man-pages/man2/ioperm.2.html):
81+
7882
```rust
7983
nix::errno::Errno::result(unsafe { libc::ioperm( 0xf4, 4, 1 )}).expect("ioperm failed");
8084
```

src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
//!
2121
//! // addr: The address of sifive_test.
2222
//! #[cfg(target_arch = "riscv64")]
23-
//! let qemu_exit_handle = qemu_exit::RISCV64::new(addr);
23+
//! let qemu_exit_handle = unsafe { qemu_exit::RISCV64::new(addr) };
2424
//!
2525
//! // io_base: I/O-base of isa-debug-exit.
2626
//! // custom_exit_success: A custom success code; Must be an odd number.
2727
//! #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
28-
//! let qemu_exit_handle = qemu_exit::X86::new(io_base, custom_exit_success);
28+
//! let qemu_exit_handle = unsafe { qemu_exit::X86::new(io_base, custom_exit_success) };
2929
//!
3030
//! qemu_exit_handle.exit(1337);
3131
//! qemu_exit_handle.exit_success();
@@ -74,7 +74,7 @@
7474
//! possible to let QEMU invoke `exit(0)`.
7575
//!
7676
//! ```ignore
77-
//! let qemu_exit_handle = qemu_exit::X86::new(io_base, custom_exit_success);
77+
//! let qemu_exit_handle = unsafe { qemu_exit::X86::new(io_base, custom_exit_success) };
7878
//! ```
7979
//!
8080
//! ## Literature

src/riscv64.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ const fn exit_code_encode(code: u32) -> u32 {
2626

2727
impl RISCV64 {
2828
/// Create an instance.
29-
pub const fn new(addr: u64) -> Self {
29+
pub const unsafe fn new(addr: u64) -> Self {
3030
RISCV64 { addr }
3131
}
3232
}

src/x86.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ fn outl(io_base: u16, code: u32) {
3333

3434
impl X86 {
3535
/// Create an instance.
36-
pub const fn new(io_base: u16, custom_exit_success: u32) -> Self {
36+
pub const unsafe fn new(io_base: u16, custom_exit_success: u32) -> Self {
3737
assert!((custom_exit_success & 1) == 1);
3838

3939
X86 {

tests/exit_13.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ mod armv7m_mps2an500;
3535
//--------------------------------------------------------------------------------------------------
3636

3737
#[cfg(target_arch = "riscv64")]
38-
const QEMU_EXIT_HANDLE: qemu_exit::RISCV64 = qemu_exit::RISCV64::new(0x100000);
38+
const QEMU_EXIT_HANDLE: qemu_exit::RISCV64 = unsafe { qemu_exit::RISCV64::new(0x100000) };
3939

4040
#[cfg(target_arch = "riscv64")]
4141
mod riscv64_virt;
@@ -45,7 +45,7 @@ mod riscv64_virt;
4545
//--------------------------------------------------------------------------------------------------
4646

4747
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
48-
const QEMU_EXIT_HANDLE: qemu_exit::X86 = qemu_exit::X86::new(0xf4, 5);
48+
const QEMU_EXIT_HANDLE: qemu_exit::X86 = unsafe { qemu_exit::X86::new(0xf4, 5) };
4949

5050
//--------------------------------------------------------------------------------------------------
5151
// Generic code

0 commit comments

Comments
 (0)