Skip to content

Commit

Permalink
apply coding style
Browse files Browse the repository at this point in the history
Taken from Documentation/process/coding-style.rst

Signed-off-by: Finn Behrens <me@kloenk.de>
  • Loading branch information
kloenk committed Oct 5, 2020
1 parent c17d290 commit beb841f
Show file tree
Hide file tree
Showing 18 changed files with 1,387 additions and 1,066 deletions.
44 changes: 24 additions & 20 deletions drivers/char/rust_example/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

use kernel::prelude::*;

module!{
module! {
type: RustExample,
name: b"rust_example",
author: b"Rust for Linux Contributors",
Expand All @@ -25,27 +25,31 @@ module!{
},
}

struct RustExample {
message: String,
struct RustExample
{
message: String,
}

impl KernelModule for RustExample {
fn init() -> KernelResult<Self> {
println!("Rust Example (init)");
println!("Am I built-in? {}", !cfg!(MODULE));
println!("Parameters:");
println!(" my_bool: {}", my_bool.read());
println!(" my_i32: {}", my_i32.read());
Ok(RustExample {
message: "on the heap!".to_owned(),
})
}
impl KernelModule for RustExample
{
fn init() -> KernelResult<Self>
{
println!("Rust Example (init)");
println!("Am I built-in? {}", !cfg!(MODULE));
println!("Parameters:");
println!(" my_bool: {}", my_bool.read());
println!(" my_i32: {}", my_i32.read());
Ok(RustExample {
message: "on the heap!".to_owned(),
})
}
}

impl Drop for RustExample {
fn drop(&mut self) {
println!("My message is {}", self.message);
println!("Rust Example (exit)");
}
impl Drop for RustExample
{
fn drop(&mut self)
{
println!("My message is {}", self.message);
println!("Rust Example (exit)");
}
}

227 changes: 118 additions & 109 deletions rust/kernel/build.rs
Original file line number Diff line number Diff line change
@@ -1,135 +1,144 @@
// SPDX-License-Identifier: GPL-2.0

use std::path::PathBuf;
use std::env;
use std::path::PathBuf;

const INCLUDED_TYPES: &[&str] = &["file_system_type", "mode_t", "umode_t", "ctl_table"];
const INCLUDED_TYPES: &[&str] =
&["file_system_type", "mode_t", "umode_t", "ctl_table"];
const INCLUDED_FUNCTIONS: &[&str] = &[
"cdev_add",
"cdev_init",
"cdev_del",
"register_filesystem",
"unregister_filesystem",
"krealloc",
"kfree",
"mount_nodev",
"kill_litter_super",
"register_sysctl",
"unregister_sysctl_table",
"access_ok",
"_copy_to_user",
"_copy_from_user",
"alloc_chrdev_region",
"unregister_chrdev_region",
"wait_for_random_bytes",
"get_random_bytes",
"rng_is_initialized",
"printk",
"add_device_randomness",
"cdev_add",
"cdev_init",
"cdev_del",
"register_filesystem",
"unregister_filesystem",
"krealloc",
"kfree",
"mount_nodev",
"kill_litter_super",
"register_sysctl",
"unregister_sysctl_table",
"access_ok",
"_copy_to_user",
"_copy_from_user",
"alloc_chrdev_region",
"unregister_chrdev_region",
"wait_for_random_bytes",
"get_random_bytes",
"rng_is_initialized",
"printk",
"add_device_randomness",
];
const INCLUDED_VARS: &[&str] = &[
"EINVAL",
"ENOMEM",
"ESPIPE",
"EFAULT",
"EAGAIN",
"__this_module",
"FS_REQUIRES_DEV",
"FS_BINARY_MOUNTDATA",
"FS_HAS_SUBTYPE",
"FS_USERNS_MOUNT",
"FS_RENAME_DOES_D_MOVE",
"BINDINGS_GFP_KERNEL",
"KERN_INFO",
"VERIFY_WRITE",
"LINUX_VERSION_CODE",
"SEEK_SET",
"SEEK_CUR",
"SEEK_END",
"O_NONBLOCK",
"param_ops_bool",
"param_ops_int",
"EINVAL",
"ENOMEM",
"ESPIPE",
"EFAULT",
"EAGAIN",
"__this_module",
"FS_REQUIRES_DEV",
"FS_BINARY_MOUNTDATA",
"FS_HAS_SUBTYPE",
"FS_USERNS_MOUNT",
"FS_RENAME_DOES_D_MOVE",
"BINDINGS_GFP_KERNEL",
"KERN_INFO",
"VERIFY_WRITE",
"LINUX_VERSION_CODE",
"SEEK_SET",
"SEEK_CUR",
"SEEK_END",
"O_NONBLOCK",
"param_ops_bool",
"param_ops_int",
];
const OPAQUE_TYPES: &[&str] = &[
// These need to be opaque because they're both packed and aligned, which rustc
// doesn't support yet. See https://github.com/rust-lang/rust/issues/59154
// and https://github.com/rust-lang/rust-bindgen/issues/1538
"desc_struct",
"xregs_state",
// These need to be opaque because they're both packed and aligned, which rustc
// doesn't support yet. See https://github.com/rust-lang/rust/issues/59154
// and https://github.com/rust-lang/rust-bindgen/issues/1538
"desc_struct",
"xregs_state",
];

// Takes the CFLAGS from the kernel Makefile and changes all the include paths to be absolute
// instead of relative.
fn prepare_cflags(cflags: &str, kernel_dir: &str) -> Vec<String> {
let cflag_parts = shlex::split(&cflags).unwrap();
let mut cflag_iter = cflag_parts.iter();
let mut kernel_args = vec![];
while let Some(arg) = cflag_iter.next() {
// TODO: bindgen complains
if arg.starts_with("-Wp,-MMD") {
continue;
}
fn prepare_cflags(cflags: &str, kernel_dir: &str) -> Vec<String>
{
let cflag_parts = shlex::split(&cflags).unwrap();
let mut cflag_iter = cflag_parts.iter();
let mut kernel_args = vec![];
while let Some(arg) = cflag_iter.next() {
// TODO: bindgen complains
if arg.starts_with("-Wp,-MMD") {
continue;
}

if arg.starts_with("-I") && !arg.starts_with("-I/") {
kernel_args.push(format!("-I{}/{}", kernel_dir, &arg[2..]));
} else if arg == "-include" {
kernel_args.push(arg.to_string());
let include_path = cflag_iter.next().unwrap();
if include_path.starts_with('/') {
kernel_args.push(include_path.to_string());
} else {
kernel_args.push(format!("{}/{}", kernel_dir, include_path));
}
} else {
kernel_args.push(arg.to_string());
if arg.starts_with("-I") && !arg.starts_with("-I/") {
kernel_args.push(format!(
"-I{}/{}",
kernel_dir,
&arg[2..]
));
} else if arg == "-include" {
kernel_args.push(arg.to_string());
let include_path = cflag_iter.next().unwrap();
if include_path.starts_with('/') {
kernel_args.push(include_path.to_string());
} else {
kernel_args.push(format!(
"{}/{}",
kernel_dir, include_path
));
}
} else {
kernel_args.push(arg.to_string());
}
}
}
kernel_args
kernel_args
}

fn main() {
println!("cargo:rerun-if-env-changed=CC");
println!("cargo:rerun-if-env-changed=RUST_BINDGEN_CFLAGS");
fn main()
{
println!("cargo:rerun-if-env-changed=CC");
println!("cargo:rerun-if-env-changed=RUST_BINDGEN_CFLAGS");

let kernel_dir = "../../";
let cflags = env::var("RUST_BINDGEN_CFLAGS")
.expect("Must be invoked from kernel makefile");
let kernel_dir = "../../";
let cflags = env::var("RUST_BINDGEN_CFLAGS")
.expect("Must be invoked from kernel makefile");

let kernel_args = prepare_cflags(&cflags, &kernel_dir);
let kernel_args = prepare_cflags(&cflags, &kernel_dir);

let target = env::var("TARGET").unwrap();
let target = env::var("TARGET").unwrap();

let mut builder = bindgen::Builder::default()
.use_core()
.ctypes_prefix("c_types")
.derive_default(true)
.size_t_is_usize(true)
.rustfmt_bindings(true);
let mut builder = bindgen::Builder::default()
.use_core()
.ctypes_prefix("c_types")
.derive_default(true)
.size_t_is_usize(true)
.rustfmt_bindings(true);

builder = builder.clang_arg(format!("--target={}", target));
for arg in kernel_args.iter() {
builder = builder.clang_arg(arg.clone());
}
builder = builder.clang_arg(format!("--target={}", target));
for arg in kernel_args.iter() {
builder = builder.clang_arg(arg.clone());
}

println!("cargo:rerun-if-changed=src/bindings_helper.h");
builder = builder.header("src/bindings_helper.h");
println!("cargo:rerun-if-changed=src/bindings_helper.h");
builder = builder.header("src/bindings_helper.h");

for t in INCLUDED_TYPES {
builder = builder.whitelist_type(t);
}
for f in INCLUDED_FUNCTIONS {
builder = builder.whitelist_function(f);
}
for v in INCLUDED_VARS {
builder = builder.whitelist_var(v);
}
for t in OPAQUE_TYPES {
builder = builder.opaque_type(t);
}
let bindings = builder.generate().expect("Unable to generate bindings");
for t in INCLUDED_TYPES {
builder = builder.whitelist_type(t);
}
for f in INCLUDED_FUNCTIONS {
builder = builder.whitelist_function(f);
}
for v in INCLUDED_VARS {
builder = builder.whitelist_var(v);
}
for t in OPAQUE_TYPES {
builder = builder.opaque_type(t);
}
let bindings = builder.generate().expect("Unable to generate bindings");

let out_path = PathBuf::from("src/bindings_gen.rs");
bindings
.write_to_file(out_path)
.expect("Couldn't write bindings!");
let out_path = PathBuf::from("src/bindings_gen.rs");
bindings.write_to_file(out_path)
.expect("Couldn't write bindings!");
}
30 changes: 19 additions & 11 deletions rust/kernel/src/allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,27 @@ use crate::c_types;

pub struct KernelAllocator;

unsafe impl GlobalAlloc for KernelAllocator {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
// krealloc is used instead of kmalloc because kmalloc is an inline function and can't be
// bound to as a result
bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8
}
unsafe impl GlobalAlloc for KernelAllocator
{
unsafe fn alloc(&self, layout: Layout) -> *mut u8
{
// krealloc is used instead of kmalloc because kmalloc is an inline function and can't be
// bound to as a result
bindings::krealloc(
ptr::null(),
layout.size(),
bindings::GFP_KERNEL,
) as *mut u8
}

unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
bindings::kfree(ptr as *const c_types::c_void);
}
unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout)
{
bindings::kfree(ptr as *const c_types::c_void);
}
}

#[alloc_error_handler]
fn oom(_layout: Layout) -> ! {
panic!("Out of memory!");
fn oom(_layout: Layout) -> !
{
panic!("Out of memory!");
}
17 changes: 9 additions & 8 deletions rust/kernel/src/bindings.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
// SPDX-License-Identifier: GPL-2.0

#[allow(
clippy::all,
non_camel_case_types,
non_upper_case_globals,
non_snake_case,
improper_ctypes
clippy::all,
non_camel_case_types,
non_upper_case_globals,
non_snake_case,
improper_ctypes
)]
mod bindings_raw {
use crate::c_types;
include!("bindings_gen.rs");
mod bindings_raw
{
use crate::c_types;
include!("bindings_gen.rs");
}
pub use bindings_raw::*;

Expand Down
Loading

0 comments on commit beb841f

Please sign in to comment.