1
0
mirror of https://github.com/sgmarz/osblog.git synced 2024-11-24 02:16:19 +04:00

Auto-drop processes, and added delete process.

This commit is contained in:
Stephen Marz 2020-04-24 15:22:57 -04:00
parent a60d84ed98
commit 2e48b86656
4 changed files with 40 additions and 17 deletions

View File

@ -54,23 +54,13 @@ impl Page {
// If this page has been marked as the final allocation,
// this function returns true. Otherwise, it returns false.
pub fn is_last(&self) -> bool {
if self.flags & PageBits::Last.val() != 0 {
true
}
else {
false
}
self.flags & PageBits::Last.val() != 0
}
// If the page is marked as being taken (allocated), then
// this function returns true. Otherwise, it returns false.
pub fn is_taken(&self) -> bool {
if self.flags & PageBits::Taken.val() != 0 {
true
}
else {
false
}
self.flags & PageBits::Taken.val() != 0
}
// This is the opposite of is_taken().
@ -221,6 +211,8 @@ pub fn dealloc(ptr: *mut u8) {
// calculate here is the page structure, not the HEAP address!
assert!(addr >= HEAP_START && addr < ALLOC_START);
let mut p = addr as *mut Page;
// println!("PTR in is {:p}, addr is 0x{:x}", ptr, addr);
assert!((*p).is_taken(), "Freeing a non-taken page?");
// Keep clearing pages until we hit the last page.
while (*p).is_taken() && !(*p).is_last() {
(*p).clear();

View File

@ -85,6 +85,26 @@ pub fn set_waiting(pid: u16) -> bool {
retval
}
pub fn delete_process(pid: u16) {
unsafe {
if let Some(mut pl) = PROCESS_LIST.take() {
for i in 0..pl.len() {
let p = pl.get_mut(i).unwrap();
if p.get_pid() == pid {
// When the structure gets dropped, all of the
// allocations get deallocated.
pl.remove(i);
break;
}
}
// Now, we no longer need the owned Deque, so we hand it
// back by replacing the PROCESS_LIST's None with the
// Some(pl).
PROCESS_LIST.replace(pl);
}
}
}
/// We will eventually move this function out of here, but its
/// job is just to take a slot in the process list.
fn init_process() {
@ -153,7 +173,7 @@ pub fn add_kernel_process(func: fn()) {
// we start getting into multi-hart processing. For now, we want
// a process. Get it to work, then improve it!
let mut ret_proc = Process { frame: zalloc(1) as *mut TrapFrame,
stack: alloc(STACK_PAGES),
stack: zalloc(STACK_PAGES),
pid: unsafe { NEXT_PID },
root: zalloc(1) as *mut Table,
state: ProcessState::Running,
@ -340,6 +360,7 @@ impl Drop for Process {
/// Since we're storing ownership of a Process in the linked list,
/// we can cause it to deallocate automatically when it is removed.
fn drop(&mut self) {
// println!("Dropping process {}", self.get_pid());
// We allocate the stack as a page.
dealloc(self.stack);
// This is unsafe, but it's at the drop stage, so we won't
@ -351,6 +372,7 @@ impl Drop for Process {
unmap(&mut *self.root);
}
dealloc(self.root as *mut u8);
dealloc(self.frame as *mut u8);
}
}

View File

@ -4,6 +4,7 @@
// 3 Jan 2020
use crate::{block::process_read, cpu::TrapFrame};
use crate::process::delete_process;
pub fn do_syscall(mepc: usize, frame: *mut TrapFrame) -> usize {
let syscall_number;
@ -62,12 +63,13 @@ pub fn do_syscall(mepc: usize, frame: *mut TrapFrame) -> usize {
// #define SYS_time 1062
// #define SYS_getmainvars 2011
match syscall_number {
0 | 93 => {
0 | 93 => unsafe {
// Exit
// Currently, we cannot kill a process, it runs forever. We will delete
// the process later and free the resources, but for now, we want to get
// used to how processes will be scheduled on the CPU.
mepc + 4
// used to how processes will be scheduled on the CPU.
delete_process((*frame).pid as u16);
0
},
1 => {
println!("Test syscall");
@ -77,6 +79,8 @@ pub fn do_syscall(mepc: usize, frame: *mut TrapFrame) -> usize {
// Read system call
// This is an asynchronous call. This will get the process going. We won't hear the answer until
// we an interrupt back.
// TODO: The buffer is a virtual memory address that needs to be translated to a physical memory
// location.
let _ = process_read(
(*frame).pid as u16,
(*frame).regs[10],

View File

@ -18,10 +18,15 @@ pub fn test_block() {
make_syscall(63, 8, buffer as usize, 1024, 1024);
for i in 0..32 {
print!("{:02x} ", buffer.add(i).read());
if (i+1) % 16 == 0 {
println!();
}
}
}
println!();
crate::kmem::kfree(buffer);
println!("Test block finished");
loop {}
unsafe {
make_syscall(93, 0, 0, 0, 0);
}
}