diff --git a/src/arch/x86_64/interrupt/syscall.rs b/src/arch/x86_64/interrupt/syscall.rs index eadd57ade98dfa87e2a7bcdcea340dffb1bcaf4b..3fd565db36b1d7456bd8acff26cc79040358aafe 100644 --- a/src/arch/x86_64/interrupt/syscall.rs +++ b/src/arch/x86_64/interrupt/syscall.rs @@ -142,7 +142,7 @@ interrupt_stack!(syscall, |stack| { let context = contexts.current(); if let Some(current) = context { let current = current.read(); - println!("Warning: Context {} used deprecated `int 0x80` construct", *current.name.read()); + println!("Warning: Context {} used deprecated `int 0x80` construct", current.name); } else { println!("Warning: Unknown context used deprecated `int 0x80` construct"); } diff --git a/src/context/context.rs b/src/context/context.rs index 374edc23a4d884ee2f09d8150fe48d9ee7e87632..c2a5a6421a4d98005073328334eb8b52e7c4b07b 100644 --- a/src/context/context.rs +++ b/src/context/context.rs @@ -1,9 +1,8 @@ use alloc::{ boxed::Box, collections::VecDeque, - string::{String}, sync::Arc, - vec::Vec, + vec::Vec, borrow::Cow, }; use core::{ alloc::GlobalAlloc, @@ -128,7 +127,7 @@ pub struct ContextSnapshot { impl ContextSnapshot { //TODO: Should this accept &mut Context to ensure name/files will not change? pub fn new(context: &Context) -> Self { - let name = context.name.read().clone(); + let name = context.name.clone().into_owned().into_boxed_str(); let mut files = Vec::new(); for descriptor_opt in context.files.read().iter() { let description = if let Some(descriptor) = descriptor_opt { @@ -240,7 +239,8 @@ pub struct Context { /// mappings are universal and independent on address spaces or contexts. pub addr_space: Option<Arc<RwLock<AddrSpace>>>, /// The name of the context - pub name: Arc<RwLock<Box<str>>>, + // TODO: fixed size ArrayString? + pub name: Cow<'static, str>, /// The open files in the scheme pub files: Arc<RwLock<Vec<Option<FileDescriptor>>>>, /// Signal actions @@ -372,7 +372,7 @@ impl Context { ksig: None, ksig_restore: false, addr_space: None, - name: Arc::new(RwLock::new(String::new().into_boxed_str())), + name: Cow::Borrowed(""), files: Arc::new(RwLock::new(Vec::new())), actions: Self::empty_actions(), regs: None, diff --git a/src/context/mod.rs b/src/context/mod.rs index 7f5fc849deec38477f80b63445d1453d62fe5b6e..5a00526fcaa46e0030a695b390e1529f1580145c 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -3,6 +3,7 @@ //! For resources on contexts, please consult [wikipedia](https://en.wikipedia.org/wiki/Context_switch) and [osdev](https://wiki.osdev.org/Context_Switching) use core::sync::atomic::Ordering; +use alloc::borrow::Cow; use alloc::sync::Arc; use spin::{RwLock, RwLockReadGuard, RwLockWriteGuard}; @@ -69,6 +70,7 @@ pub fn init() { let context_lock = contexts.insert_context_raw(id).expect("could not initialize first context"); let mut context = context_lock.write(); context.sched_affinity = Some(crate::cpu_id()); + context.name = Cow::Borrowed("kmain"); self::arch::EMPTY_CR3.call_once(|| unsafe { RmmA::table(TableKind::User) }); diff --git a/src/debugger.rs b/src/debugger.rs index 2c92efaca105529a9d16dc02b4f5353e237ba0b7..0e137bc256c74409eb6246c93ed3abf07e1e749e 100644 --- a/src/debugger.rs +++ b/src/debugger.rs @@ -161,7 +161,7 @@ pub unsafe fn debugger(target_id: Option<crate::context::ContextId>) { for (id, context_lock) in crate::context::contexts().iter() { if target_id.map_or(false, |target_id| *id != target_id) { continue; } let context = context_lock.read(); - println!("{}: {}", (*id).into(), context.name.read()); + println!("{}: {}", (*id).into(), context.name); // Switch to context page table to ensure syscall debug and stack dump will work if let Some(ref space) = context.addr_space { diff --git a/src/lib.rs b/src/lib.rs index 3c03ae532b39c79d035ca33232258bbc4a50a60a..1e9413013b896bea83adc3d1eeda7f73ad52ae63 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -73,7 +73,6 @@ extern crate spin; #[cfg(feature = "slab")] extern crate slab_allocator; -use alloc::string::ToString; use core::sync::atomic::{AtomicUsize, Ordering}; use crate::scheme::SchemeNamespace; @@ -205,7 +204,7 @@ pub fn kmain(cpus: usize, bootstrap: Bootstrap) -> ! { context.rns = SchemeNamespace::from(1); context.ens = SchemeNamespace::from(1); context.status = context::Status::Runnable; - *context.name.write() = "bootstrap".to_string().into_boxed_str(); + context.name = "bootstrap".into(); }, Err(err) => { panic!("failed to spawn userspace_init: {:?}", err); @@ -267,7 +266,7 @@ pub extern fn ksignal(signal: usize) { let contexts = context::contexts(); if let Some(context_lock) = contexts.current() { let context = context_lock.read(); - info!("NAME {}", *context.name.read()); + info!("NAME {}", context.name); } } diff --git a/src/panic.rs b/src/panic.rs index fad1d1c8fcc0cf653effc43d147455d7a4303cb0..3655327fe11a8be402e64633962f3a4a1f1da7a1 100644 --- a/src/panic.rs +++ b/src/panic.rs @@ -24,7 +24,7 @@ pub extern "C" fn rust_begin_unwind(info: &PanicInfo) -> ! { let contexts = context::contexts(); if let Some(context_lock) = contexts.current() { let context = context_lock.read(); - println!("NAME: {}", *context.name.read()); + println!("NAME: {}", context.name); if let Some((a, b, c, d, e, f)) = context.syscall { println!("SYSCALL: {}", syscall::debug::format_call(a, b, c, d, e, f)); diff --git a/src/scheme/proc.rs b/src/scheme/proc.rs index 176eac07df06a5a0eedcd09eaea0ee570a0b8895..d4bbc6acfe1d958fde88a13da9384765e23eb24d 100644 --- a/src/scheme/proc.rs +++ b/src/scheme/proc.rs @@ -324,7 +324,7 @@ impl ProcScheme { Operation::Memory { .. } => OperationData::Memory(MemData::default()), Operation::Trace => OperationData::Trace(TraceData::default()), Operation::Static(_) => OperationData::Static(StaticData::new( - target.name.read().clone().into() + target.name.clone().into_owned().into_bytes().into() )), Operation::AddrSpace { .. } => OperationData::Offset(0), _ => OperationData::Other, @@ -814,7 +814,7 @@ impl Scheme for ProcScheme { // Return read events Ok(read * mem::size_of::<PtraceEvent>()) } - Operation::Name => read_from(buf, context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.read().name.read().as_bytes(), &mut 0), + Operation::Name => read_from(buf, context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.read().name.as_bytes(), &mut 0), Operation::Sigstack => read_from(buf, &context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.read().sigstack.unwrap_or(!0).to_ne_bytes(), &mut 0), Operation::Attr(attr) => { let src_buf = match (attr, &*Arc::clone(context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?).read()) { @@ -1031,8 +1031,8 @@ impl Scheme for ProcScheme { Ok(mem::size_of::<u64>()) }, Operation::Name => { - let utf8 = alloc::string::String::from_utf8(buf.to_vec()).map_err(|_| Error::new(EINVAL))?.into_boxed_str(); - *context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.read().name.write() = utf8; + let utf8 = alloc::string::String::from_utf8(buf.to_vec()).map_err(|_| Error::new(EINVAL))?; + context::contexts().get(info.pid).ok_or(Error::new(ESRCH))?.write().name = utf8.into(); Ok(buf.len()) } Operation::Sigstack => { diff --git a/src/scheme/sys/block.rs b/src/scheme/sys/block.rs index 2d58e1619230950457bf1233342e31cdf2e4837b..b02c139661695a8cb83349d4f194746e96c46861 100644 --- a/src/scheme/sys/block.rs +++ b/src/scheme/sys/block.rs @@ -14,7 +14,7 @@ pub fn resource() -> Result<Vec<u8>> { let contexts = context::contexts(); for (id, context_lock) in contexts.iter() { let context = context_lock.read(); - rows.push((*id, context.name.read().clone(), context.status_reason)); + rows.push((*id, context.name.clone(), context.status_reason)); } } diff --git a/src/scheme/sys/context.rs b/src/scheme/sys/context.rs index dacc831bb5e3fd03a498fe7f309af71f9c292666..cb3d92796341cc06528ab04d5e05509ee8af70dc 100644 --- a/src/scheme/sys/context.rs +++ b/src/scheme/sys/context.rs @@ -116,7 +116,7 @@ pub fn resource() -> Result<Vec<u8>> { affinity, cpu_time_string, memory_string, - *context.name.read())); + context.name)); } } diff --git a/src/scheme/sys/exe.rs b/src/scheme/sys/exe.rs index 43aaf3545d68bd262131c269a61c6f8d6c5c2a74..6e01c7e1843da3a56edb531afde4ed32bfdf199f 100644 --- a/src/scheme/sys/exe.rs +++ b/src/scheme/sys/exe.rs @@ -1,19 +1,8 @@ -use alloc::{ - boxed::Box, - vec::Vec, -}; +use alloc::vec::Vec; use crate::context; -use crate::syscall::error::{Error, ESRCH, Result}; +use crate::syscall::error::Result; pub fn resource() -> Result<Vec<u8>> { - let name = { - let contexts = context::contexts(); - let context_lock = contexts.current().ok_or(Error::new(ESRCH))?; - let context = context_lock.read(); - let name = context.name.read(); - let name_bytes: Box<[u8]> = name.clone().into(); - name_bytes.into_vec() - }; - Ok(name) + Ok(context::current()?.read().name.clone().into_owned().into_bytes()) } diff --git a/src/scheme/sys/iostat.rs b/src/scheme/sys/iostat.rs index fe0d65fa655730f09cc928613f22a0ca491a1804..a9f06ed0770a574bbf1a07dd4ab86bd6bd4730a3 100644 --- a/src/scheme/sys/iostat.rs +++ b/src/scheme/sys/iostat.rs @@ -16,7 +16,7 @@ pub fn resource() -> Result<Vec<u8>> { let contexts = context::contexts(); for (id, context_lock) in contexts.iter() { let context = context_lock.read(); - rows.push((*id, context.name.read().clone(), context.files.read().clone())); + rows.push((*id, context.name.clone(), context.files.read().clone())); } } diff --git a/src/scheme/sys/syscall.rs b/src/scheme/sys/syscall.rs index 77f60959792e2f44a3e3d90773bc0cb2b7d4e4a9..5f3184f19c923337f37311b19fac8455aba87b29 100644 --- a/src/scheme/sys/syscall.rs +++ b/src/scheme/sys/syscall.rs @@ -15,7 +15,7 @@ pub fn resource() -> Result<Vec<u8>> { let contexts = context::contexts(); for (id, context_lock) in contexts.iter() { let context = context_lock.read(); - rows.push((*id, context.name.read().clone(), context.syscall)); + rows.push((*id, context.name.clone(), context.syscall)); } } diff --git a/src/scheme/user.rs b/src/scheme/user.rs index cb78da39886013ee4fa68b161133ebc90f1aff1e..e6b09e3c63eaba2084ee5075b6b57982d25d175a 100644 --- a/src/scheme/user.rs +++ b/src/scheme/user.rs @@ -294,7 +294,7 @@ impl UserInner { let context_lock = Arc::clone(context::contexts().current().ok_or(Error::new(ESRCH))?); let context = context_lock.read(); if map.size % PAGE_SIZE != 0 { - log::warn!("Unaligned map size for context {:?}", context.name.try_read().as_deref()); + log::warn!("Unaligned map size for context `{}`", context.name); } // TODO: Faster, cleaner mechanism to get descriptor let scheme = self.scheme_id.load(Ordering::SeqCst); diff --git a/src/syscall/process.rs b/src/syscall/process.rs index f7b1186dc43cde980f22c163f6fc5837a13d2492..dbd45254f13796e041855545ad48a3d3b6bb9638 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -42,7 +42,7 @@ fn empty<'lock>(context_lock: &'lock RwLock<Context>, mut context: RwLockWriteGu for grant in addr_space.grants.into_iter() { let unmap_result = if reaping { - log::error!("{}: {}: Grant should not exist: {:?}", context.id.into(), *context.name.read(), grant); + log::error!("{}: {}: Grant should not exist: {:?}", context.id.into(), context.name, grant); grant.unmap(mapper, &mut InactiveFlusher::new()) } else {