Skip to content

Commit

Permalink
wip: update, sys_read wait for fixing
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed May 10, 2022
1 parent 97046d0 commit 612439b
Show file tree
Hide file tree
Showing 27 changed files with 360 additions and 183 deletions.
33 changes: 32 additions & 1 deletion Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ members = [
"pkg/fs",
"pkg/kernel",
"pkg/lib",
"pkg/app/sh"
"pkg/app/sh",
"pkg/app/hello"
]
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,16 @@ $(ESP)/KERNEL.ELF: target/x86_64-unknown-none/$(MODE)/ggos_kernel
cp $< $@
$(ESP)/APP: target/x86_64-unknown-ggos/$(MODE)
@for app in $(APPS); do \
mkdir -p $(@D)/APP; \
cp $</ggos_$$app $(@D)/APP/$$app; \
mkdir -p $(ESP)/APP; \
cp $</ggos_$$app $(ESP)/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)
target/x86_64-unknown-ggos/$(MODE):
@for app in $(APPS); do \
echo "Building $$app"; \
cd $(APP_PATH)/$$app && cargo build $(BUILD_ARGS); \
cd $(APP_PATH)/$$app && cargo build $(BUILD_ARGS) || exit; \
done
6 changes: 6 additions & 0 deletions pkg/app/hello/.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"]
10 changes: 10 additions & 0 deletions pkg/app/hello/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "ggos_hello"
version = "0.1.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]
lib = { path="../../lib", package="gglib"}
15 changes: 15 additions & 0 deletions pkg/app/hello/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#![no_std]
#![no_main]

use lib::*;

extern crate lib;

fn main() {
println!("Hello, world!!!");
let time = lib::sys_time();
println!("Now at: {}", time);
lib::sys_exit(0);
}

entry!(main);
52 changes: 50 additions & 2 deletions pkg/app/sh/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,59 @@
#![no_std]
#![no_main]

#[macro_use]
extern crate alloc;

use lib::*;
use alloc::vec::Vec;
use alloc::string::*;
use lib::io::stdin;

extern crate lib;

fn main() {
println!("Hello, world!");
let mut root_dir = String::from("/APP/");

loop {
print!("[{}] ", root_dir);
let input = stdin().read_line();
let line: Vec<&str> = input.trim().split(' ').collect();
match line[0] {
"exit" => break,
"ps" => sys_stat(),
"ls" => sys_list_dir(root_dir.as_str()),
"cat" => {
// ggos::filesystem::cat(root_dir.as_str(), line[1]);
}
"cd" => {
match line[1] {
".." => {
if root_dir.as_str() == "/" {
break;
}
root_dir.pop();
let pos = root_dir.rfind('/').unwrap();
root_dir = root_dir[..pos + 1].to_string();
},
_ => {
root_dir.push_str(line[1]);
root_dir.push('/');
root_dir = root_dir.to_ascii_uppercase();
}
}
}
"exec" => {
let path = root_dir.clone() + line[1];
let pid = sys_spawn(path.as_str());
if pid == 0 {
println!("failed to spawn process: {}#{}", line[1], pid);
} else {
println!("spawned process: {}#{}", line[1], pid);
}
}
_ => println!("[=] {}", input),
}
}

lib::sys_exit(0);
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/kernel/.cargo/config
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ build-std = ["core", "compiler_builtins", "alloc"]

[env]
LOG_LEVEL = "debug"

[profile.release]
debug = true
3 changes: 2 additions & 1 deletion pkg/kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ 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 }
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'] }
Expand All @@ -26,3 +26,4 @@ pc-keyboard = "0.5.1"
crossbeam-queue = { version = "0.3.5", default-features = false, features = ["alloc"] }
volatile = "0.4.4"
xmas-elf = "0.8"
num_enum = { version = "0.5.7", default-features = false }
1 change: 0 additions & 1 deletion pkg/kernel/src/drivers/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ impl Device<u8> for Console {
if offset + size >= buf.len() {
return Err(DeviceError::ReadError);
}

x86_64::instructions::interrupts::without_interrupts(|| {
let stdin = get_input_buf_for_sure();
let mut read_count = 0;
Expand Down
13 changes: 8 additions & 5 deletions pkg/kernel/src/drivers/input.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::drivers::{console, serial};
use alloc::string::String;
use crossbeam_queue::ArrayQueue;
use pc_keyboard::DecodedKey;
use crate::drivers::{serial, console};
use x86_64::instructions::interrupts;
use alloc::string::String;

once_mutex!(pub INPUT_BUF: ArrayQueue<DecodedKey>);

Expand All @@ -26,9 +26,12 @@ pub fn try_get_key() -> Option<DecodedKey> {

pub fn get_key() -> DecodedKey {
loop {
if let Some(k) = try_get_key() {
return k;
}
crate::utils::halt();
interrupts::without_interrupts(|| {
if let Some(k) = try_get_key() {
return k;
}
})
}
}

Expand Down
15 changes: 5 additions & 10 deletions pkg/kernel/src/interrupt/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ pub unsafe fn reg_idt(idt: &mut InterruptDescriptorTable) {
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.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 @@ -157,15 +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) {
let args =
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);
})
}
super::ack(consts::Interrupts::Syscall as u8);
x86_64::instructions::interrupts::without_interrupts(|| {
super::syscall::dispatcher(&mut regs, &mut sf);
});
}

as_handler!(syscall);
87 changes: 31 additions & 56 deletions pkg/kernel/src/interrupt/syscall/mod.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
use crate::utils::*;
use core::alloc::Layout;
use x86_64::structures::idt::InterruptStackFrame;
use num_enum::TryFromPrimitive;
use core::convert::TryFrom;

mod service;
use service::*;

#[derive(Clone, Debug)]
#[repr(u8)]
#[derive(Clone, Debug, TryFromPrimitive)]
pub enum Syscall {
SpwanProcess = 1,
SpawnProcess = 1,
ExitProcess = 2,
Read = 3,
Write = 4,
Open = 5,
Close = 6,
Stat = 7,
Clock = 8,
Draw = 9,
Time = 8,
DirectoryList = 9,
Allocate = 10,
Deallocate = 11,
None = 0xdeadbeef,
Draw = 12,
#[num_enum(default)]
None = 255,
}

#[derive(Clone, Debug)]
Expand All @@ -29,61 +33,32 @@ pub struct SyscallArgs {
pub arg2: usize,
}

#[allow(unused_variables)]
pub unsafe fn dispatcher(args: SyscallArgs, regs: &mut Registers, sf: &mut InterruptStackFrame) {
pub fn dispatcher(regs: &mut Registers, sf: &mut InterruptStackFrame) {
let args = super::syscall::SyscallArgs::new(
Syscall::try_from(regs.rax as u8).unwrap(),
regs.rdi,
regs.rsi,
regs.rdx
);
debug!("{}", args);
debug!("{:#?}\n{:#?}", sf, regs);

match args.syscall {
Syscall::SpwanProcess => {}
Syscall::ExitProcess => exit_process(regs, sf), // todo: handle exit code
Syscall::Read => match args.arg0 {
0 => {}
fd => warn!("SYSCALL: cannot read from fd: {}", fd),
},
Syscall::Write => {
let s = core::str::from_utf8_unchecked(core::slice::from_raw_parts(
args.arg1 as *const u8,
args.arg2,
));
match args.arg0 {
1 => print!("{}", s),
fd => warn!("SYSCALL: cannot write to fd: {}", fd),
}
}
Syscall::SpawnProcess => regs.set_rax(spawn_process(&args)),
Syscall::ExitProcess => exit_process(regs, sf),
Syscall::Read => regs.set_rax(sys_read(&args)),
Syscall::Write => sys_write(&args),
Syscall::Open => {}
Syscall::Close => {}
Syscall::Stat => {}
Syscall::Clock => regs.set_rax(sys_clock() as usize),
Syscall::Draw => sys_draw(args.arg0, args.arg1, args.arg2),
Syscall::Allocate => {
regs.set_rax(sys_allocate((args.arg0 as *const Layout).as_ref().unwrap()) as usize)
}
Syscall::Deallocate => sys_deallocate(
args.arg0 as *mut u8,
(args.arg1 as *const Layout).as_ref().unwrap(),
),
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 => {}
}
}

impl From<usize> for Syscall {
fn from(num: usize) -> Self {
match num {
1 => Self::SpwanProcess,
2 => Self::ExitProcess,
3 => Self::Read,
4 => Self::Write,
5 => Self::Open,
6 => Self::Close,
7 => Self::Stat,
8 => Self::Clock,
9 => Self::Draw,
10 => Self::Allocate,
11 => Self::Deallocate,
_ => {
warn!("Unknown SYSCALL: {}", num);
Self::None
}
}
}
debug!("syscall finished.");
}

impl SyscallArgs {
Expand Down
Loading

0 comments on commit 612439b

Please sign in to comment.