Skip to content
This repository was archived by the owner on Jan 14, 2026. It is now read-only.

Commit b3935da

Browse files
committed
add paging test
1 parent 1cf5bc7 commit b3935da

2 files changed

Lines changed: 54 additions & 20 deletions

File tree

src/boot/config.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
KERNEL_OFFSET EQU 0x10000 ; カーネルをロードするアドレス
2-
SECTOR_COUNT EQU 38 ; 読み込むセクタの数
2+
SECTOR_COUNT EQU 39 ; 読み込むセクタの数
33
START_SECTOR EQU 2 ; 開始するセクタ番号
44
CYLINDER_NUM EQU 0 ; シリンダ番号
55
HEAD_NUM EQU 0 ; ヘッド番号

src/kernel/tests/paging_test.c

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,61 @@
11
#include <config.h>
22
#include <console.h>
33
#include <mem/paging.h>
4+
#include <mem/map.h>
45

56
void paging_test(void) {
6-
paging_init_identity(16);
7-
printk("identity map set\n");
7+
printk("[PAGING TEST] start\n");
88

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 */
933
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");
2761
}

0 commit comments

Comments
 (0)