/
TLB.cpp
345 lines (314 loc) · 11.4 KB
/
TLB.cpp
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
#include "stdafx.h"
#include "TLB.h"
#include <Project64-core/Debugger.h>
#include <Project64-core/N64System/Mips/MemoryVirtualMem.h>
#include <Project64-core/N64System/Mips/Register.h>
#include <Project64-core/N64System/Recompiler/Recompiler.h>
#include <Project64-core/N64System/SystemGlobals.h>
CTLB::CTLB(CMipsMemoryVM & MMU, CRegisters & Reg, CRecompiler *& Recomp) :
m_MMU(MMU),
m_Reg(Reg),
m_Recomp(Recomp)
{
WriteTrace(TraceTLB, TraceDebug, "Start");
memset(m_tlb, 0, sizeof(m_tlb));
memset(m_FastTlb, 0, sizeof(m_FastTlb));
Reset(true);
WriteTrace(TraceTLB, TraceDebug, "Done");
}
CTLB::~CTLB()
{
WriteTrace(TraceTLB, TraceDebug, "Start");
WriteTrace(TraceTLB, TraceDebug, "Done");
}
void CTLB::Reset(bool InvalidateTLB)
{
uint32_t count;
for (count = 0; count < 64; count++)
{
m_FastTlb[count].ValidEntry = false;
}
if (InvalidateTLB)
{
for (count = 0; count < 32; count++)
{
m_tlb[count].EntryDefined = false;
}
}
else
{
for (count = 0; count < 32; count++)
{
SetupTLB_Entry(count, false);
}
}
}
bool CTLB::AddressDefined(uint64_t VAddr)
{
if (VAddr >= 0x80000000 && VAddr <= 0xBFFFFFFF)
{
return true;
}
for (uint32_t i = 0; i < 64; i++)
{
if (m_FastTlb[i].ValidEntry &&
VAddr >= m_FastTlb[i].VSTART &&
VAddr <= m_FastTlb[i].VEND)
{
return true;
}
}
return false;
}
TLB_ENTRY & CTLB::TlbEntry(int32_t Entry)
{
return m_tlb[Entry];
}
void CTLB::Probe()
{
int Counter;
WriteTrace(TraceTLB, TraceDebug, "Start");
m_Reg.INDEX_REGISTER |= 0x80000000;
for (Counter = 0; Counter < 32; Counter++)
{
if (!m_tlb[Counter].EntryDefined)
{
continue;
}
uint64_t & TlbEntryHiValue = m_tlb[Counter].EntryHi.Value;
uint32_t Mask = (uint32_t)(~m_tlb[Counter].PageMask.Mask << 13);
uint32_t TlbValueMasked = TlbEntryHiValue & Mask;
uint32_t EntryHiMasked = m_Reg.ENTRYHI_REGISTER.Value & Mask;
if (TlbValueMasked == EntryHiMasked)
{
if ((TlbEntryHiValue & 0x100) != 0 || // Global
((TlbEntryHiValue & 0xFF) == (m_Reg.ENTRYHI_REGISTER.Value & 0xFF))) // SameAsid
{
m_Reg.INDEX_REGISTER = Counter;
int FastIndx = Counter << 1;
m_FastTlb[FastIndx].Probed = true;
m_FastTlb[FastIndx + 1].Probed = true;
return;
}
}
}
WriteTrace(TraceTLB, TraceDebug, "Done");
}
void CTLB::ReadEntry()
{
uint32_t Index = m_Reg.INDEX_REGISTER & 0x1F;
m_Reg.PAGE_MASK_REGISTER = m_tlb[Index].PageMask.Value;
m_Reg.ENTRYHI_REGISTER.Value = (m_tlb[Index].EntryHi.Value & ~m_tlb[Index].PageMask.Value);
m_Reg.ENTRYLO0_REGISTER = m_tlb[Index].EntryLo0.Value;
m_Reg.ENTRYLO1_REGISTER = m_tlb[Index].EntryLo1.Value;
}
void CTLB::WriteEntry(uint32_t Index, bool Random)
{
uint32_t FastIndx;
WriteTrace(TraceTLB, TraceDebug, "%02d %d %08X %08X %08X %08X ", Index, Random, m_Reg.PAGE_MASK_REGISTER, m_Reg.ENTRYHI_REGISTER, m_Reg.ENTRYLO0_REGISTER, m_Reg.ENTRYLO1_REGISTER);
// Check to see if entry is unmapping itself
if (m_tlb[Index].EntryDefined)
{
FastIndx = Index << 1;
if (*_PROGRAM_COUNTER >= m_FastTlb[FastIndx].VSTART &&
*_PROGRAM_COUNTER < m_FastTlb[FastIndx].VEND &&
m_FastTlb[FastIndx].ValidEntry && m_FastTlb[FastIndx].VALID)
{
WriteTrace(TraceTLB, TraceDebug, "Ignored PC: %X VAddr Start: %X VEND: %X", *_PROGRAM_COUNTER, m_FastTlb[FastIndx].VSTART, m_FastTlb[FastIndx].VEND);
return;
}
if (*_PROGRAM_COUNTER >= m_FastTlb[FastIndx + 1].VSTART &&
*_PROGRAM_COUNTER < m_FastTlb[FastIndx + 1].VEND &&
m_FastTlb[FastIndx + 1].ValidEntry && m_FastTlb[FastIndx + 1].VALID)
{
WriteTrace(TraceTLB, TraceDebug, "Ignored PC: %X VAddr Start: %X VEND: %X", *_PROGRAM_COUNTER, m_FastTlb[FastIndx + 1].VSTART, m_FastTlb[FastIndx + 1].VEND);
return;
}
}
// Reset old addresses
if (m_tlb[Index].EntryDefined)
{
for (FastIndx = Index << 1; FastIndx <= (Index << 1) + 1; FastIndx++)
{
if (!m_FastTlb[FastIndx].ValidEntry)
{
continue;
}
if (!m_FastTlb[FastIndx].VALID)
{
continue;
}
if (m_tlb[Index].PageMask.Value == m_Reg.PAGE_MASK_REGISTER &&
m_tlb[Index].EntryHi.Value == m_Reg.ENTRYHI_REGISTER.Value)
{
if (FastIndx == (Index << 1) && m_tlb[Index].EntryLo0.Value == m_Reg.ENTRYLO0_REGISTER)
{
continue;
}
if (FastIndx != (Index << 1) && m_tlb[Index].EntryLo1.Value == m_Reg.ENTRYLO1_REGISTER)
{
continue;
}
}
TLB_Unmaped(m_FastTlb[FastIndx].VSTART, m_FastTlb[FastIndx].Length);
}
}
// Fill in m_tlb entry
m_tlb[Index].PageMask.Value = (uint32_t)m_Reg.PAGE_MASK_REGISTER;
m_tlb[Index].EntryHi.Value = (uint32_t)m_Reg.ENTRYHI_REGISTER.Value;
m_tlb[Index].EntryLo0.Value = (uint32_t)m_Reg.ENTRYLO0_REGISTER;
m_tlb[Index].EntryLo1.Value = (uint32_t)m_Reg.ENTRYLO1_REGISTER;
m_tlb[Index].EntryDefined = true;
SetupTLB_Entry(Index, Random);
if (g_Debugger != nullptr)
{
g_Debugger->TLBChanged();
}
}
void CTLB::SetupTLB_Entry(uint32_t Index, bool Random)
{
// Fix up fast TLB entries
if (!m_tlb[Index].EntryDefined)
{
return;
}
uint32_t FastIndx = Index << 1;
if (m_FastTlb[FastIndx].VALID)
{
TLB_Unmaped(m_FastTlb[FastIndx].VSTART, m_FastTlb[FastIndx].Length);
}
m_FastTlb[FastIndx].Length = (uint32_t)((m_tlb[Index].PageMask.Mask << 12) + 0xFFF);
m_FastTlb[FastIndx].VSTART = (uint32_t)(m_tlb[Index].EntryHi.VPN2 << 13);
m_FastTlb[FastIndx].VEND = m_FastTlb[FastIndx].VSTART + m_FastTlb[FastIndx].Length;
m_FastTlb[FastIndx].PHYSSTART = (uint32_t)(m_tlb[Index].EntryLo0.PFN << 12);
m_FastTlb[FastIndx].PHYSEND = m_FastTlb[FastIndx].PHYSSTART + m_FastTlb[FastIndx].Length;
m_FastTlb[FastIndx].VALID = m_tlb[Index].EntryLo0.V;
m_FastTlb[FastIndx].DIRTY = m_tlb[Index].EntryLo0.D;
m_FastTlb[FastIndx].GLOBAL = m_tlb[Index].EntryLo0.GLOBAL & m_tlb[Index].EntryLo1.GLOBAL;
m_FastTlb[FastIndx].ValidEntry = false;
m_FastTlb[FastIndx].Random = Random;
m_FastTlb[FastIndx].Probed = false;
FastIndx = (Index << 1) + 1;
if (m_FastTlb[FastIndx].VALID)
{
TLB_Unmaped(m_FastTlb[FastIndx].VSTART, m_FastTlb[FastIndx].Length);
}
m_FastTlb[FastIndx].Length = (uint32_t)((m_tlb[Index].PageMask.Mask << 12) + 0xFFF);
m_FastTlb[FastIndx].VSTART = (uint32_t)((m_tlb[Index].EntryHi.VPN2 << 13) + (m_FastTlb[FastIndx].Length + 1));
m_FastTlb[FastIndx].VEND = m_FastTlb[FastIndx].VSTART + m_FastTlb[FastIndx].Length;
m_FastTlb[FastIndx].PHYSSTART = (uint32_t)(m_tlb[Index].EntryLo1.PFN << 12);
m_FastTlb[FastIndx].PHYSEND = m_FastTlb[FastIndx].PHYSSTART + m_FastTlb[FastIndx].Length;
m_FastTlb[FastIndx].VALID = m_tlb[Index].EntryLo1.V;
m_FastTlb[FastIndx].DIRTY = m_tlb[Index].EntryLo1.D;
m_FastTlb[FastIndx].GLOBAL = m_tlb[Index].EntryLo0.GLOBAL & m_tlb[Index].EntryLo1.GLOBAL;
m_FastTlb[FastIndx].ValidEntry = false;
m_FastTlb[FastIndx].Random = Random;
m_FastTlb[FastIndx].Probed = false;
// Test both entries to see if they are valid
for (FastIndx = Index << 1; FastIndx <= (Index << 1) + 1; FastIndx++)
{
if (!m_FastTlb[FastIndx].VALID)
{
m_FastTlb[FastIndx].ValidEntry = true;
continue;
}
if (m_FastTlb[FastIndx].VEND <= m_FastTlb[FastIndx].VSTART)
{
continue;
}
if (m_FastTlb[FastIndx].VSTART >= 0x80000000 && m_FastTlb[FastIndx].VEND <= 0xBFFFFFFF)
{
continue;
}
if (m_FastTlb[FastIndx].PHYSEND > 0x1FFFFFFF)
{
continue;
}
// Map the new m_tlb entry for reading and writing
m_FastTlb[FastIndx].ValidEntry = true;
m_MMU.TLB_Mapped(m_FastTlb[FastIndx].VSTART, m_FastTlb[FastIndx].Length, m_FastTlb[FastIndx].PHYSSTART, !m_FastTlb[FastIndx].DIRTY);
}
}
void CTLB::TLB_Unmaped(uint32_t VAddr, uint32_t Len)
{
m_MMU.TLB_Unmaped(VAddr, Len);
if (m_Recomp && bSMM_TLB())
{
m_Recomp->ClearRecompCode_Virt(VAddr, Len, CRecompiler::Remove_TLB);
}
}
bool CTLB::PAddrToVAddr(uint32_t PAddr, uint32_t & VAddr, uint32_t & Index)
{
for (int i = Index; i < 64; i++)
{
if (m_FastTlb[i].ValidEntry == false)
{
continue;
}
if (PAddr >= m_FastTlb[i].PHYSSTART && PAddr < m_FastTlb[i].PHYSEND)
{
VAddr = m_FastTlb[i].VSTART + (PAddr - m_FastTlb[i].PHYSSTART);
Index = i + 1;
return true;
}
}
return false;
}
void CTLB::RecordDifference(CLog & LogFile, const CTLB & rTLB)
{
for (int i = 0, n = sizeof(m_tlb) / sizeof(m_tlb[0]); i < n; i++)
{
if (m_tlb[i].EntryDefined != rTLB.m_tlb[i].EntryDefined)
{
LogFile.LogF("TLB[%d] Defined: %s %s\r\n", i, m_tlb[i].EntryDefined ? "Yes" : "No", rTLB.m_tlb[i].EntryDefined ? "Yes" : "No");
continue;
}
if (!m_tlb[i].EntryDefined)
{
continue;
}
if (m_tlb[i].PageMask.Value != rTLB.m_tlb[i].PageMask.Value)
{
LogFile.LogF("TLB[%d] PageMask: %X %X\r\n", i, m_tlb[i].PageMask.Value, rTLB.m_tlb[i].PageMask.Value);
}
if (m_tlb[i].EntryHi.Value != rTLB.m_tlb[i].EntryHi.Value)
{
LogFile.LogF("TLB[%d] EntryHi: %X %X\r\n", i, m_tlb[i].EntryHi.Value, rTLB.m_tlb[i].EntryHi.Value);
}
if (m_tlb[i].EntryLo0.Value != rTLB.m_tlb[i].EntryLo0.Value)
{
LogFile.LogF("TLB[%d] EntryLo0: %X %X\r\n", i, m_tlb[i].EntryLo0.Value, rTLB.m_tlb[i].EntryLo0.Value);
}
if (m_tlb[i].EntryLo1.Value != rTLB.m_tlb[i].EntryLo1.Value)
{
LogFile.LogF("TLB[%d] EntryLo1: %X %X\r\n", i, m_tlb[i].EntryLo1.Value, rTLB.m_tlb[i].EntryLo1.Value);
}
}
}
bool CTLB::operator==(const CTLB & rTLB) const
{
const size_t n = sizeof(m_tlb) / sizeof(m_tlb[0]);
for (size_t i = 0; i < n; i++)
{
if (m_tlb[i].EntryDefined != rTLB.m_tlb[i].EntryDefined)
{
return false;
}
if (!m_tlb[i].EntryDefined)
{
continue;
}
if (m_tlb[i].PageMask.Value != rTLB.m_tlb[i].PageMask.Value ||
m_tlb[i].EntryHi.Value != rTLB.m_tlb[i].EntryHi.Value ||
m_tlb[i].EntryLo0.Value != rTLB.m_tlb[i].EntryLo0.Value ||
m_tlb[i].EntryLo1.Value != rTLB.m_tlb[i].EntryLo1.Value)
{
return false;
}
}
return true;
}
bool CTLB::operator!=(const CTLB & rTLB) const
{
return !(*this == rTLB);
}