Compare commits
No commits in common. "modernize" and "master" have entirely different histories.
@ -1,10 +1,2 @@
|
|||||||
[unstable]
|
|
||||||
build-std = ["core", "compiler_builtins"]
|
|
||||||
build-std-features = ["compiler-builtins-mem"]
|
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
target = "x86_64_target.json"
|
target = "os.json"
|
||||||
|
|
||||||
# Facilitates running tests with qemu
|
|
||||||
[target.'cfg(target_os = "none")']
|
|
||||||
runner = "bootimage runner"
|
|
||||||
|
98
Cargo.lock
generated
98
Cargo.lock
generated
@ -1,98 +0,0 @@
|
|||||||
# 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,22 +2,17 @@
|
|||||||
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 = "2021"
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
#bootloader = "0.10.12"
|
bootloader = { version = "0.6.4", features = ["map_physical_memory"] }
|
||||||
bootloader = "0.9.8"
|
volatile = "0.2.6"
|
||||||
spin = "0.9.2"
|
spin = "0.5.0"
|
||||||
x86_64 = "0.14.8"
|
x86_64 = "0.7.2"
|
||||||
uart_16550 = "0.2.16"
|
pic8259_simple = "0.1.1"
|
||||||
|
pc-keyboard = "0.5.0"
|
||||||
|
|
||||||
[dependencies.lazy_static]
|
[dependencies.lazy_static]
|
||||||
version = "1.4.0"
|
version = "1.3.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
7
justfile
@ -1,7 +0,0 @@
|
|||||||
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
Executable file
5
run.sh
Executable file
@ -0,0 +1,5 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
cargo bootimage
|
||||||
|
qemu-system-x86_64 -drive format=raw,file=target/os/debug/bootimage-rustos.bin
|
||||||
|
|
32
src/gdt.rs
32
src/gdt.rs
@ -1,15 +1,18 @@
|
|||||||
use lazy_static::lazy_static;
|
|
||||||
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector};
|
|
||||||
use x86_64::structures::tss::TaskStateSegment;
|
|
||||||
use x86_64::VirtAddr;
|
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;
|
pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref TSS: TaskStateSegment = {
|
static ref TSS: TaskStateSegment = {
|
||||||
let mut tss = TaskStateSegment::new();
|
let mut tss = TaskStateSegment::new();
|
||||||
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
|
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
|
||||||
const STACK_SIZE: usize = 4096 * 5;
|
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];
|
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
||||||
let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
|
let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
|
||||||
let stack_end = stack_start + STACK_SIZE;
|
let stack_end = stack_start + STACK_SIZE;
|
||||||
@ -19,15 +22,26 @@ lazy_static! {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct Selectors {
|
||||||
|
code_selector: SegmentSelector,
|
||||||
|
data_selector: SegmentSelector,
|
||||||
|
}
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
static ref GDT: GlobalDescriptorTable = {
|
static ref GDT: (GlobalDescriptorTable, Selectors) = {
|
||||||
let mut gdt = GlobalDescriptorTable::new();
|
let mut gdt = GlobalDescriptorTable::new();
|
||||||
gdt.add_entry(Descriptor::kernel_code_segment());
|
let code_selector = gdt.add_entry(Descriptor::kernel_code_segment());
|
||||||
gdt.add_entry(Descriptor::tss_segment(&TSS));
|
let data_selector = gdt.add_entry(Descriptor::tss_segment(&TSS));
|
||||||
gdt
|
(gdt, Selectors { code_selector, data_selector })
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init() {
|
pub fn init() {
|
||||||
GDT.load();
|
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,26 +1,98 @@
|
|||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
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! {
|
lazy_static! {
|
||||||
static ref IDT: InterruptDescriptorTable = {
|
static ref IDT: InterruptDescriptorTable = {
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
let mut idt = InterruptDescriptorTable::new();
|
||||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
||||||
idt.double_fault.set_handler_fn(double_fault_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
|
idt
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn init_idt() {
|
pub fn init_idt() {
|
||||||
IDT.load();
|
IDT.load();
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
pub fn initalize_pics() {
|
||||||
println!("EXCEPTION: Breakpoint\n{:#?}", stack_frame);
|
unsafe {
|
||||||
|
PICS.lock().initialize();
|
||||||
|
}
|
||||||
|
x86_64::instructions::interrupts::enable();
|
||||||
}
|
}
|
||||||
|
|
||||||
// The error code of the double-fault handler is always 0
|
extern "x86-interrupt" fn breakpoint_handler(stack_frame: &mut InterruptStackFrame) {
|
||||||
extern "x86-interrupt" fn double_fault_handler(
|
|
||||||
stack_frame: InterruptStackFrame,
|
println!("EXCEPTION - BREAKPOINT\n{:#?}", stack_frame);
|
||||||
_error_code: u64,
|
}
|
||||||
) -> ! {
|
|
||||||
panic!("Exception: Double fault\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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
54
src/main.rs
54
src/main.rs
@ -1,51 +1,41 @@
|
|||||||
#![feature(abi_x86_interrupt)]
|
#![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;
|
||||||
#[macro_use]
|
|
||||||
mod serial;
|
|
||||||
mod gdt;
|
|
||||||
mod interrupts;
|
mod interrupts;
|
||||||
mod test_utils;
|
mod gdt;
|
||||||
|
|
||||||
|
use core::panic::PanicInfo;
|
||||||
|
|
||||||
#[cfg(not(test))]
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn panic(info: &core::panic::PanicInfo) -> ! {
|
fn panic(info: &PanicInfo) -> ! {
|
||||||
println!("{info}");
|
println!("{}", info);
|
||||||
loop {}
|
halt_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() -> ! {
|
||||||
#[cfg(test)]
|
|
||||||
test_main();
|
|
||||||
|
|
||||||
println!("Gamarjoba, munde!");
|
|
||||||
init();
|
init();
|
||||||
|
|
||||||
// Deliberate breakpoint exception
|
|
||||||
x86_64::instructions::interrupts::int3();
|
|
||||||
|
|
||||||
println!("We're here now");
|
|
||||||
loop {}
|
for i in 1..10 {
|
||||||
|
println!("Gamarjoba, munde: {}", i);
|
||||||
|
}
|
||||||
|
halt_loop();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test_case]
|
pub fn halt_loop() -> ! {
|
||||||
fn basic_test() {
|
loop {
|
||||||
assert_eq!(5, 5);
|
x86_64::instructions::hlt();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test_case]
|
|
||||||
fn basic_test2() {
|
|
||||||
assert_eq!(4, 4);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn init() {
|
|
||||||
gdt::init();
|
|
||||||
interrupts::init_idt();
|
|
||||||
}
|
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
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)*));
|
|
||||||
}
|
|
@ -1,54 +0,0 @@
|
|||||||
#![cfg(test)]
|
|
||||||
|
|
||||||
use core::panic::PanicInfo;
|
|
||||||
|
|
||||||
#[panic_handler]
|
|
||||||
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,6 +1,8 @@
|
|||||||
|
use volatile::Volatile;
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use lazy_static::lazy_static;
|
|
||||||
use spin::Mutex;
|
use spin::Mutex;
|
||||||
|
use lazy_static::lazy_static;
|
||||||
|
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! print {
|
macro_rules! print {
|
||||||
@ -16,13 +18,10 @@ 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)]
|
||||||
@ -49,10 +48,10 @@ pub enum Color {
|
|||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct ColorCode(u8);
|
struct ColorCode(u8);
|
||||||
|
|
||||||
impl ColorCode {
|
impl ColorCode {
|
||||||
pub fn new(foreground: Color, background: Color) -> ColorCode {
|
fn new(foreground: Color, background: Color) -> ColorCode {
|
||||||
ColorCode((background as u8) << 4 | (foreground as u8))
|
ColorCode((background as u8) << 4 | (foreground as u8))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -68,8 +67,8 @@ const BUFFER_WIDTH: usize = 80;
|
|||||||
const BUFFER_HEIGHT: usize = 25;
|
const BUFFER_HEIGHT: usize = 25;
|
||||||
|
|
||||||
#[repr(transparent)]
|
#[repr(transparent)]
|
||||||
pub struct Buffer {
|
struct Buffer {
|
||||||
chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Writer {
|
pub struct Writer {
|
||||||
@ -79,18 +78,11 @@ pub struct Writer {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Writer {
|
impl Writer {
|
||||||
pub fn new(color_code: ColorCode) -> Self {
|
|
||||||
Writer {
|
|
||||||
column_position: 0,
|
|
||||||
color_code,
|
|
||||||
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pub fn write_string(&mut self, s: &str) {
|
pub fn write_string(&mut self, s: &str) {
|
||||||
for byte in s.bytes() {
|
for byte in s.bytes() {
|
||||||
match byte {
|
match byte {
|
||||||
0x20..=0x7e | b'\n' => self.write_byte(byte),
|
0x20..=0x7e | b'\n' => self.write_byte(byte),
|
||||||
_ => self.write_byte(0xfe), // 0xfe is the character ■
|
_ => self.write_byte(0xfe), // a ■
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -105,17 +97,9 @@ impl Writer {
|
|||||||
let row = BUFFER_HEIGHT - 1;
|
let row = BUFFER_HEIGHT - 1;
|
||||||
let col = self.column_position;
|
let col = self.column_position;
|
||||||
let color_code = self.color_code;
|
let color_code = self.color_code;
|
||||||
|
self.buffer.chars[row][col].write(ScreenChar {
|
||||||
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
|
ascii_char: byte, color_code,
|
||||||
let screenchar = ScreenChar {
|
});
|
||||||
ascii_char: byte,
|
|
||||||
color_code,
|
|
||||||
};
|
|
||||||
|
|
||||||
unsafe {
|
|
||||||
core::ptr::write_volatile(ptr, screenchar);
|
|
||||||
}
|
|
||||||
|
|
||||||
self.column_position += 1;
|
self.column_position += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -124,14 +108,8 @@ impl Writer {
|
|||||||
fn new_line(&mut self) {
|
fn new_line(&mut self) {
|
||||||
for row in 1..BUFFER_HEIGHT {
|
for row in 1..BUFFER_HEIGHT {
|
||||||
for col in 0..BUFFER_WIDTH {
|
for col in 0..BUFFER_WIDTH {
|
||||||
let character = unsafe {
|
let character = self.buffer.chars[row][col].read();
|
||||||
core::ptr::read_volatile(&self.buffer.chars[row][col] as *const ScreenChar)
|
self.buffer.chars[row - 1][col].write(character);
|
||||||
};
|
|
||||||
|
|
||||||
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.clear_row(BUFFER_HEIGHT - 1);
|
||||||
@ -144,10 +122,7 @@ impl Writer {
|
|||||||
color_code: self.color_code,
|
color_code: self.color_code,
|
||||||
};
|
};
|
||||||
for col in 0..BUFFER_WIDTH {
|
for col in 0..BUFFER_WIDTH {
|
||||||
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
|
self.buffer.chars[row][col].write(blank);
|
||||||
unsafe {
|
|
||||||
core::ptr::write_volatile(ptr, blank);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -159,31 +134,11 @@ impl fmt::Write for Writer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
pub static ref WRITER: Mutex<Writer> =
|
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
|
||||||
Mutex::new(Writer::new(ColorCode::new(Color::Yellow, Color::Black)));
|
column_position: 0, color_code: 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user