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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
#![feature(lang_items, start, llvm_asm, global_asm, naked_functions, core_intrinsics, const_fn, abi_x86_interrupt)]
#![no_std]
#![cfg_attr(target_os = "none", no_main)]
#![warn(unused)]
#![allow(missing_debug_implementations)]
#![allow(unused_unsafe)]
#![allow(unreachable_code)]
#![allow(dead_code)]
#![cfg_attr(test, allow(unused_imports))]
#![deny(intra_doc_link_resolution_failure)]
#![allow(missing_docs, clippy::missing_docs_in_private_items)]
#![deny(intra_doc_link_resolution_failure)]
#![allow(clippy::cast_lossless)]
#[cfg(not(any(target_arch = "x86", test, doc)))]
compile_error!("WTF");
#[cfg(not(target_os = "none"))]
extern crate std;
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate bitflags;
#[macro_use]
extern crate static_assertions;
use core::fmt::Write;
pub mod bootstrap_logging;
pub mod gdt;
pub mod address;
pub mod paging;
pub mod frame_alloc;
pub mod elf_loader;
pub mod bootstrap_stack;
use crate::bootstrap_logging::Serial;
use crate::frame_alloc::FrameAllocator;
use crate::paging::{PageTablesSet, KernelLand};
use crate::bootstrap_stack::BootstrapStack;
#[repr(align(4096))]
pub struct AlignedStack([u8; 4096 * 4]);
pub static mut STACK: AlignedStack = AlignedStack([0; 4096 * 4]);
pub fn print_stack() {
unsafe {
let sp: usize;
llvm_asm!("mov $0, esp" : "=r"(sp) : : : "intel");
let sp_start = sp - crate::STACK.0.as_ptr() as usize;
sunrise_libutils::print_hexdump(&mut Serial, &crate::STACK.0[sp_start..]);
}
}
#[cfg(any(target_os = "none", doc))]
#[no_mangle]
pub unsafe extern fn bootstrap_start() -> ! {
llvm_asm!("
// Memset the bss. Hopefully memset doesn't actually use the bss...
mov eax, BSS_END
sub eax, BSS_START
push eax
push 0
push BSS_START
call memset
add esp, 12
// Create the stack
mov esp, $0
add esp, 16383
mov ebp, esp
// Save multiboot infos addr present in ebx
push ebx
call do_bootstrap" : : "m"(&STACK) : : "intel", "volatile");
core::intrinsics::unreachable()
}
#[no_mangle]
pub extern "C" fn do_bootstrap(multiboot_info_addr: usize) -> ! {
unsafe { bootstrap_logging::init_bootstrap_log() };
let _ = writeln!(Serial, "Bootstrap starts...");
gdt::init_gdt();
let _ = writeln!(Serial, "= Gdt initialized");
let boot_info = unsafe { multiboot2::load(multiboot_info_addr) };
let _ = write!(Serial, "{:?}", boot_info);
let _ = writeln!(Serial, "= Parsed multiboot informations");
FrameAllocator::init(&boot_info);
let _ = writeln!(Serial, "= Initialized frame allocator");
let mut page_tables = unsafe { paging::map_bootstrap(&boot_info) };
let _ = writeln!(Serial, "= Created page tables");
let kernel_entry_point = elf_loader::load_kernel(&mut page_tables, &boot_info);
let _ = writeln!(Serial, "= Loaded kernel");
let multiboot_info_page = page_tables.get_page::<KernelLand>();
let multiboot_phys_page = page_tables.get_phys(multiboot_info_page).unwrap();
let total_size = boot_info.total_size();
assert!(total_size <= paging::PAGE_SIZE, "Expected multiboot info to fit in a page");
unsafe {
core::ptr::copy(multiboot_info_addr as *const u8,
multiboot_phys_page.addr() as *mut u8,
total_size);
}
let _ = writeln!(Serial, "= Copied multiboot info to page {:#010x}", multiboot_info_page.addr());
unsafe { page_tables.enable_paging() }
let _ = writeln!(Serial, "= Paging on");
let new_stack = BootstrapStack::allocate_stack()
.expect("Cannot allocate bootstrap stack");
let _ = writeln!(Serial, "= Created kernel stack");
let new_ebp_esp = new_stack.get_stack_start();
let _ = writeln!(Serial, "= Jumping to kernel");
#[cfg(not(test))]
unsafe {
llvm_asm!("
// save multiboot info pointer
mov ebx, $0
// switch to the new stack
mov ebp, $1
mov esp, $1
// jump to the kernel
jmp $2"
:
: "r"(multiboot_info_page), "r"(new_ebp_esp), "r"(kernel_entry_point)
: "memory", "ebx"
: "intel", "volatile");
}
unreachable!()
}
#[cfg(target_os = "none")]
#[lang = "eh_personality"] #[no_mangle] pub extern fn eh_personality() {}
#[cfg(target_os = "none")]
#[panic_handler] #[no_mangle]
pub extern fn panic_fmt(p: &::core::panic::PanicInfo<'_>) -> ! {
let _ = writeln!(Serial,
"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\
! Bootstrap panic!\n\
! {}\n\
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!",
p);
loop { unsafe { llvm_asm!("HLT"); } }
}
macro_rules! multiboot_header {
($($name:ident: $tagty:ident :: $method:ident($($args:expr),*)),*) => {
#[repr(C)]
#[allow(dead_code)]
pub struct MultiBootHeader {
magic: u32,
architecture: u32,
header_length: u32,
checksum: u32,
$($name: $tagty),*
}
#[used]
#[cfg_attr(target_os = "none", link_section = ".multiboot_header")]
pub static MULTIBOOT_HEADER: MultiBootHeader = MultiBootHeader {
magic: 0xe85250d6,
architecture: 0,
header_length: core::mem::size_of::<MultiBootHeader>() as u32,
checksum: u32::max_value() - (0xe85250d6 + 0 + core::mem::size_of::<MultiBootHeader>() as u32) + 1,
$($name: $tagty::$method($($args),*)),*
};
}
}
#[repr(C, align(8))]
struct EndTag {
tag: u16,
flag: u16,
size: u32
}
impl EndTag {
const fn default() -> EndTag {
EndTag {
tag: 0,
flag: 0,
size: ::core::mem::size_of::<Self>() as u32
}
}
}
#[repr(C, align(8))]
struct FramebufferTag {
tag: u16,
flags: u16,
size: u32,
width: u32,
height: u32,
depth: u32
}
impl FramebufferTag {
const fn new(width: u32, height: u32, depth: u32) -> FramebufferTag {
FramebufferTag {
tag: 5,
flags: 0,
size: ::core::mem::size_of::<Self>() as u32,
width: width,
height: height,
depth: depth
}
}
}
#[repr(C, align(8))]
struct ModuleAlignmentTag {
tag: u16,
flags: u16,
size: u32,
}
impl ModuleAlignmentTag {
const fn new() -> ModuleAlignmentTag {
ModuleAlignmentTag {
tag: 6,
flags: 0,
size: ::core::mem::size_of::<Self>() as u32,
}
}
}
multiboot_header! {
framebuffer: FramebufferTag::new(1280, 800, 32),
end: EndTag::default()
}