Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'it runs' tests #4

Open
v-thakkar opened this issue Jul 9, 2018 · 6 comments
Open

Add 'it runs' tests #4

v-thakkar opened this issue Jul 9, 2018 · 6 comments
Assignees

Comments

@v-thakkar
Copy link
Collaborator

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
Copy link
Collaborator

sekineh commented Jul 22, 2018

Some idea:

Most trival one:

  • Compile a main that returns 42.
  • Run it on QEMU and confirm the returned value is 42.
    (Assuming it can return result int)

More complex one:

  • Compile a main that uses various crates.
  • Run it on QEMU and confirm the returned value.
    (Assuming it can return result int)

Notes

  • In real world, most embedded programs never return, run forever.
  • We need to exit/return the program for our test purpose.

@sekineh sekineh self-assigned this Aug 22, 2018
@sekineh
Copy link
Collaborator

sekineh commented Aug 25, 2018

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/git.luolix.top-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
Copy link
Collaborator

sekineh commented Aug 28, 2018

How to exit from qemu cleanly

  • Use -semihosting (Angel interface) feature of qemu
  • Angel interface provides 'system call'
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
Copy link
Collaborator

sekineh commented Aug 28, 2018

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/git.luolix.top-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
Copy link
Collaborator

sekineh commented Sep 2, 2018

I submitted this PR:

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

@sekineh
Copy link
Collaborator

sekineh commented Sep 6, 2018

I opened this:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants