Initial commit

This commit is contained in:
Zoe
2026-08-18 07:16:11 -05:00
commit 8dc3839ba4
18 changed files with 724 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
#[cfg(target_arch = "x86_64")]
mod x86_64;
#[cfg(target_arch = "x86_64")]
pub use x86_64::*;
+77
View File
@@ -0,0 +1,77 @@
/* Tell the linker that we want an x86_64 ELF64 output file */
OUTPUT_FORMAT(elf64-x86-64)
OUTPUT_ARCH(i386:x86-64)
/* We want the symbol _start to be our entry point */
ENTRY(_start)
/* Define the program headers we want so the bootloader gives us the right */
/* MMU permissions; this also allows us to exert more control over the linking */
/* process. */
PHDRS
{
headers PT_PHDR PHDRS;
text PT_LOAD FILEHDR PHDRS;
rodata PT_LOAD;
data PT_LOAD;
dynamic PT_DYNAMIC;
}
SECTIONS
{
/* We want to be placed in the topmost 2GiB of the address space, for optimisations */
/* and because that is what the Limine spec mandates. */
/* Any address in this region will do, but often 0xffffffff80000000 is chosen as */
/* that is the beginning of the region. */
/* Additionally, leave space for the ELF headers by adding SIZEOF_HEADERS to the */
/* base load address. */
. = 0xffffffff80000000 + SIZEOF_HEADERS;
.text : {
*(.text .text.*)
} :text
/* Move to the next memory page for .rodata */
. = ALIGN(CONSTANT(MAXPAGESIZE));
.rodata : {
*(.rodata .rodata.*)
} :rodata
/* Move to the next memory page for .data */
. = ALIGN(CONSTANT(MAXPAGESIZE));
.data : {
*(.data .data.*)
/* Place the sections that contain the Limine requests as part of the .data */
/* output section. */
KEEP(*(.requests_start_marker))
KEEP(*(.requests))
KEEP(*(.requests_end_marker))
} :data
/* Dynamic section for relocations, both in its own PHDR and inside data PHDR. */
.dynamic : {
*(.dynamic)
} :data :dynamic
/* NOTE: .bss needs to be the last thing mapped to :data, otherwise lots of */
/* unnecessary zeros will be written to the binary. */
/* If you need, for example, .init_array and .fini_array, those should be placed */
/* above this. */
.bss : {
*(.bss .bss.*)
*(COMMON)
} :data
/* Discard .note.* and .eh_frame* since they may cause issues on some hosts. */
/* Also discard the program interpreter section since we do not need one. This is */
/* more or less equivalent to the --no-dynamic-linker linker flag, except that it */
/* works with ld.gold. */
/DISCARD/ : {
*(.eh_frame*)
*(.note .note.*)
*(.interp)
}
}
+1
View File
@@ -0,0 +1 @@
pub mod port;
+105
View File
@@ -0,0 +1,105 @@
use core::arch::asm;
#[inline(always)]
pub unsafe fn read_u8(port: u16) -> u8 {
let value: u8;
unsafe {
asm!(
"in al, dx",
in("dx") port,
out("al") value,
options(nomem, nostack, preserves_flags),
);
}
value
}
#[inline(always)]
pub unsafe fn read_u8_slice(port: u16, slice: &mut [u8]) {
unsafe {
asm!(
"rep insb",
in("dx") port,
inout("rdi") slice.as_mut_ptr() => _,
inout("rcx") slice.len() => _,
options(nostack, preserves_flags),
);
}
}
#[inline(always)]
pub unsafe fn read_u16(port: u16) -> u16 {
let value: u16;
unsafe {
asm!(
"in ax, dx",
in("dx") port,
out("ax") value,
options(nomem, nostack, preserves_flags),
);
}
value
}
#[inline(always)]
pub unsafe fn read_u32(port: u16) -> u32 {
let value: u32;
unsafe {
asm!(
"in eax, dx",
in("dx") port,
out("eax") value,
options(nomem, nostack, preserves_flags),
);
}
value
}
#[inline(always)]
pub unsafe fn write_u8(port: u16, value: u8) {
unsafe {
asm!(
"out dx, al",
in("dx") port,
in("al") value,
options(nomem, nostack, preserves_flags),
);
}
}
#[inline(always)]
pub unsafe fn write_u8_slice(port: u16, slice: &[u8]) {
unsafe {
asm!(
"rep outsb",
in("dx") port,
inout("rsi") slice.as_ptr() => _,
inout("rcx") slice.len() => _,
options(nostack, preserves_flags),
);
}
}
#[inline(always)]
pub unsafe fn write_u16(port: u16, value: u16) {
unsafe {
asm!(
"out dx, ax",
in("dx") port,
in("ax") value,
options(nomem, nostack, preserves_flags),
);
}
}
#[inline(always)]
pub unsafe fn write_u32(port: u16, value: u32) {
unsafe {
asm!(
"out dx, eax",
in("dx") port,
in("eax") value,
options(nomem, nostack, preserves_flags),
);
}
}
+20
View File
@@ -0,0 +1,20 @@
{
"arch": "x86_64",
"cpu": "x86-64",
"data-layout": "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128",
"llvm-target": "x86_64-unknown-none",
"target-endian": "little",
"target-pointer-width": 64,
"target-c-int-width": 32,
"features": "-mmx,-sse,+soft-float",
"rustc-abi": "softfloat",
"os": "DawnOS",
"linker": "rust-lld",
"linker-flavor": "ld.lld",
"pre-link-args": {
"ld.lld": ["-melf_x86_64", "--script=./src/arch/x86_64/linker.ld"]
},
"panic-strategy": "abort",
"exe-suffix": ".elf",
"disable-redzone": true
}
+22
View File
@@ -0,0 +1,22 @@
use limine::request::FramebufferRequest;
use limine::{BaseRevision, RequestsEndMarker, RequestsStartMarker};
/// Sets the base revision to the latest revision supported by the crate.
/// See specification for further info.
/// Be sure to mark all limine requests with #[used], otherwise they may be removed by the compiler.
#[used]
// The .requests section allows limine to find the requests faster and more safely.
#[unsafe(link_section = ".requests")]
pub static BASE_REVISION: BaseRevision = BaseRevision::new();
#[used]
#[unsafe(link_section = ".requests")]
pub static FRAMEBUFFER_REQUEST: FramebufferRequest = FramebufferRequest::new();
/// Define the stand and end markers for Limine requests.
#[used]
#[unsafe(link_section = ".requests_start_marker")]
static _START_MARKER: RequestsStartMarker = RequestsStartMarker::new();
#[used]
#[unsafe(link_section = ".requests_end_marker")]
static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new();
+1
View File
@@ -0,0 +1 @@
pub mod limine;
+1
View File
@@ -0,0 +1 @@
pub mod serial;
+179
View File
@@ -0,0 +1,179 @@
use crate::arch::port::{read_u8, write_u8};
const COM1_BASE: u16 = 0x3F8;
mod register {
pub const DATA_BUFFER: u16 = 0;
pub const DIVISOR_LOW: u16 = 0;
pub const INTERRUPT_ENABLE: u16 = 1;
pub const DIVISOR_HIGH: u16 = 1;
pub const INTERRUPT_IDENTIFICATION: u16 = 2;
pub const FIFO_CONTROL: u16 = 2;
pub const LINE_CONTROL: u16 = 3;
pub const MODEM_CONTROL: u16 = 4;
pub const LINE_STATUS: u16 = 5;
pub const MODEM_STATUS: u16 = 6;
pub const SCRATCH: u16 = 7;
}
mod fifo_control {
pub const ENABLE: u8 = 1 << 0;
pub const CLEAR_RECEIVE: u8 = 1 << 1;
pub const CLEAR_TRANSMIT: u8 = 1 << 2;
pub const TRIGGER_LEVEL_14: u8 = 0b11 << 6;
}
mod line_control {
pub const DATA_BITS_8: u8 = 0b11;
pub const DLAB: u8 = 1 << 7;
}
mod line_status {
pub const TRANSMITTER_EMPTY: u8 = 1 << 5;
}
mod modem_control {
pub const DATA_TERMINAL_READY: u8 = 1 << 0;
pub const REQUEST_TO_SEND: u8 = 1 << 1;
// commonly controls whether IRQs are enabled or disabled
pub const OUT_1: u8 = 1 << 2;
// unused in PC implementations
pub const OUT_2: u8 = 1 << 3;
pub const LOOPBACK: u8 = 1 << 4;
}
#[derive(Debug)]
pub enum SerialPortError {
NotFound,
Faulty,
}
struct SerialPort {
base: u16,
}
impl SerialPort {
pub fn new(base: u16) -> Self {
Self { base }
}
pub fn init(&self) -> Result<(), SerialPortError> {
const SCRATCH_TEST_VALUE: u8 = 0x42;
const LOOPBACK_TEST_VALUE: u8 = 0xAE;
// The scratch register has no hardware-defined behavior, so it can be used to probe the port.
self.write_register(register::SCRATCH, SCRATCH_TEST_VALUE);
if self.read_register(register::SCRATCH) != SCRATCH_TEST_VALUE {
return Err(SerialPortError::NotFound);
}
self.write_register(register::INTERRUPT_ENABLE, 0);
// DLAB changes registers 0 and 1 into the low and high divisor registers so we can set the baud rate
self.write_register(register::LINE_CONTROL, line_control::DLAB);
// in this case we are setting the baud rate to 115200 baud
self.write_register(register::DIVISOR_LOW, 1);
self.write_register(register::DIVISOR_HIGH, 0);
self.write_register(register::LINE_CONTROL, line_control::DATA_BITS_8);
self.write_register(
register::FIFO_CONTROL,
fifo_control::ENABLE
| fifo_control::CLEAR_RECEIVE
| fifo_control::CLEAR_TRANSMIT
| fifo_control::TRIGGER_LEVEL_14,
);
self.write_register(
register::MODEM_CONTROL,
modem_control::DATA_TERMINAL_READY
| modem_control::REQUEST_TO_SEND
| modem_control::OUT_2,
);
self.write_register(
register::MODEM_CONTROL,
modem_control::REQUEST_TO_SEND
| modem_control::OUT_1
| modem_control::OUT_2
| modem_control::LOOPBACK,
);
self.write_byte(LOOPBACK_TEST_VALUE);
if self.read_register(register::DATA_BUFFER) != LOOPBACK_TEST_VALUE {
return Err(SerialPortError::Faulty);
}
self.write_register(
register::MODEM_CONTROL,
modem_control::DATA_TERMINAL_READY
| modem_control::REQUEST_TO_SEND
| modem_control::OUT_1
| modem_control::OUT_2,
);
Ok(())
}
fn write_register(&self, register: u16, value: u8) {
unsafe { write_u8(self.base + register, value) };
}
fn read_register(&self, register: u16) -> u8 {
unsafe { read_u8(self.base + register) }
}
fn can_transfer(&self) -> bool {
self.read_register(register::LINE_STATUS) & line_status::TRANSMITTER_EMPTY != 0
}
pub fn write_byte(&self, byte: u8) {
if byte == b'\n' {
self.write_byte(b'\r');
}
while !self.can_transfer() {
core::hint::spin_loop();
}
self.write_register(register::DATA_BUFFER, byte);
}
}
impl core::fmt::Write for SerialPort {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
for byte in s.bytes() {
self.write_byte(byte);
}
Ok(())
}
}
fn com1() -> SerialPort {
SerialPort::new(COM1_BASE)
}
pub fn init() -> Result<(), SerialPortError> {
com1().init()
}
pub fn print(args: core::fmt::Arguments) {
use core::fmt::Write;
let mut serial = com1();
let _ = serial.write_fmt(args);
}
#[macro_export]
macro_rules! print {
($($arg:tt)*) => ($crate::drivers::serial::print(format_args!($($arg)*)));
}
#[macro_export]
macro_rules! println {
() => ($crate::drivers::serial::print(format_args!("\n")));
($($arg:tt)*) => ($crate::drivers::serial::print(format_args!("{}\n", format_args!($($arg)*))));
}
+66
View File
@@ -0,0 +1,66 @@
#![feature(abi_x86_interrupt, negative_impls)]
#![allow(clippy::needless_return)]
#![no_std]
#![no_main]
mod arch;
mod boot;
mod drivers;
use boot::limine::{BASE_REVISION, FRAMEBUFFER_REQUEST};
use crate::drivers::serial;
#[unsafe(no_mangle)]
pub extern "C" fn _start() -> ! {
serial::init().unwrap();
assert!(BASE_REVISION.is_supported());
draw_gradient();
hcf();
}
fn draw_gradient() {
if let Some(framebuffer_response) = FRAMEBUFFER_REQUEST.response() {
if let Some(&framebuffer) = framebuffer_response.framebuffers().first() {
let buffer = unsafe {
core::slice::from_raw_parts_mut(
framebuffer.address().cast::<u32>(),
framebuffer.size() / 4,
)
};
for y in 0..framebuffer.height {
for x in 0..framebuffer.width {
let r = (255 * x) / (framebuffer.width - 1);
let g = (255 * y) / (framebuffer.height - 1);
let b = 255 - r;
let pixel = ((r as u32) << 16) | ((g as u32) << 8) | (b as u32);
buffer
[(((y * framebuffer.pitch) / (framebuffer.bpp as u64 / 8)) + x) as usize] =
pixel
}
}
}
}
}
#[panic_handler]
fn panic(_info: &core::panic::PanicInfo) -> ! {
hcf();
}
pub fn hcf() -> ! {
loop {
unsafe {
#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
core::arch::asm!("hlt");
#[cfg(any(target_arch = "aarch64", target_arch = "riscv64"))]
core::arch::asm!("wfi");
}
}
}