Compare commits
33 Commits
650f34f354
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 72fe78cbc6 | |||
| f9bc75c4f3 | |||
| 494d00c53b | |||
| 114c70ffe9 | |||
| 361c67764d | |||
| 36cb118933 | |||
| 7b3ba170f0 | |||
| 49880fa9d7 | |||
| 0bbbf653f8 | |||
| 40ad5dbbf4 | |||
| 2b2219f5be | |||
| c1a8afb836 | |||
| 28afe25cca | |||
| 90faace7a2 | |||
| 5b1f04c1da | |||
| 3a232c8023 | |||
| 5eaf0d7c69 | |||
| b4d0b05e13 | |||
| 969756c691 | |||
| 5c9717d384 | |||
| bb5bf9115b | |||
| ae82e9c24b | |||
| ab0ec35094 | |||
| 2ee21dea05 | |||
| d12160c5d0 | |||
| f5f5aeb8dc | |||
| 34213ca744 | |||
| 3aca8fd720 | |||
| a88059c5ff | |||
| 68c3d2fa0e | |||
| af3215d887 | |||
| 11d63535e9 | |||
| ee8dae4981 |
+1
-1
@@ -3,7 +3,7 @@ target = "x86_64-kernel"
|
|||||||
target-dir = "build/target"
|
target-dir = "build/target"
|
||||||
|
|
||||||
[unstable]
|
[unstable]
|
||||||
build-std = ["core", "compiler_builtins"]
|
build-std = ["core", "compiler_builtins", "alloc"]
|
||||||
build-std-features = ["compiler-builtins-mem"]
|
build-std-features = ["compiler-builtins-mem"]
|
||||||
|
|
||||||
[env]
|
[env]
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ main ]
|
||||||
|
pull-request:
|
||||||
|
branches: [ main ]
|
||||||
|
|
||||||
|
name: Continuous integration
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: build
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions-rust-lang/setup-rust-toolchain@v1
|
||||||
|
with:
|
||||||
|
target: x86_64-unknown-none
|
||||||
|
components: rust-src, llvm-tools-preview
|
||||||
|
toolchain: nightly
|
||||||
|
- uses: actions/checkout@v4
|
||||||
|
with:
|
||||||
|
submodules: true
|
||||||
|
- run: cargo build
|
||||||
@@ -10,3 +10,6 @@
|
|||||||
[submodule "lib/lib_serial"]
|
[submodule "lib/lib_serial"]
|
||||||
path = lib/lib_serial
|
path = lib/lib_serial
|
||||||
url = https://git.zxq5.dev/OsDev/FoundryOS-lib_serial.git
|
url = https://git.zxq5.dev/OsDev/FoundryOS-lib_serial.git
|
||||||
|
[submodule "lib/lib_alloc"]
|
||||||
|
path = lib/lib_alloc
|
||||||
|
url = https://git.zxq5.dev/OsDev/FoundryOS-lib_alloc.git
|
||||||
|
|||||||
Vendored
+9
@@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"rust-analyzer.cargo.allTargets": false,
|
||||||
|
"rust-analyzer.cargo.target": "x86_64-kernel",
|
||||||
|
"[rust]": {
|
||||||
|
"editor.defaultFormatter": "rust-lang.rust-analyzer",
|
||||||
|
"editor.formatOnSave": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Generated
+57
-13
@@ -2,19 +2,6 @@
|
|||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 4
|
version = 4
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "FoundryOS"
|
|
||||||
version = "0.1.0"
|
|
||||||
dependencies = [
|
|
||||||
"cc",
|
|
||||||
"lib_ascii",
|
|
||||||
"lib_framebuffer",
|
|
||||||
"lib_serial",
|
|
||||||
"limine",
|
|
||||||
"spin",
|
|
||||||
"x86_64",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "autocfg"
|
name = "autocfg"
|
||||||
version = "1.4.0"
|
version = "1.4.0"
|
||||||
@@ -42,6 +29,22 @@ dependencies = [
|
|||||||
"shlex",
|
"shlex",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "foundry_os"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"cc",
|
||||||
|
"lib_alloc",
|
||||||
|
"lib_ascii",
|
||||||
|
"lib_framebuffer",
|
||||||
|
"lib_serial",
|
||||||
|
"limine",
|
||||||
|
"pc-keyboard",
|
||||||
|
"pic8259",
|
||||||
|
"spin",
|
||||||
|
"x86_64",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lazy_static"
|
name = "lazy_static"
|
||||||
version = "1.5.0"
|
version = "1.5.0"
|
||||||
@@ -51,6 +54,14 @@ dependencies = [
|
|||||||
"spin",
|
"spin",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "lib_alloc"
|
||||||
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"linked_list_allocator",
|
||||||
|
"x86_64",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lib_application"
|
name = "lib_application"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
@@ -94,6 +105,15 @@ dependencies = [
|
|||||||
"bitflags",
|
"bitflags",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "linked_list_allocator"
|
||||||
|
version = "0.10.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9afa463f5405ee81cdb9cc2baf37e08ec7e4c8209442b5d72c04cfb2cd6e6286"
|
||||||
|
dependencies = [
|
||||||
|
"spinning_top",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lock_api"
|
name = "lock_api"
|
||||||
version = "0.4.12"
|
version = "0.4.12"
|
||||||
@@ -104,6 +124,21 @@ dependencies = [
|
|||||||
"scopeguard",
|
"scopeguard",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pc-keyboard"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f0ca629cbb3f0d5b699c338f0129ff78c9bfd7ea8b1258ad529bff490dc8ed5a"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pic8259"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "62d9a86c292b165f757e47e7fd66855def189b2564609bc4203727b27c33db22"
|
||||||
|
dependencies = [
|
||||||
|
"x86_64",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rustversion"
|
name = "rustversion"
|
||||||
version = "1.0.19"
|
version = "1.0.19"
|
||||||
@@ -131,6 +166,15 @@ dependencies = [
|
|||||||
"lock_api",
|
"lock_api",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "spinning_top"
|
||||||
|
version = "0.2.5"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "5b9eb1a2f4c41445a3a0ff9abc5221c5fcd28e1f13cd7c0397706f9ac938ddb0"
|
||||||
|
dependencies = [
|
||||||
|
"lock_api",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "volatile"
|
name = "volatile"
|
||||||
version = "0.4.6"
|
version = "0.4.6"
|
||||||
|
|||||||
+6
-5
@@ -1,10 +1,12 @@
|
|||||||
[workspace]
|
[workspace]
|
||||||
members = [
|
members = [
|
||||||
"lib/lib_framebuffer",
|
"lib/lib_framebuffer",
|
||||||
"lib/lib_serial",
|
"lib/lib_serial",
|
||||||
"lib/lib_ascii",
|
"lib/lib_ascii",
|
||||||
"kernel"
|
"kernel",
|
||||||
, "lib/lib_application"]
|
"lib/lib_application",
|
||||||
|
"lib/lib_alloc",
|
||||||
|
]
|
||||||
resolver = "2"
|
resolver = "2"
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
@@ -21,11 +23,10 @@ incremental = false
|
|||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
opt-level = "z"
|
opt-level = 3
|
||||||
debug = false
|
debug = false
|
||||||
debug-assertions = false
|
debug-assertions = false
|
||||||
overflow-checks = false
|
overflow-checks = false
|
||||||
lto = true
|
lto = true
|
||||||
incremental = false
|
incremental = false
|
||||||
codegen-units = 1
|
codegen-units = 1
|
||||||
|
|
||||||
|
|||||||
@@ -1,2 +1,22 @@
|
|||||||
# FoundryOS
|
# FoundryOS
|
||||||
|
|
||||||
|
## Cloning and building
|
||||||
|
|
||||||
|
Here are some simple steps to get started:
|
||||||
|
|
||||||
|
```sh
|
||||||
|
# If you have not yet cloned the repo:
|
||||||
|
git clone --recurse-submodules https://git.zxq5.dev/OsDev/FoundryOS.git
|
||||||
|
# If you already cloned the repo:
|
||||||
|
git submodule update --init --recursive
|
||||||
|
|
||||||
|
cargo build
|
||||||
|
# This will build the binaries if required - no need to call cargo build.
|
||||||
|
cargo run
|
||||||
|
```
|
||||||
|
|
||||||
|
### Build dependencies
|
||||||
|
|
||||||
|
* jq: checks whether the app is to be run in debugging mode.
|
||||||
|
* libisoburn: creates ISO images to be booted from.
|
||||||
|
* qemu: to run the kernel.
|
||||||
|
|||||||
+4
-1
@@ -1,5 +1,5 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "FoundryOS"
|
name = "foundry_os"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
@@ -8,8 +8,11 @@ limine = "0.3.1"
|
|||||||
lib_framebuffer = { path = "../lib/lib_framebuffer" }
|
lib_framebuffer = { path = "../lib/lib_framebuffer" }
|
||||||
lib_serial = { path = "../lib/lib_serial" }
|
lib_serial = { path = "../lib/lib_serial" }
|
||||||
lib_ascii = { path = "../lib/lib_ascii" }
|
lib_ascii = { path = "../lib/lib_ascii" }
|
||||||
|
lib_alloc = { path = "../lib/lib_alloc" }
|
||||||
x86_64 = "0.15.2"
|
x86_64 = "0.15.2"
|
||||||
spin = "0.9.8"
|
spin = "0.9.8"
|
||||||
|
pic8259 = "0.11.0"
|
||||||
|
pc-keyboard = "0.8.0"
|
||||||
|
|
||||||
[build-dependencies]
|
[build-dependencies]
|
||||||
cc = "1.2.14"
|
cc = "1.2.14"
|
||||||
|
|||||||
+2
-2
@@ -1,10 +1,10 @@
|
|||||||
|
use cc;
|
||||||
use std::process::Command;
|
use std::process::Command;
|
||||||
use std::{env, path::Path};
|
use std::{env, path::Path};
|
||||||
use cc;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Tell cargo to rerun if these files change
|
// Tell cargo to rerun if these files change
|
||||||
println!("cargo:rerun-if-changed=src");
|
println!("cargo:rerun-if-changed=src");
|
||||||
println!("cargo:rerun-if-changed=linker.ld");
|
println!("cargo:rerun-if-changed=linker.ld");
|
||||||
println!("cargo:rerun-if-changed=../config/limine.conf");
|
println!("cargo:rerun-if-changed=../config/limine.conf");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
pub mod x86_64;
|
pub mod x86_64;
|
||||||
|
|||||||
@@ -0,0 +1,69 @@
|
|||||||
|
use x86_64::{
|
||||||
|
instructions::tables::load_tss,
|
||||||
|
registers::segmentation::{Segment, CS, DS, ES, SS},
|
||||||
|
structures::{
|
||||||
|
gdt::{Descriptor, GlobalDescriptorTable, SegmentSelector},
|
||||||
|
tss::TaskStateSegment,
|
||||||
|
},
|
||||||
|
VirtAddr,
|
||||||
|
};
|
||||||
|
|
||||||
|
use spin::Lazy;
|
||||||
|
|
||||||
|
pub const DOUBLE_FAULT_1ST_INDEX: u16 = 0;
|
||||||
|
|
||||||
|
static TSS: Lazy<TaskStateSegment> = Lazy::new(|| {
|
||||||
|
let mut tss = TaskStateSegment::new();
|
||||||
|
tss.interrupt_stack_table[DOUBLE_FAULT_1ST_INDEX as usize] = {
|
||||||
|
const STACK_SIZE: usize = 4096 * 8;
|
||||||
|
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
|
||||||
|
|
||||||
|
let stack_start = VirtAddr::from_ptr(&raw const STACK);
|
||||||
|
let stack_end = stack_start + STACK_SIZE.try_into().unwrap();
|
||||||
|
stack_end
|
||||||
|
};
|
||||||
|
tss
|
||||||
|
});
|
||||||
|
|
||||||
|
static GDT: Lazy<(GlobalDescriptorTable, Selectors)> = Lazy::new(|| {
|
||||||
|
let mut gdt = GlobalDescriptorTable::new();
|
||||||
|
let code_selector = gdt.append(Descriptor::kernel_code_segment());
|
||||||
|
let data_selector = gdt.append(Descriptor::kernel_data_segment());
|
||||||
|
|
||||||
|
let user_code_selector = gdt.append(Descriptor::user_code_segment());
|
||||||
|
let user_data_selector = gdt.append(Descriptor::user_data_segment());
|
||||||
|
|
||||||
|
let tss_selector = gdt.append(Descriptor::tss_segment(&TSS));
|
||||||
|
|
||||||
|
(
|
||||||
|
gdt,
|
||||||
|
Selectors {
|
||||||
|
code_selector,
|
||||||
|
data_selector,
|
||||||
|
user_code_selector,
|
||||||
|
user_data_selector,
|
||||||
|
tss_selector,
|
||||||
|
},
|
||||||
|
)
|
||||||
|
});
|
||||||
|
|
||||||
|
struct Selectors {
|
||||||
|
code_selector: SegmentSelector,
|
||||||
|
data_selector: SegmentSelector,
|
||||||
|
user_code_selector: SegmentSelector,
|
||||||
|
user_data_selector: SegmentSelector,
|
||||||
|
tss_selector: SegmentSelector,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn init() {
|
||||||
|
GDT.0.load();
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
CS::set_reg(GDT.1.code_selector);
|
||||||
|
load_tss(GDT.1.tss_selector);
|
||||||
|
|
||||||
|
DS::set_reg(self::GDT.1.data_selector);
|
||||||
|
ES::set_reg(self::GDT.1.data_selector);
|
||||||
|
SS::set_reg(self::GDT.1.data_selector);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,22 +1,132 @@
|
|||||||
use lib_ascii::println_log;
|
use lib_ascii::{print, println_log};
|
||||||
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
|
use lib_serial::serial_println;
|
||||||
|
use x86_64::instructions::port::Port;
|
||||||
|
use x86_64::registers::control::Cr2;
|
||||||
|
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame, PageFaultErrorCode};
|
||||||
|
|
||||||
use spin::Lazy;
|
use pic8259::ChainedPics;
|
||||||
|
use spin::{Lazy, Mutex};
|
||||||
|
|
||||||
|
use super::gdt;
|
||||||
|
|
||||||
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
|
static IDT: Lazy<InterruptDescriptorTable> = Lazy::new(|| {
|
||||||
let mut idt = InterruptDescriptorTable::new();
|
let mut idt = InterruptDescriptorTable::new();
|
||||||
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
idt.breakpoint.set_handler_fn(breakpoint_handler);
|
||||||
|
|
||||||
|
unsafe {
|
||||||
|
idt.double_fault
|
||||||
|
.set_handler_fn(double_fault_handler)
|
||||||
|
.set_stack_index(gdt::DOUBLE_FAULT_1ST_INDEX);
|
||||||
|
}
|
||||||
|
|
||||||
|
idt.general_protection_fault
|
||||||
|
.set_handler_fn(general_protection_fault_handler);
|
||||||
|
idt.page_fault.set_handler_fn(page_fault_handler);
|
||||||
|
|
||||||
|
idt[InterruptIndex::Timer.as_u8()].set_handler_fn(timer_interrupt_handler);
|
||||||
|
idt[InterruptIndex::Keyboard.as_u8()].set_handler_fn(keyboard_interrupt_handler);
|
||||||
idt
|
idt
|
||||||
});
|
});
|
||||||
|
|
||||||
|
pub const PIC_1_OFFSET: u8 = 32;
|
||||||
|
pub const PIC_2_OFFSET: u8 = PIC_1_OFFSET + 8;
|
||||||
|
|
||||||
|
pub static PICS: Mutex<ChainedPics> =
|
||||||
|
Mutex::new(unsafe { ChainedPics::new(PIC_1_OFFSET, PIC_2_OFFSET) });
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
#[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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn init_idt() {
|
pub fn init_idt() {
|
||||||
IDT.load();
|
IDT.load();
|
||||||
|
unsafe {
|
||||||
|
PICS.lock().initialize();
|
||||||
|
PICS.lock().write_masks(0xfc, 0xff);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "x86-interrupt" fn breakpoint_handler(
|
extern "x86-interrupt" fn breakpoint_handler(stack_frame: InterruptStackFrame) {
|
||||||
stack_frame: InterruptStackFrame
|
serial_println!("Exception: Breakpoint\n{:#?}", stack_frame);
|
||||||
) {
|
|
||||||
println_log!("Exception: Breakpoint\n{:#?}", stack_frame);
|
println_log!("Exception: Breakpoint\n{:#?}", stack_frame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extern "x86-interrupt" fn general_protection_fault_handler(
|
||||||
|
stack_frame: InterruptStackFrame,
|
||||||
|
_error_code: u64,
|
||||||
|
) {
|
||||||
|
serial_println!("Exception: General Protection Fault\n{:#?}", stack_frame);
|
||||||
|
panic!("Exception: General Protection Fault\n{:#?}", stack_frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "x86-interrupt" fn double_fault_handler(
|
||||||
|
stack_frame: InterruptStackFrame,
|
||||||
|
_error_code: u64,
|
||||||
|
) -> ! {
|
||||||
|
serial_println!("Exception: Double Fault\n{:#?}", stack_frame);
|
||||||
|
panic!("Exception: Double Fault\n{:#?}", stack_frame);
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "x86-interrupt" fn keyboard_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
||||||
|
use pc_keyboard::{layouts, DecodedKey, HandleControl, Keyboard, ScancodeSet1};
|
||||||
|
use spin::Mutex;
|
||||||
|
use x86_64::instructions::port::Port;
|
||||||
|
|
||||||
|
static KEYBOARD: Lazy<Mutex<Keyboard<layouts::Us104Key, ScancodeSet1>>> = Lazy::new(|| {
|
||||||
|
Mutex::new(Keyboard::new(
|
||||||
|
ScancodeSet1::new(),
|
||||||
|
layouts::Us104Key,
|
||||||
|
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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "x86-interrupt" fn timer_interrupt_handler(_stack_frame: InterruptStackFrame) {
|
||||||
|
unsafe {
|
||||||
|
PICS.lock()
|
||||||
|
.notify_end_of_interrupt(InterruptIndex::Timer.as_u8());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extern "x86-interrupt" fn page_fault_handler(
|
||||||
|
stack_frame: InterruptStackFrame,
|
||||||
|
error_code: PageFaultErrorCode,
|
||||||
|
) {
|
||||||
|
serial_println!("Exception: Page Fault");
|
||||||
|
serial_println!("Accessed Address: {:?}", Cr2::read());
|
||||||
|
serial_println!("Error Code: {:?}", error_code);
|
||||||
|
serial_println!("{:#?}", stack_frame);
|
||||||
|
|
||||||
|
crate::hcf();
|
||||||
|
}
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
//! Sets up a memory map using Limine.
|
||||||
|
|
||||||
|
use limine::{
|
||||||
|
request::{HhdmRequest, KernelAddressRequest, MemoryMapRequest},
|
||||||
|
response::MemoryMapResponse,
|
||||||
|
};
|
||||||
|
use spin::Lazy;
|
||||||
|
|
||||||
|
#[used]
|
||||||
|
#[link_section = ".requests"]
|
||||||
|
static MEMORY_MAP_REQUEST: MemoryMapRequest = MemoryMapRequest::new();
|
||||||
|
|
||||||
|
#[used]
|
||||||
|
#[link_section = ".requests"]
|
||||||
|
static HIGHER_HALF_DIRECT_MAP_REQUEST: HhdmRequest = HhdmRequest::new();
|
||||||
|
|
||||||
|
/// ```rs
|
||||||
|
/// let virt_addr = phys_addr + offset;
|
||||||
|
/// let phys_addr = virt_addr - offset; // (given VA is in the HHDM). Do not use for executable code.
|
||||||
|
/// ```
|
||||||
|
pub static PHYSICAL_MEMORY_OFFSET: Lazy<u64> = Lazy::new(|| {
|
||||||
|
HIGHER_HALF_DIRECT_MAP_REQUEST
|
||||||
|
.get_response()
|
||||||
|
.unwrap()
|
||||||
|
.offset()
|
||||||
|
});
|
||||||
|
|
||||||
|
#[used]
|
||||||
|
#[link_section = ".requests"]
|
||||||
|
static KERNEL_ADDRESS_REQUEST: KernelAddressRequest = KernelAddressRequest::new();
|
||||||
|
|
||||||
|
/// Converts virtual addresses in the kernel to a physical address like this:
|
||||||
|
/// ```rs
|
||||||
|
/// let phys_addr = virt_addr - virtual_base + physical_base;
|
||||||
|
/// ```
|
||||||
|
///
|
||||||
|
/// Returns (virtual_base, physical_base)
|
||||||
|
pub static KERNEL_PHYSICAL_MEMORY_OFFSET: Lazy<(u64, u64)> = Lazy::new(|| {
|
||||||
|
let resp = KERNEL_ADDRESS_REQUEST.get_response().unwrap();
|
||||||
|
|
||||||
|
// These are base addresses, using Limine's built in page table.
|
||||||
|
(resp.virtual_base(), resp.physical_base())
|
||||||
|
});
|
||||||
|
|
||||||
|
/// Fetches the memory map from Limine.
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if the memory map was not found in MEMORY_MAP_REQUEST.
|
||||||
|
pub fn get_memory_map() -> &'static MemoryMapResponse {
|
||||||
|
if let Some(memory_map) = MEMORY_MAP_REQUEST.get_response() {
|
||||||
|
return memory_map;
|
||||||
|
} else {
|
||||||
|
unreachable!("Could not fetch memory map from Limine.")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,134 @@
|
|||||||
|
use lib_alloc::allocator::FoundryAllocator;
|
||||||
|
use limine::{memory_map::EntryType, response::MemoryMapResponse};
|
||||||
|
use x86_64::{
|
||||||
|
addr,
|
||||||
|
registers::control::Cr3,
|
||||||
|
structures::paging::{
|
||||||
|
page_table::FrameError, FrameAllocator, OffsetPageTable, PageTable, PhysFrame, Size4KiB,
|
||||||
|
},
|
||||||
|
PhysAddr, VirtAddr,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Returns a mutable reference to the current level 4 page table.
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// The caller must ensure that the level 4 page table is not modified
|
||||||
|
/// simultaneously. The caller must also ensure that the physical memory offset
|
||||||
|
/// is correct, to ensure that the correct virtual address is constructed.
|
||||||
|
unsafe fn active_l4_table(physical_memory_offset: VirtAddr) -> &'static mut PageTable {
|
||||||
|
let (level_4_frame, _) = Cr3::read();
|
||||||
|
|
||||||
|
let phys_addr = level_4_frame.start_address();
|
||||||
|
let virt = phys_addr.as_u64() + physical_memory_offset.as_u64();
|
||||||
|
&mut *(virt as *mut PageTable)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Initializes the `OffsetPageTable` for the current CPU architecture.
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// This function must be called only once and should be called before any
|
||||||
|
/// memory operations are performed that rely on virtual memory management.
|
||||||
|
/// The provided `physical_memory_offset` must be accurate to ensure correct
|
||||||
|
/// translation of physical addresses.
|
||||||
|
///
|
||||||
|
/// # Parameters
|
||||||
|
///
|
||||||
|
/// - `physical_memory_offset`: The offset to convert physical addresses to
|
||||||
|
/// virtual addresses in the higher-half direct map.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// Returns an `OffsetPageTable` that allows for manipulation of the page
|
||||||
|
/// tables for the current CPU architecture.
|
||||||
|
pub unsafe fn init(physical_memory_offset: VirtAddr) -> OffsetPageTable<'static> {
|
||||||
|
let l4_table = active_l4_table(physical_memory_offset);
|
||||||
|
OffsetPageTable::new(l4_table, physical_memory_offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct FoundryOSFrameAllocator {
|
||||||
|
memory_map: &'static MemoryMapResponse,
|
||||||
|
next: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl FoundryOSFrameAllocator {
|
||||||
|
/// Creates a new `FoundryOSFrameAllocator` from a memory map.
|
||||||
|
///
|
||||||
|
/// This function takes a reference to a `MemoryMapResponse` and initializes a
|
||||||
|
/// `FoundryOSFrameAllocator` with it. The `next` field is set to 0, indicating that
|
||||||
|
/// the first frame to be allocated is the first frame in the memory map.
|
||||||
|
pub unsafe fn init(memory_map: &'static MemoryMapResponse) -> FoundryOSFrameAllocator {
|
||||||
|
FoundryOSFrameAllocator {
|
||||||
|
memory_map,
|
||||||
|
next: 0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// An iterator over all usable frames in the memory map.
|
||||||
|
///
|
||||||
|
/// Yields one `PhysFrame` for each available 4KiB frame in the memory map.
|
||||||
|
///
|
||||||
|
/// This function is used to allocate frames for the pagemap.
|
||||||
|
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
|
||||||
|
let regions = self.memory_map.entries().iter();
|
||||||
|
let usable_regions = regions.filter(|region| region.entry_type == EntryType::USABLE);
|
||||||
|
|
||||||
|
let addr_ranges = usable_regions.map(|region| region.base..region.base + region.length);
|
||||||
|
|
||||||
|
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
||||||
|
|
||||||
|
frame_addresses.map(|addr| PhysFrame::from_start_address(PhysAddr::new(addr)).unwrap())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
unsafe impl FrameAllocator<Size4KiB> for FoundryOSFrameAllocator {
|
||||||
|
/// Allocates a frame from the list of usable frames.
|
||||||
|
///
|
||||||
|
/// This function returns the next available `PhysFrame` from the memory map,
|
||||||
|
/// if one exists. Once a frame is allocated, the internal counter is incremented
|
||||||
|
/// to point to the next frame for future allocations.
|
||||||
|
///
|
||||||
|
/// # Returns
|
||||||
|
///
|
||||||
|
/// - `Some(PhysFrame)`: If a usable frame is available.
|
||||||
|
/// - `None`: If there are no more usable frames to allocate.
|
||||||
|
|
||||||
|
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||||
|
let frame = self.usable_frames().nth(self.next);
|
||||||
|
self.next += 1;
|
||||||
|
frame
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
|
||||||
|
// translate_addr_inner(addr, physical_memory_offset)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// fn translate_addr_inner(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
|
||||||
|
// let (l4_table_frame, _) = Cr3::read();
|
||||||
|
|
||||||
|
// let table_indexes = [
|
||||||
|
// addr.p4_index(),
|
||||||
|
// addr.p3_index(),
|
||||||
|
// addr.p2_index(),
|
||||||
|
// addr.p1_index(),
|
||||||
|
// ];
|
||||||
|
// let mut frame = l4_table_frame;
|
||||||
|
|
||||||
|
// for &i in &table_indexes {
|
||||||
|
// let virt = physical_memory_offset + frame.start_address().as_u64();
|
||||||
|
// let table_ptr: *const PageTable = virt.as_ptr();
|
||||||
|
// let table = unsafe { &*table_ptr };
|
||||||
|
|
||||||
|
// let entry = &table[i];
|
||||||
|
|
||||||
|
// frame = match entry.frame() {
|
||||||
|
// Ok(frame) => frame,
|
||||||
|
// Err(FrameError::FrameNotPresent) => return None,
|
||||||
|
// Err(FrameError::HugeFrame) => panic!("huge frames are not supported!"),
|
||||||
|
// };
|
||||||
|
// }
|
||||||
|
|
||||||
|
// Some(frame.start_address() + u64::from(addr.page_offset()))
|
||||||
|
// }
|
||||||
@@ -1 +1,7 @@
|
|||||||
pub mod interrupts;
|
pub mod gdt;
|
||||||
|
|
||||||
|
pub mod interrupts;
|
||||||
|
|
||||||
|
pub mod memory;
|
||||||
|
|
||||||
|
pub(crate) mod memmap;
|
||||||
|
|||||||
+43
-10
@@ -1,19 +1,20 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
#![feature(abi_x86_interrupt)]
|
#![feature(abi_x86_interrupt)]
|
||||||
|
|
||||||
|
extern crate alloc;
|
||||||
|
|
||||||
use core::arch::asm;
|
use core::arch::asm;
|
||||||
|
use lib_alloc::allocator::init_heap;
|
||||||
use limine::request::{RequestsEndMarker, RequestsStartMarker};
|
use limine::request::{RequestsEndMarker, RequestsStartMarker};
|
||||||
use limine::BaseRevision;
|
use limine::BaseRevision;
|
||||||
|
|
||||||
pub use lib_serial::{serial_print, serial_println, serial_read};
|
|
||||||
pub use lib_ascii::{print, print_log, println, println_log, WRITER};
|
pub use lib_ascii::{print, print_log, println, println_log, WRITER};
|
||||||
|
pub use lib_serial::{serial_print, serial_println, serial_read};
|
||||||
|
use x86_64::structures::paging::Translate;
|
||||||
|
use x86_64::{PhysAddr, VirtAddr};
|
||||||
|
|
||||||
mod arch;
|
mod arch;
|
||||||
|
|
||||||
use lib_framebuffer;
|
|
||||||
|
|
||||||
|
|
||||||
/// Sets the base revision to the latest revision supported by the crate.
|
/// Sets the base revision to the latest revision supported by the crate.
|
||||||
/// See specification for further info.
|
/// See specification for further info.
|
||||||
/// Be sure to mark all limine requests with #[used], otherwise they may be removed by the compiler.
|
/// Be sure to mark all limine requests with #[used], otherwise they may be removed by the compiler.
|
||||||
@@ -32,6 +33,8 @@ static _END_MARKER: RequestsEndMarker = RequestsEndMarker::new();
|
|||||||
|
|
||||||
#[panic_handler]
|
#[panic_handler]
|
||||||
fn rust_panic(_info: &core::panic::PanicInfo) -> ! {
|
fn rust_panic(_info: &core::panic::PanicInfo) -> ! {
|
||||||
|
println!("Kernel panic: {}", _info);
|
||||||
|
serial_println!("Kernel panic: {}", _info);
|
||||||
hcf();
|
hcf();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,14 +52,44 @@ pub fn hcf() -> ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn boot() -> Result<(), &'static str> {
|
pub fn boot() -> Result<(), &'static str> {
|
||||||
|
|
||||||
if !BASE_REVISION.is_supported() {
|
if !BASE_REVISION.is_supported() {
|
||||||
return Err("base revision not supported");
|
return Err("base revision not supported");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
use arch::x86_64::{gdt, interrupts, memmap, memory};
|
||||||
|
|
||||||
|
let memory_map = memmap::get_memory_map();
|
||||||
|
|
||||||
|
print_log!(" Initialising Serial... ");
|
||||||
lib_serial::init()?;
|
lib_serial::init()?;
|
||||||
arch::x86_64::interrupts::init_idt();
|
println_log!("[Success]");
|
||||||
|
|
||||||
|
print_log!(" Setting Up Global Descriptor Table... ");
|
||||||
|
gdt::init();
|
||||||
|
println_log!("[Success]");
|
||||||
|
|
||||||
|
print_log!(" Setting Up Interrupt Descriptor Table... ");
|
||||||
|
interrupts::init_idt();
|
||||||
|
println_log!("[Success]");
|
||||||
|
|
||||||
|
print_log!(" Setting Up Page Table... ");
|
||||||
|
let mut frame_allocator = unsafe { memory::FoundryOSFrameAllocator::init(memory_map) };
|
||||||
|
println_log!("[Success]");
|
||||||
|
|
||||||
|
print_log!(" Initialising Memory Subsystem... ");
|
||||||
|
let physical_memory_offset = VirtAddr::new(*memmap::PHYSICAL_MEMORY_OFFSET);
|
||||||
|
let mut l4_table = unsafe { memory::init(physical_memory_offset) };
|
||||||
|
println_log!("[Success]");
|
||||||
|
|
||||||
|
print_log!(" Initialising Heap... ");
|
||||||
|
if let Err(e) = init_heap(&mut l4_table, &mut frame_allocator) {
|
||||||
|
return Err("Failed to initialise heap");
|
||||||
|
}
|
||||||
|
println_log!("[Success]");
|
||||||
|
|
||||||
|
print_log!(" Enabling Interrupts... ");
|
||||||
|
x86_64::instructions::interrupts::enable();
|
||||||
|
println_log!("[Success]");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-14
@@ -1,27 +1,32 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
use FoundryOS::{
|
extern crate alloc;
|
||||||
println, println_log
|
|
||||||
};
|
use alloc::vec::Vec;
|
||||||
|
|
||||||
|
use foundry_os::{println, println_log};
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
unsafe extern "C" fn kmain() -> ! {
|
unsafe extern "C" fn kmain() -> ! {
|
||||||
// All limine requests must also be referenced in a called function, otherwise they may be
|
// All limine requests must also be referenced in a called function, otherwise they may be
|
||||||
// removed by the linker.
|
// removed by the linker.
|
||||||
|
|
||||||
FoundryOS::boot();
|
println_log!(" [ Initialising Kernel Systems ] ");
|
||||||
|
if let Err(err) = foundry_os::boot() {
|
||||||
|
panic!("{}", err);
|
||||||
|
}
|
||||||
|
|
||||||
|
println_log!("[ Kernel Initialised Successfully ] ");
|
||||||
|
|
||||||
let dimensions = lib_ascii::screensize_chars();
|
let dimensions = lib_ascii::screensize_chars();
|
||||||
let dimensions2 = lib_framebuffer::screensize_px();
|
let dimensions2 = lib_framebuffer::screensize_px();
|
||||||
println_log!(" [ Initialising ] ");
|
|
||||||
|
|
||||||
x86_64::instructions::interrupts::int3();
|
|
||||||
|
|
||||||
println!("Dimensions: {}x{} (px)", dimensions2.0, dimensions2.1);
|
println!("Dimensions: {}x{} (px)", dimensions2.0, dimensions2.1);
|
||||||
println!("Dimensions: {}x{} (chars)", dimensions.0, dimensions.1);
|
println!("Dimensions: {}x{} (chars)", dimensions.0, dimensions.1);
|
||||||
|
|
||||||
println!("
|
println!(
|
||||||
|
"
|
||||||
$$$$$$$$\\ $$\\
|
$$$$$$$$\\ $$\\
|
||||||
$$ _____| $$ |
|
$$ _____| $$ |
|
||||||
$$ | $$$$$$\\ $$\\ $$\\ $$$$$$$\\ $$$$$$$ | $$$$$$\\ $$\\ $$\\
|
$$ | $$$$$$\\ $$\\ $$\\ $$$$$$$\\ $$$$$$$ | $$$$$$\\ $$\\ $$\\
|
||||||
@@ -38,11 +43,8 @@ unsafe extern "C" fn kmain() -> ! {
|
|||||||
$$ | $$ |$$\\ $$ | \\$$$ / $$ |
|
$$ | $$ |$$\\ $$ | \\$$$ / $$ |
|
||||||
$$$$$$ |\\$$$$$$ | \\$ / $$$$$$\\
|
$$$$$$ |\\$$$$$$ | \\$ / $$$$$$\\
|
||||||
\\______/ \\______/ \\_/ \\______|
|
\\______/ \\______/ \\_/ \\______|
|
||||||
");
|
"
|
||||||
|
);
|
||||||
|
|
||||||
|
loop {}
|
||||||
FoundryOS::hcf();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,8 @@
|
|||||||
"--no-pie",
|
"--no-pie",
|
||||||
"--gc-sections",
|
"--gc-sections",
|
||||||
"--build-id=none",
|
"--build-id=none",
|
||||||
"-z", "max-page-size=0x1000"
|
"-z",
|
||||||
|
"max-page-size=0x1000"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Submodule
+1
Submodule lib/lib_alloc added at af814bf2ab
+1
-1
Submodule lib/lib_application updated: df1cbd1170...4b1c60676a
+1
-1
Submodule lib/lib_ascii updated: fefe217980...70d528b2e8
+1
-1
Submodule lib/lib_framebuffer updated: c515b89136...53553278d1
+1
-1
Submodule lib/lib_serial updated: 92032eb5b4...ed2fa6b501
+1
-1
@@ -85,7 +85,7 @@ mkdir -p "$iso_root/EFI/BOOT"
|
|||||||
if [ ! -d "$build_dir/limine" ]; then
|
if [ ! -d "$build_dir/limine" ]; then
|
||||||
compiling "limine bootloader"
|
compiling "limine bootloader"
|
||||||
cd "$build_dir"
|
cd "$build_dir"
|
||||||
git clone https://github.com/limine-bootloader/limine.git --branch=v8.x-binary --depth=1 "$build_dir/limine" || error "failed to clone limine"
|
git clone https://github.com/limine-bootloader/limine.git --branch=v9.x-binary --depth=1 "$build_dir/limine" || error "failed to clone limine"
|
||||||
make -C "$build_dir/limine" || error "failed to build limine"
|
make -C "$build_dir/limine" || error "failed to build limine"
|
||||||
cd "$project_root"
|
cd "$project_root"
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user