Skip to content

Commit

Permalink
wip: pid & async test
Browse files Browse the repository at this point in the history
  • Loading branch information
GZTimeWalker committed May 12, 2022
1 parent 612439b commit 3732a89
Show file tree
Hide file tree
Showing 20 changed files with 332 additions and 104 deletions.
37 changes: 37 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions pkg/kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ crossbeam-queue = { version = "0.3.5", default-features = false, features = ["al
volatile = "0.4.4"
xmas-elf = "0.8"
num_enum = { version = "0.5.7", default-features = false }
futures-util = { version = "0.3.21", default-features = false, features = ["alloc"] }
19 changes: 2 additions & 17 deletions pkg/kernel/src/drivers/console.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use crate::drivers::display::get_display_for_sure;
use crate::input::get_input_buf_for_sure;
use crate::utils::colors;
use crate::utils::font;
use alloc::string::ToString;
use core::fmt::Write;
use embedded_graphics::{
mono_font::{MonoFont, MonoTextStyle},
Expand All @@ -11,7 +9,6 @@ use embedded_graphics::{
text::{renderer::CharacterStyle, Baseline, Text},
};
use fs::*;
use pc_keyboard::DecodedKey;

once_mutex!(pub CONSOLE: Console);

Expand Down Expand Up @@ -184,20 +181,8 @@ 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;
while !stdin.is_empty() && read_count < size {
if let Some(DecodedKey::Unicode(c)) = stdin.pop() {
let s = c.to_string();
let len = s.len();
buf[offset + read_count..offset + read_count + len]
.copy_from_slice(s.as_bytes());
read_count += len;
}
}
Ok(read_count)
})
// TODO: get key
Ok(0)
}

fn write(&mut self, buf: &[u8], offset: usize, size: usize) -> Result<usize, DeviceError> {
Expand Down
14 changes: 6 additions & 8 deletions pkg/kernel/src/drivers/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,18 @@ pub fn init() {
pub fn try_get_key() -> Option<DecodedKey> {
interrupts::without_interrupts(|| {
if let Some(key) = get_input_buf_for_sure().pop() {
return Some(key);
Some(key)
} else {
None
}
None
})
}

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

Expand Down
2 changes: 1 addition & 1 deletion pkg/kernel/src/drivers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ pub mod serial;
pub mod console;
pub mod display;
pub mod keyboard;
pub mod input;
// pub mod input;
pub mod filesystem;
pub mod ata;
4 changes: 2 additions & 2 deletions pkg/kernel/src/interrupt/keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use x86_64::{
structures::idt::{InterruptDescriptorTable, InterruptStackFrame},
};
use pc_keyboard::DecodedKey;
use crate::{keyboard::get_keyboard_for_sure, drivers::input::get_input_buf_for_sure};
use crate::{keyboard::get_keyboard_for_sure, push_key};

pub unsafe fn reg_idt(idt: &mut InterruptDescriptorTable) {
idt[(consts::Interrupts::IRQ0 as u8 + consts::IRQ::Keyboard as u8) as usize]
Expand Down Expand Up @@ -38,6 +38,6 @@ pub fn receive() -> Option<DecodedKey> {
pub extern "x86-interrupt" fn interrupt_handler(_st: InterruptStackFrame) {
super::ack(super::consts::IRQ::Keyboard as u8);
if let Some(key) = receive() {
get_input_buf_for_sure().push(key).unwrap();
push_key(key);
}
}
7 changes: 2 additions & 5 deletions pkg/kernel/src/interrupt/serial.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
use super::consts;
use x86_64::structures::idt::{InterruptDescriptorTable, InterruptStackFrame};
use pc_keyboard::DecodedKey;
use crate::drivers::{
input::get_input_buf_for_sure,
serial::get_serial_for_sure
};
use crate::{drivers::serial::get_serial_for_sure, push_key};

pub unsafe fn reg_idt(idt: &mut InterruptDescriptorTable) {
idt[(consts::Interrupts::IRQ0 as u8 + consts::IRQ::Serial0 as u8) as usize]
Expand Down Expand Up @@ -33,6 +30,6 @@ pub fn receive() -> Option<DecodedKey> {
pub extern "x86-interrupt" fn interrupt_handler(_st: InterruptStackFrame) {
super::ack(super::consts::IRQ::Serial0 as u8);
if let Some(key) = receive() {
get_input_buf_for_sure().push(key).unwrap();
push_key(key);
}
}
4 changes: 1 addition & 3 deletions pkg/kernel/src/interrupt/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ pub fn dispatcher(regs: &mut Registers, sf: &mut InterruptStackFrame) {
regs.rsi,
regs.rdx
);
debug!("{}", args);
debug!("{:#?}\n{:#?}", sf, regs);

match args.syscall {
Syscall::SpawnProcess => regs.set_rax(spawn_process(&args)),
Expand All @@ -58,7 +56,7 @@ pub fn dispatcher(regs: &mut Registers, sf: &mut InterruptStackFrame) {
Syscall::Draw => sys_draw(&args),
Syscall::None => {}
}
debug!("syscall finished.");
// debug!("syscall finished.");
}

impl SyscallArgs {
Expand Down
9 changes: 2 additions & 7 deletions pkg/kernel/src/interrupt/syscall/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ pub fn spawn_process(args: &SyscallArgs) -> usize {
warn!("spawn_process: failed to spawn process: {}", path);
return 0;
}
pid.unwrap() as usize
u16::from(pid.unwrap()) as usize
}

pub fn sys_read(args: &SyscallArgs) -> usize {
Expand All @@ -74,12 +74,7 @@ pub fn sys_read(args: &SyscallArgs) -> usize {
let buf = unsafe {
core::slice::from_raw_parts_mut(args.arg1 as *mut u8, args.arg2)
};
return if let Ok(size) = res.read(buf) {
debug!(
"read {} bytes: {:?}",
size,
unsafe{ core::str::from_utf8_unchecked(&buf[..size]) }
);
if let Ok(size) = res.read(buf) {
size
} else {
0
Expand Down
4 changes: 3 additions & 1 deletion pkg/kernel/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub mod drivers;
pub use drivers::*;

pub mod memory;
pub mod tasks;

pub use tasks::*;
use memory::gdt;
use memory::allocator;

Expand All @@ -48,7 +51,6 @@ pub fn init(boot_info: &'static BootInfo) {
allocator::init(); // init heap allocator
process::init(); // init process manager
keyboard::init(); // init keyboard
input::init(); // init input manager
ata::init(); // init ata
filesystem::init(); // init filesystem

Expand Down
21 changes: 13 additions & 8 deletions pkg/kernel/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#![no_std]
#![no_main]

use ggos::*;
use ggos_kernel as ggos;

extern crate alloc;
Expand All @@ -10,14 +11,18 @@ boot::entry_point!(kernal_main);
pub fn kernal_main(boot_info: &'static boot::BootInfo) -> ! {
ggos::init(boot_info);

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

while ggos::process::still_alive(pid) {
unsafe {
core::arch::asm!("hlt");
}
}
// while ggos::process::still_alive(pid) {
// unsafe {
// core::arch::asm!("hlt");
// }
// }

ggos::shutdown(boot_info);
let mut executor = Executor::new();
executor.spawn(Task::new(get_key()));
executor.run();

// ggos::shutdown(boot_info);
}
26 changes: 9 additions & 17 deletions pkg/kernel/src/process/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ guard_access_fn! {
}

pub struct ProcessManager {
/// the next pid to be assigned
next_pid: u16,
/// pid of the current running process
cur_pid: u16,
cur_pid: ProcessId,
processes: Vec<Process>,
}

Expand All @@ -24,8 +22,7 @@ impl ProcessManager {
let mut processes = Vec::<Process>::new();
processes.push(init);
Self {
cur_pid: 0,
next_pid: 1,
cur_pid: ProcessId(0),
processes,
}
}
Expand All @@ -50,7 +47,7 @@ impl ProcessManager {
current.tick();
current.save(regs, sf);
}
debug!("Paused process #{}", self.cur_pid);
// debug!("Paused process #{}", self.cur_pid);
}

fn get_next_pos(&self) -> usize {
Expand All @@ -67,15 +64,15 @@ impl ProcessManager {
}
}

pub fn still_alive(&self, pid: u16) -> bool {
pub fn still_alive(&self, pid: ProcessId) -> bool {
self.processes.iter().any(|x| x.pid() == pid)
}

pub fn switch_next(&mut self, regs: &mut Registers, sf: &mut InterruptStackFrame) {
let pos = self.get_next_pos();
let p = &mut self.processes[pos];

debug!("Next process {} #{}", p.name(), p.pid());
// debug!("Next process {} #{}", p.name(), p.pid());
if p.pid() == self.cur_pid {
// the next process to be resumed is the same as the current one
p.resume();
Expand All @@ -90,12 +87,11 @@ impl ProcessManager {
&mut self,
elf: &ElfFile,
name: String,
parent: u16,
parent: ProcessId,
proc_data: Option<ProcessData>,
) -> u16 {
) -> ProcessId {
let mut p = Process::new(
&mut *crate::memory::get_frame_alloc_for_sure(),
self.next_pid,
name,
parent,
proc_data,
Expand All @@ -110,7 +106,6 @@ impl ProcessManager {
// info!("Spawn process:\n\n{:?}\n", p);
let pid = p.pid();
self.processes.push(p);
self.next_pid += 1; // TODO: recycle PID
pid
}

Expand All @@ -119,23 +114,20 @@ impl ProcessManager {
entry: VirtAddr,
stack_top: VirtAddr,
name: String,
parent: u16,
parent: ProcessId,
proc_data: Option<ProcessData>,
) -> u16 {
) -> ProcessId {
let mut p = Process::new(
&mut *crate::memory::get_frame_alloc_for_sure(),
self.next_pid,
name,
parent,
proc_data,
);
p.pause();
p.init_stack_frame(entry, stack_top);
info!("Spawn process: {}#{}", p.name(), p.pid());
// info!("Spawn process:\n\n{:?}\n", p);
let pid = p.pid();
self.processes.push(p);
self.next_pid += 1; // TODO: recycle PID
pid
}

Expand Down
Loading

0 comments on commit 3732a89

Please sign in to comment.