1414
1515static 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// 管理情報をまとめた構造体
1823static 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 */
127152uint32_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
0 commit comments