Skip to content

Commit dcbcbef

Browse files
committed
Fix cortex-m-rt qemu test by removing 'nomem' from semihosting_syscall asm, add inline to most cortex_m::asm methods
1 parent f2feeb2 commit dcbcbef

File tree

6 files changed

+35
-48
lines changed

6 files changed

+35
-48
lines changed

cortex-m-rt/examples/qemu.rs

+13-17
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
1-
// #![feature(stdsimd)]
21
#![no_main]
32
#![no_std]
43

5-
extern crate cortex_m;
6-
extern crate cortex_m_rt as rt;
7-
extern crate cortex_m_semihosting as semihosting;
4+
use core::fmt::Write;
85

9-
extern crate panic_halt;
10-
11-
use cortex_m::asm;
12-
use rt::entry;
13-
14-
#[entry]
6+
#[cortex_m_rt::entry]
157
fn main() -> ! {
16-
use core::fmt::Write;
178
let x = 42;
189

1910
loop {
20-
asm::nop();
21-
22-
// write something through semihosting interface
23-
let mut hstdout = semihosting::hio::hstdout().unwrap();
11+
let mut hstdout = cortex_m_semihosting::hio::hstdout().unwrap();
2412
write!(hstdout, "x = {}\n", x).unwrap();
25-
// exit from qemu
26-
semihosting::debug::exit(semihosting::debug::EXIT_SUCCESS);
13+
cortex_m_semihosting::debug::exit(cortex_m_semihosting::debug::EXIT_SUCCESS);
14+
}
15+
}
16+
17+
// Define a panic handler that uses semihosting to exit immediately,
18+
// so that any panics cause qemu to quit instead of hang.
19+
#[panic_handler]
20+
fn panic(_: &core::panic::PanicInfo) -> ! {
21+
loop {
22+
cortex_m_semihosting::debug::exit(cortex_m_semihosting::debug::EXIT_FAILURE);
2723
}
2824
}

cortex-m-rt/link.x.in

+2
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ SECTIONS
6666
/* ### Vector table */
6767
.vector_table ORIGIN(FLASH) :
6868
{
69+
__vector_table = .;
70+
6971
/* Initial Stack Pointer (SP) value */
7072
LONG(_stack_start);
7173

cortex-m-rt/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,9 @@
439439

440440
extern crate cortex_m_rt_macros as macros;
441441

442-
use core::fmt;
443442
#[cfg(cortex_m)]
444443
use core::arch::global_asm;
444+
use core::fmt;
445445

446446
// HardFault exceptions are bounced through this trampoline which grabs the stack pointer at
447447
// the time of the exception and passes it to th euser's HardFault handler in r0.

cortex-m-semihosting/src/lib.rs

+3-14
Original file line numberDiff line numberDiff line change
@@ -193,21 +193,10 @@ pub unsafe fn syscall<T>(nr: usize, arg: &T) -> usize {
193193
#[inline(always)]
194194
pub unsafe fn syscall1(_nr: usize, _arg: usize) -> usize {
195195
match () {
196-
#[cfg(all(thumb, not(feature = "no-semihosting")))]
197-
() => {
198-
let mut nr = _nr;
199-
core::arch::asm!(
200-
"bkpt #0xab",
201-
inout("r0") nr,
202-
in("r1") _arg,
203-
options(nomem, nostack, preserves_flags)
204-
);
205-
nr
206-
}
207-
208-
#[cfg(all(thumb, feature = "no-semihosting"))]
196+
#[cfg(all(thumb, not(feature="no-semihosting")))]
197+
() => cortex_m::asm::semihosting_syscall(_nr as u32, _arg as u32) as usize,
198+
#[cfg(all(thumb, feature="no-semihosting"))]
209199
() => 0,
210-
211200
#[cfg(not(thumb))]
212201
() => unimplemented!(),
213202
}

src/asm.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub fn delay(cycles: u32) {
4444
}
4545

4646
/// A no-operation. Useful to prevent delay loops from being optimized away.
47-
#[inline]
47+
#[inline(always)]
4848
pub fn nop() {
4949
// NOTE: This is a `pure` asm block, but applying that option allows the compiler to eliminate
5050
// the nop entirely (or to collapse multiple subsequent ones). Since the user probably wants N
@@ -59,28 +59,28 @@ pub fn nop() {
5959
///
6060
/// Can be used as a stable alternative to `core::intrinsics::abort`.
6161
#[cfg(cortex_m)]
62-
#[inline]
62+
#[inline(always)]
6363
pub fn udf() -> ! {
6464
unsafe { asm!("udf #0", options(noreturn, nomem, nostack, preserves_flags)) };
6565
}
6666

6767
/// Wait For Event
6868
#[cfg(cortex_m)]
69-
#[inline]
69+
#[inline(always)]
7070
pub fn wfe() {
7171
unsafe { asm!("wfe", options(nomem, nostack, preserves_flags)) };
7272
}
7373

7474
/// Wait For Interrupt
7575
#[cfg(cortex_m)]
76-
#[inline]
76+
#[inline(always)]
7777
pub fn wfi() {
7878
unsafe { asm!("wfi", options(nomem, nostack, preserves_flags)) };
7979
}
8080

8181
/// Send Event
8282
#[cfg(cortex_m)]
83-
#[inline]
83+
#[inline(always)]
8484
pub fn sev() {
8585
unsafe { asm!("sev", options(nomem, nostack, preserves_flags)) };
8686
}
@@ -89,7 +89,7 @@ pub fn sev() {
8989
///
9090
/// Flushes the pipeline in the processor, so that all instructions following the `ISB` are fetched
9191
/// from cache or memory, after the instruction has been completed.
92-
#[inline]
92+
#[inline(always)]
9393
pub fn isb() {
9494
compiler_fence(Ordering::SeqCst);
9595
#[cfg(cortex_m)]
@@ -106,7 +106,7 @@ pub fn isb() {
106106
///
107107
/// * any explicit memory access made before this instruction is complete
108108
/// * all cache and branch predictor maintenance operations before this instruction complete
109-
#[inline]
109+
#[inline(always)]
110110
pub fn dsb() {
111111
compiler_fence(Ordering::SeqCst);
112112
#[cfg(cortex_m)]
@@ -121,7 +121,7 @@ pub fn dsb() {
121121
/// Ensures that all explicit memory accesses that appear in program order before the `DMB`
122122
/// instruction are observed before any explicit memory accesses that appear in program order
123123
/// after the `DMB` instruction.
124-
#[inline]
124+
#[inline(always)]
125125
pub fn dmb() {
126126
compiler_fence(Ordering::SeqCst);
127127
#[cfg(cortex_m)]
@@ -136,7 +136,7 @@ pub fn dmb() {
136136
/// Queries the Security state and access permissions of a memory location.
137137
/// Returns a Test Target Response Payload (cf section D1.2.215 of
138138
/// Armv8-M Architecture Reference Manual).
139-
#[inline]
139+
#[inline(always)]
140140
#[cfg(armv8m)]
141141
// The __tt function does not dereference the pointer received.
142142
#[allow(clippy::not_unsafe_ptr_arg_deref)]
@@ -158,7 +158,7 @@ pub fn tt(addr: *mut u32) -> u32 {
158158
/// access to that location.
159159
/// Returns a Test Target Response Payload (cf section D1.2.215 of
160160
/// Armv8-M Architecture Reference Manual).
161-
#[inline]
161+
#[inline(always)]
162162
#[cfg(armv8m)]
163163
// The __ttt function does not dereference the pointer received.
164164
#[allow(clippy::not_unsafe_ptr_arg_deref)]
@@ -181,7 +181,7 @@ pub fn ttt(addr: *mut u32) -> u32 {
181181
/// undefined if used from Non-Secure state.
182182
/// Returns a Test Target Response Payload (cf section D1.2.215 of
183183
/// Armv8-M Architecture Reference Manual).
184-
#[inline]
184+
#[inline(always)]
185185
#[cfg(armv8m)]
186186
// The __tta function does not dereference the pointer received.
187187
#[allow(clippy::not_unsafe_ptr_arg_deref)]
@@ -204,7 +204,7 @@ pub fn tta(addr: *mut u32) -> u32 {
204204
/// state and is undefined if used from Non-Secure state.
205205
/// Returns a Test Target Response Payload (cf section D1.2.215 of
206206
/// Armv8-M Architecture Reference Manual).
207-
#[inline]
207+
#[inline(always)]
208208
#[cfg(armv8m)]
209209
// The __ttat function does not dereference the pointer received.
210210
#[allow(clippy::not_unsafe_ptr_arg_deref)]
@@ -224,7 +224,7 @@ pub fn ttat(addr: *mut u32) -> u32 {
224224
///
225225
/// See section C2.4.26 of Armv8-M Architecture Reference Manual for details.
226226
/// Undefined if executed in Non-Secure state.
227-
#[inline]
227+
#[inline(always)]
228228
#[cfg(armv8m)]
229229
pub unsafe fn bx_ns(addr: u32) {
230230
asm!("bxns {}", in(reg) addr, options(nomem, nostack, preserves_flags));
@@ -234,9 +234,9 @@ pub unsafe fn bx_ns(addr: u32) {
234234
///
235235
/// This method is used by cortex-m-semihosting to provide semihosting syscalls.
236236
#[cfg(cortex_m)]
237-
#[inline]
237+
#[inline(always)]
238238
pub unsafe fn semihosting_syscall(mut nr: u32, arg: u32) -> u32 {
239-
asm!("bkpt #0xab", inout("r0") nr, in("r1") arg, options(nomem, nostack, preserves_flags));
239+
asm!("bkpt #0xab", inout("r0") nr, in("r1") arg, options(nostack, preserves_flags));
240240
nr
241241
}
242242

src/peripheral/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@
5757
//!
5858
//! - ARMv7-M Architecture Reference Manual (Issue E.b) - Chapter B3
5959
60+
use crate::interrupt;
6061
use core::marker::PhantomData;
6162
use core::ops;
62-
use crate::interrupt;
6363

6464
#[cfg(cm7)]
6565
pub mod ac;

0 commit comments

Comments
 (0)