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

Commit edb3f8c

Browse files
committed
fix bug
1 parent 573118f commit edb3f8c

2 files changed

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

src/kernel/mem/paging.c

Lines changed: 29 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,6 @@
33
#include <mem/paging.h>
44
#include <mem/map.h>
55
#include <stddef.h>
6-
7-
/* helpers: in current identity-map boot situation, phys==virt, so these are identity
8-
* conversions. If kernel later runs with higher-half mapping, update these helpers
9-
* to translate between physical and kernel virtual addresses accordingly.
10-
*/
11-
static inline void *phys_to_virt(uint32_t phys) {
12-
return (void *)phys;
13-
}
14-
static inline uint32_t virt_to_phys(void *virt) {
15-
return (uint32_t)virt;
16-
}
176
#include <console.h>
187
#include <interrupt/irq.h>
198

@@ -37,7 +26,11 @@ void *alloc_page_table(void) {
3726
return frame;
3827
}
3928

40-
// 1つの4KBページをマッピングする: phys -> virt(flags付き)。成功時0、失敗時-1
29+
/**
30+
* @fn map_page
31+
* @brief 物理アドレスphysを仮想アドレスvirtにflags属性でマッピングする
32+
* @return 0: 成功 -1: 失敗
33+
*/
4134
int map_page(uint32_t phys, uint32_t virt, uint32_t flags) {
4235
if ((flags & PAGING_PRESENT) == 0) flags |= PAGING_PRESENT;
4336
uint32_t pd_idx = (virt >> 22) & 0x3FF;
@@ -59,7 +52,12 @@ int map_page(uint32_t phys, uint32_t virt, uint32_t flags) {
5952
return 0;
6053
}
6154

62-
// 1つの4KBページをアンマップする。成功時0、未マップ時-1
55+
/**
56+
* @fn unmap_page
57+
* @brief 仮想アドレスvirtに対応するページをアンマップする
58+
* @param virt アンマップする仮想アドレス
59+
* @return 0: 成功 -1: 失敗
60+
*/
6361
int unmap_page(uint32_t virt) {
6462
uint32_t pd_idx = (virt >> 22) & 0x3FF;
6563
uint32_t pt_idx = (virt >> 12) & 0x3FF;
@@ -73,48 +71,32 @@ int unmap_page(uint32_t virt) {
7371
return 0;
7472
}
7573

76-
// 指定されたMB数分のメモリを同一マッピングで初期化する
74+
/**
75+
* @fn paging_init_identity
76+
* @brief 最初のmap_mb MB分を同一マッピングで初期化する
77+
* @param map_mb 同一マッピングするMB数
78+
*/
7779
void paging_init_identity(uint32_t map_mb) {
78-
/* map_mb MB (rounded up to full pages) as identity mapping */
7980
uint32_t pages = ((map_mb * 1024u * 1024u) + 0xFFF) / 0x1000;
80-
uint32_t pages_remaining = pages;
81-
82-
/* number of page tables needed (1024 entries per table -> 4MB each) */
83-
uint32_t needed_pt = (pages + 1023) / 1024;
84-
85-
/* initialize page directory to not present */
86-
for (uint32_t i = 0; i < 1024; ++i) page_directory[i] = 0x00000000;
8781

88-
for (uint32_t pt_index = 0; pt_index < needed_pt; ++pt_index) {
89-
/* use first_table for pt_index==0 for static allocation */
90-
uint32_t *pt = NULL;
91-
if (pt_index == 0) {
92-
pt = first_table;
93-
} else {
94-
void *alloc = alloc_page_table();
95-
if (!alloc) {
96-
printk("paging: failed to alloc page table for pt_index=%u\n", (unsigned)pt_index);
97-
break;
98-
}
99-
pt = (uint32_t *)alloc;
100-
}
82+
// 最初のテーブル: 最初の4MBを同一マッピング(1024エントリ)
83+
for (uint32_t i = 0; i < 1024; ++i) {
84+
first_table[i] = (i * 0x1000) | 3u; // present + rw
85+
}
10186

102-
/* fill table entries for this PT */
103-
for (uint32_t i = 0; i < 1024 && pages_remaining > 0; ++i, --pages_remaining) {
104-
uint32_t phys = (pt_index * 1024u + i) * 0x1000u;
105-
pt[i] = (phys & 0xFFFFF000u) | (PAGING_PRESENT | PAGING_RW);
106-
}
87+
// ディレクトリエントリ0はfirst_tableを指す
88+
page_directory[0] = ((uint32_t)first_table) | 3u;
10789

108-
/* write PDE: PDE must contain physical address of pt */
109-
uint32_t pt_phys = virt_to_phys((void *)pt);
110-
page_directory[pt_index] = (pt_phys & 0xFFFFF000u) | (PAGING_PRESENT | PAGING_RW);
111-
}
90+
// 残りのPDEをnot presentにする
91+
for (uint32_t i = 1; i < 1024; ++i) page_directory[i] = 0x00000000;
11292

113-
printk("paging: identity map initialized for %u MB (pages=%u, tables=%u)\n",
114-
(unsigned)map_mb, (unsigned)pages, (unsigned)needed_pt);
93+
printk("paging: identity map initialized for %u MB (pages=%u)\n", (unsigned)map_mb, (unsigned)pages);
11594
}
11695

117-
// ページング機能を有効化する
96+
/**
97+
* @fn paging_enable
98+
* @brief ページングを有効化
99+
*/
118100
void paging_enable(void) {
119101
// CR3をロード
120102
asm volatile("mov %%eax, %%cr3" :: "a"((uint32_t)page_directory));
@@ -125,7 +107,6 @@ void paging_enable(void) {
125107
asm volatile("mov %%eax, %%cr0" :: "a" (cr0));
126108
}
127109

128-
// ページフォルト発生時のハンドラ
129110
void page_fault_handler(uint32_t vec) {
130111
(void)vec;
131112
uint32_t fault_addr;

0 commit comments

Comments
 (0)