From 1274be88b61b685a7792e77fc38b90b8a30fcab7 Mon Sep 17 00:00:00 2001 From: Richard Qin Date: Wed, 13 May 2026 15:46:07 +0800 Subject: feat: add Page structure and PageState enumeration for physical memory management --- na/include/lume/frame.h | 59 ------------------------------------------------- na/include/lume/page.h | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 59 deletions(-) delete mode 100644 na/include/lume/frame.h create mode 100644 na/include/lume/page.h (limited to 'na') diff --git a/na/include/lume/frame.h b/na/include/lume/frame.h deleted file mode 100644 index 4424979..0000000 --- a/na/include/lume/frame.h +++ /dev/null @@ -1,59 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0-or-later */ -/* Copyright (C) 2026 Richard Qin */ - -#pragma once - -/* - * Frame — Physical page frame descriptor - * - * Each Frame describes one 4KB physical page. A flat array of Frame - * structs (frame_map) covers all physical memory discovered via FDT. - * - * Memory layout is compressed using a union: only one of free/slab - * metadata is valid at a time, determined by `state`. The refcount - * field is independent of the union to support COW sharing. - * - * Reference: docs/specs/pmm.md §3.1 - */ - -#include -#include -#include - -/* Forward declaration — defined in slab module (Phase 2) */ -class KmemCache; - -enum class FrameState : uint8 { - Free, // In Buddy free list - PcpCached, // In Per-CPU cache (invisible to buddy coalescing) - Allocated, // Handed out to Slab / VMM / user - Slab, // Backing a Slab cache -}; - -struct Frame { - /* - * Reference count (atomic). - * Starts at 1 on allocation. Incremented by fork() for COW pages. - * When decremented to 0, the frame is returned to the free pool. - */ - lume::atomic refcount; - - FrameState state; // Current usage state - uint8 order; // Buddy order (0–10, i.e. 4KB–4MB) - - union { - /* Valid when state == Free */ - struct { - list_node free_link; // Link into Buddy or PCP free list - } free; - - /* Valid when state == Slab */ - struct { - KmemCache* cache; // Owning slab cache - uint32 obj_count; // Number of allocated objects on this page - void* freelist; // Head of in-page free object linked list - } slab; - - /* state == Allocated: no extra metadata (yet) */ - }; -}; diff --git a/na/include/lume/page.h b/na/include/lume/page.h new file mode 100644 index 0000000..7f3f8d1 --- /dev/null +++ b/na/include/lume/page.h @@ -0,0 +1,59 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* Copyright (C) 2026 Richard Qin */ + +#pragma once + +/* + * Page — Physical page descriptor + * + * Each Page describes one 4KB physical page. A flat array of Page + * structs (page_map) covers all physical memory discovered via FDT. + * + * Memory layout is compressed using a union: only one of free/slab + * metadata is valid at a time, determined by `state`. The refcount + * field is independent of the union to support COW sharing. + * + * Reference: docs/specs/pmm.md §3.1 + */ + +#include +#include +#include + +/* Forward declaration — defined in slab module (Phase 2) */ +class KmemCache; + +enum class PageState : uint8 { + Free, // In Buddy free list + PcpCached, // In Per-CPU cache (invisible to buddy coalescing) + Allocated, // Handed out to Slab / VMM / user + Slab, // Backing a Slab cache +}; + +struct Page { + /* + * Reference count (atomic). + * Starts at 1 on allocation. Incremented by fork() for COW pages. + * When decremented to 0, the page is returned to the free pool. + */ + lume::atomic refcount; + + PageState state; // Current usage state + uint8 order; // Buddy order (0–10, i.e. 4KB–4MB) + + union { + /* Valid when state == Free */ + struct { + list_node free_link; // Link into Buddy or PCP free list + } free; + + /* Valid when state == Slab */ + struct { + KmemCache* cache; // Owning slab cache + uint32 obj_count; // Number of allocated objects on this page + void* freelist; // Head of in-page free object linked list + } slab; + + /* state == Allocated: no extra metadata (yet) */ + }; +}; -- cgit v1.2.3