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

Commit fc6c40b

Browse files
committed
upd sector count and add alloc_irq_test functionality
1 parent c8685d3 commit fc6c40b

5 files changed

Lines changed: 99 additions & 7 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 31 ; 読み込むセクタの数
2+
SECTOR_COUNT EQU 33 ; 読み込むセクタの数
33
START_SECTOR EQU 2 ; 開始するセクタ番号
44
CYLINDER_NUM EQU 0 ; シリンダ番号
55
HEAD_NUM EQU 0 ; ヘッド番号

src/include/tests/define.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#define INTERRUPT_TEST
1212
// 割り込みベクタのテスト
1313
#define INTERRUPT_VECTOR_TEST
14+
// 割り込みハンドラでメモリのテスト
15+
#define ALLOC_IRQ_TEST
1416

1517
/* -------- 宣言 -------- */
1618
#ifdef MEM_TEST
@@ -25,6 +27,10 @@ void interrupt_test();
2527
void interrupt_vector_test();
2628
#endif /* INTERRUPT_VECTOR_TEST */
2729

30+
#ifdef ALLOC_IRQ_TEST
31+
void alloc_irq_test();
32+
#endif /* ALLOC_IRQ_TEST */
33+
2834
#endif /* TEST_TRUE */
2935

3036
#endif /* _TESTS_CONFIG_H */

src/kernel/mem/map.c

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414

1515
static uint32_t bitmap[(MAX_FRAMES + 31) / 32];
1616

17+
/* protect bitmap operations */
18+
static volatile uint32_t memmap_lock_storage = 0;
19+
// reuse existing spinlock type via pointer cast to avoid header include cycles
20+
// we'll wrap with helper functions below
21+
1722
// 管理情報をまとめた構造体
1823
static memmap_t memmap = {0};
1924

@@ -60,8 +65,15 @@ void memmap_init(uint32_t start, uint32_t end) {
6065
memmap.bitmap = bitmap;
6166

6267
// ビットマップをクリア(0 = free)
63-
for (uint32_t i = 0; i < (memmap.frames + 31) / 32; ++i) {
64-
memmap.bitmap[i] = 0;
68+
{
69+
uint32_t flags = 0;
70+
extern void spin_lock_irqsave(volatile uint32_t *lock, uint32_t *flagsptr);
71+
extern void spin_unlock_irqrestore(volatile uint32_t *lock, uint32_t flags);
72+
spin_lock_irqsave(&memmap_lock_storage, &flags);
73+
for (uint32_t i = 0; i < (memmap.frames + 31) / 32; ++i) {
74+
memmap.bitmap[i] = 0;
75+
}
76+
spin_unlock_irqrestore(&memmap_lock_storage, flags);
6577
}
6678

6779
printk("MemoryMap initialized: frames=%u start_frame=%u\n", (unsigned int)memmap.frames, (unsigned int)memmap.start_frame);
@@ -77,14 +89,20 @@ void* alloc_frame(void) {
7789
return NULL;
7890
}
7991

92+
uint32_t flags = 0;
93+
extern void spin_lock_irqsave(volatile uint32_t *lock, uint32_t *flagsptr);
94+
extern void spin_unlock_irqrestore(volatile uint32_t *lock, uint32_t flags);
95+
spin_lock_irqsave(&memmap_lock_storage, &flags);
8096
for (uint32_t i = 0; i < memmap.frames; ++i) {
8197
if (!bitmap_test(i)) {
8298
bitmap_set(i);
8399
uint32_t frame_no = memmap.start_frame + i;
84100
void* addr = (void*)(frame_no * FRAME_SIZE);
101+
spin_unlock_irqrestore(&memmap_lock_storage, flags);
85102
return addr;
86103
}
87104
}
105+
spin_unlock_irqrestore(&memmap_lock_storage, flags);
88106

89107
return NULL; // 空き無し
90108
}
@@ -116,7 +134,14 @@ void free_frame(void* addr) {
116134
return;
117135
}
118136

119-
bitmap_clear(idx);
137+
{
138+
uint32_t flags = 0;
139+
extern void spin_lock_irqsave(volatile uint32_t *lock, uint32_t *flagsptr);
140+
extern void spin_unlock_irqrestore(volatile uint32_t *lock, uint32_t flags);
141+
spin_lock_irqsave(&memmap_lock_storage, &flags);
142+
bitmap_clear(idx);
143+
spin_unlock_irqrestore(&memmap_lock_storage, flags);
144+
}
120145
}
121146

122147
/**
@@ -125,7 +150,14 @@ void free_frame(void* addr) {
125150
* @return 管理しているフレーム数
126151
*/
127152
uint32_t frame_count(void) {
128-
return memmap.frames;
153+
uint32_t f = 0;
154+
uint32_t flags = 0;
155+
extern void spin_lock_irqsave(volatile uint32_t *lock, uint32_t *flagsptr);
156+
extern void spin_unlock_irqrestore(volatile uint32_t *lock, uint32_t flags);
157+
spin_lock_irqsave(&memmap_lock_storage, &flags);
158+
f = memmap.frames;
159+
spin_unlock_irqrestore(&memmap_lock_storage, flags);
160+
return f;
129161
}
130162

131163

@@ -148,8 +180,15 @@ void memmap_reserve(uint32_t start, uint32_t end) {
148180
uint32_t s = (start_frame < memmap.start_frame) ? 0 : (start_frame - memmap.start_frame);
149181
uint32_t e = (end_frame > memmap.start_frame + memmap.frames) ? memmap.frames : (end_frame - memmap.start_frame);
150182

151-
for (uint32_t i = s; i < e; ++i) {
152-
bitmap_set(i);
183+
{
184+
uint32_t flags = 0;
185+
extern void spin_lock_irqsave(volatile uint32_t *lock, uint32_t *flagsptr);
186+
extern void spin_unlock_irqrestore(volatile uint32_t *lock, uint32_t flags);
187+
spin_lock_irqsave(&memmap_lock_storage, &flags);
188+
for (uint32_t i = s; i < e; ++i) {
189+
bitmap_set(i);
190+
}
191+
spin_unlock_irqrestore(&memmap_lock_storage, flags);
153192
}
154193
}
155194

src/kernel/tests/alloc_irq_test.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <tests/define.h>
2+
3+
#ifdef ALLOC_IRQ_TEST
4+
5+
#include <config.h>
6+
#include <console.h>
7+
#include <interrupt/irq.h>
8+
#include <mem/map.h>
9+
10+
static void irq_alloc_handler(uint32_t payload, void* ctx) {
11+
(void)ctx; (void)payload;
12+
void* a = alloc_frame();
13+
if (a) {
14+
printk("alloc_irq: alloc_frame -> %x\n", (unsigned)a);
15+
free_frame(a);
16+
printk("alloc_irq: free_frame done\n");
17+
} else {
18+
printk("alloc_irq: alloc_frame failed\n");
19+
}
20+
}
21+
22+
void alloc_irq_test() {
23+
/* register handler to IRQ 2 (arbitrary) */
24+
interrupt_register(2, irq_alloc_handler, NULL);
25+
26+
/* simulate IRQ 2 events */
27+
for (int i = 0; i < 4; ++i) {
28+
interrupt_raise((2u << 16) | (uint32_t)i);
29+
}
30+
31+
/* dispatch queued events synchronously */
32+
interrupt_dispatch_all();
33+
}
34+
35+
#endif /* ALLOC_IRQ_TEST */

src/kernel/tests/run.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@ void run_test() {
77
printk("> MEMORY TEST\n");
88
mem_test();
99
#endif
10+
1011
new_line();
12+
1113
#ifdef INTERRUPT_TEST
1214
printk("> INTERRUPT TEST\n");
1315
interrupt_test();
1416
#endif
17+
18+
new_line();
19+
1520
#ifdef INTERRUPT_VECTOR_TEST
1621
printk("> INTERRUPT VECTOR TEST\n");
1722
interrupt_vector_test();
1823
#endif
24+
25+
new_line();
26+
27+
#ifdef ALLOC_IRQ_TEST
28+
printk("> ALOOC IRQ TEST\n");
29+
alloc_irq_test();
30+
#endif
1931
}

0 commit comments

Comments
 (0)