feat: implement capability-based memory management foundation

This commit is contained in:
Faynot
2026-05-05 01:30:16 +03:00
parent a45587042b
commit d542b5586d
8 changed files with 200 additions and 80 deletions

View File

@@ -12,6 +12,7 @@ use limine::request::{FramebufferRequest, RequestsEndMarker, RequestsStartMarker
use crate::mem::paging::{PageTable, PageTableFlags};
use crate::mem::address::{PhysAddr, VirtAddr};
use crate::cap::{Relation, CapRights, Capability, CapObject};
pub mod cap;
mod mem;
@@ -157,7 +158,6 @@ static _START_MARKER: RequestsStartMarker = RequestsStartMarker::new();
#[used] #[unsafe(link_section = ".requests_end_marker")]
static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new();
// Точка входа
#[unsafe(no_mangle)]
unsafe extern "C" fn kmain() -> ! {
assert!(BASE_REVISION.is_supported());
@@ -174,12 +174,10 @@ unsafe extern "C" fn kmain() -> ! {
info!(console, "BOOT", "RINA Kernel Starting...");
// 1. Инициализация PMM
unsafe { mem::pmm::BitmapPMM::init(&mmap_res, hhdm_offset); }
info!(console, "MEM", "Physical Memory Manager initialized.");
// 2. Инициализация страничного управления
let p4_phys = mem::pmm::alloc_page().expect("OOM: Failed to allocate P4 table");
let p4_phys = mem::pmm::alloc_frame().expect("OOM: Failed to allocate P4 table");
let p4 = unsafe { &mut *p4_phys.to_virt(hhdm_offset).as_mut_ptr::<PageTable>() };
unsafe { core::ptr::write_bytes(p4 as *mut _ as *mut u8, 0, 4096); }
@@ -199,11 +197,10 @@ unsafe extern "C" fn kmain() -> ! {
info!(console, "MMU", "Switching to Kernel Page Tables...");
unsafe { p4.activate(p4_phys); }
// 3. Инициализация Кучи
let heap_start = 0xFFFF_9000_0000_0000;
let heap_size = 8 * 1024 * 1024;
for i in (0..heap_size).step_by(4096) {
let frame = mem::pmm::alloc_page().expect("OOM: Heap allocation failed");
let frame = mem::pmm::alloc_frame().expect("OOM: Heap allocation failed");
p4.map_page(VirtAddr(heap_start + i as u64), frame, flags, hhdm_offset);
}
@@ -213,28 +210,33 @@ unsafe extern "C" fn kmain() -> ! {
}
info!(console, "HEAP", "Global Allocator is online.");
// --- Инициализация Модели Capabilities ---
// Создаем корневой CNode для ядра на 256 слотов
let mut root_cnode = cap::CNode::new(256);
let root_cnode = cap::CNode::new(256);
// Пример: Превращаем физическую страницу в Capability "Memory"
if let Some(frame) = mem::pmm::alloc_page() {
let mem_cap = cap::Capability {
object: cap::CapObject::Memory { frame, size: 4096 },
rights: cap::CapRights::READ | cap::CapRights::WRITE,
if let Some(frame) = mem::pmm::alloc_frame() {
let mem_cap = Capability {
object: CapObject::Memory { phys: frame, size_pages: 1 },
rights: CapRights::all(),
relation: Relation::Strong,
token_sig: 0x1,
};
// Помещаем в слот 0. Теперь "Дескриптор 0" — это право доступа к этой памяти.
root_cnode.insert(0, mem_cap).unwrap();
info!(console, "CAP", "Created memory capability at slot 0");
info!(console, "CAP", "Root capability created at slot 0");
}
// Проверка
if let Some(c) = root_cnode.get(0) {
info!(console, "CAP", "Slot 0 verification: {:?}", c.object);
root_cnode.mint(0, 10, Relation::Borrow, CapRights::READ | CapRights::WRITE).unwrap();
if let Some(c) = root_cnode.get_cap(10) {
info!(console, "CAP", "Slot 10 (Borrowed): {:?}", c.relation);
}
root_cnode.revoke(0);
if let Some(c) = root_cnode.get_cap(10) {
if !c.is_valid() {
info!(console, "CAP", "Slot 10 successfully revoked.");
}
}
let logo = r#"
###########
##################