summaryrefslogtreecommitdiff
path: root/include/kernel/pmm.h
blob: 7156c76b01541fce485864743c966dd9fe627b63 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#pragma once
#include "common/types.h"
#include "kernel/spinlock.h"

// Physical Memory Layout Definition
#define KERNBASE 0x80000000
// Set the memory size to 128MB
#define PHYSIZE (128 * 1024 * 1024)
#define PHYSTOP (KERNBASE + PHYSIZE)

#define PGSIZE 4096
#define PGSHIFT 12

// Buddy System Max Order
// Order 10 => 2^10 pages = 1024 * 4KB = 4MB contiguous block
// Order 11 => 2^11 pages = 8MB
#define MAX_ORDER 11

// Page flag
#define PG_used 0x01     // This page is occupied
#define PG_reserved 0x02 // This page is reserved (kernel code/data, DTB, MemMap)

// Physical Page Metadata Structure
struct Page
{
    uint8 flags;       // Page Status (PG_used, PG_reserved)
    uint8 order;       // If it is the first page of a free block, record the order of the current block
    uint16 refcnt;     // Reference Counting
    struct Page *next; // Free list pointer (points to the next free block of the same size)
    struct Page *prev; // Doubly linked list (easy to remove)
};

namespace PMM
{
    // Initialize physical memory management
    void init();

    // Core Allocation Interface (Buddy System)
    // order: 0 ~ MAX_ORDER-1
    void *alloc_pages(int order);
    void free_pages(void *pa, int order);

    // Compatible with old interface (allocate single page with order=0 and automatically reset to zero)
    void *alloc_page();
    void free_page(void *pa);

    // Helper function
    struct Page *pa_to_page(uint64 pa);
    uint64 page_to_pa(struct Page *page);

    // Debug information
    void print_free_lists();
}