From 28894906576715901a7e528e95e88d04c26d4aa9 Mon Sep 17 00:00:00 2001 From: Greg Shuflin Date: Sun, 13 Feb 2022 19:56:35 -0800 Subject: [PATCH] Add modern (2022) version of inital code --- .cargo/config | 7 ++- Cargo.lock | 16 ++++++ Cargo.toml | 14 ++--- justfile | 7 +++ run.sh | 5 -- src/gdt.rs | 47 ----------------- src/interrupts.rs | 98 ----------------------------------- src/main.rs | 38 ++++++-------- os.json => x86_64_target.json | 0 9 files changed, 49 insertions(+), 183 deletions(-) create mode 100644 Cargo.lock create mode 100644 justfile delete mode 100755 run.sh delete mode 100644 src/gdt.rs delete mode 100644 src/interrupts.rs rename os.json => x86_64_target.json (100%) diff --git a/.cargo/config b/.cargo/config index dbf2731..f6a4ad5 100644 --- a/.cargo/config +++ b/.cargo/config @@ -1,2 +1,7 @@ +[unstable] +build-std = ["core", "compiler_builtins"] +build-std-features = ["compiler-builtins-mem"] + [build] -target = "os.json" +target = "x86_64_target.json" + diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..9a16c44 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,16 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "bootloader" +version = "0.9.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a62c8f6168cd106687ee36a2b71a46c4144d73399f72814104d33094b8092fd2" + +[[package]] +name = "rustos" +version = "0.1.0" +dependencies = [ + "bootloader", +] diff --git a/Cargo.toml b/Cargo.toml index 09834f2..7dade6f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,17 +2,9 @@ name = "rustos" version = "0.1.0" authors = ["greg "] -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" - -[dependencies.lazy_static] -version = "1.3.0" -features = ["spin_no_std"] +#bootloader = "0.10.12" +bootloader = "0.9.8" diff --git a/justfile b/justfile new file mode 100644 index 0000000..720494d --- /dev/null +++ b/justfile @@ -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 + diff --git a/run.sh b/run.sh deleted file mode 100755 index 3543c53..0000000 --- a/run.sh +++ /dev/null @@ -1,5 +0,0 @@ -#!/bin/sh - -cargo bootimage -qemu-system-x86_64 -drive format=raw,file=target/os/debug/bootimage-rustos.bin - diff --git a/src/gdt.rs b/src/gdt.rs deleted file mode 100644 index 2748534..0000000 --- a/src/gdt.rs +++ /dev/null @@ -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); - } -} diff --git a/src/interrupts.rs b/src/interrupts.rs deleted file mode 100644 index e8fcfab..0000000 --- a/src/interrupts.rs +++ /dev/null @@ -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 = - 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> = - 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()) - } -} diff --git a/src/main.rs b/src/main.rs index 0408d47..fb7b430 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,41 +1,37 @@ -#![feature(abi_x86_interrupt, asm)] +//#![feature(abi_x86_interrupt, asm)] #![no_std] #![no_main] +/* #[macro_use] mod vga_buffer; -mod interrupts; -mod gdt; +*/ use core::panic::PanicInfo; #[panic_handler] -fn panic(info: &PanicInfo) -> ! { - println!("{}", info); - halt_loop(); +fn panic(_info: &PanicInfo) -> ! { + loop {} } -pub fn init() { - interrupts::initalize_pics(); - gdt::init(); - interrupts::init_idt(); -} +static GAMARJOBA: &[u8] = b"Gamarjoba, MVNDE!"; #[no_mangle] pub extern "C" fn _start() -> ! { - init(); + let vga_buffer = 0xb8000 as *mut u8; + + for (i, &byte) in GAMARJOBA.iter().enumerate() { + let i = i as isize; + unsafe { + *vga_buffer.offset(i * 2) = byte; + *vga_buffer.offset(i * 2 + 1) = 0xd; + } - for i in 1..10 { - println!("Gamarjoba, munde: {}", i); - } - halt_loop(); + } + + loop {} } -pub fn halt_loop() -> ! { - loop { - x86_64::instructions::hlt(); - } -} diff --git a/os.json b/x86_64_target.json similarity index 100% rename from os.json rename to x86_64_target.json