Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fde7fa8
Started refactoring and making the slab allocator useable.
sagi21805 Jul 25, 2026
f928932
more refactoring
sagi21805 Jul 26, 2026
ff05a7b
Cleaned depracated code.
sagi21805 Jul 26, 2026
b1aafaa
Cleand deprecated and added some default trait implementaitons.
sagi21805 Jul 26, 2026
0018dd4
Moved some code out of the macro and improved macro readability.
sagi21805 Jul 26, 2026
d9e97f9
Add buddy and sync as dependency
sagi21805 Jul 26, 2026
a138bb9
Add SlabBlock trait
sagi21805 Jul 26, 2026
601c488
Started implementing a cool idea to store prev pointer on full
sagi21805 Jul 26, 2026
7596f6b
moved preallocated to it's own file.
sagi21805 Jul 28, 2026
7b8a56f
started replacing only runtime state into runtime and compile time
sagi21805 Aug 1, 2026
2f2b830
included sign extension
sagi21805 Aug 2, 2026
e2252eb
improved allocation functions
sagi21805 Aug 2, 2026
e6526c1
Add used state.
sagi21805 Aug 2, 2026
336b4a9
Finished writing deallocation for slabs
sagi21805 Aug 2, 2026
16fcaec
Removed slice pointer from slab descriptor making the struct the same
sagi21805 Aug 2, 2026
19c813a
add attach and detach functions
sagi21805 Aug 5, 2026
05c4032
add find function that is probably not working good.
sagi21805 Aug 5, 2026
3089f65
changed impl const to const impl for 1.99 nightly
sagi21805 Aug 7, 2026
fa1f24c
Fixed target rust abi change for nightly 1.99
sagi21805 Aug 7, 2026
c2ae803
removed prinln that corrupted screen with \n
sagi21805 Aug 7, 2026
1c7c6ec
more refactoring
sagi21805 Aug 7, 2026
56d5ac7
included sign extension
sagi21805 Aug 7, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,12 @@ members = [
"crates/drivers/vga-display",
"crates/memory/bump",
"crates/memory/buddy",
# "crates/memory/slab",
"crates/memory/slab",
"crates/memory/page",
"crates/sync",
"crates/libk",
"kernel",
"tests",
# "tests",
"xtask",
# "snippets",
]
Expand Down
4 changes: 1 addition & 3 deletions bootloader/first_stage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,4 @@ pub extern "C" fn obtain_memory_map() {
);
}
#[panic_handler]
pub fn panic_handler(_info: &PanicInfo) -> ! {
loop {}
}
pub fn panic_handler(_info: &PanicInfo) -> ! { loop {} }
4 changes: 2 additions & 2 deletions bootloader/second_stage/32bit_target.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"vendor": "unknown",
"relocation-model": "static",
"features": "+soft-float,-sse,-mmx",
"rustc-abi": "x86-softfloat"
}
"rustc-abi": "softfloat"
}
4 changes: 1 addition & 3 deletions bootloader/second_stage/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,4 @@ pub unsafe extern "C" fn second_stage() -> ! {
}

#[panic_handler]
unsafe fn panic(_info: &PanicInfo) -> ! {
loop {}
}
unsafe fn panic(_info: &PanicInfo) -> ! { loop {} }
3 changes: 1 addition & 2 deletions crates/arch/x86/src/memory_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ pub struct MemoryRegion {
pub region_type: MemoryRegionType,
}

#[rustfmt::skip]
impl const Default for MemoryRegion {
const impl Default for MemoryRegion {
fn default() -> Self {
MemoryRegion {
base_address: 0,
Expand Down
6 changes: 2 additions & 4 deletions crates/arch/x86/src/structures/global_descriptor_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ struct GlobalDescriptorTableEntry32 {
base_high: u8,
}

#[rustfmt::skip]
impl const Default for GlobalDescriptorTableEntry32 {
const impl Default for GlobalDescriptorTableEntry32 {
fn default() -> Self {
GlobalDescriptorTableEntry32 {
limit_low: 0,
Expand Down Expand Up @@ -118,8 +117,7 @@ pub struct SystemSegmentDescriptor64 {
_reserved: u32,
}

#[rustfmt::skip]
impl const Default for SystemSegmentDescriptor64 {
const impl Default for SystemSegmentDescriptor64 {
fn default() -> Self {
SystemSegmentDescriptor64 {
limit_low: 0,
Expand Down
43 changes: 13 additions & 30 deletions crates/common/src/address_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use core::ptr::NonNull;

#[cfg(target_arch = "x86_64")]
use crate::constants::PHYSICAL_MEMORY_OFFSET;
use crate::enums::PageTableLevel;
use crate::{constants::KERNEL_OFFSET, enums::PageTableLevel};

use derive_more::{
Add, AddAssign, AsMut, AsRef, Div, DivAssign, Mul, MulAssign, Sub,
Expand Down Expand Up @@ -87,11 +87,8 @@ pub const trait Address: Sized + Clone + Copy {
#[repr(C)]
pub struct PhysicalAddress(usize);

#[rustfmt::skip]
impl const Address for PhysicalAddress {
unsafe fn new_unchecked(address: usize) -> Self {
Self(address)
}
const impl Address for PhysicalAddress {
unsafe fn new_unchecked(address: usize) -> Self { Self(address) }

fn new(address: usize) -> Option<Self> {
#[cfg(not(target_arch = "x86"))]
Expand All @@ -109,30 +106,23 @@ impl const Address for PhysicalAddress {
}
}

fn as_usize(&self) -> usize {
self.0
}
fn as_usize(&self) -> usize { self.0 }
}

#[rustfmt::skip]
impl const From<usize> for PhysicalAddress {
const impl From<usize> for PhysicalAddress {
fn from(value: usize) -> Self {
unsafe { PhysicalAddress::new_unchecked(value) }
}
}

#[rustfmt::skip]
impl const From<u64> for PhysicalAddress {
const impl From<u64> for PhysicalAddress {
fn from(value: u64) -> Self {
unsafe { PhysicalAddress::new_unchecked(value as usize) }
}
}

#[rustfmt::skip]
impl const From<PhysicalAddress> for u64 {
fn from(value: PhysicalAddress) -> Self {
value.0 as u64
}
const impl From<PhysicalAddress> for u64 {
fn from(value: PhysicalAddress) -> Self { value.0 as u64 }
}

#[derive(
Expand Down Expand Up @@ -160,18 +150,14 @@ pub struct VirtualAddress(usize);
#[rustfmt::skip]
impl const Address for VirtualAddress {
unsafe fn new_unchecked(address: usize) -> Self {
Self(address)
Self(((address << 16) as isize >> 16) as usize)
}
Comment on lines +153 to 154

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Find target specifications and per-crate target settings.
fd -e json -e toml --hidden | rg -n 'target|x86' | head -50
fd '^\.cargo$' -t d --hidden --exec cat {}/config.toml
rg -n 'target_arch\s*=\s*"x86"' --type rust -C2
rg -n 'common' -g 'Cargo.toml' -C2 bootloader

Repository: sagi21805/LearnixOS

Length of output: 439


🏁 Script executed:

#!/bin/bash
set -e

# Locate and inspect the relevant Rust source and Cargo metadata around common/arch targets.
printf 'Tracked Rust/config files:\n'
git ls-files | rg '(^crates/|Cargo\.toml$|\.cargo/config|32bit_target|64bit_target|16bit_target)' || true

printf '\naddress_types.rs outline:\n'
ast-grep outline crates/common/src/address_types.rs || true

printf '\naddress_types.rs relevant lines:\n'
cat -n crates/common/src/address_types.rs | sed -n '1,240p'

printf '\nTarget json files:\n'
cat -n bootloader/second_stage/32bit_target.json
printf '\n---\n'
cat -n kernel/64bit_target.json

printf '\nCommon references in Cargo.toml files:\n'
rg -n 'common|crates/common|crate_name|target =' -g 'Cargo.toml' -C2 .

printf '\nArchitecture target checks in Rust:\n'
rg -n 'target_arch\s*=\s*"x86"|target_arch\s*=\s*"x86_64"|target_pointer_width\s*=\s*"|new_unchecked|canonicalize' --type rust -C2 .

Repository: sagi21805/LearnixOS

Length of output: 50375


Gate virtual-address canonicalization to 64-bit pointer width.

VirtualAddress::new_unchecked signs-extends bit 47 after shifting by 16, but usize is 32 bits on the target_arch = "x86" build (p:32:32). That canonicalization no longer applies only to the 64-bit path; apply it only when target_pointer_width = "64" or use fixed-width u64/i64 arithmetic.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@crates/common/src/address_types.rs` around lines 163 - 164, Update
VirtualAddress::new_unchecked so the bit-47 sign-extension is performed only
when target_pointer_width is 64; preserve the direct address conversion on
32-bit targets, using the existing target configuration or fixed-width
arithmetic to avoid applying 64-bit canonicalization to usize.


fn new(address: usize) -> Option<Self> {
#[cfg(not(target_arch = "x86"))]
{
if address < (1usize << 48) {
return Some(unsafe {
Self::new_unchecked(
(((address << 16) as isize) >> 16) as usize,
)
});
if address < (1 << 48) {
return Some(unsafe { Self::new_unchecked(address) });
} else {
None
}
Expand All @@ -183,9 +169,7 @@ impl const Address for VirtualAddress {
}
}

fn as_usize(&self) -> usize {
self.0
}
fn as_usize(&self) -> usize { self.0 }
}

impl<T> From<NonNull<T>> for VirtualAddress {
Expand All @@ -194,8 +178,7 @@ impl<T> From<NonNull<T>> for VirtualAddress {
}
}

#[rustfmt::skip]
impl const From<usize> for VirtualAddress {
const impl From<usize> for VirtualAddress {
fn from(value: usize) -> Self {
unsafe { VirtualAddress::new_unchecked(value) }
}
Expand Down
6 changes: 2 additions & 4 deletions crates/common/src/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ pub struct Allocation {
pub base: VirtualAddress,
}

#[rustfmt::skip]
impl const Default for Allocation {
const impl Default for Allocation {
fn default() -> Self {
Allocation {
layout: unsafe {
Expand All @@ -34,8 +33,7 @@ pub struct Allocations<const N: usize> {
pub index: usize,
}

#[rustfmt::skip]
impl<const N: usize> const Default for Allocations<N> {
const impl<const N: usize> Default for Allocations<N> {
fn default() -> Self {
Allocations {
allocations: [Allocation::default(); N],
Expand Down
3 changes: 1 addition & 2 deletions crates/common/src/enums/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,7 @@ pub enum PS2ScanCode {
ReleasedSuperKey,
}

#[rustfmt::skip]
impl const From<u8> for PS2ScanCode {
const impl From<u8> for PS2ScanCode {
fn from(value: u8) -> Self {
match value {
// Number row
Expand Down
3 changes: 1 addition & 2 deletions crates/common/src/enums/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ impl PageSize {
}
}

#[rustfmt::skip]
impl const From<PageSize> for Layout {
const impl From<PageSize> for Layout {
fn from(val: PageSize) -> Self {
unsafe {
match val {
Expand Down
6 changes: 2 additions & 4 deletions crates/common/src/late_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,17 +45,15 @@ impl<T: Clone + Copy> LateInit<T> {
}
}

#[rustfmt::skip]
impl<T> const Deref for LateInit<T> {
const impl<T> Deref for LateInit<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
unsafe { self.0.assume_init_ref() }
}
}

#[rustfmt::skip]
impl<T> const DerefMut for LateInit<T> {
const impl<T> DerefMut for LateInit<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { self.0.assume_init_mut() }
}
Expand Down
3 changes: 1 addition & 2 deletions crates/drivers/vga-display/src/color_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ pub struct ColorCode {
pub background: B4,
}

#[rustfmt::skip]
impl const Default for ColorCode {
const impl Default for ColorCode {
fn default() -> Self {
ColorCode::new()
.foreground(Color::White)
Expand Down
3 changes: 1 addition & 2 deletions crates/drivers/vga-display/src/screen_char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ impl ScreenChar {
}
}

#[rustfmt::skip]
impl const Default for ScreenChar {
const impl Default for ScreenChar {
/// Create a default Screen char with Space as char
/// value, and with the default [`ColorCode`]
fn default() -> Self {
Expand Down
3 changes: 1 addition & 2 deletions crates/drivers/vga-display/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ impl<const W: usize, const H: usize> SimpleWriter<W, H> {
}
}

#[rustfmt::skip]
impl<const W: usize, const H: usize> const Default for SimpleWriter<W, H> {
const impl<const W: usize, const H: usize> Default for SimpleWriter<W, H> {
fn default() -> Self {
Self {
color: ColorCode::default(),
Expand Down
4 changes: 2 additions & 2 deletions crates/macros/src/bitfields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,10 @@ impl<'a> BitFields<'a> {
let struct_name = self.struct_name;
let struct_type = &self.struct_type;
quote! {
impl const ::core::convert::From<#struct_type> for #struct_name {
const impl ::core::convert::From<#struct_type> for #struct_name {
fn from(value: #struct_type) -> Self { #struct_name(value) }
}
impl const ::core::convert::From<#struct_name> for #struct_type {
const impl ::core::convert::From<#struct_name> for #struct_type {
fn from(value: #struct_name) -> Self { value.0 }
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/memory/page/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2024"
common = { path = "../../common" }
x86 = { path = "../../arch/x86" }
buddy = { path = "../buddy" }
slab = { path = "../slab" }
num_enum = { git = "https://github.com/sagi21805/num_enum.git", default-features = false, features = [
"complex-expressions",
] }
Expand Down
47 changes: 21 additions & 26 deletions crates/memory/page/src/arena.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use buddy::meta::{
Regular,
};

use crate::{Page, meta::PageMeta};
use crate::Page;

pub struct PageMap {
inner: Box<[Page]>,
Expand All @@ -45,14 +45,12 @@ impl BuddyArena<Page> for PageMap {
let mut prev = &mut page_map[0];

*prev = Page {
meta: PageMeta {
buddy: BuddyMeta::<Regular>::new(
NonNull::from_ref(head),
BuddyFlags::new()
.order(BuddyOrder::Order0)
.allocated(false),
),
},
buddy: BuddyMeta::<Regular>::new(
NonNull::from_ref(head),
BuddyFlags::new()
.order(BuddyOrder::Order0)
.allocated(false),
),
};

head.attach_block(NonNull::from_ref(prev));
Expand All @@ -62,16 +60,14 @@ impl BuddyArena<Page> for PageMap {
prev = left.last_mut().unwrap();
let next = right.first_mut().unwrap();
*next = Page {
meta: PageMeta {
buddy: BuddyMeta::<Regular>::new(
NonNull::from_ref(prev.meta()),
BuddyFlags::new()
.order(BuddyOrder::Order0)
.allocated(false),
),
},
buddy: BuddyMeta::<Regular>::new(
NonNull::from_ref(prev.meta()),
BuddyFlags::new()
.order(BuddyOrder::Order0)
.allocated(false),
),
};
prev.meta.buddy.attach_block(NonNull::from_mut(next));
prev.buddy.attach_block(NonNull::from_mut(next));
}

PageMap { inner: page_map }
Expand Down Expand Up @@ -102,7 +98,7 @@ impl BuddyArena<Page> for PageMap {
&self,
block: NonNull<Page>,
) -> Result<NonNull<Page>, BuddyError> {
let order = unsafe { block.as_ref().meta.buddy.flags.get_order() };
let order = unsafe { block.as_ref().buddy.flags.get_order() };

let (section_idx, section_offset) =
unsafe { self.section_index_of(block) };
Expand Down Expand Up @@ -130,24 +126,23 @@ impl BuddyArena<Page> for PageMap {
) -> Result<NonNull<Page>, BuddyError> {
debug_assert_eq!(self.buddy_of(block)?, buddy);
debug_assert!(unsafe {
block.as_ref().meta.buddy.flags.get_order()
== buddy.as_ref().meta.buddy.flags.get_order()
block.as_ref().buddy.flags.get_order()
== buddy.as_ref().buddy.flags.get_order()
});
debug_assert!(
unsafe { block.as_ref().meta.buddy.flags.get_order() }
unsafe { block.as_ref().buddy.flags.get_order() }
!= BuddyOrder::None
);
debug_assert!(unsafe {
!block.as_ref().meta.buddy.flags.is_allocated()
!block.as_ref().buddy.flags.is_allocated()
});
debug_assert!(unsafe {
!buddy.as_ref().meta.buddy.flags.is_allocated()
!buddy.as_ref().buddy.flags.is_allocated()
});

let next_order = unsafe {
block
.as_ref()
.meta
.buddy
.flags
.get_order()
Expand Down Expand Up @@ -197,7 +192,7 @@ impl BuddyArena<Page> for PageMap {
let mut buddy = self.buddy_of(block)?;

unsafe {
buddy.as_mut().meta.buddy.flags.set_order(prev_order);
buddy.as_mut().buddy.flags.set_order(prev_order);
}

Ok((block, buddy))
Expand Down
Loading