refactored for architectures and added riscv support

This commit is contained in:
Bryan McShea
2024-01-24 00:41:51 -05:00
parent e5a6b6073f
commit 461269ed32
30 changed files with 683 additions and 425 deletions

10
kernel/.cargo/config.toml Normal file
View File

@@ -0,0 +1,10 @@
[build]
# rustflags = ["-g"]
target = ["riscv64gc-unknown-none-elf", "x86_64-unknown-none"]
[target.riscv64gc-unknown-none-elf]
rustflags = [
"-C", "link-arg=-Tsrc/arch/riscv64/link.ld",
"-C", "link-arg=--omagic",
]

216
kernel/Cargo.lock generated Normal file
View File

@@ -0,0 +1,216 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "autocfg"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
[[package]]
name = "az"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b7e4c2464d97fe331d41de9d5db0def0a96f4d823b8b32a2efd503578988973"
[[package]]
name = "bit_field"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc827186963e592360843fb5ba4b973e145841266c1357f7180c43526f2e5b61"
[[package]]
name = "bitflags"
version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bitflags"
version = "2.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
[[package]]
name = "bootloader_api"
version = "0.11.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "daaa1fb791aea49e19db7b7b3662665d511eb4e30a0627fa525fd92652f39358"
[[package]]
name = "byteorder"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
[[package]]
name = "embedded-graphics"
version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0649998afacf6d575d126d83e68b78c0ab0e00ca2ac7e9b3db11b4cbe8274ef0"
dependencies = [
"az",
"byteorder",
"embedded-graphics-core",
"float-cmp",
"micromath",
]
[[package]]
name = "embedded-graphics-core"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ba9ecd261f991856250d2207f6d8376946cd9f412a2165d3b75bc87a0bc7a044"
dependencies = [
"az",
"byteorder",
]
[[package]]
name = "float-cmp"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98de4bbd547a563b716d8dfa9aad1cb19bfab00f4fa09a6a4ed21dbcf44ce9c4"
dependencies = [
"num-traits",
]
[[package]]
name = "kernel"
version = "0.1.0"
dependencies = [
"bootloader_api",
"embedded-graphics",
"lazy_static",
"pc-keyboard",
"pic8259",
"spin 0.9.8",
"uart_16550",
"x86_64",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [
"spin 0.5.2",
]
[[package]]
name = "lock_api"
version = "0.4.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c168f8615b12bc01f9c17e2eb0cc07dcae1940121185446edc3744920e8ef45"
dependencies = [
"autocfg",
"scopeguard",
]
[[package]]
name = "micromath"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3c8dda44ff03a2f238717214da50f65d5a53b45cd213a7370424ffdb6fae815"
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
[[package]]
name = "pc-keyboard"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c6f2d937e3b8d63449b01401e2bae4041bc9dd1129c2e3e0d239407cf6635ac"
[[package]]
name = "pic8259"
version = "0.10.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb844b5b01db1e0b17938685738f113bfc903846f18932b378bc0eabfa40e194"
dependencies = [
"x86_64",
]
[[package]]
name = "raw-cpuid"
version = "10.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c297679cb867470fa8c9f67dbba74a78d78e3e98d7cf2b08d6d71540f797332"
dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "rustversion"
version = "1.0.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ffc183a10b4478d04cbbbfc96d0873219d962dd5accaff2ffbd4ceb7df837f4"
[[package]]
name = "scopeguard"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
dependencies = [
"lock_api",
]
[[package]]
name = "uart_16550"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6dc00444796f6c71f47c85397a35e9c4dbf9901902ac02386940d178e2b78687"
dependencies = [
"bitflags 1.3.2",
"rustversion",
"x86",
]
[[package]]
name = "volatile"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"
[[package]]
name = "x86"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2781db97787217ad2a2845c396a5efe286f87467a5810836db6d74926e94a385"
dependencies = [
"bit_field",
"bitflags 1.3.2",
"raw-cpuid",
]
[[package]]
name = "x86_64"
version = "0.14.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b835097a84e4457323331ec5d6eb23d096066cbfb215d54096dcb4b2e85f500"
dependencies = [
"bit_field",
"bitflags 2.4.2",
"rustversion",
"volatile",
]

View File

@@ -9,15 +9,23 @@ test = false
bench = false
[dependencies]
bootloader_api = "0.11.5"
embedded-graphics = "0.8.1"
uart_16550 = "0.3.0"
x86_64 = "0.14.11"
spin = "0.9.8"
pic8259 = "0.10.1"
pc-keyboard = "0.5.0"
[target.'cfg(target_arch = "x86_64")'.dependencies]
pic8259 = "0.10.1"
bootloader_api = "0.11.5"
x86_64 = "0.14.11"
uart_16550 = "0.3.0"
[dependencies.lazy_static]
version = "1.4.0"
features = ["spin_no_std"]
[profile.dev]
panic = "abort"
[profile.release]
panic = "abort"

9
kernel/src/arch/mod.rs Normal file
View File

@@ -0,0 +1,9 @@
#[cfg(target_arch = "x86_64")]
mod x86_64;
#[cfg(target_arch = "x86_64")]
pub use x86_64::*;
#[cfg(target_arch = "riscv64")]
mod riscv64;
#[cfg(target_arch = "riscv64")]
pub use riscv64::*;

View File

@@ -0,0 +1,26 @@
OUTPUT_ARCH( "riscv" )
ENTRY( _start )
SECTIONS
{
. = 0x80000000;
.text : {
*(.text.init)
*(.text)
}
PROVIDE(global_pointer = .);
.rodata : {
*(.rodata .rodata.*)
}
.data : {
*(.sdata .sdata.*) *(.data .data.*)
}
.bss : {
PROVIDE(_bss_start = .);
*(.sbss .sbss.*) *(.bss .bss.*)
PROVIDE(_bss_end = .);
}
. += 0x8000;
. = ALIGN(16);
PROVIDE(stack_top = .);
}

View File

@@ -0,0 +1,34 @@
use crate::main;
pub mod qemu;
#[no_mangle]
#[link_section = ".text.init"]
pub extern "C" fn _start() -> ! {
unsafe {
core::arch::asm!(
"csrr t0, mhartid",
"bnez t0, {_start}",
_start = sym _start
);
core::arch::asm!(
".option push",
".option norelax",
"la gp, global_pointer",
"la sp, stack_top",
"tail {entry}",
entry = sym entry,
options(noreturn)
);
}
}
extern "C" fn entry() -> ! {
main()
}
pub fn hlt_loop() -> ! {
loop {}
}

View File

@@ -0,0 +1,28 @@
const UART_BASE: u32 = 0x10010000;
const UART_REG_TXFIFO: *mut i32 = (UART_BASE + 0) as *mut i32;
pub fn exit() -> ! {
unsafe {
core::arch::asm!(
"li t0, 0x20026",
"sw t0, 0(sp)",
"move a1, sp",
"li a0, 0x18",
".balign 16",
".option push",
".option norvc",
"slli zero, zero, 0x1f",
"ebreak",
"srai zero, zero, 0x7",
options(noreturn)
);
}
}
pub fn _print(args: core::fmt::Arguments<'_>) {
let msg = args.as_str().expect("bruh");
for b in msg.as_bytes() {
while unsafe { *UART_REG_TXFIFO } < 0 {}
unsafe { *UART_REG_TXFIFO = *b as i32 }
}
}

View File

@@ -1,7 +1,8 @@
use crate::{gdt, print, println};
use super::gdt;
use crate::{print, println};
use lazy_static::lazy_static;
use pic8259::ChainedPics;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
#[derive(Debug, Clone, Copy)]
#[repr(u8)]
@@ -36,6 +37,7 @@ lazy_static! {
idt.breakpoint.set_handler_fn(breakpoint);
idt[InterruptIndex::Timer.as_usize()].set_handler_fn(timer);
idt[InterruptIndex::Keyboard.as_usize()].set_handler_fn(keyboard);
idt.page_fault.set_handler_fn(page_fault);
idt
};
}
@@ -56,16 +58,28 @@ extern "x86-interrupt" fn double_fault(stack_frame: InterruptStackFrame, _error_
panic!("double fault exception: {:#?}", stack_frame);
}
extern "x86-interrupt" fn page_fault(
stack_frame: InterruptStackFrame,
error_code: PageFaultErrorCode,
) {
use x86_64::registers::control::Cr2;
println!("EXCEPTION: PAGE FAULT");
println!("Accessed Address: {:?}", Cr2::read());
println!("Error Code: {:?}", error_code);
println!("{:#?}", stack_frame);
super::hlt_loop();
}
extern "x86-interrupt" fn keyboard(_stack_frame: InterruptStackFrame) {
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
use spin::Mutex;
use x86_64::instructions::port::Port;
lazy_static! {
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> =
Mutex::new(Keyboard::new(layouts::Us104Key, ScancodeSet1,
HandleControl::Ignore)
);
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> = Mutex::new(
Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore)
);
}
let mut keyboard = KEYBOARD.lock();

View File

@@ -0,0 +1,23 @@
use crate::main;
pub mod framebuffer;
pub mod gdt;
pub mod interrupts;
pub mod qemu;
bootloader_api::entry_point!(_start);
fn _start(boot_info: &'static mut bootloader_api::BootInfo) -> ! {
gdt::init();
interrupts::init();
if let Some(framebuffer) = boot_info.framebuffer.as_mut() {
framebuffer::draw_test(framebuffer);
}
main();
}
pub fn hlt_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
}

View File

@@ -0,0 +1,25 @@
use core::fmt::Arguments;
use spin::Mutex;
use uart_16550::SerialPort;
use x86_64::instructions::{interrupts, port::Port};
use super::hlt_loop;
pub static UART: Mutex<SerialPort> = Mutex::new(unsafe { SerialPort::new(0x3F8) });
pub fn exit() -> ! {
unsafe {
let mut port = Port::new(0xf4);
port.write(0x10u32);
}
hlt_loop()
}
#[doc(hidden)]
pub fn _print(args: Arguments<'_>) {
use core::fmt::Write;
interrupts::without_interrupts(|| {
UART.lock().write_fmt(args).unwrap();
})
}

View File

@@ -1,25 +0,0 @@
#![no_std]
#![feature(abi_x86_interrupt)]
pub mod framebuffer;
pub mod gdt;
pub mod interrupts;
pub mod qemu;
pub mod log;
pub fn init() {
gdt::init();
interrupts::init();
}
pub fn exit() -> ! {
qemu::exit();
hlt_loop()
}
pub fn hlt_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
}

26
kernel/src/lib2.rs Normal file
View File

@@ -0,0 +1,26 @@
#![no_std]
#![feature(abi_x86_interrupt)]
pub mod arch;
pub mod log;
pub mod qemu;
pub fn main() -> ! {
println!("we out here vibin");
for _ in 0..20000000 {}
qemu::exit();
}
pub fn exit() -> ! {
qemu::exit();
}
pub fn hlt_loop() -> ! {
arch::hlt_loop();
}
#[panic_handler]
pub fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{}", info);
exit()
}

View File

@@ -1,20 +1,19 @@
#![no_std]
#![no_main]
#![feature(abi_x86_interrupt)]
use kernel::{framebuffer, init, exit, println, hlt_loop};
bootloader_api::entry_point!(kernel_main);
pub mod arch;
pub mod log;
pub mod qemu;
fn kernel_main(boot_info: &'static mut bootloader_api::BootInfo) -> ! {
init();
if let Some(framebuffer) = boot_info.framebuffer.as_mut() {
framebuffer::draw_test(framebuffer);
}
pub fn main() -> ! {
println!("we out here vibin");
for _ in 0..20000000 {}
hlt_loop();
qemu::exit();
}
#[panic_handler]
fn panic(info: &core::panic::PanicInfo) -> ! {
pub fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{}", info);
exit()
qemu::exit()
}

View File

@@ -1,29 +1,8 @@
use core::fmt::Arguments;
use spin::Mutex;
use uart_16550::SerialPort;
use x86_64::instructions::{interrupts, port::Port};
pub static UART: Mutex<SerialPort> = Mutex::new(unsafe { SerialPort::new(0x3F8) });
pub fn exit() {
unsafe {
let mut port = Port::new(0xf4);
port.write(0x10u32);
}
}
#[doc(hidden)]
pub fn _print(args: Arguments<'_>) {
use core::fmt::Write;
interrupts::without_interrupts(|| {
UART.lock().write_fmt(args).unwrap();
})
}
pub use crate::arch::qemu::*;
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::qemu::_print(format_args!($($arg)*)));
($($arg:tt)*) => ($crate::arch::qemu::_print(format_args!($($arg)*)));
}
#[macro_export]