|
1 | 1 | #include <config.h> |
2 | 2 | #include <console.h> |
3 | 3 | #include <mem/paging.h> |
| 4 | +#include <mem/map.h> |
4 | 5 |
|
5 | 6 | void paging_test(void) { |
6 | | - paging_init_identity(16); |
7 | | - printk("identity map set\n"); |
| 7 | + printk("[PAGING TEST] start\n"); |
8 | 8 |
|
| 9 | + /* Set up a small identity mapping for low memory so kernel stays accessible */ |
| 10 | + paging_init_identity(1); /* map first 1 MB */ |
| 11 | + printk("[PAGING TEST] identity map (1MB) set\n"); |
| 12 | + |
| 13 | + /* Allocate a physical frame to map */ |
| 14 | + void *frame = alloc_frame(); |
| 15 | + if (!frame) { |
| 16 | + printk("[PAGING TEST] alloc_frame failed\n"); |
| 17 | + return; |
| 18 | + } |
| 19 | + uint32_t phys = (uint32_t)frame; |
| 20 | + printk("[PAGING TEST] allocated phys frame=0x%x\n", (unsigned)phys); |
| 21 | + |
| 22 | + /* choose a virtual address likely unmapped (above identity region) */ |
| 23 | + uint32_t virt = 0x4000000; /* 64MB */ |
| 24 | + |
| 25 | + /* map the page (present + rw) */ |
| 26 | + if (map_page(phys, virt, PAGING_PRESENT | PAGING_RW) != 0) { |
| 27 | + printk("[PAGING TEST] map_page failed\n"); |
| 28 | + return; |
| 29 | + } |
| 30 | + printk("[PAGING TEST] mapped phys 0x%x -> virt 0x%x\n", (unsigned)phys, (unsigned)virt); |
| 31 | + |
| 32 | + /* enable paging */ |
9 | 33 | paging_enable(); |
10 | | - printk("paging enabled\n"); |
11 | | - |
12 | | - /* Access a low physical address (identity mapped) to ensure no PF */ |
13 | | - volatile uint32_t *p = (uint32_t *)0x1000; |
14 | | - uint32_t val = *p; /* might be zero or garbage but should not fault */ |
15 | | - (void)val; |
16 | | - printk("read from 0x1000 OK\n"); |
17 | | - |
18 | | - /* Touch a high unmapped address to trigger PF (optional) */ |
19 | | -#ifdef PAGING_TEST_TRIGGER_PF |
20 | | - volatile uint32_t *bad = (uint32_t *)0xC0000000; /* likely unmapped */ |
21 | | - uint32_t badval = *bad; /* expect page fault handler */ |
22 | | - (void)badval; |
23 | | - printk("unexpected: no page fault\n"); |
24 | | -#endif |
25 | | - |
26 | | - printk("done\n"); |
| 34 | + printk("[PAGING TEST] paging enabled\n"); |
| 35 | + |
| 36 | + /* Access the newly mapped virtual address */ |
| 37 | + volatile uint32_t *p = (uint32_t *)virt; |
| 38 | + *p = 0xdeadbeef; |
| 39 | + uint32_t v = *p; |
| 40 | + printk("[PAGING TEST] write/read virt 0x%x -> 0x%x\n", (unsigned)virt, (unsigned)v); |
| 41 | + |
| 42 | + /* unmap the page (do not access after this to avoid page fault) */ |
| 43 | + if (unmap_page(virt) == 0) { |
| 44 | + printk("[PAGING TEST] unmapped virt 0x%x\n", (unsigned)virt); |
| 45 | + } else { |
| 46 | + printk("[PAGING TEST] unmap_page failed for 0x%x\n", (unsigned)virt); |
| 47 | + } |
| 48 | + |
| 49 | + /* remap the same physical frame to a different virtual address and verify */ |
| 50 | + uint32_t virt2 = 0x4100000; /* 65MB */ |
| 51 | + if (map_page(phys, virt2, PAGING_PRESENT | PAGING_RW) != 0) { |
| 52 | + printk("[PAGING TEST] remap failed\n"); |
| 53 | + return; |
| 54 | + } |
| 55 | + printk("[PAGING TEST] remapped phys 0x%x -> virt 0x%x\n", (unsigned)phys, (unsigned)virt2); |
| 56 | + volatile uint32_t *p2 = (uint32_t *)virt2; |
| 57 | + uint32_t v2 = *p2; /* should reflect previous write if caching coherent */ |
| 58 | + printk("[PAGING TEST] read virt2 0x%x -> 0x%x\n", (unsigned)virt2, (unsigned)v2); |
| 59 | + |
| 60 | + printk("[PAGING TEST] done\n"); |
27 | 61 | } |
0 commit comments