diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..dd7c71c --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,48 @@ +name: Build material + +on: + push: + pull_request: + +jobs: + build: + runs-on: ubuntu-20.04 + concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + steps: + - uses: actions/checkout@v3 + + - name: Install tools + uses: taiki-e/install-action@v2 + with: + tool: mdslides@0.3,mdbook@0.4,mdbook-mermaid@0.12 + + - name: Find slug name + run: | + slug=$(./describe.sh "${GITHUB_REF}") + echo "Building with slug '${slug}'" + echo "slug=${slug}" >> "${GITHUB_ENV}" + + - name: Build the material + run: | + ./build.sh + + - name: Assemble Artifacts + run: | + ./assemble.sh "${{ env.slug }}" + + - name: Upload Artifacts + uses: actions/upload-artifact@v3 + if: ${{success()}} + with: + name: Artifacts + if-no-files-found: error + path: | + ./why-ferrocene-*/ + + - name: Create and Upload Release + if: github.event_name == 'push' && startswith(github.ref, 'refs/tags/') + id: create_release + uses: ncipollo/release-action@v1 + with: + artifacts: ./why-ferrocene-${{ env.slug }}.zip diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..9d402da --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +book +slides +why-ferrocene*.zip +/why-ferrocene* diff --git a/LICENSE-APACHE b/LICENSE-APACHE new file mode 100644 index 0000000..16fe87b --- /dev/null +++ b/LICENSE-APACHE @@ -0,0 +1,201 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. diff --git a/LICENSE-MIT b/LICENSE-MIT new file mode 100644 index 0000000..8c2147f --- /dev/null +++ b/LICENSE-MIT @@ -0,0 +1,25 @@ +Copyright (c) 2023 Ferrous Systems + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d8b59fe --- /dev/null +++ b/README.md @@ -0,0 +1,68 @@ +# Why Ferrocene? + +An introduction to the Ferrocene toolchain, what is is (and isn't) and what it +can do. + +## Introduction + +In our one hour session on _Why Ferrocene?_ you will learn what Ferrocene is +about, where it is from and where it is going, and whether it is right for you +to look at Ferrocene for your next security-related or safety-critical project. +The agenda includes a short lecture, a live programming demonstration, and a +Q&A session. + +We conduct all training sessions remotely using modern video-conferencing tools +to ensure the best learning experience. + +This repository contains the teaching material for this course. + +## Booking + +See to book or to discuss customising +this material to your needs. + +## Learning Goals + +These are the questions you will be able to answer after attending this course: + +- What is Ferrocene? +- Is Ferrocene a fork of Rust? +- What support is available for Ferrocene? +- What is it like using Ferrocene? + +## Timetable + +Our standard timetable for this course is as follows: + +| Duration | Contents | +| :------: | :--------------------------------------------------- | +| 0:05 | Room open, meet and greet | +| 0:20 | Session 1 - [What is Ferrocene?](#what-is-ferrocene) | +| 0:20 | Session 2 - [A Live Demo](#a-live-demo) | +| 0:15 | Q&A | + +## Content + +### What is Ferrocene? + +- Ferrocene is just Rust +- The downstream model +- The supported host platforms +- The supported target platforms +- ISO 26262 Qualification +- Commercial Support +- The supported platforms + +### A Live Demo + +- Installing Ferrocene today +- Exploring the installation +- Writing and running an `aarch64-unknown-none` program +- Q&A + +## Licence + +Ferrous Systems offers our training material under +[MIT](./LICENSE-MIT)/[Apache-2.0](./LICENSE-APACHE) license for source code +(examples and exercises) and Creative Commons ([CC-BY-SA +4.0](https://creativecommons.org/licenses/by-sa/4.0/)) for textual content. diff --git a/assemble.sh b/assemble.sh new file mode 100755 index 0000000..8c550f8 --- /dev/null +++ b/assemble.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -euo pipefail + +ENV_SLUG=$1 + +echo "Making ./why-ferrocene-${ENV_SLUG}..." +mkdir -p "./why-ferrocene-${ENV_SLUG}" +mv ./slides "./why-ferrocene-${ENV_SLUG}/slides" +mv ./book "./why-ferrocene-${ENV_SLUG}/book" +cp -r ./example-code "./why-ferrocene-${ENV_SLUG}/example-code" +cp -r ./docker "./why-ferrocene-${ENV_SLUG}/docker" +echo "Compressing ./why-ferrocene-${ENV_SLUG}.zip..." +zip -r "./why-ferrocene-${ENV_SLUG}.zip" "./why-ferrocene-${ENV_SLUG}" diff --git a/book.toml b/book.toml new file mode 100644 index 0000000..3bf8aea --- /dev/null +++ b/book.toml @@ -0,0 +1,6 @@ +[book] +authors = ["Jonathan Pallant (Ferrous Systems)"] +language = "en" +multilingual = false +src = "src" +title = "Why Ferrocene?" diff --git a/build.sh b/build.sh new file mode 100755 index 0000000..14c76a0 --- /dev/null +++ b/build.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -euo pipefail + +rm -rf ./slides || true +mkdir ./slides +mkdir ./slides/images +mdslides --output-dir ./slides --template ./template.html --index-template ./index-template.html +cp ./src/images/*.png ./slides/images +mdbook build diff --git a/describe.sh b/describe.sh new file mode 100755 index 0000000..ed834df --- /dev/null +++ b/describe.sh @@ -0,0 +1,19 @@ +#!/bin/bash + +set -euo pipefail + +GIVEN_REF=$1 + +case "${GIVEN_REF}" in + refs/heads/*) + slug="$(git branch --show)-$(git rev-parse --short HEAD)" + ;; + refs/tags/*) + slug="$(echo "${GIVEN_REF}" | awk '{split($0,a,"/"); print a[3]}')" + ;; + refs/pull/*/merge) + slug="pr-$(echo "${GIVEN_REF}" | awk '{split($0,a,"/"); print a[3]}')-$(git rev-parse --short HEAD)" + ;; +esac + +echo "${slug}" diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..ed2c90a --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,55 @@ +# syntax=docker/dockerfile:1 + +# Dockerfile for Ferrocene 23.06 +# +# Copyright (c) Ferrous Systems, 2023 +# +# If you have the ferrocene install files in the CWD, you can run: +# +# docker build . -t ferrocene-23.06 -f ./path/to/this/Dockerfile + +FROM --platform=linux/amd64 ubuntu:18.04 as install + +RUN apt-get -y update && \ + DEBIAN_FRONTEND=noninteractive apt-get -y upgrade && \ + DEBIAN_FRONTEND=noninteractive apt-get -y install xz-utils + +COPY cargo-1.68.2-x86_64-unknown-linux-gnu.tar.xz /tmp +COPY ferrocene-docs-1.68.2.tar.xz /tmp +COPY ferrocene-docs-qualification-1.68.2.tar.xz /tmp +COPY ferrocene-self-test-1.68.2-x86_64-unknown-linux-gnu.tar.xz /tmp +COPY llvm-tools-1.68.2-x86_64-unknown-linux-gnu.tar.xz /tmp +COPY rust-src-1.68.2.tar.xz /tmp +COPY rust-std-1.68.2-aarch64-unknown-none.tar.xz /tmp +COPY rust-std-1.68.2-x86_64-unknown-linux-gnu.tar.xz /tmp +COPY rustc-1.68.2-x86_64-unknown-linux-gnu.tar.xz /tmp + +ENV FERROCENE_PATH=/opt/ferrocene-23.06 + +RUN mkdir ${FERROCENE_PATH} + +WORKDIR ${FERROCENE_PATH} + +RUN tar xvf /tmp/cargo-1.68.2-x86_64-unknown-linux-gnu.tar.xz -C ${FERROCENE_PATH} +RUN tar xvf /tmp/ferrocene-docs-1.68.2.tar.xz -C ${FERROCENE_PATH} +RUN tar xvf /tmp/ferrocene-docs-qualification-1.68.2.tar.xz -C ${FERROCENE_PATH} +RUN tar xvf /tmp/ferrocene-self-test-1.68.2-x86_64-unknown-linux-gnu.tar.xz -C ${FERROCENE_PATH} +RUN tar xvf /tmp/llvm-tools-1.68.2-x86_64-unknown-linux-gnu.tar.xz -C ${FERROCENE_PATH} +RUN tar xvf /tmp/rust-src-1.68.2.tar.xz -C ${FERROCENE_PATH} +RUN tar xvf /tmp/rust-std-1.68.2-aarch64-unknown-none.tar.xz -C ${FERROCENE_PATH} +RUN tar xvf /tmp/rust-std-1.68.2-x86_64-unknown-linux-gnu.tar.xz -C ${FERROCENE_PATH} +RUN tar xvf /tmp/rustc-1.68.2-x86_64-unknown-linux-gnu.tar.xz -C ${FERROCENE_PATH} + +FROM --platform=linux/amd64 ubuntu:18.04 as ferrocene + +# Update packages +RUN apt-get update -y && \ + DEBIAN_FRONTEND=noninteractive apt-get upgrade -y + +# Install Ferrocene 23.06 dependencies and useful tools +RUN DEBIAN_FRONTEND=noninteractive apt-get -y install gcc-aarch64-linux-gnu gcc build-essential qemu-system-aarch64 + +ENV FERROCENE_PATH=/opt/ferrocene-23.06 +ENV PATH=${PATH}:${FERROCENE_PATH}/bin + +COPY --from=install /opt /opt diff --git a/docker/README.md b/docker/README.md new file mode 100644 index 0000000..056cc94 --- /dev/null +++ b/docker/README.md @@ -0,0 +1,28 @@ +# Dockerfile for Ferrocene + +Ferrocene 23.06 is only supported inside a Ubuntu 18.04 environment. + +This [`Dockerfile`](./Dockerfile) creates an Ubuntu 18.04 container and installs +Ferrocene and an `aarch64-unknown-linux-gnu` toolchain to act as a target +linker/assembler. + +Change to the directory where you have downloaded Ferrocene and run: + +```console +docker build . -t ferrocene-23.06 -f ./path/to/this/Dockerfile +``` + +## License + +Licensed under either of + +* Apache License, Version 2.0 ([LICENSE-APACHE](../LICENSE-APACHE) or + ) +* MIT license ([LICENSE-MIT](../LICENSE-MIT) or +) 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. diff --git a/example-code/.cargo/config.toml b/example-code/.cargo/config.toml new file mode 100644 index 0000000..255022f --- /dev/null +++ b/example-code/.cargo/config.toml @@ -0,0 +1,12 @@ +[target.aarch64-unknown-none] +linker = "aarch64-linux-gnu-gcc" +rustflags = [ + "-Clinker-flavor=gcc", + "-Clink-arg=-ffreestanding", + "-Clink-arg=-nostdlib", + "-Clink-arg=-Wl,-Tlinker.ld", +] +runner = "qemu-system-aarch64 -machine virt -cpu cortex-a57 -semihosting -nographic -kernel" + +[build] +target = ["aarch64-unknown-none"] diff --git a/example-code/.gitignore b/example-code/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/example-code/.gitignore @@ -0,0 +1 @@ +/target diff --git a/example-code/Cargo.lock b/example-code/Cargo.lock new file mode 100644 index 0000000..1d0a256 --- /dev/null +++ b/example-code/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "basic-rust" +version = "0.1.0" diff --git a/example-code/Cargo.toml b/example-code/Cargo.toml new file mode 100644 index 0000000..4cdd6be --- /dev/null +++ b/example-code/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "basic-rust" +version = "0.1.0" +edition = "2021" +authors = ["Ferrous Systems"] +license = "MIT OR Apache-2.0" +description = "A simple Aarch64 demo application that runs in QEMU and compiles with Ferrocene" + +[dependencies] + +[profile.release] +opt-level = "s" diff --git a/example-code/README.md b/example-code/README.md new file mode 100644 index 0000000..5ce425a --- /dev/null +++ b/example-code/README.md @@ -0,0 +1,215 @@ +# Ferrocene 23.06 on QEMU Demo + +This repository contains a small example application that can be built using the +[Ferrocene] 23.06 toolchain from [Ferrous Systems]. + +[Ferrocene]: https://ferrocene.dev +[Ferrous Systems]: https://ferrous-systems.com + +## Supported Platforms + +Ferrocene 23.06 is supported on the following host platforms: + +* Ubuntu 18.04 AMD64, with GCC 7.5 (`x86_64-unknown-linux-gnu`) + +Using that host, it supports the following target platforms: + +* Ubuntu 18.04 AMD64, with GCC 7.5 (`x86_64-unknown-linux-gnu`) + * With `libstd` support for Linux + * So that Ferrocene can compile and execute build scripts and proc-macros +* ARM ARMv8-A and ARMv9-A Aarch64 systems (`aarch64-unknown-none`) + * `gcc-aarch64-linux-gnu` version 7.5 is the supported linker + * There is no `libstd`, and therefore there is no Operating System support. + Users are exected to use the Rust FFI mechanism to call their chosen + Operating System's C APIs. + +This demo therefore builds on Ubuntu 18.04 AMD64, and creates binaries suitable +for running on `qemu-system-aarch64`. This demo has not been certified as +meeting any particular standard or process and is offered merely as an example. + +This set of hosts and targets is expected to change in future releases of +Ferrocene. + +## Limitations of Ferrocene 23.06 + +Ferrocene 23.06 is the first release of the Ferrocene toolchain. As such it has +some limitations, which this demo accomodates: + +* There is no installer - you have to manually unpack the supplied `.tar.xz` + files. This demo unpacks them into `/opt/ferrocene-23.06`. +* Only GCC 7.5 is supported as the target linker +* Only `rustc` has been submitted for qualification, and not `cargo`. You should + therefore arrange to call `rustc` directly in your production builds. + +## Demo contents + +This demo is a simple application designed to run inside a QEMU virtual machine. + +1. It prints "Hello, world!" to the first QEMU UART, which is typically + connected to the console when you run QEMU. +2. It then causes a `panic!` which causes the custom panic handler to execute. +3. The the panic handler also prints to the same UART. +4. The panic handler exits QEMU using a semihosting operation that QEMU + understands to mean "exit QEMU". + +Once you have built the demo, the QEMU command line is something like: + +```console +$ qemu-system-aarch64 -machine virt -cpu cortex-a57 -semihosting -nographic -kernel target/production/kernel.elf +Hello, world! +PANIC: PanicInfo { + payload: Any { .. }, + message: Some( + Let's try a panic?, + ), + location: Location { + file: "src/main.rs", + line: 28, + col: 5, + }, + can_unwind: true, +} +``` + +See [`qemu.sh`](./qemu.sh). + +## Building with `cargo` and Rust + +Ferrocene compiles standard Rust source code, and so this project has also been +set up as a valid Rust project. The [`.cargo/config.toml`](./.cargo/config.toml) +file configures the default target as `aarch64-unknown-none` and selects +`aarch64-linux-gnu-gcc` as the linker. It also sets up the linker arguments to +ensure that [`./linker.ld`](./linker.ld) is used as the linker script. + +Before the build, `cargo` will compile and execute `build.rs`, which will: + +1. Generate the name of AS (the assembler) and AR (the archiver) by taking the + current linker name and changing `gcc` for `as` and `ar` respectively. +2. Copy the linker script to the `cargo` temporary output directory where the + linker will look for it. +3. Assemble `src/boot.S` as `/boot.o` +4. Add the assembled `/` file to `/libboot.a` +5. Tell `cargo` to link against `libboot.a` + +If you don't have the supported linker `aarch64-linux-gnu-gcc` and wish to +substitute it with an unsupported linker, you can run something like: + +```console +cargo build --release --config "target.aarch64-unknown-none.linker=\"aarch64-elf-gcc\"" +``` + +The compiled outputs will go into `./target/aarch64-none-eabi/`, where +`` is `debug` or `release`. The binary is called `basic-rust`, because +that's the name given in the `Cargo.toml` file. + +```console +$ cargo build --release + Finished release [optimized] target(s) in 0.00s +$ aarch64-elf-size target/aarch64-unknown-none/release/basic-rust + text data bss dec hex filename + 23146 0 0 23146 5a6a target/aarch64-unknown-none/release/basic-rust +$ cargo run --release + Compiling basic-rust v0.1.0 (/Users/jonathan/Downloads/ferrocene-23.06/work/basic-rust) + Finished release [optimized] target(s) in 0.16s + Running `qemu-system-aarch64 -machine virt -cpu cortex-a57 -semihosting -nographic -kernel target/aarch64-unknown-none/release/basic-rust` +Hello, world! +PANIC: PanicInfo { + payload: Any { .. }, + message: Some( + Let's try a panic?, + ), + location: Location { + file: "src/main.rs", + line: 28, + col: 5, + }, + can_unwind: true, +} +``` + +## Building with Ferrocene + +Because the `cargo` binary shipped with Ferrocene is not qualified, you may +prefer to use your own build system, or call `rustc` directly. + +This demo includes a [`build.sh`](./build.sh) shell script to build our binary +by calling `rustc` directly. This script will: + +1. Assemble `src/boot.S` as `/boot.o` +2. Add the assembled `/` file to `/libboot.a` +3. Call `rustc` to compile `src/main.rs` into `/basic-rust` +4. Generate `asm` and `map` files from the `/basic-rust` + +If you don't have the supported linker `aarch64-linux-gnu-gcc` and wish to +substitute it with an unsupported linker, you can set the C toolchain prefix +with: + +```console +PREFIX=aarch64-elf ./build.sh" +``` + +The outputs will go into `./target/production` and the binary is called +`basic-rust`. You can choose any suitable directory, but avoid clashing with +anything you do using `cargo`. + +```console +$ ./build.sh +Running as... +Running ar.. +Running rustc... +Generating asm... +Generating map... +$ aarch64-linux-gnu-size ./target/production/basic-rust + text data bss dec hex filename + 24127 0 0 24127 5e3f ./target/production/basic-rust +$ qemu-system-aarch64 \ + -machine virt \ + -cpu cortex-a57 \ + -semihosting \ + -nographic \ + -kernel ./target/production/basic-rust +Hello, world! +PANIC: PanicInfo { + payload: Any { .. }, + message: Some( + Let's try a panic?, + ), + location: Location { + file: "src/main.rs", + line: 28, + col: 5, + }, + can_unwind: true, +} +``` + +## Creating a Docker Container + +If you don't have an Ubuntu 18.04 machine, but you do have Docker available, you +can use `docker/Dockerfile` to create environment. + +You should change to the location where you downloaded the Ferrocene install +tarballs, and run: + +```console +docker build . -t ferrocene-23.06 -f ./path/to/docker/Dockerfile +``` + +Adjust the final path to point to where the +[`./docker/Dockerfile`](./docker/Dockerfile) is located relative to your +download area. + +## License + +Licensed under either of + +* Apache License, Version 2.0 ([LICENSE-APACHE](../LICENSE-APACHE) or + ) +* MIT license ([LICENSE-MIT](../LICENSE-MIT) or +) 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. diff --git a/example-code/build.rs b/example-code/build.rs new file mode 100644 index 0000000..ed3e303 --- /dev/null +++ b/example-code/build.rs @@ -0,0 +1,65 @@ +//! # Build script for the QEMU Ferrocene demo project +//! +//! This script only executes when using `cargo` to build the project. + +use std::io::Write; + +fn main() { + // Find the right tools. + let linker = std::env::var("RUSTC_LINKER").unwrap(); + let aarch64_as = linker.replace("gcc", "as"); + let aarch64_ar = linker.replace("gcc", "ar"); + + // Put `linker.ld` file in our output directory and ensure it's on the + // linker search path. + let out = &std::path::PathBuf::from(std::env::var_os("OUT_DIR").unwrap()); + std::fs::File::create(out.join("linker.ld")) + .unwrap() + .write_all(include_bytes!("linker.ld")) + .unwrap(); + println!("cargo:rustc-link-search={}", out.display()); + + // Assembly src/boot.S + let boot_object = out.join("boot.o"); + let output = std::process::Command::new(&aarch64_as) + .arg("src/boot.S") + .arg("-o") + .arg(&boot_object) + .output() + .map(|h| h.status.success()); + match output { + Ok(true) => { + // Ran OK + } + Ok(false) => { + // Didn't launch + panic!("Failed to launch {aarch64_as}"); + } + Err(e) => { + // Failed to run + panic!("Failed to run {aarch64_as}: {e:?}"); + } + } + let libboot_file = out.join("libboot.a"); + let output = std::process::Command::new(&aarch64_ar) + .arg("rcs") + .arg(&libboot_file) + .arg(&boot_object) + .output() + .map(|h| h.status.success()); + match output { + Ok(true) => { + // Ran OK + } + Ok(false) => { + // Didn't launch + panic!("Failed to launch {aarch64_ar}"); + } + Err(e) => { + // Failed to run + panic!("Failed to run {aarch64_ar}: {e:?}"); + } + } + // Link against our libboot.a library + println!("cargo:rustc-link-lib=static=boot"); +} diff --git a/example-code/build.sh b/example-code/build.sh new file mode 100755 index 0000000..46d5d8c --- /dev/null +++ b/example-code/build.sh @@ -0,0 +1,33 @@ +#!/bin/bash + +set -euo pipefail + +TARGET_DIR=target/production +OUTPUT_BINARY=${TARGET_DIR}/basic-rust +OUTPUT_MAP=${TARGET_DIR}/basic-rust.map +OUTPUT_ASM=${TARGET_DIR}/basic-rust.asm +OUR_PREFIX="${PREFIX:=aarch64-linux-gnu}" +CC=${OUR_PREFIX}-gcc +AS=${OUR_PREFIX}-as +AR=${OUR_PREFIX}-ar +OBJDUMP=${OUR_PREFIX}-objdump +RUSTC="rustc \ + --target aarch64-unknown-none \ + -Clinker=${CC} \ + -Clinker-flavor=gcc \ + -Clink-arg=-ffreestanding \ + -Clink-arg=-nostdlib \ + -L ${TARGET_DIR}" + +rm -rf ${TARGET_DIR} +mkdir -p ${TARGET_DIR} +echo Running as... +${AS} src/boot.S -o ${TARGET_DIR}/boot.o +echo Running ar.. +${AR} rcs ${TARGET_DIR}/libboot.a ${TARGET_DIR}/boot.o +echo Running rustc... +${RUSTC} src/main.rs -Clink-arg=-Tlinker.ld -lboot -Copt-level=s --edition 2021 -o ${OUTPUT_BINARY} +echo Generating asm... +${OBJDUMP} -d ${OUTPUT_BINARY} > ${OUTPUT_ASM} +echo Generating map... +${OBJDUMP} -t ${OUTPUT_BINARY} > ${OUTPUT_MAP} diff --git a/example-code/commands.gdb b/example-code/commands.gdb new file mode 100644 index 0000000..bbfd2a5 --- /dev/null +++ b/example-code/commands.gdb @@ -0,0 +1,3 @@ +target extended-remote :1234 +layout split + diff --git a/example-code/linker.ld b/example-code/linker.ld new file mode 100644 index 0000000..010b2a3 --- /dev/null +++ b/example-code/linker.ld @@ -0,0 +1,21 @@ +/* RAM starts at 0x40000000 but if we ask to load the kernel there, QEMU will not load a DTB */ + +MEMORY { + RAM : ORIGIN = 0x40100000, LENGTH = 128M +} + +ENTRY(_start) +SECTIONS { + .startup ORIGIN(RAM) : { + *(.text.startup) + } > RAM + .text : { *(.text .text*) } > RAM + .data : { *(.data .data*) } > RAM + .bss : { *(.bss .bss* COMMON) } > RAM + /DISCARD/ : { + *(.note .note*) + } + . = ALIGN(8); + . += 0x1000; /* 4kB of stack memory */ + stack_top = .; +} diff --git a/example-code/qemu.sh b/example-code/qemu.sh new file mode 100755 index 0000000..9b50949 --- /dev/null +++ b/example-code/qemu.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +TARGET_DIR=target/production +BINARY=${TARGET_DIR}/basic-rust +qemu-system-aarch64 -machine virt -cpu cortex-a57 -semihosting -nographic -kernel ${BINARY} diff --git a/example-code/src/boot.S b/example-code/src/boot.S new file mode 100644 index 0000000..67c7462 --- /dev/null +++ b/example-code/src/boot.S @@ -0,0 +1,19 @@ +.section .text.startup +.global _start + +// Assumes we are in EL1 + +_start: + // Set stack pointer + ldr x30, =stack_top + mov sp, x30 + // Set FPEN bits [21:20] to 0b11 to prevent trapping. + mov x0, #3 << 20 + msr cpacr_el1, x0 +0: + // Clear interrupt bit + msr daifclr, #0x4 + // Jump to application + bl kmain + // In case the application returns, loop forever + b . diff --git a/example-code/src/main.rs b/example-code/src/main.rs new file mode 100644 index 0000000..c52c027 --- /dev/null +++ b/example-code/src/main.rs @@ -0,0 +1,95 @@ +//! An example program for QEMU's Aarch64 Virtual Machine +//! +//! Written by Jonathan Pallant at Ferrous Systems +//! +//! Copyright (c) Ferrous Systems, 2023 +//! +//! To + +#![no_std] +#![no_main] + +use core::fmt::Write; + +/// Represents an emulated QEMU UART. +struct Uart { + base: *mut u32, +} + +impl Uart { + /// Create a handle to the first UART + pub fn uart0() -> Uart { + const UART0_ADDR: usize = 0x0000_0000_0900_0000; + Uart { + base: UART0_ADDR as *mut u32, + } + } + + /// Write one byte to the QEMU virtual UART. + /// + /// We don't have to check for FIFO space as the emulated FIFO never runs + /// out of space. + pub fn putchar(&mut self, byte: u8) { + unsafe { + self.base.write_volatile(u32::from(byte)); + } + } +} + +impl core::fmt::Write for Uart { + fn write_str(&mut self, s: &str) -> core::fmt::Result { + for b in s.bytes() { + self.putchar(b); + } + Ok(()) + } +} + +/// The entry-point to the Rust application. +/// +/// It is called by the start-up code in [`boot.S`](./boot.S) and thus exported +/// as a C-compatible symbol. +#[no_mangle] +pub extern "C" fn kmain() { + if let Err(e) = main() { + panic!("main returned {:?}", e); + } +} + +/// The main function of our Rust application. +/// +/// Called by [`kmain`]. +fn main() -> Result<(), core::fmt::Error> { + let mut c = Uart::uart0(); + writeln!(c, "Hello, this is Rust!")?; + for x in 1..=10 { + for y in 1..=10 { + let z = x * y; + write!(c, "{z:4}")?; + } + writeln!(c)?; + } + panic!("I am a panic"); +} + +/// Called when the application raises an unrecoverable `panic!`. +/// +/// Prints the panic to the console and then exits QEMU using a semihosting +/// breakpoint. +#[panic_handler] +fn panic(info: &core::panic::PanicInfo) -> ! { + const SYS_REPORTEXC: u64 = 0x18; + let mut c = Uart::uart0(); + let _ = writeln!(c, "PANIC: {:#?}", info); + loop { + // Exit, using semihosting + unsafe { + core::arch::asm!( + "hlt 0xf000", + in("x0") SYS_REPORTEXC + ) + } + } +} + +// End of file diff --git a/index-template.html b/index-template.html new file mode 100644 index 0000000..7062f27 --- /dev/null +++ b/index-template.html @@ -0,0 +1,2486 @@ + + + + + + + + + $TITLE + + + + + + +
+ +
+ $INDEX +
+ +
+

Credits

+

The development of this course is financed by Ferrous Systems. They are open sourced as a contribution to + the growth of the Rust language.

+

If you wish to fund further development of the course, book a training!

+

License

+

+ + + +

+

This work is licensed under a Creative Commons + Attribution-ShareAlike 4.0 International License.

+

We encourage the use of this material, under the terms of the above license, in the production and/or + delivery of commercial or open-source Rust training programmes.

+

Copyright (c) Ferrous Systems, 2023

+
+ +
+ + + \ No newline at end of file diff --git a/src/SUMMARY.md b/src/SUMMARY.md new file mode 100644 index 0000000..06de2eb --- /dev/null +++ b/src/SUMMARY.md @@ -0,0 +1,13 @@ +# Summary + +[Front Matter](./front-matter.md) + +- [Ferrocene is just Rust](./just-rust.md) +- [The downstream model](./downstream-model.md) +- [The supported host platforms](./supported-hosts.md) +- [The supported target platforms](./supported-targets.md) +- [ISO 26262 Qualification](./iso26262.md) +- [Commercial Support](./support.md) +- [The Demo](./demo.md) + +[Wash-up](./wash-up.md) diff --git a/src/demo.md b/src/demo.md new file mode 100644 index 0000000..9927712 --- /dev/null +++ b/src/demo.md @@ -0,0 +1 @@ +# The Demo diff --git a/src/downstream-model.md b/src/downstream-model.md new file mode 100644 index 0000000..521152c --- /dev/null +++ b/src/downstream-model.md @@ -0,0 +1 @@ +# The downstream model diff --git a/src/front-matter.md b/src/front-matter.md new file mode 100644 index 0000000..58f39b2 --- /dev/null +++ b/src/front-matter.md @@ -0,0 +1 @@ +# Front Matter diff --git a/src/images/logo_ferrous-systems_rgb.png b/src/images/logo_ferrous-systems_rgb.png new file mode 100644 index 0000000..26d39bf Binary files /dev/null and b/src/images/logo_ferrous-systems_rgb.png differ diff --git a/src/iso26262.md b/src/iso26262.md new file mode 100644 index 0000000..30701ad --- /dev/null +++ b/src/iso26262.md @@ -0,0 +1 @@ +# ISO 26262 Qualification diff --git a/src/just-rust.md b/src/just-rust.md new file mode 100644 index 0000000..3711594 --- /dev/null +++ b/src/just-rust.md @@ -0,0 +1 @@ +# Ferrocene is just Rust diff --git a/src/support.md b/src/support.md new file mode 100644 index 0000000..e3ce779 --- /dev/null +++ b/src/support.md @@ -0,0 +1 @@ +# Commercial Support diff --git a/src/supported-hosts.md b/src/supported-hosts.md new file mode 100644 index 0000000..6364c1b --- /dev/null +++ b/src/supported-hosts.md @@ -0,0 +1 @@ +# The supported host platforms diff --git a/src/supported-targets.md b/src/supported-targets.md new file mode 100644 index 0000000..2143abc --- /dev/null +++ b/src/supported-targets.md @@ -0,0 +1 @@ +# The supported target platforms diff --git a/src/wash-up.md b/src/wash-up.md new file mode 100644 index 0000000..687136d --- /dev/null +++ b/src/wash-up.md @@ -0,0 +1 @@ +# Wash-up diff --git a/template.html b/template.html new file mode 100644 index 0000000..d2eb343 --- /dev/null +++ b/template.html @@ -0,0 +1,328 @@ + + + + + + + + + + + + + + $TITLE + + + + +
+
+
+ +
+ +
+
+ +
+
+ + + + + + + + + + + + +