ps2sdk  1.1
A collection of Open Source libraries used for developing applications on Sony's PlayStation 2® (PS2).
tlbfunc.c
Go to the documentation of this file.
1 /*
2 # _____ ___ ____ ___ ____
3 # ____| | ____| | | |____|
4 # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
5 #-----------------------------------------------------------------------
6 # Licenced under Academic Free License version 2.0
7 # Review ps2sdk README & LICENSE files for further details.
8 */
9 
10 #include <kernel.h>
11 #include <stdio.h>
12 
13 #define kprintf(args...) //sio_printf(args)
14 
15 static int InitTLB32MB(void);
16 
17 struct SyscallData{
18  int syscall;
19  void *function;
20 };
21 
22 static const struct SyscallData SysEntry[]={
23  {0x5A, &kCopy},
24  {0x5B, (void*)0x80075000},
25  {0x54, NULL}, //???
26  {0x55, NULL}, //PutTLBEntry
27  {0x56, NULL}, //SetTLBEntry
28  {0x57, NULL}, //GetTLBEntry
29  {0x58, NULL}, //ProbeTLBEntry
30  {0x59, NULL}, //ExpandScratchPad
31 };
32 
33 extern char **_kExecArg;
34 
35 extern unsigned char tlbsrc[];
36 extern unsigned int size_tlbsrc;
37 
39 void setup(int syscall, void *function);
40 
41 void InitTLBFunctions(void){
42  int i;
43 
44  setup(SysEntry[0].syscall, SysEntry[0].function);
45  Copy((void*)0x80075000, tlbsrc, size_tlbsrc);
46  FlushCache(0);
47  FlushCache(2);
48  setup(SysEntry[1].syscall, SysEntry[1].function);
49 
50  for(i=3; i<8; i++){
52  }
53 
55 }
56 
57 void InitTLB(void){
58  if(GetMemorySize()==0x2000000){
59  InitTLB32MB();
60  }
61  else{
62  _InitTLB();
63  }
64 }
65 
66 struct TLBEntry{
67  unsigned int PageMask;
68  unsigned int EntryHi;
69  unsigned int EntryLo0;
70  unsigned int EntryLo1;
71 };
72 
73 struct TLBInfo{
74  unsigned int NumKernelTLBEntries;
75  unsigned int NumDefaultTLBEntries;
76  unsigned int NumExtendedTLBEntries;
77  unsigned int NumWiredEntries;
78  const struct TLBEntry *kernelTLB;
79  const struct TLBEntry *defaultTLB;
80  const struct TLBEntry *extendedTLB;
81 };
82 
83 #define TLB_NUM_KERNEL_ENTRIES 0x0D
84 #define TLB_NUM_DEFAULT_ENTRIES 0x12
85 #define TLB_NUM_EXTENDED_ENTRIES 0x08
86 
87 //Compile-time sanity checks.
88 #if (TLB_NUM_KERNEL_ENTRIES+TLB_NUM_DEFAULT_ENTRIES+TLB_NUM_EXTENDED_ENTRIES>=0x31)
89  #error TLB over flow
90 #endif
91 
92 static const struct TLBEntry kernelTLB[TLB_NUM_KERNEL_ENTRIES]={
93  {0x00000000, 0x70000000, 0x80000007, 0x00000007},
94  {0x00006000, 0xFFFF8000, 0x00001E1F, 0x00001F1F},
95  {0x00000000, 0x10000000, 0x00400017, 0x00400053},
96  {0x00000000, 0x10002000, 0x00400097, 0x004000D7},
97  {0x00000000, 0x10004000, 0x00400117, 0x00400157},
98  {0x00000000, 0x10006000, 0x00400197, 0x004001D7},
99  {0x00000000, 0x10008000, 0x00400217, 0x00400257},
100  {0x00000000, 0x1000A000, 0x00400297, 0x004002D7},
101  {0x00000000, 0x1000C000, 0x00400313, 0x00400357},
102  {0x00000000, 0x1000E000, 0x00400397, 0x004003D7},
103  {0x0001E000, 0x11000000, 0x00440017, 0x00440415},
104  {0x0001E000, 0x12000000, 0x00480017, 0x00480415},
105  {0x01FFE000, 0x1E000000, 0x00780017, 0x007C0017}
106 };
107 
108 static const struct TLBEntry defaultTLB[TLB_NUM_DEFAULT_ENTRIES]={
109  {0x0007E000, 0x00080000, 0x0000201F, 0x0000301F},
110  {0x0007E000, 0x00100000, 0x0000401F, 0x0000501F},
111  {0x0007E000, 0x00180000, 0x0000601F, 0x0000701F},
112  {0x001FE000, 0x00200000, 0x0000801F, 0x0000C01F},
113  {0x001FE000, 0x00400000, 0x0001001F, 0x0001401F},
114  {0x001FE000, 0x00600000, 0x0001801F, 0x0001C01F},
115  {0x007FE000, 0x00800000, 0x0002001F, 0x0003001F},
116  {0x007FE000, 0x01000000, 0x0004001F, 0x0005001F},
117  {0x007FE000, 0x01800000, 0x0006001F, 0x0007001F},
118  {0x0007E000, 0x20080000, 0x00002017, 0x00003017},
119  {0x0007E000, 0x20100000, 0x00004017, 0x00005017},
120  {0x0007E000, 0x20180000, 0x00006017, 0x00007017},
121  {0x001FE000, 0x20200000, 0x00008017, 0x0000C017},
122  {0x001FE000, 0x20400000, 0x00010017, 0x00014017},
123  {0x001FE000, 0x20600000, 0x00018017, 0x0001C017},
124  {0x007FE000, 0x20800000, 0x00020017, 0x00030017},
125  {0x007FE000, 0x21000000, 0x00040017, 0x00050017},
126  {0x007FE000, 0x21800000, 0x00060017, 0x00070017}
127 };
128 
129 static const struct TLBEntry extendTLB[TLB_NUM_EXTENDED_ENTRIES]={
130  {0x0007E000, 0x30100000, 0x0000403F, 0x0000503F},
131  {0x0007E000, 0x30180000, 0x0000603F, 0x0000703F},
132  {0x001FE000, 0x30200000, 0x0000803F, 0x0000C03F},
133  {0x001FE000, 0x30400000, 0x0001003F, 0x0001403F},
134  {0x001FE000, 0x30600000, 0x0001803F, 0x0001C03F},
135  {0x007FE000, 0x30800000, 0x0002003F, 0x0003003F},
136  {0x007FE000, 0x31000000, 0x0004003F, 0x0005003F},
137  {0x007FE000, 0x31800000, 0x0006003F, 0x0007003F}
138 };
139 
141 
142 static int InitTLB32MB(void){
143  unsigned int i, NumTlbEntries, value, TlbEndIndex;
144  const struct TLBEntry *TLBEntry;
145 
147 
148  __asm volatile( "mtc0 $zero, $6\n"
149  "sync.p\n");
150 
151  if(TLBInfo.NumKernelTLBEntries>=0x31){
152  kprintf("# TLB over flow (1)");
153  Exit(1);
154  }
155 
158  }
159 
160  if(TLBInfo.NumDefaultTLBEntries+i>=0x31){
161  kprintf("# TLB over flow (2)");
162  Exit(1);
163  }
164 
165  for(TLBEntry=TLBInfo.defaultTLB,TlbEndIndex=TLBInfo.NumDefaultTLBEntries+i; i<TlbEndIndex; i++,TLBEntry++){
167  }
168 
169  TLBInfo.NumWiredEntries=NumTlbEntries=i;
170  __asm volatile( "mtc0 %0, $6\n"
171  "sync.p\n" ::"r"(NumTlbEntries));
172 
174  if(TLBInfo.NumExtendedTLBEntries+i>=0x31){
175  kprintf("# TLB over flow (3)");
176  Exit(1);
177  }
178 
179  for(TLBEntry=TLBInfo.extendedTLB,TlbEndIndex=TLBInfo.NumExtendedTLBEntries+i; i<TlbEndIndex; i++,TLBEntry++,NumTlbEntries++){
181  }
182  }
183 
184  for(value=0xE0000000+(NumTlbEntries<<13); i<0x30; i++,value+=0x2000){
185  _SetTLBEntry(i, 0, value, 0, 0);
186  }
187 
188  return NumTlbEntries;
189 }
void Exit(s32 exit_code)
int Copy(void *dest, const void *src, int size)
void FlushCache(s32 operation)
int _InitTLB(void)
int kCopy(void *dest, const void *src, int size)
s32 GetMemorySize(void)
int _SetTLBEntry(unsigned int index, unsigned int PageMask, unsigned int EntryHi, unsigned int EntryLo0, unsigned int EntryLo1)
int syscall
Definition: alarm.c:22
unsigned int EntryLo0
Definition: tlbfunc.c:69
unsigned int EntryHi
Definition: tlbfunc.c:68
unsigned int EntryLo1
Definition: tlbfunc.c:70
unsigned int PageMask
Definition: tlbfunc.c:67
const struct TLBEntry * extendedTLB
Definition: tlbfunc.c:80
unsigned int NumKernelTLBEntries
Definition: tlbfunc.c:74
const struct TLBEntry * defaultTLB
Definition: tlbfunc.c:79
unsigned int NumWiredEntries
Definition: tlbfunc.c:77
unsigned int NumDefaultTLBEntries
Definition: tlbfunc.c:75
unsigned int NumExtendedTLBEntries
Definition: tlbfunc.c:76
const struct TLBEntry * kernelTLB
Definition: tlbfunc.c:78
#define NULL
Definition: tamtypes.h:91
#define TLB_NUM_KERNEL_ENTRIES
Definition: tlbfunc.c:83
static int InitTLB32MB(void)
Definition: tlbfunc.c:142
#define TLB_NUM_EXTENDED_ENTRIES
Definition: tlbfunc.c:85
static const struct TLBEntry defaultTLB[TLB_NUM_DEFAULT_ENTRIES]
Definition: tlbfunc.c:108
static const struct TLBEntry extendTLB[TLB_NUM_EXTENDED_ENTRIES]
Definition: tlbfunc.c:129
void InitTLB(void)
Definition: tlbfunc.c:57
#define TLB_NUM_DEFAULT_ENTRIES
Definition: tlbfunc.c:84
void * GetEntryAddress(int syscall)
static const struct TLBEntry kernelTLB[TLB_NUM_KERNEL_ENTRIES]
Definition: tlbfunc.c:92
void InitTLBFunctions(void)
Definition: tlbfunc.c:41
#define kprintf(args...)
Definition: tlbfunc.c:13
static const struct SyscallData SysEntry[]
Definition: tlbfunc.c:22
unsigned char tlbsrc[]
void setup(int syscall, void *function)
char ** _kExecArg
Definition: tlbsrc.c:26
unsigned int size_tlbsrc