Skip to content

Commit

Permalink
v0.7.0: launch program from disk
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed May 10, 2022
1 parent 339020b commit 97046d0
Show file tree
Hide file tree
Showing 33 changed files with 758 additions and 245 deletions.
19 changes: 9 additions & 10 deletions Cargo.lock

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

23 changes: 21 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ BUILD_ARGS :=
QEMU_ARGS := -serial stdio
MODE ?= release
RUN_MODE ?=
CUR_PATH := $(shell pwd)
APP_PATH := $(CUR_PATH)/pkg/app
APPS := $(shell find $(APP_PATH) -maxdepth 1 -type d)
APPS := $(filter-out $(APP_PATH),$(patsubst $(APP_PATH)/%, %, $(APPS)))
APPS := $(filter-out config,$(APPS))

ifeq (${MODE}, release)
BUILD_ARGS += --release
Expand All @@ -15,7 +20,8 @@ endif

.PHONY: build run debug clean launch \
target/x86_64-unknown-uefi/$(MODE)/ggos_boot.efi \
target/x86_64-unknown-none/$(MODE)/ggos_kernel
target/x86_64-unknown-none/$(MODE)/ggos_kernel \
target/x86_64-unknown-ggos/$(MODE)

run: build launch

Expand All @@ -37,9 +43,12 @@ debug: build
clean:
@cargo clean

list:
@for dir in $(APPS); do echo $$dir || exit; done

build: $(ESP)

$(ESP): $(ESP)/EFI/BOOT/BOOTX64.EFI $(ESP)/KERNEL.ELF $(ESP)/EFI/BOOT/boot.conf
$(ESP): $(ESP)/EFI/BOOT/BOOTX64.EFI $(ESP)/KERNEL.ELF $(ESP)/EFI/BOOT/boot.conf $(ESP)/APP

$(ESP)/EFI/BOOT/BOOTX64.EFI: target/x86_64-unknown-uefi/$(MODE)/ggos_boot.efi
@mkdir -p $(@D)
Expand All @@ -50,8 +59,18 @@ $(ESP)/EFI/BOOT/boot.conf: pkg/kernel/config/boot.conf
$(ESP)/KERNEL.ELF: target/x86_64-unknown-none/$(MODE)/ggos_kernel
@mkdir -p $(@D)
cp $< $@
$(ESP)/APP: target/x86_64-unknown-ggos/$(MODE)
@for app in $(APPS); do \
mkdir -p $(@D)/APP; \
cp $</ggos_$$app $(@D)/APP/$$app; \
done

target/x86_64-unknown-uefi/$(MODE)/ggos_boot.efi: pkg/boot
cd pkg/boot && cargo build $(BUILD_ARGS)
target/x86_64-unknown-none/$(MODE)/ggos_kernel: pkg/kernel
cd pkg/kernel && cargo build $(BUILD_ARGS)
target/x86_64-unknown-ggos/$(MODE): pkg/app/$(APPS)
@for app in $(APPS); do \
echo "Building $$app"; \
cd $(APP_PATH)/$$app && cargo build $(BUILD_ARGS); \
done
30 changes: 30 additions & 0 deletions pkg/app/config/app.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
ENTRY(_start)

SECTIONS {
. = 0x111100000000;

.rodata ALIGN(4K):
{
*(.rodata .rodata.*)
}

.text ALIGN(4K):
{
*(.text .text.*)
}

.data ALIGN(4K):
{
*(.data .data.*)
}

.got ALIGN(4K):
{
*(.got .got.*)
}

.bss ALIGN(4K):
{
*(.bss .bss.*)
}
}
18 changes: 18 additions & 0 deletions pkg/app/config/x86_64-unknown-ggos.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"llvm-target": "x86_64-unknown-ggos",
"data-layout": "e-m:e-i64:64-f80:128-n8:16:32:64-S128",
"linker-flavor": "ld.lld",
"target-endian": "little",
"target-pointer-width": "64",
"target-c-int-width": "32",
"arch": "x86_64",
"os": "none",
"executables": true,
"linker": "rust-lld",
"disable-redzone": true,
"features": "-mmx,-sse,+soft-float",
"panic-strategy": "abort",
"pre-link-args": {
"ld.lld": ["-Tpkg/app/config/app.ld"]
}
}
6 changes: 6 additions & 0 deletions pkg/app/sh/.cargo/config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[build]
target = "../config/x86_64-unknown-ggos.json"

[unstable]
build-std-features = ["compiler-builtins-mem"]
build-std = ["core", "compiler_builtins", "alloc"]
2 changes: 1 addition & 1 deletion pkg/app/sh/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "ggos_app_sh"
name = "ggos_sh"
version = "0.1.0"
edition = "2021"
authors = ["GZTime <Time.GZ@outlook.com>"]
Expand Down
9 changes: 8 additions & 1 deletion pkg/app/sh/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
use lib;
#![no_std]
#![no_main]

#[macro_use]
extern crate lib;

fn main() {
println!("Hello, world!");
lib::sys_exit(0);
}

entry!(main);
2 changes: 1 addition & 1 deletion pkg/elf/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ authors = ["GZTime <Time.GZ@outlook.com>"]
[dependencies]
log = "0.4.16"
x86_64 = "0.14.9"
xmas-elf = "0.8"
xmas-elf = "0.8"
10 changes: 5 additions & 5 deletions pkg/elf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ pub fn map_elf(
page_table: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), MapToError<Size4KiB>> {
debug!("Mapping ELF file...");
let kernel_start = PhysAddr::new(elf.input.as_ptr() as u64);
debug!("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, kernel_start, page_table, frame_allocator)?;
map_segment(&segment, start, page_table, frame_allocator)?;
}
Ok(())
}
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn map_stack(

fn map_segment(
segment: &program::ProgramHeader,
kernel_start: PhysAddr,
start: PhysAddr,
page_table: &mut impl Mapper<Size4KiB>,
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
) -> Result<(), MapToError<Size4KiB>> {
Expand All @@ -100,7 +100,7 @@ fn map_segment(
let mem_size = segment.mem_size();
let file_size = segment.file_size();
let file_offset = segment.offset() & !0xfff;
let phys_start_addr = kernel_start + file_offset;
let phys_start_addr = start + file_offset;
let virt_start_addr = VirtAddr::new(segment.virtual_addr());

let start_page: Page = Page::containing_address(virt_start_addr);
Expand Down
2 changes: 1 addition & 1 deletion pkg/fs/src/device/disk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ where
}

fn read_block(&self, offset: usize) -> Result<Block, DeviceError> {
debug!(
trace!(
"read_block offset: {}, volume lba start: {}",
offset,
self.meta.begin_lba()
Expand Down
4 changes: 3 additions & 1 deletion pkg/kernel/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
[package]
name = "ggos_kernel"
version = "0.6.4"
version = "0.7.0"
edition = "2021"
authors = ["GZTime <Time.GZ@outlook.com>"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
boot = { path = "../boot", package = "ggos_boot", default-features = false }
fs = { path = "../fs", package = "ggfs" }
elf = { package = "ggos_elf", path = "../elf" }
embedded-graphics = { version = "0.7.1", features = ['fixed_point'] }
lazy_static = { version = "1.4.0", features = ["spin_no_std"] }
paste = "1.0.7"
Expand All @@ -24,3 +25,4 @@ linked_list_allocator = "0.9.1"
pc-keyboard = "0.5.1"
crossbeam-queue = { version = "0.3.5", default-features = false, features = ["alloc"] }
volatile = "0.4.4"
xmas-elf = "0.8"
9 changes: 0 additions & 9 deletions pkg/kernel/config/boot.conf
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,3 @@ physical_memory_offset=0xFFFF800000000000

# The path of kernel ELF
kernel_path=\KERNEL.ELF

# The resolution of graphic output
resolution=800x600

# Kernel Command Line
# cmdline=

# The path of initramfs
# initramfs=\EFI\rCore\initramfs.img
2 changes: 1 addition & 1 deletion pkg/kernel/src/drivers/ata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ impl Bus {

fn write_command(&mut self, cmd: ATACommand) -> Result<(), ()> {
unsafe { self.command.write(cmd as u8) }
debug!("Wrote command {:?}", cmd);
trace!("Wrote command {:?}", cmd);
self.status(); // Ignore results of first read
self.clear_interrupt();
if self.status() == 0 {
Expand Down
35 changes: 28 additions & 7 deletions pkg/kernel/src/drivers/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,10 @@ pub fn get_volume() -> &'static Volume {
}

#[derive(Debug, Clone)]
pub struct StdIO;

impl StdIO {
pub fn new() -> Self {
Self {}
}
pub enum StdIO {
Stdin,
Stdout,
Stderr,
}

pub fn init() {
Expand All @@ -29,7 +27,7 @@ pub fn init() {
info!("Initialized Filesystem.");
}

fn resolve_path(root_path: &str) -> Option<Directory> {
pub fn resolve_path(root_path: &str) -> Option<Directory> {
let mut path = root_path.to_owned();
let mut root = fs::root_dir();

Expand All @@ -48,6 +46,7 @@ fn resolve_path(root_path: &str) -> Option<Directory> {
root = tmp.unwrap();

path = path[pos + 1..].to_string();
trace!("Resolving path: {}", path);

if path.len() == 0 {
break;
Expand All @@ -57,6 +56,28 @@ fn resolve_path(root_path: &str) -> Option<Directory> {
Some(root)
}

pub fn try_get_file(path: &str) -> Result<File, VolumeError> {
let path = path.to_owned();
let pos = path.rfind('/');

if pos.is_none() {
return Err(VolumeError::FileNotFound);
}
let pos = pos.unwrap();

trace!("root: {}, filename: {}", &path[..pos + 1], &path[pos + 1..]);

let root = resolve_path(&path[..pos + 1]);
let filename = &path[pos + 1..];

if root.is_none() {
return Err(VolumeError::FileNotFound);
}
let root = root.unwrap();

fs::open_file(get_volume(), &root, filename, file::Mode::ReadOnly)
}

pub fn ls(root_path: &str) {

let root = match resolve_path(root_path) {
Expand Down
28 changes: 25 additions & 3 deletions pkg/kernel/src/interrupt/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub unsafe fn reg_idt(idt: &mut InterruptDescriptorTable) {
idt.general_protection_fault
.set_handler_fn(general_protection_fault_handler);
idt.page_fault.set_handler_fn(page_fault_handler);
idt.alignment_check.set_handler_fn(alignment_check_handler);
idt.machine_check.set_handler_fn(machine_check_handler);
idt.simd_floating_point.set_handler_fn(simd_floating_point_handler);

idt[(consts::Interrupts::IRQ0 as u8 + consts::IRQ::Timer as u8) as usize]
.set_handler_fn(clock_handler)
Expand Down Expand Up @@ -73,7 +76,7 @@ pub extern "x86-interrupt" fn double_fault_handler(
error_code: u64,
) -> ! {
panic!(
"EXCEPTION: DOUBLE FAULT, ERROR_CODE: 0x{:16x}\n\n{:#?}",
"EXCEPTION: DOUBLE FAULT, ERROR_CODE: 0x{:016x}\n\n{:#?}",
error_code, stack_frame
);
}
Expand All @@ -83,7 +86,7 @@ pub extern "x86-interrupt" fn invalid_tss_handler(
error_code: u64,
) {
panic!(
"EXCEPTION: INVALID TSS, ERROR_CODE: 0x{:16x}\n\n{:#?}",
"EXCEPTION: INVALID TSS, ERROR_CODE: 0x{:016x}\n\n{:#?}",
error_code, stack_frame
);
}
Expand Down Expand Up @@ -128,6 +131,24 @@ pub extern "x86-interrupt" fn page_fault_handler(
);
}

pub extern "x86-interrupt" fn alignment_check_handler(
stack_frame: InterruptStackFrame,
error_code: u64,
) {
panic!(
"EXCEPTION: ALIGNMENT CHECK, ERROR_CODE: 0x{:016x}\n\n{:#?}",
error_code, stack_frame
);
}

pub extern "x86-interrupt" fn machine_check_handler(stack_frame: InterruptStackFrame) -> ! {
panic!("EXCEPTION: MACHINE CHECK\n\n{:#?}", stack_frame);
}

pub extern "x86-interrupt" fn simd_floating_point_handler(stack_frame: InterruptStackFrame) {
panic!("EXCEPTION: SIMD FLOATING POINT\n\n{:#?}", stack_frame);
}

pub extern "C" fn clock(mut regs: Registers, mut sf: InterruptStackFrame) {
super::ack(consts::Interrupts::IRQ0 as u8);
crate::process::switch(&mut regs, &mut sf);
Expand All @@ -137,7 +158,8 @@ as_handler!(clock);

pub extern "C" fn syscall(mut regs: Registers, mut sf: InterruptStackFrame) {
let args =
super::syscall::SyscallArgs::new(Syscall::from(regs.rax), regs.rbx, regs.rcx, regs.rdx);
super::syscall::SyscallArgs::new(Syscall::from(regs.rax), regs.rdi, regs.rsi, regs.rdx);
trace!("{}", args);
unsafe {
x86_64::instructions::interrupts::without_interrupts(|| {
super::syscall::dispatcher(args, &mut regs, &mut sf);
Expand Down
Loading

0 comments on commit 97046d0

Please sign in to comment.