1 /*
2  * Copyright (c) 2024 Google Inc. All rights reserved
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files
6  * (the "Software"), to deal in the Software without restriction,
7  * including without limitation the rights to use, copy, modify, merge,
8  * publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so,
10  * subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 //! Rust support library for the Trusty kernel
25 
26 #![no_std]
27 #![feature(cfg_version)]
28 // C string literals were stabilized in Rust 1.77
29 #![cfg_attr(not(version("1.77")), feature(c_str_literals))]
30 #![deny(unsafe_op_in_unsafe_fn)]
31 
32 use alloc::format;
33 use core::ffi::CStr;
34 use core::panic::PanicInfo;
35 
36 mod sys {
37     #![allow(clippy::upper_case_acronyms)]
38     #![allow(unused)]
39     #![allow(non_camel_case_types)]
40     #![allow(non_upper_case_globals)]
41     use num_derive::FromPrimitive;
42     include!(env!("BINDGEN_INC_FILE"));
43 }
44 
45 pub mod err;
46 pub mod handle;
47 pub mod handle_set;
48 pub mod init;
49 pub mod ipc;
50 pub mod log;
51 pub mod macros;
52 pub mod mmu;
53 pub mod sync;
54 pub mod thread;
55 pub mod vmm;
56 
57 pub use sys::paddr_t;
58 pub use sys::status_t;
59 pub use sys::vaddr_t;
60 pub use sys::Error;
61 
62 // NOTE: `INFINITE_TIME` is defined in `lk/types.h` as `UINT32_MAX`,
63 // which in turn is defined as `UINT_MAX`, which is not recognized
64 // by bindgen according to the bug below so we use `u32::MAX`.
65 // See <https://github.com/rust-lang/rust-bindgen/issues/1636>.
66 pub const INFINITE_TIME: u32 = u32::MAX;
67 
68 #[panic_handler]
handle_panic(info: &PanicInfo) -> !69 fn handle_panic(info: &PanicInfo) -> ! {
70     let panic_message = format!("{info}\0");
71     let panic_message_c = CStr::from_bytes_with_nul(panic_message.as_bytes())
72         .expect("Unexpected null byte in panic message");
73     // SAFETY: Calling C function with string pointers that outlive the call
74     unsafe { sys::_panic(c"Rust in Trusty kernel %s\n".as_ptr(), panic_message_c.as_ptr()) }
75 }
76