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

Commit bb7b51f

Browse files
committed
acpi torima commit
1 parent 0ea5830 commit bb7b51f

21 files changed

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

src/include/device/acpi.h

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
#ifndef _DEVICE_ACPI_H
2+
#define _DEVICE_ACPI_H
3+
4+
#include <config.h>
5+
#include <stdint.h>
6+
7+
/**
8+
* ACPI RSDP (Root System Description Pointer) 構造体
9+
*/
10+
struct acpi_rsdp {
11+
char signature[8]; /* "RSD PTR " */
12+
uint8_t checksum;
13+
char oem_id[6];
14+
uint8_t revision;
15+
uint32_t rsdt_address; /* RSDT物理アドレス */
16+
} __attribute__((packed));
17+
18+
/**
19+
* ACPI RSDP 2.0+ 拡張部分
20+
*/
21+
struct acpi_rsdp_ext {
22+
struct acpi_rsdp rsdp;
23+
uint32_t length;
24+
uint64_t xsdt_address;
25+
uint8_t extended_checksum;
26+
uint8_t reserved[3];
27+
} __attribute__((packed));
28+
29+
/**
30+
* ACPI SDT (System Description Table) ヘッダー
31+
*/
32+
struct acpi_sdt_header {
33+
char signature[4];
34+
uint32_t length;
35+
uint8_t revision;
36+
uint8_t checksum;
37+
char oem_id[6];
38+
char oem_table_id[8];
39+
uint32_t oem_revision;
40+
uint32_t creator_id;
41+
uint32_t creator_revision;
42+
} __attribute__((packed));
43+
44+
/**
45+
* ACPI RSDT (Root System Description Table)
46+
*/
47+
struct acpi_rsdt {
48+
struct acpi_sdt_header header;
49+
uint32_t sdt_pointers[]; /* 可変長の配列 */
50+
} __attribute__((packed));
51+
52+
/**
53+
* ACPI FADT (Fixed ACPI Description Table) - PM Timer用
54+
*/
55+
struct acpi_fadt {
56+
struct acpi_sdt_header header;
57+
uint32_t firmware_ctrl;
58+
uint32_t dsdt;
59+
uint8_t reserved;
60+
uint8_t preferred_pm_profile;
61+
uint16_t sci_interrupt;
62+
uint32_t smi_command_port;
63+
uint8_t acpi_enable;
64+
uint8_t acpi_disable;
65+
uint8_t s4bios_req;
66+
uint8_t pstate_control;
67+
uint32_t pm1a_event_block;
68+
uint32_t pm1b_event_block;
69+
uint32_t pm1a_control_block;
70+
uint32_t pm1b_control_block;
71+
uint32_t pm2_control_block;
72+
uint32_t pm_timer_block; /* PM Timer I/Oポート */
73+
uint32_t gpe0_block;
74+
uint32_t gpe1_block;
75+
uint8_t pm1_event_length;
76+
uint8_t pm1_control_length;
77+
uint8_t pm2_control_length;
78+
uint8_t pm_timer_length; /* PM Timer長(通常4バイト)*/
79+
uint8_t gpe0_length;
80+
uint8_t gpe1_length;
81+
uint8_t gpe1_base;
82+
uint8_t cstate_control;
83+
uint16_t worst_c2_latency;
84+
uint16_t worst_c3_latency;
85+
uint16_t flush_size;
86+
uint16_t flush_stride;
87+
uint8_t duty_offset;
88+
uint8_t duty_width;
89+
uint8_t day_alarm;
90+
uint8_t month_alarm;
91+
uint8_t century;
92+
uint16_t boot_arch_flags;
93+
uint8_t reserved2;
94+
uint32_t flags; /* bit 8: TMR_VAL_EXT (32bit timer) */
95+
/* 以降は拡張フィールド(省略可能) */
96+
} __attribute__((packed));
97+
98+
/**
99+
* ACPI初期化
100+
* @return 0: 成功, -1: 失敗
101+
*/
102+
int acpi_init(void);
103+
104+
/**
105+
* ACPI PM Timerから現在のカウント値を取得
106+
* @return タイマーカウント値(3.579545 MHzでカウント)
107+
*/
108+
uint32_t acpi_timer_read(void);
109+
110+
/**
111+
* ACPI PM Timerが利用可能かチェック
112+
* @return 1: 利用可能, 0: 利用不可
113+
*/
114+
int acpi_timer_available(void);
115+
116+
/**
117+
* システム起動からの経過時間をマイクロ秒で取得
118+
* @return マイクロ秒
119+
*/
120+
uint64_t acpi_get_uptime_us(void);
121+
122+
/**
123+
* システム起動からの経過時間をミリ秒で取得
124+
* @return ミリ秒
125+
*/
126+
uint64_t acpi_get_uptime_ms(void);
127+
128+
#endif /* _DEVICE_ACPI_H */

src/include/driver/timer/apic.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#ifndef _DRIVER_TIMER_APIC_H
2+
#define _DRIVER_TIMER_APIC_H
3+
4+
#include <config.h>
5+
#include <stdint.h>
6+
7+
/* APIC レジスタのベースアドレス (デフォルト) */
8+
#define APIC_BASE_DEFAULT 0xFEE00000
9+
10+
/* APIC レジスタオフセット */
11+
#define APIC_ID 0x020 /* Local APIC ID */
12+
#define APIC_VERSION 0x030 /* Local APIC Version */
13+
#define APIC_TPR 0x080 /* Task Priority Register */
14+
#define APIC_EOI 0x0B0 /* End Of Interrupt */
15+
#define APIC_LDR 0x0D0 /* Logical Destination Register */
16+
#define APIC_DFR 0x0E0 /* Destination Format Register */
17+
#define APIC_SPURIOUS 0x0F0 /* Spurious Interrupt Vector Register */
18+
#define APIC_ESR 0x280 /* Error Status Register */
19+
#define APIC_TIMER_LVT 0x320 /* LVT Timer Register */
20+
#define APIC_TIMER_INIT 0x380 /* Timer Initial Count */
21+
#define APIC_TIMER_CURRENT 0x390 /* Timer Current Count */
22+
#define APIC_TIMER_DIV 0x3E0 /* Timer Divide Configuration */
23+
24+
/* APIC Timer モード */
25+
#define APIC_TIMER_MODE_ONESHOT 0x00000000
26+
#define APIC_TIMER_MODE_PERIODIC 0x00020000
27+
#define APIC_TIMER_MODE_TSCDEADLINE 0x00040000
28+
29+
/* APIC Timer 分周比 */
30+
#define APIC_TIMER_DIV_1 0x0B
31+
#define APIC_TIMER_DIV_2 0x00
32+
#define APIC_TIMER_DIV_4 0x01
33+
#define APIC_TIMER_DIV_8 0x02
34+
#define APIC_TIMER_DIV_16 0x03
35+
#define APIC_TIMER_DIV_32 0x08
36+
#define APIC_TIMER_DIV_64 0x09
37+
#define APIC_TIMER_DIV_128 0x0A
38+
39+
int apic_timer_init(void);
40+
int apic_timer_available(void);
41+
void apic_timer_tick(uint32_t irq, void *context);
42+
uint64_t apic_get_uptime_us(void);
43+
uint64_t apic_get_uptime_ms(void);
44+
uint32_t apic_timer_get_frequency(void);
45+
void apic_timer_delay_us(uint32_t us);
46+
void apic_timer_delay_ms(uint32_t ms);
47+
48+
#endif /* _DRIVER_TIMER_APIC_H */

src/include/tests/define.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#define _TESTS_CONFIG_H
33

44
// テストを実行するかどうか
5-
//#define TEST_TRUE
5+
// #define TEST_TRUE
66

77
#ifdef TEST_TRUE
88
// メモリマップとメモリのテスト
@@ -23,6 +23,10 @@
2323
#define FAT12_TEST
2424
// ext2 test
2525
#define EXT2_TEST
26+
// ACPI Timer test
27+
// #define ACPI_TIMER_TEST
28+
// APIC Timer test
29+
// #define APIC_TIMER_TEST
2630

2731
/* -------- 宣言 -------- */
2832
#ifdef MEM_TEST
@@ -61,6 +65,14 @@ void fat12_test();
6165
void ext2_test();
6266
#endif
6367

68+
#ifdef ACPI_TIMER_TEST
69+
void acpi_timer_test();
70+
#endif
71+
72+
#ifdef APIC_TIMER_TEST
73+
void apic_timer_test();
74+
#endif
75+
6476
#endif /* TEST_TRUE */
6577

6678
#endif /* _TESTS_CONFIG_H */

src/include/util/io.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,7 @@ uint8_t inb(uint16_t port);
1515
void outb(uint16_t port, uint8_t value);
1616
uint16_t inw(uint16_t port);
1717
void outw(uint16_t port, uint16_t value);
18+
uint32_t inl(uint16_t port);
19+
void outl(uint16_t port, uint32_t value);
1820

1921
#endif /* _IO_H */

src/kernel/device/keyboard.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,9 @@ static void kbd_process(uint32_t sc_payload, void *ctx) {
158158
void keyboard_init(void) {
159159
// 同期側の処理ハンドラを登録 (FIFOイベントの処理)
160160
interrupt_register(1, kbd_process, NULL);
161-
161+
#ifdef INIT_MSG
162162
printk("Keyboard initialize success\n");
163+
#endif
163164
}
164165

165166
void keyboard_poll(void) {

src/kernel/driver/ata.c

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,9 @@ static void ata_get_base(uint8_t drive, uint16_t *base, uint8_t *drive_sel) {
7474
* @brief ATAドライバを初期化する
7575
*/
7676
int ata_init(void) {
77+
#ifdef INIT_MSG
7778
printk("ATA: Initializing ATA driver\n");
78-
79+
#endif
7980
/* 試すドライブのリスト */
8081
const struct {
8182
uint16_t base;
@@ -112,17 +113,23 @@ int ata_init(void) {
112113

113114
/* ドライブが存在しない */
114115
if (status == 0 || status == 0xFF) {
116+
#ifdef INIT_MSG
115117
printk("ATA: No drive (status=0x%x)\n", status);
118+
#endif
116119
continue;
117120
}
118121

119122
/* エラーチェック */
120123
if (status & ATA_SR_ERR) {
121124
uint8_t err = inb(drives[i].base + 1);
125+
#ifdef INIT_MSG
122126
printk("ATA: Error detected (err=0x%x)\n", err);
127+
#endif
123128
/* ATAPI デバイスの場合はスキップ */
124129
if (err == 0x01) {
130+
#ifdef INIT_MSG
125131
printk("ATA: ATAPI device (not supported)\n");
132+
#endif
126133
}
127134
continue;
128135
}
@@ -156,15 +163,17 @@ int ata_init(void) {
156163
drives[i].base);
157164
continue;
158165
}
159-
166+
#ifdef INIT_MSG
160167
/* IDENTIFYデータを読み取る(256ワード = 512バイト)*/
161168
printk("ATA: reading IDENTIFY data from base 0x%x\n",
162169
drives[i].base);
170+
#endif
163171
for (int j = 0; j < 256; j++) {
164172
(void)inw(drives[i].base);
165173
}
166-
174+
#ifdef INIT_MSG
167175
printk("ATA: %s detected successfully!\n", drives[i].name);
176+
#endif
168177
return 0;
169178
}
170179

@@ -229,7 +238,7 @@ int ata_read_sectors(uint8_t drive, uint32_t lba, uint8_t sectors,
229238
for (int i = 0; i < 4; i++) {
230239
inb(base + 7);
231240
}
232-
241+
233242
/* セクタ読み取り後に割り込みを処理 */
234243
interrupt_dispatch_all();
235244
}

0 commit comments

Comments
 (0)