Skip to content

Commit

Permalink
Merge pull request #6 from InfiniteCoder01/master
Browse files Browse the repository at this point in the history
Paging basically done
  • Loading branch information
jaxxcoder authored Feb 24, 2025
2 parents 0cc137a + da0b1db commit f3743d4
Show file tree
Hide file tree
Showing 29 changed files with 905 additions and 316 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ panic ="abort"

[dependencies]
thiserror = { version = "2.0.9", default-features = false }
lock_api = "0.4.12"
spin = "0.9.8"

bitfield-struct = "0.10.0"
bitflags = "2.6.0"

memory_addr = "0.3.1"
lock_api = "0.4.12"
lock_free_buddy_allocator = "0.1.0"
talc = "4.4.2"

[target.'cfg(any(target_arch = "x86_64", target_arch = "x86"))'.dependencies]
multiboot2 = { version = "0.23.1", default-features = false }
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,8 @@ Then, run the build script by issuing `./build.sh`
| x86 (i486) | Works | |
| x86 (i386) | Unsupported | TLB flushing |
| x86_64 | TODO | |

## Help!!!
Here are some things you could help with:
- Find a way to make freeing in ZonedBuddy page allocator checked (you can free a random page rigth now and it won't even panic)
- Find a way to also check freeing in NestedPageTable
Binary file modified a.out
Binary file not shown.
3 changes: 3 additions & 0 deletions bochsrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
ata0-slave: type=cdrom, path=bin/os.iso, status=inserted
boot: cdrom
magic_break: enabled=1
21 changes: 15 additions & 6 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

# If this is not your path, kindly change it
export CROSS_CC="${CROSS_CC:-$HOME/opt/cross/bin/i686-elf-gcc}"
export ARCH=x86/x32
export QEMU_SYSTEM=i386
export ARCH="${ARCH:-x86/x32}"
export QEMU_SYSTEM="${QEMU_SYSTEM:-i386}"
export EMULATOR="${EMULATOR:-qemu}"

# Colors
if command -v tput &> /dev/null; then
Expand All @@ -27,7 +28,7 @@ fi
# Utils
build() {
if ! cargo build; then
return -1
return 1
fi

KERNEL="target/target/debug/satan"
Expand All @@ -44,12 +45,20 @@ build() {
}

run() {
qemu-system-$QEMU_SYSTEM -d guest_errors -no-reboot -cdrom bin/os.iso
if [ $EMULATOR = "bochs" ]; then
bochs -q
else
qemu-system-$QEMU_SYSTEM -d guest_errors -no-reboot -cdrom bin/os.iso
fi
}

debug() {
qemu-system-$QEMU_SYSTEM -d guest_errors -no-reboot -cdrom bin/os.iso -s -S &
rust-gdb target/target/debug/satan -x gdbinit
if [ $EMULATOR = "bochs" ]; then
bochs -q
else
qemu-system-$QEMU_SYSTEM -d guest_errors -no-reboot -cdrom bin/os.iso -s -S &
rust-gdb target/target/debug/satan -x gdbinit
fi
}

clean() {
Expand Down
2 changes: 1 addition & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{
devShell = pkgs.mkShell {
buildInputs = with pkgs; [
qemu
qemu bochs
libisoburn mtools
pkgs-crosssystem.buildPackages.grub2
pkgs-crosssystem.buildPackages.gcc
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "nightly-2024-12-11"
channel = "nightly"
components = ["rust-src", "rust-analyzer"]
16 changes: 15 additions & 1 deletion src/arch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,18 @@
/// x86 and x86_64 architectures
pub mod x86;
#[cfg(any(target_arch = "x86_64", target_arch = "x86"))]
pub use x86::*;
pub use x86 as current;

// * Stub imports to make it easier to see required functions and types

pub use current::{_panic, _print};

/// Instructions like cpuid
pub mod instructions {
pub use super::current::instructions::cpu_id;
}

/// Interrupt handling
pub mod interrupts {
pub use super::current::interrupts::{disable, enable};
}
4 changes: 4 additions & 0 deletions src/arch/x86/instructions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/// Get a CPU identifier
pub fn cpu_id() -> usize {
0
}
20 changes: 14 additions & 6 deletions src/arch/x86/interrupts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,24 +31,32 @@ struct InterruptStackFrame {

/// Central interrupt handler, all interrupts come here specifying an interrupt number
fn interrupt_handler(interrupt: u8, error_code: usize) {
if interrupt == 0x20 {
// Timer
return;
}
if interrupt == 0x0E {
// Page fault
crate::println!("Page fault!\nError code:\n{:#032b}", error_code);
crate::println!(" ^ ^^IRUWP");
crate::println!(" SGX SSPK ");
return;
}
if interrupt == 0x20 {
// Timer
return;
panic!("Halt");
}
if interrupt == 0x21 {
// Keyboard
let scancode = u8::read(0x60);
crate::println!("Keyboard: {}", scancode);
return;
}

if interrupt <= 0x08 {
panic!("Double fault!!!\nError code: {:#x}", error_code);
}
if interrupt <= 0x1F {
panic!(
"Unhandled exception: {:#x}\nError code: {:#x}",
interrupt, error_code
);
}
loop {}
}

Expand Down
27 changes: 23 additions & 4 deletions src/arch/x86/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,42 @@
#[cfg(target_arch = "x86")]
core::arch::global_asm!(include_str!("x32/bootstrap.S"), options(att_syntax));

/// Instructions like cpuid
pub mod instructions;

/// Early logging facilities
pub mod early_logger;
pub use early_logger::{_panic, _print};

/// Interrupts and IDT
pub mod interrupts;

/// Paging implementation
/// I spent a lot of time here
/// I spent a lot of time here.
/// And I hate every single second of it.
pub mod paging;

mod allocator {
const SIZE: usize = 0x1000;
static mut ARENA: [u8; SIZE] = [0; SIZE];

// TODO: Use system allocator on OOM
#[global_allocator]
static ALLOCATOR: talc::Talck<spin::Mutex<()>, talc::ClaimOnOom> = talc::Talc::new(unsafe {
// if we're in a hosted environment, the Rust runtime may allocate before
// main() is called, so we need to initialize the arena automatically
talc::ClaimOnOom::new(talc::Span::from_slice(core::ptr::addr_of_mut!(ARENA)))
})
.lock();
}

/// Kernel setup function. First thing that is called
/// after assembly bootstrap setus up GDT and higher-half address space
#[no_mangle]
pub extern "cdecl" fn ksetup(mb_magic: u32, mbi_ptr: u32) -> ! {
// loop {}
crate::println!("Hello, SATAN!");
interrupts::setup();

let boot_info = if mb_magic == multiboot2::MAGIC {
let boot_info = unsafe {
multiboot2::BootInformation::load(mbi_ptr as *const multiboot2::BootInformationHeader)
Expand All @@ -34,8 +55,6 @@ pub extern "cdecl" fn ksetup(mb_magic: u32, mbi_ptr: u32) -> ! {
);
};

crate::println!("Hello, SATAN!");
interrupts::setup();
paging::setup_paging(&boot_info);

loop {}
Expand Down
Loading

0 comments on commit f3743d4

Please sign in to comment.