Commit 5e95eaa3 authored by Nifou's avatar Nifou

Deallocate all used frames when a process exits

parent 6dc48538
......@@ -17,7 +17,10 @@
//! Definition of a Process which regroups a set of resources (memory, ...)
use core::sync::atomic::{AtomicUsize, Ordering};
use alloc::prelude::v1::Vec;
use x86_64::structures::paging::page_table::PageTableFlags as Flags;
use x86_64::structures::paging::{
FrameDeallocator,
page_table::PageTableFlags as Flags,
};
use crate::memory;
use super::{
......@@ -107,6 +110,32 @@ impl Process {
(process, rsp, info.first_arg_addr)
}
/// Exit handler called when the process exits
pub fn exit(&self) {
// Deallocate all used frames
for area in &self.memory.areas {
// If the memory is used by the kernel, don't deallocate this area!
if !area.flags.contains(Flags::USER_ACCESSIBLE) {
continue;
}
// If the memory is shared with the parent, don't deallocate this area!
if area.flags.contains(Flags::BIT_9) && self.parent.is_some() {
continue;
}
for mapping in &area.mappings {
unsafe {
memory::FRAME_ALLOCATOR
.r#try()
.unwrap()
.lock()
.deallocate_frame(mapping.0);
}
}
}
}
}
#[derive(Debug, Clone)]
......
......@@ -59,10 +59,10 @@ unsafe impl Sync for SchedulerCurrentAlgo {}
unsafe impl Send for SchedulerCurrentAlgo {}
/// If `all_threads` is false, remove the current thread and switch to the next thread
/// (do the job of the `exit` syscall)
/// (does the job of the `exit` syscall)
///
/// Else, remove all threads of the process of the current thread and switch to the next thread ()
/// (do the job of the `exit_group` syscall)
/// Else, remove all threads of the process of the current thread and switch to the next thread
/// (does the job of the `exit_group` syscall)
pub fn exit(all_threads: bool) {
let mut processes = PROCESSES.write();
let tid = SCHEDULER.get().current().expect("No current process").tid;
......@@ -80,6 +80,7 @@ pub fn exit(all_threads: bool) {
// If there is no other threads, remove the process
if processes.get(&pid).unwrap().threads.is_empty() {
processes.get(&pid).unwrap().exit();
processes.remove(&pid);
}
......
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