Compare commits
11 Commits
master
...
integratio
Author | SHA1 | Date | |
---|---|---|---|
|
a15d7d820f | ||
|
7cac67b0fc | ||
|
cd685e79c7 | ||
|
ad88a0784b | ||
|
56f003e8cc | ||
|
aa03bdad88 | ||
|
a5fb50ffb5 | ||
|
4d7aa79434 | ||
|
1cfd133cb2 | ||
|
5c0ea91e65 | ||
|
2889490657 |
@ -1,2 +1,10 @@
|
|||||||
|
[unstable]
|
||||||
|
build-std = ["core", "compiler_builtins"]
|
||||||
|
build-std-features = ["compiler-builtins-mem"]
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
target = "os.json"
|
target = "x86_64_target.json"
|
||||||
|
|
||||||
|
# Facilitates running tests with qemu
|
||||||
|
[target.'cfg(target_os = "none")']
|
||||||
|
runner = "bootimage runner"
|
||||||
|
98
Cargo.lock
generated
Normal file
98
Cargo.lock
generated
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
# This file is automatically @generated by Cargo.
|
||||||
|
# It is not intended for manual editing.
|
||||||
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bit_field"
|
||||||
|
version = "0.10.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bitflags"
|
||||||
|
version = "1.3.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "bootloader"
|
||||||
|
version = "0.9.21"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a62c8f6168cd106687ee36a2b71a46c4144d73399f72814104d33094b8092fd2"
|
||||||
|
|
||||||
|
[[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.6"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "88943dd7ef4a2e5a4bfa2753aaab3013e34ce2533d1996fb18ef591e315e2b3b"
|
||||||
|
dependencies = [
|
||||||
|
"scopeguard",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rustos"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"bootloader",
|
||||||
|
"lazy_static",
|
||||||
|
"spin 0.9.2",
|
||||||
|
"uart_16550",
|
||||||
|
"x86_64",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "scopeguard"
|
||||||
|
version = "1.1.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
|
||||||
|
|
||||||
|
[[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.2"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "511254be0c5bcf062b019a6c89c01a664aa359ded62f78aa72c6fc137c0590e5"
|
||||||
|
dependencies = [
|
||||||
|
"lock_api",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "uart_16550"
|
||||||
|
version = "0.2.16"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "af81448a9a53c7b0f66198381f80912fd18f2c8965f9da4319e6f92e740bca5b"
|
||||||
|
dependencies = [
|
||||||
|
"bitflags",
|
||||||
|
"x86_64",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "volatile"
|
||||||
|
version = "0.4.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "e4c2dbd44eb8b53973357e6e207e370f0c1059990df850aca1eca8947cf464f0"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "x86_64"
|
||||||
|
version = "0.14.8"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "958ab3202b01bc43ba2eb832102c4a487ed93151667a2289062e5f2b00058be2"
|
||||||
|
dependencies = [
|
||||||
|
"bit_field",
|
||||||
|
"bitflags",
|
||||||
|
"volatile",
|
||||||
|
]
|
21
Cargo.toml
21
Cargo.toml
@ -2,17 +2,22 @@
|
|||||||
name = "rustos"
|
name = "rustos"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["greg <greg.shuflin@protonmail.com>"]
|
authors = ["greg <greg.shuflin@protonmail.com>"]
|
||||||
edition = "2018"
|
edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
bootloader = { version = "0.6.4", features = ["map_physical_memory"] }
|
#bootloader = "0.10.12"
|
||||||
volatile = "0.2.6"
|
bootloader = "0.9.8"
|
||||||
spin = "0.5.0"
|
spin = "0.9.2"
|
||||||
x86_64 = "0.7.2"
|
x86_64 = "0.14.8"
|
||||||
pic8259_simple = "0.1.1"
|
uart_16550 = "0.2.16"
|
||||||
pc-keyboard = "0.5.0"
|
|
||||||
|
|
||||||
[dependencies.lazy_static]
|
[dependencies.lazy_static]
|
||||||
version = "1.3.0"
|
version = "1.4.0"
|
||||||
features = ["spin_no_std"]
|
features = ["spin_no_std"]
|
||||||
|
|
||||||
|
|
||||||
|
[package.metadata.bootimage]
|
||||||
|
test-args = ["-device", "isa-debug-exit,iobase=0xf4,iosize=0x04", "-serial", "stdio",
|
||||||
|
"-display", "none"]
|
||||||
|
test-success-exit-code = 33 # (0x10 << 1) | 1
|
||||||
|
test-timeout = 10 # timeout in seconds
|
||||||
|
7
justfile
Normal file
7
justfile
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
default:
|
||||||
|
just --list
|
||||||
|
|
||||||
|
run_os:
|
||||||
|
cargo bootimage
|
||||||
|
qemu-system-x86_64 -drive format=raw,file=target/x86_64_target/debug/bootimage-rustos.bin
|
||||||
|
|
5
run.sh
5
run.sh
@ -1,5 +0,0 @@
|
|||||||
#!/bin/sh
|
|
||||||
|
|
||||||
cargo bootimage
|
|
||||||
qemu-system-x86_64 -drive format=raw,file=target/os/debug/bootimage-rustos.bin
|
|
||||||
|
|
47
src/gdt.rs
47
src/gdt.rs
@ -1,47 +0,0 @@
|
|||||||
use x86_64::VirtAddr;
|
|
||||||
use x86_64::structures::tss::TaskStateSegment;
|
|
||||||
use x86_64::structures::gdt::{GlobalDescriptorTable, Descriptor, SegmentSelector};
|
|
||||||
use lazy_static::lazy_static;
|
|
||||||
|
|
||||||
//double fault interrupt stack table
|
|
||||||
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref TSS: TaskStateSegment = {
|
|
||||||
let mut tss = TaskStateSegment::new();
|
|
||||||
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
|
|
||||||
const STACK_SIZE: usize = 4096; //4k stack
|
|
||||||
|
|
||||||
//this static mut is temporarily the stack until proper allocation can be implemented
|
|
||||||
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
|
||||||
let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
|
|
||||||
let stack_end = stack_start + STACK_SIZE;
|
|
||||||
stack_end
|
|
||||||
};
|
|
||||||
tss
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
struct Selectors {
|
|
||||||
code_selector: SegmentSelector,
|
|
||||||
data_selector: SegmentSelector,
|
|
||||||
}
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref GDT: (GlobalDescriptorTable, Selectors) = {
|
|
||||||
let mut gdt = GlobalDescriptorTable::new();
|
|
||||||
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
|
|
||||||
let data_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
|
|
||||||
(gdt, Selectors { code_selector, data_selector })
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init() {
|
|
||||||
use x86_64::instructions::segmentation::set_cs;
|
|
||||||
use x86_64::instructions::tables::load_tss;
|
|
||||||
GDT.0.load();
|
|
||||||
unsafe {
|
|
||||||
set_cs(GDT.1.code_selector);
|
|
||||||
load_tss(GDT.1.data_selector);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,98 +0,0 @@
|
|||||||
use lazy_static::lazy_static;
|
|
||||||
use x86_64::structures::idt::{InterruptStackFrame, InterruptDescriptorTable};
|
|
||||||
use crate::println;
|
|
||||||
use pic8259_simple::ChainedPics;
|
|
||||||
use spin;
|
|
||||||
use crate::gdt;
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug)]
|
|
||||||
#[repr(u8)]
|
|
||||||
pub enum InterruptIndex {
|
|
||||||
Timer = PIC_1_OFFSET,
|
|
||||||
Keyboard,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InterruptIndex {
|
|
||||||
fn as_u8(self) -> u8 {
|
|
||||||
self as u8
|
|
||||||
}
|
|
||||||
fn as_usize(self) -> usize {
|
|
||||||
usize::from(self as u8)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const PIC_1_OFFSET: u8 = 32;
|
|
||||||
const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
|
|
||||||
static PICS: spin::Mutex<ChainedPics> =
|
|
||||||
spin::Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref IDT: InterruptDescriptorTable = {
|
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
|
||||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
|
||||||
unsafe {
|
|
||||||
idt.double_fault.set_handler_fn(double_fault_handler)
|
|
||||||
.set_stack_index(gdt::DOUBLE_FAULT_IST_INDEX);
|
|
||||||
}
|
|
||||||
idt[InterruptIndex::Timer.as_usize()]
|
|
||||||
.set_handler_fn(timer_interrupt_handler);
|
|
||||||
idt[InterruptIndex::Keyboard.as_usize()]
|
|
||||||
.set_handler_fn(keyboard_interrupt_handler);
|
|
||||||
idt
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init_idt() {
|
|
||||||
IDT.load();
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn initalize_pics() {
|
|
||||||
unsafe {
|
|
||||||
PICS.lock().initialize();
|
|
||||||
}
|
|
||||||
x86_64::instructions::interrupts::enable();
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) {
|
|
||||||
|
|
||||||
println!("EXCEPTION - BREAKPOINT\n{:#?}", stack_frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn double_fault_handler(stack_frame: &mut InterruptStackFrame, code: u64) {
|
|
||||||
panic!("EXCEPTION - DOUBLE FAULT (code {})\n{:#?}", code, stack_frame);
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
|
||||||
print!(".");
|
|
||||||
unsafe {
|
|
||||||
PICS.lock().notify_end_of_interrupt(InterruptIndex::Timer.as_u8())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: &mut InterruptStackFrame) {
|
|
||||||
use x86_64::instructions::port::Port;
|
|
||||||
use pc_keyboard::{Keyboard, ScancodeSet1, DecodedKey, HandleControl, layouts};
|
|
||||||
use spin::Mutex;
|
|
||||||
|
|
||||||
lazy_static! {
|
|
||||||
static ref KEYBOARD: Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>> =
|
|
||||||
Mutex::new(Keyboard::new(layouts::Us104Key, ScancodeSet1, HandleControl::Ignore));
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut keyboard = KEYBOARD.lock();
|
|
||||||
let mut port = Port::new(0x60);
|
|
||||||
let scancode: u8 = unsafe { port.read() };
|
|
||||||
|
|
||||||
if let Ok(Some(key_event)) = keyboard.add_byte(scancode) {
|
|
||||||
if let Some(key) = keyboard.process_keyevent(key_event) {
|
|
||||||
match key {
|
|
||||||
DecodedKey::Unicode(character) => print!("{}", character),
|
|
||||||
DecodedKey::RawKey(key) => print!("{:?}", key),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
unsafe {
|
|
||||||
PICS.lock().notify_end_of_interrupt(InterruptIndex::Keyboard.as_u8())
|
|
||||||
}
|
|
||||||
}
|
|
16
src/lib.rs
Normal file
16
src/lib.rs
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![cfg_attr(test, no_main)]
|
||||||
|
#![feature(custom_test_frameworks)]
|
||||||
|
#![test_runner(crate::test_utils::test_runner)]
|
||||||
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
pub mod test_utils;
|
||||||
|
pub mod serial;
|
||||||
|
pub mod vga_buffer;
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn _start() {
|
||||||
|
test_main();
|
||||||
|
loop {}
|
||||||
|
}
|
47
src/main.rs
47
src/main.rs
@ -1,41 +1,38 @@
|
|||||||
#![feature(abi_x86_interrupt, asm)]
|
// #![feature(abi_x86_interrupt, asm)]
|
||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
#![feature(custom_test_frameworks)]
|
||||||
|
#![test_runner(crate::test_utils::test_runner)]
|
||||||
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod vga_buffer;
|
mod vga_buffer;
|
||||||
mod interrupts;
|
#[macro_use]
|
||||||
mod gdt;
|
mod serial;
|
||||||
|
mod test_utils;
|
||||||
use core::panic::PanicInfo;
|
|
||||||
|
|
||||||
|
#[cfg(not(test))]
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(info: &PanicInfo) -> ! {
|
fn panic(info: &core::panic::PanicInfo) -> ! {
|
||||||
println!("{}", info);
|
println!("{info}");
|
||||||
halt_loop();
|
loop {}
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init() {
|
|
||||||
interrupts::initalize_pics();
|
|
||||||
gdt::init();
|
|
||||||
interrupts::init_idt();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
pub extern "C" fn _start() -> ! {
|
pub extern "C" fn _start() -> ! {
|
||||||
init();
|
// #[cfg(test)]
|
||||||
|
// test_main();
|
||||||
|
|
||||||
|
println!("Gamarjoba, munde!");
|
||||||
|
loop {}
|
||||||
for i in 1..10 {
|
|
||||||
println!("Gamarjoba, munde: {}", i);
|
|
||||||
}
|
|
||||||
halt_loop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn halt_loop() -> ! {
|
#[test_case]
|
||||||
loop {
|
fn basic_test() {
|
||||||
x86_64::instructions::hlt();
|
assert_eq!(5, 5);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn basic_test2() {
|
||||||
|
assert_eq!(4, 4);
|
||||||
|
}
|
||||||
|
32
src/serial.rs
Normal file
32
src/serial.rs
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
use lazy_static::lazy_static;
|
||||||
|
use spin::Mutex;
|
||||||
|
use uart_16550::SerialPort;
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref SERIAL1: Mutex<SerialPort> = {
|
||||||
|
let mut serial_port = unsafe { SerialPort::new(0x3f8) }; // 0x3f8 is standard address of 1st serial port
|
||||||
|
serial_port.init();
|
||||||
|
Mutex::new(serial_port)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[doc(hidden)]
|
||||||
|
pub fn _print(args: ::core::fmt::Arguments) {
|
||||||
|
use core::fmt::Write;
|
||||||
|
SERIAL1.lock().write_fmt(args).expect("Serial print failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! serial_print {
|
||||||
|
($($arg:tt)*) => {
|
||||||
|
$crate::serial::_print(format_args!($($arg)*));
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[macro_export]
|
||||||
|
macro_rules! serial_println {
|
||||||
|
() => ($crate::serial_print!("\n"));
|
||||||
|
($fmt:expr) => ($crate::serial_print!(concat!($fmt, "\n")));
|
||||||
|
($fmt:expr, $($arg:tt)*) => ($crate::serial_print!(
|
||||||
|
concat!($fmt, "\n"), $($arg)*));
|
||||||
|
}
|
54
src/test_utils.rs
Normal file
54
src/test_utils.rs
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#![cfg(test)]
|
||||||
|
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
#[panic_handler]
|
||||||
|
pub fn panic(info: &PanicInfo) -> ! {
|
||||||
|
serial_println!("[failed]\n");
|
||||||
|
serial_println!("Error: {}", info);
|
||||||
|
exit_qemu(QemuExitCode::Failed);
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// This function is hooked up to the #![test_runner] attribute made possible by the
|
||||||
|
/// https://doc.rust-lang.org/unstable-book/language-features/custom-test-frameworks.html unstable
|
||||||
|
/// feature. cf. https://os.phil-opp.com/testing/
|
||||||
|
pub fn test_runner(tests: &[&dyn TestFunction]) {
|
||||||
|
serial_println!("Running {} test(s)", tests.len());
|
||||||
|
for test in tests {
|
||||||
|
test.run();
|
||||||
|
}
|
||||||
|
|
||||||
|
exit_qemu(QemuExitCode::Success);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait TestFunction {
|
||||||
|
fn run(&self) -> ();
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> TestFunction for T
|
||||||
|
where
|
||||||
|
T: Fn(),
|
||||||
|
{
|
||||||
|
fn run(&self) {
|
||||||
|
serial_print!("{}...\t", core::any::type_name::<T>());
|
||||||
|
self();
|
||||||
|
serial_println!("[ok]");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Exits qemu by writing to a special qemu emulated port.
|
||||||
|
pub fn exit_qemu(exit_code: QemuExitCode) {
|
||||||
|
use x86_64::instructions::port::Port;
|
||||||
|
unsafe {
|
||||||
|
let mut port = Port::new(0xf4); // isa-debug-exit port, see Cargo.toml
|
||||||
|
port.write(exit_code as u32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
|
#[repr(u32)]
|
||||||
|
pub enum QemuExitCode {
|
||||||
|
Success = 0x10,
|
||||||
|
Failed = 0x11,
|
||||||
|
}
|
@ -1,8 +1,6 @@
|
|||||||
use volatile::Volatile;
|
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use spin::Mutex;
|
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
use spin::Mutex;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! print {
|
macro_rules! print {
|
||||||
@ -18,10 +16,13 @@ macro_rules! println {
|
|||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub fn _print(args: fmt::Arguments) {
|
pub fn _print(args: fmt::Arguments) {
|
||||||
use core::fmt::Write;
|
use core::fmt::Write;
|
||||||
|
WRITER.lock().write_fmt(args).unwrap();
|
||||||
|
/*
|
||||||
use x86_64::instructions::interrupts;
|
use x86_64::instructions::interrupts;
|
||||||
interrupts::without_interrupts(|| {
|
interrupts::without_interrupts(|| {
|
||||||
WRITER.lock().write_fmt(args).unwrap();
|
WRITER.lock().write_fmt(args).unwrap();
|
||||||
});
|
});
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(dead_code)]
|
#[allow(dead_code)]
|
||||||
@ -48,97 +49,141 @@ pub enum Color {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
struct ColorCode(u8);
|
pub struct ColorCode(u8);
|
||||||
|
|
||||||
impl ColorCode {
|
impl ColorCode {
|
||||||
fn new(foreground: Color, background: Color) -> ColorCode {
|
pub fn new(foreground: Color, background: Color) -> ColorCode {
|
||||||
ColorCode((background as u8) << 4 | (foreground as u8))
|
ColorCode((background as u8) << 4 | (foreground as u8))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
struct ScreenChar {
|
struct ScreenChar {
|
||||||
ascii_char: u8,
|
ascii_char: u8,
|
||||||
color_code: ColorCode,
|
color_code: ColorCode,
|
||||||
}
|
}
|
||||||
|
|
||||||
const BUFFER_WIDTH: usize = 80;
|
const BUFFER_WIDTH: usize = 80;
|
||||||
const BUFFER_HEIGHT: usize = 25;
|
const BUFFER_HEIGHT: usize = 25;
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
struct Buffer {
|
pub struct Buffer {
|
||||||
chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Writer {
|
pub struct Writer {
|
||||||
column_position: usize,
|
column_position: usize,
|
||||||
color_code: ColorCode,
|
color_code: ColorCode,
|
||||||
buffer: &'static mut Buffer,
|
buffer: &'static mut Buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Writer {
|
impl Writer {
|
||||||
pub fn write_string(&mut self, s: &str) {
|
pub fn new(color_code: ColorCode) -> Self {
|
||||||
for byte in s.bytes() {
|
Writer {
|
||||||
match byte {
|
column_position: 0,
|
||||||
0x20..=0x7e | b'\n' => self.write_byte(byte),
|
color_code,
|
||||||
_ => self.write_byte(0xfe), // a ■
|
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
pub fn write_string(&mut self, s: &str) {
|
||||||
|
for byte in s.bytes() {
|
||||||
pub fn write_byte(&mut self, byte: u8) {
|
match byte {
|
||||||
match byte {
|
0x20..=0x7e | b'\n' => self.write_byte(byte),
|
||||||
b'\n' => self.new_line(),
|
_ => self.write_byte(0xfe), // 0xfe is the character ■
|
||||||
byte => {
|
}
|
||||||
if self.column_position >= BUFFER_WIDTH {
|
|
||||||
self.new_line();
|
|
||||||
}
|
}
|
||||||
let row = BUFFER_HEIGHT - 1;
|
|
||||||
let col = self.column_position;
|
|
||||||
let color_code = self.color_code;
|
|
||||||
self.buffer.chars[row][col].write(ScreenChar {
|
|
||||||
ascii_char: byte, color_code,
|
|
||||||
});
|
|
||||||
self.column_position += 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fn new_line(&mut self) {
|
pub fn write_byte(&mut self, byte: u8) {
|
||||||
for row in 1..BUFFER_HEIGHT {
|
match byte {
|
||||||
for col in 0..BUFFER_WIDTH {
|
b'\n' => self.new_line(),
|
||||||
let character = self.buffer.chars[row][col].read();
|
byte => {
|
||||||
self.buffer.chars[row - 1][col].write(character);
|
if self.column_position >= BUFFER_WIDTH {
|
||||||
}
|
self.new_line();
|
||||||
}
|
}
|
||||||
self.clear_row(BUFFER_HEIGHT - 1);
|
let row = BUFFER_HEIGHT - 1;
|
||||||
self.column_position = 0;
|
let col = self.column_position;
|
||||||
}
|
let color_code = self.color_code;
|
||||||
|
|
||||||
fn clear_row(&mut self, row: usize) {
|
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
|
||||||
let blank = ScreenChar {
|
let screenchar = ScreenChar {
|
||||||
ascii_char: b' ',
|
ascii_char: byte,
|
||||||
color_code: self.color_code,
|
color_code,
|
||||||
};
|
};
|
||||||
for col in 0..BUFFER_WIDTH {
|
|
||||||
self.buffer.chars[row][col].write(blank);
|
unsafe {
|
||||||
|
core::ptr::write_volatile(ptr, screenchar);
|
||||||
|
}
|
||||||
|
|
||||||
|
self.column_position += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_line(&mut self) {
|
||||||
|
for row in 1..BUFFER_HEIGHT {
|
||||||
|
for col in 0..BUFFER_WIDTH {
|
||||||
|
let character = unsafe {
|
||||||
|
core::ptr::read_volatile(&self.buffer.chars[row][col] as *const ScreenChar)
|
||||||
|
};
|
||||||
|
|
||||||
|
let ptr = &mut self.buffer.chars[row - 1][col] as *mut ScreenChar;
|
||||||
|
unsafe {
|
||||||
|
core::ptr::write_volatile(ptr, character);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
self.clear_row(BUFFER_HEIGHT - 1);
|
||||||
|
self.column_position = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn clear_row(&mut self, row: usize) {
|
||||||
|
let blank = ScreenChar {
|
||||||
|
ascii_char: b' ',
|
||||||
|
color_code: self.color_code,
|
||||||
|
};
|
||||||
|
for col in 0..BUFFER_WIDTH {
|
||||||
|
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
|
||||||
|
unsafe {
|
||||||
|
core::ptr::write_volatile(ptr, blank);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Write for Writer {
|
impl fmt::Write for Writer {
|
||||||
fn write_str(&mut self, s: &str) -> fmt::Result {
|
fn write_str(&mut self, s: &str) -> fmt::Result {
|
||||||
self.write_string(s);
|
self.write_string(s);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
|
pub static ref WRITER: Mutex<Writer> =
|
||||||
column_position: 0, color_code: ColorCode::new(Color::Yellow, Color::Black),
|
Mutex::new(Writer::new(ColorCode::new(Color::Yellow, Color::Black)));
|
||||||
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn test_println_simple() {
|
||||||
|
println!("Does this work?");
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn test_println_many() {
|
||||||
|
for _ in 0..200 {
|
||||||
|
println!("Test many output");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test_case]
|
||||||
|
fn test_println_output() {
|
||||||
|
let s = "Test string that fits on a single line";
|
||||||
|
println!("{}", s);
|
||||||
|
for (i, c) in s.chars().enumerate() {
|
||||||
|
let screen_char_ptr =
|
||||||
|
&WRITER.lock().buffer.chars[BUFFER_HEIGHT - 2][i] as *const ScreenChar;
|
||||||
|
let screen_char = unsafe { core::ptr::read_volatile(screen_char_ptr) };
|
||||||
|
assert_eq!(char::from(screen_char.ascii_char), c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
23
tests/basic_boot.rs
Normal file
23
tests/basic_boot.rs
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
#![no_std]
|
||||||
|
#![no_main]
|
||||||
|
#![feature(custom_test_frameworks)]
|
||||||
|
#![test_runner(crate::test_utils::test_runner)]
|
||||||
|
#![reexport_test_harness_main = "test_main"]
|
||||||
|
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
|
#[no_mangle] // don't mangle the name of this function
|
||||||
|
pub extern "C" fn _start() -> ! {
|
||||||
|
test_main();
|
||||||
|
|
||||||
|
loop {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_runner(tests: &[&dyn Fn()]) {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
|
||||||
|
#[panic_handler]
|
||||||
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
|
loop {}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user