2023-07-18 18:03:45 +03:00
|
|
|
//! osdev-x kernel crate
|
|
|
|
#![feature(
|
|
|
|
naked_functions,
|
|
|
|
asm_const,
|
|
|
|
panic_info_message,
|
|
|
|
optimize_attribute,
|
|
|
|
const_trait_impl,
|
|
|
|
maybe_uninit_slice,
|
2023-07-25 16:47:00 +03:00
|
|
|
arbitrary_self_types,
|
2023-07-28 14:26:39 +03:00
|
|
|
const_mut_refs,
|
2023-08-02 19:53:54 +03:00
|
|
|
let_chains,
|
2023-08-21 17:26:44 +03:00
|
|
|
linked_list_cursors,
|
2023-08-31 13:40:17 +03:00
|
|
|
rustc_private,
|
|
|
|
allocator_api
|
2023-07-18 18:03:45 +03:00
|
|
|
)]
|
2023-08-01 18:05:10 +03:00
|
|
|
#![allow(clippy::new_without_default, clippy::fn_to_numeric_cast)]
|
2023-07-18 18:03:45 +03:00
|
|
|
#![warn(missing_docs)]
|
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
|
2023-08-18 22:53:58 +03:00
|
|
|
use sync::SpinFence;
|
2023-08-21 17:26:44 +03:00
|
|
|
use task::spawn_kernel_closure;
|
2023-08-18 22:53:58 +03:00
|
|
|
|
|
|
|
use crate::{
|
|
|
|
arch::{Architecture, ArchitectureImpl, ARCHITECTURE},
|
|
|
|
mem::heap,
|
2023-08-31 13:40:17 +03:00
|
|
|
task::Cpu,
|
2023-08-02 19:53:54 +03:00
|
|
|
};
|
|
|
|
|
2023-07-29 19:31:56 +03:00
|
|
|
extern crate yggdrasil_abi as abi;
|
2023-08-02 20:43:21 +03:00
|
|
|
|
2023-07-30 16:40:30 +03:00
|
|
|
extern crate alloc;
|
2023-08-21 17:26:44 +03:00
|
|
|
extern crate compiler_builtins;
|
2023-08-01 11:09:56 +03:00
|
|
|
|
2023-07-29 19:31:56 +03:00
|
|
|
#[macro_use]
|
|
|
|
pub mod debug;
|
2023-07-18 18:03:45 +03:00
|
|
|
#[macro_use]
|
|
|
|
pub mod arch;
|
|
|
|
|
2023-07-29 21:11:15 +03:00
|
|
|
pub mod device;
|
2023-08-02 19:53:54 +03:00
|
|
|
pub mod fs;
|
2023-08-18 22:53:58 +03:00
|
|
|
pub mod init;
|
2023-07-29 19:31:56 +03:00
|
|
|
pub mod mem;
|
2023-08-01 11:09:56 +03:00
|
|
|
pub mod panic;
|
2023-08-02 19:53:54 +03:00
|
|
|
pub mod proc;
|
2023-07-29 19:31:56 +03:00
|
|
|
pub mod sync;
|
2023-08-02 19:53:54 +03:00
|
|
|
pub mod syscall;
|
2023-08-01 18:05:10 +03:00
|
|
|
pub mod task;
|
2023-08-31 19:54:09 +03:00
|
|
|
pub mod util;
|
2023-08-02 19:53:54 +03:00
|
|
|
|
2023-08-18 22:53:58 +03:00
|
|
|
static CPU_INIT_FENCE: SpinFence = SpinFence::new();
|
|
|
|
|
|
|
|
/// Common kernel main function for application processors
|
|
|
|
pub fn kernel_secondary_main() -> ! {
|
|
|
|
// Synchronize the CPUs to this point
|
|
|
|
CPU_INIT_FENCE.signal();
|
|
|
|
CPU_INIT_FENCE.wait_all(ArchitectureImpl::cpu_count());
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
task::enter();
|
|
|
|
}
|
2023-08-02 19:53:54 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 22:53:58 +03:00
|
|
|
/// Common kernel main function. Must be called for BSP processor only.
|
|
|
|
///
|
|
|
|
/// # Prerequisites
|
2023-08-02 19:53:54 +03:00
|
|
|
///
|
2023-08-18 22:53:58 +03:00
|
|
|
/// Before the function can be called, the following preparations must be made:
|
2023-08-02 19:53:54 +03:00
|
|
|
///
|
2023-08-18 22:53:58 +03:00
|
|
|
/// * Virtual memory set up according to the architecture's memory map
|
|
|
|
/// * Physical memory
|
|
|
|
/// * Heap
|
|
|
|
/// * Basic debugging facilities
|
|
|
|
/// * Initrd
|
|
|
|
pub fn kernel_main() -> ! {
|
|
|
|
debugln!("Heap: {:#x?}", heap::heap_range());
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
ARCHITECTURE.start_application_processors();
|
2023-08-08 20:00:24 +03:00
|
|
|
}
|
2023-08-02 19:53:54 +03:00
|
|
|
|
2023-08-31 13:40:17 +03:00
|
|
|
Cpu::init_ipi_queues();
|
2023-08-18 22:53:58 +03:00
|
|
|
|
|
|
|
// Wait until all APs initialize
|
|
|
|
CPU_INIT_FENCE.signal();
|
|
|
|
CPU_INIT_FENCE.wait_all(ArchitectureImpl::cpu_count());
|
|
|
|
|
|
|
|
task::init().expect("Failed to initialize the scheduler");
|
|
|
|
|
|
|
|
spawn_kernel_closure("[kinit]", init::kinit).expect("Could not spawn [kinit]");
|
|
|
|
|
|
|
|
infoln!("All cpus ready");
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
task::enter();
|
2023-08-02 19:53:54 +03:00
|
|
|
}
|
|
|
|
}
|