Skip to content

Commit

Permalink
Added ps process.
Browse files Browse the repository at this point in the history
Added printheap option to shell.
  • Loading branch information
arbel03 committed May 5, 2018
1 parent b9ffc37 commit ba061f7
Show file tree
Hide file tree
Showing 17 changed files with 251 additions and 84 deletions.
4 changes: 4 additions & 0 deletions bitmap_allocator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ impl BitmapAllocator {
pub fn get_block_count(&self) -> usize {
return self.block_count;
}

pub fn get_block_size(&self) -> usize {
return self.block_size;
}
}

unsafe impl<'a> Alloc for &'a BitmapAllocator {
Expand Down
6 changes: 5 additions & 1 deletion filesystem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ path = "ls.rs"

[[bin]]
name = "stat"
path = "stat.rs"
path = "stat.rs"

[[bin]]
name = "ps"
path = "ps.rs"
2 changes: 1 addition & 1 deletion filesystem/Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bin_target := i686-bin
bin_names := shell cat ls stat
bin_names := shell cat ls stat ps
bin_files := $(patsubst %, filesystem/target/$(bin_target)/debug/%, $(bin_names))

.PHONY: filesystem
Expand Down
2 changes: 1 addition & 1 deletion filesystem/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn main(argc: usize, argv: *const *const u8) {
} else {
println!("Printing contents of \"{}\":", file_name);
let file_stat = std::syscalls::stat(file_name, 0);
let mut vector = vec![0u8;file_stat.directory_size];
let mut vector = vec![0u8;file_stat.directory_size as usize];
std::syscalls::read(fd, &mut vector);
println!("{}", unsafe { str::from_utf8_unchecked(&vector) });
}
Expand Down
33 changes: 15 additions & 18 deletions filesystem/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ extern crate alloc;
extern crate std;

use std::io;
use alloc::Vec;
use std::syscalls::Stat;
use alloc::string::ToString;
use alloc::string::String;
Expand All @@ -27,36 +26,34 @@ pub fn main(argc: usize, argv: *const *const u8) {
recursive_ls(".", 0);
} else {
let stat = std::syscalls::stat(&second_param, 0);
for i in 0..stat.child_nodes_count {
for i in 0..stat.child_nodes_count as usize {
println!("{}", read_name(&second_param, i).1);
}
}
}

pub fn recursive_ls(path: &str, level: usize) {
let current_status = std::syscalls::stat(path, 0);
unsafe {
for child in 0..current_status.child_nodes_count {
let (stat, name) = read_name(path, child);
println!("{}{}", "\t".repeat(level) ,name);
if stat.is_folder && name != "." && name != ".." {
let mut current_path = if path == "." {
String::new()
} else {
let mut new_path = path.to_string();
new_path.push_str("/");
new_path
};
current_path.push_str(&name);
recursive_ls(&current_path, level + 1);
}
for child in 0..current_status.child_nodes_count as usize {
let (stat, name) = read_name(path, child);
println!("{}{}", "\t".repeat(level) ,name);
if stat.is_folder && name != "." && name != ".." {
let mut current_path = if path == "." {
String::new()
} else {
let mut new_path = path.to_string();
new_path.push_str("/");
new_path
};
current_path.push_str(&name);
recursive_ls(&current_path, level + 1);
}
}
}

pub fn read_name(parent_directory: &str, child_node: usize) -> (Stat, String) {
let child_status = std::syscalls::stat(parent_directory, child_node+1);
let mut name = vec![0u8;child_status.directory_name_length];
let mut name = vec![0u8;child_status.directory_name_length as usize];
unsafe {
std::syscalls::read_name(parent_directory, &mut name, child_node);
let string = ::core::str::from_utf8_unchecked(&name);
Expand Down
30 changes: 30 additions & 0 deletions filesystem/ps.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#![feature(alloc)]
#![feature(start)]
#![no_main]
#![no_std]

#[macro_use]
extern crate std;

use std::io;

#[no_mangle]
pub unsafe fn main(_argc: usize, _argv: *const *const u8) {
let mut occupied_size = 0;
for i in 0..100 {
if let Some(proc_info) = std::syscalls::proc_info(i) {
println!("Process number {}", i);
println!("\tProcess start: {:#x}", proc_info.process_base);
println!("\tProcess end: {:#x}", proc_info.process_total_size);
occupied_size += proc_info.process_total_size as usize;
} else {
break;
}
}
let total_size = std::syscalls::get_proccess_area_size();
let free = total_size - occupied_size;
println!("\n===== Process area info =====");
println!("Total: {} bytes.", total_size);
println!("Free: {} bytes.", free);
println!("Occupied: {} bytes.", occupied_size);
}
51 changes: 50 additions & 1 deletion filesystem/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,14 @@ pub fn main(argc: usize, argv: *const *const u8) {
}
},
"help" => {
println!("Available commands:\n\t-echo\n\t-help");
println!("Available commands:\n\t-echo\n\t-help\n\t-printheap\n\t-bin/ls\n\t-bin/cat\n\t-bin/stat\n\t-bin/ps");
},
"printheap" => {
println!("Enter heap read start and read size.");
println!("Heap size is {}. Don't go out of bounds.", std::HEAP_SIZE);
let start = input_number("Enter read start");
let size = input_number("Enter read size");
unsafe { print_heap(start, size); }
},
_ => {
let result = run_exec(command[0], &command[1..]);
Expand All @@ -45,4 +52,46 @@ pub fn main(argc: usize, argv: *const *const u8) {

fn run_exec(path_name: &str, args: &[&str]) -> usize {
std::syscalls::execv(path_name, args)
}

pub fn input_number(prompt: &str) -> usize {
loop {
print!("{}: ", prompt);
if let Ok(number) = io::read_string().parse() {
return number;
} else {
println!("Please enter a valid number.");
}
}
}

pub unsafe fn print_heap(start: usize, size: usize) {
use std::HEAP_SIZE;
use std::HEAP_AREA;
// use bitmap_allocator::CellState;
// let allocator = &HEAP;
// println!("Printing bitmap:");
// let bitmap_size = allocator.get_block_count();
// for index in 0..bitmap_size {
// let block = allocator.get_cell(index).clone();
// let block_string = match block {
// CellState::Free => "_",
// CellState::Boundary => "<",
// CellState::Allocated => "=",
// };
// print!("{}", block_string);
// }
// print!("\n");
if start >= HEAP_SIZE {
println!("Can't start reading heap from outside of bounds.");
println!("Please choose a lower value than {}.", HEAP_SIZE);
} else if start + size > HEAP_SIZE {
println!("End of read is at {}", start+size);
println!("Maximum bytes that can be read is {}", HEAP_SIZE - start);
} else {
let ptr = (&HEAP_AREA).as_ptr();
let len = HEAP_SIZE;
let string = core::str::from_utf8_unchecked(core::slice::from_raw_parts(ptr, len));
println!("{}", &string[start..start+size]);
}
}
2 changes: 1 addition & 1 deletion filesystem/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use std::io;
pub unsafe fn main(argc: usize, argv: *const *const u8) {
let args = &std::args::get_args(argc, argv);
if args.len() == 1 {
println!("Usage:\n\t{0} DIRECTORY_NAME\n\tDIRECTORY_NAME can be either a folder or a file.\n\t This command prints information about the given directory.", args[0]);
println!("Usage:\n\t{0} DIRECTORY_NAME\n\t\tDIRECTORY_NAME can be either a folder or a file.\n\t\t This command prints information about the given directory.", args[0]);
return;
}
let stat = std::syscalls::stat(args[1], 0);
Expand Down
24 changes: 4 additions & 20 deletions filesystem/std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub mod syscalls;
// Setting up heap
use bitmap_allocator::BitmapAllocator;

const HEAP_SIZE: usize = 1024*20;
static mut HEAP_AREA: [u8;HEAP_SIZE] = [0u8;HEAP_SIZE];
pub const HEAP_SIZE: usize = 1024*20;
pub static mut HEAP_AREA: [u8;HEAP_SIZE] = [0u8;HEAP_SIZE];

#[global_allocator]
static mut HEAP: BitmapAllocator = BitmapAllocator::new(0, HEAP_SIZE, ::core::mem::size_of::<usize>());
Expand All @@ -42,23 +42,6 @@ unsafe fn exit() {
asm!("int 0x82" :::: "intel");
}

// pub unsafe fn print_heap() {
// use bitmap_allocator::CellState;
// let allocator = &HEAP;
// println!("Printing bitmap:");
// let bitmap_size = allocator.get_block_count();
// for index in 0..bitmap_size {
// let block = allocator.get_cell(index).clone();
// let block_string = match block {
// CellState::Free => "_",
// CellState::Boundary => "<",
// CellState::Allocated => "=",
// };
// print!("{}", block_string);
// }
// print!("\n");
// }

#[lang = "eh_personality"]
extern fn eh_personality() {

Expand All @@ -67,5 +50,6 @@ extern fn eh_personality() {
#[no_mangle]
#[lang = "panic_fmt"]
pub extern fn panic_fmt(_fmt: core::fmt::Arguments, _file: &'static str, _line: u32) -> ! {
loop {}
unsafe { exit(); }
loop {};
}
14 changes: 7 additions & 7 deletions filesystem/std/src/syscalls/fs.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
#[repr(packed)]
#[derive(Debug)]
pub struct Stat {
pub directory_name_length: usize,
pub directory_size: usize,
pub directory_name_length: u32,
pub directory_size: u32,
pub is_folder: bool,
pub child_nodes_count: usize,
pub child_nodes_count: u32,
}

pub fn open(file_path: &str) -> usize {
use syscalls::syscall::syscall2;
// SYSCALL(SYS_FOPEN, ptr, size)
// SYSCALL(FS_OPEN, ptr, size)
unsafe {
syscall2(0x1, file_path.as_ptr() as usize, file_path.len())
}
}

pub fn read(fd: usize, buffer: &mut [u8]) -> usize {
use syscalls::syscall::syscall3;
// SYSCALL(SYS_READ, fd, ptr, size)
// SYSCALL(FS_READ, fd, ptr, size)
unsafe {
syscall3(0x3, fd, buffer.as_ptr() as usize, buffer.len())
}
}

pub fn stat(parent_directory: &str, child_node: usize) -> Stat {
// SYSCALL(SYS_STAT, ptr, size, stat_structure_ptr, child_node)
// SYSCALL(FS_STAT, ptr, size, stat_structure_ptr, child_node)
use syscalls::syscall::syscall4;

let mut stat = Stat {
Expand All @@ -41,7 +41,7 @@ pub fn stat(parent_directory: &str, child_node: usize) -> Stat {
}

pub fn read_name(parent_directory: &str, read_buffer: &mut [u8], child_node: usize) {
// SYSCALL(SYS_STAT, ptr, size, read_buffer_ptr, read_buffer_size, child_node)
// SYSCALL(FS_DIR_NAME, ptr, size, read_buffer_ptr, read_buffer_size, child_node)
use syscalls::syscall::syscall5;

unsafe {
Expand Down
34 changes: 34 additions & 0 deletions filesystem/std/src/syscalls/task.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
#[repr(packed)]
#[derive(Debug, Default)]
pub struct ProcInfo {
pub process_index: u32,
pub process_name_length: u32,
pub process_base: u32,
pub process_total_size: u32,
pub arguments_count: u32,
pub process_stack_size: u32,
}

pub fn execv(path_name: &str, args: &[&str]) -> usize {
// SYSCALL(PROC_EXECV, path_ptr, path_len, args_ptr, args_len)
use syscalls::syscall::syscall4;
use alloc::{ Vec, String };
let mut arguments: Vec<String> = Vec::new();
Expand All @@ -15,3 +27,25 @@ pub fn execv(path_name: &str, args: &[&str]) -> usize {
syscall4(0x07, path_name.as_ptr() as usize, path_name.len(), ptr_list.as_ptr() as usize, args.len())
}
}

pub fn proc_info(proc_index: usize) -> Option<ProcInfo> {
// SYSCALL(PROC_INFO, proc_info_ptr, proc_index)
use syscalls::syscall::syscall2;

let proc_info = ProcInfo::default();

let result = unsafe { syscall2(0x09, &proc_info as *const ProcInfo as usize, proc_index) };
if result == 0xffffffff {
None
} else {
Some(proc_info)
}
}

pub fn get_proccess_area_size() -> usize {
// SYSCALL(PROC_SIZE)
use syscalls::syscall::syscall0;
unsafe {
syscall0(0x10)
}
}
14 changes: 7 additions & 7 deletions src/syscall/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ pub unsafe fn stat(directory_path: &str, stat_ptr: *mut u8, child_node: usize) -

#[repr(packed)]
pub struct Stat {
pub directory_name_length: usize,
pub directory_size: usize,
pub directory_name_length: u32,
pub directory_size: u32,
pub is_folder: bool,
pub child_nodes_count: usize,
pub child_nodes_count: u32,
}

let parent_directory = if directory_path == "." {
Expand All @@ -42,17 +42,17 @@ pub unsafe fn stat(directory_path: &str, stat_ptr: *mut u8, child_node: usize) -
};

let mut stat = Stat {
directory_name_length: directory.get_name().len(),
directory_size: directory.get_size(),
directory_name_length: directory.get_name().len() as u32,
directory_size: directory.get_size() as u32,
is_folder: directory.get_fat_dir().is_folder(),
child_nodes_count: 0,
};

if directory.get_fat_dir().is_folder() {
if directory.get_fat_dir().get_cluster() == 0 {
stat.child_nodes_count = child_dirs.len();
stat.child_nodes_count = child_dirs.len() as u32;
} else {
stat.child_nodes_count = FILESYSTEM.as_mut().unwrap().get_child_directories(&directory).len();
stat.child_nodes_count = FILESYSTEM.as_mut().unwrap().get_child_directories(&directory).len() as u32;
}
}
ptr::write(stat_ptr as *mut Stat, stat);
Expand Down
Loading

0 comments on commit ba061f7

Please sign in to comment.