Skip to content

Import cortex-m-semihosting and panic-semihosting into this repo #263

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

Merged
merged 5 commits into from
Oct 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ inline-asm = []
linker-plugin-lto = []

[workspace]
members = ["xtask"]
members = ["xtask", "cortex-m-semihosting", "panic-semihosting"]

[package.metadata.docs.rs]
targets = [
Expand Down
7 changes: 7 additions & 0 deletions asm/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,13 @@ pub unsafe fn __wfi() {
asm!("wfi");
}

/// Semihosting syscall.
#[inline(always)]
pub unsafe fn __syscall(mut nr: u32, arg: u32) -> u32 {
asm!("bkpt #0xab", inout("r0") nr, in("r1") arg);
nr
}

// v7m *AND* v8m.main, but *NOT* v8m.base
#[cfg(any(armv7m, armv8m_main))]
pub use self::v7m::*;
Expand Down
5 changes: 3 additions & 2 deletions asm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ macro_rules! shims {
pub unsafe extern "C" fn $name(
$($arg: $argty),*
) $(-> $ret)? {
crate::inline::$name($($arg)*)
crate::inline::$name($($arg),*)
}
)+
};
Expand All @@ -72,9 +72,10 @@ shims! {
fn __udf();
fn __wfe();
fn __wfi();
fn __syscall(nr: u32, arg: u32) -> u32;
}

// v7m *AND* v8m.main, but *NOT* v8m.base
// v7m *AND* v8m.main, but *NOT* v8m.base
#[cfg(any(armv7m, armv8m_main))]
shims! {
fn __basepri_max(val: u8);
Expand Down
Binary file modified bin/thumbv6m-none-eabi-lto.a
Binary file not shown.
Binary file modified bin/thumbv6m-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv7em-none-eabi-lto.a
Binary file not shown.
Binary file modified bin/thumbv7em-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv7em-none-eabihf-lto.a
Binary file not shown.
Binary file modified bin/thumbv7em-none-eabihf.a
Binary file not shown.
Binary file modified bin/thumbv7m-none-eabi-lto.a
Binary file not shown.
Binary file modified bin/thumbv7m-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv8m.base-none-eabi-lto.a
Binary file not shown.
Binary file modified bin/thumbv8m.base-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv8m.main-none-eabi-lto.a
Binary file not shown.
Binary file modified bin/thumbv8m.main-none-eabi.a
Binary file not shown.
Binary file modified bin/thumbv8m.main-none-eabihf-lto.a
Binary file not shown.
Binary file modified bin/thumbv8m.main-none-eabihf.a
Binary file not shown.
123 changes: 123 additions & 0 deletions cortex-m-semihosting/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
# Change Log

All notable changes to this project will be documented in this file.
This project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

## [v0.3.5] - 2019-08-29

### Added

- Adds a feature to work around JLink quirks
- Adds a dbg! macro using heprintln
- Added thumbv8m.main support on stable

### Fixed

- Now Rust 2018 edition

## [v0.3.4] - 2019-08-13

### Fixed

- Support for thumbv8 mainline hf target

## [v0.3.3] - 2019-04-22

### Added

- Adds support for thumbv8 and cortex-m v0.6.0

## [v0.3.2] - 2018-11-04

### Added

- Added a family of `hprint` macros for printing to the host standard output /
error via globally shared `HStdout` / `HStderr` handles .

## [v0.3.1] - 2018-08-27

### Changed

- This crate no longer depends on `arm-none-eabi-gcc`.

## [v0.3.0] - 2018-05-10

### Changed

- [breaking-change] `inline-asm` is no longer a default feature (i.e. a feature that's enabled by
default). The consequence is that this crate now compiles on 1.27 (beta) by default, and opting
into `inline-asm` requires nightly.

## [v0.2.1] - 2018-04-25

### Added

- An opt-out "inline-asm" Cargo feature. When this feature is disabled semihosting is implemented
using an external assembly file instead of using the unstable inline assembly (`asm!`) feature
meaning that this crate can be compiled on stable.

## [v0.2.0] - 2017-07-07

### Added

- `exit` and `report_exception` syscalls

- `HStdout` and `HStderr` structs that represent handles to the host stdout and
stderr stream respectively.

### Changed

- [breaking-change] The `io` module has been renamed to `hio` to reflect that
this is I/O *on the host*.

### Removed

- [breaking-change] the family of `write` functions in the `io` module. Instead
use `HStdout` / `HStderr` and its `write_all` method and `fmt::Write`
implementation.

- [breaking-change] the `hprint!` family of macros. Instead use `HStdout` and
the standard `write!` macro.

## [v0.1.3] - 2017-02-27

### Added

- A family of `ewrite` functions and `ehprint!` macros to write to the host's
stderr.

### Fixed

- `write_all` logic when a single write doesn't write all the buffer bytes

## [v0.1.2] - 2017-02-15

### Fixed

- the `hprintln!` macro when called without arguments.

## [v0.1.1] - 2017-01-22

### Added

- Expose a family of `write` functions to write to the host's stdout without
going through the `hprint!` macros.

## v0.1.0 - 2017-01-22

- Initial release

[Unreleased]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.3.5...HEAD
[v0.3.5]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.3.4...v0.3.5
[v0.3.4]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.3.3...v0.3.4
[v0.3.3]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.3.2...v0.3.3
[v0.3.2]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.3.1...v0.3.2
[v0.3.1]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.3.0...v0.3.1
[v0.3.0]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.2.1...v0.3.0
[v0.2.1]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.2.0...v0.2.1
[v0.2.0]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.1.3...v0.2.0
[v0.1.3]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.1.2...v0.1.3
[v0.1.2]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.1.1...v0.1.2
[v0.1.1]: https://github.com/rust-embedded/cortex-m-semihosting/compare/v0.1.0...v0.1.1
22 changes: 22 additions & 0 deletions cortex-m-semihosting/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
authors = [
"The Cortex-M Team <[email protected]>",
"Jorge Aparicio <[email protected]>",
]
description = "Semihosting for ARM Cortex-M processors"
documentation = "https://docs.rs/cortex-m-semihosting"
keywords = ["semihosting", "arm", "cortex-m"]
license = "MIT OR Apache-2.0"
name = "cortex-m-semihosting"
readme = "README.md"
repository = "https://github.com/rust-embedded/cortex-m"
version = "0.3.5"
edition = "2018"

[features]
inline-asm = []
jlink-quirks = []
no-semihosting = []

[dependencies]
cortex-m = { path = "..", version = ">= 0.5.8, < 0.7" }
40 changes: 40 additions & 0 deletions cortex-m-semihosting/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
[![crates.io](https://img.shields.io/crates/v/cortex-m-semihosting.svg)](https://crates.io/crates/cortex-m-semihosting)
[![crates.io](https://img.shields.io/crates/d/cortex-m-semihosting.svg)](https://crates.io/crates/cortex-m-semihosting)

# `cortex-m-semihosting`

> Semihosting for ARM Cortex-M processors

This project is developed and maintained by the [Cortex-M team][team].

## [Documentation](https://docs.rs/cortex-m-semihosting)

# Minimum Supported Rust Version (MSRV)

This crate is guaranteed to compile on stable Rust 1.33.0 and up. It *might*
compile with older versions but that may change in any new patch release.

## License

Licensed under either of

- Apache License, Version 2.0 ([LICENSE-APACHE](../LICENSE-APACHE) or
http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT](../LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.

## Code of Conduct

Contribution to this crate is organized under the terms of the [Rust Code of
Conduct][CoC], the maintainer of this crate, the [Cortex-M team][team], promises
to intervene to uphold that code of conduct.

[CoC]: ../CODE_OF_CONDUCT.md
[team]: https://github.com/rust-embedded/wg#the-cortex-m-team
23 changes: 23 additions & 0 deletions cortex-m-semihosting/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::path::PathBuf;
use std::{env, fs};

fn main() {
let target = env::var("TARGET").unwrap();
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
let name = env::var("CARGO_PKG_NAME").unwrap();

if target.starts_with("thumbv") {
if env::var_os("CARGO_FEATURE_INLINE_ASM").is_none() {
fs::copy(
format!("../bin/{}.a", target),
out_dir.join(format!("lib{}.a", name)),
)
.unwrap();

println!("cargo:rustc-link-lib=static={}", name);
println!("cargo:rustc-link-search={}", out_dir.display());
}

println!("cargo:rustc-cfg=thumb");
}
}
96 changes: 96 additions & 0 deletions cortex-m-semihosting/src/debug.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
//! Interacting with debugging agent
//!
//! # Example
//!
//! This example will show how to terminate the QEMU session. The program
//! should be running under QEMU with semihosting enabled
//! (use `-semihosting` flag).
//!
//! Target program:
//!
//! ```no_run
//! use cortex_m_semihosting::debug::{self, EXIT_SUCCESS, EXIT_FAILURE};
//!
//! fn main() {
//! if 2 == 2 {
//! // report success
//! debug::exit(EXIT_SUCCESS);
//! } else {
//! // report failure
//! debug::exit(EXIT_FAILURE);
//! }
//! }
//!

/// This values are taken from section 5.5.2 of
/// ADS Debug Target Guide (DUI0058).
// TODO document
#[allow(missing_docs)]
pub enum Exception {
// Hardware reason codes
BranchThroughZero = 0x20000,
UndefinedInstr = 0x20001,
SoftwareInterrupt = 0x20002,
PrefetchAbort = 0x20003,
DataAbort = 0x20004,
AddressException = 0x20005,
IRQ = 0x20006,
FIQ = 0x20007,
// Software reason codes
BreakPoint = 0x20020,
WatchPoint = 0x20021,
StepComplete = 0x20022,
RunTimeErrorUnknown = 0x20023,
InternalError = 0x20024,
UserInterruption = 0x20025,
ApplicationExit = 0x20026,
StackOverflow = 0x20027,
DivisionByZero = 0x20028,
OSSpecific = 0x20029,
}

/// Status enum for `exit` syscall.
pub type ExitStatus = Result<(), ()>;

/// Successful execution of a program.
pub const EXIT_SUCCESS: ExitStatus = Ok(());

/// Unsuccessful execution of a program.
pub const EXIT_FAILURE: ExitStatus = Err(());

/// Reports to the debugger that the execution has completed.
///
/// This call can be used to terminate QEMU session and report back success
/// or failure. If you need to pass more than one type of error, consider
/// using `report_exception` syscall instead.
///
/// This call should not return. However, it is possible for the debugger
/// to request that the application continue. In that case this call
/// returns normally.
///
pub fn exit(status: ExitStatus) {
match status {
EXIT_SUCCESS => report_exception(Exception::ApplicationExit),
EXIT_FAILURE => report_exception(Exception::RunTimeErrorUnknown),
}
}

/// Report an exception to the debugger directly.
///
/// Exception handlers can use this SWI at the end of handler chains
/// as the default action, to indicate that the exception has not been handled.
///
/// This call should not return. However, it is possible for the debugger
/// to request that the application continue. In that case this call
/// returns normally.
///
/// # Arguments
///
/// * `reason` - A reason code reported back to the debugger.
///
pub fn report_exception(reason: Exception) {
let code = reason as usize;
unsafe {
syscall1!(REPORT_EXCEPTION, code);
}
}
Loading