forked from 400plus/400plus
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcache_hacks.c
509 lines (416 loc) · 11.7 KB
/
cache_hacks.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*
* This file comes from Magic Lantern.
* Thanks to g3gg0 for the idea and implementation.
* Thanks to Sergei for porting it.
*/
#include <vxworks.h>
#include <stdint.h>
#include "cache_hacks.h"
static inline uint32_t cli(void)
{
uint32_t old_cpsr = 0;
uint32_t new_cpsr = 0;
asm volatile (
"MRS %0, CPSR\n"
"ORR %1, %0, #0x80\n" // set I flag to disable IRQ
"MSR CPSR_c, %1\n"
: "=r"(old_cpsr)
: "r"(new_cpsr)
);
return old_cpsr & 0x80; // return true if the flags are set
}
static inline void sei( uint32_t old_cpsr )
{
uint32_t new_cpsr = 0;
asm volatile (
"MRS %0, CPSR\n"
"BIC %0, %0, #0x80\n"
"ORR %0, %0, %1\n"
"MSR CPSR_c, %0"
:
: "r"(new_cpsr), "r"(old_cpsr)
);
}
/* return cache size in bits (13 -> 2^13 -> 8192 -> 8KiB) */
static uint32_t cache_get_size(uint32_t type)
{
uint32_t cache_info = 0;
// get cache type register
// On a 550D: 0x0F112112. 8KB I/D Cache. 4 way set associative.
asm volatile (
"MRC p15, 0, %0, c0, c0, 1\n"
: "=r"(cache_info)
);
/* dcache is described at bit pos 12 */
if (type == TYPE_DCACHE) {
cache_info >>= 12;
}
/* check if size is invalid, or absent flag is set */
uint32_t size = (cache_info >> 6) & 0x0F;
uint32_t absent = (cache_info >> 2) & 0x01;
if((size < 3) || absent) {
return 0;
}
/* return as 2^x */
return size + 9;
}
static uint32_t cache_patch_single_word(uint32_t address, uint32_t data, uint32_t type)
{
uint32_t cache_seg_index_word = (address & (CACHE_INDEX_ADDRMASK(type) | CACHE_WORD_ADDRMASK(type)));
uint32_t cache_tag_index = (address & (CACHE_TAG_ADDRMASK(type) | CACHE_INDEX_ADDRMASK(type))) | 0x10;
if(type == TYPE_ICACHE) {
asm volatile (
/* write index for address to write */
"MCR p15, 3, %0, c15, c0, 0\n"
/* set TAG at given index */
"MCR p15, 3, %1, c15, c1, 0\n"
/* write instruction */
"MCR p15, 3, %2, c15, c3, 0\n"
:
: "r"(cache_seg_index_word), "r"(cache_tag_index), "r"(data)
);
} else {
asm volatile (
/* write index for address to write */
"MCR p15, 3, %0, c15, c0, 0\n"
/* set TAG at given index */
"MCR p15, 3, %1, c15, c2, 0\n"
/* write data */
"MCR p15, 3, %2, c15, c4, 0\n"
:
: "r"(cache_seg_index_word), "r"(cache_tag_index), "r"(data));
}
return 1;
}
/* fetch all the instructions in that temp_cacheline the given address is in.
this is *required* before patching a single address.
if you omit this, only the patched instruction will be correct, the
other instructions around will simply be crap.
warning - this is doing a data fetch (LDR) so DCache patches may cause
unwanted or wanted behavior. make sure you know what you do :)
same applies to dcache routines
*/
static void cache_fetch_line(uint32_t address, uint32_t type)
{
uint32_t base = (address & ~0x1F);
uint32_t temp_cacheline[8];
// our ARM946 has 0x20 byte temp_cachelines. fetch the current line
// thanks to unified memories, we can do LDR on instructions.
uint32_t pos;
for(pos = 0; pos < 8; pos++) {
temp_cacheline[pos] = ((uint32_t *)base)[pos];
}
/* and nail it into locked cache */
for(pos = 0; pos < 8; pos++) {
cache_patch_single_word(base + pos * 4, temp_cacheline[pos], type);
}
}
/* return the tag and content at given index (segment+index+word) */
static void cache_get_content(uint32_t segment, uint32_t index, uint32_t word, uint32_t type, uint32_t *tag, uint32_t *data)
{
uint32_t cache_seg_index_word = 0;
uint32_t stored_tag_index = 0;
uint32_t stored_data = 0;
cache_seg_index_word |= ((segment & CACHE_SEGMENT_BITMASK(type)) << CACHE_SEGMENT_TAGOFFSET(type));
cache_seg_index_word |= ((index & CACHE_INDEX_BITMASK(type)) << CACHE_INDEX_TAGOFFSET(type));
cache_seg_index_word |= ((word & CACHE_WORD_BITMASK(type)) << CACHE_WORD_TAGOFFSET(type));
if(type == TYPE_ICACHE) {
asm volatile (
/* write index for address to write */
"MCR p15, 3, %2, c15, c0, 0\n"
/* get TAG at given index */
"MRC p15, 3, %0, c15, c1, 0\n"
/* get DATA at given index */
"MRC p15, 3, %1, c15, c3, 0\n"
: "=r"(stored_tag_index), "=r"(stored_data)
: "r"(cache_seg_index_word)
);
} else {
asm volatile (
/* write index for address to write */
"MCR p15, 3, %2, c15, c0, 0\n"
/* get TAG at given index */
"MRC p15, 3, %0, c15, c2, 0\n"
/* get DATA at given index */
"MRC p15, 3, %1, c15, c4, 0\n"
: "=r"(stored_tag_index), "=r"(stored_data)
: "r"(cache_seg_index_word)
);
}
*tag = stored_tag_index;
*data = stored_data;
}
/* check if given address is already used or if it is usable for patching */
static uint32_t cache_is_patchable(uint32_t address, uint32_t type)
{
uint32_t stored_tag_index = 0;
uint32_t stored_data = 0;
cache_get_content(
0,
(address & CACHE_INDEX_ADDRMASK(type)) >> CACHE_INDEX_TAGOFFSET(type),
(address & CACHE_WORD_ADDRMASK(type)) >> CACHE_WORD_TAGOFFSET(type),
type,
&stored_tag_index,
&stored_data
);
/* this line is free, so can be used for patching */
if((stored_tag_index & 0x10) == 0) {
return 1;
}
/* now check if the TAG RAM content matches with what we expect and valid bit is set */
uint32_t tag_index_mask = CACHE_TAG_ADDRMASK(type) | CACHE_INDEX_ADDRMASK(type);
uint32_t cache_tag_index = address & tag_index_mask;
if((stored_tag_index & tag_index_mask) == cache_tag_index) {
/* that line is used by the right address, now check data */
if(stored_data == *(uint32_t*)address) { // data is original, so it is patchable
return 1;
}
return 2; // its already patched. so return 2
}
/* oh, its already used by some other patch. sorry. */
return 0;
}
/* check if given address is already in cache */
static uint32_t cache_get_cached(uint32_t address, uint32_t type)
{
uint32_t cache_seg_index_word = (address & (CACHE_INDEX_ADDRMASK(type) | CACHE_WORD_ADDRMASK(type)));
uint32_t stored_tag_index = 0;
if(type == TYPE_ICACHE) {
asm volatile (
/* write index for address to write */
"MCR p15, 3, %1, c15, c0, 0\n"
/* get TAG at given index */
"MRC p15, 3, %0, c15, c1, 0\n"
: "=r"(stored_tag_index)
: "r"(cache_seg_index_word)
);
} else {
asm volatile (
/* write index for address to write */
"MCR p15, 3, %1, c15, c0, 0\n"
/* get TAG at given index */
"MRC p15, 3, %0, c15, c2, 0\n"
: "=r"(stored_tag_index)
: "r"(cache_seg_index_word)
);
}
/* now check if the TAG RAM content matches with what we expect and valid bit is set */
uint32_t tag_index_valid_mask = CACHE_TAG_ADDRMASK(type) | CACHE_INDEX_ADDRMASK(type) | 0x10;
uint32_t cache_tag_index = (address & tag_index_valid_mask) | 0x10;
if((stored_tag_index & tag_index_valid_mask) == cache_tag_index) {
return 1;
}
return 0;
}
static inline void clean_d_cache(void)
{
uint32_t segment = 0;
do {
uint32_t line = 0;
for( ; line != 0x400 ; line += 0x20 ) {
asm(
"MCR p15, 0, %0, c7, c14, 2"
:
: "r"( line | segment )
);
}
} while( segment += 0x40000000 );
}
static void icache_unlock(void)
{
uint32_t old_int = cli();
/* make sure all entries are set to invalid */
uint32_t index;
for(index = 0; index < (1U<<CACHE_INDEX_BITS(TYPE_ICACHE)); index++) {
asm volatile (
/* write index for address to write */
"MOV R0, %0, LSL #5\n"
"MCR p15, 3, R0, c15, c0, 0\n"
/* set TAG at given index */
"MCR p15, 3, R0, c15, c1, 0\n"
:
: "r"(index)
: "r0"
);
}
/* disable cache lockdown */
asm volatile (
"MOV R0, #0\n"
"MCR p15, 0, R0, c9, c0, 1\n"
:
:
: "r0"
);
/* and flush cache again to make sure its consistent */
asm volatile (
"MOV R0, #0\n"
"MCR p15, 0, R0, c7, c5, 0\n"
"MCR p15, 0, R0, c7, c10, 4\n"
:
:
: "r0"
);
sei(old_int);
}
static void dcache_unlock(void)
{
uint32_t old_int = cli();
/* first clean and flush dcache entries */
uint32_t segment, index;
for(segment = 0; segment < (1U<<CACHE_SEGMENT_BITS(TYPE_DCACHE)); segment++ ) {
for(index = 0; index < (1U<<CACHE_INDEX_BITS(TYPE_DCACHE)); index++) {
uint32_t seg_index = (segment << CACHE_SEGMENT_TAGOFFSET(TYPE_DCACHE)) | (index << CACHE_INDEX_TAGOFFSET(TYPE_DCACHE));
asm volatile (
"MCR p15, 0, %0, c7, c14, 2\n"
:
: "r"(seg_index)
);
}
}
/* make sure all entries are set to invalid */
for(index = 0; index < (1U<<CACHE_INDEX_BITS(TYPE_ICACHE)); index++) {
asm volatile (
/* write index for address to write */
"MOV R0, %0, LSL #5\n"
"MCR p15, 3, R0, c15, c0, 0\n"
/* set TAG at given index */
"MCR p15, 3, R0, c15, c2, 0\n"
:
: "r"(index)
: "r0"
);
}
/* disable cache lockdown */
asm volatile (
"MOV R0, #0\n"
"MCR p15, 0, R0, c9, c0, 0\n"
:
:
: "r0"
);
/* and flush cache again to make sure its consistent */
asm volatile (
"MOV R0, #0\n"
"MCR p15, 0, R0, c7, c6, 0\n"
"MCR p15, 0, R0, c7, c10, 4\n"
:
:
: "r0"
);
sei(old_int);
}
static inline void icache_lock(void)
{
uint32_t old_int = cli();
/* no need to clean entries, directly flush and lock cache */
asm volatile (
/* flush cache pages */
"MCR p15, 0, R0, c7, c5, 0\n"
/* enable cache lockdown for segment 0 (of 4) */
"MOV R0, #0x80000000\n"
"MCR p15, 0, R0, c9, c0, 1\n"
/* finalize lockdown */
"MOV R0, #1\n"
"MCR p15, 0, R0, c9, c0, 1\n"
:
:
: "r0"
);
/* make sure all entries are set to invalid */
uint32_t index;
for(index = 0; index < (1U<<CACHE_INDEX_BITS(TYPE_ICACHE)); index++) {
asm volatile (
/* write index for address to write */
"MOV R0, %0, LSL #5\n"
"MCR p15, 3, R0, c15, c0, 0\n"
/* set TAG at given index */
"MCR p15, 3, R0, c15, c1, 0\n"
:
: "r"(index)
: "r0"
);
}
sei(old_int);
}
static inline void dcache_lock(void)
{
uint32_t index;
uint32_t old_int = cli();
clean_d_cache();
/* then lockdown data cache */
asm volatile (
/* enable cache lockdown for segment 0 (of 4) */
"MOV R0, #0x80000000\n"
"MCR p15, 0, R0, c9, c0, 0\n"
/* finalize lockdown */
"MOV R0, #1\n"
"MCR p15, 0, R0, c9, c0, 0\n"
:
:
: "r0"
);
/* make sure all entries are set to invalid */
for(index = 0; index < (1U<<CACHE_INDEX_BITS(TYPE_DCACHE)); index++) {
asm volatile (
/* write index for address to write */
"MOV R0, %0, LSL #5\n"
"MCR p15, 3, R0, c15, c0, 0\n"
/* set TAG at given index */
"MCR p15, 3, R0, c15, c2, 0\n"
:
: "r"(index)
: "r0"
);
}
sei(old_int);
}
/* these are the "public" functions. please use only these if you are not sure what the others are for */
uint32_t cache_locked(void)
{
uint32_t status = 0;
asm volatile (
/* get lockdown status */
"MRC p15, 0, %0, c9, c0, 1\n"
: "=r"(status)
:
: "r0"
);
return status;
}
inline void cache_lock(void)
{
icache_lock();
dcache_lock();
}
void cache_unlock(void)
{
icache_unlock();
dcache_unlock();
}
uint32_t cache_fake(uint32_t address, uint32_t data, uint32_t type)
{
/* that word is already patched? return failure */
/* Disabled, as it always returns FALSE */
if(FALSE && !cache_is_patchable(address, type)) {
return 0;
}
/* is that line not in cache yet? */
if(!cache_get_cached(address, type)) {
/* no, then fetch it */
cache_fetch_line(address, type);
}
return cache_patch_single_word(address, data, type);
}
void flush_caches(void)
{
uint32_t reg = 0;
asm(
"MOV %0, #0\n"
"MCR p15, 0, %0, c7, c5, 0\n" // entire I cache
"MOV %0, #0\n"
"MCR p15, 0, %0, c7, c6, 0\n" // entire D cache
"MCR p15, 0, %0, c7, c10, 4\n" // drain write buffer
:
: "r"(reg)
);
}