Skip to content

Commit

Permalink
Kernel: Fix kernel (typo) issue with s2s header
Browse files Browse the repository at this point in the history
  • Loading branch information
corigan01 committed Jan 6, 2025
1 parent 8d32680 commit 3c103f8
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 47 deletions.
9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,15 @@ overflow-checks = true
[profile.stage-64bit]
inherits = "release"
panic = "abort"
opt-level="z"
strip = false
lto = true
debug = 0
overflow-checks = true

[profile.kernel]
inherits = "release"
panic = "abort"
strip = true
debug = 1
overflow-checks = true
4 changes: 2 additions & 2 deletions bootloader/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ use mem::phys::PhysMemoryMap;
pub const MEMORY_REGIONS: usize = 64;

/// Kernel fn ptr
pub type KernelEntryFn = fn(u64) -> !;
pub type KernelEntryFn = extern "C" fn(u64) -> !;

/// # Max Memory Map Entries
/// This is the max number of entries that can fit in the Stage-to-Stage info block.
Expand Down Expand Up @@ -68,7 +68,7 @@ pub struct Stage32toStage64 {
}

/// # `Stage64` to `Kernel` Info Block
#[repr(C)]
#[derive(Debug)]
pub struct KernelBootHeader {
pub phys_mem_map: &'static PhysMemoryMap<MEMORY_REGIONS>,
pub video_mode: (VesaModeId, VesaMode),
Expand Down
15 changes: 11 additions & 4 deletions bootloader/stage-64bit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ fn main(stage_to_stage: &Stage32toStage64) {
log!("Loading ELF (");
elf.load_into(|h| {
if h.segment_kind() != SegmentKind::Load {
log!("X");
return None;
}
log!(".");
Expand All @@ -109,14 +110,14 @@ fn main(stage_to_stage: &Stage32toStage64) {
let mm = &mut *MEMORY_MAP.get();
let s2k = &mut *KERNEL_INFO.get();

s2k.replace(KernelBootHeader {
*s2k = Some(KernelBootHeader {
phys_mem_map: mm,
video_mode: stage_to_stage.video_mode,
});

jmp_to_kernel(
virt_info.exe_start_virt as *const KernelEntryFn,
virt_info.stack_start_virt,
virt_info.stack_end_virt,
s2k.as_ref().unwrap(),
);
}
Expand All @@ -127,11 +128,17 @@ unsafe fn jmp_to_kernel(
kernel_stack_ptr: u64,
s2k: &'static KernelBootHeader,
) -> ! {
logln!(
"Kernel \n - EXE : {:#016x}\n - STACK : {:#016x}\n - S2K : {:#016x}",
fn_ptr as u64,
kernel_stack_ptr,
s2k as *const _ as u64
);
unsafe {
asm!(
"mov rsp, {stack}",
"jmp {kern:r}",
in("rdi") &raw const s2k,
"call {kern:r}",
in("rdi") s2k,
kern = in(reg) fn_ptr,
stack = in(reg) kernel_stack_ptr
);
Expand Down
18 changes: 9 additions & 9 deletions bootloader/stage-64bit/src/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,19 @@ static TABLE_LVL2_ID: SyncUnsafeCell<[PageMapLvl2; IDMAP_GIG_AMOUNT]> =
static TABLE_LVL3_KERN: SyncUnsafeCell<PageMapLvl3> = SyncUnsafeCell::new(PageMapLvl3::new());
static TABLE_LVL2_KERN: SyncUnsafeCell<PageMapLvl2> = SyncUnsafeCell::new(PageMapLvl2::new());

#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct PageTableConfig {
pub kernel_exe_phys: (u64, usize),
pub kernel_stack_phys: (u64, usize),
pub kernel_virt: u64,
}

#[derive(Debug)]
#[derive(Debug, Copy, Clone)]
pub struct KernelVirtInfo {
pub exe_start_virt: u64,
pub exe_end_virt: u64,
pub stack_start_virt: u64,
pub _stack_end_virt: u64,
pub _stack_start_virt: u64,
pub stack_end_virt: u64,
}

impl KernelVirtInfo {
Expand All @@ -77,8 +77,8 @@ impl KernelVirtInfo {
pub fn _stack_slice(&mut self) -> &'static mut [u8] {
unsafe {
core::slice::from_raw_parts_mut(
self.stack_start_virt as *mut u8,
(self._stack_end_virt - self.stack_start_virt) as usize,
self._stack_start_virt as *mut u8,
(self.stack_end_virt - self._stack_start_virt) as usize,
)
}
}
Expand Down Expand Up @@ -168,15 +168,15 @@ pub fn build_page_tables(c: PageTableConfig) -> KernelVirtInfo {
let lvl4_entry = PageEntryLvl4::new()
.set_present_flag(true)
.set_read_write_flag(true)
.set_next_entry_phy_address(unsafe { (*TABLE_LVL3_ID.get()).table_ptr() });
.set_next_entry_phy_address(unsafe { (*TABLE_LVL3_KERN.get()).table_ptr() });

unsafe { (*TABLE_LVL4.get()).store(lvl4_entry, tbl4_offset) };

KernelVirtInfo {
exe_start_virt: c.kernel_virt,
exe_end_virt: c.kernel_virt + (exe_pages * PAGE_2M) as u64,
stack_start_virt: c.kernel_virt + ((exe_pages + 1) * PAGE_2M) as u64,
_stack_end_virt: c.kernel_virt + ((exe_pages + stack_pages + 1) * PAGE_2M) as u64,
_stack_start_virt: c.kernel_virt + ((exe_pages + 1) * PAGE_2M) as u64,
stack_end_virt: c.kernel_virt + ((exe_pages + stack_pages + 1) * PAGE_2M) as u64,
}
}

Expand Down
10 changes: 5 additions & 5 deletions crates/arch/src/paging64.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ impl PageMapLvl1 {
if addr > Self::SIZE_FOR_TABLE {
None
} else {
Some(((addr.saturating_sub(1)) / Self::SIZE_PER_INDEX) as usize + 1)
Some((addr / Self::SIZE_PER_INDEX) as usize)
}
}

Expand Down Expand Up @@ -410,7 +410,7 @@ impl PageMapLvl2 {
if addr > Self::SIZE_FOR_TABLE {
None
} else {
Some(((addr.saturating_sub(1)) / Self::SIZE_PER_INDEX) as usize + 1)
Some((addr / Self::SIZE_PER_INDEX) as usize)
}
}

Expand Down Expand Up @@ -443,7 +443,7 @@ impl PageMapLvl3 {
if addr > Self::SIZE_FOR_TABLE {
None
} else {
Some(((addr.saturating_sub(1)) / Self::SIZE_PER_INDEX) as usize + 1)
Some((addr / Self::SIZE_PER_INDEX) as usize)
}
}

Expand Down Expand Up @@ -476,7 +476,7 @@ impl PageMapLvl4 {
if addr > Self::SIZE_FOR_TABLE {
None
} else {
Some(((addr.saturating_sub(1)) / Self::SIZE_PER_INDEX) as usize + 1)
Some((addr / Self::SIZE_PER_INDEX) as usize)
}
}

Expand Down Expand Up @@ -505,7 +505,7 @@ impl PageMapLvl5 {
if addr > Self::SIZE_FOR_TABLE {
None
} else {
Some(((addr.saturating_sub(1)) / Self::SIZE_PER_INDEX) as usize + 1)
Some((addr / Self::SIZE_PER_INDEX) as usize)
}
}

Expand Down
1 change: 0 additions & 1 deletion kernel/rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
[toolchain]
channel = "nightly"
target = ["x86_64-unknown-none"]
components = ["rustfmt", "clippy", "rust-src", "llvm-tools-preview"]
13 changes: 7 additions & 6 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
\___\_\_,_/\_,_/_//_/\__/\_,_/_/_/_/ /_/|_|\__/_/ /_//_/\__/_/
Part of the Quantum OS Kernel
Copyright 2024 Gavin Kellam
Copyright 2025 Gavin Kellam
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
associated documentation files (the "Software"), to deal in the Software without restriction,
Expand All @@ -23,8 +23,9 @@ DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#![no_main]
#![no_std]
#![no_main]
#![feature(sync_unsafe_cell)]

mod panic;

Expand All @@ -38,13 +39,13 @@ make_debug! {

#[unsafe(no_mangle)]
#[unsafe(link_section = ".start")]
extern "C" fn _start(stage_to_stage: u64) {
main(unsafe { &(*(stage_to_stage as *const KernelBootHeader)) });
extern "C" fn _start(stage_to_stage: u64) -> ! {
main(unsafe { &*(stage_to_stage as *const KernelBootHeader) });
panic!("Main should not return");
}

#[debug_ready]
fn main(_stage_to_stage: &KernelBootHeader) {
logln!("Kernel!");
fn main(stage_to_stage: &KernelBootHeader) {
logln!("{:#?}", stage_to_stage);
loop {}
}
48 changes: 30 additions & 18 deletions kernel/x86-64-quantum_kernel.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
{
"llvm-target": "x86_64-unknown-none",
"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",
"arch": "x86_64",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"os": "none",
"executables": true,
"linker-flavor": "ld.lld",
"linker": "rust-lld",
"panic-strategy": "abort",
"disable-redzone": true,
"features": "-mmx,-sse,+soft-float,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2",
"pre-link-args": {
"ld.lld": [
""
]
}
"arch": "x86_64",
"code-model": "kernel",
"cpu": "x86-64",
"crt-objects-fallback": "false",
"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",
"disable-redzone": true,
"features": "-mmx,-sse,-sse2,-sse3,-ssse3,-sse4.1,-sse4.2,-avx,-avx2,+soft-float",
"linker": "rust-lld",
"linker-flavor": "gnu-lld",
"llvm-target": "x86_64-unknown-none-elf",
"max-atomic-width": 64,
"metadata": {
"description": "Freestanding/bare-metal x86_64 softfloat",
"host_tools": false,
"std": false,
"tier": 2
},
"panic-strategy": "abort",
"plt-by-default": false,
"position-independent-executables": false,
"relro-level": "full",
"stack-probes": {
"kind": "inline"
},
"static-position-independent-executables": false,
"supported-sanitizers": [
"kcfi",
"kernel-address"
],
"target-pointer-width": "64"
}
12 changes: 10 additions & 2 deletions meta/src/artifacts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ enum ArchSelect {
I686,
/// # Intel IA-32A (64bit mode)
X64,
/// # Intel IA-32A (64bit mode)
Kernel,
}

impl Display for ArchSelect {
Expand All @@ -50,6 +52,12 @@ impl Display for ArchSelect {
.join("linkerscripts/x86-64-quantum_loader.json")
.to_string_lossy(),
)),
Self::Kernel => f.write_fmt(format_args!(
"{}",
current_dir
.join("../kernel/x86-64-quantum_kernel.json")
.to_string_lossy(),
)),
}
}
}
Expand Down Expand Up @@ -93,7 +101,7 @@ async fn cargo_helper(profile: Option<&str>, package: &str, arch: ArchSelect) ->
async fn convert_bin(path: &Path, arch: ArchSelect) -> Result<PathBuf> {
let arch = match arch {
ArchSelect::I386 => "elf32-i386",
ArchSelect::I686 | ArchSelect::X64 => "elf64-x86-64",
ArchSelect::I686 | ArchSelect::X64 | ArchSelect::Kernel => "elf64-x86-64",
};

let bin_path = path.with_extension("bin");
Expand Down Expand Up @@ -149,7 +157,7 @@ pub async fn build_project() -> Result<Artifacts> {
cargo_helper(Some("stage-16bit"), "stage-16bit", ArchSelect::I386),
cargo_helper(Some("stage-32bit"), "stage-32bit", ArchSelect::I686),
cargo_helper(Some("stage-64bit"), "stage-64bit", ArchSelect::X64),
cargo_helper(None, "kernel", ArchSelect::X64),
cargo_helper(Some("kernel"), "kernel", ArchSelect::Kernel),
build_bootloader_config(),
)?;

Expand Down

0 comments on commit 3c103f8

Please sign in to comment.