PS2SDK
PS2 Homebrew Libraries
Loading...
Searching...
No Matches
kernel.h
Go to the documentation of this file.
1/*
2# _____ ___ ____ ___ ____
3# ____| | ____| | | |____|
4# | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5#-----------------------------------------------------------------------
6# (C)2001, Gustavo Scotti (gustavo@scotti.com)
7# (c) 2003 Marcus R. Brown <mrbrown@0xd6.org>
8# Licenced under Academic Free License version 2.0
9# Review ps2sdk README & LICENSE files for further details.
10*/
11
17#ifndef __KERNEL_H__
18#define __KERNEL_H__
19
20#include <stddef.h>
21#include <stdarg.h>
22#include <sifdma.h>
23
24#define DI DIntr
25#define EI EIntr
26
27// Workaround for EE kernel bug: call this immediately before returning from any interrupt handler.
28#define ExitHandler() asm volatile("sync\nei\n")
29
30// note: 'sync' is the same as 'sync.l'
31#define EE_SYNC() __asm__ volatile("sync")
32#define EE_SYNCL() __asm__ volatile("sync.l")
33#define EE_SYNCP() __asm__ volatile("sync.p")
34
35#define UNCACHED_SEG(x) \
36 ((void *)(((u32)(x)) | 0x20000000))
37
38#define IS_UNCACHED_SEG(x) \
39 (((u32)(x)) & 0x20000000)
40
41#define UCAB_SEG(x) \
42 ((void *)(((u32)(x)) | 0x30000000))
43
44#define PUSHDATA(t, x, v, l) \
45 *(t *)(x) = (v); \
46 (l) = sizeof(t)
47
48#define POPDATA(t, x, v, l) \
49 (v) = *(t *)(x); \
50 (l) = sizeof(t)
51
52#define ALIGNED(x) __attribute__((aligned((x))))
53
54// GP functions
55void *ChangeGP(void *gp);
56void SetGP(void *gp);
57void *GetGP(void);
58
59extern void *_gp;
60#define SetModuleGP() ChangeGP(&_gp)
61
67#define TH_SELF 0
68
70#define MAX_THREADS 256 // A few will be used for the kernel patches. Thread 0 is always the idle thread.
71#define MAX_SEMAPHORES 256 // A few will be used for the kernel patches.
72#define MAX_PRIORITY 128
73#define MAX_HANDLERS 128
74#define MAX_ALARMS 64
75
77#define WRITEBACK_DCACHE 0
78#define INVALIDATE_DCACHE 1
79#define INVALIDATE_ICACHE 2
80#define INVALIDATE_CACHE 3 // Invalidate both data & instruction caches.
81
83enum {
84 INTC_GS,
85 INTC_SBUS,
86 INTC_VBLANK_S,
87 INTC_VBLANK_E,
88 INTC_VIF0,
89 INTC_VIF1,
90 INTC_VU0,
91 INTC_VU1,
92 INTC_IPU,
93 INTC_TIM0,
94 INTC_TIM1,
95 INTC_TIM2,
96 // INTC_TIM3, // Reserved by the EE kernel for alarms (do not use)
97 INTC_SFIFO = 13, // Error encountered during SFIFO transfer
98 INTC_VU0WD // VU0 WatchDog; ForceBreak is sent to VU0 if left in RUN state for extended periods of time.
99};
100
101// For backward-compatibility
102#define kINTC_GS INTC_GS
103#define kINTC_SBUS INTC_SBUS
104#define kINTC_VBLANK_START INTC_VBLANK_S
105#define kINTC_VBLANK_END INTC_VBLANK_E
106#define kINTC_VIF0 INTC_VIF0
107#define kINTC_VIF1 INTC_VIF1
108#define kINTC_VU0 INTC_VU0
109#define kINTC_VU1 INTC_VU1
110#define kINTC_IPU INTC_IPU
111#define kINTC_TIMER0 INTC_TIM0
112#define kINTC_TIMER1 INTC_TIM1
113
115enum {
116 DMAC_VIF0,
117 DMAC_VIF1,
118 DMAC_GIF,
119 DMAC_FROM_IPU,
120 DMAC_TO_IPU,
121 DMAC_SIF0,
122 DMAC_SIF1,
123 DMAC_SIF2,
124 DMAC_FROM_SPR,
125 DMAC_TO_SPR,
126
127 DMAC_CIS = 13, // Channel interrupt
128 DMAC_MEIS, // MemFIFO empty interrupt
129 DMAC_BEIS, // Bus error interrupt
130};
131
133#define INIT_DMAC 0x01
134#define INIT_VU1 0x02
135#define INIT_VIF1 0x04
136#define INIT_GIF 0x08
137#define INIT_VU0 0x10
138#define INIT_VIF0 0x20
139#define INIT_IPU 0x40
140
141static inline void nopdelay(void)
142{
143 int i = 0xfffff;
144
145 do {
146 __asm__("nop\nnop\nnop\nnop\nnop\n");
147 } while (i-- != -1);
148}
149
150static inline int ee_get_opmode(void)
151{
152 u32 status;
153
154 __asm__ volatile(
155 ".set\tpush\n\t"
156 ".set\tnoreorder\n\t"
157 "mfc0\t%0, $12\n\t"
158 ".set\tpop\n\t"
159 : "=r"(status));
160
161 return ((status >> 3) & 3);
162}
163
164static inline int ee_set_opmode(u32 opmode)
165{
166 u32 status, mask;
167
168 __asm__ volatile(
169 ".set\tpush\n\t"
170 ".set\tnoreorder\n\t"
171 "mfc0\t%0, $12\n\t"
172 "li\t%1, 0xffffffe7\n\t"
173 "and\t%0, %1\n\t"
174 "or\t%0, %2\n\t"
175 "mtc0\t%0, $12\n\t"
176 "sync.p\n\t"
177 ".set\tpop\n\t"
178 : "=r"(status), "=r"(mask)
179 : "r"(opmode));
180
181 return ((status >> 3) & 3);
182}
183
184static inline int ee_kmode_enter()
185{
186 u32 status, mask;
187
188 __asm__ volatile(
189 ".set\tpush\n\t"
190 ".set\tnoreorder\n\t"
191 "mfc0\t%0, $12\n\t"
192 "li\t%1, 0xffffffe7\n\t"
193 "and\t%0, %1\n\t"
194 "mtc0\t%0, $12\n\t"
195 "sync.p\n\t"
196 ".set\tpop\n\t"
197 : "=r"(status), "=r"(mask));
198
199 return status;
200}
201
202static inline int ee_kmode_exit()
203{
204 int status;
205
206 __asm__ volatile(
207 ".set\tpush\n\t"
208 ".set\tnoreorder\n\t"
209 "mfc0\t%0, $12\n\t"
210 "ori\t%0, 0x10\n\t"
211 "mtc0\t%0, $12\n\t"
212 "sync.p\n\t"
213 ".set\tpop\n\t"
214 : "=r"(status));
215
216 return status;
217}
218
219typedef struct t_ee_sema
220{
221 int count,
222 max_count,
223 init_count,
224 wait_threads;
225 u32 attr,
226 option;
227} ee_sema_t;
228
229typedef struct t_ee_thread
230{
231 int status; // 0x00
232 void *func; // 0x04
233 void *stack; // 0x08
234 int stack_size; // 0x0C
235 void *gp_reg; // 0x10
236 int initial_priority; // 0x14
237 int current_priority; // 0x18
238 u32 attr; // 0x1C
239 u32 option; // 0x20 Do not use - officially documented to not work.
240
242
244#define THS_RUN 0x01
245#define THS_READY 0x02
246#define THS_WAIT 0x04
247#define THS_SUSPEND 0x08
248#define THS_WAITSUSPEND 0x0c
249#define THS_DORMANT 0x10
250
252#define TSW_NONE 0 // Thread is not in WAIT state
253#define TSW_SLEEP 1
254#define TSW_SEMA 2
255
256// sizeof() == 0x30
257typedef struct t_ee_thread_status
258{
259 int status; // 0x00
260 void *func; // 0x04
261 void *stack; // 0x08
262 int stack_size; // 0x0C
263 void *gp_reg; // 0x10
264 int initial_priority; // 0x14
265 int current_priority; // 0x18
266 u32 attr; // 0x1C
267 u32 option; // 0x20
268 u32 waitType; // 0x24
269 u32 waitId; // 0x28
270 u32 wakeupCount; // 0x2C
272
275 CPU_CONFIG_ENABLE_DIE = 0, // Enable Dual Issue
276 CPU_CONFIG_ENABLE_ICE, // Enable Instruction Cache
277 CPU_CONFIG_ENABLE_DCE, // Enable Data Cache
278 CPU_CONFIG_DISBLE_DIE, // Disable Dual Issue
279 CPU_CONFIG_DISBLE_ICE, // Disable Instruction Cache
280 CPU_CONFIG_DISBLE_DCE // Disable Data Cache
281};
282
284#define CPU_DATA_CACHE 1
285#define CPU_INSTRUCTION_CACHE 2
286
288enum {
289 COP0_INDEX,
290 COP0_RANDOM,
291 COP0_ENTRYLO0,
292 COP0_ENTRYLO1,
293 COP0_CONTEXT,
294 COP0_PAGEMASK,
295 COP0_WIRED,
296 // 7 reserved
297 COP0_BADVADDR = 8,
298 COP0_COUNT,
299 COP0_ENTRYHI,
300 COP0_COMPARE,
301 COP0_STATUS,
302 COP0_CAUSE,
303 COP0_EPC,
304 COP0_PRID,
305 COP0_CONFIG,
306 // 17-22 reserved
307 COP0_BADPADDR = 23,
308 COP0_DEBUG,
309 COP0_PERF,
310 // 26-27 reserved
311 COP0_TAGLO = 28,
312 COP0_TAGHI,
313 COP0_ERROREPC,
314};
315
316#ifdef __cplusplus
317extern "C" {
318#endif
319
320/* Initialization/deinitialization routines. */
321void _InitSys(void); // Run by crt0
322
323void TerminateLibrary(void); // Run by crt0
324
325/* Thread update functions */
326int InitThread(void); // Run by _InitSys
327
328s32 iWakeupThread(s32 thread_id);
329s32 iRotateThreadReadyQueue(s32 priority);
330s32 iSuspendThread(s32 thread_id);
331
332/* TLB update functions */
333void InitTLBFunctions(void); // Run by _InitSys
334
335void InitTLB(void);
336void Exit(s32 exit_code) __attribute__((noreturn));
337s32 ExecPS2(void *entry, void *gp, int num_args, char *args[]);
338void LoadExecPS2(const char *filename, s32 num_args, char *args[]) __attribute__((noreturn));
339void ExecOSD(int num_args, char *args[]) __attribute__((noreturn));
340
341/* Alarm update functions */
342void InitAlarm(void); // Run by _InitSys
343
344/* libosd update functions */
345void InitExecPS2(void); // ExecPS2 patch only. Run by _InitSys, Exit, LoadExecPS2, ExecPS2 and ExecOSD
346void InitOsd(void); // ExecPS2 + System Configuration patches. Please refer to the comments within libosd_full.c
347
348int PatchIsNeeded(void); // Indicates whether the patch is required.
349
350// Debug (print) update functions:
351void InitDebug(void);
352
353/* Glue routines. */
354int DIntr(void);
355int EIntr(void);
356
357int EnableIntc(int intc);
358int DisableIntc(int intc);
359int EnableDmac(int dmac);
360int DisableDmac(int dmac);
361
362int iEnableIntc(int intc);
363int iDisableIntc(int intc);
364int iEnableDmac(int dmac);
365int iDisableDmac(int dmac);
366
367void SyncDCache(void *start, void *end);
368void iSyncDCache(void *start, void *end);
369void InvalidDCache(void *start, void *end);
370void iInvalidDCache(void *start, void *end);
371
372/* System call prototypes */
373void ResetEE(u32 init_bitfield);
374void SetGsCrt(s16 interlace, s16 pal_ntsc, s16 field);
375void KExit(s32 exit_code) __attribute__((noreturn));
376void _LoadExecPS2(const char *filename, s32 num_args, char *args[]) __attribute__((noreturn));
377s32 _ExecPS2(void *entry, void *gp, int num_args, char *args[]);
378void RFU009(u32 arg0, u32 arg1);
379s32 AddSbusIntcHandler(s32 cause, void (*handler)(int call));
380s32 RemoveSbusIntcHandler(s32 cause);
381s32 Interrupt2Iop(s32 cause);
382void SetVTLBRefillHandler(s32 handler_num, void *handler_func);
383void SetVCommonHandler(s32 handler_num, void *handler_func);
384void SetVInterruptHandler(s32 handler_num, void *handler_func);
385s32 AddIntcHandler(s32 cause, s32 (*handler_func)(s32 cause), s32 next);
386s32 AddIntcHandler2(s32 cause, s32 (*handler_func)(s32 cause, void *arg, void *addr), s32 next, void *arg);
387s32 RemoveIntcHandler(s32 cause, s32 handler_id);
388s32 AddDmacHandler(s32 channel, s32 (*handler)(s32 channel), s32 next);
389s32 AddDmacHandler2(s32 channel, s32 (*handler)(s32 channel, void *arg, void *addr), s32 next, void *arg);
390s32 RemoveDmacHandler(s32 channel, s32 handler_id);
391s32 _EnableIntc(s32 cause);
392s32 _DisableIntc(s32 cause);
393s32 _EnableDmac(s32 channel);
394s32 _DisableDmac(s32 channel);
395
396// Alarm value is in H-SYNC ticks.
397s32 SetAlarm(u16 time, void (*callback)(s32 alarm_id, u16 time, void *common), void *common);
398s32 _SetAlarm(u16 time, void (*callback)(s32 alarm_id, u16 time, void *common), void *common);
399s32 ReleaseAlarm(s32 alarm_id);
400s32 _ReleaseAlarm(s32 alarm_id);
401
402s32 _iEnableIntc(s32 cause);
403s32 _iDisableIntc(s32 cause);
404s32 _iEnableDmac(s32 channel);
405s32 _iDisableDmac(s32 channel);
406
407s32 iSetAlarm(u16 time, void (*callback)(s32 alarm_id, u16 time, void *common), void *common);
408s32 _iSetAlarm(u16 time, void (*callback)(s32 alarm_id, u16 time, void *common), void *common);
409s32 iReleaseAlarm(s32 alarm_id);
410s32 _iReleaseAlarm(s32 alarm_id);
411
412s32 CreateThread(ee_thread_t *thread);
413s32 DeleteThread(s32 thread_id);
414s32 StartThread(s32 thread_id, void *args);
415void ExitThread(void);
416void ExitDeleteThread(void);
417s32 TerminateThread(s32 thread_id);
418s32 iTerminateThread(s32 thread_id);
419// void DisableDispatchThread(void); // not supported
420// void EnableDispatchThread(void); // not supported
421s32 ChangeThreadPriority(s32 thread_id, s32 priority);
422s32 iChangeThreadPriority(s32 thread_id, s32 priority);
423s32 RotateThreadReadyQueue(s32 priority);
424s32 _iRotateThreadReadyQueue(s32 priority);
425s32 ReleaseWaitThread(s32 thread_id);
426s32 iReleaseWaitThread(s32 thread_id);
427s32 GetThreadId(void);
428s32 _iGetThreadId(void); // This is actually GetThreadId(), used for a hack by SCE to work around the iWakeupThread design flaw.
429s32 ReferThreadStatus(s32 thread_id, ee_thread_status_t *info);
430s32 iReferThreadStatus(s32 thread_id, ee_thread_status_t *info);
431s32 SleepThread(void);
432s32 WakeupThread(s32 thread_id);
433s32 _iWakeupThread(s32 thread_id);
434s32 CancelWakeupThread(s32 thread_id);
435s32 iCancelWakeupThread(s32 thread_id);
436s32 SuspendThread(s32 thread_id);
437s32 _iSuspendThread(s32 thread_id);
438s32 ResumeThread(s32 thread_id);
439s32 iResumeThread(s32 thread_id);
440
441u8 RFU059(void);
442
443void *SetupThread(void *gp, void *stack, s32 stack_size, void *args, void *root_func);
444void SetupHeap(void *heap_start, s32 heap_size);
445void *EndOfHeap(void);
446
447s32 CreateSema(ee_sema_t *sema);
448s32 DeleteSema(s32 sema_id);
449s32 SignalSema(s32 sema_id);
450s32 iSignalSema(s32 sema_id);
451s32 WaitSema(s32 sema_id);
452s32 PollSema(s32 sema_id);
453s32 iPollSema(s32 sema_id);
454s32 ReferSemaStatus(s32 sema_id, ee_sema_t *sema);
455s32 iReferSemaStatus(s32 sema_id, ee_sema_t *sema);
456s32 iDeleteSema(s32 sema_id);
457void SetOsdConfigParam(void *addr);
458void GetOsdConfigParam(void *addr);
459void GetGsHParam(void *addr1, void *addr2, void *addr3);
460s32 GetGsVParam(void);
461void SetGsHParam(void *addr1, void *addr2, void *addr3, void *addr4);
462void SetGsVParam(s32 arg1);
463
464// TLB functions are only available if InitTLBFunctions() is run (Normally run by crt0).
465int PutTLBEntry(unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1);
466int iPutTLBEntry(unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1);
467int _SetTLBEntry(unsigned int index, unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1);
468int iSetTLBEntry(unsigned int index, unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1);
469int GetTLBEntry(unsigned int index, unsigned int *PageMask, unsigned int *EntryHi, unsigned int *EntryLo0, unsigned int *EntryLo1);
470int iGetTLBEntry(unsigned int index, unsigned int *PageMask, unsigned int *EntryHi, unsigned int *EntryLo0, unsigned int *EntryLo1);
471int ProbeTLBEntry(unsigned int EntryHi, unsigned int *PageMask, unsigned int *EntryLo0, unsigned int *EntryLo1);
472int iProbeTLBEntry(unsigned int EntryHi, unsigned int *PageMask, unsigned int *EntryLo0, unsigned int *EntryLo1);
473int ExpandScratchPad(unsigned int page);
474
475void EnableIntcHandler(u32 cause);
476void iEnableIntcHandler(u32 cause);
477void DisableIntcHandler(u32 cause);
478void iDisableIntcHandler(u32 cause);
479void EnableDmacHandler(u32 channel);
480void iEnableDmacHandler(u32 channel);
481void DisableDmacHandler(u32 channel);
482void iDisableDmacHandler(u32 channel);
483void KSeg0(s32 arg1);
484s32 EnableCache(s32 cache);
485s32 DisableCache(s32 cache);
486u32 GetCop0(s32 reg_id);
487void FlushCache(s32 operation);
488u32 CpuConfig(u32 config);
489u32 iGetCop0(s32 reg_id);
490void iFlushCache(s32 operation);
491u32 iCpuConfig(u32 config);
492void SetCPUTimerHandler(void (*handler)(void));
493void SetCPUTimer(s32 compval);
494
495// These two are not available in the unpatched Protokernel (Unpatched SCPH-10000 and SCPH-15000 kernels).
496void SetOsdConfigParam2(void *config, s32 size, s32 offset);
497void GetOsdConfigParam2(void *config, s32 size, s32 offset);
498
499u64 GsGetIMR(void);
500u64 iGsGetIMR(void);
501u64 GsPutIMR(u64 imr);
502u64 iGsPutIMR(u64 imr);
503void SetPgifHandler(void *handler);
504void SetVSyncFlag(u32 *, u64 *);
505void SetSyscall(s32 syscall_num, void *handler);
506void _print(const char *fmt, ...); // Disabled by default, must call InitDebug() to enable
507
508void SifStopDma(void); // Disables SIF0 (IOP -> EE).
509
510s32 SifDmaStat(u32 id);
511s32 iSifDmaStat(u32 id);
512u32 SifSetDma(SifDmaTransfer_t *sdd, s32 len);
513u32 iSifSetDma(SifDmaTransfer_t *sdd, s32 len);
514
515// Enables SIF0 (IOP -> EE). Sets channel 5 CHCR to 0x184 (CHAIN, TIE and STR).
516void SifSetDChain(void);
517void iSifSetDChain(void);
518
519// Sets/gets SIF register values (Refer to sifdma.h for a register list).
520int SifSetReg(u32 register_num, int register_value);
521int SifGetReg(u32 register_num);
522
523void _ExecOSD(int num_args, char *args[]) __attribute__((noreturn));
524s32 Deci2Call(s32, u32 *);
525void PSMode(void);
526s32 MachineType(void);
527s32 GetMemorySize(void);
528
529// Internal function for getting board-specific offsets, only present in later kernels (ROMVER > 20010608).
530void _GetGsDxDyOffset(int mode, int *dx, int *dy, int *dw, int *dh);
531
532// Internal function for reinitializing the TLB, only present in later kernels. Please use InitTLB() instead to initialize the TLB with all kernels.
533int _InitTLB(void);
534/* (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.
535 The stack pointer must remain in range of usable memory, or a TLB exception will occur. */
536int SetMemoryMode(int mode); // Arbitrarily named.
537
538void _SyncDCache(void *start, void *end);
539void _InvalidDCache(void *start, void *end);
540
541void *GetSyscallHandler(int syscall_no);
542void *GetExceptionHandler(int except_no);
543void *GetInterruptHandler(int intr_no);
544
545/* Helper functions for kernel patching */
546int kCopy(void *dest, const void *src, int size);
547int kCopyBytes(void *dest, const void *src, int size);
548int Copy(void *dest, const void *src, int size);
549void setup(int syscall_num, void *handler); // alias of "SetSyscall"
550void *GetEntryAddress(int syscall);
551
552// Helpers marcos for no-patch versions
553// Useful to build a special version of libkernel that does not contain any runtime patches (useful for loaders/resident programs).
554#define DISABLE_PATCHED_Exit() \
555 void Exit(s32 exit_code) { KExit(exit_code); }
556
557#define DISABLE_PATCHED_LoadExecPS2() \
558 void LoadExecPS2(const char *filename, s32 num_args, char *args[]) { _LoadExecPS2(filename, num_args, args); }
559
560#define NO_PATCHED_ExecOSD() \
561 void ExecOSD(int num_args, char *args[]) { _ExecOSD(num_args, args); }
562
563#define DISABLE_TimerSystemTime() \
564 s32 InitTimer(s32 in_mode) {(void)in_mode; return 0;} \
565 s32 EndTimer(void) {return 0;} \
566 s32 StartTimerSystemTime(void) {return 0;} \
567 s32 StopTimerSystemTime(void) {return 0;}
568
569#define DISABLE_TimerAlarm() \
570 void ForTimer_InitAlarm(void) {}
571
572#define DISABLE_PATCHED_ALARMS() \
573 void InitAlarm(void) {}
574
575#define DISABLE_PATCHED_THREADS() \
576 int InitThread(void) { return 0; } \
577 s32 iRotateThreadReadyQueue(s32 priority) { return _iRotateThreadReadyQueue(priority); } \
578 s32 iWakeupThread(s32 thread_id) { return _iWakeupThread(thread_id); } \
579 s32 iSuspendThread(s32 thread_id) { return _iSuspendThread(thread_id); }
580
581#define DISABLE_PATCHED_ExecPS2() \
582 void InitExecPS2(void) {} \
583 s32 ExecPS2(void *entry, void *gp, int num_args, char *args[]) { return _ExecPS2(entry, gp, num_args, args); }
584
585#define DISABLE_PATCHED_TLBFunctions() \
586 void InitTLBFunctions(void) {} \
587 void InitTLB(void) {}
588
589#define DISABLE_PATCHED_FUNCTIONS() \
590 DISABLE_PATCHED_ALARMS() \
591 DISABLE_PATCHED_THREADS() \
592 DISABLE_PATCHED_ExecPS2() \
593 DISABLE_PATCHED_TLBFunctions() \
594 DISABLE_PATCHED_Exit() \
595 DISABLE_PATCHED_LoadExecPS2()
596
597#define DISABLE_EXTRA_TIMERS_FUNCTIONS() \
598 DISABLE_TimerSystemTime() \
599 DISABLE_TimerAlarm()
600
601#ifdef __cplusplus
602}
603#endif
604
605#endif /* __KERNEL_H__ */
CPU_CONFIG
Definition kernel.h:274