Isabelle L. há 5 anos
cometimento
d0ac3d0475
10 ficheiros alterados com 430 adições e 0 eliminações
  1. +8
    -0
      .cargo/config.toml
  2. +1
    -0
      .gitignore
  3. +79
    -0
      Cargo.lock
  4. +31
    -0
      Cargo.toml
  5. +7
    -0
      LICENSE
  6. +1
    -0
      README.md
  7. +86
    -0
      src/main.rs
  8. +33
    -0
      src/serial.rs
  9. +169
    -0
      src/vga.rs
  10. +15
    -0
      target.json

+ 8
- 0
.cargo/config.toml Ver ficheiro

@@ -0,0 +1,8 @@
[unstable]
build-std = ["core", "compiler_builtins"]

[build]
target = "target.json"

[target.'cfg(target_os = "none")']
runner = "bootimage runner"

+ 1
- 0
.gitignore Ver ficheiro

@@ -0,0 +1 @@
/target

+ 79
- 0
Cargo.lock Ver ficheiro

@@ -0,0 +1,79 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "bit_field"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed8765909f9009617974ab6b7d332625b320b33c326b1e9321382ef1999b5d56"

[[package]]
name = "bitflags"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"

[[package]]
name = "bootloader"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ad686b9b47363de7d36c05fb6885f17d08d0f2d15b1bc782a101fe3c94a2c7c"

[[package]]
name = "glade"
version = "0.1.0"
dependencies = [
"bootloader",
"lazy_static",
"rlibc",
"spin",
"uart_16550",
"volatile",
"x86_64",
]

[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
dependencies = [
"spin",
]

[[package]]
name = "rlibc"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fc874b127765f014d792f16763a81245ab80500e2ad921ed4ee9e82481ee08fe"

[[package]]
name = "spin"
version = "0.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6e63cff320ae2c57904679ba7cb63280a3dc4613885beafb148ee7bf9aa9042d"

[[package]]
name = "uart_16550"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e58fc40dc1712664fc9b0a7bd8ca2f21ab49960924fb245a80a05e1e92f3dfe9"
dependencies = [
"bitflags",
"x86_64",
]

[[package]]
name = "volatile"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f6b06ad3ed06fef1713569d547cdbdb439eafed76341820fb0e0344f29a41945"

[[package]]
name = "x86_64"
version = "0.11.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aa580d2cf2a6a8c55f6283d6d06271b1ccab4d93cb3741edab290d5408d848c4"
dependencies = [
"bit_field",
"bitflags",
]

+ 31
- 0
Cargo.toml Ver ficheiro

@@ -0,0 +1,31 @@
[package]
name = "glade"
version = "0.1.0"
authors = ["Isabelle Lesko <me@izzabelle.dev>"]
edition = "2018"

#[profile.dev]
#panic = "abort"

[profile.release]
panic = "abort"

[dependencies]
rlibc = "1.0.0"
bootloader = "0.9.8"
volatile = "0.2.6"
spin = "0.5.2"
x86_64 = "0.11.7"
uart_16550 = "0.2.7"

[dependencies.lazy_static]
version = "1.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

+ 7
- 0
LICENSE Ver ficheiro

@@ -0,0 +1,7 @@
Copyright 2020 Isabelle L.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

+ 1
- 0
README.md Ver ficheiro

@@ -0,0 +1 @@
# OH GOD OH FUCK AAAAAAAAAAAA OS DEV REEEEEEEEE

+ 86
- 0
src/main.rs Ver ficheiro

@@ -0,0 +1,86 @@
#![no_std]
#![no_main]
#![feature(custom_test_frameworks)]
#![test_runner(crate::test_runner)]
#![reexport_test_harness_main = "test_main"]

// link the libc crate
extern crate rlibc;

// modules
mod serial;
mod vga;

// namespacing
use core::panic::PanicInfo;

// panic handler definition
#[cfg(not(test))]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
println!("{}", info);
loop {}
}

#[cfg(test)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
sprintln!("[err]\n");
sprintln!("Error: {}", info);
exit_qemu(QemuExitCode::Failed);
loop {}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum QemuExitCode {
Success = 0x10,
Failed = 0x11,
}

pub fn exit_qemu(exit_code: QemuExitCode) {
use x86_64::instructions::port::Port;

unsafe {
let mut port = Port::new(0xf4);
port.write(exit_code as u32);
}
}

// _start definition
#[no_mangle]
pub extern "C" fn _start() -> ! {
println!("gladeOS online");

#[cfg(test)]
test_main();

panic!("oh god oh fuck");

#[allow(unreachable_code)]
loop {}
}

pub trait Testable {
fn run(&self) -> ();
}

impl<T: Fn()> Testable for T {
fn run(&self) -> () {
sprint!("{}...\t", core::any::type_name::<T>());
self();
sprintln!("[oki]")
}
}

#[cfg(test)]
fn test_runner(tests: &[&dyn Testable]) {
sprintln!("running {} tests", tests.len());
tests.iter().for_each(|test| test.run());
sprintln!("all tests passed!");
exit_qemu(QemuExitCode::Success);
}

#[test_case]
fn very_trivial_assertion() {
assert!(1 == 1);
}

+ 33
- 0
src/serial.rs Ver ficheiro

@@ -0,0 +1,33 @@
use lazy_static::lazy_static;
use spin::Mutex;
use uart_16550::SerialPort;

lazy_static! {
pub static ref SERIAL_ONE: Mutex<SerialPort> = {
let mut serial_port = unsafe { SerialPort::new(0x3f8) };
serial_port.init();
Mutex::new(serial_port)
};
}

#[doc(hidden)]
pub fn _print(args: ::core::fmt::Arguments) {
use core::fmt::Write;
SERIAL_ONE.lock().write_fmt(args).expect("failed to write to serial");
}

/// prints to the host through a serial interface
#[macro_export]
macro_rules! sprint {
($($arg:tt)*) => {
$crate::serial::_print(format_args!($($arg)*));
};
}

/// prints to the host through a serial interface and appends a newline
#[macro_export]
macro_rules! sprintln {
() => ($crate::sprint!("\n"));
($fmt:expr) => ($crate::sprint!(concat!($fmt, "\n")));
($fmt:expr, $($arg:tt)*) => ($crate::sprint!(concat!($fmt, "\n"), $($arg)*));
}

+ 169
- 0
src/vga.rs Ver ficheiro

@@ -0,0 +1,169 @@
// namespacing
use core::fmt;
use lazy_static::lazy_static;
use spin::Mutex;
use volatile::Volatile;

// consts
const BUFFER_WIDTH: usize = 80;
const BUFFER_HEIGHT: usize = 25;

// static vga buffer
lazy_static! {
pub static ref VGA_WRITER: Mutex<VgaWriter> = Mutex::new(VgaWriter {
column_position: 0,
color_code: ColorCode::new(VgaColor::Pink, VgaColor::Black),
buffer: unsafe { &mut *(0xB8000 as *mut VgaBuffer) }
});
}

/// the availible color nibbles for vga
#[allow(dead_code)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VgaColor {
Black = 0,
Blue = 1,
Green = 2,
Cyan = 3,
Red = 4,
Magenta = 5,
Brown = 6,
LightGrey = 7,
DarkGrey = 8,
LightBlue = 9,
LightGreen = 10,
LightCyan = 11,
LightRed = 12,
Pink = 13,
Yellow = 14,
White = 15,
}

/// a set of two color nibbles for a color byte
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(transparent)]
struct ColorCode(u8);

impl ColorCode {
/// create a new color code
pub fn new(foreground: VgaColor, background: VgaColor) -> ColorCode {
ColorCode((background as u8) << 4 | (foreground as u8))
}
}

/// an ascii character and a color byte
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(C)]
struct VgaChar {
ascii_char: u8,
color_code: ColorCode,
}

/// the core vga buffer
#[repr(transparent)]
struct VgaBuffer {
data: [[Volatile<VgaChar>; BUFFER_WIDTH]; BUFFER_HEIGHT],
}

/// the writer structure
pub struct VgaWriter {
column_position: usize,
color_code: ColorCode,
buffer: &'static mut VgaBuffer,
}

impl VgaWriter {
/// write a new byte to the buffer
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;

self.buffer.data[row][col].write(VgaChar { ascii_char: byte, color_code });
self.column_position += 1;
}
}
}

/// write a str slice to the buffer
pub fn write_slice(&mut self, s: &str) {
for byte in s.bytes() {
match byte {
0x20..=0x7E | b'\n' => self.write_byte(byte),
_ => self.write_byte(0xfe),
}
}
}

// write a new line byte to the buffer
fn new_line(&mut self) {
for row in 1..BUFFER_HEIGHT {
for col in 0..BUFFER_WIDTH {
let character = self.buffer.data[row][col].read();
self.buffer.data[row - 1][col].write(character);
}
}
self.clear_row(BUFFER_HEIGHT - 1);
self.column_position = 0;
}

// clear a row
fn clear_row(&mut self, row: usize) {
let blank = VgaChar { ascii_char: b' ', color_code: self.color_code };

for col in 0..BUFFER_WIDTH {
self.buffer.data[row][col].write(blank);
}
}
}

impl fmt::Write for VgaWriter {
fn write_str(&mut self, s: &str) -> fmt::Result {
self.write_slice(s);
Ok(())
}
}

#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::vga::_print(format_args!($($arg)*)));
}

#[macro_export]
macro_rules! println {
() => ($crate::print!("\n"));
($($arg:tt)*) => ($crate::print!("{}\n", format_args!($($arg)*)));
}

#[doc(hidden)]
pub fn _print(args: fmt::Arguments) {
use core::fmt::Write;
VGA_WRITER.lock().write_fmt(args).unwrap();
}

#[test_case]
fn test_println_simple() {
println!("test_println_simple_output");
}

#[test_case]
fn test_println_many() {
(0..100).for_each(|i| println!("test_println_many output {}", i));
}

#[test_case]
fn test_println_output() {
let s = "some test string that fits in a line";
println!("{}", s);
s.chars().enumerate().for_each(|(i, c)| {
let screen_char = VGA_WRITER.lock().buffer.data[BUFFER_HEIGHT - 2][i].read();
assert!(char::from(screen_char.ascii_char) == c);
});
}

+ 15
- 0
target.json Ver ficheiro

@@ -0,0 +1,15 @@
{
"llvm-target": "x86_64-unknown-linux-gnu",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"arch": "x86_64",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"os": "none",
"executables": true,
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"panic-strategy": "abort",
"disable-redzone": true,
"features": "-mmx,-sse,+soft-float"
}

Carregando…
Cancelar
Guardar