-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
131 lines (88 loc) · 3.33 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
// clang-format off
/*
* sdx | simple and dirty UNIX
* written by ngn (https://ngn.tf) (2024)
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// clang-format on
#include "core/im.h"
#include "core/pci.h"
#include "core/pic.h"
#include "core/serial.h"
#include "boot/end.h"
#include "boot/multiboot.h"
#include "mm/pm.h"
#include "mm/vmm.h"
#include "fs/vfs.h"
#include "video.h"
#include "util/panic.h"
#include "util/printk.h"
void entry() {
/*
* here, we initialize video memory in width the framebuffer mode
* framebuffer info is obtained from the multiboot data
* see boot/multiboot.S
* also cars 2 is the best cars movie btw
*/
if (!video_init(VIDEO_MODE_FRAMEBUFFER))
panic(__func__, "Failed to initialize the framebuffer video mode");
// initialize serial communication ports (UART)
serial_init();
/*
* obtain the available memory region start and the end address
* for the start: we check if the page table's end address is larger then
* the end address for the kernel, if so, we use the page table's end address
* for the end: we check if the last mapped memory address is larger then the
* available memory region's end address, if so, we use the region's address
*/
uint64_t avail_start = pm_end > end_addr ? pm_end : end_addr;
uint64_t avail_end = pm_mapped > mb_mem_avail_limit ? mb_mem_avail_limit : pm_mapped;
/*
* paging is done in before switching to long mode
* here we are just doing some extra checks and logging
* see boot/paging.S and boot/multiboot.S
*/
if (!pm_init(avail_start, avail_end))
panic(__func__, "Failed to initialize the paging manager");
/*
* initialize the interrupt manager (IM)
* by default all the interrupts are handled by the default handler
* this can be changed using the other IM functions
*/
im_init();
/*
* initialize the programmable interrupt controller (PIC)
* we need to enable this before enabling interrupts
* otherwise since the vector offset is not set we would get a random
* exception interrupt from the PIC
*/
if (!pic_init())
panic(__func__, "Failed to initialize the PIC");
if (!pic_enable())
panic(__func__, "Failed to enable the PIC");
if (!pic_mask(PIC_IRQ_TIMER))
panic(__func__, "Failed to mask timer IRQ");
// enable the interrupts
im_enable();
// initialize peripheral component interconnect (PCI) devices
pci_init();
// mount any potential root VFS
if (NULL == vfs_detect())
panic(__func__, "No available root partition");
/*
* this is where we are gonna start the init program and wait for userland calls
* for now tho, we'll just print a hello world message and return to halt the system (see boot/loader.S)
*/
while (true)
continue;
return;
}