-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fc0bb89
commit 5450916
Showing
12 changed files
with
193 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/// Implementation of [`ps4k::version::Thread`] for 11.00. | ||
#[repr(C)] | ||
pub struct Thread { | ||
pad: [u8; 0x398], | ||
ret: [usize; 2], // td_retval | ||
} | ||
|
||
impl ps4k::version::Thread for Thread { | ||
fn ret(&self, i: usize) -> usize { | ||
self.ret[i] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "ps4k-macros" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
proc-macro = true | ||
|
||
[dependencies] | ||
proc-macro2 = "1.0.81" | ||
quote = "1.0.36" | ||
syn = { version = "2.0.60", features = ["full"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use proc_macro::TokenStream; | ||
use syn::{parse_macro_input, Error, LitInt}; | ||
|
||
mod offset; | ||
|
||
#[proc_macro_attribute] | ||
pub fn offset(args: TokenStream, item: TokenStream) -> TokenStream { | ||
let args = parse_macro_input!(args as LitInt); | ||
let item = parse_macro_input!(item as self::offset::OffsetItem); | ||
|
||
self::offset::transform(args, item) | ||
.unwrap_or_else(Error::into_compile_error) | ||
.into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::parse::{Parse, ParseStream}; | ||
use syn::punctuated::Punctuated; | ||
use syn::{parenthesized, Error, Ident, LitInt, Pat, PatType, Receiver, ReturnType, Token}; | ||
|
||
pub fn transform(args: LitInt, item: OffsetItem) -> syn::Result<TokenStream> { | ||
match item { | ||
OffsetItem::Method(v) => transform_method(args, v), | ||
} | ||
} | ||
|
||
fn transform_method(args: LitInt, item: Method) -> syn::Result<TokenStream> { | ||
// Assemble. | ||
let offset: usize = args.base10_parse()?; | ||
let unsafety = item.unsafety; | ||
let ident = item.ident; | ||
let receiver = item.receiver; | ||
let params = item.params; | ||
let ret = item.ret; | ||
let args: Punctuated<&Pat, Token![,]> = params.iter().map(|p| p.pat.as_ref()).collect(); | ||
|
||
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) } | ||
} | ||
}) | ||
} | ||
|
||
/// Item of `offset` attribute. | ||
pub enum OffsetItem { | ||
Method(Method), | ||
} | ||
|
||
impl Parse for OffsetItem { | ||
fn parse(input: ParseStream) -> syn::Result<Self> { | ||
let unsafety = input.parse()?; | ||
let item = if input.parse::<Option<Token![fn]>>()?.is_some() { | ||
// Parse name. | ||
let ident = input.parse()?; | ||
let params; | ||
|
||
parenthesized!(params in input); | ||
|
||
// Parse receiver. | ||
let receiver = params.parse()?; | ||
|
||
params.parse::<Option<Token![,]>>()?; | ||
|
||
// Parse return type. | ||
let ret = input.parse()?; | ||
|
||
input.parse::<Token![;]>()?; | ||
|
||
Self::Method(Method { | ||
unsafety, | ||
ident, | ||
receiver, | ||
params: params.parse_terminated(PatType::parse, Token![,])?, | ||
ret, | ||
}) | ||
} else { | ||
return Err(Error::new(input.span(), "unsupported offset item")); | ||
}; | ||
|
||
Ok(item) | ||
} | ||
} | ||
|
||
/// A method that have `offset` attribute. | ||
pub struct Method { | ||
unsafety: Option<Token![unsafe]>, | ||
ident: Ident, | ||
receiver: Receiver, | ||
params: Punctuated<PatType, Token![,]>, | ||
ret: ReturnType, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[dependencies] | ||
ps4k-macros = { path = "../ps4k-macros" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,8 @@ | |
|
||
use self::version::KernelVersion; | ||
|
||
pub use ps4k_macros::*; | ||
|
||
pub mod elf; | ||
pub mod version; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
use core::arch::asm; | ||
|
||
/// Represents `thread` structure. | ||
pub trait Thread: Sized { | ||
fn current() -> *mut Self { | ||
let mut p; | ||
|
||
unsafe { | ||
asm!("mov {}, gs:[0]", out(reg) p, options(readonly, pure, preserves_flags, nostack)) | ||
}; | ||
|
||
p | ||
} | ||
|
||
/// Returns value of `td_retval[i]`. | ||
/// | ||
/// # Panics | ||
/// If `i` is not `0` or `1`. | ||
fn ret(&self, i: usize) -> usize; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters