From f025ece6149d74b0d0be335bc2e4ee84520ce0a0 Mon Sep 17 00:00:00 2001 From: 4lDO2 <4lDO2@protonmail.com> Date: Wed, 6 Sep 2023 08:31:28 +0200 Subject: [PATCH] Replace from/into with get/new when necessary. --- src/common/int_like.rs | 3 ++- src/context/context.rs | 16 ++++++++-------- src/context/memory.rs | 7 ------- src/context/signal.rs | 4 ++-- src/debugger.rs | 2 +- src/scheme/proc.rs | 4 ++-- src/scheme/sys/context.rs | 10 +++++----- src/scheme/sys/iostat.rs | 6 +++--- src/scheme/sys/scheme_num.rs | 2 +- src/syscall/fs.rs | 4 ++-- src/syscall/mod.rs | 4 ++-- src/syscall/privilege.rs | 12 ++++++------ src/syscall/process.rs | 24 ++++++++++++------------ 13 files changed, 46 insertions(+), 52 deletions(-) diff --git a/src/common/int_like.rs b/src/common/int_like.rs index 5be7e1ab67..18981bbc7a 100644 --- a/src/common/int_like.rs +++ b/src/common/int_like.rs @@ -72,10 +72,11 @@ macro_rules! int_like { container: $backing_atomic_type::new(x.get()) } } + // TODO: Rename/replace with Default trait? #[allow(dead_code)] #[inline] pub const fn default() -> Self { - Self::new($new_type_name::from(0)) + Self::new($new_type_name::new(0)) } #[allow(dead_code)] #[inline] diff --git a/src/context/context.rs b/src/context/context.rs index 29ca6049da..e939080988 100644 --- a/src/context/context.rs +++ b/src/context/context.rs @@ -415,8 +415,8 @@ impl Context { /// Get a file pub fn get_file(&self, i: FileHandle) -> Option { let files = self.files.read(); - if i.into() < files.len() { - files[i.into()].clone() + if i.get() < files.len() { + files[i.get()].clone() } else { None } @@ -426,12 +426,12 @@ impl Context { /// Return the file descriptor number or None if the slot was not empty, or i was invalid pub fn insert_file(&self, i: FileHandle, file: FileDescriptor) -> Option { let mut files = self.files.write(); - if i.into() < super::CONTEXT_MAX_FILES { - while i.into() >= files.len() { + if i.get() < super::CONTEXT_MAX_FILES { + while i.get() >= files.len() { files.push(None); } - if files[i.into()].is_none() { - files[i.into()] = Some(file); + if files[i.get()].is_none() { + files[i.get()] = Some(file); Some(i) } else { None @@ -445,8 +445,8 @@ impl Context { // TODO: adjust files vector to smaller size if possible pub fn remove_file(&self, i: FileHandle) -> Option { let mut files = self.files.write(); - if i.into() < files.len() { - files[i.into()].take() + if i.get() < files.len() { + files[i.get()].take() } else { None } diff --git a/src/context/memory.rs b/src/context/memory.rs index 92127c300b..8e3761a362 100644 --- a/src/context/memory.rs +++ b/src/context/memory.rs @@ -1631,8 +1631,6 @@ fn correct_inner<'l>(addr_space_lock: &'l Arc>, mut addr_space let mut allow_writable = true; - let mut debug = false; - let frame = match grant_info.provider { Provider::Allocated { .. } | Provider::AllocatedShared { .. } if access == AccessMode::Write => { match faulting_pageinfo_opt { @@ -1673,8 +1671,6 @@ fn correct_inner<'l>(addr_space_lock: &'l Arc>, mut addr_space base.next_by(pages_from_grant_start) } Provider::External { address_space: ref foreign_address_space, src_base, .. } => { - debug = true; - let foreign_address_space = Arc::clone(foreign_address_space); if Arc::ptr_eq(addr_space_lock, &foreign_address_space) { @@ -1773,9 +1769,6 @@ fn correct_inner<'l>(addr_space_lock: &'l Arc>, mut addr_space } }; - if super::context_id().into() == 3 && debug { - //log::info!("Correcting {:?} => {:?} (base {:?} info {:?})", faulting_page, frame, grant_base, grant_info); - } let new_flags = grant_flags.write(grant_flags.has_write() && allow_writable); let Some(flush) = (unsafe { addr_space.table.utable.map_phys(faulting_page.start_address(), frame.start_address(), new_flags) }) else { // TODO diff --git a/src/context/signal.rs b/src/context/signal.rs index 2497d43a83..de19966da1 100644 --- a/src/context/signal.rs +++ b/src/context/signal.rs @@ -62,7 +62,7 @@ pub extern "C" fn signal_handler(sig: usize) { pgid: Some(pgid) }, (pid, 0xFFFF)); } else { - println!("{}: {} not found for continue", pid.into(), ppid.into()); + println!("{}: {} not found for continue", pid.get(), ppid.get()); } } }, @@ -90,7 +90,7 @@ pub extern "C" fn signal_handler(sig: usize) { pgid: Some(pgid) }, (pid, (sig << 8) | 0x7F)); } else { - println!("{}: {} not found for stop", pid.into(), ppid.into()); + println!("{}: {} not found for stop", pid.get(), ppid.get()); } } diff --git a/src/debugger.rs b/src/debugger.rs index 664bb7f20c..3476785275 100644 --- a/src/debugger.rs +++ b/src/debugger.rs @@ -191,7 +191,7 @@ pub unsafe fn debugger(target_id: Option) { 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); + println!("{}: {}", (*id).get(), 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/scheme/proc.rs b/src/scheme/proc.rs index 7a364704bc..3eb7519de7 100644 --- a/src/scheme/proc.rs +++ b/src/scheme/proc.rs @@ -554,7 +554,7 @@ impl Scheme for ProcScheme { } else if self.access == Access::Restricted { return Err(Error::new(EACCES)); } else { - ContextId::from(pid_str.parse().map_err(|_| Error::new(ENOENT))?) + ContextId::new(pid_str.parse().map_err(|_| Error::new(ENOENT))?) }; self.open_inner(pid, parts.next(), flags, uid, gid) @@ -1096,7 +1096,7 @@ impl KernelScheme for ProcScheme { let handles = self.handles.read(); let handle = handles.get(&id).ok_or(Error::new(EBADF))?; - let path = format!("proc:{}/{}", handle.info.pid.into(), match handle.info.operation { + let path = format!("proc:{}/{}", handle.info.pid.get(), match handle.info.operation { Operation::Regs(RegsKind::Float) => "regs/float", Operation::Regs(RegsKind::Int) => "regs/int", Operation::Regs(RegsKind::Env) => "regs/env", diff --git a/src/scheme/sys/context.rs b/src/scheme/sys/context.rs index c94f38de37..533470fc31 100644 --- a/src/scheme/sys/context.rs +++ b/src/scheme/sys/context.rs @@ -100,15 +100,15 @@ pub fn resource() -> Result> { }; string.push_str(&format!("{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<6}{:<12}{:<8}{}\n", - context.id.into(), - context.pgid.into(), - context.ppid.into(), + context.id.get(), + context.pgid.get(), + context.ppid.get(), context.ruid, context.rgid, - context.rns.into(), + context.rns.get(), context.euid, context.egid, - context.ens.into(), + context.ens.get(), stat_string, cpu_string, affinity, diff --git a/src/scheme/sys/iostat.rs b/src/scheme/sys/iostat.rs index a9f06ed077..ee3e96c0e8 100644 --- a/src/scheme/sys/iostat.rs +++ b/src/scheme/sys/iostat.rs @@ -38,7 +38,7 @@ pub fn resource() -> Result> { match schemes.get(description.scheme) { Some(scheme) => scheme.clone(), None => { - let _ = writeln!(string, " {:>4}: {:>8} {:>8} {:>08X}: no scheme", fd, description.scheme.into(), description.number, description.flags); + let _ = writeln!(string, " {:>4}: {:>8} {:>8} {:>08X}: no scheme", fd, description.scheme.get(), description.number, description.flags); continue; } } @@ -48,10 +48,10 @@ pub fn resource() -> Result> { match scheme.fpath(description.number, &mut fpath) { Ok(path_len) => { let fname = str::from_utf8(&fpath[..path_len]).unwrap_or("?"); - let _ = writeln!(string, "{:>6}: {:>8} {:>8} {:>08X}: {}", fd, description.scheme.into(), description.number, description.flags, fname); + let _ = writeln!(string, "{:>6}: {:>8} {:>8} {:>08X}: {}", fd, description.scheme.get(), description.number, description.flags, fname); }, Err(err) => { - let _ = writeln!(string, "{:>6}: {:>8} {:>8} {:>08X}: {}", fd, description.scheme.into(), description.number, description.flags, err); + let _ = writeln!(string, "{:>6}: {:>8} {:>8} {:>08X}: {}", fd, description.scheme.get(), description.number, description.flags, err); } } } diff --git a/src/scheme/sys/scheme_num.rs b/src/scheme/sys/scheme_num.rs index 37e5649f15..091284dcfb 100644 --- a/src/scheme/sys/scheme_num.rs +++ b/src/scheme/sys/scheme_num.rs @@ -16,7 +16,7 @@ pub fn resource() -> Result> { let schemes = scheme::schemes(); for (name, &scheme_id) in schemes.iter_name(scheme_ns) { - data.extend_from_slice(format!("{:>4}: ", scheme_id.into()).as_bytes()); + data.extend_from_slice(format!("{:>4}: ", scheme_id.get()).as_bytes()); data.extend_from_slice(name.as_bytes()); data.push(b'\n'); } diff --git a/src/syscall/fs.rs b/src/syscall/fs.rs index a16d545079..0751ef3ce0 100644 --- a/src/syscall/fs.rs +++ b/src/syscall/fs.rs @@ -277,7 +277,7 @@ pub fn fcntl(fd: FileHandle, cmd: usize, arg: usize) -> Result { let context = context_lock.read(); let mut files = context.files.write(); - match *files.get_mut(fd.into()).ok_or(Error::new(EBADF))? { + match *files.get_mut(fd.get()).ok_or(Error::new(EBADF))? { Some(ref mut file) => match cmd { F_GETFD => { if file.cloexec { @@ -346,7 +346,7 @@ pub fn fstat(fd: FileHandle, user_buf: UserSliceWo) -> Result { // TODO: Ensure only the kernel can access the stat when st_dev is set, or use another API // for retrieving the scheme ID from a file descriptor. // TODO: Less hacky method. - let st_dev = scheme_id.into().try_into().map_err(|_| Error::new(EOVERFLOW))?; + let st_dev = scheme_id.get().try_into().map_err(|_| Error::new(EOVERFLOW))?; user_buf.advance(memoffset::offset_of!(Stat, st_dev)).and_then(|b| b.limit(8)).ok_or(Error::new(EIO))?.copy_from_slice(&u64::to_ne_bytes(st_dev))?; Ok(0) diff --git a/src/syscall/mod.rs b/src/syscall/mod.rs index 41f3016bda..3ac81becaf 100644 --- a/src/syscall/mod.rs +++ b/src/syscall/mod.rs @@ -202,7 +202,7 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, stack let contexts = crate::context::contexts(); if let Some(context_lock) = contexts.current() { let context = context_lock.read(); - print!("{} ({}): ", context.name, context.id.into()); + print!("{} ({}): ", context.name, context.id.get()); } // Do format_call outside print! so possible exception handlers cannot reentrantly @@ -244,7 +244,7 @@ pub fn syscall(a: usize, b: usize, c: usize, d: usize, e: usize, f: usize, stack let contexts = crate::context::contexts(); if let Some(context_lock) = contexts.current() { let context = context_lock.read(); - print!("{} ({}): ", context.name, context.id.into()); + print!("{} ({}): ", context.name, context.id.get()); } // Do format_call outside print! so possible exception handlers cannot reentrantly diff --git a/src/syscall/privilege.rs b/src/syscall/privilege.rs index 6165171d28..151d414cc8 100644 --- a/src/syscall/privilege.rs +++ b/src/syscall/privilege.rs @@ -138,10 +138,10 @@ pub fn setrens(rns: SchemeNamespace, ens: SchemeNamespace) -> Result { let mut context = context_lock.write(); let setrns = - if rns.into() == 0 { + if rns.get() == 0 { // Allow entering capability mode true - } else if context.rns.into() == 0 { + } else if context.rns.get() == 0 { // Do not allow leaving capability mode return Err(Error::new(EPERM)); } else if context.euid == 0 { @@ -153,7 +153,7 @@ pub fn setrens(rns: SchemeNamespace, ens: SchemeNamespace) -> Result { } else if rns == context.rns { // Allow setting RNS if used for RNS true - } else if rns.into() as isize == -1 { + } else if rns.get() as isize == -1 { // Ignore RNS if -1 is passed false } else { @@ -162,10 +162,10 @@ pub fn setrens(rns: SchemeNamespace, ens: SchemeNamespace) -> Result { }; let setens = - if ens.into() == 0 { + if ens.get() == 0 { // Allow entering capability mode true - } else if context.ens.into() == 0 { + } else if context.ens.get() == 0 { // Do not allow leaving capability mode return Err(Error::new(EPERM)); } else if context.euid == 0 { @@ -177,7 +177,7 @@ pub fn setrens(rns: SchemeNamespace, ens: SchemeNamespace) -> Result { } else if ens == context.rns { // Allow setting ENS if used for RNS true - } else if ens.into() as isize == -1 { + } else if ens.get() as isize == -1 { // Ignore ENS if -1 is passed false } else { diff --git a/src/syscall/process.rs b/src/syscall/process.rs index 1be39d4ef2..9f46b6087c 100644 --- a/src/syscall/process.rs +++ b/src/syscall/process.rs @@ -120,7 +120,7 @@ pub fn getpid() -> Result { pub fn getpgid(pid: ContextId) -> Result { let contexts = context::contexts(); - let context_lock = if pid.into() == 0 { + let context_lock = if pid.get() == 0 { contexts.current().ok_or(Error::new(ESRCH))? } else { contexts.get(pid).ok_or(Error::new(ESRCH))? @@ -174,7 +174,7 @@ pub fn kill(pid: ContextId, sig: usize) -> Result { } }; - if pid.into() as isize > 0 { + if pid.get() as isize > 0 { // Send to a single process if let Some(context_lock) = contexts.get(pid) { let mut context = context_lock.write(); @@ -184,12 +184,12 @@ pub fn kill(pid: ContextId, sig: usize) -> Result { sent += 1; } } - } else if pid.into() as isize == -1 { + } else if pid.get() as isize == -1 { // Send to every process with permission, except for init for (_id, context_lock) in contexts.iter() { let mut context = context_lock.write(); - if context.id.into() > 2 { + if context.id.get() > 2 { found += 1; if send(&mut context) { @@ -198,10 +198,10 @@ pub fn kill(pid: ContextId, sig: usize) -> Result { } } } else { - let pgid = if pid.into() == 0 { + let pgid = if pid.get() == 0 { current_pgid } else { - ContextId::from(-(pid.into() as isize) as usize) + ContextId::from(-(pid.get() as isize) as usize) }; // Send to every process in the process group whose ID @@ -251,7 +251,7 @@ pub fn setpgid(pid: ContextId, pgid: ContextId) -> Result { context.id }; - let context_lock = if pid.into() == 0 { + let context_lock = if pid.get() == 0 { contexts.current().ok_or(Error::new(ESRCH))? } else { contexts.get(pid).ok_or(Error::new(ESRCH))? @@ -259,7 +259,7 @@ pub fn setpgid(pid: ContextId, pgid: ContextId) -> Result { let mut context = context_lock.write(); if context.id == current_pid || context.ppid == current_pid { - if pgid.into() == 0 { + if pgid.get() == 0 { context.pgid = context.id; } else { context.pgid = pgid; @@ -409,7 +409,7 @@ pub fn waitpid(pid: ContextId, status_ptr: Option, flags: WaitFlags }; loop { - let res_opt = if pid.into() == 0 { + let res_opt = if pid.get() == 0 { // Check for existence of child { let mut found = false; @@ -438,8 +438,8 @@ pub fn waitpid(pid: ContextId, status_ptr: Option, flags: WaitFlags let (_wid, (w_pid, status)) = waitpid.receive_any("waitpid any"); grim_reaper(w_pid, status) } - } else if (pid.into() as isize) < 0 { - let pgid = ContextId::from(-(pid.into() as isize) as usize); + } else if (pid.get() as isize) < 0 { + let pgid = ContextId::from(-(pid.get() as isize) as usize); // Check for existence of child in process group PGID { @@ -481,7 +481,7 @@ pub fn waitpid(pid: ContextId, status_ptr: Option, flags: WaitFlags let context_lock = contexts.get(pid).ok_or(Error::new(ECHILD))?; let mut context = context_lock.write(); if context.ppid != ppid { - println!("TODO: Hack for rustc - changing ppid of {} from {} to {}", context.id.into(), context.ppid.into(), ppid.into()); + println!("TODO: Hack for rustc - changing ppid of {} from {} to {}", context.id.get(), context.ppid.get(), ppid.get()); context.ppid = ppid; //return Err(Error::new(ECHILD)); Some(context.status.clone())