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 // TODO: replace with `trusty-log` crate once it is `no_std`-compatible
25 
26 use alloc::ffi::CString;
27 use alloc::format;
28 use core::ffi::c_uint;
29 use log::{LevelFilter, Log, Metadata, Record};
30 
31 use crate::init::lk_init_level;
32 use crate::LK_INIT_HOOK;
33 
34 use crate::sys::fflush;
35 use crate::sys::fputs;
36 use crate::sys::lk_stderr;
37 use crate::sys::LK_LOGLEVEL_RUST;
38 
39 static TRUSTY_LOGGER: TrustyKernelLogger = TrustyKernelLogger;
40 
41 pub struct TrustyKernelLogger;
42 
43 impl Log for TrustyKernelLogger {
enabled(&self, _metadata: &Metadata) -> bool44     fn enabled(&self, _metadata: &Metadata) -> bool {
45         true
46     }
47 
log(&self, record: &Record)48     fn log(&self, record: &Record) {
49         if self.enabled(record.metadata()) {
50             let cstr = CString::new(format!("{} - {}\n", record.level(), record.args())).unwrap();
51             // Safety:
52             // The pointer returned by `cstr.as_ptr()` is valid because the lifetime of the
53             // `CString` encompasses the lifetime of the unsafe block.
54             // `lk_stderr()` returns a FILE pointer that is valid or null.
55             unsafe { fputs(cstr.as_ptr(), lk_stderr()) };
56         }
57     }
58 
flush(&self)59     fn flush(&self) {
60         // Safety:
61         // `lk_stderr()` returns a FILE pointer that is valid or null.
62         unsafe { fflush(lk_stderr()) };
63     }
64 }
65 
66 /// Initialize logging for Rust in the kernel
67 ///
68 /// By default, only warnings and errors are logged (even in debug builds).
69 ///
70 /// The log level (`LK_LOGLEVEL_RUST`) is controlled by these make variables:
71 /// - `LOG_LEVEL_KERNEL_RUST` if set,
72 /// - `LOG_LEVEL_KERNEL` if set, and
73 /// - `DEBUG` otherwise.
74 ///
75 /// Values below (above) expected values sets the log level to off (trace).
kernel_log_init_func(_level: c_uint)76 extern "C" fn kernel_log_init_func(_level: c_uint) {
77     log::set_logger(&TRUSTY_LOGGER).unwrap();
78     // Level or LevelFilter cannot be created directly from integers
79     // https://github.com/rust-lang/log/issues/460
80     //
81     // bindgen emits `LK_LOGLEVEL_RUST` as `u32` when the value is
82     // a positive integer and omits it otherwise thus causing the
83     // build to fail.
84     log::set_max_level(match LK_LOGLEVEL_RUST {
85         0 => LevelFilter::Off,
86         1 => LevelFilter::Error,
87         2 => LevelFilter::Warn, // the default for Trusty
88         3 => LevelFilter::Info,
89         4 => LevelFilter::Debug,
90         _ => LevelFilter::Trace, // enable trace! at 5+
91     });
92 }
93 
94 LK_INIT_HOOK!(kernel_log_init, kernel_log_init_func, lk_init_level::LK_INIT_LEVEL_HEAP);
95