Merge #273

273: Make ErrorCode an enum that exactly represents the ErrorCode values the kernel can return. r=hudson-ayers a=jrvanwhy

I previously made `ErrorCode` a struct that can represent any `u32` value because it was unclear what values future Tock versions may return. The Tock 2.0 TRD now says that the kernel will never return an error code of 0 or an error code greater than 1023, so we can rely on that.

This has the benefit of enabling niche optimizations on the type. The zero value is now a usable niche (zero being the most efficient value to compare against in many cases), as well as all values above 1023.

There are two drawbacks to this approach:

1. 3 more uses of `unsafe` in `CommandReturn`, maybe a few extra uses in the `Syscalls` implementation.
2. A huge block of reserved numbers in `ErrorCode`.

I used the following code to generate the number block, and vim commands to format it:
```
fn main() {
    for i in 1..=1023 {
        print!("N{:05} = {:05}, ", i, i);
        if i % 5 == 0 { println!(); }
    }
}
```

Co-authored-by: Johnathan Van Why <jrvanwhy@google.com>
tree: ec072e26d875afa854b8d97a4fdd4a75fa075cfe
  1. .cargo/
  2. .github/
  3. .vscode/
  4. boards/
  5. codegen/
  6. core/
  7. doc/
  8. examples/
  9. examples-features/
  10. src/
  11. test_runner/
  12. tools/
  13. .gitignore
  14. .gitmodules
  15. bors.toml
  16. build.rs
  17. Cargo.toml
  18. CHANGELOG.md
  19. CONTRIBUTING.md
  20. layout_generic.ld
  21. LICENSE-APACHE
  22. LICENSE-MIT
  23. Makefile
  24. README.md
  25. rust-toolchain
  26. rustfmt.toml
README.md

Build Status

libtock-rs

Rust userland library for Tock (WIP)

Generally this library was tested with tock Release 1.5. Since then changes have been made that might not work with the Tock release 1.5, but instead target Tock master. For example this library might support newer boards (Apollo3), changed boards (HiFive1 revB) or new drivers (HMAC).

The library works in principle on most boards, but there is currently the showstopper bug #28 that prevents the generation of relocatable code. This means that all applications must be installed at the flash address they are compiled with, which usually means that they must be compiled especially for your board and that there can only be one application written in rust at a time and it must be installed as the first application on the board, unless you want to play games with linker scripts. There are some boards/layout_*.ld files provided that allow to run the examples on common boards. Due to MPU region alignment issues they may not work for applications that use a lot of RAM, in that case you may have to change the SRAM start address to fit your application.

Getting Started

This project is nascent and still under heavy development, but first steps:

  1. Ensure you have rustup installed.

  2. Clone the repository:

    git clone --recursive https://github.com/tock/libtock-rs
    cd libtock-rs
    
  3. Install the dependencies:

    make setup
    
  4. Use make to build examples

    make nrf52 # Builds all examples for the nrf52 platform
    
    make opentitan # Builds all examples for the OpenTitan platform
    
    make opentitan FEATURES=alloc # Builds all examples for the OpenTitan platform, with alloc feature enabled
    
    make flash-hail EXAMPLE=blink # Flash the example 'blink' program to the hail platform
    

    For an unknown platform, you may have to create your own memory layout definition. Place the layout definition file at boards/layout_<platform>.ld and do not forget to enhance the tockloader_flags dispatching section in tools/flash.sh. You are welcome to create a PR, s.t. the number of supported platforms grows.

Using libtock-rs

The easiest way to start using libtock-rs is adding an example to the examples folder. The boiler plate code you would write is

#![no_std]

use libtock::result::TockResult;

#[libtock::main]
async fn main() -> TockResult<()> {
  // Your code
}

If you want to use heap based allocation you will have to add

extern crate alloc;

to the preamble and store your example in the examples-alloc folder.

To build the examples for your board you can use

make <platform> [FEATURES=alloc]

An example can be flashed to your board after the build process by running:

make flash-<platform> EXAMPLE=<example>

This script does the following steps for you:

  • cross-compile your program
  • create a TAB (tock application bundle)
  • if you have a J-Link compatible board connected: flash this TAB to your board (using tockloader)

License

libtock-rs is licensed under either of

at your option.

Submodules have their own licenses.

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.

The contribution guidelines can be found here: contribution guidelines