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,
|
|
|
|
linked_list_cursors
|
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-02 19:53:54 +03:00
|
|
|
use abi::{
|
|
|
|
error::Error,
|
|
|
|
io::{FileMode, OpenOptions, RawFd},
|
|
|
|
};
|
|
|
|
use fs::{devfs, FileBlockAllocator, INITRD_DATA};
|
|
|
|
use memfs::MemoryFilesystem;
|
|
|
|
use vfs::{Filesystem, IoContext, VnodeRef};
|
|
|
|
|
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-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-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-07-29 19:31:56 +03:00
|
|
|
pub mod util;
|
2023-08-02 19:53:54 +03:00
|
|
|
|
|
|
|
fn setup_root() -> Result<VnodeRef, Error> {
|
|
|
|
let initrd_data = INITRD_DATA.get();
|
|
|
|
let fs = MemoryFilesystem::<FileBlockAllocator>::from_slice(initrd_data.data).unwrap();
|
|
|
|
fs.root()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Entry point for common kernel code.
|
|
|
|
///
|
|
|
|
/// # Note
|
|
|
|
///
|
|
|
|
/// This function is meant to be used as a kernel-space process after all the platform-specific
|
|
|
|
/// initialization has finished.
|
|
|
|
pub fn kernel_main() {
|
2023-08-13 21:23:58 +03:00
|
|
|
infoln!("In main");
|
|
|
|
|
2023-08-08 20:00:24 +03:00
|
|
|
#[cfg(feature = "fb_console")]
|
|
|
|
{
|
2023-08-09 00:14:25 +03:00
|
|
|
use core::time::Duration;
|
|
|
|
|
2023-08-08 20:00:24 +03:00
|
|
|
use device::display::console::task_update_consoles;
|
2023-08-09 00:14:25 +03:00
|
|
|
use task::tasklet;
|
|
|
|
|
2023-08-08 20:00:24 +03:00
|
|
|
tasklet::add_periodic(
|
|
|
|
"update-console",
|
|
|
|
Duration::from_millis(15),
|
|
|
|
task_update_consoles,
|
|
|
|
);
|
|
|
|
}
|
2023-08-02 19:53:54 +03:00
|
|
|
|
|
|
|
let root = match setup_root() {
|
|
|
|
Ok(root) => root,
|
|
|
|
Err(err) => {
|
|
|
|
warnln!("Could not setup root from initrd: {:?}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let ioctx = IoContext::new(root);
|
|
|
|
let node = ioctx.find(None, "/init", true, true).unwrap();
|
|
|
|
let file = node.open(OpenOptions::READ, FileMode::empty()).unwrap();
|
|
|
|
|
|
|
|
let devfs = devfs::root();
|
2023-08-02 20:43:21 +03:00
|
|
|
#[cfg(target_arch = "x86_64")]
|
2023-08-02 19:53:54 +03:00
|
|
|
let console = ioctx.find(Some(devfs.clone()), "tty0", true, true).unwrap();
|
2023-08-02 20:43:21 +03:00
|
|
|
#[cfg(target_arch = "aarch64")]
|
|
|
|
let console = ioctx
|
|
|
|
.find(Some(devfs.clone()), "ttyS0", true, true)
|
|
|
|
.unwrap();
|
2023-08-02 19:53:54 +03:00
|
|
|
let stdin = console.open(OpenOptions::READ, FileMode::empty()).unwrap();
|
|
|
|
let stdout = console.open(OpenOptions::WRITE, FileMode::empty()).unwrap();
|
|
|
|
let stderr = stdout.clone();
|
|
|
|
|
|
|
|
{
|
|
|
|
let user_init = proc::exec::load_elf("init", file, &["/init", "xxx"]).unwrap();
|
|
|
|
let mut io = user_init.io.lock();
|
|
|
|
io.set_ioctx(ioctx);
|
|
|
|
io.set_file(RawFd::STDIN, stdin).unwrap();
|
|
|
|
io.set_file(RawFd::STDOUT, stdout).unwrap();
|
|
|
|
io.set_file(RawFd::STDERR, stderr).unwrap();
|
|
|
|
drop(io);
|
|
|
|
|
|
|
|
user_init.set_session_terminal(console);
|
|
|
|
|
|
|
|
user_init.enqueue_somewhere();
|
|
|
|
}
|
|
|
|
}
|