├── .gitignore ├── .github ├── CODEOWNERS └── workflows │ └── build.yml ├── rust-toolchain.toml ├── .vscode └── settings.json ├── .cargo └── config.toml ├── tests ├── riscv64_virt │ ├── fw_jump.elf │ ├── mod.rs │ └── link.ld ├── armv7m_mps2an500 │ ├── startup.S │ ├── mod.rs │ └── link.ld ├── aarch64_raspi3 │ ├── mod.rs │ └── link.ld ├── test_runner_wrapper.sh └── exit_13.rs ├── .rustfmt.toml ├── .editorconfig ├── THIRD_PARTY_NOTICES.md ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE-MIT ├── Makefile ├── LICENSE-BSD-2 ├── src ├── riscv64.rs ├── x86.rs ├── aarch32.rs ├── aarch64.rs └── lib.rs ├── README.md ├── CODE_OF_CONDUCT.md └── LICENSE-APACHE /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | Cargo.lock 3 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @rust-embedded/libs 2 | -------------------------------------------------------------------------------- /rust-toolchain.toml: -------------------------------------------------------------------------------- 1 | [toolchain] 2 | channel = "nightly" 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "editor.formatOnSave": true, 3 | "editor.rulers": [100] 4 | } 5 | -------------------------------------------------------------------------------- /.cargo/config.toml: -------------------------------------------------------------------------------- 1 | [target.'cfg(target_os = "none")'] 2 | runner = "tests/test_runner_wrapper.sh" 3 | -------------------------------------------------------------------------------- /tests/riscv64_virt/fw_jump.elf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rust-embedded/qemu-exit/HEAD/tests/riscv64_virt/fw_jump.elf -------------------------------------------------------------------------------- /tests/armv7m_mps2an500/startup.S: -------------------------------------------------------------------------------- 1 | .section ".text.boot","ax" 2 | 3 | .thumb 4 | .thumb_func 5 | .globl _vectors 6 | _vectors: 7 | .word 0x2000 8 | .word reset 9 | 10 | .thumb_func 11 | reset: 12 | bl entry 13 | -------------------------------------------------------------------------------- /.rustfmt.toml: -------------------------------------------------------------------------------- 1 | newline_style = "Unix" 2 | edition = "2018" 3 | merge_imports = true 4 | format_code_in_doc_comments = true 5 | normalize_comments = true 6 | wrap_comments = true 7 | comment_width = 100 8 | report_fixme = "Always" 9 | report_todo = "Always" 10 | -------------------------------------------------------------------------------- /tests/riscv64_virt/mod.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2019-2022 Esteban Blanc 4 | 5 | //! RISCV64 specific setup code. 6 | 7 | use core::arch::asm; 8 | 9 | #[no_mangle] 10 | unsafe fn _start() -> ! { 11 | asm!("la sp, _stack"); 12 | 13 | super::test_main() 14 | } 15 | -------------------------------------------------------------------------------- /tests/aarch64_raspi3/mod.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2019-2022 Andre Richter 4 | 5 | //! AArch64 specific setup code. 6 | 7 | use core::arch::asm; 8 | 9 | #[no_mangle] 10 | unsafe fn _start() -> ! { 11 | asm!("mov sp, #0x80000"); 12 | 13 | super::test_main() 14 | } 15 | -------------------------------------------------------------------------------- /tests/armv7m_mps2an500/mod.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2024 Philipp Schulz 4 | 5 | //! AArch32 specific setup code. 6 | 7 | use core::arch::global_asm; 8 | 9 | global_asm!(include_str!("./startup.S")); 10 | 11 | #[no_mangle] 12 | extern "C" fn entry() { 13 | super::test_main() 14 | } 15 | -------------------------------------------------------------------------------- /tests/aarch64_raspi3/link.ld: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: MIT OR Apache-2.0 2 | * 3 | * Copyright (c) 2020-2022 Andre Richter 4 | */ 5 | 6 | SECTIONS 7 | { 8 | /* Set current address to the value from which the RPi starts execution */ 9 | . = 0x80000; 10 | 11 | .text : 12 | { 13 | *(.text._start) 14 | *(.text) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /tests/armv7m_mps2an500/link.ld: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: MIT OR Apache-2.0 2 | * 3 | * Copyright (c) 2024 Philipp Schulz 4 | */ 5 | ENTRY(_vectors) 6 | 7 | SECTIONS 8 | { 9 | . = 0x0; 10 | .boot : 11 | { 12 | KEEP(*(.text.boot)) 13 | KEEP(*(.data.boot)) 14 | } 15 | 16 | .text : 17 | { 18 | *(.vectors) 19 | *(.text._start) 20 | *(.text) 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # editorconfig.org 2 | 3 | root = true 4 | 5 | [*] 6 | charset = utf-8 7 | end_of_line = lf 8 | insert_final_newline = true 9 | indent_style = space 10 | trim_trailing_whitespace = true 11 | max_line_length = 100 12 | 13 | [Makefile] 14 | indent_style = tab 15 | indent_size = 8 16 | 17 | [*.rs] 18 | indent_size = 4 19 | 20 | [*.sh] 21 | indent_size = 4 22 | 23 | [*.toml] 24 | indent_size = 4 25 | 26 | [*.{yml,yaml}] 27 | indent_size = 2 28 | -------------------------------------------------------------------------------- /tests/riscv64_virt/link.ld: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: MIT OR Apache-2.0 2 | * 3 | * Copyright (c) 2020-2022 Esteban Blanc 4 | */ 5 | 6 | ENTRY(_start) 7 | 8 | SECTIONS 9 | { 10 | /* Set current address to the address where OpenSBI will jump */ 11 | . = 0x80200000; 12 | 13 | .text : { 14 | *(.text._start) 15 | *(.text) 16 | } 17 | .rodata : { *(.rodata*) } 18 | 19 | PROVIDE(_stack = . + 1M); 20 | } 21 | -------------------------------------------------------------------------------- /THIRD_PARTY_NOTICES.md: -------------------------------------------------------------------------------- 1 | # Third Party Notices 2 | 3 | This project includes or partly uses code from the following open source software subject to the 4 | following open source licenses. 5 | 6 | ## OpenSBI 7 | Copyright (c) 2019 Western Digital Corporation or its affiliates and other contributors. 8 | 9 | The OpenSBI [binary](tests/riscv64_virt/fw_jump.elf) found in this project is used under the terms 10 | of the BSD 2-Clause license. The full text of this license can be found in the file 11 | [LICENSE-BSD-2](LICENSE-BSD-2). 12 | -------------------------------------------------------------------------------- /tests/test_runner_wrapper.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | TIMEOUT="timeout 10" 4 | if [[ $1 == *"aarch64"* ]]; then 5 | $TIMEOUT qemu-system-aarch64 -M raspi3b -display none -semihosting -kernel $1 6 | elif [[ $1 == *"thumbv7em"* ]]; then 7 | qemu-system-arm -m 16M -nographic -M mps2-an500 -cpu cortex-m7 -serial mon:stdio -semihosting -kernel $1 8 | elif [[ $1 == *"riscv64"* ]]; then 9 | $TIMEOUT qemu-system-riscv64 -M virt -bios tests/riscv64_virt/fw_jump.elf -display none -kernel $1 10 | fi 11 | 12 | let "status = $? - 13" 13 | exit $status 14 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), 6 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 7 | 8 | ## [Unreleased] 9 | 10 | ### Added 11 | 12 | - Added a changelog. 13 | - Added support for 32-bit ARM architectures ([#33]). 14 | 15 | [#33]: https://github.com/rust-embedded/qemu-exit/pull/33 16 | 17 | ### Changed 18 | 19 | - Moved the repository to [rust-embedded](https://github.com/rust-embedded) under the libs team. 20 | 21 | [Unreleased]: https://github.com/rust-embedded/qemu-exit/compare/v3.0.2...HEAD 22 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "qemu-exit" 3 | version = "3.0.2" 4 | authors = ["Andre Richter "] 5 | description = "Exit QEMU with user-defined code" 6 | homepage = "https://github.com/rust-embedded/qemu-exit" 7 | repository = "https://github.com/rust-embedded/qemu-exit" 8 | readme = "README.md" 9 | keywords = ["aarch64", "x86_64", "risc-v", "qemu", "exit"] 10 | categories = ["embedded", "hardware-support", "no-std"] 11 | license = "MIT/Apache-2.0" 12 | edition = "2018" 13 | exclude = [ 14 | ".editorconfig", 15 | ".gitignore", 16 | ".rustfmt.toml", 17 | ".vscode", 18 | "Makefile" 19 | ] 20 | 21 | ##-------------------------------------------------------------------------------------------------- 22 | ## Testing 23 | ##-------------------------------------------------------------------------------------------------- 24 | 25 | [lib] 26 | test = false 27 | 28 | [[test]] 29 | name = "exit_13" 30 | harness = false 31 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (C) 2019-2022 by the respective authors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | TARGET_AARCH64 := aarch64-unknown-none-softfloat 2 | TARGET_AARCH32 := thumbv7em-none-eabi 3 | LINKER_SCRIPT_AARCH64 := tests/aarch64_raspi3/link.ld 4 | LINKER_SCRIPT_AARCH32 := tests/armv7m_mps2an500/link.ld 5 | 6 | TARGET_RISCV64 := riscv64gc-unknown-none-elf 7 | LINKER_SCRIPT_RISCV64 := tests/riscv64_virt/link.ld 8 | 9 | default: 10 | cargo build --target $(TARGET_AARCH64) --release 11 | cargo build --target $(TARGET_AARCH32) --release 12 | cargo build --target $(TARGET_RISCV64) --release 13 | cargo build --release 14 | 15 | clippy: 16 | cargo clippy --target $(TARGET_AARCH64) 17 | cargo clippy --target $(TARGET_AARCH32) 18 | cargo clippy --target $(TARGET_RISCV64) 19 | cargo clippy 20 | 21 | test: 22 | RUSTFLAGS="-C link-arg=-T$(LINKER_SCRIPT_AARCH64)" \ 23 | cargo test \ 24 | --target $(TARGET_AARCH64) \ 25 | --release 26 | RUSTFLAGS="-C link-arg=-T$(LINKER_SCRIPT_AARCH32) -Clink-args=-Map=/tmp/qemuexit-mapfile.map" \ 27 | cargo test \ 28 | --target $(TARGET_AARCH32) \ 29 | --release 30 | RUSTFLAGS="-C link-arg=-T$(LINKER_SCRIPT_RISCV64)" \ 31 | cargo test \ 32 | --target $(TARGET_RISCV64) \ 33 | --release 34 | 35 | fmt: 36 | cargo fmt 37 | 38 | ready: clippy fmt 39 | git pull 40 | cargo package --allow-dirty 41 | 42 | clean: 43 | cargo clean 44 | -------------------------------------------------------------------------------- /LICENSE-BSD-2: -------------------------------------------------------------------------------- 1 | The 2-Clause BSD License 2 | SPDX short identifier: BSD-2-Clause 3 | 4 | Copyright (c) 2019 Western Digital Corporation or its affiliates and other 5 | contributors. 6 | 7 | Redistribution and use in source and binary forms, with or without 8 | modification, are permitted provided that the following conditions are met: 9 | 10 | 1. Redistributions of source code must retain the above copyright notice, this 11 | list of conditions and the following disclaimer. 12 | 2. Redistributions in binary form must reproduce the above copyright notice, 13 | this list of conditions and the following disclaimer in the documentation 14 | and/or other materials provided with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR 20 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 23 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 25 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 | -------------------------------------------------------------------------------- /src/riscv64.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2020-2022 Esteban Blanc 4 | 5 | //! RISCV64. 6 | 7 | use crate::QEMUExit; 8 | use core::arch::asm; 9 | 10 | const EXIT_SUCCESS: u32 = 0x5555; // Equals `exit(0)`. 11 | 12 | const EXIT_FAILURE_FLAG: u32 = 0x3333; 13 | const EXIT_FAILURE: u32 = exit_code_encode(1); // Equals `exit(1)`. 14 | const EXIT_RESET: u32 = 0x7777; 15 | 16 | /// RISCV64 configuration 17 | pub struct RISCV64 { 18 | /// Address of the sifive_test mapped device. 19 | addr: u64, 20 | } 21 | 22 | /// Encode the exit code using EXIT_FAILURE_FLAG. 23 | const fn exit_code_encode(code: u32) -> u32 { 24 | (code << 16) | EXIT_FAILURE_FLAG 25 | } 26 | 27 | impl RISCV64 { 28 | /// Create an instance. 29 | pub const fn new(addr: u64) -> Self { 30 | RISCV64 { addr } 31 | } 32 | } 33 | 34 | impl QEMUExit for RISCV64 { 35 | /// Exit qemu with specified exit code. 36 | fn exit(&self, code: u32) -> ! { 37 | // If code is not a special value, we need to encode it with EXIT_FAILURE_FLAG. 38 | let code_new = match code { 39 | EXIT_SUCCESS | EXIT_FAILURE | EXIT_RESET => code, 40 | _ => exit_code_encode(code), 41 | }; 42 | 43 | unsafe { 44 | asm!( 45 | "sw {0}, 0({1})", 46 | in(reg)code_new, in(reg)self.addr 47 | ); 48 | 49 | // For the case that the QEMU exit attempt did not work, transition into an infinite 50 | // loop. Calling `panic!()` here is unfeasible, since there is a good chance 51 | // this function here is the last expression in the `panic!()` handler 52 | // itself. This prevents a possible infinite loop. 53 | loop { 54 | asm!("wfi", options(nomem, nostack)); 55 | } 56 | } 57 | } 58 | 59 | fn exit_success(&self) -> ! { 60 | self.exit(EXIT_SUCCESS); 61 | } 62 | 63 | fn exit_failure(&self) -> ! { 64 | self.exit(EXIT_FAILURE); 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/x86.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2019-2022 Andre Richter 4 | 5 | //! x86 (i386) and x86_64. 6 | 7 | use crate::QEMUExit; 8 | use core::arch::asm; 9 | 10 | const EXIT_FAILURE: u32 = 0; // since ((0 << 1) | 1) = 1. 11 | 12 | /// x86/x86_64 configuration. 13 | pub struct X86 { 14 | /// Port number of the isa-debug-exit device. 15 | io_base: u16, 16 | /// Since QEMU's isa-debug-exit cannot exit(0), choose a value that represents success for you. 17 | /// 18 | /// Note: Only odd values will work. 19 | custom_exit_success: u32, 20 | } 21 | 22 | /// Output a long on an io port 23 | fn outl(io_base: u16, code: u32) { 24 | unsafe { 25 | asm!( 26 | "out dx, eax", 27 | in("dx") io_base, 28 | in("eax") code, 29 | options(nomem, nostack) 30 | ); 31 | } 32 | } 33 | 34 | impl X86 { 35 | /// Create an instance. 36 | pub const fn new(io_base: u16, custom_exit_success: u32) -> Self { 37 | assert!((custom_exit_success & 1) == 1); 38 | 39 | X86 { 40 | io_base, 41 | custom_exit_success, 42 | } 43 | } 44 | } 45 | 46 | impl QEMUExit for X86 { 47 | fn exit(&self, code: u32) -> ! { 48 | outl(self.io_base, code); // QEMU will execute `exit(((code << 1) | 1))`. 49 | 50 | // For the case that the QEMU exit attempt did not work, transition into an infinite loop. 51 | // Calling `panic!()` here is unfeasible, since there is a good chance this function here is 52 | // the last expression in the `panic!()` handler itself. This prevents a possible infinite 53 | // loop. 54 | loop { 55 | unsafe { 56 | asm!("hlt", options(nomem, nostack)); 57 | } 58 | } 59 | } 60 | 61 | fn exit_success(&self) -> ! { 62 | self.exit(self.custom_exit_success >> 1) // Shift because QEMU does ((code << 1) | 1). 63 | } 64 | 65 | fn exit_failure(&self) -> ! { 66 | self.exit(EXIT_FAILURE) 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/aarch32.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2024 Philipp Schulz 4 | 5 | //! AArch32. 6 | 7 | use crate::QEMUExit; 8 | use core::arch::asm; 9 | 10 | const EXIT_SUCCESS: u32 = 0; 11 | const EXIT_FAILURE: u32 = 1; 12 | 13 | #[allow(non_upper_case_globals)] 14 | const ADP_Stopped_ApplicationExit: u32 = 0x20026; 15 | 16 | /// The parameter block layout that is expected by QEMU. 17 | /// 18 | /// If QEMU finds `ADP_Stopped_ApplicationExit` in the first parameter, it uses the second parameter 19 | /// as exit code. 20 | /// 21 | /// If first paraemter != `ADP_Stopped_ApplicationExit`, exit code `1` is used. 22 | #[repr(C)] 23 | struct QEMUParameterBlock { 24 | arg0: u32, 25 | arg1: u32, 26 | } 27 | 28 | /// AArch32 configuration. 29 | pub struct AArch32 {} 30 | 31 | /// A Semihosting call using `0x20` - `SYS_EXIT_EXTENDED`. 32 | fn semihosting_sys_exit_call(block: &QEMUParameterBlock) -> ! { 33 | unsafe { 34 | asm!( 35 | "bkpt #0xab", 36 | in("r0") 0x20, 37 | in("r1") block as *const _ as u32, 38 | options(nostack) 39 | ); 40 | 41 | // For the case that the QEMU exit attempt did not work, transition into an infinite loop. 42 | // Calling `panic!()` here is unfeasible, since there is a good chance this function here is 43 | // the last expression in the `panic!()` handler itself. This prevents a possible 44 | // infinite loop. 45 | loop { 46 | asm!("wfe", options(nomem, nostack)); 47 | } 48 | } 49 | } 50 | 51 | impl AArch32 { 52 | /// Create an instance. 53 | pub const fn new() -> Self { 54 | AArch32 {} 55 | } 56 | } 57 | 58 | impl QEMUExit for AArch32 { 59 | fn exit(&self, code: u32) -> ! { 60 | let block = QEMUParameterBlock { 61 | arg0: ADP_Stopped_ApplicationExit, 62 | arg1: code as u32, 63 | }; 64 | 65 | semihosting_sys_exit_call(&block) 66 | } 67 | 68 | fn exit_success(&self) -> ! { 69 | self.exit(EXIT_SUCCESS) 70 | } 71 | 72 | fn exit_failure(&self) -> ! { 73 | self.exit(EXIT_FAILURE) 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/aarch64.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2019-2022 Andre Richter 4 | 5 | //! AArch64. 6 | 7 | use crate::QEMUExit; 8 | use core::arch::asm; 9 | 10 | const EXIT_SUCCESS: u32 = 0; 11 | const EXIT_FAILURE: u32 = 1; 12 | 13 | #[allow(non_upper_case_globals)] 14 | const ADP_Stopped_ApplicationExit: u64 = 0x20026; 15 | 16 | /// The parameter block layout that is expected by QEMU. 17 | /// 18 | /// If QEMU finds `ADP_Stopped_ApplicationExit` in the first parameter, it uses the second parameter 19 | /// as exit code. 20 | /// 21 | /// If first paraemter != `ADP_Stopped_ApplicationExit`, exit code `1` is used. 22 | #[repr(C)] 23 | struct QEMUParameterBlock { 24 | arg0: u64, 25 | arg1: u64, 26 | } 27 | 28 | /// AArch64 configuration. 29 | pub struct AArch64 {} 30 | 31 | /// A Semihosting call using `0x18` - `SYS_EXIT`. 32 | fn semihosting_sys_exit_call(block: &QEMUParameterBlock) -> ! { 33 | unsafe { 34 | asm!( 35 | "hlt #0xF000", 36 | in("x0") 0x18, 37 | in("x1") block as *const _ as u64, 38 | options(nostack) 39 | ); 40 | 41 | // For the case that the QEMU exit attempt did not work, transition into an infinite loop. 42 | // Calling `panic!()` here is unfeasible, since there is a good chance this function here is 43 | // the last expression in the `panic!()` handler itself. This prevents a possible 44 | // infinite loop. 45 | loop { 46 | asm!("wfe", options(nomem, nostack)); 47 | } 48 | } 49 | } 50 | 51 | impl AArch64 { 52 | /// Create an instance. 53 | pub const fn new() -> Self { 54 | AArch64 {} 55 | } 56 | } 57 | 58 | impl QEMUExit for AArch64 { 59 | fn exit(&self, code: u32) -> ! { 60 | let block = QEMUParameterBlock { 61 | arg0: ADP_Stopped_ApplicationExit, 62 | arg1: code as u64, 63 | }; 64 | 65 | semihosting_sys_exit_call(&block) 66 | } 67 | 68 | fn exit_success(&self) -> ! { 69 | self.exit(EXIT_SUCCESS) 70 | } 71 | 72 | fn exit_failure(&self) -> ! { 73 | self.exit(EXIT_FAILURE) 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /tests/exit_13.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2019-2022 Andre Richter 4 | 5 | //! A simple test that exits QEMU with code 13. 6 | 7 | #![no_main] 8 | #![no_std] 9 | 10 | use core::panic::PanicInfo; 11 | use qemu_exit::QEMUExit; 12 | 13 | //-------------------------------------------------------------------------------------------------- 14 | // AArch64 15 | //-------------------------------------------------------------------------------------------------- 16 | 17 | #[cfg(target_arch = "aarch64")] 18 | const QEMU_EXIT_HANDLE: qemu_exit::AArch64 = qemu_exit::AArch64::new(); 19 | 20 | #[cfg(target_arch = "aarch64")] 21 | mod aarch64_raspi3; 22 | 23 | //-------------------------------------------------------------------------------------------------- 24 | // Aarch32 25 | //-------------------------------------------------------------------------------------------------- 26 | 27 | #[cfg(target_arch = "arm")] 28 | const QEMU_EXIT_HANDLE: qemu_exit::AArch32 = qemu_exit::AArch32::new(); 29 | 30 | #[cfg(target_arch = "arm")] 31 | mod armv7m_mps2an500; 32 | 33 | //-------------------------------------------------------------------------------------------------- 34 | // RISCV64 35 | //-------------------------------------------------------------------------------------------------- 36 | 37 | #[cfg(target_arch = "riscv64")] 38 | const QEMU_EXIT_HANDLE: qemu_exit::RISCV64 = qemu_exit::RISCV64::new(0x100000); 39 | 40 | #[cfg(target_arch = "riscv64")] 41 | mod riscv64_virt; 42 | 43 | //-------------------------------------------------------------------------------------------------- 44 | // x86 45 | //-------------------------------------------------------------------------------------------------- 46 | 47 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 48 | const QEMU_EXIT_HANDLE: qemu_exit::X86 = qemu_exit::X86::new(0xf4, 5); 49 | 50 | //-------------------------------------------------------------------------------------------------- 51 | // Generic code 52 | //-------------------------------------------------------------------------------------------------- 53 | 54 | #[panic_handler] 55 | fn panic(_info: &PanicInfo) -> ! { 56 | QEMU_EXIT_HANDLE.exit_failure() 57 | } 58 | 59 | fn test_main() -> ! { 60 | QEMU_EXIT_HANDLE.exit(13) 61 | } 62 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Build 2 | 3 | on: 4 | merge_group: 5 | workflow_dispatch: 6 | pull_request: 7 | branches: 8 | - main 9 | push: 10 | branches: 11 | - main 12 | schedule: 13 | - cron: "0 5 * * *" 14 | 15 | jobs: 16 | build: 17 | name: Build Library 18 | runs-on: ubuntu-latest 19 | env: { "RUSTFLAGS": "-D warnings" } 20 | strategy: 21 | matrix: 22 | target: 23 | - "thumbv7em-none-eabi" 24 | - "aarch64-unknown-none-softfloat" 25 | - "riscv64gc-unknown-none-elf" 26 | - "x86_64-unknown-linux-gnu" 27 | steps: 28 | - uses: dtolnay/rust-toolchain@master 29 | with: 30 | toolchain: nightly 31 | target: ${{ matrix.target }} 32 | 33 | - uses: actions/checkout@v5 34 | 35 | - name: cargo build 36 | run: cargo build --target ${{ matrix.target }} 37 | 38 | format: 39 | name: Format 40 | runs-on: ubuntu-latest 41 | steps: 42 | - uses: actions/checkout@v5 43 | - uses: dtolnay/rust-toolchain@master 44 | with: 45 | toolchain: nightly 46 | components: rustfmt 47 | - run: cargo fmt --all -- --check 48 | 49 | test: 50 | name: Test QEMU Exit 51 | runs-on: ubuntu-latest 52 | 53 | strategy: 54 | matrix: 55 | target: 56 | - "thumbv7em-none-eabi" 57 | - "aarch64-unknown-none-softfloat" 58 | - "riscv64gc-unknown-none-elf" 59 | 60 | steps: 61 | - uses: dtolnay/rust-toolchain@master 62 | with: 63 | toolchain: nightly 64 | target: ${{ matrix.target }} 65 | components: llvm-tools 66 | 67 | - name: Check out repository 68 | uses: actions/checkout@v5 69 | 70 | - name: Install QEMU 71 | run: | 72 | sudo apt update 73 | sudo apt install --no-install-recommends qemu-system 74 | 75 | - name: Run tests AARCH32 76 | if: matrix.target == 'thumbv7em-none-eabi' 77 | run: | 78 | RUSTFLAGS="-D warnings -C link-arg=-Ttests/armv7m_mps2an500/link.ld" cargo test --target ${{ matrix.target }} --release 79 | 80 | - name: Run tests AARCH64 81 | if: matrix.target == 'aarch64-unknown-none-softfloat' 82 | run: | 83 | RUSTFLAGS="-D warnings -C link-arg=-Ttests/aarch64_raspi3/link.ld" cargo test --target ${{ matrix.target }} --release 84 | 85 | - name: Run tests RISCV64 86 | if: matrix.target == 'riscv64gc-unknown-none-elf' 87 | run: | 88 | RUSTFLAGS="-D warnings -C link-arg=-Ttests/riscv64_virt/link.ld" cargo test --target ${{ matrix.target }} --release 89 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT OR Apache-2.0 2 | // 3 | // Copyright (c) 2019-2022 Andre Richter 4 | 5 | //! Exit QEMU with user-defined code. 6 | //! 7 | //! Quit a running QEMU session with user-defined exit code. Useful for unit or integration tests 8 | //! using QEMU. 9 | //! 10 | //! ## TL;DR 11 | //! 12 | //! ```ignore 13 | //! use qemu_exit::QEMUExit; 14 | //! 15 | //! #[cfg(target_arch = "aarch64")] 16 | //! let qemu_exit_handle = qemu_exit::AArch64::new(); 17 | //! 18 | //! #[cfg(target_arch = "arm")] 19 | //! let qemu_exit_handle = qemu_exit::Aarch32::new(); 20 | //! 21 | //! // addr: The address of sifive_test. 22 | //! #[cfg(target_arch = "riscv64")] 23 | //! let qemu_exit_handle = qemu_exit::RISCV64::new(addr); 24 | //! 25 | //! // io_base: I/O-base of isa-debug-exit. 26 | //! // custom_exit_success: A custom success code; Must be an odd number. 27 | //! #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 28 | //! let qemu_exit_handle = qemu_exit::X86::new(io_base, custom_exit_success); 29 | //! 30 | //! qemu_exit_handle.exit(1337); 31 | //! qemu_exit_handle.exit_success(); 32 | //! qemu_exit_handle.exit_failure(); 33 | //! ``` 34 | //! 35 | //! ## Architecture Specific Configuration 36 | //! 37 | //! ### AArch64 38 | //! 39 | //! Pass the `-semihosting` argument to the QEMU invocation, e.g. 40 | //! 41 | //! ```bash 42 | //! qemu-system-aarch64 -M raspi3 -serial stdio -semihosting -kernel kernel8.img 43 | //! ``` 44 | //! 45 | //! ### AArch32 46 | //! 47 | //! Pass the `-semihosting` argument to the QEMU invocation, e.g. 48 | //! 49 | //! ```bash 50 | //! qemu-system-arm -m 16M -nographic -M mps2-an500 -cpu cortex-m7 -serial mon:stdio -semihosting -kernel kernel.img 51 | //! ``` 52 | //! 53 | //! ### RISCV64 54 | //! 55 | //! You need to chose a machine with the `sifive_test` device, for exemple `-M virt`: 56 | //! 57 | //! ```bash 58 | //! qemu-system-riscv64 -M virt -nographic -monitor none -serial stdio -kernel kernel.elf 59 | //! ``` 60 | //! 61 | //! ### x86_64 62 | //! 63 | //! Add the special ISA debug exit device by passing the flags: 64 | //! 65 | //! ```bash 66 | //! -device isa-debug-exit,iobase=0xf4,iosize=0x04 67 | //! ``` 68 | //! 69 | //! When instantiating the handle, `iobase` must be given as the first parameter. 70 | //! 71 | //! The second parameter must be an `EXIT_SUCCESS` code of your choice that is an odd number, aka 72 | //! bit number zero must be `1`. This is needed because in QEMU, the provided code is internally 73 | //! binary-OR'ed with `0x1`. This is hardcoded and therefore, with `isa-debug-exit`, it is not 74 | //! possible to let QEMU invoke `exit(0)`. 75 | //! 76 | //! ```ignore 77 | //! let qemu_exit_handle = qemu_exit::X86::new(io_base, custom_exit_success); 78 | //! ``` 79 | //! 80 | //! ## Literature 81 | //! 82 | //! - [Semihosting for AArch32 and AArch64](https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst) 83 | //! - [QEMU isa-debug-exit source](https://gitlab.com/qemu-project/qemu/-/blob/master/hw/misc/debugexit.c) 84 | //! - [QEMU sifive_test source](https://gitlab.com/qemu-project/qemu/-/blob/master/hw/misc/sifive_test.c) 85 | 86 | #![deny(missing_docs)] 87 | #![no_std] 88 | 89 | #[cfg(target_arch = "aarch64")] 90 | pub mod aarch64; 91 | 92 | #[cfg(target_arch = "aarch64")] 93 | pub use aarch64::*; 94 | 95 | #[cfg(target_arch = "arm")] 96 | pub mod aarch32; 97 | 98 | #[cfg(target_arch = "arm")] 99 | pub use aarch32::*; 100 | 101 | #[cfg(target_arch = "riscv64")] 102 | pub mod riscv64; 103 | 104 | #[cfg(target_arch = "riscv64")] 105 | pub use riscv64::*; 106 | 107 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 108 | pub mod x86; 109 | 110 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 111 | pub use x86::*; 112 | 113 | /// Generic interface for exiting QEMU. 114 | pub trait QEMUExit { 115 | /// Exit with specified return code. 116 | /// 117 | /// Note: For `X86`, code is binary-OR'ed with `0x1` inside QEMU. 118 | fn exit(&self, code: u32) -> !; 119 | 120 | /// Exit QEMU using `EXIT_SUCCESS`, aka `0`, if possible. 121 | /// 122 | /// Note: Not possible for `X86`. 123 | fn exit_success(&self) -> !; 124 | 125 | /// Exit QEMU using `EXIT_FAILURE`, aka `1`. 126 | fn exit_failure(&self) -> !; 127 | } 128 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![crates.io](https://img.shields.io/crates/d/qemu-exit.svg)](https://crates.io/crates/qemu-exit) 2 | [![crates.io](https://img.shields.io/crates/v/qemu-exit.svg)](https://crates.io/crates/qemu-exit) 3 | ![Build](https://github.com/rust-embedded/qemu-exit/workflows/Build/badge.svg) 4 | 5 | # qemu-exit 6 | 7 | Exit QEMU with user-defined code. 8 | 9 | Quit a running QEMU session with user-defined exit code. Useful for unit or integration tests using 10 | QEMU. 11 | 12 | This project is developed and maintained by the [libs team]. 13 | 14 | ## TL;DR 15 | 16 | ```rust 17 | use qemu_exit::QEMUExit; 18 | 19 | #[cfg(target_arch = "aarch64")] 20 | let qemu_exit_handle = qemu_exit::AArch64::new(); 21 | 22 | // addr: The address of sifive_test. 23 | #[cfg(target_arch = "riscv64")] 24 | let qemu_exit_handle = qemu_exit::RISCV64::new(addr); 25 | 26 | // io_base: I/O-base of isa-debug-exit. 27 | // custom_exit_success: A custom success code; Must be an odd number. 28 | #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] 29 | let qemu_exit_handle = qemu_exit::X86::new(io_base, custom_exit_success); 30 | 31 | qemu_exit_handle.exit(1337); 32 | qemu_exit_handle.exit_success(); 33 | qemu_exit_handle.exit_failure(); 34 | ``` 35 | 36 | ## Architecture Specific Configuration 37 | 38 | ### AArch64/AArch32 39 | 40 | Pass the `-semihosting` argument to the QEMU invocation, e.g.: 41 | ``` 42 | qemu-system-aarch64 -M raspi3 -serial stdio -semihosting -kernel kernel8.img 43 | qemu-system-arm -nographic -M mps2-an500 -cpu cortex-m7 -serial mon:stdio -semihosting -kernel 44 | kernel.img 45 | ``` 46 | 47 | ### RISCV64 48 | 49 | You need to chose a machine with the `sifive_test` device, for exemple `-M virt`: 50 | ``` 51 | qemu-system-riscv64 -M virt -nographic -monitor none -serial stdio -kernel kernel.elf 52 | ``` 53 | 54 | ### x86/x86_64 55 | 56 | Add the special ISA debug exit device by passing the flags: 57 | ``` 58 | -device isa-debug-exit,iobase=0xf4,iosize=0x04 59 | ``` 60 | 61 | When instantiating the handle with `qemu_exit::X86::new()`, `iobase` must be given as the first 62 | parameter. 63 | 64 | The second parameter must be an `EXIT_SUCCESS` code of your choice that is an odd number, aka 65 | bit number zero must be `1`. This is needed because in QEMU, the provided code is internally 66 | binary-OR'ed with `0x1`. This is hardcoded and therefore, with `isa-debug-exit`, it is not 67 | possible to let QEMU invoke `exit(0)`. 68 | 69 | ```rust 70 | let qemu_exit_handle = qemu_exit::X86::new(io_base, custom_exit_success); 71 | ``` 72 | 73 | #### x86/x86_64 Linux 74 | 75 | To use this mechanism from Linux userspace, the kernel must be compiled with 76 | `CONFIG_X86_IOPL_IOPERM=y` (which is the default) and the process must start with root privileges 77 | (or `CAP_SYS_RAWIO`) and call: [`ioperm(2)`](https://man7.org/linux/man-pages/man2/ioperm.2.html): 78 | ```rust 79 | nix::errno::Errno::result(unsafe { libc::ioperm( 0xf4, 4, 1 )}).expect("ioperm failed"); 80 | ``` 81 | 82 | Privileges/capabilities can then be dropped. Normal users can subsequently call 83 | `qemu_exit_handle.exit*()`. 84 | 85 | ## Literature 86 | 87 | - [Semihosting for AArch32 and AArch64](https://github.com/ARM-software/abi-aa/blob/main/semihosting/semihosting.rst) 88 | - [QEMU isa-debug-exit source](https://gitlab.com/qemu-project/qemu/-/blob/master/hw/misc/debugexit.c) 89 | - [QEMU sifive_test source](https://gitlab.com/qemu-project/qemu/-/blob/master/hw/misc/sifive_test.c) 90 | 91 | ## Authors 92 | 93 | - [**@andre-richter**](https://github.com/andre-richter) Andre Richter 94 | - [**@Skallwar**](https://github.com/Skallwar) Esteban Blanc 95 | 96 | ## License 97 | 98 | Licensed under either of 99 | 100 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0) 101 | - MIT License ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) 102 | 103 | at your option. 104 | 105 | See the [THIRD_PARTY_NOTICES.md](THIRD_PARTY_NOTICES.md) for more information about utilized third 106 | party projects and their respective licenses. 107 | 108 | ### Contribution 109 | 110 | Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the 111 | work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any 112 | additional terms or conditions. 113 | 114 | [libs team]: https://github.com/rust-embedded/wg#the-libs-team 115 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # The Rust Code of Conduct 2 | 3 | ## Conduct 4 | 5 | **Contact**: [Libs team](https://github.com/rust-embedded/wg#the-libs-team) 6 | 7 | * We are committed to providing a friendly, safe and welcoming environment for all, regardless of level of experience, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, nationality, or other similar characteristic. 8 | * On IRC, please avoid using overtly sexual nicknames or other nicknames that might detract from a friendly, safe and welcoming environment for all. 9 | * Please be kind and courteous. There's no need to be mean or rude. 10 | * Respect that people have differences of opinion and that every design or implementation choice carries a trade-off and numerous costs. There is seldom a right answer. 11 | * Please keep unstructured critique to a minimum. If you have solid ideas you want to experiment with, make a fork and see how it works. 12 | * We will exclude you from interaction if you insult, demean or harass anyone. That is not welcome behavior. We interpret the term "harassment" as including the definition in the [Citizen Code of Conduct](http://citizencodeofconduct.org/); if you have any lack of clarity about what might be included in that concept, please read their definition. In particular, we don't tolerate behavior that excludes people in socially marginalized groups. 13 | * Private harassment is also unacceptable. No matter who you are, if you feel you have been or are being harassed or made uncomfortable by a community member, please contact one of the channel ops or any of the [Libs team][team] immediately. Whether you're a regular contributor or a newcomer, we care about making this community a safe place for you and we've got your back. 14 | * Likewise any spamming, trolling, flaming, baiting or other attention-stealing behavior is not welcome. 15 | 16 | ## Moderation 17 | 18 | These are the policies for upholding our community's standards of conduct. 19 | 20 | 1. Remarks that violate the Rust standards of conduct, including hateful, hurtful, oppressive, or exclusionary remarks, are not allowed. (Cursing is allowed, but never targeting another user, and never in a hateful manner.) 21 | 2. Remarks that moderators find inappropriate, whether listed in the code of conduct or not, are also not allowed. 22 | 3. Moderators will first respond to such remarks with a warning. 23 | 4. If the warning is unheeded, the user will be "kicked," i.e., kicked out of the communication channel to cool off. 24 | 5. If the user comes back and continues to make trouble, they will be banned, i.e., indefinitely excluded. 25 | 6. Moderators may choose at their discretion to un-ban the user if it was a first offense and they offer the offended party a genuine apology. 26 | 7. If a moderator bans someone and you think it was unjustified, please take it up with that moderator, or with a different moderator, **in private**. Complaints about bans in-channel are not allowed. 27 | 8. Moderators are held to a higher standard than other community members. If a moderator creates an inappropriate situation, they should expect less leeway than others. 28 | 29 | In the Rust community we strive to go the extra step to look out for each other. Don't just aim to be technically unimpeachable, try to be your best self. In particular, avoid flirting with offensive or sensitive issues, particularly if they're off-topic; this all too often leads to unnecessary fights, hurt feelings, and damaged trust; worse, it can drive people away from the community entirely. 30 | 31 | And if someone takes issue with something you said or did, resist the urge to be defensive. Just stop doing what it was they complained about and apologize. Even if you feel you were misinterpreted or unfairly accused, chances are good there was something you could've communicated better — remember that it's your responsibility to make your fellow Rustaceans comfortable. Everyone wants to get along and we are all here first and foremost because we want to talk about cool technology. You will find that people will be eager to assume good intent and forgive as long as you earn their trust. 32 | 33 | The enforcement policies listed above apply to all official embedded WG venues; including official IRC channels (#rust-embedded); GitHub repositories under rust-embedded; and all forums under rust-embedded.org (forum.rust-embedded.org). 34 | 35 | *Adapted from the [Node.js Policy on Trolling](http://blog.izs.me/post/30036893703/policy-on-trolling) as well as the [Contributor Covenant v1.3.0](https://www.contributor-covenant.org/version/1/3/0/).* 36 | 37 | [team]: https://github.com/rust-embedded/wg#the-libs-team 38 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------