Skip to content

Commit

Permalink
v0.7.2: hello world, again
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed May 13, 2022
1 parent ae743a7 commit 46ea283
Show file tree
Hide file tree
Showing 20 changed files with 175 additions and 71 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ ifeq (${RUN_MODE}, nographic)
QEMU_ARGS = -nographic
endif

.PHONY: build run debug clean launch \
.PHONY: build run debug clean launch intdbg \
target/x86_64-unknown-uefi/$(MODE)/ggos_boot.efi \
target/x86_64-unknown-none/$(MODE)/ggos_kernel \
target/x86_64-unknown-ggos/$(MODE)
Expand All @@ -32,6 +32,13 @@ launch:
$(QEMU_ARGS) \
-drive format=raw,file=fat:rw:${ESP}

intdbg:
@qemu-system-x86_64 \
-bios ${OVMF} \
-net none \
$(QEMU_ARGS) \
-drive format=raw,file=fat:rw:${ESP} -no-reboot -d int,cpu_reset

debug: build
@qemu-system-x86_64 \
-bios ${OVMF} \
Expand Down
3 changes: 2 additions & 1 deletion pkg/app/hello/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ fn main() {
println!("Hello, world!!!");
let time = lib::sys_time();
println!("Now at: {}", time);
lib::sys_exit(0);
println!("Exiting...");
lib::sys_exit(233);
}

entry!(main);
21 changes: 15 additions & 6 deletions pkg/app/sh/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ extern crate lib;

fn main() {

sys_list_dir("/");

let mut root_dir = String::from("/APP/");

loop {
Expand All @@ -28,6 +26,9 @@ fn main() {
// ggos::filesystem::cat(root_dir.as_str(), line[1]);
}
"cd" => {
if line[1].starts_with("/") {
root_dir = String::from(line[1]);
}
match line[1] {
".." => {
if root_dir.as_str() == "/" {
Expand All @@ -37,6 +38,7 @@ fn main() {
let pos = root_dir.rfind('/').unwrap();
root_dir = root_dir[..pos + 1].to_string();
},
"." => break,
_ => {
root_dir.push_str(line[1]);
root_dir.push('/');
Expand All @@ -46,15 +48,22 @@ fn main() {
}
"exec" => {
let path = root_dir.clone() + line[1];
println!("ready to exec {}...", path);
let start = sys_time();

let pid = sys_spawn(path.as_str());
if pid == 0 {
println!("failed to spawn process: {}#{}", line[1], pid);
println!("[!] failed to spawn process: {}#{}", line[1], pid);
continue;
} else {
println!("spawned process: {}#{}", line[1], pid);
println!("[+] spawned process: {}#{}", line[1], pid);
}

let ret = sys_wait_pid(pid);
let time = sys_time() - start ;

println!("[+] process exited with code {} @ {}s", ret, time.num_seconds());
}
_ => println!("[=] {}", input),
_ => println!("[=] you said \"{}\"", input),
}
}

Expand Down
12 changes: 6 additions & 6 deletions pkg/elf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn map_physical_memory(
page_table: &mut impl Mapper<Size2MiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) {
debug!("Mapping physical memory...");
trace!("Mapping physical memory...");
let start_frame = PhysFrame::containing_address(PhysAddr::new(0));
let end_frame = PhysFrame::containing_address(PhysAddr::new(max_addr));

Expand All @@ -40,7 +40,7 @@ pub fn map_elf(
page_table: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), MapToError<Size4KiB>> {
debug!("Mapping ELF file...{:?}", elf.input.as_ptr());
trace!("Mapping ELF file...{:?}", elf.input.as_ptr());
let start = PhysAddr::new(elf.input.as_ptr() as u64);
for segment in elf.program_iter() {
map_segment(&segment, start, page_table, frame_allocator)?;
Expand All @@ -50,7 +50,7 @@ pub fn map_elf(

/// Unmap ELF file
pub fn unmap_elf(elf: &ElfFile, page_table: &mut impl Mapper<Size4KiB>) -> Result<(), UnmapError> {
debug!("Unmapping ELF file...");
trace!("Unmapping ELF file...");
let kernel_start = PhysAddr::new(elf.input.as_ptr() as u64);
for segment in elf.program_iter() {
unmap_segment(&segment, kernel_start, page_table)?;
Expand All @@ -65,7 +65,7 @@ pub fn map_stack(
page_table: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), MapToError<Size4KiB>> {
debug!("mapping stack at {:#x}", addr);
trace!("mapping stack at {:#x}", addr);
// create a stack
let stack_start = Page::containing_address(VirtAddr::new(addr));
let stack_end = stack_start + pages;
Expand Down Expand Up @@ -96,7 +96,7 @@ fn map_segment(
return Ok(());
}

debug!("Mapping segment: {:#x?}", segment);
trace!("Mapping segment: {:#x?}", segment);
let mem_size = segment.mem_size();
let file_size = segment.file_size();
let file_offset = segment.offset() & !0xfff;
Expand Down Expand Up @@ -201,7 +201,7 @@ fn unmap_segment(
if segment.get_type().unwrap() != program::Type::Load {
return Ok(());
}
debug!("Unmapping segment: {:#x?}", segment);
trace!("Unmapping segment: {:#x?}", segment);
let mem_size = segment.mem_size();
let file_size = segment.file_size();
let file_offset = segment.offset() & !0xfff;
Expand Down
4 changes: 2 additions & 2 deletions pkg/fs/src/device/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ where
where
F: FnMut(&DirEntry),
{
debug!("Iterating directory: {:?}", dir);
trace!("Iterating directory: {:?}", dir);
let mut current_cluster = Some(dir.cluster);
let mut dir_sector_num = self.cluster_to_sector(&dir.cluster);
let dir_size = match dir.cluster {
Expand All @@ -201,7 +201,7 @@ where
if dir_entry.is_eod() {
return Ok(());
} else if dir_entry.is_valid() && !dir_entry.is_long_name() {
debug!("found file {}", dir_entry.filename());
trace!("found file {}", dir_entry.filename());
func(&dir_entry);
}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ggos_kernel"
version = "0.7.0"
version = "0.7.2"
edition = "2021"
authors = ["GZTime <Time.GZ@outlook.com>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
8 changes: 5 additions & 3 deletions pkg/kernel/src/interrupt/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ pub unsafe fn reg_idt(idt: &mut InterruptDescriptorTable) {

idt[(consts::Interrupts::IRQ0 as u8 + consts::IRQ::Timer as u8) as usize]
.set_handler_fn(clock_handler)
.set_stack_index(crate::gdt::CONTEXT_SWITCH);
.set_stack_index(crate::gdt::CONTEXT_SWITCH_IST_INDEX);

idt[consts::Interrupts::Syscall as usize]
.set_handler_fn(syscall_handler)
.set_stack_index(crate::gdt::CONTEXT_SWITCH)
.set_stack_index(crate::gdt::SYSCALL_IST_INDEX)
.set_privilege_level(x86_64::PrivilegeLevel::Ring3);
}

Expand Down Expand Up @@ -158,7 +158,9 @@ pub extern "C" fn clock(mut regs: Registers, mut sf: InterruptStackFrame) {
as_handler!(clock);

pub extern "C" fn syscall(mut regs: Registers, mut sf: InterruptStackFrame) {
super::syscall::dispatcher(&mut regs, &mut sf);
x86_64::instructions::interrupts::without_interrupts(|| {
super::syscall::dispatcher(&mut regs, &mut sf);
});
}

as_handler!(syscall);
28 changes: 15 additions & 13 deletions pkg/kernel/src/interrupt/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub enum Syscall {
Allocate = 10,
Deallocate = 11,
Draw = 12,
WaitPid = 13,
#[num_enum(default)]
None = 255,
}
Expand All @@ -42,19 +43,20 @@ pub fn dispatcher(regs: &mut Registers, sf: &mut InterruptStackFrame) {
);

match args.syscall {
Syscall::SpawnProcess => regs.set_rax(spawn_process(&args)),
Syscall::ExitProcess => exit_process(regs, sf),
Syscall::Read => regs.set_rax(sys_read(&args)),
Syscall::Write => regs.set_rax(sys_write(&args)),
Syscall::Open => {}
Syscall::Close => {}
Syscall::Stat => list_process(),
Syscall::Time => regs.set_rax(sys_clock() as usize),
Syscall::DirectoryList => list_dir(&args),
Syscall::Allocate => regs.set_rax(sys_allocate(&args)),
Syscall::Deallocate => sys_deallocate(&args),
Syscall::Draw => sys_draw(&args),
Syscall::None => {}
Syscall::SpawnProcess => regs.set_rax(spawn_process(&args)),
Syscall::ExitProcess => exit_process(&args, regs, sf),
Syscall::Read => regs.set_rax(sys_read(&args)),
Syscall::Write => regs.set_rax(sys_write(&args)),
Syscall::Open => {}
Syscall::Close => {}
Syscall::Stat => list_process(),
Syscall::Time => regs.set_rax(sys_clock() as usize),
Syscall::DirectoryList => list_dir(&args),
Syscall::Allocate => regs.set_rax(sys_allocate(&args)),
Syscall::Deallocate => sys_deallocate(&args),
Syscall::Draw => sys_draw(&args),
Syscall::WaitPid => regs.set_rax(sys_wait_pid(&args)),
Syscall::None => {}
}
}

Expand Down
11 changes: 9 additions & 2 deletions pkg/kernel/src/interrupt/syscall/service.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use core::alloc::Layout;

use crate::process::ProcessId;
use crate::utils::Registers;
use crate::{display::get_display_for_sure, utils::*};
use embedded_graphics::prelude::*;
Expand Down Expand Up @@ -95,8 +96,8 @@ pub fn sys_write(args: &SyscallArgs) -> usize {
}
}

pub fn exit_process(regs: &mut Registers, sf: &mut InterruptStackFrame) {
crate::process::process_exit(regs, sf);
pub fn exit_process(args: &SyscallArgs, regs: &mut Registers, sf: &mut InterruptStackFrame) {
crate::process::process_exit(args.arg0 as isize, regs, sf);
}

pub fn list_process() {
Expand All @@ -116,3 +117,9 @@ pub fn list_dir(args: &SyscallArgs) {
pub fn get_handle(fd: u8) -> Option<Resource> {
crate::process::handle(fd)
}

pub fn sys_wait_pid(args: &SyscallArgs) -> usize {
let pid = ProcessId(args.arg0 as u16);
let ret = crate::process::wait_pid(pid);
ret as usize
}
1 change: 1 addition & 0 deletions pkg/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#![feature(alloc_error_handler)]
#![feature(type_alias_impl_trait)]
#![feature(panic_info_message)]
#![feature(map_try_insert)]

extern crate alloc;
#[macro_use]
Expand Down
11 changes: 7 additions & 4 deletions pkg/kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ pub fn kernal_main(boot_info: &'static boot::BootInfo) -> ! {

let mut executor = Executor::new();

let sh_file = ggos::filesystem::try_get_file("/APP/SH").unwrap();
let pid = ggos::process::spawn(&sh_file).unwrap();

executor.run(pid);
// TODO: use executor.spawn() to spawn kernel tasks

executor.run(spawn_init());
ggos::shutdown(boot_info);
}

pub fn spawn_init() -> ggos::process::ProcessId {
let sh_file = ggos::filesystem::try_get_file("/APP/SH").unwrap();
ggos::process::spawn(&sh_file).unwrap()
}
16 changes: 13 additions & 3 deletions pkg/kernel/src/memory/gdt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ use x86_64::structures::tss::TaskStateSegment;
use x86_64::VirtAddr;

pub const DOUBLE_FAULT_IST_INDEX: u16 = 0;
pub const CONTEXT_SWITCH: u16 = 0;
pub const SYSCALL_IST_INDEX: u16 = 1;
pub const CONTEXT_SWITCH_IST_INDEX: u16 = 0;

lazy_static! {
static ref TSS: TaskStateSegment = {
let mut tss = TaskStateSegment::new();
tss.interrupt_stack_table[CONTEXT_SWITCH as usize] = {
const STACK_SIZE: usize = 4096;
tss.interrupt_stack_table[DOUBLE_FAULT_IST_INDEX as usize] = {
const STACK_SIZE: usize = 0x1000;
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
let stack_end = stack_start + STACK_SIZE;
info!("Double Fault IST: 0x{:016x}-0x{:016x}", stack_start.as_u64(), stack_end.as_u64());
stack_end
};
tss.interrupt_stack_table[SYSCALL_IST_INDEX as usize] = {
const STACK_SIZE: usize = 0x4000;
static mut STACK: [u8; STACK_SIZE] = [0; STACK_SIZE];
let stack_start = VirtAddr::from_ptr(unsafe { &STACK });
let stack_end = stack_start + STACK_SIZE;
info!("Syscall IST: 0x{:016x}-0x{:016x}", stack_start.as_u64(), stack_end.as_u64());
stack_end
};
tss
Expand Down
Loading

0 comments on commit 46ea283

Please sign in to comment.