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

Commit 88e9d97

Browse files
committed
install kernel GDT
1 parent f8c6a38 commit 88e9d97

8 files changed

Lines changed: 129 additions & 1 deletion

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

src/include/config.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
*/
2626
typedef unsigned char uint8_t;
2727

28+
/**
29+
* @typedef uint16_t
30+
* @brief 16ビット符号なし整数型
31+
*/
32+
typedef unsigned short uint16_t;
33+
2834
/**
2935
* @typedef uint32_t
3036
* @brief 32bit符号なし整数型

src/include/mem/segment.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef _MEM_SEGMENT_H
2+
#define _MEM_SEGMENT_H
3+
4+
void gdt_install();
5+
6+
#endif /* _MEM_SEGMENT_H */

src/include/tests/define.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
#define INTERRUPT_VECTOR_TEST
1414
// 割り込みハンドラでメモリのテスト
1515
#define ALLOC_IRQ_TEST
16+
// GDT(セグメント)再構築テスト
17+
#define GDT_TEST
1618

1719
/* -------- 宣言 -------- */
1820
#ifdef MEM_TEST
@@ -31,6 +33,10 @@ void interrupt_vector_test();
3133
void alloc_irq_test();
3234
#endif /* ALLOC_IRQ_TEST */
3335

36+
#ifdef GDT_TEST
37+
void gdt_test();
38+
#endif /* GDT_TEST */
39+
3440
#endif /* TEST_TRUE */
3541

3642
#endif /* _TESTS_CONFIG_H */

src/kernel/main.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <interrupt/idt.h>
77
#include <mem/map.h>
88
#include <mem/manager.h>
9+
#include <mem/segment.h>
910

1011
#include <tests/define.h>
1112
#include <tests/run.h>
@@ -18,6 +19,7 @@ void kloop();
1819
*/
1920
void kmain() {
2021
console_init();
22+
gdt_install();
2123

2224
printk("Welcome to Litecore kernel!\n");
2325
printk(" Version: %s\n", VERSION);

src/kernel/mem/segment.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <config.h>
2+
#include <stdint.h>
3+
4+
/* GDTエントリ構造体 */
5+
struct gdt_entry {
6+
uint16_t limit_low;
7+
uint16_t base_low;
8+
uint8_t base_middle;
9+
uint8_t access;
10+
uint8_t granularity;
11+
uint8_t base_high;
12+
} __attribute__((packed));
13+
14+
struct gdt_ptr {
15+
uint16_t limit;
16+
uint32_t base;
17+
} __attribute__((packed));
18+
19+
/* ここではNULL, kernel code, kernel data, user code, user dataの5つ */
20+
static struct gdt_entry gdt_entries[5];
21+
static struct gdt_ptr gp;
22+
23+
static void gdt_set_gate(int num, uint32_t base, uint32_t limit, uint8_t access, uint8_t gran) {
24+
gdt_entries[num].base_low = (base & 0xFFFF);
25+
gdt_entries[num].base_middle = (base >> 16) & 0xFF;
26+
gdt_entries[num].base_high = (base >> 24) & 0xFF;
27+
28+
gdt_entries[num].limit_low = (limit & 0xFFFF);
29+
gdt_entries[num].granularity = (limit >> 16) & 0x0F;
30+
31+
gdt_entries[num].granularity |= (gran & 0xF0);
32+
gdt_entries[num].access = access;
33+
}
34+
35+
/**
36+
* @fn gdt_install
37+
* @brief カーネル側でGDTを再構築してロードする
38+
*/
39+
void gdt_install() {
40+
gdt_set_gate(0, 0, 0, 0, 0);
41+
42+
/* Kernel code segment: base=0, limit=0xFFFFF, access=0x9A, gran=0xCF */
43+
gdt_set_gate(1, 0x0, 0xFFFFF, 0x9A, 0xCF);
44+
/* Kernel data segment */
45+
gdt_set_gate(2, 0x0, 0xFFFFF, 0x92, 0xCF);
46+
/* User code (ring3) */
47+
gdt_set_gate(3, 0x0, 0xFFFFF, 0xFA, 0xCF);
48+
/* User data (ring3) */
49+
gdt_set_gate(4, 0x0, 0xFFFFF, 0xF2, 0xCF);
50+
51+
gp.limit = (sizeof(struct gdt_entry) * 5) - 1;
52+
gp.base = (uint32_t)&gdt_entries;
53+
54+
asm volatile(
55+
"lgdt (%0)\n"
56+
"pushl $0x08\n"
57+
"pushl $1f\n"
58+
"lret\n"
59+
"1:\n"
60+
"mov $0x10, %%ax\n"
61+
"mov %%ax, %%ds\n"
62+
"mov %%ax, %%es\n"
63+
"mov %%ax, %%fs\n"
64+
"mov %%ax, %%gs\n"
65+
"mov %%ax, %%ss\n"
66+
:: "r"(&gp) : "eax", "memory"
67+
);
68+
}

src/kernel/tests/gdt_test.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <tests/define.h>
2+
3+
#ifdef GDT_TEST
4+
5+
#include <config.h>
6+
#include <console.h>
7+
8+
static inline uint16_t read_cs(void) {
9+
uint16_t sel;
10+
asm volatile ("mov %%cs, %0" : "=r" (sel));
11+
return sel;
12+
}
13+
14+
static inline uint16_t read_ds(void) {
15+
uint16_t sel;
16+
asm volatile ("mov %%ds, %0" : "=r" (sel));
17+
return sel;
18+
}
19+
20+
void gdt_test() {
21+
uint16_t cs = read_cs();
22+
uint16_t ds = read_ds();
23+
printk("> GDT TEST\n");
24+
printk("CS = 0x%x\n", (unsigned)cs);
25+
printk("DS = 0x%x\n", (unsigned)ds);
26+
/* check expected values: kernel code=0x08, data=0x10 */
27+
if (cs == 0x08 && ds == 0x10) {
28+
printk("GDT test: OK\n");
29+
} else {
30+
printk("GDT test: FAILED (cs=0x%x ds=0x%x)\n", (unsigned)cs, (unsigned)ds);
31+
}
32+
}
33+
34+
#endif /* GDT_TEST */

src/kernel/tests/run.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,10 @@ void run_test() {
2828
printk("> ALOOC IRQ TEST\n");
2929
alloc_irq_test();
3030
#endif
31+
32+
new_line();
33+
34+
#ifdef GDT_TEST
35+
gdt_test();
36+
#endif
3137
}

0 commit comments

Comments
 (0)