From bbff8bf1a2261a224cedfd4172b0b1f1f918fdfb Mon Sep 17 00:00:00 2001 From: Putta Khunchalee Date: Wed, 15 Jan 2025 02:46:04 +0700 Subject: [PATCH] Implements uk_ppera calculation (#1248) --- kernel/src/config/aarch64.rs | 4 +-- kernel/src/config/mod.rs | 4 +++ kernel/src/config/x86_64.rs | 4 +-- kernel/src/uma/keg.rs | 53 ++++++++++++++++++++++++++++++++---- kernel/src/uma/mod.rs | 2 ++ legacy/src/pcpu/mod.rs | 25 ----------------- 6 files changed, 56 insertions(+), 36 deletions(-) delete mode 100644 legacy/src/pcpu/mod.rs diff --git a/kernel/src/config/aarch64.rs b/kernel/src/config/aarch64.rs index 3b0c07aa2..f53566a04 100644 --- a/kernel/src/config/aarch64.rs +++ b/kernel/src/config/aarch64.rs @@ -1,3 +1 @@ -use core::num::NonZero; - -pub const PAGE_SIZE: NonZero = unsafe { NonZero::new_unchecked(0x4000) }; +pub const PAGE_SHIFT: usize = 14; // 16K diff --git a/kernel/src/config/mod.rs b/kernel/src/config/mod.rs index e77f260e4..d6c0bdd36 100644 --- a/kernel/src/config/mod.rs +++ b/kernel/src/config/mod.rs @@ -1,10 +1,14 @@ pub use self::arch::*; +use core::num::NonZero; use macros::elf_note; #[cfg_attr(target_arch = "aarch64", path = "aarch64.rs")] #[cfg_attr(target_arch = "x86_64", path = "x86_64.rs")] mod arch; +pub const PAGE_SIZE: NonZero = NonZero::new(1 << PAGE_SHIFT).unwrap(); +pub const PAGE_MASK: NonZero = NonZero::new(PAGE_SIZE.get() - 1).unwrap(); + #[elf_note(section = ".note.obkrnl.page-size", name = "obkrnl", ty = 0)] static NOTE_PAGE_SIZE: [u8; size_of::()] = PAGE_SIZE.get().to_ne_bytes(); diff --git a/kernel/src/config/x86_64.rs b/kernel/src/config/x86_64.rs index a9264d884..c6b11a886 100644 --- a/kernel/src/config/x86_64.rs +++ b/kernel/src/config/x86_64.rs @@ -1,3 +1 @@ -use core::num::NonZero; - -pub const PAGE_SIZE: NonZero = unsafe { NonZero::new_unchecked(0x1000) }; +pub const PAGE_SHIFT: usize = 12; // 4K diff --git a/kernel/src/uma/keg.rs b/kernel/src/uma/keg.rs index a4e85cd6f..e9283a4f0 100644 --- a/kernel/src/uma/keg.rs +++ b/kernel/src/uma/keg.rs @@ -1,10 +1,10 @@ use super::slab::RcFree; use super::UmaFlags; -use crate::config::PAGE_SIZE; +use crate::config::{PAGE_MASK, PAGE_SHIFT, PAGE_SIZE}; use crate::uma::slab::{Free, SlabHdr}; use crate::uma::Uma; use core::alloc::Layout; -use core::cmp::max; +use core::cmp::{max, min}; use core::num::NonZero; /// Implementation of `uma_keg` structure. @@ -30,8 +30,28 @@ impl UmaKeg { flags |= UmaFlags::VToSlab; } - if flags.has(UmaFlags::CacheSpread) { - todo!() + // Get uk_ppera. + let ppera = if flags.has(UmaFlags::CacheSpread) { + // Round size. + let size = if (size.get() & align) == 0 { + size.get() + } else { + (size.get() & !align) + align + 1 + }; + + // Get uk_rsize. + let align = align + 1; + let rsize = if (size & align) == 0 { + // TODO: What is this? + size + align + } else { + size + }; + + // Get uk_ppera. + let pages = (PAGE_SIZE.get() / align * rsize) >> PAGE_SHIFT; + + min(pages, (128 * 1024) / PAGE_SIZE) } else { // Check if item size exceed slab size. let min = Layout::new::(); @@ -57,6 +77,15 @@ impl UmaKeg { flags |= UmaFlags::Hash; } } + + // Get uk_ppera. + let mut ppera = size.get() >> PAGE_SHIFT; + + if size.get() > (size.get() & !PAGE_MASK.get()) { + ppera += 1; + } + + ppera } else { // Get uk_rsize. let rsize = max(size, Uma::SMALLEST_UNIT); @@ -77,8 +106,10 @@ impl UmaKeg { { todo!() } + + 1 } - } + }; if flags.has(UmaFlags::Offpage) { if flags.has(UmaFlags::RefCnt) { @@ -88,6 +119,18 @@ impl UmaKeg { } } + if ppera == 1 { + // TODO: Set uk_allocf and uk_freef. + } + + if flags.has(UmaFlags::MtxClass) { + todo!() + } + + if !flags.has(UmaFlags::Offpage) { + todo!() + } + todo!() } } diff --git a/kernel/src/uma/mod.rs b/kernel/src/uma/mod.rs index 0a7bf2534..e7dfec7cf 100644 --- a/kernel/src/uma/mod.rs +++ b/kernel/src/uma/mod.rs @@ -59,6 +59,8 @@ pub enum UmaFlags { Offpage = 0x8, /// `UMA_ZONE_MALLOC`. Malloc = 0x10, + /// `UMA_ZONE_MTXCLASS`. + MtxClass = 0x40, /// `UMA_ZONE_VM`. Vm = 0x80, /// `UMA_ZONE_HASH`. diff --git a/legacy/src/pcpu/mod.rs b/legacy/src/pcpu/mod.rs deleted file mode 100644 index 00fc13a5f..000000000 --- a/legacy/src/pcpu/mod.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::process::VThread; -use std::marker::PhantomData; -use std::rc::Rc; -use std::sync::Weak; - -/// Implementation of `pcpu` structure. -pub struct Pcpu { - id: usize, // pc_cpuid - idle_thread: Weak, // pc_idlethread - phantom: PhantomData>, // For !Send and !Sync. -} - -impl Pcpu { - pub fn new(id: usize, idle_thread: Weak) -> Self { - Self { - id, - idle_thread, - phantom: PhantomData, - } - } - - pub fn idle_thread(&self) -> &Weak { - &self.idle_thread - } -}