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

Commit 4e749ae

Browse files
committed
add TSS logic
1 parent 3b315ff commit 4e749ae

24 files changed

Lines changed: 1048 additions & 330 deletions

Makefile

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,16 +120,16 @@ $(K_OUT_DIR)/%.o: $(SRC_KERNEL)/%.asm
120120

121121
$(OUT_DIR)/usr/%.o: $(SRC_USER)/%.c
122122
@mkdir -p $(dir $@)
123-
@$(CC) $(CFLAGS) -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
123+
@$(CC) $(CFLAGS) -fcf-protection=none -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
124124

125125
$(APP_OUT_DIR)/%.o: $(SRC_APPS)/%.c
126126
@mkdir -p $(dir $@)
127-
@$(CC) $(CFLAGS) -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
127+
@$(CC) $(CFLAGS) -fcf-protection=none -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
128128

129129

130130
$(APP_OUT_DIR)/syscall.o: $(SRC_USER)/syscall.c
131131
@mkdir -p $(dir $@)
132-
@$(CC) $(CFLAGS) -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
132+
@$(CC) $(CFLAGS) -fcf-protection=none -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
133133

134134
$(APP_OUT_DIR)/crt.o: $(CRT_SRC)
135135
@mkdir -p $(dir $@)
@@ -141,7 +141,7 @@ $(APP_OUT_DIR)/crt.o: $(CRT_SRC)
141141

142142
$(APP_OUT_DIR)/stdio.o: $(SRC_USER)/stdio_stub.c
143143
@mkdir -p $(dir $@)
144-
@$(CC) $(CFLAGS) -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
144+
@$(CC) $(CFLAGS) -fcf-protection=none -D_FORTIFY_SOURCE=0 -fno-builtin -I$(BIN_LIB_DIR)/targ-include -c $< -o $@
145145

146146

147147
user: lib $(ALL_USER_ELFS) $(APP_ELFS)
@@ -161,6 +161,11 @@ $(OUT_DIR)/usr/hello.elf: $(OUT_DIR)/usr/hello.o $(OUT_DIR)/usr/syscall.o
161161
@echo "Linking user ELF (hello, no -lc): $@"
162162
@$(CC) -nostdlib -static $^ -o $@
163163

164+
$(APP_OUT_DIR)/test.elf: $(APP_OUT_DIR)/test.o
165+
@mkdir -p $(dir $@)
166+
@echo "Linking minimal test ELF: $@"
167+
@$(CC) -nostdlib -static -no-pie -Wl,--build-id=none $< -o $@
168+
164169
$(OUT_DIR)/usr/malloc_printf_test.elf: $(OUT_DIR)/usr/malloc_printf_test.o $(OUT_DIR)/usr/syscall.o
165170
@mkdir -p $(dir $@)
166171
@echo "Linking user ELF: $@"

docs/_config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
title: LiteCore Document
2-
description: シンプル、軽量、高速なオペレーティングシステムLiteCoreのドキュメント
1+
title: liteCore Document
2+
description: シンプル、軽量、高速なオペレーティングシステムliteCoreのドキュメント
33

44

55
domain: core.nekogakure.jp

src/apps/test.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
void _start(void) {
2+
while (1) {
3+
asm volatile("nop");
4+
}
5+
}

src/kernel/driver/ata.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,6 @@ int ata_read_sectors(uint8_t drive, uint32_t lba, uint8_t sectors,
215215
/* ベースアドレスとドライブ選択を取得 */
216216
ata_get_base(drive, &base, &drive_sel);
217217

218-
/* Diagnostic: announce ATA read operation start */
219-
printk("ATA: ata_read_sectors start (drive=%u lba=%u sectors=%u)\n",
220-
drive, lba, sectors);
221-
222218
/* 割り込みを無効化 */
223219
outb(base + 0x206, 0x02);
224220

@@ -241,8 +237,6 @@ int ata_read_sectors(uint8_t drive, uint32_t lba, uint8_t sectors,
241237

242238
/* READコマンドを送信 */
243239
outb(base + 7, ATA_CMD_READ_PIO);
244-
printk("ATA: READ_PIO sent (base=0x%x lba=%u sectors=%u drive=%u)\n",
245-
base, lba, sectors, drive);
246240

247241
/* 各セクタを読み取る */
248242
for (int s = 0; s < sectors; s++) {

src/kernel/fs/block_cache.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,6 @@ int block_cache_read(struct block_cache *cache, uint32_t block_num,
188188
}
189189

190190
/* ディスクから読み込む */
191-
printk("BlockCache: reading block %u (block_size=%u drive=%u)\n",
192-
block_num, cache->block_size, cache->drive);
193191
if (read_block_from_disk(cache, block_num, lru->data) != 0) {
194192
/* Diagnostic: report block read failure */
195193
printk("BlockCache: failed to read block %u (block_size=%u entries=%u drive=%u)\n",

src/kernel/fs/fat/fat16.c

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,7 @@ static int fat16_find_root_entry_bytes(struct fat16_super *sb, const char *name,
4242
uint8_t *sec = fat16_sector_scratch;
4343
uint8_t shortname[11];
4444
make_shortname(name, (char *)shortname);
45-
/* Debug: log the requested shortname for lookup */
46-
{
47-
char dbgname[12];
48-
for (int i = 0; i < 11; ++i)
49-
dbgname[i] = shortname[i] ? shortname[i] : ' ';
50-
dbgname[11] = '\0';
51-
printk("fat16: find_root_entry looking for '%s' (orig='%s')\n",
52-
dbgname, name);
53-
}
45+
5446
uint32_t first_free_off = 0xFFFFFFFF;
5547
for (uint32_t s = 0; s < sectors; ++s) {
5648
if (fat16_read_sector(sb, root_sector + s, sec) != 0) {
@@ -411,10 +403,8 @@ static int fat16_resolve_path_bytes(struct fat16_super *sb, const char *path,
411403
comp[ci] = '\0';
412404
while (*p == '/')
413405
p++;
406+
414407
int is_last = (*p == '\0');
415-
/* Debug: component being resolved */
416-
printk("fat16: resolve component '%s' (is_last=%d) dir_cluster=%u\n",
417-
comp, is_last, dir_cluster);
418408
int r;
419409
uint32_t found_off = 0;
420410
uint8_t local_ent[32];
@@ -755,14 +745,12 @@ int fat16_get_file_size(struct fat16_super *sb, const char *name,
755745
uint32_t *out_size) {
756746
if (!sb || !name || !out_size)
757747
return -1;
758-
printk("fat16: fat16_get_file_size called for '%s'\n", name);
759748
uint8_t ent_buf[32];
760749
uint32_t ent_off = 0;
761750
uint32_t free_off = 0;
762751
uint16_t parent = 0;
763752
int r = fat16_resolve_path_bytes(sb, name, ent_buf, &ent_off, &free_off,
764753
&parent);
765-
printk("fat16: fat16_get_file_size -> resolve r=%d\n", r);
766754
if (r != 0)
767755
return -2;
768756
uint32_t size = le32(ent_buf + 28);
@@ -802,8 +790,6 @@ int fat16_read_file(struct fat16_super *sb, const char *name, void *buf,
802790
return -2;
803791
uint16_t start_cluster = le16(ent_buf + 26);
804792
uint32_t file_size = le32(ent_buf + 28);
805-
printk("fat16: read_file name='%s' start_cluster=%u file_size=%u len=%u\n",
806-
name, start_cluster, file_size, (uint32_t)len);
807793
if (file_size == 0) {
808794
if (out_len)
809795
*out_len = 0;

src/kernel/fs/vfs.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ int vfs_write(int fd, const void *buf, size_t len) {
277277
int vfs_read(int fd, void *buf, size_t len) {
278278
if (buf == NULL)
279279
return -1;
280-
printk("vfs: vfs_read called fd=%d len=%u\n", fd, (uint32_t)len);
281280
if (fd == 0) {
282281
char *out = (char *)buf;
283282
size_t i;
@@ -504,8 +503,7 @@ int vfs_read_file_all(const char *path, void **out_buf, uint32_t *out_size) {
504503
* backend read failures (e.g. block cache / ATA hiccups). */
505504
for (int attempt = 0; attempt < 3; ++attempt) {
506505
int gret = active_backend->get_file_size(active_sb, path, &sz);
507-
printk("vfs: vfs_read_file_all attempt %d get_file_size('%s') -> rc=%d sz=%u\n",
508-
attempt, path, gret, sz);
506+
509507
if (gret != 0)
510508
continue;
511509
if (sz == 0) {
@@ -518,9 +516,6 @@ int vfs_read_file_all(const char *path, void **out_buf, uint32_t *out_size) {
518516
uint32_t heap_largest = heap_largest_free_block();
519517
/* Allocate extra space aligned to page boundary for safety */
520518
uint32_t alloc_size = ((sz + 4095) / 4096) * 4096;
521-
printk("vfs: kmalloc request %u bytes (file size %u). heap_free=%u largest=%u\n",
522-
alloc_size, (uint32_t)sz, heap_free,
523-
heap_largest);
524519
}
525520
/* Allocate page-aligned buffer to avoid memory corruption issues */
526521
uint32_t alloc_size = ((sz + 4095) / 4096) * 4096;
@@ -535,8 +530,6 @@ int vfs_read_file_all(const char *path, void **out_buf, uint32_t *out_size) {
535530
out_len = 0;
536531
int rret = active_backend->read_file(active_sb, path, buf, sz,
537532
&out_len);
538-
printk("vfs: vfs_read_file_all attempt %d read_file('%s') -> rc=%d out_len=%u\n",
539-
attempt, path, rret, (uint32_t)out_len);
540533
if (rret == 0) {
541534
((uint8_t *)buf)[out_len] = '\0';
542535
*out_buf = buf;

src/kernel/interrupt/idt.c

Lines changed: 133 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -122,31 +122,79 @@ extern void page_fault_handler(uint32_t vec);
122122
extern void page_fault_handler_ex(uint32_t vec, uint32_t error_code,
123123
uint32_t eip);
124124

125-
void irq_exception_c(uint32_t vec) {
126-
if (vec == 14) {
127-
page_fault_handler(vec);
128-
} else {
129-
/* other exceptions: just spin */
130-
printk("exception vec=%u\n", (unsigned)vec);
131-
while (1) {
132-
}
133-
}
134-
}
135-
125+
/**
126+
* @fn irq_exception_ex
127+
* @brief 例外発生時の拡張ハンドラ
128+
*/
136129
/**
137130
* @fn irq_exception_ex
138131
* @brief 例外発生時の拡張ハンドラ
139132
*/
140133
void irq_exception_ex(uint32_t vec, uint32_t error_code) {
134+
const char *exception_names[] = { "Divide by Zero",
135+
"Debug",
136+
"NMI",
137+
"Breakpoint",
138+
"Overflow",
139+
"Bound Range Exceeded",
140+
"Invalid Opcode",
141+
"Device Not Available",
142+
"Double Fault",
143+
"Coprocessor Segment Overrun",
144+
"Invalid TSS",
145+
"Segment Not Present",
146+
"Stack-Segment Fault",
147+
"General Protection Fault",
148+
"Page Fault",
149+
"Reserved",
150+
"x87 FPU Error",
151+
"Alignment Check",
152+
"Machine Check",
153+
"SIMD FP Exception",
154+
"Virtualization Exception",
155+
"Control Protection Exception" };
156+
const char *name = (vec < 22) ? exception_names[vec] :
157+
"Unknown Exception";
158+
159+
printk("\n!!! CPU EXCEPTION !!!\n");
160+
printk("Exception: %s (vector %u)\n", name, (unsigned)vec);
161+
printk("Error code: 0x%x\n", (unsigned)error_code);
162+
141163
if (vec == 14) {
142-
// C言語からEIPを正確に取得するのは難しいため、ここでは0を渡す
143-
page_fault_handler_ex(vec, error_code, 0);
144-
} else {
145-
printk("exception ex vec=%u err=0x%x\n", (unsigned)vec,
146-
(unsigned)error_code);
147-
while (1) {
164+
// Page Fault - print CR2
165+
uint64_t fault_addr;
166+
asm volatile("mov %%cr2, %0" : "=r"(fault_addr));
167+
printk("Page Fault at address: 0x%lx\n", fault_addr);
168+
printk("Error code bits: P=%d W=%d U=%d R=%d I=%d\n",
169+
error_code & 1, (error_code >> 1) & 1,
170+
(error_code >> 2) & 1, (error_code >> 3) & 1,
171+
(error_code >> 4) & 1);
172+
} else if (vec == 13) {
173+
// GPF
174+
printk("GPF Error code breakdown:\n");
175+
printk(" External: %d\n", (error_code >> 0) & 1);
176+
printk(" IDT: %d\n", (error_code >> 1) & 1);
177+
printk(" TI: %d\n", (error_code >> 2) & 1);
178+
printk(" Selector Index: 0x%x\n", (error_code >> 3) & 0x1FFF);
179+
}
180+
181+
uint64_t rsp, cr3;
182+
asm volatile("mov %%rsp, %0" : "=r"(rsp));
183+
asm volatile("mov %%cr3, %0" : "=r"(cr3));
184+
printk("RSP=0x%lx CR3=0x%lx\n", rsp, cr3);
185+
186+
// スタック上のiretqフレームを表示(可能であれば)
187+
if (vec == 6) { // Invalid Opcode
188+
printk("Attempting to read iretq frame from stack:\n");
189+
uint64_t *stack_ptr = (uint64_t *)rsp;
190+
for (int i = 0; i < 8; i++) {
191+
printk(" [RSP+%d] = 0x%016lx\n", i * 8, stack_ptr[i]);
148192
}
149193
}
194+
195+
while (1) {
196+
asm volatile("hlt");
197+
}
150198
}
151199

152200
/**
@@ -156,6 +204,40 @@ void irq_exception_ex(uint32_t vec, uint32_t error_code) {
156204
void idt_init(void) {
157205
pic_remap();
158206

207+
/* CPU例外ハンドラ (0-31) */
208+
extern void isr0(void);
209+
extern void isr1(void);
210+
extern void isr2(void);
211+
extern void isr3(void);
212+
extern void isr4(void);
213+
extern void isr5(void);
214+
extern void isr6(void);
215+
extern void isr7(void);
216+
extern void isr8(void);
217+
extern void isr9(void);
218+
extern void isr10(void);
219+
extern void isr11(void);
220+
extern void isr12(void);
221+
extern void isr13(void);
222+
extern void isr14(void);
223+
extern void isr15(void);
224+
extern void isr16(void);
225+
extern void isr17(void);
226+
extern void isr18(void);
227+
extern void isr19(void);
228+
extern void isr20(void);
229+
extern void isr21(void);
230+
extern void isr22(void);
231+
extern void isr23(void);
232+
extern void isr24(void);
233+
extern void isr25(void);
234+
extern void isr26(void);
235+
extern void isr27(void);
236+
extern void isr28(void);
237+
extern void isr29(void);
238+
extern void isr30(void);
239+
extern void isr31(void);
240+
159241
/* 明示的に各isrシンボルをextern宣言し、それをIDTに登録する(脳筋だぜぇ〜www */
160242
extern void isr32(void);
161243
extern void isr33(void);
@@ -177,7 +259,41 @@ void idt_init(void) {
177259

178260
extern void isr128(void);
179261

262+
/* CPU例外を登録 */
263+
idt_set_gate(0, (uint64_t)isr0);
264+
idt_set_gate(1, (uint64_t)isr1);
265+
idt_set_gate(2, (uint64_t)isr2);
266+
idt_set_gate(3, (uint64_t)isr3);
267+
idt_set_gate(4, (uint64_t)isr4);
268+
idt_set_gate(5, (uint64_t)isr5);
269+
idt_set_gate(6, (uint64_t)isr6);
270+
idt_set_gate(7, (uint64_t)isr7);
271+
idt_set_gate(8, (uint64_t)isr8);
272+
idt_set_gate(9, (uint64_t)isr9);
273+
idt_set_gate(10, (uint64_t)isr10);
274+
idt_set_gate(11, (uint64_t)isr11);
275+
idt_set_gate(12, (uint64_t)isr12);
276+
idt_set_gate(13, (uint64_t)isr13);
180277
idt_set_gate(14, (uint64_t)isr14); /* page fault */
278+
idt_set_gate(15, (uint64_t)isr15);
279+
idt_set_gate(16, (uint64_t)isr16);
280+
idt_set_gate(17, (uint64_t)isr17);
281+
idt_set_gate(18, (uint64_t)isr18);
282+
idt_set_gate(19, (uint64_t)isr19);
283+
idt_set_gate(20, (uint64_t)isr20);
284+
idt_set_gate(21, (uint64_t)isr21);
285+
idt_set_gate(22, (uint64_t)isr22);
286+
idt_set_gate(23, (uint64_t)isr23);
287+
idt_set_gate(24, (uint64_t)isr24);
288+
idt_set_gate(25, (uint64_t)isr25);
289+
idt_set_gate(26, (uint64_t)isr26);
290+
idt_set_gate(27, (uint64_t)isr27);
291+
idt_set_gate(28, (uint64_t)isr28);
292+
idt_set_gate(29, (uint64_t)isr29);
293+
idt_set_gate(30, (uint64_t)isr30);
294+
idt_set_gate(31, (uint64_t)isr31);
295+
296+
/* IRQハンドラ */
181297
idt_set_gate(32, (uint64_t)isr32);
182298
idt_set_gate(33, (uint64_t)isr33);
183299
idt_set_gate(34, (uint64_t)isr34);

0 commit comments

Comments
 (0)