Skip to content

Commit

Permalink
Implements sys_dynlib_get_info (obhq#672)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuchAFuriousDeath committed Apr 5, 2024
1 parent f6023d4 commit b741095
Show file tree
Hide file tree
Showing 19 changed files with 303 additions and 103 deletions.
21 changes: 19 additions & 2 deletions src/elf/src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ impl FileInfo {
let id = LE::read_u16(&data[6..]);

// Lookup name.
let name = match self.read_str(name.try_into().unwrap()) {
let name = match self.read_str(name as usize) {
Ok(v) => v.to_owned(),
Err(e) => return Err(ReadModuleError::InvalidNameOffset(name, e)),
};
Expand All @@ -301,6 +301,15 @@ impl FileInfo {
Ok(LibraryInfo::new(id, name, LibraryFlags::empty()))
}

pub fn read_fingerprint(&self, offset: usize) -> [u8; 20] {
let offset = offset + self.dynoff;

let mut fingerprint = [0u8; 20];
fingerprint.copy_from_slice(&self.data[offset..(offset + 20)]);

fingerprint
}

pub fn read_str(&self, offset: usize) -> Result<&str, StringTableError> {
// Get raw string.
let tab = &self.data[self.strtab..(self.strtab + self.strsz)];
Expand All @@ -316,7 +325,9 @@ impl FileInfo {
};

// Get Rust string.
std::str::from_utf8(raw).map_err(|_| StringTableError::NotUtf8)
let string = std::str::from_utf8(raw)?;

Ok(string)
}
}

Expand Down Expand Up @@ -419,3 +430,9 @@ pub enum StringTableError {
#[error("the offset is not a UTF-8 string")]
NotUtf8,
}

impl From<std::str::Utf8Error> for StringTableError {
fn from(_: std::str::Utf8Error) -> Self {
Self::NotUtf8
}
}
4 changes: 4 additions & 0 deletions src/elf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,10 @@ impl<I: Read + Seek> Elf<I> {
self.programs.as_slice()
}

pub fn relro(&self) -> Option<usize> {
self.relro
}

pub fn dynamic(&self) -> Option<usize> {
self.dynamic
}
Expand Down
10 changes: 10 additions & 0 deletions src/kernel/src/budget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,13 @@ pub enum ProcType {
MiniApp,
System, // TODO: Verify this.
}

impl Into<u32> for ProcType {
fn into(self) -> u32 {
match self {
ProcType::BigApp => 0,
ProcType::MiniApp => 1,
ProcType::System => 2,
}
}
}
13 changes: 3 additions & 10 deletions src/kernel/src/dev/dmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,10 @@ impl TryInto<DmemContainer> for i32 {
}
}

impl TryInto<DmemContainer> for usize {
type Error = SysErr;

fn try_into(self) -> Result<DmemContainer, Self::Error> {
(self as i32).try_into()
}
}

impl DeviceDriver for Dmem {
fn ioctl(
&self,
dev: &Arc<CharacterDevice>,
_: &Arc<CharacterDevice>,
cmd: IoCmd,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
Expand All @@ -76,7 +68,8 @@ impl DeviceDriver for Dmem {
}

match cmd {
IoCmd::DMEM10(size) => *size = self.total_size,
// TODO: properly implement this
IoCmd::DMEMTOTAL(size) => *size = self.total_size,
IoCmd::DMEMGETPRT(_prt) => todo!(),
IoCmd::DMEMGETAVAIL(_avail) => todo!(),
IoCmd::DMEMALLOC(_alloc) => todo!(),
Expand Down
6 changes: 6 additions & 0 deletions src/kernel/src/dmem/blockpool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ use std::sync::Arc;
#[derive(Debug)]
pub struct BlockPool {}

impl BlockPool {
pub fn new() -> Arc<Self> {
Arc::new(Self {})
}
}

impl FileBackend for BlockPool {
#[allow(unused_variables)] // TODO: remove when implementing
fn ioctl(
Expand Down
32 changes: 18 additions & 14 deletions src/kernel/src/dmem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
use thiserror::Error;

use crate::dev::{Dmem, DmemContainer};
use crate::errno::EINVAL;
use crate::fs::{make_dev, CharacterDevice, DriverFlags, Fs, MakeDevError, MakeDevFlags, Mode};
use crate::fs::{
make_dev, CharacterDevice, DriverFlags, Fs, MakeDevError, MakeDevFlags, Mode, VFile, VFileType,
};
use crate::info;
use crate::process::{VProc, VThread};
use crate::process::VThread;
use crate::syscalls::{SysErr, SysIn, SysOut, Syscalls};
use crate::ucred::{Gid, Privilege, Uid};
use crate::ucred::{Gid, Uid};
use std::ops::Index;
use std::sync::Arc;
use thiserror::Error;

pub use self::blockpool::*;

Expand Down Expand Up @@ -112,24 +113,31 @@ impl DmemManager {
fn sys_dmem_container(self: &Arc<Self>, td: &VThread, i: &SysIn) -> Result<SysOut, SysErr> {
let dmem_id: i32 = i.args[0].try_into().unwrap();

let mut dmem_container = td.proc().dmem_container_mut();
let old_dmem_container = *dmem_container;
let dmem_container = td.proc().dmem_container_mut();
let current_container = *dmem_container;

if dmem_id != -1 {
todo!()
}

Ok(old_dmem_container.into())
Ok(current_container.into())
}

fn sys_blockpool_open(self: &Arc<Self>, _td: &VThread, i: &SysIn) -> Result<SysOut, SysErr> {
fn sys_blockpool_open(self: &Arc<Self>, td: &VThread, i: &SysIn) -> Result<SysOut, SysErr> {
let flags: u32 = i.args[0].try_into().unwrap();

if flags & 0xffafffff != 0 {
return Err(SysErr::Raw(EINVAL));
}

todo!("sys_blockpool_open on new FS")
let bp = BlockPool::new();

let fd = td
.proc()
.files()
.alloc(Arc::new(VFile::new(VFileType::Blockpool(bp))));

Ok(fd.into())
}

fn sys_blockpool_map(self: &Arc<Self>, _: &VThread, i: &SysIn) -> Result<SysOut, SysErr> {
Expand Down Expand Up @@ -158,10 +166,6 @@ impl DmemManager {
fn sys_blockpool_move(self: &Arc<Self>, _: &VThread, _i: &SysIn) -> Result<SysOut, SysErr> {
todo!()
}

fn get_dmem_device<'a>(self: &'a Arc<Self>, dmem: DmemContainer) -> &'a DmemDevice {
self.index(dmem)
}
}

impl Index<DmemContainer> for DmemManager {
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct VFile {
}

impl VFile {
pub(super) fn new(ty: VFileType) -> Self {
pub fn new(ty: VFileType) -> Self {
let gg = GutexGroup::new();

Self {
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/fs/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ commands! {
DIPSWCHECK2(&mut i32) = 0x40048806,

/// Get total size?
DMEM10(&mut usize) = 0x4008800a,
DMEMTOTAL(&mut usize) = 0x4008800a,
/// Get PRT aperture
DMEMGETPRT(&mut PrtAperture) = 0xC018800C,
/// Get available memory size
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ impl Fs {
// Our IoCmd contains both the command and the argument (if there is one).
let cmd = IoCmd::try_from_raw_parts(i.args[1].into(), i.args[2].into())?;

info!("Executing ioctl({cmd:?}) on file descriptor {fd}.");
info!("Executing ioctl {cmd:?} on file descriptor {fd}.");

self.ioctl(fd, cmd, td)?;

Expand Down
4 changes: 2 additions & 2 deletions src/kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ fn run() -> Result<(), KernelError> {
.load(path, flags, false, true, &main)
.map_err(|e| KernelError::FailedToLoadLibkernel(e.into()))?;

libkernel.flags_mut().remove(ModuleFlags::UNK2);
libkernel.flags_mut().remove(ModuleFlags::IS_NEW);
libkernel.print(info!());

ld.set_kernel(libkernel);
Expand All @@ -448,7 +448,7 @@ fn run() -> Result<(), KernelError> {
.load(path, flags, false, true, &main)
.map_err(|e| KernelError::FailedToLoadLibSceLibcInternal(e.into()))?;

libc.flags_mut().remove(ModuleFlags::UNK2);
libc.flags_mut().remove(ModuleFlags::IS_NEW);
libc.print(info!());

drop(libc);
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/process/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ impl VProc {

ProcTypeInfo {
len: size_of::<ProcTypeInfo>(),
ty: self.budget_ptype as u32,
ty: self.budget_ptype.into(),
flags,
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/regmgr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ impl RegMgr {
// Lookup the entry.
let key = RegKey::new(key);
let entry = self.lookup(key).ok_or(RegError::NotFound(key))?;
let web = if cred.is_webcore_process() || cred.is_diskplayerui_process() {
let web = if cred.is_libkernel_web() || cred.is_webprocess_webapp_or_webmas() {
1
} else {
0
Expand Down
12 changes: 8 additions & 4 deletions src/kernel/src/rtld/mem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct Memory {
base: usize,
text: usize,
data: usize,
relro: Option<usize>,
obcode: usize,
obcode_sealed: Gutex<usize>,
obdata: usize,
Expand Down Expand Up @@ -61,10 +62,8 @@ impl Memory {
}
}
ProgramType::PT_SCE_RELRO => {
if relro.is_some() {
if relro.replace(segments.len()).is_some() {
return Err(MapError::MultipleRelroProgram);
} else {
relro = Some(segments.len());
}
}
_ => continue,
Expand Down Expand Up @@ -132,7 +131,7 @@ impl Memory {
len += segment.len;
segments.push(segment);

// Create workspace for our data. We cannot mix this the code because the executable-space
// Create workspace for our data. We cannot mix this with the code because the executable-space
// protection on some system don't allow execution on writable page.
let obdata = segments.len();
let segment = MemorySegment {
Expand Down Expand Up @@ -180,6 +179,7 @@ impl Memory {
base,
text,
data,
relro,
obcode,
obcode_sealed: gg.spawn(0),
obdata,
Expand Down Expand Up @@ -212,6 +212,10 @@ impl Memory {
&self.segments[self.data]
}

pub fn relro_segment(&self) -> Option<&MemorySegment> {
self.relro.as_ref().map(|i| &self.segments[*i])
}

/// # Safety
/// Some part of the returned slice may not readable.
pub unsafe fn as_bytes(&self) -> &[u8] {
Expand Down
Loading

0 comments on commit b741095

Please sign in to comment.