Skip to content

Commit

Permalink
Adds fdrop for 11.00
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon committed May 11, 2024
1 parent d6d7cbb commit 431fdf1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
14 changes: 14 additions & 0 deletions korbis-1100/src/file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use core::sync::atomic::AtomicU32;

/// Implementation of [`korbis::file::File`] for 11.00.
#[repr(C)]
pub struct File {
pad: [u8; 0x28],
refcnt: AtomicU32,
}

impl korbis::file::File for File {
fn refcnt(&self) -> &AtomicU32 {
&self.refcnt
}
}
6 changes: 6 additions & 0 deletions korbis-1100/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
#![no_std]

use self::file::File;
use self::thread::Thread;
use core::ffi::{c_char, c_int};
use korbis::offset;
use korbis::uio::UioSeg;

mod file;
mod thread;

/// Implementation of [`ps4k::Kernel`] for 11.00.
#[derive(Clone, Copy)]
pub struct Kernel(&'static [u8]);

impl korbis::Kernel for Kernel {
type File = File;
type Thread = Thread;

unsafe fn new(base: *const u8) -> Self {
Expand All @@ -22,6 +25,9 @@ impl korbis::Kernel for Kernel {
self.0
}

#[offset(0x4161B0)]
unsafe fn fdrop(self, fp: *mut Self::File, td: *mut Self::Thread) -> c_int;

#[offset(0xE63B0)]
unsafe fn kern_openat(
self,
Expand Down
6 changes: 3 additions & 3 deletions korbis-macros/src/offset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ fn transform_method(args: LitInt, item: Method) -> syn::Result<TokenStream> {

Ok(quote! {
#unsafety fn #ident(#receiver, #params) #ret {
let ad = unsafe { self.elf().as_ptr().add(#offset) };
let fp: unsafe extern "C" fn(#params) #ret = unsafe { core::mem::transmute(ad) };
unsafe { fp(#args) }
let _addr = unsafe { self.elf().as_ptr().add(#offset) };
let _fp: unsafe extern "C" fn(#params) #ret = unsafe { core::mem::transmute(_addr) };
unsafe { _fp(#args) }
}
})
}
Expand Down
29 changes: 29 additions & 0 deletions korbis/src/file/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use crate::thread::Thread;
use crate::Kernel;
use core::sync::atomic::{fence, AtomicU32, Ordering};

/// Represents `file` structure.
pub trait File: Sized {
/// Returns `f_count` field.
fn refcnt(&self) -> &AtomicU32;
}

/// RAII struct to decrease `file::f_count` when dropped.
pub struct OwnedFile<K: Kernel> {
kernel: K,
file: *mut K::File,
}

impl<K: Kernel> Drop for OwnedFile<K> {
fn drop(&mut self) {
// See Drop implementation on Arc how this thing work.
if unsafe { (*self.file).refcnt().fetch_sub(1, Ordering::Release) } != 1 {
return;
}

fence(Ordering::Acquire);

// The kernel itself does not check if fdrop is success so we don't need to.
unsafe { self.kernel.fdrop(self.file, K::Thread::current()) };
}
}
10 changes: 10 additions & 0 deletions korbis/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#![no_std]

use self::elf::ProgramType;
use self::file::File;
use self::thread::Thread;
use self::uio::UioSeg;
use core::ffi::{c_char, c_int};

pub use korbis_macros::*;

pub mod elf;
pub mod file;
pub mod thread;
pub mod uio;

/// Provides methods to access the PS4 kernel for a specific version.
pub trait Kernel: Copy + Send + Sync + 'static {
type File: File;
type Thread: Thread;

/// # Safety
Expand All @@ -30,6 +33,13 @@ pub trait Kernel: Copy + Send + Sync + 'static {
/// can mutate at any time. The whole slice is guarantee to be readable.
unsafe fn elf(self) -> &'static [u8];

/// # Panics
/// If [`File::refcnt()`] of `fp` is not zero.
///
/// # Safety
/// - `fp` cannot be null.
unsafe fn fdrop(self, fp: *mut Self::File, td: *mut Self::Thread) -> c_int;

/// # Safety
/// - `td` cannot be null.
/// - `path` cannot be null and must point to a null-terminated string if `seg` is [`UioSeg::Kernel`].
Expand Down

0 comments on commit 431fdf1

Please sign in to comment.