aboutsummaryrefslogtreecommitdiff
path: root/src/alloc
diff options
context:
space:
mode:
Diffstat (limited to 'src/alloc')
-rw-r--r--src/alloc/Cargo.toml15
-rw-r--r--src/alloc/alloc.rs77
-rw-r--r--src/alloc/pool.rs13
3 files changed, 105 insertions, 0 deletions
diff --git a/src/alloc/Cargo.toml b/src/alloc/Cargo.toml
new file mode 100644
index 0000000..2c7f4b8
--- /dev/null
+++ b/src/alloc/Cargo.toml
@@ -0,0 +1,15 @@
+[package]
+name = "alloc_system"
+version = "0.1.0"
+description = "Windows Kernel Mode support library."
+
+authors = ["pravic <[email protected]>"]
+license = "MIT"
+
+[lib]
+name = "alloc_system"
+path = "alloc.rs"
+test = false
+
+[dependencies]
+core = { path= "../../../libcore" }
diff --git a/src/alloc/alloc.rs b/src/alloc/alloc.rs
new file mode 100644
index 0000000..963c3e3
--- /dev/null
+++ b/src/alloc/alloc.rs
@@ -0,0 +1,77 @@
+//! Kernel Mode Allocator
+
+// The compiler needs to be instructed that this crate is an allocator in order
+// to realize that when this is linked in another allocator like jemalloc should
+// not be linked in
+#![feature(allocator)]
+#![allocator]
+
+// Allocators are not allowed to depend on the standard library which in turn
+// requires an allocator in order to avoid circular dependencies. This crate,
+// however, can use all of libcore.
+#![no_std]
+
+#![crate_name = "alloc_system"]
+#![crate_type = "rlib"]
+
+mod pool;
+
+// Listed below are the five allocation functions currently required by custom
+// allocators. Their signatures and symbol names are not currently typechecked
+// by the compiler, but this is a future extension and are required to match
+// what is found below.
+//
+// Note that the standard `malloc` and `realloc` functions do not provide a way
+// to communicate alignment so this implementation would need to be improved
+// with respect to alignment in that aspect.
+
+const KMRS_TAG: u32 = 0x4B4D5253; // 'KMRS'
+use pool::{ExAllocatePoolWithTag, ExFreePoolWithTag, POOL_TYPE};
+
+extern "C"
+{
+ // from ntoskrnl
+ pub fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8;
+}
+
+#[no_mangle]
+pub extern "C" fn __rust_allocate(size: usize, _align: usize) -> *mut u8
+{
+ unsafe { ExAllocatePoolWithTag(POOL_TYPE::PagedPool, size, KMRS_TAG) }
+}
+
+#[no_mangle]
+pub extern "C" fn __rust_deallocate(ptr: *mut u8, _old_size: usize, _align: usize)
+{
+ unsafe { ExFreePoolWithTag(ptr, KMRS_TAG) };
+}
+
+#[no_mangle]
+pub extern "C" fn __rust_reallocate(old: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8
+{
+ unsafe {
+ // http://en.cppreference.com/w/c/memory/realloc
+ let minsize = if size < old_size { size } else { old_size };
+ let new = __rust_allocate(size, align);
+ if new.is_null() {
+ return new;
+ }
+ if !old.is_null() && old_size > 0 {
+ memcpy(new, old, minsize);
+ __rust_deallocate(old, old_size, align);
+ }
+ return new;
+ }
+}
+
+#[no_mangle]
+pub extern "C" fn __rust_reallocate_inplace(_ptr: *mut u8, old_size: usize, _size: usize, _align: usize) -> usize
+{
+ old_size // this api is not supported
+}
+
+#[no_mangle]
+pub extern "C" fn __rust_usable_size(size: usize, _align: usize) -> usize
+{
+ size
+}
diff --git a/src/alloc/pool.rs b/src/alloc/pool.rs
new file mode 100644
index 0000000..5b120ac
--- /dev/null
+++ b/src/alloc/pool.rs
@@ -0,0 +1,13 @@
+#[repr(C)]
+pub enum POOL_TYPE
+{
+ PagedPool,
+}
+
+pub type PVOID = *mut u8;
+
+extern "system"
+{
+ pub fn ExAllocatePoolWithTag(PoolType: POOL_TYPE, NumberOfBytes: usize, Tag: u32) -> PVOID;
+ pub fn ExFreePoolWithTag(P: PVOID, Tag: u32);
+}