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

Commit e281aa8

Browse files
authored
Merge branch 'master' into no-more-rust-target-path
2 parents 3a35fab + 2349935 commit e281aa8

38 files changed

+432
-275
lines changed

.travis.yml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
sudo: required
1+
matrix:
2+
include:
3+
- dist: xenial
24

35
language: rust
46
rust:
57
- nightly
68
cache:
79
- cargo: true
8-
- directories:
9-
- tests/target/
1010

1111
branches:
1212
only:
@@ -21,10 +21,16 @@ install:
2121
else
2222
cargo install --force cargo-xbuild
2323
fi
24-
- rustup component add rust-src
24+
- rustup component add rust-src rustfmt
2525

2626
script:
27-
- RUST_BACKTRACE=1 ./tests/run_tests.py
27+
- ./tests/run_tests.py
28+
- |
29+
for p in . hello-world tests/*; do
30+
if [ -d "$p" ]; then
31+
(cd "$p" && cargo fmt --all -- --check) || exit 1
32+
fi
33+
done
2834
2935
after_failure:
3036
- dmesg

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "linux-kernel-module"
33
version = "0.1.0"
44
authors = ["Alex Gaynor <[email protected]>"]
5+
edition = "2018"
56

67
[dependencies]
78
bitflags = "1"

build.rs

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
extern crate bindgen;
2-
extern crate cc;
3-
extern crate shlex;
1+
use bindgen;
2+
use cc;
3+
use shlex;
44

55
use std::env;
66
use std::path::PathBuf;
@@ -19,6 +19,8 @@ const INCLUDED_FUNCTIONS: &[&str] = &[
1919
"access_ok",
2020
"_copy_to_user",
2121
"_copy_from_user",
22+
"alloc_chrdev_region",
23+
"unregister_chrdev_region",
2224
];
2325
const INCLUDED_VARS: &[&str] = &[
2426
"EINVAL",
@@ -34,15 +36,16 @@ const INCLUDED_VARS: &[&str] = &[
3436
"KERN_INFO",
3537
"VERIFY_WRITE",
3638
];
39+
const OPAQUE_TYPES: &[&str] = &[
40+
// These need to be opaque because they're both packed and aligned, which rustc
41+
// doesn't support yet. See https://github.com/rust-lang/rust/issues/59154
42+
// and https://github.com/rust-lang/rust-bindgen/issues/1538
43+
"desc_struct",
44+
"xregs_state",
45+
];
3746

3847
fn main() {
39-
let mut builder = bindgen::Builder::default()
40-
.use_core()
41-
.ctypes_prefix("c_types")
42-
.no_copy(".*")
43-
.derive_default(true)
44-
.rustfmt_bindings(true);
45-
48+
println!("rerun-if-env-changed=KDIR");
4649
let output = String::from_utf8(
4750
Command::new("make")
4851
.arg("-C")
@@ -51,8 +54,16 @@ fn main() {
5154
.output()
5255
.unwrap()
5356
.stdout,
54-
).unwrap();
57+
)
58+
.unwrap();
5559

60+
let mut builder = bindgen::Builder::default()
61+
.use_core()
62+
.ctypes_prefix("c_types")
63+
.derive_default(true)
64+
.rustfmt_bindings(true);
65+
66+
builder = builder.clang_arg("--target=x86_64-linux-kernel-module");
5667
for arg in shlex::split(&output).unwrap() {
5768
builder = builder.clang_arg(arg.to_string());
5869
}
@@ -69,6 +80,9 @@ fn main() {
6980
for v in INCLUDED_VARS {
7081
builder = builder.whitelist_var(v);
7182
}
83+
for t in OPAQUE_TYPES {
84+
builder = builder.opaque_type(t);
85+
}
7286
let bindings = builder.generate().expect("Unable to generate bindings");
7387

7488
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());
@@ -79,6 +93,7 @@ fn main() {
7993
let mut builder = cc::Build::new();
8094
println!("cargo:rerun-if-env-changed=CLANG");
8195
builder.compiler(env::var("CLANG").unwrap_or("clang".to_string()));
96+
builder.target("x86_64-linux-kernel-module");
8297
builder.warnings(false);
8398
builder.file("src/helpers.c");
8499
for arg in shlex::split(&output).unwrap() {

example-sysctl/Cargo.toml

Lines changed: 0 additions & 10 deletions
This file was deleted.

example-sysctl/Makefile

Lines changed: 0 additions & 9 deletions
This file was deleted.

example-sysctl/src/lib.rs

Lines changed: 0 additions & 41 deletions
This file was deleted.

hello-world/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
name = "hello-world"
33
version = "0.1.0"
44
authors = ["Geoffrey Thomas <[email protected]>"]
5+
edition = "2018"
56

67
[lib]
78
crate-type = ["staticlib"]

hello-world/Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
obj-m := helloworld.o
22
helloworld-objs := target/x86_64-linux-kernel-module/debug/libhello_world.a
33
EXTRA_LDFLAGS += --entry=init_module
4+
KDIR ?= /lib/modules/$(shell uname -r)/build
45

56
all:
6-
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURDIR)
7+
$(MAKE) -C $(KDIR) M=$(CURDIR)
78

89
clean:
9-
$(MAKE) -C /lib/modules/$(shell uname -r)/build M=$(CURDIR) clean
10+
$(MAKE) -C $(KDIR) M=$(CURDIR) clean

hello-world/src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
#![no_std]
2-
#![feature(alloc)]
2+
#![feature(const_str_as_bytes)]
33

44
extern crate alloc;
5+
56
use alloc::borrow::ToOwned;
6-
use alloc::String;
7+
use alloc::string::String;
78

8-
#[macro_use]
9-
extern crate linux_kernel_module;
9+
use linux_kernel_module;
10+
use linux_kernel_module::println;
1011

1112
struct HelloWorldModule {
1213
message: String,
@@ -27,7 +28,8 @@ impl Drop for HelloWorldModule {
2728
println!("Goodbye kernel module!");
2829
}
2930
}
30-
kernel_module!(
31+
32+
linux_kernel_module::kernel_module!(
3133
HelloWorldModule,
3234
author: "Alex Gaynor and Geoffrey Thomas",
3335
description: "An extremely simple kernel module",

src/allocator.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
use core::alloc::{AllocErr, GlobalAlloc, Layout};
1+
use core::alloc::{GlobalAlloc, Layout};
2+
use core::ptr;
23

3-
use bindings;
4-
use c_types;
4+
use crate::bindings;
5+
use crate::c_types;
56

67
pub struct KernelAllocator;
78

89
unsafe impl GlobalAlloc for KernelAllocator {
910
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
1011
// krealloc is used instead of kmalloc because kmalloc is an inline function and can't be
1112
// bound to as a result
12-
return bindings::krealloc(
13-
0 as *const c_types::c_void,
14-
layout.size(),
15-
bindings::GFP_KERNEL,
16-
) as *mut u8;
13+
return bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8;
1714
}
1815

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

24-
#[lang = "oom"]
25-
extern "C" fn oom(_err: AllocErr) -> ! {
21+
#[alloc_error_handler]
22+
fn oom(_layout: Layout) -> ! {
2623
panic!("Out of memory!");
2724
}

src/bindings.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![allow(non_camel_case_types, non_upper_case_globals, non_snake_case, improper_ctypes)]
22

3-
use c_types;
3+
use crate::c_types;
44

55
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
66

src/bindings_helper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#include <linux/module.h>
21
#include <linux/fs.h>
2+
#include <linux/module.h>
33
#include <linux/slab.h>
44
#include <linux/uaccess.h>
55

src/chrdev.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
use core::convert::TryInto;
2+
use core::ops::Range;
3+
4+
use crate::bindings;
5+
use crate::c_types;
6+
use crate::error;
7+
8+
pub fn builder(name: &'static str, minors: Range<u16>) -> error::KernelResult<Builder> {
9+
if !name.ends_with('\x00') {
10+
return Err(error::Error::EINVAL);
11+
}
12+
13+
return Ok(Builder { name, minors });
14+
}
15+
16+
pub struct Builder {
17+
name: &'static str,
18+
minors: Range<u16>,
19+
}
20+
21+
impl Builder {
22+
pub fn build(self) -> error::KernelResult<Registration> {
23+
let mut dev: bindings::dev_t = 0;
24+
let res = unsafe {
25+
bindings::alloc_chrdev_region(
26+
&mut dev,
27+
self.minors.start.into(),
28+
self.minors.len().try_into()?,
29+
self.name.as_ptr() as *const c_types::c_char,
30+
)
31+
};
32+
if res != 0 {
33+
return Err(error::Error::from_kernel_errno(res));
34+
}
35+
return Ok(Registration {
36+
dev,
37+
count: self.minors.len(),
38+
});
39+
}
40+
}
41+
42+
pub struct Registration {
43+
dev: bindings::dev_t,
44+
count: usize,
45+
}
46+
47+
impl Drop for Registration {
48+
fn drop(&mut self) {
49+
unsafe {
50+
bindings::unregister_chrdev_region(self.dev, self.count as _);
51+
}
52+
}
53+
}

src/error.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
use bindings;
2-
use c_types;
1+
use core::num::TryFromIntError;
2+
3+
use crate::bindings;
4+
use crate::c_types;
35

46
pub struct Error(c_types::c_int);
57

@@ -17,4 +19,10 @@ impl Error {
1719
}
1820
}
1921

22+
impl From<TryFromIntError> for Error {
23+
fn from(_: TryFromIntError) -> Error {
24+
return Error::EINVAL;
25+
}
26+
}
27+
2028
pub type KernelResult<T> = Result<T, Error>;

src/filesystem.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ use core::default::Default;
33
use core::marker;
44
use core::mem;
55

6-
use bindings;
7-
use c_types;
8-
use error;
6+
use bitflags;
7+
8+
use crate::bindings;
9+
use crate::c_types;
10+
use crate::error;
911

1012
pub struct FileSystemRegistration<T: FileSystem> {
1113
_phantom: marker::PhantomData<T>,
@@ -24,7 +26,7 @@ pub trait FileSystem {
2426
const FLAGS: FileSystemFlags;
2527
}
2628

27-
bitflags! {
29+
bitflags::bitflags! {
2830
pub struct FileSystemFlags: c_types::c_int {
2931
const FS_REQUIRES_DEV = bindings::FS_REQUIRES_DEV as c_types::c_int;
3032
const FS_BINARY_MOUNTDATA = bindings::FS_BINARY_MOUNTDATA as c_types::c_int;
@@ -34,12 +36,6 @@ bitflags! {
3436
}
3537
}
3638

37-
impl FileSystemFlags {
38-
pub const fn const_empty() -> FileSystemFlags {
39-
FileSystemFlags { bits: 0 }
40-
}
41-
}
42-
4339
extern "C" fn fill_super_callback<T: FileSystem>(
4440
_sb: *mut bindings::super_block,
4541
_data: *mut c_types::c_void,

0 commit comments

Comments
 (0)