From 0542277e313ef316a436610f6840dc9a62fc6dac Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Mon, 5 Feb 2024 21:55:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=86=85=E5=AD=98allocate?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=9C=89=E5=8F=AF=E8=83=BD=E8=AE=BF=E9=97=AE?= =?UTF-8?q?=E6=9C=AA=E7=9F=A5=E5=86=85=E5=AD=98=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Cargo.toml | 9 --------- include/kernel/memm.h | 4 ++-- src/kernel/memm/memm.c | 3 ++- src/kernel/memm/memm.rs | 14 ++++++++++++-- src/metaverse.lds | 4 ++++ 5 files changed, 20 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f89fb5e..d2d3873 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,14 +6,5 @@ edition = "2021" # 此Cargo.toml仅用于rust-analyzer识别rust部分的代码 # 不应使用cargo编译 -[build] -type = "staticlib" -rustflags = [ - "-C", "embed-bitcode=no", - "-C", "code-model=large", - "-C", "relocation-model=static", - "-L", "crate=${PWD}/../rustlib/${ARCH}/src/" -] - [target.'cfg(target_arch = "x86_64")'] target = "x86_64-unknown-none" diff --git a/include/kernel/memm.h b/include/kernel/memm.h index d3f730f..f4714ae 100644 --- a/include/kernel/memm.h +++ b/include/kernel/memm.h @@ -139,9 +139,9 @@ allocator对象在进程与内核之间传递时一律使用内核空间的映 */ void *memm_allocate(usize size, usize pid); #define memm_addr_set_allocator(mem, allocator) \ - *(allocator_t **)(mem - 16) = allocator; + *(allocator_t **)((void *)(mem) - 16) = allocator; #define memm_addr_get_allocator(mem) \ - ((*(allocator_t **)(mem - 16))) + ((*(allocator_t **)((void *)(mem) - 16))) void *memm_kernel_allocate(usize size); diff --git a/src/kernel/memm/memm.c b/src/kernel/memm/memm.c index 2b152e6..787befa 100644 --- a/src/kernel/memm/memm.c +++ b/src/kernel/memm/memm.c @@ -200,7 +200,8 @@ void *memm_allocate(usize size, usize pid) ptr = new_allocator->allocate(&new_allocator->allocator_instance, orgsize, 0); after_allocation: - memm_addr_set_allocator(ptr, allocator); + if (ptr != nullptr) + memm_addr_set_allocator(ptr, allocator); return ptr; } diff --git a/src/kernel/memm/memm.rs b/src/kernel/memm/memm.rs index 3995400..fea17bf 100644 --- a/src/kernel/memm/memm.rs +++ b/src/kernel/memm/memm.rs @@ -1,6 +1,9 @@ extern crate core; -use core::alloc::{GlobalAlloc, Layout}; +use core::{ + alloc::{GlobalAlloc, Layout}, + ptr::null_mut, +}; extern "C" { pub fn memm_kernel_allocate(size: usize) -> *mut u8; @@ -12,7 +15,14 @@ pub struct KernelAllocator {} unsafe impl GlobalAlloc for KernelAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - memm_kernel_allocate(layout.size()) + let res = memm_kernel_allocate(layout.size()); + if res == null_mut() { + panic!( + "Kernel allocator failed to allocate {} byte(s) memory.", + layout.size() + ); + } + res } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { diff --git a/src/metaverse.lds b/src/metaverse.lds index 3ef868d..87da7f2 100644 --- a/src/metaverse.lds +++ b/src/metaverse.lds @@ -96,6 +96,10 @@ SECTIONS { { *(.debug_pubtypes) } + .debug_frame : + { + *(.debug_frame) + } .kend : { *(.kend)