From 07393d8c7152254bbf394b6234926486a7aad575 Mon Sep 17 00:00:00 2001 From: pjht Date: Fri, 6 Sep 2024 10:45:48 -0500 Subject: [PATCH] Show argv[0] when printing process memory usage --- src/tasking.rs | 40 +++++++++++++++++++++++++++------------- 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/src/tasking.rs b/src/tasking.rs index 2a71d7b..befa0d8 100644 --- a/src/tasking.rs +++ b/src/tasking.rs @@ -2,7 +2,9 @@ use crate::{ gdt, println, qemu_exit, virtual_memory::{ASpaceMutex, AddressSpace, PagingError, ACTIVE_SPACE, KERNEL_SPACE}, }; -use alloc::{borrow::ToOwned, boxed::Box, collections::VecDeque, ffi::CString, vec::Vec}; +use alloc::{ + borrow::ToOwned, boxed::Box, collections::VecDeque, ffi::CString, string::ToString, vec::Vec, +}; use core::{ alloc::Layout, arch::asm, @@ -14,7 +16,6 @@ use humansize::{SizeFormatter, BINARY}; use slab::Slab; use spin::{Lazy, Mutex, RwLock}; use x86_64::{ - instructions::interrupts, structures::paging::{Page, PageTableFlags}, VirtAddr, }; @@ -64,17 +65,17 @@ extern "C" fn switch_to_asm_exit(next_stack: *mut usize) { extern "C" fn task_init() { unsafe { asm!( - "pop rcx", // Get the user stack pointer - "pop rbx", // Get the entry point - "push 43", // Push the stack segment selector - same as data + "pop rcx", // Get the user stack pointer + "pop rbx", // Get the entry point + "push 43", // Push the stack segment selector - same as data "push rcx", // Push the stack pointer - "pushfq", // Get the flags into RAX + "pushfq", // Get the flags into RAX "pop rax", "or rax, 0x200", // Enable interrupts in the stored copy - "push rax", // Push the flags - "push 51", // Push the code selector - "push rbx", // Push the entry point - "iretq", // Return from the fake interrupt and enter user mode + "push rax", // Push the flags + "push 51", // Push the code selector + "push rbx", // Push the entry point + "iretq", // Return from the fake interrupt and enter user mode options(noreturn) ) } @@ -168,8 +169,9 @@ impl Tasking { )] args_layout.align_to(4096).unwrap().pad_to_align() }; - let user_arg_mem = - address_space.map_free(args_layout.size() / 4096, PageTableFlags::USER_ACCESSIBLE)?; + let user_arg_mem = KERNEL_SPACE + .lock() + .map_free(args_layout.size() / 4096, PageTableFlags::USER_ACCESSIBLE)?; address_space.run(|| unsafe { let mut ptr_ptr: *mut *const u8 = user_arg_mem.cast(); for (&offset, argument) in arg_offsets.iter().zip(arguments.iter()) { @@ -391,7 +393,19 @@ impl Tasking { |space| space.get_bytes_allocated(), ); total += bytes_used; - println!("[TASKING] PID {} used {}", i, SizeFormatter::new(bytes_used, BINARY),); + let name = if process.arguments.1 > 0 { + unsafe { CStr::from_ptr(process.arguments.0.read().cast()) } + .to_string_lossy() + .into_owned() + } else { + "UNKNOWN".to_string() + }; + println!( + "[TASKING] PID {} ({}) used {}", + i, + name, + SizeFormatter::new(bytes_used, BINARY), + ); } println!("[TASKING] Total used {} ({})", SizeFormatter::new(total, BINARY), total / 4096); }