Add kernel-common crate
This commit is contained in:
parent
8eba784e7c
commit
084b005dab
14 changed files with 124 additions and 41 deletions
17
kernel/Cargo.lock
generated
17
kernel/Cargo.lock
generated
|
@ -32,14 +32,6 @@ dependencies = [
|
|||
"wyz",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "com_logger"
|
||||
version = "0.1.1"
|
||||
source = "git+https://git.strypsteen.com/mathieu/com_logger#4e27766d141872d398a6f58e8c9580f7317591b2"
|
||||
dependencies = [
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "funty"
|
||||
version = "2.0.0"
|
||||
|
@ -52,12 +44,19 @@ version = "0.0.1"
|
|||
dependencies = [
|
||||
"bitfield",
|
||||
"bitvec",
|
||||
"com_logger",
|
||||
"kernel-common",
|
||||
"log",
|
||||
"spin",
|
||||
"static-alloc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "kernel-common"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lock_api"
|
||||
version = "0.4.12"
|
||||
|
|
|
@ -6,7 +6,7 @@ edition = "2021"
|
|||
[dependencies]
|
||||
bitfield = "0.15.0"
|
||||
bitvec = {version = "1.0.1", default-features = false, features = ["alloc", "atomic"]}
|
||||
com_logger = { git = "https://git.strypsteen.com/mathieu/com_logger", version = "0.1.1" }
|
||||
kernel-common = {path = "../lib/kernel-common"}
|
||||
log = "0.4.22"
|
||||
spin = "0.9.8"
|
||||
static-alloc = "0.2.5"
|
||||
|
|
|
@ -1,8 +1,7 @@
|
|||
use core::arch::asm;
|
||||
|
||||
use alloc::boxed::Box;
|
||||
use bitfield::bitfield;
|
||||
use bitvec::{bitvec, order::Lsb0};
|
||||
use kernel_common::instructions::load_cr3;
|
||||
use log::info;
|
||||
use spin::{Mutex, Once};
|
||||
|
||||
|
@ -94,7 +93,7 @@ fn virt_to_phys(virt: u64) -> u64 {
|
|||
}
|
||||
}
|
||||
}
|
||||
pub fn map(pml4: &mut PageTable, virt: u64, phys: u64, user: bool, write: bool, exec: bool) {
|
||||
fn map(pml4: &mut PageTable, virt: u64, phys: u64, user: bool, write: bool, exec: bool) {
|
||||
if virt < 0x1000 {
|
||||
panic!("First page shouldn't be mapped");
|
||||
}
|
||||
|
@ -179,7 +178,7 @@ pub fn setup_paging(loader_struct: &LoaderStruct, phys_start: u64) {
|
|||
map(pml4, i * 0x1000, phys, false, true, false);
|
||||
}
|
||||
unsafe {
|
||||
asm!("mov cr3, {}", in(reg) virt_to_phys(pml4 as *const PageTable as u64));
|
||||
load_cr3(virt_to_phys(pml4 as *const PageTable as u64));
|
||||
}
|
||||
let mut current_pml4 = CURRENT_PML4.lock();
|
||||
*current_pml4 = Some(pml4);
|
||||
|
|
|
@ -3,12 +3,13 @@
|
|||
|
||||
extern crate alloc;
|
||||
|
||||
use core::{
|
||||
arch::{asm, global_asm},
|
||||
panic::PanicInfo,
|
||||
};
|
||||
use core::{arch::global_asm, panic::PanicInfo};
|
||||
|
||||
use cpu::{gdt::setup_gdt, idt::setup_idt, paging::setup_paging};
|
||||
use kernel_common::{
|
||||
instructions::{cli, hlt},
|
||||
log::init_logger,
|
||||
};
|
||||
use log::{error, info};
|
||||
use misc::loader_struct::LoaderStruct;
|
||||
use static_alloc::Bump;
|
||||
|
@ -36,7 +37,7 @@ global_asm!(
|
|||
|
||||
#[no_mangle]
|
||||
extern "C" fn main(temp_loader_struct: &LoaderStruct) -> ! {
|
||||
com_logger::init();
|
||||
init_logger();
|
||||
info!("Starting kernel...");
|
||||
let loader_struct = temp_loader_struct.clone();
|
||||
if loader_struct.magic != 0x123456789abcdef0 {
|
||||
|
@ -46,17 +47,14 @@ extern "C" fn main(temp_loader_struct: &LoaderStruct) -> ! {
|
|||
setup_idt();
|
||||
setup_paging(&loader_struct, loader_struct.phys_kernel_start);
|
||||
loop {
|
||||
unsafe {
|
||||
asm!("hlt");
|
||||
}
|
||||
hlt();
|
||||
}
|
||||
}
|
||||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
error!("{}", info);
|
||||
cli();
|
||||
loop {
|
||||
unsafe {
|
||||
asm!("hlt");
|
||||
}
|
||||
hlt();
|
||||
}
|
||||
}
|
||||
|
|
16
lib/kernel-common/Cargo.lock
generated
Normal file
16
lib/kernel-common/Cargo.lock
generated
Normal file
|
@ -0,0 +1,16 @@
|
|||
# This file is automatically @generated by Cargo.
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "kernel-common"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a7a70ba024b9dc04c27ea2f0c0548feb474ec5c54bba33a7f72f873a39d07b24"
|
7
lib/kernel-common/Cargo.toml
Normal file
7
lib/kernel-common/Cargo.toml
Normal file
|
@ -0,0 +1,7 @@
|
|||
[package]
|
||||
name = "kernel-common"
|
||||
version = "0.0.1"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
log = "0.4.22"
|
15
lib/kernel-common/src/instructions.rs
Normal file
15
lib/kernel-common/src/instructions.rs
Normal file
|
@ -0,0 +1,15 @@
|
|||
use core::arch::asm;
|
||||
|
||||
pub fn cli() {
|
||||
unsafe {
|
||||
asm!("cli");
|
||||
}
|
||||
}
|
||||
pub fn hlt() {
|
||||
unsafe {
|
||||
asm!("hlt");
|
||||
}
|
||||
}
|
||||
pub unsafe fn load_cr3(cr3: u64) {
|
||||
asm!("mov cr3, {}", in(reg) cr3);
|
||||
}
|
5
lib/kernel-common/src/ioports.rs
Normal file
5
lib/kernel-common/src/ioports.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
use core::arch::asm;
|
||||
|
||||
pub unsafe fn outb(port: u16, value: u8) {
|
||||
asm!("out dx, al", in("al") value, in("dx") port);
|
||||
}
|
5
lib/kernel-common/src/lib.rs
Normal file
5
lib/kernel-common/src/lib.rs
Normal file
|
@ -0,0 +1,5 @@
|
|||
#![no_std]
|
||||
|
||||
pub mod instructions;
|
||||
pub mod ioports;
|
||||
pub mod log;
|
36
lib/kernel-common/src/log.rs
Normal file
36
lib/kernel-common/src/log.rs
Normal file
|
@ -0,0 +1,36 @@
|
|||
use core::fmt::{Result, Write};
|
||||
|
||||
use log::{LevelFilter, Log, Metadata};
|
||||
|
||||
use crate::ioports::outb;
|
||||
|
||||
struct SerialWriter;
|
||||
pub struct SerialLogger;
|
||||
|
||||
static LOGGER: SerialLogger = SerialLogger {};
|
||||
|
||||
impl Write for SerialWriter {
|
||||
fn write_str(&mut self, s: &str) -> Result {
|
||||
for byte in s.bytes() {
|
||||
unsafe {
|
||||
outb(0x3f8, byte);
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
impl Log for SerialLogger {
|
||||
fn enabled(&self, _metadata: &Metadata) -> bool {
|
||||
true
|
||||
}
|
||||
fn log(&self, record: &log::Record) {
|
||||
let mut writer = SerialWriter {};
|
||||
writeln!(writer, "{}: {}", record.level(), record.args()).unwrap();
|
||||
}
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
pub fn init_logger() {
|
||||
log::set_logger(&LOGGER).unwrap();
|
||||
log::set_max_level(LevelFilter::Trace);
|
||||
}
|
17
loader/Cargo.lock
generated
17
loader/Cargo.lock
generated
|
@ -32,27 +32,26 @@ version = "1.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
||||
|
||||
[[package]]
|
||||
name = "com_logger"
|
||||
version = "0.1.1"
|
||||
source = "git+https://git.strypsteen.com/mathieu/com_logger#4e27766d141872d398a6f58e8c9580f7317591b2"
|
||||
dependencies = [
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "elf"
|
||||
version = "0.7.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4445909572dbd556c457c849c4ca58623d84b27c8fff1e74b0b4227d8b90d17b"
|
||||
|
||||
[[package]]
|
||||
name = "kernel-common"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"log",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "loader"
|
||||
version = "0.0.1"
|
||||
dependencies = [
|
||||
"bitfield",
|
||||
"com_logger",
|
||||
"elf",
|
||||
"kernel-common",
|
||||
"log",
|
||||
"static-alloc",
|
||||
"uefi",
|
||||
|
|
|
@ -5,8 +5,8 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
bitfield = "0.15.0"
|
||||
com_logger = { git = "https://git.strypsteen.com/mathieu/com_logger", version = "0.1.1" }
|
||||
elf = {version = "0.7.4", default-features = false}
|
||||
kernel-common = {path = "../lib/kernel-common"}
|
||||
log = "0.4.22"
|
||||
static-alloc = "0.2.5"
|
||||
uefi = "0.29.0"
|
||||
|
|
|
@ -3,9 +3,13 @@
|
|||
|
||||
extern crate alloc;
|
||||
|
||||
use core::{arch::asm, mem, panic::PanicInfo};
|
||||
use core::{mem, panic::PanicInfo};
|
||||
|
||||
use elf::{load_kernel, map_kernel};
|
||||
use kernel_common::{
|
||||
instructions::{cli, hlt},
|
||||
log::init_logger,
|
||||
};
|
||||
use loader_struct::{generate_loader_struct, LoaderStruct};
|
||||
use log::{error, info};
|
||||
use paging::setup_paging;
|
||||
|
@ -27,7 +31,7 @@ const KERNEL: &[u8] = include_bytes!("../../kernel/target/x86_64-unknown-none/re
|
|||
|
||||
#[entry]
|
||||
fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
|
||||
com_logger::init();
|
||||
init_logger();
|
||||
info!("Starting bootloader...");
|
||||
uefi::helpers::init(&mut system_table).unwrap();
|
||||
let (kernel_start, kernel_entry) = load_kernel(KERNEL, &system_table);
|
||||
|
@ -46,9 +50,8 @@ fn main(image_handle: Handle, mut system_table: SystemTable<Boot>) -> Status {
|
|||
#[panic_handler]
|
||||
fn panic(info: &PanicInfo) -> ! {
|
||||
error!("{}", info);
|
||||
cli();
|
||||
loop {
|
||||
unsafe {
|
||||
asm!("hlt");
|
||||
}
|
||||
hlt();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ use core::arch::asm;
|
|||
|
||||
use alloc::boxed::Box;
|
||||
use bitfield::bitfield;
|
||||
use kernel_common::instructions::load_cr3;
|
||||
use uefi::table::boot::{MemoryMap, MemoryType};
|
||||
|
||||
bitfield! {
|
||||
|
@ -82,7 +83,7 @@ pub fn setup_paging(memory_map: &MemoryMap) -> &mut PageTable {
|
|||
}
|
||||
}
|
||||
unsafe {
|
||||
asm!("mov cr3, {}", in(reg) pml4 as *const PageTable as u64);
|
||||
load_cr3(pml4 as *const PageTable as u64);
|
||||
// Write Protect
|
||||
asm!("mov rax, cr0; bts rax, 16; mov cr0, rax", out("rax") _);
|
||||
// No-Execute Enable
|
||||
|
|
Loading…
Add table
Reference in a new issue