diff options
author | Stephen Crane <cranes@google.com> | 2024-03-07 23:36:51 +0000 |
---|---|---|
committer | Stephen Crane <cranes@google.com> | 2024-03-07 23:37:29 +0000 |
commit | d3d90dd086e4defaeb6f9bf9d99531aa03e7a7d8 (patch) | |
tree | 95633c67068efc43ea2237acfbccb6f9d34d39da | |
parent | 429141b90ba027aaf49990e9c9f07790db5df779 (diff) | |
download | common-d3d90dd086e4defaeb6f9bf9d99531aa03e7a7d8.tar.gz |
rust: Add panic handler for the kernel
Adds a Rust panic handler for the Trusty kernel that prints the panic message
and location before calling the kernel panic API.
Bug: 328519718
Test: Manually add panic from Rust in the kernel
Change-Id: I9ed88037f9432d36e8c215994bd8c8f2bc2e9e3e
-rw-r--r-- | kernel/rules.mk | 1 | ||||
-rw-r--r-- | lib/rust_support/bindings.h | 1 | ||||
-rw-r--r-- | lib/rust_support/lib.rs | 44 | ||||
-rw-r--r-- | lib/rust_support/rules.mk | 42 |
4 files changed, 88 insertions, 0 deletions
diff --git a/kernel/rules.mk b/kernel/rules.mk index 433cda8e..e1719a61 100644 --- a/kernel/rules.mk +++ b/kernel/rules.mk @@ -5,6 +5,7 @@ MODULE := $(LOCAL_DIR) MODULE_DEPS := \ lib/debug \ lib/heap \ + lib/rust_support \ trusty/kernel/lib/rand ifeq ($(LK_LIBC_IMPLEMENTATION),lk) diff --git a/lib/rust_support/bindings.h b/lib/rust_support/bindings.h new file mode 100644 index 00000000..4fda8c0c --- /dev/null +++ b/lib/rust_support/bindings.h @@ -0,0 +1 @@ +#include <panic.h> diff --git a/lib/rust_support/lib.rs b/lib/rust_support/lib.rs new file mode 100644 index 00000000..8f6464a3 --- /dev/null +++ b/lib/rust_support/lib.rs @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2024 Google Inc. All rights reserved + * + * 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. + */ + +//! Rust support library for the Trusty kernel + +#![no_std] +#![feature(c_str_literals)] + +use alloc::format; +use core::ffi::CStr; +use core::panic::PanicInfo; + +mod sys { + include!(env!("BINDGEN_INC_FILE")); +} + +#[panic_handler] +fn handle_panic(info: &PanicInfo) -> ! { + let panic_message = format!("{info}\0"); + let panic_message_c = CStr::from_bytes_with_nul(panic_message.as_bytes()) + .expect("Unexpected null byte in panic message"); + // SAFETY: Calling C function with string pointers that outlive the call + unsafe { sys::_panic(c"Rust in Trusty kernel %s\n".as_ptr(), panic_message_c.as_ptr()) } +} diff --git a/lib/rust_support/rules.mk b/lib/rust_support/rules.mk new file mode 100644 index 00000000..2fd8d422 --- /dev/null +++ b/lib/rust_support/rules.mk @@ -0,0 +1,42 @@ +# +# Copyright (c) 2024, Google, Inc. All rights reserved +# +# 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. +# + +LOCAL_DIR := $(GET_LOCAL_DIR) + +MODULE := $(LOCAL_DIR) + +MODULE_CRATE_NAME := rust_support + +MODULE_SRCS := \ + $(LOCAL_DIR)/lib.rs \ + +MODULE_DEPS := \ + trusty/user/base/lib/liballoc-rust \ + trusty/user/base/lib/trusty-std \ + +MODULE_BINDGEN_ALLOW_FUNCTIONS := \ + _panic \ + +MODULE_BINDGEN_SRC_HEADER := $(LOCAL_DIR)/bindings.h + +include make/module.mk |