-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
257 lines (206 loc) · 6.51 KB
/
main.c
File metadata and controls
257 lines (206 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#include <stdint.h>
#include <stddef.h>
#include <stdbool.h>
#include "drv/io/io.h"
#include "base/int/idt.h"
#include "base/time/timer.h"
#include "base/time/clock.h"
#include "base/mem/mem.h"
#include "libc/string.h"
#include "base/sched/sched.h"
#include "base/mb2/mb2.h"
#include "drv/fb/fb.h"
#include "base/term/term.h"
#include "drv/pci/pci.h"
#include "base/rsod/rsod.h"
#include "drv/sound/pcs/pcs.h"
#include "drv/kbd/kbd.h"
#include "drv/acpi/acpi.h"
#include "drv/disk/ide.h"
#include "drv/block/blockdev.h"
#include "drv/usb/usbdev/usbdev.h"
#include "drv/usb/ehci/ehci.h"
#include "base/mem/pmm.h"
#include "base/mem/paging.h"
#include "base/term/tio.h"
#include "drv/disk/ahci.h"
#include "fs/vfs/vfs.h"
#include "fs/exfat/exfat.h"
#include "fs/devfs/devfs.h"
/* символы из link.ld */
extern char _heap_start;
extern char _heap_end;
// Глобальные переменные
extern void acpi_monitor_power_button(void);
extern bool input_waiting;
framebuffer_t fb;
ide_disk_t disks[4];
pmm_t pmm;
term_t* term;
vfs_inode_t *fs_root = NULL;
blockdev_t *fs_disk = NULL;
int term_pid = 0;
static void terminal_thread(void);
static void termsaver_recovery(void) {
// waiting 10 sec
wait(10);
task_create(terminal_thread, KSTACK_SIZE * 2, "ALKShell");
task_exit(1);
}
static void termsaver(void) {
while (task_is_alive(term_pid)) {
asm volatile ("hlt");
}
// Terminal is terminated
while (!task_is_alive(term_pid)) {
fb_fill_rect(&fb,
term->x, term->y,
term->cols * term->char_width,
term->rows * term->char_height,
COLOR_BLACK);
fb_set_color(&fb, COLOR_RED, COLOR_BLACK);
fb_printf(&fb, "TERMINAL WAS TERMINATED!\n");
fb_printf(&fb, "TRYING TO RECOVERY...\n");
task_create(termsaver_recovery, 0, "RECOVERY");
mwait(50);
fb_fill_rect(&fb,
term->x, term->y,
term->cols * term->char_width,
term->rows * term->char_height,
COLOR_RED);
fb_set_color(&fb, COLOR_BLACK, COLOR_RED);
fb_printf(&fb, "TERMINAL WAS TERMINATED!\n");
fb_printf(&fb, "TRYING TO RECOVERY...\n");
}
}
static void terminal_thread(void) {
term_clear(term);
term_pid = get_current_task()->pid;
tio_printf("\n[TERM] Terminal started (PID: %d)\n",
term_pid);
// Включаем промпт
term_enable_prompt(term);
term_set_prompt_text(term, "> ");
// Бесконечный цикл терминала
while (1) {
// Обрабатываем ввод из буфера
term_process_input(term);
// Небольшая пауза, чтобы не грузить CPU
asm volatile("pause");
// или mwait(1); если есть
}
}
void zombie_reaper_task(void)
{
for (;;)
{
reap_zombies();
asm volatile("hlt");
}
}
void show_alk_logo(void) {
tio_printf("\n");
tio_printf("\n");
tio_printf(" _____ __ __ __ ____ ____\n");
mwait(50);
tio_printf(" / __ | | | | / / / \\| ___|\n");
mwait(50);
tio_printf(" / /__| | | | |/ / | /\\ |||___\n");
mwait(50);
tio_printf(" / __ | | | | | || ||___ |\n");
mwait(50);
tio_printf(" / / | | |___| |\\ \\ | \\/ | __| |\n");
mwait(50);
tio_printf("/__/ |__|______|__| \\_\\ \\____/|____|\n");
tio_printf("\n");
}
void fs_init(void) {
tio_printf("\n[FS] Initializing filesystem layer...\n");
// 1. Инициализируем VFS
vfs_init();
// 2. Регистрируем exFAT
exfat_init();
tio_printf("[FS] Ready\n");
}
/*-------------------------------------------------------------
Основная функция ядра
-------------------------------------------------------------*/
void kmain(uint64_t mb2_addr)
{
idt_install();
init_system_clock();
init_timer(500);
outb(0x21, 0xF0); // маска прерываний
outb(0xA1, 0xFF);
can_type = false;
input_waiting = false;
mb2_parse(mb2_addr);
if(!fb_init(&fb)) {
for (;;) asm volatile ("hlt");
}
pmm_init(&pmm, mb2_addr);
size_t heap_size = (size_t)((uintptr_t)&_heap_end - (uintptr_t)&_heap_start);
malloc_init(&_heap_start, heap_size);
uint64_t total_ram = get_total_memory();
paging_init(total_ram);
fb_alloc_backbuffer(&fb);
fb_enable_vsync(&fb, 60);
scheduler_init();
uint32_t cols = fb.width / (FONT_WIDTH + 1);
uint32_t rows = fb.height / (FONT_HEIGHT + 2);
term = term_init(&fb, 0, 0, cols, rows);
tio_init(term);
fb_fill_rect(&fb, 0, 0, fb.width, fb.height, term->bg_color);
tio_printf("[TERM] Initialized\n");
task_create(zombie_reaper_task, 0, "ZombieReap");
tio_printf("[SCHEDULER] Created task: 'ZombieReap'\n");
task_create(terminal_thread, KSTACK_SIZE * 2, "ALKShell");
task_create(termsaver, 0, "ShellSave");
pci_init();
tio_printf("[PCI] Initialized\n");
pc_speaker_init();
if (acpi_init()) {
tio_printf("[ACPI] Found %d CPU(s)\n", acpi_get_cpu_count());
} else {
rsod("ACPI_INIT_FAILED", "ACPI");
}
tio_printf("Initializing disk controllers...\n");
for (int ch = 0; ch < 2; ch++) {
for (int dr = 0; dr < 2; dr++) {
int idx = ch * 2 + dr;
tio_printf("Initializing %s/%s... ",
ch == 0 ? "Primary" : "Secondary",
dr == 0 ? "Master" : "Slave");
int result = ide_init(&disks[idx], (ide_channel_t)ch, dr);
if (result == IDE_OK) {
tio_printf("OK (Type: %s)\n",
disks[idx].type == IDE_TYPE_ATA ? "ATA" :
disks[idx].type == IDE_TYPE_ATAPI ? "ATAPI" : "NONE");
} else {
tio_printf("Failed (%d)\n", result);
}
}
}
tio_printf("Initializing block device layer...\n");
blockdev_init();
blockdev_scan_all_disks(1);
if (ahci_init() == 0) {
blockdev_scan_all_disks(2);
} else {
tio_printf("[AHCI] Failed.\n");
}
usb_core_init(term);
ehci_init(term, &pmm);
fs_init();
devfs_init();
/* Разрешаем прерывания */
asm volatile("sti");
mwait(200);
term_clear(term);
show_alk_logo();
can_type = true;
for (;;)
{
asm volatile("hlt");
}
}