jamesmunns / irr-embedded-2018

Increasing Rust's Reach, Embedded Group, 2018
2 stars 0 forks source link

Add 'it runs' tests #4

Open v-thakkar opened 6 years ago

v-thakkar commented 6 years ago

These tests are the ambitious part of the project. So, while it be good to have tests for a minimal embedded program which is executed in QEMU and completes successfully. Code must be in beta before September 13, 2018 if we want these tests to be shipped with Rust 1.30.

Note other issues will be given priority over this.

sekineh commented 6 years ago

Some idea:

Most trival one:

More complex one:

Notes

sekineh commented 6 years ago

Emulator

I installed qemu-system-arm locally:

$ sudo apt install qemu-system-arm

Crate

try https://github.com/japaric/lm3s6965evb

cargo build

cargo build

beta

I got:

   Compiling panic-abort v0.2.0
error[E0554]: #![feature] may not be used on the beta release channel
  --> /home/sekineh/.cargo/registry/src/github.com-1ecc6299db9ec823/panic-abort-0.2.0/src/lib.rs:21:1
   |
21 | #![feature(core_intrinsics)]

nightly

go to nightly:

sekineh@sekineh-VirtualBox:~/lm3s6965evb$ rustup default nightly
info: using existing install for 'nightly-x86_64-unknown-linux-gnu'
info: default toolchain set to 'nightly-x86_64-unknown-linux-gnu'

  nightly-x86_64-unknown-linux-gnu unchanged - rustc 1.29.0-nightly (6a1c0637c 2018-07-23)

Now the build succeeds with one harmless warning:

sekineh@sekineh-VirtualBox:~/lm3s6965evb$ cargo build
   Compiling cc v1.0.22                                                         
   Compiling vcell v0.1.0
   Compiling aligned v0.2.0
   Compiling bare-metal v0.2.3
   Compiling r0 v0.2.2
   Compiling panic-abort v0.2.0
   Compiling volatile-register v0.2.0
   Compiling cortex-m v0.5.4
   Compiling cortex-m-rt v0.5.2
   Compiling lm3s6965evb v0.1.0 (file:///home/sekineh/lm3s6965evb)
warning: unused variable: `x`
  --> src/main.rs:19:9
   |
19 |     let x = 42;
   |         ^ help: consider using `_x` instead
   |
   = note: #[warn(unused_variables)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 3.24s
sekineh commented 6 years ago

How to exit from qemu cleanly

register int reg0 asm("r0");
register int reg1 asm("r1");

reg0 = 0x18;    // angel_SWIreason_ReportException
reg1 = 0x20026; // ADP_Stopped_ApplicationExit

asm("svc 0x00123456");  // make semihosting call

The above will be translated to exit(0). The value other than ADP_Stopped_ApplicationExit will result in exit(1).

References

Update

The above decription is for normal Arm architecture. For thumb target, svc w0x123456 won't compile.

http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0471c/Bgbjjgij.html says:

SVC 0x123456
In ARM state for all architectures.

SVC 0xAB
In ARM state and Thumb state, excluding ARMv6-M and ARMv7-M. This behavior is not guaranteed on all debug targets from ARM or from third parties.

BKPT 0xAB
For ARMv6-M and ARMv7-M, Thumb state only.

Best way in the Rust CI

You can use cortex-m-semihosting crate without playing with asm!.

extern crate cortex_m_semihosting as semihosting;
(snip)
        unsafe { semihosting::syscall1(
            semihosting::nr::REPORT_EXCEPTION,
            semihosting::debug::Exception::ApplicationExit as usize,
        );

You can return successful result:

sekineh@sekineh-VirtualBox:~/lm3s6965evb_me$ qemu-system-arm       -cpu cortex-m3       -machine lm3s6965evb       -gdb tcp::3333              -nographic       -kernel target/thumbv7m-none-eabi/debug/lm3s6965evb -semihosting
sekineh@sekineh-VirtualBox:~/lm3s6965evb_me$ echo $?
0 (<- SUCCESS)

You can also return failure like this using other than semihosting::debug::Exception::ApplicationExit.

sekineh@sekineh-VirtualBox:~/lm3s6965evb_me$ qemu-system-arm       -cpu cortex-m3       -machine lm3s6965evb       -gdb tcp::3333              -nographic       -kernel target/thumbv7m-none-eabi/debug/lm3s6965evb -semihosting
sekineh@sekineh-VirtualBox:~/lm3s6965evb_me$ echo $?
1 (<- FAILURE)
sekineh commented 6 years ago

One terminal (qemu)

sekineh@sekineh-VirtualBox:~/lm3s6965evb$ qemu-system-arm \
>       -cpu cortex-m3 \
>       -machine lm3s6965evb \
>       -gdb tcp::3333 \
>       -S \
>       -nographic \
>       -kernel target/thumbv7m-none-eabi/debug/lm3s6965evb

tips: (C-a x to exit)

On another terminal (gdb)

like this:

sekineh@sekineh-VirtualBox:~/lm3s6965evb$ arm-none-eabi-gdb target/thumbv7m-none-eabi/debug/lm3s6965evb
GNU gdb (7.10-1ubuntu3+9) 7.10
(snip)
(gdb) target remote :3333
Remote debugging using :3333
Reset ()
    at /home/sekineh/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-rt-0.5.2/src/lib.rs:478
478 pub unsafe extern "C" fn Reset() -> ! {
(gdb) cont
Continuing.
^C
Program received signal SIGINT, Interrupt.
0x00000410 in lm3s6965evb::main::h27fb30d692100457 () at src/main.rs:22
22          unsafe { arm::__NOP() }
(gdb) disassemble 
Dump of assembler code for function lm3s6965evb::main::h27fb30d692100457:
   0x00000404 <+0>: sub sp, #8
   0x00000406 <+2>: movs    r0, #42 ; 0x2a
   0x00000408 <+4>: str r0, [sp, #4]
   0x0000040a <+6>: b.n 0x40c <lm3s6965evb::main::h27fb30d692100457+8>
   0x0000040c <+8>: bl  0x400 <core::coresimd::arm::cmsis::__NOP::h9be69ba0d9ff0355>
=> 0x00000410 <+12>:    b.n 0x40c <lm3s6965evb::main::h27fb30d692100457+8>
End of assembler dump.
(gdb) 
sekineh commented 6 years ago

I submitted this PR:

This is the prerequisite before the main PR submitted to the Rust repository.

sekineh commented 6 years ago

I opened this:

I don't think it's safe to merge now. We need to wait for stable panic_handler.