Compare commits

...

11 Commits

Author SHA1 Message Date
Greg Shuflin
a15d7d820f WIP integration test 2022-04-03 21:16:33 -07:00
Greg Shuflin
7cac67b0fc Add more tests for println 2022-04-03 21:03:45 -07:00
Greg Shuflin
cd685e79c7 Add some doc clarifications for the test framework 2022-04-03 19:18:53 -07:00
Greg Shuflin
ad88a0784b Move test utils into separate module 2022-02-25 19:39:09 -08:00
Greg Shuflin
56f003e8cc Simple test 2022-02-20 12:10:34 -08:00
Greg Shuflin
aa03bdad88 More test niceties 2022-02-20 02:15:04 -08:00
Greg Shuflin
a5fb50ffb5 Tests and test output over serial working 2022-02-20 02:01:45 -08:00
Greg Shuflin
4d7aa79434 Have panic handler print message 2022-02-19 01:01:32 -08:00
Greg Shuflin
1cfd133cb2 Run cargo fmt 2022-02-19 01:00:56 -08:00
Greg Shuflin
5c0ea91e65 Add back code for writing to screen 2022-02-19 01:00:27 -08:00
Greg Shuflin
2889490657 Add modern (2022) version of inital code 2022-02-13 19:56:35 -08:00
14 changed files with 384 additions and 249 deletions

View File

@ -1,2 +1,10 @@
[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]
[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
View 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",
]

View File

@ -2,17 +2,22 @@
name = "rustos"
version = "0.1.0"
authors = ["greg <greg.shuflin@protonmail.com>"]
edition = "2018"
edition = "2021"
[dependencies]
bootloader = { version = "0.6.4", features = ["map_physical_memory"] }
volatile = "0.2.6"
spin = "0.5.0"
x86_64 = "0.7.2"
pic8259_simple = "0.1.1"
pc-keyboard = "0.5.0"
#bootloader = "0.10.12"
bootloader = "0.9.8"
spin = "0.9.2"
x86_64 = "0.14.8"
uart_16550 = "0.2.16"
[dependencies.lazy_static]
version = "1.3.0"
version = "1.4.0"
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
View 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
View File

@ -1,5 +0,0 @@
#!/bin/sh
cargo bootimage
qemu-system-x86_64 -drive format=raw,file=target/os/debug/bootimage-rustos.bin

View File

@ -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);
}
}

View File

@ -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
View 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 {}
}

View File

@ -1,41 +1,38 @@
#![feature(abi_x86_interrupt, asm)]
// #![feature(abi_x86_interrupt, asm)]
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_utils::test_runner)]
#![reexport_test_harness_main = "test_main"]
#[macro_use]
mod vga_buffer;
mod interrupts;
mod gdt;
use core::panic::PanicInfo;
#[macro_use]
mod serial;
mod test_utils;
#[cfg(not(test))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
halt_loop();
}
pub fn init() {
interrupts::initalize_pics();
gdt::init();
interrupts::init_idt();
fn panic(info: &core::panic::PanicInfo) -> ! {
println!("{info}");
loop {}
}
#[no_mangle]
pub extern "C" fn _start() -> ! {
init();
// #[cfg(test)]
// test_main();
for i in 1..10 {
println!("Gamarjoba, munde: {}", i);
}
halt_loop();
println!("Gamarjoba, munde!");
loop {}
}
pub fn halt_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
#[test_case]
fn basic_test() {
assert_eq!(5, 5);
}
#[test_case]
fn basic_test2() {
assert_eq!(4, 4);
}

32
src/serial.rs Normal file
View 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
View 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,
}

View File

@ -1,8 +1,6 @@
use volatile::Volatile;
use core::fmt;
use spin::Mutex;
use lazy_static::lazy_static;
use spin::Mutex;
#[macro_export]
macro_rules! print {
@ -18,10 +16,13 @@ macro_rules! println {
#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use core::fmt::Write;
WRITER.lock().write_fmt(args).unwrap();
/*
use x86_64::instructions::interrupts;
interrupts::without_interrupts(|| {
WRITER.lock().write_fmt(args).unwrap();
});
*/
}
#[allow(dead_code)]
@ -48,97 +49,141 @@ pub enum Color {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
struct ColorCode(u8);
pub struct ColorCode(u8);
impl ColorCode {
fn new(foreground: Color, background: Color) -> ColorCode {
ColorCode((background as u8) << 4 | (foreground as u8))
}
pub fn new(foreground: Color, background: Color) -> ColorCode {
ColorCode((background as u8) << 4 | (foreground as u8))
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
struct ScreenChar {
ascii_char: u8,
color_code: ColorCode,
ascii_char: u8,
color_code: ColorCode,
}
const BUFFER_WIDTH: usize = 80;
const BUFFER_HEIGHT: usize = 25;
#[repr(transparent)]
struct Buffer {
chars: [[Volatile<ScreenChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
pub struct Buffer {
chars: [[ScreenChar; BUFFER_WIDTH]; BUFFER_HEIGHT],
}
pub struct Writer {
column_position: usize,
color_code: ColorCode,
buffer: &'static mut Buffer,
column_position: usize,
color_code: ColorCode,
buffer: &'static mut Buffer,
}
impl Writer {
pub fn write_string(&mut self, s: &str) {
for byte in s.bytes() {
match byte {
0x20..=0x7e | b'\n' => self.write_byte(byte),
_ => self.write_byte(0xfe), // a ■
}
}
}
pub fn write_byte(&mut self, byte: u8) {
match byte {
b'\n' => self.new_line(),
byte => {
if self.column_position >= BUFFER_WIDTH {
self.new_line();
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) {
for byte in s.bytes() {
match byte {
0x20..=0x7e | b'\n' => self.write_byte(byte),
_ => self.write_byte(0xfe), // 0xfe is the character ■
}
}
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) {
for row in 1..BUFFER_HEIGHT {
for col in 0..BUFFER_WIDTH {
let character = self.buffer.chars[row][col].read();
self.buffer.chars[row - 1][col].write(character);
}
}
self.clear_row(BUFFER_HEIGHT - 1);
self.column_position = 0;
}
pub fn write_byte(&mut self, byte: u8) {
match byte {
b'\n' => self.new_line(),
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;
fn clear_row(&mut self, row: usize) {
let blank = ScreenChar {
ascii_char: b' ',
color_code: self.color_code,
};
for col in 0..BUFFER_WIDTH {
self.buffer.chars[row][col].write(blank);
let ptr = &mut self.buffer.chars[row][col] as *mut ScreenChar;
let screenchar = ScreenChar {
ascii_char: byte,
color_code,
};
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 {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_string(s);
Ok(())
}
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_string(s);
Ok(())
}
}
lazy_static! {
pub static ref WRITER: Mutex<Writer> = Mutex::new(Writer {
column_position: 0, color_code: ColorCode::new(Color::Yellow, Color::Black),
buffer: unsafe { &mut *(0xb8000 as *mut Buffer) },
});
pub static ref WRITER: Mutex<Writer> =
Mutex::new(Writer::new(ColorCode::new(Color::Yellow, Color::Black)));
}
#[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
View 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 {}
}