-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathmsstor_cache.c
289 lines (236 loc) · 6.97 KB
/
msstor_cache.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
* This file is part of PRO CFW.
* PRO CFW is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* PRO CFW is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with PRO CFW. If not, see <http://www.gnu.org/licenses/ .
*/
#include <pspsdk.h>
#include <pspsysmem_kernel.h>
#include <pspkernel.h>
#include <psputilsforkernel.h>
#include <pspsysevent.h>
#include <pspiofilemgr.h>
#include <stdio.h>
#include <string.h>
#include "printk.h"
#include "utils.h"
#include "main.h"
#include "systemctrl_patch_offset.h"
#define CACHE_BUFSIZE (16 * 1024)
#define CACHE_BUFSIZE_GO (8 * 1024)
static int (*msstor_read)(PspIoDrvFileArg *arg, char *data, int len) = NULL;
static int (*msstor_write)(PspIoDrvFileArg *arg, const char *data, int len) = NULL;
static SceOff (*msstor_lseek)(PspIoDrvFileArg *arg, SceOff ofs, int whence) = NULL;
static int(*msstor_open)(PspIoDrvFileArg *arg, char *file, int flags, SceMode mode) = NULL;
static u32 read_call = 0;
static u32 read_hit = 0;
static u32 read_missed = 0;
static u32 read_uncacheable = 0;
struct MsCache {
char *buf;
int bufsize;
SceOff pos; /* -1 = invalid */
int age;
};
static struct MsCache g_cache;
static int g_cache_cap = 0;
static inline int is_within_range(SceOff pos, SceOff start, int len)
{
if(pos >= start && pos < start + len) {
return 1;
}
return 0;
}
static struct MsCache *get_hit_cache(SceOff pos, int len)
{
if(g_cache.pos != -1) {
if(is_within_range(pos, g_cache.pos, g_cache.bufsize) && is_within_range(pos+len-1, g_cache.pos, g_cache.bufsize)) {
return &g_cache;
}
}
return NULL;
}
static void disable_cache(struct MsCache *cache)
{
cache->pos = -1;
cache->bufsize = 0;
}
static void disable_cache_within_range(SceOff pos, int len)
{
if(g_cache.pos != -1) {
if(is_within_range(pos, g_cache.pos, g_cache.bufsize)) {
disable_cache(&g_cache);
return;
}
if(is_within_range(pos+len-1, g_cache.pos, g_cache.bufsize)) {
disable_cache(&g_cache);
return;
}
if(pos < g_cache.pos && pos+len-1 >= g_cache.pos + g_cache.bufsize) {
disable_cache(&g_cache);
return;
}
}
}
static int msstor_cache_read(PspIoDrvFileArg *arg, char *data, int len)
{
int ret, read_len;
SceOff pos;
struct MsCache *cache;
pos = (*msstor_lseek)(arg, 0, PSP_SEEK_CUR);
cache = get_hit_cache(pos, len);
// cache hit?
if(cache != NULL) {
#if 1
read_len = MIN(len, cache->pos + cache->bufsize - pos);
memcpy(data, cache->buf + pos - cache->pos, read_len);
ret = read_len;
(*msstor_lseek)(arg, pos + ret, PSP_SEEK_SET);
read_hit += len;
#else
// Check validate code
ret = (*msstor_read)(arg, data, len);
if(0 != memcmp(data, cache->buf + pos - cache->pos, len)) {
char buf[256];
sprintf(buf, "%s: 0x%08X <%d> cache mismatched!!!\n", __func__, (uint)pos, (int)len);
sceIoWrite(1, buf, strlen(buf));
}
#endif
} else {
#if 0
{
char buf[256];
sprintf(buf, "%s: 0x%08X <%d>\n", __func__, (uint)pos, (int)len);
sceIoWrite(1, buf, strlen(buf));
}
#endif
cache = &g_cache;
if(len <= g_cache_cap) {
disable_cache(cache);
ret = (*msstor_read)(arg, cache->buf, g_cache_cap);
if(ret >= 0) {
read_len = MIN(len, ret);
memcpy(data, cache->buf, read_len);
cache->pos = pos;
cache->bufsize = ret;
ret = read_len;
(*msstor_lseek)(arg, pos + ret, PSP_SEEK_SET);
} else {
printk("%s: read -> 0x%08X\n", __func__, ret);
}
read_missed += len;
} else {
ret = (*msstor_read)(arg, data, len);
// printk("%s: read len %d too large\n", __func__, len);
read_uncacheable += len;
}
}
read_call += len;
return ret;
}
static int msstor_cache_write(PspIoDrvFileArg *arg, const char *data, int len)
{
int ret;
SceOff pos;
pos = (*msstor_lseek)(arg, 0, PSP_SEEK_CUR);
disable_cache_within_range(pos, len);
ret = (*msstor_write)(arg, data, len);
return ret;
}
static int msstor_cache_open(PspIoDrvFileArg *arg, char *file, int flags, SceMode mode)
{
int ret;
#if 0
{
char buf[256];
sprintf(buf, "%s: %s 0x%08X 0x%08X\n", __func__, file, flags, mode);
sceIoWrite(1, buf, strlen(buf));
}
#endif
disable_cache(&g_cache);
ret = (*msstor_open)(arg, file, flags, mode);
return ret;
}
int msstor_init(void)
{
PspIoDrvFuncs *funcs;
PspIoDrv *pdrv;
SceUID memid;
int bufsize;
if(psp_model == PSP_GO) {
bufsize = CACHE_BUFSIZE_GO;
} else {
bufsize = CACHE_BUFSIZE;
}
if((bufsize % 0x200) != 0) {
printk("%s: alignment error\n", __func__);
return -1;
}
memid = sctrlKernelAllocPartitionMemory(1, "MsStorCache", PSP_SMEM_High, bufsize + 64, NULL);
if(memid < 0) {
printk("%s: sctrlKernelAllocPartitionMemory -> 0x%08X\n", __func__, memid);
return -2;
}
g_cache.buf = sctrlKernelGetBlockHeadAddr(memid);
if(g_cache.buf == NULL) {
return -3;
}
g_cache.buf = (void*)(((u32)g_cache.buf & (~(64-1))) + 64);
g_cache_cap = bufsize;
disable_cache(&g_cache);
if(psp_model == PSP_GO && sctrlKernelBootFrom() == 0x50) {
pdrv = sctrlHENFindDriver("eflash0a0f1p");
} else {
pdrv = sctrlHENFindDriver("msstor0p");
}
if(pdrv == NULL) {
return -4;
}
funcs = pdrv->funcs;
msstor_read = funcs->IoRead;
msstor_write = funcs->IoWrite;
msstor_lseek = funcs->IoLseek;
msstor_open = funcs->IoOpen;
funcs->IoRead = msstor_cache_read;
funcs->IoWrite = msstor_cache_write;
funcs->IoOpen= msstor_cache_open;
return 0;
}
// call @SystemControl:SystemCtrlPrivate,0xD3014719@
void msstor_stat(int reset)
{
char buf[256];
if(read_call != 0) {
sprintf(buf, "Mstor cache size: %dKB\n", g_cache_cap / 1024);
sceIoWrite(1, buf, strlen(buf));
sprintf(buf, "hit percent: %02d%%/%02d%%/%02d%%, [%d/%d/%d/%d]\n",
(int)(100 * read_hit / read_call),
(int)(100 * read_missed / read_call),
(int)(100 * read_uncacheable / read_call),
(int)read_hit, (int)read_missed, (int)read_uncacheable, (int)read_call);
sceIoWrite(1, buf, strlen(buf));
sprintf(buf, "caches stat:\n");
sceIoWrite(1, buf, strlen(buf));
sprintf(buf, "Cache Pos: 0x%08X Bufsize: %d Buf: 0x%08X\n", (uint)g_cache.pos, g_cache.bufsize, (uint)g_cache.buf);
sceIoWrite(1, buf, strlen(buf));
} else {
sprintf(buf, "no msstor cache call yet\n");
sceIoWrite(1, buf, strlen(buf));
}
if(reset) {
read_call = read_hit = read_missed = read_uncacheable = 0;
}
}
// call @SystemControl:SystemCtrlPrivate,0xC0E151D0@
void msstor_disable_cache(void)
{
disable_cache(&g_cache);
}