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

Commit 4605003

Browse files
committed
add cooperative multi task
1 parent ae94f04 commit 4605003

28 files changed

Lines changed: 1501 additions & 277 deletions

File tree

src/include/driver/usb/hid.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,37 @@
44
#include <stdint.h>
55

66
/* HID Class Descriptor Types */
7-
#define HID_DT_HID 0x21
8-
#define HID_DT_REPORT 0x22
9-
#define HID_DT_PHYSICAL 0x23
7+
#define HID_DT_HID 0x21
8+
#define HID_DT_REPORT 0x22
9+
#define HID_DT_PHYSICAL 0x23
1010

1111
/* HID Class Requests */
12-
#define HID_REQ_GET_REPORT 0x01
13-
#define HID_REQ_GET_IDLE 0x02
14-
#define HID_REQ_GET_PROTOCOL 0x03
15-
#define HID_REQ_SET_REPORT 0x09
16-
#define HID_REQ_SET_IDLE 0x0A
17-
#define HID_REQ_SET_PROTOCOL 0x0B
12+
#define HID_REQ_GET_REPORT 0x01
13+
#define HID_REQ_GET_IDLE 0x02
14+
#define HID_REQ_GET_PROTOCOL 0x03
15+
#define HID_REQ_SET_REPORT 0x09
16+
#define HID_REQ_SET_IDLE 0x0A
17+
#define HID_REQ_SET_PROTOCOL 0x0B
1818

1919
/* HID Protocol */
20-
#define HID_PROTOCOL_BOOT 0
21-
#define HID_PROTOCOL_REPORT 1
20+
#define HID_PROTOCOL_BOOT 0
21+
#define HID_PROTOCOL_REPORT 1
2222

2323
/* Boot Protocol Keyboard Report */
2424
struct hid_keyboard_report {
25-
uint8_t modifier; /* Ctrl, Shift, Alt, GUI */
25+
uint8_t modifier; /* Ctrl, Shift, Alt, GUI */
2626
uint8_t reserved;
27-
uint8_t keycode[6]; /* 同時押し可能なキー(最大6個) */
27+
uint8_t keycode[6]; /* 同時押し可能なキー(最大6個) */
2828
} __attribute__((packed));
2929

3030
/* Modifier bits */
31-
#define HID_MOD_LEFT_CTRL 0x01
32-
#define HID_MOD_LEFT_SHIFT 0x02
33-
#define HID_MOD_LEFT_ALT 0x04
34-
#define HID_MOD_LEFT_GUI 0x08
35-
#define HID_MOD_RIGHT_CTRL 0x10
31+
#define HID_MOD_LEFT_CTRL 0x01
32+
#define HID_MOD_LEFT_SHIFT 0x02
33+
#define HID_MOD_LEFT_ALT 0x04
34+
#define HID_MOD_LEFT_GUI 0x08
35+
#define HID_MOD_RIGHT_CTRL 0x10
3636
#define HID_MOD_RIGHT_SHIFT 0x20
37-
#define HID_MOD_RIGHT_ALT 0x40
38-
#define HID_MOD_RIGHT_GUI 0x80
37+
#define HID_MOD_RIGHT_ALT 0x40
38+
#define HID_MOD_RIGHT_GUI 0x80
3939

4040
#endif /* _USB_HID_H */

src/include/driver/usb/usb.h

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,35 @@
44
#include <stdint.h>
55

66
/* USB Request Types */
7-
#define USB_DIR_OUT 0x00
8-
#define USB_DIR_IN 0x80
7+
#define USB_DIR_OUT 0x00
8+
#define USB_DIR_IN 0x80
99

10-
#define USB_TYPE_STANDARD 0x00
11-
#define USB_TYPE_CLASS 0x20
12-
#define USB_TYPE_VENDOR 0x40
10+
#define USB_TYPE_STANDARD 0x00
11+
#define USB_TYPE_CLASS 0x20
12+
#define USB_TYPE_VENDOR 0x40
1313

14-
#define USB_RECIP_DEVICE 0x00
15-
#define USB_RECIP_INTERFACE 0x01
16-
#define USB_RECIP_ENDPOINT 0x02
14+
#define USB_RECIP_DEVICE 0x00
15+
#define USB_RECIP_INTERFACE 0x01
16+
#define USB_RECIP_ENDPOINT 0x02
1717

1818
/* USB Standard Requests */
19-
#define USB_REQ_GET_STATUS 0x00
20-
#define USB_REQ_CLEAR_FEATURE 0x01
21-
#define USB_REQ_SET_FEATURE 0x03
22-
#define USB_REQ_SET_ADDRESS 0x05
23-
#define USB_REQ_GET_DESCRIPTOR 0x06
24-
#define USB_REQ_SET_DESCRIPTOR 0x07
19+
#define USB_REQ_GET_STATUS 0x00
20+
#define USB_REQ_CLEAR_FEATURE 0x01
21+
#define USB_REQ_SET_FEATURE 0x03
22+
#define USB_REQ_SET_ADDRESS 0x05
23+
#define USB_REQ_GET_DESCRIPTOR 0x06
24+
#define USB_REQ_SET_DESCRIPTOR 0x07
2525
#define USB_REQ_GET_CONFIGURATION 0x08
2626
#define USB_REQ_SET_CONFIGURATION 0x09
27-
#define USB_REQ_GET_INTERFACE 0x0A
28-
#define USB_REQ_SET_INTERFACE 0x0B
27+
#define USB_REQ_GET_INTERFACE 0x0A
28+
#define USB_REQ_SET_INTERFACE 0x0B
2929

3030
/* Descriptor Types */
31-
#define USB_DT_DEVICE 0x01
32-
#define USB_DT_CONFIG 0x02
33-
#define USB_DT_STRING 0x03
34-
#define USB_DT_INTERFACE 0x04
35-
#define USB_DT_ENDPOINT 0x05
31+
#define USB_DT_DEVICE 0x01
32+
#define USB_DT_CONFIG 0x02
33+
#define USB_DT_STRING 0x03
34+
#define USB_DT_INTERFACE 0x04
35+
#define USB_DT_ENDPOINT 0x05
3636

3737
/* USB Setup Packet */
3838
struct usb_setup_packet {
@@ -97,30 +97,30 @@ struct usb_endpoint_descriptor {
9797
} __attribute__((packed));
9898

9999
/* USB Device Class Codes */
100-
#define USB_CLASS_AUDIO 0x01
101-
#define USB_CLASS_COMM 0x02
102-
#define USB_CLASS_HID 0x03
103-
#define USB_CLASS_PRINTER 0x07
104-
#define USB_CLASS_MASS_STORAGE 0x08
105-
#define USB_CLASS_HUB 0x09
106-
#define USB_CLASS_DATA 0x0A
107-
#define USB_CLASS_VENDOR_SPEC 0xFF
100+
#define USB_CLASS_AUDIO 0x01
101+
#define USB_CLASS_COMM 0x02
102+
#define USB_CLASS_HID 0x03
103+
#define USB_CLASS_PRINTER 0x07
104+
#define USB_CLASS_MASS_STORAGE 0x08
105+
#define USB_CLASS_HUB 0x09
106+
#define USB_CLASS_DATA 0x0A
107+
#define USB_CLASS_VENDOR_SPEC 0xFF
108108

109109
/* HID Subclass Codes */
110-
#define HID_SUBCLASS_NONE 0x00
111-
#define HID_SUBCLASS_BOOT 0x01
110+
#define HID_SUBCLASS_NONE 0x00
111+
#define HID_SUBCLASS_BOOT 0x01
112112

113113
/* HID Protocol Codes */
114-
#define HID_PROTOCOL_NONE 0x00
115-
#define HID_PROTOCOL_KEYBOARD 0x01
116-
#define HID_PROTOCOL_MOUSE 0x02
114+
#define HID_PROTOCOL_NONE 0x00
115+
#define HID_PROTOCOL_KEYBOARD 0x01
116+
#define HID_PROTOCOL_MOUSE 0x02
117117

118118
/* HID Class Requests */
119-
#define HID_REQ_GET_REPORT 0x01
120-
#define HID_REQ_GET_IDLE 0x02
121-
#define HID_REQ_GET_PROTOCOL 0x03
122-
#define HID_REQ_SET_REPORT 0x09
123-
#define HID_REQ_SET_IDLE 0x0A
124-
#define HID_REQ_SET_PROTOCOL 0x0B
119+
#define HID_REQ_GET_REPORT 0x01
120+
#define HID_REQ_GET_IDLE 0x02
121+
#define HID_REQ_GET_PROTOCOL 0x03
122+
#define HID_REQ_SET_REPORT 0x09
123+
#define HID_REQ_SET_IDLE 0x0A
124+
#define HID_REQ_SET_PROTOCOL 0x0B
125125

126126
#endif /* _USB_H */

src/include/driver/usb/xhci.h

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ struct xhci_input_context {
188188
struct xhci_endpoint_context ep[31];
189189
} __attribute__((packed));
190190

191-
192191
/* xHCI Host Controller 構造体 */
193192
struct xhci_hc {
194193
/* MMIO Base Address */
@@ -243,7 +242,6 @@ struct xhci_hc {
243242

244243
typedef struct xhci_hc xhci_controller;
245244

246-
247245
struct xhci_hc *xhci_find_controller(void);
248246
int xhci_init(void);
249247
int xhci_reset_controller(struct xhci_hc *hc);
@@ -254,7 +252,8 @@ uint32_t xhci_get_port_status(struct xhci_hc *hc, uint32_t port_num);
254252
int xhci_setup_command_ring(struct xhci_hc *hc);
255253
int xhci_setup_event_ring(struct xhci_hc *hc);
256254
void xhci_ring_doorbell(struct xhci_hc *hc, uint8_t slot_id, uint8_t target);
257-
int xhci_wait_for_event(struct xhci_hc *hc, struct xhci_trb *event_trb, uint32_t timeout_ms);
255+
int xhci_wait_for_event(struct xhci_hc *hc, struct xhci_trb *event_trb,
256+
uint32_t timeout_ms);
258257
void xhci_handle_events(struct xhci_hc *hc);
259258

260259
/* Phase 3: Device Enumeration functions */
@@ -266,24 +265,26 @@ void xhci_handle_port_status_change(struct xhci_hc *hc, uint8_t port);
266265

267266
/* Phase 4: USB Protocol functions */
268267
int xhci_control_transfer(struct xhci_hc *hc, uint8_t slot_id,
269-
uint8_t request_type, uint8_t request,
270-
uint16_t value, uint16_t index,
271-
void *data, uint16_t length);
272-
int xhci_get_descriptor(struct xhci_hc *hc, uint8_t slot_id,
273-
uint8_t desc_type, uint8_t desc_index,
274-
void *buffer, uint16_t length);
275-
int xhci_set_configuration(struct xhci_hc *hc, uint8_t slot_id, uint8_t config_value);
268+
uint8_t request_type, uint8_t request, uint16_t value,
269+
uint16_t index, void *data, uint16_t length);
270+
int xhci_get_descriptor(struct xhci_hc *hc, uint8_t slot_id, uint8_t desc_type,
271+
uint8_t desc_index, void *buffer, uint16_t length);
272+
int xhci_set_configuration(struct xhci_hc *hc, uint8_t slot_id,
273+
uint8_t config_value);
276274

277275
/* Helper functions */
278276
void *xhci_alloc_aligned(uint32_t size, uint32_t alignment);
279277
void xhci_free_aligned(void *ptr);
280278
uint8_t xhci_get_keyboard_slot(struct xhci_hc *hc);
281279
int xhci_setup_keyboard_polling(struct xhci_hc *hc, uint8_t slot_id);
282-
int xhci_poll_keyboard(struct xhci_hc *hc, uint8_t slot_id, uint8_t *report_buffer);
280+
int xhci_poll_keyboard(struct xhci_hc *hc, uint8_t slot_id,
281+
uint8_t *report_buffer);
283282

284283
/* HID Keyboard specific */
285284
int xhci_configure_keyboard(struct xhci_hc *hc, uint8_t slot_id);
286-
int xhci_set_boot_protocol(struct xhci_hc *hc, uint8_t slot_id, uint8_t interface);
287-
int xhci_submit_interrupt_in(struct xhci_hc *hc, uint8_t slot_id, uint8_t endpoint, void *buffer, uint16_t length);
285+
int xhci_set_boot_protocol(struct xhci_hc *hc, uint8_t slot_id,
286+
uint8_t interface);
287+
int xhci_submit_interrupt_in(struct xhci_hc *hc, uint8_t slot_id,
288+
uint8_t endpoint, void *buffer, uint16_t length);
288289

289290
#endif /* _USB_XHCI_H */

src/include/task/multi_task.h

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#ifndef _TASK_TASK_H
2+
#define _TASK_TASK_H
3+
4+
#include <stdint.h>
5+
#include <stddef.h>
6+
7+
/**
8+
* @brief タスク状態
9+
*/
10+
typedef enum {
11+
TASK_STATE_READY = 0, // 実行可能
12+
TASK_STATE_RUNNING = 1, // 実行中
13+
TASK_STATE_BLOCKED = 2, // ブロック中
14+
TASK_STATE_DEAD = 3 // 終了済み
15+
} task_state_t;
16+
17+
/**
18+
* @brief レジスタコンテキスト(x86-64)
19+
* コンテキストスイッチ時に保存/復元されるレジスタ群
20+
*/
21+
typedef struct {
22+
uint64_t rax, rbx, rcx, rdx;
23+
uint64_t rsi, rdi, rbp, rsp;
24+
uint64_t r8, r9, r10, r11, r12, r13, r14, r15;
25+
uint64_t rip; // 命令ポインタ
26+
uint64_t rflags; // フラグレジスタ
27+
uint64_t cr3; // ページディレクトリベースレジスタ
28+
} registers_t;
29+
30+
/**
31+
* @brief タスク制御ブロック(TCB)
32+
*/
33+
typedef struct task {
34+
uint32_t tid; // タスクID
35+
char name[32]; // タスク名(デバッグ用)
36+
task_state_t state; // タスク状態
37+
int kernel_mode; // カーネルモードフラグ(1=カーネル、0=ユーザー)
38+
registers_t regs; // レジスタコンテキスト
39+
uint64_t kernel_stack; // カーネルスタックポインタ
40+
uint64_t user_stack; // ユーザースタックポインタ
41+
uint64_t page_directory; // ページディレクトリの物理アドレス
42+
uint64_t time_slice; // タイムスライス(ticks)
43+
uint64_t total_time; // 累計実行時間(ticks)
44+
struct task *next; // 次のタスク(リンクリスト)
45+
} task_t;
46+
47+
/**
48+
* @brief タスクシステムを初期化
49+
*/
50+
void task_init(void);
51+
52+
/**
53+
* @brief 新しいタスクを作成
54+
* @param entry タスクのエントリポイント
55+
* @param name タスク名
56+
* @param kernel_mode カーネルモードで実行する場合は1、ユーザーモードは0
57+
* @return 作成されたタスクのポインタ、失敗時はNULL
58+
*/
59+
task_t *task_create(void (*entry)(void), const char *name, int kernel_mode);
60+
61+
/**
62+
* @brief タスクをレディキューに追加
63+
* @param task 追加するタスク
64+
*/
65+
void task_ready(task_t *task);
66+
67+
/**
68+
* @brief 現在実行中のタスクを取得
69+
* @return 現在のタスクポインタ
70+
*/
71+
task_t *task_current(void);
72+
73+
/**
74+
* @brief スケジューラを実行(次のタスクに切り替え)
75+
* この関数はタイマー割り込みハンドラから呼ばれる
76+
*/
77+
void task_schedule(void);
78+
79+
/**
80+
* @brief 現在のタスクを終了
81+
*/
82+
void task_exit(void);
83+
84+
/**
85+
* @brief タスクを自発的に譲る(yield)
86+
*/
87+
void task_yield(void);
88+
89+
/**
90+
* @brief コンテキストスイッチを実行(アセンブリ実装)
91+
* @param old_regs 現在のタスクのレジスタ保存先
92+
* @param new_regs 次のタスクのレジスタ
93+
*/
94+
extern void task_switch(registers_t *old_regs, registers_t *new_regs);
95+
96+
#endif /* _TASK_TASK_H */

src/include/tests/define.h

Lines changed: 8 additions & 2 deletions
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
// メモリマップとメモリのテスト
@@ -24,11 +24,13 @@
2424
// ext2 test
2525
#define EXT2_TEST
2626
// ACPI Timer test
27-
#define ACPI_TIMER_TEST
27+
// #define ACPI_TIMER_TEST
2828
// APIC Timer test
2929
// #define APIC_TIMER_TEST
3030
// xHCI USB test
3131
#define XHCI_TEST
32+
// Multi Task test
33+
#define MULTI_TASK_TEST
3234

3335
/* -------- 宣言 -------- */
3436
#ifdef MEM_TEST
@@ -79,6 +81,10 @@ void apic_timer_test();
7981
void xhci_test();
8082
#endif
8183

84+
#ifdef MULTI_TASK_TEST
85+
void multi_task_test();
86+
#endif
87+
8288
#endif /* TEST_TRUE */
8389

8490
#endif /* _TESTS_CONFIG_H */

0 commit comments

Comments
 (0)