2022-11-01 07:24:50 -05:00
|
|
|
#![no_std]
|
|
|
|
#![no_main]
|
|
|
|
#![feature(abi_x86_interrupt)]
|
|
|
|
#![feature(alloc_error_handler)]
|
|
|
|
#![feature(allocator_api)]
|
|
|
|
#![feature(naked_functions)]
|
2024-07-14 10:45:40 -05:00
|
|
|
#![feature(stmt_expr_attributes)]
|
|
|
|
#![feature(exposed_provenance)]
|
|
|
|
// #![feature(strict_provenance)]
|
|
|
|
#![deny(
|
|
|
|
unsafe_op_in_unsafe_fn,
|
|
|
|
reason = "Unsafe blocks make it clear what parts of the code is unsafe and should not be skipped in unsafe functions, which are only unsafe to call."
|
|
|
|
)]
|
|
|
|
#![deny(
|
|
|
|
unfulfilled_lint_expectations,
|
|
|
|
reason = "If the expected lint goes away, this most likely means the expect attribute should be removed"
|
|
|
|
)]
|
|
|
|
// #![deny(fuzzy_provenance_casts)]
|
|
|
|
#![allow(clippy::duplicated_attributes, reason = "Identical reasons trigger this falsely")]
|
|
|
|
#![deny(
|
|
|
|
clippy::unwrap_used,
|
|
|
|
reason = "Unwraps must be used only when they will provably not panic, and the justification must be provided"
|
|
|
|
)]
|
|
|
|
#![deny(
|
|
|
|
clippy::expect_used,
|
|
|
|
reason = "Expects will cause a kernel panic. Kernel panics must be justified, as the kernel crashing is irrecoverable and brings the whole system down."
|
|
|
|
)]
|
|
|
|
#![deny(
|
|
|
|
clippy::panic,
|
|
|
|
reason = "Kernel panics must be justified, as the kernel crashing is irrecoverable and brings the whole system down."
|
|
|
|
)]
|
|
|
|
#![deny(
|
|
|
|
clippy::expect_used,
|
|
|
|
reason = "Expects will cause a kernel panic. Kernel panics must be justified, as the kernel crashing is irrecoverable and brings the whole system down."
|
|
|
|
)]
|
|
|
|
#![deny(
|
|
|
|
clippy::allow_attributes,
|
|
|
|
reason = "Expect attributes warn when the expected lint is not present, which is preferrable"
|
|
|
|
)]
|
|
|
|
#![deny(clippy::allow_attributes_without_reason, reason = "Allowing lints needs a justification")]
|
|
|
|
#![deny(clippy::arithmetic_side_effects)]
|
|
|
|
#![deny(clippy::indexing_slicing)]
|
|
|
|
#![warn(clippy::unimplemented)]
|
|
|
|
#![warn(clippy::todo)]
|
|
|
|
#![warn(clippy::pedantic)]
|
|
|
|
#![allow(clippy::cast_lossless, reason = "Covered by as_conversions")]
|
|
|
|
#![allow(clippy::cast_possible_truncation, reason = "Covered by as_conversions")]
|
|
|
|
#![allow(clippy::cast_possible_wrap, reason = "Covered by as_conversions")]
|
|
|
|
#![allow(clippy::cast_precision_loss, reason = "Covered by as_conversions")]
|
|
|
|
#![allow(clippy::cast_sign_loss, reason = "Covered by as_conversions")]
|
|
|
|
#![allow(
|
|
|
|
clippy::match_same_arms,
|
|
|
|
reason = "Repeated match arms usually exist for readability or when the ordering is important"
|
|
|
|
)]
|
|
|
|
#![allow(clippy::missing_panics_doc, reason = "Don't care")]
|
|
|
|
#![allow(clippy::missing_errors_doc, reason = "Don't care")]
|
|
|
|
#![allow(clippy::similar_names, reason = "Don't care")]
|
|
|
|
#![allow(clippy::too_many_lines, reason = "Don't care")]
|
|
|
|
#![warn(clippy::nursery)]
|
|
|
|
#![allow(clippy::suspicious_operation_groupings, reason = "Too easy for false positives")]
|
|
|
|
#![allow(
|
|
|
|
clippy::option_if_let_else,
|
|
|
|
reason = "if-let is for imperative code, map_or for functional. Not the same unlike what this lint says."
|
|
|
|
)]
|
|
|
|
#![allow(clippy::non_send_fields_in_send_ty, reason = "Too easy for false positives")]
|
|
|
|
#![allow(clippy::missing_const_for_fn, reason = "Most triggers don't actually make sense as const")]
|
|
|
|
#![allow(
|
|
|
|
clippy::while_float,
|
|
|
|
reason = "Lint checks for a construct you'd have to be really stupid to write and has easy false positives"
|
|
|
|
)]
|
|
|
|
#![deny(clippy::as_conversions)]
|
2022-11-01 07:24:50 -05:00
|
|
|
|
|
|
|
extern crate alloc;
|
|
|
|
|
|
|
|
mod bootinfo;
|
|
|
|
mod gdt;
|
|
|
|
mod interrupts;
|
|
|
|
mod kernel_heap;
|
|
|
|
mod panic_handler;
|
|
|
|
mod physical_memory;
|
|
|
|
mod pit;
|
|
|
|
mod qemu_exit;
|
|
|
|
mod serial;
|
|
|
|
mod start;
|
|
|
|
mod tasking;
|
|
|
|
mod virtual_memory;
|
|
|
|
|
2024-07-14 10:45:40 -05:00
|
|
|
use core::{ptr, slice};
|
2024-06-06 22:02:00 -05:00
|
|
|
|
2024-07-21 10:56:43 -05:00
|
|
|
use alloc::ffi::CString;
|
2024-06-06 22:02:00 -05:00
|
|
|
use bootinfo::BOOTINFO;
|
2024-07-14 10:45:40 -05:00
|
|
|
use cast::usize;
|
2024-07-07 14:37:21 -05:00
|
|
|
use elf::{
|
|
|
|
abi::{
|
|
|
|
PT_DYNAMIC, PT_GNU_EH_FRAME, PT_GNU_RELRO, PT_GNU_STACK, PT_LOAD, PT_NULL, PT_PHDR,
|
|
|
|
R_X86_64_RELATIVE, SHT_REL, SHT_RELA,
|
|
|
|
},
|
|
|
|
endian::AnyEndian,
|
|
|
|
ElfBytes,
|
|
|
|
};
|
2024-08-16 19:23:31 -05:00
|
|
|
use physical_memory::PHYSICAL_MEMORY;
|
2024-07-07 07:59:27 -05:00
|
|
|
use serial::SECOND_PORT;
|
2024-08-16 19:23:31 -05:00
|
|
|
use spin::lazy::Lazy;
|
2022-11-01 07:24:50 -05:00
|
|
|
use tar_no_std::TarArchiveRef;
|
2023-09-29 15:44:53 -05:00
|
|
|
use tasking::TASKING;
|
2024-08-16 19:23:31 -05:00
|
|
|
use virtual_memory::{ACTIVE_SPACE, KERNEL_SPACE};
|
2024-07-07 14:37:21 -05:00
|
|
|
use x86_64::{
|
|
|
|
registers::rflags::{self, RFlags},
|
|
|
|
structures::paging::{Page, PageTableFlags},
|
|
|
|
VirtAddr,
|
|
|
|
};
|
2024-06-06 22:02:00 -05:00
|
|
|
|
|
|
|
use crate::virtual_memory::AddressSpace;
|
2022-11-01 07:24:50 -05:00
|
|
|
|
2024-06-06 22:02:00 -05:00
|
|
|
// pub static INITRD: &[u8] = include_bytes!("../initrd.tar");
|
2023-09-29 15:44:53 -05:00
|
|
|
|
2024-07-14 10:45:40 -05:00
|
|
|
#[expect(
|
|
|
|
clippy::expect_used,
|
|
|
|
reason = "Nothing to do here but panic on errors, this is the top level"
|
|
|
|
)]
|
2022-11-01 07:24:50 -05:00
|
|
|
pub fn main() {
|
|
|
|
let mut rflags_data = rflags::read();
|
|
|
|
rflags_data |= RFlags::IOPL_HIGH | RFlags::IOPL_LOW;
|
|
|
|
unsafe {
|
|
|
|
rflags::write(rflags_data);
|
|
|
|
}
|
|
|
|
gdt::init();
|
2024-08-16 19:23:31 -05:00
|
|
|
Lazy::force(&PHYSICAL_MEMORY);
|
|
|
|
Lazy::force(&KERNEL_SPACE);
|
|
|
|
Lazy::force(&ACTIVE_SPACE);
|
2022-11-01 07:24:50 -05:00
|
|
|
interrupts::init();
|
|
|
|
pit::init(100);
|
2024-06-06 22:02:00 -05:00
|
|
|
let initrd = unsafe {
|
2024-07-14 10:45:40 -05:00
|
|
|
let ramdisk_start = BOOTINFO.ramdisk_addr.into_option().expect("initrd no present");
|
2024-06-06 22:02:00 -05:00
|
|
|
let ramdisk_len = BOOTINFO.ramdisk_len;
|
2024-07-14 10:45:40 -05:00
|
|
|
slice::from_raw_parts(
|
|
|
|
ptr::with_exposed_provenance(usize(ramdisk_start)),
|
|
|
|
usize(ramdisk_len),
|
|
|
|
)
|
2024-06-06 22:02:00 -05:00
|
|
|
};
|
2024-07-14 10:45:40 -05:00
|
|
|
let initrd = TarArchiveRef::new(initrd).expect("initrd not valid TAR archive");
|
2024-06-06 22:02:00 -05:00
|
|
|
let init_data = initrd
|
2023-09-29 15:44:53 -05:00
|
|
|
.entries()
|
2024-07-14 10:45:40 -05:00
|
|
|
.find(|x| x.filename().as_str() == Ok("bin/init"))
|
2023-09-29 15:44:53 -05:00
|
|
|
.expect("Could not find init in initrd")
|
|
|
|
.data();
|
2024-07-14 10:45:40 -05:00
|
|
|
let init = ElfBytes::<AnyEndian>::minimal_parse(init_data).expect("init not valid ELF file");
|
|
|
|
let mut init_addr_space = AddressSpace::new().expect("failed to create address space for init");
|
|
|
|
let pheaders = init.segments().expect("init has no program headers (not an executable?)");
|
|
|
|
for mut pheader in pheaders {
|
2024-06-06 22:02:00 -05:00
|
|
|
match pheader.p_type {
|
|
|
|
PT_NULL => (),
|
2024-07-07 14:37:21 -05:00
|
|
|
PT_LOAD => {
|
2024-06-06 22:02:00 -05:00
|
|
|
if pheader.p_vaddr < 0x1000 {
|
|
|
|
if pheader.p_memsz < 0x1000 {
|
|
|
|
continue;
|
|
|
|
}
|
2024-07-14 10:45:40 -05:00
|
|
|
#[expect(
|
|
|
|
clippy::arithmetic_side_effects,
|
|
|
|
reason = "p_vaddr has been cheched to be below 0x1000, thus this cannot underflow"
|
|
|
|
)]
|
|
|
|
{
|
|
|
|
pheader.p_offset += 0x1000 - pheader.p_vaddr;
|
|
|
|
pheader.p_memsz -= 0x1000 - pheader.p_vaddr;
|
|
|
|
pheader.p_filesz -= 0x1000 - pheader.p_vaddr;
|
|
|
|
}
|
2024-06-06 22:02:00 -05:00
|
|
|
pheader.p_vaddr = 0x1000;
|
|
|
|
}
|
|
|
|
let start_page = Page::containing_address(VirtAddr::new(pheader.p_vaddr));
|
2024-07-14 10:45:40 -05:00
|
|
|
let num_pages = if pheader.p_vaddr.trailing_zeros() >= 12 {
|
|
|
|
usize(pheader.p_memsz.div_ceil(4096))
|
|
|
|
} else {
|
|
|
|
#[expect(
|
|
|
|
clippy::arithmetic_side_effects,
|
|
|
|
reason = "The RHS is always < 4096, thus this cannot underflow"
|
|
|
|
)]
|
|
|
|
let page_part_sz = 4096 - (pheader.p_vaddr & 0xFFF);
|
|
|
|
if pheader.p_memsz < page_part_sz {
|
|
|
|
1
|
|
|
|
} else {
|
|
|
|
#[expect(
|
|
|
|
clippy::arithmetic_side_effects,
|
|
|
|
reason = "Sub: pheader.p_memsz >= page_part_sz, thus this cannot underflow. Add: usize::MAX.div_ceil(4096) < usize::MAX, so this cannot overflow."
|
|
|
|
)]
|
|
|
|
let res = 1 + usize((pheader.p_memsz - page_part_sz).div_ceil(4096));
|
|
|
|
res
|
|
|
|
}
|
|
|
|
};
|
2024-06-06 22:02:00 -05:00
|
|
|
init_addr_space
|
|
|
|
.map_only_unused(start_page, num_pages, PageTableFlags::USER_ACCESSIBLE)
|
|
|
|
.expect("Unable to map region");
|
|
|
|
init_addr_space.run(|| unsafe {
|
2024-07-07 14:37:21 -05:00
|
|
|
let dst = slice::from_raw_parts_mut(
|
2024-07-14 10:45:40 -05:00
|
|
|
ptr::with_exposed_provenance_mut(usize(pheader.p_vaddr)),
|
|
|
|
usize(pheader.p_memsz),
|
2024-07-07 14:37:21 -05:00
|
|
|
);
|
2024-07-14 10:45:40 -05:00
|
|
|
dst.get_mut(0..usize(pheader.p_filesz))
|
|
|
|
.expect("Pheader filesize greater tham memsize")
|
|
|
|
.copy_from_slice(
|
|
|
|
init_data
|
|
|
|
.get(
|
|
|
|
usize(pheader.p_offset)
|
|
|
|
..usize(
|
|
|
|
pheader
|
|
|
|
.p_offset
|
|
|
|
.checked_add(pheader.p_filesz)
|
|
|
|
.expect("End of segment in file wraps around"),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.expect(
|
|
|
|
"Program header references data beyond the end of the file",
|
|
|
|
),
|
|
|
|
);
|
|
|
|
dst.get_mut(usize(pheader.p_filesz)..usize(pheader.p_memsz))
|
|
|
|
.expect("Pheader filesize greater than memsize")
|
|
|
|
.fill(0);
|
2024-06-06 22:02:00 -05:00
|
|
|
});
|
|
|
|
}
|
2024-07-07 14:37:21 -05:00
|
|
|
PT_GNU_RELRO => (),
|
2024-06-06 22:02:00 -05:00
|
|
|
PT_GNU_EH_FRAME => (),
|
|
|
|
PT_GNU_STACK => (),
|
|
|
|
PT_DYNAMIC => (),
|
|
|
|
PT_PHDR => (),
|
|
|
|
_ => println!("Warning: Unimplemented ELF program header type {:#x}", pheader.p_type),
|
|
|
|
}
|
|
|
|
}
|
2024-07-14 10:45:40 -05:00
|
|
|
if let Some(section_headers) = init.section_headers() {
|
|
|
|
for section in section_headers.iter() {
|
|
|
|
if section.sh_type == SHT_REL {
|
|
|
|
#[expect(
|
|
|
|
clippy::never_loop,
|
|
|
|
reason = "This loop exists as a template to add relocations when they are implemented"
|
|
|
|
)]
|
|
|
|
#[expect(
|
|
|
|
clippy::unwrap_used,
|
|
|
|
reason = "section_data_as_rels requires the section type to be SHT_REL, which we already checked"
|
|
|
|
)]
|
|
|
|
for rel in init.section_data_as_rels(§ion).unwrap() {
|
|
|
|
#[expect(
|
|
|
|
clippy::match_single_binding,
|
|
|
|
reason = "This match exists as a template to add relocations when they are implemented"
|
|
|
|
)]
|
|
|
|
match rel.r_type {
|
|
|
|
_ => unimplemented!("ELF relocation type {}", rel.r_type),
|
|
|
|
}
|
2024-06-06 22:02:00 -05:00
|
|
|
}
|
|
|
|
}
|
2024-07-14 10:45:40 -05:00
|
|
|
if section.sh_type == SHT_RELA {
|
|
|
|
#[expect(
|
|
|
|
clippy::unwrap_used,
|
|
|
|
reason = "section_data_as_relas requires the section type to be SHT_RELA, which we already checked"
|
|
|
|
)]
|
|
|
|
for rela in init.section_data_as_relas(§ion).unwrap() {
|
|
|
|
match rela.r_type {
|
|
|
|
R_X86_64_RELATIVE => {
|
|
|
|
init_addr_space.run(|| unsafe {
|
|
|
|
let ptr =
|
|
|
|
ptr::with_exposed_provenance_mut::<u64>(usize(rela.r_offset));
|
|
|
|
ptr.write(
|
|
|
|
rela.r_addend
|
|
|
|
.try_into()
|
|
|
|
.expect("Invalid addend for relocation"),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => unimplemented!("ELF relocation type {}", rela.r_type),
|
2024-06-06 22:02:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-07 07:59:27 -05:00
|
|
|
|
|
|
|
// Before starting init, write the pcapng section header + interface description to the second serial port
|
|
|
|
SECOND_PORT.write_u32s(&[
|
2024-07-14 10:45:40 -05:00
|
|
|
0x0A0D_0D0A, // SHB type
|
2024-07-07 07:59:27 -05:00
|
|
|
7 * 4, // Total block length
|
2024-07-14 10:45:40 -05:00
|
|
|
0x1A2B_3C4D, // Byte order magic
|
2024-07-07 07:59:27 -05:00
|
|
|
0x0000_0001, // Version (1.0)
|
2024-07-14 10:45:40 -05:00
|
|
|
0xFFFF_FFFF, // Length upper (-1) across both
|
|
|
|
0xFFFF_FFFF, // Length lower
|
2024-07-07 07:59:27 -05:00
|
|
|
7 * 4, // Total block length
|
|
|
|
0x1, // IDB type
|
|
|
|
5 * 4, // Total block length
|
|
|
|
147, // Link type
|
|
|
|
4096 + 8, // Packet length limit,
|
|
|
|
5 * 4, // Total block length
|
|
|
|
]);
|
|
|
|
|
2024-07-14 10:45:40 -05:00
|
|
|
#[expect(
|
2024-07-21 10:56:43 -05:00
|
|
|
clippy::unwrap_used,
|
|
|
|
reason = "Argument does not contain a null byte, thus this cannot panic"
|
2024-07-14 10:45:40 -05:00
|
|
|
)]
|
2022-11-01 07:24:50 -05:00
|
|
|
TASKING
|
2024-07-21 10:56:43 -05:00
|
|
|
.new_process(
|
|
|
|
ptr::with_exposed_provenance(usize(init.ehdr.e_entry)),
|
|
|
|
init_addr_space,
|
|
|
|
&[&CString::new(b"init").unwrap()],
|
|
|
|
)
|
2022-11-01 07:24:50 -05:00
|
|
|
.expect("Failed to create init process");
|
|
|
|
}
|