mirror of
https://github.com/DragonOS-Community/DragonOS.git
synced 2025-06-19 17:26:31 +00:00
新的内存管理模块 (#303)
  实现了具有优秀架构设计的新的内存管理模块,对内核空间和用户空间的内存映射、分配、释放、管理等操作进行了封装,使得内核开发者可以更加方便地进行内存管理。   内存管理模块主要由以下类型的组件组成: - **硬件抽象层(MemoryManagementArch)** - 提供对具体处理器架构的抽象,使得内存管理模块可以在不同的处理器架构上运行 - **页面映射器(PageMapper)**- 提供对虚拟地址和物理地址的映射,以及页表的创建、填写、销毁、权限管理等操作。分为两种类型:内核页表映射器(KernelMapper)和用户页表映射器(位于具体的用户地址空间结构中) - **页面刷新器(PageFlusher)** - 提供对页表的刷新操作(整表刷新、单页刷新、跨核心刷新) - **页帧分配器(FrameAllocator)** - 提供对页帧的分配、释放、管理等操作。具体来说,包括BumpAllocator、BuddyAllocator - **小对象分配器** - 提供对小内存对象的分配、释放、管理等操作。指的是内核里面的SlabAllocator (SlabAllocator的实现目前还没有完成) - **MMIO空间管理器** - 提供对MMIO地址空间的分配、管理操作。(目前这个模块待进一步重构) - **用户地址空间管理机制** - 提供对用户地址空间的管理。 - VMA机制 - 提供对用户地址空间的管理,包括VMA的创建、销毁、权限管理等操作 - 用户映射管理 - 与VMA机制共同作用,管理用户地址空间的映射 - **系统调用层** - 提供对用户空间的内存管理系统调用,包括mmap、munmap、mprotect、mremap等 - **C接口兼容层** - 提供对原有的C代码的接口,是的C代码能够正常运行。 除上面的新增内容以外,其它的更改内容: - 新增二进制加载器,以及elf的解析器 - 解决由于local_irq_save、local_irq_restore函数的汇编不规范导致影响栈行为的bug。 - 解决local_irq_save未关中断的错误。 - 修复sys_gettimeofday对timezone参数的处理的bug --------- Co-authored-by: kong <kongweichao@dragonos.org>
This commit is contained in:
.vscode
MakefileREADME.mdREADME_EN.mddocs
Multiboot2 Specification version 2.0.pdf
community
contact
introduction
kernel
core_api
memory_management
process_management
kernel
Cargo.toml
src
Makefile
arch
common
driver
acpi
base
disk
ahci
interrupt
apic
pci
uart
video
virtio
exception
filesystem
head.Sinclude
bindings
ipc
lib.rslibs
main.cmm
Makefileallocator.rs
allocator
c_adapter.rsinternal.hkernel_mapper.rsmm-stat.cmm-types.hmm.cmm.hmmap.cmmio.cmmio.hmmio_buddy.rsmod.rsno_init.rspage.rsslab.cslab.hsyscall.rsucontext.rsutils.cvma.cprocess
sched
smp
syscall
time
user
@ -1,12 +1,13 @@
|
||||
use core::{
|
||||
ffi::c_void,
|
||||
mem::ManuallyDrop,
|
||||
ptr::{null_mut, read_volatile, write_volatile},
|
||||
};
|
||||
|
||||
use alloc::{boxed::Box, sync::Arc};
|
||||
|
||||
use crate::{
|
||||
arch::{asm::current::current_pcb, fpu::FpState},
|
||||
arch::asm::current::current_pcb,
|
||||
filesystem::vfs::{
|
||||
file::{File, FileDescriptorVec, FileMode},
|
||||
FileType, ROOT_INODE,
|
||||
@ -16,6 +17,7 @@ use crate::{
|
||||
PROC_UNINTERRUPTIBLE,
|
||||
},
|
||||
libs::casting::DowncastArc,
|
||||
mm::ucontext::AddressSpace,
|
||||
net::socket::SocketInode,
|
||||
sched::core::{cpu_executing, sched_enqueue},
|
||||
smp::core::{smp_get_processor_id, smp_send_reschedule},
|
||||
@ -311,94 +313,41 @@ impl process_control_block {
|
||||
.expect("Not a socket inode");
|
||||
return Some(socket);
|
||||
}
|
||||
}
|
||||
|
||||
// =========== 导出到C的函数,在将来,进程管理模块被完全重构之后,需要删掉他们 BEGIN ============
|
||||
|
||||
/// @brief 初始化当前进程的文件描述符数组
|
||||
/// 请注意,如果当前进程已经有文件描述符数组,那么本操作将被禁止
|
||||
#[no_mangle]
|
||||
pub extern "C" fn process_init_files() -> i32 {
|
||||
let r = current_pcb().init_files();
|
||||
if r.is_ok() {
|
||||
return 0;
|
||||
} else {
|
||||
return r.unwrap_err().to_posix_errno();
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief 拷贝当前进程的文件描述符信息
|
||||
///
|
||||
/// @param clone_flags 克隆标志位
|
||||
/// @param pcb 新的进程的pcb
|
||||
#[no_mangle]
|
||||
pub extern "C" fn process_copy_files(
|
||||
clone_flags: u64,
|
||||
from: &'static process_control_block,
|
||||
) -> i32 {
|
||||
let r = current_pcb().copy_files(clone_flags, from);
|
||||
if r.is_ok() {
|
||||
return 0;
|
||||
} else {
|
||||
return r.unwrap_err().to_posix_errno();
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief 回收进程的文件描述符数组
|
||||
///
|
||||
/// @param pcb 要被回收的进程的pcb
|
||||
///
|
||||
/// @return i32
|
||||
#[no_mangle]
|
||||
pub extern "C" fn process_exit_files(pcb: &'static mut process_control_block) -> i32 {
|
||||
let r: Result<(), SystemError> = pcb.exit_files();
|
||||
if r.is_ok() {
|
||||
return 0;
|
||||
} else {
|
||||
return r.unwrap_err().to_posix_errno();
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief 复制当前进程的浮点状态
|
||||
#[allow(dead_code)]
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rs_dup_fpstate() -> *mut c_void {
|
||||
// 如果当前进程没有浮点状态,那么就返回一个默认的浮点状态
|
||||
if current_pcb().fp_state == null_mut() {
|
||||
return Box::leak(Box::new(FpState::default())) as *mut FpState as usize as *mut c_void;
|
||||
} else {
|
||||
// 如果当前进程有浮点状态,那么就复制一个新的浮点状态
|
||||
let state = current_pcb().fp_state as usize as *mut FpState;
|
||||
unsafe {
|
||||
let s = state.as_ref().unwrap();
|
||||
let state: &mut FpState = Box::leak(Box::new(s.clone()));
|
||||
|
||||
return state as *mut FpState as usize as *mut c_void;
|
||||
/// 释放pcb中存储的地址空间的指针
|
||||
pub unsafe fn drop_address_space(&mut self) {
|
||||
let p = self.address_space as *const AddressSpace;
|
||||
if p.is_null() {
|
||||
return;
|
||||
}
|
||||
let p: Arc<AddressSpace> = Arc::from_raw(p);
|
||||
drop(p);
|
||||
self.address_space = null_mut();
|
||||
}
|
||||
}
|
||||
|
||||
/// @brief 释放进程的浮点状态所占用的内存
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rs_process_exit_fpstate(pcb: &'static mut process_control_block) {
|
||||
if pcb.fp_state != null_mut() {
|
||||
let state = pcb.fp_state as usize as *mut FpState;
|
||||
unsafe {
|
||||
drop(Box::from_raw(state));
|
||||
/// 设置pcb中存储的地址空间的指针
|
||||
///
|
||||
/// ## panic
|
||||
/// 如果当前pcb已经有地址空间,那么panic
|
||||
pub unsafe fn set_address_space(&mut self, address_space: Arc<AddressSpace>) {
|
||||
assert!(self.address_space.is_null(), "Address space already set");
|
||||
self.address_space = Arc::into_raw(address_space) as *mut c_void;
|
||||
}
|
||||
|
||||
/// 获取当前进程的地址空间的指针
|
||||
pub fn address_space(&self) -> Option<Arc<AddressSpace>> {
|
||||
let ptr = self.address_space as *const AddressSpace;
|
||||
if ptr.is_null() {
|
||||
return None;
|
||||
}
|
||||
// 为了防止pcb中的指针被释放,这里需要将其包装一下,使得Arc的drop不会被调用
|
||||
let arc_wrapper = ManuallyDrop::new(unsafe { Arc::from_raw(ptr) });
|
||||
|
||||
let result = Arc::clone(&arc_wrapper);
|
||||
return Some(result);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rs_init_stdio() -> i32 {
|
||||
let r = init_stdio();
|
||||
if r.is_ok() {
|
||||
return 0;
|
||||
} else {
|
||||
return r.unwrap_err().to_posix_errno();
|
||||
}
|
||||
}
|
||||
// =========== 以上为导出到C的函数,在将来,进程管理模块被完全重构之后,需要删掉他们 END ============
|
||||
|
||||
/// @brief 初始化pid=1的进程的stdio
|
||||
pub fn init_stdio() -> Result<(), SystemError> {
|
||||
|
Reference in New Issue
Block a user