-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.c
161 lines (128 loc) · 3.48 KB
/
main.c
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
#include <elf.h>
#include <ng/arch.h>
#include <ng/commandline.h>
#include <ng/debug.h>
#include <ng/event_log.h>
#include <ng/fs/init.h>
#include <ng/limine.h>
#include <ng/panic.h>
#include <ng/pci.h>
#include <ng/pmm.h>
#include <ng/proc_files.h>
#include <ng/random.h>
#include <ng/serial.h>
#include <ng/tarfs.h>
#include <ng/tests.h>
#include <ng/thread.h>
#include <ng/timer.h>
#include <ng/tty.h>
#include <ng/x86/acpi.h>
#include <ng/x86/cpu.h>
#include <ng/x86/interrupt.h>
#include <stdio.h>
#include <stdlib.h>
#include <version.h>
struct tar_header *initfs;
int have_fsgsbase = 0;
bool initialized = false;
const char *banner = "\n\
********************************\n\
\n\
The Nightingale Operating System\n\
Version " NIGHTINGALE_VERSION "\n\
\n\
********************************\n\
\n\
Copyright (C) 2017-2024, Tyler Philbrick\n\
\n\
This program is free software: you can redistribute it and/or modify\n\
it under the terms of the GNU General Public License as published by\n\
the Free Software Foundation, either version 3 of the License, or\n\
(at your option) any later version.\n\
\n\
This program is distributed in the hope that it will be useful,\n\
but WITHOUT ANY WARRANTY; without even the implied warranty of\n\
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n\
GNU General Public License for more details.\n\
\n\
You should have received a copy of the GNU General Public License\n\
along with this program. If not, see <https://www.gnu.org/licenses/>.\n\n";
bool print_boot_info = true;
extern struct thread thread_zero;
[[noreturn]] void real_main();
[[noreturn]] void ap_kernel_main();
extern char hhstack_top;
void early_init() {
set_gs_base(thread_cpus[0]);
idt_install();
heap_init(__global_heap_ptr, early_malloc_pool, EARLY_MALLOC_POOL_LEN);
serial_init();
arch_init();
acpi_init(limine_rsdp());
tty_init();
pm_init();
limine_init();
}
uint64_t tsc;
__USED
[[noreturn]] void kernel_main() {
tsc = rdtsc();
early_init();
random_add_boot_randomness();
event_log_init();
timer_init();
longjump_kcode((uintptr_t)real_main, (uintptr_t)&hhstack_top);
}
[[noreturn]] void real_main() {
printf("real_main\n");
void *kernel_file_ptr = limine_kernel_file_ptr();
size_t kernel_file_len = limine_kernel_file_len();
limine_load_kernel_elf(kernel_file_ptr, kernel_file_len);
initfs = (struct tar_header *)limine_module();
fs_init(initfs);
threads_init();
if (print_boot_info)
pci_enumerate_bus_and_print();
procfs_init();
run_all_tests();
initialized = true;
const char *init_program = get_kernel_argument("init");
if (!init_program)
init_program = "/bin/init";
bootstrap_usermode(init_program);
// ext2_info();
printf("%s", banner);
void video();
video();
void rbtree_test();
rbtree_test();
pci_address_t addr;
if (pci_find_device_by_id(0x10ec, 0x8139) != -1) {
void rtl_test();
rtl_test();
}
if ((addr = pci_find_device_by_id(0x8086, 0x100e)) != -1) {
void e1000_test(pci_address_t);
e1000_test(addr);
}
void print_test();
print_test();
void net_test();
net_test();
printf("threads: usermode thread installed\n");
printf("initialization took: %li\n", rdtsc() - tsc);
printf("cpu: allowing irqs\n");
enable_irqs();
limine_smp_init((limine_goto_address)ap_kernel_main);
while (true)
__asm__ volatile("hlt");
panic("kernel_main tried to return!");
}
[[noreturn]] void ap_kernel_main() {
printf("\nthis is the application processor\n");
arch_ap_init();
printf("lapic: initialized\n");
for (;;) {
__asm__ volatile("hlt");
}
}