Commit 5c223784 authored by Nifou's avatar Nifou

Implement the `getcwd` syscall

parent 49d7d45e
......@@ -60,7 +60,8 @@ fn end(id: IrqId) {
#[naked]
pub extern "x86-interrupt" fn timer_interrupt_handler(_: &mut InterruptStackFrame) {
unsafe {
// Save registers which will be modified during the next lines
// Save registers which will be modified during the next lines (see this function with a
// disassembler)
llvm_asm!("
sub rsp, 16
push rdi
......
......@@ -133,6 +133,7 @@ const FORK: usize = 57;
const EXECVE: usize = 59;
const EXIT: usize = 60;
const WAIT4: usize = 61;
const GETCWD: usize = 79;
const ARCH_PRCTL: usize = 158;
const GETTID: usize = 186;
const TKILL: usize = 200;
......@@ -173,6 +174,7 @@ impl Syscall {
EXECVE => self.execve(args[0], args[1], args[2]),
EXIT => self.exit(args[0]),
WAIT4 => self.wait4(args[0], args[1], args[2], args[3]),
GETCWD => self.getcwd(args[0], args[1]),
ARCH_PRCTL => self.arch_prctl(args[0], args[1]),
GETTID => self.gettid(),
TKILL => self.tkill(args[0], args[1]),
......
......@@ -152,6 +152,24 @@ impl Syscall {
Ok(SCHEDULER.get().current().unwrap().tid.0)
}
/// Get the working directory
pub fn getcwd(&self, buf: usize, size: usize) -> Result {
println!("getcwd({:#x}, {}) = {:#x}", buf, size, buf);
let processes = PROCESSES.read();
let current = SCHEDULER.get().current().unwrap().process;
let cwd = &processes.get(&current).unwrap().cwd;
let ptr = buf as *mut u8;
let addr = cwd.as_ptr();
let len = cwd.len();
unsafe {
ptr.copy_from(addr, len);
ptr.add(len).write(0);
}
Ok(buf)
}
/// Create a new child process from the current
pub fn fork(&self) -> Result {
print!("fork() = ");
......
......@@ -15,7 +15,7 @@
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Definition of a Process which regroups a set of resources (memory, ...)
use alloc::prelude::v1::Vec;
use alloc::prelude::v1::{Vec, String};
use core::sync::atomic::{AtomicUsize, Ordering};
use x86_64::structures::paging::{page_table::PageTableFlags as Flags, FrameDeallocator};
......@@ -65,6 +65,9 @@ pub struct Process {
/// Info about the process (arguments, environment, ...)
pub info: ProcessInfo,
/// The working directory
pub cwd: String,
/// Threads of the process
pub threads: Vec<Tid>,
......@@ -92,6 +95,7 @@ impl Process {
state: ProcessState::Alive,
memory: ProcessMemory::new(),
info: info.clone(),
cwd: String::from("/"),
threads: vec![],
children: vec![],
parent: None,
......
......@@ -15,7 +15,13 @@
* along with this program. If not, see https://www.gnu.org/licenses.
*/
//! Definition of a thread which will be chosen (by the scheduler) and executed
use alloc::prelude::v1::Box;
use alloc::{
prelude::v1::Box,
alloc::{
Layout,
GlobalAlloc,
},
};
use core::{
mem::{size_of, transmute},
sync::atomic::{AtomicUsize, Ordering},
......@@ -28,7 +34,10 @@ use x86_64::{
use super::{context::Registers, info::StackWriter, process::Pid};
use crate::{
gdt,
memory::{Mappings, KERNEL_MAPPER},
memory::{
heap::HEAP_ALLOCATOR,
Mappings, KERNEL_MAPPER
},
};
/// The next Thread ID which will be used
......@@ -60,7 +69,7 @@ impl Thread {
/// Create a new Thread
pub fn new(process: Pid, rip: usize, rsp: usize, cr3: usize, argc: usize, argv: u64) -> Self {
let kstack_pointer =
Box::into_raw(Box::new(vec![0; KSTACK_SIZE])) as u64 + KSTACK_SIZE as u64;
Box::into_raw(unsafe { Box::from_raw(HEAP_ALLOCATOR.alloc(Layout::from_size_align_unchecked(KSTACK_SIZE, 4096)) as *mut [u8; KSTACK_SIZE]) }) as u64 + KSTACK_SIZE as u64;
// Create a new set of registers
let mut regs = Registers::default();
......@@ -97,7 +106,7 @@ impl Thread {
/// Create a new Thread used during the `fork` syscall
pub fn fork(process: Pid, mut regs: Registers) -> (Self, Mappings) {
let kstack_pointer =
Box::into_raw(Box::new(vec![0; KSTACK_SIZE])) as u64 + KSTACK_SIZE as u64;
Box::into_raw(unsafe { Box::from_raw(HEAP_ALLOCATOR.alloc(Layout::from_size_align_unchecked(KSTACK_SIZE, 4096)) as *mut [u8; KSTACK_SIZE]) }) as u64 + KSTACK_SIZE as u64;
// Set 0 in rax (return value of the `fork` syscall in the child process)
regs.rax = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment