PatchworkOS
Loading...
Searching...
No Matches
init.c
Go to the documentation of this file.
1#include <kernel/init/init.h>
2
3#include <kernel/acpi/acpi.h>
7#include <kernel/cpu/gdt.h>
8#include <kernel/cpu/idt.h>
9#include <kernel/cpu/smp.h>
10#include <kernel/cpu/syscalls.h>
12#include <kernel/drivers/gop.h>
13#include <kernel/drivers/ps2/ps2.h>
14#include <kernel/fs/ramfs.h>
15#include <kernel/fs/sysfs.h>
16#include <kernel/fs/vfs.h>
17#include <kernel/ipc/pipe.h>
18#include <kernel/ipc/shmem.h>
19#include <kernel/log/log.h>
20#include <kernel/log/log_file.h>
21#include <kernel/log/panic.h>
22#include <kernel/mem/pmm.h>
23#include <kernel/mem/vmm.h>
24#include <kernel/net/net.h>
25#include <kernel/proc/process.h>
26#include <kernel/sched/loader.h>
27#include <kernel/sched/sched.h>
28#include <kernel/sched/timer.h>
29#include <kernel/sched/wait.h>
31
32#include <boot/boot_info.h>
33
35
36#include <stdlib.h>
37#include <strings.h>
38
39void init_early(const boot_info_t* bootInfo)
40{
41 gdt_init();
42 idt_init();
43
45
46 log_init(&bootInfo->gop);
47
48 pmm_init(&bootInfo->memory.map);
49 vmm_init(&bootInfo->memory, &bootInfo->gop, &bootInfo->kernel);
50 _std_init();
51
52 panic_symbols_init(&bootInfo->kernel);
53
54 acpi_tables_init(bootInfo->rsdp);
55
57
58 LOG_DEBUG("early init done, jumping to boot thread\n");
61 bootThread->frame.rdi = (uintptr_t)bootInfo;
67
71
72 // This will trigger a page fault. But thats intended as we use page faults to dynamically grow the
73 // threads kernel stack and the stack starts out unmapped.
75}
76
78{
79 for (uint64_t i = 0; i < map->length; i++)
80 {
81 const EFI_MEMORY_DESCRIPTOR* desc = BOOT_MEMORY_MAP_GET_DESCRIPTOR(map, i);
82
83 if (desc->Type == EfiLoaderData)
84 {
85 pmm_free_region((void*)desc->VirtualStart, desc->NumberOfPages);
86 LOG_INFO("free boot memory [0x%016lx-0x%016lx]\n", desc->VirtualStart,
87 ((uintptr_t)desc->VirtualStart) + desc->NumberOfPages * PAGE_SIZE);
88 }
89 }
90}
91
92static void init_finalize(const boot_info_t* bootInfo)
93{
96
98
99 vfs_init();
100 ramfs_init(&bootInfo->disk);
101 sysfs_init();
102
103 aml_init();
105 acpi_reclaim_memory(&bootInfo->memory.map);
106
109
110 module_init();
111
114
115 const_init();
116 ps2_init();
117 net_init();
118 pipe_init();
119 shmem_init();
120 gop_init(&bootInfo->gop);
122
124
126
127 init_free_loader_data(&bootInfo->memory.map);
129
130 LOG_INFO("kernel initalized using %llu kb of memory\n", pmm_reserved_amount() * PAGE_SIZE / 1024);
131}
132
133static inline void init_process_spawn(void)
134{
135 LOG_INFO("spawning init process\n");
136 const char* argv[] = {"/bin/init", NULL};
137 thread_t* initThread = loader_spawn(argv, PRIORITY_MAX_USER - 2, NULL);
138 if (initThread == NULL)
139 {
140 panic(NULL, "Failed to spawn init process");
141 }
142
143 // Set klog as stdout for init process
144 file_t* klog = vfs_open(PATHNAME("/dev/klog"), initThread->process);
145 if (klog == NULL)
146 {
147 panic(NULL, "Failed to open klog");
148 }
149 if (vfs_ctx_set_fd(&initThread->process->vfsCtx, STDOUT_FILENO, klog) == ERR)
150 {
151 panic(NULL, "Failed to set klog as stdout for init process");
152 }
153 ref_dec(klog);
154
155 sched_push(initThread, NULL);
156}
157
158void kmain(const boot_info_t* bootInfo)
159{
160 // The stack pointer is expected to be somewhere near the top.
162
163 LOG_DEBUG("kmain entered\n");
164
165 init_finalize(bootInfo);
166
167 asm volatile("sti");
168
170
171 LOG_INFO("done with boot thread\n");
173}
#define assert(expression)
Definition assert.h:29
#define BOOT_MEMORY_MAP_GET_DESCRIPTOR(map, index)
Definition boot_info.h:41
#define CLOCKS_NEVER
Definition clock_t.h:16
void const_init(void)
Definition const.c:100
void kmain(const boot_info_t *bootInfo)
Kernel main function.
Definition init.c:158
void init_early(const boot_info_t *bootInfo)
Early kernel initialization.
Definition init.c:39
uint64_t aml_namespace_expose(void)
Expose the entire namespace heirarchy to sysfs.
Definition namespace.c:80
void aml_init(void)
Initialize the AML subsystem.
Definition aml.c:107
void acpi_devices_init(void)
Enumerate and configure ACPI devices.
Definition devices.c:98
void acpi_tables_init(rsdp_t *rsdp)
Initialize ACPI tables and call their init handlers.
Definition tables.c:201
void acpi_tables_expose(void)
Expose ACPI tables to sysfs.
Definition tables.c:231
void acpi_reclaim_memory(const boot_memory_map_t *map)
Reclaim ACPI memory regions.
Definition acpi.c:44
void gdt_init(void)
Initialize the GDT.
Definition gdt.c:35
@ GDT_CS_RING0
Value to load into the CS register for kernel code.
Definition gdt.h:45
@ GDT_SS_RING0
Value to load into the SS register for kernel data.
Definition gdt.h:46
void idt_init(void)
Initialize the IDT structure in memory.
Definition idt.c:26
void smp_bootstrap_init(void)
Initializes the bootstrap CPU structure.
Definition smp.c:31
static cpu_t * smp_self_unsafe(void)
Returns a pointer to the cpu_t structure of the current CPU.
Definition smp.h:90
void smp_bootstrap_init_early(void)
Early initialization of the bootstrap CPU.
Definition smp.c:26
void smp_others_init(void)
Initializes the other CPUs.
Definition smp.c:40
void syscall_table_init(void)
Initialize the syscall table.
Definition syscalls.c:23
void ps2_init(void)
Initialize the PS/2 controller.
Definition ps2.c:324
void statistics_init(void)
Initializes the statistics driver.
Definition statistics.c:100
#define PATHNAME(string)
Helper to create a pathname.
Definition path.h:156
void ramfs_init(const boot_disk_t *disk)
Registers the ramfs filesystem and mounts it as the root filesystem.
Definition ramfs.c:385
fd_t vfs_ctx_set_fd(vfs_ctx_t *ctx, fd_t fd, file_t *file)
Allocate a specific file descriptor in a VFS context.
Definition vfs_ctx.c:164
file_t * vfs_open(const pathname_t *pathname, process_t *process)
Open a file.
Definition vfs.c:531
void vfs_init(void)
Initializes the VFS.
Definition vfs.c:83
void pipe_init(void)
Definition pipe.c:206
void shmem_init(void)
Initializes the shared memory subsystem.
Definition shmem.c:196
void log_file_expose(void)
Expose the kernel log file to userspace in sysfs.
Definition log_file.c:58
void panic_symbols_init(const boot_kernel_t *kernel)
Initialize panic symbols from the bootloader-provided kernel information.
Definition panic.c:321
NORETURN void panic(const interrupt_frame_t *frame, const char *format,...)
Panic the kernel, printing a message and halting.
Definition panic.c:362
#define LOG_INFO(format,...)
Definition log.h:87
#define LOG_DEBUG(format,...)
Definition log.h:81
void log_init(const boot_gop_t *gop)
Initialize the logging system.
Definition log.c:50
void pmm_init(const boot_memory_map_t *map)
Initializes the Physical Memory Manager.
Definition pmm.c:152
void pmm_free_region(void *address, uint64_t count)
Frees a contiguous region of physical pages.
Definition pmm.c:226
uint64_t pmm_reserved_amount(void)
Retrieves the amount of reserved physical memory.
Definition pmm.c:244
#define VMM_KERNEL_STACKS_MAX
The maximum address for kernel stacks.
Definition vmm.h:65
void vmm_init(const boot_memory_t *memory, const boot_gop_t *gop, const boot_kernel_t *kernel)
Initializes the Virtual Memory Manager.
Definition vmm.c:41
void vmm_unmap_bootloader_lower_half(thread_t *bootThread)
Unmaps the lower half of the address space after kernel initialization.
Definition vmm.c:129
void vmm_map_bootloader_lower_half(thread_t *bootThread)
Maps the lower half of the address space to the boot thread during kernel initialization.
Definition vmm.c:121
void module_init(void)
Initializes the module system.
Definition module.c:8
void net_init(void)
Initialize the networking subsystem.
Definition net.c:12
void process_procfs_init(void)
Initializes the /proc directory.
Definition process.c:567
thread_t * loader_spawn(const char **argv, priority_t priority, const path_t *cwd)
Spawns a child process from an executable file.
Definition loader.c:154
_NORETURN void thread_jump(thread_t *thread)
Jump to a thread by calling thread_load() and then loading its interrupt frame.
thread_t * thread_get_boot(void)
Retrieves the boot thread.
Definition thread.c:159
@ THREAD_RUNNING
Is currently running on a cpu.
Definition thread.h:32
void sched_push(thread_t *thread, cpu_t *target)
Pushes a thread onto a scheduling queue.
Definition sched.c:305
NORETURN void sched_done_with_boot_thread(void)
Specify that the boot thread is no longer needed.
Definition sched.c:121
static void ref_dec(void *ptr)
Decrement reference count.
Definition ref.h:127
#define STDOUT_FILENO
Definition io.h:45
#define PRIORITY_MAX_USER
Definition proc.h:43
#define PAGE_SIZE
Memory page size.
Definition proc.h:140
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
void gop_init(const boot_gop_t *in)
Definition gop.c:43
static void init_free_loader_data(const boot_memory_map_t *map)
Definition init.c:77
static void init_process_spawn(void)
Definition init.c:133
static void init_finalize(const boot_info_t *bootInfo)
Definition init.c:92
static thread_t bootThread
Definition thread.c:19
void _std_init(void)
Definition init.c:13
boot_memory_map_t * map
Definition mem.c:19
#define RFLAGS_ALWAYS_SET
Definition regs.h:24
static uint64_t rsp_read()
Definition regs.h:138
#define atomic_store(object, desired)
Definition stdatomic.h:289
__UINT64_TYPE__ uint64_t
Definition stdint.h:17
__UINTPTR_TYPE__ uintptr_t
Definition stdint.h:43
boot_memory_t memory
Definition boot_info.h:107
boot_disk_t disk
Definition boot_info.h:105
boot_gop_t gop
Definition boot_info.h:102
void * rsdp
Definition boot_info.h:103
boot_kernel_t kernel
Definition boot_info.h:106
uint64_t length
Definition boot_info.h:47
boot_memory_map_t map
Definition boot_info.h:96
sched_cpu_ctx_t sched
Definition cpu.h:51
File structure.
Definition file.h:37
uint64_t rflags
Definition interrupt.h:64
vfs_ctx_t vfsCtx
Definition process.h:61
thread_t * runThread
The currently running thread.
Definition sched.h:119
clock_t deadline
The time when the time slice will actually expire, only valid while the thread is running.
Definition sched.h:64
uintptr_t top
The top of the stack, this address is not inclusive.
Thread of execution structure.
Definition thread.h:55
sched_thread_ctx_t sched
Definition thread.h:70
process_t * process
The parent process that the thread executes within.
Definition thread.h:57
interrupt_frame_t frame
Definition thread.h:79
stack_pointer_t kernelStack
The kernel stack of the thread.
Definition thread.h:68
void sysfs_init(void)
Initializes the SysFS.
Definition sysfs.c:79
fd_t klog
Definition thrd_create.c:11