Some work on GDT setup

This commit is contained in:
River 2025-05-19 10:16:51 -04:00
parent 04d953fba0
commit 154c9512dc
Signed by: shibedrill
GPG Key ID: 5FE0CB25945EFAA2
12 changed files with 148 additions and 58 deletions

43
Cargo.lock generated
View File

@ -100,11 +100,22 @@ dependencies = [
"acpi", "acpi",
"enumflags2", "enumflags2",
"flagset", "flagset",
"lazy_static",
"limine", "limine",
"lzma-rs", "lzma-rs",
"once_cell", "once_cell",
"spin", "spin 0.10.0",
"talc", "talc",
"x86_64",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
dependencies = [
"spin 0.9.8",
] ]
[[package]] [[package]]
@ -182,12 +193,24 @@ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]]
name = "rustversion"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.2.0" version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "spin"
version = "0.9.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6980e8d7511241f8acf4aebddbb1ff938df5eebe98691418c4468d0b72a96a67"
[[package]] [[package]]
name = "spin" name = "spin"
version = "0.10.0" version = "0.10.0"
@ -222,3 +245,21 @@ name = "unicode-ident"
version = "1.0.18" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]]
name = "volatile"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "442887c63f2c839b346c192d047a7c87e73d0689c9157b00b53dcc27dd5ea793"
[[package]]
name = "x86_64"
version = "0.15.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0f042214de98141e9c8706e8192b73f56494087cc55ebec28ce10f26c5c364ae"
dependencies = [
"bit_field",
"bitflags",
"rustversion",
"volatile",
]

View File

@ -7,12 +7,16 @@ edition = "2024"
acpi = "5.1.0" acpi = "5.1.0"
enumflags2 = "0.7.11" enumflags2 = "0.7.11"
flagset = "0.4.7" flagset = "0.4.7"
lazy_static = { version = "1.5.0", default-features = false, features = ["spin_no_std"] }
limine = "0.4.0" limine = "0.4.0"
lzma-rs = { git = "https://github.com/glaeqen/lzma-no-std-rs/", version = "0.2.0", default-features = false } lzma-rs = { git = "https://github.com/glaeqen/lzma-no-std-rs/", version = "0.2.0", default-features = false }
once_cell = { version = "1.20.3", default-features = false, features = ["alloc", "critical-section"] } once_cell = { version = "1.20.3", default-features = false, features = ["alloc", "critical-section"] }
spin = "0.10.0" spin = "0.10.0"
talc = "4.4.2" talc = "4.4.2"
[target.'cfg(target_arch = "x86_64")'.dependencies]
x86_64 = "0.15.2"
[lib] [lib]
name = "gila" name = "gila"
path = "src/lib/lib.rs" path = "src/lib/lib.rs"

View File

@ -62,7 +62,7 @@ args = ["-rf", "build/initramfs", "build/initramfs.tar.lzma"]
condition = { files_modified = { input = ["src/lib/**/*.rs"], output = ["${ARTIFACTDIR}/libgila.rlib"] }, fail_message = "(inputs unchanged)" } condition = { files_modified = { input = ["src/lib/**/*.rs"], output = ["${ARTIFACTDIR}/libgila.rlib"] }, fail_message = "(inputs unchanged)" }
dependencies = ["prepare"] dependencies = ["prepare"]
command = "cargo" command = "cargo"
args = ["build", "--lib"] args = ["build", "--profile", "${PROFILE}", "--lib"]
[tasks.kernel] [tasks.kernel]
condition = { files_modified = { input = [ condition = { files_modified = { input = [

View File

@ -46,7 +46,8 @@ Licensed under the GNU Public License v3. See [LICENSE](LICENSE) for details.
- [kernel/](src/kernel/): Kernel-specific code. - [kernel/](src/kernel/): Kernel-specific code.
- [arch/](src/kernel/arch/): Architecture specific features like the display, serial, and interrupts. Each architecture is a subfolder, containing a file or module for each feature. - [arch/](src/kernel/arch/): Architecture specific features like the display, serial, and interrupts. Each architecture is a subfolder, containing a file or module for each feature.
- [boot.rs](src/kernel/boot.rs): Handles bootloader-managed data structures. Gila uses Limine. Other bootloaders are NOT supported. - [boot.rs](src/kernel/boot.rs): Handles bootloader-managed data structures. Gila uses Limine. Other bootloaders are NOT supported.
- [log.rs](src/kernel/log.rs): Logging structures and singletons for logging to serial or the display. - [constants.rs](src/kernel/constants.rs): Constants referenced elsewhere in the kernel.
- [log.rs](src/kernel/log.rs): Logging structures, macros, and singletons for logging to serial or the display.
- [main.rs](src/kernel/main.rs): The entry point that gets called by the bootloader. - [main.rs](src/kernel/main.rs): The entry point that gets called by the bootloader.
- [memory.rs](src/kernel/memory.rs): Types relating to memory regions and allocation. - [memory.rs](src/kernel/memory.rs): Types relating to memory regions and allocation.
- [panic.rs](src/kernel/panic.rs): The panic handler and associated functionality. - [panic.rs](src/kernel/panic.rs): The panic handler and associated functionality.
@ -100,13 +101,13 @@ Kernel parameters are passed as part of the `cmdline` through [limine.conf](limi
List of current extant kernel parameters: List of current extant kernel parameters:
- `-loglevel`: Can be a number or string corresponding to a log level. Only one value supported. - `-loglevel`: Can be a number or string corresponding to a log level. Only one value supported. Current options are `Disabled`, `Trace`, `Info`, `Warning`, `Error`, and `Critical`.
- `-logdev`: A sequence of one or more values representing devices to log to. Current options are `display` and `serial`. - `-logdev`: A sequence of one or more values representing devices to log to. Current options are `display` and `serial`.
- `-initramfs`: A valid path to a module to serve as the initramfs (containing the init binary). Only one value supported. - `-initramfs`: A valid path to a module to serve as the initramfs (containing the init binary). Only one value supported.
The default behavior for each parameter, when not supplied, is: The default behavior for each parameter, when not supplied, is:
`-loglevel=Trace -initramfs=/boot/initramfs.tar.lzma` `-loglevel=Info -initramfs=/boot/initramfs.tar.lzma`
## Credits ## Credits

View File

@ -4,19 +4,19 @@
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
pub mod x86_64; pub mod x86_64;
#[cfg(target_arch = "x86_64")] #[cfg(target_arch = "x86_64")]
pub use x86_64 as current; pub use x86_64::asm;
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
pub mod aarch64; pub mod aarch64;
#[cfg(target_arch = "aarch64")] #[cfg(target_arch = "aarch64")]
pub use aarch64 as current; pub use aarch64::asm;
#[cfg(target_arch = "riscv64")] #[cfg(target_arch = "riscv64")]
pub mod riscv64; pub mod riscv64;
#[cfg(target_arch = "riscv64")] #[cfg(target_arch = "riscv64")]
pub use riscv64 as current; pub use riscv64::asm;
#[cfg(target_arch = "loongarch64")] #[cfg(target_arch = "loongarch64")]
pub mod loongarch64; pub mod loongarch64;
#[cfg(target_arch = "loongarch64")] #[cfg(target_arch = "loongarch64")]
pub use loongarch64 as current; pub use loongarch64::asm;

View File

@ -6,6 +6,18 @@
use core::arch::asm; use core::arch::asm;
pub unsafe fn interrupt_disable() {
unsafe {
asm!("cli");
}
}
pub unsafe fn interrupt_enable() {
unsafe {
asm!("sti");
}
}
pub unsafe fn halt() { pub unsafe fn halt() {
unsafe { unsafe {
asm!("hlt"); asm!("hlt");

View File

@ -0,0 +1,23 @@
use x86_64::structures::gdt::{Descriptor, GlobalDescriptorTable};
use lazy_static::lazy_static;
use super::asm::*;
lazy_static! {
pub static ref GDT: GlobalDescriptorTable = {
let mut gdt = GlobalDescriptorTable::new();
gdt.append(Descriptor::kernel_code_segment());
gdt
};
}
pub fn setup_gdt() {
unsafe {
interrupt_disable();
}
GDT.load();
unsafe {
interrupt_enable();
}
}

View File

@ -3,4 +3,5 @@
pub mod acpi; pub mod acpi;
pub mod asm; pub mod asm;
pub mod gdt;
pub mod serial; pub mod serial;

View File

@ -1,7 +1,8 @@
// Copyright (c) 2025 shibedrill // Copyright (c) 2025 shibedrill
// SPDX-License-Identifier: GPL-3.0-or-later // SPDX-License-Identifier: GPL-3.0-or-later
use crate::{asm::*, log::LogSubscriber}; use crate::arch::asm::*;
use crate::log::LogSubscriber;
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct Serialport { pub struct Serialport {

View File

@ -1,9 +1,39 @@
use crate::format;
use crate::log::LogLevel; use crate::log::LogLevel;
use crate::memory::alloc::string::String;
pub static INITRAMFS_DEFAULT_PATH: &str = "/boot/initramfs.tar.lzma"; pub static INITRAMFS_DEFAULT_PATH: &str = "/boot/initramfs.tar.lzma";
pub static LOG_DEFAULT_LEVEL: LogLevel = LogLevel::Info; pub static LOG_DEFAULT_LEVEL: LogLevel = LogLevel::Info;
pub static KERNEL_BUILD_PROFILE: &str = {
if cfg!(debug_assertions) {
"debug"
} else {
"release"
}
};
pub static KERNEL_ARCH: &str = {
if cfg!(target_arch = "x86_64") {
"x86_64"
} else if cfg!(target_arch = "aarch64") {
"aarch64"
} else if cfg!(target_arch = "loongarch64") {
"loongarch64"
} else if cfg!(target_arch = "riscv64") {
"riscv64"
} else {
"unsupported"
}
};
pub fn kernel_version_string() -> String {
format!(
"{KERNEL_VERSION_MAJOR}.{KERNEL_VERSION_MINOR}.{KERNEL_VERSION_PATCH}-{KERNEL_ARCH}-{KERNEL_BUILD_PROFILE}"
)
}
// FUCKING ADD CONST UNWRAP!!! // FUCKING ADD CONST UNWRAP!!!
pub static KERNEL_VERSION_MAJOR: u8 = match u8::from_str_radix(env!("CARGO_PKG_VERSION_MAJOR"), 10) pub static KERNEL_VERSION_MAJOR: u8 = match u8::from_str_radix(env!("CARGO_PKG_VERSION_MAJOR"), 10)
{ {

View File

@ -3,6 +3,7 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
#![feature(cfg_match)]
#![feature(allocator_api)] #![feature(allocator_api)]
mod arch; mod arch;
@ -17,7 +18,9 @@ mod process;
mod resources; mod resources;
mod syscall_runner; mod syscall_runner;
use arch::current::*; use core::ptr;
use arch::x86_64::gdt::{self, GDT};
use arch::x86_64::serial::Serialport; use arch::x86_64::serial::Serialport;
use boot::*; use boot::*;
use constants::*; use constants::*;
@ -72,9 +75,7 @@ unsafe extern "C" fn main() -> ! {
"Boot: Kernel file path: {}", "Boot: Kernel file path: {}",
String::from_utf8_lossy(executable_file_response.file().path().to_bytes()) String::from_utf8_lossy(executable_file_response.file().path().to_bytes())
); );
log_info!( log_info!("Boot: Booting gila version {}", kernel_version_string());
"Boot: Booting gila version {KERNEL_VERSION_MAJOR}.{KERNEL_VERSION_MINOR}.{KERNEL_VERSION_PATCH}"
);
log_info!("Boot: Trans rights!"); log_info!("Boot: Trans rights!");
@ -110,10 +111,14 @@ unsafe extern "C" fn main() -> ! {
module_response.modules().len() module_response.modules().len()
); );
if !module_response.modules().is_empty() { if !module_response.modules().is_empty() {
log_trace!("Boot: Kernel modules list:"); let mut log_msg: String = String::from("Boot: Kernel modules list:\n");
for module in module_response.modules() { for module in module_response.modules() {
log_trace!("\t{}", String::from_utf8_lossy(module.path().to_bytes())); log_msg.push_str(&format!(
"\t\t{}",
String::from_utf8_lossy(module.path().to_bytes())
));
} }
log_trace!("{log_msg}")
} }
let irfs_path = { let irfs_path = {
@ -140,14 +145,14 @@ unsafe extern "C" fn main() -> ! {
if let Some(mmap_response) = MEMMAP_REQUEST.get_response() { if let Some(mmap_response) = MEMMAP_REQUEST.get_response() {
log_info!("Boot: Memory map received"); log_info!("Boot: Memory map received");
if !mmap_response.entries().is_empty() { if !mmap_response.entries().is_empty() {
log_trace!("Boot: Memory map entries list:"); let mut log_msg: String = String::from("Boot: Memory map:");
let mut usable: u64 = 0; let mut usable: u64 = 0;
let mut reclaimable: u64 = 0; let mut reclaimable: u64 = 0;
let mut hardware: u64 = 0; let mut hardware: u64 = 0;
let mut unusable: u64 = 0; let mut unusable: u64 = 0;
for entry in mmap_response.entries() { for entry in mmap_response.entries() {
log_trace!( log_msg.push_str(&format!(
"\t{} bytes @ 0x{:x}: {}", "\n\t\t0x{:x} bytes @ 0x{:x}: {}",
entry.length, entry.length,
entry.base, entry.base,
match entry.entry_type { match entry.entry_type {
@ -186,17 +191,23 @@ unsafe extern "C" fn main() -> ! {
} }
_ => "unidentified", _ => "unidentified",
} }
) ));
} }
log_trace!("{log_msg}");
let total = usable + reclaimable + hardware + unusable;
log_info!( log_info!(
"Boot: Memory report:\n\tFree: {usable}\n\tAvailable: {reclaimable}\n\tUsable: {}\n\tHardware: {hardware}\n\tUnusable: {unusable}\n\tTotal: {}", "Boot: Memory report:\n\t\tFree: {usable}\n\t\tAvailable: {reclaimable}\n\t\tUsable: {}\n\t\tHardware: {hardware}\n\t\tUnusable: {unusable}\n\t\tTotal: {}",
usable + reclaimable, usable + reclaimable,
usable + reclaimable + hardware + unusable total
); );
} }
} }
panic!("Test panic"); log_trace!(
"GDT: Setting up global descriptor table at 0x{:x}",
ptr::addr_of!(GDT) as usize
);
gdt::setup_gdt();
#[allow(unreachable_code)] #[allow(unreachable_code)]
loop { loop {

View File

@ -2,13 +2,12 @@
use enumflags2::{BitFlags, bitflags, make_bitflags}; use enumflags2::{BitFlags, bitflags, make_bitflags};
use crate::arch::current; use crate::arch::asm;
use crate::memory::MemoryRegion; use crate::memory::MemoryRegion;
use crate::memory::MemoryRegionFlags; use crate::memory::MemoryRegionFlags;
use crate::memory::alloc; use crate::memory::alloc;
use alloc::string::String; use alloc::string::String;
use alloc::vec::Vec; use alloc::vec::Vec;
use current::asm;
#[allow(dead_code)] #[allow(dead_code)]
pub struct ProcessTable {} pub struct ProcessTable {}
@ -33,10 +32,6 @@ pub struct Process {
mem_region: MemoryRegion, mem_region: MemoryRegion,
/// Process priority. Lower number is higher priority. /// Process priority. Lower number is higher priority.
prio: u16, prio: u16,
/// Which semaphor the process is waiting on.
semaphor_wait: Option<u32>,
/// What capabilities the process has
capabilities: BitFlags<ProcessCapabilities>,
} }
#[allow(dead_code)] #[allow(dead_code)]
@ -57,35 +52,6 @@ pub enum ProcessState {
Suspended, Suspended,
} }
#[bitflags]
#[repr(u32)]
#[derive(Copy, Clone)]
pub enum ProcessCapabilities {
// Process capabilities
ProcessEnum, // Enumerate running process IDs and names
ProcessRead, // Read information from a process struct
ProcessKill, // Kill any process
ProcessSpawn, // Create a new process
ProcessExec, // Replace self with new process image
ProcessSession, // Create and accept IPC requests.
// Capability meta
CapabilityRead, // Inspect a process's capabilities
CapabilityAdd, // Add a capability to a process
// Hardware access capabilities
HardwareWrite, // Write to memory-mapped IO
HardwareRead, // Read from memory-mapped IO
HardwareLock, // Obtain exclusive access to a device
HardwareQuery, // Check on the lock status of a device
// Kernel config capabilities
KernelCfgRead, // Read kernel configurations
KernelCfgWrite, // Modify kernel configurations
KernelProtCall, // Call protected kernel functions
// Server resolution capabilities
ServerEnum, // Enumerate server processes
ServerGet, // Get the PID of a specific server for IPC.
ServerRegister, // Register a process as a server.
}
// Interprocess communication system: // Interprocess communication system:
// Processes communicate through "sessions" which are mediated by the kernel. // Processes communicate through "sessions" which are mediated by the kernel.
// Sessions can only be opened if two processes both request a session with // Sessions can only be opened if two processes both request a session with