@@ -27,11 +27,6 @@ int map_page_64(uint64_t pml4_phys, uint64_t phys, uint64_t virt,
2727 if ((flags & PAGING_PRESENT ) == 0 )
2828 flags |= PAGING_PRESENT ;
2929
30- /* Debug: print incoming args to help diagnose caller/ABI issues */
31- printk ("map_page_64: enter pml4_phys=0x%016lx phys=0x%016lx virt=0x%016lx flags=0x%08x\n" ,
32- (unsigned long )pml4_phys , (unsigned long )phys ,
33- (unsigned long )virt , (unsigned )flags );
34-
3530 // 64ビットページングのインデックス計算
3631 uint64_t pml4_idx = (virt >> 39 ) & 0x1FF ; // bits 47-39
3732 uint64_t pdpt_idx = (virt >> 30 ) & 0x1FF ; // bits 38-30
@@ -111,11 +106,6 @@ int map_page_64(uint64_t pml4_phys, uint64_t phys, uint64_t virt,
111106 PAGING_PRESENT | PAGING_RW | PAGING_USER ;
112107 entry &= ~(1ULL << 63 ); // NXビットをクリア
113108 pdpt [pdpt_idx ] = entry ;
114- /* Debug: print the PD we just created for this PDPT index */
115- printk ("map_page_64: created PD: pd_phys=0x%016lx pd_virt=0x%016lx pdpt_idx=%lu\n" ,
116- (unsigned long )pd_phys ,
117- (unsigned long )(uintptr_t )pd_virt ,
118- (unsigned long )pdpt_idx );
119109 } else {
120110 // 既存のエントリ - NXビットをクリア
121111 pdpt [pdpt_idx ] &= ~(1ULL << 63 );
@@ -156,44 +146,43 @@ int map_page_64(uint64_t pml4_phys, uint64_t phys, uint64_t virt,
156146 entry &= ~(1ULL << 63 ); // NXビットをクリア
157147 pd [pd_idx ] = entry ;
158148 } else if (pd [pd_idx ] & (1ULL << 7 )) {
159- // 既存エントリが2MBラージページ(PS bit=1)の場合:分割する
160- printk ("map_page_64: Breaking 2MB large page at PD[%lu] into 4KB pages\n" ,
161- (unsigned long )pd_idx );
162-
163149 uint64_t large_page_base = pd [pd_idx ] & 0xFFFFFFFFFFE00000ULL ;
164150 uint64_t large_page_flags = pd [pd_idx ] & 0xFFF ;
165-
151+
166152 // 新しいPTを割り当て
167153 void * pt_virt = alloc_page_table ();
168154 if (!pt_virt ) {
169155 printk ("map_page_64: Failed to allocate PT for page split\n" );
170156 return -1 ;
171157 }
172158 uint64_t * pt_split = (uint64_t * )pt_virt ;
173-
159+
174160 // 2MBラージページを512個の4KBページに分割
175161 for (int i = 0 ; i < 512 ; i ++ ) {
176- uint64_t page_phys = large_page_base + ((uint64_t )i * 0x1000 );
162+ uint64_t page_phys =
163+ large_page_base + ((uint64_t )i * 0x1000 );
177164 // PS bitを除去し、他のフラグは保持
178165 pt_split [i ] = (page_phys & 0xFFFFFFFFFFFFF000ULL ) |
179166 (large_page_flags & ~(1ULL << 7 ));
180167 }
181-
182- uint64_t pt_phys = vmem_virt_to_phys64 ((uint64_t )(uintptr_t )pt_virt );
168+
169+ uint64_t pt_phys =
170+ vmem_virt_to_phys64 ((uint64_t )(uintptr_t )pt_virt );
183171 if (pt_phys == UINT64_MAX ) {
184172 printk ("map_page_64: vmem_virt_to_phys64 failed for pt_virt=0x%016lx\n" ,
185173 (unsigned long )(uintptr_t )pt_virt );
186174 return -1 ;
187175 }
188-
176+
189177 // PDエントリを新しいPTを指すように更新(PS bitをクリア)
190178 pd [pd_idx ] = (pt_phys & 0xFFFFFFFFFFFFF000ULL ) |
191179 PAGING_PRESENT | PAGING_RW | PAGING_USER ;
192180 pd [pd_idx ] &= ~(1ULL << 63 ); // NXビットをクリア
193-
181+
194182 // TLB を無効化(2MB分)
195183 for (uint64_t i = 0 ; i < 512 ; i ++ ) {
196- uint64_t flush_addr = (virt & 0xFFFFFFFFFFE00000ULL ) + (i * 0x1000 );
184+ uint64_t flush_addr =
185+ (virt & 0xFFFFFFFFFFE00000ULL ) + (i * 0x1000 );
197186 invlpg ((void * )(uintptr_t )flush_addr );
198187 }
199188 } else {
@@ -216,12 +205,6 @@ int map_page_64(uint64_t pml4_phys, uint64_t phys, uint64_t virt,
216205 uint64_t entry = (phys & 0xFFFFFFFFFFFFF000ULL ) | (flags & 0xFFF );
217206 entry &= ~(1ULL << 63 ); // NXビットをクリア(実行可能)
218207 pt [pt_idx ] = entry ;
219- /* Debug: verify write */
220- printk ("map_page_64: set PTE: pml4_phys=0x%016lx pml4_idx=0x%03lx pdpt_idx=0x%03lx pd_idx=0x%03lx pt_idx=0x%03lx phys=0x%016lx entry=0x%016lx readback=0x%016lx\n" ,
221- (unsigned long )pml4_phys , (unsigned long )pml4_idx ,
222- (unsigned long )pdpt_idx , (unsigned long )pd_idx ,
223- (unsigned long )pt_idx , (unsigned long )phys , (unsigned long )entry ,
224- (unsigned long )pt [pt_idx ]);
225208
226209 // TLBを無効化
227210 invlpg ((void * )(uintptr_t )virt );
@@ -336,14 +319,6 @@ void paging64_init_kernel_pml4(void) {
336319
337320 // カーネルPML4の物理アドレスを保存
338321 kernel_pml4_phys = new_pml4_phys ;
339-
340- printk ("paging64_init_kernel_pml4: Switched from UEFI PML4 (0x%016lx) to kernel PML4 (0x%016lx)\n" ,
341- uefi_cr3 , new_pml4_phys );
342- /* Debug: print the newly created PML4[0] so we can verify the low-4GB
343- * identity mapping was installed correctly. */
344- printk ("paging64_init_kernel_pml4: new_pml4[0]=0x%016lx uefi_pml4[0]=0x%016lx\n" ,
345- (unsigned long )new_pml4 [0 ], (unsigned long )uefi_pml4 [0 ]);
346- printk ("paging64_init_kernel_pml4: Added identity mapping for low 4GB (0x0-0xFFFFFFFF)\n" );
347322}
348323
349324/**
@@ -402,7 +377,7 @@ uint64_t paging64_create_user_pml4(void) {
402377 new_pml4 [i ] = kernel_pml4 [i ];
403378 }
404379
405- /*
380+ /*
406381 * IMPORTANT: Copy kernel_pml4[0] to enable access to kernel code/data
407382 * in low memory (identity-mapped region) even after CR3 switch.
408383 * Without this, iretq instruction itself (which is in kernel code at low
@@ -411,10 +386,7 @@ uint64_t paging64_create_user_pml4(void) {
411386 * User mappings at low addresses will still work because map_page_64
412387 * creates 4KB page tables which override the 2MB large pages from kernel.
413388 */
414- new_pml4 [0 ] = kernel_pml4 [0 ];
415- printk ("paging64_create_user_pml4: kernel_pml4_phys=0x%016lx kernel_pml4[0]=0x%016lx new_pml4[0]=0x%016lx\n" ,
416- (unsigned long )kernel_pml4_phys , (unsigned long )kernel_pml4 [0 ],
417- (unsigned long )new_pml4 [0 ]);
389+ new_pml4 [0 ] = kernel_pml4 [0 ];
418390
419391 return new_pml4_phys ;
420392}
0 commit comments