1#include <acpi/acpi.h>
2#include <acpi/cst.h>
3#include <acpi/hpet.h>
4#include <acpi/ioapic.h>
5#include <acpi/lapic.h>
6#include <acpi/uacpi_interface.h>
7#include <asm.h>
8#include <boot/gdt.h>
9#include <bootstage.h>
10#include <cmdline.h>
11#include <compiler.h>
12#include <console/printf.h>
13#include <crypto/prng.h>
14#include <drivers/iommu/iommu.h>
15#include <drivers/mmio.h>
16#include <elf.h>
17#include <fs/vfs.h>
18#include <global.h>
19#include <irq/idt.h>
20#include <limine.h>
21#include <log.h>
22#include <logo.h>
23#include <mem/address_range.h>
24#include <mem/alloc.h>
25#include <mem/asan.h>
26#include <mem/buddy.h>
27#include <mem/domain.h>
28#include <mem/movealloc.h>
29#include <mem/page_alloc.h>
30#include <mem/pmm.h>
31#include <mem/slab.h>
32#include <mem/tlb.h>
33#include <mem/vmm.h>
34#include <registry.h>
35#include <requests.h>
36#include <sch/domain.h>
37#include <sch/periodic_work.h>
38#include <sch/sched.h>
39#include <smp/core.h>
40#include <smp/domain.h>
41#include <smp/percpu.h>
42#include <smp/perdomain.h>
43#include <smp/pernode.h>
44#include <smp/smp.h>
45#include <stdint.h>
46#include <sync/rcu.h>
47#include <sync/turnstile.h>
48#include <syscall.h>
49#include <test.h>
50#include <thread/dpc.h>
51#include <thread/reaper.h>
52#include <thread/thread.h>
53#include <thread/workqueue.h>
54
55struct globals global = {0};
56
57#define BEHAVIOR /* avoids undefined behavior */
58
59__no_sanitize_address void k_main(void) {
60 disable_interrupts();
61 global.core_count = mp_request.response->cpu_count;
62 global.hhdm_offset = hhdm_request.response->offset;
63 global.pt_epoch = 1;
64
65 printf_init(fb: framebuffer_request.response->framebuffers[0]);
66 bootstage_advance(new: BOOTSTAGE_EARLY_FB);
67
68 if (cmdline_wants_help(input: cmdline_request.response->cmdline))
69 cmdline_dump_help();
70
71 pmm_early_init(m: memmap_request);
72 vmm_init(memmap: memmap_request.response, xa: xa_request.response);
73 pmm_mid_init();
74
75 address_ranges_init();
76 slab_allocator_init();
77 page_alloc_init();
78
79#ifdef DEBUG_ASAN
80 asan_init();
81#endif
82
83 log_sites_init();
84 bootstage_advance(new: BOOTSTAGE_EARLY_ALLOCATORS);
85 gdt_install();
86 syscall_setup(syscall_entry);
87 smp_setup_bsp();
88
89 mmio_init();
90 irq_init();
91 uacpi_init(rsdp: rsdp_request.response->address);
92 x2apic_init();
93 lapic_init();
94 hpet_init();
95 ioapic_init();
96 acpi_find_cst();
97 bootstage_advance(new: BOOTSTAGE_EARLY_DEVICES);
98
99 srat_init();
100 slit_init();
101 iommu_init();
102
103 domain_init();
104 pmm_late_init();
105 slab_domain_init();
106
107 smp_init();
108
109 domain_init_after_smp();
110 domain_buddies_init_after_smp();
111 thread_init_thread_ids();
112
113 scheduler_init();
114 turnstiles_init();
115
116 tests_hook_boot();
117 cmdline_parse(input: cmdline_request.response->cmdline);
118 lapic_timer_init(/* core_id = */ 0);
119 dpc_init_percpu();
120 smp_wake(mpr: mp_request.response);
121
122 topology_init();
123 scheduler_domains_init();
124 bootstage_advance(new: BOOTSTAGE_MID_TOPOLOGY);
125 struct core *iter;
126 for_each_cpu_struct(iter) {
127 smp_dump_core(c: iter);
128 }
129
130 percpu_obj_init();
131 perdomain_obj_init();
132 pernode_obj_init();
133 scheduler_periodic_work_init();
134 movealloc_exec_all();
135 bootstage_advance(new: BOOTSTAGE_MID_ALLOCATORS);
136
137 scheduler_yield();
138}
139
140void k_sch_main(void *nop) {
141 (void) nop;
142 /* make sure everyone else is idle before we
143 * advance the bootstage here... */
144 smp_wait_for_others_to_idle();
145
146 /* we have to force everyone to disable their
147 * ticks because this prevents anyone from
148 * possibly entering an ISR since IRQL
149 * operations check bootstages and they can
150 * see the MID_ALLOCATORS bootstage (no-op) in
151 * an early `irql_raise` and the LATE_DEVICES
152 * bootstage later on (causing mis-raised IRQLs) */
153 smp_disable_all_ticks();
154
155 bootstage_advance(new: BOOTSTAGE_LATE);
156
157 smp_enable_all_ticks();
158
159 rcu_init();
160 workqueues_permanent_init();
161 defer_init();
162 slab_domain_init_late();
163 domain_buddies_init_late();
164 reaper_init();
165
166 registry_setup();
167 tests_run();
168 bootstage_advance(new: BOOTSTAGE_COMPLETE);
169
170 thread_print(t: thread_get_current());
171
172 domain_buddy_dump();
173}
174