-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.rs
75 lines (62 loc) · 1.92 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
//! A kernel module
#![no_std]
#![feature(allocator_api, global_asm)]
use alloc::boxed::Box;
use core::{ptr, slice};
use kernel::prelude::*;
module! {
type: KmallocBox,
name: b"kmalloc_box",
author: b"[email protected]",
description: b"A kernel module",
license: b"Dual MIT/GPL",
}
struct KAllocator;
unsafe impl alloc::alloc::Allocator for KAllocator {
fn allocate(
&self,
layout: alloc::alloc::Layout,
) -> core::result::Result<ptr::NonNull<[u8]>, alloc::alloc::AllocError> {
use kernel::bindings;
let size = layout.size();
let kmalloc_ptr = unsafe { bindings::__kmalloc(size, bindings::GFP_KERNEL) as *mut u8 };
pr_info!(
"__kmalloc({}, GFP_KERNEL) returned {:?}\n",
size,
kmalloc_ptr
);
let slab = unsafe { slice::from_raw_parts_mut(kmalloc_ptr, size) };
let alloc_ptr = slab as *mut [u8];
ptr::NonNull::new(alloc_ptr).ok_or(alloc::alloc::AllocError)
}
unsafe fn deallocate(&self, ptr: ptr::NonNull<u8>, _layout: alloc::alloc::Layout) {
use kernel::{bindings, c_types};
pr_info!("kfree({:?})\n", ptr);
unsafe { bindings::kfree(ptr.as_ptr() as *const c_types::c_void) };
}
}
struct KmallocBox {
buf: Option<Box<[u8; 1024], KAllocator>>,
}
impl KmallocBox {
fn new() -> Self {
KmallocBox { buf: None }
}
fn run(&mut self) -> Result<()> {
let buf: [u8; 1024] = [0; 1024];
let mut boxed_buf = Box::try_new_in(buf, KAllocator)?;
for b in boxed_buf.iter_mut() {
*b = 'm' as u8;
}
// Will be freed on KmallocBox drop
self.buf = Some(boxed_buf);
Ok(())
}
}
impl KernelModule for KmallocBox {
fn init(_name: &'static CStr, _module: &'static ThisModule) -> Result<Self> {
let mut kmalloc_box = KmallocBox::new();
kmalloc_box.run()?;
Ok(kmalloc_box)
}
}