summaryrefslogtreecommitdiff
path: root/na/test/test_pmm.cc
blob: 948e444330ea64f6031c0882019ed4a5b681000a (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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* SPDX-License-Identifier: GPL-2.0-or-later */
/* Copyright (C) 2026 Richard Qin */

/*
 * Boot Self-Test: Physical Memory Manager (PMM)
 *
 * Tests:
 *   1. Single frame alloc/free round-trip
 *   2. Frame address alignment (4KB)
 *   3. No duplicate frames from consecutive allocs
 *   4. Free then re-alloc returns the same frame (PCP cache)
 *   5. Multi-frame (order > 0) alloc/free
 *   6. Bulk alloc stress test (alloc many, free all)
 *   7. Reference counting (incref/decref)
 *   8. frame_to_pa / pa_to_frame round-trip
 */

#include <lume/selftest.h>
#include <lume/pmm.h>
#include <lume/addr.h>

void selftest_pmm()
{
    st_begin("pmm: single frame alloc/free");
    {
        Frame* f = pmm_alloc_frame();
        ST_ASSERT(f != nullptr);
        ST_ASSERT(f->state == FrameState::Allocated);
        ST_ASSERT(f->order == 0);

        uint64 pa = frame_to_pa(f).raw;
        ST_ASSERT_EQ(pa % kPageSize, 0ULL);  // 4KB aligned

        uint64 va = frame_to_va(f).raw;
        ST_ASSERT_EQ(va, pa_to_va(phys_addr(pa)).raw);

        pmm_free_frame(f);
    }
    st_pass();

    st_begin("pmm: no duplicate frames");
    {
        Frame* f1 = pmm_alloc_frame();
        Frame* f2 = pmm_alloc_frame();
        ST_ASSERT(f1 != nullptr);
        ST_ASSERT(f2 != nullptr);
        ST_ASSERT_NE(f1, f2);
        ST_ASSERT_NE(frame_to_pa(f1).raw, frame_to_pa(f2).raw);
        pmm_free_frame(f2);
        pmm_free_frame(f1);
    }
    st_pass();

    st_begin("pmm: free then re-alloc (PCP)");
    {
        Frame* f1 = pmm_alloc_frame();
        uint64 pa1 = frame_to_pa(f1).raw;
        pmm_free_frame(f1);

        // PCP should return the most recently freed frame
        Frame* f2 = pmm_alloc_frame();
        uint64 pa2 = frame_to_pa(f2).raw;
        ST_ASSERT_EQ(pa1, pa2);
        pmm_free_frame(f2);
    }
    st_pass();

    st_begin("pmm: multi-frame alloc (order 2 = 4 pages)");
    {
        Frame* f = pmm_alloc_frames(2);
        ST_ASSERT(f != nullptr);
        ST_ASSERT(f->order == 2);

        uint64 pa = frame_to_pa(f).raw;
        ST_ASSERT_EQ(pa % (kPageSize * 4), 0ULL);  // Naturally aligned

        pmm_free_frames(f, 2);
    }
    st_pass();

    st_begin("pmm: bulk alloc/free stress (256 frames)");
    {
        constexpr int N = 256;
        Frame* frames[N];

        // Allocate all
        for (int i = 0; i < N; i++) {
            frames[i] = pmm_alloc_frame();
            ST_ASSERT(frames[i] != nullptr);
        }

        // Verify no duplicates (spot check: consecutive pairs)
        for (int i = 1; i < N; i++) {
            ST_ASSERT_NE(frames[i], frames[i - 1]);
        }

        // Free all
        for (int i = 0; i < N; i++) {
            pmm_free_frame(frames[i]);
        }
    }
    st_pass();

    st_begin("pmm: frame_to_pa / pa_to_frame round-trip");
    {
        Frame* f = pmm_alloc_frame();
        ST_ASSERT(f != nullptr);
        uint64 pa = frame_to_pa(f).raw;
        Frame* f2 = pa_to_frame(phys_addr(pa));
        ST_ASSERT_EQ(f, f2);
        pmm_free_frame(f);
    }
    st_pass();

    st_begin("pmm: refcount incref/decref");
    {
        Frame* f = pmm_alloc_frame();
        ST_ASSERT(f != nullptr);
        ST_ASSERT(f->refcount.load() == 1);

        frame_incref(f);
        ST_ASSERT(f->refcount.load() == 2);

        frame_decref(f);  // 2 -> 1, should NOT free
        ST_ASSERT(f->refcount.load() == 1);
        ST_ASSERT(f->state == FrameState::Allocated);

        frame_decref(f);  // 1 -> 0, should free to pool
        // After decref to 0, frame is returned to pool.
        // We can't safely check f->state because it might be reused.
    }
    st_pass();
    st_begin("pmm: pmm_alloc_frames natural alignment (order-3)");
    {
        Frame* f = pmm_alloc_frames(3);
        ST_ASSERT(f != nullptr);
        uint64 pa = frame_to_pa(f).raw;
        ST_ASSERT_EQ(pa % (kPageSize * 8), 0ULL);
        pmm_free_frames(f, 3);
    }
    st_pass();

    st_begin("pmm: frame_decref to 0 and reuse (order-0)");
    {
        Frame* f = pmm_alloc_frame();
        uint64 pa = frame_to_pa(f).raw;
        frame_decref(f); // drops 1 -> 0, freeing it
        
        // Next alloc from PCP should give back the same frame
        Frame* f2 = pmm_alloc_frame();
        ST_ASSERT_EQ(frame_to_pa(f2).raw, pa);
        pmm_free_frame(f2);
    }
    st_pass();

    st_begin("pmm: frame_decref to 0 on multi-page blocks (order-3)");
    {
        Frame* f = pmm_alloc_frames(3);
        uint64 pa = frame_to_pa(f).raw;
        frame_decref(f); // Should internally route to pmm_free_frames(f, 3)
        
        // Re-allocate order-3 and see if we get it back (buddy coalescing was respected)
        Frame* f2 = pmm_alloc_frames(3);
        ST_ASSERT_EQ(frame_to_pa(f2).raw, pa);
        pmm_free_frames(f2, 3);
    }
    st_pass();

    st_begin("pmm: buddy split and block coalescing correctness");
    {
        // Force split: allocate order-1, free it, allocate two order-0
        Frame* f_ord1 = pmm_alloc_frames(1);
        uint64 pa1 = frame_to_pa(f_ord1).raw;
        pmm_free_frames(f_ord1, 1);
        
        Frame* f0_a = pmm_alloc_frame();
        Frame* f0_b = pmm_alloc_frame();
        
        // Free them to trigger coalesce
        pmm_free_frame(f0_a);
        pmm_free_frame(f0_b);
        
        // Allocate order-1 again to verify successful coalescing
        Frame* f_ord1_again = pmm_alloc_frames(1);
        ST_ASSERT_EQ(frame_to_pa(f_ord1_again).raw, pa1);
        pmm_free_frames(f_ord1_again, 1);
    }
    st_pass();

    st_begin("pmm: mixed order cross allocation");
    {
        Frame* f0 = pmm_alloc_frame();
        Frame* f2 = pmm_alloc_frames(2);
        Frame* f0b = pmm_alloc_frame();
        
        ST_ASSERT(f0 != nullptr);
        ST_ASSERT(f2 != nullptr);
        ST_ASSERT(f0b != nullptr);
        ST_ASSERT_NE(frame_to_pa(f0).raw, frame_to_pa(f2).raw);
        
        pmm_free_frame(f0);
        pmm_free_frames(f2, 2);
        pmm_free_frame(f0b);
    }
    st_pass();

    st_begin("pmm: PCP drain/refill boundaries");
    {
        // kPcpHighWatermark = 64. Allocating and freeing 65 blocks triggers drain.
        Frame* frames[65];
        for (int i=0; i<65; i++) frames[i] = pmm_alloc_frame();
        for (int i=0; i<65; i++) pmm_free_frame(frames[i]);
        
        // The system shouldn't crash and PCP logic should handle this.
        Frame* verify_f = pmm_alloc_frame();
        ST_ASSERT(verify_f != nullptr);
        pmm_free_frame(verify_f);
    }
    st_pass();

    st_begin("pmm: OOM exhaustion stability");
    {
        Frame* head = nullptr;
        // First allocate large chunks to be fast
        while (true) {
            Frame* f = pmm_alloc_frames(5);
            if (!f) break;
            uint64* ptr = reinterpret_cast<uint64*>(frame_to_va(f).raw);
            *ptr = reinterpret_cast<uint64>(head);
            // encode order in the second word
            *(ptr + 1) = 5;
            head = f;
        }
        // Then allocate single frames until absolute zero
        while (true) {
            Frame* f = pmm_alloc_frame();
            if (!f) break;
            uint64* ptr = reinterpret_cast<uint64*>(frame_to_va(f).raw);
            *ptr = reinterpret_cast<uint64>(head);
            *(ptr + 1) = 0;
            head = f;
        }
        
        // We hit nullptr. System is completely exhausted.
        ST_ASSERT(pmm_alloc_frame() == nullptr);
        
        // Free everything
        while (head) {
            uint64* ptr = reinterpret_cast<uint64*>(frame_to_va(head).raw);
            Frame* nxt = reinterpret_cast<Frame*>(*ptr);
            uint64 order = *(ptr + 1);
            if (order == 5) pmm_free_frames(head, 5);
            else pmm_free_frame(head);
            head = nxt;
        }
    }
    st_pass();

    st_begin("pmm: out of bounds order allocation gracefully fails");
    {
        // kMaxOrder is 11, so passing 11 or higher should safely return nullptr.
        Frame* f = pmm_alloc_frames(11);
        ST_ASSERT(f == nullptr);
        f = pmm_alloc_frames(255);
        ST_ASSERT(f == nullptr);
    }
    st_pass();

    st_begin("pmm: pmm_get_mem_base / pmm_get_mem_size sanity");
    {
        uint64 base = pmm_get_mem_base().raw;
        uint64 size = pmm_get_mem_size();

        // We no longer assert on QEMU specific memory address
        // ST_ASSERT(base >= 0x80000000ULL);
        ST_ASSERT(size > 0);
        // Size must be page-aligned
        ST_ASSERT_EQ(size % kPageSize, 0ULL);
        // Base must be page-aligned
        ST_ASSERT_EQ(base % kPageSize, 0ULL);
    }
    st_pass();
}