Seregon/ShadPKG

A tool for deriving PKG packet encryption keys for ps4 written in c++

C++/47.3 KB/No license
ps4MEL/ps4_tls.h
ShadPKG / ps4MEL / ps4_tls.h
1/*
2 * ╔═══════════════════════════════════════════════════════════════════════════╗
3 * ║ PS4 TLS (Thread Local Storage) ║
4 * ╠═══════════════════════════════════════════════════════════════════════════╣
5 * ║ Emulates PS4's Thread Control Block for cross-platform compatibility. ║
6 * ║ Based on shadPS4 implementation patterns. ║
7 * ╚═══════════════════════════════════════════════════════════════════════════╝
8 */
9 
10#pragma once
11 
12#include <cstdint>
13 
14namespace PS4Emu {
15 
16/*
17 * ┌─────────────────────────────────────────────────────────────────┐
18 * │ THREAD CONTROL BLOCK (TCB) │
19 * │ │
20 * │ PS4 Layout (simplified): │
21 * │ ┌──────────┬──────────┬──────────┬──────────┐ │
22 * │ │ self ptr │ dtv ptr │ reserved │ errno │ │
23 * │ └──────────┴──────────┴──────────┴──────────┘ │
24 * └─────────────────────────────────────────────────────────────────┘
25 */
26struct Tcb {
27 Tcb *tcb_self; // Pointer to self (for FS/GS base access)
28 void *tcb_dtv; // Dynamic Thread Vector for TLS
29 void *tcb_thread; // Current thread pointer
30 int32_t tcb_errno; // Thread-local errno
31 uint64_t tcb_reserved[4]; // Reserved space
32 
33 // Padding to match PS4's TCB size
34 uint8_t padding[256 - 56];
35};
36 
37/*
38 * ┌─────────────────────────────────────────────────────────────────┐
39 * │ PUBLIC API │
40 * └─────────────────────────────────────────────────────────────────┘
41 */
42 
43// Initialize TLS subsystem (call once at startup)
44bool InitializeTLS();
45 
46// Shutdown TLS subsystem
47void ShutdownTLS();
48 
49// Set the current thread's TCB base pointer
50void SetTcbBase(Tcb *tcb);
51 
52// Get the current thread's TCB base pointer
53Tcb *GetTcbBase();
54 
55// Create a new TCB for a thread
56Tcb *CreateTcb();
57 
58// Destroy a TCB
59void DestroyTcb(Tcb *tcb);
60 
61// Get thread-local errno (for PS4 syscall emulation)
62int32_t *GetErrno();
63 
64} // namespace PS4Emu
65