-
Notifications
You must be signed in to change notification settings - Fork 8
/
cp15.c
executable file
·378 lines (313 loc) · 8.54 KB
/
cp15.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
//(c) uARM project https://github.com/uARM-Palm/uARM [email protected]
#include <string.h>
#include <stdlib.h>
#include "util.h"
#include "cp15.h"
struct ArmCP15 {
struct ArmCpu* cpu;
struct ArmMmu* mmu;
struct icache *ic;
uint32_t control;
uint32_t ttb;
uint32_t FSR; //fault sttaus register
uint32_t FAR; //fault address register
union {
struct {
uint32_t CPAR; //coprocessor access register
uint32_t ACP; //auxilary control reg for xscale
}; //xscale
struct {
uint8_t cfg, iMin, iMax;
uint16_t tid;
}; //omap
};
uint32_t mmuSwitchCy;
uint32_t cpuid;
uint32_t cacheId;
bool xscale, omap;
};
void cp15Cycle(struct ArmCP15* cp15) //mmu on/off lags by a cycle
{
if (cp15->mmuSwitchCy) {
if (!--cp15->mmuSwitchCy)
mmuSetTTP(cp15->mmu, (cp15->control & 0x00000001UL) ? cp15->ttb : MMU_DISABLED_TTP);
}
}
static bool cp15prvCoprocRegXferFunc(struct ArmCpu* cpu, void* userData, bool two, bool read, uint8_t op1, uint8_t Rx, uint8_t CRn, uint8_t CRm, uint8_t op2)
{
struct ArmCP15 *cp15 = (struct ArmCP15*)userData;
uint32_t val = 0, tmp;
if (!read)
val = cpuGetRegExternal(cpu, Rx);
if (op1 != 0 || two)
goto fail; //CP15 only accessed with MCR/MRC with op1 == 0
switch (CRn) {
case 0: //ID codes
if (!read)
goto fail; //cannot write to ID codes register
if (CRm != 0)
goto fail; //CRm must be zero for this read
if (op2 == 0) { //main ID register
val = cp15->cpuid;
goto success;
}
else if (op2 == 1) { //cache type register - we lie here
val = cp15->cacheId;
goto success;
}
break;
case 1: //control register
if (op2 == 0 && CRm == 0) {
if (read)
val = cp15->control;
else {
uint32_t origVal = val;
//some bits ignore writes. pretend they were wirtten as proper
val |= 0x0070;
val &=~ 0x0080;
tmp = val ^ cp15->control; //see what changed and mask off then chack for what we support changing of
if (tmp & 0x84F0UL) {
fprintf(stderr, "cp15: unknown bits changed (0x%08lx) 0x%08lx -> 0x%08lx, halting\n", (unsigned long)(tmp & 0x84F0UL), (unsigned long)cp15->control, (unsigned long)origVal);
while (true);
}
if (tmp & 0x00002000UL) { // V bit
cpuSetVectorAddr(cp15->cpu, (val & 0x00002000UL) ? 0xFFFF0000UL : 0x00000000UL);
cp15->control ^= 0x00002000UL;
}
if (tmp & 0x00000200UL) { // R bit
mmuSetR(cp15->mmu, (val & 0x00000200UL) != 0);
cp15->control ^= 0x00000200UL;
}
if (tmp & 0x00000100UL) { // S bit
mmuSetS(cp15->mmu, (val & 0x00000100UL) != 0);
cp15->control ^= 0x00000100UL;
}
if (tmp & 0x00000001UL) { // M bit
cp15->mmuSwitchCy = 2;
cp15->control ^= 0x00000001UL;
}
}
}
else if (CRm == 1) {
fprintf(stderr, "sony cr1 bug?\n");
if (read)
val = 0;
}
else if (cp15->xscale && op2 == 1) { //PXA-specific thing
if (read)
val = cp15->ACP;
else
cp15->ACP = val;
}
else
break;
goto success;
case 2: //translation tabler base
if (read)
val = cp15->ttb;
else {
if (cp15->control & 0x00000001UL) { //mmu is on
mmuSetTTP(cp15->mmu, val);
}
cp15->ttb = val;
}
goto success;
case 3: //domain access control
if (read)
val = mmuGetDomainCfg(cp15->mmu);
else {
mmuSetDomainCfg(cp15->mmu, val);
//dcacheFlushPermInfo(cp15->dc);
}
goto success;
case 5: //FSR
if (read)
val = cp15->FSR;
else
cp15->FSR = val;
goto success;
case 6: //FAR
if (read)
val = cp15->FAR;
else
cp15->FAR = val;
goto success;
case 7: //cache ops
if ((CRm == 5 || CRm == 7) && op2 == 0) {
icacheInval(cp15->ic); //invalidate entire {icache(5) or both i and dcache(7)}
if (CRm == 7) {
//dcacheInval(cp15->dc);
}
}
else if ((CRm == 5 || CRm == 7) && op2 == 1) {
icacheInvalAddr(cp15->ic, val); //invalidate {icache(5) or both i and dcache(7)} line, given VA
if (CRm == 7) {
//dcacheInvalAddr(cp15->dc, val);
}
}
else if ((CRm == 5 || CRm == 7) && op2 == 2) {
icacheInval(cp15->ic); //invalidate {icache(5) or both i and dcache(7)} line, given set/index. i dont know how to do this, so flush the whole thing
if (CRm == 7) {
//dcacheInvalSetWayRaw(cp15->dc, val);
}
}
else if (CRm == 10 && op2 == 4)
{/* drain write buffer = nothing */}
else if (CRm == 10 && op2 == 1) {
//dcacheCleanSetWayRaw(cp15->dc, val);
}
else if (CRm == 6 && op2 == 0) {
//dcacheInval(cp15->dc);
}
else if (CRm == 6 && op2 == 1) {
//dcacheInvalAddr(cp15->dc, val);
}
else if (CRm == 6 && op2 == 2) {
//dcacheInvalSetWayRaw(cp15->dc, val);
}
else if (CRm == 2 && op2 == 5) {
//dcacheAllocAddr(cp15->dc, val);
}
else if (CRm == 5 && op2 == 6)
{/* flush btb = nothing */}
else if (CRm == 0 && op2 == 4)
{/* idle = nothing */ }
else if (CRm == 14 && op2 == 2)
{/* clean and inval d-cache line */}
else if (CRm == 10 && op2 == 0)
{/* clean entire d-cache - omap can do this */}
else if (CRm == 10 && op2 == 2)
{/* clean d-cache line */}
else
break;
goto success;
case 8: //TLB ops
mmuTlbFlush(cp15->mmu);
//dcacheFlushPermInfo(cp15->dc);
goto success;
case 9: //cache lockdown
if (CRm == 1 && op2 == 0)
fprintf(stderr, "Attempt to lock 0x%08lx+32 in icache\r\n", (unsigned long)val);
else if (CRm == 2 && op2 == 0)
fprintf(stderr, "Dcache now %s lock mode\r\n", val ? "in" : "out of");
else
break;
goto success;
case 10: //TLB lockdown
if (!read && CRm == 0 && (op2 == 0 || op2 == 1)) {
goto success;
}
break;
case 13: //FCSE
if (read)
val = cpuGetPid(cp15->cpu);
else
cpuSetPid(cp15->cpu, val & 0xfe000000ul);
goto success;
case 14: //xscale debug
if (cp15->xscale) {
if (CRm == 8 && op2 == 0) { //ICBR0
val = 0;
goto success;
}
if (CRm == 9 && op2 == 0) { //ICBR1
val = 0;
goto success;
}
if (CRm == 0 && op2 == 0) { //DBR0
val = 0;
goto success;
}
if (CRm == 3 && op2 == 0) { //DBR1
val = 0;
goto success;
}
if (CRm == 4 && op2 == 0) { //DBCON
val = 0;
goto success;
}
}
break;
case 15:
if (cp15->xscale && op2 == 0 && CRm == 1){ //CPAR on xscale
if (read)
val = cpuGetCPAR(cp15->cpu);
else
cpuSetCPAR(cp15->cpu, val & 0x3FFF);
goto success;
}
else if (cp15->omap) { //omap shit
if (CRm == 1 && op2 == 0) {
if (read)
val = cp15->cfg;
else
cp15->cfg = val & 0x87;
goto success;
}
else if (CRm == 2 && op2 == 0) {
if (read)
val = cp15->iMax;
else
cp15->iMax = val;
goto success;
}
else if (CRm == 3 && op2 == 0) {
if (read)
val = cp15->iMin;
else
cp15->iMin = val;
goto success;
}
else if (CRm == 4 && op2 == 0) {
if (read)
val = cp15->tid;
else
cp15->tid = val;
goto success;
}
else if (CRm == 8 && op2 == 0 && read) {
val = 0;
goto success;
}
else if (CRm == 8 && op2 == 2 && !read) {
//WFI
goto success;
}
}
break;
}
fail:
return false;
success:
if(read)
cpuSetReg(cpu, Rx, val);
return true;
}
struct ArmCP15* cp15Init(struct ArmCpu* cpu, struct ArmMmu* mmu, struct icache *ic, uint32_t cpuid, uint32_t cacheId, bool xscale, bool omap)
{
struct ArmCP15 *cp15 = (struct ArmCP15*)malloc(sizeof(*cp15));
struct ArmCoprocessor cp = {
.regXfer = cp15prvCoprocRegXferFunc,
.userData = cp15,
};
if (!cp15)
ERR("cannot alloc CP15");
memset(cp15, 0, sizeof (*cp15));
cp15->cpu = cpu;
cp15->mmu = mmu;
cp15->ic = ic;
cp15->control = 0x00004072UL;
cp15->cpuid = cpuid;
cp15->cacheId = cacheId;
cp15->xscale = xscale;
cp15->omap = omap;
if (omap)
cp15->iMax = 0xff;
cpuCoprocessorRegister(cpu, 15, &cp);
return cp15;
}
void cp15SetFaultStatus(struct ArmCP15* cp15, uint32_t addr, uint_fast8_t faultStatus)
{
cp15->FAR = addr;
cp15->FSR = faultStatus;
}