Skip to content

Commit

Permalink
Merge pull request #669 from ojeda/printk
Browse files Browse the repository at this point in the history
rust: kernel: support `!CONFIG_PRINTK`
  • Loading branch information
ojeda authored Feb 11, 2022
2 parents 5bded08 + 47d226f commit b5d158b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
7 changes: 6 additions & 1 deletion rust/kernel/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use crate::{clk::Clk, error::from_kernel_err_ptr};

use crate::{
bindings, c_str, c_types,
bindings,
revocable::{Revocable, RevocableGuard},
str::CStr,
sync::{NeedsLockClass, RevocableMutex, RevocableMutexGuard, UniqueRef},
Expand All @@ -20,6 +20,9 @@ use core::{
pin::Pin,
};

#[cfg(CONFIG_PRINTK)]
use crate::{c_str, c_types};

/// A raw device.
///
/// # Safety
Expand Down Expand Up @@ -138,10 +141,12 @@ pub unsafe trait RawDevice {
///
/// Callers must ensure that `klevel` is null-terminated; in particular, one of the
/// `KERN_*`constants, for example, `KERN_CRIT`, `KERN_ALERT`, etc.
#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
unsafe fn printk(&self, klevel: &[u8], msg: fmt::Arguments<'_>) {
// SAFETY: `klevel` is null-terminated and one of the kernel constants. `self.raw_device`
// is valid because `self` is valid. The "%pA" format string expects a pointer to
// `fmt::Arguments`, which is what we're passing as the last argument.
#[cfg(CONFIG_PRINTK)]
unsafe {
bindings::_dev_printk(
klevel as *const _ as *const c_types::c_char,
Expand Down
13 changes: 11 additions & 2 deletions rust/kernel/print.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
use core::fmt;

use crate::c_types::{c_char, c_void};
use crate::{bindings, str::Formatter};
use crate::{
c_types::{c_char, c_void},
str::Formatter,
};

#[cfg(CONFIG_PRINTK)]
use crate::bindings;

// Called from `vsprintf` with format specifier `%pA`.
#[no_mangle]
Expand Down Expand Up @@ -93,12 +98,14 @@ pub mod format_strings {
///
/// [`_printk`]: ../../../../include/linux/_printk.h
#[doc(hidden)]
#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
pub unsafe fn call_printk(
format_string: &[u8; format_strings::LENGTH],
module_name: &[u8],
args: fmt::Arguments<'_>,
) {
// `_printk` does not seem to fail in any path.
#[cfg(CONFIG_PRINTK)]
unsafe {
bindings::_printk(
format_string.as_ptr() as _,
Expand All @@ -114,10 +121,12 @@ pub unsafe fn call_printk(
///
/// [`_printk`]: ../../../../include/linux/printk.h
#[doc(hidden)]
#[cfg_attr(not(CONFIG_PRINTK), allow(unused_variables))]
pub fn call_printk_cont(args: fmt::Arguments<'_>) {
// `_printk` does not seem to fail in any path.
//
// SAFETY: The format string is fixed.
#[cfg(CONFIG_PRINTK)]
unsafe {
bindings::_printk(
format_strings::CONT.as_ptr() as _,
Expand Down

0 comments on commit b5d158b

Please sign in to comment.