Sony PlayStation2 Study
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

498 lines
18 KiB

#ifndef __SPS2_BASIC_SYSTEM_HEADER__
#define __SPS2_BASIC_SYSTEM_HEADER__
#include "../../core/base/basicTypes.h"
#include "sifdma.h"
#define DI DIntr
#define EI EIntr
// Workaround for EE kernel bug: call this immediately before returning from any interrupt handler.
#define ExitHandler() asm volatile("sync\nei\n")
// note: 'sync' is the same as 'sync.l'
#define EE_SYNC() __asm__ volatile ("sync")
#define EE_SYNCL() __asm__ volatile ("sync.l")
#define EE_SYNCP() __asm__ volatile ("sync.p")
#define UNCACHED_SEG(x) \
((void *)(((uint32_t)(x)) | 0x20000000))
#define IS_UNCACHED_SEG(x) \
(((uint32_t)(x)) & 0x20000000)
#define UCAB_SEG(x) \
((void *)(((uint32_t)(x)) | 0x30000000))
#define PUSHDATA( t, x, v, l) \
*(t *)(x) = (v); (l) = sizeof(t)
#define POPDATA( t, x, v, l) \
(v) = *(t *)(x); (l) = sizeof(t)
#define ALIGNED(x) __attribute__((aligned((x))))
// GP functions
void * ChangeGP(void *gp);
void SetGP(void *gp);
void * GetGP(void);
extern void *_gp;
#define SetModuleGP() ChangeGP(&_gp)
/** Special thread ID for referring to the running thread.
Unlike the IOP kernel, this is only supported by ReferThreadStatus() and ChangeThreadPriority().
It can also be used by the iWakeupThread() _syscall_.
But because the libkernel patch may call WakeupThread() to avoid the defect within iWakeupThread() that prevents the running thread from being woken up,
THS_SELF should not be used with iWakeupThread(). */
#define TH_SELF 0
/** Limits */
#define MAX_THREADS 256 //A few will be used for the kernel patches. Thread 0 is always the idle thread.
#define MAX_SEMAPHORES 256 //A few will be used for the kernel patches.
#define MAX_PRIORITY 128
#define MAX_HANDLERS 128
#define MAX_ALARMS 64
/** Modes for FlushCache */
#define WRITEBACK_DCACHE 0
#define INVALIDATE_DCACHE 1
#define INVALIDATE_ICACHE 2
#define INVALIDATE_CACHE 3 //Invalidate both data & instruction caches.
/** EE Interrupt Controller (INTC) interrupt numbers */
typedef enum e_initc_irq {
INTC_GS = 0,
INTC_SBUS = 1,
INTC_VBLANK_S = 2,
INTC_VBLANK_E = 3,
INTC_VIF0 = 4,
INTC_VIF1 = 5,
INTC_VU0 = 6,
INTC_VU1 = 7,
INTC_IPU = 8,
INTC_TIM0 = 9,
INTC_TIM1 = 10,
INTC_TIM2 = 11,
INTC_TIM3 = 12, //Reserved by the EE kernel for alarms (do not use)
INTC_SFIFO = 13, //Error encountered during SFIFO transfer
INTC_VU0WD = 14 //VU0 WatchDog; ForceBreak is sent to VU0 if left in RUN state for extended periods of time.
}initc_irq_t;
//For backward-compatibility
#define kINTC_GS INTC_GS
#define kINTC_SBUS INTC_SBUS
#define kINTC_VBLANK_START INTC_VBLANK_S
#define kINTC_VBLANK_END INTC_VBLANK_E
#define kINTC_VIF0 INTC_VIF0
#define kINTC_VIF1 INTC_VIF1
#define kINTC_VU0 INTC_VU0
#define kINTC_VU1 INTC_VU1
#define kINTC_IPU INTC_IPU
#define kINTC_TIMER0 INTC_TIM0
#define kINTC_TIMER1 INTC_TIM1
/** EE Direct Memory Access Controller (DMAC) interrupt numbers */
typedef enum e_dmac_irq {
DMAC_VIF0 = 0,
DMAC_VIF1 = 1,
DMAC_GIF = 3,
DMAC_FROM_IPU = 4,
DMAC_TO_IPU = 5,
DMAC_SIF0 = 6,
DMAC_SIF1 = 7,
DMAC_SIF2 = 8,
DMAC_FROM_SPR = 9,
DMAC_TO_SPR = 10,
DMAC_CIS = 13, //Channel interrupt
DMAC_MEIS = 14, //MemFIFO empty interrupt
DMAC_BEIS = 15, //Bus error interrupt
} dmac_irq_t;
/** ResetEE argument bits */
#define INIT_DMAC 0x01
#define INIT_VU1 0x02
#define INIT_VIF1 0x04
#define INIT_GIF 0x08
#define INIT_VU0 0x10
#define INIT_VIF0 0x20
#define INIT_IPU 0x40
static inline void nopdelay(void){
uint32_t i = 0xfffff;
do{ __asm__ ("nop\nnop\nnop\nnop\nnop\n"); } while( i-- );
};
static inline uint32_t ee_get_opmode(void) {
uint32_t status;
__asm__ volatile (
".set\tpush\n\t" \
".set\tnoreorder\n\t" \
"mfc0\t%0, $12\n\t" \
".set\tpop\n\t" : "=r" (status));
return (status >> 3) & 3;
};
static inline uint32_t ee_set_opmode( uint32_t opmode ){
uint32_t status, mask;
__asm__ volatile (
".set\tpush\n\t" \
".set\tnoreorder\n\t" \
"mfc0\t%0, $12\n\t" \
"li\t%1, 0xffffffe7\n\t" \
"and\t%0, %1\n\t" \
"or\t%0, %2\n\t" \
"mtc0\t%0, $12\n\t" \
"sync.p\n\t"
".set\tpop\n\t" : "=r" (status), "=r" (mask) : "r" (opmode));
return (status >> 3) & 3;
};
static inline uint32_t ee_kmode_enter(){
uint32_t status, mask;
__asm__ volatile (
".set\tpush\n\t" \
".set\tnoreorder\n\t" \
"mfc0\t%0, $12\n\t" \
"li\t%1, 0xffffffe7\n\t" \
"and\t%0, %1\n\t" \
"mtc0\t%0, $12\n\t" \
"sync.p\n\t"
".set\tpop\n\t" : "=r" (status), "=r" (mask));
return status;
};
static inline uint32_t ee_kmode_exit(){
uint32_t status;
__asm__ volatile (
".set\tpush\n\t" \
".set\tnoreorder\n\t" \
"mfc0\t%0, $12\n\t" \
"ori\t%0, 0x10\n\t" \
"mtc0\t%0, $12\n\t" \
"sync.p\n\t" \
".set\tpop\n\t" : "=r" (status));
return status;
};
typedef struct s_ee_sema{
uint32_t count,
max_count,
init_count,
wait_threads;
uint32_t attr,
option;
} ee_sema_t;
typedef struct s_ee_thread {
uint32_t status; // 0x00
void * func; // 0x04
void * stack; // 0x08
uint32_t stack_size; // 0x0C
void * gp_reg; // 0x10
uint32_t initial_priority; // 0x14
uint32_t current_priority; // 0x18
uint32_t attr; // 0x1C
uint32_t option; // 0x20 Do not use - officially documented to not work.
} ee_thread_t;
/** Thread status */
#define THS_RUN 0x01
#define THS_READY 0x02
#define THS_WAIT 0x04
#define THS_SUSPEND 0x08
#define THS_WAITSUSPEND 0x0c
#define THS_DORMANT 0x10
/** Thread WAIT Status */
#define TSW_NONE 0 //Thread is not in WAIT state
#define TSW_SLEEP 1
#define TSW_SEMA 2
// sizeof() == 0x30
typedef struct s_ee_thread_status{
uint32_t status; // 0x00
void * func; // 0x04
void * stack; // 0x08
uint32_t stack_size; // 0x0C
void * gp_reg; // 0x10
uint32_t initial_priority; // 0x14
uint32_t current_priority; // 0x18
uint32_t attr; // 0x1C
uint32_t option; // 0x20
uint32_t waitType; // 0x24
uint32_t waitId; // 0x28
uint32_t wakeupCount; // 0x2C
} ee_thread_status_t;
/** CpuConfig options */
enum CPU_CONFIG {
CPU_CONFIG_ENABLE_DIE = 0, //Enable Dual Issue
CPU_CONFIG_ENABLE_ICE, //Enable Instruction Cache
CPU_CONFIG_ENABLE_DCE, //Enable Data Cache
CPU_CONFIG_DISBLE_DIE, //Disable Dual Issue
CPU_CONFIG_DISBLE_ICE, //Disable Instruction Cache
CPU_CONFIG_DISBLE_DCE //Disable Data Cache
};
/** EnableCache & DisableCache options (multiple options may be specified) */
#define CPU_DATA_CACHE 1
#define CPU_INSTRUCTION_CACHE 2
#ifdef __cplusplus
extern "C" {
#endif
/* Initialization/deinitialization routines. */
void _InitSys(void); //Run by crt0
void TerminateLibrary(void); //Run by crt0
/* Thread update functions */
int InitThread(void); //Run by _InitSys
int32_t iWakeupThread(int32_t thread_id );
int32_t iRotateThreadReadyQueue(int32_t priority);
int32_t iSuspendThread(int32_t thread_id);
/* TLB update functions */
void InitTLBFunctions(void); //Run by _InitSys
void InitTLB(void);
void Exit(int32_t exit_code) __attribute__((noreturn));
int32_t ExecPS2(void *entry, void *gp, int num_args, char *args[]);
void LoadExecPS2(const char *filename, int32_t num_args, char *args[]) __attribute__((noreturn));
void ExecOSD(int num_args, char *args[]) __attribute__((noreturn));
/* Alarm update functions */
void InitAlarm(void); //Run by _InitSys
/* libosd update functions */
void InitExecPS2(void); //ExecPS2 patch only. Run by _InitSys, Exit, LoadExecPS2, ExecPS2 and ExecOSD
void InitOsd(void); //ExecPS2 + System Configuration patches. Please refer to the comments within libosd_full.c
int PatchIsNeeded(void); //Indicates whether the patch is required.
/* Glue routines. */
int DIntr(void);
int EIntr(void);
int EnableIntc(int intc);
int DisableIntc(int intc);
int EnableDmac(int dmac);
int DisableDmac(int dmac);
int iEnableIntc(int intc);
int iDisableIntc(int intc);
int iEnableDmac(int dmac);
int iDisableDmac(int dmac);
void SyncDCache(void *start, void *end);
void iSyncDCache(void *start, void *end);
void InvalidDCache(void *start, void *end);
void iInvalidDCache(void *start, void *end);
/* System call prototypes */
void ResetEE( uint32_t init_bitfield );
void SetGsCrt(int16_t interlace, int16_t pal_ntsc, int16_t field);
void _Exit(int32_t exit_code) __attribute__((noreturn));
void _LoadExecPS2(const char *filename, int32_t num_args, char *args[]) __attribute__((noreturn));
int32_t _ExecPS2(void *entry, void *gp, int num_args, char *args[]);
void RFU009(uint32_t arg0, uint32_t arg1);
int32_t AddSbusIntcHandler(int32_t cause, void (*handler)(int call));
int32_t RemoveSbusIntcHandler(int32_t cause);
int32_t Interrupt2Iop(int32_t cause);
void SetVTLBRefillHandler(int32_t handler_num, void* handler_func);
void SetVCommonHandler(int32_t handler_num, void* handler_func);
void SetVInterruptHandler(int32_t handler_num, void* handler_func);
int32_t AddIntcHandler(int32_t cause, int32_t(*handler_func)(int32_t cause), int32_t next);
int32_t AddIntcHandler2(int32_t cause, int32_t(*handler_func)(int32_t cause, void* arg, void* addr), int32_t next, void* arg);
int32_t RemoveIntcHandler(int32_t cause, int32_t handler_id);
int32_t AddDmacHandler(int32_t channel, int32_t (*handler)(int32_t channel), int32_t next);
int32_t AddDmacHandler2(int32_t channel, int32_t (*handler)(int32_t channel, void *arg, void *addr), int32_t next, void* arg);
int32_t RemoveDmacHandler(int32_t channel, int32_t handler_id);
int32_t _EnableIntc(int32_t cause);
int32_t _DisableIntc(int32_t cause);
int32_t _EnableDmac(int32_t channel);
int32_t _DisableDmac(int32_t channel);
//Alarm value is in H-SYNC ticks.
int32_t SetAlarm(uint16_t time, void (*callback)(int32_t alarm_id, uint16_t time, void *common), void *common);
int32_t _SetAlarm(uint16_t time, void (*callback)(int32_t alarm_id, uint16_t time, void *common), void *common);
int32_t ReleaseAlarm(int32_t alarm_id);
int32_t _ReleaseAlarm(int32_t alarm_id);
int32_t _iEnableIntc(int32_t cause);
int32_t _iDisableIntc(int32_t cause);
int32_t _iEnableDmac(int32_t channel);
int32_t _iDisableDmac(int32_t channel);
int32_t iSetAlarm(uint16_t time, void (*callback)(int32_t alarm_id, uint16_t time, void *common), void *common);
int32_t _iSetAlarm(uint16_t time, void (*callback)(int32_t alarm_id, uint16_t time, void *common), void *common);
int32_t iReleaseAlarm(int32_t alarm_id);
int32_t _iReleaseAlarm(int32_t alarm_id);
int32_t CreateThread(ee_thread_t *thread);
int32_t DeleteThread(int32_t thread_id);
int32_t StartThread(int32_t thread_id, void *args);
void ExitThread(void);
void ExitDeleteThread(void);
int32_t TerminateThread(int32_t thread_id);
int32_t iTerminateThread(int32_t thread_id);
// void DisableDispatchThread(void); // not supported
// void EnableDispatchThread(void); // not supported
int32_t ChangeThreadPriority(int32_t thread_id, int32_t priority);
int32_t iChangeThreadPriority(int32_t thread_id, int32_t priority);
int32_t RotateThreadReadyQueue(int32_t priority);
int32_t _iRotateThreadReadyQueue(int32_t priority);
int32_t ReleaseWaitThread(int32_t thread_id);
int32_t iReleaseWaitThread(int32_t thread_id);
int32_t GetThreadId(void);
int32_t _iGetThreadId(void); //This is actually GetThreadId(), used for a hack by SCE to work around the iWakeupThread design flaw.
int32_t ReferThreadStatus(int32_t thread_id, ee_thread_status_t *info);
int32_t iReferThreadStatus(int32_t thread_id, ee_thread_status_t *info);
int32_t SleepThread(void);
int32_t WakeupThread(int32_t thread_id);
int32_t _iWakeupThread(int32_t thread_id);
int32_t CancelWakeupThread(int32_t thread_id);
int32_t iCancelWakeupThread(int32_t thread_id);
int32_t SuspendThread(int32_t thread_id);
int32_t _iSuspendThread(int32_t thread_id);
int32_t ResumeThread(int32_t thread_id);
int32_t iResumeThread(int32_t thread_id);
uint8_t RFU059(void);
void * SetupThread(void * gp, void * stack, int32_t stack_size, void * args, void * root_func);
void SetupHeap(void * heap_start, int32_t heap_size);
void *EndOfHeap(void);
int32_t CreateSema(ee_sema_t *sema);
int32_t DeleteSema(int32_t sema_id);
int32_t SignalSema(int32_t sema_id);
int32_t iSignalSema(int32_t sema_id);
int32_t WaitSema(int32_t sema_id);
int32_t PollSema(int32_t sema_id);
int32_t iPollSema(int32_t sema_id);
int32_t ReferSemaStatus(int32_t sema_id, ee_sema_t *sema);
int32_t iReferSemaStatus(int32_t sema_id, ee_sema_t *sema);
int32_t iDeleteSema(int32_t sema_id);
void SetOsdConfigParam(void* addr);
void GetOsdConfigParam(void* addr);
void GetGsHParam(void* addr1, void* addr2, void* addr3);
int32_t GetGsVParam(void);
void SetGsHParam(void* addr1, void* addr2, void* addr3, void* addr4);
void SetGsVParam(int32_t arg1);
//TLB functions are only available if InitTLBFunctions() is run (Normally run by crt0).
int PutTLBEntry(unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1);
int iPutTLBEntry(unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1);
int _SetTLBEntry(unsigned int index, unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1);
int iSetTLBEntry(unsigned int index, unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1);
int GetTLBEntry(unsigned int index, unsigned int *PageMask, unsigned int *EntryHi, unsigned int *EntryLo0, unsigned int *EntryLo1);
int iGetTLBEntry(unsigned int index, unsigned int *PageMask, unsigned int *EntryHi, unsigned int *EntryLo0, unsigned int *EntryLo1);
int ProbeTLBEntry(unsigned int EntryHi, unsigned int *PageMask, unsigned int *EntryLo0, unsigned int *EntryLo1);
int iProbeTLBEntry(unsigned int EntryHi, unsigned int *PageMask, unsigned int *EntryLo0, unsigned int *EntryLo1);
int ExpandScratchPad(unsigned int page);
void EnableIntcHandler(uint32_t cause);
void iEnableIntcHandler(uint32_t cause);
void DisableIntcHandler(uint32_t cause);
void iDisableIntcHandler(uint32_t cause);
void EnableDmacHandler(uint32_t channel);
void iEnableDmacHandler(uint32_t channel);
void DisableDmacHandler(uint32_t channel);
void iDisableDmacHandler(uint32_t channel);
void KSeg0(int32_t arg1);
int32_t EnableCache(int32_t cache);
int32_t DisableCache(int32_t cache);
uint32_t GetCop0(int32_t reg_id);
void FlushCache(int32_t operation);
uint32_t CpuConfig(uint32_t config);
uint32_t iGetCop0(int32_t reg_id);
void iFlushCache(int32_t operation);
uint32_t iCpuConfig(uint32_t config);
void SetCPUTimerHandler(void (*handler)(void));
void SetCPUTimer(int32_t compval);
//These two are not available in the unpatched Protokernel (Unpatched SCPH-10000 and SCPH-15000 kernels).
void SetOsdConfigParam2(void* config, int32_t size, int32_t offset);
void GetOsdConfigParam2(void* config, int32_t size, int32_t offset);
uint64_t GsGetIMR(void);
uint64_t iGsGetIMR(void);
uint64_t GsPutIMR(uint64_t imr);
uint64_t iGsPutIMR(uint64_t imr);
void SetPgifHandler(void* handler);
void SetVSyncFlag(uint32_t *, uint64_t *);
void SetSyscall(int32_t syscall_num, void* handler);
//void _print(const char *fmt, ...); // null function
void SifStopDma(void); //Disables SIF0 (IOP -> EE).
uint32_t SifDmaStat( uint32_t id );
uint32_t iSifDmaStat( uint32_t id );
uint32_t SifSetDma( sif_dma_transfer_t *sdd, uint32_t len);
uint32_t iSifSetDma( sif_dma_transfer_t *sdd, uint32_t len);
//Enables SIF0 (IOP -> EE). Sets channel 5 CHCR to 0x184 (CHAIN, TIE and STR).
void SifSetDChain(void);
void iSifSetDChain(void);
//Sets/gets SIF register values (Refer to sifdma.h for a register list).
int SifSetReg(uint32_t register_num, int register_value);
int SifGetReg(uint32_t register_num);
void _ExecOSD(int num_args, char *args[]) __attribute__((noreturn));
int32_t Deci2Call(int32_t , uint32_t *);
void PSMode(void);
int32_t MachineType(void);
int32_t GetMemorySize(void);
//Internal function for getting board-specific offsets, only present in later kernels (ROMVER > 20010608).
void _GetGsDxDyOffset(int mode, int *dx, int *dy, int *dw, int *dh);
//Internal function for reinitializing the TLB, only present in later kernels. Please use InitTLB() instead to initialize the TLB with all kernels.
int _InitTLB(void);
/* (PSX only) Sets the memory size. 0 = 64MB mode, 1 = 32MB mode. The mode is only binding when either _InitTLB() or the PSX ExecPS2() syscall is called.
The stack pointer must remain in range of usable memory, or a TLB exception will occur. */
int SetMemoryMode(int mode); //Arbitrarily named.
void _SyncDCache(void *start, void *end);
void _InvalidDCache(void *start, void *end);
/* stdlib - program termination */
void abort(void) __attribute__((noreturn));
void exit(int retval) __attribute__((noreturn));
void _exit(int retval) __attribute__((noreturn));
/* errno.h */
#ifndef errno
extern int errno __attribute__((section("data")));
int *__errno(void);
#endif
void *GetSyscallHandler(int syscall_no);
void *GetExceptionHandler(int except_no);
void *GetInterruptHandler(int intr_no);
/* Helper functions for kernel patching */
int kCopy(void *dest, const void *src, int size);
int kCopyBytes(void *dest, const void *src, int size);
int Copy(void *dest, const void *src, int size);
void setup(int syscall_num, void* handler); //alias of "SetSyscall"
void *GetEntryAddress(int syscall);
#ifdef __cplusplus
}
#endif
__externC uint32_t *sps2_syscall( uint32_t syscall_no, ... );
#endif /* __KERNEL_H__ */