Add modern (2022) version of inital code

This commit is contained in:
Greg Shuflin 2022-02-13 19:56:35 -08:00
parent 7af0fe2c6c
commit 2889490657
9 changed files with 49 additions and 183 deletions

View File

@ -1,2 +1,7 @@
[unstable]
build-std = ["core", "compiler_builtins"]
build-std-features = ["compiler-builtins-mem"]
[build] [build]
target = "os.json" target = "x86_64_target.json"

16
Cargo.lock generated Normal file
View File

@ -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",
]

View File

@ -2,17 +2,9 @@
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"
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"]

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())
}
}

View File

@ -1,41 +1,37 @@
#![feature(abi_x86_interrupt, asm)] //#![feature(abi_x86_interrupt, asm)]
#![no_std] #![no_std]
#![no_main] #![no_main]
/*
#[macro_use] #[macro_use]
mod vga_buffer; mod vga_buffer;
mod interrupts; */
mod gdt;
use core::panic::PanicInfo; use core::panic::PanicInfo;
#[panic_handler] #[panic_handler]
fn panic(info: &PanicInfo) -> ! { fn panic(_info: &PanicInfo) -> ! {
println!("{}", info); loop {}
halt_loop();
} }
pub fn init() { static GAMARJOBA: &[u8] = b"Gamarjoba, MVNDE!";
interrupts::initalize_pics();
gdt::init();
interrupts::init_idt();
}
#[no_mangle] #[no_mangle]
pub extern "C" fn _start() -> ! { 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);
} loop {}
halt_loop();
} }
pub fn halt_loop() -> ! {
loop {
x86_64::instructions::hlt();
}
}