Skip to content
This repository has been archived by the owner on Mar 7, 2021. It is now read-only.

Commit

Permalink
fixes #263 -- remove printk indirection (#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
alex authored Aug 29, 2020
1 parent d9a3d83 commit 3ad884e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 12 deletions.
1 change: 1 addition & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const INCLUDED_FUNCTIONS: &[&str] = &[
"wait_for_random_bytes",
"get_random_bytes",
"rng_is_initialized",
"printk",
"add_device_randomness",
];
const INCLUDED_VARS: &[&str] = &[
Expand Down
5 changes: 0 additions & 5 deletions src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,6 @@
#include <linux/version.h>


int printk_helper(const unsigned char *s, int len)
{
return printk(KERN_INFO "%.*s", len, (const char *)s);
}

void bug_helper(void)
{
BUG();
Expand Down
15 changes: 8 additions & 7 deletions src/printk.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use core::cmp;
use core::fmt;

use crate::bindings;
use crate::c_types::c_int;

pub struct KernelConsole;

extern "C" {
fn printk_helper(s: *const u8, len: c_int) -> c_int;
}

#[doc(hidden)]
pub fn printk(s: &[u8]) {
// Don't copy the trailing NUL from `KERN_INFO`.
let mut fmt_str = [0; bindings::KERN_INFO.len() - 1 + b"%.*s\0".len()];
fmt_str[..bindings::KERN_INFO.len() - 1]
.copy_from_slice(&bindings::KERN_INFO[..bindings::KERN_INFO.len() - 1]);
fmt_str[bindings::KERN_INFO.len() - 1..].copy_from_slice(b"%.*s\0");

// TODO: I believe printk never fails
unsafe { printk_helper(s.as_ptr(), s.len() as c_int) };
unsafe { bindings::printk(fmt_str.as_ptr() as _, s.len() as c_int, s.as_ptr()) };
}

// From kernel/print/printk.c
Expand Down

0 comments on commit 3ad884e

Please sign in to comment.