From 831c7a689f6968386fa8190f27c55e5e8c55bbc2 Mon Sep 17 00:00:00 2001 From: pointer-to-bios Date: Sun, 21 Jan 2024 19:49:22 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E5=86=85=E5=AD=98=E7=AE=A1?= =?UTF-8?q?=E7=90=86=E6=A8=A1=E5=9D=97=E7=9A=84=E5=86=85=E5=AD=98=E5=88=86?= =?UTF-8?q?=E9=85=8D=E5=87=BD=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/kernel/memm.h | 4 ++++ src/kernel/memm/memm.c | 19 +++++++++++++------ src/kernel/memm/memm.rs | 5 +++-- src/libk/lst.c | 4 ++-- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/include/kernel/memm.h b/include/kernel/memm.h index 27557a7..d3f730f 100644 --- a/include/kernel/memm.h +++ b/include/kernel/memm.h @@ -143,6 +143,10 @@ void *memm_allocate(usize size, usize pid); #define memm_addr_get_allocator(mem) \ ((*(allocator_t **)(mem - 16))) +void *memm_kernel_allocate(usize size); + +void *memm_user_allocate(usize size, usize pid); + /* 释放内存 */ diff --git a/src/kernel/memm/memm.c b/src/kernel/memm/memm.c index aecfe24..2b152e6 100644 --- a/src/kernel/memm/memm.c +++ b/src/kernel/memm/memm.c @@ -192,7 +192,7 @@ void *memm_allocate(usize size, usize pid) MEMM_RAW_ALLOCATOR, pid); allocator = new_allocator; - allocator_iterator_t *allind = memm_allocate(sizeof(allocator_iterator_t), 0); + allocator_iterator_t *allind = memm_kernel_allocate(sizeof(allocator_iterator_t)); allind->allocator = new_allocator; allind->left = nullptr; allind->right = nullptr; @@ -201,14 +201,21 @@ void *memm_allocate(usize size, usize pid) after_allocation: memm_addr_set_allocator(ptr, allocator); - if (pid != 0) - { // TODO 进程管理中应该有一个用户地址-内核地址映射表 - // 在进程分配时将页映射到用户空间中,并将这个映射关系记录进这个表中 - // 需要返回的是用户空间的地址 - } return ptr; } +void *memm_kernel_allocate(usize size) +{ + return memm_allocate(size, 0); +} + +void *memm_user_allocate(usize size, usize pid) +{ + void *res = memm_allocate(size, pid); + // TODO 将内存空间映射到用户空间 + return res; +} + void memm_free(void *mem) { allocator_t *allocator = memm_addr_get_allocator(mem); diff --git a/src/kernel/memm/memm.rs b/src/kernel/memm/memm.rs index 471c332..fc41bf3 100644 --- a/src/kernel/memm/memm.rs +++ b/src/kernel/memm/memm.rs @@ -3,7 +3,8 @@ extern crate core; use core::alloc::{GlobalAlloc, Layout}; extern "C" { - pub fn memm_allocate(size: usize, pid: usize) -> *mut u8; + pub fn memm_kernel_allocate(size: usize) -> *mut u8; + pub fn memm_user_allocate(size: usize, pid: usize); pub fn memm_free(mem: *mut u8); } @@ -11,7 +12,7 @@ pub struct KernelAllocator {} unsafe impl GlobalAlloc for KernelAllocator { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - memm_allocate(layout.size(), 0) + memm_kernel_allocate(layout.size()) } unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { diff --git a/src/libk/lst.c b/src/libk/lst.c index cbfcce0..71b1cef 100644 --- a/src/libk/lst.c +++ b/src/libk/lst.c @@ -7,7 +7,7 @@ lst_iterator_t *lst_new(usize start, usize end) { - lst_iterator_t *lst = memm_allocate(sizeof(lst_iterator_t), 0); + lst_iterator_t *lst = memm_kernel_allocate(sizeof(lst_iterator_t), 0); lst->line.left = start; lst->line.right = end; lst->next = nullptr; @@ -128,7 +128,7 @@ bool lst_add(lst_iterator_t *lst, usize left, usize right, bool force) lst->line.left = left; else { - lst_iterator_t *new_node = memm_allocate(sizeof(lst_iterator_t), 0); + lst_iterator_t *new_node = memm_kernel_allocate(sizeof(lst_iterator_t)); new_node->line = line; new_node->next = lst; if (last != nullptr)