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
#![feature(lang_items, start, llvm_asm, global_asm, naked_functions, core_intrinsics, const_fn, abi_x86_interrupt, allocator_api, box_syntax, no_more_cas, step_trait, step_trait_ext, thread_local, nll, exclusive_range_pattern)]
#![no_std]
#![cfg_attr(target_os = "none", no_main)]
#![recursion_limit = "1024"]
#![warn(unused)]
#![warn(missing_debug_implementations)]
#![allow(unused_unsafe)]
#![allow(unreachable_code)]
#![allow(dead_code)]
#![cfg_attr(test, allow(unused_imports))]
#![warn(missing_docs)]
#![deny(intra_doc_link_resolution_failure)]
#[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;
#[macro_use]
extern crate alloc;
#[macro_use]
extern crate log;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate bitfield;
#[cfg(test)]
#[macro_use]
extern crate mashup;
use core::fmt::Write;
use crate::utils::io;
pub mod paging;
pub mod event;
pub mod error;
pub mod log_impl;
#[cfg(any(target_arch = "x86", test, doc))]
#[macro_use]
pub mod i386;
pub mod syscalls;
pub mod frame_allocator;
pub mod heap_allocator;
pub mod devices;
pub mod sync;
pub mod timer;
pub mod process;
pub mod scheduler;
pub mod mem;
pub mod ipc;
pub mod elf_loader;
pub mod utils;
pub mod checks;
pub mod cpu_locals;
pub mod panic;
#[cfg(target_os = "none")]
pub use crate::heap_allocator::rust_oom;
#[cfg(not(test))]
#[global_allocator]
static ALLOCATOR: heap_allocator::Allocator = heap_allocator::Allocator::new();
use crate::i386::stack;
use crate::paging::PAGE_SIZE;
use crate::mem::VirtualAddress;
use crate::process::ProcessStruct;
use crate::cpu_locals::init_cpu_locals;
use sunrise_libkern::process::*;
unsafe fn force_double_fault() {
loop {
llvm_asm!("push 0" :::: "intel", "volatile");
}
}
fn main() {
info!("Loading all the init processes");
for module in i386::multiboot::get_boot_information().module_tags().skip(1) {
info!("Loading {}", module.name());
let mapped_module = elf_loader::map_grub_module(module)
.unwrap_or_else(|_| panic!("Unable to find available memory for module {}", module.name()));
let kip_header = elf_loader::get_kip_header(&mapped_module)
.unwrap_or_else(|| panic!("Unable to find KIP header for module {}", module.name()));
let mut flags = ProcInfoFlags(0);
flags.set_address_space_type(ProcInfoAddrSpace::AS32Bit);
flags.set_debug(true);
flags.set_pool_partition(PoolPartition::Sysmodule);
let aslr_base = 0x400000;
let procinfo = ProcInfo {
name: kip_header.name,
process_category: kip_header.process_category,
title_id: kip_header.title_id,
code_addr: aslr_base as _,
code_num_pages: 0,
flags,
resource_limit_handle: None,
system_resource_num_pages: 0
};
let proc = ProcessStruct::new(&procinfo, elf_loader::get_kacs(&mapped_module)).unwrap();
{
let mut pmemlock = proc.pmemory.lock();
elf_loader::load_builtin(&mut pmemlock, &mapped_module, aslr_base);
};
ProcessStruct::start(&proc, u32::from(kip_header.main_thread_priority), kip_header.stack_page_count as usize * PAGE_SIZE)
.expect("failed creating process");
}
let lock = sync::SpinLockIRQ::new(());
loop {
let _ = scheduler::unschedule(&lock, lock.lock());
}
}
#[cfg(any(target_os = "none", doc))]
#[no_mangle]
pub unsafe extern fn 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
// Save multiboot infos addr present in ebx
push ebx
call common_start" : : : : "intel", "volatile");
core::intrinsics::unreachable()
}
#[cfg(any(target_os = "none", doc))]
#[no_mangle]
pub extern "C" fn common_start(multiboot_info_addr: usize) -> ! {
use crate::devices::rs232::{SerialAttributes, SerialColor};
log_impl::early_init();
let log = &mut devices::rs232::SerialLogger;
let _ = writeln!(log, "\n# Welcome to {}SunriseOS{}!\n",
SerialAttributes::fg(SerialColor::LightCyan),
SerialAttributes::default());
let boot_info = unsafe { multiboot2::load(multiboot_info_addr) };
info!("Parsed multiboot informations");
frame_allocator::init(&boot_info);
info!("Initialized frame allocator");
info!("Initializing gdt...");
i386::gdt::init_gdt();
info!("Gdt initialized");
i386::multiboot::init(boot_info);
log_impl::init();
info!("Start ACPI detection");
unsafe { i386::acpi::init(); }
info!("Allocating cpu_locals");
init_cpu_locals(1);
info!("Enabling interrupts");
unsafe { i386::interrupt_service_routines::init(); }
devices::init_timer();
info!("Becoming the first process");
unsafe { scheduler::create_first_process() };
info!("Calling main()");
main();
loop {
#[cfg(target_os = "none")]
unsafe { llvm_asm!("HLT"); }
}
}
#[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<'_>) -> ! {
panic::kernel_panic(&panic::PanicOrigin::KernelAssert {
panic_message: format_args!("{}", p)
});
}