Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement device-mapper and bio for a simple dm_sworndisk module #19

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@
#![feature(unboxed_closures)]
#![feature(unsize)]

mod error;
pub mod error;
mod layers;
mod os;
mod prelude;
mod tx;
mod util;

extern crate alloc;

pub use layers::bio::{BlockId, BlockSet, Buf, BufMut, BufRef, BLOCK_SIZE};
#[cfg(feature = "linux")]
pub use os::{Arc, Mutex, Vec};
3 changes: 3 additions & 0 deletions core/src/os/linux/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ use crate::{
prelude::{Error, Result},
};

/// Reuse `spawn` and `JoinHandle` in `bindings::thread`.
pub use bindings::thread::{spawn, JoinHandle, Thread};

/// Wrap `alloc::boxed::Box` provided by kernel.
#[repr(transparent)]
pub struct Box<T: ?Sized> {
Expand Down
24 changes: 12 additions & 12 deletions core/src/os/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,34 @@
mod jinux;
#[cfg(feature = "jinux")]
pub use self::jinux::{
Aead, AeadIv, AeadKey, AeadMac, Arc, Box, CurrentThread, HashMap, HashSet, Mutex, MutexGuard,
Pages, Rng, RwLock, RwLockReadGuard, RwLockWriteGuard, Skcipher, SkcipherIv, SkcipherKey,
String, Tid, ToString, Vec, Weak, PAGE_SIZE,
spawn, Aead, AeadIv, AeadKey, AeadMac, Arc, Box, CurrentThread, HashMap, HashSet, JoinHandle,
Mutex, MutexGuard, Pages, Rng, RwLock, RwLockReadGuard, RwLockWriteGuard, Skcipher, SkcipherIv,
SkcipherKey, String, Thread, Tid, ToString, Vec, Weak, PAGE_SIZE,
};

#[cfg(feature = "linux")]
mod linux;
#[cfg(feature = "linux")]
pub use self::linux::{
Aead, AeadIv, AeadKey, AeadMac, Arc, Box, CurrentThread, HashMap, HashSet, Mutex, MutexGuard,
Pages, Rng, RwLock, RwLockReadGuard, RwLockWriteGuard, Skcipher, SkcipherIv, SkcipherKey,
String, Tid, ToString, Vec, Weak, PAGE_SIZE,
spawn, Aead, AeadIv, AeadKey, AeadMac, Arc, Box, CurrentThread, HashMap, HashSet, JoinHandle,
Mutex, MutexGuard, Pages, Rng, RwLock, RwLockReadGuard, RwLockWriteGuard, Skcipher, SkcipherIv,
SkcipherKey, String, Thread, Tid, ToString, Vec, Weak, PAGE_SIZE,
};

#[cfg(all(feature = "occlum", target_env = "sgx"))]
mod occlum;
#[cfg(all(feature = "occlum", target_env = "sgx"))]
pub use self::occlum::{
Aead, AeadIv, AeadKey, AeadMac, Arc, Box, CurrentThread, HashMap, HashSet, Mutex, MutexGuard,
Pages, Rng, RwLock, RwLockReadGuard, RwLockWriteGuard, Skcipher, SkcipherIv, SkcipherKey,
String, Tid, ToString, Vec, Weak, PAGE_SIZE,
spawn, Aead, AeadIv, AeadKey, AeadMac, Arc, Box, CurrentThread, HashMap, HashSet, JoinHandle,
Mutex, MutexGuard, Pages, Rng, RwLock, RwLockReadGuard, RwLockWriteGuard, Skcipher, SkcipherIv,
SkcipherKey, String, Thread, Tid, ToString, Vec, Weak, PAGE_SIZE,
};

#[cfg(feature = "std")]
mod std;
#[cfg(feature = "std")]
pub use self::std::{
Aead, AeadIv, AeadKey, AeadMac, Arc, Box, CurrentThread, HashMap, HashSet, Mutex, MutexGuard,
Pages, Rng, RwLock, RwLockReadGuard, RwLockWriteGuard, Skcipher, SkcipherIv, SkcipherKey,
String, Tid, ToString, Vec, Weak, PAGE_SIZE,
spawn, Aead, AeadIv, AeadKey, AeadMac, Arc, Box, CurrentThread, HashMap, HashSet, JoinHandle,
Mutex, MutexGuard, Pages, Rng, RwLock, RwLockReadGuard, RwLockWriteGuard, Skcipher, SkcipherIv,
SkcipherKey, String, Thread, Tid, ToString, Vec, Weak, PAGE_SIZE,
};
3 changes: 3 additions & 0 deletions core/src/os/std/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ pub use spin::{
Mutex, MutexGuard, RwLock, RwLockReadGuard, RwLockUpgradableGuard, RwLockWriteGuard,
};

/// Reuse `spawn` and `JoinHandle` in `std::thread`.
pub use std::thread::{spawn, JoinHandle, Thread};

/// Reuse `std::thread::ThreadId`.
pub type Tid = std::thread::ThreadId;

Expand Down
4 changes: 4 additions & 0 deletions linux/bindings/src/bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* Sorted alphabetically.
*/

#include <linux/bio.h>
#include <linux/delay.h>
#include <linux/device-mapper.h>
#include <linux/kthread.h>
#include <linux/lockdep_types.h>
#include <linux/refcount.h>
#include <linux/rwsem.h>
37 changes: 37 additions & 0 deletions linux/bindings/src/helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
*/

#include <linux/atomic.h>
#include <linux/bio.h>
#include <linux/refcount.h>
#include <linux/sched/task.h>

refcount_t helper_REFCOUNT_INIT(int n)
{
Expand All @@ -47,3 +49,38 @@ int helper_atomic_fetch_add_release(int i, atomic_t *v)
{
return atomic_fetch_add_release(i, v);
}

struct task_struct *helper_get_task_struct(struct task_struct *t)
{
return get_task_struct(t);
}

void helper_put_task_struct(struct task_struct *t)
{
put_task_struct(t);
}

void helper_bio_get(struct bio *bio)
{
bio_get(bio);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the indentation here four spaces? Seems a little longer than rust codes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The C kernel code uses tabs for indentation.

}

void helper_bio_set_dev(struct bio *bio, struct block_device *bdev)
{
bio_set_dev(bio, bdev);
}

bool helper_bio_has_data(struct bio *bio)
{
return bio_has_data(bio);
}

struct page *helper_virt_to_page(void *addr)
{
return virt_to_page(addr);
}

void *helper_page_to_virt(struct page *page)
{
return page_to_virt(page);
}
22 changes: 20 additions & 2 deletions linux/bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,28 @@
#![feature(allocator_api)]
#![feature(coerce_unsized)]
#![feature(dispatch_from_dyn)]
#![feature(layout_for_ptr)]
#![feature(receiver_trait)]
#![feature(unsize)]

mod bindings_raw {
// Use glob import here to expose all helpers.
// Symbols defined within the module will take precedence to the glob import.
pub use super::bindings_helper::*;
include!("./bindings_generated.rs");
}

// When both a directly exposed symbol and a helper exists for the same function,
// the directly exposed symbol is preferred and the helper becomes dead code, so
// ignore the warning here.
#[allow(dead_code)]
mod bindings_helper {
// Import the generated bindings for types.
use super::bindings_raw::*;
include!("./bindings_helpers_generated.rs");
}

pub mod sync;
pub mod thread;

include!("./bindings_generated.rs");
include!("./bindings_helpers_generated.rs");
pub use bindings_raw::*;
8 changes: 6 additions & 2 deletions linux/bindings/src/sync/arc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use alloc::boxed::Box;
use core::{
alloc::AllocError,
alloc::{AllocError, Allocator, Layout},
fmt,
marker::{PhantomData, Unsize},
mem::MaybeUninit,
Expand Down Expand Up @@ -466,7 +466,11 @@ impl<T: ?Sized> Drop for Weak<T> {
// Weak count reached zero, we must free the memory.
//
// The pointer was initialized from the result of `Box::leak`.
unsafe { drop(Box::from_raw(self.ptr.as_ptr())) };
unsafe {
let arc_inner = Box::from_raw(self.ptr.as_ptr());
let (leaked, alloc) = Box::into_raw_with_allocator(arc_inner);
alloc.deallocate(self.ptr.cast(), Layout::for_value_raw(self.ptr.as_ptr()));
};
}
}

Expand Down
Loading
Loading